From patchwork Thu Apr 28 08:09:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 567138 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6886:0:0:0:0 with SMTP id m6csp5233678map; Thu, 28 Apr 2022 01:13:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdTzcQWgpMHQAv2q3nUF131g293az3zBXu4MszQgbPdRHF7e6Z3BK52ukP3SUMj3Jl2LFx X-Received: by 2002:a17:907:3f24:b0:6f3:c2df:3432 with SMTP id hq36-20020a1709073f2400b006f3c2df3432mr7647619ejc.614.1651133615319; Thu, 28 Apr 2022 01:13:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651133615; cv=none; d=google.com; s=arc-20160816; b=AVPr4z6KcpFUE54/b76trahRqwhE0zEFL3A4yYD8GpLiYoejum78zKkmxhqPtPwzN3 lbYTr0bpY/LQRiWNAbRSNXNEtCz6vSAWbcjtRQ+cr4yVaul42L0+v5nqsBZhQDlv0Ugf /2IqOeOD1pud8XYVCho9IXpnwBRh1H3TcXXBzVdGSKr7dljxEHKAY4bA2nz/VnHXJXPi iirJ/3T34kl8X4Olp6B+OFtHQWTtQENDOzSaY9tVuVJV312ST2FzqVYcvf3ve5Jqfwem kczOhSuCV8uTQMdTrM938vm/j2GWhcxDTz2Z3hlju97uT27fisWUdh3dSK0VhLUjdAhA zcfA== 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=lX31U9MXgpDF4WofAMeq6aQhG9iXZD3fdgc2+/bk0Lg=; b=iItGkS5US5cYaFwb6lEf155B2XTYLC/dHftS8wB9HjfVbBrCFj/LOoGUxsSXitNhUL X6SSeXkcogkW4WAA9YP61l5SNIRoqol2UjKl0SmHup/mS3Y+2ZadoF1wN0/TWbnlOhBY SmZTvFJbCTQtJQmpEN9YFu7HLbZ+sJzJsuDqN/qubXlWRgBPV/JURv1mYUrTfW44/42w 63/2wIugLdzVSUwy70RFpQ4UGbrgp3vp9GxKBg+Aqpj0tT2R32t9660i19dsDOApWC6K 3J70ctOQLcRC3d2s6TcVOzDLRgCQ9vVIUmlFUnLCmT64TE4xp7uucFQv1id5Ukn1axmY kCPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BA61FZDJ; 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 u11-20020aa7db8b000000b00424085b80c5si3286932edt.215.2022.04.28.01.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:13:35 -0700 (PDT) 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=BA61FZDJ; 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 0334983EE4; Thu, 28 Apr 2022 10:12:47 +0200 (CEST) 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="BA61FZDJ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2237A83565; Thu, 28 Apr 2022 10:11:32 +0200 (CEST) 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-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) (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 2F32683DCA for ; Thu, 28 Apr 2022 10:11:18 +0200 (CEST) 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-pl1-x62e.google.com with SMTP id p6so3677426plf.9 for ; Thu, 28 Apr 2022 01:11:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lX31U9MXgpDF4WofAMeq6aQhG9iXZD3fdgc2+/bk0Lg=; b=BA61FZDJ/CKRNRrY23WLeDN6CJySooO5kM3tw85lWnHnjwl/d+F5kNmumOOu86IfVX xLpjSekVN6BfUS/cynyg6r7JLg3+FEQ+EkxRKa1CCFLFQfE6UJLpuVZSrdkgfsKKuIbq 85fop11VKKYYJke7cf6VoqLJSxq6z1KtvZVNlpRqMWMy33dmjqtXJkoaGrT6suczFYYP 1HtbI/C5+YTmeCDTnbW8pwHSlOhHc4DZIE7vYjz1dhH30UfvjPnx9JM5M2MUv7UGt5m6 ciEO2oWZ+21oJWbfIC4R/SBLnqvwV1kIZiWhFF7eYGbu+BEZsFhYlFiIv65YDQCCbO8g asVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lX31U9MXgpDF4WofAMeq6aQhG9iXZD3fdgc2+/bk0Lg=; b=YBdwVy1cFFKS8XoCJF0OVarOP38A8Uxy6Hr8R8pn80PAjQGdlwiEIyRViUFyrZhflS cEOiF1+bbFumA0j+ExwcRuGxQaV92D+vhkPGsMOoiZ70s0OP7UtcoFDAAKrnPm5TMBWZ DSqZllSyXIPthS+m5Pthw+Tp+t0pVc9PVysClbis0d2t8qBjBk8SU5isDDBjDonHFY7d UzVaJdvXlznogfPvnSYMrPWO7oCC+9o/KYu2OGNSwFyhhcMX8pGJTARTrZMkAEh1bgn/ KSif2TpZwJ9cobNac/1QwBHBqd1hyD5HIUFSTiOOMhFiHyU18TMAb8jIxB2vp0b5Gc7y tE+w== X-Gm-Message-State: AOAM531gt3YsNmaMw187T6+Uxd8Z3jT+aobLkOJN/kgUJgIqQ/9ZEjy5 //mS1ZFZ85dJyiCLLMY3hgeJiL38f04GtA== X-Received: by 2002:a17:90a:4414:b0:1cd:4c16:e7eb with SMTP id s20-20020a17090a441400b001cd4c16e7ebmr48461955pjg.166.1651133476455; Thu, 28 Apr 2022 01:11:16 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id b15-20020a17090a7acf00b001cd4989ff5fsm5684259pjl.38.2022.04.28.01.11.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:11:16 -0700 (PDT) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Francois Ozog , Mark Kettenis , Masahisa Kojima Subject: [PATCH v5 14/17] efi_loader: menu-driven deletion of UEFI boot variable Date: Thu, 28 Apr 2022 17:09:47 +0900 Message-Id: <20220428080950.23509-15-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220428080950.23509-1-masahisa.kojima@linaro.org> References: <20220428080950.23509-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.5 at phobos.denx.de X-Virus-Status: Clean This commit adds the menu-driven deletion of UEFI boot variable. Signed-off-by: Masahisa Kojima --- Changes in v5: - split into the separate patch lib/efi_loader/efi_bootmenu_maintenance.c | 142 ++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/lib/efi_loader/efi_bootmenu_maintenance.c b/lib/efi_loader/efi_bootmenu_maintenance.c index 77401a7829..26320a8b73 100644 --- a/lib/efi_loader/efi_bootmenu_maintenance.c +++ b/lib/efi_loader/efi_bootmenu_maintenance.c @@ -70,6 +70,12 @@ struct efi_bootmenu_item { void *data; }; +struct efi_bootmenu_boot_selection_data { + u16 bootorder_index; + void *load_option; + int *selected; +}; + struct efi_bootmenu_boot_option { struct efi_simple_file_system_protocol *current_volume; struct efi_device_path *dp_volume; @@ -322,6 +328,89 @@ out: return ret; } +static efi_status_t efi_bootmenu_process_boot_selected(void *data, bool *exit) +{ + struct efi_bootmenu_boot_selection_data *info = data; + + *exit = true; + + if (info) + *info->selected = info->bootorder_index; + + return EFI_SUCCESS; +} + +static efi_status_t efi_bootmenu_show_boot_selection(u16 *bootorder, efi_uintn_t count, + int *selected) +{ + u32 i; + efi_status_t ret; + efi_uintn_t size; + void *load_option; + struct efi_load_option lo; + u16 varname[] = u"Boot####"; + struct efi_bootmenu_item *menu_item, *iter; + + menu_item = calloc(count + 1, sizeof(struct efi_bootmenu_item)); + if (!menu_item) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + + iter = menu_item; + for (i = 0; i < count; i++) { + efi_create_indexed_name(varname, sizeof(varname), + "Boot", bootorder[i]); + load_option = efi_get_var(varname, &efi_global_variable_guid, &size); + if (!load_option) + continue; + + ret = efi_deserialize_load_option(&lo, load_option, &size); + if (ret != EFI_SUCCESS) { + log_warning("Invalid load option for %ls\n", varname); + free(load_option); + continue; + } + + if (lo.attributes & LOAD_OPTION_ACTIVE) { + struct efi_bootmenu_boot_selection_data *info; + + info = calloc(1, sizeof(struct efi_bootmenu_boot_selection_data)); + if (!info) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + + info->bootorder_index = i; + info->load_option = load_option; + info->selected = selected; + iter->title = lo.label; + iter->func = efi_bootmenu_process_boot_selected; + iter->data = info; + iter++; + } + } + + /* add "Quit" entry */ + iter->title = u"Quit"; + iter->func = NULL; + iter->data = NULL; + count += 1; + + ret = efi_bootmenu_process_common(menu_item, count, -1); + +out: + iter = menu_item; + for (i = 0; i < count - 1; i++, iter++) { + free(((struct efi_bootmenu_boot_selection_data *)iter->data)->load_option); + free(iter->data); + } + + free(menu_item); + + return ret; +} + static efi_status_t efi_bootmenu_volume_selected(void *data, bool *exit) { struct efi_bootmenu_volume_entry_data *info = data; @@ -817,6 +906,58 @@ out: return ret; } +static efi_status_t delete_boot_option(u16 *bootorder, u16 index, efi_uintn_t size) +{ + u16 var_name[9]; + efi_status_t ret; + efi_uintn_t num; + + efi_create_indexed_name(var_name, sizeof(var_name), + "Boot", bootorder[index]); + ret = efi_set_variable_int(var_name, &efi_global_variable_guid, + 0, 0, NULL, false); + if (ret != EFI_SUCCESS) { + log_err("delete boot option(%ls) failed\n", var_name); + return ret; + } + + /* update BootOrder */ + num = size / sizeof(u16); + memmove(&bootorder[index], &bootorder[index + 1], + (num - index - 1) * sizeof(u16)); + size -= sizeof(u16); + ret = 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; +} + +static efi_status_t efi_bootmenu_process_delete_boot_option(void *data, bool *exit) +{ + int selected; + u16 *bootorder; + efi_status_t ret; + efi_uintn_t num, size; + + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); + if (!bootorder) { + ret = EFI_NOT_FOUND; + return ret; + } + + num = size / sizeof(u16); + ret = efi_bootmenu_show_boot_selection(bootorder, num, &selected); + if (ret == EFI_SUCCESS) + ret = delete_boot_option(bootorder, selected, size); + + free(bootorder); + + return ret; +} + static efi_status_t efi_bootmenu_init(void) { efi_status_t ret; @@ -845,6 +986,7 @@ static efi_status_t efi_bootmenu_init(void) static const struct efi_bootmenu_item maintenance_menu_items[] = { {u"Add Boot Option", efi_bootmenu_process_add_boot_option}, + {u"Delete Boot Option", efi_bootmenu_process_delete_boot_option}, {u"Quit", NULL}, };