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