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