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 | " -p NUM Size (in blocks) of the disk to pad between the\n" |
| 23 | " primary GPT header and its entries, default 0\n" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 24 | "\n", progname); |
| 25 | } |
| 26 | |
| 27 | int cmd_create(int argc, char *argv[]) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 28 | CgptCreateParams params; |
| 29 | memset(¶ms, 0, sizeof(params)); |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 30 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 31 | int c; |
| 32 | int errorcnt = 0; |
Albert Chaulk | 494646d | 2013-07-19 12:56:38 -0700 | [diff] [blame] | 33 | char *e = 0; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 34 | |
| 35 | opterr = 0; // quiet, you |
Nam T. Nguyen | 8577b53 | 2014-11-25 13:26:53 -0800 | [diff] [blame^] | 36 | while ((c=getopt(argc, argv, ":hzp:D:")) != -1) |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 37 | { |
| 38 | switch (c) |
| 39 | { |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 40 | case 'D': |
| 41 | params.drive_size = strtoull(optarg, &e, 0); |
| 42 | if (!*optarg || (e && *e)) |
| 43 | { |
| 44 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 45 | errorcnt++; |
| 46 | } |
| 47 | break; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 48 | case 'z': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 49 | params.zap = 1; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 50 | break; |
Nam T. Nguyen | a2d72f7 | 2014-08-22 15:01:38 -0700 | [diff] [blame] | 51 | case 'p': |
| 52 | params.padding = strtoull(optarg, &e, 0); |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 53 | if (!*optarg || (e && *e)) |
| 54 | { |
| 55 | Error("invalid argument to -%c: \"%s\"\n", c, optarg); |
| 56 | errorcnt++; |
| 57 | } |
Nam T. Nguyen | a2d72f7 | 2014-08-22 15:01:38 -0700 | [diff] [blame] | 58 | break; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 59 | case 'h': |
| 60 | Usage(); |
| 61 | return CGPT_OK; |
| 62 | case '?': |
| 63 | Error("unrecognized option: -%c\n", optopt); |
| 64 | errorcnt++; |
| 65 | break; |
| 66 | case ':': |
| 67 | Error("missing argument to -%c\n", optopt); |
| 68 | errorcnt++; |
| 69 | break; |
| 70 | default: |
| 71 | errorcnt++; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | if (errorcnt) |
| 76 | { |
| 77 | Usage(); |
| 78 | return CGPT_FAILED; |
| 79 | } |
| 80 | |
| 81 | if (optind >= argc) { |
| 82 | Usage(); |
| 83 | return CGPT_FAILED; |
| 84 | } |
| 85 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 86 | params.drive_name = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 87 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 88 | return CgptCreate(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 89 | } |