[BACK]Return to show.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sbin / gpt

Annotation of src/sbin/gpt/show.c, Revision 1.35

1.1       christos    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:
1.16      christos   27: #if HAVE_NBTOOL_CONFIG_H
                     28: #include "nbtool_config.h"
                     29: #endif
                     30:
1.1       christos   31: #include <sys/cdefs.h>
1.2       christos   32: #ifdef __FBSDID
1.1       christos   33: __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
1.2       christos   34: #endif
                     35: #ifdef __RCSID
1.35    ! christos   36: __RCSID("$NetBSD: show.c,v 1.34 2015/12/26 13:08:45 martin Exp $");
1.2       christos   37: #endif
1.1       christos   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"
1.24      christos   50: #include "gpt_private.h"
1.1       christos   51:
1.25      christos   52: static int cmd_show(gpt_t, int, char *[]);
1.3       riz        53:
1.25      christos   54: static const char *showhelp[] = {
1.33      wiz        55:        "[-aglu] [-i index]",
1.25      christos   56: };
                     57:
1.28      christos   58: #define SHOW_UUID  1
                     59: #define SHOW_GUID  2
                     60: #define SHOW_LABEL 4
1.32      martin     61: #define SHOW_ALL   8
1.28      christos   62:
1.25      christos   63: struct gpt_cmd c_show = {
                     64:        "show",
                     65:        cmd_show,
                     66:        showhelp, __arraycount(showhelp),
                     67:        GPT_READONLY,
                     68: };
1.1       christos   69:
1.25      christos   70: #define usage() gpt_usage(NULL, &c_show)
1.1       christos   71:
1.32      martin     72: static void
                     73: print_part_type(int map_type, int flags, void *map_data, off_t map_start)
1.1       christos   74: {
                     75:        off_t start;
1.32      martin     76:        map_t p;
1.1       christos   77:        struct mbr *mbr;
                     78:        struct gpt_ent *ent;
                     79:        unsigned int i;
1.28      christos   80:        char buf[128], *b = buf;
1.27      christos   81:        uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
1.1       christos   82:
1.32      martin     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:
                    147: static int
                    148: show(gpt_t gpt, int show)
                    149: {
                    150:        map_t m;
                    151:
1.24      christos  152:        printf("  %*s", gpt->lbawidth, "start");
                    153:        printf("  %*s", gpt->lbawidth, "size");
1.1       christos  154:        printf("  index  contents\n");
                    155:
1.24      christos  156:        m = map_first(gpt);
1.1       christos  157:        while (m != NULL) {
1.24      christos  158:                printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
                    159:                printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
1.1       christos  160:                putchar(' ');
                    161:                putchar(' ');
                    162:                if (m->map_index > 0)
                    163:                        printf("%5d", m->map_index);
                    164:                else
                    165:                        printf("     ");
                    166:                putchar(' ');
                    167:                putchar(' ');
1.32      martin    168:                print_part_type(m->map_type, show, m->map_data, m->map_start);
1.1       christos  169:                putchar('\n');
                    170:                m = m->map_next;
                    171:        }
1.24      christos  172:        return 0;
1.1       christos  173: }
                    174:
1.24      christos  175: static int
1.28      christos  176: show_one(gpt_t gpt, unsigned int entry)
1.12      jnemeth   177: {
1.24      christos  178:        map_t m;
1.12      jnemeth   179:        struct gpt_ent *ent;
1.19      christos  180:        char s1[128], s2[128];
1.27      christos  181:        uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
1.12      jnemeth   182:
1.24      christos  183:        for (m = map_first(gpt); m != NULL; m = m->map_next)
1.12      jnemeth   184:                if (entry == m->map_index)
                    185:                        break;
                    186:        if (m == NULL) {
1.24      christos  187:                gpt_warnx(gpt, "Could not find index %d", entry);
                    188:                return -1;
1.12      jnemeth   189:        }
                    190:        ent = m->map_data;
                    191:
                    192:        printf("Details for index %d:\n", entry);
1.29      christos  193:        gpt_show_num("Start", (uintmax_t)(m->map_start * gpt->secsz));
                    194:        gpt_show_num("Size", (uintmax_t)(m->map_size * gpt->secsz));
1.12      jnemeth   195:
1.19      christos  196:        gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
1.20      jnemeth   197:        gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
1.12      jnemeth   198:        if (strcmp(s1, s2) == 0)
1.19      christos  199:                strlcpy(s1, "unknown", sizeof(s1));
1.12      jnemeth   200:        printf("Type: %s (%s)\n", s1, s2);
                    201:
1.19      christos  202:        gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
1.12      jnemeth   203:        printf("GUID: %s\n", s2);
                    204:
1.27      christos  205:        utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
                    206:        printf("Label: %s\n", (char *)utfbuf);
1.12      jnemeth   207:
1.31      christos  208:        printf("Attributes: ");
1.28      christos  209:        if (ent->ent_attr == 0) {
1.31      christos  210:                printf("None\n");
                    211:        } else  {
                    212:                char buf[1024];
                    213:                printf("%s\n", gpt_attr_list(buf, sizeof(buf), ent->ent_attr));
1.12      jnemeth   214:        }
1.31      christos  215:
1.24      christos  216:        return 0;
1.12      jnemeth   217: }
                    218:
1.25      christos  219: static int
1.32      martin    220: show_all(gpt_t gpt)
                    221: {
                    222:        map_t m;
                    223:        struct gpt_ent *ent;
                    224:        char s1[128], s2[128];
1.34      martin    225: #ifdef HN_AUTOSCALE
                    226:        char human_num[8];
                    227: #endif
1.32      martin    228:        uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
                    229: #define PFX "                                 "
                    230:
                    231:        printf("  %*s", gpt->lbawidth, "start");
                    232:        printf("  %*s", gpt->lbawidth, "size");
                    233:        printf("  index  contents\n");
                    234:
                    235:        m = map_first(gpt);
                    236:        while (m != NULL) {
                    237:                printf("  %*llu", gpt->lbawidth, (long long)m->map_start);
                    238:                printf("  %*llu", gpt->lbawidth, (long long)m->map_size);
                    239:                putchar(' ');
                    240:                putchar(' ');
                    241:                if (m->map_index > 0) {
                    242:                        printf("%5d  ", m->map_index);
                    243:                        print_part_type(m->map_type, 0, m->map_data,
                    244:                            m->map_start);
                    245:                        putchar('\n');
                    246:
                    247:                        ent = m->map_data;
                    248:
                    249:                        gpt_uuid_snprintf(s1, sizeof(s1), "%s", ent->ent_type);
                    250:                        gpt_uuid_snprintf(s2, sizeof(s2), "%d", ent->ent_type);
                    251:                        if (strcmp(s1, s2) == 0)
                    252:                                strlcpy(s1, "unknown", sizeof(s1));
1.34      martin    253:                        printf(PFX "Type: %s\n", s1);
                    254:                        printf(PFX "TypeID: %s\n", s2);
1.32      martin    255:
                    256:                        gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
                    257:                        printf(PFX "GUID: %s\n", s2);
                    258:
1.34      martin    259:                        printf(PFX "Size: ");
                    260: #ifdef HN_AUTOSCALE
                    261:                        if (humanize_number(human_num, sizeof(human_num),
                    262:                            (int64_t)(m->map_size * gpt->secsz),
                    263:                            "", HN_AUTOSCALE, HN_B) < 0) {
                    264: #endif
                    265:                                printf("%ju",
                    266:                                    (int64_t)(m->map_size * gpt->secsz));
                    267: #ifdef HN_AUTOSCALE
                    268:                        } else {
                    269:                                printf("%s", human_num);
                    270:                        }
                    271: #endif
                    272:                        putchar('\n');
                    273:
1.32      martin    274:                        utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
                    275:                        printf(PFX "Label: %s\n", (char *)utfbuf);
                    276:
                    277:                        printf(PFX "Attributes: ");
                    278:                        if (ent->ent_attr == 0) {
                    279:                                printf("None\n");
                    280:                        } else  {
                    281:                                char buf[1024];
                    282:
                    283:                                printf("%s\n", gpt_attr_list(buf, sizeof(buf),
                    284:                                    ent->ent_attr));
                    285:                        }
                    286:                } else {
                    287:                        printf("       ");
                    288:                        print_part_type(m->map_type, 0, m->map_data,
                    289:                            m->map_start);
                    290:                        putchar('\n');
                    291:                }
                    292:                m = m->map_next;
                    293:        }
                    294:        return 0;
                    295: }
                    296:
                    297: static int
1.24      christos  298: cmd_show(gpt_t gpt, int argc, char *argv[])
1.1       christos  299: {
1.24      christos  300:        int ch;
1.28      christos  301:        int xshow = 0;
                    302:        unsigned int entry = 0;
1.1       christos  303:
1.32      martin    304:        while ((ch = getopt(argc, argv, "gi:lua")) != -1) {
1.1       christos  305:                switch(ch) {
1.32      martin    306:                case 'a':
                    307:                        xshow |= SHOW_ALL;
                    308:                        break;
1.12      jnemeth   309:                case 'g':
1.28      christos  310:                        xshow |= SHOW_GUID;
1.12      jnemeth   311:                        break;
                    312:                case 'i':
1.35    ! christos  313:                        if (gpt_uint_get(gpt, &entry) == -1)
1.25      christos  314:                                return usage();
1.12      jnemeth   315:                        break;
1.1       christos  316:                case 'l':
1.28      christos  317:                        xshow |= SHOW_LABEL;
1.1       christos  318:                        break;
                    319:                case 'u':
1.28      christos  320:                        xshow |= SHOW_UUID;
1.1       christos  321:                        break;
                    322:                default:
1.25      christos  323:                        return usage();
1.1       christos  324:                }
                    325:        }
                    326:
1.24      christos  327:        if (argc != optind)
1.25      christos  328:                return usage();
1.1       christos  329:
1.32      martin    330:        if (xshow & SHOW_ALL)
                    331:                return show_all(gpt);
                    332:
1.28      christos  333:        return entry > 0 ? show_one(gpt, entry) : show(gpt, xshow);
1.1       christos  334: }

CVSweb <webmaster@jp.NetBSD.org>