show.c revision 1.14
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#ifdef __FBSDID 29__FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $"); 30#endif 31#ifdef __RCSID 32__RCSID("$NetBSD: show.c,v 1.14 2013/12/09 01:35:02 jnemeth Exp $"); 33#endif 34 35#include <sys/types.h> 36 37#include <err.h> 38#include <stddef.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <unistd.h> 43 44#include "map.h" 45#include "gpt.h" 46 47static int show_label = 0; 48static int show_uuid = 0; 49static int show_guid = 0; 50static unsigned int entry = 0; 51 52const char showmsg[] = "show [-glu] [-i index] device ..."; 53 54__dead static void 55usage_show(void) 56{ 57 58 fprintf(stderr, 59 "usage: %s %s\n", getprogname(), showmsg); 60 exit(1); 61} 62 63static const char * 64friendly(uuid_t *t) 65{ 66 static const uuid_t efi_slice = GPT_ENT_TYPE_EFI; 67 static const uuid_t bios_boot = GPT_ENT_TYPE_BIOS; 68 static const uuid_t msdata = GPT_ENT_TYPE_MS_BASIC_DATA; 69 static const uuid_t freebsd = GPT_ENT_TYPE_FREEBSD; 70 static const uuid_t hfs = GPT_ENT_TYPE_APPLE_HFS; 71 static const uuid_t linuxdata = GPT_ENT_TYPE_LINUX_DATA; 72 static const uuid_t linuxswap = GPT_ENT_TYPE_LINUX_SWAP; 73 static const uuid_t msr = GPT_ENT_TYPE_MS_RESERVED; 74 static const uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP; 75 static const uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS; 76 static const uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM; 77 static const uuid_t zfs = GPT_ENT_TYPE_FREEBSD_ZFS; 78 static const uuid_t nb_swap = GPT_ENT_TYPE_NETBSD_SWAP; 79 static const uuid_t nb_ffs = GPT_ENT_TYPE_NETBSD_FFS; 80 static const uuid_t nb_lfs = GPT_ENT_TYPE_NETBSD_LFS; 81 static const uuid_t nb_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME; 82 static const uuid_t nb_ccd = GPT_ENT_TYPE_NETBSD_CCD; 83 static const uuid_t nb_cgd = GPT_ENT_TYPE_NETBSD_CGD; 84 static char buf[80]; 85 char *s; 86 87 if (show_uuid) 88 goto unfriendly; 89 90 if (uuid_equal(t, &efi_slice, NULL)) 91 return ("EFI System"); 92 if (uuid_equal(t, &bios_boot, NULL)) 93 return ("BIOS Boot"); 94 if (uuid_equal(t, &nb_swap, NULL)) 95 return ("NetBSD swap"); 96 if (uuid_equal(t, &nb_ffs, NULL)) 97 return ("NetBSD FFSv1/FFSv2"); 98 if (uuid_equal(t, &nb_lfs, NULL)) 99 return ("NetBSD LFS"); 100 if (uuid_equal(t, &nb_raid, NULL)) 101 return ("NetBSD RAIDFrame component"); 102 if (uuid_equal(t, &nb_ccd, NULL)) 103 return ("NetBSD ccd component"); 104 if (uuid_equal(t, &nb_cgd, NULL)) 105 return ("NetBSD Cryptographic Disk"); 106 if (uuid_equal(t, &swap, NULL)) 107 return ("FreeBSD swap"); 108 if (uuid_equal(t, &ufs, NULL)) 109 return ("FreeBSD UFS/UFS2"); 110 if (uuid_equal(t, &vinum, NULL)) 111 return ("FreeBSD vinum"); 112 if (uuid_equal(t, &zfs, NULL)) 113 return ("FreeBSD ZFS"); 114 if (uuid_equal(t, &freebsd, NULL)) 115 return ("FreeBSD legacy"); 116 if (uuid_equal(t, &msdata, NULL)) 117 return ("Windows basic data"); 118 if (uuid_equal(t, &msr, NULL)) 119 return ("Windows reserved"); 120 if (uuid_equal(t, &linuxdata, NULL)) 121 return ("Linux data"); 122 if (uuid_equal(t, &linuxswap, NULL)) 123 return ("Linux swap"); 124 if (uuid_equal(t, &hfs, NULL)) 125 return ("Apple HFS"); 126 127unfriendly: 128 uuid_to_string(t, &s, NULL); 129 strlcpy(buf, s, sizeof buf); 130 free(s); 131 return (buf); 132} 133 134static void 135show(void) 136{ 137 uuid_t type; 138 off_t start; 139 map_t *m, *p; 140 struct mbr *mbr; 141 struct gpt_ent *ent; 142 unsigned int i; 143 char *s; 144 145 printf(" %*s", lbawidth, "start"); 146 printf(" %*s", lbawidth, "size"); 147 printf(" index contents\n"); 148 149 m = map_first(); 150 while (m != NULL) { 151 printf(" %*llu", lbawidth, (long long)m->map_start); 152 printf(" %*llu", lbawidth, (long long)m->map_size); 153 putchar(' '); 154 putchar(' '); 155 if (m->map_index > 0) 156 printf("%5d", m->map_index); 157 else 158 printf(" "); 159 putchar(' '); 160 putchar(' '); 161 switch (m->map_type) { 162 case MAP_TYPE_MBR: 163 if (m->map_start != 0) 164 printf("Extended "); 165 printf("MBR"); 166 break; 167 case MAP_TYPE_PRI_GPT_HDR: 168 printf("Pri GPT header"); 169 break; 170 case MAP_TYPE_SEC_GPT_HDR: 171 printf("Sec GPT header"); 172 break; 173 case MAP_TYPE_PRI_GPT_TBL: 174 printf("Pri GPT table"); 175 break; 176 case MAP_TYPE_SEC_GPT_TBL: 177 printf("Sec GPT table"); 178 break; 179 case MAP_TYPE_MBR_PART: 180 p = m->map_data; 181 if (p->map_start != 0) 182 printf("Extended "); 183 printf("MBR part "); 184 mbr = p->map_data; 185 for (i = 0; i < 4; i++) { 186 start = le16toh(mbr->mbr_part[i].part_start_hi); 187 start = (start << 16) + 188 le16toh(mbr->mbr_part[i].part_start_lo); 189 if (m->map_start == p->map_start + start) 190 break; 191 } 192 printf("%d", mbr->mbr_part[i].part_typ); 193 break; 194 case MAP_TYPE_GPT_PART: 195 printf("GPT part "); 196 ent = m->map_data; 197 if (show_label) { 198 printf("- \"%s\"", 199 utf16_to_utf8(ent->ent_name)); 200 } else if (show_guid) { 201 uuid_to_string((uuid_t *)ent->ent_guid, 202 &s, NULL); 203 printf("- %s", s); 204 free(s); 205 } else { 206 le_uuid_dec(ent->ent_type, &type); 207 printf("- %s", friendly(&type)); 208 } 209 break; 210 case MAP_TYPE_PMBR: 211 printf("PMBR"); 212 break; 213 } 214 putchar('\n'); 215 m = m->map_next; 216 } 217} 218 219static void 220show_one(void) 221{ 222 uuid_t type; 223 map_t *m; 224 struct gpt_ent *ent; 225 const char *s1; 226 char *s2, human_num[5]; 227 228 for (m = map_first(); m != NULL; m = m->map_next) 229 if (entry == m->map_index) 230 break; 231 if (m == NULL) { 232 warnx("%s: error: could not find index %d", 233 device_name, entry); 234 return; 235 } 236 ent = m->map_data; 237 238 printf("Details for index %d:\n", entry); 239 if (humanize_number(human_num, 5, (int64_t)(m->map_start * secsz), 240 "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0) 241 human_num[0] = '\0'; 242 if (human_num[0] != '\0') 243 printf("Start: %llu (%s)\n", (long long)m->map_start, 244 human_num); 245 else 246 printf("Start: %llu\n", (long long)m->map_start); 247 if (humanize_number(human_num, 5, (int64_t)(m->map_size * secsz), 248 "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0) 249 human_num[0] = '\0'; 250 if (human_num[0] != '\0') 251 printf("Size: %llu (%s)\n", (long long)m->map_size, human_num); 252 else 253 printf("Size: %llu\n", (long long)m->map_size); 254 255 le_uuid_dec(ent->ent_type, &type); 256 s1 = friendly(&type); 257 uuid_to_string(&type, &s2, NULL); 258 if (strcmp(s1, s2) == 0) 259 s1 = "unknown"; 260 printf("Type: %s (%s)\n", s1, s2); 261 free(s2); 262 263 uuid_to_string((uuid_t *)ent->ent_guid, &s2, NULL); 264 printf("GUID: %s\n", s2); 265 free(s2); 266 267 printf("Label: %s\n", utf16_to_utf8(ent->ent_name)); 268 269 printf("Attributes:\n"); 270 if (ent->ent_attr == 0) 271 printf(" None\n"); 272 else { 273 if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION) 274 printf(" required for platform to function\n"); 275 if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL) 276 printf(" UEFI won't recognize file system\n"); 277 if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE) 278 printf(" legacy BIOS boot partition\n"); 279 if (ent->ent_attr & GPT_ENT_ATTR_BOOTME) 280 printf(" indicates a bootable partition\n"); 281 if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE) 282 printf(" attempt to boot this partition only once\n"); 283 if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED) 284 printf(" partition that was marked bootonce but failed to boot\n"); 285 } 286} 287 288int 289cmd_show(int argc, char *argv[]) 290{ 291 char *p; 292 int ch, fd; 293 294 while ((ch = getopt(argc, argv, "gi:lu")) != -1) { 295 switch(ch) { 296 case 'g': 297 show_guid = 1; 298 break; 299 case 'i': 300 if (entry > 0) 301 usage_show(); 302 entry = strtoul(optarg, &p, 10); 303 if (*p != 0 || entry < 1) 304 usage_show(); 305 break; 306 case 'l': 307 show_label = 1; 308 break; 309 case 'u': 310 show_uuid = 1; 311 break; 312 default: 313 usage_show(); 314 } 315 } 316 317 if (argc == optind) 318 usage_show(); 319 320 while (optind < argc) { 321 fd = gpt_open(argv[optind++]); 322 if (fd == -1) { 323 warn("unable to open device '%s'", device_name); 324 continue; 325 } 326 327 if (entry > 0) 328 show_one(); 329 else 330 show(); 331 332 gpt_close(fd); 333 } 334 335 return (0); 336} 337