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