show.c revision 1.1 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.1 christos #include <sys/cdefs.h>
28 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
29 1.1 christos
30 1.1 christos #include <sys/types.h>
31 1.1 christos
32 1.1 christos #include <err.h>
33 1.1 christos #include <stddef.h>
34 1.1 christos #include <stdio.h>
35 1.1 christos #include <stdlib.h>
36 1.1 christos #include <string.h>
37 1.1 christos #include <unistd.h>
38 1.1 christos
39 1.1 christos #include "map.h"
40 1.1 christos #include "gpt.h"
41 1.1 christos
42 1.1 christos static int show_label = 0;
43 1.1 christos static int show_uuid = 0;
44 1.1 christos
45 1.1 christos static void
46 1.1 christos usage_show(void)
47 1.1 christos {
48 1.1 christos
49 1.1 christos fprintf(stderr,
50 1.1 christos "usage: %s [-lu] device ...\n", getprogname());
51 1.1 christos exit(1);
52 1.1 christos }
53 1.1 christos
54 1.1 christos static const char *
55 1.1 christos friendly(uuid_t *t)
56 1.1 christos {
57 1.1 christos static uuid_t efi_slice = GPT_ENT_TYPE_EFI;
58 1.1 christos static uuid_t mslinux = GPT_ENT_TYPE_MS_BASIC_DATA;
59 1.1 christos static uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
60 1.1 christos static uuid_t hfs = GPT_ENT_TYPE_APPLE_HFS;
61 1.1 christos static uuid_t linuxswap = GPT_ENT_TYPE_LINUX_SWAP;
62 1.1 christos static uuid_t msr = GPT_ENT_TYPE_MS_RESERVED;
63 1.1 christos static uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
64 1.1 christos static uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
65 1.1 christos static uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
66 1.1 christos static char buf[80];
67 1.1 christos char *s;
68 1.1 christos
69 1.1 christos if (show_uuid)
70 1.1 christos goto unfriendly;
71 1.1 christos
72 1.1 christos if (uuid_equal(t, &efi_slice, NULL))
73 1.1 christos return ("EFI System");
74 1.1 christos if (uuid_equal(t, &swap, NULL))
75 1.1 christos return ("FreeBSD swap");
76 1.1 christos if (uuid_equal(t, &ufs, NULL))
77 1.1 christos return ("FreeBSD UFS/UFS2");
78 1.1 christos if (uuid_equal(t, &vinum, NULL))
79 1.1 christos return ("FreeBSD vinum");
80 1.1 christos
81 1.1 christos if (uuid_equal(t, &freebsd, NULL))
82 1.1 christos return ("FreeBSD legacy");
83 1.1 christos if (uuid_equal(t, &mslinux, NULL))
84 1.1 christos return ("Linux/Windows");
85 1.1 christos if (uuid_equal(t, &linuxswap, NULL))
86 1.1 christos return ("Linux swap");
87 1.1 christos if (uuid_equal(t, &msr, NULL))
88 1.1 christos return ("Windows reserved");
89 1.1 christos if (uuid_equal(t, &hfs, NULL))
90 1.1 christos return ("Apple HFS");
91 1.1 christos
92 1.1 christos unfriendly:
93 1.1 christos uuid_to_string(t, &s, NULL);
94 1.1 christos strlcpy(buf, s, sizeof buf);
95 1.1 christos free(s);
96 1.1 christos return (buf);
97 1.1 christos }
98 1.1 christos
99 1.1 christos static void
100 1.1 christos show(int fd __unused)
101 1.1 christos {
102 1.1 christos uuid_t type;
103 1.1 christos off_t start;
104 1.1 christos map_t *m, *p;
105 1.1 christos struct mbr *mbr;
106 1.1 christos struct gpt_ent *ent;
107 1.1 christos unsigned int i;
108 1.1 christos
109 1.1 christos printf(" %*s", lbawidth, "start");
110 1.1 christos printf(" %*s", lbawidth, "size");
111 1.1 christos printf(" index contents\n");
112 1.1 christos
113 1.1 christos m = map_first();
114 1.1 christos while (m != NULL) {
115 1.1 christos printf(" %*llu", lbawidth, (long long)m->map_start);
116 1.1 christos printf(" %*llu", lbawidth, (long long)m->map_size);
117 1.1 christos putchar(' ');
118 1.1 christos putchar(' ');
119 1.1 christos if (m->map_index > 0)
120 1.1 christos printf("%5d", m->map_index);
121 1.1 christos else
122 1.1 christos printf(" ");
123 1.1 christos putchar(' ');
124 1.1 christos putchar(' ');
125 1.1 christos switch (m->map_type) {
126 1.1 christos case MAP_TYPE_MBR:
127 1.1 christos if (m->map_start != 0)
128 1.1 christos printf("Extended ");
129 1.1 christos printf("MBR");
130 1.1 christos break;
131 1.1 christos case MAP_TYPE_PRI_GPT_HDR:
132 1.1 christos printf("Pri GPT header");
133 1.1 christos break;
134 1.1 christos case MAP_TYPE_SEC_GPT_HDR:
135 1.1 christos printf("Sec GPT header");
136 1.1 christos break;
137 1.1 christos case MAP_TYPE_PRI_GPT_TBL:
138 1.1 christos printf("Pri GPT table");
139 1.1 christos break;
140 1.1 christos case MAP_TYPE_SEC_GPT_TBL:
141 1.1 christos printf("Sec GPT table");
142 1.1 christos break;
143 1.1 christos case MAP_TYPE_MBR_PART:
144 1.1 christos p = m->map_data;
145 1.1 christos if (p->map_start != 0)
146 1.1 christos printf("Extended ");
147 1.1 christos printf("MBR part ");
148 1.1 christos mbr = p->map_data;
149 1.1 christos for (i = 0; i < 4; i++) {
150 1.1 christos start = le16toh(mbr->mbr_part[i].part_start_hi);
151 1.1 christos start = (start << 16) +
152 1.1 christos le16toh(mbr->mbr_part[i].part_start_lo);
153 1.1 christos if (m->map_start == p->map_start + start)
154 1.1 christos break;
155 1.1 christos }
156 1.1 christos printf("%d", mbr->mbr_part[i].part_typ);
157 1.1 christos break;
158 1.1 christos case MAP_TYPE_GPT_PART:
159 1.1 christos printf("GPT part ");
160 1.1 christos ent = m->map_data;
161 1.1 christos if (show_label) {
162 1.1 christos printf("- \"%s\"",
163 1.1 christos utf16_to_utf8(ent->ent_name));
164 1.1 christos } else {
165 1.1 christos le_uuid_dec(&ent->ent_type, &type);
166 1.1 christos printf("- %s", friendly(&type));
167 1.1 christos }
168 1.1 christos break;
169 1.1 christos case MAP_TYPE_PMBR:
170 1.1 christos printf("PMBR");
171 1.1 christos break;
172 1.1 christos }
173 1.1 christos putchar('\n');
174 1.1 christos m = m->map_next;
175 1.1 christos }
176 1.1 christos }
177 1.1 christos
178 1.1 christos int
179 1.1 christos cmd_show(int argc, char *argv[])
180 1.1 christos {
181 1.1 christos int ch, fd;
182 1.1 christos
183 1.1 christos while ((ch = getopt(argc, argv, "lu")) != -1) {
184 1.1 christos switch(ch) {
185 1.1 christos case 'l':
186 1.1 christos show_label = 1;
187 1.1 christos break;
188 1.1 christos case 'u':
189 1.1 christos show_uuid = 1;
190 1.1 christos break;
191 1.1 christos default:
192 1.1 christos usage_show();
193 1.1 christos }
194 1.1 christos }
195 1.1 christos
196 1.1 christos if (argc == optind)
197 1.1 christos usage_show();
198 1.1 christos
199 1.1 christos while (optind < argc) {
200 1.1 christos fd = gpt_open(argv[optind++]);
201 1.1 christos if (fd == -1) {
202 1.1 christos warn("unable to open device '%s'", device_name);
203 1.1 christos continue;
204 1.1 christos }
205 1.1 christos
206 1.1 christos show(fd);
207 1.1 christos
208 1.1 christos gpt_close(fd);
209 1.1 christos }
210 1.1 christos
211 1.1 christos return (0);
212 1.1 christos }
213