gpt_fill_header() hard-codes 32 blocks for the GPT Partition Entry
Array when computing first_usable_lba, and 34 (32 PTE blocks + 1
primary header block + 1 for the last LBA offset) for
last_usable_lba. These values are only correct when the block size
is 512 bytes and the default 16 KB Partition Entry Array size is used.

Compute the number of blocks required for the PTE based on device's
actual block size and use this value in place of the hard-coded
constants, so the first/last usable LBA are calculated correctly.

Signed-off-by: Sumit Singh Dhami <[email protected]>
---
 disk/part_efi.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/disk/part_efi.c b/disk/part_efi.c
index d8b17ec2e91..99f5df1c37d 100644
--- a/disk/part_efi.c
+++ b/disk/part_efi.c
@@ -604,16 +604,19 @@ static uint32_t partition_entries_offset(struct blk_desc 
*desc)
 int gpt_fill_header(struct blk_desc *desc, gpt_header *gpt_h, char *str_guid,
                    int parts_count)
 {
+       u32 pte_blk_cnt = BLOCK_CNT(GPT_ENTRY_NUMBERS * sizeof(gpt_entry),
+                                   desc);
+
        gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE_UBOOT);
        gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
        gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
        gpt_h->my_lba = cpu_to_le64(1);
        gpt_h->alternate_lba = cpu_to_le64(desc->lba - 1);
-       gpt_h->last_usable_lba = cpu_to_le64(desc->lba - 34);
+       gpt_h->last_usable_lba = cpu_to_le64(desc->lba - pte_blk_cnt - 2);
        gpt_h->partition_entry_lba =
                cpu_to_le64(partition_entries_offset(desc));
        gpt_h->first_usable_lba =
-               cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 32);
+               cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 
pte_blk_cnt);
        gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
        gpt_h->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
        gpt_h->header_crc32 = 0;
-- 
2.55.0

Reply via email to