show.c revision 1.41 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.41 christos __RCSID("$NetBSD: show.c,v 1.41 2017/09/07 10:23:33 christos 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.24 christos static int
190 1.28 christos show_one(gpt_t gpt, unsigned int entry)
191 1.12 jnemeth {
192 1.24 christos map_t m;
193 1.12 jnemeth struct gpt_ent *ent;
194 1.19 christos char s1[128], s2[128];
195 1.27 christos uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
196 1.12 jnemeth
197 1.24 christos for (m = map_first(gpt); m != NULL; m = m->map_next)
198 1.12 jnemeth if (entry == m->map_index)
199 1.12 jnemeth break;
200 1.12 jnemeth if (m == NULL) {
201 1.24 christos gpt_warnx(gpt, "Could not find index %d", entry);
202 1.24 christos return -1;
203 1.12 jnemeth }
204 1.12 jnemeth ent = m->map_data;
205 1.12 jnemeth
206 1.12 jnemeth printf("Details for index %d:\n", entry);
207 1.29 christos gpt_show_num("Start", (uintmax_t)(m->map_start * gpt->secsz));
208 1.29 christos gpt_show_num("Size", (uintmax_t)(m->map_size * gpt->secsz));
209 1.12 jnemeth
210 1.19 christos gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
211 1.20 jnemeth gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
212 1.12 jnemeth if (strcmp(s1, s2) == 0)
213 1.19 christos strlcpy(s1, "unknown", sizeof(s1));
214 1.12 jnemeth printf("Type: %s (%s)\n", s1, s2);
215 1.12 jnemeth
216 1.19 christos gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
217 1.12 jnemeth printf("GUID: %s\n", s2);
218 1.12 jnemeth
219 1.41 christos utf16_to_utf8(ent->ent_name, __arraycount(ent->ent_name), utfbuf,
220 1.41 christos __arraycount(utfbuf));
221 1.27 christos printf("Label: %s\n", (char *)utfbuf);
222 1.12 jnemeth
223 1.31 christos printf("Attributes: ");
224 1.28 christos if (ent->ent_attr == 0) {
225 1.31 christos printf("None\n");
226 1.31 christos } else {
227 1.31 christos char buf[1024];
228 1.31 christos printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
229 1.12 jnemeth }
230 1.31 christos
231 1.24 christos return 0;
232 1.12 jnemeth }
233 1.12 jnemeth
234 1.25 christos static int
235 1.32 martin show_all(gpt_t gpt)
236 1.32 martin {
237 1.32 martin map_t m;
238 1.32 martin struct gpt_ent *ent;
239 1.32 martin char s1[128], s2[128];
240 1.34 martin #ifdef HN_AUTOSCALE
241 1.34 martin char human_num[8];
242 1.34 martin #endif
243 1.32 martin uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
244 1.32 martin #define PFX " "
245 1.32 martin
246 1.32 martin printf(" %*s", gpt->lbawidth, "start");
247 1.32 martin printf(" %*s", gpt->lbawidth, "size");
248 1.32 martin printf(" index contents\n");
249 1.32 martin
250 1.32 martin m = map_first(gpt);
251 1.32 martin while (m != NULL) {
252 1.32 martin printf(" %*llu", gpt->lbawidth, (long long)m->map_start);
253 1.32 martin printf(" %*llu", gpt->lbawidth, (long long)m->map_size);
254 1.32 martin putchar(' ');
255 1.32 martin putchar(' ');
256 1.32 martin if (m->map_index > 0) {
257 1.32 martin printf("%5d ", m->map_index);
258 1.32 martin print_part_type(m->map_type, 0, m->map_data,
259 1.32 martin m->map_start);
260 1.32 martin putchar('\n');
261 1.32 martin
262 1.32 martin ent = m->map_data;
263 1.32 martin
264 1.32 martin gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
265 1.32 martin gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
266 1.32 martin if (strcmp(s1, s2) == 0)
267 1.32 martin strlcpy(s1, "unknown", sizeof(s1));
268 1.34 martin printf(PFX "Type: %s\n", s1);
269 1.34 martin printf(PFX "TypeID: %s\n", s2);
270 1.32 martin
271 1.32 martin gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
272 1.32 martin printf(PFX "GUID: %s\n", s2);
273 1.32 martin
274 1.34 martin printf(PFX "Size: ");
275 1.34 martin #ifdef HN_AUTOSCALE
276 1.34 martin if (humanize_number(human_num, sizeof(human_num),
277 1.34 martin (int64_t)(m->map_size * gpt->secsz),
278 1.34 martin "", HN_AUTOSCALE, HN_B) < 0) {
279 1.34 martin #endif
280 1.34 martin printf("%ju",
281 1.34 martin (int64_t)(m->map_size * gpt->secsz));
282 1.34 martin #ifdef HN_AUTOSCALE
283 1.34 martin } else {
284 1.34 martin printf("%s", human_num);
285 1.34 martin }
286 1.34 martin #endif
287 1.34 martin putchar('\n');
288 1.34 martin
289 1.41 christos utf16_to_utf8(ent->ent_name,
290 1.41 christos __arraycount(ent->ent_name), utfbuf,
291 1.41 christos __arraycount(utfbuf));
292 1.32 martin printf(PFX "Label: %s\n", (char *)utfbuf);
293 1.32 martin
294 1.32 martin printf(PFX "Attributes: ");
295 1.32 martin if (ent->ent_attr == 0) {
296 1.32 martin printf("None\n");
297 1.32 martin } else {
298 1.32 martin char buf[1024];
299 1.32 martin
300 1.32 martin printf("%s\n", gpt_attr_list(buf, sizeof(buf),
301 1.32 martin ent->ent_attr));
302 1.32 martin }
303 1.32 martin } else {
304 1.32 martin printf(" ");
305 1.32 martin print_part_type(m->map_type, 0, m->map_data,
306 1.32 martin m->map_start);
307 1.32 martin putchar('\n');
308 1.32 martin }
309 1.32 martin m = m->map_next;
310 1.32 martin }
311 1.32 martin return 0;
312 1.32 martin }
313 1.32 martin
314 1.32 martin static int
315 1.24 christos cmd_show(gpt_t gpt, int argc, char *argv[])
316 1.1 christos {
317 1.24 christos int ch;
318 1.28 christos int xshow = 0;
319 1.28 christos unsigned int entry = 0;
320 1.1 christos
321 1.32 martin while ((ch = getopt(argc, argv, "gi:lua")) != -1) {
322 1.1 christos switch(ch) {
323 1.32 martin case 'a':
324 1.32 martin xshow |= SHOW_ALL;
325 1.32 martin break;
326 1.12 jnemeth case 'g':
327 1.28 christos xshow |= SHOW_GUID;
328 1.12 jnemeth break;
329 1.12 jnemeth case 'i':
330 1.35 christos if (gpt_uint_get(gpt, &entry) == -1)
331 1.25 christos return usage();
332 1.12 jnemeth break;
333 1.1 christos case 'l':
334 1.28 christos xshow |= SHOW_LABEL;
335 1.1 christos break;
336 1.1 christos case 'u':
337 1.28 christos xshow |= SHOW_UUID;
338 1.1 christos break;
339 1.1 christos default:
340 1.25 christos return usage();
341 1.1 christos }
342 1.1 christos }
343 1.1 christos
344 1.24 christos if (argc != optind)
345 1.25 christos return usage();
346 1.1 christos
347 1.32 martin if (xshow & SHOW_ALL)
348 1.32 martin return show_all(gpt);
349 1.32 martin
350 1.28 christos return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
351 1.1 christos }
352