From patchwork Mon Jun 13 09:38:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 581415 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5806:0:0:0:0 with SMTP id j6csp1798236max; Mon, 13 Jun 2022 02:39:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+S4cyejI92TudHloCK7EtSmM2ga1spkYrfuPHdDxe3/A/MlVgL0PQF4jRh8PgqhH6npT1 X-Received: by 2002:a05:6638:328a:b0:333:f1fe:79bc with SMTP id f10-20020a056638328a00b00333f1fe79bcmr2069161jav.170.1655113171734; Mon, 13 Jun 2022 02:39:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655113171; cv=none; d=google.com; s=arc-20160816; b=tZ2aq4K4hj+5PfDROxvOlogDT2yC42HUQKZOjGGDJDNYWzydtYc0fZG4X8VkqEDL1s kkr4pMcP6o85+wQXB8tbLPwTQlLz1EFXWPbRiUjHYBlvoiTQeW0ARZOtvMrf4ThzyPsb ULN2oAxbjL8G0cN80vjzVQgK70dC+yhJFIw9/lu6Nhn/cqTRb0Rt+Wa1vO/QPvObIq6S 4/34Qe7wwFH9LvQeiXnxiXQjjSVDH7xXZFV7+fl7SllMmZ9wDa95tZE2za3Tq1nn1G/+ S23LpiuqFCGMTZcVfukQr1If56n3T8R4eANtwaX4fachNFoY6Z7eHQLJNGS9owNNCodm UqeQ== 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=ipqYrDgdo+rU6I5+nXrw9+NuktsvngSgJ2DIkqyEAr0=; b=DuLz5d25XLjBfMf8TVQPhtwU8a1bny+MWiCC996rHLnPmcnVqpuu3iFz6LnlCGDxai azJItvIjCPBX97rzZUyeKqMae1+8nS4ubpiQyKSftopJpYgVIePQhRcP2112wkL08nD+ PfP5yavS1pC21tvJ08XqGYoTiSTvZy7dLGGhKbiPrlmAmc39luCMcTfiJa/Dg1Q8k5co rz3h7uBo5T8h4nfoFreIo2KflSfE1xbRoxm1BkCNax8D7QgIo/YCukFzGmj8i+pVkCY6 POiSSW30FVZFt5TVb6/pwQWkPplJRlgAUC4lOf8Lhy98IO7ue00fvI3cGijAhkNOA+WV LTXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=N6pmFh89; 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 10-20020a056e0216ca00b002cde0eaf204si8798700ilx.15.2022.06.13.02.39.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jun 2022 02:39:31 -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=N6pmFh89; 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 3018284455; Mon, 13 Jun 2022 11:38:42 +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="N6pmFh89"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3327984434; Mon, 13 Jun 2022 11:38:37 +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-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) (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 7E01C84442 for ; Mon, 13 Jun 2022 11:38:32 +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-pf1-x42d.google.com with SMTP id 187so5250181pfu.9 for ; Mon, 13 Jun 2022 02:38:32 -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=ipqYrDgdo+rU6I5+nXrw9+NuktsvngSgJ2DIkqyEAr0=; b=N6pmFh89V1sF3KjHxfatVKvUjrCNs/ZE97Rw0BdBSVQo35EuV4VuqKcyoJ+SplmbXj RBxtbI2+pIQhxQo74UvWWFboTAP7aSJH+6x/vuOV3qQ6I/Axw6Iq9tB1rL1d/sASMLff mvhYG1m0wfNcnLdSKWv5xfQ6PJx7oxDJpi+9n/kFBO/7vwrz8W4RK0UKSa6DzpxQxl5K l/RVNff9zEHVOXBX1GXlHqPHTfA7AxRYXxexSj9OGHMt970zNC3pvtoo47CpRUle1gRs HcEavXvG0TdAvlLnGlkjodBEf14Fbq5SiZ6RLRFlWvtOPgngc0lNVmnh1vgP/0O8SRhH tDWA== 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=ipqYrDgdo+rU6I5+nXrw9+NuktsvngSgJ2DIkqyEAr0=; b=HjcjvIxGuefGUI2zGWV84DTGHpkdOmUh871k0nYuqiqxOTgsnTzOOxhUF9ZbK2EZJB 0JXTeP89E8ZTp8yUBaUA1CRbcxNEGLyl7vFVffd9swULsE/swPeIsoOMB5oLyQH3v5bX 296D3IDxbY3fNx/g289QNTme1lmrMzt7pf1lu2wRBZ/ZAQkRY7Lvr+TJrOsD9RnCOt4E IuZDIKFcsUcl2DvsIVbuaXnLMphEixzBSC4RyivlCK/E2t9ceIrmRgTnHWCh4tN5PnxJ LayajuYdJCcncQ3etlhwQR9WE//iTzGQ10v25S0iTTt0meqxFX5zun/2AFBpfx4HbRUU GEgA== X-Gm-Message-State: AOAM530FTWeosVoZrPt0K43W45wTBjo5tFh1LpVAPDBGYe6XFdmLB47g fb8ftDKen1FQgTwOW+GC9M0w6geDH4YSiA== X-Received: by 2002:a62:7cd2:0:b0:51b:9ba6:a028 with SMTP id x201-20020a627cd2000000b0051b9ba6a028mr65986838pfc.24.1655113110643; Mon, 13 Jun 2022 02:38:30 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id q6-20020a170902a3c600b00163f7935772sm4629227plb.46.2022.06.13.02.38.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jun 2022 02:38:30 -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 v7 5/9] efimenu: add "Change Boot Order" menu entry Date: Mon, 13 Jun 2022 18:38:49 +0900 Message-Id: <20220613093853.23865-6-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220613093853.23865-1-masahisa.kojima@linaro.org> References: <20220613093853.23865-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 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 v7: - use UP/DOWN and PLUS/MINUS key to change to order no update in v6: cmd/efimenu.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) diff --git a/cmd/efimenu.c b/cmd/efimenu.c index 918e9dffa2..1be483a082 100644 --- a/cmd/efimenu.c +++ b/cmd/efimenu.c @@ -70,6 +70,13 @@ struct efimenu_file_entry_data { u16 *file_name; }; +struct efimenu_boot_order { + u32 num; + u16 *description; + u32 prev_index; + struct list_head list; +}; + /** * efimenu_print_msg() - print message * @@ -1359,6 +1366,205 @@ out: return ret; } +static void efimenu_display_change_boot_order(struct list_head *bo_list, + struct efimenu *efi_menu) +{ + bool reverse; + struct list_head *pos, *n; + struct efimenu_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 efimenu_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); + } +} + +static efi_status_t efimenu_choice_change_boot_order(struct list_head *bo_list, + struct efimenu *efi_menu) +{ + int esc = 0; + struct list_head *pos, *n; + struct efimenu_boot_order *tmp; + enum bootmenu_key key = KEY_NONE; + struct efimenu_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 efimenu_boot_order, list); + if (entry->num == efi_menu->active) + break; + } + tmp = list_entry(pos->prev, struct efimenu_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 - 1) { + list_for_each_safe(pos, n, bo_list) { + entry = list_entry(pos, struct efimenu_boot_order, list); + if (entry->num == efi_menu->active) + break; + } + tmp = list_entry(pos->next, struct efimenu_boot_order, list); + entry->num++; + tmp->num--; + list_del(&entry->list); + list_add(&entry->list, &tmp->list); + } + fallthrough; + case KEY_DOWN: + if (efi_menu->active < efi_menu->count - 1) + ++efi_menu->active; + return EFI_NOT_READY; + case KEY_SELECT: + return EFI_SUCCESS; + case KEY_QUIT: + return EFI_ABORTED; + default: + break; + } + } +} + +static efi_status_t efimenu_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 efimenu_boot_order *entry; + struct efimenu efi_menu; + + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); + if (!bootorder) { + 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 efimenu_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); + } + + efi_menu.delay = -1; + efi_menu.active = 0; + efi_menu.count = num; + + while (1) { + efimenu_display_change_boot_order(&bo_list, &efi_menu); + + ret = efimenu_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 efimenu_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 efimenu_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; +} + static efi_status_t efimenu_init(void) { efi_status_t ret; @@ -1393,6 +1599,7 @@ static efi_status_t efimenu_init(void) static const struct efimenu_item maintenance_menu_items[] = { {"Add Boot Option", efimenu_process_add_boot_option}, {"Edit Boot Option", efimenu_process_edit_boot_option}, + {"Change Boot Order", efimenu_process_change_boot_order}, {"Quit", efimenu_process_quit}, };