From patchwork Thu Apr 28 08:09:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 567131 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6886:0:0:0:0 with SMTP id m6csp5232848map; Thu, 28 Apr 2022 01:12:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4GjGnJ3REpEVYuz5H6tA6LrVnwBP9WZJg48x9n06dm1RtSPVZuW2qtvBXMa85a+l4OwB7 X-Received: by 2002:a17:907:7f13:b0:6f3:bfbb:2f9b with SMTP id qf19-20020a1709077f1300b006f3bfbb2f9bmr8375649ejc.101.1651133527069; Thu, 28 Apr 2022 01:12:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651133527; cv=none; d=google.com; s=arc-20160816; b=RsuNuz/Zayanc9tqCZDQriOdIZmQAAP+7wp/H76USQ7delPCZxu7gQ2qXRfEOyvEHh aEw4SkHMcju11LdhDqHmLKx+gegUd0fY+ByUOrcfAa6DyUQ1/Fz2ZmKleL6FyOtdZ6PF VggAK+vrjxGraJTgTxPTuyBuacwpRkWI1jMahJ7q2V5Ya0rcxCqmNb0xUU4fubxH1rFP G0S6ujVvo3q8TXGWdmwDSUkVLyo8Wt0zVWjHB0ukvUKmt9/kYxcO4BBWKmmhcK8C0gAG A3HJl1NaTC04ypN4UYB8rG4XVHDbVpDSuFMU+k+6u7u6WjXshjw0ZdEaCDf2GdhcaL8d w1MQ== 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=HOOiTkHqbSMr7iKUGSNzxIwAONnXeJsOnOrhzB5sHvc=; b=YUoqpSLR2CS2ag9D5dcijQhZQmTPcdyu9Mswz9Jr/CHkB8ACIDPlFQWpEA4piFIyPs g50+np7orBMOXdqo9HQPbVqtiaq1t9l0h0HaO0WDfRDuJ+F+GaFjy20EMhRaDUCTlb9p oKBdj//C4d5NtKhQFU4gUgEKUktLvbPajmr8pnudHbXKXvQ8HCcQTGIkCBthrJTUZ4Y3 LxKqH7Vv+en2FzShrQYlV+jQ0cQ7AkQ72DLfe5CS5X/gf4Wjz2DoDS4d0i4K/tbHYKMB eY0g0MJw4gCiaUFX0o+L3AHzzIfx+nMkpbNVvApVaJGF00RuQkodhE0xuneOwxKDjvrh OhLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="o1J/CtZy"; 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 oy12-20020a170907104c00b006df76385e39si2597720ejb.729.2022.04.28.01.12.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:12:07 -0700 (PDT) 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="o1J/CtZy"; 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 EBC1F83EF1; Thu, 28 Apr 2022 10:11:31 +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="o1J/CtZy"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 46B8383B32; Thu, 28 Apr 2022 10:11:03 +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 autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) (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 96E54802EF for ; Thu, 28 Apr 2022 10:10:54 +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-pj1-x1032.google.com with SMTP id w17-20020a17090a529100b001db302efed6so2213140pjh.4 for ; Thu, 28 Apr 2022 01:10:54 -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=HOOiTkHqbSMr7iKUGSNzxIwAONnXeJsOnOrhzB5sHvc=; b=o1J/CtZy+OQZM/xYxqzZCZli4ASIq7ViO3SccElDjxiX+EsehOlsnCYmw/rgyJFrca biQcCmcf6laeioiQq9FdX9EFzc0UfFqLUUVilsMh9kM+U+f6rTl3UA6+EPi42K6Sardh yXQQLkup2aqokfZg0Ch6VxEsFJf8QjcNPGi/bAv/GBu/CqVtSAzoBRjZDxxXOTrlANY4 smaOviVNc8xdobWWUfAKgiL58PQ0H9UrNN0wlccQVjgCIycPV8p05U1imY4Ml8Aco7N9 U0eoEN6ZnrRkgIjmpUfDKGgP90rOvlXt1+Rude6JNJ0SnI0+PHzYi/8nwDpCsyf1RVoJ d5uA== 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=HOOiTkHqbSMr7iKUGSNzxIwAONnXeJsOnOrhzB5sHvc=; b=dGnbdhXOzksjNfrYT+8i0pRHDx5hKM7CUP8gdUdQGofxmuMzOCOXlD+y4PWa0MI8d0 QaDU+1K0xi8FLmRLe16mL8sGi6g3nR9PiIKTVMwQiz02SiophwmIj6o1JfuT0aF9HlwK xmIFo2fSG4dIl/SpCZAeoDOrobzC1YyDuv12FWVqATJ1DQtVgx+x2l36yyrNmv8Gqcpu +LuB11DEoUt7NivJJxxC2j5lzwfCO8ChLGx4aEaq2I9tI8AEShtLPm0F3+hJP6yvrqf3 gmZ1KwnXr1JIL2/n6TE7Kcqc71SzIvbXgQjf2YdzaF3kPs5AEKHsaZ1FtHsfUeiVDEAu iOhw== X-Gm-Message-State: AOAM531ujQDw1jMybWoKvm/eh8OXim8YSmXQUlXIVwUxox+87Ul71WFZ FlIJnCDsCpK3XXN8sbHPuQszXVqe/z7Klw== X-Received: by 2002:a17:90b:1809:b0:1d9:c10f:7ed6 with SMTP id lw9-20020a17090b180900b001d9c10f7ed6mr12115625pjb.57.1651133452858; Thu, 28 Apr 2022 01:10:52 -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.10.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:10:52 -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 , Mark Kettenis , Peter Hoyes , Neil Armstrong , Andre Przywara Subject: [PATCH v5 06/17] efi_loader: bootmgr: add booting from removable media Date: Thu, 28 Apr 2022 17:09:39 +0900 Message-Id: <20220428080950.23509-7-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 From: AKASHI Takahiro Under the current implementation, booting from removable media using a architecture-specific default image name, say BOOTAA64.EFI, is supported only in distro_bootcmd script. See the commit 74522c898b35 ("efi_loader: Add distro boot script for removable media"). This is, however, half-baked implementation because 1) UEFI specification requires this feature to be implemented as part of Boot Manager's responsibility: 3 - Boot Manager 3.5.1 Boot via the Simple File Protocol When booting via the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, the FilePath will start with a device path that points to the device that implements the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL or the EFI_BLOCK_IO_PROTOCOL. The next part of the FilePath may point to the file name, including subdirectories, which contain the bootable image. If the file name is a null device path, the file name must be generated from the rules defined below. ... 3.5.1.1 Removable Media Boot Behavior To generate a file name when none is present in the FilePath, the firmware must append a default file name in the form \EFI\BOOT\BOOT{machine type short-name}.EFI ... 2) So (1) entails the hehavior that the user's preference of boot media order should be determined by Boot#### and BootOrder variables. With this patch, the semantics mentioned above is fully implemented. For example, if you want to boot the system from USB and SCSI in this order, * define Boot0001 which contains only a device path to the USB device (without any file path/name) * define Boot0002 which contains only a device path to the SCSI device, and * set BootOrder to Boot0001:Boot0002 To avoid build error for sandbox, default file name "BOOTSANDBOX.efi" is defined even if it is out of scope of UEFI specification. Signed-off-by: AKASHI Takahiro Signed-off-by: Masahisa Kojima --- Changes in v5: - add default file name definition for SANDBOX to avoid build error Changes from original version: - create new include file "efi_default_filename.h" to avoid conflict with config_distro_bootcmd.h - modify the target pointer of efi_free_pool(), expand_media_path() should only free the pointer allocated by efi_dp_from_file() function. include/config_distro_bootcmd.h | 14 +-------- include/efi_default_filename.h | 33 ++++++++++++++++++++++ lib/efi_loader/efi_bootmgr.c | 50 ++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 include/efi_default_filename.h diff --git a/include/config_distro_bootcmd.h b/include/config_distro_bootcmd.h index c55023889c..6a3110f27b 100644 --- a/include/config_distro_bootcmd.h +++ b/include/config_distro_bootcmd.h @@ -91,19 +91,7 @@ #endif #ifdef CONFIG_EFI_LOADER -#if defined(CONFIG_ARM64) -#define BOOTEFI_NAME "bootaa64.efi" -#elif defined(CONFIG_ARM) -#define BOOTEFI_NAME "bootarm.efi" -#elif defined(CONFIG_X86_RUN_32BIT) -#define BOOTEFI_NAME "bootia32.efi" -#elif defined(CONFIG_X86_RUN_64BIT) -#define BOOTEFI_NAME "bootx64.efi" -#elif defined(CONFIG_ARCH_RV32I) -#define BOOTEFI_NAME "bootriscv32.efi" -#elif defined(CONFIG_ARCH_RV64I) -#define BOOTEFI_NAME "bootriscv64.efi" -#endif +#include #endif #ifdef BOOTEFI_NAME diff --git a/include/efi_default_filename.h b/include/efi_default_filename.h new file mode 100644 index 0000000000..cb2ef9e131 --- /dev/null +++ b/include/efi_default_filename.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Default boot file name when none is present in the FilePath. + * This is defined in the UEFI specification. + * + * Copyright (c) 2022, Linaro Limited + */ +#ifndef _EFI_DEFAULT_FILENAME_H +#define _EFI_DEFAULT_FILENAME_H + +#if defined(CONFIG_ARM64) +#define BOOTEFI_NAME "BOOTAA64.EFI" +#elif defined(CONFIG_ARM) +#define BOOTEFI_NAME "BOOTARM.EFI" +#elif defined(CONFIG_X86_64) +#define BOOTEFI_NAME "BOOTX64.EFI" +#elif defined(CONFIG_X86) +#define BOOTEFI_NAME "BOOTIA32.EFI" +#elif defined(CONFIG_ARCH_RV32I) +#define BOOTEFI_NAME "BOOTRISCV32.EFI" +#elif defined(CONFIG_ARCH_RV64I) +#define BOOTEFI_NAME "BOOTRISCV64.EFI" +#elif defined(CONFIG_SANDBOX) +/* + * SANDBOX is not defined in UEFI specification, but + * this definition avoids build failure for SANDBOX. + */ +#define BOOTEFI_NAME "BOOTSANDBOX.EFI" +#else +#error Unsupported UEFI architecture +#endif + +#endif diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c index 8c04ecbdc8..22a4302aac 100644 --- a/lib/efi_loader/efi_bootmgr.c +++ b/lib/efi_loader/efi_bootmgr.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +31,50 @@ static const struct efi_runtime_services *rs; * should do normal or recovery boot. */ +/** + * expand_media_path() - expand a device path for default file name + * @device_path: device path to check against + * + * If @device_path is a media or disk partition which houses a file + * system, this function returns a full device path which contains + * an architecture-specific default file name for removable media. + * + * Return: a newly allocated device path + */ +static +struct efi_device_path *expand_media_path(struct efi_device_path *device_path) +{ + struct efi_device_path *dp, *full_path; + efi_handle_t handle; + efi_status_t ret; + + if (!device_path) + return NULL; + + /* + * If device_path is a (removable) media or partition which provides + * simple file system protocol, append a default file name to support + * booting from removable media. + */ + dp = device_path; + ret = efi_locate_device_path(&efi_simple_file_system_protocol_guid, + &dp, &handle); + if (ret == EFI_SUCCESS) { + if (dp->type == DEVICE_PATH_TYPE_END) { + dp = efi_dp_from_file(NULL, 0, + "/EFI/BOOT/" BOOTEFI_NAME); + full_path = efi_dp_append(device_path, dp); + efi_free_pool(dp); + } else { + full_path = efi_dp_dup(device_path); + } + } else { + full_path = efi_dp_dup(device_path); + } + + return full_path; +} + /** * try_load_entry() - try to load image for boot option * @@ -68,13 +113,16 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, } if (lo.attributes & LOAD_OPTION_ACTIVE) { + struct efi_device_path *file_path; u32 attributes; log_debug("%s: trying to load \"%ls\" from %pD\n", __func__, lo.label, lo.file_path); - ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path, + file_path = expand_media_path(lo.file_path); + ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0, handle)); + efi_free_pool(file_path); if (ret != EFI_SUCCESS) { log_warning("Loading %ls '%ls' failed\n", varname, lo.label);