Skip to content

Commit f3b4fd4

Browse files
mike-scottandersson
authored andcommitted
gpt: gpt_load_table_from_partition: fix off-by-one error for num_sectors
When using first_lba and last_lba information from the GPT entry to calculate the number of sectors, we need to add 1 to the result. Imagine if both values were 1. This means there is 1 sector of data and the first and last LBA values are same. However, the current calculation is: num_sectors = last_lba - first_lba. In this case, it would return a value of 0. (Always 1 less sector than it should). Tested on the CDT partition of RB3 Gen2: In the rawprogram3.xml file it has num_partition_sectors="32" (4096 byte sector size). This should result in a binary size of 131072. Pre-patch: $ qdl --storage ufs prog_firehose_ddr.elf read 3/cdt cdt-bad.bin waiting for programmer... partition 6 has not GPT header partition 7 has not GPT header 0 patches applied read "cdt-bad.bin" successfully $ ls -l cdt-bad.bin -rw-r--r-- 1 user user 126976 Sep 16 21:02 cdt-bad.bin Post-patch: $ qdl --storage ufs prog_firehose_ddr.elf read 3/cdt cdt-good.bin Waiting for EDL device waiting for programmer... partition 6 has not GPT header partition 7 has not GPT header 0 patches applied read "cdt-good.bin" successfully $ ls -l cdt-good.bin -rw-r--r-- 1 user user 131072 Sep 16 21:04 cdt-good.bin Signed-off-by: Michael Scott <[email protected]>
1 parent 1b9974c commit f3b4fd4

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

gpt.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@ static int gpt_load_table_from_partition(struct qdl_device *qdl, unsigned int ph
201201
partition->name = strdup(name);
202202
partition->partition = phys_partition;
203203
partition->start_sector = entry->first_lba;
204-
partition->num_sectors = entry->last_lba - entry->first_lba;
204+
/* if first_lba == last_lba there is 1 sector worth of data (IE: add 1 below) */
205+
partition->num_sectors = entry->last_lba - entry->first_lba + 1;
205206

206-
ux_debug(" %3d: %s sector %u to %u\n", i, partition->name,
207-
partition->start_sector, partition->start_sector + partition->num_sectors);
207+
ux_debug(" %3d: %s start sector %u, num sectors %u\n", i, partition->name,
208+
partition->start_sector, partition->num_sectors);
208209

209210
if (gpt_partitions) {
210211
gpt_partitions_last->next = partition;

0 commit comments

Comments
 (0)