[PATCH] efi_loader: Add tests for efi_dp_find_obj()

Ilias Apalodimas ilias.apalodimas at linaro.org
Tue Dec 9 11:49:08 CET 2025


We recently changed efi_dp_find_obj() and extended it's matching patterns.
The function used to try and match device paths that are only an exact
match or shorter than the provided one. It can now match device paths
that are longer, but we have no selftests for it.

Start by adding basic testing.  Install a device path on a dummy handle
that contains
- Vendor GUID
- NVME
- partition
- file

and try to find an exact match, a match if longer matches are not
allowed and a longer match if longer matches are allowed.

Signed-off-by: Ilias Apalodimas <ilias.apalodimas at linaro.org>
---

Heinrich, I understand that is not covering all use cases, but it's a start
Andrew (cc'ed) will add
- A second longer device path to make sure we match the longest one
- Multiple ESPs and partitions to test device_has_esp() as described
  in https://lore.kernel.org/u-boot/20251208151503.1681988-1-ilias.apalodimas@linaro.org/

 test/lib/efi_device_path.c | 106 +++++++++++++++++++++++++++++++++++++
 1 file changed, 106 insertions(+)

diff --git a/test/lib/efi_device_path.c b/test/lib/efi_device_path.c
index 5a358ddcb934..483546b2224c 100644
--- a/test/lib/efi_device_path.c
+++ b/test/lib/efi_device_path.c
@@ -10,6 +10,66 @@
 #include <test/lib.h>
 #include <test/test.h>
 #include <test/ut.h>
+#include <mapmem.h>
+
+#define FILE_NAME_SIZE 16
+#define GUID_VENDOR \
+	EFI_GUID(0xdbca4c99, 0x6db1, 0x694d, \
+		 0x08, 0x75, 0x82, 0x9b, 0x64, 0xf1, 0xbc, 0xd1)
+
+static struct {
+	struct efi_device_path_vendor vendor;
+	struct efi_device_path_nvme nvme;
+	struct efi_device_path_hard_drive_path part;
+	struct efi_device_path_file_path file;
+	u16 file_name[FILE_NAME_SIZE];
+	struct efi_device_path end;
+} dp1 = {
+	{
+		{
+			DEVICE_PATH_TYPE_HARDWARE_DEVICE,
+			DEVICE_PATH_SUB_TYPE_VENDOR,
+			sizeof(struct efi_device_path_vendor),
+		},
+		GUID_VENDOR,
+	},
+	{
+		{
+			DEVICE_PATH_TYPE_MESSAGING_DEVICE,
+			DEVICE_PATH_SUB_TYPE_MSG_NVME,
+			sizeof(struct efi_device_path_nvme),
+		},
+		1,
+		{0, 1, 2, 3, 4, 5, 6, 7},
+	},
+	{
+		{
+			DEVICE_PATH_TYPE_MEDIA_DEVICE,
+			DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH,
+			sizeof(struct efi_device_path_hard_drive_path),
+		},
+		5,
+		0,
+		1024,
+		{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
+		2,
+		2,
+	},
+	{
+		{
+			DEVICE_PATH_TYPE_MEDIA_DEVICE,
+			DEVICE_PATH_SUB_TYPE_FILE_PATH,
+			sizeof(struct efi_device_path_file_path) +
+			       FILE_NAME_SIZE * sizeof(u16),
+		},
+	},
+	u"\\test_image.efi",
+	{
+		DEVICE_PATH_TYPE_END,
+		DEVICE_PATH_SUB_TYPE_END,
+		sizeof(struct efi_device_path),
+	},
+};

 static int lib_test_efi_dp_check_length(struct unit_test_state *uts)
 {
@@ -47,3 +107,49 @@ static int lib_test_efi_dp_check_length(struct unit_test_state *uts)
 	return 0;
 }
 LIB_TEST(lib_test_efi_dp_check_length, 0);
+
+static int lib_test_efi_obj_find(struct unit_test_state *uts)
+{
+	static efi_handle_t dummy_handle, handle;
+	struct efi_device_path *device, *image;
+	efi_status_t ret;
+
+	ret = efi_install_multiple_protocol_interfaces(&dummy_handle,
+						       &efi_guid_device_path,
+						       &dp1,
+						       NULL);
+	ut_assertok(ret);
+
+	ret = efi_dp_split_file_path((struct efi_device_path *)&dp1, &device,
+				     &image);
+	ut_assertok(ret);
+
+	/* find handle by exact match */
+	handle = efi_dp_find_obj((struct efi_device_path *)&dp1, NULL, NULL,
+				 false);
+	ut_asserteq_addr(handle, dummy_handle);
+
+	/*
+	 * Try to find handle by partial match if the dp is smaller while not
+	 * allowing matching for longer device paths discovered
+	 */
+	handle = efi_dp_find_obj(device, NULL, NULL, false);
+	ut_assertnull(handle);
+
+	/* find handle by partial match if the dp is smaller */
+	handle = efi_dp_find_obj(device, NULL, NULL, true);
+	ut_asserteq_addr(handle, dummy_handle);
+
+	ret = efi_uninstall_multiple_protocol_interfaces(dummy_handle,
+							 &efi_guid_device_path,
+							 &dp1,
+							 NULL);
+	ut_assertok(ret);
+
+	efi_free_pool(device);
+	efi_free_pool(image);
+
+	return 0;
+}
+
+LIB_TEST(lib_test_efi_obj_find, 0);
--
2.43.0



More information about the U-Boot mailing list