From: krw Date: Wed, 16 Jun 2021 15:40:47 +0000 (+0000) Subject: Ensure that the '-i' command line option and the 'reinit [mbr]' X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c94b0391c2425cf229f3d2fc83575cde3a9c2897;p=openbsd Ensure that the '-i' command line option and the 'reinit [mbr]' editing commnd completely remove any existing on-disk GPT header when writing the MBR. '-ig'/'reinit gpt' must be used to create GPT format. Previous interactions between '-i', '-b' and overly clever 'reinit' produced surprising, inconsistent and incorrect behaviour. ok deraadt@ kettenis@ --- diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index 542035dd986..fe7c21f871d 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.115 2021/06/14 17:34:06 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.116 2021/06/16 15:40:47 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -45,9 +45,9 @@ int Xreinit(char *args, struct mbr *mbr) { struct dos_mbr dos_mbr; - int efi, dogpt; + int dogpt; - efi = MBR_protective_mbr(mbr); + dogpt = 0; if (strncasecmp(args, "gpt", 3) == 0) dogpt = 1; @@ -56,10 +56,7 @@ Xreinit(char *args, struct mbr *mbr) else if (strlen(args) > 0) { printf("Unrecognized modifier '%s'\n", args); return (CMD_CONT); - } else if (efi != -1) - dogpt = 1; - else - dogpt = 0; + } MBR_make(&initial_mbr, &dos_mbr); MBR_parse(&dos_mbr, mbr->offset, mbr->reloffset, mbr); @@ -455,8 +452,7 @@ Xwrite(char *args, struct mbr *mbr) return (CMD_CONT); } } else { - /* Ensure any on-disk GPT headers are zeroed. */ - MBR_zapgpt(&dos_mbr, DL_GETDSIZE(&dl) - 1); + GPT_zap_headers(); } /* Refresh in memory copy to reflect what was just written. */ diff --git a/sbin/fdisk/fdisk.8 b/sbin/fdisk/fdisk.8 index 45bc95ae954..fb083588556 100644 --- a/sbin/fdisk/fdisk.8 +++ b/sbin/fdisk/fdisk.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: fdisk.8,v 1.96 2021/01/30 15:15:20 naddy Exp $ +.\" $OpenBSD: fdisk.8,v 1.97 2021/06/16 15:40:47 krw Exp $ .\" .\" .\" Copyright (c) 1997 Tobias Weingartner @@ -15,7 +15,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: January 30 2021 $ +.Dd $Mdocdate: June 16 2021 $ .Dt FDISK 8 .Os .Sh NAME @@ -371,19 +371,11 @@ Display this manual page. .It Cm reinit Op Cm gpt | Cm mbr Initialize the currently selected, in-memory copy of the boot block. +.Pp +By default an MBR is initialized. If .Cm gpt is specified a protective MBR and a GPT are initialized. -If -.Cm mbr -is specified only an MBR is initialized. -If neither -.Cm mbr -nor -.Cm gpt -are specified then a protective MBR and a GPT are initialized if a protective -MBR already exists. -Otherwise only an MBR is initialized. .It Cm disk Display the current drive geometry that .Nm diff --git a/sbin/fdisk/gpt.c b/sbin/fdisk/gpt.c index f123d876a48..b54e27f7d08 100644 --- a/sbin/fdisk/gpt.c +++ b/sbin/fdisk/gpt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gpt.c,v 1.29 2021/06/14 12:46:47 krw Exp $ */ +/* $OpenBSD: gpt.c,v 1.30 2021/06/16 15:40:47 krw Exp $ */ /* * Copyright (c) 2015 Markus Muller * Copyright (c) 2015 Kenneth R Westerback @@ -451,6 +451,35 @@ GPT_init(void) return rslt; } +void +GPT_zap_headers(void) +{ + char *secbuf; + uint64_t sig; + + secbuf = DISK_readsector(GPTSECTOR); + if (secbuf == NULL) + return; + + memcpy(&sig, secbuf, sizeof(sig)); + if (letoh64(sig) == GPTSIGNATURE) { + memset(secbuf, 0, dl.d_secsize); + DISK_writesector(secbuf, GPTSECTOR); + } + free(secbuf); + + secbuf = DISK_readsector(DL_GETDSIZE(&dl) - 1); + if (secbuf == NULL) + return; + + memcpy(&sig, secbuf, sizeof(sig)); + if (letoh64(sig) == GPTSIGNATURE) { + memset(secbuf, 0, dl.d_secsize); + DISK_writesector(secbuf, DL_GETDSIZE(&dl) - 1); + } + free(secbuf); +} + int GPT_write(void) { diff --git a/sbin/fdisk/gpt.h b/sbin/fdisk/gpt.h index 93a8e4796f7..f349b81ba26 100644 --- a/sbin/fdisk/gpt.h +++ b/sbin/fdisk/gpt.h @@ -1,4 +1,4 @@ -/* $OpenBSD: gpt.h,v 1.11 2021/06/12 17:19:13 krw Exp $ */ +/* $OpenBSD: gpt.h,v 1.12 2021/06/16 15:40:47 krw Exp $ */ /* * Copyright (c) 2015 Markus Muller * Copyright (c) 2015 Kenneth R Westerback @@ -22,6 +22,7 @@ int GPT_get_lba_end(unsigned int); int GPT_init(void); int GPT_write(void); +void GPT_zap_headers(void); void GPT_print(char *, int); void GPT_print_part(int, char *, int); void GPT_print_parthdr(int); diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c index 72382043e98..27fe2e4eff1 100644 --- a/sbin/fdisk/mbr.c +++ b/sbin/fdisk/mbr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.c,v 1.75 2021/06/10 15:30:49 krw Exp $ */ +/* $OpenBSD: mbr.c,v 1.76 2021/06/16 15:40:47 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -247,50 +247,6 @@ MBR_write(off_t where, struct dos_mbr *dos_mbr) return (0); } -/* - * If *dos_mbr has a 0xee or 0xef partition, nothing needs to happen. If no - * such partition is present but the first or last sector on the disk has a - * GPT, zero the GPT to ensure the MBR takes priority and fewer BIOSes get - * confused. - */ -void -MBR_zapgpt(struct dos_mbr *dos_mbr, uint64_t lastsec) -{ - struct dos_partition dos_parts[NDOSPART]; - char *secbuf; - uint64_t sig; - int i; - - memcpy(dos_parts, dos_mbr->dmbr_parts, sizeof(dos_parts)); - - for (i = 0; i < NDOSPART; i++) - if ((dos_parts[i].dp_typ == DOSPTYP_EFI) || - (dos_parts[i].dp_typ == DOSPTYP_EFISYS)) - return; - - secbuf = DISK_readsector(GPTSECTOR); - if (secbuf == NULL) - return; - - memcpy(&sig, secbuf, sizeof(sig)); - if (letoh64(sig) == GPTSIGNATURE) { - memset(secbuf, 0, sizeof(sig)); - DISK_writesector(secbuf, GPTSECTOR); - } - free(secbuf); - - secbuf = DISK_readsector(lastsec); - if (secbuf == NULL) - return; - - memcpy(&sig, secbuf, sizeof(sig)); - if (letoh64(sig) == GPTSIGNATURE) { - memset(secbuf, 0, sizeof(sig)); - DISK_writesector(secbuf, lastsec); - } - free(secbuf); -} - /* * Return the index into dp[] of the EFI GPT (0xEE) partition, or -1 if no such * partition exists. diff --git a/sbin/fdisk/mbr.h b/sbin/fdisk/mbr.h index 29b704c6616..ccc55418bb4 100644 --- a/sbin/fdisk/mbr.h +++ b/sbin/fdisk/mbr.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.h,v 1.30 2021/05/20 14:27:14 krw Exp $ */ +/* $OpenBSD: mbr.h,v 1.31 2021/06/16 15:40:47 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -36,7 +36,6 @@ void MBR_init(struct mbr *); void MBR_init_GPT(struct mbr *); int MBR_read(off_t, struct dos_mbr *); int MBR_write(off_t, struct dos_mbr *); -void MBR_zapgpt(struct dos_mbr *, uint64_t); int MBR_protective_mbr(struct mbr *); #endif /* _MBR_H */