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