From mboxrd@z Thu Jan 1 00:00:00 1970 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 29644C433F5 for ; Mon, 16 May 2022 11:00:57 +0000 (UTC) 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) 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-Google-Smtp-Source: ABdhPJzkeg0CBr9lN0c3G9Rzj4D0z4jYhuh/Y5OxuJhmJnVLAvZXwFInWP80zxmCzlYQXBXI7uITxQ== 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}, }; -- 2.17.1