Page MenuHomeFreeBSD

mkimg: Respect gpt first usable LBA
Needs ReviewPublic

Authored by manu on May 2 2017, 9:34 AM.
Tags
None
Referenced Files
Unknown Object (File)
Jan 29 2024, 4:13 AM
Unknown Object (File)
Jan 8 2024, 2:48 AM
Unknown Object (File)
Dec 31 2023, 1:40 PM
Unknown Object (File)
Dec 20 2023, 7:35 AM
Unknown Object (File)
Dec 11 2023, 2:45 AM
Unknown Object (File)
Nov 29 2023, 9:05 PM
Unknown Object (File)
Nov 29 2023, 4:48 PM
Unknown Object (File)
Nov 28 2023, 5:39 AM
Subscribers

Details

Reviewers
marcel
bapt
Summary

The UEFI specification say that the minimum size for the GPT partition
entry array is 16384 bytes. This place the first usable LBA at 34 for
a 512 bytes sector drive and at 6 for a 4K one for example.
Add this minimum size to the calculation of the gpt metadata size.

Update the tests files for the new gpt calculation and while here fix
the tests Makefile to use the correct script.

Sponsored-by: Gandi.net

Test Plan

Create some partition with different sector size and check at which lba they where placed.

Diff Detail

Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 9167
Build 9608: arc lint + arc unit

Event Timeline

usr.bin/mkimg/gpt.c
143

Please move this underneath the includes. It helps to have all the defines in 1 place and not scattered within the file.

148

Why not change gpt_tblsz()? There's no real downside to it and makes sure we're consistent.

Please also avoid using min/MIN and max/MAX. They're non-portable and typically tend to break compiling mkimg(1) on different OSes.

Move logic into gpt_tblsz

manu marked 2 inline comments as done.May 3 2017, 12:06 PM
usr.bin/mkimg/gpt.c
142

Now that the complete logic is in a single function, it's easy to see that we use secsz to compute the number of sectors given the number of partitions but we use blksz to compute the minimum number of sectors.

If mkimg is run with -P 4K and -S 512, then we would obviously not get the desired result of having 32 sectors for the GPT entries.

Both should use secsz, because the computation needs the logical sector size.

An alternative approach is:

bytesize = nparts * sizeof(struct gpt_ent);
if (bytesize < GPT_PARTITION_ARRAY_MIN_SIZE)
    bytesize = GPT_PARTITION_ARRAY_MIN_SIZE;
sectors = (bytesize + secz - 1) / secsz;
return (sectors);

This makes sure that if GPT_PARTITION_ARRAY_MIN_SIZE is not a multiple of the sector size (unlikely, but possible because secsz is user input), then the logic is still correct. Consider an invocation like: mkimg -P 64K -S 64K ...

Update to use logical sector size and not physical ones.

manu added inline comments.
usr.bin/mkimg/gpt.c
142

Yes sorry about that, I've took your solution.

Is there a reason this hasn't been committed?