Page MenuHomeFreeBSD

mkimg: Respect gpt first usable LBA
Needs ReviewPublic

Authored by manu on May 2 2017, 9:34 AM.

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 OK
Unit
No Unit Test Coverage
Build Status
Buildable 9167
Build 9608: arc lint + arc unit

Event Timeline

manu created this revision.May 2 2017, 9:34 AM
marcel added inline comments.May 2 2017, 6:32 PM
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.

manu updated this revision to Diff 27974.May 3 2017, 12:06 PM

Move logic into gpt_tblsz

manu marked 2 inline comments as done.May 3 2017, 12:06 PM
marcel added inline comments.May 3 2017, 2:57 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 ...

manu updated this revision to Diff 28194.May 10 2017, 11:41 AM

Update to use logical sector size and not physical ones.

manu marked an inline comment as done.May 10 2017, 11:42 AM
manu added inline comments.
usr.bin/mkimg/gpt.c
142

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