Skip to content

Commit

Permalink
image-hd: set the first usable lba correctly
Browse files Browse the repository at this point in the history
Before this, the begining of the first partition was used. This leaves
some unusable space between the partition table and the first partition
and causes some Windows versions to alter the GPT.

So set the first usable lba correctly: Set it to the first lba after the
last non-partition data before the first partition.
This will either be directly behind the GPT entry array or, for example
after a bootloader that is placed between the partition table and the
first partition.

Signed-off-by: Michael Olbrich <[email protected]>
  • Loading branch information
michaelolbrich committed Nov 8, 2024
1 parent 4bbbb09 commit ee1a5bd
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions image-hd.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ static int hdimage_insert_gpt(struct image *image, struct list_head *partitions)
const char *outfile = imageoutfile(image);
struct gpt_header header;
struct gpt_partition_entry table[GPT_ENTRIES];
unsigned long long smallest_offset = ~0ULL;
unsigned long long smallest_offset = ~0ULL, extra_offset = 0;
struct partition *part;
unsigned i, j;
int ret;
Expand Down Expand Up @@ -526,10 +526,21 @@ static int hdimage_insert_gpt(struct image *image, struct list_head *partitions)

i++;
}
if (smallest_offset == ~0ULL)
smallest_offset = hd->gpt_location + (GPT_SECTORS - 1)*512;
header.first_usable_lba = htole64(smallest_offset / 512);
/*
* Find the last non-partition data before the first partition.
* This can be a bootloader of the GPT partition table.
* */
list_for_each_entry(part, partitions, list) {
unsigned long long end;

if (part->in_partition_table)
continue;

end = part->offset + part->size;
if (end <= smallest_offset && end > extra_offset)
extra_offset = end;
}
header.first_usable_lba = htole64(roundup(extra_offset, 512) / 512);

header.table_crc = htole32(crc32(table, sizeof(table)));

Expand Down

0 comments on commit ee1a5bd

Please sign in to comment.