show.c revision 1.31 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.31 2015/12/06 00:39:26 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 "[-glu] [-i index]",
56 };
57
58 #define SHOW_UUID 1
59 #define SHOW_GUID 2
60 #define SHOW_LABEL 4
61
62 struct gpt_cmd c_show = {
63 "show",
64 cmd_show,
65 showhelp, __arraycount(showhelp),
66 GPT_READONLY,
67 };
68
69 #define usage() gpt_usage(NULL, &c_show)
70
71 static int
72 show(gpt_t gpt, int show)
73 {
74 off_t start;
75 map_t m, p;
76 struct mbr *mbr;
77 struct gpt_ent *ent;
78 unsigned int i;
79 char buf[128], *b = buf;
80 uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
81
82 printf(" %*s", gpt->lbawidth, "start");
83 printf(" %*s", gpt->lbawidth, "size");
84 printf(" index contents\n");
85
86 m = map_first(gpt);
87 while (m != NULL) {
88 printf(" %*llu", gpt->lbawidth, (long long)m->map_start);
89 printf(" %*llu", gpt->lbawidth, (long long)m->map_size);
90 putchar(' ');
91 putchar(' ');
92 if (m->map_index > 0)
93 printf("%5d", m->map_index);
94 else
95 printf(" ");
96 putchar(' ');
97 putchar(' ');
98 switch (m->map_type) {
99 case MAP_TYPE_UNUSED:
100 printf("Unused");
101 break;
102 case MAP_TYPE_MBR:
103 if (m->map_start != 0)
104 printf("Extended ");
105 printf("MBR");
106 break;
107 case MAP_TYPE_PRI_GPT_HDR:
108 printf("Pri GPT header");
109 break;
110 case MAP_TYPE_SEC_GPT_HDR:
111 printf("Sec GPT header");
112 break;
113 case MAP_TYPE_PRI_GPT_TBL:
114 printf("Pri GPT table");
115 break;
116 case MAP_TYPE_SEC_GPT_TBL:
117 printf("Sec GPT table");
118 break;
119 case MAP_TYPE_MBR_PART:
120 p = m->map_data;
121 if (p->map_start != 0)
122 printf("Extended ");
123 printf("MBR part ");
124 mbr = p->map_data;
125 for (i = 0; i < 4; i++) {
126 start = le16toh(mbr->mbr_part[i].part_start_hi);
127 start = (start << 16) +
128 le16toh(mbr->mbr_part[i].part_start_lo);
129 if (m->map_start == p->map_start + start)
130 break;
131 }
132 printf("%d", mbr->mbr_part[i].part_typ);
133 break;
134 case MAP_TYPE_GPT_PART:
135 printf("GPT part ");
136 ent = m->map_data;
137 if (show & SHOW_LABEL) {
138 utf16_to_utf8(ent->ent_name, utfbuf,
139 sizeof(utfbuf));
140 b = (char *)utfbuf;
141 } else if (show & SHOW_GUID) {
142 gpt_uuid_snprintf( buf, sizeof(buf), "%d",
143 ent->ent_guid);
144 } else if (show & SHOW_UUID) {
145 gpt_uuid_snprintf(buf, sizeof(buf),
146 "%d", ent->ent_type);
147 } else {
148 gpt_uuid_snprintf(buf, sizeof(buf), "%ls",
149 ent->ent_type);
150 }
151 printf("- %s", b);
152 break;
153 case MAP_TYPE_PMBR:
154 printf("PMBR");
155 break;
156 default:
157 printf("Unknown %#x", m->map_type);
158 break;
159 }
160 putchar('\n');
161 m = m->map_next;
162 }
163 return 0;
164 }
165
166 static int
167 show_one(gpt_t gpt, unsigned int entry)
168 {
169 map_t m;
170 struct gpt_ent *ent;
171 char s1[128], s2[128];
172 uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
173
174 for (m = map_first(gpt); m != NULL; m = m->map_next)
175 if (entry == m->map_index)
176 break;
177 if (m == NULL) {
178 gpt_warnx(gpt, "Could not find index %d", entry);
179 return -1;
180 }
181 ent = m->map_data;
182
183 printf("Details for index %d:\n", entry);
184 gpt_show_num("Start", (uintmax_t)(m->map_start * gpt->secsz));
185 gpt_show_num("Size", (uintmax_t)(m->map_size * gpt->secsz));
186
187 gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
188 gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
189 if (strcmp(s1, s2) == 0)
190 strlcpy(s1, "unknown", sizeof(s1));
191 printf("Type: %s (%s)\n", s1, s2);
192
193 gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
194 printf("GUID: %s\n", s2);
195
196 utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
197 printf("Label: %s\n", (char *)utfbuf);
198
199 printf("Attributes: ");
200 if (ent->ent_attr == 0) {
201 printf("None\n");
202 } else {
203 char buf[1024];
204 printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
205 }
206
207 return 0;
208 }
209
210 static int
211 cmd_show(gpt_t gpt, int argc, char *argv[])
212 {
213 int ch;
214 int xshow = 0;
215 unsigned int entry = 0;
216
217 while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
218 switch(ch) {
219 case 'g':
220 xshow |= SHOW_GUID;
221 break;
222 case 'i':
223 if (gpt_uint_get(&entry) == -1)
224 return usage();
225 break;
226 case 'l':
227 xshow |= SHOW_LABEL;
228 break;
229 case 'u':
230 xshow |= SHOW_UUID;
231 break;
232 default:
233 return usage();
234 }
235 }
236
237 if (argc != optind)
238 return usage();
239
240 return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
241 }
242