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