diff options
Diffstat (limited to 'cgpt')
-rw-r--r-- | cgpt/cgpt.c | 1 | ||||
-rw-r--r-- | cgpt/cgpt.h | 1 | ||||
-rw-r--r-- | cgpt/cgpt_edit.c | 47 | ||||
-rw-r--r-- | cgpt/cmd_edit.c | 78 |
4 files changed, 127 insertions, 0 deletions
diff --git a/cgpt/cgpt.c b/cgpt/cgpt.c index 38092006..0977b654 100644 --- a/cgpt/cgpt.c +++ b/cgpt/cgpt.c @@ -34,6 +34,7 @@ struct { {"repair", cmd_repair, "Repair damaged GPT headers and tables"}, {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"}, {"find", cmd_find, "Locate a partition by its GUID"}, + {"edit", cmd_edit, "Edit a drive entry"}, {"prioritize", cmd_prioritize, "Reorder the priority of all kernel partitions"}, {"legacy", cmd_legacy, "Switch between GPT and Legacy GPT"}, diff --git a/cgpt/cgpt.h b/cgpt/cgpt.h index 86f4b851..c53f0f32 100644 --- a/cgpt/cgpt.h +++ b/cgpt/cgpt.h @@ -201,6 +201,7 @@ int cmd_create(int argc, char *argv[]); int cmd_add(int argc, char *argv[]); int cmd_boot(int argc, char *argv[]); int cmd_find(int argc, char *argv[]); +int cmd_edit(int argc, char *argv[]); int cmd_prioritize(int argc, char *argv[]); int cmd_legacy(int argc, char *argv[]); diff --git a/cgpt/cgpt_edit.c b/cgpt/cgpt_edit.c new file mode 100644 index 00000000..098187f6 --- /dev/null +++ b/cgpt/cgpt_edit.c @@ -0,0 +1,47 @@ +// Copyright 2018 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 "cgpt_params.h" +#include "cgptlib_internal.h" +#include "vboot_host.h" + +int CgptEdit(CgptEditParams *params) { + struct drive drive; + GptHeader *h; + int gpt_retval; + + if (params == NULL) + return CGPT_FAILED; + + if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR, 0)) + return CGPT_FAILED; + + if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive.gpt))) { + Error("GptSanityCheck() returned %d: %s\n", + gpt_retval, GptError(gpt_retval)); + goto bad; + } + + if (CGPT_OK != CheckValid(&drive)) { + Error("Please run 'cgpt repair' before changing settings.\n"); + goto bad; + } + + h = (GptHeader *)drive.gpt.primary_header; + memcpy(&h->disk_uuid, ¶ms->unique_guid, sizeof(h->disk_uuid)); + // Copy to secondary + RepairHeader(&drive.gpt, MASK_PRIMARY); + drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_HEADER2); + + UpdateCrc(&drive.gpt); + + // Write it all out. + return DriveClose(&drive, 1); + +bad: + + DriveClose(&drive, 0); + return CGPT_FAILED; +} diff --git a/cgpt/cmd_edit.c b/cgpt/cmd_edit.c new file mode 100644 index 00000000..4f4290b9 --- /dev/null +++ b/cgpt/cmd_edit.c @@ -0,0 +1,78 @@ +// Copyright 2018 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 <getopt.h> + +#include "cgpt.h" +#include "vboot_host.h" + +extern const char* progname; + +static void Usage(void) +{ + printf("\nUsage: %s edit [OPTIONS] DRIVE\n\n" + "Edit a drive's parameters.\n\n" + "Options:\n" + " -u GUID Drive Unique ID\n" + "\n", progname); +} + +int cmd_edit(int argc, char *argv[]) { + + CgptEditParams params; + memset(¶ms, 0, sizeof(params)); + + int c; + int errorcnt = 0; + + opterr = 0; // quiet, you + while ((c=getopt(argc, argv, ":hu:")) != -1) + { + switch (c) + { + case 'u': + params.set_unique = 1; + if (CGPT_OK != StrToGuid(optarg, ¶ms.unique_guid)) { + Error("invalid argument to -%c: %s\n", c, optarg); + errorcnt++; + } + 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; + } + + params.drive_name = argv[optind]; + + if (!params.set_unique) + { + Error("no parameters were edited\n"); + return CGPT_FAILED; + } + + return CgptEdit(¶ms); +} |