From patchwork Wed Aug 24 04:27:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 599712 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:4388:0:0:0:0 with SMTP id w8csp2894333mae; Tue, 23 Aug 2022 21:27:02 -0700 (PDT) X-Google-Smtp-Source: AA6agR46AOJtl0/StN1sOWEIfbyfb++2JUsCHKHMD9RR6LbT1Tu8oFdjITisBgz/0TT3qQN73zLj X-Received: by 2002:aa7:da17:0:b0:447:519b:1ac9 with SMTP id r23-20020aa7da17000000b00447519b1ac9mr2741626eds.426.1661315222754; Tue, 23 Aug 2022 21:27:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661315222; cv=none; d=google.com; s=arc-20160816; b=nheSIRqTFc652hvt0d2eDc1DRc0H4GGlcEAKKN000vsxWOyGzcGEba4ckAiVxakt8r vIZe+bM3P6xlCM+d1OWUUlcbzODEfmk4syA8PaRWV06X87hXXXRO1mEmu/3fdWL16HCZ 1gN1Pgx5NGT1rxyud6DKZSoEy3cP5XF/hvaMEaod6dfKldFSbn0V2Kkdzwjpi8ALQJl1 NUd09hXDGLO7O5HUnkZYij2z1IyesrN5kFQsnvrH6caw86M6F1X/Nl11i5RWBXJ5L8nZ l3UorNpDrV1WzGSZrhuOAuom5qvzI8MXy/ZK/kMqsQeS4UfwNIOzjHIjvJVhZyw38diI tqjw== 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=94rzOrzG3r/de9UfFgNoHPvWRgtEDy/Cbijw5hFvyG8=; b=dnuRK9mgRGBHpxVh3molpJe4DFwd6U1BY+oJinKJfjjgS0lqJn6Z7t68P/BUcFLy3b 0IvYaVSvePqzxTxdc9Y9OrtrnS6r6UMr+GjrvB10o+KthD3+g/GH/dE3EMwYP8IoLMQm sH86XLxbH3jGdgWpniavVHax2U7PTT+37cLUW2kg6F9wa/lFkET6EgVCxiCM4t4EOXlZ P8SqYq7I7YAyC68dNr6LyiuI/d2S3E+H6tMH3CVBsAbCYNbI7VHE4U2kzvV3A6vtekCI kEbD2vemlyHgwfGiHTUNivWUlYawBM/V59aYAMoz5Vx7dd6PonmQKpumBQhiMeG19ktI Cuvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HCbeAh43; 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 a25-20020a509b59000000b00445e8087801si3287863edj.298.2022.08.23.21.27.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Aug 2022 21:27:02 -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=HCbeAh43; 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 EA7F68484C; Wed, 24 Aug 2022 06:26:57 +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="HCbeAh43"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7E57984840; Wed, 24 Aug 2022 06:26:56 +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-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) (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 1D64084811 for ; Wed, 24 Aug 2022 06:26:52 +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-pj1-x102e.google.com with SMTP id e19so14688177pju.1 for ; Tue, 23 Aug 2022 21:26:52 -0700 (PDT) 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; bh=94rzOrzG3r/de9UfFgNoHPvWRgtEDy/Cbijw5hFvyG8=; b=HCbeAh43S467rQxISWcfhYeTSzlrtMy/4PyOam5Q4PgDZq8xt6JCWXVOIw7ZVW8eoj D2m25o2ih3TPAjxfVJSoa/jV7h55zbuGtVWvbQRl04UuvMeq/4bbFpSey0eFm7ctBEXs snmmTCK9Z82IFeUCtmznr7xttTbJh8LhDCR3RyfL55pKpVqNVfBB3XSkN+ZCN5irjBEV kLi6U7UV7z0Iziod9+h9A9iHQqMTITk5j7140VGLXQTe3My+YKzsU9oZCwE6/WbkflZq skSs8CXLKufeJEyBGapBoYSd6S8zmQE4fwvNySepIy52QxAWBEGa0p6/b3/yUl/OOs5s ihMw== 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; bh=94rzOrzG3r/de9UfFgNoHPvWRgtEDy/Cbijw5hFvyG8=; b=BuxnbNTY+0670eh+UePj8w0treZwwdnVnYU9t/hIat155QHdZ1+QILDdtxsiid1H2l 08H0gkge8sFlFOfAKWiSiwsQWNapJG4Z6inv+4VUI3FSxOMcC5c/dECLAaZX2uu/DW7m 2vLzqRTJIQsm5q0NPxHe25TLKVsY0LttS2cuFny9F+i2eHEuxAAGFt3CLDYOZmxTWkfc PwXictdRwfC60CaJEj641mfYmJ04ODDUG3Md2g3rnV7a4LK7X/zMIrjE48bAYpdr5amf PKKZI47t+YmW0+j94u8Swg1v52OzgVbJOHZytIKrc6zXchBIIymBeTxhI163DZyef5L4 CJRQ== X-Gm-Message-State: ACgBeo0gJp1dP407kJxIqQ4WQVHgkrbHqTzMn6AqdQhAeNoMxWsJDrwR 74p3LWSLVpzrTEHhQtJKmuyq2v7qT8+YXw== X-Received: by 2002:a17:902:d4c4:b0:170:9fdb:4a2a with SMTP id o4-20020a170902d4c400b001709fdb4a2amr26169089plg.137.1661315209961; Tue, 23 Aug 2022 21:26:49 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id g21-20020a170902d1d500b0016397da033csm11280965plb.62.2022.08.23.21.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Aug 2022 21:26:49 -0700 (PDT) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Mark Kettenis , Masahisa Kojima Subject: [PATCH v12 2/9] eficonfig: add "Edit Boot Option" menu entry Date: Wed, 24 Aug 2022 13:27:42 +0900 Message-Id: <20220824042749.27434-3-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220824042749.27434-1-masahisa.kojima@linaro.org> References: <20220824042749.27434-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 This commit adds the menu entry to edit the existing BOOT#### variable contents. User selects the item from the boot option list, then user can edit the description, file path and optional_data. Note that automatically generated boot option entry by bootmenu to support the removable media device is filtered out and user can not edit the automatically generated entry. Signed-off-by: Masahisa Kojima --- No update since v11 Changes in v11: - remove BootOrder variable dependency - change the list load option order 1) in the order of BootOrder 2) remaing load option that is not included in the BootOrder - add check for the number of menu entry exceeds max - truncate the long load option label when user edits - add EFICONFIG_VOLUME_PATH_MAX to display text converted volume device path in case the volume does not exist Changes in v10: - update eficonfig_edit_boot_option() argument Changes in v9: - add function comment Changes in v8: - fix menu header string - fix function and structure prefix to "eficonfig" Newly created in v7 cmd/eficonfig.c | 277 +++++++++++++++++++++++++++++++++++++++++-- include/efi_config.h | 1 + 2 files changed, 269 insertions(+), 9 deletions(-) diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c index 9709222378..22389b537f 100644 --- a/cmd/eficonfig.c +++ b/cmd/eficonfig.c @@ -40,7 +40,7 @@ struct eficonfig_filepath_info { * * @file_info: user selected file info * @initrd_info: user selected initrd file info - * @boot_index: index of the UEFI BootOrder variable + * @boot_index: index of the boot option * @description: pointer to the description string * @optional_data: pointer to the optional_data * @edit_completed: flag indicates edit complete @@ -80,6 +80,17 @@ struct eficonfig_file_entry_data { u16 *file_name; }; +/** + * struct eficonfig_boot_selection_data - structure to be used to select the boot option entry + * + * @boot_index: index of the boot option + * @selected: pointer to store the selected index in the BootOrder variable + */ +struct eficonfig_boot_selection_data { + u16 boot_index; + int *selected; +}; + /** * eficonfig_print_msg() - print message * @@ -1143,34 +1154,58 @@ static efi_status_t prepare_file_selection_entry(struct efimenu *efi_menu, char { u32 len; efi_status_t ret; - u16 *file_name, *p; + u16 *file_name = NULL, *p; efi_handle_t handle; - char devname[BOOTMENU_DEVICE_NAME_MAX] = {0}; + char *devname; + + devname = calloc(1, EFICONFIG_VOLUME_PATH_MAX + 1); + if (!devname) + return EFI_OUT_OF_RESOURCES; /* get the device name only when the user already selected the file path */ handle = efi_dp_find_obj(file_info->dp_volume, NULL, NULL); if (handle) { - ret = efi_disk_get_device_name(handle, devname, BOOTMENU_DEVICE_NAME_MAX); + ret = efi_disk_get_device_name(handle, devname, EFICONFIG_VOLUME_PATH_MAX); if (ret != EFI_SUCCESS) - return ret; + goto out; + } + + /* + * If the preconfigured volume does not exist in the system, display the text + * converted volume device path instead of U-Boot friendly name(e.g. "usb 0:1"). + */ + if (!handle && file_info->dp_volume) { + u16 *dp_str; + char *q = devname; + + dp_str = efi_dp_str(file_info->dp_volume); + if (dp_str) + utf16_utf8_strncpy(&q, dp_str, EFICONFIG_VOLUME_PATH_MAX); + + efi_free_pool(dp_str); } /* append u'/' to devname, it is just for display purpose. */ if (file_info->current_path[0] != u'\0' && file_info->current_path[0] != u'/') - strlcat(devname, "/", BOOTMENU_DEVICE_NAME_MAX); + strlcat(devname, "/", EFICONFIG_VOLUME_PATH_MAX + 1); len = strlen(devname); len += utf16_utf8_strlen(file_info->current_path) + 1; file_name = calloc(1, len * sizeof(u16)); - if (!file_name) - return ret; + if (!file_name) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } p = file_name; utf8_utf16_strcpy(&p, devname); u16_strlcat(file_name, file_info->current_path, len); ret = create_boot_option_entry(efi_menu, title, file_name, eficonfig_select_file_handler, file_info); +out: + free(devname); free(file_name); + return ret; } @@ -1337,10 +1372,14 @@ static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_bo if (ret != EFI_SUCCESS) goto out; - if (!lo.label || (lo.label && u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX)) { + if (!lo.label) { ret = EFI_INVALID_PARAMETER; goto out; } + /* truncate the long label string */ + if (u16_strlen(lo.label) >= EFICONFIG_DESCRIPTION_MAX) + lo.label[EFICONFIG_DESCRIPTION_MAX - 1] = u'\0'; + u16_strcpy(bo->description, lo.label); /* EFI image file path is a first instance */ @@ -1458,6 +1497,225 @@ out: return ret; } +/** + * eficonfig_process_boot_selected() - handler to select boot option entry + * + * @data: pointer to the data for each entry + * Return: status code + */ +static efi_status_t eficonfig_process_boot_selected(void *data) +{ + struct eficonfig_boot_selection_data *info = data; + + if (info) + *info->selected = info->boot_index; + + return EFI_SUCCESS; +} + +/** + * search_bootorder() - search the boot option index in BootOrder + * + * @bootorder: pointer to the BootOrder variable + * @num: number of BootOrder entry + * @target: target boot option index to search + * @index: pointer to store the index of BootOrder variable + * Return: true if exists, false otherwise + */ +static bool search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index) +{ + u32 i; + + for (i = 0; i < num; i++) { + if (target == bootorder[i]) { + if (index) + *index = i; + + return true; + } + } + + return false; +} + +/** + * eficonfig_add_boot_selection_entry() - add boot option menu entry + * + * @efi_menu: pointer to store the efimenu structure + * @boot_index: boot option index to be added + * @selected: pointer to store the selected boot option index + * Return: status code + */ +static efi_status_t eficonfig_add_boot_selection_entry(struct efimenu *efi_menu, + unsigned int boot_index, + unsigned int *selected) +{ + char *buf, *p; + efi_status_t ret; + efi_uintn_t size; + void *load_option; + struct efi_load_option lo; + u16 varname[] = u"Boot####"; + struct eficonfig_boot_selection_data *info; + + efi_create_indexed_name(varname, sizeof(varname), "Boot", boot_index); + load_option = efi_get_var(varname, &efi_global_variable_guid, &size); + if (!load_option) + return EFI_SUCCESS; + + 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); + return ret; + } + + if (size >= sizeof(efi_guid_t) && + !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) { + /* + * auto generated entry has GUID in optional_data, + * skip auto generated entry because it will be generated + * again even if it is edited or deleted. + */ + free(load_option); + return EFI_SUCCESS; + } + + info = calloc(1, sizeof(struct eficonfig_boot_selection_data)); + if (!info) { + free(load_option); + return EFI_OUT_OF_RESOURCES; + } + + buf = calloc(1, utf16_utf8_strlen(lo.label) + 1); + if (!buf) { + free(load_option); + free(info); + return EFI_OUT_OF_RESOURCES; + } + p = buf; + utf16_utf8_strcpy(&p, lo.label); + info->boot_index = boot_index; + info->selected = selected; + ret = append_entry(efi_menu, buf, eficonfig_process_boot_selected, info); + if (ret != EFI_SUCCESS) { + free(load_option); + free(info); + return ret; + } + free(load_option); + + return EFI_SUCCESS; +} + +/** + * eficonfig_show_boot_selection() - construct boot option menu entry + * + * @selected: pointer to store the selected boot option index + * Return: status code + */ +static efi_status_t eficonfig_show_boot_selection(unsigned int *selected) +{ + u32 i; + u16 *bootorder; + efi_status_t ret; + efi_uintn_t num, size; + struct efimenu *efi_menu; + + efi_menu = calloc(1, sizeof(struct efimenu)); + if (!efi_menu) + return EFI_OUT_OF_RESOURCES; + + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); + + INIT_LIST_HEAD(&efi_menu->list); + num = size / sizeof(u16); + /* list the load option in the order of BootOrder variable */ + for (i = 0; i < num; i++) { + ret = eficonfig_add_boot_selection_entry(efi_menu, bootorder[i], selected); + if (ret != EFI_SUCCESS) + goto out; + + if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1) + break; + } + + /* list the remaining load option not included in the BootOrder */ + for (i = 0; i <= 0xFFFF; i++) { + /* If the index is included in the BootOrder, skip it */ + if (search_bootorder(bootorder, num, i, NULL)) + continue; + + ret = eficonfig_add_boot_selection_entry(efi_menu, i, selected); + if (ret != EFI_SUCCESS) + goto out; + + if (efi_menu->count >= EFICONFIG_ENTRY_NUM_MAX - 1) + break; + } + + ret = append_quit_entry(efi_menu); + if (ret != EFI_SUCCESS) + goto out; + + ret = eficonfig_process_common(efi_menu, " ** Select Boot Option **"); +out: + eficonfig_destroy(efi_menu, true); + + return ret; +} + +/** + * eficonfig_process_edit_boot_option() - handler to edit boot option + * + * @data: pointer to the data for each entry + * Return: status code + */ +static efi_status_t eficonfig_process_edit_boot_option(void *data) +{ + efi_status_t ret; + efi_uintn_t size; + struct eficonfig_boot_option *bo = NULL; + + while (1) { + unsigned int selected; + void *load_option; + u16 varname[] = u"Boot####"; + + ret = eficonfig_show_boot_selection(&selected); + if (ret != EFI_SUCCESS) + break; + + bo = calloc(1, sizeof(struct eficonfig_boot_option)); + if (!bo) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + + bo->boot_index = selected; + efi_create_indexed_name(varname, sizeof(varname), "Boot", selected); + load_option = efi_get_var(varname, &efi_global_variable_guid, &size); + if (!load_option) { + free(bo); + ret = EFI_NOT_FOUND; + goto out; + } + + ret = eficonfig_edit_boot_option(varname, bo, load_option, size, + " ** Edit Boot Option ** "); + + free(load_option); + free(bo); + if (ret != EFI_SUCCESS && ret != EFI_ABORTED) + break; + } +out: + /* to stay the parent menu */ + ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret; + + return ret; +} + /** * eficonfig_init() - do required initialization for eficonfig command * @@ -1487,6 +1745,7 @@ static efi_status_t eficonfig_init(void) static const struct eficonfig_item maintenance_menu_items[] = { {"Add Boot Option", eficonfig_process_add_boot_option}, + {"Edit Boot Option", eficonfig_process_edit_boot_option}, {"Quit", eficonfig_process_quit}, }; diff --git a/include/efi_config.h b/include/efi_config.h index 2a4052ba3d..974d8ce1ee 100644 --- a/include/efi_config.h +++ b/include/efi_config.h @@ -11,6 +11,7 @@ #include #define EFICONFIG_ENTRY_NUM_MAX 99 +#define EFICONFIG_VOLUME_PATH_MAX 512 #define EFICONFIG_FILE_PATH_MAX 512 #define EFICONFIG_FILE_PATH_BUF_SIZE (EFICONFIG_FILE_PATH_MAX * sizeof(u16))