blob: e963142ab80afc5fe1e3e416cc3cf68304e5ef98 [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"},
Bill Richardsonf1372d92010-06-11 09:15:55 -070033};
34
Bill Richardsonf1372d92010-06-11 09:15:55 -070035void Usage(void) {
36 int i;
37
Bill Richardson3430b322010-11-29 14:24:51 -080038 printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n"
Bill Richardsonf1372d92010-06-11 09:15:55 -070039 "Supported COMMANDs:\n\n",
40 progname);
41
42 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080043 printf(" %-15s %s\n", cmds[i].name, cmds[i].comment);
Bill Richardsonf1372d92010-06-11 09:15:55 -070044 }
45 printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname);
46}
47
48
49
50int main(int argc, char *argv[]) {
51 int i;
Bill Richardson3430b322010-11-29 14:24:51 -080052 int match_count = 0;
53 int match_index = 0;
Bill Richardsonf1372d92010-06-11 09:15:55 -070054
Jay Srinivasan5fac7572012-02-23 10:59:01 -080055 uuid_generator = uuid_generate;
56
Bill Richardsonf1372d92010-06-11 09:15:55 -070057 progname = strrchr(argv[0], '/');
58 if (progname)
59 progname++;
60 else
61 progname = argv[0];
62
63 if (argc < 2) {
64 Usage();
65 return CGPT_FAILED;
66 }
67
68 // increment optind now, so that getopt skips argv[0] in command function
69 command = argv[optind++];
70
71 // Find the command to invoke.
72 for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
Bill Richardson3430b322010-11-29 14:24:51 -080073 // exact match?
Bill Richardsonf1372d92010-06-11 09:15:55 -070074 if (0 == strcmp(cmds[i].name, command)) {
Bill Richardson3430b322010-11-29 14:24:51 -080075 match_index = i;
76 match_count = 1;
77 break;
78 }
79 // unique match?
80 else if (0 == strncmp(cmds[i].name, command, strlen(command))) {
81 match_index = i;
82 match_count++;
Bill Richardsonf1372d92010-06-11 09:15:55 -070083 }
84 }
85
Bill Richardson3430b322010-11-29 14:24:51 -080086 if (match_count == 1)
87 return cmds[match_index].fp(argc, argv);
88
89 // Couldn't find a single matching command.
Bill Richardsonf1372d92010-06-11 09:15:55 -070090 Usage();
91
92 return CGPT_FAILED;
93}