1// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#define __STDC_FORMAT_MACROS
6#include <getopt.h>
7#include <inttypes.h>
8#include <string.h>
9
10#include "cgpt.h"
11#include "vboot_host.h"
12
13extern const char* progname;
14
15static void Usage(void)
16{
17  printf("\nUsage: %s show [OPTIONS] DRIVE\n\n"
18         "Display the GPT table\n\n"
19         "Options:\n"
20         "  -D NUM       Size (in bytes) of the disk where partitions reside\n"
21         "                 default 0, meaning partitions and GPT structs are\n"
22         "                 both on DRIVE\n"
23         "  -n           Numeric output only\n"
24         "  -v           Verbose output\n"
25         "  -q           Quick output\n"
26         "  -i NUM       Show specified partition only - pick one of:\n"
27         "               -b  beginning sector\n"
28         "               -s  partition size\n"
29         "               -t  type guid\n"
30         "               -u  unique guid\n"
31         "               -l  label\n"
32         "               -S  Successful flag\n"
33         "               -T  Tries flag\n"
34         "               -P  Priority flag\n"
35         "               -A  raw 64-bit attribute value\n"
36         "  -d           Debug output (including invalid headers)\n"
37         "\n", progname);
38}
39
40int cmd_show(int argc, char *argv[]) {
41  CgptShowParams params;
42  memset(&params, 0, sizeof(params));
43
44  int c;
45  int errorcnt = 0;
46  char *e = 0;
47
48  opterr = 0;                     // quiet, you
49  while ((c=getopt(argc, argv, ":hnvqi:bstulSTPAdD:")) != -1)
50  {
51    switch (c)
52    {
53    case 'D':
54      params.drive_size = strtoull(optarg, &e, 0);
55      if (!*optarg || (e && *e))
56      {
57        Error("invalid argument to -%c: \"%s\"\n", c, optarg);
58        errorcnt++;
59      }
60      break;
61    case 'n':
62      params.numeric = 1;
63      break;
64    case 'v':
65      params.verbose = 1;
66      break;
67    case 'q':
68      params.quick = 1;
69      break;
70    case 'i':
71      params.partition = (uint32_t)strtoul(optarg, &e, 0);
72      if (!*optarg || (e && *e))
73      {
74        Error("invalid argument to -%c: \"%s\"\n", c, optarg);
75        errorcnt++;
76      }
77      break;
78    case 'b':
79    case 's':
80    case 't':
81    case 'u':
82    case 'l':
83    case 'S':
84    case 'T':
85    case 'P':
86    case 'A':
87      params.single_item = c;
88      break;
89
90    case 'd':
91      params.debug = 1;
92      break;
93
94    case 'h':
95      Usage();
96      return CGPT_OK;
97    case '?':
98      Error("unrecognized option: -%c\n", optopt);
99      errorcnt++;
100      break;
101    case ':':
102      Error("missing argument to -%c\n", optopt);
103      errorcnt++;
104      break;
105    default:
106      errorcnt++;
107      break;
108    }
109  }
110  if (errorcnt)
111  {
112    Usage();
113    return CGPT_FAILED;
114  }
115
116  if (optind >= argc) {
117    Error("missing drive argument\n");
118    Usage();
119    return CGPT_FAILED;
120  }
121
122  params.drive_name = argv[optind];
123
124  return CgptShow(&params);
125}
126