Home | History | Annotate | Line # | Download | only in gpt
show.c revision 1.7.8.1
      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.1  christos #include <sys/cdefs.h>
     28      1.2  christos #ifdef __FBSDID
     29      1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
     30      1.2  christos #endif
     31      1.2  christos #ifdef __RCSID
     32  1.7.8.1       tls __RCSID("$NetBSD: show.c,v 1.7.8.1 2013/02/25 00:28:08 tls Exp $");
     33      1.2  christos #endif
     34      1.1  christos 
     35      1.1  christos #include <sys/types.h>
     36      1.1  christos 
     37      1.1  christos #include <err.h>
     38      1.1  christos #include <stddef.h>
     39      1.1  christos #include <stdio.h>
     40      1.1  christos #include <stdlib.h>
     41      1.1  christos #include <string.h>
     42      1.1  christos #include <unistd.h>
     43      1.1  christos 
     44      1.1  christos #include "map.h"
     45      1.1  christos #include "gpt.h"
     46      1.1  christos 
     47      1.1  christos static int show_label = 0;
     48      1.1  christos static int show_uuid = 0;
     49      1.1  christos 
     50      1.3       riz const char showmsg[] = "show [-lu] device ...";
     51      1.3       riz 
     52      1.7     joerg __dead static void
     53      1.1  christos usage_show(void)
     54      1.1  christos {
     55      1.1  christos 
     56      1.1  christos 	fprintf(stderr,
     57      1.3       riz 	    "usage: %s %s\n", getprogname(), showmsg);
     58      1.1  christos 	exit(1);
     59      1.1  christos }
     60      1.1  christos 
     61      1.1  christos static const char *
     62      1.1  christos friendly(uuid_t *t)
     63      1.1  christos {
     64      1.1  christos 	static uuid_t efi_slice = GPT_ENT_TYPE_EFI;
     65      1.5  christos 	static uuid_t bios_boot = GPT_ENT_TYPE_BIOS;
     66  1.7.8.1       tls 	static uuid_t msdata = GPT_ENT_TYPE_MS_BASIC_DATA;
     67      1.1  christos 	static uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
     68      1.1  christos 	static uuid_t hfs = GPT_ENT_TYPE_APPLE_HFS;
     69  1.7.8.1       tls 	static uuid_t linuxdata = GPT_ENT_TYPE_LINUX_DATA;
     70      1.1  christos 	static uuid_t linuxswap = GPT_ENT_TYPE_LINUX_SWAP;
     71      1.1  christos 	static uuid_t msr = GPT_ENT_TYPE_MS_RESERVED;
     72      1.1  christos 	static uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
     73      1.1  christos 	static uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
     74      1.1  christos 	static uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
     75      1.4       riz 	static uuid_t nb_swap = GPT_ENT_TYPE_NETBSD_SWAP;
     76      1.6  jakllsch 	static uuid_t nb_ffs = GPT_ENT_TYPE_NETBSD_FFS;
     77      1.4       riz 	static uuid_t nb_lfs = GPT_ENT_TYPE_NETBSD_LFS;
     78      1.4       riz 	static uuid_t nb_raid = GPT_ENT_TYPE_NETBSD_RAIDFRAME;
     79      1.4       riz 	static uuid_t nb_ccd = GPT_ENT_TYPE_NETBSD_CCD;
     80      1.4       riz 	static uuid_t nb_cgd = GPT_ENT_TYPE_NETBSD_CGD;
     81      1.1  christos 	static char buf[80];
     82      1.1  christos 	char *s;
     83      1.1  christos 
     84      1.1  christos 	if (show_uuid)
     85      1.1  christos 		goto unfriendly;
     86      1.1  christos 
     87      1.1  christos 	if (uuid_equal(t, &efi_slice, NULL))
     88      1.1  christos 		return ("EFI System");
     89      1.5  christos 	if (uuid_equal(t, &bios_boot, NULL))
     90      1.5  christos 		return ("BIOS Boot");
     91      1.4       riz 	if (uuid_equal(t, &nb_swap, NULL))
     92      1.4       riz 		return ("NetBSD swap");
     93      1.6  jakllsch 	if (uuid_equal(t, &nb_ffs, NULL))
     94      1.6  jakllsch 		return ("NetBSD FFSv1/FFSv2");
     95      1.4       riz 	if (uuid_equal(t, &nb_lfs, NULL))
     96      1.4       riz 		return ("NetBSD LFS");
     97      1.4       riz 	if (uuid_equal(t, &nb_raid, NULL))
     98      1.4       riz 		return ("NetBSD RAIDFrame component");
     99      1.4       riz 	if (uuid_equal(t, &nb_ccd, NULL))
    100      1.4       riz 		return ("NetBSD ccd component");
    101      1.4       riz 	if (uuid_equal(t, &nb_cgd, NULL))
    102      1.4       riz 		return ("NetBSD Cryptographic Disk");
    103      1.1  christos 	if (uuid_equal(t, &swap, NULL))
    104      1.1  christos 		return ("FreeBSD swap");
    105      1.1  christos 	if (uuid_equal(t, &ufs, NULL))
    106      1.1  christos 		return ("FreeBSD UFS/UFS2");
    107      1.1  christos 	if (uuid_equal(t, &vinum, NULL))
    108      1.1  christos 		return ("FreeBSD vinum");
    109      1.1  christos 
    110      1.1  christos 	if (uuid_equal(t, &freebsd, NULL))
    111      1.1  christos 		return ("FreeBSD legacy");
    112  1.7.8.1       tls 	if (uuid_equal(t, &msdata, NULL))
    113  1.7.8.1       tls 		return ("Windows basic data");
    114      1.1  christos 	if (uuid_equal(t, &msr, NULL))
    115      1.1  christos 		return ("Windows reserved");
    116  1.7.8.1       tls 	if (uuid_equal(t, &linuxdata, NULL))
    117  1.7.8.1       tls 		return ("Linux data");
    118  1.7.8.1       tls 	if (uuid_equal(t, &linuxswap, NULL))
    119  1.7.8.1       tls 		return ("Linux swap");
    120      1.1  christos 	if (uuid_equal(t, &hfs, NULL))
    121      1.1  christos 		return ("Apple HFS");
    122      1.1  christos 
    123      1.1  christos unfriendly:
    124      1.1  christos 	uuid_to_string(t, &s, NULL);
    125      1.1  christos 	strlcpy(buf, s, sizeof buf);
    126      1.1  christos 	free(s);
    127      1.1  christos 	return (buf);
    128      1.1  christos }
    129      1.1  christos 
    130      1.1  christos static void
    131      1.1  christos show(int fd __unused)
    132      1.1  christos {
    133      1.1  christos 	uuid_t type;
    134      1.1  christos 	off_t start;
    135      1.1  christos 	map_t *m, *p;
    136      1.1  christos 	struct mbr *mbr;
    137      1.1  christos 	struct gpt_ent *ent;
    138      1.1  christos 	unsigned int i;
    139      1.1  christos 
    140      1.1  christos 	printf("  %*s", lbawidth, "start");
    141      1.1  christos 	printf("  %*s", lbawidth, "size");
    142      1.1  christos 	printf("  index  contents\n");
    143      1.1  christos 
    144      1.1  christos 	m = map_first();
    145      1.1  christos 	while (m != NULL) {
    146      1.1  christos 		printf("  %*llu", lbawidth, (long long)m->map_start);
    147      1.1  christos 		printf("  %*llu", lbawidth, (long long)m->map_size);
    148      1.1  christos 		putchar(' ');
    149      1.1  christos 		putchar(' ');
    150      1.1  christos 		if (m->map_index > 0)
    151      1.1  christos 			printf("%5d", m->map_index);
    152      1.1  christos 		else
    153      1.1  christos 			printf("     ");
    154      1.1  christos 		putchar(' ');
    155      1.1  christos 		putchar(' ');
    156      1.1  christos 		switch (m->map_type) {
    157      1.1  christos 		case MAP_TYPE_MBR:
    158      1.1  christos 			if (m->map_start != 0)
    159      1.1  christos 				printf("Extended ");
    160      1.1  christos 			printf("MBR");
    161      1.1  christos 			break;
    162      1.1  christos 		case MAP_TYPE_PRI_GPT_HDR:
    163      1.1  christos 			printf("Pri GPT header");
    164      1.1  christos 			break;
    165      1.1  christos 		case MAP_TYPE_SEC_GPT_HDR:
    166      1.1  christos 			printf("Sec GPT header");
    167      1.1  christos 			break;
    168      1.1  christos 		case MAP_TYPE_PRI_GPT_TBL:
    169      1.1  christos 			printf("Pri GPT table");
    170      1.1  christos 			break;
    171      1.1  christos 		case MAP_TYPE_SEC_GPT_TBL:
    172      1.1  christos 			printf("Sec GPT table");
    173      1.1  christos 			break;
    174      1.1  christos 		case MAP_TYPE_MBR_PART:
    175      1.1  christos 			p = m->map_data;
    176      1.1  christos 			if (p->map_start != 0)
    177      1.1  christos 				printf("Extended ");
    178      1.1  christos 			printf("MBR part ");
    179      1.1  christos 			mbr = p->map_data;
    180      1.1  christos 			for (i = 0; i < 4; i++) {
    181      1.1  christos 				start = le16toh(mbr->mbr_part[i].part_start_hi);
    182      1.1  christos 				start = (start << 16) +
    183      1.1  christos 				    le16toh(mbr->mbr_part[i].part_start_lo);
    184      1.1  christos 				if (m->map_start == p->map_start + start)
    185      1.1  christos 					break;
    186      1.1  christos 			}
    187      1.1  christos 			printf("%d", mbr->mbr_part[i].part_typ);
    188      1.1  christos 			break;
    189      1.1  christos 		case MAP_TYPE_GPT_PART:
    190      1.1  christos 			printf("GPT part ");
    191      1.1  christos 			ent = m->map_data;
    192      1.1  christos 			if (show_label) {
    193      1.1  christos 				printf("- \"%s\"",
    194      1.1  christos 				    utf16_to_utf8(ent->ent_name));
    195      1.1  christos 			} else {
    196      1.1  christos 				le_uuid_dec(&ent->ent_type, &type);
    197      1.1  christos 				printf("- %s", friendly(&type));
    198      1.1  christos 			}
    199      1.1  christos 			break;
    200      1.1  christos 		case MAP_TYPE_PMBR:
    201      1.1  christos 			printf("PMBR");
    202      1.1  christos 			break;
    203      1.1  christos 		}
    204      1.1  christos 		putchar('\n');
    205      1.1  christos 		m = m->map_next;
    206      1.1  christos 	}
    207      1.1  christos }
    208      1.1  christos 
    209      1.1  christos int
    210      1.1  christos cmd_show(int argc, char *argv[])
    211      1.1  christos {
    212      1.1  christos 	int ch, fd;
    213      1.1  christos 
    214      1.1  christos 	while ((ch = getopt(argc, argv, "lu")) != -1) {
    215      1.1  christos 		switch(ch) {
    216      1.1  christos 		case 'l':
    217      1.1  christos 			show_label = 1;
    218      1.1  christos 			break;
    219      1.1  christos 		case 'u':
    220      1.1  christos 			show_uuid = 1;
    221      1.1  christos 			break;
    222      1.1  christos 		default:
    223      1.1  christos 			usage_show();
    224      1.1  christos 		}
    225      1.1  christos 	}
    226      1.1  christos 
    227      1.1  christos 	if (argc == optind)
    228      1.1  christos 		usage_show();
    229      1.1  christos 
    230      1.1  christos 	while (optind < argc) {
    231      1.1  christos 		fd = gpt_open(argv[optind++]);
    232      1.1  christos 		if (fd == -1) {
    233      1.1  christos 			warn("unable to open device '%s'", device_name);
    234      1.1  christos 			continue;
    235      1.1  christos 		}
    236      1.1  christos 
    237      1.1  christos 		show(fd);
    238      1.1  christos 
    239      1.1  christos 		gpt_close(fd);
    240      1.1  christos 	}
    241      1.1  christos 
    242      1.1  christos 	return (0);
    243      1.1  christos }
    244