From patchwork Thu Feb 10 07:05:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 541393 Delivered-To: patch@linaro.org Received: by 2002:ad5:420f:0:0:0:0:0 with SMTP id e15csp2005534imo; Wed, 9 Feb 2022 23:06:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJzvXZbW5IsUL9SfrdFls69UUbEWe9m5ySs0r0ER8E3MwDH/HjoLChYAM95F/CAxrKjzNTPU X-Received: by 2002:a17:906:190f:: with SMTP id a15mr5270914eje.707.1644476764833; Wed, 09 Feb 2022 23:06:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644476764; cv=none; d=google.com; s=arc-20160816; b=OLhMFOuMsBnLH4VZxMEWLLqvhfA5WT1Ru2IW765p6Myj8Xplh/ZLdFAL2M3oH0zTXB k9kZnz2AiANs/M1wLAVsA+Sdc7zpCFG24cFZMFTbUf4oKM1k/39s7wuQpleJKYP3LVSG zRuWssl7VREQ0xGIWaVVZRBk9g4vei8Xva2XBuy1Po4bOgHlKaYnuyczJBUT/P3WqpmP FrMJ1Q4O/yrJbZljXYeqDGrN8Ct3UfjW35EnkQNp6HGBB9dO0WGOrg7zUJj+/ppDt7UC v2zIMgn/pR67Nuh3+bJ0LnqxYkR+4cRbMkNFXjCE0Q1sECRRjefykQGWlHIBviTm29n0 HwJA== 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=dcUPIicorP4jH++eOEt5wOao/DHulCxZ55tiZ323fDw=; b=ZkH/Vo31VQ336dKl9N6mgLgflqqnx66yp7qCeYZpUBa/2dHcfw/R/cDsc9kdFpUbbm lCOwR78RonCfXcJHXgaGkBvilYrAszd7MGJBFB/2ibhU7YslVAUAwdEBqfeTXnbDxh+9 nd5NZbm5rXF1nFP67/ScxxcJJ0tMgj/JieIeH44Jjm5qmgfTaS1bBicy/Vu2EfCPFCLO JuatjHX0rVzCUapkMrR1ktcSvXzrdDStNaw0W2oC1F3mORgGVreciH7Um0ZkSYGshr4z EIRQln9bK32SWiGj/PyCgwfbMVOReZhzj3n5QCCRK3S7JY5fzGaKg6AV6zstXVp3O75y cVUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="URs/q2lv"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id p26si7401783edx.185.2022.02.09.23.06.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 23:06:04 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="URs/q2lv"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 86C99815D1; Thu, 10 Feb 2022 08:05:40 +0100 (CET) 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="URs/q2lv"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BF3DD81FC6; Thu, 10 Feb 2022 08:05:38 +0100 (CET) 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-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) (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 C916A8141A for ; Thu, 10 Feb 2022 08:05:30 +0100 (CET) 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-x102a.google.com with SMTP id a11-20020a17090a740b00b001b8b506c42fso7695179pjg.0 for ; Wed, 09 Feb 2022 23:05:30 -0800 (PST) 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=dcUPIicorP4jH++eOEt5wOao/DHulCxZ55tiZ323fDw=; b=URs/q2lvFHbzxx7Y3KyjVMHmFGS1ulr4oXP/86xxBWRf5lrY4B72q30gSfj5K4KSBT pPF9DLn3rgr0liUg59gWvmtJ0zHCsHBeIY6nrE9f86rH2FvJjEWyAaw2bUVnFNcPs/aa pOdm7BqGUMdgC3GaZ3usA74P04aNerj6OqWuwv9Ngimzlfws8Tm3Q6rRgryIghHY1obh 86uXOQXPEa+IwTq/k/tdrIDDZz3xvkn065xsy48NiKJwSZOZLWG6EUhAlIpNZ2Id6fB6 fbRAwILLEvFXAVEEMQkvPqpKOJ4+aF0bCYlECD+o9JYPozBoflYUDl8Io/1cqPm8YjdH vjpw== 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=dcUPIicorP4jH++eOEt5wOao/DHulCxZ55tiZ323fDw=; b=e/LZIpcek0hpEotEoLhjFo3+fKxpAkgjahEI5AV9kc6+zJCPbrg0LlK5BLxrEslY2+ gxPUP3/4/GF27/slMYq0oEv3Tghxhu6KQN72nV9Jrrwp/6irEV9Lifd2u4+btjZHD5jy QWKO4Us8kmnwHfZ1858g5zgliuC6Add8Skv7i80Fxr57q4j/wt4XEKu8ursYgN1P5Zzs 6HyFnOa97jZpbCSVFWPJNlRVFigaOwtubFqswZy3U/W2iS1qsRAcJQvk0p3XGVUC/ZEX FCq8FNY3DKfAmZs20H17wpBIdtanDYjHezyooStmlowLf0nen+jF5Ti5FFHv0ty+BYWC 5vHA== X-Gm-Message-State: AOAM533Qyv/aEojEpPtTHSSM4uG/4wanCT533VQNlNg4fpwZEYhHYnU/ g9AiRfEVnHZoKabVCSQHGKkLtK37wyBZkA== X-Received: by 2002:a17:90a:aa96:: with SMTP id l22mr1339681pjq.188.1644476729089; Wed, 09 Feb 2022 23:05:29 -0800 (PST) Received: from localhost.localdomain ([2400:2411:502:a100:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id q1sm984428pjd.48.2022.02.09.23.05.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 23:05:28 -0800 (PST) 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 3/3] efi_loader: add menu-driven UEFI Boot Variable maintenance Date: Thu, 10 Feb 2022 16:05:36 +0900 Message-Id: <20220210070537.28667-4-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220210070537.28667-1-masahisa.kojima@linaro.org> References: <20220210070537.28667-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 UEFI Boot Variable maintenance. User can add and delete the Boot#### variable, and update the BootOrder variable through menu operation. Signed-off-by: Masahisa Kojima --- lib/efi_loader/efi_bootmgr.c | 720 +++++++++++++++++++++++++++++++++++ 1 file changed, 720 insertions(+) diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c index 013d868f23..739140f742 100644 --- a/lib/efi_loader/efi_bootmgr.c +++ b/lib/efi_loader/efi_bootmgr.c @@ -32,6 +32,8 @@ static const struct efi_runtime_services *rs; */ #define EFI_BOOTMGR_MENU_ENTRY_NUM_MAX 1024 +#define EFI_BOOTMGR_FILE_PATH_MAX 512 +#define EFI_BOOTMGR_BOOT_NAME_MAX 64 typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit); @@ -95,12 +97,49 @@ struct efi_bootmgr_boot_selection_data { static efi_status_t efi_bootmgr_process_boot_selected(void *data, bool *exit); static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit); +static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit); +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit); +static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit); +static efi_status_t efi_bootmgr_process_change_boot_order(void *data, bool *exit); static struct efi_bootmgr_menu_item bootmgr_menu_items[] = { {u"Boot Manager", efi_bootmgr_process_boot_selection}, + {u"Boot Manager maintenance", efi_bootmgr_process_maintenance}, {u"Quit", NULL}, }; +static struct efi_bootmgr_menu_item maintenance_menu_items[] = { + {u"Add Boot Option", efi_bootmgr_process_add_boot_option}, + {u"Delete Boot Option", efi_bootmgr_process_delete_boot_option}, + {u"Change Boot Order", efi_bootmgr_process_change_boot_order}, + {u"Quit", NULL}, +}; + +struct efi_bootmgr_boot_option { + struct efi_simple_file_system_protocol *current_volume; + struct efi_device_path *dp_volume; + u16 *current_path; + u16 *boot_name; + bool file_selected; +}; + +static const struct efi_device_path END = { + .type = DEVICE_PATH_TYPE_END, + .sub_type = DEVICE_PATH_SUB_TYPE_END, + .length = sizeof(END), +}; + +struct efi_bootmgr_volume_entry_data { + struct efi_bootmgr_boot_option *bo; + struct efi_simple_file_system_protocol *v; + struct efi_device_path *dp; +}; + +struct efi_bootmgr_file_entry_data { + struct efi_bootmgr_boot_option *bo; + struct efi_file_info *f; +}; + static void efi_bootmgr_menu_print_entry(void *data) { struct efi_bootmgr_menu_entry *entry = data; @@ -558,6 +597,687 @@ static efi_status_t efi_bootmgr_process_boot_selection(void *data, bool *exit) return ret; } +static efi_status_t efi_bootmgr_volume_selected(void *data, bool *exit) +{ + struct efi_bootmgr_volume_entry_data *info = data; + + *exit = true; + + if (info) { + info->bo->current_volume = info->v; + info->bo->dp_volume = info->dp; + } + + return EFI_SUCCESS; +} + +static efi_status_t efi_bootmgr_file_selected(void *data, bool *exit) +{ + struct efi_bootmgr_file_entry_data *info = data; + + *exit = true; + + if (!info) + return EFI_INVALID_PARAMETER; + + if (u16_strncmp(info->f->file_name, u".", 1) == 0 && + u16_strlen(info->f->file_name) == 1) { + /* stay current path */ + } else if (u16_strncmp(info->f->file_name, u"..", 2) == 0 && + u16_strlen(info->f->file_name) == 2) { + u32 i; + int len = u16_strlen(info->bo->current_path); + + for (i = len - 2; i > 0; i--) { + if (info->bo->current_path[i] == u'\\') + break; + } + + if (i == 0) + info->bo->current_path[0] = u'\0'; + else + info->bo->current_path[i + 1] = u'\0'; + } else { + size_t new_len; + + new_len = u16_strlen(info->bo->current_path) + + u16_strlen(info->f->file_name) + 1; + if (new_len >= EFI_BOOTMGR_FILE_PATH_MAX) { + /* TODO: show error notification to user */ + log_err("file path is too long\n"); + return EFI_INVALID_PARAMETER; + } + u16_strcat(info->bo->current_path, info->f->file_name); + if (info->f->attribute & EFI_FILE_DIRECTORY) { + if (new_len + 1 >= EFI_BOOTMGR_FILE_PATH_MAX) { + log_err("file path is too long\n"); + return EFI_INVALID_PARAMETER; + } + u16_strcat(info->bo->current_path, u"\\"); + } else { + info->bo->file_selected = true; + } + } + return EFI_SUCCESS; +} + +static efi_status_t efi_bootmgr_select_volume(struct efi_bootmgr_boot_option *bo) +{ + u16 *name; + u32 i; + efi_status_t ret; + efi_uintn_t count; + struct efi_device_path *device_path; + efi_handle_t *volume_handles = NULL; + struct efi_simple_file_system_protocol *v; + struct efi_device_path_to_text_protocol *text; + struct efi_bootmgr_menu_item *menu_item, *iter; + + ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, &efi_system_partition_guid, + NULL, &count, + (efi_handle_t **)&volume_handles)); + if (ret != EFI_SUCCESS) + return ret; + + ret = EFI_CALL(systab.boottime->locate_protocol(&efi_guid_device_path_to_text_protocol, + NULL, (void **)&text)); + if (ret != EFI_SUCCESS) + goto out1; + + menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item)); + if (!menu_item) { + ret = EFI_OUT_OF_RESOURCES; + goto out1; + } + + iter = menu_item; + for (i = 0; i < count; i++) { + struct efi_bootmgr_volume_entry_data *info; + + ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i], + &efi_simple_file_system_protocol_guid, + (void **)&v, efi_root, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); + if (ret != EFI_SUCCESS) + continue; + + ret = EFI_CALL(systab.boottime->open_protocol(volume_handles[i], + &efi_guid_device_path, + (void **)&device_path, efi_root, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); + if (ret != EFI_SUCCESS) + continue; + + name = text->convert_device_path_to_text(device_path, true, true); + if (!name) { + ret = EFI_OUT_OF_RESOURCES; + goto out2; + } + + info = calloc(1, sizeof(struct efi_bootmgr_volume_entry_data)); + if (!info) { + ret = EFI_OUT_OF_RESOURCES; + goto out2; + } + + info->v = v; + info->dp = device_path; + info->bo = bo; + iter->title = name; + iter->func = efi_bootmgr_volume_selected; + iter->data = info; + iter++; + } + + iter->title = u"Quit"; + iter->func = NULL; + iter->data = NULL; + count += 1; + + ret = efi_bootmgr_process_common(menu_item, count, false); + +out2: + iter = menu_item; + for (i = 0; i < count - 1; i++) { + struct efi_bootmgr_volume_entry_data *p; + + p = (struct efi_bootmgr_volume_entry_data *)(iter->data); + efi_free_pool(iter->title); + free(p); + iter++; + } + + free(menu_item); + +out1: + efi_free_pool(volume_handles); + + return ret; +} + +static efi_status_t efi_bootmgr_select_file(struct efi_bootmgr_boot_option *bo, + struct efi_file_handle *root) +{ + char *buf; + u32 i; + char *dir_buf; + efi_uintn_t len; + efi_status_t ret; + efi_uintn_t size; + u32 count = 0; + struct efi_file_handle *f; + struct efi_file_info *ptr; + struct efi_bootmgr_menu_item *menu_item, *iter; + + buf = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX); + if (!buf) + return EFI_OUT_OF_RESOURCES; + + while (!bo->file_selected) { + size = 0; + count = 0; + + ret = EFI_CALL(root->open(root, &f, bo->current_path, + EFI_FILE_MODE_READ, 0)); + if (ret != EFI_SUCCESS) + return ret; + + /* calculate directory information total size */ + for (;;) { + len = EFI_BOOTMGR_FILE_PATH_MAX; + ret = EFI_CALL(f->read(f, &len, buf)); + if (ret != EFI_SUCCESS || len == 0) + break; + + size += len; + count++; + } + + dir_buf = calloc(1, size); + if (!dir_buf) { + EFI_CALL(f->close(f)); + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item)); + if (!menu_item) { + EFI_CALL(f->close(f)); + free(dir_buf); + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + + /* read directory and construct menu structure */ + f->setpos(f, 0); + iter = menu_item; + ptr = (struct efi_file_info *)dir_buf; + for (i = 0; i < count; i++) { + int name_len; + u16 *name; + struct efi_bootmgr_file_entry_data *info; + + len = size; + ret = EFI_CALL(f->read(f, &len, ptr)); + if (ret != EFI_SUCCESS || len == 0) + goto err; + + if (ptr->attribute & EFI_FILE_DIRECTORY) { + /* append u'/' at the end of directory name */ + name_len = u16_strsize(ptr->file_name) + sizeof(u16); + name = calloc(1, name_len); + if (!name) { + ret = EFI_OUT_OF_RESOURCES; + goto err; + } + u16_strcpy(name, ptr->file_name); + name[u16_strlen(ptr->file_name)] = u'/'; + } else { + name_len = u16_strsize(ptr->file_name); + name = calloc(1, name_len); + if (!name) { + ret = EFI_OUT_OF_RESOURCES; + goto err; + } + u16_strcpy(name, ptr->file_name); + } + + info = calloc(1, sizeof(struct efi_bootmgr_file_entry_data)); + if (!info) { + ret = EFI_OUT_OF_RESOURCES; + goto err; + } + info->f = ptr; + info->bo = bo; + iter->title = name; + iter->func = efi_bootmgr_file_selected; + iter->data = info; + iter++; + + size -= len; + ptr = (struct efi_file_info *)((char *)ptr + len); + } + + /* add "Quit" entry */ + iter->title = u"Quit"; + iter->func = NULL; + iter->data = NULL; + count += 1; + + ret = efi_bootmgr_process_common(menu_item, count, false); +err: + EFI_CALL(f->close(f)); + iter = menu_item; + for (i = 0; i < count - 1; i++, iter++) { + free(iter->title); + free(iter->data); + } + + free(dir_buf); + free(menu_item); + + if (ret != EFI_SUCCESS) + break; + } + +out: + free(buf); + return ret; +} + +static efi_status_t efi_bootmgr_boot_add_enter_name(struct efi_bootmgr_boot_option *bo) +{ + int c; + int len = 0; + char name[EFI_BOOTMGR_BOOT_NAME_MAX] = {0}; + + puts(ANSI_CLEAR_CONSOLE); + + printf(ANSI_CURSOR_POSITION, 1, 1); + puts(ANSI_CLEAR_LINE); + printf(ANSI_CURSOR_POSITION, 2, 1); + puts(" *** U-Boot EFI Boot Manager Menu ***"); + puts(ANSI_CLEAR_LINE_TO_END); + printf(ANSI_CURSOR_POSITION, 3, 1); + puts(ANSI_CLEAR_LINE); + printf(ANSI_CURSOR_POSITION, 4, 1); + puts(" enter name:"); + puts(ANSI_CLEAR_LINE_TO_END); + + printf(ANSI_CURSOR_POSITION, 8, 1); + puts(ANSI_CLEAR_LINE); + puts(" ENTER to complete, ESC/CTRL+C to quit"); + + printf(ANSI_CURSOR_POSITION, 4, 15); + puts(ANSI_CURSOR_SHOW); + + for (;;) { + while (!tstc()) { + WATCHDOG_RESET(); + mdelay(10); + } + + c = getchar(); + + if ((c == ' ') || (('0' <= c) && (c <= '9')) || + (('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) { + if (len >= (EFI_BOOTMGR_BOOT_NAME_MAX - 1)) + continue; + + name[len] = (char)c; + len++; + printf(ANSI_CURSOR_POSITION, 4, 15); + puts(ANSI_CLEAR_LINE_TO_END); + printf("%s", name); + } else if (c == '\b') { + if (len > 0) + name[--len] = '\0'; + + printf(ANSI_CURSOR_POSITION, 4, 15); + puts(ANSI_CLEAR_LINE_TO_END); + printf("%s", name); + } else if (c == '\r') { + u16 *p; + + name[len] = '\0'; + p = bo->boot_name; + utf8_utf16_strncpy(&p, name, len); + return EFI_SUCCESS; + } else if (c == 0x3) { + return EFI_ABORTED; + } else if (c == '\e') { /* TODO: correctly handle escape sequence */ + return EFI_ABORTED; + } + } +} + +static efi_status_t efi_bootmgr_change_boot_order(int selected, int max, int *new) +{ + int c; + int len = 0; + char new_order[6] = {0}; + + puts(ANSI_CLEAR_CONSOLE); + + printf(ANSI_CURSOR_POSITION, 1, 1); + puts(ANSI_CLEAR_LINE); + printf(ANSI_CURSOR_POSITION, 2, 1); + puts(" *** U-Boot EFI Boot Manager Menu ***"); + puts(ANSI_CLEAR_LINE_TO_END); + printf(ANSI_CURSOR_POSITION, 3, 1); + puts(ANSI_CLEAR_LINE); + printf(ANSI_CURSOR_POSITION, 4, 1); + printf(" current boot order : %d", selected); + puts(ANSI_CLEAR_LINE_TO_END); + + printf(ANSI_CURSOR_POSITION, 5, 1); + puts(ANSI_CLEAR_LINE); + printf(ANSI_CURSOR_POSITION, 6, 1); + printf(" new boot order(0 - %4d): ", max); + puts(ANSI_CLEAR_LINE_TO_END); + + printf(ANSI_CURSOR_POSITION, 8, 1); + puts(ANSI_CLEAR_LINE); + puts(" ENTER to complete, ESC/CTRL+C to quit"); + + printf(ANSI_CURSOR_POSITION, 6, 29); + puts(ANSI_CURSOR_SHOW); + + for (;;) { + while (!tstc()) { + WATCHDOG_RESET(); + mdelay(10); + } + + c = getchar(); + + if ('0' <= c && c <= '9') { + if (len >= 5) + continue; + + new_order[len] = (char)c; + len++; + printf(ANSI_CURSOR_POSITION, 6, 29); + puts(ANSI_CLEAR_LINE_TO_END); + printf("%s", new_order); + } else if (c == '\b') { + if (len > 0) + new_order[--len] = '\0'; + + printf(ANSI_CURSOR_POSITION, 6, 29); + puts(ANSI_CLEAR_LINE_TO_END); + printf("%s", new_order); + } else if (c == '\r') { + int i; + int val = 0; + + for (i = 0; i < len; i++) + val = (val * 10) + (new_order[i] - '0'); + + if (val > max) /* TODO: show error notification */ + continue; + + *new = val; + return EFI_SUCCESS; + } else if (c == 0x3) { + return EFI_ABORTED; + } else if (c == '\e') { /* TODO: correctly handle escape sequence */ + return EFI_ABORTED; + } + } +} + +static efi_status_t efi_bootmgr_select_file_handler(struct efi_bootmgr_boot_option *bo) +{ + efi_status_t ret; + struct efi_file_handle *root; + + bo->file_selected = false; + + while (!bo->file_selected) { + bo->current_volume = NULL; + memset(bo->current_path, 0, sizeof(bo->current_path)); + + ret = efi_bootmgr_select_volume(bo); + if (ret != EFI_SUCCESS) + goto out; + + if (!bo->current_volume) + return EFI_INVALID_PARAMETER; + + ret = EFI_CALL(bo->current_volume->open_volume(bo->current_volume, &root)); + if (ret != EFI_SUCCESS) + return ret; + + ret = efi_bootmgr_select_file(bo, root); + + if (ret != EFI_SUCCESS) + goto out; + } + + ret = efi_bootmgr_boot_add_enter_name(bo); + +out: + return ret; +} + +static efi_status_t efi_bootmgr_process_maintenance(void *data, bool *exit) +{ + return efi_bootmgr_process_common(maintenance_menu_items, + ARRAY_SIZE(maintenance_menu_items), + false); +} + +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit) +{ + u32 index; + void *p = NULL; + char *buf = NULL; + efi_status_t ret; + char *iter = NULL; + u16 var_name[9]; + u16 *bootorder = NULL; + u16 *new_bootorder = NULL; + struct efi_load_option lo; + efi_uintn_t dp_size, fp_size; + efi_uintn_t last, size, new_size; + struct efi_bootmgr_boot_option bo; + struct efi_device_path_file_path *fp; + + /* get unused Boot#### */ + for (index = 0; index <= 0xFFFF; index++) { + size = 0; + efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index); + ret = efi_get_variable_int(var_name, &efi_global_variable_guid, + NULL, &size, NULL, NULL); + if (ret == EFI_BUFFER_TOO_SMALL) + continue; + else + break; + } + + if (index >= 0xFFFF) + return EFI_OUT_OF_RESOURCES; + + efi_create_indexed_name(var_name, sizeof(var_name), "Boot", index); + + bo.current_path = calloc(1, EFI_BOOTMGR_FILE_PATH_MAX); + if (!bo.current_path) + goto out; + + bo.boot_name = calloc(1, EFI_BOOTMGR_BOOT_NAME_MAX * sizeof(u16)); + if (!bo.boot_name) + goto out; + + ret = efi_bootmgr_select_file_handler(&bo); + if (ret == EFI_ABORTED) + goto out; + + dp_size = efi_dp_size(bo.dp_volume); + fp_size = sizeof(struct efi_device_path) + + ((u16_strlen(bo.current_path) + 1) * sizeof(u16)); + buf = calloc(1, dp_size + fp_size + sizeof(END)); + if (!buf) + goto out; + + iter = buf; + memcpy(iter, bo.dp_volume, dp_size); + iter += dp_size; + + fp = (struct efi_device_path_file_path *)iter; + fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; + fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH; + fp->dp.length = (u16)fp_size; + u16_strcpy(fp->str, bo.current_path); + iter += fp_size; + *((struct efi_device_path *)iter) = END; + + lo.file_path = (struct efi_device_path *)buf; + lo.file_path_length = efi_dp_size((struct efi_device_path *)buf) + sizeof(END); + lo.attributes = LOAD_OPTION_ACTIVE; + lo.optional_data = NULL; + lo.label = bo.boot_name; + + size = efi_serialize_load_option(&lo, (u8 **)&p); + if (!size) { + ret = EFI_INVALID_PARAMETER; + goto out; + } + + ret = efi_set_variable_int(var_name, &efi_global_variable_guid, + EFI_VARIABLE_NON_VOLATILE | + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS, + size, p, false); + if (ret != EFI_SUCCESS) + goto out; + + /* append new boot option */ + bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size); + last = size / sizeof(u16); + new_size = size + sizeof(u16); + new_bootorder = calloc(1, new_size); + if (!new_bootorder) { + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + memcpy(new_bootorder, bootorder, size); + new_bootorder[last] = (u16)index; + + ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid, + EFI_VARIABLE_NON_VOLATILE | + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS, + new_size, new_bootorder, false); + if (ret != EFI_SUCCESS) + goto out; + +out: + free(p); + free(buf); + free(bootorder); + free(new_bootorder); + free(bo.boot_name); + free(bo.current_path); + + return ret; +} + +static efi_status_t efi_bootmgr_process_delete_boot_option(void *data, bool *exit) +{ + int selected; + u16 *bootorder; + u16 var_name[9]; + 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_bootmgr_show_boot_selection(bootorder, num, &selected); + if (ret == EFI_SUCCESS) { + /* delete selected boot option */ + efi_create_indexed_name(var_name, sizeof(var_name), + "Boot", bootorder[selected]); + 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); + goto out; + } + + /* update BootOrder */ + memmove(&bootorder[selected], &bootorder[selected + 1], + (num - selected - 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); + if (ret != EFI_SUCCESS) + goto out; + } + +out: + free(bootorder); + + return ret; +} + +static efi_status_t efi_bootmgr_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_bootmgr_show_boot_selection(bootorder, num, &selected); + if (ret != EFI_SUCCESS) + goto out; + + ret = efi_bootmgr_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 */ + } + 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); +out: + free(bootorder); + + return ret; +} + /** * try_load_entry() - try to load image for boot option *