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 repair [OPTIONS] DRIVE\n\n"
16          "Repair damaged GPT headers and tables.\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          "  -v           Verbose\n"
22          "\n", progname);
23 }
24 
cmd_repair(int argc,char * argv[])25 int cmd_repair(int argc, char *argv[]) {
26   CgptRepairParams params;
27   memset(&params, 0, sizeof(params));
28 
29   int c;
30   char* e = 0;
31   int errorcnt = 0;
32 
33   opterr = 0;                     // quiet, you
34   while ((c=getopt(argc, argv, ":hvD:")) != -1)
35   {
36     switch (c)
37     {
38     case 'D':
39       params.drive_size = strtoull(optarg, &e, 0);
40       if (!*optarg || (e && *e))
41       {
42         Error("invalid argument to -%c: \"%s\"\n", c, optarg);
43         errorcnt++;
44       }
45       break;
46     case 'v':
47       params.verbose++;
48       break;
49 
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   params.drive_name = argv[optind];
73 
74   return CgptRepair(&params);
75 }
76