summaryrefslogtreecommitdiff
path: root/cgpt/cmd_repair.c
diff options
context:
space:
mode:
authorBill Richardson <wfrichar@chromium.org>2010-06-11 09:15:55 -0700
committerBill Richardson <wfrichar@chromium.org>2010-06-11 09:15:55 -0700
commitf1372d9109d638fbb1a177a89ebaf64e7ee0637e (patch)
tree243cdacbc1028e6a987d582d33927560af0b47e0 /cgpt/cmd_repair.c
parent6a97b3e2a1bee35bf3c00f2fb0faafde4aaab9e2 (diff)
downloadvboot-f1372d9109d638fbb1a177a89ebaf64e7ee0637e.tar.gz
Nearly complete rewrite of cgpt tool.
This fixes a number of bugs, adds a bunch of commands, and essentially makes cgpt ready to use as a replacement for gpt. Still to do is to add commands and options that will let it generated intentionally bad partitions, for use in testing. Review URL: http://codereview.chromium.org/2719008
Diffstat (limited to 'cgpt/cmd_repair.c')
-rw-r--r--cgpt/cmd_repair.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/cgpt/cmd_repair.c b/cgpt/cmd_repair.c
new file mode 100644
index 00000000..aafdc938
--- /dev/null
+++ b/cgpt/cmd_repair.c
@@ -0,0 +1,85 @@
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "cgpt.h"
+
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "cgptlib_internal.h"
+
+static void Usage(void)
+{
+ printf("\nUsage: %s repair [OPTIONS] DRIVE\n\n"
+ "Repair damaged GPT headers and tables.\n\n"
+ "Options:\n"
+ " -v Verbose\n"
+ "\n", progname);
+}
+
+int cmd_repair(int argc, char *argv[]) {
+ struct drive drive;
+ int verbose = 0;
+
+ int c;
+ int errorcnt = 0;
+
+ opterr = 0; // quiet, you
+ while ((c=getopt(argc, argv, ":hv")) != -1)
+ {
+ switch (c)
+ {
+ case 'v':
+ verbose++;
+ break;
+
+ case 'h':
+ Usage();
+ return CGPT_OK;
+ case '?':
+ Error("unrecognized option: -%c\n", optopt);
+ errorcnt++;
+ break;
+ case ':':
+ Error("missing argument to -%c\n", optopt);
+ errorcnt++;
+ break;
+ default:
+ errorcnt++;
+ break;
+ }
+ }
+ if (errorcnt)
+ {
+ Usage();
+ return CGPT_FAILED;
+ }
+
+ if (optind >= argc) {
+ Error("missing drive argument\n");
+ return CGPT_FAILED;
+ }
+
+ if (CGPT_OK != DriveOpen(argv[optind], &drive))
+ return CGPT_FAILED;
+
+ int gpt_retval = GptSanityCheck(&drive.gpt);
+ if (verbose)
+ printf("GptSanityCheck() returned %d: %s\n",
+ gpt_retval, GptError(gpt_retval));
+
+ GptRepair(&drive.gpt);
+ if (drive.gpt.modified & GPT_MODIFIED_HEADER1)
+ printf("Primary Header is updated.\n");
+ if (drive.gpt.modified & GPT_MODIFIED_ENTRIES1)
+ printf("Primary Entries is updated.\n");
+ if (drive.gpt.modified & GPT_MODIFIED_ENTRIES2)
+ printf("Secondary Entries is updated.\n");
+ if (drive.gpt.modified & GPT_MODIFIED_HEADER2)
+ printf("Secondary Header is updated.\n");
+
+ return DriveClose(&drive, 1);
+}