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