From 3d40667ede5978c665cc4fbff0aaf3b92c16a5da Mon Sep 17 00:00:00 2001 From: krw Date: Tue, 26 Jul 2022 14:30:37 +0000 Subject: [PATCH] Allow editing of an MBR of all zeros. Fallout from regress failure spotted by anton@. --- sbin/fdisk/mbr.c | 13 +++++++++++-- sbin/fdisk/mbr.h | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c index 2ef6783110f..6e6b6c58caf 100644 --- a/sbin/fdisk/mbr.c +++ b/sbin/fdisk/mbr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.c,v 1.120 2022/07/25 17:45:16 krw Exp $ */ +/* $OpenBSD: mbr.c,v 1.121 2022/07/26 14:30:37 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -105,7 +105,13 @@ dos_mbr_to_mbr(const struct dos_mbr *dmbr, const uint64_t lba_self, const uint64_t lba_firstembr, struct mbr *mbr) { struct dos_partition dos_parts[NDOSPART]; - int i; + uint8_t *p; + unsigned int i; + + p = (uint8_t *)dmbr; + mbr->mbr_dmbrzeros = 0; + for (i = 0; i < sizeof(struct dos_mbr) && *p == 0; i++, p++) + mbr->mbr_dmbrzeros++; memcpy(mbr->mbr_code, dmbr->dmbr_boot, sizeof(mbr->mbr_code)); mbr->mbr_lba_self = lba_self; @@ -188,6 +194,9 @@ MBR_valid_prt(const struct mbr *mbr) unsigned int i, nprt; unsigned char id; + if (mbr->mbr_dmbrzeros == sizeof(struct dos_mbr)) + return 1; /* All zeros struct dos_mbr is editable. */ + nprt = 0; for (i = 0; i < NDOSPART; i++) { bs = mbr->mbr_prt[i].prt_bs; diff --git a/sbin/fdisk/mbr.h b/sbin/fdisk/mbr.h index 0c56a3db4e0..3aee1dabdb5 100644 --- a/sbin/fdisk/mbr.h +++ b/sbin/fdisk/mbr.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.h,v 1.43 2022/07/25 17:45:16 krw Exp $ */ +/* $OpenBSD: mbr.h,v 1.44 2022/07/26 14:30:37 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -22,6 +22,7 @@ struct mbr { unsigned char mbr_code[DOSPARTOFF]; struct prt mbr_prt[NDOSPART]; uint16_t mbr_signature; + unsigned int mbr_dmbrzeros; }; extern struct dos_mbr default_dmbr; -- 2.20.1