show.c revision 1.20
1/*-
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#if HAVE_NBTOOL_CONFIG_H
28#include "nbtool_config.h"
29#endif
30
31#include <sys/cdefs.h>
32#ifdef __FBSDID
33__FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
34#endif
35#ifdef __RCSID
36__RCSID("$NetBSD: show.c,v 1.20 2014/09/30 20:23:23 jnemeth Exp $");
37#endif
38
39#include <sys/types.h>
40
41#include <err.h>
42#include <stddef.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48#include "map.h"
49#include "gpt.h"
50
51static int show_label = 0;
52static int show_uuid = 0;
53static int show_guid = 0;
54static unsigned int entry = 0;
55
56const char showmsg[] = "show [-glu] [-i index] device ...";
57
58__dead static void
59usage_show(void)
60{
61
62	fprintf(stderr,
63	    "usage: %s %s\n", getprogname(), showmsg);
64	exit(1);
65}
66
67static void
68show(void)
69{
70	off_t start;
71	map_t *m, *p;
72	struct mbr *mbr;
73	struct gpt_ent *ent;
74	unsigned int i;
75
76	printf("  %*s", lbawidth, "start");
77	printf("  %*s", lbawidth, "size");
78	printf("  index  contents\n");
79
80	m = map_first();
81	while (m != NULL) {
82		printf("  %*llu", lbawidth, (long long)m->map_start);
83		printf("  %*llu", lbawidth, (long long)m->map_size);
84		putchar(' ');
85		putchar(' ');
86		if (m->map_index > 0)
87			printf("%5d", m->map_index);
88		else
89			printf("     ");
90		putchar(' ');
91		putchar(' ');
92		switch (m->map_type) {
93		case MAP_TYPE_MBR:
94			if (m->map_start != 0)
95				printf("Extended ");
96			printf("MBR");
97			break;
98		case MAP_TYPE_PRI_GPT_HDR:
99			printf("Pri GPT header");
100			break;
101		case MAP_TYPE_SEC_GPT_HDR:
102			printf("Sec GPT header");
103			break;
104		case MAP_TYPE_PRI_GPT_TBL:
105			printf("Pri GPT table");
106			break;
107		case MAP_TYPE_SEC_GPT_TBL:
108			printf("Sec GPT table");
109			break;
110		case MAP_TYPE_MBR_PART:
111			p = m->map_data;
112			if (p->map_start != 0)
113				printf("Extended ");
114			printf("MBR part ");
115			mbr = p->map_data;
116			for (i = 0; i < 4; i++) {
117				start = le16toh(mbr->mbr_part[i].part_start_hi);
118				start = (start << 16) +
119				    le16toh(mbr->mbr_part[i].part_start_lo);
120				if (m->map_start == p->map_start + start)
121					break;
122			}
123			printf("%d", mbr->mbr_part[i].part_typ);
124			break;
125		case MAP_TYPE_GPT_PART:
126			printf("GPT part ");
127			ent = m->map_data;
128			if (show_label) {
129				printf("- \"%s\"",
130				    utf16_to_utf8(ent->ent_name));
131			} else if (show_guid) {
132				char buf[128];
133				gpt_uuid_snprintf(
134				    buf, sizeof(buf), "%d", ent->ent_guid);
135				printf("- %s", buf);
136			} else {
137				char buf[128];
138				gpt_uuid_snprintf(
139				    buf, sizeof(buf), "%s", ent->ent_type);
140				printf("- %s", buf);
141			}
142			break;
143		case MAP_TYPE_PMBR:
144			printf("PMBR");
145			break;
146		}
147		putchar('\n');
148		m = m->map_next;
149	}
150}
151
152static void
153show_one(void)
154{
155	map_t *m;
156	struct gpt_ent *ent;
157	char s1[128], s2[128];
158#ifdef HN_AUTOSCALE
159	char human_num[5];
160#endif
161
162	for (m = map_first(); m != NULL; m = m->map_next)
163		if (entry == m->map_index)
164			break;
165	if (m == NULL) {
166		warnx("%s: error: could not find index %d",
167		    device_name, entry);
168		return;
169	}
170	ent = m->map_data;
171
172	printf("Details for index %d:\n", entry);
173#ifdef HN_AUTOSCALE
174	if (humanize_number(human_num, 5, (int64_t)(m->map_start * secsz),
175	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
176		human_num[0] = '\0';
177	if (human_num[0] != '\0')
178		printf("Start: %llu (%s)\n", (long long)m->map_start,
179		    human_num);
180	else
181#endif
182		printf("Start: %llu\n", (long long)m->map_start);
183#ifdef HN_AUTOSCALE
184	if (humanize_number(human_num, 5, (int64_t)(m->map_size * secsz),
185	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
186		human_num[0] = '\0';
187	if (human_num[0] != '\0')
188		printf("Size: %llu (%s)\n", (long long)m->map_size, human_num);
189	else
190#endif
191		printf("Size: %llu\n", (long long)m->map_size);
192
193	gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
194	gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
195	if (strcmp(s1, s2) == 0)
196		strlcpy(s1, "unknown", sizeof(s1));
197	printf("Type: %s (%s)\n", s1, s2);
198
199	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
200	printf("GUID: %s\n", s2);
201
202	printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
203
204	printf("Attributes:\n");
205	if (ent->ent_attr == 0)
206		printf("  None\n");
207	else {
208		if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
209			printf("  required for platform to function\n");
210		if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
211			printf("  UEFI won't recognize file system\n");
212		if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
213			printf("  legacy BIOS boot partition\n");
214		if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
215			printf("  indicates a bootable partition\n");
216		if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
217			printf("  attempt to boot this partition only once\n");
218		if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
219			printf("  partition that was marked bootonce but failed to boot\n");
220	}
221}
222
223int
224cmd_show(int argc, char *argv[])
225{
226	char *p;
227	int ch, fd;
228
229	while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
230		switch(ch) {
231		case 'g':
232			show_guid = 1;
233			break;
234		case 'i':
235			if (entry > 0)
236				usage_show();
237			entry = strtoul(optarg, &p, 10);
238			if (*p != 0 || entry < 1)
239				usage_show();
240			break;
241		case 'l':
242			show_label = 1;
243			break;
244		case 'u':
245			show_uuid = 1;
246			break;
247		default:
248			usage_show();
249		}
250	}
251
252	if (argc == optind)
253		usage_show();
254
255	while (optind < argc) {
256		fd = gpt_open(argv[optind++]);
257		if (fd == -1) {
258			warn("unable to open device '%s'", device_name);
259			continue;
260		}
261
262		if (entry > 0)
263			show_one();
264		else
265			show();
266
267		gpt_close(fd);
268	}
269
270	return (0);
271}
272