xref: /aosp_15_r20/external/vboot_reference/cgpt/cmd_repair.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 repair [OPTIONS] DRIVE\n\n"
17          "Repair damaged GPT headers and tables.\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          "  -v           Verbose\n"
23          "\n", progname);
24 }
25 
cmd_repair(int argc,char * argv[])26 int cmd_repair(int argc, char *argv[]) {
27   CgptRepairParams params;
28   memset(&params, 0, sizeof(params));
29 
30   int c;
31   char* e = 0;
32   int errorcnt = 0;
33 
34   opterr = 0;                     // quiet, you
35   while ((c=getopt(argc, argv, ":hvD:")) != -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 'v':
44       params.verbose++;
45       break;
46 
47     case 'h':
48       Usage();
49       return CGPT_OK;
50     case '?':
51       Error("unrecognized option: -%c\n", optopt);
52       errorcnt++;
53       break;
54     case ':':
55       Error("missing argument to -%c\n", optopt);
56       errorcnt++;
57       break;
58     default:
59       errorcnt++;
60       break;
61     }
62   }
63   if (errorcnt)
64   {
65     Usage();
66     return CGPT_FAILED;
67   }
68 
69   params.drive_name = argv[optind];
70 
71   return CgptRepair(&params);
72 }
73