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 6712FC77B7C for ; Fri, 26 May 2023 07:02:27 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A4532846F2; Fri, 26 May 2023 09:02:24 +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="gO8YNLMq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1E624846F2; Fri, 26 May 2023 09:02:23 +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 5BDE080375 for ; Fri, 26 May 2023 09:02:18 +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=1685084533; i=xypron.glpk@gmx.de; bh=OmyT9nnJ5gqnJObXocetqgcV8zc4S477ors+MCO5HYQ=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From:In-Reply-To; b=gO8YNLMqhzyce0o/R1JMLZV0SlFQ3uWwj8293eCVExWuklWgdi/I9UiVA7Y9zPx84 HPzQU+iSUkyiMlBqIHIAakBSMrSewuJ6UkSOl6gRJWh1eOfyHhmVo873Z1JU6SyxqY wf8/ZlnyxM2O0AuO6KkQ277SOZviY9H8wh+iSVg8gY+O1sNlOG6oyxjmtRAG7wmSuj uzGpbThbhocv1qU9r3666o42bENNp/lpVaDcIDXHo/fUuIIbKIYJ+GyrE+d/mAQ0pQ eky/niJtyMS5D47BGVaq/luhnw6vh618RE4imcxBECZM532YBK8Xu7zI7TTQ+wjF8P 4JPKUjjjQlqDw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.123.94] ([88.152.145.137]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MjjCL-1qW1ug2onL-00lGkU; Fri, 26 May 2023 09:02:13 +0200 Message-ID: Date: Fri, 26 May 2023 09:02:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.1 Subject: Re: [PATCH v4 1/3] Move bootorder and bootoption apis to lib Content-Language: en-US To: Raymond Mao Cc: Ilias Apalodimas , Simon Glass , Masahisa Kojima , u-boot@lists.denx.de References: <20230523191819.80258-1-raymond.mao@linaro.org> From: Heinrich Schuchardt In-Reply-To: <20230523191819.80258-1-raymond.mao@linaro.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:2nGEB5svM63OhPomC6suefYtgO5n1Y3PSRTr47qAbZKmW5XHst5 +1BUx9Rs2/xrrxcPBtkwhx/rZgawK9pkb1Biy2g24LbT0ESqUkqtemMY5XZ6hi95wBrOj8+ oipCRc0gwqIogz0/RIlT9RLhlmpnZxWc1ItJ3ICK1Do/HEv8sb6Y6VLjH++NI+E/a5LOLVV seQ4ZGFj7l3Gc3U6g3PMg== UI-OutboundReport: notjunk:1;M01:P0:03LyYC7PgLI=;TtEzzI9H8MxlLEIyy98ohjn3wPo SUaH5DU3NMqmZHcLZMddBGe6/Q7Kao/vAODSG5l946WvSbe1ZEaJen+pMxZNihbf+LL2LQao7 fwMCtNAg45xgZOkE1V/j4yMt4IZG9e5U2T3byGytdB4Q3E56uAN37V/dGkmUOTpJ+RnoZt91+ qCjCBz1fzcT53DWD96uj/upJdgdM1esj6njsfjOMNjUGICU6py1gFg/kL2o9xFMMlru3L8HyF DaXc6ZyEfuccCXWQKfkPLgelyU9Wy1cadB2yqZuWfszPSBNvnf91xzZcRUljobVnJ/1Z+Ohgo 6vdJ0mG6JRHYRx9rUnm06SdNxV2N+B/y404XG/j/w2OrtVuznwvn6qRajXP95Uv7W/VHjhmZO I7kxSUYM0c6GL1M9eqeGaS5iLcIbHzN9B487WDbXyLRtINCUSUoq4O/0lZqikWUiEFlcyUPL4 No/eyzq/igytyDCdo7yWgLsHX0PJkhLnPg9RMeqYqqATsRSOzrvY/vwEOZzSOGtwGIqH/YNLO iG8jBL9uPkLDBfVmOPmQgNCEDYK+Tq9nMszsrwJCSHXVqWHGD8/IbH0BnXXkSrjWyiSLm2D73 ZrNAut94++vQrH3ZiZYHVn3s/FpOXD/HQQQZgpbBc/LFk3qeE3DecKC1VJGFHeT8S5VUED1YY SjrPS3mBooBdTaXK6V3854GUv1TS1dDY9QHWAYzQHMypla+098sTqcRgizI6Z+H79A43vTsZZ RvqDf7l9UyL36DIxy4bLqXxjGYFC7orXYN8n4kjZb8J/K2WQ365c8IS3TCxwalj/UPdj8PsSf shwCIFv4mjXHKTQGoKrxLTmcoO71PU1My+GcMcuqqns66RfLq6KZH+FEh3YrvdKX5CFdtx2HR 7XhMbWMc00BnfVwgX3Sb2gZWeu1xrK/HCoD2ghq0k9L2fBa9KZCdQFBXL+KAX5PXkxlYBhFaN 89SbvRtH+xT+YJzbvFXJdywKWXA= 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/23/23 21:18, 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 The commit message says that this patch does not change the code! But there are code changes, e.g. < for (i =3D 0; i < count; i++) < free(opt[i].lo); =2D-- > for (i =3D 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. > --- > 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. > > 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(-) > > 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..c329428973 100644 > --- a/lib/efi_loader/efi_bootmgr.c > +++ b/lib/efi_loader/efi_bootmgr.c > @@ -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 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")); 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 > + 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) { > + log_debug("last efi var\n"); > + /* > + * 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) { > + log_debug("found index %d can be deleted\n", index); > + 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++) { > + log_debug("deleting index %d\n", delete_index_list[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, > + * 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++) { > + if (opt[i].lo) > + 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; > +}