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 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 5 | #include <getopt.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 6 | #include <string.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 7 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 8 | #include "cgpt.h" |
| 9 | #include "vboot_host.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 10 | |
Bill Richardson | 4cb5497 | 2014-06-20 14:33:00 -0700 | [diff] [blame] | 11 | extern const char* progname; |
| 12 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 13 | static void Usage(void) |
| 14 | { |
| 15 | printf("\nUsage: %s create [OPTIONS] DRIVE\n\n" |
| 16 | "Create or reset an empty GPT.\n\n" |
| 17 | "Options:\n" |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame^] | 18 | " -D NUM Size (in bytes) of the disk where partitions reside\n" |
| 19 | " default 0, meaning partitions and GPT structs are\n" |
| 20 | " both on DRIVE\n" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 21 | " -z Zero the sectors of the GPT table and entries\n" |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame^] | 22 | " -s NUM Size (in bytes) of the disk (MTD only)\n" |
| 23 | " -p NUM Size (in blocks) of the disk to pad between the\n" |
| 24 | " primary GPT header and its entries, default 0\n" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 25 | "\n", progname); |
| 26 | } |
| 27 | |
| 28 | int cmd_create(int argc, char *argv[]) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 29 | CgptCreateParams params; |
| 30 | memset(¶ms, 0, sizeof(params)); |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 31 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 32 | int c; |
| 33 | int errorcnt = 0; |
Albert Chaulk | 494646d | 2013-07-19 12:56:38 -0700 | [diff] [blame] | 34 | char *e = 0; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 35 | |
| 36 | opterr = 0; // quiet, you |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame^] | 37 | while ((c=getopt(argc, argv, ":hzs:p:D:")) != -1) |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 38 | { |
| 39 | switch (c) |
| 40 | { |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame^] | 41 | case 'D': |
| 42 | params.drive_size = strtoull(optarg, &e, 0); |
| 43 | if (!*optarg || (e && *e)) |
| 44 | { |
| 45 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 46 | errorcnt++; |
| 47 | } |
| 48 | break; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 49 | case 'z': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 50 | params.zap = 1; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 51 | break; |
Albert Chaulk | 494646d | 2013-07-19 12:56:38 -0700 | [diff] [blame] | 52 | case 's': |
| 53 | params.size = strtoull(optarg, &e, 0); |
| 54 | break; |
Nam T. Nguyen | a2d72f7 | 2014-08-22 15:01:38 -0700 | [diff] [blame] | 55 | case 'p': |
| 56 | params.padding = strtoull(optarg, &e, 0); |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame^] | 57 | if (!*optarg || (e && *e)) |
| 58 | { |
| 59 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 60 | errorcnt++; |
| 61 | } |
Nam T. Nguyen | a2d72f7 | 2014-08-22 15:01:38 -0700 | [diff] [blame] | 62 | break; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 63 | case 'h': |
| 64 | Usage(); |
| 65 | return CGPT_OK; |
| 66 | case '?': |
| 67 | Error("unrecognized option: -%c\n", optopt); |
| 68 | errorcnt++; |
| 69 | break; |
| 70 | case ':': |
| 71 | Error("missing argument to -%c\n", optopt); |
| 72 | errorcnt++; |
| 73 | break; |
| 74 | default: |
| 75 | errorcnt++; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | if (errorcnt) |
| 80 | { |
| 81 | Usage(); |
| 82 | return CGPT_FAILED; |
| 83 | } |
| 84 | |
| 85 | if (optind >= argc) { |
| 86 | Usage(); |
| 87 | return CGPT_FAILED; |
| 88 | } |
| 89 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 90 | params.drive_name = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 91 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 92 | return CgptCreate(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 93 | } |