cgpt.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* 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. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <uuid/uuid.h>
  12. #include "cgpt.h"
  13. #include "vboot_host.h"
  14. const char* progname;
  15. int GenerateGuid(Guid *newguid)
  16. {
  17. /* From libuuid */
  18. uuid_generate(newguid->u.raw);
  19. return CGPT_OK;
  20. }
  21. struct {
  22. const char *name;
  23. int (*fp)(int argc, char *argv[]);
  24. const char *comment;
  25. } cmds[] = {
  26. {"create", cmd_create, "Create or reset GPT headers and tables"},
  27. {"add", cmd_add, "Add, edit or remove a partition entry"},
  28. {"show", cmd_show, "Show partition table and entries"},
  29. {"repair", cmd_repair, "Repair damaged GPT headers and tables"},
  30. {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"},
  31. {"find", cmd_find, "Locate a partition by its GUID"},
  32. {"prioritize", cmd_prioritize,
  33. "Reorder the priority of all kernel partitions"},
  34. {"legacy", cmd_legacy, "Switch between GPT and Legacy GPT"},
  35. };
  36. void Usage(void) {
  37. int i;
  38. printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n"
  39. "Supported COMMANDs:\n\n",
  40. progname);
  41. for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
  42. printf(" %-15s %s\n", cmds[i].name, cmds[i].comment);
  43. }
  44. printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname);
  45. }
  46. int main(int argc, char *argv[]) {
  47. int i;
  48. int match_count = 0;
  49. int match_index = 0;
  50. char* command;
  51. progname = strrchr(argv[0], '/');
  52. if (progname)
  53. progname++;
  54. else
  55. progname = argv[0];
  56. if (argc < 2) {
  57. Usage();
  58. return CGPT_FAILED;
  59. }
  60. // increment optind now, so that getopt skips argv[0] in command function
  61. command = argv[optind++];
  62. // Find the command to invoke.
  63. for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
  64. // exact match?
  65. if (0 == strcmp(cmds[i].name, command)) {
  66. match_index = i;
  67. match_count = 1;
  68. break;
  69. }
  70. // unique match?
  71. else if (0 == strncmp(cmds[i].name, command, strlen(command))) {
  72. match_index = i;
  73. match_count++;
  74. }
  75. }
  76. if (match_count == 1)
  77. return cmds[match_index].fp(argc, argv);
  78. // Couldn't find a single matching command.
  79. Usage();
  80. return CGPT_FAILED;
  81. }