diff mbox series

[v2,4/6] efi_loader: support boot from URI device path

Message ID 20230901102542.609239-5-masahisa.kojima@linaro.org
State New
Headers show
Series Add EFI HTTP boot support | expand

Commit Message

Masahisa Kojima Sept. 1, 2023, 10:25 a.m. UTC
This supports to boot from the URI device path.
When user selects the URI device path, bootmgr downloads
the file using wget into the address specified by loadaddr
env variable.
If the file is .iso or .img file, mount the image with blkmap
then try to boot with the default file(e.g. EFI/BOOT/BOOTAA64.EFI).
If the file is .efi file, load and start the downloaded file.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
 lib/efi_loader/efi_bootmgr.c | 197 +++++++++++++++++++++++++++++++++++
 1 file changed, 197 insertions(+)

Comments

Ilias Apalodimas Sept. 14, 2023, 1:56 p.m. UTC | #1
Kojima-san

On Fri, Sep 01, 2023 at 07:25:40PM +0900, Masahisa Kojima wrote:
> This supports to boot from the URI device path.
> When user selects the URI device path, bootmgr downloads
> the file using wget into the address specified by loadaddr
> env variable.
> If the file is .iso or .img file, mount the image with blkmap
> then try to boot with the default file(e.g. EFI/BOOT/BOOTAA64.EFI).
> If the file is .efi file, load and start the downloaded file.
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> ---
>  lib/efi_loader/efi_bootmgr.c | 197 +++++++++++++++++++++++++++++++++++
>  1 file changed, 197 insertions(+)
>
> diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> index a40762c74c..0e8d2ca9d1 100644
> --- a/lib/efi_loader/efi_bootmgr.c
> +++ b/lib/efi_loader/efi_bootmgr.c
> @@ -7,10 +7,14 @@
>
>  #define LOG_CATEGORY LOGC_EFI
>
> +#include <blk.h>
> +#include <blkmap.h>
>  #include <common.h>
>  #include <charset.h>
> +#include <dm.h>
>  #include <log.h>
>  #include <malloc.h>
> +#include <net.h>
>  #include <efi_default_filename.h>
>  #include <efi_loader.h>
>  #include <efi_variable.h>
> @@ -168,6 +172,193 @@ out:
>  	return ret;
>  }
>
> +#if (IS_ENABLED(CONFIG_BLKMAP) && IS_ENABLED(CONFIG_CMD_WGET) && IS_ENABLED(CONFIG_CMD_DNS))
> +/**
> + * mount_image() - mount the image with blkmap
> + *
> + * @lo_label	u16 label string of load option
> + * @image_addr:	image address
> + * @image_size	image size
> + * Return:	pointer to the UCLASS_BLK udevice, NULL if failed
> + */
> +static struct udevice *mount_image(u16 *lo_label, ulong image_addr, int image_size)
> +{
> +	int err;
> +	struct blkmap *bm;
> +	struct udevice *bm_dev;
> +	char *label = NULL, *p;
> +
> +	label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
> +	if (!label)
> +		return NULL;
> +
> +	p = label;
> +	utf16_utf8_strcpy(&p, lo_label);
> +	err = blkmap_create_ramdisk(label, image_addr, image_size, &bm_dev);
> +	if (err) {
> +		efi_free_pool(label);
> +		return NULL;
> +	}
> +	bm = dev_get_plat(bm_dev);
> +
> +	efi_free_pool(label);
> +
> +	return bm->blk;
> +}
> +
> +/**
> + * try_load_default_file() - try to load the default file
> + *
> + * Search the device having EFI_SIMPLE_FILE_SYSTEM_PROTOCOL,
> + * then try to load with the default boot file(e.g. EFI/BOOT/BOOTAA64.EFI).
> + *
> + * @dev			pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
> + * @image_handle:	pointer to handle for newly installed image
> + * Return:		status code
> + */
> +static efi_status_t try_load_default_file(struct udevice *dev,
> +					  efi_handle_t *image_handle)
> +{
> +	efi_status_t ret;
> +	efi_handle_t bm_handle;
> +	struct efi_handler *handler;
> +	struct efi_device_path *file_path;
> +	struct efi_device_path *device_path;
> +
> +	if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&bm_handle)) {
> +		log_warning("DM_TAG_EFI not found\n");
> +		return EFI_INVALID_PARAMETER;
> +	}
> +
> +	ret = efi_search_protocol(bm_handle,
> +				  &efi_simple_file_system_protocol_guid, &handler);
> +	if (ret != EFI_SUCCESS)
> +		return ret;
> +
> +	ret = efi_search_protocol(bm_handle, &efi_guid_device_path, &handler);
> +	if (ret != EFI_SUCCESS)
> +		return ret;
> +
> +	ret = efi_protocol_open(handler, (void **)&device_path, efi_root, NULL,
> +				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> +	if (ret != EFI_SUCCESS)
> +		return ret;
> +
> +	file_path = expand_media_path(device_path);
> +	ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0,
> +				      image_handle));
> +
> +	efi_free_pool(file_path);
> +
> +	return ret;
> +}

We need to decide what we want here.  There were recently some patches from
Raymond [0] which piggybacked on your earlier eficonfig work.  I think the
last patch of this series hasn't been merged due to a test failing, but we
should fix it.
That patch has a different approach.  Everytime a disk appears it will add
a boot option if the default filepath is found and that's how we fixed the
behaviour of efibootmgr to adhere to the EFI spec.  This patch is doing the
opposite, trying to detect the BOOTAA64.EFI etc on the fly.  I think I
prefer the approach you have here, but we should end up with one solution
to solve both.

> +
> +/**
> + * load_default_file_from_blk_dev() - load the default file
> + *
> + * @blk		pointer to the UCLASS_BLK udevice
> + * @handle:	pointer to handle for newly installed image
> + * Return:	status code
> + */
> +static efi_status_t load_default_file_from_blk_dev(struct udevice *blk,
> +						   efi_handle_t *handle)
> +{
> +	efi_status_t ret;
> +	struct udevice *partition;
> +
> +	/* image that has no partition table but a file system */
> +	ret = try_load_default_file(blk, handle);
> +	if (ret == EFI_SUCCESS)
> +		return ret;
> +
> +	/* try the partitions */
> +	device_foreach_child(partition, blk) {
> +		enum uclass_id id;
> +
> +		id = device_get_uclass_id(partition);
> +		if (id != UCLASS_PARTITION)
> +			continue;
> +
> +		ret = try_load_default_file(partition, handle);
> +		if (ret == EFI_SUCCESS)
> +			return ret;
> +	}
> +
> +	return EFI_NOT_FOUND;
> +}
> +
> +/**
> + * try_load_from_uri_path() - Handle the URI device path
> + *
> + * @uridp:	uri device path
> + * @lo_label	label of load option
> + * @handle:	pointer to handle for newly installed image
> + * Return:	status code
> + */
> +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
> +					   u16 *lo_label,
> +					   efi_handle_t *handle)
> +{
> +	char *s;
> +	int uri_len;
> +	int image_size;
> +	efi_status_t ret;
> +	ulong image_addr;
> +
> +	s = env_get("loadaddr");
> +	if (!s) {
> +		log_err("Error: loadaddr is not set\n");
> +		return EFI_INVALID_PARAMETER;
> +	}
> +	image_addr = hextoul(s, NULL);
> +	image_size = wget_with_dns(image_addr, uridp->uri);
> +	if (image_size < 0)
> +		return EFI_INVALID_PARAMETER;
> +
> +	/*
> +	 * If the file extension is ".iso" or ".img", mount it and try to load
> +	 * the default file.

Don't we have a better way to validate isos and efi apps instead of
the extension?  The efi is checked against a valid PE/COFF image so I guess
we'll really on the mount to fail for isos?

> +	 * If the file is ".efi" and PE-COFF image, load the downloaded file.
> +	 */
> +	uri_len = strlen(uridp->uri); /* todo: directly use uridp->uri */
> +	if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
> +	    !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
> +		struct udevice *blk;
> +
> +		blk = mount_image(lo_label, image_addr, image_size);
> +		if (!blk)
> +			return EFI_INVALID_PARAMETER;
> +
> +		ret = load_default_file_from_blk_dev(blk, handle);
> +	} else if (!strncmp(&uridp->uri[uri_len - 4], ".efi", 4)) {
> +		efi_handle_t mem_handle = NULL;
> +		struct efi_device_path *file_path = NULL;
> +
> +		ret = efi_check_pe((void *)image_addr, image_size, NULL);
> +		if (ret != EFI_SUCCESS) {
> +			log_err("Error: downloaded image is not a PE-COFF image\n");
> +			return EFI_INVALID_PARAMETER;
> +		}
> +
> +		file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
> +					    (uintptr_t)image_addr, image_size);
> +		ret = efi_install_multiple_protocol_interfaces(
> +			&mem_handle, &efi_guid_device_path, file_path, NULL);
> +		if (ret != EFI_SUCCESS)
> +			return EFI_INVALID_PARAMETER;
> +
> +		ret = EFI_CALL(efi_load_image(false, efi_root, file_path,
> +					      (void *)image_addr, image_size,

[0] https://lore.kernel.org/u-boot/20230619212303.128288-1-raymond.mao@linaro.org/

Thanks
/Ilias
Heinrich Schuchardt Sept. 14, 2023, 2:55 p.m. UTC | #2
On 01.09.23 12:25, Masahisa Kojima wrote:
> This supports to boot from the URI device path.
> When user selects the URI device path, bootmgr downloads
> the file using wget into the address specified by loadaddr
> env variable.
> If the file is .iso or .img file, mount the image with blkmap
> then try to boot with the default file(e.g. EFI/BOOT/BOOTAA64.EFI).
> If the file is .efi file, load and start the downloaded file.
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> ---
>   lib/efi_loader/efi_bootmgr.c | 197 +++++++++++++++++++++++++++++++++++
>   1 file changed, 197 insertions(+)
>
> diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> index a40762c74c..0e8d2ca9d1 100644
> --- a/lib/efi_loader/efi_bootmgr.c
> +++ b/lib/efi_loader/efi_bootmgr.c
> @@ -7,10 +7,14 @@
>
>   #define LOG_CATEGORY LOGC_EFI
>
> +#include <blk.h>
> +#include <blkmap.h>
>   #include <common.h>
>   #include <charset.h>
> +#include <dm.h>
>   #include <log.h>
>   #include <malloc.h>
> +#include <net.h>
>   #include <efi_default_filename.h>
>   #include <efi_loader.h>
>   #include <efi_variable.h>
> @@ -168,6 +172,193 @@ out:
>   	return ret;
>   }
>
> +#if (IS_ENABLED(CONFIG_BLKMAP) && IS_ENABLED(CONFIG_CMD_WGET) && IS_ENABLED(CONFIG_CMD_DNS))
> +/**
> + * mount_image() - mount the image with blkmap
> + *
> + * @lo_label	u16 label string of load option
> + * @image_addr:	image address
> + * @image_size	image size
> + * Return:	pointer to the UCLASS_BLK udevice, NULL if failed
> + */
> +static struct udevice *mount_image(u16 *lo_label, ulong image_addr, int image_size)
> +{
> +	int err;
> +	struct blkmap *bm;
> +	struct udevice *bm_dev;
> +	char *label = NULL, *p;
> +
> +	label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
> +	if (!label)
> +		return NULL;
> +
> +	p = label;
> +	utf16_utf8_strcpy(&p, lo_label);
> +	err = blkmap_create_ramdisk(label, image_addr, image_size, &bm_dev);
> +	if (err) {
> +		efi_free_pool(label);
> +		return NULL;
> +	}
> +	bm = dev_get_plat(bm_dev);
> +
> +	efi_free_pool(label);
> +
> +	return bm->blk;
> +}
> +
> +/**
> + * try_load_default_file() - try to load the default file
> + *
> + * Search the device having EFI_SIMPLE_FILE_SYSTEM_PROTOCOL,
> + * then try to load with the default boot file(e.g. EFI/BOOT/BOOTAA64.EFI).
> + *
> + * @dev			pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
> + * @image_handle:	pointer to handle for newly installed image
> + * Return:		status code
> + */
> +static efi_status_t try_load_default_file(struct udevice *dev,
> +					  efi_handle_t *image_handle)
> +{
> +	efi_status_t ret;
> +	efi_handle_t bm_handle;
> +	struct efi_handler *handler;
> +	struct efi_device_path *file_path;
> +	struct efi_device_path *device_path;
> +
> +	if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&bm_handle)) {
> +		log_warning("DM_TAG_EFI not found\n");
> +		return EFI_INVALID_PARAMETER;
> +	}
> +
> +	ret = efi_search_protocol(bm_handle,
> +				  &efi_simple_file_system_protocol_guid, &handler);
> +	if (ret != EFI_SUCCESS)
> +		return ret;
> +
> +	ret = efi_search_protocol(bm_handle, &efi_guid_device_path, &handler);
> +	if (ret != EFI_SUCCESS)
> +		return ret;
> +
> +	ret = efi_protocol_open(handler, (void **)&device_path, efi_root, NULL,
> +				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> +	if (ret != EFI_SUCCESS)
> +		return ret;
> +
> +	file_path = expand_media_path(device_path);
> +	ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0,
> +				      image_handle));
> +
> +	efi_free_pool(file_path);
> +
> +	return ret;
> +}
> +
> +/**
> + * load_default_file_from_blk_dev() - load the default file
> + *
> + * @blk		pointer to the UCLASS_BLK udevice
> + * @handle:	pointer to handle for newly installed image
> + * Return:	status code
> + */
> +static efi_status_t load_default_file_from_blk_dev(struct udevice *blk,
> +						   efi_handle_t *handle)
> +{
> +	efi_status_t ret;
> +	struct udevice *partition;
> +
> +	/* image that has no partition table but a file system */
> +	ret = try_load_default_file(blk, handle);
> +	if (ret == EFI_SUCCESS)
> +		return ret;
> +
> +	/* try the partitions */
> +	device_foreach_child(partition, blk) {
> +		enum uclass_id id;
> +
> +		id = device_get_uclass_id(partition);
> +		if (id != UCLASS_PARTITION)
> +			continue;
> +
> +		ret = try_load_default_file(partition, handle);
> +		if (ret == EFI_SUCCESS)
> +			return ret;
> +	}
> +
> +	return EFI_NOT_FOUND;
> +}
> +
> +/**
> + * try_load_from_uri_path() - Handle the URI device path
> + *
> + * @uridp:	uri device path
> + * @lo_label	label of load option
> + * @handle:	pointer to handle for newly installed image
> + * Return:	status code
> + */
> +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
> +					   u16 *lo_label,
> +					   efi_handle_t *handle)
> +{
> +	char *s;
> +	int uri_len;
> +	int image_size;
> +	efi_status_t ret;
> +	ulong image_addr;
> +
> +	s = env_get("loadaddr");
> +	if (!s) {
> +		log_err("Error: loadaddr is not set\n");
> +		return EFI_INVALID_PARAMETER;
> +	}
> +	image_addr = hextoul(s, NULL);
> +	image_size = wget_with_dns(image_addr, uridp->uri);
> +	if (image_size < 0)
> +		return EFI_INVALID_PARAMETER;
> +
> +	/*
> +	 * If the file extension is ".iso" or ".img", mount it and try to load
> +	 * the default file.
> +	 * If the file is ".efi" and PE-COFF image, load the downloaded file.
> +	 */

There is no requirement that EFI binaries have an .efi extension.
Does EDK II require it?

Best regards

Heinrich


> +	uri_len = strlen(uridp->uri); /* todo: directly use uridp->uri */
> +	if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
> +	    !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
> +		struct udevice *blk;
> +
> +		blk = mount_image(lo_label, image_addr, image_size);
> +		if (!blk)
> +			return EFI_INVALID_PARAMETER;
> +
> +		ret = load_default_file_from_blk_dev(blk, handle);
> +	} else if (!strncmp(&uridp->uri[uri_len - 4], ".efi", 4)) {
> +		efi_handle_t mem_handle = NULL;
> +		struct efi_device_path *file_path = NULL;
> +
> +		ret = efi_check_pe((void *)image_addr, image_size, NULL);
> +		if (ret != EFI_SUCCESS) {
> +			log_err("Error: downloaded image is not a PE-COFF image\n");
> +			return EFI_INVALID_PARAMETER;
> +		}
> +
> +		file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
> +					    (uintptr_t)image_addr, image_size);
> +		ret = efi_install_multiple_protocol_interfaces(
> +			&mem_handle, &efi_guid_device_path, file_path, NULL);
> +		if (ret != EFI_SUCCESS)
> +			return EFI_INVALID_PARAMETER;
> +
> +		ret = EFI_CALL(efi_load_image(false, efi_root, file_path,
> +					      (void *)image_addr, image_size,
> +					      handle));
> +	} else {
> +		log_err("Error: file type is not supported\n");
> +		return EFI_INVALID_PARAMETER;
> +	}
> +
> +	return ret;
> +}
> +#endif
> +
>   /**
>    * try_load_entry() - try to load image for boot option
>    *
> @@ -211,6 +402,12 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
>   		if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
>   			/* file_path doesn't contain a device path */
>   			ret = try_load_from_short_path(lo.file_path, handle);
> +#if (IS_ENABLED(CONFIG_BLKMAP) && IS_ENABLED(CONFIG_CMD_WGET) && IS_ENABLED(CONFIG_CMD_DNS))
> +		} else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
> +			ret = try_load_from_uri_path(
> +				(struct efi_device_path_uri *)lo.file_path,
> +				lo.label, handle);
> +#endif
>   		} else {
>   			file_path = expand_media_path(lo.file_path);
>   			ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
Masahisa Kojima Sept. 15, 2023, 4:15 a.m. UTC | #3
Hi Ilias,

On Thu, 14 Sept 2023 at 22:56, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Kojima-san
>
> On Fri, Sep 01, 2023 at 07:25:40PM +0900, Masahisa Kojima wrote:
> > This supports to boot from the URI device path.
> > When user selects the URI device path, bootmgr downloads
> > the file using wget into the address specified by loadaddr
> > env variable.
> > If the file is .iso or .img file, mount the image with blkmap
> > then try to boot with the default file(e.g. EFI/BOOT/BOOTAA64.EFI).
> > If the file is .efi file, load and start the downloaded file.
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> > ---
> >  lib/efi_loader/efi_bootmgr.c | 197 +++++++++++++++++++++++++++++++++++
> >  1 file changed, 197 insertions(+)
> >
> > diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> > index a40762c74c..0e8d2ca9d1 100644
> > --- a/lib/efi_loader/efi_bootmgr.c
> > +++ b/lib/efi_loader/efi_bootmgr.c
> > @@ -7,10 +7,14 @@
> >
> >  #define LOG_CATEGORY LOGC_EFI
> >
> > +#include <blk.h>
> > +#include <blkmap.h>
> >  #include <common.h>
> >  #include <charset.h>
> > +#include <dm.h>
> >  #include <log.h>
> >  #include <malloc.h>
> > +#include <net.h>
> >  #include <efi_default_filename.h>
> >  #include <efi_loader.h>
> >  #include <efi_variable.h>
> > @@ -168,6 +172,193 @@ out:
> >       return ret;
> >  }
> >
> > +#if (IS_ENABLED(CONFIG_BLKMAP) && IS_ENABLED(CONFIG_CMD_WGET) && IS_ENABLED(CONFIG_CMD_DNS))
> > +/**
> > + * mount_image() - mount the image with blkmap
> > + *
> > + * @lo_label u16 label string of load option
> > + * @image_addr:      image address
> > + * @image_size       image size
> > + * Return:   pointer to the UCLASS_BLK udevice, NULL if failed
> > + */
> > +static struct udevice *mount_image(u16 *lo_label, ulong image_addr, int image_size)
> > +{
> > +     int err;
> > +     struct blkmap *bm;
> > +     struct udevice *bm_dev;
> > +     char *label = NULL, *p;
> > +
> > +     label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
> > +     if (!label)
> > +             return NULL;
> > +
> > +     p = label;
> > +     utf16_utf8_strcpy(&p, lo_label);
> > +     err = blkmap_create_ramdisk(label, image_addr, image_size, &bm_dev);
> > +     if (err) {
> > +             efi_free_pool(label);
> > +             return NULL;
> > +     }
> > +     bm = dev_get_plat(bm_dev);
> > +
> > +     efi_free_pool(label);
> > +
> > +     return bm->blk;
> > +}
> > +
> > +/**
> > + * try_load_default_file() - try to load the default file
> > + *
> > + * Search the device having EFI_SIMPLE_FILE_SYSTEM_PROTOCOL,
> > + * then try to load with the default boot file(e.g. EFI/BOOT/BOOTAA64.EFI).
> > + *
> > + * @dev                      pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
> > + * @image_handle:    pointer to handle for newly installed image
> > + * Return:           status code
> > + */
> > +static efi_status_t try_load_default_file(struct udevice *dev,
> > +                                       efi_handle_t *image_handle)
> > +{
> > +     efi_status_t ret;
> > +     efi_handle_t bm_handle;
> > +     struct efi_handler *handler;
> > +     struct efi_device_path *file_path;
> > +     struct efi_device_path *device_path;
> > +
> > +     if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&bm_handle)) {
> > +             log_warning("DM_TAG_EFI not found\n");
> > +             return EFI_INVALID_PARAMETER;
> > +     }
> > +
> > +     ret = efi_search_protocol(bm_handle,
> > +                               &efi_simple_file_system_protocol_guid, &handler);
> > +     if (ret != EFI_SUCCESS)
> > +             return ret;
> > +
> > +     ret = efi_search_protocol(bm_handle, &efi_guid_device_path, &handler);
> > +     if (ret != EFI_SUCCESS)
> > +             return ret;
> > +
> > +     ret = efi_protocol_open(handler, (void **)&device_path, efi_root, NULL,
> > +                             EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> > +     if (ret != EFI_SUCCESS)
> > +             return ret;
> > +
> > +     file_path = expand_media_path(device_path);
> > +     ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0,
> > +                                   image_handle));
> > +
> > +     efi_free_pool(file_path);
> > +
> > +     return ret;
> > +}
>
> We need to decide what we want here.  There were recently some patches from
> Raymond [0] which piggybacked on your earlier eficonfig work.  I think the
> last patch of this series hasn't been merged due to a test failing, but we
> should fix it.
> That patch has a different approach.  Everytime a disk appears it will add
> a boot option if the default filepath is found and that's how we fixed the

efi_bootmgr_update_media_device_boot_option() automatically adds the
boot option if the device handle installed EFI_SIMPLE_FILE_SYSTEM_PROTOCOL,
but it does not check whether the default file path is found or not.
When the user selects the automatically created boot option,
expand_media_path()
is called and efibootmgr tries to boot with the default file.

> behaviour of efibootmgr to adhere to the EFI spec.  This patch is doing the
> opposite, trying to detect the BOOTAA64.EFI etc on the fly.  I think I
> prefer the approach you have here, but we should end up with one solution
> to solve both.

This HTTP Boot case is slightly different from the case where the user selects
the automatically added boot option.
In this case, user selects the URI device path boot option. The
efibootmgr downloads the
file, mount the image, and try to boot with the default file name on the fly.
When the patch[0] is merged, it is possible to boot the downloaded iso
image from the
automatically added "blkmap" boot option, but I think efibootmgr needs
to boot with
the URI device path user selected that this series does, not boot
from the "blkmap" boot option.

>
> > +
> > +/**
> > + * load_default_file_from_blk_dev() - load the default file
> > + *
> > + * @blk              pointer to the UCLASS_BLK udevice
> > + * @handle:  pointer to handle for newly installed image
> > + * Return:   status code
> > + */
> > +static efi_status_t load_default_file_from_blk_dev(struct udevice *blk,
> > +                                                efi_handle_t *handle)
> > +{
> > +     efi_status_t ret;
> > +     struct udevice *partition;
> > +
> > +     /* image that has no partition table but a file system */
> > +     ret = try_load_default_file(blk, handle);
> > +     if (ret == EFI_SUCCESS)
> > +             return ret;
> > +
> > +     /* try the partitions */
> > +     device_foreach_child(partition, blk) {
> > +             enum uclass_id id;
> > +
> > +             id = device_get_uclass_id(partition);
> > +             if (id != UCLASS_PARTITION)
> > +                     continue;
> > +
> > +             ret = try_load_default_file(partition, handle);
> > +             if (ret == EFI_SUCCESS)
> > +                     return ret;
> > +     }
> > +
> > +     return EFI_NOT_FOUND;
> > +}
> > +
> > +/**
> > + * try_load_from_uri_path() - Handle the URI device path
> > + *
> > + * @uridp:   uri device path
> > + * @lo_label label of load option
> > + * @handle:  pointer to handle for newly installed image
> > + * Return:   status code
> > + */
> > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
> > +                                        u16 *lo_label,
> > +                                        efi_handle_t *handle)
> > +{
> > +     char *s;
> > +     int uri_len;
> > +     int image_size;
> > +     efi_status_t ret;
> > +     ulong image_addr;
> > +
> > +     s = env_get("loadaddr");
> > +     if (!s) {
> > +             log_err("Error: loadaddr is not set\n");
> > +             return EFI_INVALID_PARAMETER;
> > +     }
> > +     image_addr = hextoul(s, NULL);
> > +     image_size = wget_with_dns(image_addr, uridp->uri);
> > +     if (image_size < 0)
> > +             return EFI_INVALID_PARAMETER;
> > +
> > +     /*
> > +      * If the file extension is ".iso" or ".img", mount it and try to load
> > +      * the default file.
>
> Don't we have a better way to validate isos and efi apps instead of
> the extension?  The efi is checked against a valid PE/COFF image so I guess
> we'll really on the mount to fail for isos?

EDK2 reference implementation checks the file type with the following priority.
 1) "Content-Type" header in HTTP response message
 2) Filename Extensions
Documentation is here[1].

Since "Content-Type" is not available in the current U-Boot wget, file extension
is used to identify ISO images.

[1] https://github.com/tianocore/tianocore.github.io/wiki/HTTP-Boot#ram-disk-boot-from-http

Thanks,
Masahisa Kojima


>
> > +      * If the file is ".efi" and PE-COFF image, load the downloaded file.
> > +      */
> > +     uri_len = strlen(uridp->uri); /* todo: directly use uridp->uri */
> > +     if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
> > +         !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
> > +             struct udevice *blk;
> > +
> > +             blk = mount_image(lo_label, image_addr, image_size);
> > +             if (!blk)
> > +                     return EFI_INVALID_PARAMETER;
> > +
> > +             ret = load_default_file_from_blk_dev(blk, handle);
> > +     } else if (!strncmp(&uridp->uri[uri_len - 4], ".efi", 4)) {
> > +             efi_handle_t mem_handle = NULL;
> > +             struct efi_device_path *file_path = NULL;
> > +
> > +             ret = efi_check_pe((void *)image_addr, image_size, NULL);
> > +             if (ret != EFI_SUCCESS) {
> > +                     log_err("Error: downloaded image is not a PE-COFF image\n");
> > +                     return EFI_INVALID_PARAMETER;
> > +             }
> > +
> > +             file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
> > +                                         (uintptr_t)image_addr, image_size);
> > +             ret = efi_install_multiple_protocol_interfaces(
> > +                     &mem_handle, &efi_guid_device_path, file_path, NULL);
> > +             if (ret != EFI_SUCCESS)
> > +                     return EFI_INVALID_PARAMETER;
> > +
> > +             ret = EFI_CALL(efi_load_image(false, efi_root, file_path,
> > +                                           (void *)image_addr, image_size,
>
> [0] https://lore.kernel.org/u-boot/20230619212303.128288-1-raymond.mao@linaro.org/
>
> Thanks
> /Ilias
Masahisa Kojima Sept. 15, 2023, 4:17 a.m. UTC | #4
Hi Heinrich,

On Thu, 14 Sept 2023 at 23:55, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 01.09.23 12:25, Masahisa Kojima wrote:
> > This supports to boot from the URI device path.
> > When user selects the URI device path, bootmgr downloads
> > the file using wget into the address specified by loadaddr
> > env variable.
> > If the file is .iso or .img file, mount the image with blkmap
> > then try to boot with the default file(e.g. EFI/BOOT/BOOTAA64.EFI).
> > If the file is .efi file, load and start the downloaded file.
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> > ---
> >   lib/efi_loader/efi_bootmgr.c | 197 +++++++++++++++++++++++++++++++++++
> >   1 file changed, 197 insertions(+)
> >
> > diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> > index a40762c74c..0e8d2ca9d1 100644
> > --- a/lib/efi_loader/efi_bootmgr.c
> > +++ b/lib/efi_loader/efi_bootmgr.c
> > @@ -7,10 +7,14 @@
> >
> >   #define LOG_CATEGORY LOGC_EFI
> >
> > +#include <blk.h>
> > +#include <blkmap.h>
> >   #include <common.h>
> >   #include <charset.h>
> > +#include <dm.h>
> >   #include <log.h>
> >   #include <malloc.h>
> > +#include <net.h>
> >   #include <efi_default_filename.h>
> >   #include <efi_loader.h>
> >   #include <efi_variable.h>
> > @@ -168,6 +172,193 @@ out:
> >       return ret;
> >   }
> >
> > +#if (IS_ENABLED(CONFIG_BLKMAP) && IS_ENABLED(CONFIG_CMD_WGET) && IS_ENABLED(CONFIG_CMD_DNS))
> > +/**
> > + * mount_image() - mount the image with blkmap
> > + *
> > + * @lo_label u16 label string of load option
> > + * @image_addr:      image address
> > + * @image_size       image size
> > + * Return:   pointer to the UCLASS_BLK udevice, NULL if failed
> > + */
> > +static struct udevice *mount_image(u16 *lo_label, ulong image_addr, int image_size)
> > +{
> > +     int err;
> > +     struct blkmap *bm;
> > +     struct udevice *bm_dev;
> > +     char *label = NULL, *p;
> > +
> > +     label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
> > +     if (!label)
> > +             return NULL;
> > +
> > +     p = label;
> > +     utf16_utf8_strcpy(&p, lo_label);
> > +     err = blkmap_create_ramdisk(label, image_addr, image_size, &bm_dev);
> > +     if (err) {
> > +             efi_free_pool(label);
> > +             return NULL;
> > +     }
> > +     bm = dev_get_plat(bm_dev);
> > +
> > +     efi_free_pool(label);
> > +
> > +     return bm->blk;
> > +}
> > +
> > +/**
> > + * try_load_default_file() - try to load the default file
> > + *
> > + * Search the device having EFI_SIMPLE_FILE_SYSTEM_PROTOCOL,
> > + * then try to load with the default boot file(e.g. EFI/BOOT/BOOTAA64.EFI).
> > + *
> > + * @dev                      pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
> > + * @image_handle:    pointer to handle for newly installed image
> > + * Return:           status code
> > + */
> > +static efi_status_t try_load_default_file(struct udevice *dev,
> > +                                       efi_handle_t *image_handle)
> > +{
> > +     efi_status_t ret;
> > +     efi_handle_t bm_handle;
> > +     struct efi_handler *handler;
> > +     struct efi_device_path *file_path;
> > +     struct efi_device_path *device_path;
> > +
> > +     if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&bm_handle)) {
> > +             log_warning("DM_TAG_EFI not found\n");
> > +             return EFI_INVALID_PARAMETER;
> > +     }
> > +
> > +     ret = efi_search_protocol(bm_handle,
> > +                               &efi_simple_file_system_protocol_guid, &handler);
> > +     if (ret != EFI_SUCCESS)
> > +             return ret;
> > +
> > +     ret = efi_search_protocol(bm_handle, &efi_guid_device_path, &handler);
> > +     if (ret != EFI_SUCCESS)
> > +             return ret;
> > +
> > +     ret = efi_protocol_open(handler, (void **)&device_path, efi_root, NULL,
> > +                             EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> > +     if (ret != EFI_SUCCESS)
> > +             return ret;
> > +
> > +     file_path = expand_media_path(device_path);
> > +     ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0,
> > +                                   image_handle));
> > +
> > +     efi_free_pool(file_path);
> > +
> > +     return ret;
> > +}
> > +
> > +/**
> > + * load_default_file_from_blk_dev() - load the default file
> > + *
> > + * @blk              pointer to the UCLASS_BLK udevice
> > + * @handle:  pointer to handle for newly installed image
> > + * Return:   status code
> > + */
> > +static efi_status_t load_default_file_from_blk_dev(struct udevice *blk,
> > +                                                efi_handle_t *handle)
> > +{
> > +     efi_status_t ret;
> > +     struct udevice *partition;
> > +
> > +     /* image that has no partition table but a file system */
> > +     ret = try_load_default_file(blk, handle);
> > +     if (ret == EFI_SUCCESS)
> > +             return ret;
> > +
> > +     /* try the partitions */
> > +     device_foreach_child(partition, blk) {
> > +             enum uclass_id id;
> > +
> > +             id = device_get_uclass_id(partition);
> > +             if (id != UCLASS_PARTITION)
> > +                     continue;
> > +
> > +             ret = try_load_default_file(partition, handle);
> > +             if (ret == EFI_SUCCESS)
> > +                     return ret;
> > +     }
> > +
> > +     return EFI_NOT_FOUND;
> > +}
> > +
> > +/**
> > + * try_load_from_uri_path() - Handle the URI device path
> > + *
> > + * @uridp:   uri device path
> > + * @lo_label label of load option
> > + * @handle:  pointer to handle for newly installed image
> > + * Return:   status code
> > + */
> > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
> > +                                        u16 *lo_label,
> > +                                        efi_handle_t *handle)
> > +{
> > +     char *s;
> > +     int uri_len;
> > +     int image_size;
> > +     efi_status_t ret;
> > +     ulong image_addr;
> > +
> > +     s = env_get("loadaddr");
> > +     if (!s) {
> > +             log_err("Error: loadaddr is not set\n");
> > +             return EFI_INVALID_PARAMETER;
> > +     }
> > +     image_addr = hextoul(s, NULL);
> > +     image_size = wget_with_dns(image_addr, uridp->uri);
> > +     if (image_size < 0)
> > +             return EFI_INVALID_PARAMETER;
> > +
> > +     /*
> > +      * If the file extension is ".iso" or ".img", mount it and try to load
> > +      * the default file.
> > +      * If the file is ".efi" and PE-COFF image, load the downloaded file.
> > +      */
>
> There is no requirement that EFI binaries have an .efi extension.
> Does EDK II require it?

No, sorry.
".efi" extension check should be removed.

Thanks,
Masahisa Kojima

>
> Best regards
>
> Heinrich
>
>
> > +     uri_len = strlen(uridp->uri); /* todo: directly use uridp->uri */
> > +     if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
> > +         !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
> > +             struct udevice *blk;
> > +
> > +             blk = mount_image(lo_label, image_addr, image_size);
> > +             if (!blk)
> > +                     return EFI_INVALID_PARAMETER;
> > +
> > +             ret = load_default_file_from_blk_dev(blk, handle);
> > +     } else if (!strncmp(&uridp->uri[uri_len - 4], ".efi", 4)) {
> > +             efi_handle_t mem_handle = NULL;
> > +             struct efi_device_path *file_path = NULL;
> > +
> > +             ret = efi_check_pe((void *)image_addr, image_size, NULL);
> > +             if (ret != EFI_SUCCESS) {
> > +                     log_err("Error: downloaded image is not a PE-COFF image\n");
> > +                     return EFI_INVALID_PARAMETER;
> > +             }
> > +
> > +             file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
> > +                                         (uintptr_t)image_addr, image_size);
> > +             ret = efi_install_multiple_protocol_interfaces(
> > +                     &mem_handle, &efi_guid_device_path, file_path, NULL);
> > +             if (ret != EFI_SUCCESS)
> > +                     return EFI_INVALID_PARAMETER;
> > +
> > +             ret = EFI_CALL(efi_load_image(false, efi_root, file_path,
> > +                                           (void *)image_addr, image_size,
> > +                                           handle));
> > +     } else {
> > +             log_err("Error: file type is not supported\n");
> > +             return EFI_INVALID_PARAMETER;
> > +     }
> > +
> > +     return ret;
> > +}
> > +#endif
> > +
> >   /**
> >    * try_load_entry() - try to load image for boot option
> >    *
> > @@ -211,6 +402,12 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
> >               if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
> >                       /* file_path doesn't contain a device path */
> >                       ret = try_load_from_short_path(lo.file_path, handle);
> > +#if (IS_ENABLED(CONFIG_BLKMAP) && IS_ENABLED(CONFIG_CMD_WGET) && IS_ENABLED(CONFIG_CMD_DNS))
> > +             } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
> > +                     ret = try_load_from_uri_path(
> > +                             (struct efi_device_path_uri *)lo.file_path,
> > +                             lo.label, handle);
> > +#endif
> >               } else {
> >                       file_path = expand_media_path(lo.file_path);
> >                       ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
>
Ilias Apalodimas Sept. 15, 2023, 6:31 a.m. UTC | #5
Hi Kojima-san, 

> > > +                                       efi_handle_t *image_handle)
> > > +{
> > > +     efi_status_t ret;
> > > +     efi_handle_t bm_handle;
> > > +     struct efi_handler *handler;
> > > +     struct efi_device_path *file_path;
> > > +     struct efi_device_path *device_path;
> > > +
> > > +     if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&bm_handle)) {
> > > +             log_warning("DM_TAG_EFI not found\n");
> > > +             return EFI_INVALID_PARAMETER;
> > > +     }
> > > +
> > > +     ret = efi_search_protocol(bm_handle,
> > > +                               &efi_simple_file_system_protocol_guid, &handler);
> > > +     if (ret != EFI_SUCCESS)
> > > +             return ret;
> > > +
> > > +     ret = efi_search_protocol(bm_handle, &efi_guid_device_path, &handler);
> > > +     if (ret != EFI_SUCCESS)
> > > +             return ret;
> > > +
> > > +     ret = efi_protocol_open(handler, (void **)&device_path, efi_root, NULL,
> > > +                             EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> > > +     if (ret != EFI_SUCCESS)
> > > +             return ret;
> > > +
> > > +     file_path = expand_media_path(device_path);
> > > +     ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0,
> > > +                                   image_handle));
> > > +
> > > +     efi_free_pool(file_path);
> > > +
> > > +     return ret;
> > > +}
> >
> > We need to decide what we want here.  There were recently some patches from
> > Raymond [0] which piggybacked on your earlier eficonfig work.  I think the
> > last patch of this series hasn't been merged due to a test failing, but we
> > should fix it.
> > That patch has a different approach.  Everytime a disk appears it will add
> > a boot option if the default filepath is found and that's how we fixed the
> 
> efi_bootmgr_update_media_device_boot_option() automatically adds the
> boot option if the device handle installed EFI_SIMPLE_FILE_SYSTEM_PROTOCOL,
> but it does not check whether the default file path is found or not.
> When the user selects the automatically created boot option,
> expand_media_path()
> is called and efibootmgr tries to boot with the default file.
> 

Ah correct.  On the function above though, we are we open coding a
different version of efi_open_protocol()?  Can't we call that instead of
efi_search_protocol()/efi_protocol_open()? 

> > behaviour of efibootmgr to adhere to the EFI spec.  This patch is doing the
> > opposite, trying to detect the BOOTAA64.EFI etc on the fly.  I think I
> > prefer the approach you have here, but we should end up with one solution
> > to solve both.
> 
> This HTTP Boot case is slightly different from the case where the user selects
> the automatically added boot option.
> In this case, user selects the URI device path boot option. The
> efibootmgr downloads the
> file, mount the image, and try to boot with the default file name on the fly.
> When the patch[0] is merged, it is possible to boot the downloaded iso
> image from the
> automatically added "blkmap" boot option, but I think efibootmgr needs
> to boot with
> the URI device path user selected that this series does, not boot
> from the "blkmap" boot option.
> 

Indeed, the used must still select the 'http://' boot option.  
It's been a while and I don't remember the eficonfig details.  Do you
remember why we decided to store the load options after scanning back then?  
IOW if we pick this up, can we also use it on the efibootmgr code and scan
all disks on the fly instead of adding boot options?

> >
> > > +
> > > +/**
> > > + * load_default_file_from_blk_dev() - load the default file
> > > + *
> > > + * @blk              pointer to the UCLASS_BLK udevice
> > > + * @handle:  pointer to handle for newly installed image
> > > + * Return:   status code
> > > + */
> > > +static efi_status_t load_default_file_from_blk_dev(struct udevice *blk,
> > > +                                                efi_handle_t *handle)
> > > +{
> > > +     efi_status_t ret;
> > > +     struct udevice *partition;
> > > +
> > > +     /* image that has no partition table but a file system */
> > > +     ret = try_load_default_file(blk, handle);
> > > +     if (ret == EFI_SUCCESS)
> > > +             return ret;
> > > +
> > > +     /* try the partitions */
> > > +     device_foreach_child(partition, blk) {
> > > +             enum uclass_id id;
> > > +
> > > +             id = device_get_uclass_id(partition);
> > > +             if (id != UCLASS_PARTITION)
> > > +                     continue;
> > > +
> > > +             ret = try_load_default_file(partition, handle);
> > > +             if (ret == EFI_SUCCESS)
> > > +                     return ret;
> > > +     }
> > > +
> > > +     return EFI_NOT_FOUND;
> > > +}
> > > +
> > > +/**
> > > + * try_load_from_uri_path() - Handle the URI device path
> > > + *
> > > + * @uridp:   uri device path
> > > + * @lo_label label of load option
> > > + * @handle:  pointer to handle for newly installed image
> > > + * Return:   status code
> > > + */
> > > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
> > > +                                        u16 *lo_label,
> > > +                                        efi_handle_t *handle)
> > > +{
> > > +     char *s;
> > > +     int uri_len;
> > > +     int image_size;
> > > +     efi_status_t ret;
> > > +     ulong image_addr;
> > > +
> > > +     s = env_get("loadaddr");
> > > +     if (!s) {
> > > +             log_err("Error: loadaddr is not set\n");
> > > +             return EFI_INVALID_PARAMETER;
> > > +     }
> > > +     image_addr = hextoul(s, NULL);
> > > +     image_size = wget_with_dns(image_addr, uridp->uri);
> > > +     if (image_size < 0)
> > > +             return EFI_INVALID_PARAMETER;
> > > +
> > > +     /*
> > > +      * If the file extension is ".iso" or ".img", mount it and try to load
> > > +      * the default file.
> >
> > Don't we have a better way to validate isos and efi apps instead of
> > the extension?  The efi is checked against a valid PE/COFF image so I guess
> > we'll really on the mount to fail for isos?
> 
> EDK2 reference implementation checks the file type with the following priority.
>  1) "Content-Type" header in HTTP response message
>  2) Filename Extensions
> Documentation is here[1].
> 
> Since "Content-Type" is not available in the current U-Boot wget, file extension
> is used to identify ISO images.

Ok fair enough, we can go back and improve this once lwip is merged I guess

> 
> [1] https://github.com/tianocore/tianocore.github.io/wiki/HTTP-Boot#ram-disk-boot-from-http
> 

[...]

Thanks
/Ilias
Masahisa Kojima Sept. 15, 2023, 7:45 a.m. UTC | #6
On Fri, 15 Sept 2023 at 15:32, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Hi Kojima-san,
>
> > > > +                                       efi_handle_t *image_handle)
> > > > +{
> > > > +     efi_status_t ret;
> > > > +     efi_handle_t bm_handle;
> > > > +     struct efi_handler *handler;
> > > > +     struct efi_device_path *file_path;
> > > > +     struct efi_device_path *device_path;
> > > > +
> > > > +     if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&bm_handle)) {
> > > > +             log_warning("DM_TAG_EFI not found\n");
> > > > +             return EFI_INVALID_PARAMETER;
> > > > +     }
> > > > +
> > > > +     ret = efi_search_protocol(bm_handle,
> > > > +                               &efi_simple_file_system_protocol_guid, &handler);
> > > > +     if (ret != EFI_SUCCESS)
> > > > +             return ret;
> > > > +
> > > > +     ret = efi_search_protocol(bm_handle, &efi_guid_device_path, &handler);
> > > > +     if (ret != EFI_SUCCESS)
> > > > +             return ret;
> > > > +
> > > > +     ret = efi_protocol_open(handler, (void **)&device_path, efi_root, NULL,
> > > > +                             EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> > > > +     if (ret != EFI_SUCCESS)
> > > > +             return ret;
> > > > +
> > > > +     file_path = expand_media_path(device_path);
> > > > +     ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0,
> > > > +                                   image_handle));
> > > > +
> > > > +     efi_free_pool(file_path);
> > > > +
> > > > +     return ret;
> > > > +}
> > >
> > > We need to decide what we want here.  There were recently some patches from
> > > Raymond [0] which piggybacked on your earlier eficonfig work.  I think the
> > > last patch of this series hasn't been merged due to a test failing, but we
> > > should fix it.
> > > That patch has a different approach.  Everytime a disk appears it will add
> > > a boot option if the default filepath is found and that's how we fixed the
> >
> > efi_bootmgr_update_media_device_boot_option() automatically adds the
> > boot option if the device handle installed EFI_SIMPLE_FILE_SYSTEM_PROTOCOL,
> > but it does not check whether the default file path is found or not.
> > When the user selects the automatically created boot option,
> > expand_media_path()
> > is called and efibootmgr tries to boot with the default file.
> >
>
> Ah correct.  On the function above though, we are we open coding a
> different version of efi_open_protocol()?  Can't we call that instead of
> efi_search_protocol()/efi_protocol_open()?

I will call efi_open_protocol() instead. Thanks.

>
> > > behaviour of efibootmgr to adhere to the EFI spec.  This patch is doing the
> > > opposite, trying to detect the BOOTAA64.EFI etc on the fly.  I think I
> > > prefer the approach you have here, but we should end up with one solution
> > > to solve both.
> >
> > This HTTP Boot case is slightly different from the case where the user selects
> > the automatically added boot option.
> > In this case, user selects the URI device path boot option. The
> > efibootmgr downloads the
> > file, mount the image, and try to boot with the default file name on the fly.
> > When the patch[0] is merged, it is possible to boot the downloaded iso
> > image from the
> > automatically added "blkmap" boot option, but I think efibootmgr needs
> > to boot with
> > the URI device path user selected that this series does, not boot
> > from the "blkmap" boot option.
> >
>
> Indeed, the used must still select the 'http://' boot option.
> It's been a while and I don't remember the eficonfig details.  Do you
> remember why we decided to store the load options after scanning back then?

We just followed the EDK2 reference implementation.

> IOW if we pick this up, can we also use it on the efibootmgr code and scan
> all disks on the fly instead of adding boot options?

Yes, it is possible, but I'm not sure scanning all the disks on the fly
is a good idea. For users, it is hard to control which device is selected
to boot.

Now come to think of it, we add the one load option for each disk when the
disk is detected, then try to boot with the default file by scanning
the default boot file
in the selected disk.

In current implementation, the following load options are automatically created.

Boot0000:
attributes: A-- (0x00000001)
  label: virtio 0:1
  file_path: /HD(1,GPT,b6b38698-f211-4d78-b208-f68e5523e588,0x800,0x100000)
  data:
    00000000: c1 ac c1 38 c0 9f f0 41 b9 01 fa 74 d6 d6 e4 de  ...8...A...t....
Boot0001:
attributes: A-- (0x00000001)
  label: virtio 0:2
  file_path: /HD(2,GPT,98533ff2-11d5-428c-b323-9afaf6b4abd8,0x100800,0x1122000)
  data:
    00000000: c1 ac c1 38 c0 9f f0 41 b9 01 fa 74 d6 d6 e4 de  ...8...A...t....

For this disk, only the block device "virtio 0" load option is enough,
we can search
for the default boot file on the fly. This is what EDK2 does.

Thanks,
Masahisa Kojima



>
> > >
> > > > +
> > > > +/**
> > > > + * load_default_file_from_blk_dev() - load the default file
> > > > + *
> > > > + * @blk              pointer to the UCLASS_BLK udevice
> > > > + * @handle:  pointer to handle for newly installed image
> > > > + * Return:   status code
> > > > + */
> > > > +static efi_status_t load_default_file_from_blk_dev(struct udevice *blk,
> > > > +                                                efi_handle_t *handle)
> > > > +{
> > > > +     efi_status_t ret;
> > > > +     struct udevice *partition;
> > > > +
> > > > +     /* image that has no partition table but a file system */
> > > > +     ret = try_load_default_file(blk, handle);
> > > > +     if (ret == EFI_SUCCESS)
> > > > +             return ret;
> > > > +
> > > > +     /* try the partitions */
> > > > +     device_foreach_child(partition, blk) {
> > > > +             enum uclass_id id;
> > > > +
> > > > +             id = device_get_uclass_id(partition);
> > > > +             if (id != UCLASS_PARTITION)
> > > > +                     continue;
> > > > +
> > > > +             ret = try_load_default_file(partition, handle);
> > > > +             if (ret == EFI_SUCCESS)
> > > > +                     return ret;
> > > > +     }
> > > > +
> > > > +     return EFI_NOT_FOUND;
> > > > +}
> > > > +
> > > > +/**
> > > > + * try_load_from_uri_path() - Handle the URI device path
> > > > + *
> > > > + * @uridp:   uri device path
> > > > + * @lo_label label of load option
> > > > + * @handle:  pointer to handle for newly installed image
> > > > + * Return:   status code
> > > > + */
> > > > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
> > > > +                                        u16 *lo_label,
> > > > +                                        efi_handle_t *handle)
> > > > +{
> > > > +     char *s;
> > > > +     int uri_len;
> > > > +     int image_size;
> > > > +     efi_status_t ret;
> > > > +     ulong image_addr;
> > > > +
> > > > +     s = env_get("loadaddr");
> > > > +     if (!s) {
> > > > +             log_err("Error: loadaddr is not set\n");
> > > > +             return EFI_INVALID_PARAMETER;
> > > > +     }
> > > > +     image_addr = hextoul(s, NULL);
> > > > +     image_size = wget_with_dns(image_addr, uridp->uri);
> > > > +     if (image_size < 0)
> > > > +             return EFI_INVALID_PARAMETER;
> > > > +
> > > > +     /*
> > > > +      * If the file extension is ".iso" or ".img", mount it and try to load
> > > > +      * the default file.
> > >
> > > Don't we have a better way to validate isos and efi apps instead of
> > > the extension?  The efi is checked against a valid PE/COFF image so I guess
> > > we'll really on the mount to fail for isos?
> >
> > EDK2 reference implementation checks the file type with the following priority.
> >  1) "Content-Type" header in HTTP response message
> >  2) Filename Extensions
> > Documentation is here[1].
> >
> > Since "Content-Type" is not available in the current U-Boot wget, file extension
> > is used to identify ISO images.
>
> Ok fair enough, we can go back and improve this once lwip is merged I guess
>
> >
> > [1] https://github.com/tianocore/tianocore.github.io/wiki/HTTP-Boot#ram-disk-boot-from-http
> >
>
> [...]
>
> Thanks
> /Ilias
Ilias Apalodimas Sept. 15, 2023, 8:11 a.m. UTC | #7
Kojima-san

[...]

>
> We just followed the EDK2 reference implementation.
>
> > IOW if we pick this up, can we also use it on the efibootmgr code and scan
> > all disks on the fly instead of adding boot options?
>
> Yes, it is possible, but I'm not sure scanning all the disks on the fly
> is a good idea. For users, it is hard to control which device is selected
> to boot.
>
> Now come to think of it, we add the one load option for each disk when the
> disk is detected, then try to boot with the default file by scanning
> the default boot file
> in the selected disk.
>
> In current implementation, the following load options are automatically created.
>
> Boot0000:
> attributes: A-- (0x00000001)
>   label: virtio 0:1
>   file_path: /HD(1,GPT,b6b38698-f211-4d78-b208-f68e5523e588,0x800,0x100000)
>   data:
>     00000000: c1 ac c1 38 c0 9f f0 41 b9 01 fa 74 d6 d6 e4 de  ...8...A...t....
> Boot0001:
> attributes: A-- (0x00000001)
>   label: virtio 0:2
>   file_path: /HD(2,GPT,98533ff2-11d5-428c-b323-9afaf6b4abd8,0x100800,0x1122000)
>   data:
>     00000000: c1 ac c1 38 c0 9f f0 41 b9 01 fa 74 d6 d6 e4 de  ...8...A...t....

Yep, that's exactly what I was thinking as an alternative.  On the
automatically scan boot option, just search for '1234567' on the load
options.
Keep in mind that patch from Raymon is failing on an erofs selftest.
But when I was looking at it, I came to the conclusion, that due to
the EFI subsystem coming up earlier a print changes and that test gets
utterly confused.  IOW that patch seems fine, but you'll probably need
to adjust a selftest as well.

Thanks
/Ilias
>
> For this disk, only the block device "virtio 0" load option is enough,
> we can search
> for the default boot file on the fly. This is what EDK2 does.
>
> Thanks,
> Masahisa Kojima
>
>
>
> >
> > > >
> > > > > +
> > > > > +/**
> > > > > + * load_default_file_from_blk_dev() - load the default file
> > > > > + *
> > > > > + * @blk              pointer to the UCLASS_BLK udevice
> > > > > + * @handle:  pointer to handle for newly installed image
> > > > > + * Return:   status code
> > > > > + */
> > > > > +static efi_status_t load_default_file_from_blk_dev(struct udevice *blk,
> > > > > +                                                efi_handle_t *handle)
> > > > > +{
> > > > > +     efi_status_t ret;
> > > > > +     struct udevice *partition;
> > > > > +
> > > > > +     /* image that has no partition table but a file system */
> > > > > +     ret = try_load_default_file(blk, handle);
> > > > > +     if (ret == EFI_SUCCESS)
> > > > > +             return ret;
> > > > > +
> > > > > +     /* try the partitions */
> > > > > +     device_foreach_child(partition, blk) {
> > > > > +             enum uclass_id id;
> > > > > +
> > > > > +             id = device_get_uclass_id(partition);
> > > > > +             if (id != UCLASS_PARTITION)
> > > > > +                     continue;
> > > > > +
> > > > > +             ret = try_load_default_file(partition, handle);
> > > > > +             if (ret == EFI_SUCCESS)
> > > > > +                     return ret;
> > > > > +     }
> > > > > +
> > > > > +     return EFI_NOT_FOUND;
> > > > > +}
> > > > > +
> > > > > +/**
> > > > > + * try_load_from_uri_path() - Handle the URI device path
> > > > > + *
> > > > > + * @uridp:   uri device path
> > > > > + * @lo_label label of load option
> > > > > + * @handle:  pointer to handle for newly installed image
> > > > > + * Return:   status code
> > > > > + */
> > > > > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
> > > > > +                                        u16 *lo_label,
> > > > > +                                        efi_handle_t *handle)
> > > > > +{
> > > > > +     char *s;
> > > > > +     int uri_len;
> > > > > +     int image_size;
> > > > > +     efi_status_t ret;
> > > > > +     ulong image_addr;
> > > > > +
> > > > > +     s = env_get("loadaddr");
> > > > > +     if (!s) {
> > > > > +             log_err("Error: loadaddr is not set\n");
> > > > > +             return EFI_INVALID_PARAMETER;
> > > > > +     }
> > > > > +     image_addr = hextoul(s, NULL);
> > > > > +     image_size = wget_with_dns(image_addr, uridp->uri);
> > > > > +     if (image_size < 0)
> > > > > +             return EFI_INVALID_PARAMETER;
> > > > > +
> > > > > +     /*
> > > > > +      * If the file extension is ".iso" or ".img", mount it and try to load
> > > > > +      * the default file.
> > > >
> > > > Don't we have a better way to validate isos and efi apps instead of
> > > > the extension?  The efi is checked against a valid PE/COFF image so I guess
> > > > we'll really on the mount to fail for isos?
> > >
> > > EDK2 reference implementation checks the file type with the following priority.
> > >  1) "Content-Type" header in HTTP response message
> > >  2) Filename Extensions
> > > Documentation is here[1].
> > >
> > > Since "Content-Type" is not available in the current U-Boot wget, file extension
> > > is used to identify ISO images.
> >
> > Ok fair enough, we can go back and improve this once lwip is merged I guess
> >
> > >
> > > [1] https://github.com/tianocore/tianocore.github.io/wiki/HTTP-Boot#ram-disk-boot-from-http
> > >
> >
> > [...]
> >
> > Thanks
> > /Ilias
diff mbox series

Patch

diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
index a40762c74c..0e8d2ca9d1 100644
--- a/lib/efi_loader/efi_bootmgr.c
+++ b/lib/efi_loader/efi_bootmgr.c
@@ -7,10 +7,14 @@ 
 
 #define LOG_CATEGORY LOGC_EFI
 
+#include <blk.h>
+#include <blkmap.h>
 #include <common.h>
 #include <charset.h>
+#include <dm.h>
 #include <log.h>
 #include <malloc.h>
+#include <net.h>
 #include <efi_default_filename.h>
 #include <efi_loader.h>
 #include <efi_variable.h>
@@ -168,6 +172,193 @@  out:
 	return ret;
 }
 
+#if (IS_ENABLED(CONFIG_BLKMAP) && IS_ENABLED(CONFIG_CMD_WGET) && IS_ENABLED(CONFIG_CMD_DNS))
+/**
+ * mount_image() - mount the image with blkmap
+ *
+ * @lo_label	u16 label string of load option
+ * @image_addr:	image address
+ * @image_size	image size
+ * Return:	pointer to the UCLASS_BLK udevice, NULL if failed
+ */
+static struct udevice *mount_image(u16 *lo_label, ulong image_addr, int image_size)
+{
+	int err;
+	struct blkmap *bm;
+	struct udevice *bm_dev;
+	char *label = NULL, *p;
+
+	label = efi_alloc(utf16_utf8_strlen(lo_label) + 1);
+	if (!label)
+		return NULL;
+
+	p = label;
+	utf16_utf8_strcpy(&p, lo_label);
+	err = blkmap_create_ramdisk(label, image_addr, image_size, &bm_dev);
+	if (err) {
+		efi_free_pool(label);
+		return NULL;
+	}
+	bm = dev_get_plat(bm_dev);
+
+	efi_free_pool(label);
+
+	return bm->blk;
+}
+
+/**
+ * try_load_default_file() - try to load the default file
+ *
+ * Search the device having EFI_SIMPLE_FILE_SYSTEM_PROTOCOL,
+ * then try to load with the default boot file(e.g. EFI/BOOT/BOOTAA64.EFI).
+ *
+ * @dev			pointer to the UCLASS_BLK or UCLASS_PARTITION udevice
+ * @image_handle:	pointer to handle for newly installed image
+ * Return:		status code
+ */
+static efi_status_t try_load_default_file(struct udevice *dev,
+					  efi_handle_t *image_handle)
+{
+	efi_status_t ret;
+	efi_handle_t bm_handle;
+	struct efi_handler *handler;
+	struct efi_device_path *file_path;
+	struct efi_device_path *device_path;
+
+	if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&bm_handle)) {
+		log_warning("DM_TAG_EFI not found\n");
+		return EFI_INVALID_PARAMETER;
+	}
+
+	ret = efi_search_protocol(bm_handle,
+				  &efi_simple_file_system_protocol_guid, &handler);
+	if (ret != EFI_SUCCESS)
+		return ret;
+
+	ret = efi_search_protocol(bm_handle, &efi_guid_device_path, &handler);
+	if (ret != EFI_SUCCESS)
+		return ret;
+
+	ret = efi_protocol_open(handler, (void **)&device_path, efi_root, NULL,
+				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+	if (ret != EFI_SUCCESS)
+		return ret;
+
+	file_path = expand_media_path(device_path);
+	ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0,
+				      image_handle));
+
+	efi_free_pool(file_path);
+
+	return ret;
+}
+
+/**
+ * load_default_file_from_blk_dev() - load the default file
+ *
+ * @blk		pointer to the UCLASS_BLK udevice
+ * @handle:	pointer to handle for newly installed image
+ * Return:	status code
+ */
+static efi_status_t load_default_file_from_blk_dev(struct udevice *blk,
+						   efi_handle_t *handle)
+{
+	efi_status_t ret;
+	struct udevice *partition;
+
+	/* image that has no partition table but a file system */
+	ret = try_load_default_file(blk, handle);
+	if (ret == EFI_SUCCESS)
+		return ret;
+
+	/* try the partitions */
+	device_foreach_child(partition, blk) {
+		enum uclass_id id;
+
+		id = device_get_uclass_id(partition);
+		if (id != UCLASS_PARTITION)
+			continue;
+
+		ret = try_load_default_file(partition, handle);
+		if (ret == EFI_SUCCESS)
+			return ret;
+	}
+
+	return EFI_NOT_FOUND;
+}
+
+/**
+ * try_load_from_uri_path() - Handle the URI device path
+ *
+ * @uridp:	uri device path
+ * @lo_label	label of load option
+ * @handle:	pointer to handle for newly installed image
+ * Return:	status code
+ */
+static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
+					   u16 *lo_label,
+					   efi_handle_t *handle)
+{
+	char *s;
+	int uri_len;
+	int image_size;
+	efi_status_t ret;
+	ulong image_addr;
+
+	s = env_get("loadaddr");
+	if (!s) {
+		log_err("Error: loadaddr is not set\n");
+		return EFI_INVALID_PARAMETER;
+	}
+	image_addr = hextoul(s, NULL);
+	image_size = wget_with_dns(image_addr, uridp->uri);
+	if (image_size < 0)
+		return EFI_INVALID_PARAMETER;
+
+	/*
+	 * If the file extension is ".iso" or ".img", mount it and try to load
+	 * the default file.
+	 * If the file is ".efi" and PE-COFF image, load the downloaded file.
+	 */
+	uri_len = strlen(uridp->uri); /* todo: directly use uridp->uri */
+	if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
+	    !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
+		struct udevice *blk;
+
+		blk = mount_image(lo_label, image_addr, image_size);
+		if (!blk)
+			return EFI_INVALID_PARAMETER;
+
+		ret = load_default_file_from_blk_dev(blk, handle);
+	} else if (!strncmp(&uridp->uri[uri_len - 4], ".efi", 4)) {
+		efi_handle_t mem_handle = NULL;
+		struct efi_device_path *file_path = NULL;
+
+		ret = efi_check_pe((void *)image_addr, image_size, NULL);
+		if (ret != EFI_SUCCESS) {
+			log_err("Error: downloaded image is not a PE-COFF image\n");
+			return EFI_INVALID_PARAMETER;
+		}
+
+		file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
+					    (uintptr_t)image_addr, image_size);
+		ret = efi_install_multiple_protocol_interfaces(
+			&mem_handle, &efi_guid_device_path, file_path, NULL);
+		if (ret != EFI_SUCCESS)
+			return EFI_INVALID_PARAMETER;
+
+		ret = EFI_CALL(efi_load_image(false, efi_root, file_path,
+					      (void *)image_addr, image_size,
+					      handle));
+	} else {
+		log_err("Error: file type is not supported\n");
+		return EFI_INVALID_PARAMETER;
+	}
+
+	return ret;
+}
+#endif
+
 /**
  * try_load_entry() - try to load image for boot option
  *
@@ -211,6 +402,12 @@  static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
 		if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
 			/* file_path doesn't contain a device path */
 			ret = try_load_from_short_path(lo.file_path, handle);
+#if (IS_ENABLED(CONFIG_BLKMAP) && IS_ENABLED(CONFIG_CMD_WGET) && IS_ENABLED(CONFIG_CMD_DNS))
+		} else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) {
+			ret = try_load_from_uri_path(
+				(struct efi_device_path_uri *)lo.file_path,
+				lo.label, handle);
+#endif
 		} else {
 			file_path = expand_media_path(lo.file_path);
 			ret = EFI_CALL(efi_load_image(true, efi_root, file_path,