cmd_create.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 create [OPTIONS] DRIVE\n\n"
  12. "Create or reset an empty GPT.\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. " -z Zero the sectors of the GPT table and entries\n"
  18. " -p NUM Size (in blocks) of the disk to pad between the\n"
  19. " primary GPT header and its entries, default 0\n"
  20. "\n", progname);
  21. }
  22. int cmd_create(int argc, char *argv[]) {
  23. CgptCreateParams params;
  24. memset(&params, 0, sizeof(params));
  25. int c;
  26. int errorcnt = 0;
  27. char *e = 0;
  28. opterr = 0; // quiet, you
  29. while ((c=getopt(argc, argv, ":hzp:D:")) != -1)
  30. {
  31. switch (c)
  32. {
  33. case 'D':
  34. params.drive_size = strtoull(optarg, &e, 0);
  35. errorcnt += check_int_parse(c, e);
  36. break;
  37. case 'z':
  38. params.zap = 1;
  39. break;
  40. case 'p':
  41. params.padding = strtoull(optarg, &e, 0);
  42. errorcnt += check_int_parse(c, e);
  43. break;
  44. case 'h':
  45. Usage();
  46. return CGPT_OK;
  47. case '?':
  48. Error("unrecognized option: -%c\n", optopt);
  49. errorcnt++;
  50. break;
  51. case ':':
  52. Error("missing argument to -%c\n", optopt);
  53. errorcnt++;
  54. break;
  55. default:
  56. errorcnt++;
  57. break;
  58. }
  59. }
  60. if (errorcnt)
  61. {
  62. Usage();
  63. return CGPT_FAILED;
  64. }
  65. if (optind >= argc) {
  66. Usage();
  67. return CGPT_FAILED;
  68. }
  69. params.drive_name = argv[optind];
  70. return CgptCreate(&params);
  71. }