Re: [RFC v2 15/20] efi_loader: disk: a helper function to create efi_disk objects from udevice

2022-01-05 Thread AKASHI Takahiro
On Sun, Jan 02, 2022 at 10:18:18AM +0100, Heinrich Schuchardt wrote:
> On 12/10/21 07:49, AKASHI Takahiro wrote:
> > Add efi_disk_probe() function.
> > This function creates an efi_disk object for a raw disk device (UCLASS_BLK)
> > and additional objects for related partitions (UCLASS_PARTITION).
> > 
> > So this function is expected to be called through driver model's "probe"
> > interface every time one raw disk device is detected and activated.
> > We assume that partition devices (UCLASS_PARTITION) have been created
> > when this function is invoked.
> > 
> > Signed-off-by: AKASHI Takahiro 
> > ---
> >   include/efi_loader.h   |   4 +-
> >   lib/efi_loader/Kconfig |   2 +
> >   lib/efi_loader/efi_disk.c  | 206 -
> >   lib/efi_loader/efi_setup.c |  11 +-
> >   4 files changed, 142 insertions(+), 81 deletions(-)
> > 
> > diff --git a/include/efi_loader.h b/include/efi_loader.h
> > index d52e399841ba..a51095930efa 100644
> > --- a/include/efi_loader.h
> > +++ b/include/efi_loader.h
> > @@ -519,8 +519,8 @@ efi_status_t EFIAPI efi_convert_pointer(efi_uintn_t 
> > debug_disposition,
> >   void efi_carve_out_dt_rsv(void *fdt);
> >   /* Called by bootefi to make console interface available */
> >   efi_status_t efi_console_register(void);
> > -/* Called by bootefi to make all disk storage accessible as EFI objects */
> > -efi_status_t efi_disk_register(void);
> > +/* Called by efi_init_obj_list() to initialize efi_disks */
> > +efi_status_t efi_disk_init(void);
> >   /* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
> >   efi_status_t efi_rng_register(void);
> >   /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
> > diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
> > index 700dc838ddb9..108c00343fce 100644
> > --- a/lib/efi_loader/Kconfig
> > +++ b/lib/efi_loader/Kconfig
> > @@ -11,6 +11,7 @@ config EFI_LOADER
> > # We need EFI_STUB_32BIT to be set on x86_32 with EFI_STUB
> > depends on !EFI_STUB || !X86 || X86_64 || EFI_STUB_32BIT
> > depends on BLK
> > +   depends on EVENT
> > depends on DM_ETH || !NET
> > depends on !EFI_APP
> > default y if !ARM || SYS_CPU = armv7 || SYS_CPU = armv8
> > @@ -41,6 +42,7 @@ config CMD_BOOTEFI_BOOTMGR
> > 
> >   config EFI_SETUP_EARLY
> > bool
> > +   default y
> > 
> >   choice
> > prompt "Store for non-volatile UEFI variables"
> > diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c
> > index 45127d176869..2941b0c3db47 100644
> > --- a/lib/efi_loader/efi_disk.c
> > +++ b/lib/efi_loader/efi_disk.c
> > @@ -10,6 +10,9 @@
> >   #include 
> >   #include 
> >   #include 
> > +#include 
> > +#include 
> > +#include 
> >   #include 
> >   #include 
> >   #include 
> > @@ -487,103 +490,158 @@ error:
> > return ret;
> >   }
> > 
> > -/**
> > - * efi_disk_create_partitions() - create handles and protocols for 
> > partitions
> > +/*
> > + * Create a handle for a whole raw disk
> >*
> > - * Create handles and protocols for the partitions of a block device.
> > + * @devuclass device (UCLASS_BLK)
> >*
> > - * @parent:handle of the parent disk
> > - * @desc:  block device
> > - * @if_typename:   interface type
> > - * @diskid:device number
> > - * @pdevname:  device name
> > - * Return: number of partitions created
> > + * Create an efi_disk object which is associated with @dev.
> > + * The type of @dev must be UCLASS_BLK.
> > + *
> > + * @return 0 on success, -1 otherwise
> >*/
> > -int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
> > -  const char *if_typename, int diskid,
> > -  const char *pdevname)
> > +static int efi_disk_create_raw(struct udevice *dev)
> >   {
> > -   int disks = 0;
> > -   char devname[32] = { 0 }; /* dp->str is u16[32] long */
> > -   int part;
> > -   struct efi_device_path *dp = NULL;
> > +   struct efi_disk_obj *disk;
> > +   struct blk_desc *desc;
> > +   const char *if_typename;
> > +   int diskid;
> > efi_status_t ret;
> > -   struct efi_handler *handler;
> > 
> > -   /* Get the device path of the parent */
> > -   ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
> > -   if (ret == EFI_SUCCESS)
> > -   dp = handler->protocol_interface;
> > -
> > -   /* Add devices for each partition */
> > -   for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
> > -   struct disk_partition info;
> > -
> > -   if (part_get_info(desc, part, &info))
> > -   continue;
> > -   snprintf(devname, sizeof(devname), "%s:%x", pdevname,
> > -part);
> > -   ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
> > -  &info, part, NULL);
> > -   if (ret != EFI_SUCCESS) {
> > -   log_err("Adding partition %s failed\n", pdevname);
> > -

Re: [RFC v2 15/20] efi_loader: disk: a helper function to create efi_disk objects from udevice

2022-01-02 Thread Heinrich Schuchardt

On 12/10/21 07:49, AKASHI Takahiro wrote:

Add efi_disk_probe() function.
This function creates an efi_disk object for a raw disk device (UCLASS_BLK)
and additional objects for related partitions (UCLASS_PARTITION).

So this function is expected to be called through driver model's "probe"
interface every time one raw disk device is detected and activated.
We assume that partition devices (UCLASS_PARTITION) have been created
when this function is invoked.

Signed-off-by: AKASHI Takahiro 
---
  include/efi_loader.h   |   4 +-
  lib/efi_loader/Kconfig |   2 +
  lib/efi_loader/efi_disk.c  | 206 -
  lib/efi_loader/efi_setup.c |  11 +-
  4 files changed, 142 insertions(+), 81 deletions(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index d52e399841ba..a51095930efa 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -519,8 +519,8 @@ efi_status_t EFIAPI efi_convert_pointer(efi_uintn_t 
debug_disposition,
  void efi_carve_out_dt_rsv(void *fdt);
  /* Called by bootefi to make console interface available */
  efi_status_t efi_console_register(void);
-/* Called by bootefi to make all disk storage accessible as EFI objects */
-efi_status_t efi_disk_register(void);
+/* Called by efi_init_obj_list() to initialize efi_disks */
+efi_status_t efi_disk_init(void);
  /* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
  efi_status_t efi_rng_register(void);
  /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index 700dc838ddb9..108c00343fce 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -11,6 +11,7 @@ config EFI_LOADER
# We need EFI_STUB_32BIT to be set on x86_32 with EFI_STUB
depends on !EFI_STUB || !X86 || X86_64 || EFI_STUB_32BIT
depends on BLK
+   depends on EVENT
depends on DM_ETH || !NET
depends on !EFI_APP
default y if !ARM || SYS_CPU = armv7 || SYS_CPU = armv8
@@ -41,6 +42,7 @@ config CMD_BOOTEFI_BOOTMGR

  config EFI_SETUP_EARLY
bool
+   default y

  choice
prompt "Store for non-volatile UEFI variables"
diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c
index 45127d176869..2941b0c3db47 100644
--- a/lib/efi_loader/efi_disk.c
+++ b/lib/efi_loader/efi_disk.c
@@ -10,6 +10,9 @@
  #include 
  #include 
  #include 
+#include 
+#include 
+#include 
  #include 
  #include 
  #include 
@@ -487,103 +490,158 @@ error:
return ret;
  }

-/**
- * efi_disk_create_partitions() - create handles and protocols for partitions
+/*
+ * Create a handle for a whole raw disk
   *
- * Create handles and protocols for the partitions of a block device.
+ * @devuclass device (UCLASS_BLK)
   *
- * @parent:handle of the parent disk
- * @desc:  block device
- * @if_typename:   interface type
- * @diskid:device number
- * @pdevname:  device name
- * Return: number of partitions created
+ * Create an efi_disk object which is associated with @dev.
+ * The type of @dev must be UCLASS_BLK.
+ *
+ * @return 0 on success, -1 otherwise
   */
-int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
-  const char *if_typename, int diskid,
-  const char *pdevname)
+static int efi_disk_create_raw(struct udevice *dev)
  {
-   int disks = 0;
-   char devname[32] = { 0 }; /* dp->str is u16[32] long */
-   int part;
-   struct efi_device_path *dp = NULL;
+   struct efi_disk_obj *disk;
+   struct blk_desc *desc;
+   const char *if_typename;
+   int diskid;
efi_status_t ret;
-   struct efi_handler *handler;

-   /* Get the device path of the parent */
-   ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
-   if (ret == EFI_SUCCESS)
-   dp = handler->protocol_interface;
-
-   /* Add devices for each partition */
-   for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
-   struct disk_partition info;
-
-   if (part_get_info(desc, part, &info))
-   continue;
-   snprintf(devname, sizeof(devname), "%s:%x", pdevname,
-part);
-   ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
-  &info, part, NULL);
-   if (ret != EFI_SUCCESS) {
-   log_err("Adding partition %s failed\n", pdevname);
-   continue;
-   }
-   disks++;
+   desc = dev_get_uclass_plat(dev);
+   if_typename = blk_get_if_type_name(desc->if_type);
+   diskid = desc->devnum;
+
+   ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
+  diskid, NULL, 0, &disk);
+   if (ret != EFI_SUCCESS) {
+   if (ret == EFI_NOT_READY)
+  

[RFC v2 15/20] efi_loader: disk: a helper function to create efi_disk objects from udevice

2021-12-09 Thread AKASHI Takahiro
Add efi_disk_probe() function.
This function creates an efi_disk object for a raw disk device (UCLASS_BLK)
and additional objects for related partitions (UCLASS_PARTITION).

So this function is expected to be called through driver model's "probe"
interface every time one raw disk device is detected and activated.
We assume that partition devices (UCLASS_PARTITION) have been created
when this function is invoked.

Signed-off-by: AKASHI Takahiro 
---
 include/efi_loader.h   |   4 +-
 lib/efi_loader/Kconfig |   2 +
 lib/efi_loader/efi_disk.c  | 206 -
 lib/efi_loader/efi_setup.c |  11 +-
 4 files changed, 142 insertions(+), 81 deletions(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index d52e399841ba..a51095930efa 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -519,8 +519,8 @@ efi_status_t EFIAPI efi_convert_pointer(efi_uintn_t 
debug_disposition,
 void efi_carve_out_dt_rsv(void *fdt);
 /* Called by bootefi to make console interface available */
 efi_status_t efi_console_register(void);
-/* Called by bootefi to make all disk storage accessible as EFI objects */
-efi_status_t efi_disk_register(void);
+/* Called by efi_init_obj_list() to initialize efi_disks */
+efi_status_t efi_disk_init(void);
 /* Called by efi_init_obj_list() to install EFI_RNG_PROTOCOL */
 efi_status_t efi_rng_register(void);
 /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig
index 700dc838ddb9..108c00343fce 100644
--- a/lib/efi_loader/Kconfig
+++ b/lib/efi_loader/Kconfig
@@ -11,6 +11,7 @@ config EFI_LOADER
# We need EFI_STUB_32BIT to be set on x86_32 with EFI_STUB
depends on !EFI_STUB || !X86 || X86_64 || EFI_STUB_32BIT
depends on BLK
+   depends on EVENT
depends on DM_ETH || !NET
depends on !EFI_APP
default y if !ARM || SYS_CPU = armv7 || SYS_CPU = armv8
@@ -41,6 +42,7 @@ config CMD_BOOTEFI_BOOTMGR
 
 config EFI_SETUP_EARLY
bool
+   default y
 
 choice
prompt "Store for non-volatile UEFI variables"
diff --git a/lib/efi_loader/efi_disk.c b/lib/efi_loader/efi_disk.c
index 45127d176869..2941b0c3db47 100644
--- a/lib/efi_loader/efi_disk.c
+++ b/lib/efi_loader/efi_disk.c
@@ -10,6 +10,9 @@
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -487,103 +490,158 @@ error:
return ret;
 }
 
-/**
- * efi_disk_create_partitions() - create handles and protocols for partitions
+/*
+ * Create a handle for a whole raw disk
  *
- * Create handles and protocols for the partitions of a block device.
+ * @devuclass device (UCLASS_BLK)
  *
- * @parent:handle of the parent disk
- * @desc:  block device
- * @if_typename:   interface type
- * @diskid:device number
- * @pdevname:  device name
- * Return: number of partitions created
+ * Create an efi_disk object which is associated with @dev.
+ * The type of @dev must be UCLASS_BLK.
+ *
+ * @return 0 on success, -1 otherwise
  */
-int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
-  const char *if_typename, int diskid,
-  const char *pdevname)
+static int efi_disk_create_raw(struct udevice *dev)
 {
-   int disks = 0;
-   char devname[32] = { 0 }; /* dp->str is u16[32] long */
-   int part;
-   struct efi_device_path *dp = NULL;
+   struct efi_disk_obj *disk;
+   struct blk_desc *desc;
+   const char *if_typename;
+   int diskid;
efi_status_t ret;
-   struct efi_handler *handler;
 
-   /* Get the device path of the parent */
-   ret = efi_search_protocol(parent, &efi_guid_device_path, &handler);
-   if (ret == EFI_SUCCESS)
-   dp = handler->protocol_interface;
-
-   /* Add devices for each partition */
-   for (part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
-   struct disk_partition info;
-
-   if (part_get_info(desc, part, &info))
-   continue;
-   snprintf(devname, sizeof(devname), "%s:%x", pdevname,
-part);
-   ret = efi_disk_add_dev(parent, dp, if_typename, desc, diskid,
-  &info, part, NULL);
-   if (ret != EFI_SUCCESS) {
-   log_err("Adding partition %s failed\n", pdevname);
-   continue;
-   }
-   disks++;
+   desc = dev_get_uclass_plat(dev);
+   if_typename = blk_get_if_type_name(desc->if_type);
+   diskid = desc->devnum;
+
+   ret = efi_disk_add_dev(NULL, NULL, if_typename, desc,
+  diskid, NULL, 0, &disk);
+   if (ret != EFI_SUCCESS) {
+   if (ret == EFI_NOT_READY)
+   log_notice("Disk %s not ready\n", dev->name);
+