From patchwork Fri Jul 15 14:47:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 590850 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5817:0:0:0:0 with SMTP id j23csp570539max; Fri, 15 Jul 2022 07:46:39 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s/Zlkn3zdebZbjXaJHGCTqSgXFaL2vJesUUM6Sa3AWYNhWncAbeE0aBhYy4UZJ3+qQk6OM X-Received: by 2002:a05:6e02:190a:b0:2dc:852c:bb4a with SMTP id w10-20020a056e02190a00b002dc852cbb4amr7551710ilu.208.1657896398028; Fri, 15 Jul 2022 07:46:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657896398; cv=none; d=google.com; s=arc-20160816; b=ZPbfQpXIUnW88bOnKlcePn7qUuaIr8sAesKLjH4FAco6euV0ljdR5OHjJtxa+zjwn4 H0M3RiX1laloOsokbSSH8jFGrPfjE9NkPiVgkzBd9Iizz3HriLpS4EhZDTGqOxB/tO6q 5WtUAayFEfrve6zbG47TN90TQQ6Q1Jzz1ISslj4WBzN6sFYb7q8H0pHs3rAN0A4HlqKI pNELmdQcXEJoP2keKlS7vBVAJ1GR6mdNs/pW5jEakDBt3Q7aA65XXlAmZyk/683EFr0z Qn5zdDPrLTSM/G4W6ZZGSXYLWanVFrrc4wtcgBanwt81yhZq6dg7lNiWgt68SyxC+vC+ 14+g== 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=M/lT3nJ1t8yhYNtoubxwDJTFByAtQ3M/a3iGy6nNdVU=; b=anpxjlzDgbTRVwtQcVJ+40WZ7fJ9fXFSrYTISQm4Jjjj/IzBJSOZ57YigLYM9q6VZV g273tciU87/PKaJjtCoduZF7iUIUTYrXX3ZRm0Ts6/Af8XidFNlDUa5PYcfzqtDo4cRT 0LdG/Llpk+2QBusCI6Q7XBNVTkNjxKHjtv8ECzE6ajzfQyMQiCSW4zumSCVgSYlFxC4v Dhr6w+iJWoBZXhRsMGch1IrlgXfa+Z9Ibof/QWF2hbOsaeK2RO+uF65D2VZ4kUR19bBR +R6uDVDB0lBmWTBehN4MRTaLEYTFaIzENLuF9zfvV29zLbIo6SbKTqt6nh15N1JVkoW3 P5UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CjhArkIy; 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 t2-20020a056638204200b00339e612b77fsi7188956jaj.29.2022.07.15.07.46.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jul 2022 07:46:38 -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=CjhArkIy; 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 DBB1881E76; Fri, 15 Jul 2022 16:45:48 +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="CjhArkIy"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9408681DE8; Fri, 15 Jul 2022 16:45:38 +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-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) (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 6093481110 for ; Fri, 15 Jul 2022 16:45:26 +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-pg1-x532.google.com with SMTP id bh13so4596696pgb.4 for ; Fri, 15 Jul 2022 07:45:26 -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=M/lT3nJ1t8yhYNtoubxwDJTFByAtQ3M/a3iGy6nNdVU=; b=CjhArkIyqguys2DloMFTfWCJ4wlgU/OsU2sBy/ihfUeObx6EwWa9vt6rdFZJmmXpFz sB/OlIqnIYtBUJ4RQVdrEOHfm+7iEXLYvcbbTWuzTTGta5If4ZeLU2reGDx4eWPk6PuM G/kk9Gay96tNnUmBDIIAd/FEeome4mcmApNjL1m8SbEd487vcWVF5d75sn0iU0E9bc1K ZQTWBbxGrMJx2ZipyT1BecDabJLRiq3RBefI4e80ufgtUnSkO6lMvxun+mO11e5A0DVs IbyQNQUQbbyg+R4IPg1krBmGUMjRqHKo5XgdsbEsOjVIHXnMI7HNg+YR9WnjdvFx1nNf DKmw== 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=M/lT3nJ1t8yhYNtoubxwDJTFByAtQ3M/a3iGy6nNdVU=; b=UI7fXn3Rh7pnSfQfTaHoGXGf01EtGCCplGgVukxE6hfIsXG2Vb7PrrtWk7i67Aae6H DANBLFNbI2tyWCSARVNb/gwwUPjFHbCNQl0boDfTFfuYPJe6i/EVOB5GUc+VtLi4N0M5 otrxnvnLOHaDOb5ZVKv8nLYyRA5ozmuqOBpNftj+eeZuE9gCbG1ll3S4wTP9otX14UZB yxzPNX4g3YCeZ77C7eJoiCXiAR0X8YziWi6FzhXGBrNV5wSAtoWNG8U7RLufh9aMKDZL TMok4IvmR8Vf8cXa2JMK1U99LFNh7ZgPrxtTX7c+k7cPXgZY/5J0TV0Ng9EC6sv79pFn 6hpA== X-Gm-Message-State: AJIora8uDUWYTFW297UwNKcSfcTnaQ3lIJod1HqlBJVGRLWEgYr3lqtV FBVKwdLTAhjkP0j4W4sH9LlkJTxN0+qObw== X-Received: by 2002:a63:2c89:0:b0:411:66bf:9efc with SMTP id s131-20020a632c89000000b0041166bf9efcmr12235232pgs.589.1657896324360; Fri, 15 Jul 2022 07:45:24 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id u16-20020a170903125000b001690d398401sm3719923plh.88.2022.07.15.07.45.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Jul 2022 07:45:24 -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: [RESEND v9 5/9] eficonfig: add "Change Boot Order" menu entry Date: Fri, 15 Jul 2022 23:47:45 +0900 Message-Id: <20220715144749.30564-6-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220715144749.30564-1-masahisa.kojima@linaro.org> References: <20220715144749.30564-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 update UEFI BootOrder variable. User moves the entry with UP/DOWN key, changes the order with PLUS/MINUS key, then finalizes the order by ENTER key. The U-Boot menu framework is well designed for static menu, this commit implements the own menu display and key handling for dynamically change the order of menu entry. Signed-off-by: Masahisa Kojima --- Changes in v9: - add function comment Changes in v8: - add "Save" and "Quit" entries Changes in v7: - use UP/DOWN and PLUS/MINUS key to change to order no update in v6: cmd/eficonfig.c | 265 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c index d923afe726..aeb60f1d09 100644 --- a/cmd/eficonfig.c +++ b/cmd/eficonfig.c @@ -89,6 +89,21 @@ struct eficonfig_boot_selection_data { int *selected; }; +/** + * struct eficonfig_boot_order - structure to be used to update BootOrder variable + * + * @num: index in the menu entry + * @description: pointer to the description string + * @prev_index: index in the BootOrder variable before changing order + * @list: list structure + */ +struct eficonfig_boot_order { + u32 num; + u16 *description; + u32 prev_index; + struct list_head list; +}; + /** * eficonfig_print_msg() - print message * @@ -1447,6 +1462,255 @@ out: return ret; } +/** + * eficonfig_display_change_boot_order() - display the BootOrder list + * + * @bo_list: pointer to the list structure + * @efi_menu: pointer to the efimenu structure + * Return: status code + */ +static void eficonfig_display_change_boot_order(struct list_head *bo_list, + struct efimenu *efi_menu) +{ + bool reverse; + struct list_head *pos, *n; + struct eficonfig_boot_order *entry; + + printf(ANSI_CLEAR_CONSOLE + ANSI_CURSOR_POSITION + "\n ** Change Boot Order **\n" + ANSI_CURSOR_POSITION + "\n" + " Press UP/DOWN to move, +/- to change order\n ENTER to complete, ESC/CTRL+C to quit" + ANSI_CURSOR_POSITION, + 0, 1, efi_menu->count + 5, 1, 4, 1); + + /* draw boot order list */ + list_for_each_safe(pos, n, bo_list) { + entry = list_entry(pos, struct eficonfig_boot_order, list); + reverse = (entry->num == efi_menu->active); + puts(" "); + if (reverse) + puts(ANSI_COLOR_REVERSE); + + printf("%ls\n", entry->description); + + if (reverse) + puts(ANSI_COLOR_RESET); + } +} + +/** + * eficonfig_choice_change_boot_order() - handle the BootOrder update + * + * @bo_list: pointer to the list structure + * @efi_menu: pointer to the efimenu structure + * Return: status code + */ +static efi_status_t eficonfig_choice_change_boot_order(struct list_head *bo_list, + struct efimenu *efi_menu) +{ + int esc = 0; + struct list_head *pos, *n; + struct eficonfig_boot_order *tmp; + enum bootmenu_key key = KEY_NONE; + struct eficonfig_boot_order *entry; + + while (1) { + bootmenu_loop(NULL, &key, &esc); + + switch (key) { + case KEY_PLUS: + if (efi_menu->active > 0) { + list_for_each_safe(pos, n, bo_list) { + entry = list_entry(pos, struct eficonfig_boot_order, list); + if (entry->num == efi_menu->active) + break; + } + tmp = list_entry(pos->prev, struct eficonfig_boot_order, list); + entry->num--; + tmp->num++; + list_del(&tmp->list); + list_add(&tmp->list, &entry->list); + } + fallthrough; + case KEY_UP: + if (efi_menu->active > 0) + --efi_menu->active; + return EFI_NOT_READY; + case KEY_MINUS: + if (efi_menu->active < efi_menu->count - 3) { + list_for_each_safe(pos, n, bo_list) { + entry = list_entry(pos, struct eficonfig_boot_order, list); + if (entry->num == efi_menu->active) + break; + } + tmp = list_entry(pos->next, struct eficonfig_boot_order, list); + entry->num++; + tmp->num--; + list_del(&entry->list); + list_add(&entry->list, &tmp->list); + + ++efi_menu->active; + } + return EFI_NOT_READY; + case KEY_DOWN: + if (efi_menu->active < efi_menu->count - 1) + ++efi_menu->active; + return EFI_NOT_READY; + case KEY_SELECT: + /* "Save" */ + if (efi_menu->active == efi_menu->count - 2) + return EFI_SUCCESS; + + /* "Quit" */ + if (efi_menu->active == efi_menu->count - 1) + return EFI_ABORTED; + + break; + case KEY_QUIT: + return EFI_ABORTED; + default: + break; + } + } +} + +/** + * eficonfig_process_change_boot_order() - handler to change boot order + * + * @data: pointer to the data for each entry + * Return: status code + */ +static efi_status_t eficonfig_process_change_boot_order(void *data) +{ + u32 i; + u16 *bootorder; + efi_status_t ret; + efi_uintn_t num, size; + struct list_head bo_list; + struct list_head *pos, *n; + struct eficonfig_boot_order *entry; + struct efimenu efi_menu; + + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); + if (!bootorder) { + eficonfig_print_msg("BootOrder is not defined!"); + ret = EFI_NOT_FOUND; + return ret; + } + + INIT_LIST_HEAD(&bo_list); + + num = size / sizeof(u16); + for (i = 0; i < num; i++) { + void *load_option; + struct efi_load_option lo; + u16 varname[] = u"Boot####"; + + efi_create_indexed_name(varname, sizeof(varname), + "Boot", bootorder[i]); + load_option = efi_get_var(varname, &efi_global_variable_guid, &size); + if (!load_option) { + ret = EFI_NOT_FOUND; + goto out; + } + + ret = efi_deserialize_load_option(&lo, load_option, &size); + if (ret != EFI_SUCCESS) { + free(load_option); + goto out; + } + + entry = calloc(1, sizeof(struct eficonfig_boot_order)); + if (!entry) { + free(load_option); + goto out; + } + entry->num = i; + entry->description = u16_strdup(lo.label); + entry->prev_index = i; + list_add_tail(&entry->list, &bo_list); + + free(load_option); + } + + /* add "Save" and "Quit" entries */ + entry = calloc(1, sizeof(struct eficonfig_boot_order)); + if (!entry) + goto out; + + entry->num = i++; + entry->description = u16_strdup(u"Save"); + entry->prev_index = 0; + list_add_tail(&entry->list, &bo_list); + + entry = calloc(1, sizeof(struct eficonfig_boot_order)); + if (!entry) + goto out; + + entry->num = i++; + entry->description = u16_strdup(u"Quit"); + entry->prev_index = 0; + list_add_tail(&entry->list, &bo_list); + + efi_menu.delay = -1; + efi_menu.active = 0; + efi_menu.count = i; + + while (1) { + eficonfig_display_change_boot_order(&bo_list, &efi_menu); + + ret = eficonfig_choice_change_boot_order(&bo_list, &efi_menu); + if (ret == EFI_SUCCESS) { + u32 i = 0; + u16 *new_bootorder; + + /* update the BootOrder variable */ + size = num * sizeof(u16); + new_bootorder = calloc(1, size); + if (!new_bootorder) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + + list_for_each_safe(pos, n, &bo_list) { + entry = list_entry(pos, struct eficonfig_boot_order, list); + new_bootorder[i] = bootorder[entry->prev_index]; + i++; + } + + ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, + EFI_VARIABLE_NON_VOLATILE | + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS, + size, new_bootorder, false); + + free(new_bootorder); + goto out; + } else if (ret == EFI_NOT_READY) { + continue; + } else { + goto out; + } + } + +out: + list_for_each_safe(pos, n, &bo_list) { + entry = list_entry(pos, struct eficonfig_boot_order, list); + list_del(&entry->list); + free(entry->description); + free(entry); + } + + free(bootorder); + + /* to stay the parent menu */ + ret = (ret == EFI_ABORTED) ? EFI_NOT_READY : ret; + + return ret; +} + /** * eficonfig_init() - do required initialization for eficonfig command * @@ -1477,6 +1741,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}, + {"Change Boot Order", eficonfig_process_change_boot_order}, {"Quit", eficonfig_process_quit}, };