show.c revision 1.23 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.23 christos __RCSID("$NetBSD: show.c,v 1.23 2015/11/29 14:12:56 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.1 christos
51 1.1 christos static int show_label = 0;
52 1.1 christos static int show_uuid = 0;
53 1.12 jnemeth static int show_guid = 0;
54 1.12 jnemeth static unsigned int entry = 0;
55 1.1 christos
56 1.12 jnemeth const char showmsg[] = "show [-glu] [-i index] device ...";
57 1.3 riz
58 1.7 joerg __dead static void
59 1.1 christos usage_show(void)
60 1.1 christos {
61 1.1 christos
62 1.1 christos fprintf(stderr,
63 1.3 riz "usage: %s %s\n", getprogname(), showmsg);
64 1.1 christos exit(1);
65 1.1 christos }
66 1.1 christos
67 1.1 christos static void
68 1.12 jnemeth show(void)
69 1.1 christos {
70 1.1 christos off_t start;
71 1.1 christos map_t *m, *p;
72 1.1 christos struct mbr *mbr;
73 1.1 christos struct gpt_ent *ent;
74 1.1 christos unsigned int i;
75 1.1 christos
76 1.1 christos printf(" %*s", lbawidth, "start");
77 1.1 christos printf(" %*s", lbawidth, "size");
78 1.1 christos printf(" index contents\n");
79 1.1 christos
80 1.1 christos m = map_first();
81 1.1 christos while (m != NULL) {
82 1.1 christos printf(" %*llu", lbawidth, (long long)m->map_start);
83 1.1 christos printf(" %*llu", lbawidth, (long long)m->map_size);
84 1.1 christos putchar(' ');
85 1.1 christos putchar(' ');
86 1.1 christos if (m->map_index > 0)
87 1.1 christos printf("%5d", m->map_index);
88 1.1 christos else
89 1.1 christos printf(" ");
90 1.1 christos putchar(' ');
91 1.1 christos putchar(' ');
92 1.1 christos switch (m->map_type) {
93 1.23 christos case MAP_TYPE_UNUSED:
94 1.23 christos printf("Unused");
95 1.23 christos break;
96 1.1 christos case MAP_TYPE_MBR:
97 1.1 christos if (m->map_start != 0)
98 1.1 christos printf("Extended ");
99 1.1 christos printf("MBR");
100 1.1 christos break;
101 1.1 christos case MAP_TYPE_PRI_GPT_HDR:
102 1.1 christos printf("Pri GPT header");
103 1.1 christos break;
104 1.1 christos case MAP_TYPE_SEC_GPT_HDR:
105 1.1 christos printf("Sec GPT header");
106 1.1 christos break;
107 1.1 christos case MAP_TYPE_PRI_GPT_TBL:
108 1.1 christos printf("Pri GPT table");
109 1.1 christos break;
110 1.1 christos case MAP_TYPE_SEC_GPT_TBL:
111 1.1 christos printf("Sec GPT table");
112 1.1 christos break;
113 1.1 christos case MAP_TYPE_MBR_PART:
114 1.1 christos p = m->map_data;
115 1.1 christos if (p->map_start != 0)
116 1.1 christos printf("Extended ");
117 1.1 christos printf("MBR part ");
118 1.1 christos mbr = p->map_data;
119 1.1 christos for (i = 0; i < 4; i++) {
120 1.1 christos start = le16toh(mbr->mbr_part[i].part_start_hi);
121 1.1 christos start = (start << 16) +
122 1.1 christos le16toh(mbr->mbr_part[i].part_start_lo);
123 1.1 christos if (m->map_start == p->map_start + start)
124 1.1 christos break;
125 1.1 christos }
126 1.1 christos printf("%d", mbr->mbr_part[i].part_typ);
127 1.1 christos break;
128 1.1 christos case MAP_TYPE_GPT_PART:
129 1.1 christos printf("GPT part ");
130 1.1 christos ent = m->map_data;
131 1.1 christos if (show_label) {
132 1.1 christos printf("- \"%s\"",
133 1.1 christos utf16_to_utf8(ent->ent_name));
134 1.12 jnemeth } else if (show_guid) {
135 1.19 christos char buf[128];
136 1.19 christos gpt_uuid_snprintf(
137 1.19 christos buf, sizeof(buf), "%d", ent->ent_guid);
138 1.19 christos printf("- %s", buf);
139 1.1 christos } else {
140 1.19 christos char buf[128];
141 1.21 jnemeth if (show_uuid || gpt_uuid_snprintf(buf,
142 1.21 jnemeth sizeof(buf), "%ls", ent->ent_type) == -1)
143 1.21 jnemeth gpt_uuid_snprintf(buf, sizeof(buf),
144 1.21 jnemeth "%d", ent->ent_type);
145 1.19 christos printf("- %s", buf);
146 1.1 christos }
147 1.1 christos break;
148 1.1 christos case MAP_TYPE_PMBR:
149 1.1 christos printf("PMBR");
150 1.1 christos break;
151 1.23 christos default:
152 1.23 christos printf("Unknown %#x", m->map_type);
153 1.23 christos break;
154 1.1 christos }
155 1.1 christos putchar('\n');
156 1.1 christos m = m->map_next;
157 1.1 christos }
158 1.1 christos }
159 1.1 christos
160 1.12 jnemeth static void
161 1.12 jnemeth show_one(void)
162 1.12 jnemeth {
163 1.12 jnemeth map_t *m;
164 1.12 jnemeth struct gpt_ent *ent;
165 1.19 christos char s1[128], s2[128];
166 1.16 christos #ifdef HN_AUTOSCALE
167 1.17 christos char human_num[5];
168 1.16 christos #endif
169 1.12 jnemeth
170 1.12 jnemeth for (m = map_first(); m != NULL; m = m->map_next)
171 1.12 jnemeth if (entry == m->map_index)
172 1.12 jnemeth break;
173 1.12 jnemeth if (m == NULL) {
174 1.12 jnemeth warnx("%s: error: could not find index %d",
175 1.12 jnemeth device_name, entry);
176 1.12 jnemeth return;
177 1.12 jnemeth }
178 1.12 jnemeth ent = m->map_data;
179 1.12 jnemeth
180 1.12 jnemeth printf("Details for index %d:\n", entry);
181 1.16 christos #ifdef HN_AUTOSCALE
182 1.14 jnemeth if (humanize_number(human_num, 5, (int64_t)(m->map_start * secsz),
183 1.14 jnemeth "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
184 1.14 jnemeth human_num[0] = '\0';
185 1.14 jnemeth if (human_num[0] != '\0')
186 1.14 jnemeth printf("Start: %llu (%s)\n", (long long)m->map_start,
187 1.14 jnemeth human_num);
188 1.14 jnemeth else
189 1.16 christos #endif
190 1.14 jnemeth printf("Start: %llu\n", (long long)m->map_start);
191 1.16 christos #ifdef HN_AUTOSCALE
192 1.14 jnemeth if (humanize_number(human_num, 5, (int64_t)(m->map_size * secsz),
193 1.14 jnemeth "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
194 1.14 jnemeth human_num[0] = '\0';
195 1.14 jnemeth if (human_num[0] != '\0')
196 1.14 jnemeth printf("Size: %llu (%s)\n", (long long)m->map_size, human_num);
197 1.14 jnemeth else
198 1.16 christos #endif
199 1.14 jnemeth printf("Size: %llu\n", (long long)m->map_size);
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.12 jnemeth printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
211 1.12 jnemeth
212 1.12 jnemeth printf("Attributes:\n");
213 1.12 jnemeth if (ent->ent_attr == 0)
214 1.12 jnemeth printf(" None\n");
215 1.12 jnemeth else {
216 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
217 1.12 jnemeth printf(" required for platform to function\n");
218 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
219 1.12 jnemeth printf(" UEFI won't recognize file system\n");
220 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
221 1.12 jnemeth printf(" legacy BIOS boot partition\n");
222 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
223 1.12 jnemeth printf(" indicates a bootable partition\n");
224 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
225 1.12 jnemeth printf(" attempt to boot this partition only once\n");
226 1.12 jnemeth if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
227 1.12 jnemeth printf(" partition that was marked bootonce but failed to boot\n");
228 1.12 jnemeth }
229 1.12 jnemeth }
230 1.12 jnemeth
231 1.1 christos int
232 1.1 christos cmd_show(int argc, char *argv[])
233 1.1 christos {
234 1.12 jnemeth char *p;
235 1.1 christos int ch, fd;
236 1.1 christos
237 1.12 jnemeth while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
238 1.1 christos switch(ch) {
239 1.12 jnemeth case 'g':
240 1.12 jnemeth show_guid = 1;
241 1.12 jnemeth break;
242 1.12 jnemeth case 'i':
243 1.12 jnemeth if (entry > 0)
244 1.12 jnemeth usage_show();
245 1.12 jnemeth entry = strtoul(optarg, &p, 10);
246 1.12 jnemeth if (*p != 0 || entry < 1)
247 1.12 jnemeth usage_show();
248 1.12 jnemeth break;
249 1.1 christos case 'l':
250 1.1 christos show_label = 1;
251 1.1 christos break;
252 1.1 christos case 'u':
253 1.1 christos show_uuid = 1;
254 1.1 christos break;
255 1.1 christos default:
256 1.1 christos usage_show();
257 1.1 christos }
258 1.1 christos }
259 1.1 christos
260 1.1 christos if (argc == optind)
261 1.1 christos usage_show();
262 1.1 christos
263 1.1 christos while (optind < argc) {
264 1.22 christos fd = gpt_open(argv[optind++], 0);
265 1.22 christos if (fd == -1)
266 1.1 christos continue;
267 1.1 christos
268 1.13 jnemeth if (entry > 0)
269 1.12 jnemeth show_one();
270 1.12 jnemeth else
271 1.12 jnemeth show();
272 1.1 christos
273 1.1 christos gpt_close(fd);
274 1.1 christos }
275 1.1 christos
276 1.1 christos return (0);
277 1.1 christos }
278