Skip to contentSkip to conversation

efi_loader: Code refactoring and improvement

PATCH Superseded Simon Glass 2 participants 15 messages Nov 14, 2018 – Nov 24, 2018 Library code

v14 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v14 cover efi_loader: Code refactoring and improvement

This collects the patches previously sent to break up the very large functions in efi_loader into smaller pieces. Now that the other sandbox stuff is applied, perhaps it is time to apply these patches.

This also adds a few new patches to fix more recent breakages. Unfortunately we still cannot enable the efi loader tests since one of the tests fails. Thus we should expect additional failures to appear until that is resolved.

Changes in v14:
- Fix condition for invalid pointer
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Changes in v13:
- Drop 'efi_loader: Drop setup_ok' as we have an existing patch for that
- Drop patches previously applied
- Rebase to efi/efi-next
- Update code to deal with the struct_table_address member

Changes in v12:
- Rename image to image_prot

Changes in v11:
- Drop patches previously applied
- Fix the EFI code that has since been added and relies on broken behaviour

Changes in v9:
- Add comments to bootefi_test_prepare() about the memset()s

Changes in v7:
- Drop patch "efi: Init the 'rows' and 'cols' variables"
- Drop patches previous applied

Changes in v5:
- Drop call to efi_init_obj_list() which is now done in do_bootefi()
- Introduce load_options_path to specifyc U-Boot env var for load_options_path
- Rebase to master

Changes in v4:
- Rebase to master

Changes in v3:
- Add new patch to rename bootefi_test_finish() to bootefi_run_finish()
- Add new patch to split out test init/uninit into functions
- Add patch to create a function to set up for running EFI code
- Drop incorrect map_sysmem() in write_smbios_table()

Simon Glass (4):
sandbox: smbios: Update to support sandbox
efi: Split out test init/uninit into functions
efi: Create a function to set up for running EFI code
efi: Rename bootefi_test_finish() to bootefi_run_finish()

 cmd/bootefi.c               | 120 +++++++++++++++++++++++++++---------
 lib/efi_loader/efi_smbios.c |  20 +++---
 lib/smbios.c                |  52 +++++++++++++---
 3 files changed, 147 insertions(+), 45 deletions(-)
Signature · 1 line
2.19.1.1215.g8438c0b245-goog
#1 lore raw

PATCH v14 1/4 sandbox: smbios: Update to support sandbox

At present this code casts addresses to pointers so cannot be used with sandbox. Update it to use mapmem instead.

---

Changes in v14:
- Fix condition for invalid pointer

Changes in v13:
- Update code to deal with the struct_table_address member

Changes in v12: None
Changes in v11:
- Fix the EFI code that has since been added and relies on broken behaviour

Changes in v9: None
Changes in v7: None
Changes in v5: None
Changes in v4: None
Changes in v3:
- Drop incorrect map_sysmem() in write_smbios_table()

 lib/efi_loader/efi_smbios.c | 20 +++++++++-----
 lib/smbios.c                | 52 ++++++++++++++++++++++++++++++-------
 2 files changed, 56 insertions(+), 16 deletions(-)
lib/efi_loader/efi_smbios.c +13 -7
index 38e42fa2432..a81488495e2 100644
@@ -7,6 +7,7 @@
 
 #include <common.h>
 #include <efi_loader.h>
+#include <mapmem.h>
 #include <smbios.h>
 
 static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
@@ -19,17 +20,19 @@ static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
 efi_status_t efi_smbios_register(void)
 {
 	/* Map within the low 32 bits, to allow for 32bit SMBIOS tables */
-	u64 dmi = U32_MAX;
+	u64 dmi_addr = U32_MAX;
 	efi_status_t ret;
+	void *dmi;
 
 	/* Reserve 4kiB page for SMBIOS */
 	ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
-				 EFI_RUNTIME_SERVICES_DATA, 1, &dmi);
+				 EFI_RUNTIME_SERVICES_DATA, 1, &dmi_addr);
 
 	if (ret != EFI_SUCCESS) {
 		/* Could not find space in lowmem, use highmem instead */
 		ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
-					 EFI_RUNTIME_SERVICES_DATA, 1, &dmi);
+					 EFI_RUNTIME_SERVICES_DATA, 1,
+					 &dmi_addr);
 
 		if (ret != EFI_SUCCESS)
 			return ret;
@@ -39,11 +42,14 @@ efi_status_t efi_smbios_register(void)
 	 * Generate SMBIOS tables - we know that efi_allocate_pages() returns
 	 * a 4k-aligned address, so it is safe to assume that
 	 * write_smbios_table() will write the table at that address.
+	 *
+	 * Note that on sandbox, efi_allocate_pages() unfortunately returns a
+	 * pointer even though it uses a uint64_t type. Convert it.
 	 */
-	assert(!(dmi & 0xf));
-	write_smbios_table(dmi);
+	assert(!(dmi_addr & 0xf));
+	dmi = (void *)(uintptr_t)dmi_addr;
+	write_smbios_table(map_to_sysmem(dmi));
 
 	/* And expose them to our EFI payload */
-	return efi_install_configuration_table(&smbios_guid,
-					       (void *)(uintptr_t)dmi);
+	return efi_install_configuration_table(&smbios_guid, dmi);
 }
lib/smbios.c +43 -9

177 lines not on this page · raw message

Signature · 1 line
2.19.1.1215.g8438c0b245-goog
#2 lore raw

PATCH v14 2/4 efi: Split out test init/uninit into functions

In reply to Simon Glass

The functions in bootefi are very long because they mix high-level code and control with the low-level implementation. To help with this, create functions which handle preparing for running the test and cleaning up afterwards.

Also shorten the awfully long variable names here.

---

Changes in v14:
- Go back to the horrible long variable names

Changes in v13:
- Rebase to efi/efi-next

Changes in v12:
- Rename image to image_prot

Changes in v11: None
Changes in v9:
- Add comments to bootefi_test_prepare() about the memset()s

Changes in v7: None
Changes in v5:
- Drop call to efi_init_obj_list() which is now done in do_bootefi()

Changes in v4: None
Changes in v3:
- Add new patch to split out test init/uninit into functions

 cmd/bootefi.c | 81 +++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 65 insertions(+), 16 deletions(-)
cmd/bootefi.c +65 -16
index 3e37805ea13..b633e956c23 100644
@@ -453,6 +453,67 @@ exit:
 	return ret;
 }
 
+#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
+/**
+ * bootefi_test_prepare() - prepare to run an EFI test
+ *
+ * This sets things up so we can call EFI functions. This involves preparing
+ * the 'gd' pointer and setting up the load ed image data structures.
+ *
+ * @image_objp: loaded_image_infop: Pointer to a struct which will hold the
+ *    loaded image object. This struct will be inited by this function before
+ *    use.
+ * @loaded_image_infop: Pointer to a struct which will hold the loaded image
+ *    info. This struct will be inited by this function before use.
+ * @path: File path to the test being run (often just the test name with a
+ *    backslash before it
+ * @test_func: Address of the test function that is being run
+ * @return 0 if OK, -ve on error
+ */
+static efi_status_t bootefi_test_prepare(
+		struct efi_loaded_image_obj **image_objp,
+		struct efi_loaded_image **loaded_image_infop,
+		const char *path,
+		ulong test_func)
+{
+	efi_status_t r;
+
+	/* Construct a dummy device path */
+	bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
+					      (uintptr_t)test_func,
+					      (uintptr_t)test_func);
+	bootefi_image_path = efi_dp_from_file(NULL, 0, path);
+	r = efi_setup_loaded_image(bootefi_device_path, bootefi_image_path,
+				   image_objp, loaded_image_infop);
+	if (r)
+		return r;
+	/*
+	 * gd lives in a fixed register which may get clobbered while we execute
+	 * the payload. So save it here and restore it on every callback entry
+	 */
+	efi_save_gd();
+
+	/* Transfer environment variable efi_selftest as load options */
+	set_load_options(*loaded_image_infop, "efi_selftest");
+
+	return 0;
+}
+
+/**
+ * bootefi_test_finish() - finish up after running an EFI test
+ *
+ * @image_obj: Pointer to a struct which holds the loaded image object
+ * @loaded_image_info: Pointer to a struct which holds the loaded image info
+ */
+static void bootefi_test_finish(struct efi_loaded_image_obj *image_obj,
+				struct efi_loaded_image *loaded_image_info)
+{
+	efi_restore_gd();
+	free(loaded_image_info->load_options);
+	efi_delete_handle(&image_obj->header);
+}
+#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
+
 static int do_bootefi_bootmgr_exec(void)
 {
 	struct efi_device_path *device_path, *file_path;
@@ -528,26 +589,14 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		struct efi_loaded_image_obj *image_obj;
 		struct efi_loaded_image *loaded_image_info;
 
-		/* Construct a dummy device path. */
-		bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
-						      (uintptr_t)&efi_selftest,
-						      (uintptr_t)&efi_selftest);
-		bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
-
-		r = efi_setup_loaded_image(bootefi_device_path,
-					   bootefi_image_path, &image_obj,
-					   &loaded_image_info);
-		if (r != EFI_SUCCESS)
+		if (bootefi_test_prepare(&image_obj, &loaded_image_info,
+					 "\\selftest",
+					 (uintptr_t)&efi_selftest))
 			return CMD_RET_FAILURE;
 
-		efi_save_gd();
-		/* Transfer environment variable efi_selftest as load options */
-		set_load_options(loaded_image_info, "efi_selftest");
 		/* Execute the test */
 		r = efi_selftest(&image_obj->header, &systab);
-		efi_restore_gd();
-		free(loaded_image_info->load_options);
-		efi_delete_handle(&image_obj->header);
+		bootefi_test_finish(image_obj, loaded_image_info);
 		return r != EFI_SUCCESS;
 	} else
 #endif
Signature · 1 line
2.19.1.1215.g8438c0b245-goog
#3 lore raw

PATCH v14 2/4 efi: Split out test init/uninit into functions

Quoted 50 lines · Simon Glass

The functions in bootefi are very long because they mix high-level code and control with the low-level implementation. To help with this, create functions which handle preparing for running the test and cleaning up afterwards.

Also shorten the awfully long variable names here.

---

Changes in v14:
- Go back to the horrible long variable names

Changes in v13:
- Rebase to efi/efi-next

Changes in v12:
- Rename image to image_prot

Changes in v11: None
Changes in v9:
- Add comments to bootefi_test_prepare() about the memset()s

Changes in v7: None
Changes in v5:
- Drop call to efi_init_obj_list() which is now done in do_bootefi()

Changes in v4: None
Changes in v3:
- Add new patch to split out test init/uninit into functions

 cmd/bootefi.c | 81 +++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 65 insertions(+), 16 deletions(-)
cmd/bootefi.c +8 -0
index 3e37805ea13..b633e956c23 100644
@@ -453,6 +453,67 @@ exit:
 	return ret;
 }
 
+#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
+/**
+ * bootefi_test_prepare() - prepare to run an EFI test
+ *
+ * This sets things up so we can call EFI functions. This involves preparing
+ * the 'gd' pointer and setting up the load ed image data structures.
+ *
+ * @image_objp: loaded_image_infop: Pointer to a struct which will hold the

cmd/bootefi.c exit: · line 473

Quoted 10 lines
cmd/bootefi.c +10 -0
+ *    loaded image object. This struct will be inited by this function before
+ *    use.
+ * @loaded_image_infop: Pointer to a struct which will hold the loaded image
+ *    info. This struct will be inited by this function before use.
+ * @path: File path to the test being run (often just the test name with a
+ *    backslash before it
+ * @test_func: Address of the test function that is being run
+ * @return 0 if OK, -ve on error
+ */
+static efi_status_t bootefi_test_prepare(

CHECK: Lines should not end with a '('
#43: FILE: cmd/bootefi.c:474:
+static efi_status_t bootefi_test_prepare(

So the parenthesis has to go onto the next line.

cmd/bootefi.c exit: · line 474

Quoted 1 line
cmd/bootefi.c +1 -0
+		struct efi_loaded_image_obj **image_objp,

(struct efi_loaded_image_obj **image_objp,

cmd/bootefi.c exit: · line 484

Quoted 10 lines
cmd/bootefi.c +10 -0
+		struct efi_loaded_image **loaded_image_infop,
+		const char *path,
+		ulong test_func)
+{
+	efi_status_t r;
+
+	/* Construct a dummy device path */
+	bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
+					      (uintptr_t)test_func,
+					      (uintptr_t)test_func);

We have to check the return value. It may be NULL.

cmd/bootefi.c exit: · line 485

Quoted 1 line
cmd/bootefi.c +1 -0
+	bootefi_image_path = efi_dp_from_file(NULL, 0, path);

Same here.

cmd/bootefi.c exit: · line 494

Quoted 9 lines
cmd/bootefi.c +9 -0
+	r = efi_setup_loaded_image(bootefi_device_path, bootefi_image_path,
+				   image_objp, loaded_image_infop);
+	if (r)
+		return r;
+	/*
+	 * gd lives in a fixed register which may get clobbered while we execute
+	 * the payload. So save it here and restore it on every callback entry
+	 */
+	efi_save_gd();

We do this already in efi_init_obj_list().

Best regards

Heinrich

Quoted 57 lines
cmd/bootefi.c +26 -16
+
+	/* Transfer environment variable efi_selftest as load options */
+	set_load_options(*loaded_image_infop, "efi_selftest");
+
+	return 0;
+}
+
+/**
+ * bootefi_test_finish() - finish up after running an EFI test
+ *
+ * @image_obj: Pointer to a struct which holds the loaded image object
+ * @loaded_image_info: Pointer to a struct which holds the loaded image info
+ */
+static void bootefi_test_finish(struct efi_loaded_image_obj *image_obj,
+				struct efi_loaded_image *loaded_image_info)
+{
+	efi_restore_gd();
+	free(loaded_image_info->load_options);
+	efi_delete_handle(&image_obj->header);
+}
+#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
+
 static int do_bootefi_bootmgr_exec(void)
 {
 	struct efi_device_path *device_path, *file_path;
@@ -528,26 +589,14 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		struct efi_loaded_image_obj *image_obj;
 		struct efi_loaded_image *loaded_image_info;
 
-		/* Construct a dummy device path. */
-		bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
-						      (uintptr_t)&efi_selftest,
-						      (uintptr_t)&efi_selftest);
-		bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
-
-		r = efi_setup_loaded_image(bootefi_device_path,
-					   bootefi_image_path, &image_obj,
-					   &loaded_image_info);
-		if (r != EFI_SUCCESS)
+		if (bootefi_test_prepare(&image_obj, &loaded_image_info,
+					 "\\selftest",
+					 (uintptr_t)&efi_selftest))
 			return CMD_RET_FAILURE;
 
-		efi_save_gd();
-		/* Transfer environment variable efi_selftest as load options */
-		set_load_options(loaded_image_info, "efi_selftest");
 		/* Execute the test */
 		r = efi_selftest(&image_obj->header, &systab);
-		efi_restore_gd();
-		free(loaded_image_info->load_options);
-		efi_delete_handle(&image_obj->header);
+		bootefi_test_finish(image_obj, loaded_image_info);
 		return r != EFI_SUCCESS;
 	} else
 #endif
#4 lore raw

PATCH v14 3/4 efi: Create a function to set up for running EFI code

In reply to Simon Glass

There is still duplicated code in efi_loader for tests and normal operation.

Add a new bootefi_run_prepare() function which holds common code used to set up U-Boot to run EFI code. Make use of this from the existing bootefi_test_prepare() function, as well as do_bootefi_exec().

Also shorten a few variable names.

---

Changes in v14:
- Go back to the horrible long variable names

Changes in v13: None
Changes in v12: None
Changes in v11: None
Changes in v9: None
Changes in v7: None
Changes in v5:
- Drop call to efi_init_obj_list() which is now done in do_bootefi()
- Introduce load_options_path to specifyc U-Boot env var for load_options_path

Changes in v4:
- Rebase to master

Changes in v3:
- Add patch to create a function to set up for running EFI code

 cmd/bootefi.c | 63 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 37 insertions(+), 26 deletions(-)
cmd/bootefi.c +37 -26
index b633e956c23..ab7ada9f2d6 100644
@@ -325,6 +325,31 @@ static efi_status_t efi_install_fdt(ulong fdt_addr)
 	return ret;
 }
 
+static efi_status_t bootefi_run_prepare(const char *load_options_path,
+		struct efi_device_path *device_path,
+		struct efi_device_path *image_path,
+		struct efi_loaded_image_obj **image_objp,
+		struct efi_loaded_image **loaded_image_infop)
+{
+	efi_status_t ret;
+
+	ret = efi_setup_loaded_image(device_path, image_path, image_objp,
+				     loaded_image_infop);
+	if (ret != EFI_SUCCESS)
+		return ret;
+
+	/*
+	 * gd lives in a fixed register which may get clobbered while we execute
+	 * the payload. So save it here and restore it on every callback entry
+	 */
+	efi_save_gd();
+
+	/* Transfer environment variable as load options */
+	set_load_options(*loaded_image_infop, load_options_path);
+
+	return 0;
+}
+
 /**
  * do_bootefi_exec() - execute EFI binary
  *
@@ -374,13 +399,11 @@ static efi_status_t do_bootefi_exec(void *efi,
 		assert(device_path && image_path);
 	}
 
-	ret = efi_setup_loaded_image(device_path, image_path, &image_obj,
-				     &loaded_image_info);
-	if (ret != EFI_SUCCESS)
-		goto exit;
+	ret = bootefi_run_prepare("bootargs", device_path, image_path,
+				  &image_obj, &loaded_image_info);
+	if (ret)
+		return ret;
 
-	/* Transfer environment variable bootargs as load options */
-	set_load_options(loaded_image_info, "bootargs");
 	/* Load the EFI payload */
 	entry = efi_load_pe(image_obj, efi, loaded_image_info);
 	if (!entry) {
@@ -468,35 +491,23 @@ exit:
  * @path: File path to the test being run (often just the test name with a
  *    backslash before it
  * @test_func: Address of the test function that is being run
+ * @load_options_path: U-Boot environment variable to use as load options
  * @return 0 if OK, -ve on error
  */
 static efi_status_t bootefi_test_prepare(
 		struct efi_loaded_image_obj **image_objp,
-		struct efi_loaded_image **loaded_image_infop,
-		const char *path,
-		ulong test_func)
+		struct efi_loaded_image **loaded_image_infop, const char *path,
+		ulong test_func, const char *load_options_path)
 {
-	efi_status_t r;
-
 	/* Construct a dummy device path */
 	bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
 					      (uintptr_t)test_func,
 					      (uintptr_t)test_func);
 	bootefi_image_path = efi_dp_from_file(NULL, 0, path);
-	r = efi_setup_loaded_image(bootefi_device_path, bootefi_image_path,
-				   image_objp, loaded_image_infop);
-	if (r)
-		return r;
-	/*
-	 * gd lives in a fixed register which may get clobbered while we execute
-	 * the payload. So save it here and restore it on every callback entry
-	 */
-	efi_save_gd();
-
-	/* Transfer environment variable efi_selftest as load options */
-	set_load_options(*loaded_image_infop, "efi_selftest");
 
-	return 0;
+	return bootefi_run_prepare(load_options_path, bootefi_device_path,
+				   bootefi_image_path, image_objp,
+				   loaded_image_infop);
 }
 
 /**
@@ -590,8 +601,8 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		struct efi_loaded_image *loaded_image_info;
 
 		if (bootefi_test_prepare(&image_obj, &loaded_image_info,
-					 "\\selftest",
-					 (uintptr_t)&efi_selftest))
+					 "\\selftest", (uintptr_t)&efi_selftest,
+					 "efi_selftest"))
 			return CMD_RET_FAILURE;
 
 		/* Execute the test */
Signature · 1 line
2.19.1.1215.g8438c0b245-goog
#5 lore raw

PATCH v14 3/4 efi: Create a function to set up for running EFI code

Quoted 88 lines · Simon Glass

There is still duplicated code in efi_loader for tests and normal operation.

Add a new bootefi_run_prepare() function which holds common code used to set up U-Boot to run EFI code. Make use of this from the existing bootefi_test_prepare() function, as well as do_bootefi_exec().

Also shorten a few variable names.

---

Changes in v14:
- Go back to the horrible long variable names

Changes in v13: None
Changes in v12: None
Changes in v11: None
Changes in v9: None
Changes in v7: None
Changes in v5:
- Drop call to efi_init_obj_list() which is now done in do_bootefi()
- Introduce load_options_path to specifyc U-Boot env var for load_options_path

Changes in v4:
- Rebase to master

Changes in v3:
- Add patch to create a function to set up for running EFI code

 cmd/bootefi.c | 63 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 37 insertions(+), 26 deletions(-)
cmd/bootefi.c +29 -6
index b633e956c23..ab7ada9f2d6 100644
@@ -325,6 +325,31 @@ static efi_status_t efi_install_fdt(ulong fdt_addr)
 	return ret;
 }
 
+static efi_status_t bootefi_run_prepare(const char *load_options_path,
+		struct efi_device_path *device_path,
+		struct efi_device_path *image_path,
+		struct efi_loaded_image_obj **image_objp,
+		struct efi_loaded_image **loaded_image_infop)
+{
+	efi_status_t ret;
+
+	ret = efi_setup_loaded_image(device_path, image_path, image_objp,
+				     loaded_image_infop);
+	if (ret != EFI_SUCCESS)
+		return ret;
+
+	/*
+	 * gd lives in a fixed register which may get clobbered while we execute
+	 * the payload. So save it here and restore it on every callback entry
+	 */
+	efi_save_gd();
+
+	/* Transfer environment variable as load options */
+	set_load_options(*loaded_image_infop, load_options_path);
+
+	return 0;
+}
+
 /**
  * do_bootefi_exec() - execute EFI binary
  *
@@ -374,13 +399,11 @@ static efi_status_t do_bootefi_exec(void *efi,
 		assert(device_path && image_path);
 	}
 
-	ret = efi_setup_loaded_image(device_path, image_path, &image_obj,
-				     &loaded_image_info);
-	if (ret != EFI_SUCCESS)
-		goto exit;
+	ret = bootefi_run_prepare("bootargs", device_path, image_path,
+				  &image_obj, &loaded_image_info);
+	if (ret)
+		return ret;
 
-	/* Transfer environment variable bootargs as load options */
-	set_load_options(loaded_image_info, "bootargs");
 	/* Load the EFI payload */
 	entry = efi_load_pe(image_obj, efi, loaded_image_info);
 	if (!entry) {

cmd/bootefi.c exit: · line 497

Quoted 7 lines
cmd/bootefi.c +1 -0
@@ -468,35 +491,23 @@ exit:
  * @path: File path to the test being run (often just the test name with a
  *    backslash before it
  * @test_func: Address of the test function that is being run
+ * @load_options_path: U-Boot environment variable to use as load options
  * @return 0 if OK, -ve on error
  */
 static efi_status_t bootefi_test_prepare(

CHECK: Alignment should match open parenthesis
#29: FILE: cmd/bootefi.c:330:
+static efi_status_t bootefi_run_prepare(const char *load_options_path,
+ struct efi_device_path *device_path,

Quoted 46 lines
cmd/bootefi.c +7 -20
 		struct efi_loaded_image_obj **image_objp,
-		struct efi_loaded_image **loaded_image_infop,
-		const char *path,
-		ulong test_func)
+		struct efi_loaded_image **loaded_image_infop, const char *path,
+		ulong test_func, const char *load_options_path)
 {
-	efi_status_t r;
-
 	/* Construct a dummy device path */
 	bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
 					      (uintptr_t)test_func,
 					      (uintptr_t)test_func);
 	bootefi_image_path = efi_dp_from_file(NULL, 0, path);
-	r = efi_setup_loaded_image(bootefi_device_path, bootefi_image_path,
-				   image_objp, loaded_image_infop);
-	if (r)
-		return r;
-	/*
-	 * gd lives in a fixed register which may get clobbered while we execute
-	 * the payload. So save it here and restore it on every callback entry
-	 */
-	efi_save_gd();
-
-	/* Transfer environment variable efi_selftest as load options */
-	set_load_options(*loaded_image_infop, "efi_selftest");
 
-	return 0;
+	return bootefi_run_prepare(load_options_path, bootefi_device_path,
+				   bootefi_image_path, image_objp,
+				   loaded_image_infop);
 }
 
 /**
@@ -590,8 +601,8 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		struct efi_loaded_image *loaded_image_info;
 
 		if (bootefi_test_prepare(&image_obj, &loaded_image_info,
-					 "\\selftest",
-					 (uintptr_t)&efi_selftest))
+					 "\\selftest", (uintptr_t)&efi_selftest,
+					 "efi_selftest"))
 			return CMD_RET_FAILURE;
 
 		/* Execute the test */
#6 lore raw

PATCH v14 4/4 efi: Rename bootefi_test_finish() to bootefi_run_finish()

In reply to Simon Glass

This function can be used from do_bootefi_exec() so that we use mostly the same code for a normal EFI application and an EFI test.

Rename the function and use it in both places.

---

Changes in v14:
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Changes in v13:
- Drop 'efi_loader: Drop setup_ok' as we have an existing patch for that
- Drop patches previously applied

Changes in v12: None
Changes in v11:
- Drop patches previously applied

Changes in v9: None
Changes in v7:
- Drop patch "efi: Init the 'rows' and 'cols' variables"
- Drop patches previous applied

Changes in v5:
- Rebase to master

Changes in v4:
- Rebase to master

Changes in v3:
- Add new patch to rename bootefi_test_finish() to bootefi_run_finish()

 cmd/bootefi.c | 46 ++++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
cmd/bootefi.c +24 -22
index ab7ada9f2d6..a627f689f95 100644
@@ -350,6 +350,20 @@ static efi_status_t bootefi_run_prepare(const char *load_options_path,
 	return 0;
 }
 
+/**
+ * bootefi_run_finish() - finish up after running an EFI test
+ *
+ * @loaded_image_info: Pointer to a struct which holds the loaded image info
+ * @image_objj: Pointer to a struct which holds the loaded image object
+ */
+static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
+			       struct efi_loaded_image *loaded_image_info)
+{
+	efi_restore_gd();
+	free(loaded_image_info->load_options);
+	efi_delete_handle(&image_obj->header);
+}
+
 /**
  * do_bootefi_exec() - execute EFI binary
  *
@@ -390,11 +404,11 @@ static efi_status_t do_bootefi_exec(void *efi,
 		 */
 		ret = efi_create_handle(&mem_handle);
 		if (ret != EFI_SUCCESS)
-			goto exit;
+			return ret;
 		ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
 				       device_path);
 		if (ret != EFI_SUCCESS)
-			goto exit;
+			goto err_add_protocol;
 	} else {
 		assert(device_path && image_path);
 	}
@@ -402,13 +416,13 @@ static efi_status_t do_bootefi_exec(void *efi,
 	ret = bootefi_run_prepare("bootargs", device_path, image_path,
 				  &image_obj, &loaded_image_info);
 	if (ret)
-		return ret;
+		goto err_prepare;
 
 	/* Load the EFI payload */
 	entry = efi_load_pe(image_obj, efi, loaded_image_info);
 	if (!entry) {
 		ret = EFI_LOAD_ERROR;
-		goto exit;
+		goto err_prepare;
 	}
 
 	if (memdp) {
@@ -428,7 +442,7 @@ static efi_status_t do_bootefi_exec(void *efi,
 
 	if (setjmp(&image_obj->exit_jmp)) {
 		ret = image_obj->exit_status;
-		goto exit;
+		goto err_prepare;
 	}
 
 #ifdef CONFIG_ARM64
@@ -466,10 +480,11 @@ static efi_status_t do_bootefi_exec(void *efi,
 
 	ret = efi_do_enter(&image_obj->header, &systab, entry);
 
-exit:
+err_prepare:
 	/* image has returned, loaded-image obj goes *poof*: */
-	if (image_obj)
-		efi_delete_handle(&image_obj->header);
+	bootefi_run_finish(image_obj, loaded_image_info);
+
+err_add_protocol:
 	if (mem_handle)
 		efi_delete_handle(mem_handle);
 
@@ -510,19 +525,6 @@ static efi_status_t bootefi_test_prepare(
 				   loaded_image_infop);
 }
 
-/**
- * bootefi_test_finish() - finish up after running an EFI test
- *
- * @image_obj: Pointer to a struct which holds the loaded image object
- * @loaded_image_info: Pointer to a struct which holds the loaded image info
- */
-static void bootefi_test_finish(struct efi_loaded_image_obj *image_obj,
-				struct efi_loaded_image *loaded_image_info)
-{
-	efi_restore_gd();
-	free(loaded_image_info->load_options);
-	efi_delete_handle(&image_obj->header);
-}
 #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
 
 static int do_bootefi_bootmgr_exec(void)
@@ -607,7 +609,7 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 		/* Execute the test */
 		r = efi_selftest(&image_obj->header, &systab);
-		bootefi_test_finish(image_obj, loaded_image_info);
+		bootefi_run_finish(image_obj, loaded_image_info);
 		return r != EFI_SUCCESS;
 	} else
 #endif
Signature · 1 line
2.19.1.1215.g8438c0b245-goog
#7 lore raw

PATCH v14 4/4 efi: Rename bootefi_test_finish() to bootefi_run_finish()

Quoted 63 lines · Simon Glass

This function can be used from do_bootefi_exec() so that we use mostly the same code for a normal EFI application and an EFI test.

Rename the function and use it in both places.

---

Changes in v14:
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Changes in v13:
- Drop 'efi_loader: Drop setup_ok' as we have an existing patch for that
- Drop patches previously applied

Changes in v12: None
Changes in v11:
- Drop patches previously applied

Changes in v9: None
Changes in v7:
- Drop patch "efi: Init the 'rows' and 'cols' variables"
- Drop patches previous applied

Changes in v5:
- Rebase to master

Changes in v4:
- Rebase to master

Changes in v3:
- Add new patch to rename bootefi_test_finish() to bootefi_run_finish()

 cmd/bootefi.c | 46 ++++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
cmd/bootefi.c +14 -0
index ab7ada9f2d6..a627f689f95 100644
@@ -350,6 +350,20 @@ static efi_status_t bootefi_run_prepare(const char *load_options_path,
 	return 0;
 }
 
+/**
+ * bootefi_run_finish() - finish up after running an EFI test
+ *
+ * @loaded_image_info: Pointer to a struct which holds the loaded image info
+ * @image_objj: Pointer to a struct which holds the loaded image object
+ */
+static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
+			       struct efi_loaded_image *loaded_image_info)
+{
+	efi_restore_gd();
+	free(loaded_image_info->load_options);
+	efi_delete_handle(&image_obj->header);
+}
+
 /**
  * do_bootefi_exec() - execute EFI binary
  *

cmd/bootefi.c static efi_status_t do_bootefi_exec(void *efi, · line 393

Quoted 4 lines
cmd/bootefi.c +0 -1
@@ -390,11 +404,11 @@ static efi_status_t do_bootefi_exec(void *efi,
 		 */
 		ret = efi_create_handle(&mem_handle);
 		if (ret != EFI_SUCCESS)
-			goto exit;

You are leaking a device path.

Best regards

Heinrich

Quoted 79 lines
cmd/bootefi.c +10 -21
+			return ret;
 		ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
 				       device_path);
 		if (ret != EFI_SUCCESS)
-			goto exit;
+			goto err_add_protocol;
 	} else {
 		assert(device_path && image_path);
 	}
@@ -402,13 +416,13 @@ static efi_status_t do_bootefi_exec(void *efi,
 	ret = bootefi_run_prepare("bootargs", device_path, image_path,
 				  &image_obj, &loaded_image_info);
 	if (ret)
-		return ret;
+		goto err_prepare;
 
 	/* Load the EFI payload */
 	entry = efi_load_pe(image_obj, efi, loaded_image_info);
 	if (!entry) {
 		ret = EFI_LOAD_ERROR;
-		goto exit;
+		goto err_prepare;
 	}
 
 	if (memdp) {
@@ -428,7 +442,7 @@ static efi_status_t do_bootefi_exec(void *efi,
 
 	if (setjmp(&image_obj->exit_jmp)) {
 		ret = image_obj->exit_status;
-		goto exit;
+		goto err_prepare;
 	}
 
 #ifdef CONFIG_ARM64
@@ -466,10 +480,11 @@ static efi_status_t do_bootefi_exec(void *efi,
 
 	ret = efi_do_enter(&image_obj->header, &systab, entry);
 
-exit:
+err_prepare:
 	/* image has returned, loaded-image obj goes *poof*: */
-	if (image_obj)
-		efi_delete_handle(&image_obj->header);
+	bootefi_run_finish(image_obj, loaded_image_info);
+
+err_add_protocol:
 	if (mem_handle)
 		efi_delete_handle(mem_handle);
 
@@ -510,19 +525,6 @@ static efi_status_t bootefi_test_prepare(
 				   loaded_image_infop);
 }
 
-/**
- * bootefi_test_finish() - finish up after running an EFI test
- *
- * @image_obj: Pointer to a struct which holds the loaded image object
- * @loaded_image_info: Pointer to a struct which holds the loaded image info
- */
-static void bootefi_test_finish(struct efi_loaded_image_obj *image_obj,
-				struct efi_loaded_image *loaded_image_info)
-{
-	efi_restore_gd();
-	free(loaded_image_info->load_options);
-	efi_delete_handle(&image_obj->header);
-}
 #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
 
 static int do_bootefi_bootmgr_exec(void)
@@ -607,7 +609,7 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 		/* Execute the test */
 		r = efi_selftest(&image_obj->header, &systab);
-		bootefi_test_finish(image_obj, loaded_image_info);
+		bootefi_run_finish(image_obj, loaded_image_info);
 		return r != EFI_SUCCESS;
 	} else
 #endif
#8 lore raw

PATCH v14 4/4 efi: Rename bootefi_test_finish() to bootefi_run_finish()

Hi Heinrich,

Quoted 63 lines · Heinrich Schuchardt
Quoted 61 lines · Simon Glass

This function can be used from do_bootefi_exec() so that we use mostly the same code for a normal EFI application and an EFI test.

Rename the function and use it in both places.

---

Changes in v14:
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Changes in v13:
- Drop 'efi_loader: Drop setup_ok' as we have an existing patch for that
- Drop patches previously applied

Changes in v12: None
Changes in v11:
- Drop patches previously applied

Changes in v9: None
Changes in v7:
- Drop patch "efi: Init the 'rows' and 'cols' variables"
- Drop patches previous applied

Changes in v5:
- Rebase to master

Changes in v4:
- Rebase to master

Changes in v3:
- Add new patch to rename bootefi_test_finish() to bootefi_run_finish()

 cmd/bootefi.c | 46 ++++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
cmd/bootefi.c +14 -0
index ab7ada9f2d6..a627f689f95 100644
@@ -350,6 +350,20 @@ static efi_status_t bootefi_run_prepare(const char *load_options_path,
      return 0;
 }
 
+/**
+ * bootefi_run_finish() - finish up after running an EFI test
+ *
+ * @loaded_image_info: Pointer to a struct which holds the loaded image info
+ * @image_objj: Pointer to a struct which holds the loaded image object
+ */
+static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
+                            struct efi_loaded_image *loaded_image_info)
+{
+     efi_restore_gd();
+     free(loaded_image_info->load_options);
+     efi_delete_handle(&image_obj->header);
+}
+
 /**
  * do_bootefi_exec() - execute EFI binary
Quoted 8 lines

cmd/bootefi.c static efi_status_t do_bootefi_exec(void *efi, · line 393

Quoted 6 lines
cmd/bootefi.c +0 -1
  *
@@ -390,11 +404,11 @@ static efi_status_t do_bootefi_exec(void *efi,
               */
              ret = efi_create_handle(&mem_handle);
              if (ret != EFI_SUCCESS)
-                     goto exit;

You are leaking a device path.

I cannot see anywhere where these are freed. Are you sure this is supported?

I'll add a comment for now. In any case, this matches the previous code.

Regards,
Simon

#9 lore raw

PATCH v14 4/4 efi: Rename bootefi_test_finish() to bootefi_run_finish()

Quoted 75 lines · Simon Glass

Hi Heinrich,

Quoted 63 lines · Heinrich Schuchardt
Quoted 61 lines · Simon Glass

This function can be used from do_bootefi_exec() so that we use mostly the same code for a normal EFI application and an EFI test.

Rename the function and use it in both places.

---

Changes in v14:
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Changes in v13:
- Drop 'efi_loader: Drop setup_ok' as we have an existing patch for that
- Drop patches previously applied

Changes in v12: None
Changes in v11:
- Drop patches previously applied

Changes in v9: None
Changes in v7:
- Drop patch "efi: Init the 'rows' and 'cols' variables"
- Drop patches previous applied

Changes in v5:
- Rebase to master

Changes in v4:
- Rebase to master

Changes in v3:
- Add new patch to rename bootefi_test_finish() to bootefi_run_finish()

 cmd/bootefi.c | 46 ++++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
cmd/bootefi.c +14 -0
index ab7ada9f2d6..a627f689f95 100644
@@ -350,6 +350,20 @@ static efi_status_t bootefi_run_prepare(const char *load_options_path,
      return 0;
 }
 
+/**
+ * bootefi_run_finish() - finish up after running an EFI test
+ *
+ * @loaded_image_info: Pointer to a struct which holds the loaded image info
+ * @image_objj: Pointer to a struct which holds the loaded image object
+ */
+static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
+                            struct efi_loaded_image *loaded_image_info)
+{
+     efi_restore_gd();
+     free(loaded_image_info->load_options);
+     efi_delete_handle(&image_obj->header);
+}
+
 /**
  * do_bootefi_exec() - execute EFI binary
Quoted 8 lines

cmd/bootefi.c static efi_status_t do_bootefi_exec(void *efi, · line 393

Quoted 6 lines
cmd/bootefi.c +0 -1
  *
@@ -390,11 +404,11 @@ static efi_status_t do_bootefi_exec(void *efi,
               */
              ret = efi_create_handle(&mem_handle);
              if (ret != EFI_SUCCESS)
-                     goto exit;

You are leaking a device path.

Quoted 1 line

I cannot see anywhere where these are freed. Are you sure this is supported?

According to the UEFI standard handles are deleted when the last protocol is removed. See efi_uninstall_protocol_interface(). I am aware that our cleanup after running binaries is still incomplete.

In this function you could use efi_delete_handle() in the error handler.

Regards

Heinrich

Quoted 6 lines

I'll add a comment for now. In any case, this matches the previous code.

Regards,
Simon

#10 lore raw

PATCH v14 4/4 efi: Rename bootefi_test_finish() to bootefi_run_finish()

Hi Heinrich,

Quoted 77 lines · Heinrich Schuchardt
Quoted 75 lines · Simon Glass

Hi Heinrich,

Quoted 71 lines · Heinrich Schuchardt
Quoted 63 lines · Simon Glass

This function can be used from do_bootefi_exec() so that we use mostly the same code for a normal EFI application and an EFI test.

Rename the function and use it in both places.

---

Changes in v14:
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Changes in v13:
- Drop 'efi_loader: Drop setup_ok' as we have an existing patch for that
- Drop patches previously applied

Changes in v12: None
Changes in v11:
- Drop patches previously applied

Changes in v9: None
Changes in v7:
- Drop patch "efi: Init the 'rows' and 'cols' variables"
- Drop patches previous applied

Changes in v5:
- Rebase to master

Changes in v4:
- Rebase to master

Changes in v3:
- Add new patch to rename bootefi_test_finish() to bootefi_run_finish()

 cmd/bootefi.c | 46 ++++++++++++++++++++++++----------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
cmd/bootefi.c +14 -0
index ab7ada9f2d6..a627f689f95 100644
@@ -350,6 +350,20 @@ static efi_status_t bootefi_run_prepare(const char *load_options_path,
      return 0;
 }
 
+/**
+ * bootefi_run_finish() - finish up after running an EFI test
+ *
+ * @loaded_image_info: Pointer to a struct which holds the loaded image info
+ * @image_objj: Pointer to a struct which holds the loaded image object
+ */
+static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
+                            struct efi_loaded_image *loaded_image_info)
+{
+     efi_restore_gd();
+     free(loaded_image_info->load_options);
+     efi_delete_handle(&image_obj->header);
+}
+
 /**
  * do_bootefi_exec() - execute EFI binary
  *

cmd/bootefi.c static efi_status_t do_bootefi_exec(void *efi, · line 393

Quoted 4 lines
cmd/bootefi.c +0 -1
@@ -390,11 +404,11 @@ static efi_status_t do_bootefi_exec(void *efi,
               */
              ret = efi_create_handle(&mem_handle);
              if (ret != EFI_SUCCESS)
-                     goto exit;

You are leaking a device path.

Quoted 7 lines
Quoted 1 line

I cannot see anywhere where these are freed. Are you sure this is supported?

According to the UEFI standard handles are deleted when the last protocol is removed. See efi_uninstall_protocol_interface(). I am aware that our cleanup after running binaries is still incomplete.

In this function you could use efi_delete_handle() in the error handler.

Actually the code is pretty impenetrable.

efi_delete_handle() takes an efi_handle_t which, confusingly, is a pointer to an efi_object. Also we should not typedef structs in U-Boot, particularly not with a different name.

In this case efi_create_handle() failed so we don't need to delete that. We need to delete the efi_dp_from_mem() object. But the type of that is struct efi_device_path, so I cannot call efi_delete_handle() with it.

My suggestion is that if these patches land, you take a look at it and try to figure out how to finish improving the error handling in this function (which I started on patch v14 of this series). If I had my way this code would be much, much simpler to read.

Regards,
Simon

#11 lore raw

v14 cover efi_loader: Code refactoring and improvement

In reply to Simon Glass

Hi Alex,

Quoted 10 lines · Simon Glass

This collects the patches previously sent to break up the very large functions in efi_loader into smaller pieces. Now that the other sandbox stuff is applied, perhaps it is time to apply these patches.

This also adds a few new patches to fix more recent breakages. Unfortunately we still cannot enable the efi loader tests since one of the tests fails. Thus we should expect additional failures to appear until that is resolved.

Quoted 4 lines

Changes in v14:
- Fix condition for invalid pointer
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Any thoughts on this series please?

Regards,
Simon

#12 lore raw

v14 cover efi_loader: Code refactoring and improvement

Hi Heinrich,

Quoted 14 lines · Simon Glass

Hi Alex,

Quoted 10 lines · Simon Glass

This collects the patches previously sent to break up the very large functions in efi_loader into smaller pieces. Now that the other sandbox stuff is applied, perhaps it is time to apply these patches.

This also adds a few new patches to fix more recent breakages. Unfortunately we still cannot enable the efi loader tests since one of the tests fails. Thus we should expect additional failures to appear until that is resolved.

Quoted 6 lines
Quoted 4 lines

Changes in v14:
- Fix condition for invalid pointer
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Any thoughts on this series please?

Could you please take another look at this? I would very much like to put it to bed.

Regards,
Simon

#13 lore raw

v14 cover efi_loader: Code refactoring and improvement

Quoted 22 lines · Simon Glass

Hi Heinrich,

Quoted 19 lines · Simon Glass

Hi Alex,

Quoted 14 lines · Simon Glass

This collects the patches previously sent to break up the very large functions in efi_loader into smaller pieces. Now that the other sandbox stuff is applied, perhaps it is time to apply these patches.

This also adds a few new patches to fix more recent breakages. Unfortunately we still cannot enable the efi loader tests since one of the tests fails. Thus we should expect additional failures to appear until that is resolved.

Changes in v14:
- Fix condition for invalid pointer
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Quoted 8 lines
Quoted 1 line

Any thoughts on this series please?

Could you please take another look at this? I would very much like to put it to bed.

Regards,
Simon

Refactoring into shorter functions makes sense to me.

The errors I mentioned in the review comments for the individual patches were not introduced by you but we should not simply copy but correct them.

Best regards

Heinrich

#14 lore raw

v14 cover efi_loader: Code refactoring and improvement

Hi Heinrich,

Quoted 29 lines · Heinrich Schuchardt
Quoted 27 lines · Simon Glass

Hi Heinrich,

Quoted 14 lines · Simon Glass

Hi Alex,

Quoted 10 lines · Simon Glass

This collects the patches previously sent to break up the very large functions in efi_loader into smaller pieces. Now that the other sandbox stuff is applied, perhaps it is time to apply these patches.

This also adds a few new patches to fix more recent breakages. Unfortunately we still cannot enable the efi loader tests since one of the tests fails. Thus we should expect additional failures to appear until that is resolved.

Quoted 6 lines
Quoted 4 lines

Changes in v14:
- Fix condition for invalid pointer
- Go back to the horrible long variable names
- Hopefully correct error paths in do_bootefi_exec()

Any thoughts on this series please?

Could you please take another look at this? I would very much like to put it to bed.

Quoted 8 lines
Quoted 3 lines

Regards,
Simon

Refactoring into shorter functions makes sense to me.

The errors I mentioned in the review comments for the individual patches were not introduced by you but we should not simply copy but correct them.

I agree in principal, but within reason. It is easier in this case to add patches afterwards.

Regards,
Simon