Home | History | Annotate | Line # | Download | only in gpt
show.c revision 1.13
      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.13 2013/12/08 08:30:01 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 
     47 static int show_label = 0;
     48 static int show_uuid = 0;
     49 static int show_guid = 0;
     50 static unsigned int entry = 0;
     51 
     52 const char showmsg[] = "show [-glu] [-i index] device ...";
     53 
     54 __dead static void
     55 usage_show(void)
     56 {
     57 
     58 	fprintf(stderr,
     59 	    "usage: %s %s\n", getprogname(), showmsg);
     60 	exit(1);
     61 }
     62 
     63 static const char *
     64 friendly(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 
    127 unfriendly:
    128 	uuid_to_string(t, &s, NULL);
    129 	strlcpy(buf, s, sizeof buf);
    130 	free(s);
    131 	return (buf);
    132 }
    133 
    134 static void
    135 show(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 
    219 static void
    220 show_one(void)
    221 {
    222 	uuid_t type;
    223 	map_t *m;
    224 	struct gpt_ent *ent;
    225 	const char *s1;
    226 	char *s2;
    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 	printf("Start: %llu\n", (long long)m->map_start);
    240 	printf("Size: %llu\n", (long long)m->map_size);
    241 
    242 	le_uuid_dec(ent->ent_type, &type);
    243 	s1 = friendly(&type);
    244 	uuid_to_string(&type, &s2, NULL);
    245 	if (strcmp(s1, s2) == 0)
    246 		s1 = "unknown";
    247 	printf("Type: %s (%s)\n", s1, s2);
    248 	free(s2);
    249 
    250 	uuid_to_string((uuid_t *)ent->ent_guid, &s2, NULL);
    251 	printf("GUID: %s\n", s2);
    252 	free(s2);
    253 
    254 	printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
    255 
    256 	printf("Attributes:\n");
    257 	if (ent->ent_attr == 0)
    258 		printf("  None\n");
    259 	else {
    260 		if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
    261 			printf("  required for platform to function\n");
    262 		if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
    263 			printf("  UEFI won't recognize file system\n");
    264 		if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
    265 			printf("  legacy BIOS boot partition\n");
    266 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
    267 			printf("  indicates a bootable partition\n");
    268 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
    269 			printf("  attempt to boot this partition only once\n");
    270 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
    271 			printf("  partition that was marked bootonce but failed to boot\n");
    272 	}
    273 }
    274 
    275 int
    276 cmd_show(int argc, char *argv[])
    277 {
    278 	char *p;
    279 	int ch, fd;
    280 
    281 	while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
    282 		switch(ch) {
    283 		case 'g':
    284 			show_guid = 1;
    285 			break;
    286 		case 'i':
    287 			if (entry > 0)
    288 				usage_show();
    289 			entry = strtoul(optarg, &p, 10);
    290 			if (*p != 0 || entry < 1)
    291 				usage_show();
    292 			break;
    293 		case 'l':
    294 			show_label = 1;
    295 			break;
    296 		case 'u':
    297 			show_uuid = 1;
    298 			break;
    299 		default:
    300 			usage_show();
    301 		}
    302 	}
    303 
    304 	if (argc == optind)
    305 		usage_show();
    306 
    307 	while (optind < argc) {
    308 		fd = gpt_open(argv[optind++]);
    309 		if (fd == -1) {
    310 			warn("unable to open device '%s'", device_name);
    311 			continue;
    312 		}
    313 
    314 		if (entry > 0)
    315 			show_one();
    316 		else
    317 			show();
    318 
    319 		gpt_close(fd);
    320 	}
    321 
    322 	return (0);
    323 }
    324