xref: /aosp_15_r20/external/vboot_reference/cgpt/cmd_create.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2012 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <getopt.h>
7 #include <string.h>
8 
9 #include "cgpt.h"
10 #include "vboot_host.h"
11 
12 extern const char* progname;
13 
Usage(void)14 static void Usage(void)
15 {
16   printf("\nUsage: %s create [OPTIONS] DRIVE\n\n"
17          "Create or reset an empty GPT.\n\n"
18          "Options:\n"
19          "  -D NUM       Size (in bytes) of the disk where partitions reside;\n"
20          "                 default 0, meaning partitions and GPT structs are\n"
21          "                 both on DRIVE\n"
22          "  -z           Zero the blocks of the GPT table and entries\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"
25          "\n", progname);
26 }
27 
cmd_create(int argc,char * argv[])28 int cmd_create(int argc, char *argv[]) {
29   CgptCreateParams params;
30   memset(&params, 0, sizeof(params));
31 
32   int c;
33   int errorcnt = 0;
34   char *e = 0;
35 
36   opterr = 0;                     // quiet, you
37   while ((c=getopt(argc, argv, ":hzp:D:")) != -1)
38   {
39     switch (c)
40     {
41     case 'D':
42       params.drive_size = strtoull(optarg, &e, 0);
43       errorcnt += check_int_parse(c, e);
44       break;
45     case 'z':
46       params.zap = 1;
47       break;
48     case 'p':
49       params.padding = strtoull(optarg, &e, 0);
50       errorcnt += check_int_parse(c, e);
51       break;
52     case 'h':
53       Usage();
54       return CGPT_OK;
55     case '?':
56       Error("unrecognized option: -%c\n", optopt);
57       errorcnt++;
58       break;
59     case ':':
60       Error("missing argument to -%c\n", optopt);
61       errorcnt++;
62       break;
63     default:
64       errorcnt++;
65       break;
66     }
67   }
68   if (errorcnt)
69   {
70     Usage();
71     return CGPT_FAILED;
72   }
73 
74   if (optind >= argc) {
75     Usage();
76     return CGPT_FAILED;
77   }
78 
79   params.drive_name = argv[optind];
80 
81   return CgptCreate(&params);
82 }
83