Module Name:    src
Committed By:   christos
Date:           Sun Dec  6 00:39:26 UTC 2015

Modified Files:
        src/sbin/gpt: gpt.8 gpt.c gpt.h gpt_uuid.c gpt_uuid.h set.c show.c
            type.c unset.c

Log Message:
Add listing commands for type, set and unset.
Add help and formatting for set and unset.
Change show to print all the attribute info in one line.


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/sbin/gpt/gpt.8
cvs rdiff -u -r1.63 -r1.64 src/sbin/gpt/gpt.c
cvs rdiff -u -r1.30 -r1.31 src/sbin/gpt/gpt.h src/sbin/gpt/show.c
cvs rdiff -u -r1.12 -r1.13 src/sbin/gpt/gpt_uuid.c src/sbin/gpt/type.c
cvs rdiff -u -r1.5 -r1.6 src/sbin/gpt/gpt_uuid.h
cvs rdiff -u -r1.11 -r1.12 src/sbin/gpt/set.c src/sbin/gpt/unset.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/gpt.8
diff -u src/sbin/gpt/gpt.8:1.41 src/sbin/gpt/gpt.8:1.42
--- src/sbin/gpt/gpt.8:1.41	Wed Dec  2 07:36:53 2015
+++ src/sbin/gpt/gpt.8	Sat Dec  5 19:39:26 2015
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.41 2015/12/02 12:36:53 christos Exp $
+.\" $NetBSD: gpt.8,v 1.42 2015/12/06 00:39:26 christos Exp $
 .\"
 .\" Copyright (c) 2002 Marcel Moolenaar
 .\" All rights reserved.
@@ -488,26 +488,33 @@ will automatically adjust.
 However, the new disk must use the same sector size as the old disk.
 .\" ==== set ====
 .It Nm Ic set Fl a Ar attribute Fl i Ar index
+.It Nm Ic set Fl l
 The
 .Ic set
 command sets various partition attributes.
 The
+.Fl l
+flag lists all available attributes.
+The
 .Fl a
-option specifies which attributes to set and may be specified more than once.
+option specifies which attributes to set and may be specified more than once,
+or the attributes can be comma-separated.
 The
 .Fl i
 option specifies which entry to update.
 The possible attributes are
 .Do biosboot Dc ,
 .Do bootme Dc ,
-.Do bootonce Dc , and
-.Do bootfailed Dc .
+.Do bootonce Dc ,
+.Do bootfailed Dc ,
+.Do noblockio Dc , and
+.Do required Dc .
 The biosboot flag is used to indicate which partition should be booted
 by legacy BIOS boot code.
 See the
 .Ic biosboot
 command for more information.
-The other three attributes are for compatibility with
+The other attributes are for compatibility with
 .Fx
 and are not currently used by any
 .Nx
@@ -546,8 +553,10 @@ The order of precedence for the options 
 .\" ==== type ====
 .It Nm Ic type Oo Fl a Oc Fl T Ar newtype
 .It Nm Ic type Oo Fl b Ar blocknr Oc Oo Fl i Ar index Oc \
+.It Nm Ic type Oo Fl b Ar blocknr Oc Oo Fl i Ar index Oc \
 Oo Fl L Ar label Oc Oo Fl s Ar sectors Oc Oo Fl t Ar type Oc \
 Fl T Ar newtype
+.It Nm Ic type Fl l
 The
 .Ic type
 command allows the user to change the type of any and all partitions
@@ -556,12 +565,19 @@ It uses the same selection options as th
 .Ic label
 command.
 See above for a description of these options.
+The
+.Fl l
+flag lists available types.
 .\" ==== unset ====
 .It Nm Ic unset Fl a Ar attribute Fl i Ar index
+.It Nm Ic unset Fl l 
 The
 .Ic unset
 command unsets various partition attributes.
 The
+.Fl l
+flag lists all available attributes.
+The
 .Fl a
 option specifies which attributes to unset and may be specified more than once.
 The
@@ -570,14 +586,16 @@ option specifies which entry to update.
 The possible attributes are
 .Do biosboot Dc ,
 .Do bootme Dc ,
-.Do bootonce Dc , and
-.Do bootfailed Dc .
+.Do bootonce Dc ,
+.Do bootfailed Dc ,
+.Do noblockio Dc , and
+.Do required Dc .
 The biosboot flag is used to indicate which partition should be booted
 by legacy BIOS boot code.
 See the
 .Ic biosboot
 command for more information.
-The other three attributes are for compatibility with
+The other attributes are for compatibility with
 .Fx
 and are not currently used by any
 .Nx

Index: src/sbin/gpt/gpt.c
diff -u src/sbin/gpt/gpt.c:1.63 src/sbin/gpt/gpt.c:1.64
--- src/sbin/gpt/gpt.c:1.63	Fri Dec  4 11:46:24 2015
+++ src/sbin/gpt/gpt.c	Sat Dec  5 19:39:26 2015
@@ -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.63 2015/12/04 16:46:24 christos Exp $");
+__RCSID("$NetBSD: gpt.c,v 1.64 2015/12/06 00:39:26 christos Exp $");
 #endif
 
 #include <sys/param.h>
@@ -1076,21 +1076,90 @@ gpt_check_ais(gpt_t gpt, off_t alignment
 		return size / gpt->secsz;
 	return 0;
 }
+
+static const struct nvd {
+	const char *name;
+	uint64_t mask;
+	const char *description;
+} gpt_attr[] = {
+	{
+		"biosboot",
+		GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE,
+		"Legacy BIOS boot partition",
+	},
+	{
+		"bootme",
+		GPT_ENT_ATTR_BOOTME,
+		"Bootable partition",
+	},
+	{
+		"bootfailed",
+		GPT_ENT_ATTR_BOOTFAILED,
+		"Partition that marked bootonce failed to boot",
+	},
+	{
+		"bootonce",
+		GPT_ENT_ATTR_BOOTONCE,
+		"Attempt to boot this partition only once",
+	},
+	{
+		"noblockio",
+		GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL,
+		"UEFI won't recognize file system for block I/O",
+	},
+	{
+		"required",
+		GPT_ENT_ATTR_REQUIRED_PARTITION,
+		"Partition required for platform to function",
+	},
+};
+
 int
-gpt_attr_get(uint64_t *attributes)
+gpt_attr_get(gpt_t gpt, uint64_t *attributes)
 {
-	if (strcmp(optarg, "biosboot") == 0)
-		*attributes |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
-	else if (strcmp(optarg, "bootme") == 0)
-		*attributes |= GPT_ENT_ATTR_BOOTME;
-	else if (strcmp(optarg, "bootonce") == 0)
-		*attributes |= GPT_ENT_ATTR_BOOTONCE;
-	else if (strcmp(optarg, "bootfailed") == 0)
-		*attributes |= GPT_ENT_ATTR_BOOTFAILED;
-	else
-		return -1;
-	return 0;
+	size_t i;
+	int rv = 0;
+	char *ptr;
+
+	*attributes = 0;
+
+	for (ptr = strtok(optarg, ","); ptr; ptr = strtok(NULL, ",")) {
+		for (i = 0; i < __arraycount(gpt_attr); i++)
+			if (strcmp(gpt_attr[i].name, ptr) == 0)
+				break;
+		if (i == __arraycount(gpt_attr)) {
+			gpt_warnx(gpt, "Unregognized attribute `%s'", ptr);
+			rv = -1;
+		} else
+			*attributes |= gpt_attr[i].mask;
+	}
+	return rv;
 }
+
+void
+gpt_attr_help(const char *prefix)
+{
+	size_t i;
+
+	for (i = 0; i < __arraycount(gpt_attr); i++)
+		printf("%s%10.10s\t%s\n", prefix, gpt_attr[i].name,
+		    gpt_attr[i].description);
+}
+
+const char *
+gpt_attr_list(char *buf, size_t len, uint64_t attributes)
+{
+	size_t i;
+	strlcpy(buf, "", len);	
+
+	for (i = 0; i < __arraycount(gpt_attr); i++)
+		if (attributes & gpt_attr[i].mask) {
+			strlcat(buf, buf[0] ? "," : "", len); 
+			strlcat(buf, gpt_attr[i].name, len);
+		}
+	return buf;
+}
+
 int
 gpt_attr_update(gpt_t gpt, u_int entry, uint64_t set, uint64_t clr)
 {

Index: src/sbin/gpt/gpt.h
diff -u src/sbin/gpt/gpt.h:1.30 src/sbin/gpt/gpt.h:1.31
--- src/sbin/gpt/gpt.h:1.30	Fri Dec  4 11:46:24 2015
+++ src/sbin/gpt/gpt.h	Sat Dec  5 19:39:26 2015
@@ -119,7 +119,9 @@ int	gpt_add_find(gpt_t, struct gpt_find 
 int	gpt_add_ais(gpt_t, off_t *, u_int *, off_t *, int);
 off_t	gpt_check_ais(gpt_t, off_t, u_int, off_t);
 
-int	gpt_attr_get(uint64_t *);
+int	gpt_attr_get(gpt_t, uint64_t *);
+const char *gpt_attr_list(char *, size_t, uint64_t);
+void	gpt_attr_help(const char *);
 int	gpt_attr_update(gpt_t, u_int, uint64_t, uint64_t);
 int	gpt_uint_get(u_int *);
 int	gpt_human_get(off_t *);
Index: src/sbin/gpt/show.c
diff -u src/sbin/gpt/show.c:1.30 src/sbin/gpt/show.c:1.31
--- src/sbin/gpt/show.c:1.30	Thu Dec  3 20:46:12 2015
+++ src/sbin/gpt/show.c	Sat Dec  5 19:39:26 2015
@@ -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.30 2015/12/04 01:46:12 christos Exp $");
+__RCSID("$NetBSD: show.c,v 1.31 2015/12/06 00:39:26 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -196,23 +196,14 @@ show_one(gpt_t gpt, unsigned int entry)
 	utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
 	printf("Label: %s\n", (char *)utfbuf);
 
-	printf("Attributes:\n");
+	printf("Attributes: ");
 	if (ent->ent_attr == 0) {
-		printf("  None\n");
-		return 0;
+		printf("None\n");
+	} else  {	
+		char buf[1024];
+		printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
 	}
-	if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
-		printf("  required for platform to function\n");
-	if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
-		printf("  UEFI won't recognize file system\n");
-	if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
-		printf("  legacy BIOS boot partition\n");
-	if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
-		printf("  indicates a bootable partition\n");
-	if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
-		printf("  attempt to boot this partition only once\n");
-	if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
-		printf("  partition was marked bootonce but failed to boot\n");
+
 	return 0;
 }
 

Index: src/sbin/gpt/gpt_uuid.c
diff -u src/sbin/gpt/gpt_uuid.c:1.12 src/sbin/gpt/gpt_uuid.c:1.13
--- src/sbin/gpt/gpt_uuid.c:1.12	Wed Dec  2 21:02:43 2015
+++ src/sbin/gpt/gpt_uuid.c	Sat Dec  5 19:39:26 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: gpt_uuid.c,v 1.12 2015/12/03 02:02:43 christos Exp $	*/
+/*	$NetBSD: gpt_uuid.c,v 1.13 2015/12/06 00:39:26 christos Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 #ifdef __RCSID
-__RCSID("$NetBSD: gpt_uuid.c,v 1.12 2015/12/03 02:02:43 christos Exp $");
+__RCSID("$NetBSD: gpt_uuid.c,v 1.13 2015/12/06 00:39:26 christos Exp $");
 #endif
 
 #include <err.h>
@@ -231,6 +231,15 @@ gpt_uuid_parse(const char *s, gpt_uuid_t
 }
 
 void
+gpt_uuid_help(const char *prefix)
+{
+	size_t i;
+
+	for (i = 0; i < __arraycount(gpt_nv); i++)
+		printf("%s%18.18s\t%s\n", prefix, gpt_nv[i].n, gpt_nv[i].d);
+}
+
+void
 gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s)
 {
 	gpt_dce_to_uuid(&gpt_nv[t].u, u);
Index: src/sbin/gpt/type.c
diff -u src/sbin/gpt/type.c:1.12 src/sbin/gpt/type.c:1.13
--- src/sbin/gpt/type.c:1.12	Wed Dec  2 20:07:28 2015
+++ src/sbin/gpt/type.c	Sat Dec  5 19:39:26 2015
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: type.c,v 1.12 2015/12/03 01:07:28 christos Exp $");
+__RCSID("$NetBSD: type.c,v 1.13 2015/12/06 00:39:26 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -54,6 +54,7 @@ static int cmd_type(gpt_t, int, char *[]
 static const char *typehelp[] = {
 	"-a -T newtype",
 	"[-b blocknr] [-i index] [-L label] [-s sectors] [-t type] -T newtype",
+	"-l",
 };
 
 struct gpt_cmd c_type = {
@@ -84,8 +85,11 @@ cmd_type(gpt_t gpt, int argc, char *argv
 	find.msg = "type changed";
 
 	/* Get the type options */
-	while ((ch = getopt(argc, argv, GPT_FIND "T:")) != -1) {
+	while ((ch = getopt(argc, argv, GPT_FIND "T:l")) != -1) {
 		switch(ch) {
+		case 'l':
+			gpt_uuid_help("\t");
+			return 0;
 		case 'T':
 			if (gpt_uuid_get(gpt, &newtype) == -1)
 				return -1;

Index: src/sbin/gpt/gpt_uuid.h
diff -u src/sbin/gpt/gpt_uuid.h:1.5 src/sbin/gpt/gpt_uuid.h:1.6
--- src/sbin/gpt/gpt_uuid.h:1.5	Tue Dec  1 18:29:07 2015
+++ src/sbin/gpt/gpt_uuid.h	Sat Dec  5 19:39:26 2015
@@ -97,6 +97,8 @@ int gpt_uuid_parse(const char *, gpt_uui
 struct gpt;
 int gpt_uuid_generate(struct gpt *, gpt_uuid_t);
 
+void gpt_uuid_help(const char *);
+
 __END_DECLS
 
 #endif /* _GPT_UUID_T */

Index: src/sbin/gpt/set.c
diff -u src/sbin/gpt/set.c:1.11 src/sbin/gpt/set.c:1.12
--- src/sbin/gpt/set.c:1.11	Wed Dec  2 21:02:43 2015
+++ src/sbin/gpt/set.c	Sat Dec  5 19:39:26 2015
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: set.c,v 1.11 2015/12/03 02:02:43 christos Exp $");
+__RCSID("$NetBSD: set.c,v 1.12 2015/12/06 00:39:26 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -53,6 +53,7 @@ static int cmd_set(gpt_t, int, char *[])
 
 static const char *sethelp[] = {
 	"-a attribute -i index",
+	"-l",
 };
 
 struct gpt_cmd c_set = {
@@ -71,16 +72,19 @@ cmd_set(gpt_t gpt, int argc, char *argv[
 	unsigned int entry = 0;
 	uint64_t attributes = 0;
 
-	while ((ch = getopt(argc, argv, "a:i:")) != -1) {
+	while ((ch = getopt(argc, argv, "a:i:l")) != -1) {
 		switch(ch) {
 		case 'a':
-			if (gpt_attr_get(&attributes) == -1)
+			if (gpt_attr_get(gpt, &attributes) == -1)
 				return usage();
 			break;
 		case 'i':
 			if (gpt_uint_get(&entry) == -1)
 				return usage();
 			break;
+		case 'l':
+			gpt_attr_help("\t");
+			return 0;
 		default:
 			return usage();
 		}
Index: src/sbin/gpt/unset.c
diff -u src/sbin/gpt/unset.c:1.11 src/sbin/gpt/unset.c:1.12
--- src/sbin/gpt/unset.c:1.11	Wed Dec  2 21:02:43 2015
+++ src/sbin/gpt/unset.c	Sat Dec  5 19:39:26 2015
@@ -33,7 +33,7 @@
 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
 #endif
 #ifdef __RCSID
-__RCSID("$NetBSD: unset.c,v 1.11 2015/12/03 02:02:43 christos Exp $");
+__RCSID("$NetBSD: unset.c,v 1.12 2015/12/06 00:39:26 christos Exp $");
 #endif
 
 #include <sys/types.h>
@@ -71,16 +71,19 @@ cmd_unset(gpt_t gpt, int argc, char *arg
 	unsigned int entry = 0;
 	uint64_t attributes = 0;
 
-	while ((ch = getopt(argc, argv, "a:i:")) != -1) {
+	while ((ch = getopt(argc, argv, "a:i:l")) != -1) {
 		switch(ch) {
 		case 'a':
-			if (gpt_attr_get(&attributes) == -1)
+			if (gpt_attr_get(gpt, &attributes) == -1)
 				return usage();
 			break;
 		case 'i':
 			if (gpt_uint_get(&entry) == -1)
 				return usage();
 			break;
+		case 'l':
+			gpt_attr_help("\t");
+			return 0;
 		default:
 			return usage();
 		}

Reply via email to