Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010 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 | * Utility for ChromeOS-specific GPT partitions, Please see corresponding .c |
| 6 | * files for more details. |
| 7 | */ |
| 8 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | #include <unistd.h> |
Jay Srinivasan | 5fac757 | 2012-02-23 10:59:01 -0800 | [diff] [blame] | 12 | #include <uuid/uuid.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 13 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 14 | #include "cgpt.h" |
| 15 | #include "vboot_host.h" |
| 16 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 17 | const char* progname; |
| 18 | const char* command; |
Jay Srinivasan | 5fac757 | 2012-02-23 10:59:01 -0800 | [diff] [blame] | 19 | void (*uuid_generator)(uint8_t* buffer); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 20 | |
| 21 | struct { |
| 22 | const char *name; |
| 23 | int (*fp)(int argc, char *argv[]); |
| 24 | const char *comment; |
| 25 | } cmds[] = { |
| 26 | {"create", cmd_create, "Create or reset GPT headers and tables"}, |
| 27 | {"add", cmd_add, "Add, edit or remove a partition entry"}, |
| 28 | {"show", cmd_show, "Show partition table and entries"}, |
| 29 | {"repair", cmd_repair, "Repair damaged GPT headers and tables"}, |
| 30 | {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"}, |
Bill Richardson | 4a20931 | 2010-07-02 11:34:38 -0700 | [diff] [blame] | 31 | {"find", cmd_find, "Locate a partition by its GUID"}, |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 32 | {"prioritize", cmd_prioritize, |
| 33 | "Reorder the priority of all kernel partitions"}, |
Stefan Reinauer | b7b865c | 2012-08-23 15:06:25 -0700 | [diff] [blame] | 34 | {"legacy", cmd_legacy, "Switch between GPT and Legacy GPT"}, |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 37 | void Usage(void) { |
| 38 | int i; |
| 39 | |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 40 | printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 41 | "Supported COMMANDs:\n\n", |
| 42 | progname); |
| 43 | |
| 44 | for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) { |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 45 | printf(" %-15s %s\n", cmds[i].name, cmds[i].comment); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 46 | } |
| 47 | printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | int main(int argc, char *argv[]) { |
| 53 | int i; |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 54 | int match_count = 0; |
| 55 | int match_index = 0; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 56 | |
Jay Srinivasan | 5fac757 | 2012-02-23 10:59:01 -0800 | [diff] [blame] | 57 | uuid_generator = uuid_generate; |
| 58 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 59 | progname = strrchr(argv[0], '/'); |
| 60 | if (progname) |
| 61 | progname++; |
| 62 | else |
| 63 | progname = argv[0]; |
| 64 | |
| 65 | if (argc < 2) { |
| 66 | Usage(); |
| 67 | return CGPT_FAILED; |
| 68 | } |
| 69 | |
| 70 | // increment optind now, so that getopt skips argv[0] in command function |
| 71 | command = argv[optind++]; |
| 72 | |
| 73 | // Find the command to invoke. |
| 74 | for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) { |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 75 | // exact match? |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 76 | if (0 == strcmp(cmds[i].name, command)) { |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 77 | match_index = i; |
| 78 | match_count = 1; |
| 79 | break; |
| 80 | } |
| 81 | // unique match? |
| 82 | else if (0 == strncmp(cmds[i].name, command, strlen(command))) { |
| 83 | match_index = i; |
| 84 | match_count++; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Bill Richardson | 3430b32 | 2010-11-29 14:24:51 -0800 | [diff] [blame] | 88 | if (match_count == 1) |
| 89 | return cmds[match_index].fp(argc, argv); |
| 90 | |
| 91 | // Couldn't find a single matching command. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 92 | Usage(); |
| 93 | |
| 94 | return CGPT_FAILED; |
| 95 | } |