xref: /aosp_15_r20/external/vboot_reference/cgpt/cmd_edit.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2018 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 
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 edit [OPTIONS] DRIVE\n\n"
16          "Edit a drive's parameters.\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          "  -u GUID      Drive Unique ID\n"
22          "\n", progname);
23 }
24 
cmd_edit(int argc,char * argv[])25 int cmd_edit(int argc, char *argv[]) {
26 
27   CgptEditParams params;
28   memset(&params, 0, sizeof(params));
29 
30   int c;
31   int errorcnt = 0;
32   char *e = 0;
33 
34   opterr = 0;                     // quiet, you
35   while ((c=getopt(argc, argv, ":hu:D:")) != -1)
36   {
37     switch (c)
38     {
39     case 'D':
40       params.drive_size = strtoull(optarg, &e, 0);
41       errorcnt += check_int_parse(c, e);
42       break;
43     case 'u':
44       params.set_unique = 1;
45       if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
46         Error("invalid argument to -%c: %s\n", c, optarg);
47         errorcnt++;
48       }
49       break;
50     case 'h':
51       Usage();
52       return CGPT_OK;
53     case '?':
54       Error("unrecognized option: -%c\n", optopt);
55       errorcnt++;
56       break;
57     case ':':
58       Error("missing argument to -%c\n", optopt);
59       errorcnt++;
60       break;
61     default:
62       errorcnt++;
63       break;
64     }
65   }
66   if (errorcnt)
67   {
68     Usage();
69     return CGPT_FAILED;
70   }
71 
72   if (optind >= argc)
73   {
74     Error("missing drive argument\n");
75     return CGPT_FAILED;
76   }
77 
78   params.drive_name = argv[optind];
79 
80   if (!params.set_unique)
81   {
82     Error("no parameters were edited\n");
83     return CGPT_FAILED;
84   }
85 
86   return CgptEdit(&params);
87 }
88