show.c revision 1.33
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.33 2015/12/25 12:16:03 wiz 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 52static int cmd_show(gpt_t, int, char *[]); 53 54static const char *showhelp[] = { 55 "[-aglu] [-i index]", 56}; 57 58#define SHOW_UUID 1 59#define SHOW_GUID 2 60#define SHOW_LABEL 4 61#define SHOW_ALL 8 62 63struct 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 72static void 73print_part_type(int map_type, int flags, void *map_data, off_t map_start) 74{ 75 off_t start; 76 map_t p; 77 struct mbr *mbr; 78 struct gpt_ent *ent; 79 unsigned int i; 80 char buf[128], *b = buf; 81 uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1]; 82 83 switch (map_type) { 84 case MAP_TYPE_UNUSED: 85 printf("Unused"); 86 break; 87 case MAP_TYPE_MBR: 88 if (map_start != 0) 89 printf("Extended "); 90 printf("MBR"); 91 break; 92 case MAP_TYPE_PRI_GPT_HDR: 93 printf("Pri GPT header"); 94 break; 95 case MAP_TYPE_SEC_GPT_HDR: 96 printf("Sec GPT header"); 97 break; 98 case MAP_TYPE_PRI_GPT_TBL: 99 printf("Pri GPT table"); 100 break; 101 case MAP_TYPE_SEC_GPT_TBL: 102 printf("Sec GPT table"); 103 break; 104 case MAP_TYPE_MBR_PART: 105 p = map_data; 106 if (p->map_start != 0) 107 printf("Extended "); 108 printf("MBR part "); 109 mbr = p->map_data; 110 for (i = 0; i < 4; i++) { 111 start = le16toh(mbr->mbr_part[i].part_start_hi); 112 start = (start << 16) + 113 le16toh(mbr->mbr_part[i].part_start_lo); 114 if (map_start == p->map_start + start) 115 break; 116 } 117 printf("%d", mbr->mbr_part[i].part_typ); 118 break; 119 case MAP_TYPE_GPT_PART: 120 printf("GPT part "); 121 ent = map_data; 122 if (flags & SHOW_LABEL) { 123 utf16_to_utf8(ent->ent_name, utfbuf, 124 sizeof(utfbuf)); 125 b = (char *)utfbuf; 126 } else if (flags & SHOW_GUID) { 127 gpt_uuid_snprintf( buf, sizeof(buf), "%d", 128 ent->ent_guid); 129 } else if (flags & SHOW_UUID) { 130 gpt_uuid_snprintf(buf, sizeof(buf), 131 "%d", ent->ent_type); 132 } else { 133 gpt_uuid_snprintf(buf, sizeof(buf), "%ls", 134 ent->ent_type); 135 } 136 printf("- %s", b); 137 break; 138 case MAP_TYPE_PMBR: 139 printf("PMBR"); 140 break; 141 default: 142 printf("Unknown %#x", map_type); 143 break; 144 } 145} 146 147static int 148show(gpt_t gpt, int show) 149{ 150 map_t m; 151 152 printf(" %*s", gpt->lbawidth, "start"); 153 printf(" %*s", gpt->lbawidth, "size"); 154 printf(" index contents\n"); 155 156 m = map_first(gpt); 157 while (m != NULL) { 158 printf(" %*llu", gpt->lbawidth, (long long)m->map_start); 159 printf(" %*llu", gpt->lbawidth, (long long)m->map_size); 160 putchar(' '); 161 putchar(' '); 162 if (m->map_index > 0) 163 printf("%5d", m->map_index); 164 else 165 printf(" "); 166 putchar(' '); 167 putchar(' '); 168 print_part_type(m->map_type, show, m->map_data, m->map_start); 169 putchar('\n'); 170 m = m->map_next; 171 } 172 return 0; 173} 174 175static int 176show_one(gpt_t gpt, unsigned int entry) 177{ 178 map_t m; 179 struct gpt_ent *ent; 180 char s1[128], s2[128]; 181 uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1]; 182 183 for (m = map_first(gpt); m != NULL; m = m->map_next) 184 if (entry == m->map_index) 185 break; 186 if (m == NULL) { 187 gpt_warnx(gpt, "Could not find index %d", entry); 188 return -1; 189 } 190 ent = m->map_data; 191 192 printf("Details for index %d:\n", entry); 193 gpt_show_num("Start", (uintmax_t)(m->map_start * gpt->secsz)); 194 gpt_show_num("Size", (uintmax_t)(m->map_size * gpt->secsz)); 195 196 gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type); 197 gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type); 198 if (strcmp(s1, s2) == 0) 199 strlcpy(s1, "unknown", sizeof(s1)); 200 printf("Type: %s (%s)\n", s1, s2); 201 202 gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid); 203 printf("GUID: %s\n", s2); 204 205 utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf)); 206 printf("Label: %s\n", (char *)utfbuf); 207 208 printf("Attributes: "); 209 if (ent->ent_attr == 0) { 210 printf("None\n"); 211 } else { 212 char buf[1024]; 213 printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr)); 214 } 215 216 return 0; 217} 218 219static int 220show_all(gpt_t gpt) 221{ 222 map_t m; 223 struct gpt_ent *ent; 224 char s1[128], s2[128]; 225 uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1]; 226#define PFX " " 227 228 printf(" %*s", gpt->lbawidth, "start"); 229 printf(" %*s", gpt->lbawidth, "size"); 230 printf(" index contents\n"); 231 232 m = map_first(gpt); 233 while (m != NULL) { 234 printf(" %*llu", gpt->lbawidth, (long long)m->map_start); 235 printf(" %*llu", gpt->lbawidth, (long long)m->map_size); 236 putchar(' '); 237 putchar(' '); 238 if (m->map_index > 0) { 239 printf("%5d ", m->map_index); 240 print_part_type(m->map_type, 0, m->map_data, 241 m->map_start); 242 putchar('\n'); 243 244 ent = m->map_data; 245 246 gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type); 247 gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type); 248 if (strcmp(s1, s2) == 0) 249 strlcpy(s1, "unknown", sizeof(s1)); 250 printf(PFX "Type: %s (%s)\n", s1, s2); 251 252 gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid); 253 printf(PFX "GUID: %s\n", s2); 254 255 utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf)); 256 printf(PFX "Label: %s\n", (char *)utfbuf); 257 258 printf(PFX "Attributes: "); 259 if (ent->ent_attr == 0) { 260 printf("None\n"); 261 } else { 262 char buf[1024]; 263 264 printf("%s\n", gpt_attr_list(buf, sizeof(buf), 265 ent->ent_attr)); 266 } 267 } else { 268 printf(" "); 269 print_part_type(m->map_type, 0, m->map_data, 270 m->map_start); 271 putchar('\n'); 272 } 273 m = m->map_next; 274 } 275 return 0; 276} 277 278static int 279cmd_show(gpt_t gpt, int argc, char *argv[]) 280{ 281 int ch; 282 int xshow = 0; 283 unsigned int entry = 0; 284 285 while ((ch = getopt(argc, argv, "gi:lua")) != -1) { 286 switch(ch) { 287 case 'a': 288 xshow |= SHOW_ALL; 289 break; 290 case 'g': 291 xshow |= SHOW_GUID; 292 break; 293 case 'i': 294 if (gpt_uint_get(&entry) == -1) 295 return usage(); 296 break; 297 case 'l': 298 xshow |= SHOW_LABEL; 299 break; 300 case 'u': 301 xshow |= SHOW_UUID; 302 break; 303 default: 304 return usage(); 305 } 306 } 307 308 if (argc != optind) 309 return usage(); 310 311 if (xshow & SHOW_ALL) 312 return show_all(gpt); 313 314 return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow); 315} 316