From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8BF5DC77B73 for ; Tue, 6 Jun 2023 08:13:43 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3C95E859F2; Tue, 6 Jun 2023 10:13:41 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=xypron.glpk@gmx.de header.b="JcdBqS/W"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6555C85B6E; Tue, 6 Jun 2023 10:13:38 +0200 (CEST) Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1A80F847BE for ; Tue, 6 Jun 2023 10:13:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1686039211; x=1686644011; i=xypron.glpk@gmx.de; bh=Vs29CvGQFf0ubWuLABmSNsvQAkfWGuXM1dsu5HQ4aW0=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From:In-Reply-To; b=JcdBqS/W03f7Qb1d+BXa5c6ulSqF0o+70QXHdZ+jdGNbwj6cvDuJ4DPsOZGrO5Ry7GZMW2P Pgd0dzyUDmOeDlD7JLw7JhzEQVFYQzmNz0o3vTYq60reQAzJH+MkEiIXV36PqkjihIt5gCH0X Lf0/AUg2b+drZXCaddWhArL3pD3vHRv5O28jc5oTpu5Qnktq5gJ9GKLYi6zaM2uzbgAZ5SbbH AXApsAlo/TG1a5B73TYRATpNZicjo3kx6b6VrZfo966WYixctQWwexIghmLIayjaeqMKRO3Wa tKHxxyZZfdaMd3gxyJjlqgDTRMKp5SpX8ARLM/cUgXH8XYnhhyCg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [10.105.0.58] ([90.160.41.100]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1M89Kr-1q1mzR2V6p-005K4H; Tue, 06 Jun 2023 10:13:30 +0200 Message-ID: <0186ee49-e5d3-9dca-1533-14d352375f7d@gmx.de> Date: Tue, 6 Jun 2023 10:13:23 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.2 Subject: Re: [PATCH v7 1/4] Move bootorder and bootoption apis to lib To: Raymond Mao Cc: Ilias Apalodimas , Simon Glass , Masahisa Kojima , u-boot@lists.denx.de References: <20230531174515.41758-1-raymond.mao@linaro.org> <20230531174515.41758-2-raymond.mao@linaro.org> Content-Language: en-US From: Heinrich Schuchardt In-Reply-To: <20230531174515.41758-2-raymond.mao@linaro.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:My744xPWtsyuaSv2tyIvRjZ/nVSMBqtCEcDDODiNhXaz8vzojqh T7h4Jxa/XxS2nLaH79lw7ab9XrxFM3XnWc1mlTX4oBVi2WnLsZr1zwvkmXQ3b+oiuZ+1hp0 23HnwKH3DU38vQNJ8HqWl/Wxhnd7ohSRoB0/erPtAxd6RcAI6b3wTlvJojs9QFLzCYi4FgG Rsiq6y9YKnvm5KDjxaY0Q== UI-OutboundReport: notjunk:1;M01:P0:4Fprfi0f9eo=;p0sayK8Jhf11g7AYVU4F3C0wHia NfbOJ89cV4rF916kAXH10lbGyY6Vv6VWXtn7y4smrr4Jzt75dunngBPcCsPZ6swVfIfaGSq/9 By7/GGjBh6TQ5FwKZdRqVh3Cb492k61ABsQLRVc1FU85UcWRCjjf5fhAtDg2KR0Smjrb0MFbE IfK1gVAs0EcA2qAHfGnpMc+dvYveEDSygIGGBwiRjeHEV39dbwYwtxxfG36kEkh5Ll9/93cyd NfzVkg6pUv9SvX8Fp1bHybw7KyR6xoDP6hGMoq5o5H4nGChfYg8iXsEIRIwarOuLrAtuAE2iS AK80F/j4ompxRek3D/OA1sJv+UVMNJrsgyEQZQtgvROBfvbLayx0cObiCkB9jSyo8H8IWwXgc mJfHvkfjuV50EcsBOshqTWikEAus9GLJjZikM4VnDw8uzDSzeacBQN06aDxHsE4WUM2HjZa5x TvR2tLCU/97TRTIZnaxpoVQ0qSOERJPx2z6zwJSS1vh5SkLw9MWeecYMiM+c7VU5y3JcQVfhX c4ayVgr7IXUaWwFsDJEYRTM9pmsxaB5EERcSYukowDZRYoVjWUb+63sjjNyJMkGBI9t+/pKC4 zslmChYKuPHhTTyZKHXPlsUKmrtY0U2e77AYZWXzVq0V1vtBH5yXFkth2SGS6lFCdSIFOBq5d HJWnK8Xc7xVkPkCCxrn4GdZlON4Mct5uo3IiF/53sFLzA41fLTjbT6Y8L3jyf2XYv7snmQBYK WSc7iqD5PkijNQGYMNFBvQCd38wVhCbl34sbX9nYEXY6BHfsT+U504HoDzMJxWuenfcSebQfD 9y7J7gact784L/yMZpHGyht7O4VssRpo2aThYQXpJO6SKGNVvNx7HE5oqImGg4MuN5zY1YRA8 4L2KuF9s4eTqd7jSHjbiVCNpOaT7hVYXgV5g2tI8frnDM84eSp5KWwM1KqfyssigpmyOWbn1u Bvnfoj5cdXLnq6yf+Rhq9g5eUow= X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean On 5/31/23 19:45, Raymond Mao wrote: > Rename and move bootorder and bootoption apis from cmd to lib > for re-use between eficonfig and bootmgr > > Signed-off-by: Raymond Mao > Reviewed-by: Heinrich Schuchardt > --- > Changes in v3 > - Split the patch into moving and renaming functions and > individual patches for each changed functionality > Changes in v6 > - Revert v3 changes in function > efi_bootmgr_update_media_device_boot_option and > efi_bootmgr_delete_invalid_boot_option > > cmd/bootmenu.c | 2 +- > cmd/eficonfig.c | 408 +---------------------------------- > include/efi_config.h | 5 - > include/efi_loader.h | 11 + > lib/efi_loader/efi_bootmgr.c | 375 ++++++++++++++++++++++++++++++++ > lib/efi_loader/efi_helper.c | 25 +++ > 6 files changed, 418 insertions(+), 408 deletions(-) > > diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c > index 6baeedc69f..01daddca7b 100644 > --- a/cmd/bootmenu.c > +++ b/cmd/bootmenu.c > @@ -351,7 +351,7 @@ static struct bootmenu_data *bootmenu_create(int del= ay) > * UEFI specification requires booting from removal media using > * a architecture-specific default image name such as BOOTAA64.EFI. > */ > - efi_ret =3D eficonfig_generate_media_device_boot_option(); > + efi_ret =3D efi_bootmgr_update_media_device_boot_option(); > if (efi_ret !=3D EFI_SUCCESS && efi_ret !=3D EFI_NOT_FOUND) > goto cleanup; > > diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c > index 720f52b48b..82a80306f4 100644 > --- a/cmd/eficonfig.c > +++ b/cmd/eficonfig.c > @@ -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 =3D 0; i <=3D 0xFFFF; i++) { > - size =3D 0; > - efi_create_indexed_name(buf, buf_size, "Boot", i); > - ret =3D efi_get_variable_int(buf, &efi_global_variable_guid, > - NULL, &size, NULL, NULL); > - if (ret =3D=3D EFI_BUFFER_TOO_SMALL) > - continue; > - else > - break; > - } > - > - if (i > 0xFFFF) > - return EFI_OUT_OF_RESOURCES; > - > - *index =3D 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 v= ariable > - * > - * @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 =3D NULL; > - efi_uintn_t last, size, new_size; > - > - /* append new boot option */ > - bootorder =3D efi_get_var(u"BootOrder", &efi_global_variable_guid, &si= ze); > - last =3D size / sizeof(u16); > - new_size =3D size + sizeof(u16); > - new_bootorder =3D calloc(1, new_size); > - if (!new_bootorder) { > - ret =3D EFI_OUT_OF_RESOURCES; > - goto out; > - } > - memcpy(new_bootorder, bootorder, size); > - new_bootorder[last] =3D index; > - > - ret =3D 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 !=3D 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_opt= ion(void *data) > if (!bo) > return EFI_OUT_OF_RESOURCES; > > - ret =3D eficonfig_get_unused_bootoption(varname, sizeof(varname), &bo-= >boot_index); > + ret =3D efi_bootmgr_get_unused_bootoption(varname, sizeof(varname), &b= o->boot_index); > if (ret !=3D EFI_SUCCESS) > return ret; > > @@ -1627,7 +1550,7 @@ static efi_status_t eficonfig_process_add_boot_opt= ion(void *data) > if (ret !=3D EFI_SUCCESS) > goto out; > > - ret =3D eficonfig_append_bootorder((u16)bo->boot_index); > + ret =3D efi_bootmgr_append_bootorder((u16)bo->boot_index); > if (ret !=3D EFI_SUCCESS) > goto out; > > @@ -1656,31 +1579,6 @@ static efi_status_t eficonfig_process_boot_select= ed(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 targe= t, u32 *index) > -{ > - u32 i; > - > - for (i =3D 0; i < num; i++) { > - if (target =3D=3D bootorder[i]) { > - if (index) > - *index =3D 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 =3D eficonfig_add_boot_selection_entry(efi_menu, index, selecte= d); > @@ -2202,7 +2100,7 @@ static efi_status_t eficonfig_create_change_boot_o= rder_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 =3D eficonfig_add_change_boot_order_entry(efi_menu, index, fals= e); > @@ -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 =3D efi_set_variable_int(varname, &efi_global_variable_guid, > - 0, 0, NULL, false); > - if (ret !=3D EFI_SUCCESS) { > - log_err("delete boot option(%ls) failed\n", varname); > - return ret; > - } > - > - /* update BootOrder if necessary */ > - bootorder =3D efi_get_var(u"BootOrder", &efi_global_variable_guid, &si= ze); > - if (!bootorder) > - return EFI_SUCCESS; > - > - num =3D 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 -=3D sizeof(u16); > - ret =3D 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 opt= ion > * > @@ -2362,7 +2216,7 @@ static efi_status_t eficonfig_process_delete_boot_= option(void *data) > while (1) { > ret =3D eficonfig_show_boot_selection(&selected); > if (ret =3D=3D EFI_SUCCESS) > - ret =3D delete_boot_option(selected); > + ret =3D efi_bootmgr_delete_boot_option(selected); > > if (ret !=3D EFI_SUCCESS) > break; > @@ -2374,256 +2228,6 @@ static efi_status_t eficonfig_process_delete_boo= t_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_boo= t_option *opt, > - efi_handle_t *volume_handles, efi_status_t count) > -{ > - u32 i; > - struct efi_handler *handler; > - efi_status_t ret =3D EFI_SUCCESS; > - > - for (i =3D 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 =3D efi_search_protocol(volume_handles[i], &efi_guid_device_path,= &handler); > - if (ret !=3D EFI_SUCCESS) > - continue; > - ret =3D efi_protocol_open(handler, (void **)&device_path, > - efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); > - if (ret !=3D EFI_SUCCESS) > - continue; > - > - ret =3D efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEV= ICE_NAME_MAX); > - if (ret !=3D EFI_SUCCESS) > - continue; > - > - p =3D dev_name; > - utf8_utf16_strncpy(&p, buf, strlen(buf)); > - > - lo.label =3D dev_name; > - lo.attributes =3D LOAD_OPTION_ACTIVE; > - lo.file_path =3D device_path; > - lo.file_path_length =3D 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-terminat= ed > - * utf8 string, so set the "1234567" string to allocate enough space > - * to store guid, instead of realloc the load_option. > - */ > - lo.optional_data =3D "1234567"; > - opt[i].size =3D efi_serialize_load_option(&lo, (u8 **)&opt[i].lo); > - if (!opt[i].size) { > - ret =3D EFI_OUT_OF_RESOURCES; > - goto out; > - } > - /* set the guid */ > - optional_data =3D (char *)opt[i].lo + (opt[i].size - u16_strsize(u"12= 34567")); > - memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_g= uid_t)); > - } > - > -out: > - return ret; > -} > - > -/** > - * eficonfig_delete_invalid_boot_option() - delete non-existing boot op= tion > - * > - * @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_medi= a_boot_option *opt, > - efi_status_t count) > -{ > - efi_uintn_t size; > - void *load_option; > - u32 i, list_size =3D 0; > - struct efi_load_option lo; > - u16 *var_name16 =3D NULL; > - u16 varname[] =3D u"Boot####"; > - efi_status_t ret =3D EFI_SUCCESS; > - u16 *delete_index_list =3D NULL, *p; > - efi_uintn_t buf_size; > - > - buf_size =3D 128; > - var_name16 =3D malloc(buf_size); > - if (!var_name16) > - return EFI_OUT_OF_RESOURCES; > - > - var_name16[0] =3D 0; > - for (;;) { > - int index; > - efi_guid_t guid; > - efi_uintn_t tmp; > - > - ret =3D efi_next_variable_name(&buf_size, &var_name16, &guid); > - if (ret =3D=3D EFI_NOT_FOUND) { > - /* > - * EFI_NOT_FOUND indicates we retrieved all EFI variables. > - * This should be treated as success. > - */ > - ret =3D EFI_SUCCESS; > - break; > - } > - if (ret !=3D 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 =3D efi_get_var(varname, &efi_global_variable_guid, &size= ); > - if (!load_option) > - continue; > - > - tmp =3D size; > - ret =3D efi_deserialize_load_option(&lo, load_option, &size); > - if (ret !=3D EFI_SUCCESS) > - goto next; > - > - if (size >=3D sizeof(efi_guid_bootmenu_auto_generated) && > - !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) { > - for (i =3D 0; i < count; i++) { > - if (opt[i].size =3D=3D tmp && > - memcmp(opt[i].lo, load_option, tmp) =3D=3D 0) { > - opt[i].exist =3D 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 =3D=3D count) { > - p =3D realloc(delete_index_list, sizeof(u32) * > - (list_size + 1)); > - if (!p) { > - ret =3D EFI_OUT_OF_RESOURCES; > - goto out; > - } > - delete_index_list =3D p; > - delete_index_list[list_size++] =3D index; > - } > - } > -next: > - free(load_option); > - } > - > - /* delete all invalid boot options */ > - for (i =3D 0; i < list_size; i++) { > - ret =3D delete_boot_option(delete_index_list[i]); > - if (ret !=3D EFI_SUCCESS) > - goto out; > - } > - > -out: > - free(var_name16); > - free(delete_index_list); > - > - return ret; > -} > - > -/** > - * eficonfig_generate_media_device_boot_option() - generate the media d= evice boot option > - * > - * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYST= EM_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 detecte= d 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 =3D NULL; > - struct eficonfig_media_boot_option *opt =3D NULL; > - > - ret =3D efi_locate_handle_buffer_int(BY_PROTOCOL, &efi_simple_file_sys= tem_protocol_guid, > - NULL, &count, (efi_handle_t **)&volume_handles); > - if (ret !=3D EFI_SUCCESS) > - return ret; > - > - opt =3D calloc(count, sizeof(struct eficonfig_media_boot_option)); > - if (!opt) > - goto out; > - > - /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */ > - ret =3D eficonfig_enumerate_boot_option(opt, volume_handles, count); > - if (ret !=3D 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 =3D eficonfig_delete_invalid_boot_option(opt, count); > - if (ret !=3D EFI_SUCCESS) > - goto out; > - > - /* add non-existent boot option */ > - for (i =3D 0; i < count; i++) { > - u32 boot_index; > - u16 var_name[9]; > - > - if (!opt[i].exist) { > - ret =3D eficonfig_get_unused_bootoption(var_name, sizeof(var_name), > - &boot_index); > - if (ret !=3D EFI_SUCCESS) > - goto out; > - > - ret =3D 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 !=3D EFI_SUCCESS) > - goto out; > - > - ret =3D eficonfig_append_bootorder(boot_index); > - if (ret !=3D EFI_SUCCESS) { > - efi_set_variable_int(var_name, &efi_global_variable_guid, > - 0, 0, NULL, false); > - goto out; > - } > - } > - } > - > -out: > - if (opt) { > - for (i =3D 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 !=3D EFI_SUCCESS) > return CMD_RET_FAILURE; > > - ret =3D eficonfig_generate_media_device_boot_option(); > + ret =3D efi_bootmgr_update_media_device_boot_option(); > if (ret !=3D EFI_SUCCESS && ret !=3D EFI_NOT_FOUND) > return ret; > > diff --git a/include/efi_config.h b/include/efi_config.h > index 01ce9b2b06..d7c1601137 100644 > --- a/include/efi_config.h > +++ b/include/efi_config.h > @@ -105,11 +105,6 @@ efi_status_t eficonfig_process_common(struct efimen= u *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); > diff --git a/include/efi_loader.h b/include/efi_loader.h > index 1542b4b625..31ca1f5d1d 100644 > --- a/include/efi_loader.h > +++ b/include/efi_loader.h > @@ -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 */ > diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c > index 4b24b41047..48cd5f0636 100644 > --- a/lib/efi_loader/efi_bootmgr.c > +++ b/lib/efi_loader/efi_bootmgr.c > @@ -347,3 +347,378 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle= , void **load_options) > error: > return ret; > } > + > +/** > + * efi_bootmgr_enumerate_boot_option() - enumerate the possible bootabl= e 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 =3D EFI_SUCCESS; > + > + for (i =3D 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 =3D efi_search_protocol(volume_handles[i], &efi_guid_device_path,= &handler); > + if (ret !=3D EFI_SUCCESS) > + continue; > + ret =3D efi_protocol_open(handler, (void **)&device_path, > + efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); > + if (ret !=3D EFI_SUCCESS) > + continue; > + > + ret =3D efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEV= ICE_NAME_MAX); > + if (ret !=3D EFI_SUCCESS) > + continue; > + > + p =3D dev_name; > + utf8_utf16_strncpy(&p, buf, strlen(buf)); > + > + lo.label =3D dev_name; > + lo.attributes =3D LOAD_OPTION_ACTIVE; > + lo.file_path =3D device_path; > + lo.file_path_length =3D 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-terminat= ed > + * utf8 string, so set the "1234567" string to allocate enough space > + * to store guid, instead of realloc the load_option. > + */ > + lo.optional_data =3D "1234567"; > + opt[i].size =3D efi_serialize_load_option(&lo, (u8 **)&opt[i].lo); > + if (!opt[i].size) { > + ret =3D EFI_OUT_OF_RESOURCES; > + goto out; > + } > + /* set the guid */ > + optional_data =3D (char *)opt[i].lo + (opt[i].size - u16_strsize(u"12= 34567")); > + memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_g= uid_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 efico= nfig_media_boot_option *opt, > + efi_status_t count) > +{ > + efi_uintn_t size; > + void *load_option; > + u32 i, list_size =3D 0; > + struct efi_load_option lo; > + u16 *var_name16 =3D NULL; > + u16 varname[] =3D u"Boot####"; > + efi_status_t ret =3D EFI_SUCCESS; > + u16 *delete_index_list =3D NULL, *p; > + efi_uintn_t buf_size; > + > + buf_size =3D 128; > + var_name16 =3D malloc(buf_size); > + if (!var_name16) > + return EFI_OUT_OF_RESOURCES; > + > + var_name16[0] =3D 0; > + for (;;) { > + int index; > + efi_guid_t guid; > + efi_uintn_t tmp; > + > + ret =3D efi_next_variable_name(&buf_size, &var_name16, &guid); > + if (ret =3D=3D EFI_NOT_FOUND) { > + /* > + * EFI_NOT_FOUND indicates we retrieved all EFI variables. > + * This should be treated as success. > + */ > + ret =3D EFI_SUCCESS; > + break; > + } > + > + if (ret !=3D 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 =3D efi_get_var(varname, &efi_global_variable_guid, &size= ); > + if (!load_option) > + continue; > + > + tmp =3D size; > + ret =3D efi_deserialize_load_option(&lo, load_option, &size); > + if (ret !=3D EFI_SUCCESS) > + goto next; > + > + if (size >=3D sizeof(efi_guid_bootmenu_auto_generated) && > + !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) { > + for (i =3D 0; i < count; i++) { > + if (opt[i].size =3D=3D tmp && > + memcmp(opt[i].lo, load_option, tmp) =3D=3D 0) { > + opt[i].exist =3D 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 =3D=3D count) { > + p =3D realloc(delete_index_list, sizeof(u32) * > + (list_size + 1)); > + if (!p) { > + ret =3D EFI_OUT_OF_RESOURCES; > + goto out; > + } > + delete_index_list =3D p; > + delete_index_list[list_size++] =3D index; > + } > + } > +next: > + free(load_option); > + } > + > + /* delete all invalid boot options */ > + for (i =3D 0; i < list_size; i++) { > + ret =3D efi_bootmgr_delete_boot_option(delete_index_list[i]); > + if (ret !=3D 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 bu= f_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 =3D 0; i <=3D 0xFFFF; i++) { > + size =3D 0; > + efi_create_indexed_name(buf, buf_size, "Boot", i); > + ret =3D efi_get_variable_int(buf, &efi_global_variable_guid, > + NULL, &size, NULL, NULL); > + if (ret =3D=3D EFI_BUFFER_TOO_SMALL) > + continue; > + else > + break; > + } > + > + if (i > 0xFFFF) > + return EFI_OUT_OF_RESOURCES; > + > + *index =3D 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 =3D NULL; > + efi_uintn_t last, size, new_size; > + > + /* append new boot option */ > + bootorder =3D efi_get_var(u"BootOrder", &efi_global_variable_guid, &si= ze); > + last =3D size / sizeof(u16); > + new_size =3D size + sizeof(u16); > + new_bootorder =3D calloc(1, new_size); > + if (!new_bootorder) { > + ret =3D EFI_OUT_OF_RESOURCES; > + goto out; > + } > + memcpy(new_bootorder, bootorder, size); > + new_bootorder[last] =3D index; > + > + ret =3D 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 !=3D 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 =3D efi_set_variable_int(varname, &efi_global_variable_guid, > + 0, 0, NULL, false); > + if (ret !=3D EFI_SUCCESS) { > + log_err("delete boot option(%ls) failed\n", varname); > + return ret; > + } > + > + /* update BootOrder if necessary */ > + bootorder =3D efi_get_var(u"BootOrder", &efi_global_variable_guid, &si= ze); > + if (!bootorder) > + return EFI_SUCCESS; > + > + num =3D 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 -=3D sizeof(u16); > + ret =3D 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 d= evice boot option > + * > + * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYST= EM_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 detecte= d device, With this patch applied to origin/next I get: ./lib/efi_loader/efi_bootmgr.c:651:Unexpected indentation. We have doc/api/efi.rst:44: .. kernel-doc:: lib/efi_loader/efi_bootmgr.c So this file goes into documentation. Please, provide a follow up patch to fix 'make htmldocs'. See https://u-boot.readthedocs.io/en/latest/build/documentation.html . Best regards Heinrich > + * 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 =3D NULL; > + struct eficonfig_media_boot_option *opt =3D NULL; > + > + ret =3D efi_locate_handle_buffer_int(BY_PROTOCOL, > + &efi_simple_file_system_protocol_guid, > + NULL, &count, > + (efi_handle_t **)&volume_handles); > + if (ret !=3D EFI_SUCCESS) > + return ret; > + > + opt =3D calloc(count, sizeof(struct eficonfig_media_boot_option)); > + if (!opt) > + goto out; > + > + /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */ > + ret =3D efi_bootmgr_enumerate_boot_option(opt, volume_handles, count); > + if (ret !=3D 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 =3D efi_bootmgr_delete_invalid_boot_option(opt, count); > + if (ret !=3D EFI_SUCCESS) > + goto out; > + > + /* add non-existent boot option */ > + for (i =3D 0; i < count; i++) { > + u32 boot_index; > + u16 var_name[9]; > + > + if (!opt[i].exist) { > + ret =3D efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name)= , > + &boot_index); > + if (ret !=3D EFI_SUCCESS) > + goto out; > + > + ret =3D 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 !=3D EFI_SUCCESS) > + goto out; > + > + ret =3D efi_bootmgr_append_bootorder(boot_index); > + if (ret !=3D EFI_SUCCESS) { > + efi_set_variable_int(var_name, &efi_global_variable_guid, > + 0, 0, NULL, false); > + goto out; > + } > + } > + } > + > +out: > + if (opt) { > + for (i =3D 0; i < count; i++) > + free(opt[i].lo); > + } > + free(opt); > + efi_free_pool(volume_handles); > + > + return ret; > +} > diff --git a/lib/efi_loader/efi_helper.c b/lib/efi_loader/efi_helper.c > index 1f4ab2b419..cdfd16ea77 100644 > --- a/lib/efi_loader/efi_helper.c > +++ b/lib/efi_loader/efi_helper.c > @@ -257,3 +257,28 @@ efi_status_t efi_next_variable_name(efi_uintn_t *si= ze, 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 =3D 0; i < num; i++) { > + if (target =3D=3D bootorder[i]) { > + if (index) > + *index =3D i; > + > + return true; > + } > + } > + > + return false; > +}