cmd_legacy.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (c) 2012 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. #include <getopt.h>
  5. #include <string.h>
  6. #include "cgpt.h"
  7. #include "vboot_host.h"
  8. extern const char* progname;
  9. static void Usage(void)
  10. {
  11. printf("\nUsage: %s legacy [OPTIONS] DRIVE\n\n"
  12. "Switch GPT header signature to \"CHROMEOS\".\n\n"
  13. "Options:\n"
  14. " -D NUM Size (in bytes) of the disk where partitions reside\n"
  15. " default 0, meaning partitions and GPT structs are\n"
  16. " both on DRIVE\n"
  17. " -e Switch GPT header signature back to \"EFI PART\"\n"
  18. " -p Switch primary GPT header signature to \"IGNOREME\"\n"
  19. "\n", progname);
  20. }
  21. int cmd_legacy(int argc, char *argv[]) {
  22. CgptLegacyParams params;
  23. memset(&params, 0, sizeof(params));
  24. int c;
  25. char* e = 0;
  26. int errorcnt = 0;
  27. opterr = 0; // quiet, you
  28. while ((c=getopt(argc, argv, ":hepD:")) != -1)
  29. {
  30. switch (c)
  31. {
  32. case 'D':
  33. params.drive_size = strtoull(optarg, &e, 0);
  34. errorcnt += check_int_parse(c, e);
  35. break;
  36. case 'e':
  37. if (params.mode) {
  38. Error("Incompatible flags, pick either -e or -p\n");
  39. errorcnt++;
  40. }
  41. params.mode = CGPT_LEGACY_MODE_EFIPART;
  42. break;
  43. case 'p':
  44. if (params.mode) {
  45. Error("Incompatible flags, pick either -e or -p\n");
  46. errorcnt++;
  47. }
  48. params.mode = CGPT_LEGACY_MODE_IGNORE_PRIMARY;
  49. break;
  50. case 'h':
  51. Usage();
  52. return CGPT_OK;
  53. case '?':
  54. Error("unrecognized option: -%c\n", optopt);
  55. errorcnt++;
  56. break;
  57. case ':':
  58. Error("missing argument to -%c\n", optopt);
  59. errorcnt++;
  60. break;
  61. default:
  62. errorcnt++;
  63. break;
  64. }
  65. }
  66. if (errorcnt)
  67. {
  68. Usage();
  69. return CGPT_FAILED;
  70. }
  71. if (optind >= argc) {
  72. Usage();
  73. return CGPT_FAILED;
  74. }
  75. params.drive_name = argv[optind];
  76. return CgptLegacy(&params);
  77. }