1f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson * Use of this source code is governed by a BSD-style license that can be
3f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson * found in the LICENSE file.
4f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson *
5f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson * Utility for ChromeOS-specific GPT partitions, Please see corresponding .c
6f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson * files for more details.
7f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson */
8f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
9f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson#include <stdio.h>
10f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson#include <string.h>
11f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson#include <unistd.h>
125fac757abef456bf66d9dd96b316ec61fe04b48eJay Srinivasan#include <uuid/uuid.h>
13f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
140c3ba249abb1dc60f5ebabccf84ff13206440b83Bill Richardson#include "cgpt.h"
150c3ba249abb1dc60f5ebabccf84ff13206440b83Bill Richardson#include "vboot_host.h"
160c3ba249abb1dc60f5ebabccf84ff13206440b83Bill Richardson
17f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonconst char* progname;
184cb5497984642b8cbd592c14cb1912a787b2d4d7Bill Richardson
194cb5497984642b8cbd592c14cb1912a787b2d4d7Bill Richardsonint GenerateGuid(Guid *newguid)
204cb5497984642b8cbd592c14cb1912a787b2d4d7Bill Richardson{
214cb5497984642b8cbd592c14cb1912a787b2d4d7Bill Richardson  /* From libuuid */
224cb5497984642b8cbd592c14cb1912a787b2d4d7Bill Richardson  uuid_generate(newguid->u.raw);
234cb5497984642b8cbd592c14cb1912a787b2d4d7Bill Richardson  return CGPT_OK;
244cb5497984642b8cbd592c14cb1912a787b2d4d7Bill Richardson}
25f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
26f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonstruct {
27f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  const char *name;
28f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  int (*fp)(int argc, char *argv[]);
29f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  const char *comment;
30f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson} cmds[] = {
31f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  {"create", cmd_create, "Create or reset GPT headers and tables"},
32f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  {"add", cmd_add, "Add, edit or remove a partition entry"},
33f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  {"show", cmd_show, "Show partition table and entries"},
34f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  {"repair", cmd_repair, "Repair damaged GPT headers and tables"},
35f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"},
364a2093129f226b4b2b4684f2aebe2f4368f85facBill Richardson  {"find", cmd_find, "Locate a partition by its GUID"},
373430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson  {"prioritize", cmd_prioritize,
383430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson   "Reorder the priority of all kernel partitions"},
39b7b865cfee68190babd971ab9a897bdabbab075fStefan Reinauer  {"legacy", cmd_legacy, "Switch between GPT and Legacy GPT"},
40f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson};
41f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
42f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonvoid Usage(void) {
43f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  int i;
44f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
453430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson  printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n"
46f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson         "Supported COMMANDs:\n\n",
47f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson         progname);
48f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
49f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
503430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson    printf("    %-15s  %s\n", cmds[i].name, cmds[i].comment);
51f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  }
52f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname);
53f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson}
54f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
55f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonint main(int argc, char *argv[]) {
56f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  int i;
573430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson  int match_count = 0;
583430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson  int match_index = 0;
594cb5497984642b8cbd592c14cb1912a787b2d4d7Bill Richardson  char* command;
605fac757abef456bf66d9dd96b316ec61fe04b48eJay Srinivasan
61f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  progname = strrchr(argv[0], '/');
62f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  if (progname)
63f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson    progname++;
64f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  else
65f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson    progname = argv[0];
66f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
67f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  if (argc < 2) {
68f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson    Usage();
69f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson    return CGPT_FAILED;
70f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  }
71f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
72f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  // increment optind now, so that getopt skips argv[0] in command function
73f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  command = argv[optind++];
74f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
75f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  // Find the command to invoke.
76f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
773430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson    // exact match?
78f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson    if (0 == strcmp(cmds[i].name, command)) {
793430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson      match_index = i;
803430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson      match_count = 1;
813430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson      break;
823430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson    }
833430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson    // unique match?
843430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson    else if (0 == strncmp(cmds[i].name, command, strlen(command))) {
853430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson      match_index = i;
863430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson      match_count++;
87f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson    }
88f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  }
89f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
903430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson  if (match_count == 1)
913430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson    return cmds[match_index].fp(argc, argv);
923430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson
933430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson  // Couldn't find a single matching command.
94f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  Usage();
95f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson
96f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson  return CGPT_FAILED;
97f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson}
98