blob: 380920060d359a7d36b3b66739fd229618ba3b24 [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;
Bill Richardson4cb54972014-06-20 14:33:00 -070018
19int GenerateGuid(Guid *newguid)
20{
21 /* From libuuid */
22 uuid_generate(newguid->u.raw);
23 return CGPT_OK;
24}
Bill Richardsonf1372d92010-06-11 09:15:55 -070025
26struct {
27 const char *name;
28 int (*fp)(int argc, char *argv[]);
29 const char *comment;
30} cmds[] = {
31 {"create", cmd_create, "Create or reset GPT headers and tables"},
32 {"add", cmd_add, "Add, edit or remove a partition entry"},
33 {"show", cmd_show, "Show partition table and entries"},
34 {"repair", cmd_repair, "Repair damaged GPT headers and tables"},
35 {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"},
Bill Richardson4a209312010-07-02 11:34:38 -070036 {"find", cmd_find, "Locate a partition by its GUID"},
Bill Richardson3430b322010-11-29 14:24:51 -080037 {"prioritize", cmd_prioritize,
38 "Reorder the priority of all kernel partitions"},
Stefan Reinauerb7b865c2012-08-23 15:06:25 -070039 {"legacy", cmd_legacy, "Switch between GPT and Legacy GPT"},
Bill Richardsonf1372d92010-06-11 09:15:55 -070040};
41
Bill Richardsonf1372d92010-06-11 09:15:55 -070042void Usage(void) {
43 int i;
44
Bill Richardson3430b322010-11-29 14:24:51 -080045 printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070046 "Supported COMMANDs:\n\n",
47 progname);
48
49 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080050 printf(" %-15s %s\n", cmds[i].name, cmds[i].comment);
Bill Richardsonf1372d92010-06-11 09:15:55 -070051 }
52 printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname);
53}
54
Bill Richardsonf1372d92010-06-11 09:15:55 -070055int main(int argc, char *argv[]) {
56 int i;
Bill Richardson3430b322010-11-29 14:24:51 -080057 int match_count = 0;
58 int match_index = 0;
Bill Richardson4cb54972014-06-20 14:33:00 -070059 char* command;
Jay Srinivasan5fac7572012-02-23 10:59:01 -080060
Bill Richardsonf1372d92010-06-11 09:15:55 -070061 progname = strrchr(argv[0], '/');
62 if (progname)
63 progname++;
64 else
65 progname = argv[0];
66
67 if (argc < 2) {
68 Usage();
69 return CGPT_FAILED;
70 }
71
72 // increment optind now, so that getopt skips argv[0] in command function
73 command = argv[optind++];
74
75 // Find the command to invoke.
76 for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080077 // exact match?
Bill Richardsonf1372d92010-06-11 09:15:55 -070078 if (0 == strcmp(cmds[i].name, command)) {
Bill Richardson3430b322010-11-29 14:24:51 -080079 match_index = i;
80 match_count = 1;
81 break;
82 }
83 // unique match?
84 else if (0 == strncmp(cmds[i].name, command, strlen(command))) {
85 match_index = i;
86 match_count++;
Bill Richardsonf1372d92010-06-11 09:15:55 -070087 }
88 }
89
Bill Richardson3430b322010-11-29 14:24:51 -080090 if (match_count == 1)
91 return cmds[match_index].fp(argc, argv);
92
93 // Couldn't find a single matching command.
Bill Richardsonf1372d92010-06-11 09:15:55 -070094 Usage();
95
96 return CGPT_FAILED;
97}