[PATCH v5 04/28] efi: Locate all block devices in the app

2021-12-04 Thread Simon Glass
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.

Series-changes; 2
- Store device path in struct efi_media_plat
- Don't export efi_bind_block()
- Only bind devices for media devices, not for partitions
- Show devices that are processed
- Update documentation

Signed-off-by: Simon Glass 
---

(no changes since v1)

 doc/develop/uefi/u-boot_on_efi.rst |   4 +-
 include/efi.h  |   6 +-
 include/efi_api.h  |  15 ++
 lib/efi/efi_app.c  | 223 +
 4 files changed, 243 insertions(+), 5 deletions(-)

diff --git a/doc/develop/uefi/u-boot_on_efi.rst 
b/doc/develop/uefi/u-boot_on_efi.rst
index 5f2f850f071..8f81b799072 100644
--- a/doc/develop/uefi/u-boot_on_efi.rst
+++ b/doc/develop/uefi/u-boot_on_efi.rst
@@ -265,9 +265,7 @@ This work could be extended in a number of ways:
 
 - Figure out how to solve the interrupt problem
 
-- Add more drivers to the application side (e.g. block devices, USB,
-  environment access). This would mostly be an academic exercise as a strong
-  use case is not readily apparent, but it might be fun.
+- Add more drivers to the application side (e.g.USB, environment access).
 
 - Avoid turning off boot services in the stub. Instead allow U-Boot to make
   use of boot services in case it wants to. It is unclear what it might want
diff --git a/include/efi.h b/include/efi.h
index 0ec5913ddd1..908c5dc6ebd 100644
--- a/include/efi.h
+++ b/include/efi.h
@@ -419,10 +419,12 @@ struct efi_priv {
  *
  * @handle: handle of the controller on which this driver is installed
  * @blkio: block io protocol proxied by this driver
+ * @device_path: EFI path to the device
  */
 struct efi_media_plat {
-   efi_handle_thandle;
-   struct efi_block_io *blkio;
+   efi_handle_t handle;
+   struct efi_block_io *blkio;
+   struct efi_device_path *device_path;
 };
 
 /* Base address of the EFI image */
diff --git a/include/efi_api.h b/include/efi_api.h
index 80109f012bc..ec9fa89a935 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -2035,4 +2035,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..e38d46b15db 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,64 @@ int efi_info_get(enum efi_entry_t type, void **datap, int 
*sizep)
return -ENOSYS;
 }
 
+/**
+ * efi_print_str() - Print a UFT-16 string to the U-Boot console
+ *
+ * @str: String to print
+ */
+static void efi_print_str(const u16 *str)
+{
+   while (*str) {
+   int ch = *str++;
+
+   if (ch > ' ' && ch < 127)
+   putc(ch);
+   }
+}
+
+/**
+ * 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 device 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
+ * @device_path: EFI device path structure for this
+ * @len: Length of @device_path in bytes
+ * @devp: Returns the bound device
+ * @return 0 if OK, -ve on error
+ */
+int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio,
+  struct efi_device_path *device_path, int len,
+  struct udevice **devp)
+{
+   struct efi_media_plat plat;
+   struct udevice *dev;
+   char name[18];
+   int ret;
+
+   plat.handle = handle;
+   plat.blkio = blkio;
+   plat.device_path = malloc(device_path->length);
+   if (!plat.device_path)
+   return log_msg_ret("path", -ENOMEM);
+   memcpy(plat.device_path, device_path, device_path->length);
+   ret =

Re: [PATCH v5 04/28] efi: Locate all block devices in the app

2021-12-04 Thread Heinrich Schuchardt

On 12/4/21 16:56, 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.

Series-changes; 2
- Store device path in struct efi_media_plat
- Don't export efi_bind_block()
- Only bind devices for media devices, not for partitions
- Show devices that are processed
- Update documentation

Signed-off-by: Simon Glass 
---

(no changes since v1)

  doc/develop/uefi/u-boot_on_efi.rst |   4 +-
  include/efi.h  |   6 +-
  include/efi_api.h  |  15 ++
  lib/efi/efi_app.c  | 223 +
  4 files changed, 243 insertions(+), 5 deletions(-)

diff --git a/doc/develop/uefi/u-boot_on_efi.rst 
b/doc/develop/uefi/u-boot_on_efi.rst
index 5f2f850f071..8f81b799072 100644
--- a/doc/develop/uefi/u-boot_on_efi.rst
+++ b/doc/develop/uefi/u-boot_on_efi.rst
@@ -265,9 +265,7 @@ This work could be extended in a number of ways:

  - Figure out how to solve the interrupt problem

-- Add more drivers to the application side (e.g. block devices, USB,
-  environment access). This would mostly be an academic exercise as a strong
-  use case is not readily apparent, but it might be fun.
+- Add more drivers to the application side (e.g.USB, environment access).

  - Avoid turning off boot services in the stub. Instead allow U-Boot to make
use of boot services in case it wants to. It is unclear what it might want
diff --git a/include/efi.h b/include/efi.h
index 0ec5913ddd1..908c5dc6ebd 100644
--- a/include/efi.h
+++ b/include/efi.h
@@ -419,10 +419,12 @@ struct efi_priv {
   *
   * @handle: handle of the controller on which this driver is installed
   * @blkio: block io protocol proxied by this driver
+ * @device_path: EFI path to the device
   */
  struct efi_media_plat {
-   efi_handle_thandle;
-   struct efi_block_io *blkio;
+   efi_handle_t handle;
+   struct efi_block_io *blkio;
+   struct efi_device_path *device_path;
  };

  /* Base address of the EFI image */
diff --git a/include/efi_api.h b/include/efi_api.h
index 80109f012bc..ec9fa89a935 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -2035,4 +2035,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..e38d46b15db 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,64 @@ int efi_info_get(enum efi_entry_t type, void **datap, int 
*sizep)
return -ENOSYS;
  }

+/**
+ * efi_print_str() - Print a UFT-16 string to the U-Boot console
+ *
+ * @str: String to print
+ */
+static void efi_print_str(const u16 *str)
+{
+   while (*str) {
+   int ch = *str++;
+
+   if (ch > ' ' && ch < 127)
+   putc(ch);
+   }
+}
+
+/**
+ * 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 device 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
+ * @device_path: EFI device path structure for this
+ * @len: Length of @device_path in bytes
+ * @devp: Returns the bound device
+ * @return 0 if OK, -ve on error
+ */
+int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio,
+  struct efi_device_path *device_path, int len,
+  struct udevice **devp)
+{
+   struct efi_media_plat plat;
+   struct udevice *dev;
+   char name[18];
+   int ret;
+
+   plat.handle = handle;
+   plat.blkio = blkio;
+   plat.device_path = malloc(device_path->length);
+   if (!plat.device_path)
+   return log_msg_ret("path", -ENOMEM);
+   memcpy(plat.device_p

Re: [PATCH v5 04/28] efi: Locate all block devices in the app

2021-12-09 Thread Heinrich Schuchardt

On 12/4/21 10:43, Heinrich Schuchardt wrote:

On 12/4/21 16:56, 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.

Series-changes; 2
- Store device path in struct efi_media_plat
- Don't export efi_bind_block()
- Only bind devices for media devices, not for partitions
- Show devices that are processed
- Update documentation

Signed-off-by: Simon Glass 
---

(no changes since v1)

  doc/develop/uefi/u-boot_on_efi.rst |   4 +-
  include/efi.h  |   6 +-
  include/efi_api.h  |  15 ++
  lib/efi/efi_app.c  | 223 +
  4 files changed, 243 insertions(+), 5 deletions(-)

diff --git a/doc/develop/uefi/u-boot_on_efi.rst
b/doc/develop/uefi/u-boot_on_efi.rst
index 5f2f850f071..8f81b799072 100644
--- a/doc/develop/uefi/u-boot_on_efi.rst
+++ b/doc/develop/uefi/u-boot_on_efi.rst
@@ -265,9 +265,7 @@ This work could be extended in a number of ways:

  - Figure out how to solve the interrupt problem

-- Add more drivers to the application side (e.g. block devices, USB,
-  environment access). This would mostly be an academic exercise as a
strong
-  use case is not readily apparent, but it might be fun.
+- Add more drivers to the application side (e.g.USB, environment
access).

  - Avoid turning off boot services in the stub. Instead allow U-Boot
to make
    use of boot services in case it wants to. It is unclear what it
might want
diff --git a/include/efi.h b/include/efi.h
index 0ec5913ddd1..908c5dc6ebd 100644
--- a/include/efi.h
+++ b/include/efi.h
@@ -419,10 +419,12 @@ struct efi_priv {
   *
   * @handle: handle of the controller on which this driver is installed
   * @blkio: block io protocol proxied by this driver
+ * @device_path: EFI path to the device
   */
  struct efi_media_plat {
-    efi_handle_t    handle;
-    struct efi_block_io    *blkio;
+    efi_handle_t handle;
+    struct efi_block_io *blkio;
+    struct efi_device_path *device_path;
  };

  /* Base address of the EFI image */
diff --git a/include/efi_api.h b/include/efi_api.h
index 80109f012bc..ec9fa89a935 100644
--- a/include/efi_api.h
+++ b/include/efi_api.h
@@ -2035,4 +2035,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..e38d46b15db 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,64 @@ int efi_info_get(enum efi_entry_t type, void
**datap, int *sizep)
  return -ENOSYS;
  }

+/**
+ * efi_print_str() - Print a UFT-16 string to the U-Boot console
+ *
+ * @str: String to print
+ */
+static void efi_print_str(const u16 *str)
+{
+    while (*str) {
+    int ch = *str++;
+
+    if (ch > ' ' && ch < 127)
+    putc(ch);
+    }
+}
+
+/**
+ * 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
device 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
+ * @device_path: EFI device path structure for this
+ * @len: Length of @device_path in bytes
+ * @devp: Returns the bound device
+ * @return 0 if OK, -ve on error
+ */
+int efi_bind_block(efi_handle_t handle, struct efi_block_io *blkio,
+   struct efi_device_path *device_path, int len,
+   struct udevice **devp)
+{
+    struct efi_media_plat plat;
+    struct udevice *dev;
+    char name[18];
+    int ret;
+
+    plat.handle = handle;
+    plat.blkio = blkio;
+    plat.device_path = malloc(device_path->length);
+    if (!plat.device_path)
+    return log_msg_ret("path", -ENOMEM);
+    memcpy(plat.device_path, device_path, device_path->length);
+    ret = device_bind(dm_root(), DM_DRIVER_GET(efi_media), "efi_media",
+  &plat, ofnode_null(), &dev);
+    if

Re: [PATCH v5 04/28] efi: Locate all block devices in the app

2021-12-17 Thread Simon Glass
Hi Heinrich,

On Thu, 9 Dec 2021 at 12:23, Heinrich Schuchardt  wrote:
>
> On 12/4/21 10:43, Heinrich Schuchardt wrote:
> > On 12/4/21 16:56, 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.
> >>
> >> Series-changes; 2
> >> - Store device path in struct efi_media_plat
> >> - Don't export efi_bind_block()
> >> - Only bind devices for media devices, not for partitions
> >> - Show devices that are processed
> >> - Update documentation
> >>
> >> Signed-off-by: Simon Glass 
> >> ---
> >>
> >> (no changes since v1)
> >>
> >>   doc/develop/uefi/u-boot_on_efi.rst |   4 +-
> >>   include/efi.h  |   6 +-
> >>   include/efi_api.h  |  15 ++
> >>   lib/efi/efi_app.c  | 223 +
> >>   4 files changed, 243 insertions(+), 5 deletions(-)
[..]

> >> +static int setup_disks(void)
> >> +{
> >> +/* This is not fully implemented yet */
>
> see hint below.
>
> >> +return 0;
> >> +
> >> +efi_guid_t efi_disk_guid = EFI_DISK_IO_PROTOCOL_GUID;
>
> U-Boot does not implement the EFI_DISK_IO_PROTOCOL. So you will not be
> able to run U-Boot as an EFI app loaded by U-Boot which might be nice
> for testing.
>
> The more basic protocol is the EFI_BLOCK_IO_PROTOCOL. The difference is
> that the EFI_BLOCK_IO_PROTOCOL requires using a properly aligned buffer.
>
> >> +struct efi_boot_services *boot = efi_get_boot();
> >> +struct efi_disk *disk;
> >> +int ret;
> >> +
> >> +if (!boot)
> >> +return log_msg_ret("sys", -ENOSYS);
> >> +ret = boot->locate_protocol(&efi_disk_guid, NULL, (void **)&disk);
>
> If you want to find all handles implementing a protocol, you can use
> EFI_BOOT_SERVICES.LocateHandleBuffer() with SearchType ByProtocol.
>
> I guess we should add this patch to U-Boot once it is completed.

OK, let's drop this code then.

[..]
Regards,
Simon