Home | History | Annotate | Line # | Download | only in gpt
show.c revision 1.29
      1   1.1  christos /*-
      2   1.1  christos  * Copyright (c) 2002 Marcel Moolenaar
      3   1.1  christos  * All rights reserved.
      4   1.1  christos  *
      5   1.1  christos  * Redistribution and use in source and binary forms, with or without
      6   1.1  christos  * modification, are permitted provided that the following conditions
      7   1.1  christos  * are met:
      8   1.1  christos  *
      9   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  christos  *    documentation and/or other materials provided with the distribution.
     14   1.1  christos  *
     15   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25   1.1  christos  */
     26   1.1  christos 
     27  1.16  christos #if HAVE_NBTOOL_CONFIG_H
     28  1.16  christos #include "nbtool_config.h"
     29  1.16  christos #endif
     30  1.16  christos 
     31   1.1  christos #include <sys/cdefs.h>
     32   1.2  christos #ifdef __FBSDID
     33   1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
     34   1.2  christos #endif
     35   1.2  christos #ifdef __RCSID
     36  1.29  christos __RCSID("$NetBSD: show.c,v 1.29 2015/12/03 02:02:43 christos Exp $");
     37   1.2  christos #endif
     38   1.1  christos 
     39   1.1  christos #include <sys/types.h>
     40   1.1  christos 
     41   1.1  christos #include <err.h>
     42   1.1  christos #include <stddef.h>
     43   1.1  christos #include <stdio.h>
     44   1.1  christos #include <stdlib.h>
     45   1.1  christos #include <string.h>
     46   1.1  christos #include <unistd.h>
     47   1.1  christos 
     48   1.1  christos #include "map.h"
     49   1.1  christos #include "gpt.h"
     50  1.24  christos #include "gpt_private.h"
     51   1.1  christos 
     52  1.25  christos static int cmd_show(gpt_t, int, char *[]);
     53   1.3       riz 
     54  1.25  christos static const char *showhelp[] = {
     55  1.28  christos 	"[-glu] [-i index]",
     56  1.25  christos };
     57  1.25  christos 
     58  1.28  christos #define SHOW_UUID  1
     59  1.28  christos #define SHOW_GUID  2
     60  1.28  christos #define SHOW_LABEL 4
     61  1.28  christos 
     62  1.25  christos struct gpt_cmd c_show = {
     63  1.25  christos 	"show",
     64  1.25  christos 	cmd_show,
     65  1.25  christos 	showhelp, __arraycount(showhelp),
     66  1.25  christos 	GPT_READONLY,
     67  1.25  christos };
     68   1.1  christos 
     69  1.25  christos #define usage() gpt_usage(NULL, &c_show)
     70   1.1  christos 
     71  1.24  christos static int
     72  1.28  christos show(gpt_t gpt, int show)
     73   1.1  christos {
     74   1.1  christos 	off_t start;
     75  1.24  christos 	map_t m, p;
     76   1.1  christos 	struct mbr *mbr;
     77   1.1  christos 	struct gpt_ent *ent;
     78   1.1  christos 	unsigned int i;
     79  1.28  christos 	char buf[128], *b = buf;
     80  1.27  christos 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
     81   1.1  christos 
     82  1.24  christos 	printf("  %*s", gpt->lbawidth, "start");
     83  1.24  christos 	printf("  %*s", gpt->lbawidth, "size");
     84   1.1  christos 	printf("  index  contents\n");
     85   1.1  christos 
     86  1.24  christos 	m = map_first(gpt);
     87   1.1  christos 	while (m != NULL) {
     88  1.24  christos 		printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
     89  1.24  christos 		printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
     90   1.1  christos 		putchar(' ');
     91   1.1  christos 		putchar(' ');
     92   1.1  christos 		if (m->map_index > 0)
     93   1.1  christos 			printf("%5d", m->map_index);
     94   1.1  christos 		else
     95   1.1  christos 			printf("     ");
     96   1.1  christos 		putchar(' ');
     97   1.1  christos 		putchar(' ');
     98   1.1  christos 		switch (m->map_type) {
     99  1.23  christos 		case MAP_TYPE_UNUSED:
    100  1.23  christos 			printf("Unused");
    101  1.23  christos 			break;
    102   1.1  christos 		case MAP_TYPE_MBR:
    103   1.1  christos 			if (m->map_start != 0)
    104   1.1  christos 				printf("Extended ");
    105   1.1  christos 			printf("MBR");
    106   1.1  christos 			break;
    107   1.1  christos 		case MAP_TYPE_PRI_GPT_HDR:
    108   1.1  christos 			printf("Pri GPT header");
    109   1.1  christos 			break;
    110   1.1  christos 		case MAP_TYPE_SEC_GPT_HDR:
    111   1.1  christos 			printf("Sec GPT header");
    112   1.1  christos 			break;
    113   1.1  christos 		case MAP_TYPE_PRI_GPT_TBL:
    114   1.1  christos 			printf("Pri GPT table");
    115   1.1  christos 			break;
    116   1.1  christos 		case MAP_TYPE_SEC_GPT_TBL:
    117   1.1  christos 			printf("Sec GPT table");
    118   1.1  christos 			break;
    119   1.1  christos 		case MAP_TYPE_MBR_PART:
    120   1.1  christos 			p = m->map_data;
    121   1.1  christos 			if (p->map_start != 0)
    122   1.1  christos 				printf("Extended ");
    123   1.1  christos 			printf("MBR part ");
    124   1.1  christos 			mbr = p->map_data;
    125   1.1  christos 			for (i = 0; i < 4; i++) {
    126   1.1  christos 				start = le16toh(mbr->mbr_part[i].part_start_hi);
    127   1.1  christos 				start = (start << 16) +
    128   1.1  christos 				    le16toh(mbr->mbr_part[i].part_start_lo);
    129   1.1  christos 				if (m->map_start == p->map_start + start)
    130   1.1  christos 					break;
    131   1.1  christos 			}
    132   1.1  christos 			printf("%d", mbr->mbr_part[i].part_typ);
    133   1.1  christos 			break;
    134   1.1  christos 		case MAP_TYPE_GPT_PART:
    135   1.1  christos 			printf("GPT part ");
    136   1.1  christos 			ent = m->map_data;
    137  1.28  christos 			if (show & SHOW_LABEL) {
    138  1.27  christos 				utf16_to_utf8(ent->ent_name, utfbuf,
    139  1.27  christos 				    sizeof(utfbuf));
    140  1.28  christos 				b = (char *)buf;
    141  1.28  christos 			} else if (show & SHOW_GUID) {
    142  1.28  christos 				gpt_uuid_snprintf( buf, sizeof(buf), "%d",
    143  1.28  christos 				    ent->ent_guid);
    144  1.28  christos 			} else if (show & SHOW_UUID) {
    145  1.28  christos 				gpt_uuid_snprintf(buf, sizeof(buf),
    146  1.28  christos 				    "%d", ent->ent_type);
    147   1.1  christos 			} else {
    148  1.28  christos 				gpt_uuid_snprintf(buf, sizeof(buf), "%ls",
    149  1.28  christos 				    ent->ent_type);
    150   1.1  christos 			}
    151  1.28  christos 			printf("- %s", b);
    152   1.1  christos 			break;
    153   1.1  christos 		case MAP_TYPE_PMBR:
    154   1.1  christos 			printf("PMBR");
    155   1.1  christos 			break;
    156  1.23  christos 		default:
    157  1.23  christos 			printf("Unknown %#x", m->map_type);
    158  1.23  christos 			break;
    159   1.1  christos 		}
    160   1.1  christos 		putchar('\n');
    161   1.1  christos 		m = m->map_next;
    162   1.1  christos 	}
    163  1.24  christos 	return 0;
    164   1.1  christos }
    165   1.1  christos 
    166  1.24  christos static int
    167  1.28  christos show_one(gpt_t gpt, unsigned int entry)
    168  1.12   jnemeth {
    169  1.24  christos 	map_t m;
    170  1.12   jnemeth 	struct gpt_ent *ent;
    171  1.19  christos 	char s1[128], s2[128];
    172  1.27  christos 	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
    173  1.12   jnemeth 
    174  1.24  christos 	for (m = map_first(gpt); m != NULL; m = m->map_next)
    175  1.12   jnemeth 		if (entry == m->map_index)
    176  1.12   jnemeth 			break;
    177  1.12   jnemeth 	if (m == NULL) {
    178  1.24  christos 		gpt_warnx(gpt, "Could not find index %d", entry);
    179  1.24  christos 		return -1;
    180  1.12   jnemeth 	}
    181  1.12   jnemeth 	ent = m->map_data;
    182  1.12   jnemeth 
    183  1.12   jnemeth 	printf("Details for index %d:\n", entry);
    184  1.29  christos 	gpt_show_num("Start", (uintmax_t)(m->map_start * gpt->secsz));
    185  1.29  christos 	gpt_show_num("Size", (uintmax_t)(m->map_size * gpt->secsz));
    186  1.12   jnemeth 
    187  1.19  christos 	gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
    188  1.20   jnemeth 	gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
    189  1.12   jnemeth 	if (strcmp(s1, s2) == 0)
    190  1.19  christos 		strlcpy(s1, "unknown", sizeof(s1));
    191  1.12   jnemeth 	printf("Type: %s (%s)\n", s1, s2);
    192  1.12   jnemeth 
    193  1.19  christos 	gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
    194  1.12   jnemeth 	printf("GUID: %s\n", s2);
    195  1.12   jnemeth 
    196  1.27  christos 	utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
    197  1.27  christos 	printf("Label: %s\n", (char *)utfbuf);
    198  1.12   jnemeth 
    199  1.12   jnemeth 	printf("Attributes:\n");
    200  1.28  christos 	if (ent->ent_attr == 0) {
    201  1.12   jnemeth 		printf("  None\n");
    202  1.28  christos 		return 0;
    203  1.12   jnemeth 	}
    204  1.28  christos 	if (ent->ent_attr & GPT_ENT_ATTR_REQUIRED_PARTITION)
    205  1.28  christos 		printf("  required for platform to function\n");
    206  1.28  christos 	if (ent->ent_attr & GPT_ENT_ATTR_NO_BLOCK_IO_PROTOCOL)
    207  1.28  christos 		printf("  UEFI won't recognize file system\n");
    208  1.28  christos 	if (ent->ent_attr & GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE)
    209  1.28  christos 		printf("  legacy BIOS boot partition\n");
    210  1.28  christos 	if (ent->ent_attr & GPT_ENT_ATTR_BOOTME)
    211  1.28  christos 		printf("  indicates a bootable partition\n");
    212  1.28  christos 	if (ent->ent_attr & GPT_ENT_ATTR_BOOTONCE)
    213  1.28  christos 		printf("  attempt to boot this partition only once\n");
    214  1.28  christos 	if (ent->ent_attr & GPT_ENT_ATTR_BOOTFAILED)
    215  1.28  christos 		printf("  partition was marked bootonce but failed to boot\n");
    216  1.24  christos 	return 0;
    217  1.12   jnemeth }
    218  1.12   jnemeth 
    219  1.25  christos static int
    220  1.24  christos cmd_show(gpt_t gpt, int argc, char *argv[])
    221   1.1  christos {
    222  1.24  christos 	int ch;
    223  1.28  christos 	int xshow = 0;
    224  1.28  christos 	unsigned int entry = 0;
    225   1.1  christos 
    226  1.12   jnemeth 	while ((ch = getopt(argc, argv, "gi:lu")) != -1) {
    227   1.1  christos 		switch(ch) {
    228  1.12   jnemeth 		case 'g':
    229  1.28  christos 			xshow |= SHOW_GUID;
    230  1.12   jnemeth 			break;
    231  1.12   jnemeth 		case 'i':
    232  1.29  christos 			if (gpt_uint_get(&entry) == -1)
    233  1.25  christos 				return usage();
    234  1.12   jnemeth 			break;
    235   1.1  christos 		case 'l':
    236  1.28  christos 			xshow |= SHOW_LABEL;
    237   1.1  christos 			break;
    238   1.1  christos 		case 'u':
    239  1.28  christos 			xshow |= SHOW_UUID;
    240   1.1  christos 			break;
    241   1.1  christos 		default:
    242  1.25  christos 			return usage();
    243   1.1  christos 		}
    244   1.1  christos 	}
    245   1.1  christos 
    246  1.24  christos 	if (argc != optind)
    247  1.25  christos 		return usage();
    248   1.1  christos 
    249  1.28  christos 	return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
    250   1.1  christos }
    251