Skip to contentSkip to conversation

Move bootorder and bootoption apis to lib

PATCH Superseded Raymond Mao 3 participants 11 messages May 23, 2023 – May 26, 2023

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/3 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

---
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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

 cmd/bootmenu.c               |   2 +-
 cmd/eficonfig.c              | 408 +----------------------------------
 include/efi_config.h         |   5 -
 include/efi_loader.h         |  11 +
 lib/efi_loader/efi_bootmgr.c | 380 ++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_helper.c  |  25 +++
 6 files changed, 423 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 +380 -0

383 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
#1 lore raw

PATCH v4 2/3 Boot var automatic management for removable medias

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.

---
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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

 lib/efi_loader/efi_disk.c         |  7 +++++++
 lib/efi_loader/efi_variable.c     | 10 +++++++++-
 lib/efi_loader/efi_variable_tee.c |  5 +++++
 3 files changed, 21 insertions(+), 1 deletion(-)
lib/efi_loader/efi_disk.c +7 -0
index d2256713a8..ca5f07f2ec 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 (efi_obj_list_initialized == EFI_SUCCESS) {
+		ret = efi_bootmgr_update_media_device_boot_option();
+		if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+			return -1;
+	}
+
 	return 0;
 }
 
lib/efi_loader/efi_variable.c +9 -1
index be95ed44e6..fe71144358 100644
@@ -476,6 +476,14 @@ efi_status_t efi_init_variables(void)
 			log_err("Invalid EFI variable seed\n");
 	}
 
+	ret = efi_init_secure_state();
+	if (ret != EFI_SUCCESS)
+		return ret;
 
-	return efi_init_secure_state();
+	/* update boot option management after variable service initialized */
+	ret = efi_bootmgr_update_media_device_boot_option();
+	if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+		return ret;
+
+	return EFI_SUCCESS;
 }
lib/efi_loader/efi_variable_tee.c +5 -0
index dfef18435d..2995d4a583 100644
@@ -748,5 +748,10 @@ efi_status_t efi_init_variables(void)
 	if (ret != EFI_SUCCESS)
 		return ret;
 
+	/* update boot option management after variable service initialized */
+	ret = efi_bootmgr_update_media_device_boot_option();
+	if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+		return ret;
+
 	return EFI_SUCCESS;
 }
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v4 2/3] Boot var automatic management for removable medias

Quoted 67 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.

---
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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

 lib/efi_loader/efi_disk.c         |  7 +++++++
 lib/efi_loader/efi_variable.c     | 10 +++++++++-
 lib/efi_loader/efi_variable_tee.c |  5 +++++
 3 files changed, 21 insertions(+), 1 deletion(-)
lib/efi_loader/efi_disk.c +7 -0
index d2256713a8..ca5f07f2ec 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 (efi_obj_list_initialized == EFI_SUCCESS) {
+		ret = efi_bootmgr_update_media_device_boot_option();
+		if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+			return -1;
+	}
+
 	return 0;
 }
 
lib/efi_loader/efi_variable.c +9 -1
index be95ed44e6..fe71144358 100644
@@ -476,6 +476,14 @@ efi_status_t efi_init_variables(void)
 			log_err("Invalid EFI variable seed\n");
 	}
 
+	ret = efi_init_secure_state();
+	if (ret != EFI_SUCCESS)
+		return ret;
 
-	return efi_init_secure_state();
+	/* update boot option management after variable service initialized */
+	ret = efi_bootmgr_update_media_device_boot_option();
+	if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+		return ret;
+
+	return EFI_SUCCESS;
 }
lib/efi_loader/efi_variable_tee.c +0 -0
index dfef18435d..2995d4a583 100644

lib/efi_loader/efi_variable_tee.c efi_status_t efi_init_variables(void) · line 756

Quoted 9 lines
lib/efi_loader/efi_variable_tee.c +5 -0
@@ -748,5 +748,10 @@ efi_status_t efi_init_variables(void)
 	if (ret != EFI_SUCCESS)
 		return ret;
 
+	/* update boot option management after variable service initialized */
+	ret = efi_bootmgr_update_media_device_boot_option();
+	if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+		return ret;
+
 	return EFI_SUCCESS;

Instead of having to add identical code in the variable init code why dont we add this in efi_setup.c, right under the efi_init_variables() call?

Thanks
/Ilias

Quoted 4 lines
lib/efi_loader/efi_variable_tee.c +0 -1
 }
--

2.25.1

#3 lore raw

Re: [PATCH v4 2/3] Boot var automatic management for removable medias

Quoted 77 lines · Ilias Apalodimas
Quoted 29 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.

---
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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

 lib/efi_loader/efi_disk.c         |  7 +++++++
 lib/efi_loader/efi_variable.c     | 10 +++++++++-
 lib/efi_loader/efi_variable_tee.c |  5 +++++
 3 files changed, 21 insertions(+), 1 deletion(-)
lib/efi_loader/efi_disk.c +0 -0
index d2256713a8..ca5f07f2ec 100644

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

Quoted 4 lines
lib/efi_loader/efi_disk.c +1 -0
@@ -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 */

Review

Quoted 10 lines
lib/efi_loader/efi_disk.c +6 -0
+     if (efi_obj_list_initialized == EFI_SUCCESS) {
+             ret = efi_bootmgr_update_media_device_boot_option();
+             if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+                     return -1;
+     }
+
      return 0;
 }
 
(quoted diff) +0 -0

b/lib/efi_loader/efi_variable.c

lib/efi_loader/efi_variable.c efi_status_t efi_init_variables(void) · line 483

Quoted 13 lines

index be95ed44e6..fe71144358 100644

lib/efi_loader/efi_variable.c +4 -1
@@ -476,6 +476,14 @@ efi_status_t efi_init_variables(void)
                      log_err("Invalid EFI variable seed\n");
      }
 
+     ret = efi_init_secure_state();
+     if (ret != EFI_SUCCESS)
+             return ret;
 
-     return efi_init_secure_state();
+     /* update boot option management after variable service

initialized */

Review

Quoted 7 lines
lib/efi_loader/efi_variable.c +5 -0
+     ret = efi_bootmgr_update_media_device_boot_option();
+     if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+             return ret;
+
+     return EFI_SUCCESS;
 }
(quoted diff) +0 -0

b/lib/efi_loader/efi_variable_tee.c

lib/efi_loader/efi_variable_tee.c efi_status_t efi_init_variables(void) · line 751

Quoted 8 lines

index dfef18435d..2995d4a583 100644

lib/efi_loader/efi_variable_tee.c +1 -0
@@ -748,5 +748,10 @@ efi_status_t efi_init_variables(void)
      if (ret != EFI_SUCCESS)
              return ret;
 
+     /* update boot option management after variable service

initialized */

Quoted 9 lines

lib/efi_loader/efi_variable_tee.c efi_status_t efi_init_variables(void) · line 756

Quoted 5 lines
lib/efi_loader/efi_variable_tee.c +4 -0
+     ret = efi_bootmgr_update_media_device_boot_option();
+     if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+             return ret;
+
      return EFI_SUCCESS;

Instead of having to add identical code in the variable init code why dont we add this in efi_setup.c, right under the efi_init_variables() call?

[RM] Because I think the purpose of the
function efi_bootmgr_update_media_device_boot_option is to update the boot option which belongs to part of variable initialization.
So it is better to be inside efi_init_variables() other than efi_init_obj_list().

Quoted 8 lines

Thanks
/Ilias

Quoted 4 lines
lib/efi_loader/efi_variable_tee.c +0 -1
 }
--

2.25.1

#4 lore raw

Re: [PATCH v4 2/3] Boot var automatic management for removable medias

Quoted 86 lines · Raymond Mao
Quoted 72 lines · Ilias Apalodimas
Quoted 70 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.

---
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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

 lib/efi_loader/efi_disk.c         |  7 +++++++
 lib/efi_loader/efi_variable.c     | 10 +++++++++-
 lib/efi_loader/efi_variable_tee.c |  5 +++++
 3 files changed, 21 insertions(+), 1 deletion(-)
lib/efi_loader/efi_disk.c +7 -0
index d2256713a8..ca5f07f2ec 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 (efi_obj_list_initialized == EFI_SUCCESS) {
+             ret = efi_bootmgr_update_media_device_boot_option();
+             if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+                     return -1;
+     }
+
      return 0;
 }
 
lib/efi_loader/efi_variable.c +9 -1
index be95ed44e6..fe71144358 100644
@@ -476,6 +476,14 @@ efi_status_t efi_init_variables(void)
                      log_err("Invalid EFI variable seed\n");
      }
 
+     ret = efi_init_secure_state();
+     if (ret != EFI_SUCCESS)
+             return ret;
 
-     return efi_init_secure_state();
+     /* update boot option management after variable service initialized */
+     ret = efi_bootmgr_update_media_device_boot_option();
+     if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+             return ret;
+
+     return EFI_SUCCESS;
 }
lib/efi_loader/efi_variable_tee.c +0 -0
index dfef18435d..2995d4a583 100644
@@ -748,5 +748,10 @@ efi_status_t efi_init_variables(void)
      if (ret != EFI_SUCCESS)
              return ret;
 
Quoted 9 lines

lib/efi_loader/efi_variable_tee.c efi_status_t efi_init_variables(void) · line 756

Quoted 6 lines
lib/efi_loader/efi_variable_tee.c +5 -0
+     /* update boot option management after variable service initialized */
+     ret = efi_bootmgr_update_media_device_boot_option();
+     if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
+             return ret;
+
      return EFI_SUCCESS;

Instead of having to add identical code in the variable init code why dont we add this in efi_setup.c, right under the efi_init_variables() call?

Quoted 3 lines

[RM] Because I think the purpose of the function efi_bootmgr_update_media_device_boot_option is to update the boot option which belongs to part of variable initialization. So it is better to be inside efi_init_variables() other than efi_init_obj_list().

We also have efi_init_platform_lang(), efi_init_os_indications() which also 'just' initialize variables. So I think having it in the efi_setup() makes sense.

Thanks
/Ilias

Quoted 8 lines
Quoted 8 lines

Thanks
/Ilias

Quoted 4 lines
lib/efi_loader/efi_variable_tee.c +0 -1
 }
--

2.25.1

#5 lore raw

PATCH v4 3/3 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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

 lib/efi_loader/efi_bootmgr.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/efi_loader/efi_bootmgr.c +5 -0
index c329428973..336b9d2d8f 100644
@@ -387,6 +387,11 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
 		p = dev_name;
 		utf8_utf16_strncpy(&p, buf, strlen(buf));
 
+		/* use short form device path */
+		device_path = efi_dp_shorten(device_path);
+		if (!device_path)
+			continue;
+
 		lo.label = dev_name;
 		lo.attributes = LOAD_OPTION_ACTIVE;
 		lo.file_path = device_path;
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v4 3/3] Load option with short device path for boot vars

Hi Raymond,

Quoted 27 lines · 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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

 lib/efi_loader/efi_bootmgr.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/efi_loader/efi_bootmgr.c +0 -0
index c329428973..336b9d2d8f 100644

lib/efi_loader/efi_bootmgr.c static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo · line 392

Quoted 6 lines
lib/efi_loader/efi_bootmgr.c +3 -0
@@ -387,6 +387,11 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
 		p = dev_name;
 		utf8_utf16_strncpy(&p, buf, strlen(buf));
 
+		/* use short form device path */
+		device_path = efi_dp_shorten(device_path);
+		if (!device_path)

One nit here. I think we should print a warning if shortening the path fails. Heinrich are you ok with this? I think not falling back and adding the long form DP is fine.

Thanks
/Ilias

Quoted 8 lines
lib/efi_loader/efi_bootmgr.c +2 -1
+			continue;
+
 		lo.label = dev_name;
 		lo.attributes = LOAD_OPTION_ACTIVE;
 		lo.file_path = device_path;
--

2.25.1

#7 lore raw

Re: [PATCH v4 3/3] Load option with short device path for boot vars

Quoted 25 lines · Ilias Apalodimas

Hi Raymond,

Quoted 22 lines · 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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

  lib/efi_loader/efi_bootmgr.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 7 lines

lib/efi_loader/efi_bootmgr.c static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo · line 388

Quoted 7 lines
lib/efi_loader/efi_bootmgr.c +0 -0
index c329428973..336b9d2d8f 100644
@@ -387,6 +387,11 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
  		p = dev_name;
  		utf8_utf16_strncpy(&p, buf, strlen(buf));

At this point of the code device_path contains the device-path of a device with a simple file protocol.

Quoted 8 lines

lib/efi_loader/efi_bootmgr.c static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo · line 392

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +3 -0
+		/* use short form device path */
+		device_path = efi_dp_shorten(device_path);
+		if (!device_path)

One nit here. I think we should print a warning if shortening the path fails. Heinrich are you ok with this? I think not falling back and adding the long form DP is fine.

efi_dp_shorten() returns NULL if the device-path contains neither of
- a USB WWI node
- a hard drive node, HD()
- a file path node

Why should we skip a device with a simple file protocol here if it does not contain one of the above nodes?

Best regards

Heinrich

Quoted 11 lines

Thanks
/Ilias

Quoted 8 lines
lib/efi_loader/efi_bootmgr.c +2 -1
+			continue;
+
  		lo.label = dev_name;
  		lo.attributes = LOAD_OPTION_ACTIVE;
  		lo.file_path = device_path;
--

2.25.1

#8 lore raw

Re: [PATCH v4 3/3] Load option with short device path for boot vars

Quoted 47 lines · Heinrich Schuchardt
Quoted 25 lines · Ilias Apalodimas

Hi Raymond,

Quoted 22 lines · 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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

  lib/efi_loader/efi_bootmgr.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 7 lines

lib/efi_loader/efi_bootmgr.c static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo · line 388

Quoted 7 lines
lib/efi_loader/efi_bootmgr.c +0 -0
index c329428973..336b9d2d8f 100644
@@ -387,6 +387,11 @@ static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo
             p = dev_name;
             utf8_utf16_strncpy(&p, buf, strlen(buf));

At this point of the code device_path contains the device-path of a device with a simple file protocol.

Quoted 8 lines

lib/efi_loader/efi_bootmgr.c static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boo · line 392

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +3 -0
+            /* use short form device path */
+            device_path = efi_dp_shorten(device_path);
+            if (!device_path)

One nit here. I think we should print a warning if shortening the path fails. Heinrich are you ok with this? I think not falling back and adding the long form DP is fine.

Quoted 7 lines

efi_dp_shorten() returns NULL if the device-path contains neither of
- a USB WWI node
- a hard drive node, HD()
- a file path node

Why should we skip a device with a simple file protocol here if it does not contain one of the above nodes?

Ah fair enough I missed that on efi_dp_shorten(). We should just add the original device path.

Thanks
/Ilias

Thanks
/Lias

Quoted 17 lines

Best regards

Heinrich

Quoted 11 lines

Thanks
/Ilias

Quoted 8 lines
lib/efi_loader/efi_bootmgr.c +2 -1
+                    continue;
+
             lo.label = dev_name;
             lo.attributes = LOAD_OPTION_ACTIVE;
             lo.file_path = device_path;
--

2.25.1

#9 lore raw
In reply to Raymond Mao
Quoted 4 lines · Raymond Mao

Rename and move bootorder and bootoption apis from cmd to lib for re-use between eficonfig and bootmgr

The commit message says that this patch does not change the code! But there are code changes, e.g.

< for (i = 0; i < count; i++)
< free(opt[i].lo);
---

Quoted 4 lines
              for (i = 0; i < count; i++) {
                      if (opt[i].lo)
                              free(opt[i].lo);
              }

Please, provide a patch that only renames and moves the functions as indicated in the commit message. Please, avoid reformatting as that makes checking the diff hard.

Besides, the 'if' that you introduced is superfluous as free() already checks for NULL.

Quoted 11 lines

---
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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

Is this really a change in this patch?

Using a cover-letter for the overall description would be helpful for future submissions.

    git format-patch --cover-letter

Another comment below.

Quoted 606 lines
  cmd/bootmenu.c               |   2 +-
  cmd/eficonfig.c              | 408 +----------------------------------
  include/efi_config.h         |   5 -
  include/efi_loader.h         |  11 +
  lib/efi_loader/efi_bootmgr.c | 380 ++++++++++++++++++++++++++++++++
  lib/efi_loader/efi_helper.c  |  25 +++
  6 files changed, 423 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
index 720f52b48b..82a80306f4 100644
@@ -1134,43 +1134,6 @@ out:
  	return ret;
  }
 
-/**
- * eficonfig_get_unused_bootoption() - get unused "Boot####" index
- *
- * @buf:	pointer to the buffer to store boot option variable name
- * @buf_size:	buffer size
- * @index:	pointer to store the index in the BootOrder variable
- * Return:	status code
- */
-efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
-					     unsigned int *index)
-{
-	u32 i;
-	efi_status_t ret;
-	efi_uintn_t size;
-
-	if (buf_size < u16_strsize(u"Boot####"))
-		return EFI_BUFFER_TOO_SMALL;
-
-	for (i = 0; i <= 0xFFFF; i++) {
-		size = 0;
-		efi_create_indexed_name(buf, buf_size, "Boot", i);
-		ret = efi_get_variable_int(buf, &efi_global_variable_guid,
-					   NULL, &size, NULL, NULL);
-		if (ret == EFI_BUFFER_TOO_SMALL)
-			continue;
-		else
-			break;
-	}
-
-	if (i > 0xFFFF)
-		return EFI_OUT_OF_RESOURCES;
-
-	*index = i;
-
-	return EFI_SUCCESS;
-}
-
  /**
   * eficonfig_set_boot_option() - set boot option
   *
@@ -1208,46 +1171,6 @@ static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_pa
  	return ret;
  }
 
-/**
- * eficonfig_append_bootorder() - append new boot option in BootOrder variable
- *
- * @index:	"Boot####" index to append to BootOrder variable
- * Return:	status code
- */
-efi_status_t eficonfig_append_bootorder(u16 index)
-{
-	u16 *bootorder;
-	efi_status_t ret;
-	u16 *new_bootorder = NULL;
-	efi_uintn_t last, size, new_size;
-
-	/* append new boot option */
-	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
-	last = size / sizeof(u16);
-	new_size = size + sizeof(u16);
-	new_bootorder = calloc(1, new_size);
-	if (!new_bootorder) {
-		ret = EFI_OUT_OF_RESOURCES;
-		goto out;
-	}
-	memcpy(new_bootorder, bootorder, size);
-	new_bootorder[last] = index;
-
-	ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
-				   EFI_VARIABLE_NON_VOLATILE |
-				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
-				   EFI_VARIABLE_RUNTIME_ACCESS,
-				   new_size, new_bootorder, false);
-	if (ret != EFI_SUCCESS)
-		goto out;
-
-out:
-	free(bootorder);
-	free(new_bootorder);
-
-	return ret;
-}
-
  /**
   * create_boot_option_entry() - create boot option entry
   *
@@ -1619,7 +1542,7 @@ static efi_status_t eficonfig_process_add_boot_option(void *data)
  	if (!bo)
  		return EFI_OUT_OF_RESOURCES;
 
-	ret = eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
+	ret = efi_bootmgr_get_unused_bootoption(varname, sizeof(varname), &bo->boot_index);
  	if (ret != EFI_SUCCESS)
  		return ret;
 
@@ -1627,7 +1550,7 @@ static efi_status_t eficonfig_process_add_boot_option(void *data)
  	if (ret != EFI_SUCCESS)
  		goto out;
 
-	ret = eficonfig_append_bootorder((u16)bo->boot_index);
+	ret = efi_bootmgr_append_bootorder((u16)bo->boot_index);
  	if (ret != EFI_SUCCESS)
  		goto out;
 
@@ -1656,31 +1579,6 @@ static efi_status_t eficonfig_process_boot_selected(void *data)
  	return EFI_SUCCESS;
  }
 
-/**
- * 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
- */
-static bool 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;
-}
-
  /**
   * eficonfig_add_boot_selection_entry() - add boot option menu entry
   *
@@ -1805,7 +1703,7 @@ static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)
 
  		if (efi_varname_is_load_option(var_name16, &index)) {
  			/* If the index is included in the BootOrder, skip it */
-			if (search_bootorder(bootorder, num, index, NULL))
+			if (efi_search_bootorder(bootorder, num, index, NULL))
  				continue;
 
  			ret = eficonfig_add_boot_selection_entry(efi_menu, index, selected);
@@ -2202,7 +2100,7 @@ static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi
 
  		if (efi_varname_is_load_option(var_name16, &index)) {
  			/* If the index is included in the BootOrder, skip it */
-			if (search_bootorder(bootorder, num, index, NULL))
+			if (efi_search_bootorder(bootorder, num, index, NULL))
  				continue;
 
  			ret = eficonfig_add_change_boot_order_entry(efi_menu, index, false);
@@ -2304,50 +2202,6 @@ out:
  	return ret;
  }
 
-/**
- * delete_boot_option() - delete selected boot option
- *
- * @boot_index:	boot option index to delete
- * Return:	status code
- */
-static efi_status_t delete_boot_option(u16 boot_index)
-{
-	u16 *bootorder;
-	u16 varname[9];
-	efi_status_t ret;
-	unsigned int index;
-	efi_uintn_t num, size;
-
-	efi_create_indexed_name(varname, sizeof(varname),
-				"Boot", boot_index);
-	ret = efi_set_variable_int(varname, &efi_global_variable_guid,
-				   0, 0, NULL, false);
-	if (ret != EFI_SUCCESS) {
-		log_err("delete boot option(%ls) failed\n", varname);
-		return ret;
-	}
-
-	/* update BootOrder if necessary */
-	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
-	if (!bootorder)
-		return EFI_SUCCESS;
-
-	num = size / sizeof(u16);
-	if (!search_bootorder(bootorder, num, boot_index, &index))
-		return EFI_SUCCESS;
-
-	memmove(&bootorder[index], &bootorder[index + 1],
-		(num - index - 1) * sizeof(u16));
-	size -= sizeof(u16);
-	ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
-				   EFI_VARIABLE_NON_VOLATILE |
-				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
-				   EFI_VARIABLE_RUNTIME_ACCESS,
-				   size, bootorder, false);
-
-	return ret;
-}
-
  /**
   * eficonfig_process_delete_boot_option() - handler to delete boot option
   *
@@ -2362,7 +2216,7 @@ static efi_status_t eficonfig_process_delete_boot_option(void *data)
  	while (1) {
  		ret = eficonfig_show_boot_selection(&selected);
  		if (ret == EFI_SUCCESS)
-			ret = delete_boot_option(selected);
+			ret = efi_bootmgr_delete_boot_option(selected);
 
  		if (ret != EFI_SUCCESS)
  			break;
@@ -2374,256 +2228,6 @@ static efi_status_t eficonfig_process_delete_boot_option(void *data)
  	return ret;
  }
 
-/**
- * eficonfig_enumerate_boot_option() - enumerate the possible bootable media
- *
- * @opt:		pointer to the media boot option structure
- * @volume_handles:	pointer to the efi handles
- * @count:		number of efi handle
- * Return:		status code
- */
-efi_status_t eficonfig_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
-					     efi_handle_t *volume_handles, efi_status_t count)
-{
-	u32 i;
-	struct efi_handler *handler;
-	efi_status_t ret = EFI_SUCCESS;
-
-	for (i = 0; i < count; i++) {
-		u16 *p;
-		u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
-		char *optional_data;
-		struct efi_load_option lo;
-		char buf[BOOTMENU_DEVICE_NAME_MAX];
-		struct efi_device_path *device_path;
-
-		ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
-		if (ret != EFI_SUCCESS)
-			continue;
-		ret = efi_protocol_open(handler, (void **)&device_path,
-					efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
-		if (ret != EFI_SUCCESS)
-			continue;
-
-		ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
-		if (ret != EFI_SUCCESS)
-			continue;
-
-		p = dev_name;
-		utf8_utf16_strncpy(&p, buf, strlen(buf));
-
-		lo.label = dev_name;
-		lo.attributes = LOAD_OPTION_ACTIVE;
-		lo.file_path = device_path;
-		lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
-		/*
-		 * Set the dedicated guid to optional_data, it is used to identify
-		 * the boot option that automatically generated by the bootmenu.
-		 * efi_serialize_load_option() expects optional_data is null-terminated
-		 * utf8 string, so set the "1234567" string to allocate enough space
-		 * to store guid, instead of realloc the load_option.
-		 */
-		lo.optional_data = "1234567";
-		opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
-		if (!opt[i].size) {
-			ret = EFI_OUT_OF_RESOURCES;
-			goto out;
-		}
-		/* set the guid */
-		optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
-		memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
-	}
-
-out:
-	return ret;
-}
-
-/**
- * eficonfig_delete_invalid_boot_option() - delete non-existing boot option
- *
- * @opt:		pointer to the media boot option structure
- * @count:		number of media boot option structure
- * Return:		status code
- */
-efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
-						  efi_status_t count)
-{
-	efi_uintn_t size;
-	void *load_option;
-	u32 i, list_size = 0;
-	struct efi_load_option lo;
-	u16 *var_name16 = NULL;
-	u16 varname[] = u"Boot####";
-	efi_status_t ret = EFI_SUCCESS;
-	u16 *delete_index_list = NULL, *p;
-	efi_uintn_t buf_size;
-
-	buf_size = 128;
-	var_name16 = malloc(buf_size);
-	if (!var_name16)
-		return EFI_OUT_OF_RESOURCES;
-
-	var_name16[0] = 0;
-	for (;;) {
-		int index;
-		efi_guid_t guid;
-		efi_uintn_t tmp;
-
-		ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
-		if (ret == EFI_NOT_FOUND) {
-			/*
-			 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
-			 * This should be treated as success.
-			 */
-			ret = EFI_SUCCESS;
-			break;
-		}
-		if (ret != EFI_SUCCESS)
-			goto out;
-
-		if (!efi_varname_is_load_option(var_name16, &index))
-			continue;
-
-		efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
-		load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
-		if (!load_option)
-			continue;
-
-		tmp = size;
-		ret = efi_deserialize_load_option(&lo, load_option, &size);
-		if (ret != EFI_SUCCESS)
-			goto next;
-
-		if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
-		    !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
-			for (i = 0; i < count; i++) {
-				if (opt[i].size == tmp &&
-				    memcmp(opt[i].lo, load_option, tmp) == 0) {
-					opt[i].exist = true;
-					break;
-				}
-			}
-
-			/*
-			 * The entire list of variables must be retrieved by
-			 * efi_get_next_variable_name_int() before deleting the invalid
-			 * boot option, just save the index here.
-			 */
-			if (i == count) {
-				p = realloc(delete_index_list, sizeof(u32) *
-					    (list_size + 1));
-				if (!p) {
-					ret = EFI_OUT_OF_RESOURCES;
-					goto out;
-				}
-				delete_index_list = p;
-				delete_index_list[list_size++] = index;
-			}
-		}
-next:
-		free(load_option);
-	}
-
-	/* delete all invalid boot options */
-	for (i = 0; i < list_size; i++) {
-		ret = delete_boot_option(delete_index_list[i]);
-		if (ret != EFI_SUCCESS)
-			goto out;
-	}
-
-out:
-	free(var_name16);
-	free(delete_index_list);
-
-	return ret;
-}
-
-/**
- * eficonfig_generate_media_device_boot_option() - generate the media device boot option
- *
- * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
- * and generate the bootmenu entries.
- * This function also provide the BOOT#### variable maintenance for
- * the media device entries.
- *   - Automatically create the BOOT#### variable for the newly detected device,
- *     this BOOT#### variable is distinguished by the special GUID
- *     stored in the EFI_LOAD_OPTION.optional_data
- *   - If the device is not attached to the system, the associated BOOT#### variable
- *     is automatically deleted.
- *
- * Return:	status code
- */
-efi_status_t eficonfig_generate_media_device_boot_option(void)
-{
-	u32 i;
-	efi_status_t ret;
-	efi_uintn_t count;
-	efi_handle_t *volume_handles = NULL;
-	struct eficonfig_media_boot_option *opt = NULL;
-
-	ret = efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_system_protocol_guid,
-					   NULL, &count, (efi_handle_t **)&volume_handles);
-	if (ret != EFI_SUCCESS)
-		return ret;
-
-	opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
-	if (!opt)
-		goto out;
-
-	/* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
-	ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
-	if (ret != EFI_SUCCESS)
-		goto out;
-
-	/*
-	 * System hardware configuration may vary depending on the user setup.
-	 * The boot option is automatically added by the bootmenu.
-	 * If the device is not attached to the system, the boot option needs
-	 * to be deleted.
-	 */
-	ret = eficonfig_delete_invalid_boot_option(opt, count);
-	if (ret != EFI_SUCCESS)
-		goto out;
-
-	/* add non-existent boot option */
-	for (i = 0; i < count; i++) {
-		u32 boot_index;
-		u16 var_name[9];
-
-		if (!opt[i].exist) {
-			ret = eficonfig_get_unused_bootoption(var_name, sizeof(var_name),
-							      &boot_index);
-			if (ret != EFI_SUCCESS)
-				goto out;
-
-			ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
-						   EFI_VARIABLE_NON_VOLATILE |
-						   EFI_VARIABLE_BOOTSERVICE_ACCESS |
-						   EFI_VARIABLE_RUNTIME_ACCESS,
-						   opt[i].size, opt[i].lo, false);
-			if (ret != EFI_SUCCESS)
-				goto out;
-
-			ret = eficonfig_append_bootorder(boot_index);
-			if (ret != EFI_SUCCESS) {
-				efi_set_variable_int(var_name, &efi_global_variable_guid,
-						     0, 0, NULL, false);
-				goto out;
-			}
-		}
-	}
-
-out:
-	if (opt) {
-		for (i = 0; i < count; i++)
-			free(opt[i].lo);
-	}
-	free(opt);
-	efi_free_pool(volume_handles);
-
-	return ret;
-}
-
  /**
   * eficonfig_init() - do required initialization for eficonfig command
   *
@@ -2709,7 +2313,7 @@ static int do_eficonfig(struct cmd_tbl *cmdtp, int flag, int argc, char *const a
  	if (ret != EFI_SUCCESS)
  		return CMD_RET_FAILURE;
 
-	ret = eficonfig_generate_media_device_boot_option();
+	ret = efi_bootmgr_update_media_device_boot_option();
  	if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
  		return ret;
 
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 +49 -0
index 4b24b41047..c329428973 100644
@@ -347,3 +347,383 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
  error:
  	return ret;
  }
+
+/**
+ * efi_bootmgr_enumerate_boot_option() - enumerate the possible bootable media
+ *
+ * @opt:		pointer to the media boot option structure
+ * @volume_handles:	pointer to the efi handles
+ * @count:		number of efi handle
+ * Return:		status code
+ */
+static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
+						      efi_handle_t *volume_handles,
+						      efi_status_t count)
+{
+	u32 i;
+	struct efi_handler *handler;
+	efi_status_t ret = EFI_SUCCESS;
+
+	for (i = 0; i < count; i++) {
+		u16 *p;
+		u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
+		char *optional_data;
+		struct efi_load_option lo;
+		char buf[BOOTMENU_DEVICE_NAME_MAX];
+		struct efi_device_path *device_path;
+
+		ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
+		if (ret != EFI_SUCCESS)
+			continue;
+		ret = efi_protocol_open(handler, (void **)&device_path,
+					efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+		if (ret != EFI_SUCCESS)
+			continue;
+
+		ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
+		if (ret != EFI_SUCCESS)
+			continue;
+
+		p = dev_name;
+		utf8_utf16_strncpy(&p, buf, strlen(buf));
+
+		lo.label = dev_name;
+		lo.attributes = LOAD_OPTION_ACTIVE;
+		lo.file_path = device_path;
+		lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
+		/*
+		 * Set the dedicated guid to optional_data, it is used to identify
+		 * the boot option that automatically generated by the bootmenu.
+		 * efi_serialize_load_option() expects optional_data is null-terminated
+		 * utf8 string, so set the "1234567" string to allocate enough space

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) · line 408

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+		 * to store guid, instead of realloc the load_option.
+		 */
+		lo.optional_data = "1234567";
+		opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
+		if (!opt[i].size) {
+			ret = EFI_OUT_OF_RESOURCES;
+			goto out;
+		}
+		/* set the guid */
+		optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));

We should replace 'u16_strsize(u"1234567")' by 'sizeof(efi_guid_t)' in a follow-up patch to avoid the superfluous function call.

Best regards

Heinrich

Quoted 354 lines
lib/efi_loader/efi_bootmgr.c +321 -0
+		memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
+	}
+
+out:
+	return ret;
+}
+
+/**
+ * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
+ *
+ * @opt:		pointer to the media boot option structure
+ * @count:		number of media boot option structure
+ * Return:		status code
+ */
+static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
+							   efi_status_t count)
+{
+	efi_uintn_t size;
+	void *load_option;
+	u32 i, list_size = 0;
+	struct efi_load_option lo;
+	u16 *var_name16 = NULL;
+	u16 varname[] = u"Boot####";
+	efi_status_t ret = EFI_SUCCESS;
+	u16 *delete_index_list = NULL, *p;
+	efi_uintn_t buf_size;
+
+	buf_size = 128;
+	var_name16 = malloc(buf_size);
+	if (!var_name16)
+		return EFI_OUT_OF_RESOURCES;
+
+	var_name16[0] = 0;
+	for (;;) {
+		int index;
+		efi_guid_t guid;
+		efi_uintn_t tmp;
+
+		ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
+		if (ret == EFI_NOT_FOUND) {
+			log_debug("last efi var\n");
+			/*
+			 * EFI_NOT_FOUND indicates we retrieved all EFI variables.
+			 * This should be treated as success.
+			 */
+			ret = EFI_SUCCESS;
+			break;
+		}
+
+		if (ret != EFI_SUCCESS)
+			goto out;
+
+		if (!efi_varname_is_load_option(var_name16, &index))
+			continue;
+
+		efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
+		load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
+		if (!load_option)
+			continue;
+
+		tmp = size;
+		ret = efi_deserialize_load_option(&lo, load_option, &size);
+		if (ret != EFI_SUCCESS)
+			goto next;
+
+		if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
+		    !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
+			for (i = 0; i < count; i++) {
+				if (opt[i].size == tmp &&
+				    memcmp(opt[i].lo, load_option, tmp) == 0) {
+					opt[i].exist = true;
+					break;
+				}
+			}
+
+			/*
+			 * The entire list of variables must be retrieved by
+			 * efi_get_next_variable_name_int() before deleting the invalid
+			 * boot option, just save the index here.
+			 */
+			if (i == count) {
+				log_debug("found index %d can be deleted\n", index);
+				p = realloc(delete_index_list, sizeof(u32) *
+					    (list_size + 1));
+				if (!p) {
+					ret = EFI_OUT_OF_RESOURCES;
+					goto out;
+				}
+				delete_index_list = p;
+				delete_index_list[list_size++] = index;
+			}
+		}
+next:
+		free(load_option);
+	}
+
+	/* delete all invalid boot options */
+	for (i = 0; i < list_size; i++) {
+		log_debug("deleting index %d\n", delete_index_list[i]);
+		ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
+out:
+	free(var_name16);
+	free(delete_index_list);
+
+	return ret;
+}
+
+/**
+ * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
+ *
+ * @buf:	pointer to the buffer to store boot option variable name
+ * @buf_size:	buffer size
+ * @index:	pointer to store the index in the BootOrder variable
+ * Return:	status code
+ */
+efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
+					       unsigned int *index)
+{
+	u32 i;
+	efi_status_t ret;
+	efi_uintn_t size;
+
+	if (buf_size < u16_strsize(u"Boot####"))
+		return EFI_BUFFER_TOO_SMALL;
+
+	for (i = 0; i <= 0xFFFF; i++) {
+		size = 0;
+		efi_create_indexed_name(buf, buf_size, "Boot", i);
+		ret = efi_get_variable_int(buf, &efi_global_variable_guid,
+					   NULL, &size, NULL, NULL);
+		if (ret == EFI_BUFFER_TOO_SMALL)
+			continue;
+		else
+			break;
+	}
+
+	if (i > 0xFFFF)
+		return EFI_OUT_OF_RESOURCES;
+
+	*index = i;
+
+	return EFI_SUCCESS;
+}
+
+/**
+ * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
+ *
+ * @index:	"Boot####" index to append to BootOrder variable
+ * Return:	status code
+ */
+efi_status_t efi_bootmgr_append_bootorder(u16 index)
+{
+	u16 *bootorder;
+	efi_status_t ret;
+	u16 *new_bootorder = NULL;
+	efi_uintn_t last, size, new_size;
+
+	/* append new boot option */
+	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
+	last = size / sizeof(u16);
+	new_size = size + sizeof(u16);
+	new_bootorder = calloc(1, new_size);
+	if (!new_bootorder) {
+		ret = EFI_OUT_OF_RESOURCES;
+		goto out;
+	}
+	memcpy(new_bootorder, bootorder, size);
+	new_bootorder[last] = index;
+
+	ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
+				   EFI_VARIABLE_NON_VOLATILE |
+				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+				   EFI_VARIABLE_RUNTIME_ACCESS,
+				   new_size, new_bootorder, false);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
+out:
+	free(bootorder);
+	free(new_bootorder);
+
+	return ret;
+}
+
+/**
+ * efi_bootmgr_delete_boot_option() - delete selected boot option
+ *
+ * @boot_index:	boot option index to delete
+ * Return:	status code
+ */
+efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
+{
+	u16 *bootorder;
+	u16 varname[9];
+	efi_status_t ret;
+	unsigned int index;
+	efi_uintn_t num, size;
+
+	efi_create_indexed_name(varname, sizeof(varname),
+				"Boot", boot_index);
+	ret = efi_set_variable_int(varname, &efi_global_variable_guid,
+				   0, 0, NULL, false);
+	if (ret != EFI_SUCCESS) {
+		log_err("delete boot option(%ls) failed\n", varname);
+		return ret;
+	}
+
+	/* update BootOrder if necessary */
+	bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
+	if (!bootorder)
+		return EFI_SUCCESS;
+
+	num = size / sizeof(u16);
+	if (!efi_search_bootorder(bootorder, num, boot_index, &index))
+		return EFI_SUCCESS;
+
+	memmove(&bootorder[index], &bootorder[index + 1],
+		(num - index - 1) * sizeof(u16));
+	size -= sizeof(u16);
+	ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
+				   EFI_VARIABLE_NON_VOLATILE |
+				   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+				   EFI_VARIABLE_RUNTIME_ACCESS,
+				   size, bootorder, false);
+
+	return ret;
+}
+
+/**
+ * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
+ *
+ * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
+ * and generate the bootmenu entries.
+ * This function also provide the BOOT#### variable maintenance for
+ * the media device entries.
+ *   - Automatically create the BOOT#### variable for the newly detected device,
+ *     this BOOT#### variable is distinguished by the special GUID
+ *     stored in the EFI_LOAD_OPTION.optional_data
+ *   - If the device is not attached to the system, the associated BOOT#### variable
+ *     is automatically deleted.
+ *
+ * Return:	status code
+ */
+efi_status_t efi_bootmgr_update_media_device_boot_option(void)
+{
+	u32 i;
+	efi_status_t ret;
+	efi_uintn_t count;
+	efi_handle_t *volume_handles = NULL;
+	struct eficonfig_media_boot_option *opt = NULL;
+
+	ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
+					   &efi_simple_file_system_protocol_guid,
+					   NULL, &count,
+					   (efi_handle_t **)&volume_handles);
+	if (ret != EFI_SUCCESS)
+		return ret;
+
+	opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
+	if (!opt)
+		goto out;
+
+	/* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
+	ret = efi_bootmgr_enumerate_boot_option(opt, volume_handles, count);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
+	/*
+	 * System hardware configuration may vary depending on the user setup.
+	 * The boot option is automatically added by the bootmenu.
+	 * If the device is not attached to the system, the boot option needs
+	 * to be deleted.
+	 */
+	ret = efi_bootmgr_delete_invalid_boot_option(opt, count);
+	if (ret != EFI_SUCCESS)
+		goto out;
+
+	/* add non-existent boot option */
+	for (i = 0; i < count; i++) {
+		u32 boot_index;
+		u16 var_name[9];
+
+		if (!opt[i].exist) {
+			ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
+								&boot_index);
+			if (ret != EFI_SUCCESS)
+				goto out;
+
+			ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
+						   EFI_VARIABLE_NON_VOLATILE |
+						   EFI_VARIABLE_BOOTSERVICE_ACCESS |
+						   EFI_VARIABLE_RUNTIME_ACCESS,
+						   opt[i].size, opt[i].lo, false);
+			if (ret != EFI_SUCCESS)
+				goto out;
+
+			ret = efi_bootmgr_append_bootorder(boot_index);
+			if (ret != EFI_SUCCESS) {
+				efi_set_variable_int(var_name, &efi_global_variable_guid,
+						     0, 0, NULL, false);
+				goto out;
+			}
+		}
+	}
+
+out:
+	if (opt) {
+		for (i = 0; i < count; i++) {
+			if (opt[i].lo)
+				free(opt[i].lo);
+		}
+		free(opt);
+	}
+	efi_free_pool(volume_handles);
+
+	return ret;
+}
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;
+}
#10 lore raw

Hi Heinrich,

Quoted 17 lines · Heinrich Schuchardt
Quoted 4 lines · Raymond Mao

Rename and move bootorder and bootoption apis from cmd to lib for re-use between eficonfig and bootmgr

The commit message says that this patch does not change the code! But there are code changes, e.g.

< for (i = 0; i < count; i++)
< free(opt[i].lo);
---

Quoted 4 lines
              for (i = 0; i < count; i++) {
                      if (opt[i].lo)
                              free(opt[i].lo);
              }
Quoted 7 lines

Please, provide a patch that only renames and moves the functions as indicated in the commit message. Please, avoid reformatting as that makes checking the diff hard.

Besides, the 'if' that you introduced is superfluous as free() already checks for NULL.

[RM] It is a leftover line during debugging. I will clean it all up.

Quoted 13 lines
Quoted 12 lines

---
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 the change of introducing a bool parameter when updating the boot option. Use short-form of device path by default

Quoted 5 lines

Is this really a change in this patch?

Using a cover-letter for the overall description would be helpful for future submissions.

[RM] I catched all changes of the complete patch set here. I will separate them
for each patch respectively.

Quoted 706 lines
     git format-patch --cover-letter

Another comment below.

cmd/bootmenu.c static struct bootmenu_data *bootmenu_create(int

Quoted 14 lines
  cmd/bootmenu.c               |   2 +-
  cmd/eficonfig.c              | 408 +----------------------------------
  include/efi_config.h         |   5 -
  include/efi_loader.h         |  11 +
  lib/efi_loader/efi_bootmgr.c | 380 ++++++++++++++++++++++++++++++++
  lib/efi_loader/efi_helper.c  |  25 +++
  6 files changed, 423 insertions(+), 408 deletions(-)
cmd/bootmenu.c +0 -0
index 6baeedc69f..01daddca7b 100644
@@ -351,7 +351,7 @@ static struct bootmenu_data *bootmenu_create(int

delay)

cmd/bootmenu.c static struct bootmenu_data *bootmenu_create(int · line 351

Quoted 1 line
cmd/bootmenu.c +0 -0
               * UEFI specification requires booting from removal media

using

cmd/bootmenu.c static struct bootmenu_data *bootmenu_create(int · line 352

Quoted 1 line
cmd/bootmenu.c +0 -0
               * a architecture-specific default image name such as

BOOTAA64.EFI.

Quoted 13 lines
cmd/bootmenu.c +1 -1
               */
-             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 +0 -0
index 720f52b48b..82a80306f4 100644
@@ -1134,43 +1134,6 @@ out:
      return ret;
  }

cmd/eficonfig.c out: · line 1145

Quoted 10 lines
cmd/eficonfig.c +0 -9
 
-/**
- * eficonfig_get_unused_bootoption() - get unused "Boot####" index
- *
- * @buf:     pointer to the buffer to store boot option variable name
- * @buf_size:        buffer size
- * @index:   pointer to store the index in the BootOrder variable
- * Return:   status code
- */
-efi_status_t eficonfig_get_unused_bootoption(u16 *buf, efi_uintn_t

buf_size,

cmd/eficonfig.c static efi_status_t eficonfig_set_boot_option(u16

Quoted 32 lines
cmd/eficonfig.c +0 -28
-                                          unsigned int *index)
-{
-     u32 i;
-     efi_status_t ret;
-     efi_uintn_t size;
-
-     if (buf_size < u16_strsize(u"Boot####"))
-             return EFI_BUFFER_TOO_SMALL;
-
-     for (i = 0; i <= 0xFFFF; i++) {
-             size = 0;
-             efi_create_indexed_name(buf, buf_size, "Boot", i);
-             ret = efi_get_variable_int(buf, &efi_global_variable_guid,
-                                        NULL, &size, NULL, NULL);
-             if (ret == EFI_BUFFER_TOO_SMALL)
-                     continue;
-             else
-                     break;
-     }
-
-     if (i > 0xFFFF)
-             return EFI_OUT_OF_RESOURCES;
-
-     *index = i;
-
-     return EFI_SUCCESS;
-}
-
  /**
   * eficonfig_set_boot_option() - set boot option
   *
@@ -1208,46 +1171,6 @@ static efi_status_t eficonfig_set_boot_option(u16

*varname, struct efi_device_pa

cmd/eficonfig.c out:

Quoted 5 lines
cmd/eficonfig.c +0 -2
      return ret;
  }
 
-/**
- * eficonfig_append_bootorder() - append new boot option in BootOrder

variable

cmd/eficonfig.c static efi_status_t eficonfig_set_boot_option(u16 *varname, struct efi_device_pa

Quoted 13 lines
cmd/eficonfig.c +0 -13
- *
- * @index:   "Boot####" index to append to BootOrder variable
- * Return:   status code
- */
-efi_status_t eficonfig_append_bootorder(u16 index)
-{
-     u16 *bootorder;
-     efi_status_t ret;
-     u16 *new_bootorder = NULL;
-     efi_uintn_t last, size, new_size;
-
-     /* append new boot option */
-     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid,

&size);

cmd/eficonfig.c static efi_status_t

Quoted 29 lines
cmd/eficonfig.c +0 -25
-     last = size / sizeof(u16);
-     new_size = size + sizeof(u16);
-     new_bootorder = calloc(1, new_size);
-     if (!new_bootorder) {
-             ret = EFI_OUT_OF_RESOURCES;
-             goto out;
-     }
-     memcpy(new_bootorder, bootorder, size);
-     new_bootorder[last] = index;
-
-     ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
-                                EFI_VARIABLE_NON_VOLATILE |
-                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
-                                EFI_VARIABLE_RUNTIME_ACCESS,
-                                new_size, new_bootorder, false);
-     if (ret != EFI_SUCCESS)
-             goto out;
-
-out:
-     free(bootorder);
-     free(new_bootorder);
-
-     return ret;
-}
-
  /**
   * create_boot_option_entry() - create boot option entry
   *
@@ -1619,7 +1542,7 @@ static efi_status_t

eficonfig_process_add_boot_option(void *data)

cmd/eficonfig.c static efi_status_t eficonfig_process_add_boot_option(void *data)

Quoted 4 lines
cmd/eficonfig.c +0 -1
      if (!bo)
              return EFI_OUT_OF_RESOURCES;
 
-     ret = eficonfig_get_unused_bootoption(varname, sizeof(varname),

&bo->boot_index);

cmd/eficonfig.c static efi_status_t · line 1545

Quoted 1 line
cmd/eficonfig.c +1 -0
+     ret = efi_bootmgr_get_unused_bootoption(varname, sizeof(varname),

&bo->boot_index);

cmd/eficonfig.c static efi_status_t

Quoted 4 lines
cmd/eficonfig.c +0 -0
      if (ret != EFI_SUCCESS)
              return ret;
 
@@ -1627,7 +1550,7 @@ static efi_status_t

eficonfig_process_add_boot_option(void *data)

cmd/eficonfig.c static efi_status_t

Quoted 9 lines
cmd/eficonfig.c +1 -1
      if (ret != EFI_SUCCESS)
              goto out;
 
-     ret = eficonfig_append_bootorder((u16)bo->boot_index);
+     ret = efi_bootmgr_append_bootorder((u16)bo->boot_index);
      if (ret != EFI_SUCCESS)
              goto out;
 
@@ -1656,31 +1579,6 @@ static efi_status_t

eficonfig_process_boot_selected(void *data)

cmd/eficonfig.c static efi_status_t eficonfig_process_boot_selected(void *data)

Quoted 13 lines
cmd/eficonfig.c +0 -10
      return EFI_SUCCESS;
  }
 
-/**
- * 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
- */
-static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32

target, u32 *index)

cmd/eficonfig.c static efi_status_t

Quoted 19 lines
cmd/eficonfig.c +0 -15
-{
-     u32 i;
-
-     for (i = 0; i < num; i++) {
-             if (target == bootorder[i]) {
-                     if (index)
-                             *index = i;
-
-                     return true;
-             }
-     }
-
-     return false;
-}
-
  /**
   * eficonfig_add_boot_selection_entry() - add boot option menu entry
   *
@@ -1805,7 +1703,7 @@ static efi_status_t

eficonfig_show_boot_selection(unsigned int *selected)

cmd/eficonfig.c static efi_status_t · line 1704

Quoted 3 lines
cmd/eficonfig.c +0 -0
              if (efi_varname_is_load_option(var_name16, &index)) {
                      /* If the index is included in the BootOrder, skip

it */

cmd/eficonfig.c static efi_status_t · line 1705

Quoted 2 lines
cmd/eficonfig.c +1 -1
-                     if (search_bootorder(bootorder, num, index, NULL))
+                     if (efi_search_bootorder(bootorder, num, index,

NULL))

cmd/eficonfig.c static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)

Quoted 3 lines
cmd/eficonfig.c +0 -0
                              continue;
 
                      ret = eficonfig_add_boot_selection_entry(efi_menu,

index, selected);

cmd/eficonfig.c static efi_status_t

Quoted 1 line
cmd/eficonfig.c +0 -0
@@ -2202,7 +2100,7 @@ static efi_status_t

eficonfig_create_change_boot_order_entry(struct efimenu *efi

cmd/eficonfig.c static efi_status_t · line 2101

Quoted 3 lines
cmd/eficonfig.c +0 -0
              if (efi_varname_is_load_option(var_name16, &index)) {
                      /* If the index is included in the BootOrder, skip

it */

cmd/eficonfig.c static efi_status_t · line 2102

Quoted 2 lines
cmd/eficonfig.c +1 -1
-                     if (search_bootorder(bootorder, num, index, NULL))
+                     if (efi_search_bootorder(bootorder, num, index,

NULL))

cmd/eficonfig.c static efi_status_t eficonfig_show_boot_selection(unsigned int *selected)

Quoted 3 lines
cmd/eficonfig.c +0 -0
                              continue;
 
                      ret =

eficonfig_add_change_boot_order_entry(efi_menu, index, false);

Quoted 19 lines
cmd/eficonfig.c +0 -15
@@ -2304,50 +2202,6 @@ out:
      return ret;
  }
 
-/**
- * delete_boot_option() - delete selected boot option
- *
- * @boot_index:      boot option index to delete
- * Return:   status code
- */
-static efi_status_t delete_boot_option(u16 boot_index)
-{
-     u16 *bootorder;
-     u16 varname[9];
-     efi_status_t ret;
-     unsigned int index;
-     efi_uintn_t num, size;
-
-     efi_create_indexed_name(varname, sizeof(varname),

cmd/eficonfig.c out: · line 2331

Quoted 10 lines
cmd/eficonfig.c +0 -10
-                             "Boot", boot_index);
-     ret = efi_set_variable_int(varname, &efi_global_variable_guid,
-                                0, 0, NULL, false);
-     if (ret != EFI_SUCCESS) {
-             log_err("delete boot option(%ls) failed\n", varname);
-             return ret;
-     }
-
-     /* update BootOrder if necessary */
-     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid,

&size);

Quoted 11 lines
cmd/eficonfig.c +0 -11
-     if (!bootorder)
-             return EFI_SUCCESS;
-
-     num = size / sizeof(u16);
-     if (!search_bootorder(bootorder, num, boot_index, &index))
-             return EFI_SUCCESS;
-
-     memmove(&bootorder[index], &bootorder[index + 1],
-             (num - index - 1) * sizeof(u16));
-     size -= sizeof(u16);
-     ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,

cmd/eficonfig.c out:

Quoted 10 lines
cmd/eficonfig.c +0 -8
-                                EFI_VARIABLE_NON_VOLATILE |
-                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
-                                EFI_VARIABLE_RUNTIME_ACCESS,
-                                size, bootorder, false);
-
-     return ret;
-}
-
  /**
   * eficonfig_process_delete_boot_option() - handler to delete boot

option

cmd/eficonfig.c static efi_status_t

Quoted 2 lines
cmd/eficonfig.c +0 -0
   *
@@ -2362,7 +2216,7 @@ static efi_status_t

eficonfig_process_delete_boot_option(void *data)

cmd/eficonfig.c static efi_status_t

Quoted 9 lines
cmd/eficonfig.c +1 -1
      while (1) {
              ret = eficonfig_show_boot_selection(&selected);
              if (ret == EFI_SUCCESS)
-                     ret = delete_boot_option(selected);
+                     ret = efi_bootmgr_delete_boot_option(selected);
 
              if (ret != EFI_SUCCESS)
                      break;
@@ -2374,256 +2228,6 @@ static efi_status_t

eficonfig_process_delete_boot_option(void *data)

cmd/eficonfig.c out:

Quoted 5 lines
cmd/eficonfig.c +0 -2
      return ret;
  }
 
-/**
- * eficonfig_enumerate_boot_option() - enumerate the possible bootable

media

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 7 lines
cmd/eficonfig.c +0 -7
- *
- * @opt:             pointer to the media boot option structure
- * @volume_handles:  pointer to the efi handles
- * @count:           number of efi handle
- * Return:           status code
- */
-efi_status_t eficonfig_enumerate_boot_option(struct

eficonfig_media_boot_option *opt,

cmd/eficonfig.c static efi_status_t · line 2386

Quoted 1 line
cmd/eficonfig.c +0 -1
-                                          efi_handle_t *volume_handles,

efi_status_t count)

Quoted 4 lines
cmd/eficonfig.c +0 -4
-{
-     u32 i;
-     struct efi_handler *handler;
-     efi_status_t ret = EFI_SUCCESS;

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 10 lines
cmd/eficonfig.c +0 -10
-
-     for (i = 0; i < count; i++) {
-             u16 *p;
-             u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
-             char *optional_data;
-             struct efi_load_option lo;
-             char buf[BOOTMENU_DEVICE_NAME_MAX];
-             struct efi_device_path *device_path;
-
-             ret = efi_search_protocol(volume_handles[i],

&efi_guid_device_path, &handler);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 4 lines
cmd/eficonfig.c +0 -4
-             if (ret != EFI_SUCCESS)
-                     continue;
-             ret = efi_protocol_open(handler, (void **)&device_path,
-                                     efi_root, NULL,

EFI_OPEN_PROTOCOL_GET_PROTOCOL);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 4 lines
cmd/eficonfig.c +0 -4
-             if (ret != EFI_SUCCESS)
-                     continue;
-
-             ret = efi_disk_get_device_name(volume_handles[i], buf,

BOOTMENU_DEVICE_NAME_MAX);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 10 lines
cmd/eficonfig.c +0 -10
-             if (ret != EFI_SUCCESS)
-                     continue;
-
-             p = dev_name;
-             utf8_utf16_strncpy(&p, buf, strlen(buf));
-
-             lo.label = dev_name;
-             lo.attributes = LOAD_OPTION_ACTIVE;
-             lo.file_path = device_path;
-             lo.file_path_length = efi_dp_size(device_path) +

sizeof(END);

cmd/eficonfig.c static efi_status_t · line 2420

Quoted 2 lines
cmd/eficonfig.c +0 -2
-             /*
-              * Set the dedicated guid to optional_data, it is used to

identify

cmd/eficonfig.c static efi_status_t · line 2421

Quoted 1 line
cmd/eficonfig.c +0 -1
-              * the boot option that automatically generated by the

bootmenu.

cmd/eficonfig.c static efi_status_t · line 2422

Quoted 1 line
cmd/eficonfig.c +0 -1
-              * efi_serialize_load_option() expects optional_data is

null-terminated

cmd/eficonfig.c static efi_status_t · line 2423

Quoted 1 line
cmd/eficonfig.c +0 -1
-              * utf8 string, so set the "1234567" string to allocate

enough space

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 4 lines
cmd/eficonfig.c +0 -4
-              * to store guid, instead of realloc the load_option.
-              */
-             lo.optional_data = "1234567";
-             opt[i].size = efi_serialize_load_option(&lo, (u8

**)&opt[i].lo);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 6 lines
cmd/eficonfig.c +0 -6
-             if (!opt[i].size) {
-                     ret = EFI_OUT_OF_RESOURCES;
-                     goto out;
-             }
-             /* set the guid */
-             optional_data = (char *)opt[i].lo + (opt[i].size -

u16_strsize(u"1234567"));

cmd/eficonfig.c static efi_status_t · line 2434

Quoted 1 line
cmd/eficonfig.c +0 -1
-             memcpy(optional_data, &efi_guid_bootmenu_auto_generated,

sizeof(efi_guid_t));

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 8 lines
cmd/eficonfig.c +0 -8
-     }
-
-out:
-     return ret;
-}
-
-/**
- * eficonfig_delete_invalid_boot_option() - delete non-existing boot

option

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 6 lines
cmd/eficonfig.c +0 -6
- *
- * @opt:             pointer to the media boot option structure
- * @count:           number of media boot option structure
- * Return:           status code
- */
-efi_status_t eficonfig_delete_invalid_boot_option(struct

eficonfig_media_boot_option *opt,

Quoted 14 lines
cmd/eficonfig.c +0 -14
-                                               efi_status_t count)
-{
-     efi_uintn_t size;
-     void *load_option;
-     u32 i, list_size = 0;
-     struct efi_load_option lo;
-     u16 *var_name16 = NULL;
-     u16 varname[] = u"Boot####";
-     efi_status_t ret = EFI_SUCCESS;
-     u16 *delete_index_list = NULL, *p;
-     efi_uintn_t buf_size;
-
-     buf_size = 128;
-     var_name16 = malloc(buf_size);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 10 lines
cmd/eficonfig.c +0 -10
-     if (!var_name16)
-             return EFI_OUT_OF_RESOURCES;
-
-     var_name16[0] = 0;
-     for (;;) {
-             int index;
-             efi_guid_t guid;
-             efi_uintn_t tmp;
-
-             ret = efi_next_variable_name(&buf_size, &var_name16,

&guid);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 3 lines
cmd/eficonfig.c +0 -3
-             if (ret == EFI_NOT_FOUND) {
-                     /*
-                      * EFI_NOT_FOUND indicates we retrieved all EFI

variables.

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 12 lines
cmd/eficonfig.c +0 -12
-                      * This should be treated as success.
-                      */
-                     ret = EFI_SUCCESS;
-                     break;
-             }
-             if (ret != EFI_SUCCESS)
-                     goto out;
-
-             if (!efi_varname_is_load_option(var_name16, &index))
-                     continue;
-
-             efi_create_indexed_name(varname, sizeof(varname), "Boot",

index);

cmd/eficonfig.c static efi_status_t · line 2488

Quoted 1 line
cmd/eficonfig.c +0 -1
-             load_option = efi_get_var(varname,

&efi_global_variable_guid, &size);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 10 lines
cmd/eficonfig.c +0 -10
-             if (!load_option)
-                     continue;
-
-             tmp = size;
-             ret = efi_deserialize_load_option(&lo, load_option, &size);
-             if (ret != EFI_SUCCESS)
-                     goto next;
-
-             if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
-                 !guidcmp(lo.optional_data,

&efi_guid_bootmenu_auto_generated)) {

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 3 lines
cmd/eficonfig.c +0 -3
-                     for (i = 0; i < count; i++) {
-                             if (opt[i].size == tmp &&
-                                 memcmp(opt[i].lo, load_option, tmp) ==

0) {

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 7 lines
cmd/eficonfig.c +0 -7
-                                     opt[i].exist = true;
-                                     break;
-                             }
-                     }
-
-                     /*
-                      * The entire list of variables must be retrieved

by

cmd/eficonfig.c static efi_status_t · line 2509

Quoted 1 line
cmd/eficonfig.c +0 -1
-                      * efi_get_next_variable_name_int() before

deleting the invalid

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 4 lines
cmd/eficonfig.c +0 -4
-                      * boot option, just save the index here.
-                      */
-                     if (i == count) {
-                             p = realloc(delete_index_list, sizeof(u32)

*

Quoted 19 lines
cmd/eficonfig.c +0 -19
-                                         (list_size + 1));
-                             if (!p) {
-                                     ret = EFI_OUT_OF_RESOURCES;
-                                     goto out;
-                             }
-                             delete_index_list = p;
-                             delete_index_list[list_size++] = index;
-                     }
-             }
-next:
-             free(load_option);
-     }
-
-     /* delete all invalid boot options */
-     for (i = 0; i < list_size; i++) {
-             ret = delete_boot_option(delete_index_list[i]);
-             if (ret != EFI_SUCCESS)
-                     goto out;
-     }

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 10 lines
cmd/eficonfig.c +0 -10
-
-out:
-     free(var_name16);
-     free(delete_index_list);
-
-     return ret;
-}
-
-/**
- * eficonfig_generate_media_device_boot_option() - generate the media

device boot option

cmd/eficonfig.c static efi_status_t · line 2544

Quoted 2 lines
cmd/eficonfig.c +0 -2
- *
- * This function enumerates all devices supporting

EFI_SIMPLE_FILE_SYSTEM_PROTOCOL

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 4 lines
cmd/eficonfig.c +0 -4
- * and generate the bootmenu entries.
- * This function also provide the BOOT#### variable maintenance for
- * the media device entries.
- *   - Automatically create the BOOT#### variable for the newly

detected device,

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 3 lines
cmd/eficonfig.c +0 -3
- *     this BOOT#### variable is distinguished by the special GUID
- *     stored in the EFI_LOAD_OPTION.optional_data
- *   - If the device is not attached to the system, the associated

BOOT#### variable

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 13 lines
cmd/eficonfig.c +0 -13
- *     is automatically deleted.
- *
- * Return:   status code
- */
-efi_status_t eficonfig_generate_media_device_boot_option(void)
-{
-     u32 i;
-     efi_status_t ret;
-     efi_uintn_t count;
-     efi_handle_t *volume_handles = NULL;
-     struct eficonfig_media_boot_option *opt = NULL;
-
-     ret = efi_locate_handle_buffer_int(BY_PROTOCOL,

&efi_simple_file_system_protocol_guid,

cmd/eficonfig.c static efi_status_t · line 2565

Quoted 1 line
cmd/eficonfig.c +0 -1
-                                        NULL, &count, (efi_handle_t

**)&volume_handles);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 8 lines
cmd/eficonfig.c +0 -8
-     if (ret != EFI_SUCCESS)
-             return ret;
-
-     opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
-     if (!opt)
-             goto out;
-
-     /* enumerate all devices supporting

EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 6 lines
cmd/eficonfig.c +0 -6
-     ret = eficonfig_enumerate_boot_option(opt, volume_handles, count);
-     if (ret != EFI_SUCCESS)
-             goto out;
-
-     /*
-      * System hardware configuration may vary depending on the user

setup.

cmd/eficonfig.c static efi_status_t · line 2581

Quoted 2 lines
cmd/eficonfig.c +0 -2
-      * The boot option is automatically added by the bootmenu.
-      * If the device is not attached to the system, the boot option

needs

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 13 lines
cmd/eficonfig.c +0 -13
-      * to be deleted.
-      */
-     ret = eficonfig_delete_invalid_boot_option(opt, count);
-     if (ret != EFI_SUCCESS)
-             goto out;
-
-     /* add non-existent boot option */
-     for (i = 0; i < count; i++) {
-             u32 boot_index;
-             u16 var_name[9];
-
-             if (!opt[i].exist) {
-                     ret = eficonfig_get_unused_bootoption(var_name,

sizeof(var_name),

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 5 lines
cmd/eficonfig.c +0 -5
-                                                           &boot_index);
-                     if (ret != EFI_SUCCESS)
-                             goto out;
-
-                     ret = efi_set_variable_int(var_name,

&efi_global_variable_guid,

cmd/eficonfig.c static efi_status_t · line 2600

Quoted 1 line
cmd/eficonfig.c +0 -1
-

EFI_VARIABLE_NON_VOLATILE |

cmd/eficonfig.c static efi_status_t · line 2601

Quoted 1 line
cmd/eficonfig.c +0 -1
-

EFI_VARIABLE_BOOTSERVICE_ACCESS |

cmd/eficonfig.c static efi_status_t · line 2602

Quoted 1 line
cmd/eficonfig.c +0 -1
-

EFI_VARIABLE_RUNTIME_ACCESS,

cmd/eficonfig.c static efi_status_t · line 2603

Quoted 1 line
cmd/eficonfig.c +0 -1
-                                                opt[i].size, opt[i].lo,

false);

cmd/eficonfig.c static efi_status_t eficonfig_process_delete_boot_option(void *data)

Quoted 6 lines
cmd/eficonfig.c +0 -6
-                     if (ret != EFI_SUCCESS)
-                             goto out;
-
-                     ret = eficonfig_append_bootorder(boot_index);
-                     if (ret != EFI_SUCCESS) {
-                             efi_set_variable_int(var_name,

&efi_global_variable_guid,

cmd/eficonfig.c static int do_eficonfig(struct cmd_tbl *cmdtp, int

Quoted 21 lines
cmd/eficonfig.c +0 -17
-                                                  0, 0, NULL, false);
-                             goto out;
-                     }
-             }
-     }
-
-out:
-     if (opt) {
-             for (i = 0; i < count; i++)
-                     free(opt[i].lo);
-     }
-     free(opt);
-     efi_free_pool(volume_handles);
-
-     return ret;
-}
-
  /**
   * eficonfig_init() - do required initialization for eficonfig command
   *
@@ -2709,7 +2313,7 @@ static int do_eficonfig(struct cmd_tbl *cmdtp, int

flag, int argc, char *const a

include/efi_config.h efi_status_t eficonfig_process_common(struct

Quoted 13 lines
cmd/eficonfig.c +1 -1
      if (ret != EFI_SUCCESS)
              return CMD_RET_FAILURE;
 
-     ret = eficonfig_generate_media_device_boot_option();
+     ret = efi_bootmgr_update_media_device_boot_option();
      if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
              return ret;
 
include/efi_config.h +0 -0
index 01ce9b2b06..d7c1601137 100644
@@ -105,11 +105,6 @@ efi_status_t eficonfig_process_common(struct

efimenu *efi_menu,

include/efi_config.h efi_status_t eficonfig_process_common(struct efimenu *efi_menu,

Quoted 5 lines
include/efi_config.h +0 -2
                                    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);

include/efi_config.h efi_status_t eficonfig_process_common(struct efimenu *efi_menu,

Quoted 5 lines
include/efi_config.h +0 -3
-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,

Quoted 6 lines
include/efi_config.h +0 -0
                                       void *data);
include/efi_loader.h +0 -0
index 1542b4b625..31ca1f5d1d 100644

include/efi_loader.h extern struct list_head efi_register_notify_events; · line 523

Quoted 8 lines
include/efi_loader.h +5 -0
@@ -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);

include/efi_loader.h extern struct list_head efi_register_notify_events;

Quoted 6 lines
include/efi_loader.h +6 -0
+/* 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);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t

Quoted 8 lines
include/efi_loader.h +0 -0
  /* Set up console modes */
  void efi_setup_console_size(void);
  /* Install device tree */
lib/efi_loader/efi_bootmgr.c +0 -0
index 4b24b41047..c329428973 100644
@@ -347,3 +347,383 @@ efi_status_t efi_bootmgr_load(efi_handle_t

*handle, void **load_options)

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 6 lines
lib/efi_loader/efi_bootmgr.c +3 -0
  error:
      return ret;
  }
+
+/**
+ * efi_bootmgr_enumerate_boot_option() - enumerate the possible

bootable media

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 7 lines
lib/efi_loader/efi_bootmgr.c +7 -0
+ *
+ * @opt:             pointer to the media boot option structure
+ * @volume_handles:  pointer to the efi handles
+ * @count:           number of efi handle
+ * Return:           status code
+ */
+static efi_status_t efi_bootmgr_enumerate_boot_option(struct

eficonfig_media_boot_option *opt,

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 360

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+                                                   efi_handle_t

*volume_handles,

Quoted 5 lines
lib/efi_loader/efi_bootmgr.c +5 -0
+                                                   efi_status_t count)
+{
+     u32 i;
+     struct efi_handler *handler;
+     efi_status_t ret = EFI_SUCCESS;

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+
+     for (i = 0; i < count; i++) {
+             u16 *p;
+             u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
+             char *optional_data;
+             struct efi_load_option lo;
+             char buf[BOOTMENU_DEVICE_NAME_MAX];
+             struct efi_device_path *device_path;
+
+             ret = efi_search_protocol(volume_handles[i],

&efi_guid_device_path, &handler);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +4 -0
+             if (ret != EFI_SUCCESS)
+                     continue;
+             ret = efi_protocol_open(handler, (void **)&device_path,
+                                     efi_root, NULL,

EFI_OPEN_PROTOCOL_GET_PROTOCOL);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +4 -0
+             if (ret != EFI_SUCCESS)
+                     continue;
+
+             ret = efi_disk_get_device_name(volume_handles[i], buf,

BOOTMENU_DEVICE_NAME_MAX);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+             if (ret != EFI_SUCCESS)
+                     continue;
+
+             p = dev_name;
+             utf8_utf16_strncpy(&p, buf, strlen(buf));
+
+             lo.label = dev_name;
+             lo.attributes = LOAD_OPTION_ACTIVE;
+             lo.file_path = device_path;
+             lo.file_path_length = efi_dp_size(device_path) +

sizeof(END);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 395

Quoted 2 lines
lib/efi_loader/efi_bootmgr.c +2 -0
+             /*
+              * Set the dedicated guid to optional_data, it is used to

identify

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 396

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+              * the boot option that automatically generated by the

bootmenu.

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 397

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+              * efi_serialize_load_option() expects optional_data is

null-terminated

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 398

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+              * utf8 string, so set the "1234567" string to allocate

enough space

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +4 -0
+              * to store guid, instead of realloc the load_option.
+              */
+             lo.optional_data = "1234567";
+             opt[i].size = efi_serialize_load_option(&lo, (u8

**)&opt[i].lo);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 6 lines
lib/efi_loader/efi_bootmgr.c +6 -0
+             if (!opt[i].size) {
+                     ret = EFI_OUT_OF_RESOURCES;
+                     goto out;
+             }
+             /* set the guid */
+             optional_data = (char *)opt[i].lo + (opt[i].size -
Quoted 5 lines

u16_strsize(u"1234567"));

We should replace 'u16_strsize(u"1234567")' by 'sizeof(efi_guid_t)' in a follow-up patch to avoid the superfluous function call.

[RM] Will address in follow-up patch.

Quoted 399 lines

Best regards

Heinrich

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 409

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+             memcpy(optional_data, &efi_guid_bootmenu_auto_generated,

sizeof(efi_guid_t));

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 8 lines
lib/efi_loader/efi_bootmgr.c +8 -0
+     }
+
+out:
+     return ret;
+}
+
+/**
+ * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot

option

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 6 lines
lib/efi_loader/efi_bootmgr.c +6 -0
+ *
+ * @opt:             pointer to the media boot option structure
+ * @count:           number of media boot option structure
+ * Return:           status code
+ */
+static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct

eficonfig_media_boot_option *opt,

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 424

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+                                                        efi_status_t

count)

Quoted 13 lines
lib/efi_loader/efi_bootmgr.c +13 -0
+{
+     efi_uintn_t size;
+     void *load_option;
+     u32 i, list_size = 0;
+     struct efi_load_option lo;
+     u16 *var_name16 = NULL;
+     u16 varname[] = u"Boot####";
+     efi_status_t ret = EFI_SUCCESS;
+     u16 *delete_index_list = NULL, *p;
+     efi_uintn_t buf_size;
+
+     buf_size = 128;
+     var_name16 = malloc(buf_size);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+     if (!var_name16)
+             return EFI_OUT_OF_RESOURCES;
+
+     var_name16[0] = 0;
+     for (;;) {
+             int index;
+             efi_guid_t guid;
+             efi_uintn_t tmp;
+
+             ret = efi_next_variable_name(&buf_size, &var_name16,

&guid);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +4 -0
+             if (ret == EFI_NOT_FOUND) {
+                     log_debug("last efi var\n");
+                     /*
+                      * EFI_NOT_FOUND indicates we retrieved all EFI

variables.

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 13 lines
lib/efi_loader/efi_bootmgr.c +13 -0
+                      * This should be treated as success.
+                      */
+                     ret = EFI_SUCCESS;
+                     break;
+             }
+
+             if (ret != EFI_SUCCESS)
+                     goto out;
+
+             if (!efi_varname_is_load_option(var_name16, &index))
+                     continue;
+
+             efi_create_indexed_name(varname, sizeof(varname), "Boot",

index);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 465

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+             load_option = efi_get_var(varname,

&efi_global_variable_guid, &size);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+             if (!load_option)
+                     continue;
+
+             tmp = size;
+             ret = efi_deserialize_load_option(&lo, load_option, &size);
+             if (ret != EFI_SUCCESS)
+                     goto next;
+
+             if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
+                 !guidcmp(lo.optional_data,

&efi_guid_bootmenu_auto_generated)) {

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 3 lines
lib/efi_loader/efi_bootmgr.c +3 -0
+                     for (i = 0; i < count; i++) {
+                             if (opt[i].size == tmp &&
+                                 memcmp(opt[i].lo, load_option, tmp) ==

0) {

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 7 lines
lib/efi_loader/efi_bootmgr.c +7 -0
+                                     opt[i].exist = true;
+                                     break;
+                             }
+                     }
+
+                     /*
+                      * The entire list of variables must be retrieved

by

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 486

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+                      * efi_get_next_variable_name_int() before

deleting the invalid

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +4 -0
+                      * boot option, just save the index here.
+                      */
+                     if (i == count) {
+                             log_debug("found index %d can be

deleted\n", index);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 491

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+                             p = realloc(delete_index_list, sizeof(u32)

*

Quoted 27 lines
lib/efi_loader/efi_bootmgr.c +27 -0
+                                         (list_size + 1));
+                             if (!p) {
+                                     ret = EFI_OUT_OF_RESOURCES;
+                                     goto out;
+                             }
+                             delete_index_list = p;
+                             delete_index_list[list_size++] = index;
+                     }
+             }
+next:
+             free(load_option);
+     }
+
+     /* delete all invalid boot options */
+     for (i = 0; i < list_size; i++) {
+             log_debug("deleting index %d\n", delete_index_list[i]);
+             ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
+             if (ret != EFI_SUCCESS)
+                     goto out;
+     }
+
+out:
+     free(var_name16);
+     free(delete_index_list);
+
+     return ret;
+}

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+
+/**
+ * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
+ *
+ * @buf:     pointer to the buffer to store boot option variable name
+ * @buf_size:        buffer size
+ * @index:   pointer to store the index in the BootOrder variable
+ * Return:   status code
+ */
+efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t

buf_size,

Quoted 20 lines
lib/efi_loader/efi_bootmgr.c +20 -0
+                                            unsigned int *index)
+{
+     u32 i;
+     efi_status_t ret;
+     efi_uintn_t size;
+
+     if (buf_size < u16_strsize(u"Boot####"))
+             return EFI_BUFFER_TOO_SMALL;
+
+     for (i = 0; i <= 0xFFFF; i++) {
+             size = 0;
+             efi_create_indexed_name(buf, buf_size, "Boot", i);
+             ret = efi_get_variable_int(buf, &efi_global_variable_guid,
+                                        NULL, &size, NULL, NULL);
+             if (ret == EFI_BUFFER_TOO_SMALL)
+                     continue;
+             else
+                     break;
+     }
+

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+     if (i > 0xFFFF)
+             return EFI_OUT_OF_RESOURCES;
+
+     *index = i;
+
+     return EFI_SUCCESS;
+}
+
+/**
+ * efi_bootmgr_append_bootorder() - append new boot option in BootOrder

variable

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 13 lines
lib/efi_loader/efi_bootmgr.c +13 -0
+ *
+ * @index:   "Boot####" index to append to BootOrder variable
+ * Return:   status code
+ */
+efi_status_t efi_bootmgr_append_bootorder(u16 index)
+{
+     u16 *bootorder;
+     efi_status_t ret;
+     u16 *new_bootorder = NULL;
+     efi_uintn_t last, size, new_size;
+
+     /* append new boot option */
+     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid,

&size);

Quoted 40 lines
lib/efi_loader/efi_bootmgr.c +40 -0
+     last = size / sizeof(u16);
+     new_size = size + sizeof(u16);
+     new_bootorder = calloc(1, new_size);
+     if (!new_bootorder) {
+             ret = EFI_OUT_OF_RESOURCES;
+             goto out;
+     }
+     memcpy(new_bootorder, bootorder, size);
+     new_bootorder[last] = index;
+
+     ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
+                                EFI_VARIABLE_NON_VOLATILE |
+                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
+                                EFI_VARIABLE_RUNTIME_ACCESS,
+                                new_size, new_bootorder, false);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
+out:
+     free(bootorder);
+     free(new_bootorder);
+
+     return ret;
+}
+
+/**
+ * efi_bootmgr_delete_boot_option() - delete selected boot option
+ *
+ * @boot_index:      boot option index to delete
+ * Return:   status code
+ */
+efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
+{
+     u16 *bootorder;
+     u16 varname[9];
+     efi_status_t ret;
+     unsigned int index;
+     efi_uintn_t num, size;
+
+     efi_create_indexed_name(varname, sizeof(varname),

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+                             "Boot", boot_index);
+     ret = efi_set_variable_int(varname, &efi_global_variable_guid,
+                                0, 0, NULL, false);
+     if (ret != EFI_SUCCESS) {
+             log_err("delete boot option(%ls) failed\n", varname);
+             return ret;
+     }
+
+     /* update BootOrder if necessary */
+     bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid,

&size);

Quoted 11 lines
lib/efi_loader/efi_bootmgr.c +11 -0
+     if (!bootorder)
+             return EFI_SUCCESS;
+
+     num = size / sizeof(u16);
+     if (!efi_search_bootorder(bootorder, num, boot_index, &index))
+             return EFI_SUCCESS;
+
+     memmove(&bootorder[index], &bootorder[index + 1],
+             (num - index - 1) * sizeof(u16));
+     size -= sizeof(u16);
+     ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+                                EFI_VARIABLE_NON_VOLATILE |
+                                EFI_VARIABLE_BOOTSERVICE_ACCESS |
+                                EFI_VARIABLE_RUNTIME_ACCESS,
+                                size, bootorder, false);
+
+     return ret;
+}
+
+/**
+ * efi_bootmgr_update_media_device_boot_option() - generate the media

device boot option

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 644

Quoted 2 lines
lib/efi_loader/efi_bootmgr.c +2 -0
+ *
+ * This function enumerates all devices supporting

EFI_SIMPLE_FILE_SYSTEM_PROTOCOL

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +4 -0
+ * and generate the bootmenu entries.
+ * This function also provide the BOOT#### variable maintenance for
+ * the media device entries.
+ *   - Automatically create the BOOT#### variable for the newly

detected device,

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 3 lines
lib/efi_loader/efi_bootmgr.c +3 -0
+ *     this BOOT#### variable is distinguished by the special GUID
+ *     stored in the EFI_LOAD_OPTION.optional_data
+ *   - If the device is not attached to the system, the associated

BOOT#### variable

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +4 -0
+ *     is automatically deleted.
+ *
+ * Return:   status code
+ */

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) · line 670

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +10 -0
+efi_status_t efi_bootmgr_update_media_device_boot_option(void)
+{
+     u32 i;
+     efi_status_t ret;
+     efi_uintn_t count;
+     efi_handle_t *volume_handles = NULL;
+     struct eficonfig_media_boot_option *opt = NULL;
+
+     ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
+

&efi_simple_file_system_protocol_guid,

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 667

Quoted 2 lines
lib/efi_loader/efi_bootmgr.c +2 -0
+                                        NULL, &count,
+                                        (efi_handle_t

**)&volume_handles);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 8 lines
lib/efi_loader/efi_bootmgr.c +8 -0
+     if (ret != EFI_SUCCESS)
+             return ret;
+
+     opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
+     if (!opt)
+             goto out;
+
+     /* enumerate all devices supporting

EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 676

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+     ret = efi_bootmgr_enumerate_boot_option(opt, volume_handles,

count);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 5 lines
lib/efi_loader/efi_bootmgr.c +5 -0
+     if (ret != EFI_SUCCESS)
+             goto out;
+
+     /*
+      * System hardware configuration may vary depending on the user

setup.

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 683

Quoted 2 lines
lib/efi_loader/efi_bootmgr.c +2 -0
+      * The boot option is automatically added by the bootmenu.
+      * If the device is not attached to the system, the boot option

needs

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 13 lines
lib/efi_loader/efi_bootmgr.c +13 -0
+      * to be deleted.
+      */
+     ret = efi_bootmgr_delete_invalid_boot_option(opt, count);
+     if (ret != EFI_SUCCESS)
+             goto out;
+
+     /* add non-existent boot option */
+     for (i = 0; i < count; i++) {
+             u32 boot_index;
+             u16 var_name[9];
+
+             if (!opt[i].exist) {
+                     ret = efi_bootmgr_get_unused_bootoption(var_name,

sizeof(var_name),

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 697

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+

&boot_index);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 4 lines
lib/efi_loader/efi_bootmgr.c +4 -0
+                     if (ret != EFI_SUCCESS)
+                             goto out;
+
+                     ret = efi_set_variable_int(var_name,

&efi_global_variable_guid,

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 702

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+

EFI_VARIABLE_NON_VOLATILE |

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 703

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+

EFI_VARIABLE_BOOTSERVICE_ACCESS |

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 704

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+

EFI_VARIABLE_RUNTIME_ACCESS,

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t · line 705

Quoted 1 line
lib/efi_loader/efi_bootmgr.c +1 -0
+                                                opt[i].size, opt[i].lo,

false);

lib/efi_loader/efi_bootmgr.c efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)

Quoted 6 lines
lib/efi_loader/efi_bootmgr.c +6 -0
+                     if (ret != EFI_SUCCESS)
+                             goto out;
+
+                     ret = efi_bootmgr_append_bootorder(boot_index);
+                     if (ret != EFI_SUCCESS) {
+                             efi_set_variable_int(var_name,

&efi_global_variable_guid,

lib/efi_loader/efi_helper.c efi_status_t efi_next_variable_name(efi_uintn_t

Quoted 23 lines
lib/efi_loader/efi_bootmgr.c +18 -0
+                                                  0, 0, NULL, false);
+                             goto out;
+                     }
+             }
+     }
+
+out:
+     if (opt) {
+             for (i = 0; i < count; i++) {
+                     if (opt[i].lo)
+                             free(opt[i].lo);
+             }
+             free(opt);
+     }
+     efi_free_pool(volume_handles);
+
+     return ret;
+}
lib/efi_loader/efi_helper.c +0 -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

Quoted 4 lines
lib/efi_loader/efi_helper.c +1 -0
      return ret;
  }
+

lib/efi_loader/efi_helper.c efi_status_t efi_next_variable_name(efi_uintn_t *size, u16 **buf, efi_guid_t *gu

Quoted 10 lines
lib/efi_loader/efi_helper.c +10 -0
+/**
+ * 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)

Quoted 14 lines
lib/efi_loader/efi_helper.c +14 -0
+{
+     u32 i;
+
+     for (i = 0; i < num; i++) {
+             if (target == bootorder[i]) {
+                     if (index)
+                             *index = i;
+
+                     return true;
+             }
+     }
+
+     return false;
+}