show.c revision 1.23
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.23 2015/11/29 14:12:56 christos 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_UNUSED:
94			printf("Unused");
95			break;
96		case MAP_TYPE_MBR:
97			if (m->map_start != 0)
98				printf("Extended ");
99			printf("MBR");
100			break;
101		case MAP_TYPE_PRI_GPT_HDR:
102			printf("Pri GPT header");
103			break;
104		case MAP_TYPE_SEC_GPT_HDR:
105			printf("Sec GPT header");
106			break;
107		case MAP_TYPE_PRI_GPT_TBL:
108			printf("Pri GPT table");
109			break;
110		case MAP_TYPE_SEC_GPT_TBL:
111			printf("Sec GPT table");
112			break;
113		case MAP_TYPE_MBR_PART:
114			p = m->map_data;
115			if (p->map_start != 0)
116				printf("Extended ");
117			printf("MBR part ");
118			mbr = p->map_data;
119			for (i = 0; i < 4; i++) {
120				start = le16toh(mbr->mbr_part[i].part_start_hi);
121				start = (start << 16) +
122				    le16toh(mbr->mbr_part[i].part_start_lo);
123				if (m->map_start == p->map_start + start)
124					break;
125			}
126			printf("%d", mbr->mbr_part[i].part_typ);
127			break;
128		case MAP_TYPE_GPT_PART:
129			printf("GPT part ");
130			ent = m->map_data;
131			if (show_label) {
132				printf("- \"%s\"",
133				    utf16_to_utf8(ent->ent_name));
134			} else if (show_guid) {
135				char buf[128];
136				gpt_uuid_snprintf(
137				    buf, sizeof(buf), "%d", ent->ent_guid);
138				printf("- %s", buf);
139			} else {
140				char buf[128];
141				if (show_uuid || gpt_uuid_snprintf(buf,
142				    sizeof(buf), "%ls", ent->ent_type) == -1)
143					gpt_uuid_snprintf(buf, sizeof(buf),
144					    "%d", ent->ent_type);
145				printf("- %s", buf);
146			}
147			break;
148		case MAP_TYPE_PMBR:
149			printf("PMBR");
150			break;
151		default:
152			printf("Unknown %#x", m->map_type);
153			break;
154		}
155		putchar('\n');
156		m = m->map_next;
157	}
158}
159
160static void
161show_one(void)
162{
163	map_t *m;
164	struct gpt_ent *ent;
165	char s1[128], s2[128];
166#ifdef HN_AUTOSCALE
167	char human_num[5];
168#endif
169
170	for (m = map_first(); m != NULL; m = m->map_next)
171		if (entry == m->map_index)
172			break;
173	if (m == NULL) {
174		warnx("%s: error: could not find index %d",
175		    device_name, entry);
176		return;
177	}
178	ent = m->map_data;
179
180	printf("Details for index %d:\n", entry);
181#ifdef HN_AUTOSCALE
182	if (humanize_number(human_num, 5, (int64_t)(m->map_start * secsz),
183	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
184		human_num[0] = '\0';
185	if (human_num[0] != '\0')
186		printf("Start: %llu (%s)\n", (long long)m->map_start,
187		    human_num);
188	else
189#endif
190		printf("Start: %llu\n", (long long)m->map_start);
191#ifdef HN_AUTOSCALE
192	if (humanize_number(human_num, 5, (int64_t)(m->map_size * secsz),
193	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
194		human_num[0] = '\0';
195	if (human_num[0] != '\0')
196		printf("Size: %llu (%s)\n", (long long)m->map_size, human_num);
197	else
198#endif
199		printf("Size: %llu\n", (long long)m->map_size);
200
201	gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
202	gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
203	if (strcmp(s1, s2) == 0)
204		strlcpy(s1, "unknown", sizeof(s1));
205	printf("Type: %s (%s)\n", s1, s2);
206
207	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
208	printf("GUID: %s\n", s2);
209
210	printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
211
212	printf("Attributes:\n");
213	if (ent->ent_attr == 0)
214		printf("  None\n");
215	else {
216		if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
217			printf("  required for platform to function\n");
218		if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
219			printf("  UEFI won't recognize file system\n");
220		if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
221			printf("  legacy BIOS boot partition\n");
222		if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
223			printf("  indicates a bootable partition\n");
224		if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
225			printf("  attempt to boot this partition only once\n");
226		if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
227			printf("  partition that was marked bootonce but failed to boot\n");
228	}
229}
230
231int
232cmd_show(int argc, char *argv[])
233{
234	char *p;
235	int ch, fd;
236
237	while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
238		switch(ch) {
239		case 'g':
240			show_guid = 1;
241			break;
242		case 'i':
243			if (entry > 0)
244				usage_show();
245			entry = strtoul(optarg, &p, 10);
246			if (*p != 0 || entry < 1)
247				usage_show();
248			break;
249		case 'l':
250			show_label = 1;
251			break;
252		case 'u':
253			show_uuid = 1;
254			break;
255		default:
256			usage_show();
257		}
258	}
259
260	if (argc == optind)
261		usage_show();
262
263	while (optind < argc) {
264		fd = gpt_open(argv[optind++], 0);
265		if (fd == -1)
266			continue;
267
268		if (entry > 0)
269			show_one();
270		else
271			show();
272
273		gpt_close(fd);
274	}
275
276	return (0);
277}
278