blob: 4c9fc328824ed8e3b254fcdfa30c8d3e25eabe82 [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>
14
15
16const char* progname;
17const char* command;
18
19struct {
20 const char *name;
21 int (*fp)(int argc, char *argv[]);
22 const char *comment;
23} cmds[] = {
24 {"create", cmd_create, "Create or reset GPT headers and tables"},
25 {"add", cmd_add, "Add, edit or remove a partition entry"},
26 {"show", cmd_show, "Show partition table and entries"},
27 {"repair", cmd_repair, "Repair damaged GPT headers and tables"},
28 {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"},
Bill Richardson4a209312010-07-02 11:34:38 -070029 {"find", cmd_find, "Locate a partition by its GUID"},
Bill Richardson3430b322010-11-29 14:24:51 -080030 {"prioritize", cmd_prioritize,
31 "Reorder the priority of all kernel partitions"},
Bill Richardsonf1372d92010-06-11 09:15:55 -070032};
33
Bill Richardsonf1372d92010-06-11 09:15:55 -070034void Usage(void) {
35 int i;
36
Bill Richardson3430b322010-11-29 14:24:51 -080037 printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070038 "Supported COMMANDs:\n\n",
39 progname);
40
41 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080042 printf(" %-15s %s\n", cmds[i].name, cmds[i].comment);
Bill Richardsonf1372d92010-06-11 09:15:55 -070043 }
44 printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname);
45}
46
47
48
49int main(int argc, char *argv[]) {
50 int i;
Bill Richardson3430b322010-11-29 14:24:51 -080051 int match_count = 0;
52 int match_index = 0;
Bill Richardsonf1372d92010-06-11 09:15:55 -070053
54 progname = strrchr(argv[0], '/');
55 if (progname)
56 progname++;
57 else
58 progname = argv[0];
59
60 if (argc < 2) {
61 Usage();
62 return CGPT_FAILED;
63 }
64
65 // increment optind now, so that getopt skips argv[0] in command function
66 command = argv[optind++];
67
68 // Find the command to invoke.
69 for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080070 // exact match?
Bill Richardsonf1372d92010-06-11 09:15:55 -070071 if (0 == strcmp(cmds[i].name, command)) {
Bill Richardson3430b322010-11-29 14:24:51 -080072 match_index = i;
73 match_count = 1;
74 break;
75 }
76 // unique match?
77 else if (0 == strncmp(cmds[i].name, command, strlen(command))) {
78 match_index = i;
79 match_count++;
Bill Richardsonf1372d92010-06-11 09:15:55 -070080 }
81 }
82
Bill Richardson3430b322010-11-29 14:24:51 -080083 if (match_count == 1)
84 return cmds[match_index].fp(argc, argv);
85
86 // Couldn't find a single matching command.
Bill Richardsonf1372d92010-06-11 09:15:55 -070087 Usage();
88
89 return CGPT_FAILED;
90}