blob: a3b20d99b01620638de84c240046fdb70419d7cd [file] [log] [blame]
Bill Richardsonf1372d92010-06-11 09:15:55 -07001/* 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 Richardsonf1372d92010-06-11 09:15:55 -07009#include <stdio.h>
10#include <string.h>
11#include <unistd.h>
Jay Srinivasan5fac7572012-02-23 10:59:01 -080012#include <uuid/uuid.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -070013
Bill Richardson0c3ba242013-03-29 11:09:30 -070014#include "cgpt.h"
15#include "vboot_host.h"
16
Bill Richardsonf1372d92010-06-11 09:15:55 -070017const char* progname;
18const char* command;
Jay Srinivasan5fac7572012-02-23 10:59:01 -080019void (*uuid_generator)(uint8_t* buffer);
Bill Richardsonf1372d92010-06-11 09:15:55 -070020
21struct {
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 Richardson4a209312010-07-02 11:34:38 -070031 {"find", cmd_find, "Locate a partition by its GUID"},
Bill Richardson3430b322010-11-29 14:24:51 -080032 {"prioritize", cmd_prioritize,
33 "Reorder the priority of all kernel partitions"},
Stefan Reinauerb7b865c2012-08-23 15:06:25 -070034 {"legacy", cmd_legacy, "Switch between GPT and Legacy GPT"},
Bill Richardsonf1372d92010-06-11 09:15:55 -070035};
36
Bill Richardsonf1372d92010-06-11 09:15:55 -070037void Usage(void) {
38 int i;
39
Bill Richardson3430b322010-11-29 14:24:51 -080040 printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070041 "Supported COMMANDs:\n\n",
42 progname);
43
44 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080045 printf(" %-15s %s\n", cmds[i].name, cmds[i].comment);
Bill Richardsonf1372d92010-06-11 09:15:55 -070046 }
47 printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname);
48}
49
50
51
52int main(int argc, char *argv[]) {
53 int i;
Bill Richardson3430b322010-11-29 14:24:51 -080054 int match_count = 0;
55 int match_index = 0;
Bill Richardsonf1372d92010-06-11 09:15:55 -070056
Jay Srinivasan5fac7572012-02-23 10:59:01 -080057 uuid_generator = uuid_generate;
58
Bill Richardsonf1372d92010-06-11 09:15:55 -070059 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 Richardson3430b322010-11-29 14:24:51 -080075 // exact match?
Bill Richardsonf1372d92010-06-11 09:15:55 -070076 if (0 == strcmp(cmds[i].name, command)) {
Bill Richardson3430b322010-11-29 14:24:51 -080077 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 Richardsonf1372d92010-06-11 09:15:55 -070085 }
86 }
87
Bill Richardson3430b322010-11-29 14:24:51 -080088 if (match_count == 1)
89 return cmds[match_index].fp(argc, argv);
90
91 // Couldn't find a single matching command.
Bill Richardsonf1372d92010-06-11 09:15:55 -070092 Usage();
93
94 return CGPT_FAILED;
95}