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