• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
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 <getopt.h>
6 #include <string.h>
7 
8 #include "cgpt.h"
9 #include "vboot_host.h"
10 
11 extern const char* progname;
12 
Usage(void)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"
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"
21          "  -z           Zero the sectors of the GPT table and entries\n"
22          "  -p NUM       Size (in blocks) of the disk to pad between the\n"
23          "                 primary GPT header and its entries, default 0\n"
24          "\n", progname);
25 }
26 
cmd_create(int argc,char * argv[])27 int cmd_create(int argc, char *argv[]) {
28   CgptCreateParams params;
29   memset(&params, 0, sizeof(params));
30 
31   int c;
32   int errorcnt = 0;
33   char *e = 0;
34 
35   opterr = 0;                     // quiet, you
36   while ((c=getopt(argc, argv, ":hzp:D:")) != -1)
37   {
38     switch (c)
39     {
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;
48     case 'z':
49       params.zap = 1;
50       break;
51     case 'p':
52       params.padding = strtoull(optarg, &e, 0);
53       if (!*optarg || (e && *e))
54       {
55         Error("invalid argument to -%c: \"%s\"\n", c, optarg);
56         errorcnt++;
57       }
58       break;
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 
86   params.drive_name = argv[optind];
87 
88   return CgptCreate(&params);
89 }
90