From 199eafea20905fc7cb23d788b7222b593e25ece4 Mon Sep 17 00:00:00 2001 From: krw Date: Thu, 15 Jul 2021 21:58:02 +0000 Subject: [PATCH] No need for separate b_sectors, b_offset and b_type variables. We have struct prt to hold this info in one place. Construct a struct prt to hold the boot partition information and stash it in the struct disk for both MBR and GPT to access as needed. Move the blocks to sectors conversions into DISK_open() with all of its geometry friends. No intentional functional change. --- sbin/fdisk/cmd.c | 6 +++--- sbin/fdisk/disk.c | 12 ++++++++++-- sbin/fdisk/disk.h | 3 ++- sbin/fdisk/fdisk.c | 46 ++++++++++++++++------------------------------ sbin/fdisk/gpt.c | 16 ++++++++-------- sbin/fdisk/gpt.h | 4 ++-- sbin/fdisk/mbr.c | 22 ++++++++-------------- sbin/fdisk/misc.c | 4 ++-- sbin/fdisk/part.c | 4 ++-- 9 files changed, 53 insertions(+), 64 deletions(-) diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index 09db543b32f..ccbb51e8d94 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.127 2021/07/13 15:03:34 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.128 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -27,9 +27,9 @@ #include #include +#include "part.h" #include "disk.h" #include "misc.h" -#include "part.h" #include "mbr.h" #include "gpt.h" #include "user.h" @@ -70,7 +70,7 @@ Xreinit(char *args, struct mbr *mbr) if (dogpt) { MBR_init_GPT(mbr); - GPT_init(GHANDGP, 0); + GPT_init(GHANDGP); GPT_print("s", TERSE); } else { memset(&gh, 0, sizeof(gh)); diff --git a/sbin/fdisk/disk.c b/sbin/fdisk/disk.c index d742941505e..1f8d175b7ff 100644 --- a/sbin/fdisk/disk.c +++ b/sbin/fdisk/disk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: disk.c,v 1.64 2021/07/15 21:23:54 krw Exp $ */ +/* $OpenBSD: disk.c,v 1.65 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -32,6 +32,7 @@ #include #include +#include "part.h" #include "disk.h" #include "misc.h" @@ -42,7 +43,7 @@ void DISK_open(const char *name, const int oflags) { struct stat st; - uint64_t sz, spc; + uint64_t ns, bs, sz, spc; disk.dk_name = strdup(name); if (disk.dk_name == NULL) @@ -86,6 +87,13 @@ DISK_open(const char *name, const int oflags) if (disk.dk_size == 0) errx(1, "dk_size == 0"); + + if (disk.dk_bootprt.prt_ns > 0) { + ns = disk.dk_bootprt.prt_ns + DL_BLKSPERSEC(&dl) - 1; + bs = disk.dk_bootprt.prt_bs + DL_BLKSPERSEC(&dl) - 1; + disk.dk_bootprt.prt_ns = DL_BLKTOSEC(&dl, ns); + disk.dk_bootprt.prt_bs = DL_BLKTOSEC(&dl, bs); + } } /* diff --git a/sbin/fdisk/disk.h b/sbin/fdisk/disk.h index fe6bee275a4..bc1e11b9a9f 100644 --- a/sbin/fdisk/disk.h +++ b/sbin/fdisk/disk.h @@ -1,4 +1,4 @@ -/* $OpenBSD: disk.h,v 1.28 2021/07/15 21:23:54 krw Exp $ */ +/* $OpenBSD: disk.h,v 1.29 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -20,6 +20,7 @@ #define _DISK_H struct disk { + struct prt dk_bootprt; char *dk_name; int dk_fd; uint32_t dk_cylinders; diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index d90fb9daf10..063bdda104b 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fdisk.c,v 1.120 2021/07/15 21:23:54 krw Exp $ */ +/* $OpenBSD: fdisk.c,v 1.121 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -29,8 +29,8 @@ #include #include -#include "disk.h" #include "part.h" +#include "disk.h" #include "mbr.h" #include "misc.h" #include "cmd.h" @@ -42,11 +42,9 @@ static unsigned char builtin_mbr[] = { #include "mbrcode.h" }; -uint32_t b_sectors, b_offset; -uint8_t b_type; int A_flag, y_flag; -void parse_b(const char *, uint32_t *, uint32_t *, uint8_t *); +void parse_bootprt(const char *); static void usage(void) @@ -124,7 +122,7 @@ main(int argc, char *argv[]) g_flag = 1; break; case 'b': - parse_b(optarg, &b_sectors, &b_offset, &b_type); + parse_bootprt(optarg); break; case 'l': disk.dk_size = strtonum(optarg, BLOCKALIGNMENT, UINT32_MAX, &errstr); @@ -148,8 +146,7 @@ main(int argc, char *argv[]) /* Argument checking */ if (argc != 1 || (i_flag && u_flag) || - (i_flag == 0 && g_flag) || - (b_sectors && !(i_flag || A_flag))) + (i_flag == 0 && g_flag)) usage(); if ((disk.dk_cylinders || disk.dk_heads || disk.dk_sectors) && @@ -157,15 +154,11 @@ main(int argc, char *argv[]) usage(); DISK_open(argv[0], oflags); - - bps = DL_BLKSPERSEC(&dl); - if (b_sectors > 0) { - if (b_sectors % bps != 0) - b_sectors += bps - b_sectors % bps; - if (b_offset % bps != 0) - b_offset += bps - b_offset % bps; - b_sectors = DL_BLKTOSEC(&dl, b_sectors); - b_offset = DL_BLKTOSEC(&dl, b_offset); + if (oflags == O_RDONLY) { + if (pledge("stdio", NULL) == -1) + err(1, "pledge"); + USER_print_disk(verbosity); + goto done; } /* "proc exec" for man page display */ @@ -182,13 +175,6 @@ main(int argc, char *argv[]) if (efi != -1) GPT_read(ANYGPT); - if (oflags == O_RDONLY) { - if (pledge("stdio", NULL) == -1) - err(1, "pledge"); - USER_print_disk(verbosity); - goto done; - } - /* Create initial/default MBR. */ if (mbrfile == NULL) { memcpy(&dos_mbr, builtin_mbr, sizeof(dos_mbr)); @@ -216,13 +202,13 @@ main(int argc, char *argv[]) errx(1, "-A requires a valid GPT"); else { initial_mbr = mbr; /* Keep current MBR. */ - GPT_init(GPONLY, b_sectors); + GPT_init(GPONLY); query = "Do you wish to write new GPT?"; } } else if (i_flag) { if (g_flag) { MBR_init_GPT(&initial_mbr); - GPT_init(GHANDGP, b_sectors); + GPT_init(GHANDGP); query = "Do you wish to write new GPT?"; } else { memset(&gh, 0, sizeof(gh)); @@ -248,7 +234,7 @@ done: } void -parse_b(const char *arg, uint32_t *blocks, uint32_t *offset, uint8_t *type) +parse_bootprt(const char *arg) { const char *errstr; char *poffset, *ptype; @@ -292,7 +278,7 @@ parse_b(const char *arg, uint32_t *blocks, uint32_t *offset, uint8_t *type) partitiontype = strtol(ptype, NULL, 16); done: - *blocks = blockcount; - *offset = blockoffset; - *type = partitiontype; + disk.dk_bootprt.prt_ns = blockcount; + disk.dk_bootprt.prt_bs = blockoffset; + disk.dk_bootprt.prt_id = partitiontype; } diff --git a/sbin/fdisk/gpt.c b/sbin/fdisk/gpt.c index 9b0ebcd01e6..97a61aec220 100644 --- a/sbin/fdisk/gpt.c +++ b/sbin/fdisk/gpt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gpt.c,v 1.42 2021/07/13 15:03:34 krw Exp $ */ +/* $OpenBSD: gpt.c,v 1.43 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 2015 Markus Muller * Copyright (c) 2015 Kenneth R Westerback @@ -28,9 +28,9 @@ #include #include +#include "part.h" #include "disk.h" #include "misc.h" -#include "part.h" #include "gpt.h" #ifdef DEBUG @@ -49,7 +49,7 @@ int add_partition(const uint8_t *, const char *, uint64_t); int get_header(const uint64_t); int get_partition_table(void); int init_gh(void); -int init_gp(const int, const uint32_t); +int init_gp(const int); uint32_t crc32(const u_char *, const uint32_t); int @@ -414,7 +414,7 @@ init_gh(void) } int -init_gp(const int how, const uint32_t bootsectors) +init_gp(const int how) { struct gpt_partition oldgp[NGPTPARTITIONS]; const uint8_t gpt_uuid_efi_system[] = GPT_UUID_EFI_SYSTEM; @@ -433,9 +433,9 @@ init_gp(const int how, const uint32_t bootsectors) } rslt = 0; - if (bootsectors > 0) { + if (disk.dk_bootprt.prt_ns > 0) { rslt = add_partition(gpt_uuid_efi_system, "EFI System Area", - bootsectors); + disk.dk_bootprt.prt_ns); } if (rslt == 0) rslt = add_partition(gpt_uuid_openbsd, "OpenBSD Area", 0); @@ -447,14 +447,14 @@ init_gp(const int how, const uint32_t bootsectors) } int -GPT_init(const int how, const uint32_t bootsectors) +GPT_init(const int how) { int rslt = 0; if (how == GHANDGP) rslt = init_gh(); if (rslt == 0) - rslt = init_gp(how, bootsectors); + rslt = init_gp(how); return rslt; } diff --git a/sbin/fdisk/gpt.h b/sbin/fdisk/gpt.h index 7a87f533222..0ea4968f42b 100644 --- a/sbin/fdisk/gpt.h +++ b/sbin/fdisk/gpt.h @@ -1,4 +1,4 @@ -/* $OpenBSD: gpt.h,v 1.15 2021/07/12 22:18:54 krw Exp $ */ +/* $OpenBSD: gpt.h,v 1.16 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 2015 Markus Muller * Copyright (c) 2015 Kenneth R Westerback @@ -20,7 +20,7 @@ void GPT_read(const int); int GPT_get_lba_start(const unsigned int); int GPT_get_lba_end(const unsigned int); -int GPT_init(const int, const uint32_t); +int GPT_init(const int); int GPT_write(void); void GPT_zap_headers(void); void GPT_print(const char *, const int); diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c index 5714a26686f..c318194c1a8 100644 --- a/sbin/fdisk/mbr.c +++ b/sbin/fdisk/mbr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.c,v 1.85 2021/07/13 15:03:34 krw Exp $ */ +/* $OpenBSD: mbr.c,v 1.86 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -26,8 +26,8 @@ #include #include -#include "disk.h" #include "part.h" +#include "disk.h" #include "misc.h" #include "mbr.h" #include "gpt.h" @@ -77,8 +77,6 @@ MBR_init_GPT(struct mbr *mbr) void MBR_init(struct mbr *mbr) { - extern uint32_t b_sectors, b_offset; - extern uint8_t b_type; uint64_t adj; daddr_t daddr; @@ -134,16 +132,12 @@ MBR_init(struct mbr *mbr) /* Fix up start/length fields */ PRT_fix_BN(&mbr->mbr_prt[3], 3); #else - if (b_sectors > 0) { - mbr->mbr_prt[0].prt_id = b_type; - mbr->mbr_prt[0].prt_bs = b_offset; - mbr->mbr_prt[0].prt_ns = b_sectors; - 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]); - } + 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. */ diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index 0ca8896b261..9558142ff1a 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.75 2021/07/12 22:18:54 krw Exp $ */ +/* $OpenBSD: misc.c,v 1.76 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -28,9 +28,9 @@ #include #include +#include "part.h" #include "disk.h" #include "misc.h" -#include "part.h" struct unit_type unit_types[] = { { "b" , 1LL , "Bytes" }, diff --git a/sbin/fdisk/part.c b/sbin/fdisk/part.c index 9af78a420b4..4323cce9444 100644 --- a/sbin/fdisk/part.c +++ b/sbin/fdisk/part.c @@ -1,4 +1,4 @@ -/* $OpenBSD: part.c,v 1.96 2021/07/13 22:10:20 krw Exp $ */ +/* $OpenBSD: part.c,v 1.97 2021/07/15 21:58:02 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -26,9 +26,9 @@ #include #include +#include "part.h" #include "disk.h" #include "misc.h" -#include "part.h" int check_chs(const struct prt *); const char *ascii_id(const int); -- 2.20.1