From patchwork Mon Dec 19 02:33:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 635105 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp2095478pvb; Sun, 18 Dec 2022 18:33:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf4rpHYqLBytPomnBNPApcCBDwMtd7Fok3spgAqgFfQFqtgJA+JhhQZWSgA1mLlts2v+dTs+ X-Received: by 2002:a05:6808:69a:b0:35b:e57b:74a7 with SMTP id k26-20020a056808069a00b0035be57b74a7mr16511050oig.15.1671417213739; Sun, 18 Dec 2022 18:33:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671417213; cv=none; d=google.com; s=arc-20160816; b=Vv6Fu8wylk7ApJTnxRE7JmlI68yStwIXvbzeHms5+xl9DanEZuegFFGpNjO5R31dxI PBgBpZYFzm+6NLDynYhYtawwxVOTYfuK/p8PPud7+DMoQRmc3QaFVx7kwQar6OYYgZcM 5iZNRRI/MJBOh6boynkliDZOP3MAcxl1KrKVXMCn7mjEFkt4q+5N7JqmR/PN/dVt/1mx /oO2hYFKXjjzGEZKaW/cHRusUbaCDTzGVm0QFyzmXbQYxcPDu0AM4YptFEydRqfw1ku1 SSootOlb+tBqoZpRPSrw3lNr2hsq0OBQEwSKp/RGzFNH2rtIGN+o9yyQ+hAV6DGMsb0F IYeg== 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=1spnwCUNZhnGGHAGKLBLZcH7PnsbWro+Q0g514btXLg=; b=aj1+wPGvukAuHivnTwWerXHsXLzeW0NIi/cMQGG/FuguDYXObqClDAiZurN6TIIy07 Fr/ePy4aO19XVmXfveVWrjHkTb0/NCrVdZw4CG8WWDgzHac6dfskzPLo8Um45EyE5DJa QgYZ83xt5ZgBzalgxGVbacvZmEmmNCLYQPNkdEw2jCcUTksiSU3+jzLczQVvYhXMXsiJ Awt9Aux5Tb2h2w3bgTdzwx8RHRzhRYzIUKIPPS5Vln0rth5Rn1WT/dVox/vfLs/yvBe8 sW/Uh63lraI9FHPy+exiORoO6HFo90w6Tl6fuOAu6s33bhSbe7VlvVOBxEgWP2ZbskKZ wr5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iAdsUile; 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 v2-20020a544d02000000b0035a8cfcdbb6si6344567oix.255.2022.12.18.18.33.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Dec 2022 18:33:33 -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=iAdsUile; 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 39D0785062; Mon, 19 Dec 2022 03:33:19 +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="iAdsUile"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8FB1E836CF; Mon, 19 Dec 2022 03:33:13 +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-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) (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 3B2D6839D5 for ; Mon, 19 Dec 2022 03:33:10 +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=masahisa.kojima@linaro.org Received: by mail-pj1-x1033.google.com with SMTP id 3-20020a17090a098300b00219041dcbe9so7544776pjo.3 for ; Sun, 18 Dec 2022 18:33:10 -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=1spnwCUNZhnGGHAGKLBLZcH7PnsbWro+Q0g514btXLg=; b=iAdsUile79EHntAjJlHW0+0MbkotwFqFZfH1ycVDS9luhtbkedIxvsSDzRD+eNVL+X 3f2nx1S4/GaDz0AonaPwbdqiAwNVm0LR+LaAALHZYV2W49cg0OvmQQbe9FWPMoH97hjV OZKmNi37+h1bKEU4YZ2Hg21KxCgmxhxpgtJl79ZUeiSjgmcU1KGp7lOFuTIxnuXUfd8w zRHlkfkg41G6tmQ2DQcx28y3rBPLwNgSBheFrvUtOgXwfopjuDdwM+eenMeRqoh0O7Cl B3pELP7A9dO4SSk8HD/IPFKAPFUGJueRCz6K0+xcgGCL26MD6RUNFNHYpDCD7gpNQy+Y kB6g== 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=1spnwCUNZhnGGHAGKLBLZcH7PnsbWro+Q0g514btXLg=; b=axQv1P492Ue05BbYj6YW/Qo7sjKf8z31T4Bj91a/0WpmmZ9w00lnemJUftErL72Q8Q 21yqIF/+iVx8ON2RXVsWZxxabgw70VVqTxn/JOln2y3VcPp3qM1ssMszY249wyNAqHms ZGkIF0r2xtoWbkDN+X+mPiI4zUFajMRMYAWXIsiIvI1UmGzVirhAySk3h2UDaLZwFGw7 RSdseeA1aodkOP8EVV33t5F8S9EhN2oksv4ib+Biyi5xwef3tECCTRgzcX13t1sjWl8q 9BCFV1ncekXxjdL24bYRLes1XC+0mBmV+qIsvfzNX3zVg0nBJvMVibP6lIA/uxfw1jmy AbYw== X-Gm-Message-State: ANoB5pmU9023K/i95QZwCiHIEpIlBpzp9WeYXx6py5MbYGnzU0KDF/El jA2f1R/t0d8BimF8QgjsPuOTWBIsVoSeRvXI X-Received: by 2002:a17:90b:3c08:b0:221:1232:b0ec with SMTP id pb8-20020a17090b3c0800b002211232b0ecmr35465503pjb.21.1671417188055; Sun, 18 Dec 2022 18:33:08 -0800 (PST) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id ne8-20020a17090b374800b00219dab2d7a0sm4761523pjb.9.2022.12.18.18.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Dec 2022 18:33:07 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Masahisa Kojima Subject: [PATCH v2 1/2] eficonfig: carve out efi_get_next_variable_name_int calls Date: Mon, 19 Dec 2022 11:33:12 +0900 Message-Id: <20221219023314.23959-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221219023314.23959-1-masahisa.kojima@linaro.org> References: <20221219023314.23959-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 Reviewed-by: Heinrich Schuchardt Reviewed-by: Ilias Apalodimas --- Changes in v2: - fix typo in the commit message - rename efi_get_variable_name to efi_next_variable_name 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..0b07dfc958 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_next_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_next_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_next_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..699176872d 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_next_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..1f4ab2b419 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_next_variable_name() - get next 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_next_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 Mon Dec 19 02:33:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 635106 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp2095557pvb; Sun, 18 Dec 2022 18:33:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf4qerqdzolZlLQKXFIRJosgQ1wOV/lsZ2PPJ5xwq3zWGqaGBePrrYXVDqJ5ZYywEYLchIq7 X-Received: by 2002:a9d:6405:0:b0:661:dfeb:ea34 with SMTP id h5-20020a9d6405000000b00661dfebea34mr18342617otl.25.1671417226423; Sun, 18 Dec 2022 18:33:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671417226; cv=none; d=google.com; s=arc-20160816; b=nt2+rQ7Mw2+arhMz9LRV2wp7Q8dQ3OeVtkvK/VSa3ozgWhAHYA/PiCzvXMWPLk1GgH 6Yu43Q/MCZmhcO8GWyB7iN69VevUTnwCj5B6hjsoiTxASd1OLAJyP9erj1BwgLheG0wR YeiEnJy+4Koqjz6uYseIoRmpEp0eUHe1nELASjN57BQUdC5CBSj44Fk9bBDFAMYgJbKl ISa9cn60gwjLxb7+3GPgd9WH/JrrJm6IVY5GnluUshpaGAq6p0Mf3H0F29o9ArlGoZGv ncBD7OIUutEgJBCjvHInRQdhAjg/wD8nkdQxKDRDsMqovZW/Mb6FnKeGf64JEb2+8SeK xLHw== 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=csu5NL0LcGdAQa43Ej8iJxCtW0p+sUgW7HHoiBz+t+E=; b=RzdoOyIUyWdOacEx9j/kagyYgBoDNbppBBA1m5mcVrDJW710aYokUzsdgeQTrjxPE0 hRDnZJXJj0bgIgeMBrfLfnPdwcG7LkfR3f94Yul5BbdGGrYo1AMWeSAZTNM0KcP3Y9pC sD3BOaaiRKMpwa0pgpoYvSWAhxasaJcr5ms7w3DYAW+VOPtmpQr2ehu/6FxW/getOPX0 4RWTrfrlJxecoqLRcXO1esucsLP1iYuAGy0rHZWJcIynciq6n0rE/mkIA2URu3LZUSK6 tx4evsoImcmmjCZwxRtQ9+MbZvD69eN4pxSIbs3DcLaSATq3ufiz6zyA7ZSDZ8tPMxpp Areg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="QgXRR/Uc"; 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 h6-20020a9d5546000000b0066dc58a8d9fsi6456466oti.48.2022.12.18.18.33.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Dec 2022 18:33:46 -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="QgXRR/Uc"; 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 6C05985003; Mon, 19 Dec 2022 03:33:25 +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="QgXRR/Uc"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D57AE85003; Mon, 19 Dec 2022 03:33:19 +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-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) (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 D86C98363D for ; Mon, 19 Dec 2022 03:33:12 +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=masahisa.kojima@linaro.org Received: by mail-pj1-x1035.google.com with SMTP id e7-20020a17090a77c700b00216928a3917so11551593pjs.4 for ; Sun, 18 Dec 2022 18:33:12 -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=csu5NL0LcGdAQa43Ej8iJxCtW0p+sUgW7HHoiBz+t+E=; b=QgXRR/UcMP+4i6vYLkVkhn0LMlNbMKkB7cRkczAJIW1L/Fk0ong1X41+wAG4PY9Wy2 htyJunZxnJzsQNIqxl5e91yQCFuB4aH2ZwONVtDea9RtQXWz6CqL05DPPO+TFnWb67kF uBOtuiCakM2F4DQp3pHAE8F+UPRXE8sBgckAL8YeNZd/NhNlWgr9zwDjx6WE3HiCCQde BXrSTTW1qeb7OvnAlSoKphu3CKu/54mGOCpWfB+IrxHrP4RRzVF5JO6U+6WgcpiNkDYQ 5e9xpdXMn4Gpfqx0eWRQJq7q5h5+uJqAOgrxlM+l67ELRk2EGDveKuSXjZkBwuOTF30e d9Xg== 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=csu5NL0LcGdAQa43Ej8iJxCtW0p+sUgW7HHoiBz+t+E=; b=lf73F1K7paqmHeE0ENBEPX+wO0X9F/wJXoM1qM0Psh4u9iEDzUkV1H5o2MZ2RWI1Dp z1IL7Mo1QL21/eWt0S0iHkZhPkRwXhlH2nUT/h76QXsLXvkFzI/zcXU0qIecuDv8Rb2b nJnW7gERLznx8hOAKpBadzgkfWv3zEGTsLwW/fUyiRdy27qkU8ImFux7oZbEfOGqkUJk olR+7rZElsd13Kdrg8aw50gkTTDV4Y+I8VcOZiXCypHGq/lD55Z+KSmFXvLEMXGvaOos X2FsqV6OszfODCjaR9h3ySuf7bMpTJqitKzQJhzQPP7altUO/FHJ+OPpYkNwFltfzKeX yVHg== X-Gm-Message-State: ANoB5pnA0k1aRIrxBxgayj0lMCjCiVyMiYK24ZjKDmzlskSZrCz3qGBL onhUbZBGCbJXYwzh6shxWPAHtDLSnmO5gFfl X-Received: by 2002:a17:90a:ce16:b0:218:a672:bcab with SMTP id f22-20020a17090ace1600b00218a672bcabmr41490797pju.14.1671417190122; Sun, 18 Dec 2022 18:33:10 -0800 (PST) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id ne8-20020a17090b374800b00219dab2d7a0sm4761523pjb.9.2022.12.18.18.33.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Dec 2022 18:33:09 -0800 (PST) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Masahisa Kojima Subject: [PATCH v2 2/2] eficonfig: avoid SetVariable between GetNextVariableName calls Date: Mon, 19 Dec 2022 11:33:13 +0900 Message-Id: <20221219023314.23959-3-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221219023314.23959-1-masahisa.kojima@linaro.org> References: <20221219023314.23959-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 retrieved 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 Reviewed-by: Heinrich Schuchardt Reviewed-by: Ilias Apalodimas --- Changes in v2: - fix typos - use '!guidcmp()' instead of 'guidcmp() == 0' - remove superfluous malloc() branch cmd/eficonfig.c | 54 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c index 0b07dfc958..ce7175a566 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_next_variable_name(&buf_size, &var_name16, &guid); - if (ret == EFI_NOT_FOUND) + if (ret == EFI_NOT_FOUND) { + /* + * EFI_NOT_FOUND indicates we retrieved all EFI variables. + * This should be treated as success. + */ + ret = EFI_SUCCESS; break; + } if (ret != EFI_SUCCESS) goto out; @@ -2349,31 +2356,46 @@ 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)) { + 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); - goto out; - } + /* + * The entire list of variables must be retrieved by + * efi_get_next_variable_name_int() before deleting the invalid + * boot option, just save the index here. + */ + if (i == count) { + p = realloc(delete_index_list, sizeof(u32) * + (list_size + 1)); + if (!p) { + ret = EFI_OUT_OF_RESOURCES; + goto out; } + delete_index_list = p; + delete_index_list[list_size++] = index; } } next: free(load_option); } + /* delete all invalid boot options */ + for (i = 0; i < list_size; i++) { + ret = delete_boot_option(delete_index_list[i]); + if (ret != EFI_SUCCESS) + goto out; + } + out: free(var_name16); + free(delete_index_list); return ret; }