+ *index = i;
+ *current = iter;
+
+ return 1;
+}
+
+#if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG))
+/**
+ * prepare_uefi_bootorder_entry() - generate the uefi bootmenu entries
+ *
+ * This function read the "BootOrder" UEFI variable
+ * and generate the bootmenu entries in the order of "BootOrder".
+ *
+ * @menu: pointer to the bootmenu structure
+ * @current: pointer to the last bootmenu entry list
+ * @index: pointer to the index of the last bootmenu entry,
+ * the number of uefi entry is added by this function
+ * Return: 1 on success, negative value on error
+ */
+static int prepare_uefi_bootorder_entry(struct bootmenu_data *menu,
+ struct bootmenu_entry **current,
+ unsigned short int *index)
+{
+ u16 *bootorder;
+ efi_status_t ret;
+ unsigned short j;
+ efi_uintn_t num, size;
+ void *load_option;
+ struct efi_load_option lo;
+ u16 varname[] = u"Boot####";
+ unsigned short int i = *index;
+ struct bootmenu_entry *entry = NULL;
+ struct bootmenu_entry *iter = *current;
+
+ bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
+ if (!bootorder)
+ return -ENOENT;
+
+ num = size / sizeof(u16);
+ for (j = 0; j < num; j++) {
+ entry = malloc(sizeof(struct bootmenu_entry));
+ if (!entry)
+ return -ENOMEM;
+
+ efi_create_indexed_name(varname, sizeof(varname),
+ "Boot", bootorder[j]);
+ load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
+ if (!load_option)
+ continue;
+
+ ret = efi_deserialize_load_option(&lo, load_option, &size);
+ if (ret != EFI_SUCCESS) {
+ log_warning("Invalid load option for %ls\n", varname);
+ free(load_option);
+ free(entry);
+ continue;
+ }
+
+ if (lo.attributes & LOAD_OPTION_ACTIVE) {
+ char *buf;
+
+ buf = calloc(1, utf16_utf8_strlen(lo.label) + 1);
+ if (!buf) {
+ free(load_option);
+ free(entry);
+ free(bootorder);
+ return -ENOMEM;
+ }
+ entry->title = buf;
+ utf16_utf8_strncpy(&buf, lo.label, u16_strlen(lo.label));
+ entry->command = strdup("bootefi bootmgr");
+ sprintf(entry->key, "%d", i);
+ entry->num = i;
+ entry->menu = menu;
+ entry->type = BOOTMENU_TYPE_UEFI_BOOT_OPTION;
+ entry->bootorder = bootorder[j];
+ entry->next = NULL;
+
+ if (!iter)
+ menu->first = entry;
+ else
+ iter->next = entry;
+
+ iter = entry;
+ i++;
+ }
+
+ free(load_option);
+
+ if (i == MAX_COUNT - 1)
+ break;
+ }
+
+ free(bootorder);
+ *index = i;
+ *current = iter;
+
+ return 1;
+}
+#endif
+
+static struct bootmenu_data *bootmenu_create(int delay)
+{
+ int ret;
+ unsigned short int i = 0;
+ struct bootmenu_data *menu;
+ struct bootmenu_entry *iter = NULL;
+ struct bootmenu_entry *entry;
+ char *default_str;
+
+ menu = malloc(sizeof(struct bootmenu_data));
+ if (!menu)
+ return NULL;
+
+ menu->delay = delay;
+ menu->active = 0;
+ menu->first = NULL;
+
+ default_str = env_get("bootmenu_default");
+ if (default_str)
+ menu->active = (int)simple_strtol(default_str, NULL, 10);
+
+ ret = prepare_bootmenu_entry(menu, &iter, &i);
+ if (ret < 0)
+ goto cleanup;
+
+#if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) && (IS_ENABLED(CONFIG_CMD_EFICONFIG))
+ if (i < MAX_COUNT - 1) {
+ efi_status_t efi_ret;
+
+ /*
+ * UEFI specification requires booting from removal media using
+ * a architecture-specific default image name such as BOOTAA64.EFI.
+ */
+ efi_ret = efi_bootmgr_update_media_device_boot_option();
+ if (efi_ret != EFI_SUCCESS)
+ goto cleanup;
+
+ ret = prepare_uefi_bootorder_entry(menu, &iter, &i);
+ if (ret < 0 && ret != -ENOENT)
+ goto cleanup;
+ }
+#endif
+