diff mbox series

[v1,2/2] spl: Add support for booting from ESP

Message ID 20230914100821.755904-3-mchitale@ventanamicro.com
State Changes Requested
Delegated to: Tom Rini
Headers show
Series SPL EBBR - EFI System Partition support | expand

Commit Message

Mayuresh Chitale Sept. 14, 2023, 10:08 a.m. UTC
Some platforms as described by EBBR specification may store images in
the FIRMWARE directory of the UEFI system partition(ESP). Add support
to boot from the EFI system partition if it is enabled for a platform.

Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
---
 common/spl/Kconfig      |  7 +++++
 common/spl/spl_blk_fs.c | 61 +++++++++++++++++++++++++++++------------
 common/spl/spl_fat.c    | 34 ++++++++++++++++++++---
 3 files changed, 81 insertions(+), 21 deletions(-)

Comments

Tom Rini Sept. 14, 2023, 4:29 p.m. UTC | #1
On Thu, Sep 14, 2023 at 03:38:21PM +0530, Mayuresh Chitale wrote:

> Some platforms as described by EBBR specification may store images in
> the FIRMWARE directory of the UEFI system partition(ESP). Add support
> to boot from the EFI system partition if it is enabled for a platform.
> 
> Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
[snip]
> +config SPL_ESP_BOOT
> +	bool "Load next stage boot image from the UEFI system partition"
> +	select SPL_PARTITION_TYPE_GUID
> +	help
> +	  When enabled, first try to boot from the UEFI system partition as
> +	  described in the Ch.4 of the EBBR specification.

We need to select this by perhaps BOOT_DEFAULTS or BOOTMETH_DISTRO,
whatever is supposed to signify that yes, this is going to be a
SystemReady IR (or higher) compliant build.

[snip]
>  int spl_blk_load_image(struct spl_image_info *spl_image,
>  		       struct spl_boot_device *bootdev,
>  		       enum uclass_id uclass_id, int devnum, int partnum)
>  {
>  	const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
> -	struct legacy_img_hdr *header;
[snip]
> @@ -63,24 +88,26 @@ int spl_blk_load_image(struct spl_image_info *spl_image,
>  	}
>  
>  	blk_show_device(uclass_id, devnum);
> -	header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
> -
[snip]

Is this an artifact of Sean's patch series where "header" is no longer
actually used?  As-is this won't compile on top of next (which is where
given Sean's feedback, the series needs to be rebased upon anyhow).

> +	/*
> +	 * First try to boot from EFI System partition. In case of failure,
> +	 * fall back to the configured partition.
> +	 */

I don't know that this is the right behavior.  If we're configured to
boot from partition X, we boot from partition X.  If we're configured to
find the ESP and use that, we find and use that.
Mayuresh Chitale Sept. 21, 2023, 11:33 a.m. UTC | #2
On Thu, 2023-09-14 at 12:29 -0400, Tom Rini wrote:
> On Thu, Sep 14, 2023 at 03:38:21PM +0530, Mayuresh Chitale wrote:
> 
> > Some platforms as described by EBBR specification may store images
> > in
> > the FIRMWARE directory of the UEFI system partition(ESP). Add
> > support
> > to boot from the EFI system partition if it is enabled for a
> > platform.
> > 
> > Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
> [snip]
> > +config SPL_ESP_BOOT
> > +	bool "Load next stage boot image from the UEFI system
> > partition"
> > +	select SPL_PARTITION_TYPE_GUID
> > +	help
> > +	  When enabled, first try to boot from the UEFI system
> > partition as
> > +	  described in the Ch.4 of the EBBR specification.
> 
> We need to select this by perhaps BOOT_DEFAULTS or BOOTMETH_DISTRO,
> whatever is supposed to signify that yes, this is going to be a
> SystemReady IR (or higher) compliant build.
I am not sure about the SystemReady compliance but I think the config
option can be enabled for those platforms which require it.
> [snip]
> >  int spl_blk_load_image(struct spl_image_info *spl_image,
> >  		       struct spl_boot_device *bootdev,
> >  		       enum uclass_id uclass_id, int devnum, int
> > partnum)
> >  {
> >  	const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
> > -	struct legacy_img_hdr *header;
> [snip]
> > @@ -63,24 +88,26 @@ int spl_blk_load_image(struct spl_image_info
> > *spl_image,
> >  	}
> >  
> >  	blk_show_device(uclass_id, devnum);
> > -	header = spl_get_load_buffer(-sizeof(*header),
> > sizeof(*header));
> > -
> [snip]
> 
> Is this an artifact of Sean's patch series where "header" is no
> longer
> actually used?  As-is this won't compile on top of next (which is
> where
> given Sean's feedback, the series needs to be rebased upon anyhow).
Yes, I will rebase on the latest next without Sean's series and resend
the patch.
> > +	/*
> > +	 * First try to boot from EFI System partition. In case of
> > failure,
> > +	 * fall back to the configured partition.
> > +	 */
> 
> I don't know that this is the right behavior.  If we're configured to
> boot from partition X, we boot from partition X.  If we're configured
> to
> find the ESP and use that, we find and use that.

This is to maintain compatibility and make more attempts to boot. So
the same SPL binary can load images from devices with or without the
ESP.
Tom Rini Sept. 21, 2023, 3:43 p.m. UTC | #3
On Thu, Sep 21, 2023 at 05:03:37PM +0530, mchitale@ventanamicro.com wrote:
> On Thu, 2023-09-14 at 12:29 -0400, Tom Rini wrote:
> > On Thu, Sep 14, 2023 at 03:38:21PM +0530, Mayuresh Chitale wrote:
> > 
> > > Some platforms as described by EBBR specification may store images
> > > in
> > > the FIRMWARE directory of the UEFI system partition(ESP). Add
> > > support
> > > to boot from the EFI system partition if it is enabled for a
> > > platform.
> > > 
> > > Signed-off-by: Mayuresh Chitale <mchitale@ventanamicro.com>
> > [snip]
> > > +config SPL_ESP_BOOT
> > > +	bool "Load next stage boot image from the UEFI system
> > > partition"
> > > +	select SPL_PARTITION_TYPE_GUID
> > > +	help
> > > +	  When enabled, first try to boot from the UEFI system
> > > partition as
> > > +	  described in the Ch.4 of the EBBR specification.
> > 
> > We need to select this by perhaps BOOT_DEFAULTS or BOOTMETH_DISTRO,
> > whatever is supposed to signify that yes, this is going to be a
> > SystemReady IR (or higher) compliant build.
> I am not sure about the SystemReady compliance but I think the config
> option can be enabled for those platforms which require it.

It's "nearly everyone" that needs the option, which is why I'm saying it
needs to be part of the high level options that a board selects to say
they want to be compliant.

> > [snip]
> > >  int spl_blk_load_image(struct spl_image_info *spl_image,
> > >  		       struct spl_boot_device *bootdev,
> > >  		       enum uclass_id uclass_id, int devnum, int
> > > partnum)
> > >  {
> > >  	const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
> > > -	struct legacy_img_hdr *header;
> > [snip]
> > > @@ -63,24 +88,26 @@ int spl_blk_load_image(struct spl_image_info
> > > *spl_image,
> > >  	}
> > >  
> > >  	blk_show_device(uclass_id, devnum);
> > > -	header = spl_get_load_buffer(-sizeof(*header),
> > > sizeof(*header));
> > > -
> > [snip]
> > 
> > Is this an artifact of Sean's patch series where "header" is no
> > longer
> > actually used?  As-is this won't compile on top of next (which is
> > where
> > given Sean's feedback, the series needs to be rebased upon anyhow).
> Yes, I will rebase on the latest next without Sean's series and resend
> the patch.

OK.

> > > +	/*
> > > +	 * First try to boot from EFI System partition. In case of
> > > failure,
> > > +	 * fall back to the configured partition.
> > > +	 */
> > 
> > I don't know that this is the right behavior.  If we're configured to
> > boot from partition X, we boot from partition X.  If we're configured
> > to
> > find the ESP and use that, we find and use that.
> 
> This is to maintain compatibility and make more attempts to boot. So
> the same SPL binary can load images from devices with or without the
> ESP.

Yes, I don't think that's what we want.  It should be an either/or not a
a then b.
diff mbox series

Patch

diff --git a/common/spl/Kconfig b/common/spl/Kconfig
index 76bde18515..92984b19da 100644
--- a/common/spl/Kconfig
+++ b/common/spl/Kconfig
@@ -1241,6 +1241,13 @@  config SPL_SATA_RAW_U_BOOT_SECTOR
 	  Sector on the SATA disk to load U-Boot from, when the SATA disk is being
 	  used in raw mode. Units: SATA disk sectors (1 sector = 512 bytes).
 
+config SPL_ESP_BOOT
+	bool "Load next stage boot image from the UEFI system partition"
+	select SPL_PARTITION_TYPE_GUID
+	help
+	  When enabled, first try to boot from the UEFI system partition as
+	  described in the Ch.4 of the EBBR specification.
+
 config SPL_NVME
 	bool "NVM Express device support"
 	depends on BLK
diff --git a/common/spl/spl_blk_fs.c b/common/spl/spl_blk_fs.c
index 5268daaaff..78d1478426 100644
--- a/common/spl/spl_blk_fs.c
+++ b/common/spl/spl_blk_fs.c
@@ -9,9 +9,12 @@ 
 #include <spl.h>
 #include <image.h>
 #include <fs.h>
+#include <part.h>
 
 struct blk_dev {
 	const char *ifname;
+	int devnum;
+	int partnum;
 	char dev_part_str[8];
 };
 
@@ -39,16 +42,38 @@  static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	return actlen;
 }
 
+static int spl_blk_file_size(struct blk_dev *dev, const char *filename,
+			     loff_t *filesize)
+{
+	int ret;
+
+	snprintf(dev->dev_part_str, sizeof(dev->dev_part_str) - 1, "%x:%x",
+		 dev->devnum, dev->partnum);
+	debug("Loading file %s from %s %s\n", filename, dev->ifname,
+	      dev->dev_part_str);
+	ret = fs_set_blk_dev(dev->ifname, dev->dev_part_str, FS_TYPE_ANY);
+	if (ret) {
+		printf("spl: unable to set blk_dev %s %s. Err - %d\n",
+		       dev->ifname, dev->dev_part_str, ret);
+		return ret;
+	}
+
+	ret = fs_size(filename, filesize);
+	if (ret)
+		printf("spl: unable to get size, file: %s. Err - %d\n",
+		       filename, ret);
+	return ret;
+}
+
 int spl_blk_load_image(struct spl_image_info *spl_image,
 		       struct spl_boot_device *bootdev,
 		       enum uclass_id uclass_id, int devnum, int partnum)
 {
 	const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
-	struct legacy_img_hdr *header;
 	struct blk_desc *blk_desc;
 	loff_t filesize;
 	struct blk_dev dev;
-	int ret;
+	int ret, part;
 	struct spl_load_info load = {
 		.read = spl_fit_read,
 		.bl_len = 1,
@@ -63,24 +88,26 @@  int spl_blk_load_image(struct spl_image_info *spl_image,
 	}
 
 	blk_show_device(uclass_id, devnum);
-	header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
-
 	dev.ifname = blk_get_uclass_name(uclass_id);
-	snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x",
-		 devnum, partnum);
-	ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
-	if (ret) {
-		printf("spl: unable to set blk_dev %s %s. Err - %d\n",
-		       dev.ifname, dev.dev_part_str, ret);
-		return ret;
+	dev.devnum = devnum;
+	/*
+	 * First try to boot from EFI System partition. In case of failure,
+	 * fall back to the configured partition.
+	 */
+	if (IS_ENABLED(CONFIG_SPL_ESP_BOOT)) {
+		part = part_get_esp(blk_desc);
+		if (part) {
+			dev.partnum = part;
+			ret = spl_blk_file_size(&dev, filename, &filesize);
+			if (!ret)
+				goto out;
+		}
 	}
 
-	ret = fs_size(filename, &filesize);
-	if (ret) {
-		printf("spl: unable to get file size: %s. Err - %d\n",
-		       filename, ret);
+	dev.partnum = partnum;
+	ret = spl_blk_file_size(&dev, filename, &filesize);
+	if (ret)
 		return ret;
-	}
-
+out:
 	return spl_load(spl_image, bootdev, &load, filesize, 0);
 }
diff --git a/common/spl/spl_fat.c b/common/spl/spl_fat.c
index 6530bcd5a7..63091f2bd6 100644
--- a/common/spl/spl_fat.c
+++ b/common/spl/spl_fat.c
@@ -54,10 +54,10 @@  static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
 	return actread;
 }
 
-int spl_load_image_fat(struct spl_image_info *spl_image,
-		       struct spl_boot_device *bootdev,
-		       struct blk_desc *block_dev, int partition,
-		       const char *filename)
+int spl_load_image_fat_one(struct spl_image_info *spl_image,
+			   struct spl_boot_device *bootdev,
+			   struct blk_desc *block_dev, int partition,
+			   const char *filename)
 {
 	int err;
 	loff_t size;
@@ -96,6 +96,32 @@  end:
 	return err;
 }
 
+int spl_load_image_fat(struct spl_image_info *spl_image,
+		       struct spl_boot_device *bootdev,
+		       struct blk_desc *block_dev, int partition,
+		       const char *filename)
+{
+	int err, part;
+
+	/*
+	 * First try to boot from EFI System partition. In case of failure,
+	 * fall back to the configured partition.
+	 */
+	if (IS_ENABLED(CONFIG_SPL_ESP_BOOT)) {
+		part = part_get_esp(block_dev);
+		if (part) {
+			err = spl_load_image_fat_one(spl_image, bootdev,
+						     block_dev, part,
+						     filename);
+			if (!err)
+				return err;
+		}
+	}
+
+	return spl_load_image_fat_one(spl_image, bootdev, block_dev,
+				      partition, filename);
+}
+
 #if CONFIG_IS_ENABLED(OS_BOOT)
 int spl_load_image_fat_os(struct spl_image_info *spl_image,
 			  struct spl_boot_device *bootdev,