Home | History | Annotate | Line # | Download | only in gpt
show.c revision 1.28
      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.28 2015/12/03 01:07:28 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 cmd_show(gpt_t, int, char *[]);
     53 
     54 static const char *showhelp[] = {
     55 	"[-glu] [-i index]",
     56 };
     57 
     58 #define SHOW_UUID  1
     59 #define SHOW_GUID  2
     60 #define SHOW_LABEL 4
     61 
     62 struct gpt_cmd c_show = {
     63 	"show",
     64 	cmd_show,
     65 	showhelp, __arraycount(showhelp),
     66 	GPT_READONLY,
     67 };
     68 
     69 #define usage() gpt_usage(NULL, &c_show)
     70 
     71 static int
     72 show(gpt_t gpt, int show)
     73 {
     74 	off_t start;
     75 	map_t m, p;
     76 	struct mbr *mbr;
     77 	struct gpt_ent *ent;
     78 	unsigned int i;
     79 	char buf[128], *b = buf;
     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 & SHOW_LABEL) {
    138 				utf16_to_utf8(ent->ent_name, utfbuf,
    139 				    sizeof(utfbuf));
    140 				b = (char *)buf;
    141 			} else if (show & SHOW_GUID) {
    142 				gpt_uuid_snprintf( buf, sizeof(buf), "%d",
    143 				    ent->ent_guid);
    144 			} else if (show & SHOW_UUID) {
    145 				gpt_uuid_snprintf(buf, sizeof(buf),
    146 				    "%d", ent->ent_type);
    147 			} else {
    148 				gpt_uuid_snprintf(buf, sizeof(buf), "%ls",
    149 				    ent->ent_type);
    150 			}
    151 			printf("- %s", b);
    152 			break;
    153 		case MAP_TYPE_PMBR:
    154 			printf("PMBR");
    155 			break;
    156 		default:
    157 			printf("Unknown %#x", m->map_type);
    158 			break;
    159 		}
    160 		putchar('\n');
    161 		m = m->map_next;
    162 	}
    163 	return 0;
    164 }
    165 
    166 static void
    167 show_num(gpt_t gpt, const char *prompt, uintmax_t num)
    168 {
    169 #ifdef HN_AUTOSCALE
    170 	char human_num[5];
    171 	if (humanize_number(human_num, 5, (int64_t)(num * gpt->secsz),
    172 	    "", HN_AUTOSCALE, HN_NOSPACE|HN_B) < 0)
    173 		human_num[0] = '\0';
    174 #endif
    175 	printf("%s: %ju", prompt, num);
    176 #ifdef HN_AUTOSCALE
    177 	if (human_num[0] != '\0')
    178 		printf("(%s)", human_num);
    179 #endif
    180 
    181 	printf("\n");
    182 }
    183 
    184 static int
    185 show_one(gpt_t gpt, unsigned int entry)
    186 {
    187 	map_t m;
    188 	struct gpt_ent *ent;
    189 	char s1[128], s2[128];
    190 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
    191 
    192 	for (m = map_first(gpt); m != NULL; m = m->map_next)
    193 		if (entry == m->map_index)
    194 			break;
    195 	if (m == NULL) {
    196 		gpt_warnx(gpt, "Could not find index %d", entry);
    197 		return -1;
    198 	}
    199 	ent = m->map_data;
    200 
    201 	printf("Details for index %d:\n", entry);
    202 	show_num(gpt, "Start", m->map_start);
    203 	show_num(gpt, "Size", m->map_size);
    204 
    205 	gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
    206 	gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
    207 	if (strcmp(s1, s2) == 0)
    208 		strlcpy(s1, "unknown", sizeof(s1));
    209 	printf("Type: %s (%s)\n", s1, s2);
    210 
    211 	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
    212 	printf("GUID: %s\n", s2);
    213 
    214 	utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
    215 	printf("Label: %s\n", (char *)utfbuf);
    216 
    217 	printf("Attributes:\n");
    218 	if (ent->ent_attr == 0) {
    219 		printf("  None\n");
    220 		return 0;
    221 	}
    222 	if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
    223 		printf("  required for platform to function\n");
    224 	if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
    225 		printf("  UEFI won't recognize file system\n");
    226 	if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
    227 		printf("  legacy BIOS boot partition\n");
    228 	if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
    229 		printf("  indicates a bootable partition\n");
    230 	if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
    231 		printf("  attempt to boot this partition only once\n");
    232 	if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
    233 		printf("  partition was marked bootonce but failed to boot\n");
    234 	return 0;
    235 }
    236 
    237 static int
    238 cmd_show(gpt_t gpt, int argc, char *argv[])
    239 {
    240 	int ch;
    241 	int xshow = 0;
    242 	unsigned int entry = 0;
    243 
    244 	while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
    245 		switch(ch) {
    246 		case 'g':
    247 			xshow |= SHOW_GUID;
    248 			break;
    249 		case 'i':
    250 			if (gpt_entry_get(&entry) == -1)
    251 				return usage();
    252 			break;
    253 		case 'l':
    254 			xshow |= SHOW_LABEL;
    255 			break;
    256 		case 'u':
    257 			xshow |= SHOW_UUID;
    258 			break;
    259 		default:
    260 			return usage();
    261 		}
    262 	}
    263 
    264 	if (argc != optind)
    265 		return usage();
    266 
    267 	return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
    268 }
    269