Skip to contentSkip to conversation

Improve disk image detection in efi_bootmgr

PATCH In review Javier Tia 2 participants 5 messages Oct 9, 2025 – Oct 10, 2025

lore
Share & tools
lore

Javier Tia

author
#0 lore raw

v8 cover Improve disk image detection in efi_bootmgr

Enhances the process for identifying disk images within the EFI boot manager. Utilize part_driver_lookup_type() to verify the validity of a downloaded file as a disk image, rather than depending on file extensions.

part_driver_lookup_type() is now used in the prepare_loaded_image() function in the EFI boot manager to detect partitions on a block device created from a downloaded image. This allows the boot manager to boot from any disk image that can be recognized by a partition driver, not just ISO and IMG images.

Update prepare_loaded_image() to create the ramdisk block device internally, obtain the blk_desc and use part_driver_lookup_type() to detect a valid partition table.

In try_load_from_uri_path(), try prepare_loaded_image() first to detect disk images, and fall back to PE-COFF detection only if that fails.

Include part.h in the EFI loader and export part_driver_lookup_type and add its prototype and documentation in include/part.h. This makes the partition-driver lookup available to other code paths that need to detect partition tables on a block descriptor.

---
Changes in v8:
- Cleanup part_driver_lookup_type history after being removed and added again.
- Passing CI: https://github.com/u-boot/u-boot/pull/816

- v7 link: https://lore.kernel.org/u-boot/[email protected]/
- v6 link: https://lore.kernel.org/u-boot/[email protected]/
- v5 link: https://lore.kernel.org/u-boot/[email protected]/
- v4 link: https://lore.kernel.org/u-boot/[email protected]/
- v3 link: https://lore.kernel.org/u-boot/[email protected]/
- v2 link: https://lore.kernel.org/u-boot/[email protected]/
- v1 link: https://lore.kernel.org/u-boot/[email protected]/

---
Javier Tia (2):
part: Export part_driver_lookup_type for external use
efi_loader: Improve disk image detection in efi_bootmgr

 disk/part.c                  | 18 +---------------
 include/part.h               | 21 +++++++++++++++++++
 lib/efi_loader/efi_bootmgr.c | 49 +++++++++++++++++++++++++++++++-------------
 3 files changed, 57 insertions(+), 31 deletions(-)

---
base-commit: 0ee639ff5af33342c6c2f4579e210d707abc9bc2
change-id: 20251008-detect_disk_image_type-4f04a2a8d989

Best regards,

Signature · 1 line
Javier Tia <[email protected]>
author
#1 lore raw

PATCH v8 1/2 part: Export part_driver_lookup_type for external use

Make part_driver_lookup_type non-static so it can be used outside part.c. This allows external callers to determine the appropriate partition driver for a block device, enabling more flexible handling of partition types.

Add a prototype and kernel-doc comment in part.h to document the function contract. Provide a stub inline implementation returning NULL when partition support is disabled, ensuring build consistency across configurations.

---
 disk/part.c    | 18 +-----------------
 include/part.h | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 17 deletions(-)
disk/part.c +1 -17
index 66e2b3a7219..be2b45d5a29 100644
@@ -47,23 +47,7 @@ static struct part_driver *part_driver_get_type(int part_type)
 	return NULL;
 }
 
-/**
- * part_driver_lookup_type() - Look up the partition driver for a blk device
- *
- * If @desc->part_type is PART_TYPE_UNKNOWN, this checks each parition driver
- * against the blk device to see if there is a valid partition table acceptable
- * to that driver.
- *
- * If @desc->part_type is already set, it just returns the driver for that
- * type, without testing if the driver can find a valid partition on the
- * descriptor.
- *
- * On success it updates @desc->part_type if set to PART_TYPE_UNKNOWN on entry
- *
- * @dev_desc: Device descriptor
- * Return: Driver found, or NULL if none
- */
-static struct part_driver *part_driver_lookup_type(struct blk_desc *desc)
+struct part_driver *part_driver_lookup_type(struct blk_desc *desc)
 {
 	struct part_driver *drv =
 		ll_entry_start(struct part_driver, part_driver);
include/part.h +21 -0
index b772fb34c8a..6caaa6526aa 100644
@@ -727,6 +727,24 @@ int part_get_type_by_name(const char *name);
  */
 int part_get_bootable(struct blk_desc *desc);
 
+/**
+ * part_driver_lookup_type() - Look up the partition driver for a blk device
+ *
+ * If @desc->part_type is PART_TYPE_UNKNOWN, this checks each partition driver
+ * against the blk device to see if there is a valid partition table acceptable
+ * to that driver.
+ *
+ * If @desc->part_type is already set, it just returns the driver for that
+ * type, without testing if the driver can find a valid partition on the
+ * descriptor.
+ *
+ * On success it updates @desc->part_type if set to PART_TYPE_UNKNOWN on entry
+ *
+ * @desc: Device descriptor
+ * Return: Driver found, or NULL if none
+ */
+struct part_driver *part_driver_lookup_type(struct blk_desc *desc);
+
 #else
 static inline int part_driver_get_count(void)
 { return 0; }
@@ -737,6 +755,9 @@ static inline struct part_driver *part_driver_get_first(void)
 static inline bool part_get_bootable(struct blk_desc *desc)
 { return false; }
 
+static inline struct part_driver *part_driver_lookup_type(struct blk_desc *desc)
+{ return NULL; }
+
 #endif /* CONFIG_PARTITIONS */
 
 #endif /* _PART_H */
Signature · 1 line
2.51.0
Reviewed-by on v8 1/2
#2 lore raw

Re: [PATCH v8 1/2] part: Export part_driver_lookup_type for external use

Hi Javier,

Quoted 6 lines · Javier Tia

Make part_driver_lookup_type non-static so it can be used outside part.c. This allows external callers to determine the appropriate partition driver for a block device, enabling more flexible handling of partition types.

Quoted 6 lines

Add a prototype and kernel-doc comment in part.h to document the function contract. Provide a stub inline implementation returning NULL when partition support is disabled, ensuring build consistency across configurations.

Quoted 77 lines
---
 disk/part.c    | 18 +-----------------
 include/part.h | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 17 deletions(-)
disk/part.c +1 -17
index 66e2b3a7219..be2b45d5a29 100644
@@ -47,23 +47,7 @@ static struct part_driver *part_driver_get_type(int part_type)
        return NULL;
 }
 
-/**
- * part_driver_lookup_type() - Look up the partition driver for a blk device
- *
- * If @desc->part_type is PART_TYPE_UNKNOWN, this checks each parition driver
- * against the blk device to see if there is a valid partition table acceptable
- * to that driver.
- *
- * If @desc->part_type is already set, it just returns the driver for that
- * type, without testing if the driver can find a valid partition on the
- * descriptor.
- *
- * On success it updates @desc->part_type if set to PART_TYPE_UNKNOWN on entry
- *
- * @dev_desc: Device descriptor
- * Return: Driver found, or NULL if none
- */
-static struct part_driver *part_driver_lookup_type(struct blk_desc *desc)
+struct part_driver *part_driver_lookup_type(struct blk_desc *desc)
 {
        struct part_driver *drv =
                ll_entry_start(struct part_driver, part_driver);
include/part.h +21 -1
index b772fb34c8a..6caaa6526aa 100644
@@ -727,6 +727,24 @@ int part_get_type_by_name(const char *name);
  */
 int part_get_bootable(struct blk_desc *desc);
 
+/**
+ * part_driver_lookup_type() - Look up the partition driver for a blk device
+ *
+ * If @desc->part_type is PART_TYPE_UNKNOWN, this checks each partition driver
+ * against the blk device to see if there is a valid partition table acceptable
+ * to that driver.
+ *
+ * If @desc->part_type is already set, it just returns the driver for that
+ * type, without testing if the driver can find a valid partition on the
+ * descriptor.
+ *
+ * On success it updates @desc->part_type if set to PART_TYPE_UNKNOWN on entry
+ *
+ * @desc: Device descriptor
+ * Return: Driver found, or NULL if none
+ */
+struct part_driver *part_driver_lookup_type(struct blk_desc *desc);
+
 #else
 static inline int part_driver_get_count(void)
 { return 0; }
@@ -737,6 +755,9 @@ static inline struct part_driver *part_driver_get_first(void)
 static inline bool part_get_bootable(struct blk_desc *desc)
 { return false; }
 
+static inline struct part_driver *part_driver_lookup_type(struct blk_desc *desc)
+{ return NULL; }
+
 #endif /* CONFIG_PARTITIONS */
 
 #endif /* _PART_H */
 
--

2.51.0

author
#3 lore raw

PATCH v8 2/2 efi_loader: Improve disk image detection in efi_bootmgr

In reply to Javier Tia

Enhances the process for identifying disk images within the EFI boot manager. Utilize part_driver_lookup_type() to verify the validity of a downloaded file as a disk image, rather than depending on file extensions.

part_driver_lookup_type() is now used in the prepare_loaded_image() function in the EFI boot manager to detect partitions on a block device created from a downloaded image. This allows the boot manager to boot from any disk image that can be recognized by a partition driver, not just ISO and IMG images.

Update prepare_loaded_image() to create the ramdisk block device internally, obtain the blk_desc and use part_driver_lookup_type() to detect a valid partition table.

In try_load_from_uri_path(), try prepare_loaded_image() first to detect disk images, and fall back to PE-COFF detection only if that fails.

---
 lib/efi_loader/efi_bootmgr.c | 49 +++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 14 deletions(-)
lib/efi_loader/efi_bootmgr.c +35 -14
index 662993fb809..d472dde158a 100644
@@ -17,6 +17,7 @@
 #include <log.h>
 #include <malloc.h>
 #include <net.h>
+#include <part.h>
 #include <efi_loader.h>
 #include <efi_variable.h>
 #include <asm/unaligned.h>
@@ -347,6 +348,9 @@ static efi_status_t fill_default_file_path(struct udevice *blk,
  * @dp:		pointer to default file device path
  * @blk:	pointer to created blk udevice
  * Return:	status code
+ *
+ * This function handles device creation internally and performs cleanup
+ * on error paths.
  */
 static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
 					 struct efi_device_path **dp,
@@ -355,10 +359,31 @@ static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
 	u64 pages;
 	efi_status_t ret;
 	struct udevice *ramdisk_blk;
+	struct blk_desc *desc;
+	struct part_driver *part_drv;
 
+	/* Create the ramdisk block device internally */
 	ramdisk_blk = mount_image(label, addr, size);
-	if (!ramdisk_blk)
-		return EFI_LOAD_ERROR;
+	if (!ramdisk_blk) {
+		log_err("Failed to create ramdisk block device\n");
+		return EFI_DEVICE_ERROR;
+	}
+
+	/* Get the block descriptor and detect partitions */
+	desc = dev_get_uclass_plat(ramdisk_blk);
+	if (!desc) {
+		log_err("Failed to get block descriptor\n");
+		ret = EFI_DEVICE_ERROR;
+		goto err;
+	}
+
+	/* Use part_driver_lookup_type for comprehensive partition detection */
+	part_drv = part_driver_lookup_type(desc);
+	if (!part_drv) {
+		log_err("Image is not a valid disk image\n");
+		ret = EFI_INVALID_PARAMETER;
+		goto err;
+	}
 
 	ret = fill_default_file_path(ramdisk_blk, dp);
 	if (ret != EFI_SUCCESS) {
@@ -407,7 +432,7 @@ static efi_status_t efi_bootmgr_release_uridp(struct uridp_context *ctx)
 	if (!ctx)
 		return ret;
 
-	/* cleanup for iso or img image */
+	/* cleanup for disk image */
 	if (ctx->ramdisk_blk_dev) {
 		ret = efi_add_memory_map(ctx->image_addr, ctx->image_size,
 					 EFI_CONVENTIONAL_MEMORY);
@@ -466,7 +491,6 @@ static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
 {
 	char *s;
 	int err;
-	int uri_len;
 	efi_status_t ret;
 	void *source_buffer;
 	efi_uintn_t source_size;
@@ -516,18 +540,15 @@ static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp,
 	image_size = ALIGN(image_size, SZ_2M);
 
 	/*
-	 * If the file extension is ".iso" or ".img", mount it and try to load
-	 * the default file.
-	 * If the file is PE-COFF image, load the downloaded file.
+	 * Check if the downloaded file is a disk image or PE-COFF image.
+	 * Try disk image detection first using prepare_loaded_image().
 	 */
-	uri_len = strlen(uridp->uri);
-	if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) ||
-	    !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) {
-		ret = prepare_loaded_image(lo_label, image_addr, image_size,
-					   &loaded_dp, &blk);
-		if (ret != EFI_SUCCESS)
-			goto err;
 
+	/* First, try to treat the image as a disk image */
+	ret = prepare_loaded_image(lo_label, image_addr, image_size,
+				   &loaded_dp, &blk);
+	if (ret == EFI_SUCCESS) {
+		/* Image is a disk image, set up for disk boot */
 		source_buffer = NULL;
 		source_size = 0;
 	} else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) {
Signature · 1 line
2.51.0
Reviewed-by on v8 2/2
#4 lore raw

Re: [PATCH v8 2/2] efi_loader: Improve disk image detection in efi_bootmgr

Quoted 49 lines · Javier Tia

Enhances the process for identifying disk images within the EFI boot manager. Utilize part_driver_lookup_type() to verify the validity of a downloaded file as a disk image, rather than depending on file extensions.

part_driver_lookup_type() is now used in the prepare_loaded_image() function in the EFI boot manager to detect partitions on a block device created from a downloaded image. This allows the boot manager to boot from any disk image that can be recognized by a partition driver, not just ISO and IMG images.

Update prepare_loaded_image() to create the ramdisk block device internally, obtain the blk_desc and use part_driver_lookup_type() to detect a valid partition table.

In try_load_from_uri_path(), try prepare_loaded_image() first to detect disk images, and fall back to PE-COFF detection only if that fails.

---
 lib/efi_loader/efi_bootmgr.c | 49 +++++++++++++++++++++++++++++++-------------
 1 file changed, 35 insertions(+), 14 deletions(-)
lib/efi_loader/efi_bootmgr.c +4 -0
index 662993fb809..d472dde158a 100644
@@ -17,6 +17,7 @@
 #include <log.h>
 #include <malloc.h>
 #include <net.h>
+#include <part.h>
 #include <efi_loader.h>
 #include <efi_variable.h>
 #include <asm/unaligned.h>
@@ -347,6 +348,9 @@ static efi_status_t fill_default_file_path(struct udevice *blk,
  * @dp:                pointer to default file device path
  * @blk:       pointer to created blk udevice
  * Return:     status code
+ *
+ * This function handles device creation internally and performs cleanup
+ * on error paths.
  */
 static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
                                         struct efi_device_path **dp,
@@ -355,10 +359,31 @@ static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size,
        u64 pages;
        efi_status_t ret;

lib/efi_loader/efi_bootmgr.c static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size, · line 368

Quoted 10 lines
lib/efi_loader/efi_bootmgr.c +5 -2
        struct udevice *ramdisk_blk;
+       struct blk_desc *desc;
+       struct part_driver *part_drv;
 
+       /* Create the ramdisk block device internally */
        ramdisk_blk = mount_image(label, addr, size);
-       if (!ramdisk_blk)
-               return EFI_LOAD_ERROR;
+       if (!ramdisk_blk) {
+               log_err("Failed to create ramdisk block device\n");

I think this needs to be a log_warning() because we might be booting an EFI app. Heinrich any preference?

[....]

Other than that