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