Home | History | Annotate | Line # | Download | only in gpt
show.c revision 1.21
      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.21 2014/09/30 22:56:36 jnemeth 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 void
     68 show(void)
     69 {
     70 	off_t start;
     71 	map_t *m, *p;
     72 	struct mbr *mbr;
     73 	struct gpt_ent *ent;
     74 	unsigned int i;
     75 
     76 	printf("  %*s", lbawidth, "start");
     77 	printf("  %*s", lbawidth, "size");
     78 	printf("  index  contents\n");
     79 
     80 	m = map_first();
     81 	while (m != NULL) {
     82 		printf("  %*llu", lbawidth, (long long)m->map_start);
     83 		printf("  %*llu", lbawidth, (long long)m->map_size);
     84 		putchar(' ');
     85 		putchar(' ');
     86 		if (m->map_index > 0)
     87 			printf("%5d", m->map_index);
     88 		else
     89 			printf("     ");
     90 		putchar(' ');
     91 		putchar(' ');
     92 		switch (m->map_type) {
     93 		case MAP_TYPE_MBR:
     94 			if (m->map_start != 0)
     95 				printf("Extended ");
     96 			printf("MBR");
     97 			break;
     98 		case MAP_TYPE_PRI_GPT_HDR:
     99 			printf("Pri GPT header");
    100 			break;
    101 		case MAP_TYPE_SEC_GPT_HDR:
    102 			printf("Sec GPT header");
    103 			break;
    104 		case MAP_TYPE_PRI_GPT_TBL:
    105 			printf("Pri GPT table");
    106 			break;
    107 		case MAP_TYPE_SEC_GPT_TBL:
    108 			printf("Sec GPT table");
    109 			break;
    110 		case MAP_TYPE_MBR_PART:
    111 			p = m->map_data;
    112 			if (p->map_start != 0)
    113 				printf("Extended ");
    114 			printf("MBR part ");
    115 			mbr = p->map_data;
    116 			for (i = 0; i < 4; i++) {
    117 				start = le16toh(mbr->mbr_part[i].part_start_hi);
    118 				start = (start << 16) +
    119 				    le16toh(mbr->mbr_part[i].part_start_lo);
    120 				if (m->map_start == p->map_start + start)
    121 					break;
    122 			}
    123 			printf("%d", mbr->mbr_part[i].part_typ);
    124 			break;
    125 		case MAP_TYPE_GPT_PART:
    126 			printf("GPT part ");
    127 			ent = m->map_data;
    128 			if (show_label) {
    129 				printf("- \"%s\"",
    130 				    utf16_to_utf8(ent->ent_name));
    131 			} else if (show_guid) {
    132 				char buf[128];
    133 				gpt_uuid_snprintf(
    134 				    buf, sizeof(buf), "%d", ent->ent_guid);
    135 				printf("- %s", buf);
    136 			} else {
    137 				char buf[128];
    138 				if (show_uuid || gpt_uuid_snprintf(buf,
    139 				    sizeof(buf), "%ls", ent->ent_type) == -1)
    140 					gpt_uuid_snprintf(buf, sizeof(buf),
    141 					    "%d", ent->ent_type);
    142 				printf("- %s", buf);
    143 			}
    144 			break;
    145 		case MAP_TYPE_PMBR:
    146 			printf("PMBR");
    147 			break;
    148 		}
    149 		putchar('\n');
    150 		m = m->map_next;
    151 	}
    152 }
    153 
    154 static void
    155 show_one(void)
    156 {
    157 	map_t *m;
    158 	struct gpt_ent *ent;
    159 	char s1[128], s2[128];
    160 #ifdef HN_AUTOSCALE
    161 	char human_num[5];
    162 #endif
    163 
    164 	for (m = map_first(); m != NULL; m = m->map_next)
    165 		if (entry == m->map_index)
    166 			break;
    167 	if (m == NULL) {
    168 		warnx("%s: error: could not find index %d",
    169 		    device_name, entry);
    170 		return;
    171 	}
    172 	ent = m->map_data;
    173 
    174 	printf("Details for index %d:\n", entry);
    175 #ifdef HN_AUTOSCALE
    176 	if (humanize_number(human_num, 5, (int64_t)(m->map_start * secsz),
    177 	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
    178 		human_num[0] = '\0';
    179 	if (human_num[0] != '\0')
    180 		printf("Start: %llu (%s)\n", (long long)m->map_start,
    181 		    human_num);
    182 	else
    183 #endif
    184 		printf("Start: %llu\n", (long long)m->map_start);
    185 #ifdef HN_AUTOSCALE
    186 	if (humanize_number(human_num, 5, (int64_t)(m->map_size * secsz),
    187 	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
    188 		human_num[0] = '\0';
    189 	if (human_num[0] != '\0')
    190 		printf("Size: %llu (%s)\n", (long long)m->map_size, human_num);
    191 	else
    192 #endif
    193 		printf("Size: %llu\n", (long long)m->map_size);
    194 
    195 	gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
    196 	gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
    197 	if (strcmp(s1, s2) == 0)
    198 		strlcpy(s1, "unknown", sizeof(s1));
    199 	printf("Type: %s (%s)\n", s1, s2);
    200 
    201 	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
    202 	printf("GUID: %s\n", s2);
    203 
    204 	printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
    205 
    206 	printf("Attributes:\n");
    207 	if (ent->ent_attr == 0)
    208 		printf("  None\n");
    209 	else {
    210 		if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
    211 			printf("  required for platform to function\n");
    212 		if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
    213 			printf("  UEFI won't recognize file system\n");
    214 		if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
    215 			printf("  legacy BIOS boot partition\n");
    216 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
    217 			printf("  indicates a bootable partition\n");
    218 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
    219 			printf("  attempt to boot this partition only once\n");
    220 		if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
    221 			printf("  partition that was marked bootonce but failed to boot\n");
    222 	}
    223 }
    224 
    225 int
    226 cmd_show(int argc, char *argv[])
    227 {
    228 	char *p;
    229 	int ch, fd;
    230 
    231 	while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
    232 		switch(ch) {
    233 		case 'g':
    234 			show_guid = 1;
    235 			break;
    236 		case 'i':
    237 			if (entry > 0)
    238 				usage_show();
    239 			entry = strtoul(optarg, &p, 10);
    240 			if (*p != 0 || entry < 1)
    241 				usage_show();
    242 			break;
    243 		case 'l':
    244 			show_label = 1;
    245 			break;
    246 		case 'u':
    247 			show_uuid = 1;
    248 			break;
    249 		default:
    250 			usage_show();
    251 		}
    252 	}
    253 
    254 	if (argc == optind)
    255 		usage_show();
    256 
    257 	while (optind < argc) {
    258 		fd = gpt_open(argv[optind++]);
    259 		if (fd == -1) {
    260 			warn("unable to open device '%s'", device_name);
    261 			continue;
    262 		}
    263 
    264 		if (entry > 0)
    265 			show_one();
    266 		else
    267 			show();
    268 
    269 		gpt_close(fd);
    270 	}
    271 
    272 	return (0);
    273 }
    274