cgpt_add.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <string.h>
  5. #include "cgpt.h"
  6. #include "cgpt_params.h"
  7. #include "cgptlib_internal.h"
  8. #include "utility.h"
  9. #include "vboot_host.h"
  10. static const char* DumpCgptAddParams(const CgptAddParams *params) {
  11. static char buf[256];
  12. char tmp[64];
  13. buf[0] = 0;
  14. snprintf(tmp, sizeof(tmp), "-i %d ", params->partition);
  15. StrnAppend(buf, tmp, sizeof(buf));
  16. if (params->label) {
  17. snprintf(tmp, sizeof(tmp), "-l %s ", params->label);
  18. StrnAppend(buf, tmp, sizeof(buf));
  19. }
  20. if (params->set_begin) {
  21. snprintf(tmp, sizeof(tmp), "-b %llu ", (unsigned long long)params->begin);
  22. StrnAppend(buf, tmp, sizeof(buf));
  23. }
  24. if (params->set_size) {
  25. snprintf(tmp, sizeof(tmp), "-s %llu ", (unsigned long long)params->size);
  26. StrnAppend(buf, tmp, sizeof(buf));
  27. }
  28. if (params->set_type) {
  29. GuidToStr(&params->type_guid, tmp, sizeof(tmp));
  30. StrnAppend(buf, "-t ", sizeof(buf));
  31. StrnAppend(buf, tmp, sizeof(buf));
  32. StrnAppend(buf, " ", sizeof(buf));
  33. }
  34. if (params->set_unique) {
  35. GuidToStr(&params->unique_guid, tmp, sizeof(tmp));
  36. StrnAppend(buf, "-u ", sizeof(buf));
  37. StrnAppend(buf, tmp, sizeof(buf));
  38. StrnAppend(buf, " ", sizeof(buf));
  39. }
  40. if (params->set_successful) {
  41. snprintf(tmp, sizeof(tmp), "-S %d ", params->successful);
  42. StrnAppend(buf, tmp, sizeof(buf));
  43. }
  44. if (params->set_tries) {
  45. snprintf(tmp, sizeof(tmp), "-T %d ", params->tries);
  46. StrnAppend(buf, tmp, sizeof(buf));
  47. }
  48. if (params->set_priority) {
  49. snprintf(tmp, sizeof(tmp), "-P %d ", params->priority);
  50. StrnAppend(buf, tmp, sizeof(buf));
  51. }
  52. if (params->set_legacy_boot) {
  53. snprintf(tmp, sizeof(tmp), "-B %d ", params->legacy_boot);
  54. StrnAppend(buf, tmp, sizeof(buf));
  55. }
  56. if (params->set_raw) {
  57. snprintf(tmp, sizeof(tmp), "-A 0x%x ", params->raw_value);
  58. StrnAppend(buf, tmp, sizeof(buf));
  59. }
  60. StrnAppend(buf, "\n", sizeof(buf));
  61. return buf;
  62. }
  63. // This is the implementation-specific helper function.
  64. static int GptSetEntryAttributes(struct drive *drive,
  65. uint32_t index,
  66. CgptAddParams *params) {
  67. GptEntry *entry;
  68. entry = GetEntry(&drive->gpt, PRIMARY, index);
  69. if (params->set_begin)
  70. entry->starting_lba = params->begin;
  71. if (params->set_size)
  72. entry->ending_lba = entry->starting_lba + params->size - 1;
  73. if (params->set_unique) {
  74. memcpy(&entry->unique, &params->unique_guid, sizeof(Guid));
  75. } else if (GuidIsZero(&entry->type)) {
  76. if (CGPT_OK != GenerateGuid(&entry->unique)) {
  77. Error("Unable to generate new GUID.\n");
  78. return -1;
  79. }
  80. }
  81. if (params->set_type)
  82. memcpy(&entry->type, &params->type_guid, sizeof(Guid));
  83. if (params->label) {
  84. if (CGPT_OK != UTF8ToUTF16((uint8_t *)params->label, entry->name,
  85. sizeof(entry->name) / sizeof(entry->name[0]))) {
  86. Error("The label cannot be converted to UTF16.\n");
  87. return -1;
  88. }
  89. }
  90. return 0;
  91. }
  92. // This is an internal helper function which assumes no NULL args are passed.
  93. // It sets the given attribute values for a single entry at the given index.
  94. static int SetEntryAttributes(struct drive *drive,
  95. uint32_t index,
  96. CgptAddParams *params) {
  97. if (params->set_raw) {
  98. SetRaw(drive, PRIMARY, index, params->raw_value);
  99. } else {
  100. if (params->set_successful)
  101. SetSuccessful(drive, PRIMARY, index, params->successful);
  102. if (params->set_tries)
  103. SetTries(drive, PRIMARY, index, params->tries);
  104. if (params->set_priority)
  105. SetPriority(drive, PRIMARY, index, params->priority);
  106. if (params->set_legacy_boot)
  107. SetLegacyBoot(drive, PRIMARY, index, params->legacy_boot);
  108. }
  109. // New partitions must specify type, begin, and size.
  110. if (IsUnused(drive, PRIMARY, index)) {
  111. if (!params->set_begin || !params->set_size || !params->set_type) {
  112. Error("-t, -b, and -s options are required for new partitions\n");
  113. return -1;
  114. }
  115. if (GuidIsZero(&params->type_guid)) {
  116. Error("New partitions must have a type other than \"unused\"\n");
  117. return -1;
  118. }
  119. }
  120. return 0;
  121. }
  122. static int CgptCheckAddValidity(struct drive *drive) {
  123. int gpt_retval;
  124. if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive->gpt))) {
  125. Error("GptSanityCheck() returned %d: %s\n",
  126. gpt_retval, GptError(gpt_retval));
  127. return -1;
  128. }
  129. if (((drive->gpt.valid_headers & MASK_BOTH) != MASK_BOTH) ||
  130. ((drive->gpt.valid_entries & MASK_BOTH) != MASK_BOTH)) {
  131. Error("one of the GPT header/entries is invalid.\n"
  132. "please run 'cgpt repair' before adding anything.\n");
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. static int CgptGetUnusedPartition(struct drive *drive, uint32_t *index,
  138. CgptAddParams *params) {
  139. uint32_t i;
  140. uint32_t max_part = GetNumberOfEntries(drive);
  141. if (params->partition) {
  142. if (params->partition > max_part) {
  143. Error("invalid partition number: %d\n", params->partition);
  144. return -1;
  145. }
  146. *index = params->partition - 1;
  147. return 0;
  148. } else {
  149. // Find next empty partition.
  150. for (i = 0; i < max_part; i++) {
  151. if (IsUnused(drive, PRIMARY, i)) {
  152. params->partition = i + 1;
  153. *index = i;
  154. return 0;
  155. }
  156. }
  157. Error("no unused partitions available\n");
  158. return -1;
  159. }
  160. }
  161. int CgptSetAttributes(CgptAddParams *params) {
  162. struct drive drive;
  163. if (params == NULL)
  164. return CGPT_FAILED;
  165. if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
  166. params->drive_size))
  167. return CGPT_FAILED;
  168. if (CgptCheckAddValidity(&drive)) {
  169. goto bad;
  170. }
  171. if (params->partition == 0 ||
  172. params->partition >= GetNumberOfEntries(&drive)) {
  173. Error("invalid partition number: %d\n", params->partition);
  174. goto bad;
  175. }
  176. SetEntryAttributes(&drive, params->partition - 1, params);
  177. UpdateAllEntries(&drive);
  178. // Write it all out.
  179. return DriveClose(&drive, 1);
  180. bad:
  181. DriveClose(&drive, 0);
  182. return CGPT_FAILED;
  183. }
  184. // This method gets the partition details such as the attributes, the
  185. // guids of the partitions, etc. Input is the partition number or the
  186. // unique id of the partition. Output is populated in the respective
  187. // fields of params.
  188. int CgptGetPartitionDetails(CgptAddParams *params) {
  189. struct drive drive;
  190. int result = CGPT_FAILED;
  191. int index;
  192. if (params == NULL)
  193. return CGPT_FAILED;
  194. if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
  195. params->drive_size))
  196. return CGPT_FAILED;
  197. if (CgptCheckAddValidity(&drive)) {
  198. goto bad;
  199. }
  200. int max_part = GetNumberOfEntries(&drive);
  201. if (params->partition > 0) {
  202. if (params->partition >= max_part) {
  203. Error("invalid partition number: %d\n", params->partition);
  204. goto bad;
  205. }
  206. } else {
  207. if (!params->set_unique) {
  208. Error("either partition or unique_id must be specified\n");
  209. goto bad;
  210. }
  211. for (index = 0; index < max_part; index++) {
  212. GptEntry *entry = GetEntry(&drive.gpt, PRIMARY, index);
  213. if (GuidEqual(&entry->unique, &params->unique_guid)) {
  214. params->partition = index + 1;
  215. break;
  216. }
  217. }
  218. if (index >= max_part) {
  219. Error("no partitions with the given unique id available\n");
  220. goto bad;
  221. }
  222. }
  223. index = params->partition - 1;
  224. // GPT-specific code
  225. GptEntry *entry = GetEntry(&drive.gpt, PRIMARY, index);
  226. params->begin = entry->starting_lba;
  227. params->size = entry->ending_lba - entry->starting_lba + 1;
  228. memcpy(&params->type_guid, &entry->type, sizeof(Guid));
  229. memcpy(&params->unique_guid, &entry->unique, sizeof(Guid));
  230. params->raw_value = entry->attrs.fields.gpt_att;
  231. params->successful = GetSuccessful(&drive, PRIMARY, index);
  232. params->tries = GetTries(&drive, PRIMARY, index);
  233. params->priority = GetPriority(&drive, PRIMARY, index);
  234. result = CGPT_OK;
  235. bad:
  236. DriveClose(&drive, 0);
  237. return result;
  238. }
  239. static int GptAdd(struct drive *drive, CgptAddParams *params, uint32_t index) {
  240. GptEntry *entry, backup;
  241. int rv;
  242. entry = GetEntry(&drive->gpt, PRIMARY, index);
  243. memcpy(&backup, entry, sizeof(backup));
  244. if (SetEntryAttributes(drive, index, params) ||
  245. GptSetEntryAttributes(drive, index, params)) {
  246. memcpy(entry, &backup, sizeof(*entry));
  247. return -1;
  248. }
  249. UpdateAllEntries(drive);
  250. rv = CheckEntries((GptEntry*)drive->gpt.primary_entries,
  251. (GptHeader*)drive->gpt.primary_header);
  252. if (0 != rv) {
  253. // If the modified entry is illegal, recover it and return error.
  254. memcpy(entry, &backup, sizeof(*entry));
  255. Error("%s\n", GptErrorText(rv));
  256. Error(DumpCgptAddParams(params));
  257. return -1;
  258. }
  259. return 0;
  260. }
  261. int CgptAdd(CgptAddParams *params) {
  262. struct drive drive;
  263. uint32_t index;
  264. if (params == NULL)
  265. return CGPT_FAILED;
  266. if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
  267. params->drive_size))
  268. return CGPT_FAILED;
  269. if (CgptCheckAddValidity(&drive)) {
  270. goto bad;
  271. }
  272. if (CgptGetUnusedPartition(&drive, &index, params)) {
  273. goto bad;
  274. }
  275. if (GptAdd(&drive, params, index))
  276. goto bad;
  277. // Write it all out.
  278. return DriveClose(&drive, 1);
  279. bad:
  280. DriveClose(&drive, 0);
  281. return CGPT_FAILED;
  282. }