| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
#include <unistd.h> |
| 12 |
#include <uuid/uuid.h> |
| 13 |
|
| 14 |
#include "cgpt.h" |
| 15 |
#include "vboot_host.h" |
| 16 |
|
| 17 |
const char* progname; |
| 18 |
|
| 19 |
int GenerateGuid(Guid *newguid) |
| 20 |
{ |
| 21 |
|
| 22 |
uuid_generate(newguid->u.raw); |
| 23 |
return CGPT_OK; |
| 24 |
} |
| 25 |
|
| 26 |
struct { |
| 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"}, |
| 36 |
{"find", cmd_find, "Locate a partition by its GUID"}, |
| 37 |
{"edit", cmd_edit, "Edit a drive entry"}, |
| 38 |
{"prioritize", cmd_prioritize, |
| 39 |
"Reorder the priority of all kernel partitions"}, |
| 40 |
{"legacy", cmd_legacy, "Switch between GPT and Legacy GPT"}, |
| 41 |
}; |
| 42 |
|
| 43 |
static void Usage(void) { |
| 44 |
int i; |
| 45 |
|
| 46 |
printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n" |
| 47 |
"Supported COMMANDs:\n\n", |
| 48 |
progname); |
| 49 |
|
| 50 |
for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) { |
| 51 |
printf(" %-15s %s\n", cmds[i].name, cmds[i].comment); |
| 52 |
} |
| 53 |
printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname); |
| 54 |
} |
| 55 |
|
| 56 |
int main(int argc, char *argv[]) { |
| 57 |
int i; |
| 58 |
int match_count = 0; |
| 59 |
int match_index = 0; |
| 60 |
char* command; |
| 61 |
|
| 62 |
progname = strrchr(argv[0], '/'); |
| 63 |
if (progname) |
| 64 |
progname++; |
| 65 |
else |
| 66 |
progname = argv[0]; |
| 67 |
|
| 68 |
if (argc < 2) { |
| 69 |
Usage(); |
| 70 |
return CGPT_FAILED; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
command = argv[optind++]; |
| 75 |
|
| 76 |
|
| 77 |
for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) { |
| 78 |
|
| 79 |
if (0 == strcmp(cmds[i].name, command)) { |
| 80 |
match_index = i; |
| 81 |
match_count = 1; |
| 82 |
break; |
| 83 |
} |
| 84 |
|
| 85 |
else if (0 == strncmp(cmds[i].name, command, strlen(command))) { |
| 86 |
match_index = i; |
| 87 |
match_count++; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (match_count == 1) |
| 92 |
return cmds[match_index].fp(argc, argv); |
| 93 |
|
| 94 |
|
| 95 |
Usage(); |
| 96 |
|
| 97 |
return CGPT_FAILED; |
| 98 |
} |