xref: /netbsd-src/sbin/gpt/show.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
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.39 2016/10/05 03:06:24 kre Exp $");
37 #endif
38 
39 #include <sys/bootblock.h>
40 #include <sys/types.h>
41 
42 #include <err.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 
50 #include "map.h"
51 #include "gpt.h"
52 #include "gpt_private.h"
53 
54 static int cmd_show(gpt_t, int, char *[]);
55 
56 static const char *showhelp[] = {
57 	"[-aglu] [-i index]",
58 };
59 
60 #define SHOW_UUID  1
61 #define SHOW_GUID  2
62 #define SHOW_LABEL 4
63 #define SHOW_ALL   8
64 
65 struct gpt_cmd c_show = {
66 	"show",
67 	cmd_show,
68 	showhelp, __arraycount(showhelp),
69 	GPT_READONLY,
70 };
71 
72 #define usage() gpt_usage(NULL, &c_show)
73 
74 static void
75 print_part_type(int map_type, int flags, void *map_data, off_t map_start)
76 {
77 	off_t start;
78 	map_t p;
79 	struct mbr *mbr;
80 	struct gpt_ent *ent;
81 	unsigned int i;
82 	char buf[128], *b = buf;
83 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
84 
85 	switch (map_type) {
86 	case MAP_TYPE_UNUSED:
87 		printf("Unused");
88 		break;
89 	case MAP_TYPE_MBR:
90 		if (map_start != 0)
91 			printf("Extended ");
92 		printf("MBR");
93 		break;
94 	case MAP_TYPE_PRI_GPT_HDR:
95 		printf("Pri GPT header");
96 		break;
97 	case MAP_TYPE_SEC_GPT_HDR:
98 		printf("Sec GPT header");
99 		break;
100 	case MAP_TYPE_PRI_GPT_TBL:
101 		printf("Pri GPT table");
102 		break;
103 	case MAP_TYPE_SEC_GPT_TBL:
104 		printf("Sec GPT table");
105 		break;
106 	case MAP_TYPE_MBR_PART:
107 		p = map_data;
108 		if (p->map_start != 0)
109 			printf("Extended ");
110 		printf("MBR part ");
111 		mbr = p->map_data;
112 		for (i = 0; i < 4; i++) {
113 			start = le16toh(mbr->mbr_part[i].part_start_hi);
114 			start = (start << 16) +
115 			    le16toh(mbr->mbr_part[i].part_start_lo);
116 			if (map_start == p->map_start + start)
117 				break;
118 		}
119 		if (i == 4) {
120 			/* wasn't there */
121 			printf("[partition not found?]");
122 		} else {
123 			printf("%d%s", mbr->mbr_part[i].part_typ,
124 			    mbr->mbr_part[i].part_flag == 0x80 ?
125 			    " (active)" : "");
126 		}
127 		break;
128 	case MAP_TYPE_GPT_PART:
129 		printf("GPT part ");
130 		ent = map_data;
131 		if (flags & SHOW_LABEL) {
132 			utf16_to_utf8(ent->ent_name, utfbuf,
133 			    sizeof(utfbuf));
134 			b = (char *)utfbuf;
135 		} else if (flags & SHOW_GUID) {
136 			gpt_uuid_snprintf( buf, sizeof(buf), "%d",
137 			    ent->ent_guid);
138 		} else if (flags & SHOW_UUID) {
139 			gpt_uuid_snprintf(buf, sizeof(buf),
140 			    "%d", ent->ent_type);
141 		} else {
142 			gpt_uuid_snprintf(buf, sizeof(buf), "%ls",
143 			    ent->ent_type);
144 		}
145 		printf("- %s", b);
146 		break;
147 	case MAP_TYPE_PMBR:
148 		printf("PMBR");
149 		mbr = map_data;
150 		if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR &&
151 		    mbr->mbr_part[0].part_flag == 0x80)
152 			    printf(" (active)");
153 		break;
154 	default:
155 		printf("Unknown %#x", map_type);
156 		break;
157 	}
158 }
159 
160 static int
161 show(gpt_t gpt, int xshow)
162 {
163 	map_t m;
164 
165 	printf("  %*s", gpt->lbawidth, "start");
166 	printf("  %*s", gpt->lbawidth, "size");
167 	printf("  index  contents\n");
168 
169 	m = map_first(gpt);
170 	while (m != NULL) {
171 		printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
172 		printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
173 		putchar(' ');
174 		putchar(' ');
175 		if (m->map_index > 0)
176 			printf("%5d", m->map_index);
177 		else
178 			printf("     ");
179 		putchar(' ');
180 		putchar(' ');
181 		print_part_type(m->map_type, xshow, m->map_data, m->map_start);
182 		putchar('\n');
183 		m = m->map_next;
184 	}
185 	return 0;
186 }
187 
188 static int
189 show_one(gpt_t gpt, unsigned int entry)
190 {
191 	map_t m;
192 	struct gpt_ent *ent;
193 	char s1[128], s2[128];
194 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
195 
196 	for (m = map_first(gpt); m != NULL; m = m->map_next)
197 		if (entry == m->map_index)
198 			break;
199 	if (m == NULL) {
200 		gpt_warnx(gpt, "Could not find index %d", entry);
201 		return -1;
202 	}
203 	ent = m->map_data;
204 
205 	printf("Details for index %d:\n", entry);
206 	gpt_show_num("Start", (uintmax_t)(m->map_start * gpt->secsz));
207 	gpt_show_num("Size", (uintmax_t)(m->map_size * gpt->secsz));
208 
209 	gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
210 	gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
211 	if (strcmp(s1, s2) == 0)
212 		strlcpy(s1, "unknown", sizeof(s1));
213 	printf("Type: %s (%s)\n", s1, s2);
214 
215 	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
216 	printf("GUID: %s\n", s2);
217 
218 	utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
219 	printf("Label: %s\n", (char *)utfbuf);
220 
221 	printf("Attributes: ");
222 	if (ent->ent_attr == 0) {
223 		printf("None\n");
224 	} else  {
225 		char buf[1024];
226 		printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
227 	}
228 
229 	return 0;
230 }
231 
232 static int
233 show_all(gpt_t gpt)
234 {
235 	map_t m;
236 	struct gpt_ent *ent;
237 	char s1[128], s2[128];
238 #ifdef HN_AUTOSCALE
239 	char human_num[8];
240 #endif
241 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
242 #define PFX "                                 "
243 
244 	printf("  %*s", gpt->lbawidth, "start");
245 	printf("  %*s", gpt->lbawidth, "size");
246 	printf("  index  contents\n");
247 
248 	m = map_first(gpt);
249 	while (m != NULL) {
250 		printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
251 		printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
252 		putchar(' ');
253 		putchar(' ');
254 		if (m->map_index > 0) {
255 			printf("%5d  ", m->map_index);
256 			print_part_type(m->map_type, 0, m->map_data,
257 			    m->map_start);
258 			putchar('\n');
259 
260 			ent = m->map_data;
261 
262 			gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
263 			gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
264 			if (strcmp(s1, s2) == 0)
265 				strlcpy(s1, "unknown", sizeof(s1));
266 			printf(PFX "Type: %s\n", s1);
267 			printf(PFX "TypeID: %s\n", s2);
268 
269 			gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
270 			printf(PFX "GUID: %s\n", s2);
271 
272 			printf(PFX "Size: ");
273 #ifdef HN_AUTOSCALE
274 			if (humanize_number(human_num, sizeof(human_num),
275 			    (int64_t)(m->map_size * gpt->secsz),
276 			    "", HN_AUTOSCALE, HN_B) < 0) {
277 #endif
278 				printf("%ju",
279 				    (int64_t)(m->map_size * gpt->secsz));
280 #ifdef HN_AUTOSCALE
281 			} else {
282 				printf("%s", human_num);
283 			}
284 #endif
285 			putchar('\n');
286 
287 			utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
288 			printf(PFX "Label: %s\n", (char *)utfbuf);
289 
290 			printf(PFX "Attributes: ");
291 			if (ent->ent_attr == 0) {
292 				printf("None\n");
293 			} else  {
294 				char buf[1024];
295 
296 				printf("%s\n", gpt_attr_list(buf, sizeof(buf),
297 				    ent->ent_attr));
298 			}
299 		} else {
300 			printf("       ");
301 			print_part_type(m->map_type, 0, m->map_data,
302 			    m->map_start);
303 			putchar('\n');
304 		}
305 		m = m->map_next;
306 	}
307 	return 0;
308 }
309 
310 static int
311 cmd_show(gpt_t gpt, int argc, char *argv[])
312 {
313 	int ch;
314 	int xshow = 0;
315 	unsigned int entry = 0;
316 
317 	while ((ch = getopt(argc, argv, "gi:lua")) != -1) {
318 		switch(ch) {
319 		case 'a':
320 			xshow |= SHOW_ALL;
321 			break;
322 		case 'g':
323 			xshow |= SHOW_GUID;
324 			break;
325 		case 'i':
326 			if (gpt_uint_get(gpt, &entry) == -1)
327 				return usage();
328 			break;
329 		case 'l':
330 			xshow |= SHOW_LABEL;
331 			break;
332 		case 'u':
333 			xshow |= SHOW_UUID;
334 			break;
335 		default:
336 			return usage();
337 		}
338 	}
339 
340 	if (argc != optind)
341 		return usage();
342 
343 	if (xshow & SHOW_ALL)
344 		return show_all(gpt);
345 
346 	return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
347 }
348