Skip to content

Commit 3eac3da

Browse files
committed
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 <mark@mentovai.com>
1 parent 9e2de85 commit 3eac3da

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/ptgen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static inline void init_utf16(char *str, uint16_t *buf, unsigned bufsize)
306306
static int gen_ptable(uint32_t signature, int nr)
307307
{
308308
struct pte pte[MBR_ENTRY_MAX];
309-
unsigned long start, len, sect = 0;
309+
unsigned long start, len, sect = kb_align ? 1 : sectors;
310310
int i, fd, ret = -1;
311311

312312
memset(pte, 0, sizeof(struct pte) * MBR_ENTRY_MAX);
@@ -321,7 +321,7 @@ static int gen_ptable(uint32_t signature, int nr)
321321
pte[i].active = ((i + 1) == active) ? 0x80 : 0;
322322
pte[i].type = parts[i].type;
323323

324-
start = sect + sectors;
324+
start = sect;
325325
if (parts[i].start != 0) {
326326
if (parts[i].start * 2 < start) {
327327
fprintf(stderr, "Invalid start %ld for partition %d!\n",

0 commit comments

Comments
 (0)