cgpt: add -D support to CgptEdit

The lack of -D support in CgptEdit introduced a test failure.  This
change adds support for -D.

BRANCH=none
BUG=chromium:605348
TEST=Verified that prior to this change the tests failed:
  cros_workon --host start vboot_reference
  sudo FEATURES=test emerge vboot_reference
The tests fail in a different area prior to CgptEdit, so I applied the
following temporary change to Makefile to see the relevant failure:
  ifeq (${MINIMAL},)
  # Bitmap utility isn't compiled for minimal variant
- test_targets:: runbmptests runfutiltests
+ test_targets:: runbmptests # runfutiltests
  # Scripts don't work under qemu testing
With this change the tests pass.

Change-Id: Ia2127a3537c72e4ea6daf59c5c33b8701a89b0f6
Signed-off-by: Matt Delco <delco@google.com>
Reviewed-on: https://chromium-review.googlesource.com/1436496
Tested-by: Matt Delco <delco@chromium.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
diff --git a/cgpt/cgpt_edit.c b/cgpt/cgpt_edit.c
index 622819b..c7781ff 100644
--- a/cgpt/cgpt_edit.c
+++ b/cgpt/cgpt_edit.c
@@ -15,7 +15,8 @@
   if (params == NULL)
     return CGPT_FAILED;
 
-  if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR, 0))
+  if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
+                           params->drive_size))
     return CGPT_FAILED;
 
   if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) {
diff --git a/cgpt/cmd_edit.c b/cgpt/cmd_edit.c
index 4f4290b..fb4d919 100644
--- a/cgpt/cmd_edit.c
+++ b/cgpt/cmd_edit.c
@@ -14,6 +14,9 @@
   printf("\nUsage: %s edit [OPTIONS] DRIVE\n\n"
          "Edit a drive's parameters.\n\n"
          "Options:\n"
+         "  -D NUM       Size (in bytes) of the disk where partitions reside\n"
+         "                 default 0, meaning partitions and GPT structs are\n"
+         "                 both on DRIVE\n"
          "  -u GUID      Drive Unique ID\n"
          "\n", progname);
 }
@@ -25,12 +28,17 @@
 
   int c;
   int errorcnt = 0;
+  char *e = 0;
 
   opterr = 0;                     // quiet, you
-  while ((c=getopt(argc, argv, ":hu:")) != -1)
+  while ((c=getopt(argc, argv, ":hu:D:")) != -1)
   {
     switch (c)
     {
+    case 'D':
+      params.drive_size = strtoull(optarg, &e, 0);
+      errorcnt += check_int_parse(c, e);
+      break;
     case 'u':
       params.set_unique = 1;
       if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
diff --git a/host/include/cgpt_params.h b/host/include/cgpt_params.h
index ffd071a..8e8bfa6 100644
--- a/host/include/cgpt_params.h
+++ b/host/include/cgpt_params.h
@@ -55,6 +55,7 @@
 
 typedef struct CgptEditParams {
 	char *drive_name;
+	uint64_t drive_size;
 	Guid unique_guid;
 	int set_unique;
 } CgptEditParams;