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