diff mbox series

[v4,13/14] efi_loader: Index the GPT entries directly in tcg2_measure_gpt_data()

Message ID 9dba21af-23eb-436e-8421-43e5d834772f@gmail.com
State Deferred
Delegated to: Tom Rini
Headers show
Series block, disk: Check block sizes and partition tables of devices | expand

Commit Message

Ana Bobillo Nov. 4, 2026, 12:51 p.m. UTC
From: Ana Bobillo <anabobillo3@gmail.com>

tcg2_measure_gpt_data() now measures the GPT that part_get_gpt() gives,
whose entries are always sizeof(gpt_entry) bytes, as
validate_gpt_header() refuses any other size. So index them as an array
rather than stepping by sizeof_partition_entry, and read
num_partition_entries with le32_to_cpu(), as the rest of the GPT code
does.

No functional change on a little-endian machine.

Suggested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Ana Bobillo <anabobillo3@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
(no changes since v3)

Changes in v3:
- New patch (Simon)

 lib/efi_loader/efi_tcg2.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
diff mbox series

Patch

diff --git a/lib/efi_loader/efi_tcg2.c b/lib/efi_loader/efi_tcg2.c
index 97fce462138..4045af9afa3 100644
--- a/lib/efi_loader/efi_tcg2.c
+++ b/lib/efi_loader/efi_tcg2.c
@@ -1168,8 +1168,8 @@  tcg2_measure_gpt_data(struct udevice *dev,
 	efi_guid_t null_guid = NULL_GUID;
 	gpt_header *gpt_h;
 	gpt_entry *entry = NULL;
-	gpt_entry *gpt_e;
 	u32 num_of_valid_entry = 0;
+	u32 num_entries;
 	u32 event_size;
 	u32 i;
 
@@ -1235,17 +1235,15 @@  tcg2_measure_gpt_data(struct udevice *dev,
 	}
 
 	/* count valid GPT entry */
-	gpt_e = entry;
-	for (i = 0; i < gpt_h->num_partition_entries; i++) {
-		if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
+	num_entries = le32_to_cpu(gpt_h->num_partition_entries);
+	for (i = 0; i < num_entries; i++) {
+		if (guidcmp(&null_guid, &entry[i].partition_type_guid))
 			num_of_valid_entry++;
-
-		gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
 	}
 
 	/* prepare event data for measurement */
 	event_size = sizeof(struct efi_gpt_data) +
-		(num_of_valid_entry * gpt_h->sizeof_partition_entry);
+		(num_of_valid_entry * sizeof(gpt_entry));
 	event = calloc(1, event_size);
 	if (!event) {
 		ret = EFI_OUT_OF_RESOURCES;
@@ -1255,17 +1253,13 @@  tcg2_measure_gpt_data(struct udevice *dev,
 	put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
 
 	/* copy valid GPT entry */
-	gpt_e = entry;
 	num_of_valid_entry = 0;
-	for (i = 0; i < gpt_h->num_partition_entries; i++) {
-		if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
-			memcpy((u8 *)event->partitions +
-			       (num_of_valid_entry * gpt_h->sizeof_partition_entry),
-			       gpt_e, gpt_h->sizeof_partition_entry);
+	for (i = 0; i < num_entries; i++) {
+		if (guidcmp(&null_guid, &entry[i].partition_type_guid)) {
+			memcpy(&event->partitions[num_of_valid_entry],
+			       &entry[i], sizeof(gpt_entry));
 			num_of_valid_entry++;
 		}
-
-		gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
 	}
 
 	ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);