show.c revision 1.11
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.11 2013/10/19 02:07:08 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
47static int show_label = 0;
48static int show_uuid = 0;
49
50const char showmsg[] = "show [-lu] device ...";
51
52__dead static void
53usage_show(void)
54{
55
56	fprintf(stderr,
57	    "usage: %s %s\n", getprogname(), showmsg);
58	exit(1);
59}
60
61static const char *
62friendly(uuid_t *t)
63{
64	static const uuid_t efi_slice = GPT_ENT_TYPE_EFI;
65	static const uuid_t bios_boot = GPT_ENT_TYPE_BIOS;
66	static const uuid_t msdata = GPT_ENT_TYPE_MS_BASIC_DATA;
67	static const uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
68	static const uuid_t hfs = GPT_ENT_TYPE_APPLE_HFS;
69	static const uuid_t linuxdata = GPT_ENT_TYPE_LINUX_DATA;
70	static const uuid_t linuxswap = GPT_ENT_TYPE_LINUX_SWAP;
71	static const uuid_t msr = GPT_ENT_TYPE_MS_RESERVED;
72	static const uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
73	static const uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
74	static const uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
75	static const uuid_t zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
76	static const uuid_t nb_swap = GPT_ENT_TYPE_NETBSD_SWAP;
77	static const uuid_t nb_ffs = GPT_ENT_TYPE_NETBSD_FFS;
78	static const uuid_t nb_lfs = GPT_ENT_TYPE_NETBSD_LFS;
79	static const uuid_t nb_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
80	static const uuid_t nb_ccd = GPT_ENT_TYPE_NETBSD_CCD;
81	static const uuid_t nb_cgd = GPT_ENT_TYPE_NETBSD_CGD;
82	static char buf[80];
83	char *s;
84
85	if (show_uuid)
86		goto unfriendly;
87
88	if (uuid_equal(t, &efi_slice, NULL))
89		return ("EFI System");
90	if (uuid_equal(t, &bios_boot, NULL))
91		return ("BIOS Boot");
92	if (uuid_equal(t, &nb_swap, NULL))
93		return ("NetBSD swap");
94	if (uuid_equal(t, &nb_ffs, NULL))
95		return ("NetBSD FFSv1/FFSv2");
96	if (uuid_equal(t, &nb_lfs, NULL))
97		return ("NetBSD LFS");
98	if (uuid_equal(t, &nb_raid, NULL))
99		return ("NetBSD RAIDFrame component");
100	if (uuid_equal(t, &nb_ccd, NULL))
101		return ("NetBSD ccd component");
102	if (uuid_equal(t, &nb_cgd, NULL))
103		return ("NetBSD Cryptographic Disk");
104	if (uuid_equal(t, &swap, NULL))
105		return ("FreeBSD swap");
106	if (uuid_equal(t, &ufs, NULL))
107		return ("FreeBSD UFS/UFS2");
108	if (uuid_equal(t, &vinum, NULL))
109		return ("FreeBSD vinum");
110	if (uuid_equal(t, &zfs, NULL))
111		return ("FreeBSD ZFS");
112	if (uuid_equal(t, &freebsd, NULL))
113		return ("FreeBSD legacy");
114	if (uuid_equal(t, &msdata, NULL))
115		return ("Windows basic data");
116	if (uuid_equal(t, &msr, NULL))
117		return ("Windows reserved");
118	if (uuid_equal(t, &linuxdata, NULL))
119		return ("Linux data");
120	if (uuid_equal(t, &linuxswap, NULL))
121		return ("Linux swap");
122	if (uuid_equal(t, &hfs, NULL))
123		return ("Apple HFS");
124
125unfriendly:
126	uuid_to_string(t, &s, NULL);
127	strlcpy(buf, s, sizeof buf);
128	free(s);
129	return (buf);
130}
131
132static void
133show(int fd __unused)
134{
135	uuid_t type;
136	off_t start;
137	map_t *m, *p;
138	struct mbr *mbr;
139	struct gpt_ent *ent;
140	unsigned int i;
141
142	printf("  %*s", lbawidth, "start");
143	printf("  %*s", lbawidth, "size");
144	printf("  index  contents\n");
145
146	m = map_first();
147	while (m != NULL) {
148		printf("  %*llu", lbawidth, (long long)m->map_start);
149		printf("  %*llu", lbawidth, (long long)m->map_size);
150		putchar(' ');
151		putchar(' ');
152		if (m->map_index > 0)
153			printf("%5d", m->map_index);
154		else
155			printf("     ");
156		putchar(' ');
157		putchar(' ');
158		switch (m->map_type) {
159		case MAP_TYPE_MBR:
160			if (m->map_start != 0)
161				printf("Extended ");
162			printf("MBR");
163			break;
164		case MAP_TYPE_PRI_GPT_HDR:
165			printf("Pri GPT header");
166			break;
167		case MAP_TYPE_SEC_GPT_HDR:
168			printf("Sec GPT header");
169			break;
170		case MAP_TYPE_PRI_GPT_TBL:
171			printf("Pri GPT table");
172			break;
173		case MAP_TYPE_SEC_GPT_TBL:
174			printf("Sec GPT table");
175			break;
176		case MAP_TYPE_MBR_PART:
177			p = m->map_data;
178			if (p->map_start != 0)
179				printf("Extended ");
180			printf("MBR part ");
181			mbr = p->map_data;
182			for (i = 0; i < 4; i++) {
183				start = le16toh(mbr->mbr_part[i].part_start_hi);
184				start = (start << 16) +
185				    le16toh(mbr->mbr_part[i].part_start_lo);
186				if (m->map_start == p->map_start + start)
187					break;
188			}
189			printf("%d", mbr->mbr_part[i].part_typ);
190			break;
191		case MAP_TYPE_GPT_PART:
192			printf("GPT part ");
193			ent = m->map_data;
194			if (show_label) {
195				printf("- \"%s\"",
196				    utf16_to_utf8(ent->ent_name));
197			} else {
198				le_uuid_dec(ent->ent_type, &type);
199				printf("- %s", friendly(&type));
200			}
201			break;
202		case MAP_TYPE_PMBR:
203			printf("PMBR");
204			break;
205		}
206		putchar('\n');
207		m = m->map_next;
208	}
209}
210
211int
212cmd_show(int argc, char *argv[])
213{
214	int ch, fd;
215
216	while ((ch = getopt(argc, argv, "lu")) != -1) {
217		switch(ch) {
218		case 'l':
219			show_label = 1;
220			break;
221		case 'u':
222			show_uuid = 1;
223			break;
224		default:
225			usage_show();
226		}
227	}
228
229	if (argc == optind)
230		usage_show();
231
232	while (optind < argc) {
233		fd = gpt_open(argv[optind++]);
234		if (fd == -1) {
235			warn("unable to open device '%s'", device_name);
236			continue;
237		}
238
239		show(fd);
240
241		gpt_close(fd);
242	}
243
244	return (0);
245}
246