From 62d4fabc3bfd0d4e3debd20a6a2791c49c8bcab1 Mon Sep 17 00:00:00 2001 From: krw Date: Mon, 19 Jul 2021 14:30:08 +0000 Subject: [PATCH] Revert incorrect tweaks to disk geometry calculations and non '-b' MBR disk initialization. Detected by bluhm@'s ever vigilant regress testing. --- sbin/fdisk/disk.c | 30 +++++++++++++++++------------- sbin/fdisk/mbr.c | 16 +++++++++------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/sbin/fdisk/disk.c b/sbin/fdisk/disk.c index 60a9d3d18a9..ee1111aee47 100644 --- a/sbin/fdisk/disk.c +++ b/sbin/fdisk/disk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: disk.c,v 1.67 2021/07/17 14:16:34 krw Exp $ */ +/* $OpenBSD: disk.c,v 1.68 2021/07/19 14:30:08 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -64,29 +64,33 @@ DISK_open(const char *name, const int oflags) /* Set geometry to use in MBR partitions. */ if (disk.dk_size > 0) { /* -l has set disk size. */ - sz = DL_BLKTOSEC(&dl, disk.dk_size); + sz = disk.dk_size; disk.dk_heads = 1; disk.dk_sectors = 64; + disk.dk_size = DL_BLKTOSEC(&dl, sz); + disk.dk_cylinders = disk.dk_size / disk.dk_sectors; } else if (disk.dk_cylinders > 0) { - /* -c/-h/-c has set disk geometry. */ + /* -c/-h/-s has set disk geometry & therefore size. */ sz = disk.dk_cylinders * disk.dk_heads * disk.dk_sectors; - sz = DL_BLKTOSEC(&dl, sz); + disk.dk_size = DL_BLKTOSEC(&dl, sz); disk.dk_sectors = DL_BLKTOSEC(&dl, disk.dk_sectors); } else { - sz = DL_GETDSIZE(&dl); + disk.dk_sectors = dl.d_nsectors; + disk.dk_cylinders = dl.d_ncylinders; disk.dk_heads = dl.d_ntracks; disk.dk_sectors = dl.d_nsectors; + /* MBR handles only first UINT32_MAX sectors. */ + spc = (uint64_t)disk.dk_heads * disk.dk_sectors; + sz = DL_GETDSIZE(&dl); + if (sz > UINT32_MAX) { + disk.dk_cylinders = UINT32_MAX / spc; + disk.dk_size = disk.dk_cylinders * spc; + } else + disk.dk_size = sz; } - if (sz > UINT32_MAX) - sz = UINT32_MAX; /* MBR knows nothing > UINT32_MAX. */ - - spc = disk.dk_heads * disk.dk_sectors; - disk.dk_cylinders = sz / spc; - disk.dk_size = disk.dk_cylinders * spc; - if (disk.dk_size == 0) - errx(1, "dk_size == 0"); + errx(1, "disk size is 0"); if (disk.dk_bootprt.prt_ns > 0) { ns = disk.dk_bootprt.prt_ns + DL_BLKSPERSEC(&dl) - 1; diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c index a13ccd6ae7d..22a10124290 100644 --- a/sbin/fdisk/mbr.c +++ b/sbin/fdisk/mbr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.c,v 1.89 2021/07/18 21:40:13 krw Exp $ */ +/* $OpenBSD: mbr.c,v 1.90 2021/07/19 14:30:08 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -132,12 +132,14 @@ MBR_init(struct mbr *mbr) /* Fix up start/length fields */ PRT_fix_BN(&mbr->mbr_prt[3], 3); #else - mbr->mbr_prt[0] = disk.dk_bootprt; - PRT_fix_CHS(&mbr->mbr_prt[0]); - mbr->mbr_prt[3].prt_ns += mbr->mbr_prt[3].prt_bs; - mbr->mbr_prt[3].prt_bs = mbr->mbr_prt[0].prt_bs + mbr->mbr_prt[0].prt_ns; - mbr->mbr_prt[3].prt_ns -= mbr->mbr_prt[3].prt_bs; - PRT_fix_CHS(&mbr->mbr_prt[3]); + if (disk.dk_bootprt.prt_ns > 0) { + mbr->mbr_prt[0] = disk.dk_bootprt; + PRT_fix_CHS(&mbr->mbr_prt[0]); + mbr->mbr_prt[3].prt_ns += mbr->mbr_prt[3].prt_bs; + mbr->mbr_prt[3].prt_bs = mbr->mbr_prt[0].prt_bs + mbr->mbr_prt[0].prt_ns; + mbr->mbr_prt[3].prt_ns -= mbr->mbr_prt[3].prt_bs; + PRT_fix_CHS(&mbr->mbr_prt[3]); + } #endif /* Start OpenBSD MBR partition on a power of 2 block number. */ -- 2.20.1