Home | History | Annotate | Line # | Download | only in gpt
show.c revision 1.44
      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.44 2019/03/26 11:23:55 martin Exp $");
     37 #endif
     38 
     39 #include <sys/bootblock.h>
     40 #include <sys/types.h>
     41 
     42 #include <err.h>
     43 #include <stddef.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 
     49 
     50 #include "map.h"
     51 #include "gpt.h"
     52 #include "gpt_private.h"
     53 
     54 static int cmd_show(gpt_t, int, char *[]);
     55 
     56 static const char *showhelp[] = {
     57 	"[-aglu] [-i index]",
     58 };
     59 
     60 #define SHOW_UUID  1
     61 #define SHOW_GUID  2
     62 #define SHOW_LABEL 4
     63 #define SHOW_ALL   8
     64 
     65 struct gpt_cmd c_show = {
     66 	"show",
     67 	cmd_show,
     68 	showhelp, __arraycount(showhelp),
     69 	GPT_READONLY,
     70 };
     71 
     72 #define usage() gpt_usage(NULL, &c_show)
     73 
     74 static void
     75 print_part_type(int map_type, int flags, void *map_data, off_t map_start)
     76 {
     77 	off_t start;
     78 	map_t p;
     79 	struct mbr *mbr;
     80 	struct gpt_ent *ent;
     81 	unsigned int i;
     82 	char buf[128], *b = buf;
     83 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
     84 
     85 	switch (map_type) {
     86 	case MAP_TYPE_UNUSED:
     87 		printf("Unused");
     88 		break;
     89 	case MAP_TYPE_MBR:
     90 		if (map_start != 0)
     91 			printf("Extended ");
     92 		printf("MBR");
     93 		break;
     94 	case MAP_TYPE_PRI_GPT_HDR:
     95 		printf("Pri GPT header");
     96 		break;
     97 	case MAP_TYPE_SEC_GPT_HDR:
     98 		printf("Sec GPT header");
     99 		break;
    100 	case MAP_TYPE_PRI_GPT_TBL:
    101 		printf("Pri GPT table");
    102 		break;
    103 	case MAP_TYPE_SEC_GPT_TBL:
    104 		printf("Sec GPT table");
    105 		break;
    106 	case MAP_TYPE_MBR_PART:
    107 		p = map_data;
    108 		if (p->map_start != 0)
    109 			printf("Extended ");
    110 		printf("MBR part ");
    111 		mbr = p->map_data;
    112 		for (i = 0; i < 4; i++) {
    113 			start = le16toh(mbr->mbr_part[i].part_start_hi);
    114 			start = (start << 16) +
    115 			    le16toh(mbr->mbr_part[i].part_start_lo);
    116 			if (map_start == p->map_start + start)
    117 				break;
    118 		}
    119 		if (i == 4) {
    120 			/* wasn't there */
    121 			printf("[partition not found?]");
    122 		} else {
    123 			printf("%d%s", mbr->mbr_part[i].part_typ,
    124 			    mbr->mbr_part[i].part_flag == 0x80 ?
    125 			    " (active)" : "");
    126 		}
    127 		break;
    128 	case MAP_TYPE_GPT_PART:
    129 		printf("GPT part ");
    130 		ent = map_data;
    131 		if (flags & SHOW_LABEL) {
    132 			utf16_to_utf8(ent->ent_name,
    133 			    __arraycount(ent->ent_name), utfbuf,
    134 			    __arraycount(utfbuf));
    135 			b = (char *)utfbuf;
    136 		} else if (flags & SHOW_GUID) {
    137 			gpt_uuid_snprintf( buf, sizeof(buf), "%d",
    138 			    ent->ent_guid);
    139 		} else if (flags & SHOW_UUID) {
    140 			gpt_uuid_snprintf(buf, sizeof(buf),
    141 			    "%d", ent->ent_type);
    142 		} else {
    143 			gpt_uuid_snprintf(buf, sizeof(buf), "%ls",
    144 			    ent->ent_type);
    145 		}
    146 		printf("- %s", b);
    147 		break;
    148 	case MAP_TYPE_PMBR:
    149 		printf("PMBR");
    150 		mbr = map_data;
    151 		if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR &&
    152 		    mbr->mbr_part[0].part_flag == 0x80)
    153 			    printf(" (active)");
    154 		break;
    155 	default:
    156 		printf("Unknown %#x", map_type);
    157 		break;
    158 	}
    159 }
    160 
    161 static int
    162 show(gpt_t gpt, int xshow)
    163 {
    164 	map_t m;
    165 
    166 	printf("  %*s", gpt->lbawidth, "start");
    167 	printf("  %*s", gpt->lbawidth, "size");
    168 	printf("  index  contents\n");
    169 
    170 	m = map_first(gpt);
    171 	while (m != NULL) {
    172 		printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
    173 		printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
    174 		putchar(' ');
    175 		putchar(' ');
    176 		if (m->map_index > 0)
    177 			printf("%5d", m->map_index);
    178 		else
    179 			printf("     ");
    180 		putchar(' ');
    181 		putchar(' ');
    182 		print_part_type(m->map_type, xshow, m->map_data, m->map_start);
    183 		putchar('\n');
    184 		m = m->map_next;
    185 	}
    186 	return 0;
    187 }
    188 
    189 static void
    190 gpt_show_sec_num(const char *prompt, int64_t secsize, off_t num)
    191 {
    192 #ifdef HN_AUTOSCALE
    193 	char human_num[5];
    194 	if (humanize_number(human_num, sizeof(human_num),
    195 	    (int64_t)num*secsize,
    196 	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
    197 		human_num[0] = '\0';
    198 #endif
    199 	printf("%s: %" PRIu64, prompt, (uint64_t)num);
    200 #ifdef HN_AUTOSCALE
    201 	if (human_num[0] != '\0')
    202 		printf(" (%s)", human_num);
    203 #endif
    204 	printf("\n");
    205 }
    206 
    207 static int
    208 show_one(gpt_t gpt, unsigned int entry)
    209 {
    210 	map_t m;
    211 	struct gpt_ent *ent;
    212 	char s1[128], s2[128];
    213 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
    214 
    215 	for (m = map_first(gpt); m != NULL; m = m->map_next)
    216 		if (entry == m->map_index)
    217 			break;
    218 	if (m == NULL) {
    219 		gpt_warnx(gpt, "Could not find index %d", entry);
    220 		return -1;
    221 	}
    222 	ent = m->map_data;
    223 
    224 	printf("Details for index %d:\n", entry);
    225 	gpt_show_sec_num("Start", gpt->secsz, m->map_start);
    226 	gpt_show_sec_num("Size", gpt->secsz, m->map_size);
    227 
    228 	gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
    229 	gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
    230 	if (strcmp(s1, s2) == 0)
    231 		strlcpy(s1, "unknown", sizeof(s1));
    232 	printf("Type: %s (%s)\n", s1, s2);
    233 
    234 	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
    235 	printf("GUID: %s\n", s2);
    236 
    237 	utf16_to_utf8(ent->ent_name, __arraycount(ent->ent_name), utfbuf,
    238 	    __arraycount(utfbuf));
    239 	printf("Label: %s\n", (char *)utfbuf);
    240 
    241 	printf("Attributes: ");
    242 	if (ent->ent_attr == 0) {
    243 		printf("None\n");
    244 	} else  {
    245 		char buf[1024];
    246 		printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
    247 	}
    248 
    249 	return 0;
    250 }
    251 
    252 static int
    253 show_all(gpt_t gpt)
    254 {
    255 	map_t m;
    256 	struct gpt_ent *ent;
    257 	char s1[128], s2[128];
    258 #ifdef HN_AUTOSCALE
    259 	char human_num[8];
    260 #endif
    261 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
    262 #define PFX "                                 "
    263 
    264 	printf("  %*s", gpt->lbawidth, "start");
    265 	printf("  %*s", gpt->lbawidth, "size");
    266 	printf("  index  contents\n");
    267 
    268 	m = map_first(gpt);
    269 	while (m != NULL) {
    270 		printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
    271 		printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
    272 		putchar(' ');
    273 		putchar(' ');
    274 		if (m->map_index > 0) {
    275 			printf("%5d  ", m->map_index);
    276 			print_part_type(m->map_type, 0, m->map_data,
    277 			    m->map_start);
    278 			putchar('\n');
    279 
    280 			ent = m->map_data;
    281 
    282 			gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
    283 			gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
    284 			if (strcmp(s1, s2) == 0)
    285 				strlcpy(s1, "unknown", sizeof(s1));
    286 			printf(PFX "Type: %s\n", s1);
    287 			printf(PFX "TypeID: %s\n", s2);
    288 
    289 			gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
    290 			printf(PFX "GUID: %s\n", s2);
    291 
    292 			printf(PFX "Size: ");
    293 #ifdef HN_AUTOSCALE
    294 			if (humanize_number(human_num, sizeof(human_num),
    295 			    (int64_t)(m->map_size * gpt->secsz),
    296 			    "", HN_AUTOSCALE, HN_B) < 0) {
    297 #endif
    298 				printf("%ju",
    299 				    (int64_t)(m->map_size * gpt->secsz));
    300 #ifdef HN_AUTOSCALE
    301 			} else {
    302 				printf("%s", human_num);
    303 			}
    304 #endif
    305 			putchar('\n');
    306 
    307 			utf16_to_utf8(ent->ent_name,
    308 			    __arraycount(ent->ent_name), utfbuf,
    309 			    __arraycount(utfbuf));
    310 			printf(PFX "Label: %s\n", (char *)utfbuf);
    311 
    312 			printf(PFX "Attributes: ");
    313 			if (ent->ent_attr == 0) {
    314 				printf("None\n");
    315 			} else  {
    316 				char buf[1024];
    317 
    318 				printf("%s\n", gpt_attr_list(buf, sizeof(buf),
    319 				    ent->ent_attr));
    320 			}
    321 		} else {
    322 			printf("       ");
    323 			print_part_type(m->map_type, 0, m->map_data,
    324 			    m->map_start);
    325 			putchar('\n');
    326 		}
    327 		m = m->map_next;
    328 	}
    329 	return 0;
    330 }
    331 
    332 static int
    333 cmd_show(gpt_t gpt, int argc, char *argv[])
    334 {
    335 	int ch;
    336 	int xshow = 0;
    337 	unsigned int entry = 0;
    338 	off_t start = 0;
    339 	map_t m;
    340 
    341 	while ((ch = getopt(argc, argv, "gi:b:lua")) != -1) {
    342 		switch(ch) {
    343 		case 'a':
    344 			xshow |= SHOW_ALL;
    345 			break;
    346 		case 'g':
    347 			xshow |= SHOW_GUID;
    348 			break;
    349 		case 'i':
    350 			if (gpt_uint_get(gpt, &entry) == -1)
    351 				return usage();
    352 			break;
    353 		case 'b':
    354 			if (gpt_human_get(gpt, &start) == -1)
    355 				return usage();
    356 			break;
    357 		case 'l':
    358 			xshow |= SHOW_LABEL;
    359 			break;
    360 		case 'u':
    361 			xshow |= SHOW_UUID;
    362 			break;
    363 		default:
    364 			return usage();
    365 		}
    366 	}
    367 
    368 	if (argc != optind)
    369 		return usage();
    370 
    371 	if (map_find(gpt, MAP_TYPE_PRI_GPT_HDR) == NULL)
    372 		printf("GPT not found, displaying data from MBR.\n\n");
    373 
    374 	if (xshow & SHOW_ALL)
    375 		return show_all(gpt);
    376 
    377 	if (start > 0) {
    378 		for (m = map_first(gpt); m != NULL; m = m->map_next) {
    379 			if (m->map_type != MAP_TYPE_GPT_PART ||
    380 			    m->map_index < 1)
    381 				continue;
    382 			if (start != m->map_start)
    383 				continue;
    384 			entry = m->map_index;
    385 			break;
    386 		}
    387 	}
    388 
    389 	return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
    390 }
    391