From patchwork Tue Feb 22 16:02:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 545045 Delivered-To: patch@linaro.org Received: by 2002:adf:a34a:0:0:0:0:0 with SMTP id d10csp367851wrb; Tue, 22 Feb 2022 08:02:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJxXvLtd+ToE88Pd0+Lae6xVmWazRDZp9rD8Walb6amXmMVl/MgisO0DWUuyHmIdzsIGvzTz X-Received: by 2002:a05:6402:50cb:b0:412:ab6d:c807 with SMTP id h11-20020a05640250cb00b00412ab6dc807mr25940517edb.382.1645545729419; Tue, 22 Feb 2022 08:02:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645545729; cv=none; d=google.com; s=arc-20160816; b=a0iyyPCCEvDIoYOZ3DuzhcVFv16jZANGPxv5/hqV3B4E7eLNa7mlfiP2uNlo7ty6h/ kABbDGKP3AB8hrvf+FSlUpdArjJmFVlQ1Ad8CK1hFe5kN/K0zmEhI3sxwIAPthA4bJwN L8AAUVnmnKMtrgOa6GQD3gTio9T8DyIFDcLXvwZhF6TlnsispweXihpAZlC9KmOAkka/ T56//TRnoeqr4NkTjt/xFwzgWGv68RiNmZi/jMiCR7zfG14B0lfOGmSpGS2km+mUtLbr zULBj6ja4GY6YN/BZ7maGtK08OSMZHyAsOR2coPtGnA8GkBS/AbydtUW052fQ5mGaa6D gzIg== 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=i4lpoRTHZeLyvfiPjElKUkojjJ67+v7kuimrmdTsP4Y=; b=Z+lmHtRdhz4RdpP86p31W46qKVi4u1dts8h8MgfD1TkhZHNT58CAzr85Q/uQlM8M4L mZioWxKWeIIpZfh3vrkzfeZ0ie0XSYD0lqLzB0IFr7inLW8grRjGs4RymarlE63dQj4p /zkncuCJqlQhqdoSNPdB/1Q5oTUZo6UbYZywB/UyJeD7r+Ab6nhlxcprG5bBmKZtGCGT V/JMhlLGo6qdmkK5zrhGGq/1sEXjFkkhu7PXEg/yszi0jbEQuW35B4JN8aeblMOeRJvU aH/ulNnSIpQvvYVgHfKaxCI+ePqCtJTMOyWwFUM6QZ/v7t6iXSLIoH/izTBXEFdTlgbZ vM9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eqKjaiwl; 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 mp3si12259680ejc.597.2022.02.22.08.02.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Feb 2022 08:02:09 -0800 (PST) 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=eqKjaiwl; 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 34B8F83CBA; Tue, 22 Feb 2022 17:01:44 +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="eqKjaiwl"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 123F483C7B; Tue, 22 Feb 2022 17:01:37 +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-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) (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 1217B83C95 for ; Tue, 22 Feb 2022 17:01:26 +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-pf1-x431.google.com with SMTP id i21so12470755pfd.13 for ; Tue, 22 Feb 2022 08:01:25 -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=i4lpoRTHZeLyvfiPjElKUkojjJ67+v7kuimrmdTsP4Y=; b=eqKjaiwlJJB7UypzPglzNPks2cAjz+ldroUIwt2R6SVeC/bbIF/XZ/X1FkTjosa8sy ejuKxRmJGyxKlrY8Xosf2xnDUOIZ2bBMNKBrXuhC5jJr/1GgPAH7MCKVB9r/xUNBzefM M+gwfWx6cRR5L8+jW9mSRf4cQ0s0ocPgSahKB+xEpS+LS9eXHpiUGY4/UY7ipFCDuLdn a6f7NugHNKxdIbvj5ScxsHux+nQQjSUeVoUc0Urwr7mqZEvWXwBT3WLreuaWbzRbiV21 FtMDztmC/t+jjwGRQo67PBTdUloReVtMjkj9dbIXtzEIm0+jFBZSWP4uSjhPYIb/D/9D vN+Q== 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=i4lpoRTHZeLyvfiPjElKUkojjJ67+v7kuimrmdTsP4Y=; b=mBkfWiTbnXPzR7mK2ipzenl/FOcZxLEcaA1CUfKKfCZOpGdC23K+0cAr9kmETnPmHx j1iaTokOwVmTpQ4HjKyyV4VMeVgjFArICoINFRrPnBEohEe1Myt0sjPU833ej0ZE4tIo h1+t19d8wSBEXivqHm18NfvSfSEufN2PAQaQRmYYirEAGyMcnWUVUSD97MJE/CTul827 rZcvNvXXgyvmqVD3YgupYyh914Fe4AjZbKAiPKbg1sTO/QI7bO4/aosjE1zdpn76iShb UbxjNiLEUrYBuGGp+dhn/cqCxRxcLFGwjxDQbf1/Don0r6863N2UrwcFk6oKWqDTROty K+Qg== X-Gm-Message-State: AOAM533hr7HcVe1mJCP66w4F/4Hozlk+CkwT3mFbR5OpAd4Iuy9mWpRQ LQTPa2Wuc2rn+167NWamWeHkhOJ58suwxQ== X-Received: by 2002:a05:6a00:218a:b0:4ec:c810:67ec with SMTP id h10-20020a056a00218a00b004ecc81067ecmr21620922pfi.51.1645545684111; Tue, 22 Feb 2022 08:01:24 -0800 (PST) Received: from localhost.localdomain ([2400:2411:502:a100:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id h6sm3017683pjs.30.2022.02.22.08.01.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Feb 2022 08:01:23 -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: [RFC PATCH v2 4/4] efi_loader: add menu-driven UEFI Boot Variable maintenance Date: Wed, 23 Feb 2022 01:02:35 +0900 Message-Id: <20220222160235.24748-5-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220222160235.24748-1-masahisa.kojima@linaro.org> References: <20220222160235.24748-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 --- include/efi_loader.h | 27 ++ lib/efi_loader/efi_bootmgr.c | 687 ++++++++++++++++++++++++++++++++++ lib/efi_loader/efi_boottime.c | 55 +-- lib/efi_loader/efi_console.c | 81 ++++ lib/efi_loader/efi_file.c | 74 ++-- 5 files changed, 876 insertions(+), 48 deletions(-) diff --git a/include/efi_loader.h b/include/efi_loader.h index e390d323a9..0623e0a707 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -278,6 +278,7 @@ extern const efi_guid_t efi_guid_loaded_image; extern const efi_guid_t efi_guid_loaded_image_device_path; extern const efi_guid_t efi_guid_device_path_to_text_protocol; extern const efi_guid_t efi_simple_file_system_protocol_guid; +extern const efi_guid_t efi_system_partition_guid; extern const efi_guid_t efi_file_info_guid; /* GUID for file system information */ extern const efi_guid_t efi_file_system_info_guid; @@ -310,6 +311,9 @@ extern const efi_guid_t efi_guid_firmware_management_protocol; extern const efi_guid_t efi_esrt_guid; /* GUID of the SMBIOS table */ extern const efi_guid_t smbios_guid; +/*GUID of console */ +extern const efi_guid_t efi_guid_text_input_protocol; +extern const efi_guid_t efi_guid_text_output_protocol; extern char __efi_runtime_start[], __efi_runtime_stop[]; extern char __efi_runtime_rel_start[], __efi_runtime_rel_stop[]; @@ -998,4 +1002,27 @@ efi_status_t efi_esrt_populate(void); efi_status_t efi_load_capsule_drivers(void); efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, u32 *sz); + +efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type, + const efi_guid_t *protocol, void *search_key, + efi_uintn_t *no_handles, efi_handle_t **buffer); + +efi_status_t efi_open_volume_int(struct efi_simple_file_system_protocol *this, + struct efi_file_handle **root); +efi_status_t efi_file_open_int(struct efi_file_handle *this, + struct efi_file_handle **new_handle, + u16 *file_name, u64 open_mode, + u64 attributes); +efi_status_t efi_file_close_int(struct efi_file_handle *file); +efi_status_t efi_file_read_int(struct efi_file_handle *this, + efi_uintn_t *buffer_size, void *buffer); +efi_status_t efi_file_setpos_int(struct efi_file_handle *file, u64 pos); + +typedef efi_status_t (*efi_console_filter_func)(struct efi_input_key *key); +efi_status_t efi_console_get_u16_string + (struct efi_simple_text_input_protocol *cin, + struct efi_simple_text_output_protocol *cout, + u16 *buf, efi_uintn_t count, efi_console_filter_func filer_func, + int row, int col); + #endif /* _EFI_LOADER_H */ diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c index bbb3fac5bd..b7d1fa6f4b 100644 --- a/lib/efi_loader/efi_bootmgr.c +++ b/lib/efi_loader/efi_bootmgr.c @@ -20,6 +20,9 @@ static const struct efi_boot_services *bs; static const struct efi_runtime_services *rs; +static struct efi_simple_text_input_protocol *cin; +static struct efi_simple_text_output_protocol *cout; + /* * bootmgr implements the logic of trying to find a payload to boot * based on the BootOrder + BootXXXX variables, and then loading it. @@ -30,6 +33,9 @@ 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 32 +#define EFI_BOOT_ORDER_MAX_SIZE_IN_DECIMAL 6 typedef efi_status_t (*efi_bootmenu_entry_func)(void *data, bool *exit); @@ -83,12 +89,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; @@ -418,6 +461,646 @@ 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: try with 4K size */ + /* TODO: show error notification to user */ + log_err("file path is too long\n"); + return EFI_INVALID_PARAMETER; + } + u16_strlcat(info->bo->current_path, info->f->file_name, EFI_BOOTMGR_FILE_PATH_MAX); + 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_strlcat(info->bo->current_path, u"\\", EFI_BOOTMGR_FILE_PATH_MAX); + } 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_handler *handler; + 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_locate_handle_buffer_int(BY_PROTOCOL, &efi_system_partition_guid, + NULL, &count, (efi_handle_t **)&volume_handles); + if (ret != EFI_SUCCESS) + return ret; + + ret = efi_search_protocol(efi_root, &efi_guid_device_path_to_text_protocol, &handler); + if (ret != EFI_SUCCESS) + goto out1; + + ret = efi_protocol_open(handler, (void **)&text, efi_root, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + 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_search_protocol(volume_handles[i], + &efi_simple_file_system_protocol_guid, &handler); + if (ret != EFI_SUCCESS) + continue; + ret = efi_protocol_open(handler, (void **)&v, efi_root, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (ret != EFI_SUCCESS) + continue; + + ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler); + if (ret != EFI_SUCCESS) + continue; + ret = efi_protocol_open(handler, (void **)&device_path, + efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (ret != EFI_SUCCESS) + continue; + + name = EFI_CALL(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, -1); + +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_file_open_int(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_file_read_int(f, &len, buf); + if (ret != EFI_SUCCESS || len == 0) + break; + + size += len; + count++; + } + + dir_buf = calloc(1, size); + if (!dir_buf) { + efi_file_close_int(f); + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + menu_item = calloc(count + 1, sizeof(struct efi_bootmgr_menu_item)); + if (!menu_item) { + efi_file_close_int(f); + free(dir_buf); + ret = EFI_OUT_OF_RESOURCES; + goto out; + } + + /* read directory and construct menu structure */ + efi_file_setpos_int(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_file_read_int(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, -1); +err: + efi_file_close_int(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) +{ + efi_status_t ret; + + printf(ANSI_CURSOR_POSITION, 2, 1); + puts(" *** U-Boot EFI Boot Manager Menu ***"); + printf(ANSI_CURSOR_POSITION, 4, 1); + puts(" enter name:"); + + printf(ANSI_CURSOR_POSITION, 8, 1); + puts(" ENTER to complete, ESC/CTRL+C to quit"); + + ret = efi_console_get_u16_string(cin, cout, bo->boot_name, + EFI_BOOTMGR_BOOT_NAME_MAX, NULL, 4, 15); + + 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_bootmgr_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_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_open_volume_int(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), + -1); +} + +static efi_status_t efi_bootmgr_process_add_boot_option(void *data, bool *exit) +{ + u32 index; + void *p = NULL; + u16 var_name[9]; + char *buf = NULL; + efi_status_t ret; + char *iter = NULL; + 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 */ + 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; +} + +efi_status_t efi_bootmgr_menu_init(void) +{ + efi_status_t ret; + struct efi_handler *handler; + + ret = efi_search_protocol(efi_root, &efi_guid_text_input_protocol, &handler); + if (ret != EFI_SUCCESS) + return ret; + + ret = efi_protocol_open(handler, (void **)&cin, efi_root, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (ret != EFI_SUCCESS) + return ret; + + ret = efi_search_protocol(efi_root, &efi_guid_text_output_protocol, &handler); + if (ret != EFI_SUCCESS) + return ret; + + ret = efi_protocol_open(handler, (void **)&cout, efi_root, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (ret != EFI_SUCCESS) + return ret; + + return ret; +} + /** * try_load_entry() - try to load image for boot option * @@ -568,6 +1251,10 @@ efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options) if (IS_ENABLED(CONFIG_EFI_BOOT_MENU)) { int selected; + ret = efi_bootmgr_menu_init(); + if (ret != EFI_SUCCESS) + goto error; + bootmgr_menu_items[0].data = &selected; ret = efi_bootmgr_process_common(bootmgr_menu_items, ARRAY_SIZE(bootmgr_menu_items), diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index 82128ac1d5..c3c5f299ee 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -2454,32 +2454,13 @@ static efi_status_t EFIAPI efi_protocols_per_handle( return EFI_EXIT(EFI_SUCCESS); } -/** - * efi_locate_handle_buffer() - locate handles implementing a protocol - * @search_type: selection criterion - * @protocol: GUID of the protocol - * @search_key: registration key - * @no_handles: number of returned handles - * @buffer: buffer with the returned handles - * - * This function implements the LocateHandleBuffer service. - * - * See the Unified Extensible Firmware Interface (UEFI) specification for - * details. - * - * Return: status code - */ -efi_status_t EFIAPI efi_locate_handle_buffer( - enum efi_locate_search_type search_type, - const efi_guid_t *protocol, void *search_key, - efi_uintn_t *no_handles, efi_handle_t **buffer) +efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type, + const efi_guid_t *protocol, void *search_key, + efi_uintn_t *no_handles, efi_handle_t **buffer) { efi_status_t r; efi_uintn_t buffer_size = 0; - EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key, - no_handles, buffer); - if (!no_handles || !buffer) { r = EFI_INVALID_PARAMETER; goto out; @@ -2499,6 +2480,36 @@ efi_status_t EFIAPI efi_locate_handle_buffer( if (r == EFI_SUCCESS) *no_handles = buffer_size / sizeof(efi_handle_t); out: + return r; +} + +/** + * efi_locate_handle_buffer() - locate handles implementing a protocol + * @search_type: selection criterion + * @protocol: GUID of the protocol + * @search_key: registration key + * @no_handles: number of returned handles + * @buffer: buffer with the returned handles + * + * This function implements the LocateHandleBuffer service. + * + * See the Unified Extensible Firmware Interface (UEFI) specification for + * details. + * + * Return: status code + */ +efi_status_t EFIAPI efi_locate_handle_buffer(enum efi_locate_search_type search_type, + const efi_guid_t *protocol, void *search_key, + efi_uintn_t *no_handles, efi_handle_t **buffer) +{ + efi_status_t r; + + EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key, + no_handles, buffer); + + r = efi_locate_handle_buffer_int(search_type, protocol, search_key, + no_handles, buffer); + return EFI_EXIT(r); } diff --git a/lib/efi_loader/efi_console.c b/lib/efi_loader/efi_console.c index ba68a15017..f5002e1c99 100644 --- a/lib/efi_loader/efi_console.c +++ b/lib/efi_loader/efi_console.c @@ -5,6 +5,7 @@ * Copyright (c) 2016 Alexander Graf */ +#include #include #include #include @@ -1312,3 +1313,83 @@ out_of_memory: printf("ERROR: Out of memory\n"); return r; } + +/** + * efi_console_get_u16_string() - get user input string + * + * @cin: protocol interface to EFI_SIMPLE_TEXT_INPUT_PROTOCOL + * @cout: protocol interface to EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL + * @buf: buffer to store user input string in UTF16 + * @size: buffer size including NULL terminator + * @filter_func: callback to filter user input + * @row: row number to locate user input form + * @col: column number to locate user input form + * Return: status code + */ +efi_status_t efi_console_get_u16_string(struct efi_simple_text_input_protocol *cin, + struct efi_simple_text_output_protocol *cout, + u16 *buf, efi_uintn_t size, + efi_console_filter_func filter_func, + int row, int col) +{ + efi_status_t ret; + efi_uintn_t len = 0; + struct efi_input_key key; + + printf(ANSI_CURSOR_POSITION, row, col); + puts(ANSI_CLEAR_LINE_TO_END); + puts(ANSI_CURSOR_SHOW); + + ret = EFI_CALL(cin->reset(cin, false)); + if (ret != EFI_SUCCESS) + return ret; + + for (;;) { + do { + ret = EFI_CALL(cin->read_key_stroke(cin, &key)); + mdelay(10); + } while (ret == EFI_NOT_READY); + + if (key.unicode_char == u'\b') { + if (len > 0) + buf[--len] = u'\0'; + + printf(ANSI_CURSOR_POSITION, row, col); + ret = EFI_CALL(cout->output_string(cout, buf)); + if (ret != EFI_SUCCESS) + return ret; + + puts(ANSI_CLEAR_LINE_TO_END); + continue; + } else if (key.unicode_char == u'\r') { + if (len == 0) /* no user input */ + continue; + + buf[len] = u'\0'; + return EFI_SUCCESS; + } else if (key.unicode_char == 0x3 || key.scan_code == 23) { + return EFI_ABORTED; + } else if (key.unicode_char < 0x20) { + /* ignore control codes other than Ctrl+C, '\r' and '\b' */ + continue; + } else if (key.scan_code != 0) { + /* only accept single ESC press for cancel */ + continue; + } + + if (filter_func) { + if (filter_func(&key) != EFI_SUCCESS) + continue; + } + + if (len >= (size - 1)) + continue; + + buf[len] = key.unicode_char; + len++; + printf(ANSI_CURSOR_POSITION, row, col); + ret = EFI_CALL(cout->output_string(cout, buf)); + if (ret != EFI_SUCCESS) + return ret; + } +} diff --git a/lib/efi_loader/efi_file.c b/lib/efi_loader/efi_file.c index 7a7077e6d0..d2d7e8495d 100644 --- a/lib/efi_loader/efi_file.c +++ b/lib/efi_loader/efi_file.c @@ -246,10 +246,10 @@ error: return NULL; } -static efi_status_t efi_file_open_int(struct efi_file_handle *this, - struct efi_file_handle **new_handle, - u16 *file_name, u64 open_mode, - u64 attributes) +efi_status_t efi_file_open_int(struct efi_file_handle *this, + struct efi_file_handle **new_handle, + u16 *file_name, u64 open_mode, + u64 attributes) { struct file_handle *fh = to_fh(this); efi_status_t ret; @@ -369,11 +369,17 @@ static efi_status_t file_close(struct file_handle *fh) return EFI_SUCCESS; } -static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file) +efi_status_t efi_file_close_int(struct efi_file_handle *file) { struct file_handle *fh = to_fh(file); + + return file_close(fh); +} + +static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file) +{ EFI_ENTRY("%p", file); - return EFI_EXIT(file_close(fh)); + return EFI_EXIT(efi_file_close_int(file)); } static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file) @@ -562,8 +568,8 @@ static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size, return EFI_SUCCESS; } -static efi_status_t efi_file_read_int(struct efi_file_handle *this, - efi_uintn_t *buffer_size, void *buffer) +efi_status_t efi_file_read_int(struct efi_file_handle *this, + efi_uintn_t *buffer_size, void *buffer) { struct file_handle *fh = to_fh(this); efi_status_t ret = EFI_SUCCESS; @@ -773,24 +779,11 @@ out: return EFI_EXIT(ret); } -/** - * efi_file_setpos() - set current position in file - * - * This function implements the SetPosition service of the EFI file protocol. - * See the UEFI spec for details. - * - * @file: file handle - * @pos: new file position - * Return: status code - */ -static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file, - u64 pos) +efi_status_t efi_file_setpos_int(struct efi_file_handle *file, u64 pos) { struct file_handle *fh = to_fh(file); efi_status_t ret = EFI_SUCCESS; - EFI_ENTRY("%p, %llu", file, pos); - if (fh->isdir) { if (pos != 0) { ret = EFI_UNSUPPORTED; @@ -812,6 +805,28 @@ static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file, fh->offset = pos; error: + return ret; +} + +/** + * efi_file_setpos() - set current position in file + * + * This function implements the SetPosition service of the EFI file protocol. + * See the UEFI spec for details. + * + * @file: file handle + * @pos: new file position + * Return: status code + */ +static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file, + u64 pos) +{ + efi_status_t ret = EFI_SUCCESS; + + EFI_ENTRY("%p, %llu", file, pos); + + ret = efi_file_setpos_int(file, pos); + return EFI_EXIT(ret); } @@ -1138,17 +1153,24 @@ struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) return f; } +efi_status_t efi_open_volume_int(struct efi_simple_file_system_protocol *this, + struct efi_file_handle **root) +{ + struct file_system *fs = to_fs(this); + + *root = file_open(fs, NULL, NULL, 0, 0); + + return EFI_SUCCESS; +} + static efi_status_t EFIAPI efi_open_volume(struct efi_simple_file_system_protocol *this, struct efi_file_handle **root) { - struct file_system *fs = to_fs(this); EFI_ENTRY("%p, %p", this, root); - *root = file_open(fs, NULL, NULL, 0, 0); - - return EFI_EXIT(EFI_SUCCESS); + return EFI_EXIT(efi_open_volume_int(this, root)); } struct efi_simple_file_system_protocol *