From patchwork Thu Dec 8 13:40:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 631879 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp224558pvb; Thu, 8 Dec 2022 05:39:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf4H4bW5IWdJ+iOMepEg4v6H8UUFBhsjARTzYV9eA09iO/7IrfA7FUmE/K893JL4fhIAcAcL X-Received: by 2002:a92:60c:0:b0:303:567:2064 with SMTP id x12-20020a92060c000000b0030305672064mr1556394ilg.15.1670506780057; Thu, 08 Dec 2022 05:39:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670506780; cv=none; d=google.com; s=arc-20160816; b=HthuVTB+0toBv/bCDH1pgd+v6RVstsC99V+2TrwsEPQ0hzxLo+0lrbyfsrUy6CSUw9 OcGGT8K0l9ybB3Nc29oLCpGPQoRjllJeTA0kq62hVFV6JnPBr15tRya7Kw/EcQZY4Dme adiibkvDbtUlSpyuWc7SugAGz75GeUIBVe9GWDeNFEhnPYfKGoG9NxzAcAtwXiI0vXaK TJXdqg5aiEllzcEZkkZDejkPO8uv3vFhSFu3J1Chu6FG/1SBYN65B2jjsPAUiNtBMXSg d0JGTbHUWiHigm35FXda8MzB0/SdC44TBOqZSsMdNFKlgwcJJoCj6/PZj6kqxfNTfERj uOHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=7Ep/Lxd5EPCS9OYxuwTev2iTQVqfHq9IlXJPztl0+wY=; b=w8nnVhXA4CuKIEOTWH1h+XSh2BRdDEzraaS7avyyvAl4tsGDA3tBb+nGJb9S/yj3h8 n95ZSBn12BJK1Zc35F7YrvL2DFe2AjDDWJdwGD9xLJxb9X3yMexar/RdPzqoQ3RVshBB +QFH6S/jGSWkyGvyigtMg8U5GsvSQmglD2mL+hKzWVYmIDWDWFphtuLEoypNUB1PQn9q wcs/dYnoC5ZM+Gje3ps9FjHCAWWhtVg+i5gIv02SXb7+Q0fySgHbzOCxLU0BOUjeWzX7 wSezZe1pHlx7+tX+zdJ6OE4kkjXQbcVFB/6bb1cZynPY7z+z0E5TSuxgtmtCTp2fxVwu sqiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iB3I+Yi5; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id b9-20020a02c989000000b0038a0fdaf239si14246538jap.69.2022.12.08.05.39.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 05:39:40 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iB3I+Yi5; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2923481417; Thu, 8 Dec 2022 14:39:36 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org 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; unprotected) header.d=linaro.org header.i=@linaro.org header.b="iB3I+Yi5"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DEE7D85006; Thu, 8 Dec 2022 14:39:33 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C92D3812BE for ; Thu, 8 Dec 2022 14:39:26 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-pl1-x636.google.com with SMTP id d7so1513987pll.9 for ; Thu, 08 Dec 2022 05:39:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=7Ep/Lxd5EPCS9OYxuwTev2iTQVqfHq9IlXJPztl0+wY=; b=iB3I+Yi55RVx2n4uXdSWSwKbC2gyw2xF1JnE6mHJinEgJ4neoDM323bKS3e4xpZV66 Q2pXCyW9Xm97vQHtdjUGEsWX5VBKvYf2URFAyBdQq/TZEr0mqt4SxZK7gFYTM1v7JIAq cRvMzN5rDMRjISsLwWJQrVqBfoCy/qQx7SjZvtjs3wOLU8yaz7OkVuiJmxdFJUvD/47N UwMaOwvcGMQjVmikHGZXja3BGuwWI9D/e/PKxsq7oe43FiAmPUdF4/C/d4racZYwOIul RMwpiZeHh5H+4QFxU3k9yIygTn60OPYIfInIJrZ/+EbW9gugp1tzRa9Xx5pVvRDMHhkE C9Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7Ep/Lxd5EPCS9OYxuwTev2iTQVqfHq9IlXJPztl0+wY=; b=uReJR5es2IbHtH7B0lpBq72xlD93SiwsLJVwNRsf2/3j3tAX4I89vui7DF5oRImJRS 2jf4Vz+SVuRWmKrs5i6suhrJFfTcHDmv8/Umd1PTsD11dNOSyjHRm95YFVAlL9VPyY/h WURZLZ0nKzf53p2k3m9SIitw/eQldj12ln1iFPcynj64zyXEVFwFpyhhPjhTAR/QYEMo 66MYEKbAYb0v4yNAQgboP601r55UzKxJDEo/QNWc3v5EdUnTIZLoHPFGxtZUknTN8Rdc bnSkOstI+TbWZpUS0CL0TnuU/yBT3yHIXDqMAUWDECZJspmzT2hiHL1ZVpZn4XGYKjif 6u/w== X-Gm-Message-State: ANoB5pl2C6ly9XTTzfz6+S3Hrol0VQ1B1Y9ID0eJJ/Bisozft8nEiL2g ZcTms6S9QUR8FdPCgh2OZipnkncqVVDDN5Oq X-Received: by 2002:a17:903:31d0:b0:189:2370:7f69 with SMTP id v16-20020a17090331d000b0018923707f69mr2099359ple.9.1670506764820; Thu, 08 Dec 2022 05:39:24 -0800 (PST) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id z18-20020a170903019200b00177f4ef7970sm16716151plg.11.2022.12.08.05.39.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 05:39:24 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Masahisa Kojima Subject: [PATCH 1/2] eficonfig: curve out efi_get_next_variable_name_int calls Date: Thu, 8 Dec 2022 22:40:21 +0900 Message-Id: <20221208134022.28621-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221208134022.28621-1-masahisa.kojima@linaro.org> References: <20221208134022.28621-1-masahisa.kojima@linaro.org> 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.6 at phobos.denx.de X-Virus-Status: Clean To retrieve the EFI variable name by efi_get_next_variable_name_int(), the sequence of alloc -> efi_get_next_variable_name_int -> realloc -> efi_get_next_variable_name_int is required. In current code, this sequence repeatedly appears in the several functions. It should be curved out a common function. This commit also fixes the missing free() of var_name16 in eficonfig_delete_invalid_boot_option(). Signed-off-by: Masahisa Kojima --- cmd/eficonfig.c | 62 +++++++++---------------------------- include/efi_loader.h | 2 ++ lib/efi_loader/efi_helper.c | 34 ++++++++++++++++++++ 3 files changed, 51 insertions(+), 47 deletions(-) diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c index 394ae67cce..cd7a51cc7e 100644 --- a/cmd/eficonfig.c +++ b/cmd/eficonfig.c @@ -1683,7 +1683,7 @@ static efi_status_t eficonfig_show_boot_selection(unsigned int *selected) u32 i; u16 *bootorder; efi_status_t ret; - u16 *var_name16 = NULL, *p; + u16 *var_name16 = NULL; efi_uintn_t num, size, buf_size; struct efimenu *efi_menu; struct list_head *pos, *n; @@ -1718,24 +1718,12 @@ static efi_status_t eficonfig_show_boot_selection(unsigned int *selected) int index; efi_guid_t guid; - size = buf_size; - ret = efi_get_next_variable_name_int(&size, var_name16, &guid); + ret = efi_get_variable_name(&buf_size, &var_name16, &guid); if (ret == EFI_NOT_FOUND) break; - if (ret == EFI_BUFFER_TOO_SMALL) { - buf_size = size; - p = realloc(var_name16, buf_size); - if (!p) { - free(var_name16); - return EFI_OUT_OF_RESOURCES; - } - var_name16 = p; - ret = efi_get_next_variable_name_int(&size, var_name16, &guid); - } - if (ret != EFI_SUCCESS) { - free(var_name16); - return ret; - } + if (ret != EFI_SUCCESS) + goto out; + 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)) @@ -2026,7 +2014,7 @@ static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi u32 i; char *title; efi_status_t ret; - u16 *var_name16 = NULL, *p; + u16 *var_name16 = NULL; efi_uintn_t size, buf_size; /* list the load option in the order of BootOrder variable */ @@ -2054,19 +2042,9 @@ static efi_status_t eficonfig_create_change_boot_order_entry(struct efimenu *efi break; size = buf_size; - ret = efi_get_next_variable_name_int(&size, var_name16, &guid); + ret = efi_get_variable_name(&buf_size, &var_name16, &guid); if (ret == EFI_NOT_FOUND) break; - if (ret == EFI_BUFFER_TOO_SMALL) { - buf_size = size; - p = realloc(var_name16, buf_size); - if (!p) { - ret = EFI_OUT_OF_RESOURCES; - goto out; - } - var_name16 = p; - ret = efi_get_next_variable_name_int(&size, var_name16, &guid); - } if (ret != EFI_SUCCESS) goto out; @@ -2336,10 +2314,10 @@ efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_op efi_uintn_t size; void *load_option; struct efi_load_option lo; - u16 *var_name16 = NULL, *p; + u16 *var_name16 = NULL; u16 varname[] = u"Boot####"; efi_status_t ret = EFI_SUCCESS; - efi_uintn_t varname_size, buf_size; + efi_uintn_t buf_size; buf_size = 128; var_name16 = malloc(buf_size); @@ -2352,24 +2330,12 @@ efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_op efi_guid_t guid; efi_uintn_t tmp; - varname_size = buf_size; - ret = efi_get_next_variable_name_int(&varname_size, var_name16, &guid); + ret = efi_get_variable_name(&buf_size, &var_name16, &guid); if (ret == EFI_NOT_FOUND) break; - if (ret == EFI_BUFFER_TOO_SMALL) { - buf_size = varname_size; - p = realloc(var_name16, buf_size); - if (!p) { - free(var_name16); - return EFI_OUT_OF_RESOURCES; - } - var_name16 = p; - ret = efi_get_next_variable_name_int(&varname_size, var_name16, &guid); - } - if (ret != EFI_SUCCESS) { - free(var_name16); - return ret; - } + if (ret != EFI_SUCCESS) + goto out; + if (!efi_varname_is_load_option(var_name16, &index)) continue; @@ -2407,6 +2373,8 @@ next: } out: + free(var_name16); + return ret; } diff --git a/include/efi_loader.h b/include/efi_loader.h index 0899e293e5..f80a16108a 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -708,6 +708,8 @@ int algo_to_len(const char *algo); int efi_link_dev(efi_handle_t handle, struct udevice *dev); int efi_unlink_dev(efi_handle_t handle); bool efi_varname_is_load_option(u16 *var_name16, int *index); +efi_status_t efi_get_variable_name(efi_uintn_t *size, u16 **buf, + efi_guid_t *guid); /** * efi_size_in_pages() - convert size in bytes to size in pages diff --git a/lib/efi_loader/efi_helper.c b/lib/efi_loader/efi_helper.c index 788cb9faec..ca9854ec79 100644 --- a/lib/efi_loader/efi_helper.c +++ b/lib/efi_loader/efi_helper.c @@ -223,3 +223,37 @@ bool efi_varname_is_load_option(u16 *var_name16, int *index) return false; } + +/** + * efi_get_variable_name_() - get variable name + * + * This function is a wrapper of efi_get_next_variable_name_int(). + * If efi_get_next_variable_name_int() returns EFI_BUFFER_TOO_SMALL, + * @size and @buf are updated by new buffer size and realloced buffer. + * + * @size: pointer to the buffer size + * @buf: pointer to the buffer + * @guid: pointer to the guid + * Return: status code + */ +efi_status_t efi_get_variable_name(efi_uintn_t *size, u16 **buf, efi_guid_t *guid) +{ + u16 *p; + efi_status_t ret; + efi_uintn_t buf_size = *size; + + ret = efi_get_next_variable_name_int(&buf_size, *buf, guid); + if (ret == EFI_NOT_FOUND) + return ret; + if (ret == EFI_BUFFER_TOO_SMALL) { + p = realloc(*buf, buf_size); + if (!p) + return EFI_OUT_OF_RESOURCES; + + *buf = p; + *size = buf_size; + ret = efi_get_next_variable_name_int(&buf_size, *buf, guid); + } + + return ret; +} From patchwork Thu Dec 8 13:40:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 631880 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp224676pvb; Thu, 8 Dec 2022 05:39:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf6x0Ysui1hAzoBqLOzKRpt41IdjcjY4cNi9+3kJL4V1Qu9nGt5nLSe996oQVXJLxkxbdeV0 X-Received: by 2002:a92:d383:0:b0:303:2931:9242 with SMTP id o3-20020a92d383000000b0030329319242mr1232910ilo.8.1670506795517; Thu, 08 Dec 2022 05:39:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670506795; cv=none; d=google.com; s=arc-20160816; b=LSjuBsRpNpPcu1HijKp0UMsrjpYErPbDW4TKvRRDyqIKJRUw9n4qTqEnvwsupj4Je7 mOzRK5Ky440NrMVtPag4NB1BsG9zRM1x1kvlK0b/rh7pfkBipI/tyN3rgAwrxcE+cfMV htwgF+jCRmmgGY8XBVK9YYRZMLqa+HabO8Vb+LQBIj6ZljGQ8u6C/DHZJCtrfRK6IzHo 5L1pNurYVoY8ntvWthwPsBAcqblDa5NTuReqql8amZArZOK3QKzLqBHWMZGkba/ch7v7 tIRrmfAgqxXWpK2T0pp7zzuUM603mDlzkys+JwymSvI8gKFKM2Bj3nLu41JvWOmsOpPP /GpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=vwlK3V0Pc6DWXa+9yuvbuUlOZzBKi8psjJ8ft8srW/k=; b=OfcskFNMU20bixAf8lIukpeBA4j7nHTCpAbNeRdvxieqkdw4LFDXIlvjf0BqacduUH 7zSPh1sNLYw77ol1wQDL1pDmvj6pfp9wWbPakuC/dwAp8ocK1nhSmrf4N66vFXLYH3bK bGcJZi43wsHffL89IVcWxydVBXzsGVHWalSgFvTuim5P1twwBhTRr6XTMQSTPdSVh0xg Y6kc/4db7HOr/UksJK5JxAUb/GtnXFZJrSxBXOHD48IGlFCIhE/rEXjwZurKCVvNHFQw 32dNLGQ5nHBSVEbHgP7yEl4wMM0NuvxfLtla3WHKGdQO1a6sjFdyIEHKbCrzhbYg6g2H a8Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=l1MRSGd9; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id o22-20020a02c6b6000000b003657ef53f70si14851133jan.12.2022.12.08.05.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 05:39:55 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=l1MRSGd9; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CE9228500D; Thu, 8 Dec 2022 14:39:39 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org 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; unprotected) header.d=linaro.org header.i=@linaro.org header.b="l1MRSGd9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 10947812BE; Thu, 8 Dec 2022 14:39:35 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_PASS,T_SPF_HELO_TEMPERROR autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id F2A1C84225 for ; Thu, 8 Dec 2022 14:39:28 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-pj1-x102b.google.com with SMTP id b13-20020a17090a5a0d00b0021906102d05so1560822pjd.5 for ; Thu, 08 Dec 2022 05:39:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=vwlK3V0Pc6DWXa+9yuvbuUlOZzBKi8psjJ8ft8srW/k=; b=l1MRSGd9hvQDUaVkRZli0cxHMAMZfkHLYe6QG6f3BH6qRAVUs6HDhkc3+O2ZjwOFh1 QdjvXOwbQZ9jZF7KQkKU4SXyjtFxCTm/XKf65oczUPdFV0VJ+DYRQxJea5b+fUcaYLm6 jo5YOyinKGrWkE4npazFwHGyfKf4/yp+Xby8/+WyxLJDIpjVIEwuzp4QDT0azLqEzrWp XCjsYtcWwGOhXStjBdVxT4bJ0iX1DFDdFsxsFIhFk/Eoo8aKSbKYc/aq5kDLasbXdUsg mB7wGD1E+zhnc9dxTfyeKqWW+/6DpvG3fhVKTK1IiHrHjERGoGF4IGs41TPxxskoJR7+ 3p7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vwlK3V0Pc6DWXa+9yuvbuUlOZzBKi8psjJ8ft8srW/k=; b=B8aV3UxmNCdGFVMVo//7Y3DrIJzQZBXcnNt14Lq2jIUuPEYI0sFzc0DQX4iPFl0f/w M801n1iFjgK7wqH8L2ktvwEYePiWgxFa0rcVk+9wG6EPPPWXBF6OCAEBLb9e2X0v7qEe e76PF2iTQSfC2GB66/p5aXANL2Ap+zZ56ORNgSsOCvyJqaimE3kd+8UStQnAAooIA6PG k/TlMo7gHRoEGnTt7l6LkcncTUzX2CcLRNVxG36P1FhIyylKP1w48bmFqG58Qdqm5+Os 1J8AfAx6ggdNTSHzIdWHsXhcKoB+Cg9YDYYdL6sIz8pT1bLTT8neLywCKYkgHCjM8ocj Hw3w== X-Gm-Message-State: ANoB5pkdqDzILUcdQ3i5dWYMfpwpYgOFb/GpfnXrcJiAu90weNaOA1RP Z5wZ2D01DCZjcC1n096eDVsjBjFYnbxhQueW X-Received: by 2002:a17:902:7b97:b0:189:f84:c24 with SMTP id w23-20020a1709027b9700b001890f840c24mr2238189pll.24.1670506766958; Thu, 08 Dec 2022 05:39:26 -0800 (PST) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id z18-20020a170903019200b00177f4ef7970sm16716151plg.11.2022.12.08.05.39.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 05:39:26 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Masahisa Kojima Subject: [PATCH 2/2] eficonfig: avoid SetVariable between GetNextVariableName calls Date: Thu, 8 Dec 2022 22:40:22 +0900 Message-Id: <20221208134022.28621-3-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221208134022.28621-1-masahisa.kojima@linaro.org> References: <20221208134022.28621-1-masahisa.kojima@linaro.org> 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.6 at phobos.denx.de X-Virus-Status: Clean The current code calls efi_set_variable_int() to delete the invalid boot option between calls to efi_get_next_variable_name_int(), it may produce unpredictable results. This commit moves removal of the invalid boot option outside of the efi_get_next_variable_name_int() calls. EFI_NOT_FOUND returned from efi_get_next_variable_name_int() indicates we retrieve all EFI variables, it should be treated as EFI_SUCEESS. To address the checkpatch warning of too many leading tabs, combine two if statement into one. Signed-off-by: Masahisa Kojima --- cmd/eficonfig.c | 59 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c index cd7a51cc7e..e967d8da4d 100644 --- a/cmd/eficonfig.c +++ b/cmd/eficonfig.c @@ -2310,13 +2310,14 @@ out: efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt, efi_status_t count) { - u32 i; 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; @@ -2331,8 +2332,14 @@ efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_op efi_uintn_t tmp; ret = efi_get_variable_name(&buf_size, &var_name16, &guid); - if (ret == EFI_NOT_FOUND) + if (ret == EFI_NOT_FOUND) { + /* + * EFI_NOT_FOUND indicates we retrieve all EFI variables, + * should be treated as success. + */ + ret = EFI_SUCCESS; break; + } if (ret != EFI_SUCCESS) goto out; @@ -2349,31 +2356,55 @@ efi_status_t eficonfig_delete_invalid_boot_option(struct eficonfig_media_boot_op if (ret != EFI_SUCCESS) goto next; - if (size >= sizeof(efi_guid_bootmenu_auto_generated)) { - if (guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated) == 0) { - for (i = 0; i < count; i++) { - if (opt[i].size == tmp && - memcmp(opt[i].lo, load_option, tmp) == 0) { - opt[i].exist = true; - break; - } + if (size >= sizeof(efi_guid_bootmenu_auto_generated) && + guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated) == 0) { + for (i = 0; i < count; i++) { + if (opt[i].size == tmp && + memcmp(opt[i].lo, load_option, tmp) == 0) { + opt[i].exist = true; + break; } + } - if (i == count) { - ret = delete_boot_option(i); - if (ret != EFI_SUCCESS) { - free(load_option); + /* + * 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) { + if (!delete_index_list) { + delete_index_list = malloc(sizeof(u32)); + if (!delete_index_list) { + ret = EFI_OUT_OF_RESOURCES; goto out; } + } else { + 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; }