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