From e44ba83883ba63df9692125ecd899b026e21e9fd Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Wed, 5 Jul 2023 13:56:28 -0400 Subject: [PATCH] ptgen: don't insert CHS gap head in MBR partition tables When ptgen is aligning partitions on a specific kB boundary (-l option; OpenWrt uses 256kB for x86, but other, larger values for some other architectures) and creating an MBR-type partition table (-g option absent), it was inserting an extra head's worth of gap between partitions. For example, in the x86 case, where OpenWrt builds an image using -h 16 -s 63, consisting of 16MB and 104MB partitions by default, and 256kB alignment, ptgen produced: Device Boot Start End Sectors Size Id Type img1 * 512 33279 32768 16M 83 Linux img2 33792 246783 212992 104M 83 Linux Notice the 512-sector gap between the end of the first partition and the start of the second, despite the fact that sector 33280 (which immediately follows the first partition) would have been 256kB-aligned. This occurred because ptgen inserted a gap equal to one CHS head before 256kB-aligning the second partition. In this 63 sector/head geometry requested when OpenWrt builds an image, this gap results in 32,256 bytes of waste, which after kB-aligning, becomes 256kB of waste. The expected layout, which is produced after this patch: Device Boot Start End Sectors Size Id Type img1 * 512 33279 32768 16M 83 Linux img2 33280 246271 212992 104M 83 Linux This gapless layout more closely resembles the layout when ptgen is used in GPT (-g) mode. The 1-head gap between partitions in head-aligning mode (no -l) also appears to be unnecessary. The sole function of this gap seems to be to force the first partition to begin at the first sector of the second head rather than the first head, which would have resulted in a collision because the MBR partition table resides there. With this change, a different approach is taken, in which the first partition's position is set more directly to the start of the first head, without affecting subsequent partitions. This eliminates the 32,256-byte gap between partitions in this mode. Note that when head-aligning, each partition's size is padded as needed so as to consume an entire head, so any subsequent partition will still be head-aligned. In OpenWrt, only gemini/dlink_dns-313 and layerscape sdcard images use ptgen to produce a non-kB-aligned MBR partition table, and it doesn't appear that either will be negatively impacted by this change. Signed-off-by: Mark Mentovai --- src/ptgen.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ptgen.c b/src/ptgen.c index d480908..af17936 100644 --- a/src/ptgen.c +++ b/src/ptgen.c @@ -329,7 +329,7 @@ static inline void init_utf16(char *str, uint16_t *buf, unsigned bufsize) static int gen_ptable(uint32_t signature, int nr) { struct pte pte[MBR_ENTRY_MAX]; - unsigned long long start, len, sect = 0; + unsigned long long start, len, sect = kb_align ? 1 : sectors; int i, fd, ret = -1; memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX); @@ -344,7 +344,7 @@ static int gen_ptable(uint32_t signature, int nr) pte[i].active = ((i + 1) == active) ? 0x80 : 0; pte[i].type = parts[i].type; - start = sect + sectors; + start = sect; if (parts[i].start != 0) { if (parts[i].start * 2 < start) { fprintf(stderr, "Invalid start %lld for partition %d!\n",