Skip to contentSkip to conversation

Boot variables management for removable media

PATCH In review Raymond Mao 3 participants 13 messages Jun 19, 2023 – Sep 20, 2023

lore
Share & tools
lore
#0 lore raw

v10 cover Boot variables management for removable media

Major changes:
1.Rename and move bootorder and bootoption apis from cmd to lib
for re-use between eficonfig and bootmgr
2.Fix incorrect return code of boot option update
Correct the return code for out-of-memory and no boot option found
3.Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.
4.The boot variables automatically generated for removable medias
should be with short form of device path without device nodes. This is a requirement for the case that a removable media is plugged into a different port but is still able to work with the existing boot variables.

Raymond Mao (4):
Move bootorder and bootoption apis to lib Fix incorrect return code of boot option update Boot var automatic management for removable medias Load option with short device path for boot vars

 cmd/bootmenu.c               |   4 +-
 cmd/eficonfig.c              | 410 +----------------------------------
 include/efi_config.h         |   5 -
 include/efi_loader.h         |  11 +
 lib/efi_loader/efi_bootmgr.c | 385 ++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_disk.c    |  18 ++
 lib/efi_loader/efi_helper.c  |  25 +++
 lib/efi_loader/efi_setup.c   |   7 +
 8 files changed, 455 insertions(+), 410 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v10 1/4 Move bootorder and bootoption apis to lib

Rename and move bootorder and bootoption apis from cmd to lib for re-use between eficonfig and bootmgr
Fix 'unexpected indentation' when 'make htmldocs' after functions are moved

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v6
- Revert v3 changes in function efi_bootmgr_update_media_device_boot_option and efi_bootmgr_delete_invalid_boot_option
Changes in v8
- Fix 'unexpected indentation' when 'make htmldocs'

 cmd/bootmenu.c               |   2 +-
 cmd/eficonfig.c              | 408 +----------------------------------
 include/efi_config.h         |   5 -
 include/efi_loader.h         |  11 +
 lib/efi_loader/efi_bootmgr.c | 375 ++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_helper.c  |  25 +++
 6 files changed, 418 insertions(+), 408 deletions(-)
cmd/bootmenu.c +1 -1
index 6baeedc69f..01daddca7b 100644
@@ -351,7 +351,7 @@ static struct bootmenu_data *bootmenu_create(int delay)
 		 * UEFI specification requires booting from removal media using
 		 * a architecture-specific default image name such as BOOTAA64.EFI.
 		 */
-		efi_ret = eficonfig_generate_media_device_boot_option();
+		efi_ret = efi_bootmgr_update_media_device_boot_option();
 		if (efi_ret != EFI_SUCCESS && efi_ret != EFI_NOT_FOUND)
 			goto cleanup;
 
cmd/eficonfig.c +6 -402

474 lines not on this page · raw message

include/efi_config.h +0 -5
index 01ce9b2b06..d7c1601137 100644
@@ -105,11 +105,6 @@ efi_status_t eficonfig_process_common(struct efimenu *efi_menu,
 				      void (*item_data_print)(void *),
 				      char *(*item_choice)(void *));
 efi_status_t eficonfig_process_select_file(void *data);
-efi_status_t eficonfig_get_unused_bootoption(u16 *buf,
-					     efi_uintn_t buf_size, u32 *index);
-efi_status_t eficonfig_append_bootorder(u16 index);
-efi_status_t eficonfig_generate_media_device_boot_option(void);
-
 efi_status_t eficonfig_append_menu_entry(struct efimenu *efi_menu,
 					 char *title, eficonfig_entry_func func,
 					 void *data);
include/efi_loader.h +11 -0
index 1542b4b625..31ca1f5d1d 100644
@@ -516,6 +516,17 @@ extern struct list_head efi_register_notify_events;
 int efi_init_early(void);
 /* Initialize efi execution environment */
 efi_status_t efi_init_obj_list(void);
+/* Append new boot option in BootOrder variable */
+efi_status_t efi_bootmgr_append_bootorder(u16 index);
+/* Get unused "Boot####" index */
+efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf,
+					       efi_uintn_t buf_size, u32 *index);
+/* Generate the media device boot option */
+efi_status_t efi_bootmgr_update_media_device_boot_option(void);
+/* Delete selected boot option */
+efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index);
+/* search the boot option index in BootOrder */
+bool efi_search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index);
 /* Set up console modes */
 void efi_setup_console_size(void);
 /* Install device tree */
lib/efi_loader/efi_bootmgr.c +375 -0

378 lines not on this page · raw message

lib/efi_loader/efi_helper.c +25 -0
index 1f4ab2b419..cdfd16ea77 100644
@@ -257,3 +257,28 @@ efi_status_t efi_next_variable_name(efi_uintn_t *size, u16 **buf, efi_guid_t *gu
 
 	return ret;
 }
+
+/**
+ * efi_search_bootorder() - search the boot option index in BootOrder
+ *
+ * @bootorder:	pointer to the BootOrder variable
+ * @num:	number of BootOrder entry
+ * @target:	target boot option index to search
+ * @index:	pointer to store the index of BootOrder variable
+ * Return:	true if exists, false otherwise
+ */
+bool efi_search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index)
+{
+	u32 i;
+
+	for (i = 0; i < num; i++) {
+		if (target == bootorder[i]) {
+			if (index)
+				*index = i;
+
+			return true;
+		}
+	}
+
+	return false;
+}
Signature · 1 line
2.25.1
#2 lore raw

PATCH v10 2/4 Fix incorrect return code of boot option update

In reply to Raymond Mao

Correct the return code for out-of-memory and no boot option found

---
Changes in v7
- new patch file created

 cmd/bootmenu.c               | 2 +-
 cmd/eficonfig.c              | 2 +-
 lib/efi_loader/efi_bootmgr.c | 8 ++++++--
 3 files changed, 8 insertions(+), 4 deletions(-)
cmd/bootmenu.c +1 -1
index 01daddca7b..987b16889f 100644
@@ -352,7 +352,7 @@ static struct bootmenu_data *bootmenu_create(int delay)
 		 * a architecture-specific default image name such as BOOTAA64.EFI.
 		 */
 		efi_ret = efi_bootmgr_update_media_device_boot_option();
-		if (efi_ret != EFI_SUCCESS && efi_ret != EFI_NOT_FOUND)
+		if (efi_ret != EFI_SUCCESS)
 			goto cleanup;
 
 		ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
cmd/eficonfig.c +1 -1
index 82a80306f4..e6e8a0a488 100644
@@ -2314,7 +2314,7 @@ static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const a
 		return CMD_RET_FAILURE;
 
 	ret = efi_bootmgr_update_media_device_boot_option();
-	if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+	if (ret != EFI_SUCCESS)
 		return ret;
 
 	while (1) {
lib/efi_loader/efi_bootmgr.c +6 -2
index 97d5b8dc2b..28e800499c 100644
@@ -663,11 +663,13 @@ efi_status_t efi_bootmgr_update_media_device_boot_option(void)
 					   NULL, &count,
 					   (efi_handle_t **)&volume_handles);
 	if (ret != EFI_SUCCESS)
-		return ret;
+		goto out;
 
 	opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
-	if (!opt)
+	if (!opt) {
+		ret = EFI_OUT_OF_RESOURCES;
 		goto out;
+	}
 
 	/* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
 	ret = efi_bootmgr_enumerate_boot_option(opt, volume_handles, count);
@@ -720,5 +722,7 @@ out:
 	free(opt);
 	efi_free_pool(volume_handles);
 
+	if (ret == EFI_NOT_FOUND)
+		return EFI_SUCCESS;
 	return ret;
 }
Signature · 1 line
2.25.1
#3 lore raw

PATCH v10 3/4 Boot var automatic management for removable medias

In reply to Raymond Mao

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list()
Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

 lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++
 lib/efi_loader/efi_setup.c |  7 +++++++
 2 files changed, 25 insertions(+)
lib/efi_loader/efi_disk.c +18 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event *event)
 			return -1;
 	}
 
+	/* only do the boot option management when UEFI sub-system is initialized */
+	if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && efi_obj_list_initialized == EFI_SUCCESS) {
+		ret = efi_bootmgr_update_media_device_boot_option();
+		if (ret != EFI_SUCCESS)
+			return -1;
+	}
+
 	return 0;
 }
 
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event *event)
 		return efi_disk_delete_part(dev);
 	else
 		return 0;
+
+	/*
+	 * TODO A flag to distinguish below 2 different scenarios of this
+	 * function call is needed:
+	 * a) Unplugging of a removable media under U-Boot
+	 * b) U-Boot exiting and booting an OS
+	 * In case of scenario a), efi_bootmgr_update_media_device_boot_option()
+	 * needs to be invoked here to update the boot options and remove the
+	 * unnecessary ones.
+	 */
+
 }
 
 /**
lib/efi_loader/efi_setup.c +7 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
 	if (ret != EFI_SUCCESS)
 		goto out;
 
+	if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+		/* update boot option after variable service initialized */
+		ret = efi_bootmgr_update_media_device_boot_option();
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
 	/* Define supported languages */
 	ret = efi_init_platform_lang();
 	if (ret != EFI_SUCCESS)
Signature · 1 line
2.25.1
Reviewed-by on v10 3/4
#4 lore raw

Re: [PATCH v10 3/4] Boot var automatic management for removable medias

Quoted 62 lines · Raymond Mao

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list()
Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

  lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++
  lib/efi_loader/efi_setup.c |  7 +++++++
  2 files changed, 25 insertions(+)
lib/efi_loader/efi_disk.c +7 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event *event)
  			return -1;
  	}
 
+	/* only do the boot option management when UEFI sub-system is initialized */
+	if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && efi_obj_list_initialized == EFI_SUCCESS) {
+		ret = efi_bootmgr_update_media_device_boot_option();
+		if (ret != EFI_SUCCESS)
+			return -1;
+	}
+
  	return 0;
  }
 
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event *event)
  		return efi_disk_delete_part(dev);
  	else
  		return 0;

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event) · line 792

Quoted 10 lines
lib/efi_loader/efi_disk.c +10 -0
+
+	/*
+	 * TODO A flag to distinguish below 2 different scenarios of this
+	 * function call is needed:
+	 * a) Unplugging of a removable media under U-Boot
+	 * b) U-Boot exiting and booting an OS
+	 * In case of scenario a), efi_bootmgr_update_media_device_boot_option()
+	 * needs to be invoked here to update the boot options and remove the
+	 * unnecessary ones.
+	 */

As in future we want to integrate more EFI drivers with the driver model such a status should be in a global variable. It will have to be set in ExitBootServices() before invoking dm_remove_devices_flags().

Quoted 22 lines
lib/efi_loader/efi_disk.c +1 -0
+
  }
 
  /**
lib/efi_loader/efi_setup.c +7 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
  	if (ret != EFI_SUCCESS)
  		goto out;
 
+	if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+		/* update boot option after variable service initialized */
+		ret = efi_bootmgr_update_media_device_boot_option();
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
  	/* Define supported languages */
  	ret = efi_init_platform_lang();
  	if (ret != EFI_SUCCESS)
#5 lore raw

Re: [PATCH v10 3/4] Boot var automatic management for removable medias

Quoted 94 lines · Heinrich Schuchardt
Quoted 66 lines · Raymond Mao

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list() Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

  lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++ lib/efi_loader/efi_setup.c |  7 +++++++

  2 files changed, 25 insertions(+)
lib/efi_loader/efi_disk.c +1 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event *event)
               return -1;
       }
+    /* only do the boot option management when UEFI sub-system is

initialized */
+    if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && efi_obj_list_initialized == EFI_SUCCESS) {
+        ret = efi_bootmgr_update_media_device_boot_option(); +        if (ret != EFI_SUCCESS)
+            return -1;
+    }
+
      return 0;
  }

lib/efi_loader/efi_disk.c +0 -0
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event *event)
           return efi_disk_delete_part(dev);

      else
          return 0;
+
+    /*
+     * TODO A flag to distinguish below 2 different scenarios of this +     * function call is needed:

Quoted 8 lines

+     * a) Unplugging of a removable media under U-Boot
+     * b) U-Boot exiting and booting an OS
+     * In case of scenario a), efi_bootmgr_update_media_device_boot_option()
+     * needs to be invoked here to update the boot options and remove the
+     * unnecessary ones.
+     */

As in future we want to integrate more EFI drivers with the driver model such a status should be in a global variable. It will have to be set in ExitBootServices() before invoking dm_remove_devices_flags().

Quoted 12 lines

+
  }
  /**

lib/efi_loader/efi_setup.c +2 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
       if (ret != EFI_SUCCESS)
           goto out;
+    if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+        /* update boot option after variable service initialized */
Quoted 9 lines

lib/efi_loader/efi_setup.c efi_status_t efi_init_obj_list(void) · line 257

Quoted 8 lines
lib/efi_loader/efi_setup.c +5 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            goto out;
+    }
+
       /* Define supported languages */
       ret = efi_init_platform_lang();
       if (ret != EFI_SUCCESS)

This patch leads to a test failure in

test/py/tests/test_efi_secboot/test_signed.py::TestEfiSignedImage::test_efi_signed_image_auth2

No EFI system partition
Failed to persist EFI variables

Please, run 'make tests' before resubmitting.

Best regards

Heinrich

#6 lore raw

Re: [PATCH v10 3/4] Boot var automatic management for removable medias

Hi Heinrich,

I run 'make tests' with the patches locally but no errors observed from 'test_signed.py', below are few lines from the output console:

```
test/py/tests/test_efi_secboot/test_signed.py ssssssss

                                             [      90%]
```

Regards,
Raymond

Quoted 119 lines · Heinrich Schuchardt
Quoted 94 lines · Heinrich Schuchardt
Quoted 72 lines · Raymond Mao

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list()
Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

  lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++
  lib/efi_loader/efi_setup.c |  7 +++++++
  2 files changed, 25 insertions(+)
lib/efi_loader/efi_disk.c +1 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event *event)
              return -1;
      }
+    /* only do the boot option management when UEFI sub-system is

initialized */
+ if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
efi_obj_list_initialized == EFI_SUCCESS) {

lib/efi_loader/efi_disk.c +13 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            return -1;
+    }
+
      return 0;
  }
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event *event)
          return efi_disk_delete_part(dev);
      else
          return 0;
+
+    /*
+     * TODO A flag to distinguish below 2 different scenarios of this
+     * function call is needed:
+     * a) Unplugging of a removable media under U-Boot
+     * b) U-Boot exiting and booting an OS
+     * In case of scenario a),
 efi_bootmgr_update_media_device_boot_option()
+     * needs to be invoked here to update the boot options and remove

the

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event) · line 792

Quoted 2 lines
lib/efi_loader/efi_disk.c +2 -0
+     * unnecessary ones.
+     */

As in future we want to integrate more EFI drivers with the driver model such a status should be in a global variable. It will have to be set in ExitBootServices() before invoking dm_remove_devices_flags().

Quoted 12 lines
lib/efi_loader/efi_disk.c +1 -0
+
  }
  /**
lib/efi_loader/efi_setup.c +2 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
      if (ret != EFI_SUCCESS)
          goto out;
+    if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+        /* update boot option after variable service initialized */
Quoted 9 lines

lib/efi_loader/efi_setup.c efi_status_t efi_init_obj_list(void) · line 257

Quoted 8 lines
lib/efi_loader/efi_setup.c +5 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            goto out;
+    }
+
      /* Define supported languages */
      ret = efi_init_platform_lang();
      if (ret != EFI_SUCCESS)

This patch leads to a test failure in

test/py/tests/test_efi_secboot/test_signed.py::TestEfiSignedImage::test_efi_signed_image_auth2

No EFI system partition
Failed to persist EFI variables

Please, run 'make tests' before resubmitting.

Best regards

Heinrich

#7 lore raw

Re: [PATCH v10 3/4] Boot var automatic management for removable medias

Hi Raymond,

Quoted 7 lines · Raymond Mao

Hi Heinrich,

I run 'make tests' with the patches locally but no errors observed from 'test_signed.py', below are few lines from the output console:

```
test/py/tests/test_efi_secboot/test_signed.py ssssssss                                                                                                                                              [      90%]
```

I think the test cases are just skipped, not executed.

I guess that some required packages are missing on the host machine, such as sbsigntool, efitools and libguestfs-tools. build-sandbox/test-log.html will help to analyze why the tests are skipped.

The following document describes prerequisites to run python tests.
https://u-boot.readthedocs.io/en/latest/develop/py_testing.html

'make tests' runs many test cases and takes time. './test/py/test.py --bd sandbox --build -k efi_secboot' will run only the efi_secboot test
and it will be useful for debugging purposes.

Thanks,
Masahisa Kojima

Quoted 123 lines

Regards,
Raymond

Quoted 118 lines · Heinrich Schuchardt
Quoted 94 lines · Heinrich Schuchardt
Quoted 72 lines · Raymond Mao

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list()
Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

  lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++
  lib/efi_loader/efi_setup.c |  7 +++++++
  2 files changed, 25 insertions(+)
lib/efi_loader/efi_disk.c +1 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event *event)
              return -1;
      }
+    /* only do the boot option management when UEFI sub-system is

initialized */
+ if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
efi_obj_list_initialized == EFI_SUCCESS) {

lib/efi_loader/efi_disk.c +13 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            return -1;
+    }
+
      return 0;
  }
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event *event)
          return efi_disk_delete_part(dev);
      else
          return 0;
+
+    /*
+     * TODO A flag to distinguish below 2 different scenarios of this
+     * function call is needed:
+     * a) Unplugging of a removable media under U-Boot
+     * b) U-Boot exiting and booting an OS
+     * In case of scenario a),
 efi_bootmgr_update_media_device_boot_option()
+     * needs to be invoked here to update the boot options and remove

the

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event) · line 792

Quoted 2 lines
lib/efi_loader/efi_disk.c +2 -0
+     * unnecessary ones.
+     */

As in future we want to integrate more EFI drivers with the driver model such a status should be in a global variable. It will have to be set in ExitBootServices() before invoking dm_remove_devices_flags().

Quoted 12 lines
lib/efi_loader/efi_disk.c +1 -0
+
  }
  /**
lib/efi_loader/efi_setup.c +2 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
      if (ret != EFI_SUCCESS)
          goto out;
+    if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+        /* update boot option after variable service initialized */
Quoted 9 lines

lib/efi_loader/efi_setup.c efi_status_t efi_init_obj_list(void) · line 257

Quoted 8 lines
lib/efi_loader/efi_setup.c +5 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            goto out;
+    }
+
      /* Define supported languages */
      ret = efi_init_platform_lang();
      if (ret != EFI_SUCCESS)

This patch leads to a test failure in

test/py/tests/test_efi_secboot/test_signed.py::TestEfiSignedImage::test_efi_signed_image_auth2

No EFI system partition
Failed to persist EFI variables

Please, run 'make tests' before resubmitting.

Best regards

Heinrich

#8 lore raw

Re: [PATCH v10 3/4] Boot var automatic management for removable medias

Quoted 20 lines · Masahisa Kojima

Hi Raymond,

Quoted 7 lines · Raymond Mao

Hi Heinrich,

I run 'make tests' with the patches locally but no errors observed from 'test_signed.py', below are few lines from the output console:

```
test/py/tests/test_efi_secboot/test_signed.py ssssssss                                                                                                                                              [      90%]
```

I think the test cases are just skipped, not executed.

I guess that some required packages are missing on the host machine, such as sbsigntool, efitools and libguestfs-tools. build-sandbox/test-log.html will help to analyze why the tests are skipped.

The following document describes prerequisites to run python tests.
https://u-boot.readthedocs.io/en/latest/develop/py_testing.html

Quoted 7 lines

'make tests' runs many test cases and takes time. './test/py/test.py --bd sandbox --build -k efi_secboot' will run only the efi_secboot test
and it will be useful for debugging purposes.

Thanks,
Masahisa Kojima

To get more output describing why tests are skipped or fail use

export PYTEST_ADDOPTS="-ra"

https://source.denx.de/u-boot/custodians/u-boot-efi/-/pipelines/16948
shows the errors with the suggested patch.

Best regards

Heinrich

Quoted 125 lines
Quoted 125 lines

Regards,
Raymond

Quoted 118 lines · Heinrich Schuchardt
Quoted 94 lines · Heinrich Schuchardt
Quoted 72 lines · Raymond Mao

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list()
Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

   lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++
   lib/efi_loader/efi_setup.c |  7 +++++++
   2 files changed, 25 insertions(+)
lib/efi_loader/efi_disk.c +1 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event *event)
               return -1;
       }
+    /* only do the boot option management when UEFI sub-system is

initialized */
+ if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
efi_obj_list_initialized == EFI_SUCCESS) {

lib/efi_loader/efi_disk.c +13 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            return -1;
+    }
+
       return 0;
   }
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event *event)
           return efi_disk_delete_part(dev);
       else
           return 0;
+
+    /*
+     * TODO A flag to distinguish below 2 different scenarios of this
+     * function call is needed:
+     * a) Unplugging of a removable media under U-Boot
+     * b) U-Boot exiting and booting an OS
+     * In case of scenario a),
 efi_bootmgr_update_media_device_boot_option()
+     * needs to be invoked here to update the boot options and remove

the

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event) · line 792

Quoted 2 lines
lib/efi_loader/efi_disk.c +2 -0
+     * unnecessary ones.
+     */

As in future we want to integrate more EFI drivers with the driver model such a status should be in a global variable. It will have to be set in ExitBootServices() before invoking dm_remove_devices_flags().

Quoted 12 lines
lib/efi_loader/efi_disk.c +1 -0
+
   }
   /**
lib/efi_loader/efi_setup.c +2 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
       if (ret != EFI_SUCCESS)
           goto out;
+    if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+        /* update boot option after variable service initialized */
Quoted 9 lines

lib/efi_loader/efi_setup.c efi_status_t efi_init_obj_list(void) · line 257

Quoted 8 lines
lib/efi_loader/efi_setup.c +5 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            goto out;
+    }
+
       /* Define supported languages */
       ret = efi_init_platform_lang();
       if (ret != EFI_SUCCESS)

This patch leads to a test failure in

test/py/tests/test_efi_secboot/test_signed.py::TestEfiSignedImage::test_efi_signed_image_auth2

No EFI system partition
Failed to persist EFI variables

Please, run 'make tests' before resubmitting.

Best regards

Heinrich

#9 lore raw

Re: [PATCH v10 3/4] Boot var automatic management for removable medias

Hi Heinrich,

I checkouted the 'origin/removable_medias' branch from the 'EFI U-Boot Custodian Tree'.
Run the secureboot test suite on the sandbox locally by './test/py/test.py --bd sandbox --build -k efi_secboot'.
But there are no errors observed for 'test_signed.py'.
Below is the output logs:

```
./test/py/test.py --bd sandbox --build -k efi_secboot
+make O=/home/raymond/upstream/U-Boot-custodian/u-boot-efi/build-sandbox -s
sandbox_defconfig
+make O=/home/raymond/upstream/U-Boot-custodian/u-boot-efi/build-sandbox -s
-j2
===========================================================================================
test session starts
===========================================================================================
platform linux -- Python 3.10.9, pytest-6.2.5, py-1.10.0, pluggy-0.13.0
rootdir: /home/raymond/upstream/U-Boot-custodian/u-boot-efi/test/py,
configfile: pytest.ini
plugins: xdist-2.5.0, forked-1.6.0
collected 1147 items / 1128 deselected / 19 selected



test/py/tests/test_efi_secboot/test_authvar.py .....

                                             [ 26%]
test/py/tests/test_efi_secboot/test_signed.py ........

                                             [ 68%]
test/py/tests/test_efi_secboot/test_signed_intca.py ...

                                            [ 84%]
test/py/tests/test_efi_secboot/test_unsigned.py ...

                                            [100%]

=============================================================================
19 passed, 1128 deselected in 78.28s (0:01:18)
==============================================================================
```

Is it a pipeline issue? I didn't find a button to retrigger the failed jobs. Do I have the permissions?

The HEAD of my testing workspace is on:

```
commit c88c134c29934654a696aef61ba63ff2c9a8481e (HEAD -> removable_medias,
origin/removable_medias)
Author: Raymond Mao <[email protected]>
Date:   Mon Jun 19 14:23:00 2023 -0700

    Boot var automatic management for removable medias
```

Same as the one for pipeline:
https://source.denx.de/u-boot/custodians/u-boot-efi/-/pipelines/16948

Thanks!
Regards,
Raymond

Quoted 180 lines · Heinrich Schuchardt
Quoted 3 lines · Masahisa Kojima

Hi Raymond,

On Tue, 18 Jul 2023 at 05:23, Raymond Mao <[email protected]>

Quoted 4 lines · wrote:
Quoted 4 lines

Hi Heinrich,

I run 'make tests' with the patches locally but no errors observed from

'test_signed.py', below are few lines from the output console:

Quoted 2 lines
Quoted 2 lines

``` test/py/tests/test_efi_secboot/test_signed.py ssssssss

                                                [      90%]
Quoted 7 lines
Quoted 1 line

```

I think the test cases are just skipped, not executed.

I guess that some required packages are missing on the host machine, such as sbsigntool, efitools and libguestfs-tools. build-sandbox/test-log.html will help to analyze why the tests are

skipped.

Quoted 11 lines

The following document describes prerequisites to run python tests.
https://u-boot.readthedocs.io/en/latest/develop/py_testing.html

'make tests' runs many test cases and takes time. './test/py/test.py --bd sandbox --build -k efi_secboot' will run only the efi_secboot test
and it will be useful for debugging purposes.

Thanks,
Masahisa Kojima

To get more output describing why tests are skipped or fail use

export PYTEST_ADDOPTS="-ra"

https://source.denx.de/u-boot/custodians/u-boot-efi/-/pipelines/16948
shows the errors with the suggested patch.

Best regards

Heinrich

Quoted 7 lines
Quoted 7 lines

Regards,
Raymond

On Sat, 15 Jul 2023 at 05:19, Heinrich Schuchardt <[email protected]>

Quoted 43 lines · wrote:
Quoted 43 lines
Quoted 43 lines · Heinrich Schuchardt
Quoted 41 lines · Raymond Mao
Quoted 40 lines

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list()
Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

   lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++
   lib/efi_loader/efi_setup.c |  7 +++++++
   2 files changed, 25 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

lib/efi_loader/efi_disk.c int efi_disk_probe(void *ctx, struct event

Quoted 5 lines
lib/efi_loader/efi_disk.c +0 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event

*event)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
lib/efi_loader/efi_disk.c +1 -0
               return -1;
       }
+    /* only do the boot option management when UEFI sub-system is

initialized */
+ if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
efi_obj_list_initialized == EFI_SUCCESS) {

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event

Quoted 8 lines
lib/efi_loader/efi_disk.c +5 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            return -1;
+    }
+
       return 0;
   }
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event

*event)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event)

Quoted 6 lines
lib/efi_loader/efi_disk.c +3 -0
           return efi_disk_delete_part(dev);
       else
           return 0;
+
+    /*
+     * TODO A flag to distinguish below 2 different scenarios of

this

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event)

Quoted 6 lines
lib/efi_loader/efi_disk.c +5 -0
+     * function call is needed:
+     * a) Unplugging of a removable media under U-Boot
+     * b) U-Boot exiting and booting an OS
+     * In case of scenario a),
 efi_bootmgr_update_media_device_boot_option()
+     * needs to be invoked here to update the boot options and

remove

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event) · line 792

Quoted 3 lines

the

lib/efi_loader/efi_disk.c +2 -0
+     * unnecessary ones.
+     */

As in future we want to integrate more EFI drivers with the driver

model

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

such a status should be in a global variable. It will have to be set

in

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines

ExitBootServices() before invoking dm_remove_devices_flags().

Quoted 20 lines
lib/efi_loader/efi_disk.c +1 -0
+
   }
   /**
lib/efi_loader/efi_setup.c +7 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
       if (ret != EFI_SUCCESS)
           goto out;
+    if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+        /* update boot option after variable service initialized */
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            goto out;
+    }
+
       /* Define supported languages */
       ret = efi_init_platform_lang();
       if (ret != EFI_SUCCESS)
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

This patch leads to a test failure in

test/py/tests/test_efi_secboot/test_signed.py::TestEfiSignedImage::test_efi_signed_image_auth2

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

No EFI system partition
Failed to persist EFI variables

Please, run 'make tests' before resubmitting.

Best regards

Heinrich

#10 lore raw

Re: [PATCH v10 3/4] Boot var automatic management for removable medias

Hi Raymond, Heinrich, Ilias,

Quoted 33 lines · Raymond Mao

Hi Heinrich,

I checkouted the 'origin/removable_medias' branch from the 'EFI U-Boot Custodian Tree'.
Run the secureboot test suite on the sandbox locally by './test/py/test.py --bd sandbox --build -k efi_secboot'. But there are no errors observed for 'test_signed.py'.
Below is the output logs:
```
./test/py/test.py --bd sandbox --build -k efi_secboot

(quoted diff) +2 -0
+make O=/home/raymond/upstream/U-Boot-custodian/u-boot-efi/build-sandbox -s sandbox_defconfig
+make O=/home/raymond/upstream/U-Boot-custodian/u-boot-efi/build-sandbox -s -j2

=========================================================================================== test session starts =========================================================================================== platform linux -- Python 3.10.9, pytest-6.2.5, py-1.10.0, pluggy-0.13.0
rootdir: /home/raymond/upstream/U-Boot-custodian/u-boot-efi/test/py, configfile: pytest.ini
plugins: xdist-2.5.0, forked-1.6.0
collected 1147 items / 1128 deselected / 19 selected

test/py/tests/test_efi_secboot/test_authvar.py .....                                                                                                                                                [ 26%]
test/py/tests/test_efi_secboot/test_signed.py ........                                                                                                                                              [ 68%]
test/py/tests/test_efi_secboot/test_signed_intca.py ...                                                                                                                                             [ 84%]
test/py/tests/test_efi_secboot/test_unsigned.py ...                                                                                                                                                 [100%]

============================================================================= 19 passed, 1128 deselected in 78.28s (0:01:18) ==============================================================================

```
Is it a pipeline issue? I didn't find a button to retrigger the failed jobs. Do I have the permissions?


The HEAD of my testing workspace is on:
```
commit c88c134c29934654a696aef61ba63ff2c9a8481e (HEAD -> removable_medias, origin/removable_medias)
Author: Raymond Mao <[email protected]>
Date:   Mon Jun 19 14:23:00 2023 -0700
Quoted 3 lines

Boot var automatic management for removable medias
```
Same as the one for pipeline: https://source.denx.de/u-boot/custodians/u-boot-efi/-/pipelines/16948

I could reproduce the error on efi_secboot tests and find the cause.

The efi_secboot tests expect that BootOrder is not defined.
WIth this "Boot var automatic management for removable medias" patch, BootOrder is added when the disk is detected.
In my testing, the BootOrder variable has two entries and the test ends up with an unexpected failure since efibootmgr falls back to BootOrder when loading from BootNext failed.

===
Loading from BootNext failed, falling back to BootOrder Loading Boot0000 'mmc 1:1' failed
Loading Boot0001 'host 0:1' failed efi_bootmgr_load() returned: 14 efi_start_image() returned: 2
===

I think we need to clear the BootOrder variable at the beginning of tests. I will resend this patch with efi_secboot pytest modification.

Thanks,
Masahisa Kojima

Quoted 173 lines

Thanks!
Regards,
Raymond

Quoted 167 lines · Heinrich Schuchardt
Quoted 20 lines · Masahisa Kojima

Hi Raymond,

Quoted 7 lines · Raymond Mao

Hi Heinrich,

I run 'make tests' with the patches locally but no errors observed from 'test_signed.py', below are few lines from the output console:

```
test/py/tests/test_efi_secboot/test_signed.py ssssssss                                                                                                                                              [      90%]
```

I think the test cases are just skipped, not executed.

I guess that some required packages are missing on the host machine, such as sbsigntool, efitools and libguestfs-tools. build-sandbox/test-log.html will help to analyze why the tests are skipped.

The following document describes prerequisites to run python tests.
https://u-boot.readthedocs.io/en/latest/develop/py_testing.html

Quoted 7 lines

'make tests' runs many test cases and takes time. './test/py/test.py --bd sandbox --build -k efi_secboot' will run only the efi_secboot test
and it will be useful for debugging purposes.

Thanks,
Masahisa Kojima

To get more output describing why tests are skipped or fail use

export PYTEST_ADDOPTS="-ra"

https://source.denx.de/u-boot/custodians/u-boot-efi/-/pipelines/16948
shows the errors with the suggested patch.

Best regards

Heinrich

Quoted 125 lines
Quoted 125 lines

Regards,
Raymond

Quoted 118 lines · Heinrich Schuchardt
Quoted 94 lines · Heinrich Schuchardt
Quoted 72 lines · Raymond Mao

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list()
Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

   lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++
   lib/efi_loader/efi_setup.c |  7 +++++++
   2 files changed, 25 insertions(+)
lib/efi_loader/efi_disk.c +1 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event *event)
               return -1;
       }
+    /* only do the boot option management when UEFI sub-system is

initialized */
+ if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
efi_obj_list_initialized == EFI_SUCCESS) {

lib/efi_loader/efi_disk.c +13 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            return -1;
+    }
+
       return 0;
   }
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event *event)
           return efi_disk_delete_part(dev);
       else
           return 0;
+
+    /*
+     * TODO A flag to distinguish below 2 different scenarios of this
+     * function call is needed:
+     * a) Unplugging of a removable media under U-Boot
+     * b) U-Boot exiting and booting an OS
+     * In case of scenario a),
 efi_bootmgr_update_media_device_boot_option()
+     * needs to be invoked here to update the boot options and remove

the

lib/efi_loader/efi_disk.c

Quoted 2 lines
lib/efi_loader/efi_disk.c +2 -0
+     * unnecessary ones.
+     */

As in future we want to integrate more EFI drivers with the driver model such a status should be in a global variable. It will have to be set in ExitBootServices() before invoking dm_remove_devices_flags().

Quoted 12 lines
lib/efi_loader/efi_disk.c +1 -0
+
   }
   /**
lib/efi_loader/efi_setup.c +2 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
       if (ret != EFI_SUCCESS)
           goto out;
+    if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+        /* update boot option after variable service initialized */
Quoted 9 lines

lib/efi_loader/efi_setup.c efi_status_t efi_init_obj_list(void) · line 256

Quoted 8 lines
lib/efi_loader/efi_setup.c +5 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            goto out;
+    }
+
       /* Define supported languages */
       ret = efi_init_platform_lang();
       if (ret != EFI_SUCCESS)

This patch leads to a test failure in

test/py/tests/test_efi_secboot/test_signed.py::TestEfiSignedImage::test_efi_signed_image_auth2

No EFI system partition
Failed to persist EFI variables

Please, run 'make tests' before resubmitting.

Best regards

Heinrich

#11 lore raw

Re: [PATCH v10 3/4] Boot var automatic management for removable medias

In reply to Masahisa Kojima

Hi Masahisa,

Thanks for the hints. Found that I was missing 'guestfs-tools' for 'virt-make-fs'.
Now I am able to reproduce the failure logs that Heinrich pointed out.

Thanks!
Regards,
Raymond

Quoted 164 lines · Masahisa Kojima

Hi Raymond,

Quoted 4 lines · Raymond Mao

Hi Heinrich,

I run 'make tests' with the patches locally but no errors observed from

'test_signed.py', below are few lines from the output console:

Quoted 2 lines

``` test/py/tests/test_efi_secboot/test_signed.py ssssssss

                                                [      90%]
Quoted 1 line

```

I think the test cases are just skipped, not executed.

I guess that some required packages are missing on the host machine, such as sbsigntool, efitools and libguestfs-tools. build-sandbox/test-log.html will help to analyze why the tests are skipped.

The following document describes prerequisites to run python tests.
https://u-boot.readthedocs.io/en/latest/develop/py_testing.html

'make tests' runs many test cases and takes time. './test/py/test.py --bd sandbox --build -k efi_secboot' will run only the efi_secboot test
and it will be useful for debugging purposes.

Thanks,
Masahisa Kojima

Quoted 5 lines

Regards,
Raymond

On Sat, 15 Jul 2023 at 05:19, Heinrich Schuchardt <[email protected]>

Quoted 43 lines · wrote:
Quoted 43 lines · Heinrich Schuchardt
Quoted 41 lines · Raymond Mao
Quoted 40 lines

Changes for complying to EFI spec §3.5.1.1
'Removable Media Boot Behavior'.
Boot variables can be automatically generated during a removable media is probed. At the same time, unused boot variables will be detected and removed.

Please note that currently the function 'efi_disk_remove' has no ability to distinguish below two scenarios
a) Unplugging of a removable media under U-Boot
b) U-Boot exiting and booting an OS
Thus currently the boot variables management is not added into 'efi_disk_remove' to avoid boot options being added/erased repeatedly under scenario b) during power cycles
See TODO comments under function 'efi_disk_remove' for more details

---
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v5
- Move function call of efi_bootmgr_update_media_device_boot_option() from efi_init_variables() to efi_init_obj_list()
Changes in v6
- Revert unrelated changes
Changes in v7
- adapt the return code of function efi_bootmgr_update_media_device_boot_option()
Changes in v8
- add a note in the commit message for future reference Changes in v9
- amend the note text in the commit message
- add a TODO comment in 'efi_disk_remove'
Changes in v10
- fix typo and build failures with 'CONFIG_CMD_BOOTEFI_BOOTMGR=n'

  lib/efi_loader/efi_disk.c  | 18 ++++++++++++++++++
  lib/efi_loader/efi_setup.c |  7 +++++++
  2 files changed, 25 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

lib/efi_loader/efi_disk.c int efi_disk_probe(void *ctx, struct event

Quoted 5 lines
lib/efi_loader/efi_disk.c +0 -0
index d2256713a8..911a4adfb1 100644
@@ -687,6 +687,13 @@ int efi_disk_probe(void *ctx, struct event

*event)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
lib/efi_loader/efi_disk.c +1 -0
              return -1;
      }
+    /* only do the boot option management when UEFI sub-system is

initialized */
+ if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
efi_obj_list_initialized == EFI_SUCCESS) {

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event

Quoted 8 lines
lib/efi_loader/efi_disk.c +5 -0
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            return -1;
+    }
+
      return 0;
  }
@@ -773,6 +780,17 @@ int efi_disk_remove(void *ctx, struct event

*event)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event)

Quoted 6 lines
lib/efi_loader/efi_disk.c +3 -0
          return efi_disk_delete_part(dev);
      else
          return 0;
+
+    /*
+     * TODO A flag to distinguish below 2 different scenarios of

this

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event)

Quoted 6 lines
lib/efi_loader/efi_disk.c +5 -0
+     * function call is needed:
+     * a) Unplugging of a removable media under U-Boot
+     * b) U-Boot exiting and booting an OS
+     * In case of scenario a),
 efi_bootmgr_update_media_device_boot_option()
+     * needs to be invoked here to update the boot options and

remove

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

lib/efi_loader/efi_disk.c int efi_disk_remove(void *ctx, struct event *event) · line 792

Quoted 3 lines

the

lib/efi_loader/efi_disk.c +2 -0
+     * unnecessary ones.
+     */

As in future we want to integrate more EFI drivers with the driver

model

Quoted 1 line
Quoted 1 line
Quoted 1 line

such a status should be in a global variable. It will have to be set

in

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines

ExitBootServices() before invoking dm_remove_devices_flags().

Quoted 20 lines
lib/efi_loader/efi_disk.c +1 -0
+
  }
  /**
lib/efi_loader/efi_setup.c +7 -0
index 58d4e13402..69c8b27730 100644
@@ -245,6 +245,13 @@ efi_status_t efi_init_obj_list(void)
      if (ret != EFI_SUCCESS)
          goto out;
+    if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
+        /* update boot option after variable service initialized */
+        ret = efi_bootmgr_update_media_device_boot_option();
+        if (ret != EFI_SUCCESS)
+            goto out;
+    }
+
      /* Define supported languages */
      ret = efi_init_platform_lang();
      if (ret != EFI_SUCCESS)
Quoted 3 lines
Quoted 3 lines

This patch leads to a test failure in

test/py/tests/test_efi_secboot/test_signed.py::TestEfiSignedImage::test_efi_signed_image_auth2

Quoted 9 lines
Quoted 9 lines

No EFI system partition
Failed to persist EFI variables

Please, run 'make tests' before resubmitting.

Best regards

Heinrich

#12 lore raw

PATCH v10 4/4 Load option with short device path for boot vars

In reply to Raymond Mao

The boot variables automatically generated for removable medias should be with short form of device path without device nodes. This is a requirement for the case that a removable media is plugged into a different port but is still able to work with the existing boot variables.

---
Changes in v2
- Ignore EFI_NOT_FOUND returned from efi_bootmgr_update_media_device_boot_option which means no boot options scanned.
Changes in v3
- Split the patch into moving and renaming functions and individual patches for each changed functionality
Changes in v4
- Revert v2 change of introducing a bool parameter when updating the boot option. Use short-form of device path by default
Changes in v5
- Add warning log when a short-form device path doesn't exist Changes in v6
- Revert v5 change and use original device path if short-form does not exist

 lib/efi_loader/efi_bootmgr.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/efi_loader/efi_bootmgr.c +6 -0
index 28e800499c..398b0f89e2 100644
@@ -371,6 +371,7 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
 		struct efi_load_option lo;
 		char buf[BOOTMENU_DEVICE_NAME_MAX];
 		struct efi_device_path *device_path;
+		struct efi_device_path *short_dp;
 
 		ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
 		if (ret != EFI_SUCCESS)
@@ -387,6 +388,11 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
 		p = dev_name;
 		utf8_utf16_strncpy(&p, buf, strlen(buf));
 
+		/* prefer to short form device path */
+		short_dp = efi_dp_shorten(device_path);
+		if (short_dp)
+			device_path = short_dp;
+
 		lo.label = dev_name;
 		lo.attributes = LOAD_OPTION_ACTIVE;
 		lo.file_path = device_path;
Signature · 1 line
2.25.1