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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9CBEC433F5 for ; Wed, 27 Oct 2021 11:57:31 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 2912560D42 for ; Wed, 27 Oct 2021 11:57:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2912560D42 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D393983563; Wed, 27 Oct 2021 13:57:28 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="ZvGJRM2C"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3372E8354D; Wed, 27 Oct 2021 13:57:26 +0200 (CEST) Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C0043834D1 for ; Wed, 27 Oct 2021 13:57:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1635335834; bh=mrpw1yXrHlfKpjgZnccJgCvQHx5uUsm8rZg+Y+HlInU=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From:In-Reply-To; b=ZvGJRM2CvWdpN7oBdGxAfXCHrEoTkW0qPTVo1+9XiB24CzJ92T7vr7iMOh9Gcu5LJ w+qvjcXUPFtS4b9n3gouNVEU/oakGdYcNLbHSwOB/dGotzolsnJBDnefg5cGvju+8m 29orwLny+cGD+lHZ9aln78wZA3ZV8jZ9FWPoZ9P0= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.123.35] ([88.152.144.157]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MMXQ5-1mPXcD0iN5-00JY9a; Wed, 27 Oct 2021 13:57:14 +0200 Message-ID: <8dbc39d9-fce4-9fbc-09b7-ec6505b49d26@gmx.de> Date: Wed, 27 Oct 2021 13:57:07 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.2 Subject: Re: [PATCH v2 31/41] bootstd: Add an implementation of EFI boot Content-Language: en-US To: Simon Glass Cc: Michal Simek , Tom Rini , Ilias Apalodimas , Daniel Schwierzeck , Steffen Jaeckel , =?UTF-8?Q?Marek_Beh=c3=ban?= , Lukas Auer , Dennis Gilmore , U-Boot Mailing List References: <20211023232635.9195-1-sjg@chromium.org> <20211023232635.9195-22-sjg@chromium.org> From: Heinrich Schuchardt In-Reply-To: <20211023232635.9195-22-sjg@chromium.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:asQxg+FrPq76Ple0ZUxmkhh6VBQ4daWd7mW0T0ENkCedotrxZQN TdpReoGIHW6fwHc/StDoKqFgD714xr/mUkERV+YSTyfgZMJlX9C2PHhqurEp0v73tehcLDx OfnAoz+X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pryLVMEHqETUdfhoGQEL4 gCHUqsV6eeTVfJWhYMJekfbwnowyc3aC3b6h7p0ncec7qEN0ZIdYThrz2W6sDECtoROIqj/sU WRC/tWnh1xBg1HSbZiTYo5YIQdEMpWCKDehlXrZo+2Rrwg6+spdRUU0Z8G2umpwS6PnJe8dsf 82ln8Ap6EOh+u8= X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean On 10/24/21 01:26, Simon Glass wrote: > Add a bootmeth driver which handles EFI boot, using EFI_LOADER. > > In effect, this provides the same functionality as the 'bootefi' command > and shares the same code. But the interface into it is via a bootmeth, > so it does not require any special scripts, etc. > > For now this requires the 'bootefi' command be enabled. Future work may > tidy this up so that it can be used without CONFIG_CMDLINE being enabled= . Booting via the boot manager must occur without issuing a command on the command line. How will the boot method in this file be invoked? The bootefi command may be implemented by invoking a boot method. A boot method invoking a command does not make sense to me. Best regards Heinrich > > Signed-off-by: Simon Glass > --- > > (no changes since v1) > > boot/Kconfig | 21 ++++ > boot/Makefile | 1 + > boot/bootmeth_efi.c | 263 ++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 285 insertions(+) > create mode 100644 boot/bootmeth_efi.c > > diff --git a/boot/Kconfig b/boot/Kconfig > index 7e948f95e92..ebbd7427d7f 100644 > --- a/boot/Kconfig > +++ b/boot/Kconfig > @@ -318,6 +318,27 @@ config BOOTMETH_DISTRO_PXE > > This provides a way to try out standard boot on an existing boot fl= ow. > > +config BOOTMETH_EFILOADER > + bool "Bootdev support for EFI boot" > + depends on CMD_BOOTEFI > + default y > + help > + Enables support for EFI boot using bootdevs. This makes the > + bootdevs look for a 'boot.efi' on each filesystem > + they scan. The resulting file is booted after enabling U-Boot's > + EFI loader support. > + > + The depends on the architecture of the board: > + > + aa64 - aarch64 (ARM 64-bit) > + arm - ARM 32-bit > + ia32 - x86 32-bit > + x64 - x86 64-bit > + riscv32 - RISC-V 32-bit > + riscv64 - RISC-V 64-bit > + > + This provides a way to try out standard boot on an existing boot flo= w. > + > endif > > config LEGACY_IMAGE_FORMAT > diff --git a/boot/Makefile b/boot/Makefile > index 170fcac8ec4..c2345435201 100644 > --- a/boot/Makefile > +++ b/boot/Makefile > @@ -30,6 +30,7 @@ obj-$(CONFIG_$(SPL_TPL_)BOOTSTD) +=3D bootstd-uclass.o > > obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_DISTRO) +=3D bootmeth_distro.o > obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_DISTRO_PXE) +=3D bootmeth_pxe.o > +obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_EFILOADER) +=3D bootmeth_efi.o > > obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) +=3D image-fdt.o > obj-$(CONFIG_$(SPL_TPL_)FIT_SIGNATURE) +=3D fdt_region.o > diff --git a/boot/bootmeth_efi.c b/boot/bootmeth_efi.c > new file mode 100644 > index 00000000000..eeb8f377c50 > --- /dev/null > +++ b/boot/bootmeth_efi.c > @@ -0,0 +1,263 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * Bootmethod for distro boot via EFI > + * > + * Copyright 2021 Google LLC > + * Written by Simon Glass > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#define EFI_DIRNAME "efi/boot/" > + > +/** > + * get_efi_leafname() - Get the leaf name for the EFI file we expect > + * > + * @str: Place to put leaf name for this architecture, e.g. "bootaa64.e= fi". > + * Must have at least 16 bytes of space > + * @max_len: Length of @str, must be >=3D16 > + */ > +static int get_efi_leafname(char *str, int max_len) > +{ > + const char *base; > + > + if (max_len < 16) > + return log_msg_ret("spc", -ENOSPC); > + if (IS_ENABLED(CONFIG_ARM64)) > + base =3D "bootaa64"; > + else if (IS_ENABLED(CONFIG_ARM)) > + base =3D "bootarm"; > + else if (IS_ENABLED(CONFIG_X86_RUN_32BIT)) > + base =3D "bootia32"; > + else if (IS_ENABLED(CONFIG_X86_RUN_64BIT)) > + base =3D "bootx64"; > + else if (IS_ENABLED(CONFIG_ARCH_RV32I)) > + base =3D "bootriscv32"; > + else if (IS_ENABLED(CONFIG_ARCH_RV64I)) > + base =3D "bootriscv64"; > + else if (IS_ENABLED(CONFIG_SANDBOX)) > + base =3D "bootsbox"; > + else > + return -EINVAL; > + > + strcpy(str, base); > + strcat(str, ".efi"); > + > + return 0; > +} > + > +static int efiload_read_file(struct blk_desc *desc, struct bootflow *bf= low) > +{ > + const struct udevice *media_dev; > + int size =3D bflow->size; > + char devnum_str[9]; > + char dirname[200]; > + loff_t bytes_read; > + char *last_slash; > + ulong addr; > + char *buf; > + int ret; > + > + /* Sadly FS closes the file after fs_size() so we must redo this */ > + ret =3D fs_set_blk_dev_with_part(desc, bflow->part); > + if (ret) > + return log_msg_ret("set", ret); > + > + buf =3D malloc(size + 1); > + if (!buf) > + return log_msg_ret("buf", -ENOMEM); > + addr =3D map_to_sysmem(buf); > + > + ret =3D fs_read(bflow->fname, addr, 0, 0, &bytes_read); > + if (ret) { > + free(buf); > + return log_msg_ret("read", ret); > + } > + if (size !=3D bytes_read) > + return log_msg_ret("bread", -EINVAL); > + buf[size] =3D '\0'; > + bflow->state =3D BOOTFLOWST_READY; > + bflow->buf =3D buf; > + > + /* > + * This is a horrible hack to tell EFI about this boot device. Once we > + * unify EFI with the rest of U-Boot we can clean this up. The same ha= ck > + * exists in multiple places, e.g. in the fs, tftp and load commands. > + * > + * Once we can clean up the EFI code to make proper use of driver mode= l, > + * this can go away. > + */ > + media_dev =3D dev_get_parent(bflow->dev); > + snprintf(devnum_str, sizeof(devnum_str), "%x", dev_seq(media_dev)); > + > + strlcpy(dirname, bflow->fname, sizeof(dirname)); > + last_slash =3D strrchr(dirname, '/'); > + if (last_slash) > + *last_slash =3D '\0'; > + > + efi_set_bootdev(dev_get_uclass_name(media_dev), devnum_str, dirname, > + bflow->buf, size); > + > + return 0; > +} > + > +static int distro_efi_check(struct udevice *dev, struct bootflow_iter *= iter) > +{ > + int ret; > + > + /* This only works on block devices */ > + ret =3D bootflow_iter_uses_blk_dev(iter); > + if (ret) > + return log_msg_ret("blk", ret); > + > + return 0; > +} > + > +static int distro_efi_read_bootflow(struct udevice *dev, struct bootflo= w *bflow) > +{ > + struct blk_desc *desc =3D dev_get_uclass_plat(bflow->blk); > + char fname[sizeof(EFI_DIRNAME) + 16]; > + loff_t size; > + int ret; > + > + /* We require a partition table */ > + if (!bflow->part) > + return -ENOENT; > + > + strcpy(fname, EFI_DIRNAME); > + ret =3D get_efi_leafname(fname + strlen(fname), > + sizeof(fname) - strlen(fname)); > + if (ret) > + return log_msg_ret("leaf", ret); > + > + bflow->fname =3D strdup(fname); > + if (!bflow->fname) > + return log_msg_ret("name", -ENOMEM); > + ret =3D fs_size(bflow->fname, &size); > + bflow->size =3D size; > + if (ret) > + return log_msg_ret("size", ret); > + bflow->state =3D BOOTFLOWST_FILE; > + log_debug(" - distro file size %x\n", (uint)size); > + if (size > 0x2000000) > + return log_msg_ret("chk", -E2BIG); > + > + ret =3D efiload_read_file(desc, bflow); > + if (ret) > + return log_msg_ret("read", -EINVAL); > + > + return 0; > +} > + > +static int distro_efi_read_file(struct udevice *dev, struct bootflow *b= flow, > + const char *file_path, ulong addr, ulong *sizep) > +{ > + struct blk_desc *desc =3D dev_get_uclass_plat(bflow->blk); > + const struct udevice *media_dev; > + int size =3D bflow->size; > + char devnum_str[9]; > + char dirname[200]; > + loff_t bytes_read; > + char *last_slash; > + char *buf; > + int ret; > + > + /* Sadly FS closes the file after fs_size() so we must redo this */ > + ret =3D fs_set_blk_dev_with_part(desc, bflow->part); > + if (ret) > + return log_msg_ret("set", ret); > + > + buf =3D malloc(size + 1); > + if (!buf) > + return log_msg_ret("buf", -ENOMEM); > + addr =3D map_to_sysmem(buf); > + > + ret =3D fs_read(bflow->fname, addr, 0, 0, &bytes_read); > + if (ret) { > + free(buf); > + return log_msg_ret("read", ret); > + } > + if (size !=3D bytes_read) > + return log_msg_ret("bread", -EINVAL); > + buf[size] =3D '\0'; > + bflow->state =3D BOOTFLOWST_READY; > + bflow->buf =3D buf; > + > + /* > + * This is a horrible hack to tell EFI about this boot device. Once we > + * unify EFI with the rest of U-Boot we can clean this up. The same ha= ck > + * exists in multiple places, e.g. in the fs, tftp and load commands. > + * > + * Once we can clean up the EFI code to make proper use of driver mode= l, > + * this can go away. > + */ > + media_dev =3D dev_get_parent(bflow->dev); > + snprintf(devnum_str, sizeof(devnum_str), "%x", dev_seq(media_dev)); > + > + strlcpy(dirname, bflow->fname, sizeof(dirname)); > + last_slash =3D strrchr(dirname, '/'); > + if (last_slash) > + *last_slash =3D '\0'; > + > + efi_set_bootdev(dev_get_uclass_name(media_dev), devnum_str, dirname, > + bflow->buf, size); > + > + return 0; > +} > + > +int distro_efi_boot(struct udevice *dev, struct bootflow *bflow) > +{ > + char cmd[50]; > + > + /* > + * At some point we can add a real interface to bootefi so we can call > + * this directly. For now, go through the CLI like distro boot. > + */ > + snprintf(cmd, sizeof(cmd), "bootefi %lx %lx", > + (ulong)map_to_sysmem(bflow->buf), > + (ulong)map_to_sysmem(gd->fdt_blob)); > + if (run_command(cmd, 0)) > + return log_msg_ret("run", -EINVAL); > + > + return 0; > +} > + > +static int distro_bootmeth_efi_bind(struct udevice *dev) > +{ > + struct bootmeth_uc_plat *plat =3D dev_get_uclass_plat(dev); > + > + plat->desc =3D "EFI boot from a .efi file"; > + > + return 0; > +} > + > +static struct bootmeth_ops distro_efi_bootmeth_ops =3D { > + .check =3D distro_efi_check, > + .read_bootflow =3D distro_efi_read_bootflow, > + .read_file =3D distro_efi_read_file, > + .boot =3D distro_efi_boot, > +}; > + > +static const struct udevice_id distro_efi_bootmeth_ids[] =3D { > + { .compatible =3D "u-boot,distro-efi" }, > + { } > +}; > + > +U_BOOT_DRIVER(bootmeth_efi) =3D { > + .name =3D "bootmeth_efi", > + .id =3D UCLASS_BOOTMETH, > + .of_match =3D distro_efi_bootmeth_ids, > + .ops =3D &distro_efi_bootmeth_ops, > + .bind =3D distro_bootmeth_efi_bind, > +}; >