show.c revision 1.26 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.26 2015/12/01 23:29:07 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 show_label = 0;
53 static int show_uuid = 0;
54 static int show_guid = 0;
55 static unsigned int entry = 0;
56
57 static int cmd_show(gpt_t, int, char *[]);
58
59 static const char *showhelp[] = {
60 "[-glu] [-i index]",
61 };
62
63 struct gpt_cmd c_show = {
64 "show",
65 cmd_show,
66 showhelp, __arraycount(showhelp),
67 GPT_READONLY,
68 };
69
70 #define usage() gpt_usage(NULL, &c_show)
71
72 static int
73 show(gpt_t gpt)
74 {
75 off_t start;
76 map_t m, p;
77 struct mbr *mbr;
78 struct gpt_ent *ent;
79 unsigned int i;
80
81 printf(" %*s", gpt->lbawidth, "start");
82 printf(" %*s", gpt->lbawidth, "size");
83 printf(" index contents\n");
84
85 m = map_first(gpt);
86 while (m != NULL) {
87 printf(" %*llu", gpt->lbawidth, (long long)m->map_start);
88 printf(" %*llu", gpt->lbawidth, (long long)m->map_size);
89 putchar(' ');
90 putchar(' ');
91 if (m->map_index > 0)
92 printf("%5d", m->map_index);
93 else
94 printf(" ");
95 putchar(' ');
96 putchar(' ');
97 switch (m->map_type) {
98 case MAP_TYPE_UNUSED:
99 printf("Unused");
100 break;
101 case MAP_TYPE_MBR:
102 if (m->map_start != 0)
103 printf("Extended ");
104 printf("MBR");
105 break;
106 case MAP_TYPE_PRI_GPT_HDR:
107 printf("Pri GPT header");
108 break;
109 case MAP_TYPE_SEC_GPT_HDR:
110 printf("Sec GPT header");
111 break;
112 case MAP_TYPE_PRI_GPT_TBL:
113 printf("Pri GPT table");
114 break;
115 case MAP_TYPE_SEC_GPT_TBL:
116 printf("Sec GPT table");
117 break;
118 case MAP_TYPE_MBR_PART:
119 p = m->map_data;
120 if (p->map_start != 0)
121 printf("Extended ");
122 printf("MBR part ");
123 mbr = p->map_data;
124 for (i = 0; i < 4; i++) {
125 start = le16toh(mbr->mbr_part[i].part_start_hi);
126 start = (start << 16) +
127 le16toh(mbr->mbr_part[i].part_start_lo);
128 if (m->map_start == p->map_start + start)
129 break;
130 }
131 printf("%d", mbr->mbr_part[i].part_typ);
132 break;
133 case MAP_TYPE_GPT_PART:
134 printf("GPT part ");
135 ent = m->map_data;
136 if (show_label) {
137 printf("- \"%s\"",
138 utf16_to_utf8(ent->ent_name));
139 } else if (show_guid) {
140 char buf[128];
141 gpt_uuid_snprintf(
142 buf, sizeof(buf), "%d", ent->ent_guid);
143 printf("- %s", buf);
144 } else {
145 char buf[128];
146 if (show_uuid || gpt_uuid_snprintf(buf,
147 sizeof(buf), "%ls", ent->ent_type) == -1)
148 gpt_uuid_snprintf(buf, sizeof(buf),
149 "%d", ent->ent_type);
150 printf("- %s", buf);
151 }
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)
168 {
169 map_t m;
170 struct gpt_ent *ent;
171 char s1[128], s2[128];
172 #ifdef HN_AUTOSCALE
173 char human_num[5];
174 #endif
175
176 for (m = map_first(gpt); m != NULL; m = m->map_next)
177 if (entry == m->map_index)
178 break;
179 if (m == NULL) {
180 gpt_warnx(gpt, "Could not find index %d", entry);
181 return -1;
182 }
183 ent = m->map_data;
184
185 printf("Details for index %d:\n", entry);
186 #ifdef HN_AUTOSCALE
187 if (humanize_number(human_num, 5, (int64_t)(m->map_start * gpt->secsz),
188 "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
189 human_num[0] = '\0';
190 if (human_num[0] != '\0')
191 printf("Start: %llu (%s)\n", (long long)m->map_start,
192 human_num);
193 else
194 #endif
195 printf("Start: %llu\n", (long long)m->map_start);
196 #ifdef HN_AUTOSCALE
197 if (humanize_number(human_num, 5, (int64_t)(m->map_size * gpt->secsz),
198 "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
199 human_num[0] = '\0';
200 if (human_num[0] != '\0')
201 printf("Size: %llu (%s)\n", (long long)m->map_size, human_num);
202 else
203 #endif
204 printf("Size: %llu\n", (long long)m->map_size);
205
206 gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
207 gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
208 if (strcmp(s1, s2) == 0)
209 strlcpy(s1, "unknown", sizeof(s1));
210 printf("Type: %s (%s)\n", s1, s2);
211
212 gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
213 printf("GUID: %s\n", s2);
214
215 printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
216
217 printf("Attributes:\n");
218 if (ent->ent_attr == 0)
219 printf(" None\n");
220 else {
221 if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
222 printf(" required for platform to function\n");
223 if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
224 printf(" UEFI won't recognize file system\n");
225 if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
226 printf(" legacy BIOS boot partition\n");
227 if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
228 printf(" indicates a bootable partition\n");
229 if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
230 printf(" attempt to boot this partition only once\n");
231 if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
232 printf(" partition that was marked bootonce but failed to boot\n");
233 }
234 return 0;
235 }
236
237 static int
238 cmd_show(gpt_t gpt, int argc, char *argv[])
239 {
240 int ch;
241
242 while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
243 switch(ch) {
244 case 'g':
245 show_guid = 1;
246 break;
247 case 'i':
248 if (gpt_entry_get(&entry) == -1)
249 return usage();
250 break;
251 case 'l':
252 show_label = 1;
253 break;
254 case 'u':
255 show_uuid = 1;
256 break;
257 default:
258 return usage();
259 }
260 }
261
262 if (argc != optind)
263 return usage();
264
265 return entry > 0 ? show_one(gpt) : show(gpt);
266 }
267