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 X-Spam-Level: X-Spam-Status: No, score=-14.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4EA49C433EF for ; Wed, 8 Sep 2021 18:14:43 +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 951B661100 for ; Wed, 8 Sep 2021 18:14:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 951B661100 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 6EE688326B; Wed, 8 Sep 2021 20:14:40 +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="NfS1ZJdC"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0E99E83380; Wed, 8 Sep 2021 20:14:38 +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 9875D812B6 for ; Wed, 8 Sep 2021 20:14:34 +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=1631124871; bh=EWJscVLjee+NOV+geN1puY1ysNknqGddd4GeNoDAuAI=; h=X-UI-Sender-Class:Subject:To:Cc:References:From:Date:In-Reply-To; b=NfS1ZJdC3JG7eTLQ6Q3QyZ7i9W9yQ9l+WBqaPjIqLgNT6CzTsyatNjxqRT1FnZgPl hVLMuDDEvVUIgfKjQGdEJNfUWYPxO55N33XVmdzj3N5KLQ0TpubH5KFsc3i8UqqTbp cj391tQJENMPzhMX5aKg/UrnfQ7is0q9VsDET4eA= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.123.55] ([88.152.144.157]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MEFvj-1mGZJS0IBp-00AJDa; Wed, 08 Sep 2021 20:14:31 +0200 Subject: Re: [PATCH 14/35] efi: Locate all block devices in the app To: Simon Glass , U-Boot Mailing List Cc: Ilias Apalodimas , Bin Meng , Tom Rini , Christian Melki , Alexander Graf References: <20210908133405.696481-1-sjg@chromium.org> <20210908073355.14.I0774d3540ebe726f31838b851815e829fb6a4056@changeid> From: Heinrich Schuchardt Message-ID: <2924c018-6a5b-4261-cff3-0ad1a416ef98@gmx.de> Date: Wed, 8 Sep 2021 20:14:30 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20210908073355.14.I0774d3540ebe726f31838b851815e829fb6a4056@changeid> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:nslN79Av6ZZPE6Qk8AyNCskeXDGAa73IJTv+zzkYLi2+pjcb5QU HW07oOduNRpne22kG0jZxImpfpfaKRFbjkmwLI75X+7Z1FLZKz2/RCvsMfptzVC+sgZH9Wy DRH7qxIPOJGo0e+UDCtbTYwL75kBln86sgNbm6PgdY9RRWoXnoDhiFIYDLbF1pmTtQEfDDO IR8KCdFh77PHYcavw6CRQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:wqk0FGxKZUg=:zfDK/bC5agquUlzznn6EDe 3G1AQzM+DqLTBe8aPR7uGaGsjUOpL3d2P0J5CyXx8d2eLY9lIsm35lelbxjFOQB7x35V/9mXG kkzMbEax5yagSEncHY7m9N1tp84m4q0WEV8U0tio/MTVh+4M72+UNsSyuGw9aF52/LWYqE9gK Ke9mUFxVoNZeFx+5jCIDczxczW5cx0Dr9VPN2HQshkSSQWTrI5afhXoV/Vmi8r2aD0vp5vr2m Oo1WQmLtwV2djmzXFxSFUFJg5wr4ifhacx6hu7UMMUaQXETVbGhp2Cm5ZBR4x0f2AmCivkpGs sVR4OHZGu1wkR9hZgNchgqpR4iCoLcrgJAWKBqCgNpK+Xc+qc6P8BFkx13FNA1PyH7krfkVAO QmJfqfHkawxBGuoy/3UZWHOPAGuv/SX0dl50GHDbbx0f57Tj2eXA9qBy5OK1JVN/TfQdt4Qnf 6+rLGk7s9lGxAYv8joZjbUF1BvgXBWETQSAEpxjC9yluRONvjkbCkYQqn5Z2Fv8v+kRteUwpm mtzckXAJDh+407WZT0Vi1ECpvF6AHXvi5ODD387VXP+s/KpA798emwx3xyP5QfdXXhTl2GFy+ fcoYed3NFWVl5n3C1+UBNtdPkz5FkmSRaRnUud06ItF+E8E06karsjWGHqS+KAVo5flAU4CWK pwM2COA3IGZYw0CxXgK2HPoHAksNQgT1nfUqXBQZf4HqKwEfwq2Z9ft5ZYAMop7w3jVsgrJpj o/SLqkWw8b4MHgtSbTv0LVDMmn3P2UfXA+oc5H67BLH6o0LXg8FvgSeoqhcVksn0o6oTP/UeE 8uU0ir2jU5uqsZ4FZYC6yv7bOjCveABZtzEXPW0VPtC2KKyVXCtIUKy0m+WzR4eEIxifAtM2+ 3Hbwu2Ed8yVbu03L9pgP2QYK2R0e40nCNUKPCg65sKRCkxlNJUoncAjdnyalhBznnHfpV49yX odE6bmNro52l8vtDDKshouv3kf38+LVtyujiEVuo5bV/hFushZPmiX5R3wlymuSUOwVucU4tx J7V7Fgf5Bii9wDvz2izH9x1aex4qwGiOX62KQ+unlzxhF2Kpai/exBaLGB7P7b9N0P5uRMZde /OWaBNsFvne+eY= 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 9/8/21 3:33 PM, Simon Glass wrote: > When starting the app, locate all block devices and make them available > to U-Boot. This allows listing partitions and accessing files in > filesystems. > > EFI also has the concept of 'disks', meaning boot media. For now, this > is not obviously useful in U-Boot, but add code to at least locate these= . > This can be expanded later as needed. UEFI firmware handles with the EFI_BLOCK_IO_PROTOCOL for raw access to disks and partitions. It further provides the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL on these handles to access files on formatted media. > > Signed-off-by: Simon Glass > --- > > include/efi.h | 15 ++++++ > include/efi_api.h | 15 ++++++ > lib/efi/efi_app.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 149 insertions(+) > > diff --git a/include/efi.h b/include/efi.h > index 0ec5913ddd1..c0fddf7f6cd 100644 > --- a/include/efi.h > +++ b/include/efi.h > @@ -529,4 +529,19 @@ void efi_putc(struct efi_priv *priv, const char ch)= ; > */ > int efi_info_get(enum efi_entry_t type, void **datap, int *sizep); > > +/** > + * efi_bind_block() - bind a new block device to an EFI device > + * > + * Binds a new top-level EFI_MEDIA device as well as a child block devi= ce so > + * that the block device can be accessed in U-Boot. > + * > + * The device can then be accessed using 'part list efi 0', 'fat ls efi= 0:1', > + * for example, just like any other interface type. > + * > + * @handle: handle of the controller on which this driver is installed > + * @blkio: block io protocol proxied by this driver > + * @return 0 if OK, -ve on error > + */ > +int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio); > + > #endif /* _LINUX_EFI_H */ > diff --git a/include/efi_api.h b/include/efi_api.h > index c8f959bb720..0e88b3e5dbe 100644 > --- a/include/efi_api.h > +++ b/include/efi_api.h > @@ -1994,4 +1994,19 @@ struct efi_firmware_management_protocol { > const u16 *package_version_name); > }; > > +#define EFI_DISK_IO_PROTOCOL_GUID \ > + EFI_GUID(0xce345171, 0xba0b, 0x11d2, 0x8e, 0x4f, \ > + 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b) > + > +struct efi_disk { > + u64 revision; > + efi_status_t (EFIAPI *read_disk)(struct efi_disk *this, u32 media_id, > + u64 offset, efi_uintn_t buffer_size, > + void *buffer); > + > + efi_status_t (EFIAPI *write_disk)(struct efi_disk *this, u32 media_id, > + u64 offset, efi_uintn_t buffer_size, > + void *buffer); > +}; > + > #endif > diff --git a/lib/efi/efi_app.c b/lib/efi/efi_app.c > index f61665686c5..9ba48517422 100644 > --- a/lib/efi/efi_app.c > +++ b/lib/efi/efi_app.c > @@ -21,6 +21,9 @@ > #include > #include > #include > +#include > +#include > +#include > > DECLARE_GLOBAL_DATA_PTR; > > @@ -46,6 +49,33 @@ int efi_info_get(enum efi_entry_t type, void **datap,= int *sizep) > return -ENOSYS; > } > > +/** > + * Create a block device so U-Boot can access an EFI device > + * > + * @handle: EFI handle to bind > + * @blkio: block io protocol > + * Return: 0 =3D success > + */ > +int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio) > +{ > + struct efi_media_plat plat; > + struct udevice *dev; > + char name[18]; > + int ret; > + > + plat.handle =3D handle; > + plat.blkio =3D blkio; > + ret =3D device_bind(dm_root(), DM_DRIVER_GET(efi_media), "efi_media", > + &plat, ofnode_null(), &dev); > + if (ret) > + return log_msg_ret("bind", ret); > + > + snprintf(name, sizeof(name), "efi_media_%x", dev_seq(dev)); > + device_set_name(dev, name); > + > + return 0; > +} > + > static efi_status_t setup_memory(struct efi_priv *priv) > { > struct efi_boot_services *boot =3D priv->boot; > @@ -105,6 +135,95 @@ static void free_memory(struct efi_priv *priv) > global_data_ptr =3D NULL; > } > > +static int setup_disks(void) > +{ > + /* This is not fully implemented yet */ > + return 0; > + > + efi_guid_t efi_disk_guid =3D EFI_DISK_IO_PROTOCOL_GUID; > + struct efi_boot_services *boot =3D efi_get_boot(); > + struct efi_disk *disk; > + int ret; > + > + if (!boot) > + return log_msg_ret("sys", -ENOSYS); > + ret =3D boot->locate_protocol(&efi_disk_guid, NULL, (void **)&disk); > + if (ret) > + return log_msg_ret("prot", -ENOTSUPP); > + > + return 0; > +} > + > +static int setup_block(void) > +{ > + efi_guid_t efi_blkio_guid =3D EFI_BLOCK_IO_PROTOCOL_GUID; > + efi_guid_t efi_devpath_guid =3D EFI_DEVICE_PATH_PROTOCOL_GUID; > + struct efi_boot_services *boot =3D efi_get_boot(); > + struct efi_block_io *blkio; > + struct efi_device_path device_path; > + efi_handle_t handle[100]; > + efi_uintn_t buf_size; > + int num_handles; > + int ret, i; > + > + if (!boot) > + return log_msg_ret("sys", -ENOSYS); > + > + buf_size =3D sizeof(handle); > + ret =3D boot->locate_handle(BY_PROTOCOL, &efi_blkio_guid, NULL, > + &buf_size, handle); You could use LocateHandleBuffer() here which will allocate enough memory for all matching handles. > + if (ret) > + return log_msg_ret("loc", -ENOTSUPP); > + > + num_handles =3D buf_size / sizeof(efi_handle_t); > + log_info("Found %d EFI handles\n", num_handles); > + > + for (i =3D 0; i < num_handles; i++) { > + ret =3D boot->handle_protocol(handle[i], &efi_devpath_guid, > + (void **)&device_path); Why do you read the devicepath if you don't use it? > + if (ret) { > + log_warning("- devpath %d failed (ret=3D%d)\n", i, ret); > + continue; > + } Here some analysis of devicepaths and installed protocols is missing to find out which of the handles represents a block device and which represents a partition: If the last devicepath node is type 4 , Media Device Path with SubType 1 Hard Drive and the partition number is non-zero it is a partition. If the devicepath without the last node relates to a handle with the EFI_BLOCK_IO_PROTOCOL, this also indicates that the current handle is for a partition. Best regards Heinrich > + > + ret =3D boot->handle_protocol(handle[i], &efi_blkio_guid, > + (void **)&blkio); > + if (ret) { > + log_warning("- blkio %d failed (ret=3D%d)\n", i, ret); > + continue; > + } > + > + ret =3D efi_bind_block(handle[i], blkio); > + if (ret) { > + log_warning("- blkio bind %d failed (ret=3D%d)\n", i, ret); > + continue; > + } > + } > + if (ret) > + return log_msg_ret("prot", -ENOTSUPP); > + > + return 0; > +} > + > +int dm_scan_other(bool pre_reloc_only) > +{ > + if (gd->flags & GD_FLG_RELOC) { > + int ret; > + > + /* Find all block devices and setup EFI devices for them */ > + ret =3D setup_block(); > + if (ret) > + return ret; > + > + /* Not needed at present, but could be useful one day? */ > + ret =3D setup_disks(); > + if (ret) > + return ret; > + } > + > + return 0; > +} > + > /** > * efi_main() - Start an EFI image > * >