show.c revision 1.36 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.36 dholland __RCSID("$NetBSD: show.c,v 1.36 2016/05/31 02:29:54 dholland 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.36 dholland printf("%d", mbr->mbr_part[i].part_typ);
122 1.36 dholland }
123 1.32 martin break;
124 1.32 martin case MAP_TYPE_GPT_PART:
125 1.32 martin printf("GPT part ");
126 1.32 martin ent = map_data;
127 1.32 martin if (flags & SHOW_LABEL) {
128 1.32 martin utf16_to_utf8(ent->ent_name, utfbuf,
129 1.32 martin sizeof(utfbuf));
130 1.32 martin b = (char *)utfbuf;
131 1.32 martin } else if (flags & SHOW_GUID) {
132 1.32 martin gpt_uuid_snprintf( buf, sizeof(buf), "%d",
133 1.32 martin ent->ent_guid);
134 1.32 martin } else if (flags & SHOW_UUID) {
135 1.32 martin gpt_uuid_snprintf(buf, sizeof(buf),
136 1.32 martin "%d", ent->ent_type);
137 1.32 martin } else {
138 1.32 martin gpt_uuid_snprintf(buf, sizeof(buf), "%ls",
139 1.32 martin ent->ent_type);
140 1.32 martin }
141 1.32 martin printf("- %s", b);
142 1.32 martin break;
143 1.32 martin case MAP_TYPE_PMBR:
144 1.32 martin printf("PMBR");
145 1.32 martin break;
146 1.32 martin default:
147 1.32 martin printf("Unknown %#x", map_type);
148 1.32 martin break;
149 1.32 martin }
150 1.32 martin }
151 1.32 martin
152 1.32 martin static int
153 1.32 martin show(gpt_t gpt, int show)
154 1.32 martin {
155 1.32 martin map_t m;
156 1.32 martin
157 1.24 christos printf(" %*s", gpt->lbawidth, "start");
158 1.24 christos printf(" %*s", gpt->lbawidth, "size");
159 1.1 christos printf(" index contents\n");
160 1.1 christos
161 1.24 christos m = map_first(gpt);
162 1.1 christos while (m != NULL) {
163 1.24 christos printf(" %*llu", gpt->lbawidth, (long long)m->map_start);
164 1.24 christos printf(" %*llu", gpt->lbawidth, (long long)m->map_size);
165 1.1 christos putchar(' ');
166 1.1 christos putchar(' ');
167 1.1 christos if (m->map_index > 0)
168 1.1 christos printf("%5d", m->map_index);
169 1.1 christos else
170 1.1 christos printf(" ");
171 1.1 christos putchar(' ');
172 1.1 christos putchar(' ');
173 1.32 martin print_part_type(m->map_type, show, m->map_data, m->map_start);
174 1.1 christos putchar('\n');
175 1.1 christos m = m->map_next;
176 1.1 christos }
177 1.24 christos return 0;
178 1.1 christos }
179 1.1 christos
180 1.24 christos static int
181 1.28 christos show_one(gpt_t gpt, unsigned int entry)
182 1.12 jnemeth {
183 1.24 christos map_t m;
184 1.12 jnemeth struct gpt_ent *ent;
185 1.19 christos char s1[128], s2[128];
186 1.27 christos uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
187 1.12 jnemeth
188 1.24 christos for (m = map_first(gpt); m != NULL; m = m->map_next)
189 1.12 jnemeth if (entry == m->map_index)
190 1.12 jnemeth break;
191 1.12 jnemeth if (m == NULL) {
192 1.24 christos gpt_warnx(gpt, "Could not find index %d", entry);
193 1.24 christos return -1;
194 1.12 jnemeth }
195 1.12 jnemeth ent = m->map_data;
196 1.12 jnemeth
197 1.12 jnemeth printf("Details for index %d:\n", entry);
198 1.29 christos gpt_show_num("Start", (uintmax_t)(m->map_start * gpt->secsz));
199 1.29 christos gpt_show_num("Size", (uintmax_t)(m->map_size * gpt->secsz));
200 1.12 jnemeth
201 1.19 christos gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
202 1.20 jnemeth gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
203 1.12 jnemeth if (strcmp(s1, s2) == 0)
204 1.19 christos strlcpy(s1, "unknown", sizeof(s1));
205 1.12 jnemeth printf("Type: %s (%s)\n", s1, s2);
206 1.12 jnemeth
207 1.19 christos gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
208 1.12 jnemeth printf("GUID: %s\n", s2);
209 1.12 jnemeth
210 1.27 christos utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
211 1.27 christos printf("Label: %s\n", (char *)utfbuf);
212 1.12 jnemeth
213 1.31 christos printf("Attributes: ");
214 1.28 christos if (ent->ent_attr == 0) {
215 1.31 christos printf("None\n");
216 1.31 christos } else {
217 1.31 christos char buf[1024];
218 1.31 christos printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
219 1.12 jnemeth }
220 1.31 christos
221 1.24 christos return 0;
222 1.12 jnemeth }
223 1.12 jnemeth
224 1.25 christos static int
225 1.32 martin show_all(gpt_t gpt)
226 1.32 martin {
227 1.32 martin map_t m;
228 1.32 martin struct gpt_ent *ent;
229 1.32 martin char s1[128], s2[128];
230 1.34 martin #ifdef HN_AUTOSCALE
231 1.34 martin char human_num[8];
232 1.34 martin #endif
233 1.32 martin uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
234 1.32 martin #define PFX " "
235 1.32 martin
236 1.32 martin printf(" %*s", gpt->lbawidth, "start");
237 1.32 martin printf(" %*s", gpt->lbawidth, "size");
238 1.32 martin printf(" index contents\n");
239 1.32 martin
240 1.32 martin m = map_first(gpt);
241 1.32 martin while (m != NULL) {
242 1.32 martin printf(" %*llu", gpt->lbawidth, (long long)m->map_start);
243 1.32 martin printf(" %*llu", gpt->lbawidth, (long long)m->map_size);
244 1.32 martin putchar(' ');
245 1.32 martin putchar(' ');
246 1.32 martin if (m->map_index > 0) {
247 1.32 martin printf("%5d ", m->map_index);
248 1.32 martin print_part_type(m->map_type, 0, m->map_data,
249 1.32 martin m->map_start);
250 1.32 martin putchar('\n');
251 1.32 martin
252 1.32 martin ent = m->map_data;
253 1.32 martin
254 1.32 martin gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
255 1.32 martin gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
256 1.32 martin if (strcmp(s1, s2) == 0)
257 1.32 martin strlcpy(s1, "unknown", sizeof(s1));
258 1.34 martin printf(PFX "Type: %s\n", s1);
259 1.34 martin printf(PFX "TypeID: %s\n", s2);
260 1.32 martin
261 1.32 martin gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
262 1.32 martin printf(PFX "GUID: %s\n", s2);
263 1.32 martin
264 1.34 martin printf(PFX "Size: ");
265 1.34 martin #ifdef HN_AUTOSCALE
266 1.34 martin if (humanize_number(human_num, sizeof(human_num),
267 1.34 martin (int64_t)(m->map_size * gpt->secsz),
268 1.34 martin "", HN_AUTOSCALE, HN_B) < 0) {
269 1.34 martin #endif
270 1.34 martin printf("%ju",
271 1.34 martin (int64_t)(m->map_size * gpt->secsz));
272 1.34 martin #ifdef HN_AUTOSCALE
273 1.34 martin } else {
274 1.34 martin printf("%s", human_num);
275 1.34 martin }
276 1.34 martin #endif
277 1.34 martin putchar('\n');
278 1.34 martin
279 1.32 martin utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
280 1.32 martin printf(PFX "Label: %s\n", (char *)utfbuf);
281 1.32 martin
282 1.32 martin printf(PFX "Attributes: ");
283 1.32 martin if (ent->ent_attr == 0) {
284 1.32 martin printf("None\n");
285 1.32 martin } else {
286 1.32 martin char buf[1024];
287 1.32 martin
288 1.32 martin printf("%s\n", gpt_attr_list(buf, sizeof(buf),
289 1.32 martin ent->ent_attr));
290 1.32 martin }
291 1.32 martin } else {
292 1.32 martin printf(" ");
293 1.32 martin print_part_type(m->map_type, 0, m->map_data,
294 1.32 martin m->map_start);
295 1.32 martin putchar('\n');
296 1.32 martin }
297 1.32 martin m = m->map_next;
298 1.32 martin }
299 1.32 martin return 0;
300 1.32 martin }
301 1.32 martin
302 1.32 martin static int
303 1.24 christos cmd_show(gpt_t gpt, int argc, char *argv[])
304 1.1 christos {
305 1.24 christos int ch;
306 1.28 christos int xshow = 0;
307 1.28 christos unsigned int entry = 0;
308 1.1 christos
309 1.32 martin while ((ch = getopt(argc, argv, "gi:lua")) != -1) {
310 1.1 christos switch(ch) {
311 1.32 martin case 'a':
312 1.32 martin xshow |= SHOW_ALL;
313 1.32 martin break;
314 1.12 jnemeth case 'g':
315 1.28 christos xshow |= SHOW_GUID;
316 1.12 jnemeth break;
317 1.12 jnemeth case 'i':
318 1.35 christos if (gpt_uint_get(gpt, &entry) == -1)
319 1.25 christos return usage();
320 1.12 jnemeth break;
321 1.1 christos case 'l':
322 1.28 christos xshow |= SHOW_LABEL;
323 1.1 christos break;
324 1.1 christos case 'u':
325 1.28 christos xshow |= SHOW_UUID;
326 1.1 christos break;
327 1.1 christos default:
328 1.25 christos return usage();
329 1.1 christos }
330 1.1 christos }
331 1.1 christos
332 1.24 christos if (argc != optind)
333 1.25 christos return usage();
334 1.1 christos
335 1.32 martin if (xshow & SHOW_ALL)
336 1.32 martin return show_all(gpt);
337 1.32 martin
338 1.28 christos return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
339 1.1 christos }
340