Module Name:    src
Committed By:   christos
Date:           Mon Nov  4 18:36:17 UTC 2024

Modified Files:
        src/sbin/gpt: gpt.8 show.c

Log Message:
Additions to guid printing and an option to print start/size in hex
(from Anon Ymous)


To generate a diff of this commit:
cvs rdiff -u -r1.83 -r1.84 src/sbin/gpt/gpt.8
cvs rdiff -u -r1.45 -r1.46 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/gpt.8
diff -u src/sbin/gpt/gpt.8:1.83 src/sbin/gpt/gpt.8:1.84
--- src/sbin/gpt/gpt.8:1.83	Mon Aug 19 13:15:38 2024
+++ src/sbin/gpt/gpt.8	Mon Nov  4 13:36:16 2024
@@ -1,4 +1,4 @@
-.\" $NetBSD: gpt.8,v 1.83 2024/08/19 17:15:38 christos Exp $
+.\" $NetBSD: gpt.8,v 1.84 2024/11/04 18:36:16 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 August 19, 2024
+.Dd November 4, 2024
 .Dt GPT 8
 .Os
 .Sh NAME
@@ -632,7 +632,7 @@ They may be used by
 .Nx
 in the future.
 .\" ==== show ====
-.It Nm Ic show Oo Fl aglu Oc Oo Fl i Ar index Oc Oo Fl b Ar startsec Oc
+.It Nm Ic show Oo Fl aglux Oc Oo Fl i Ar index Oc Oo Fl b Ar startsec Oc
 The
 .Ic show
 command displays the current partitioning on the listed devices and gives
@@ -660,13 +660,17 @@ With the
 option, all information for all GPT partitions (just like with
 .Fl i Ar index )
 will be printed.
+The
+.Fl x
+option prints start/size in hex.
 None of the options have any effect on non-GPT partitions.
 The order of precedence for the options is:
 .Fl a ,
 .Fl i ,
 .Fl l ,
 .Fl g ,
-.Fl u .
+.Fl u ,
+.Fl x .
 .\" ==== 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 \

Index: src/sbin/gpt/show.c
diff -u src/sbin/gpt/show.c:1.45 src/sbin/gpt/show.c:1.46
--- src/sbin/gpt/show.c:1.45	Fri Sep 13 07:07:35 2024
+++ src/sbin/gpt/show.c	Mon Nov  4 13:36:16 2024
@@ -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.45 2024/09/13 11:07:35 mlelstv Exp $");
+__RCSID("$NetBSD: show.c,v 1.46 2024/11/04 18:36:16 christos Exp $");
 #endif
 
 #include <sys/bootblock.h>
@@ -54,13 +54,14 @@ __RCSID("$NetBSD: show.c,v 1.45 2024/09/
 static int cmd_show(gpt_t, int, char *[]);
 
 static const char *showhelp[] = {
-	"[-aglu] [-i index]",
+	"[-aglux] [-i index]",
 };
 
 #define SHOW_UUID  1
 #define SHOW_GUID  2
 #define SHOW_LABEL 4
 #define SHOW_ALL   8
+#define SHOW_HEX   16
 
 struct gpt_cmd c_show = {
 	"show",
@@ -71,6 +72,46 @@ struct gpt_cmd c_show = {
 
 #define usage() gpt_usage(NULL, &c_show)
 
+static const char *
+get_mbr_sig(char *b, size_t blen, const uint8_t *bp)
+{
+	gpt_uuid_t uuid;
+
+	/*
+	 * MBR partitions have a 4 byte signature in the MBR.  Table
+	 * 10.54 of UEFI Spec 2.10 Errata A states how this is to be
+	 * formatted as a GUID.
+	 *
+	 * XXX: I thought I had seen more on this elsewhere, but I
+	 * can't seem to find it now.  In particular, the endianness
+	 * of this quanity is not clear in the above.
+	 *
+	 * XXX: The location and size of the MBR signature should be
+	 * in 'struct mbr,' e.g.:
+	 *
+	 * struct mbr {
+	 *	uint8_t		mbr_code[440];
+	 *	uint32_t	mbr_disc_sig;
+	 *	uint16_t	mbr_unknown;
+	 *	struct mbr_part	mbr_part[4];
+	 *	uint16_t	mbr_sig;
+	 * };
+	 *
+	 * For now, we just hardcode it.  Ugh!
+	 */
+	memset(uuid, 0, sizeof(uuid));
+	memcpy(uuid, bp + 440, 4);
+	gpt_uuid_snprintf(b, blen, "%d", uuid);
+	return b;
+}
+
+static const char *
+get_gpt_hdr_guid(char *b, size_t blen, struct gpt_hdr *hdr)
+{
+	gpt_uuid_snprintf(b, blen, "%d", hdr->hdr_guid);
+	return b;
+}
+
 static void
 print_part_type(int map_type, int flags, void *map_data, off_t map_start)
 {
@@ -90,12 +131,21 @@ print_part_type(int map_type, int flags,
 		if (map_start != 0)
 			printf("Extended ");
 		printf("MBR");
+		if (map_start == 0 && flags & SHOW_GUID)
+			printf(" - %s",
+			    get_mbr_sig(buf, sizeof(buf), map_data));
 		break;
 	case MAP_TYPE_PRI_GPT_HDR:
 		printf("Pri GPT header");
+		if (flags & SHOW_GUID)
+			printf(" - %s",
+			    get_gpt_hdr_guid(buf, sizeof(buf), map_data));
 		break;
 	case MAP_TYPE_SEC_GPT_HDR:
 		printf("Sec GPT header");
+		if (flags & SHOW_GUID)
+			printf(" - %s",
+			    get_gpt_hdr_guid(buf, sizeof(buf), map_data));
 		break;
 	case MAP_TYPE_PRI_GPT_TBL:
 		printf("Pri GPT table");
@@ -169,8 +219,9 @@ show(gpt_t gpt, int xshow)
 
 	m = map_first(gpt);
 	while (m != NULL) {
-		printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
-		printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
+#define FMT (xshow & SHOW_HEX) ? "  %*jx" : "  %*ju"
+		printf(FMT, gpt->lbawidth, (uintmax_t)m->map_start);
+		printf(FMT, gpt->lbawidth, (uintmax_t)m->map_size);
 		putchar(' ');
 		putchar(' ');
 		if (m->map_index > 0)
@@ -250,7 +301,7 @@ show_one(gpt_t gpt, unsigned int entry)
 }
 
 static int
-show_all(gpt_t gpt)
+show_all(gpt_t gpt, int xshow)
 {
 	map_t m;
 	struct gpt_ent *ent;
@@ -267,8 +318,8 @@ show_all(gpt_t gpt)
 
 	m = map_first(gpt);
 	while (m != NULL) {
-		printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
-		printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
+		printf(FMT, gpt->lbawidth, (uintmax_t)m->map_start);
+		printf(FMT, gpt->lbawidth, (uintmax_t)m->map_size);
 		putchar(' ');
 		putchar(' ');
 		if (m->map_index > 0) {
@@ -284,10 +335,33 @@ show_all(gpt_t gpt)
 			if (strcmp(s1, s2) == 0)
 				strlcpy(s1, "unknown", sizeof(s1));
 			printf(PFX "Type: %s\n", s1);
-			printf(PFX "TypeID: %s\n", s2);
-
-			gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_guid);
-			printf(PFX "GUID: %s\n", s2);
+			if (m->map_type == MAP_TYPE_MBR_PART) {
+				static uint8_t unused_uuid[sizeof(gpt_uuid_t)];
+				/*
+				 * MBR part partitions don't have
+				 * GUIDs, so don't create a bogus one!
+				 *
+				 * We could get the TypeID from the
+				 * partition type (the one byte OSType
+				 * field in the partition structure),
+				 * perhaps borrowing info from fdisk.
+				 * However, some OSTypes have multiple
+				 * OSes assigned to them and many may
+				 * not have official UUIDs.
+				 *
+				 * Should we even print anything for
+				 * these, in particular the GUID?
+				 */
+				gpt_uuid_snprintf(s2, sizeof(s2), "%d",
+				    unused_uuid);
+				printf(PFX "TypeID: %s\n", s2);	/* XXX: show this? */
+				printf(PFX "GUID: %s\n", s2);	/* XXX: show this? */
+			}
+			else {
+				printf(PFX "TypeID: %s\n", s2);
+				gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_guid);
+				printf(PFX "GUID: %s\n", s2);
+			}
 
 			printf(PFX "Size: ");
 #ifdef HN_AUTOSCALE
@@ -323,6 +397,21 @@ show_all(gpt_t gpt)
 			print_part_type(m->map_type, 0, m->map_data,
 			    m->map_start);
 			putchar('\n');
+
+			switch (m->map_type) {
+			case MAP_TYPE_PRI_GPT_HDR:
+			case MAP_TYPE_SEC_GPT_HDR:
+				printf(PFX "GUID: %s\n",
+				    get_gpt_hdr_guid(s1, sizeof(s1), 
+				    m->map_data));
+				break;
+			case MAP_TYPE_MBR:
+				printf(PFX "GUID: %s\n",
+				    get_mbr_sig(s1, sizeof(s1), m->map_data));
+				break;
+			default:
+				break;
+			}
 		}
 		m = m->map_next;
 	}
@@ -338,7 +427,7 @@ cmd_show(gpt_t gpt, int argc, char *argv
 	off_t start = 0;
 	map_t m;
 
-	while ((ch = getopt(argc, argv, "gi:b:lua")) != -1) {
+	while ((ch = getopt(argc, argv, "gi:b:luax")) != -1) {
 		switch(ch) {
 		case 'a':
 			xshow |= SHOW_ALL;
@@ -360,6 +449,9 @@ cmd_show(gpt_t gpt, int argc, char *argv
 		case 'u':
 			xshow |= SHOW_UUID;
 			break;
+		case 'x':
+			xshow |= SHOW_HEX;
+			break;
 		default:
 			return usage();
 		}
@@ -372,7 +464,7 @@ cmd_show(gpt_t gpt, int argc, char *argv
 		printf("GPT not found, displaying data from MBR.\n\n");
 
 	if (xshow & SHOW_ALL)
-		return show_all(gpt);
+		return show_all(gpt, xshow);
 
 	if (start > 0) {
 		for (m = map_first(gpt); m != NULL; m = m->map_next) {

Reply via email to