[PATCH 1/1] efi_selftest: supply EFI binary for ExitBootServices

Heinrich Schuchardt heinrich.schuchardt at canonical.com
Thu Sep 8 06:52:19 CEST 2022


Calling ExitBootServices() after executing 'connect -r' in the EFI
shell has previously caused errors.

Provide an EFI binary ebstest.efi that calls ExitBootServices() for
testing.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
---
I just need the binary currently for testing.
Probably we will need something simulating 'connect -r' in the unit tests.
---
 lib/efi_selftest/Makefile  |   5 ++
 lib/efi_selftest/ebstest.c | 122 +++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+)
 create mode 100644 lib/efi_selftest/ebstest.c

diff --git a/lib/efi_selftest/Makefile b/lib/efi_selftest/Makefile
index daac6c3968..a57237c122 100644
--- a/lib/efi_selftest/Makefile
+++ b/lib/efi_selftest/Makefile
@@ -14,6 +14,8 @@ CFLAGS_efi_selftest_miniapp_exit.o := $(CFLAGS_EFI) -Os -ffreestanding
 CFLAGS_REMOVE_efi_selftest_miniapp_exit.o := $(CFLAGS_NON_EFI)
 CFLAGS_efi_selftest_miniapp_return.o := $(CFLAGS_EFI) -Os -ffreestanding
 CFLAGS_REMOVE_efi_selftest_miniapp_return.o := $(CFLAGS_NON_EFI)
+CFLAGS_ebstest.o := $(CFLAGS_EFI) -Os -ffreestanding
+CFLAGS_REMOVE_ebstest.o := $(CFLAGS_NON_EFI)
 
 obj-y += \
 efi_selftest.o \
@@ -74,6 +76,9 @@ endif
 
 obj-$(CONFIG_EFI_ESRT) += efi_selftest_esrt.o
 
+always += \
+ebstest.efi
+
 targets += \
 efi_miniapp_file_image_exception.h \
 efi_miniapp_file_image_exit.h \
diff --git a/lib/efi_selftest/ebstest.c b/lib/efi_selftest/ebstest.c
new file mode 100644
index 0000000000..c785671d2b
--- /dev/null
+++ b/lib/efi_selftest/ebstest.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Call ExitBootServices()
+ *
+ * Copyright (c) 2022 Heinrich Schuchardt <xypron.glpk at gmx.de>
+ */
+
+#include <efi_api.h>
+#include <efi_load_initrd.h>
+
+static efi_handle_t handle;
+static struct efi_system_table *systable;
+static struct efi_boot_services *boottime;
+static struct efi_simple_text_output_protocol *cout;
+
+/**
+ * color() - set foreground color
+ *
+ * @color:	foreground color
+ */
+static void color(u8 color)
+{
+	cout->set_attribute(cout, color | EFI_BACKGROUND_BLACK);
+}
+
+/**
+ * print() - print string
+ *
+ * @string:	text
+ */
+static void print(u16 *string)
+{
+	cout->output_string(cout, string);
+}
+
+/**
+ * error() - print error string
+ *
+ * @string:	error text
+ */
+static void error(u16 *string)
+{
+	color(EFI_LIGHTRED);
+	print(string);
+	color(EFI_LIGHTGRAY);
+}
+
+/**
+ * Exit the boot services.
+ *
+ * The size of the memory map is determined.
+ * Pool memory is allocated to copy the memory map.
+ * The memory map is copied and the map key is obtained.
+ * The map key is used to exit the boot services.
+ */
+efi_uintn_t exit_boot_services(void)
+{
+	efi_uintn_t map_size = 0;
+	efi_uintn_t map_key;
+	efi_uintn_t desc_size;
+	u32 desc_version;
+	efi_status_t ret;
+	struct efi_mem_desc *memory_map;
+
+	ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
+				       &desc_version);
+	if (ret != EFI_BUFFER_TOO_SMALL) {
+		error(u"GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\r\n");
+		return ret;
+	}
+	/* Allocate extra space for newly allocated memory */
+	map_size += sizeof(struct efi_mem_desc);
+	ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
+				      (void **)&memory_map);
+	if (ret != EFI_SUCCESS) {
+		error(u"AllocatePool did not return EFI_SUCCESS\r\n");
+		return ret;
+	}
+	ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
+				       &desc_size, &desc_version);
+	if (ret != EFI_SUCCESS) {
+		error(u"GetMemoryMap did not return EFI_SUCCESS\r\n");
+		return ret;
+	}
+	ret = boottime->exit_boot_services(handle, map_key);
+	if (ret != EFI_SUCCESS) {
+		error(u"ExitBootServices did not return EFI_SUCCESS\r\n");
+		return ret;
+	}
+	return EFI_SUCCESS;
+}
+
+/**
+ * efi_main() - entry point of the EFI application.
+ *
+ * @handle:	handle of the loaded image
+ * @systab:	system table
+ * Return:	status code
+ */
+efi_status_t EFIAPI efi_main(efi_handle_t image_handle,
+			     struct efi_system_table *systab)
+{
+	handle = image_handle;
+	systable = systab;
+	cout = systable->con_out;
+	boottime = systable->boottime;
+	efi_status_t ret;
+
+	color(EFI_LIGHTBLUE);
+	print(u"\r\nCalling ExitBootServices\r\n\r\n");
+	color(EFI_LIGHTGRAY);
+
+	ret = exit_boot_services();
+	if (ret != EFI_SUCCESS)
+		return ret;
+
+	systable->runtime->reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
+
+	/* hang */
+	for (;;)
+		;
+}
-- 
2.37.2



More information about the U-Boot mailing list