Home | History | Annotate | Line # | Download | only in gpt
show.c revision 1.41
      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.41 2017/09/07 10:23:33 christos 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 int
    190 show_one(gpt_t gpt, unsigned int entry)
    191 {
    192 	map_t m;
    193 	struct gpt_ent *ent;
    194 	char s1[128], s2[128];
    195 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
    196 
    197 	for (m = map_first(gpt); m != NULL; m = m->map_next)
    198 		if (entry == m->map_index)
    199 			break;
    200 	if (m == NULL) {
    201 		gpt_warnx(gpt, "Could not find index %d", entry);
    202 		return -1;
    203 	}
    204 	ent = m->map_data;
    205 
    206 	printf("Details for index %d:\n", entry);
    207 	gpt_show_num("Start", (uintmax_t)(m->map_start * gpt->secsz));
    208 	gpt_show_num("Size", (uintmax_t)(m->map_size * gpt->secsz));
    209 
    210 	gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
    211 	gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
    212 	if (strcmp(s1, s2) == 0)
    213 		strlcpy(s1, "unknown", sizeof(s1));
    214 	printf("Type: %s (%s)\n", s1, s2);
    215 
    216 	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
    217 	printf("GUID: %s\n", s2);
    218 
    219 	utf16_to_utf8(ent->ent_name, __arraycount(ent->ent_name), utfbuf,
    220 	    __arraycount(utfbuf));
    221 	printf("Label: %s\n", (char *)utfbuf);
    222 
    223 	printf("Attributes: ");
    224 	if (ent->ent_attr == 0) {
    225 		printf("None\n");
    226 	} else  {
    227 		char buf[1024];
    228 		printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
    229 	}
    230 
    231 	return 0;
    232 }
    233 
    234 static int
    235 show_all(gpt_t gpt)
    236 {
    237 	map_t m;
    238 	struct gpt_ent *ent;
    239 	char s1[128], s2[128];
    240 #ifdef HN_AUTOSCALE
    241 	char human_num[8];
    242 #endif
    243 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
    244 #define PFX "                                 "
    245 
    246 	printf("  %*s", gpt->lbawidth, "start");
    247 	printf("  %*s", gpt->lbawidth, "size");
    248 	printf("  index  contents\n");
    249 
    250 	m = map_first(gpt);
    251 	while (m != NULL) {
    252 		printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
    253 		printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
    254 		putchar(' ');
    255 		putchar(' ');
    256 		if (m->map_index > 0) {
    257 			printf("%5d  ", m->map_index);
    258 			print_part_type(m->map_type, 0, m->map_data,
    259 			    m->map_start);
    260 			putchar('\n');
    261 
    262 			ent = m->map_data;
    263 
    264 			gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
    265 			gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
    266 			if (strcmp(s1, s2) == 0)
    267 				strlcpy(s1, "unknown", sizeof(s1));
    268 			printf(PFX "Type: %s\n", s1);
    269 			printf(PFX "TypeID: %s\n", s2);
    270 
    271 			gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
    272 			printf(PFX "GUID: %s\n", s2);
    273 
    274 			printf(PFX "Size: ");
    275 #ifdef HN_AUTOSCALE
    276 			if (humanize_number(human_num, sizeof(human_num),
    277 			    (int64_t)(m->map_size * gpt->secsz),
    278 			    "", HN_AUTOSCALE, HN_B) < 0) {
    279 #endif
    280 				printf("%ju",
    281 				    (int64_t)(m->map_size * gpt->secsz));
    282 #ifdef HN_AUTOSCALE
    283 			} else {
    284 				printf("%s", human_num);
    285 			}
    286 #endif
    287 			putchar('\n');
    288 
    289 			utf16_to_utf8(ent->ent_name,
    290 			    __arraycount(ent->ent_name), utfbuf,
    291 			    __arraycount(utfbuf));
    292 			printf(PFX "Label: %s\n", (char *)utfbuf);
    293 
    294 			printf(PFX "Attributes: ");
    295 			if (ent->ent_attr == 0) {
    296 				printf("None\n");
    297 			} else  {
    298 				char buf[1024];
    299 
    300 				printf("%s\n", gpt_attr_list(buf, sizeof(buf),
    301 				    ent->ent_attr));
    302 			}
    303 		} else {
    304 			printf("       ");
    305 			print_part_type(m->map_type, 0, m->map_data,
    306 			    m->map_start);
    307 			putchar('\n');
    308 		}
    309 		m = m->map_next;
    310 	}
    311 	return 0;
    312 }
    313 
    314 static int
    315 cmd_show(gpt_t gpt, int argc, char *argv[])
    316 {
    317 	int ch;
    318 	int xshow = 0;
    319 	unsigned int entry = 0;
    320 
    321 	while ((ch = getopt(argc, argv, "gi:lua")) != -1) {
    322 		switch(ch) {
    323 		case 'a':
    324 			xshow |= SHOW_ALL;
    325 			break;
    326 		case 'g':
    327 			xshow |= SHOW_GUID;
    328 			break;
    329 		case 'i':
    330 			if (gpt_uint_get(gpt, &entry) == -1)
    331 				return usage();
    332 			break;
    333 		case 'l':
    334 			xshow |= SHOW_LABEL;
    335 			break;
    336 		case 'u':
    337 			xshow |= SHOW_UUID;
    338 			break;
    339 		default:
    340 			return usage();
    341 		}
    342 	}
    343 
    344 	if (argc != optind)
    345 		return usage();
    346 
    347 	if (xshow & SHOW_ALL)
    348 		return show_all(gpt);
    349 
    350 	return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
    351 }
    352