blob: ac7aecdd5da0665db04ce201527f853e0b9601ab [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
9#include "cgpt.h"
10
11#include <stdio.h>
12#include <string.h>
13#include <unistd.h>
Jay Srinivasan5fac7572012-02-23 10:59:01 -080014#include <uuid/uuid.h>
Bill Richardsonf1372d92010-06-11 09:15:55 -070015
16const char* progname;
17const char* command;
Jay Srinivasan5fac7572012-02-23 10:59:01 -080018void (*uuid_generator)(uint8_t* buffer);
Bill Richardsonf1372d92010-06-11 09:15:55 -070019
20struct {
21 const char *name;
22 int (*fp)(int argc, char *argv[]);
23 const char *comment;
24} cmds[] = {
25 {"create", cmd_create, "Create or reset GPT headers and tables"},
26 {"add", cmd_add, "Add, edit or remove a partition entry"},
27 {"show", cmd_show, "Show partition table and entries"},
28 {"repair", cmd_repair, "Repair damaged GPT headers and tables"},
29 {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"},
Bill Richardson4a209312010-07-02 11:34:38 -070030 {"find", cmd_find, "Locate a partition by its GUID"},
Bill Richardson3430b322010-11-29 14:24:51 -080031 {"prioritize", cmd_prioritize,
32 "Reorder the priority of all kernel partitions"},
Stefan Reinauerb7b865c2012-08-23 15:06:25 -070033 {"legacy", cmd_legacy, "Switch between GPT and Legacy GPT"},
Bill Richardsonf1372d92010-06-11 09:15:55 -070034};
35
Bill Richardsonf1372d92010-06-11 09:15:55 -070036void Usage(void) {
37 int i;
38
Bill Richardson3430b322010-11-29 14:24:51 -080039 printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070040 "Supported COMMANDs:\n\n",
41 progname);
42
43 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080044 printf(" %-15s %s\n", cmds[i].name, cmds[i].comment);
Bill Richardsonf1372d92010-06-11 09:15:55 -070045 }
46 printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname);
47}
48
49
50
51int main(int argc, char *argv[]) {
52 int i;
Bill Richardson3430b322010-11-29 14:24:51 -080053 int match_count = 0;
54 int match_index = 0;
Bill Richardsonf1372d92010-06-11 09:15:55 -070055
Jay Srinivasan5fac7572012-02-23 10:59:01 -080056 uuid_generator = uuid_generate;
57
Bill Richardsonf1372d92010-06-11 09:15:55 -070058 progname = strrchr(argv[0], '/');
59 if (progname)
60 progname++;
61 else
62 progname = argv[0];
63
64 if (argc < 2) {
65 Usage();
66 return CGPT_FAILED;
67 }
68
69 // increment optind now, so that getopt skips argv[0] in command function
70 command = argv[optind++];
71
72 // Find the command to invoke.
73 for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080074 // exact match?
Bill Richardsonf1372d92010-06-11 09:15:55 -070075 if (0 == strcmp(cmds[i].name, command)) {
Bill Richardson3430b322010-11-29 14:24:51 -080076 match_index = i;
77 match_count = 1;
78 break;
79 }
80 // unique match?
81 else if (0 == strncmp(cmds[i].name, command, strlen(command))) {
82 match_index = i;
83 match_count++;
Bill Richardsonf1372d92010-06-11 09:15:55 -070084 }
85 }
86
Bill Richardson3430b322010-11-29 14:24:51 -080087 if (match_count == 1)
88 return cmds[match_index].fp(argc, argv);
89
90 // Couldn't find a single matching command.
Bill Richardsonf1372d92010-06-11 09:15:55 -070091 Usage();
92
93 return CGPT_FAILED;
94}