From: jsing Date: Sat, 3 Jan 2015 15:50:50 +0000 (+0000) Subject: Add support for creating a protective MBR for GPT. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=1f546e5f71af657ae46be3038f09d200ead3182f;p=openbsd Add support for creating a protective MBR for GPT. Part of a diff by Markus Mueller, which was derived from Bitrig during Google Summer of Code, with further tweaks by me. ok krw@ miod@ --- diff --git a/sbin/fdisk/fdisk.8 b/sbin/fdisk/fdisk.8 index bb4f0bc5bbc..dc437808c6d 100644 --- a/sbin/fdisk/fdisk.8 +++ b/sbin/fdisk/fdisk.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: fdisk.8,v 1.76 2014/03/06 17:39:23 jmc Exp $ +.\" $OpenBSD: fdisk.8,v 1.77 2015/01/03 15:50:50 jsing Exp $ .\" .\" Copyright (c) 1997 Tobias Weingartner .\" All rights reserved. @@ -23,7 +23,7 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: March 6 2014 $ +.Dd $Mdocdate: January 3 2015 $ .Dt FDISK 8 .Os .Sh NAME @@ -31,7 +31,7 @@ .Nd MBR partition maintenance program .Sh SYNOPSIS .Nm fdisk -.Op Fl eiuy +.Op Fl egiuy .Oo .Fl c Ar cylinders .Fl h Ar heads @@ -105,6 +105,12 @@ below, for more information. Specifies an alternate MBR template file. The default file is .Pa /usr/mdec/mbr . +.It Fl g +A protective MBR for GPT will be written to disk, instead of an MBR with an +.Ox +MBR partition. +Only valid with +.Fl i . .It Fl i Requests that the MBR partition data be re-initialized. In this mode, @@ -120,6 +126,9 @@ In the default template, MBR partition number 3 will be configured as an MBR partition spanning the entire disk, except for a zone left at the start for booting. This mode is designed to initialize the MBR the very first time. +If the +.Fl g +flag is also specified, a protective MBR for GPT will be created. .It Fl l Ar blocks Specify the number of blocks in the disk, and force the MBR to be in LBA mode only. diff --git a/sbin/fdisk/fdisk.c b/sbin/fdisk/fdisk.c index 70268385202..2a169bd4923 100644 --- a/sbin/fdisk/fdisk.c +++ b/sbin/fdisk/fdisk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fdisk.c,v 1.63 2014/03/20 13:18:21 krw Exp $ */ +/* $OpenBSD: fdisk.c,v 1.64 2015/01/03 15:50:50 jsing Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -47,6 +47,7 @@ static unsigned char builtin_mbr[] = { #include "mbrcode.h" }; +int g_flag; int y_flag; static void @@ -55,7 +56,8 @@ usage(void) extern char * __progname; fprintf(stderr, "usage: %s " - "[-eiuy] [-c cylinders -h heads -s sectors] [-f mbrfile] [-l blocks] disk\n" + "[-egiuy] [-c cylinders -h heads -s sectors] [-f mbrfile] " + "[-l blocks] disk\n" "\t-i: initialize disk with virgin MBR\n" "\t-u: update MBR code, preserve partition table\n" "\t-e: edit MBRs on disk interactively\n" @@ -63,6 +65,7 @@ usage(void) "\t-chs: specify disk geometry\n" "\t-l: specify LBA block count\n" "\t-y: do not ask questions\n" + "\t-g: initialize disk with EFI/GPT partition, requires -i\n" "`disk' may be of the forms: sd0 or /dev/rsd0c.\n", __progname); exit(1); @@ -85,7 +88,7 @@ main(int argc, char *argv[]) struct mbr mbr; struct dos_mbr dos_mbr; - while ((ch = getopt(argc, argv, "ieuf:c:h:s:l:y")) != -1) { + while ((ch = getopt(argc, argv, "ieguf:c:h:s:l:y")) != -1) { const char *errstr; switch(ch) { @@ -117,6 +120,9 @@ main(int argc, char *argv[]) if (errstr) errx(1, "Sector argument %s [1..63].", errstr); break; + case 'g': + g_flag = 1; + break; case 'l': l_arg = strtonum(optarg, 64, UINT32_MAX, &errstr); if (errstr) @@ -141,6 +147,11 @@ main(int argc, char *argv[]) else disk.name = argv[0]; + if (g_flag != 0 && i_flag == 0) { + warnx("-g specified without -i"); + usage(); + } + /* Start with the disklabel geometry and get the sector size. */ DISK_getlabelgeometry(&disk); diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c index 825b896deb6..bde9732bfd8 100644 --- a/sbin/fdisk/mbr.c +++ b/sbin/fdisk/mbr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.c,v 1.40 2014/05/21 15:55:19 krw Exp $ */ +/* $OpenBSD: mbr.c,v 1.41 2015/01/03 15:50:50 jsing Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -45,11 +45,32 @@ #include "misc.h" #include "mbr.h" +void +MBR_init_GPT(struct disk *disk, struct mbr *mbr) +{ + /* Initialize a protective MBR for GPT. */ + bzero(&mbr->part, sizeof(mbr->part)); + + /* Use whole disk, starting after MBR. */ + mbr->part[0].id = DOSPTYP_EFI; + mbr->part[0].bs = 1; + mbr->part[0].ns = disk->size - 1; + + /* Fix up start/length fields. */ + PRT_fix_CHS(disk, &mbr->part[0]); +} + void MBR_init(struct disk *disk, struct mbr *mbr) { - daddr_t i; + extern int g_flag; u_int64_t adj; + daddr_t i; + + if (g_flag) { + MBR_init_GPT(disk, mbr); + return; + } /* Fix up given mbr for this disk */ mbr->part[0].flag = 0;