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 703AAC433F5 for ; Thu, 28 Apr 2022 08:14:13 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 07B5083F0A; Thu, 28 Apr 2022 10:12:59 +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="kiCjO3dJ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5577483EDF; Thu, 28 Apr 2022 10:11:38 +0200 (CEST) Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) (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 0CFEB83ED7 for ; Thu, 28 Apr 2022 10:11:21 +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-x62c.google.com with SMTP id n18so3693989plg.5 for ; Thu, 28 Apr 2022 01:11:20 -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=iKVe0Cu41bsyIFo0sDuKev4Fa1eoKYlqHnpLTZDzMaA=; b=kiCjO3dJ74DggUmxPuO3foZf6TGhSyaL+537pC9dyn2flqX8WyvkCW77L/nKy/XUbk Cjr+ZjuFvcBsnHVzQthFW7J8BF7k3Z3FofXkhFxZYYxJ5P0bJ1YCrmyWtKpMteajf9yx mB2Sn3rRZ68qun5Zs5wuLmrlVGx1gzgGzusUpz3DpwMGPtalutIyMQzqFupbXFFO74be x0R2qsLrWuP5J/PU8nMeVK3TxWepjMJVEVa9Fp4kaneqlibzNreIZ4dg7cUwyWcwyzLg NTWpZIWyutMT9bARvWtWqG947RGJNugW1QoV4NXgoY3YtKQwgBhJS0J2HFlYCKocmpt0 cUPA== 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=iKVe0Cu41bsyIFo0sDuKev4Fa1eoKYlqHnpLTZDzMaA=; b=nKM3ZWNRNhAgonq6zQv6X77hTh1n4F2/LclXBKMCVBEco4E6un1DmGg+zD21BgaRIz z4eFecBsM0EXChTvqiRW+JWCR6fl8VDuYjz4cd97SuC2fJG51zVHpgCN74YU3MdgmiIp 07xHtpX+VCalsEg0Kx9oBnNHoAYr5rPI3kJMQpsnsJvVua7Yrjv3rCEEq/joyqCq1GQn jQi+gzY5EFnN44KRMI1gnrOqNAYXXQfoQE4Cq/ot9jE8u5XttxFqEcoSyiPRrMz3TAIz qu5REnHCzpbwLFLxY5qpWH8PGC0auqnG1t+T0qJ4OA3e12+zPTjCyiJGxAGvPr1D7TJM nwmQ== X-Gm-Message-State: AOAM530adrMpsJKjhH6FFLafOipGbCWxtMkFsPF7iBYFN5jDR4ztKHnQ 4wylmGinBmVCkV9/x0pzNlRlXB61kYm3vQ== X-Google-Smtp-Source: ABdhPJzgmf4UR/ocClXE5r0g0NgRjjXRaRo8ppGCTfcfW7Emh+C1RN3X/0lZ1gL4SkW1KKw2ZeRXEA== X-Received: by 2002:a17:90a:ab90:b0:1da:375f:2f44 with SMTP id n16-20020a17090aab9000b001da375f2f44mr8180160pjq.33.1651133479303; Thu, 28 Apr 2022 01:11:19 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id b15-20020a17090a7acf00b001cd4989ff5fsm5684259pjl.38.2022.04.28.01.11.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:11:18 -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 v5 15/17] efi_loader: menu-driven update of UEFI bootorder variable Date: Thu, 28 Apr 2022 17:09:48 +0900 Message-Id: <20220428080950.23509-16-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220428080950.23509-1-masahisa.kojima@linaro.org> References: <20220428080950.23509-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 update of UEFI bootorder variable. Signed-off-by: Masahisa Kojima --- Changes in v5: - split into the separate patch lib/efi_loader/efi_bootmenu_maintenance.c | 102 ++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/lib/efi_loader/efi_bootmenu_maintenance.c b/lib/efi_loader/efi_bootmenu_maintenance.c index 26320a8b73..8c3f94c695 100644 --- a/lib/efi_loader/efi_bootmenu_maintenance.c +++ b/lib/efi_loader/efi_bootmenu_maintenance.c @@ -719,6 +719,56 @@ static efi_status_t efi_bootmenu_boot_add_enter_name(struct efi_bootmenu_boot_op return ret; } +static efi_status_t allow_decimal(struct efi_input_key *key) +{ + if (u'0' <= key->unicode_char && key->unicode_char <= u'9') + return EFI_SUCCESS; + + return EFI_INVALID_PARAMETER; +} + +static efi_status_t efi_bootmenu_change_boot_order(int selected, int max, int *new) +{ + efi_status_t ret; + u16 new_order[EFI_BOOT_ORDER_MAX_SIZE_IN_DECIMAL] = {0}; + + printf(ANSI_CURSOR_POSITION, 2, 1); + puts(" *** U-Boot EFI Boot Manager Menu ***"); + printf(ANSI_CURSOR_POSITION, 4, 1); + printf(" current boot order : %d", selected); + + printf(ANSI_CURSOR_POSITION, 6, 1); + printf(" new boot order(0 - %4d): ", max); + + printf(ANSI_CURSOR_POSITION, 8, 1); + puts(" ENTER to complete, ESC/CTRL+C to quit"); + + printf(ANSI_CURSOR_POSITION, 6, 29); + puts(ANSI_CURSOR_SHOW); + + for (;;) { + memset(new_order, 0, sizeof(new_order)); + ret = efi_console_get_u16_string(cin, cout, new_order, 6, allow_decimal, 6, 29); + if (ret == EFI_SUCCESS) { + int i; + int val = 0; + + for (i = 0; + i < u16_strnlen(new_order, EFI_BOOT_ORDER_MAX_SIZE_IN_DECIMAL - 1); + i++) + val = (val * 10) + (new_order[i] - u'0'); + + if (val > max) + continue; + + *new = val; + return EFI_SUCCESS; + } else { + return ret; + } + } +} + static efi_status_t efi_bootmenu_select_file_handler(struct efi_bootmenu_boot_option *bo) { efi_status_t ret; @@ -958,6 +1008,57 @@ static efi_status_t efi_bootmenu_process_delete_boot_option(void *data, bool *ex return ret; } +static efi_status_t efi_bootmenu_process_change_boot_order(void *data, bool *exit) +{ + int selected; + int new_order; + efi_status_t ret; + efi_uintn_t num, size; + u16 *bootorder = NULL; + u16 *new_bootorder = NULL; + + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); + if (!bootorder) + return EFI_NOT_FOUND; + + num = size / sizeof(u16); + ret = efi_bootmenu_show_boot_selection(bootorder, num, &selected); + if (ret != EFI_SUCCESS) + goto out; + + ret = efi_bootmenu_change_boot_order(selected, num - 1, &new_order); + if (ret != EFI_SUCCESS) + goto out; + + new_bootorder = calloc(1, size); + if (!new_bootorder) + goto out; + + memcpy(new_bootorder, bootorder, size); + if (selected > new_order) { + new_bootorder[new_order] = bootorder[selected]; + memcpy(&new_bootorder[new_order + 1], &bootorder[new_order], + (selected - new_order) * sizeof(u16)); + } else if (selected < new_order) { + new_bootorder[new_order] = bootorder[selected]; + memcpy(&new_bootorder[selected], &bootorder[selected + 1], + (new_order - selected) * sizeof(u16)); + } else { + /* nothing to change */ + goto out; + } + 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); +out: + free(new_bootorder); + free(bootorder); + + return ret; +} + static efi_status_t efi_bootmenu_init(void) { efi_status_t ret; @@ -987,6 +1088,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"Change Boot Order", efi_bootmenu_process_change_boot_order}, {u"Quit", NULL}, }; -- 2.17.1