cgpt.c revision 3430b32667937a75c7a3afc83f8f7a601a8187f7
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 "cgpt.h" 10f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 11f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson#include <stdio.h> 12f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson#include <string.h> 13f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson#include <unistd.h> 14f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 15f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 16f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonconst char* progname; 17f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonconst char* command; 18f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 19f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonstruct { 20f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson const char *name; 21f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson int (*fp)(int argc, char *argv[]); 22f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson const char *comment; 23f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson} cmds[] = { 24f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson {"create", cmd_create, "Create or reset GPT headers and tables"}, 25f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson {"add", cmd_add, "Add, edit or remove a partition entry"}, 26f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson {"show", cmd_show, "Show partition table and entries"}, 27f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson {"repair", cmd_repair, "Repair damaged GPT headers and tables"}, 28f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"}, 294a2093129f226b4b2b4684f2aebe2f4368f85facBill Richardson {"find", cmd_find, "Locate a partition by its GUID"}, 303430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson {"prioritize", cmd_prioritize, 313430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson "Reorder the priority of all kernel partitions"}, 32f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson}; 33f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 34f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonvoid Usage(void) { 35f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson int i; 36f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 373430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n" 38f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson "Supported COMMANDs:\n\n", 39f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson progname); 40f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 41f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) { 423430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson printf(" %-15s %s\n", cmds[i].name, cmds[i].comment); 43f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson } 44f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname); 45f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson} 46f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 47f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 48f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 49f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardsonint main(int argc, char *argv[]) { 50f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson int i; 513430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson int match_count = 0; 523430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson int match_index = 0; 53f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 54f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson progname = strrchr(argv[0], '/'); 55f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson if (progname) 56f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson progname++; 57f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson else 58f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson progname = argv[0]; 59f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 60f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson if (argc < 2) { 61f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson Usage(); 62f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson return CGPT_FAILED; 63f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson } 64f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 65f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson // increment optind now, so that getopt skips argv[0] in command function 66f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson command = argv[optind++]; 67f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 68f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson // Find the command to invoke. 69f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) { 703430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson // exact match? 71f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson if (0 == strcmp(cmds[i].name, command)) { 723430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson match_index = i; 733430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson match_count = 1; 743430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson break; 753430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson } 763430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson // unique match? 773430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson else if (0 == strncmp(cmds[i].name, command, strlen(command))) { 783430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson match_index = i; 793430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson match_count++; 80f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson } 81f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson } 82f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 833430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson if (match_count == 1) 843430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson return cmds[match_index].fp(argc, argv); 853430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson 863430b32667937a75c7a3afc83f8f7a601a8187f7Bill Richardson // Couldn't find a single matching command. 87f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson Usage(); 88f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson 89f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson return CGPT_FAILED; 90f1372d9109d638fbb1a177a89ebaf64e7ee0637eBill Richardson} 91