show.c revision 1.45 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.45 mlelstv __RCSID("$NetBSD: show.c,v 1.45 2024/09/13 11:07:35 mlelstv Exp $");
37 1.2 christos #endif
38 1.1 christos
39 1.38 kre #include <sys/bootblock.h>
40 1.1 christos #include <sys/types.h>
41 1.1 christos
42 1.1 christos #include <err.h>
43 1.1 christos #include <stddef.h>
44 1.1 christos #include <stdio.h>
45 1.1 christos #include <stdlib.h>
46 1.1 christos #include <string.h>
47 1.1 christos #include <unistd.h>
48 1.1 christos
49 1.38 kre
50 1.1 christos #include "map.h"
51 1.1 christos #include "gpt.h"
52 1.24 christos #include "gpt_private.h"
53 1.1 christos
54 1.25 christos static int cmd_show(gpt_t, int, char *[]);
55 1.3 riz
56 1.25 christos static const char *showhelp[] = {
57 1.33 wiz "[-aglu] [-i index]",
58 1.25 christos };
59 1.25 christos
60 1.28 christos #define SHOW_UUID 1
61 1.28 christos #define SHOW_GUID 2
62 1.28 christos #define SHOW_LABEL 4
63 1.32 martin #define SHOW_ALL 8
64 1.28 christos
65 1.25 christos struct gpt_cmd c_show = {
66 1.25 christos "show",
67 1.25 christos cmd_show,
68 1.25 christos showhelp, __arraycount(showhelp),
69 1.25 christos GPT_READONLY,
70 1.25 christos };
71 1.1 christos
72 1.25 christos #define usage() gpt_usage(NULL, &c_show)
73 1.1 christos
74 1.32 martin static void
75 1.32 martin print_part_type(int map_type, int flags, void *map_data, off_t map_start)
76 1.1 christos {
77 1.1 christos off_t start;
78 1.32 martin map_t p;
79 1.1 christos struct mbr *mbr;
80 1.1 christos struct gpt_ent *ent;
81 1.1 christos unsigned int i;
82 1.28 christos char buf[128], *b = buf;
83 1.27 christos uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
84 1.1 christos
85 1.32 martin switch (map_type) {
86 1.32 martin case MAP_TYPE_UNUSED:
87 1.32 martin printf("Unused");
88 1.32 martin break;
89 1.32 martin case MAP_TYPE_MBR:
90 1.32 martin if (map_start != 0)
91 1.32 martin printf("Extended ");
92 1.32 martin printf("MBR");
93 1.32 martin break;
94 1.32 martin case MAP_TYPE_PRI_GPT_HDR:
95 1.32 martin printf("Pri GPT header");
96 1.32 martin break;
97 1.32 martin case MAP_TYPE_SEC_GPT_HDR:
98 1.32 martin printf("Sec GPT header");
99 1.32 martin break;
100 1.32 martin case MAP_TYPE_PRI_GPT_TBL:
101 1.32 martin printf("Pri GPT table");
102 1.32 martin break;
103 1.32 martin case MAP_TYPE_SEC_GPT_TBL:
104 1.32 martin printf("Sec GPT table");
105 1.32 martin break;
106 1.32 martin case MAP_TYPE_MBR_PART:
107 1.32 martin p = map_data;
108 1.32 martin if (p->map_start != 0)
109 1.32 martin printf("Extended ");
110 1.32 martin printf("MBR part ");
111 1.32 martin mbr = p->map_data;
112 1.32 martin for (i = 0; i < 4; i++) {
113 1.32 martin start = le16toh(mbr->mbr_part[i].part_start_hi);
114 1.32 martin start = (start << 16) +
115 1.32 martin le16toh(mbr->mbr_part[i].part_start_lo);
116 1.32 martin if (map_start == p->map_start + start)
117 1.32 martin break;
118 1.32 martin }
119 1.36 dholland if (i == 4) {
120 1.36 dholland /* wasn't there */
121 1.36 dholland printf("[partition not found?]");
122 1.36 dholland } else {
123 1.37 christos printf("%d%s", mbr->mbr_part[i].part_typ,
124 1.37 christos mbr->mbr_part[i].part_flag == 0x80 ?
125 1.37 christos " (active)" : "");
126 1.36 dholland }
127 1.32 martin break;
128 1.32 martin case MAP_TYPE_GPT_PART:
129 1.32 martin printf("GPT part ");
130 1.32 martin ent = map_data;
131 1.32 martin if (flags & SHOW_LABEL) {
132 1.41 christos utf16_to_utf8(ent->ent_name,
133 1.41 christos __arraycount(ent->ent_name), utfbuf,
134 1.41 christos __arraycount(utfbuf));
135 1.32 martin b = (char *)utfbuf;
136 1.32 martin } else if (flags & SHOW_GUID) {
137 1.32 martin gpt_uuid_snprintf( buf, sizeof(buf), "%d",
138 1.32 martin ent->ent_guid);
139 1.32 martin } else if (flags & SHOW_UUID) {
140 1.32 martin gpt_uuid_snprintf(buf, sizeof(buf),
141 1.32 martin "%d", ent->ent_type);
142 1.32 martin } else {
143 1.32 martin gpt_uuid_snprintf(buf, sizeof(buf), "%ls",
144 1.32 martin ent->ent_type);
145 1.32 martin }
146 1.32 martin printf("- %s", b);
147 1.32 martin break;
148 1.32 martin case MAP_TYPE_PMBR:
149 1.32 martin printf("PMBR");
150 1.38 kre mbr = map_data;
151 1.38 kre if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR &&
152 1.38 kre mbr->mbr_part[0].part_flag == 0x80)
153 1.38 kre printf(" (active)");
154 1.32 martin break;
155 1.32 martin default:
156 1.32 martin printf("Unknown %#x", map_type);
157 1.32 martin break;
158 1.32 martin }
159 1.32 martin }
160 1.32 martin
161 1.32 martin static int
162 1.39 kre show(gpt_t gpt, int xshow)
163 1.32 martin {
164 1.32 martin map_t m;
165 1.32 martin
166 1.24 christos printf(" %*s", gpt->lbawidth, "start");
167 1.24 christos printf(" %*s", gpt->lbawidth, "size");
168 1.1 christos printf(" index contents\n");
169 1.1 christos
170 1.24 christos m = map_first(gpt);
171 1.1 christos while (m != NULL) {
172 1.24 christos printf(" %*llu", gpt->lbawidth, (long long)m->map_start);
173 1.24 christos printf(" %*llu", gpt->lbawidth, (long long)m->map_size);
174 1.1 christos putchar(' ');
175 1.1 christos putchar(' ');
176 1.1 christos if (m->map_index > 0)
177 1.1 christos printf("%5d", m->map_index);
178 1.1 christos else
179 1.1 christos printf(" ");
180 1.1 christos putchar(' ');
181 1.1 christos putchar(' ');
182 1.39 kre print_part_type(m->map_type, xshow, m->map_data, m->map_start);
183 1.1 christos putchar('\n');
184 1.1 christos m = m->map_next;
185 1.1 christos }
186 1.24 christos return 0;
187 1.1 christos }
188 1.1 christos
189 1.44 martin static void
190 1.44 martin gpt_show_sec_num(const char *prompt, int64_t secsize, off_t num)
191 1.44 martin {
192 1.44 martin #ifdef HN_AUTOSCALE
193 1.44 martin char human_num[5];
194 1.44 martin if (humanize_number(human_num, sizeof(human_num),
195 1.44 martin (int64_t)num*secsize,
196 1.44 martin "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
197 1.44 martin human_num[0] = '\0';
198 1.44 martin #endif
199 1.44 martin printf("%s: %" PRIu64, prompt, (uint64_t)num);
200 1.44 martin #ifdef HN_AUTOSCALE
201 1.44 martin if (human_num[0] != '\0')
202 1.44 martin printf(" (%s)", human_num);
203 1.44 martin #endif
204 1.44 martin printf("\n");
205 1.44 martin }
206 1.44 martin
207 1.24 christos static int
208 1.28 christos show_one(gpt_t gpt, unsigned int entry)
209 1.12 jnemeth {
210 1.24 christos map_t m;
211 1.12 jnemeth struct gpt_ent *ent;
212 1.19 christos char s1[128], s2[128];
213 1.27 christos uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
214 1.12 jnemeth
215 1.24 christos for (m = map_first(gpt); m != NULL; m = m->map_next)
216 1.12 jnemeth if (entry == m->map_index)
217 1.12 jnemeth break;
218 1.12 jnemeth if (m == NULL) {
219 1.24 christos gpt_warnx(gpt, "Could not find index %d", entry);
220 1.24 christos return -1;
221 1.12 jnemeth }
222 1.12 jnemeth ent = m->map_data;
223 1.12 jnemeth
224 1.12 jnemeth printf("Details for index %d:\n", entry);
225 1.44 martin gpt_show_sec_num("Start", gpt->secsz, m->map_start);
226 1.44 martin gpt_show_sec_num("Size", gpt->secsz, m->map_size);
227 1.12 jnemeth
228 1.19 christos gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
229 1.20 jnemeth gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
230 1.12 jnemeth if (strcmp(s1, s2) == 0)
231 1.19 christos strlcpy(s1, "unknown", sizeof(s1));
232 1.12 jnemeth printf("Type: %s (%s)\n", s1, s2);
233 1.12 jnemeth
234 1.19 christos gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
235 1.12 jnemeth printf("GUID: %s\n", s2);
236 1.12 jnemeth
237 1.41 christos utf16_to_utf8(ent->ent_name, __arraycount(ent->ent_name), utfbuf,
238 1.41 christos __arraycount(utfbuf));
239 1.27 christos printf("Label: %s\n", (char *)utfbuf);
240 1.12 jnemeth
241 1.31 christos printf("Attributes: ");
242 1.28 christos if (ent->ent_attr == 0) {
243 1.31 christos printf("None\n");
244 1.31 christos } else {
245 1.31 christos char buf[1024];
246 1.31 christos printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
247 1.12 jnemeth }
248 1.31 christos
249 1.24 christos return 0;
250 1.12 jnemeth }
251 1.12 jnemeth
252 1.25 christos static int
253 1.32 martin show_all(gpt_t gpt)
254 1.32 martin {
255 1.32 martin map_t m;
256 1.32 martin struct gpt_ent *ent;
257 1.32 martin char s1[128], s2[128];
258 1.34 martin #ifdef HN_AUTOSCALE
259 1.34 martin char human_num[8];
260 1.34 martin #endif
261 1.32 martin uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
262 1.32 martin #define PFX " "
263 1.32 martin
264 1.32 martin printf(" %*s", gpt->lbawidth, "start");
265 1.32 martin printf(" %*s", gpt->lbawidth, "size");
266 1.32 martin printf(" index contents\n");
267 1.32 martin
268 1.32 martin m = map_first(gpt);
269 1.32 martin while (m != NULL) {
270 1.32 martin printf(" %*llu", gpt->lbawidth, (long long)m->map_start);
271 1.32 martin printf(" %*llu", gpt->lbawidth, (long long)m->map_size);
272 1.32 martin putchar(' ');
273 1.32 martin putchar(' ');
274 1.32 martin if (m->map_index > 0) {
275 1.32 martin printf("%5d ", m->map_index);
276 1.32 martin print_part_type(m->map_type, 0, m->map_data,
277 1.32 martin m->map_start);
278 1.32 martin putchar('\n');
279 1.32 martin
280 1.32 martin ent = m->map_data;
281 1.32 martin
282 1.32 martin gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
283 1.32 martin gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
284 1.32 martin if (strcmp(s1, s2) == 0)
285 1.32 martin strlcpy(s1, "unknown", sizeof(s1));
286 1.34 martin printf(PFX "Type: %s\n", s1);
287 1.34 martin printf(PFX "TypeID: %s\n", s2);
288 1.32 martin
289 1.45 mlelstv gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_guid);
290 1.32 martin printf(PFX "GUID: %s\n", s2);
291 1.32 martin
292 1.34 martin printf(PFX "Size: ");
293 1.34 martin #ifdef HN_AUTOSCALE
294 1.34 martin if (humanize_number(human_num, sizeof(human_num),
295 1.34 martin (int64_t)(m->map_size * gpt->secsz),
296 1.34 martin "", HN_AUTOSCALE, HN_B) < 0) {
297 1.34 martin #endif
298 1.34 martin printf("%ju",
299 1.34 martin (int64_t)(m->map_size * gpt->secsz));
300 1.34 martin #ifdef HN_AUTOSCALE
301 1.34 martin } else {
302 1.34 martin printf("%s", human_num);
303 1.34 martin }
304 1.34 martin #endif
305 1.34 martin putchar('\n');
306 1.34 martin
307 1.41 christos utf16_to_utf8(ent->ent_name,
308 1.41 christos __arraycount(ent->ent_name), utfbuf,
309 1.41 christos __arraycount(utfbuf));
310 1.32 martin printf(PFX "Label: %s\n", (char *)utfbuf);
311 1.32 martin
312 1.32 martin printf(PFX "Attributes: ");
313 1.32 martin if (ent->ent_attr == 0) {
314 1.32 martin printf("None\n");
315 1.32 martin } else {
316 1.32 martin char buf[1024];
317 1.32 martin
318 1.32 martin printf("%s\n", gpt_attr_list(buf, sizeof(buf),
319 1.32 martin ent->ent_attr));
320 1.32 martin }
321 1.32 martin } else {
322 1.32 martin printf(" ");
323 1.32 martin print_part_type(m->map_type, 0, m->map_data,
324 1.32 martin m->map_start);
325 1.32 martin putchar('\n');
326 1.32 martin }
327 1.32 martin m = m->map_next;
328 1.32 martin }
329 1.32 martin return 0;
330 1.32 martin }
331 1.32 martin
332 1.32 martin static int
333 1.24 christos cmd_show(gpt_t gpt, int argc, char *argv[])
334 1.1 christos {
335 1.24 christos int ch;
336 1.28 christos int xshow = 0;
337 1.28 christos unsigned int entry = 0;
338 1.43 martin off_t start = 0;
339 1.43 martin map_t m;
340 1.1 christos
341 1.43 martin while ((ch = getopt(argc, argv, "gi:b:lua")) != -1) {
342 1.1 christos switch(ch) {
343 1.32 martin case 'a':
344 1.32 martin xshow |= SHOW_ALL;
345 1.32 martin break;
346 1.12 jnemeth case 'g':
347 1.28 christos xshow |= SHOW_GUID;
348 1.12 jnemeth break;
349 1.12 jnemeth case 'i':
350 1.35 christos if (gpt_uint_get(gpt, &entry) == -1)
351 1.25 christos return usage();
352 1.12 jnemeth break;
353 1.43 martin case 'b':
354 1.43 martin if (gpt_human_get(gpt, &start) == -1)
355 1.43 martin return usage();
356 1.43 martin break;
357 1.1 christos case 'l':
358 1.28 christos xshow |= SHOW_LABEL;
359 1.1 christos break;
360 1.1 christos case 'u':
361 1.28 christos xshow |= SHOW_UUID;
362 1.1 christos break;
363 1.1 christos default:
364 1.25 christos return usage();
365 1.1 christos }
366 1.1 christos }
367 1.1 christos
368 1.24 christos if (argc != optind)
369 1.25 christos return usage();
370 1.1 christos
371 1.42 jnemeth if (map_find(gpt, MAP_TYPE_PRI_GPT_HDR) == NULL)
372 1.42 jnemeth printf("GPT not found, displaying data from MBR.\n\n");
373 1.42 jnemeth
374 1.32 martin if (xshow & SHOW_ALL)
375 1.32 martin return show_all(gpt);
376 1.32 martin
377 1.43 martin if (start > 0) {
378 1.43 martin for (m = map_first(gpt); m != NULL; m = m->map_next) {
379 1.43 martin if (m->map_type != MAP_TYPE_GPT_PART ||
380 1.43 martin m->map_index < 1)
381 1.43 martin continue;
382 1.43 martin if (start != m->map_start)
383 1.43 martin continue;
384 1.43 martin entry = m->map_index;
385 1.43 martin break;
386 1.43 martin }
387 1.43 martin }
388 1.43 martin
389 1.28 christos return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
390 1.1 christos }
391