Module Name:    src
Committed By:   christos
Date:           Thu Jun  9 15:12:54 UTC 2016

Modified Files:
        src/sbin/gpt: biosboot.c create.c gpt.8 gpt.c gpt.h migrate.c show.c

Log Message:
PR/51230: Add the ability to set the active flag in the PMBR.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sbin/gpt/biosboot.c
cvs rdiff -u -r1.20 -r1.21 src/sbin/gpt/create.c
cvs rdiff -u -r1.45 -r1.46 src/sbin/gpt/gpt.8
cvs rdiff -u -r1.67 -r1.68 src/sbin/gpt/gpt.c
cvs rdiff -u -r1.32 -r1.33 src/sbin/gpt/gpt.h
cvs rdiff -u -r1.30 -r1.31 src/sbin/gpt/migrate.c
cvs rdiff -u -r1.36 -r1.37 src/sbin/gpt/show.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/gpt/biosboot.c
diff -u src/sbin/gpt/biosboot.c:1.24 src/sbin/gpt/biosboot.c:1.25
--- src/sbin/gpt/biosboot.c:1.24	Tue Dec 29 11:45:04 2015
+++ src/sbin/gpt/biosboot.c	Thu Jun  9 11:12:54 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: biosboot.c,v 1.24 2015/12/29 16:45:04 christos Exp $ */
+/*	$NetBSD: biosboot.c,v 1.25 2016/06/09 15:12:54 christos Exp $ */
 
 /*
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: biosboot.c,v 1.24 2015/12/29 16:45:04 christos Exp $");
+__RCSID("$NetBSD: biosboot.c,v 1.25 2016/06/09 15:12:54 christos Exp $");
 #endif
 
 #include <sys/stat.h>
@@ -68,7 +68,7 @@ __RCSID("$NetBSD: biosboot.c,v 1.24 2015
 static int cmd_biosboot(gpt_t, int, char *[]);
 
 static const char *biosboothelp[] = {
-	"[-c bootcode] [-i index] [-L label]",
+	"[-a] [-c bootcode] [-i index] [-L label]",
 #if notyet
 	"[-a alignment] [-b blocknr] [-i index] [-l label]",
 	"[-s size] [-t type]",
@@ -164,7 +164,7 @@ set_bootable(gpt_t gpt, map_t map, map_t
 
 static int
 biosboot(gpt_t gpt, daddr_t start, uint64_t size, u_int entry, uint8_t *label,
-    const char *bootpath)
+    const char *bootpath, int active)
 {
 	map_t mbrmap, m;
 	struct mbr *mbr, *bootcode;
@@ -197,6 +197,10 @@ biosboot(gpt_t gpt, daddr_t start, uint6
 		sizeof(mbr->mbr_code));
 	free(bootcode);
 
+	for (i = 0; i < __arraycount(mbr->mbr_part); i++)
+		if (mbr->mbr_part[i].part_typ == MBR_PTYPE_PMBR)
+			mbr->mbr_part[i].part_flag = active ? 0x80 : 0;
+
 	/*
 	 * Walk through the GPT and see where we can boot from
 	 */
@@ -255,12 +259,16 @@ cmd_biosboot(gpt_t gpt, int argc, char *
 	gpt_t ngpt = gpt;
 	daddr_t start = 0;
 	uint64_t size = 0;
+	int active = 0;
 	unsigned int entry = 0;
 	uint8_t *label = NULL;
 	char *bootpath = NULL;
 
-	while ((ch = getopt(argc, argv, "c:i:L:")) != -1) {
+	while ((ch = getopt(argc, argv, "ac:i:L:")) != -1) {
 		switch(ch) {
+		case 'a':
+			active = 1;
+			break;
 		case 'c':
 			if (gpt_name_get(gpt, &bootpath) == -1)
 				goto usage;
@@ -295,7 +303,7 @@ cmd_biosboot(gpt_t gpt, int argc, char *
 			goto cleanup;
 	}
 #endif
-	if (biosboot(ngpt, start, size, entry, label, bootpath) == -1)
+	if (biosboot(ngpt, start, size, entry, label, bootpath, active) == -1)
 		goto cleanup;
 	if (ngpt != gpt)
 		gpt_close(ngpt);

Index: src/sbin/gpt/create.c
diff -u src/sbin/gpt/create.c:1.20 src/sbin/gpt/create.c:1.21
--- src/sbin/gpt/create.c:1.20	Tue Dec 29 11:45:04 2015
+++ src/sbin/gpt/create.c	Thu Jun  9 11:12:54 2016
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: create.c,v 1.20 2015/12/29 16:45:04 christos Exp $");
+__RCSID("$NetBSD: create.c,v 1.21 2016/06/09 15:12:54 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -55,7 +55,7 @@ __RCSID("$NetBSD: create.c,v 1.20 2015/1
 static int cmd_create(gpt_t, int, char *[]);
 
 static const char *createhelp[] = {
-	"[-fP] [-p partitions]",
+	"[-afP] [-p partitions]",
 };
 
 struct gpt_cmd c_create = {
@@ -69,7 +69,7 @@ struct gpt_cmd c_create = {
 
 
 static int
-create(gpt_t gpt, u_int parts, int force, int primary_only)
+create(gpt_t gpt, u_int parts, int force, int primary_only, int active)
 {
 	off_t last = gpt_last(gpt);
 	map_t map;
@@ -100,7 +100,7 @@ create(gpt_t gpt, u_int parts, int force
 		}
 		memset(mbr, 0, sizeof(*mbr));
 		mbr->mbr_sig = htole16(MBR_SIG);
-		gpt_create_pmbr_part(mbr->mbr_part, last);
+		gpt_create_pmbr_part(mbr->mbr_part, last, active);
 
 		map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr, 1);
 		if (gpt_write(gpt, map) == -1) {
@@ -125,12 +125,16 @@ static int
 cmd_create(gpt_t gpt, int argc, char *argv[])
 {
 	int ch;
+	int active = 0;
 	int force = 0;
 	int primary_only = 0;
 	u_int parts = 128;
 
-	while ((ch = getopt(argc, argv, "fPp:")) != -1) {
+	while ((ch = getopt(argc, argv, "afPp:")) != -1) {
 		switch(ch) {
+		case 'a':
+			active = 1;
+			break;
 		case 'f':
 			force = 1;
 			break;
@@ -149,5 +153,5 @@ cmd_create(gpt_t gpt, int argc, char *ar
 	if (argc != optind)
 		return usage();
 
-	return create(gpt, parts, force, primary_only);
+	return create(gpt, parts, force, primary_only, active);
 }

Index: src/sbin/gpt/gpt.8
diff -u src/sbin/gpt/gpt.8:1.45 src/sbin/gpt/gpt.8:1.46
--- src/sbin/gpt/gpt.8:1.45	Fri Dec 25 07:16:03 2015
+++ src/sbin/gpt/gpt.8	Thu Jun  9 11:12:54 2016
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.45 2015/12/25 12:16:03 wiz Exp $
+.\" $NetBSD: gpt.8,v 1.46 2016/06/09 15:12:54 christos Exp $
 .\"
 .\" Copyright (c) 2002 Marcel Moolenaar
 .\" All rights reserved.
@@ -26,7 +26,7 @@
 .\"
 .\" $FreeBSD: src/sbin/gpt/gpt.8,v 1.17 2006/06/22 22:22:32 marcel Exp $
 .\"
-.Dd December 25, 2015
+.Dd June 9, 2016
 .Dt GPT 8
 .Os
 .Sh NAME
@@ -211,7 +211,7 @@ command.
 The format is a plist.
 It should not be modified.
 .\" ==== biosboot ====
-.It Nm Ic biosboot Oo Fl c Ar bootcode Oc Oo Fl i Ar index Oc \
+.It Nm Ic biosboot Oo Fl ac Ar bootcode Oc Oo Fl i Ar index Oc \
 Oo Fl L Ar label Oc
 The
 .Ic biosboot
@@ -220,6 +220,10 @@ primary bootstrap program, used during
 .Xr boot 8 .
 .Pp
 The
+.Fl a
+options sets the PMBR partition active.
+.Pp
+The
 .Fl c
 option allows the user to specify the filename that
 .Nm
@@ -238,7 +242,7 @@ option selects the partition by label.
 If there are multiple partitions with the same label, it will use the
 first one found.
 .\" ==== create ====
-.It Nm Ic create Oo Fl fP Oc Oo Fl p Ar partitions Oc
+.It Nm Ic create Oo Fl afP Oc Oo Fl p Ar partitions Oc
 The
 .Ic create
 command allows the user to create a new (empty) GPT.
@@ -252,6 +256,10 @@ option is specified, an existing MBR is 
 described by the MBR are lost.
 .Pp
 The
+.Fl a
+options sets the PMBR partition active.
+.Pp
+The
 .Fl P
 option tells
 .Nm
@@ -343,7 +351,7 @@ The
 option is used to specify the label in the command line.
 The label is assumed to be encoded in UTF-8.
 .\" ==== migrate ====
-.It Nm Ic migrate Oo Fl fs Oc Oo Fl p Ar partitions Oc
+.It Nm Ic migrate Oo Fl afs Oc Oo Fl p Ar partitions Oc
 The
 .Ic migrate
 command allows the user to migrate an MBR-based disk partitioning into a
@@ -359,6 +367,10 @@ option will cause unknown partitions to 
 to be lost.
 .Pp
 The
+.Fl a
+options sets the PMBR partition active.
+.Pp
+The
 .Fl s
 option prevents migrating
 .Bx

Index: src/sbin/gpt/gpt.c
diff -u src/sbin/gpt/gpt.c:1.67 src/sbin/gpt/gpt.c:1.68
--- src/sbin/gpt/gpt.c:1.67	Fri Jan  8 13:59:01 2016
+++ src/sbin/gpt/gpt.c	Thu Jun  9 11:12:54 2016
@@ -35,7 +35,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt.c,v 1.67 2016/01/08 18:59:01 joerg Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.68 2016/06/09 15:12:54 christos Exp $");
 #endif
 
 #include <sys/param.h>
@@ -336,7 +336,8 @@ gpt_mbr(gpt_t gpt, off_t lba)
 		/* start is relative to the offset of the MBR itself. */
 		start += lba;
 		if (gpt->verbose > 2)
-			gpt_msg(gpt, "MBR part: type=%d, start=%ju, size=%ju",
+			gpt_msg(gpt, "MBR part: flag=%#x type=%d, start=%ju, "
+			    "size=%ju", mbr->mbr_part[i].part_flag,
 			    mbr->mbr_part[i].part_typ,
 			    (uintmax_t)start, (uintmax_t)size);
 		if (mbr->mbr_part[i].part_typ != MBR_PTYPE_EXT_LBA) {
@@ -705,8 +706,9 @@ gpt_write_backup(gpt_t gpt)
 }
 
 void
-gpt_create_pmbr_part(struct mbr_part *part, off_t last)
+gpt_create_pmbr_part(struct mbr_part *part, off_t last, int active)
 {
+	part->part_flag = active ? 0x80 : 0;
 	part->part_shd = 0x00;
 	part->part_ssect = 0x02;
 	part->part_scyl = 0x00;

Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.32 src/sbin/gpt/gpt.h:1.33
--- src/sbin/gpt/gpt.h:1.32	Tue Dec 29 11:45:04 2015
+++ src/sbin/gpt/gpt.h	Thu Jun  9 11:12:54 2016
@@ -92,7 +92,7 @@ struct gpt_hdr *gpt_hdr(gpt_t);
 void	gpt_msg(gpt_t, const char *, ...) __printflike(2, 3);
 void	gpt_warn(gpt_t, const char *, ...) __printflike(2, 3);
 void	gpt_warnx(gpt_t, const char *, ...) __printflike(2, 3);
-void	gpt_create_pmbr_part(struct mbr_part *, off_t);
+void	gpt_create_pmbr_part(struct mbr_part *, off_t, int);
 struct gpt_ent *gpt_ent(map_t, map_t, unsigned int);
 struct gpt_ent *gpt_ent_primary(gpt_t, unsigned int);
 struct gpt_ent *gpt_ent_backup(gpt_t, unsigned int);

Index: src/sbin/gpt/migrate.c
diff -u src/sbin/gpt/migrate.c:1.30 src/sbin/gpt/migrate.c:1.31
--- src/sbin/gpt/migrate.c:1.30	Tue Dec 29 11:45:04 2015
+++ src/sbin/gpt/migrate.c	Thu Jun  9 11:12:54 2016
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: migrate.c,v 1.30 2015/12/29 16:45:04 christos Exp $");
+__RCSID("$NetBSD: migrate.c,v 1.31 2016/06/09 15:12:54 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -81,7 +81,7 @@ __RCSID("$NetBSD: migrate.c,v 1.30 2015/
 static int cmd_migrate(gpt_t, int, char *[]);
 
 static const char *migratehelp[] = {
-	"[-fs] [-p partitions]",
+	"[-afs] [-p partitions]",
 };
 
 struct gpt_cmd c_migrate = {
@@ -233,7 +233,7 @@ migrate_disklabel(gpt_t gpt, off_t start
 }
 
 static int
-migrate(gpt_t gpt, u_int parts, int force, int slice)
+migrate(gpt_t gpt, u_int parts, int force, int slice, int active)
 {
 	off_t last = gpt_last(gpt);
 	map_t map;
@@ -314,7 +314,7 @@ migrate(gpt_t gpt, u_int parts, int forc
 	 * Turn the MBR into a Protective MBR.
 	 */
 	memset(mbr->mbr_part, 0, sizeof(mbr->mbr_part));
-	gpt_create_pmbr_part(mbr->mbr_part, last);
+	gpt_create_pmbr_part(mbr->mbr_part, last, active);
 	if (gpt_write(gpt, map) == -1) {
 		gpt_warn(gpt, "Cant write PMBR");
 		return -1;
@@ -328,11 +328,15 @@ cmd_migrate(gpt_t gpt, int argc, char *a
 	int ch;
 	int force = 0;
 	int slice = 0;
+	int active = 0;
 	u_int parts = 128;
 
 	/* Get the migrate options */
-	while ((ch = getopt(argc, argv, "fp:s")) != -1) {
+	while ((ch = getopt(argc, argv, "afp:s")) != -1) {
 		switch(ch) {
+		case 'a':
+			active = 1;
+			break;
 		case 'f':
 			force = 1;
 			break;
@@ -351,5 +355,5 @@ cmd_migrate(gpt_t gpt, int argc, char *a
 	if (argc != optind)
 		return usage();
 
-	return migrate(gpt, parts, force, slice);
+	return migrate(gpt, parts, force, slice, active);
 }

Index: src/sbin/gpt/show.c
diff -u src/sbin/gpt/show.c:1.36 src/sbin/gpt/show.c:1.37
--- src/sbin/gpt/show.c:1.36	Mon May 30 22:29:54 2016
+++ src/sbin/gpt/show.c	Thu Jun  9 11:12:54 2016
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: show.c,v 1.36 2016/05/31 02:29:54 dholland Exp $");
+__RCSID("$NetBSD: show.c,v 1.37 2016/06/09 15:12:54 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -118,7 +118,9 @@ print_part_type(int map_type, int flags,
 			/* wasn't there */
 			printf("[partition not found?]");
 		} else {
-			printf("%d", mbr->mbr_part[i].part_typ);
+			printf("%d%s", mbr->mbr_part[i].part_typ,
+			    mbr->mbr_part[i].part_flag == 0x80 ?
+			    " (active)" : "");
 		}
 		break;
 	case MAP_TYPE_GPT_PART:

Reply via email to