From patchwork Mon May 16 11:00:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 572965 Delivered-To: patch@linaro.org Received: by 2002:a5d:5051:0:0:0:0:0 with SMTP id h17csp499306wrt; Mon, 16 May 2022 04:00:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0q9Ar1f9L7816rqbgd0N0EbC66YXxtJjTFjc/dJwUU0KGeqZCxIgVaw8eTCbHRntpG7CC X-Received: by 2002:ac2:456e:0:b0:472:12c9:42b5 with SMTP id k14-20020ac2456e000000b0047212c942b5mr12564635lfm.211.1652698856418; Mon, 16 May 2022 04:00:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652698856; cv=none; d=google.com; s=arc-20160816; b=FDtguYSPCRV3R+Sjl3hRQqj0M1Jcw49FTYUQUyO/tHhEx0t9YGThXYxGd3O0N8kkLF SXPSollaGoXWBSwrqF6CaK8NJyJ31WHo1vl6JCQuLK/aF9hrk1E8jl8wpLjCYJV4h9AI q2Zs8K6yD4WMvEZOeLpS8TrpoPskQNogdINO7BhKL5IrDiqINppmYa8Ga/B4xcY8OSn4 SKeR7nsMKFcwZdNRLBQkyqrpZsTu/D+SDouJ78YVjPeFEPvi9mHDdbCfW2wy5PiMaqHu z4re0hLGj2+JHeGrf1YP2EwlQZ52ewNFXc8XRVLj/J4I3uqydD0Os34HRLT9i1MPa9nY ms8g== 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=TbM8M76th8Lcwt6380876PJmTxre9hCFZOUodC16uQ8=; b=NPElOjN7peaDy9kDkIWrXRSGlPfTvOO9xYdxPAtIyAZ32GaHSveM1d1rCXmoRR3CYt zhVUdItxvLC9x/A7yqt9H6oah+umk86vhbBGofW7yK9tNhjZ9VgO43aDHFO16X7ksLBQ 1cgmXyCNFymHEwK0nwluckqLoS4LQ6uHkepXzoR6lCqR1w9nnNFFjiofQ70oXk/cyhup nF0jAI+Q8SevH9VMhzzRI2fftK0la0G3BVY2NtYIWg3tGiTWN7LbGYTN2Em+ZZ9+vnRt +nYKnO3FFgocZufvDRBzJqAw3Xhx2Z2MZ24/WToJaSGqhhAHO8OhFzZCwW2ooZKhSYK4 /yXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=OUR6NV+s; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id k9-20020a05651210c900b004778ecb218esi5224005lfg.467.2022.05.16.04.00.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 04:00:56 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=OUR6NV+s; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 18EC584197; Mon, 16 May 2022 13:00:55 +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="OUR6NV+s"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CC3DE84199; Mon, 16 May 2022 13:00:36 +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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) (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 D248A84099 for ; Mon, 16 May 2022 13:00:31 +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-x62f.google.com with SMTP id d17so14096908plg.0 for ; Mon, 16 May 2022 04:00:31 -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=TbM8M76th8Lcwt6380876PJmTxre9hCFZOUodC16uQ8=; b=OUR6NV+sjp/YE+X9bN+MWZYQO7QeVPnYaqR4h7v6JfTor4xHvacNn1TsBb2MTKI7cQ HA4Cih4LdEk/Gb6TuwMTRmU+hLWg7Kz64F47ZDG4vTRLd4f8XxqT9UdhouwSbXG1xZyr lHucCUfPbwgb6VaBktv8VluwRjRdrnKzBL6J6S/b+ki1DIyEb+3HbMGG76Cpl2hIko8H XTH+T6cByFw0J/ouIbn8I+lwO31gQKCxQ1W2aOAzfis4zfjD/XoJoYvCKT5Tun7ki6Sz J3OduKdnrzlwpflVdIwTHJ42ouuUssGN4j+n6ytNregXZIawkcY3tTCLGy46XZrfx2GD J0Dw== 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=TbM8M76th8Lcwt6380876PJmTxre9hCFZOUodC16uQ8=; b=ZKQhmMFTogXik5xMMx/uyfaXQsH5Lim970C1GXmR6y996ZADK8oauq00Mm/N/0A+SC UG4/BwdbLvaHbWQtyvnHl5ZB0kImy2gEYVsRpR3c2z1bOtY4aGaEcgSy+bbzu0wWFmcm eJF9hD3fsnbaj95OGAbElTEkIgC3RZTmQj93tzVcG/3kEPnvY5ViPpTgurfjQ0LuUNFJ MynhaHYong8Kip738AG9HP+xT1oKH20j7o2LZr84PMfgsM3QcNv5mgDW5DRw+dJ6Sicd 8js1YRmhiY/ZUi++c+ffvLRbZbilaIOyaTmY6fuWqWbM54ldcLmAKHoXvtwFF4WAoHNX +kew== X-Gm-Message-State: AOAM530rQsgvCEfxe40fQkOW48STWw8y/NWAsTiyc3W8tlhr5mkNxjI5 49DtKmfRpQ0f7/+Ioa0kqz8RECrD6vQT2w== X-Received: by 2002:a17:903:22ce:b0:15e:bd57:5bec with SMTP id y14-20020a17090322ce00b0015ebd575becmr16857089plg.114.1652698829507; Mon, 16 May 2022 04:00:29 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id q2-20020a170902edc200b0016173113c50sm2446480plk.92.2022.05.16.04.00.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 04:00:29 -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 v6 2/6] efi_loader: menu-driven deletion of UEFI boot variable Date: Mon, 16 May 2022 20:00:38 +0900 Message-Id: <20220516110043.31480-3-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220516110043.31480-1-masahisa.kojima@linaro.org> References: <20220516110043.31480-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 --- (no update sinch v5) changes in v5: - split into the separate patch lib/efi_loader/efi_bootmenu_maintenance.c | 136 ++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/lib/efi_loader/efi_bootmenu_maintenance.c b/lib/efi_loader/efi_bootmenu_maintenance.c index e5124a8a21..96306cd2cc 100644 --- a/lib/efi_loader/efi_bootmenu_maintenance.c +++ b/lib/efi_loader/efi_bootmenu_maintenance.c @@ -334,6 +334,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; @@ -861,6 +944,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; @@ -889,6 +1024,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}, };