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