cmd_add.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 add [OPTIONS] DRIVE\n\n"
  12. "Add, edit, or remove a partition entry.\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. " -i NUM Specify partition (default is next available)\n"
  18. " -b NUM Beginning sector\n"
  19. " -s NUM Size in sectors\n"
  20. " -t GUID Partition Type GUID\n"
  21. " -u GUID Partition Unique ID\n"
  22. " -l LABEL Label\n"
  23. " -S NUM set Successful flag (0|1)\n"
  24. " -T NUM set Tries flag (0-15)\n"
  25. " -P NUM set Priority flag (0-15)\n"
  26. " -B NUM set Legacy Boot flag (0|1)\n"
  27. " -A NUM set raw 16-bit attribute value (bits 48-63)\n"
  28. "\n"
  29. "Use the -i option to modify an existing partition.\n"
  30. "The -b, -s, and -t options must be given for new partitions.\n"
  31. "\n", progname);
  32. PrintTypes();
  33. }
  34. int cmd_add(int argc, char *argv[]) {
  35. CgptAddParams params;
  36. memset(&params, 0, sizeof(params));
  37. int c;
  38. int errorcnt = 0;
  39. char *e = 0;
  40. opterr = 0; // quiet, you
  41. while ((c=getopt(argc, argv, ":hi:b:s:t:u:l:S:T:P:B:A:D:")) != -1)
  42. {
  43. switch (c)
  44. {
  45. case 'D':
  46. params.drive_size = strtoull(optarg, &e, 0);
  47. errorcnt += check_int_parse(c, e);
  48. break;
  49. case 'i':
  50. params.partition = (uint32_t)strtoul(optarg, &e, 0);
  51. errorcnt += check_int_parse(c, e);
  52. break;
  53. case 'b':
  54. params.set_begin = 1;
  55. params.begin = strtoull(optarg, &e, 0);
  56. errorcnt += check_int_parse(c, e);
  57. break;
  58. case 's':
  59. params.set_size = 1;
  60. params.size = strtoull(optarg, &e, 0);
  61. errorcnt += check_int_parse(c, e);
  62. break;
  63. case 't':
  64. params.set_type = 1;
  65. if (CGPT_OK != SupportedType(optarg, &params.type_guid) &&
  66. CGPT_OK != StrToGuid(optarg, &params.type_guid)) {
  67. Error("invalid argument to -%c: %s\n", c, optarg);
  68. errorcnt++;
  69. }
  70. break;
  71. case 'u':
  72. params.set_unique = 1;
  73. if (CGPT_OK != StrToGuid(optarg, &params.unique_guid)) {
  74. Error("invalid argument to -%c: %s\n", c, optarg);
  75. errorcnt++;
  76. }
  77. break;
  78. case 'l':
  79. params.label = optarg;
  80. break;
  81. case 'S':
  82. params.set_successful = 1;
  83. params.successful = (uint32_t)strtoul(optarg, &e, 0);
  84. errorcnt += check_int_parse(c, e);
  85. errorcnt += check_int_limit(c, params.successful, 0, 1);
  86. break;
  87. case 'T':
  88. params.set_tries = 1;
  89. params.tries = (uint32_t)strtoul(optarg, &e, 0);
  90. errorcnt += check_int_parse(c, e);
  91. errorcnt += check_int_limit(c, params.tries, 0, 15);
  92. break;
  93. case 'P':
  94. params.set_priority = 1;
  95. params.priority = (uint32_t)strtoul(optarg, &e, 0);
  96. errorcnt += check_int_parse(c, e);
  97. errorcnt += check_int_limit(c, params.priority, 0, 15);
  98. break;
  99. case 'B':
  100. params.set_legacy_boot = 1;
  101. params.legacy_boot = (uint32_t)strtoul(optarg, &e, 0);
  102. errorcnt += check_int_parse(c, e);
  103. errorcnt += check_int_limit(c, params.legacy_boot, 0, 1);
  104. break;
  105. case 'A':
  106. params.set_raw = 1;
  107. params.raw_value = strtoull(optarg, &e, 0);
  108. errorcnt += check_int_parse(c, e);
  109. break;
  110. case 'h':
  111. Usage();
  112. return CGPT_OK;
  113. case '?':
  114. Error("unrecognized option: -%c\n", optopt);
  115. errorcnt++;
  116. break;
  117. case ':':
  118. Error("missing argument to -%c\n", optopt);
  119. errorcnt++;
  120. break;
  121. default:
  122. errorcnt++;
  123. break;
  124. }
  125. }
  126. if (errorcnt)
  127. {
  128. Usage();
  129. return CGPT_FAILED;
  130. }
  131. if (optind >= argc)
  132. {
  133. Error("missing drive argument\n");
  134. return CGPT_FAILED;
  135. }
  136. params.drive_name = argv[optind];
  137. return CgptAdd(&params);
  138. }