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