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