All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v3 06/18] efi_selftest: test EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
Date: Sun, 26 Nov 2017 14:05:11 +0100	[thread overview]
Message-ID: <20171126130523.4993-7-xypron.glpk@gmx.de> (raw)
In-Reply-To: <20171126130523.4993-1-xypron.glpk@gmx.de>

Provide a test for the EFI_DEVICE_PATH_TO_TEXT_PROTOCOL protocol.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
v3
	Print all installed device paths.
v2
	No change
---
 lib/efi_selftest/Makefile                  |   3 +
 lib/efi_selftest/efi_selftest_devicepath.c | 391 +++++++++++++++++++++++++++++
 2 files changed, 394 insertions(+)
 create mode 100644 lib/efi_selftest/efi_selftest_devicepath.c

diff --git a/lib/efi_selftest/Makefile b/lib/efi_selftest/Makefile
index 1851c17db6..d280eca5c3 100644
--- a/lib/efi_selftest/Makefile
+++ b/lib/efi_selftest/Makefile
@@ -11,6 +11,8 @@ CFLAGS_efi_selftest.o := $(CFLAGS_EFI)
 CFLAGS_REMOVE_efi_selftest.o := $(CFLAGS_NON_EFI)
 CFLAGS_efi_selftest_console.o := $(CFLAGS_EFI)
 CFLAGS_REMOVE_efi_selftest_console.o := $(CFLAGS_NON_EFI)
+CFLAGS_efi_selftest_devicepathe.o := $(CFLAGS_EFI)
+CFLAGS_REMOVE_efi_selftest_devicepath.o := $(CFLAGS_NON_EFI)
 CFLAGS_efi_selftest_events.o := $(CFLAGS_EFI)
 CFLAGS_REMOVE_efi_selftest_events.o := $(CFLAGS_NON_EFI)
 CFLAGS_efi_selftest_exitbootservices.o := $(CFLAGS_EFI)
@@ -33,6 +35,7 @@ CFLAGS_REMOVE_efi_selftest_watchdog.o := $(CFLAGS_NON_EFI)
 obj-$(CONFIG_CMD_BOOTEFI_SELFTEST) += \
 efi_selftest.o \
 efi_selftest_console.o \
+efi_selftest_devicepath.o \
 efi_selftest_events.o \
 efi_selftest_exitbootservices.o \
 efi_selftest_gop.o \
diff --git a/lib/efi_selftest/efi_selftest_devicepath.c b/lib/efi_selftest/efi_selftest_devicepath.c
new file mode 100644
index 0000000000..55f4632b14
--- /dev/null
+++ b/lib/efi_selftest/efi_selftest_devicepath.c
@@ -0,0 +1,390 @@
+/*
+ * efi_selftest_devicepath
+ *
+ * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
+ *
+ * SPDX-License-Identifier:     GPL-2.0+
+ *
+ * This unit test checks the following protocol services:
+ * DevicePathToText
+ */
+
+#include <efi_selftest.h>
+
+static struct efi_boot_services *boottime;
+
+static efi_handle_t handle1;
+static efi_handle_t handle2;
+static efi_handle_t handle3;
+
+struct interface {
+	void (EFIAPI * inc)(void);
+} interface;
+
+static efi_guid_t guid_device_path = DEVICE_PATH_GUID;
+
+static efi_guid_t guid_device_path_to_text_protocol =
+	EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
+
+static efi_guid_t guid_protocol =
+	EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
+		 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0x7d);
+
+static efi_guid_t guid_vendor1 =
+	EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
+		 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xb1);
+
+static efi_guid_t guid_vendor2 =
+	EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
+		 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xa2);
+
+static efi_guid_t guid_vendor3 =
+	EFI_GUID(0xdbca4c98, 0x6cb0, 0x694d,
+		 0x08, 0x72, 0x81, 0x9c, 0x65, 0x0c, 0xbb, 0xc3);
+
+static u8 *dp1;
+static u8 *dp2;
+static u8 *dp3;
+
+struct efi_device_path_to_text_protocol *device_path_to_text;
+
+/*
+ * Setup unit test.
+ *
+ * Create three handles. Install a new protocol on two of them and
+ * provice device paths.
+ *
+ * handle1
+ *   guid interface
+ * handle2
+ *   guid interface
+ * handle3
+ *
+ * @handle:	handle of the loaded image
+ * @systable:	system table
+ */
+static int setup(const efi_handle_t img_handle,
+		 const struct efi_system_table *systable)
+{
+	struct efi_device_path_vendor vendor_node;
+	struct efi_device_path end_node;
+	efi_status_t ret;
+
+	boottime = systable->boottime;
+
+	ret = boottime->locate_protocol(&guid_device_path_to_text_protocol,
+					NULL, (void **)&device_path_to_text);
+	if (ret != EFI_SUCCESS) {
+		device_path_to_text = NULL;
+		efi_st_error(
+			"Device path to text protocol is not available.\n");
+		return EFI_ST_FAILURE;
+	}
+
+	ret = boottime->allocate_pool(EFI_LOADER_DATA,
+				      sizeof(struct efi_device_path_vendor) +
+				      sizeof(struct efi_device_path),
+				      (void **)&dp1);
+	if (ret != EFI_SUCCESS)
+		goto out_of_memory;
+
+	ret = boottime->allocate_pool(EFI_LOADER_DATA, 2 *
+				      sizeof(struct efi_device_path_vendor) +
+				      sizeof(struct efi_device_path),
+				      (void **)&dp2);
+	if (ret != EFI_SUCCESS)
+		goto out_of_memory;
+
+	ret = boottime->allocate_pool(EFI_LOADER_DATA, 3 *
+				      sizeof(struct efi_device_path_vendor) +
+				      sizeof(struct efi_device_path),
+				      (void **)&dp3);
+	if (ret != EFI_SUCCESS)
+		goto out_of_memory;
+
+	vendor_node.dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
+	vendor_node.dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
+	vendor_node.dp.length = sizeof(struct efi_device_path_vendor);
+
+	boottime->copy_mem(&vendor_node.guid, &guid_vendor1,
+			   sizeof(efi_guid_t));
+	boottime->copy_mem(dp1, &vendor_node,
+			   sizeof(struct efi_device_path_vendor));
+	boottime->copy_mem(dp2, &vendor_node,
+			   sizeof(struct efi_device_path_vendor));
+	boottime->copy_mem(dp3, &vendor_node,
+			   sizeof(struct efi_device_path_vendor));
+
+	boottime->copy_mem(&vendor_node.guid, &guid_vendor2,
+			   sizeof(efi_guid_t));
+	boottime->copy_mem(dp2 + sizeof(struct efi_device_path_vendor),
+			   &vendor_node, sizeof(struct efi_device_path_vendor));
+	boottime->copy_mem(dp3 + sizeof(struct efi_device_path_vendor),
+			   &vendor_node, sizeof(struct efi_device_path_vendor));
+
+	boottime->copy_mem(&vendor_node.guid, &guid_vendor3,
+			   sizeof(efi_guid_t));
+	boottime->copy_mem(dp3 + 2 * sizeof(struct efi_device_path_vendor),
+			   &vendor_node, sizeof(struct efi_device_path_vendor));
+
+	end_node.type = DEVICE_PATH_TYPE_END;
+	end_node.sub_type = DEVICE_PATH_SUB_TYPE_END;
+	end_node.length = sizeof(struct efi_device_path);
+	boottime->copy_mem(dp1 + sizeof(struct efi_device_path_vendor),
+			   &end_node, sizeof(struct efi_device_path));
+	boottime->copy_mem(dp2 + 2 * sizeof(struct efi_device_path_vendor),
+			   &end_node, sizeof(struct efi_device_path));
+	boottime->copy_mem(dp3 + 3 * sizeof(struct efi_device_path_vendor),
+			   &end_node, sizeof(struct efi_device_path));
+
+	ret = boottime->install_protocol_interface(&handle1,
+						   &guid_device_path,
+						   EFI_NATIVE_INTERFACE,
+						   dp1);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("InstallProtocolInterface failed\n");
+		return EFI_ST_FAILURE;
+	}
+	ret = boottime->install_protocol_interface(&handle1,
+						   &guid_protocol,
+						   EFI_NATIVE_INTERFACE,
+						   &interface);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("InstallProtocolInterface failed\n");
+		return EFI_ST_FAILURE;
+	}
+	ret = boottime->install_protocol_interface(&handle2,
+						   &guid_device_path,
+						   EFI_NATIVE_INTERFACE,
+						   dp2);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("InstallProtocolInterface failed\n");
+		return EFI_ST_FAILURE;
+	}
+	ret = boottime->install_protocol_interface(&handle2,
+						   &guid_protocol,
+						   EFI_NATIVE_INTERFACE,
+						   &interface);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("InstallProtocolInterface failed\n");
+		return EFI_ST_FAILURE;
+	}
+	ret = boottime->install_protocol_interface(&handle3,
+						   &guid_device_path,
+						   EFI_NATIVE_INTERFACE,
+						   dp3);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("InstallProtocolInterface failed\n");
+		return EFI_ST_FAILURE;
+	}
+	return EFI_ST_SUCCESS;
+
+out_of_memory:
+	efi_st_error("Out of memory\n");
+	return EFI_ST_FAILURE;
+}
+
+/*
+ * Tear down unit test.
+ *
+ */
+static int teardown(void)
+{
+	efi_status_t ret;
+
+	ret = boottime->uninstall_protocol_interface(&handle1,
+						     &guid_device_path,
+						     dp1);
+	if (ret != EFI_SUCCESS)
+		efi_st_todo("UninstallProtocolInterface failed\n");
+	ret = boottime->uninstall_protocol_interface(&handle1,
+						     &guid_protocol,
+						     &interface);
+	if (ret != EFI_SUCCESS)
+		efi_st_todo("UninstallProtocolInterface failed\n");
+	ret = boottime->uninstall_protocol_interface(&handle2,
+						     &guid_device_path,
+						     dp2);
+	if (ret != EFI_SUCCESS)
+		efi_st_todo("UninstallProtocolInterface failed\n");
+	ret = boottime->uninstall_protocol_interface(&handle2,
+						     &guid_protocol,
+						     &interface);
+	if (ret != EFI_SUCCESS)
+		efi_st_todo("UninstallProtocolInterface failed\n");
+	ret = boottime->uninstall_protocol_interface(&handle3,
+						     &guid_device_path,
+						     dp3);
+	if (ret != EFI_SUCCESS)
+		efi_st_todo("UninstallProtocolInterface failed\n");
+	if (dp1) {
+		ret = boottime->free_pool(dp1);
+		if (ret != EFI_SUCCESS) {
+			efi_st_error("FreePool failed\n");
+			return EFI_ST_FAILURE;
+		}
+	}
+	if (dp2) {
+		ret = boottime->free_pool(dp2);
+		if (ret != EFI_SUCCESS) {
+			efi_st_error("FreePool failed\n");
+			return EFI_ST_FAILURE;
+		}
+	}
+	if (dp3) {
+		ret = boottime->free_pool(dp3);
+		if (ret != EFI_SUCCESS) {
+			efi_st_error("FreePool failed\n");
+			return EFI_ST_FAILURE;
+		}
+	}
+	return EFI_ST_SUCCESS;
+}
+
+/*
+ * Execute unit test.
+ *
+ */
+static int execute(void)
+{
+	struct efi_device_path *remaining_dp;
+	void *handle;
+	/*
+	 * This device path node ends with the letter 't' of 'u-boot'.
+	 * The following '.bin' does not belong to the node but is
+	 * helps to test the correct truncation.
+	 */
+	struct {
+		struct efi_device_path dp;
+		u16 text[12];
+	} __packed dp_node = {
+			{ DEVICE_PATH_TYPE_MEDIA_DEVICE,
+			  DEVICE_PATH_SUB_TYPE_FILE_PATH,
+			  sizeof(struct efi_device_path) + 12},
+			L"u-boot.bin",
+		};
+	u16 *string;
+	efi_status_t ret;
+	efi_uintn_t i, no_handles;
+	efi_handle_t *handles;
+	struct efi_device_path *dp;
+
+	/* Display all available device paths */
+	ret = boottime->locate_handle_buffer(BY_PROTOCOL,
+					     &guid_device_path,
+					     NULL, &no_handles, &handles);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("Cannot retrieve device path protocols.\n");
+		return EFI_ST_FAILURE;
+	}
+
+	efi_st_printf("Installed device path protocols:\n");
+	for (i = 0; i < no_handles; ++i) {
+		ret = boottime->open_protocol(handles[i], &guid_device_path,
+					      (void **)&dp, NULL, NULL,
+					      EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+		if (ret != EFI_SUCCESS) {
+			efi_st_error("Cannot open device path protocol.\n");
+			return EFI_ST_FAILURE;
+		}
+		string = device_path_to_text->convert_device_path_to_text(
+					dp, true, false);
+		if (!string) {
+			efi_st_error("ConvertDevicePathToText failed\n");
+			return EFI_ST_FAILURE;
+		}
+		efi_st_printf("%ps\n", string);
+		ret = boottime->free_pool(string);
+		if (ret != EFI_SUCCESS) {
+			efi_st_error("FreePool failed\n");
+			return EFI_ST_FAILURE;
+		}
+		ret = boottime->close_protocol(handles[i], &guid_device_path,
+					       NULL, NULL);
+		if (ret != EFI_SUCCESS)
+			efi_st_todo("Cannot close device path protocol.\n");
+	}
+	ret = boottime->free_pool(handles);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("FreePool failed\n");
+		return EFI_ST_FAILURE;
+	}
+	efi_st_printf("\n");
+
+	/* Test ConvertDevicePathToText */
+	string = device_path_to_text->convert_device_path_to_text(
+			(struct efi_device_path *)dp2, true, false);
+	if (!string) {
+		efi_st_error("ConvertDevicePathToText failed\n");
+		return EFI_ST_FAILURE;
+	}
+	efi_st_printf("dp2: %ps\n", string);
+	if (efi_st_strcmp_16_8(
+		string,
+		"/VenHw(dbca4c98-6cb0-694d-0872-819c650cbbb1)/VenHw(dbca4c98-6cb0-694d-0872-819c650cbba2)")
+	    ) {
+		efi_st_error("Incorrect text from ConvertDevicePathToText\n");
+		return EFI_ST_FAILURE;
+	}
+
+	ret = boottime->free_pool(string);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("FreePool failed\n");
+		return EFI_ST_FAILURE;
+	}
+
+	/* Test ConvertDeviceNodeToText */
+	string = device_path_to_text->convert_device_node_to_text(
+			(struct efi_device_path *)&dp_node, true, false);
+	if (!string) {
+		efi_st_error("ConvertDeviceNodeToText failed\n");
+		return EFI_ST_FAILURE;
+	}
+	efi_st_printf("dp_node: %ps\n", string);
+	ret = boottime->free_pool(string);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("FreePool failed\n");
+		return EFI_ST_FAILURE;
+	}
+	if (efi_st_strcmp_16_8(string, "u-boot")) {
+		efi_st_error(
+			"Incorrect conversion by ConvertDeviceNodeToText\n");
+		return EFI_ST_FAILURE;
+	}
+
+	/* Test LocateDevicePath */
+	remaining_dp = (struct efi_device_path *)dp3;
+	ret = boottime->locate_device_path(&guid_protocol, &remaining_dp,
+					   &handle);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("LocateDevicePath failed\n");
+		return EFI_ST_FAILURE;
+	}
+	if (handle != handle2) {
+		efi_st_error("LocateDevicePath returned wrong handle\n");
+		return EFI_ST_FAILURE;
+	}
+	string = device_path_to_text->convert_device_path_to_text(remaining_dp,
+								  true, false);
+	if (!string) {
+		efi_st_error("ConvertDevicePathToText failed\n");
+		return EFI_ST_FAILURE;
+	}
+	efi_st_printf("remaining device path: %ps\n", string);
+	if (efi_st_strcmp_16_8(string,
+			       "/VenHw(dbca4c98-6cb0-694d-0872-819c650cbbc3)")
+	    ) {
+		efi_st_error("LocateDevicePath: wrong remaining device path\n");
+		return EFI_ST_FAILURE;
+	}
+
+	return EFI_ST_SUCCESS;
+}
+
+EFI_UNIT_TEST(devicepath) = {
+	.name = "device path",
+	.phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
+	.setup = setup,
+	.execute = execute,
+	.teardown = teardown,
+};
-- 
2.14.2

  parent reply	other threads:[~2017-11-26 13:05 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-26 13:05 [U-Boot] [PATCH v3 00/18] efi_loader: manage protocols in a linked list (v3) Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 01/18] efi_loader: helloworld.c: remove superfluous include Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 02/18] efi_loader: size of media device path node represenation Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 03/18] efi_loader: efi_dp_str should print path not node Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 04/18] efi_loader: fix efi_convert_device_node_to_text Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 05/18] efi_loader: reimplement LocateDevicePath Heinrich Schuchardt
2017-11-26 13:05 ` Heinrich Schuchardt [this message]
2017-11-26 13:05 ` [U-Boot] [PATCH v3 07/18] efi_loader: efi_disk: use efi_add_protocol Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 08/18] efi_loader: efi_net: " Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 09/18] efi_loader: efi_gop: " Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 10/18] efi_loader: simplify efi_open_protocol Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 11/18] efi_loader: simplify find_obj Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 12/18] efi_loader: manage protocols in a linked list Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 13/18] efi_selftest: compile without special compiler flags Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 14/18] efi_selftest: add missing line feed Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 15/18] efi_loader: output load options in helloworld Heinrich Schuchardt
2017-12-09 23:09   ` Florian Fainelli
2017-12-10  6:40     ` Heinrich Schuchardt
2017-12-10 22:20       ` Florian Fainelli
2017-12-11  8:15         ` Alexander Graf
2017-11-26 13:05 ` [U-Boot] [PATCH v3 16/18] test/py: check return code of helloworld Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 17/18] efi_loader: pass handle of loaded image Heinrich Schuchardt
2017-11-26 13:05 ` [U-Boot] [PATCH v3 18/18] efi_loader: helper function to add EFI object to list Heinrich Schuchardt
2017-11-27 17:09 ` [U-Boot] [PATCH v3 00/18] efi_loader: manage protocols in a linked list (v3) Simon Glass
2017-12-01 12:42   ` Alexander Graf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171126130523.4993-7-xypron.glpk@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.