From 1429e715fb75362f2497053baa1892204f9199f3 Mon Sep 17 00:00:00 2001 From: krw Date: Sun, 18 Jul 2021 12:41:00 +0000 Subject: [PATCH] Put MBR_parse() invocation inside MBR_read() instead of always following an invocation of MBR_read() with a call to MBR_parse(). No functional change. --- sbin/fdisk/fdisk.c | 7 +++---- sbin/fdisk/mbr.c | 11 +++++++---- sbin/fdisk/mbr.h | 4 ++-- sbin/fdisk/user.c | 13 +++---------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index 40dfb517c5c..22e2448cf4a 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fdisk.c,v 1.124 2021/07/17 21:47:56 krw Exp $ */ +/* $OpenBSD: fdisk.c,v 1.125 2021/07/18 12:41:00 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -163,10 +163,9 @@ main(int argc, char *argv[]) if (pledge("stdio rpath wpath disklabel proc exec", NULL) == -1) err(1, "pledge"); - error = MBR_read(0, &dos_mbr); + error = MBR_read(0, 0, &mbr); if (error) - errx(1, "Can't read sector 0!"); - MBR_parse(&dos_mbr, 0, 0, &mbr); + errx(1, "Can't read MBR!"); /* Get the GPT if present. Either primary or secondary is ok. */ efi = MBR_protective_mbr(&mbr); diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c index 7bc0c2f0476..812b098d5b7 100644 --- a/sbin/fdisk/mbr.c +++ b/sbin/fdisk/mbr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.c,v 1.87 2021/07/17 14:16:34 krw Exp $ */ +/* $OpenBSD: mbr.c,v 1.88 2021/07/18 12:41:00 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -202,17 +202,20 @@ MBR_print(const struct mbr *mbr, const char *units) } int -MBR_read(const uint64_t sector, struct dos_mbr *dos_mbr) +MBR_read(const uint64_t lba_self, const uint64_t lba_firstembr, struct mbr *mbr) { + struct dos_mbr dos_mbr; char *secbuf; - secbuf = DISK_readsector(sector); + secbuf = DISK_readsector(lba_self); if (secbuf == NULL) return -1; - memcpy(dos_mbr, secbuf, sizeof(*dos_mbr)); + memcpy(&dos_mbr, secbuf, sizeof(dos_mbr)); free(secbuf); + MBR_parse(&dos_mbr, lba_self, lba_firstembr, mbr); + return 0; } diff --git a/sbin/fdisk/mbr.h b/sbin/fdisk/mbr.h index 941ee3a5974..d097e9c1d3b 100644 --- a/sbin/fdisk/mbr.h +++ b/sbin/fdisk/mbr.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.h,v 1.35 2021/07/13 15:03:34 krw Exp $ */ +/* $OpenBSD: mbr.h,v 1.36 2021/07/18 12:41:00 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -35,7 +35,7 @@ void MBR_parse(const struct dos_mbr *, const uint64_t, void MBR_make(struct mbr *, struct dos_mbr *); void MBR_init(struct mbr *); void MBR_init_GPT(struct mbr *); -int MBR_read(const uint64_t, struct dos_mbr *); +int MBR_read(const uint64_t, const uint64_t, struct mbr *); int MBR_write(const uint64_t, const struct dos_mbr *); int MBR_protective_mbr(struct mbr *); diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c index 5b2202e2838..af5ac347df0 100644 --- a/sbin/fdisk/user.c +++ b/sbin/fdisk/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.63 2021/07/13 15:03:34 krw Exp $ */ +/* $OpenBSD: user.c,v 1.64 2021/07/18 12:41:00 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -61,7 +61,6 @@ void ask_cmd(char **, char **); void USER_edit(const uint64_t lba_self, const uint64_t lba_firstembr) { - struct dos_mbr dos_mbr; struct mbr mbr; char *cmd, *args; int i, st, efi, error; @@ -70,14 +69,10 @@ USER_edit(const uint64_t lba_self, const uint64_t lba_firstembr) /* One level deeper */ editlevel += 1; - /* Read MBR & partition */ - error = MBR_read(lba_self, &dos_mbr); + error = MBR_read(lba_self, lba_firstembr, &mbr); if (error == -1) goto done; - /* Parse the sucker */ - MBR_parse(&dos_mbr, lba_self, lba_firstembr, &mbr); - if (editlevel == 1) { memset(&gh, 0, sizeof(gh)); memset(&gp, 0, sizeof(gp)); @@ -143,7 +138,6 @@ done: void USER_print_disk(const int verbosity) { - struct dos_mbr dos_mbr; struct mbr mbr; uint64_t lba_self, lba_firstembr; int i, efi, error; @@ -151,10 +145,9 @@ USER_print_disk(const int verbosity) lba_self = lba_firstembr = 0; do { - error = MBR_read(lba_self, &dos_mbr); + error = MBR_read(lba_self, lba_firstembr, &mbr); if (error == -1) break; - MBR_parse(&dos_mbr, lba_self, lba_firstembr, &mbr); if (lba_self == 0) { efi = MBR_protective_mbr(&mbr); if (efi == -1) { -- 2.20.1