Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 5 | #include <getopt.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 6 | #include <string.h> |
| 7 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 8 | #include "cgpt.h" |
| 9 | #include "vboot_host.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 10 | |
Bill Richardson | 4cb5497 | 2014-06-20 14:33:00 -0700 | [diff] [blame] | 11 | extern const char* progname; |
| 12 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 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" |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 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" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 21 | " -v Verbose\n" |
| 22 | "\n", progname); |
| 23 | } |
| 24 | |
| 25 | int cmd_repair(int argc, char *argv[]) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 26 | CgptRepairParams params; |
| 27 | memset(¶ms, 0, sizeof(params)); |
| 28 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 29 | int c; |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 30 | char* e = 0; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 31 | int errorcnt = 0; |
| 32 | |
| 33 | opterr = 0; // quiet, you |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 34 | while ((c=getopt(argc, argv, ":hvD:")) != -1) |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 35 | { |
| 36 | switch (c) |
| 37 | { |
Nam T. Nguyen | ab89959 | 2014-11-13 19:30:46 -0800 | [diff] [blame] | 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; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 46 | case 'v': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 47 | params.verbose++; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 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 | |
Jay Srinivasan | 250549d | 2012-02-16 17:40:45 -0800 | [diff] [blame] | 72 | params.drive_name = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 73 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 74 | return CgptRepair(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 75 | } |