show.c revision 1.14 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 2002 Marcel Moolenaar
3 1.1 christos * All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that the following conditions
7 1.1 christos * are met:
8 1.1 christos *
9 1.1 christos * 1. Redistributions of source code must retain the above copyright
10 1.1 christos * notice, this list of conditions and the following disclaimer.
11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer in the
13 1.1 christos * documentation and/or other materials provided with the distribution.
14 1.1 christos *
15 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 christos */
26 1.1 christos
27 1.1 christos #include <sys/cdefs.h>
28 1.2 christos #ifdef __FBSDID
29 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
30 1.2 christos #endif
31 1.2 christos #ifdef __RCSID
32 1.14 jnemeth __RCSID("$NetBSD: show.c,v 1.14 2013/12/09 01:35:02 jnemeth Exp $");
33 1.2 christos #endif
34 1.1 christos
35 1.1 christos #include <sys/types.h>
36 1.1 christos
37 1.1 christos #include <err.h>
38 1.1 christos #include <stddef.h>
39 1.1 christos #include <stdio.h>
40 1.1 christos #include <stdlib.h>
41 1.1 christos #include <string.h>
42 1.1 christos #include <unistd.h>
43 1.1 christos
44 1.1 christos #include "map.h"
45 1.1 christos #include "gpt.h"
46 1.1 christos
47 1.1 christos static int show_label = 0;
48 1.1 christos static int show_uuid = 0;
49 1.12 jnemeth static int show_guid = 0;
50 1.12 jnemeth static unsigned int entry = 0;
51 1.1 christos
52 1.12 jnemeth const char showmsg[] = "show [-glu] [-i index] device ...";
53 1.3 riz
54 1.7 joerg __dead static void
55 1.1 christos usage_show(void)
56 1.1 christos {
57 1.1 christos
58 1.1 christos fprintf(stderr,
59 1.3 riz "usage: %s %s\n", getprogname(), showmsg);
60 1.1 christos exit(1);
61 1.1 christos }
62 1.1 christos
63 1.1 christos static const char *
64 1.1 christos friendly(uuid_t *t)
65 1.1 christos {
66 1.10 jakllsch static const uuid_t efi_slice = GPT_ENT_TYPE_EFI;
67 1.10 jakllsch static const uuid_t bios_boot = GPT_ENT_TYPE_BIOS;
68 1.10 jakllsch static const uuid_t msdata = GPT_ENT_TYPE_MS_BASIC_DATA;
69 1.10 jakllsch static const uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
70 1.10 jakllsch static const uuid_t hfs = GPT_ENT_TYPE_APPLE_HFS;
71 1.10 jakllsch static const uuid_t linuxdata = GPT_ENT_TYPE_LINUX_DATA;
72 1.10 jakllsch static const uuid_t linuxswap = GPT_ENT_TYPE_LINUX_SWAP;
73 1.10 jakllsch static const uuid_t msr = GPT_ENT_TYPE_MS_RESERVED;
74 1.10 jakllsch static const uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
75 1.10 jakllsch static const uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
76 1.10 jakllsch static const uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
77 1.11 jnemeth static const uuid_t zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
78 1.10 jakllsch static const uuid_t nb_swap = GPT_ENT_TYPE_NETBSD_SWAP;
79 1.10 jakllsch static const uuid_t nb_ffs = GPT_ENT_TYPE_NETBSD_FFS;
80 1.10 jakllsch static const uuid_t nb_lfs = GPT_ENT_TYPE_NETBSD_LFS;
81 1.10 jakllsch static const uuid_t nb_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
82 1.10 jakllsch static const uuid_t nb_ccd = GPT_ENT_TYPE_NETBSD_CCD;
83 1.10 jakllsch static const uuid_t nb_cgd = GPT_ENT_TYPE_NETBSD_CGD;
84 1.1 christos static char buf[80];
85 1.1 christos char *s;
86 1.1 christos
87 1.1 christos if (show_uuid)
88 1.1 christos goto unfriendly;
89 1.1 christos
90 1.1 christos if (uuid_equal(t, &efi_slice, NULL))
91 1.1 christos return ("EFI System");
92 1.5 christos if (uuid_equal(t, &bios_boot, NULL))
93 1.5 christos return ("BIOS Boot");
94 1.4 riz if (uuid_equal(t, &nb_swap, NULL))
95 1.4 riz return ("NetBSD swap");
96 1.6 jakllsch if (uuid_equal(t, &nb_ffs, NULL))
97 1.6 jakllsch return ("NetBSD FFSv1/FFSv2");
98 1.4 riz if (uuid_equal(t, &nb_lfs, NULL))
99 1.4 riz return ("NetBSD LFS");
100 1.4 riz if (uuid_equal(t, &nb_raid, NULL))
101 1.4 riz return ("NetBSD RAIDFrame component");
102 1.4 riz if (uuid_equal(t, &nb_ccd, NULL))
103 1.4 riz return ("NetBSD ccd component");
104 1.4 riz if (uuid_equal(t, &nb_cgd, NULL))
105 1.4 riz return ("NetBSD Cryptographic Disk");
106 1.1 christos if (uuid_equal(t, &swap, NULL))
107 1.1 christos return ("FreeBSD swap");
108 1.1 christos if (uuid_equal(t, &ufs, NULL))
109 1.1 christos return ("FreeBSD UFS/UFS2");
110 1.1 christos if (uuid_equal(t, &vinum, NULL))
111 1.1 christos return ("FreeBSD vinum");
112 1.11 jnemeth if (uuid_equal(t, &zfs, NULL))
113 1.11 jnemeth return ("FreeBSD ZFS");
114 1.1 christos if (uuid_equal(t, &freebsd, NULL))
115 1.1 christos return ("FreeBSD legacy");
116 1.8 jakllsch if (uuid_equal(t, &msdata, NULL))
117 1.8 jakllsch return ("Windows basic data");
118 1.8 jakllsch if (uuid_equal(t, &msr, NULL))
119 1.8 jakllsch return ("Windows reserved");
120 1.8 jakllsch if (uuid_equal(t, &linuxdata, NULL))
121 1.8 jakllsch return ("Linux data");
122 1.1 christos if (uuid_equal(t, &linuxswap, NULL))
123 1.1 christos return ("Linux swap");
124 1.1 christos if (uuid_equal(t, &hfs, NULL))
125 1.1 christos return ("Apple HFS");
126 1.1 christos
127 1.1 christos unfriendly:
128 1.1 christos uuid_to_string(t, &s, NULL);
129 1.1 christos strlcpy(buf, s, sizeof buf);
130 1.1 christos free(s);
131 1.1 christos return (buf);
132 1.1 christos }
133 1.1 christos
134 1.1 christos static void
135 1.12 jnemeth show(void)
136 1.1 christos {
137 1.1 christos uuid_t type;
138 1.1 christos off_t start;
139 1.1 christos map_t *m, *p;
140 1.1 christos struct mbr *mbr;
141 1.1 christos struct gpt_ent *ent;
142 1.1 christos unsigned int i;
143 1.12 jnemeth char *s;
144 1.1 christos
145 1.1 christos printf(" %*s", lbawidth, "start");
146 1.1 christos printf(" %*s", lbawidth, "size");
147 1.1 christos printf(" index contents\n");
148 1.1 christos
149 1.1 christos m = map_first();
150 1.1 christos while (m != NULL) {
151 1.1 christos printf(" %*llu", lbawidth, (long long)m->map_start);
152 1.1 christos printf(" %*llu", lbawidth, (long long)m->map_size);
153 1.1 christos putchar(' ');
154 1.1 christos putchar(' ');
155 1.1 christos if (m->map_index > 0)
156 1.1 christos printf("%5d", m->map_index);
157 1.1 christos else
158 1.1 christos printf(" ");
159 1.1 christos putchar(' ');
160 1.1 christos putchar(' ');
161 1.1 christos switch (m->map_type) {
162 1.1 christos case MAP_TYPE_MBR:
163 1.1 christos if (m->map_start != 0)
164 1.1 christos printf("Extended ");
165 1.1 christos printf("MBR");
166 1.1 christos break;
167 1.1 christos case MAP_TYPE_PRI_GPT_HDR:
168 1.1 christos printf("Pri GPT header");
169 1.1 christos break;
170 1.1 christos case MAP_TYPE_SEC_GPT_HDR:
171 1.1 christos printf("Sec GPT header");
172 1.1 christos break;
173 1.1 christos case MAP_TYPE_PRI_GPT_TBL:
174 1.1 christos printf("Pri GPT table");
175 1.1 christos break;
176 1.1 christos case MAP_TYPE_SEC_GPT_TBL:
177 1.1 christos printf("Sec GPT table");
178 1.1 christos break;
179 1.1 christos case MAP_TYPE_MBR_PART:
180 1.1 christos p = m->map_data;
181 1.1 christos if (p->map_start != 0)
182 1.1 christos printf("Extended ");
183 1.1 christos printf("MBR part ");
184 1.1 christos mbr = p->map_data;
185 1.1 christos for (i = 0; i < 4; i++) {
186 1.1 christos start = le16toh(mbr->mbr_part[i].part_start_hi);
187 1.1 christos start = (start << 16) +
188 1.1 christos le16toh(mbr->mbr_part[i].part_start_lo);
189 1.1 christos if (m->map_start == p->map_start + start)
190 1.1 christos break;
191 1.1 christos }
192 1.1 christos printf("%d", mbr->mbr_part[i].part_typ);
193 1.1 christos break;
194 1.1 christos case MAP_TYPE_GPT_PART:
195 1.1 christos printf("GPT part ");
196 1.1 christos ent = m->map_data;
197 1.1 christos if (show_label) {
198 1.1 christos printf("- \"%s\"",
199 1.1 christos utf16_to_utf8(ent->ent_name));
200 1.12 jnemeth } else if (show_guid) {
201 1.12 jnemeth uuid_to_string((uuid_t *)ent->ent_guid,
202 1.12 jnemeth &s, NULL);
203 1.12 jnemeth printf("- %s", s);
204 1.12 jnemeth free(s);
205 1.1 christos } else {
206 1.9 jakllsch le_uuid_dec(ent->ent_type, &type);
207 1.1 christos printf("- %s", friendly(&type));
208 1.1 christos }
209 1.1 christos break;
210 1.1 christos case MAP_TYPE_PMBR:
211 1.1 christos printf("PMBR");
212 1.1 christos break;
213 1.1 christos }
214 1.1 christos putchar('\n');
215 1.1 christos m = m->map_next;
216 1.1 christos }
217 1.1 christos }
218 1.1 christos
219 1.12 jnemeth static void
220 1.12 jnemeth show_one(void)
221 1.12 jnemeth {
222 1.12 jnemeth uuid_t type;
223 1.12 jnemeth map_t *m;
224 1.12 jnemeth struct gpt_ent *ent;
225 1.12 jnemeth const char *s1;
226 1.14 jnemeth char *s2, human_num[5];
227 1.12 jnemeth
228 1.12 jnemeth for (m = map_first(); m != NULL; m = m->map_next)
229 1.12 jnemeth if (entry == m->map_index)
230 1.12 jnemeth break;
231 1.12 jnemeth if (m == NULL) {
232 1.12 jnemeth warnx("%s: error: could not find index %d",
233 1.12 jnemeth device_name, entry);
234 1.12 jnemeth return;
235 1.12 jnemeth }
236 1.12 jnemeth ent = m->map_data;
237 1.12 jnemeth
238 1.12 jnemeth printf("Details for index %d:\n", entry);
239 1.14 jnemeth if (humanize_number(human_num, 5, (int64_t)(m->map_start * secsz),
240 1.14 jnemeth "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
241 1.14 jnemeth human_num[0] = '\0';
242 1.14 jnemeth if (human_num[0] != '\0')
243 1.14 jnemeth printf("Start: %llu (%s)\n", (long long)m->map_start,
244 1.14 jnemeth human_num);
245 1.14 jnemeth else
246 1.14 jnemeth printf("Start: %llu\n", (long long)m->map_start);
247 1.14 jnemeth if (humanize_number(human_num, 5, (int64_t)(m->map_size * secsz),
248 1.14 jnemeth "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
249 1.14 jnemeth human_num[0] = '\0';
250 1.14 jnemeth if (human_num[0] != '\0')
251 1.14 jnemeth printf("Size: %llu (%s)\n", (long long)m->map_size, human_num);
252 1.14 jnemeth else
253 1.14 jnemeth printf("Size: %llu\n", (long long)m->map_size);
254 1.12 jnemeth
255 1.12 jnemeth le_uuid_dec(ent->ent_type, &type);
256 1.12 jnemeth s1 = friendly(&type);
257 1.12 jnemeth uuid_to_string(&type, &s2, NULL);
258 1.12 jnemeth if (strcmp(s1, s2) == 0)
259 1.12 jnemeth s1 = "unknown";
260 1.12 jnemeth printf("Type: %s (%s)\n", s1, s2);
261 1.12 jnemeth free(s2);
262 1.12 jnemeth
263 1.12 jnemeth uuid_to_string((uuid_t *)ent->ent_guid, &s2, NULL);
264 1.12 jnemeth printf("GUID: %s\n", s2);
265 1.12 jnemeth free(s2);
266 1.12 jnemeth
267 1.12 jnemeth printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
268 1.12 jnemeth
269 1.12 jnemeth printf("Attributes:\n");
270 1.12 jnemeth if (ent->ent_attr == 0)
271 1.12 jnemeth printf(" None\n");
272 1.12 jnemeth else {
273 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
274 1.12 jnemeth printf(" required for platform to function\n");
275 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
276 1.12 jnemeth printf(" UEFI won't recognize file system\n");
277 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
278 1.12 jnemeth printf(" legacy BIOS boot partition\n");
279 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
280 1.12 jnemeth printf(" indicates a bootable partition\n");
281 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
282 1.12 jnemeth printf(" attempt to boot this partition only once\n");
283 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
284 1.12 jnemeth printf(" partition that was marked bootonce but failed to boot\n");
285 1.12 jnemeth }
286 1.12 jnemeth }
287 1.12 jnemeth
288 1.1 christos int
289 1.1 christos cmd_show(int argc, char *argv[])
290 1.1 christos {
291 1.12 jnemeth char *p;
292 1.1 christos int ch, fd;
293 1.1 christos
294 1.12 jnemeth while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
295 1.1 christos switch(ch) {
296 1.12 jnemeth case 'g':
297 1.12 jnemeth show_guid = 1;
298 1.12 jnemeth break;
299 1.12 jnemeth case 'i':
300 1.12 jnemeth if (entry > 0)
301 1.12 jnemeth usage_show();
302 1.12 jnemeth entry = strtoul(optarg, &p, 10);
303 1.12 jnemeth if (*p != 0 || entry < 1)
304 1.12 jnemeth usage_show();
305 1.12 jnemeth break;
306 1.1 christos case 'l':
307 1.1 christos show_label = 1;
308 1.1 christos break;
309 1.1 christos case 'u':
310 1.1 christos show_uuid = 1;
311 1.1 christos break;
312 1.1 christos default:
313 1.1 christos usage_show();
314 1.1 christos }
315 1.1 christos }
316 1.1 christos
317 1.1 christos if (argc == optind)
318 1.1 christos usage_show();
319 1.1 christos
320 1.1 christos while (optind < argc) {
321 1.1 christos fd = gpt_open(argv[optind++]);
322 1.1 christos if (fd == -1) {
323 1.1 christos warn("unable to open device '%s'", device_name);
324 1.1 christos continue;
325 1.1 christos }
326 1.1 christos
327 1.13 jnemeth if (entry > 0)
328 1.12 jnemeth show_one();
329 1.12 jnemeth else
330 1.12 jnemeth show();
331 1.1 christos
332 1.1 christos gpt_close(fd);
333 1.1 christos }
334 1.1 christos
335 1.1 christos return (0);
336 1.1 christos }
337