Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cgpt.h" |
| 6 | |
| 7 | #define __STDC_FORMAT_MACROS |
| 8 | #include <getopt.h> |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 9 | #include <inttypes.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 10 | #include <string.h> |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 11 | #include "cgpt_params.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 12 | |
| 13 | static void Usage(void) |
| 14 | { |
| 15 | printf("\nUsage: %s show [OPTIONS] DRIVE\n\n" |
| 16 | "Display the GPT table\n\n" |
| 17 | "Options:\n" |
| 18 | " -n Numeric output only\n" |
| 19 | " -v Verbose output\n" |
| 20 | " -q Quick output\n" |
| 21 | " -i NUM Show specified partition only - pick one of:\n" |
| 22 | " -b beginning sector\n" |
| 23 | " -s partition size\n" |
| 24 | " -t type guid\n" |
| 25 | " -u unique guid\n" |
| 26 | " -l label\n" |
| 27 | " -S Successful flag\n" |
| 28 | " -T Tries flag\n" |
| 29 | " -P Priority flag\n" |
| 30 | " -A raw 64-bit attribute value\n" |
| 31 | "\n", progname); |
| 32 | } |
| 33 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 34 | int cmd_show(int argc, char *argv[]) { |
| 35 | struct drive drive; |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 36 | |
| 37 | CgptShowParams params; |
| 38 | memset(¶ms, 0, sizeof(params)); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 39 | |
| 40 | int c; |
| 41 | int errorcnt = 0; |
| 42 | char *e = 0; |
| 43 | |
| 44 | opterr = 0; // quiet, you |
| 45 | while ((c=getopt(argc, argv, ":hnvqi:bstulSTPA")) != -1) |
| 46 | { |
| 47 | switch (c) |
| 48 | { |
| 49 | case 'n': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 50 | params.numeric = 1; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 51 | break; |
| 52 | case 'v': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 53 | params.verbose = 1; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 54 | break; |
| 55 | case 'q': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 56 | params.quick = 1; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 57 | break; |
| 58 | case 'i': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 59 | params.partition = (uint32_t)strtoul(optarg, &e, 0); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 60 | if (!*optarg || (e && *e)) |
| 61 | { |
| 62 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 63 | errorcnt++; |
| 64 | } |
| 65 | break; |
| 66 | case 'b': |
| 67 | case 's': |
| 68 | case 't': |
| 69 | case 'u': |
| 70 | case 'l': |
| 71 | case 'S': |
| 72 | case 'T': |
| 73 | case 'P': |
| 74 | case 'A': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 75 | params.single_item = c; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 76 | break; |
| 77 | |
| 78 | case 'h': |
| 79 | Usage(); |
| 80 | return CGPT_OK; |
| 81 | case '?': |
| 82 | Error("unrecognized option: -%c\n", optopt); |
| 83 | errorcnt++; |
| 84 | break; |
| 85 | case ':': |
| 86 | Error("missing argument to -%c\n", optopt); |
| 87 | errorcnt++; |
| 88 | break; |
| 89 | default: |
| 90 | errorcnt++; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | if (errorcnt) |
| 95 | { |
| 96 | Usage(); |
| 97 | return CGPT_FAILED; |
| 98 | } |
| 99 | |
| 100 | if (optind >= argc) { |
| 101 | Error("missing drive argument\n"); |
| 102 | Usage(); |
| 103 | return CGPT_FAILED; |
| 104 | } |
| 105 | |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 106 | params.driveName = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 107 | |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 108 | return cgpt_show(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 109 | } |