Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/mkimg/gpt.c
Show All 36 Lines | |||||
#include <gpt.h> | #include <gpt.h> | ||||
#include <mbr.h> | #include <mbr.h> | ||||
#include "endian.h" | #include "endian.h" | ||||
#include "image.h" | #include "image.h" | ||||
#include "mkimg.h" | #include "mkimg.h" | ||||
#include "scheme.h" | #include "scheme.h" | ||||
#define GPT_PARTITION_ARRAY_MIN_SIZE 16384 | |||||
static mkimg_uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI; | static mkimg_uuid_t gpt_uuid_efi = GPT_ENT_TYPE_EFI; | ||||
static mkimg_uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; | static mkimg_uuid_t gpt_uuid_freebsd = GPT_ENT_TYPE_FREEBSD; | ||||
static mkimg_uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; | static mkimg_uuid_t gpt_uuid_freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT; | ||||
static mkimg_uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; | static mkimg_uuid_t gpt_uuid_freebsd_nandfs = GPT_ENT_TYPE_FREEBSD_NANDFS; | ||||
static mkimg_uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; | static mkimg_uuid_t gpt_uuid_freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP; | ||||
static mkimg_uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; | static mkimg_uuid_t gpt_uuid_freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS; | ||||
static mkimg_uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; | static mkimg_uuid_t gpt_uuid_freebsd_vinum = GPT_ENT_TYPE_FREEBSD_VINUM; | ||||
static mkimg_uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; | static mkimg_uuid_t gpt_uuid_freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS; | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | crc32(const void *buf, size_t sz) | ||||
while (sz--) | while (sz--) | ||||
crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8); | crc = crc32_tab[(crc ^ *p++) & 0xff] ^ (crc >> 8); | ||||
return (crc ^ ~0U); | return (crc ^ ~0U); | ||||
} | } | ||||
static u_int | static u_int | ||||
gpt_tblsz(void) | gpt_tblsz(void) | ||||
{ | { | ||||
u_int ents; | u_int bytesize, sectors; | ||||
ents = secsz / sizeof(struct gpt_ent); | bytesize = nparts * sizeof(struct gpt_ent); | ||||
return ((nparts + ents - 1) / ents); | if (bytesize < GPT_PARTITION_ARRAY_MIN_SIZE) | ||||
bytesize = GPT_PARTITION_ARRAY_MIN_SIZE; | |||||
sectors = (bytesize + secsz - 1) / secsz; | |||||
return (sectors); | |||||
} | } | ||||
marcel: Now that the complete logic is in a single function, it's easy to see that we use secsz to… | |||||
Not Done Inline ActionsYes sorry about that, I've took your solution. manu: Yes sorry about that, I've took your solution. | |||||
static lba_t | static lba_t | ||||
Done Inline ActionsPlease move this underneath the includes. It helps to have all the defines in 1 place and not scattered within the file. marcel: Please move this underneath the includes. It helps to have all the defines in 1 place and not… | |||||
gpt_metadata(u_int where, lba_t blk) | gpt_metadata(u_int where, lba_t blk) | ||||
{ | { | ||||
if (where == SCHEME_META_IMG_START || where == SCHEME_META_IMG_END) { | if (where == SCHEME_META_IMG_START || where == SCHEME_META_IMG_END) { | ||||
blk += gpt_tblsz(); | blk += gpt_tblsz(); | ||||
Done Inline ActionsWhy 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. marcel: Why not change gpt_tblsz()? There's no real downside to it and makes sure we're consistent. | |||||
blk += (where == SCHEME_META_IMG_START) ? 2 : 1; | blk += (where == SCHEME_META_IMG_START) ? 2 : 1; | ||||
} | } | ||||
return (round_block(blk)); | return (round_block(blk)); | ||||
} | } | ||||
static int | static int | ||||
gpt_write_pmbr(lba_t blks, void *bootcode) | gpt_write_pmbr(lba_t blks, void *bootcode) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 139 Lines • Show Last 20 Lines |
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:
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 ...