xref: /netbsd-src/sbin/gpt/show.c (revision 6d322f2f4598f0d8a138f10ea648ec4fabe41f8b)
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 #include <sys/cdefs.h>
28 #ifdef __FBSDID
29 __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
30 #endif
31 #ifdef __RCSID
32 __RCSID("$NetBSD: show.c,v 1.15 2013/12/18 03:20:09 jnemeth Exp $");
33 #endif
34 
35 #include <sys/types.h>
36 
37 #include <err.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "map.h"
45 #include "gpt.h"
46 
47 static int show_label = 0;
48 static int show_uuid = 0;
49 static int show_guid = 0;
50 static unsigned int entry = 0;
51 
52 const char showmsg[] = "show [-glu] [-i index] device ...";
53 
54 __dead static void
55 usage_show(void)
56 {
57 
58 	fprintf(stderr,
59 	    "usage: %s %s\n", getprogname(), showmsg);
60 	exit(1);
61 }
62 
63 static const char *
64 friendly(uuid_t *t)
65 {
66 	static const uuid_t efi_slice = GPT_ENT_TYPE_EFI;
67 	static const uuid_t bios_boot = GPT_ENT_TYPE_BIOS;
68 	static const uuid_t msdata = GPT_ENT_TYPE_MS_BASIC_DATA;
69 	static const uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
70 	static const uuid_t hfs = GPT_ENT_TYPE_APPLE_HFS;
71 	static const uuid_t linuxdata = GPT_ENT_TYPE_LINUX_DATA;
72 	static const uuid_t linuxswap = GPT_ENT_TYPE_LINUX_SWAP;
73 	static const uuid_t msr = GPT_ENT_TYPE_MS_RESERVED;
74 	static const uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
75 	static const uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
76 	static const uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
77 	static const uuid_t zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
78 	static const uuid_t nb_swap = GPT_ENT_TYPE_NETBSD_SWAP;
79 	static const uuid_t nb_ffs = GPT_ENT_TYPE_NETBSD_FFS;
80 	static const uuid_t nb_lfs = GPT_ENT_TYPE_NETBSD_LFS;
81 	static const uuid_t nb_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
82 	static const uuid_t nb_ccd = GPT_ENT_TYPE_NETBSD_CCD;
83 	static const uuid_t nb_cgd = GPT_ENT_TYPE_NETBSD_CGD;
84 	static char buf[80];
85 	char *s;
86 
87 	if (show_uuid)
88 		goto unfriendly;
89 
90 	if (uuid_equal(t, &efi_slice, NULL))
91 		return ("EFI System");
92 	if (uuid_equal(t, &bios_boot, NULL))
93 		return ("BIOS Boot");
94 	if (uuid_equal(t, &nb_swap, NULL))
95 		return ("NetBSD swap");
96 	if (uuid_equal(t, &nb_ffs, NULL))
97 		return ("NetBSD FFSv1/FFSv2");
98 	if (uuid_equal(t, &nb_lfs, NULL))
99 		return ("NetBSD LFS");
100 	if (uuid_equal(t, &nb_raid, NULL))
101 		return ("NetBSD RAIDFrame component");
102 	if (uuid_equal(t, &nb_ccd, NULL))
103 		return ("NetBSD ccd component");
104 	if (uuid_equal(t, &nb_cgd, NULL))
105 		return ("NetBSD Cryptographic Disk");
106 	if (uuid_equal(t, &swap, NULL))
107 		return ("FreeBSD swap");
108 	if (uuid_equal(t, &ufs, NULL))
109 		return ("FreeBSD UFS/UFS2");
110 	if (uuid_equal(t, &vinum, NULL))
111 		return ("FreeBSD vinum");
112 	if (uuid_equal(t, &zfs, NULL))
113 		return ("FreeBSD ZFS");
114 	if (uuid_equal(t, &freebsd, NULL))
115 		return ("FreeBSD legacy");
116 	if (uuid_equal(t, &msdata, NULL))
117 		return ("Windows basic data");
118 	if (uuid_equal(t, &msr, NULL))
119 		return ("Windows reserved");
120 	if (uuid_equal(t, &linuxdata, NULL))
121 		return ("Linux data");
122 	if (uuid_equal(t, &linuxswap, NULL))
123 		return ("Linux swap");
124 	if (uuid_equal(t, &hfs, NULL))
125 		return ("Apple HFS");
126 
127 unfriendly:
128 	uuid_to_string(t, &s, NULL);
129 	strlcpy(buf, s, sizeof buf);
130 	free(s);
131 	return (buf);
132 }
133 
134 static void
135 show(void)
136 {
137 	uuid_t guid, type;
138 	off_t start;
139 	map_t *m, *p;
140 	struct mbr *mbr;
141 	struct gpt_ent *ent;
142 	unsigned int i;
143 	char *s;
144 
145 	printf("  %*s", lbawidth, "start");
146 	printf("  %*s", lbawidth, "size");
147 	printf("  index  contents\n");
148 
149 	m = map_first();
150 	while (m != NULL) {
151 		printf("  %*llu", lbawidth, (long long)m->map_start);
152 		printf("  %*llu", lbawidth, (long long)m->map_size);
153 		putchar(' ');
154 		putchar(' ');
155 		if (m->map_index > 0)
156 			printf("%5d", m->map_index);
157 		else
158 			printf("     ");
159 		putchar(' ');
160 		putchar(' ');
161 		switch (m->map_type) {
162 		case MAP_TYPE_MBR:
163 			if (m->map_start != 0)
164 				printf("Extended ");
165 			printf("MBR");
166 			break;
167 		case MAP_TYPE_PRI_GPT_HDR:
168 			printf("Pri GPT header");
169 			break;
170 		case MAP_TYPE_SEC_GPT_HDR:
171 			printf("Sec GPT header");
172 			break;
173 		case MAP_TYPE_PRI_GPT_TBL:
174 			printf("Pri GPT table");
175 			break;
176 		case MAP_TYPE_SEC_GPT_TBL:
177 			printf("Sec GPT table");
178 			break;
179 		case MAP_TYPE_MBR_PART:
180 			p = m->map_data;
181 			if (p->map_start != 0)
182 				printf("Extended ");
183 			printf("MBR part ");
184 			mbr = p->map_data;
185 			for (i = 0; i < 4; i++) {
186 				start = le16toh(mbr->mbr_part[i].part_start_hi);
187 				start = (start << 16) +
188 				    le16toh(mbr->mbr_part[i].part_start_lo);
189 				if (m->map_start == p->map_start + start)
190 					break;
191 			}
192 			printf("%d", mbr->mbr_part[i].part_typ);
193 			break;
194 		case MAP_TYPE_GPT_PART:
195 			printf("GPT part ");
196 			ent = m->map_data;
197 			if (show_label) {
198 				printf("- \"%s\"",
199 				    utf16_to_utf8(ent->ent_name));
200 			} else if (show_guid) {
201 				le_uuid_dec(ent->ent_guid, &guid);
202 				uuid_to_string(&guid, &s, NULL);
203 				printf("- %s", s);
204 				free(s);
205 			} else {
206 				le_uuid_dec(ent->ent_type, &type);
207 				printf("- %s", friendly(&type));
208 			}
209 			break;
210 		case MAP_TYPE_PMBR:
211 			printf("PMBR");
212 			break;
213 		}
214 		putchar('\n');
215 		m = m->map_next;
216 	}
217 }
218 
219 static void
220 show_one(void)
221 {
222 	uuid_t guid, type;
223 	map_t *m;
224 	struct gpt_ent *ent;
225 	const char *s1;
226 	char *s2, human_num[5];
227 
228 	for (m = map_first(); m != NULL; m = m->map_next)
229 		if (entry == m->map_index)
230 			break;
231 	if (m == NULL) {
232 		warnx("%s: error: could not find index %d",
233 		    device_name, entry);
234 		return;
235 	}
236 	ent = m->map_data;
237 
238 	printf("Details for index %d:\n", entry);
239 	if (humanize_number(human_num, 5, (int64_t)(m->map_start * secsz),
240 	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
241 		human_num[0] = '\0';
242 	if (human_num[0] != '\0')
243 		printf("Start: %llu (%s)\n", (long long)m->map_start,
244 		    human_num);
245 	else
246 		printf("Start: %llu\n", (long long)m->map_start);
247 	if (humanize_number(human_num, 5, (int64_t)(m->map_size * secsz),
248 	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
249 		human_num[0] = '\0';
250 	if (human_num[0] != '\0')
251 		printf("Size: %llu (%s)\n", (long long)m->map_size, human_num);
252 	else
253 		printf("Size: %llu\n", (long long)m->map_size);
254 
255 	le_uuid_dec(ent->ent_type, &type);
256 	s1 = friendly(&type);
257 	uuid_to_string(&type, &s2, NULL);
258 	if (strcmp(s1, s2) == 0)
259 		s1 = "unknown";
260 	printf("Type: %s (%s)\n", s1, s2);
261 	free(s2);
262 
263 	le_uuid_dec(ent->ent_guid, &guid);
264 	uuid_to_string(&guid, &s2, NULL);
265 	printf("GUID: %s\n", s2);
266 	free(s2);
267 
268 	printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
269 
270 	printf("Attributes:\n");
271 	if (ent->ent_attr == 0)
272 		printf("  None\n");
273 	else {
274 		if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
275 			printf("  required for platform to function\n");
276 		if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
277 			printf("  UEFI won't recognize file system\n");
278 		if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
279 			printf("  legacy BIOS boot partition\n");
280 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
281 			printf("  indicates a bootable partition\n");
282 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
283 			printf("  attempt to boot this partition only once\n");
284 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
285 			printf("  partition that was marked bootonce but failed to boot\n");
286 	}
287 }
288 
289 int
290 cmd_show(int argc, char *argv[])
291 {
292 	char *p;
293 	int ch, fd;
294 
295 	while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
296 		switch(ch) {
297 		case 'g':
298 			show_guid = 1;
299 			break;
300 		case 'i':
301 			if (entry > 0)
302 				usage_show();
303 			entry = strtoul(optarg, &p, 10);
304 			if (*p != 0 || entry < 1)
305 				usage_show();
306 			break;
307 		case 'l':
308 			show_label = 1;
309 			break;
310 		case 'u':
311 			show_uuid = 1;
312 			break;
313 		default:
314 			usage_show();
315 		}
316 	}
317 
318 	if (argc == optind)
319 		usage_show();
320 
321 	while (optind < argc) {
322 		fd = gpt_open(argv[optind++]);
323 		if (fd == -1) {
324 			warn("unable to open device '%s'", device_name);
325 			continue;
326 		}
327 
328 		if (entry > 0)
329 			show_one();
330 		else
331 			show();
332 
333 		gpt_close(fd);
334 	}
335 
336 	return (0);
337 }
338