-/* $OpenBSD: mbr.c,v 1.104 2021/11/09 01:50:11 deraadt Exp $ */
+/* $OpenBSD: mbr.c,v 1.105 2021/11/09 14:02:09 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
MBR_init(struct mbr *mbr)
{
struct dos_partition dp;
+ struct prt bootprt, obsdprt;
uint64_t adj;
daddr_t daddr;
return;
}
- memset(mbr, 0, sizeof(*mbr));
- memcpy(mbr->mbr_code, default_dmbr.dmbr_boot, sizeof(mbr->mbr_code));
+ memset(&obsdprt, 0, sizeof(obsdprt));
+ memset(&bootprt, 0, sizeof(bootprt));
+
memcpy(&dp, &default_dmbr.dmbr_parts[0], sizeof(dp));
- PRT_parse(&dp, 0, 0, &mbr->mbr_prt[0]);
+ PRT_parse(&dp, 0, 0, &bootprt);
- if (mbr->mbr_prt[0].prt_flag != DOSACTIVE)
- mbr->mbr_prt[3].prt_flag = DOSACTIVE;
- mbr->mbr_signature = DOSMBR_SIGNATURE;
+ if (bootprt.prt_flag != DOSACTIVE)
+ obsdprt.prt_flag = DOSACTIVE;
/* Use whole disk. Reserve first track, or first cyl, if possible. */
- mbr->mbr_prt[3].prt_id = DOSPTYP_OPENBSD;
+ obsdprt.prt_id = DOSPTYP_OPENBSD;
if (disk.dk_heads > 1)
- mbr->mbr_prt[3].prt_shead = 1;
+ obsdprt.prt_shead = 1;
else
- mbr->mbr_prt[3].prt_shead = 0;
+ obsdprt.prt_shead = 0;
if (disk.dk_heads < 2 && disk.dk_cylinders > 1)
- mbr->mbr_prt[3].prt_scyl = 1;
+ obsdprt.prt_scyl = 1;
else
- mbr->mbr_prt[3].prt_scyl = 0;
- mbr->mbr_prt[3].prt_ssect = 1;
+ obsdprt.prt_scyl = 0;
+ obsdprt.prt_ssect = 1;
/* Go right to the end */
- mbr->mbr_prt[3].prt_ecyl = disk.dk_cylinders - 1;
- mbr->mbr_prt[3].prt_ehead = disk.dk_heads - 1;
- mbr->mbr_prt[3].prt_esect = disk.dk_sectors;
+ obsdprt.prt_ecyl = disk.dk_cylinders - 1;
+ obsdprt.prt_ehead = disk.dk_heads - 1;
+ obsdprt.prt_esect = disk.dk_sectors;
/* Fix up start/length fields */
- PRT_fix_BN(&mbr->mbr_prt[3], 3);
+ PRT_fix_BN(&obsdprt, 3);
#if defined(__powerpc__) || defined(__mips__)
/* Now fix up for the MS-DOS boot partition on PowerPC/MIPS. */
- mbr->mbr_prt[0].prt_flag = DOSACTIVE; /* Boot from dos part */
- mbr->mbr_prt[3].prt_flag = 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]);
- if ((mbr->mbr_prt[3].prt_shead != 1) || (mbr->mbr_prt[3].prt_ssect != 1)) {
+ bootprt.prt_flag = DOSACTIVE; /* Boot from dos part */
+ bootprt.prt_flag = 0;
+ obsdprt.prt_ns += obsdprt.prt_bs;
+ obsdprt.prt_bs = bootprt.prt_bs + bootprt.prt_ns;
+ obsdprt.prt_ns -= obsdprt.prt_bs;
+ PRT_fix_CHS(&obsdprt);
+ if ((obsdprt.prt_shead != 1) || (obsdprt.prt_ssect != 1)) {
/* align the partition on a cylinder boundary */
- mbr->mbr_prt[3].prt_shead = 0;
- mbr->mbr_prt[3].prt_ssect = 1;
- mbr->mbr_prt[3].prt_scyl += 1;
+ obsdprt.prt_shead = 0;
+ obsdprt.prt_ssect = 1;
+ obsdprt.prt_scyl += 1;
}
/* Fix up start/length fields */
- PRT_fix_BN(&mbr->mbr_prt[3], 3);
+ PRT_fix_BN(&obsdprt, 3);
#else
if (disk.dk_bootprt.prt_ns > 0) {
- 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]);
+ bootprt = disk.dk_bootprt;
+ PRT_fix_CHS(&bootprt);
+ obsdprt.prt_ns += obsdprt.prt_bs;
+ obsdprt.prt_bs = bootprt.prt_bs + bootprt.prt_ns;
+ obsdprt.prt_ns -= obsdprt.prt_bs;
+ PRT_fix_CHS(&obsdprt);
}
#endif
/* Start OpenBSD MBR partition on a power of 2 block number. */
daddr = 1;
- while (daddr < DL_SECTOBLK(&dl, mbr->mbr_prt[3].prt_bs))
+ while (daddr < DL_SECTOBLK(&dl, obsdprt.prt_bs))
daddr *= 2;
- adj = DL_BLKTOSEC(&dl, daddr) - mbr->mbr_prt[3].prt_bs;
- mbr->mbr_prt[3].prt_bs += adj;
- mbr->mbr_prt[3].prt_ns -= adj;
- PRT_fix_CHS(&mbr->mbr_prt[3]);
+ adj = DL_BLKTOSEC(&dl, daddr) - obsdprt.prt_bs;
+ obsdprt.prt_bs += adj;
+ obsdprt.prt_ns -= adj;
+ PRT_fix_CHS(&obsdprt);
+
+ memset(mbr, 0, sizeof(*mbr));
+ memcpy(mbr->mbr_code, default_dmbr.dmbr_boot, sizeof(mbr->mbr_code));
+ mbr->mbr_prt[0] = bootprt;
+ mbr->mbr_prt[3] = obsdprt;
+ mbr->mbr_signature = DOSMBR_SIGNATURE;
}
void