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.
-/* $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
#include <string.h>
#include <uuid.h>
+#include "part.h"
#include "disk.h"
#include "misc.h"
-#include "part.h"
#include "mbr.h"
#include "gpt.h"
#include "user.h"
if (dogpt) {
MBR_init_GPT(mbr);
- GPT_init(GHANDGP, 0);
+ GPT_init(GHANDGP);
GPT_print("s", TERSE);
} else {
memset(&gh, 0, sizeof(gh));
-/* $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
#include <unistd.h>
#include <util.h>
+#include "part.h"
#include "disk.h"
#include "misc.h"
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)
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);
+ }
}
/*
-/* $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
#define _DISK_H
struct disk {
+ struct prt dk_bootprt;
char *dk_name;
int dk_fd;
uint32_t dk_cylinders;
-/* $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
#include <string.h>
#include <unistd.h>
-#include "disk.h"
#include "part.h"
+#include "disk.h"
#include "mbr.h"
#include "misc.h"
#include "cmd.h"
#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)
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);
/* 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) &&
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 */
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));
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));
}
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;
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;
}
-/* $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 <mmu@grummel.net>
* Copyright (c) 2015 Kenneth R Westerback <krw@openbsd.org>
#include <unistd.h>
#include <uuid.h>
+#include "part.h"
#include "disk.h"
#include "misc.h"
-#include "part.h"
#include "gpt.h"
#ifdef DEBUG
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
}
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;
}
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);
}
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;
}
-/* $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 <mmu@grummel.net>
* Copyright (c) 2015 Kenneth R Westerback <krw@openbsd.org>
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);
-/* $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
#include <stdlib.h>
#include <string.h>
-#include "disk.h"
#include "part.h"
+#include "disk.h"
#include "misc.h"
#include "mbr.h"
#include "gpt.h"
void
MBR_init(struct mbr *mbr)
{
- extern uint32_t b_sectors, b_offset;
- extern uint8_t b_type;
uint64_t adj;
daddr_t daddr;
/* 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. */
-/* $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
#include <string.h>
#include <uuid.h>
+#include "part.h"
#include "disk.h"
#include "misc.h"
-#include "part.h"
struct unit_type unit_types[] = {
{ "b" , 1LL , "Bytes" },
-/* $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
#include <string.h>
#include <uuid.h>
+#include "part.h"
#include "disk.h"
#include "misc.h"
-#include "part.h"
int check_chs(const struct prt *);
const char *ascii_id(const int);