Skip to contentSkip to conversation

efi_loader: add EFI_PARTITION_INFO_PROTOCOL support

PATCH New Javier Martinez Canillas 1 participant 5 messages Jun 19, 2025

lore
Share & tools
lore
#0 lore raw

v3 cover efi_loader: add EFI_PARTITION_INFO_PROTOCOL support

This patch series implements support for the EFI_PARTITION_INFO_PROTOCOL, to provide cached partition information for GPT partition types.

The EFI specification mentions that the protocol can understand legacy MBR partition types, but that's for backward compatibility and not implemented by this series. It can be added as a follow-up if needed, to make it fully compliant with the spec.

Patch #1 moves the logic to get a GPT partition table entry to a helper part_get_gpt_pte() function.

Patch #2 exposes that helper function to be used by the efi_loader.

Patch #3 implements the actual protocol support using the mentioned helper to get the GPT partitions table entries.

Patch #4 extends the existing block io EFI selftest to also check for some cached data provided by the partition info protocol.

This is a v3 of the series, the v2 can be found here:

https://lists.denx.de/pipermail/u-boot/2025-June/592550.html

The EFI_PARTITION_INFO_PROTOCOL implementation is added unconditionally, because the u-boot-size-test.sh reports the code increase is negligible:

./u-boot-size-test.sh qemu_arm64_lwip --all
...
01: Merge patch series "list.h/treewide: get rid of no-op prefetch()"
02: disk: efi: Move logic to get a GPT entry into a helper function
03: disk: efi: expose the part_get_gpt_pte() helper function
04: efi_loader: disk: add EFI_PARTITION_INFO_PROTOCOL support
05: efi_selftest: Add basic partition info check to block io test
5 0 0 /5 qemu_arm64_lwip
Completed: 5 total built, 5 newly), duration 0:01:57, rate 0.04 /tmp/qemu_arm64_lwip/add-efi-part-info-proto-v3/boards.cfg is up to date. Nothing to do. Summary of 5 commits for 1 boards (1 thread, 8 jobs per thread)
01: Merge patch series "list.h/treewide: get rid of no-op prefetch()"
02: disk: efi: Move logic to get a GPT entry into a helper function
aarch64: (for 1/1 boards) all -64.0 text -64.0
qemu_arm64_lwip: all -64 text -64
u-boot: add: 0/0, grow: 0/-1 bytes: 0/-72 (-72)
function old new delta
part_get_info_efi 468 396 -72
03: disk: efi: expose the part_get_gpt_pte() helper function
aarch64: (for 1/1 boards) all +32.0 text +32.0
qemu_arm64_lwip: all +32 text +32
u-boot: add: 1/0, grow: 0/-1 bytes: 200/-164 (36)
function old new delta
part_get_gpt_pte - 200 +200
part_get_info_efi 396 232 -164
04: efi_loader: disk: add EFI_PARTITION_INFO_PROTOCOL support
aarch64: (for 1/1 boards) all +176.0 rodata +16.0 text +160.0
qemu_arm64_lwip: all +176 rodata +16 text +160
u-boot: add: 0/0, grow: 1/0 bytes: 148/0 (148)
function old new delta
static.efi_disk_add_dev 636 784 +148
05: efi_selftest: Add basic partition info check to block io test
aarch64: (for 1/1 boards) all +531.0 rodata +179.0 text +352.0
qemu_arm64_lwip: all +531 rodata +179 text +352
u-boot: add: 1/0, grow: 1/0 bytes: 384/0 (384)
function old new delta
execute 36428 36796 +368
partition_info_guid - 16 +16
(no errors to report)

Changes in v3:
- Add ifdef guards for {EFI,DOS}_PARTITION being disabled (Ilias Apalodimas).
- Fix build in -next due missing part_efi.h header include (Ilias Apalodimas).
- Rebase on top of -next (Ilias Apalodimas).

Changes in v2:
- Add a test for the EFI partition information protocol (Tom Rini).

Javier Martinez Canillas (4):
disk: efi: Move logic to get a GPT entry into a helper function
disk: efi: expose the part_get_gpt_pte() helper function
efi_loader: disk: add EFI_PARTITION_INFO_PROTOCOL support
efi_selftest: Add basic partition info check to block io test

 disk/part_efi.c                              | 67 ++++++++++++--------
 include/part.h                               | 14 ++++
 include/part_efi.h                           | 20 ++++++
 lib/efi_loader/efi_disk.c                    | 39 +++++++++++-
 lib/efi_selftest/efi_selftest_block_device.c | 30 +++++++++
 5 files changed, 141 insertions(+), 29 deletions(-)
Signature · 4 lines
2.49.0

base-commit: ce2a7fcbd565d55b1d3fdc0e68edd50a21eca7d4
branch: add-efi-part-info-proto-v3
#1 lore raw

PATCH v3 1/4 disk: efi: Move logic to get a GPT entry into a helper function

Factor out the logic to get the Partition Table Entry (PTE) of a given partition into a helper function, since it could be used by other code.

---

(no changes since v1)

 disk/part_efi.c | 67 +++++++++++++++++++++++++++++--------------------
 1 file changed, 40 insertions(+), 27 deletions(-)
disk/part_efi.c +40 -27
index 68ba1d11e7b6..d0b1f96adff2 100644
@@ -215,6 +215,34 @@ int get_disk_guid(struct blk_desc *desc, char *guid)
 	return 0;
 }
 
+static int part_get_gpt_pte(struct blk_desc *desc, int part, gpt_entry *gpt_e)
+{
+	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
+	gpt_entry *gpt_pte = NULL;
+
+	/* "part" argument must be at least 1 */
+	if (part < 1) {
+		log_debug("Invalid Argument(s)\n");
+		return -EINVAL;
+	}
+
+	/* This function validates AND fills in the GPT header and PTE */
+	if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
+		return -EINVAL;
+
+	if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
+	    !is_pte_valid(&gpt_pte[part - 1])) {
+		log_debug("Invalid partition number %d\n", part);
+		free(gpt_pte);
+		return -EPERM;
+	}
+
+	memcpy(gpt_e, &gpt_pte[part - 1], sizeof(*gpt_e));
+
+	free(gpt_pte);
+	return 0;
+}
+
 static void __maybe_unused part_print_efi(struct blk_desc *desc)
 {
 	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
@@ -260,45 +288,32 @@ static void __maybe_unused part_print_efi(struct blk_desc *desc)
 static int __maybe_unused part_get_info_efi(struct blk_desc *desc, int part,
 					    struct disk_partition *info)
 {
-	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
-	gpt_entry *gpt_pte = NULL;
-
-	/* "part" argument must be at least 1 */
-	if (part < 1) {
-		log_debug("Invalid Argument(s)\n");
-		return -EINVAL;
-	}
-
-	/* This function validates AND fills in the GPT header and PTE */
-	if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
-		return -EINVAL;
+	gpt_entry gpt_pte = {};
+	int ret;
 
-	if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
-	    !is_pte_valid(&gpt_pte[part - 1])) {
-		log_debug("Invalid partition number %d\n", part);
-		free(gpt_pte);
-		return -EPERM;
-	}
+	ret = part_get_gpt_pte(desc, part, &gpt_pte);
+	if (ret)
+		return ret;
 
 	/* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
-	info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
+	info->start = (lbaint_t)le64_to_cpu(gpt_pte.starting_lba);
 	/* The ending LBA is inclusive, to calculate size, add 1 to it */
-	info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
+	info->size = (lbaint_t)le64_to_cpu(gpt_pte.ending_lba) + 1
 		     - info->start;
 	info->blksz = desc->blksz;
 
 	snprintf((char *)info->name, sizeof(info->name), "%s",
-		 print_efiname(&gpt_pte[part - 1]));
+		 print_efiname(&gpt_pte));
 	strcpy((char *)info->type, "U-Boot");
-	info->bootable = get_bootable(&gpt_pte[part - 1]);
-	info->type_flags = gpt_pte[part - 1].attributes.fields.type_guid_specific;
+	info->bootable = get_bootable(&gpt_pte);
+	info->type_flags = gpt_pte.attributes.fields.type_guid_specific;
 	if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) {
-		uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b,
+		uuid_bin_to_str(gpt_pte.unique_partition_guid.b,
 				(char *)disk_partition_uuid(info),
 				UUID_STR_FORMAT_GUID);
 	}
 	if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) {
-		uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
+		uuid_bin_to_str(gpt_pte.partition_type_guid.b,
 				(char *)disk_partition_type_guid(info),
 				UUID_STR_FORMAT_GUID);
 	}
@@ -306,8 +321,6 @@ static int __maybe_unused part_get_info_efi(struct blk_desc *desc, int part,
 	log_debug("start 0x" LBAF ", size 0x" LBAF ", name %s\n", info->start,
 		  info->size, info->name);
 
-	/* Remember to free pte */
-	free(gpt_pte);
 	return 0;
 }
 
Signature · 1 line
2.49.0
#2 lore raw

PATCH v3 2/4 disk: efi: expose the part_get_gpt_pte() helper function

In reply to Javier Martinez Canillas

This function will be used by the EFI application disk support code to provide data required by the EFI_PARTITION_INFORMATION_PROTOCOL.

---

(no changes since v1)

 disk/part_efi.c |  2 +-
 include/part.h  | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
disk/part_efi.c +1 -1
index d0b1f96adff2..fb1ed534f861 100644
@@ -215,7 +215,7 @@ int get_disk_guid(struct blk_desc *desc, char *guid)
 	return 0;
 }
 
-static int part_get_gpt_pte(struct blk_desc *desc, int part, gpt_entry *gpt_e)
+int part_get_gpt_pte(struct blk_desc *desc, int part, gpt_entry *gpt_e)
 {
 	ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
 	gpt_entry *gpt_pte = NULL;
include/part.h +14 -0
index 7075b2cb1164..b3a3362b2af2 100644
@@ -647,6 +647,20 @@ int gpt_verify_partitions(struct blk_desc *desc,
  */
 int get_disk_guid(struct blk_desc *desc, char *guid);
 
+/**
+ * part_get_gpt_pte() - Get the GPT partition table entry of a partition
+ *
+ * This function reads the GPT partition table entry (PTE) for a given
+ * block device and partition number.
+ *
+ * @desc:	block device descriptor
+ * @part:	partition number for which to return the PTE
+ * @gpt_e:	GPT partition table entry
+ *
+ * Return:	0 on success, otherwise error
+ */
+int part_get_gpt_pte(struct blk_desc *desc, int part, gpt_entry *gpt_e);
+
 #endif
 
 #if CONFIG_IS_ENABLED(DOS_PARTITION)
Signature · 1 line
2.49.0
#3 lore raw

PATCH v3 3/4 efi_loader: disk: add EFI_PARTITION_INFO_PROTOCOL support

In reply to Javier Martinez Canillas

The UEFI 2.10 specification mentions that this protocol shall be installed along with EFI_BLOCK_IO_PROTOCOL. It provides cached partition information for MBR and GPT partition types.

This patch just implements support for GPT partition types. The legacy MBR partition types is only needed for backward compatibility and can be added as a follow-up if needed, to make it fully compliant with the EFI spec.

---

Changes in v3:
- Add ifdef guards for {EFI,DOS}_PARTITION being disabled (Ilias Apalodimas).

 include/part_efi.h        | 20 ++++++++++++++++++++
 lib/efi_loader/efi_disk.c | 39 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 57 insertions(+), 2 deletions(-)
include/part_efi.h +20 -0
index 59b7895b8a23..fb402df6f13e 100644
@@ -138,4 +138,24 @@ typedef struct _legacy_mbr {
 	__le16 signature;
 } __packed legacy_mbr;
 
+#define EFI_PARTITION_INFO_PROTOCOL_GUID \
+	EFI_GUID(0x8cf2f62c, 0xbc9b, 0x4821, 0x80, \
+		 0x8d, 0xec, 0x9e, 0xc4, 0x21, 0xa1, 0xa0)
+
+#define EFI_PARTITION_INFO_PROTOCOL_REVISION 0x0001000
+#define PARTITION_TYPE_OTHER 0x00
+#define PARTITION_TYPE_MBR 0x01
+#define PARTITION_TYPE_GPT 0x02
+
+struct efi_partition_info {
+	u32 revision;
+	u32 type;
+	u8 system;
+	u8 reserved[7];
+	union {
+		struct partition mbr;
+		gpt_entry gpt;
+	} info;
+} __packed;
+
 #endif	/* _DISK_PART_EFI_H */
lib/efi_loader/efi_disk.c +37 -2
index 47b583cc5e12..130c4db9606f 100644
@@ -26,6 +26,7 @@ struct efi_system_partition efi_system_partition = {
 
 const efi_guid_t efi_block_io_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
 const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
+const efi_guid_t efi_partition_info_guid = EFI_PARTITION_INFO_PROTOCOL_GUID;
 
 /**
  * struct efi_disk_obj - EFI disk object
@@ -35,6 +36,7 @@ const efi_guid_t efi_system_partition_guid = PARTITION_SYSTEM_GUID;
  * @media:	block I/O media information
  * @dp:		device path to the block device
  * @volume:	simple file system protocol of the partition
+ * @info:	EFI partition info protocol interface
  */
 struct efi_disk_obj {
 	struct efi_object header;
@@ -42,6 +44,7 @@ struct efi_disk_obj {
 	struct efi_block_io_media media;
 	struct efi_device_path *dp;
 	struct efi_simple_file_system_protocol *volume;
+	struct efi_partition_info info;
 };
 
 /**
@@ -426,6 +429,7 @@ static efi_status_t efi_disk_add_dev(
 	/* Fill in object data */
 	if (part_info) {
 		struct efi_device_path *node = efi_dp_part_node(desc, part);
+		struct efi_partition_info *info = &diskobj->info;
 		struct efi_handler *handler;
 		void *protocol_interface;
 
@@ -454,18 +458,48 @@ static efi_status_t efi_disk_add_dev(
 			goto error;
 		}
 
+		info->revision = EFI_PARTITION_INFO_PROTOCOL_REVISION;
+
+		switch (desc->part_type) {
+#if CONFIG_IS_ENABLED(EFI_PARTITION)
+		case PART_TYPE_EFI:
+			info->type = PARTITION_TYPE_GPT;
+			ret = part_get_gpt_pte(desc, part, &info->info.gpt);
+			if (ret) {
+				log_debug("get PTE for part %d failed %ld\n",
+					  part, ret);
+				goto error;
+			}
+			break;
+#endif
+#if CONFIG_IS_ENABLED(DOS_PARTITION)
+		case PART_TYPE_DOS:
+			info->type = PARTITION_TYPE_MBR;
+
+			/* TODO: implement support for MBR partition types */
+			log_debug("EFI_PARTITION_INFO_PROTOCOL doesn't support MBR\n");
+			break;
+#endif
+		default:
+			info->type = PARTITION_TYPE_OTHER;
+			break;
+		}
+
 		diskobj->dp = efi_dp_append_node(dp_parent, node);
 		efi_free_pool(node);
 		diskobj->media.last_block = part_info->size - 1;
-		if (part_info->bootable & PART_EFI_SYSTEM_PARTITION)
+		if (part_info->bootable & PART_EFI_SYSTEM_PARTITION) {
 			esp_guid = &efi_system_partition_guid;
+			info->system = 1;
+		}
+
 	} else {
 		diskobj->dp = efi_dp_from_part(desc, part);
 		diskobj->media.last_block = desc->lba - 1;
 	}
 
 	/*
-	 * Install the device path and the block IO protocol.
+	 * Install the device path, the block IO and partition info protocols.
 	 *
 	 * InstallMultipleProtocolInterfaces() checks if the device path is
 	 * already installed on an other handle and returns EFI_ALREADY_STARTED
@@ -476,6 +510,7 @@ static efi_status_t efi_disk_add_dev(
 					&handle,
 					&efi_guid_device_path, diskobj->dp,
 					&efi_block_io_guid, &diskobj->ops,
+					&efi_partition_info_guid, &diskobj->info,
 					/*
 					 * esp_guid must be last entry as it
 					 * can be NULL. Its interface is NULL.
Signature · 1 line
2.49.0
#4 lore raw

PATCH v3 4/4 efi_selftest: Add basic partition info check to block io test

In reply to Javier Martinez Canillas

Test the EFI_PARTITION_INFO_PROTOCOL in the existing EFI_BLOCK_IO_PROTOCOL unit test. It is fairly basic, since it only checks that the values of the struct efi_partition_info .revision, .type and .system fields are correct.

It doesn't check the MBR partition record information, because that's not supported by the EFI_PARTITION_INFO_PROTOCOL implementation yet. The test can be extended once the support is implemented, or if the in-memory disk image used for the test is modified to have a GPT partition type instead.

---

Changes in v3:
- Fix build in -next due missing part_efi.h header include (Ilias Apalodimas).
- Rebase on top of -next (Ilias Apalodimas).

Changes in v2:
- Add a test for the EFI partition information protocol (Tom Rini).

 lib/efi_selftest/efi_selftest_block_device.c | 30 ++++++++++++++++++++
 1 file changed, 30 insertions(+)
lib/efi_selftest/efi_selftest_block_device.c +30 -0
index a367e8b89d17..f145e58a267a 100644
@@ -18,6 +18,7 @@
 #include <efi_selftest.h>
 #include "efi_selftest_disk_image.h"
 #include <asm/cache.h>
+#include <part_efi.h>
 
 /* Block size of compressed disk image */
 #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
@@ -29,6 +30,7 @@ static struct efi_boot_services *boottime;
 
 static const efi_guid_t block_io_protocol_guid = EFI_BLOCK_IO_PROTOCOL_GUID;
 static const efi_guid_t guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
+static const efi_guid_t partition_info_guid = EFI_PARTITION_INFO_PROTOCOL_GUID;
 static const efi_guid_t guid_simple_file_system_protocol =
 					EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
 static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_GUID;
@@ -310,6 +312,7 @@ static int execute(void)
 		struct efi_file_system_info info;
 		u16 label[12];
 	} system_info;
+	struct efi_partition_info *part_info;
 	efi_uintn_t buf_size;
 	char buf[16] __aligned(ARCH_DMA_MINALIGN);
 	u32 part1_size;
@@ -375,6 +378,33 @@ static int execute(void)
 			     part1_size - 1);
 		return EFI_ST_FAILURE;
 	}
+
+	/* Open the partition information protocol  */
+	ret = boottime->open_protocol(handle_partition,
+				      &partition_info_guid,
+				      (void **)&part_info, NULL, NULL,
+				      EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+	if (ret != EFI_SUCCESS) {
+		efi_st_error("Failed to open partition information protocol\n");
+		return EFI_ST_FAILURE;
+	}
+	/* Check that cached partition information is the expected */
+	if (part_info->revision != EFI_PARTITION_INFO_PROTOCOL_REVISION) {
+		efi_st_error("Partition info revision %x, expected %x\n",
+			     part_info->revision, EFI_PARTITION_INFO_PROTOCOL_REVISION);
+		return EFI_ST_FAILURE;
+	}
+	if (part_info->type != PARTITION_TYPE_MBR) {
+		efi_st_error("Partition info type %x, expected %x\n",
+			     part_info->type, PARTITION_TYPE_MBR);
+		return EFI_ST_FAILURE;
+	}
+	if (part_info->system != 0) {
+		efi_st_error("Partition info system %x, expected 0\n",
+			     part_info->system);
+		return EFI_ST_FAILURE;
+	}
+
 	/* Open the simple file system protocol */
 	ret = boottime->open_protocol(handle_partition,
 				      &guid_simple_file_system_protocol,
Signature · 1 line
2.49.0