cgpt_create.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "cgptlib_internal.h"
  7. #include "vboot_host.h"
  8. static void AllocAndClear(uint8_t **buf, uint64_t size) {
  9. if (*buf) {
  10. memset(*buf, 0, size);
  11. } else {
  12. *buf = calloc(1, size);
  13. if (!*buf) {
  14. Error("Cannot allocate %u bytes.\n", size);
  15. abort();
  16. }
  17. }
  18. }
  19. static int GptCreate(struct drive *drive, CgptCreateParams *params) {
  20. // Allocate and/or erase the data.
  21. // We cannot assume the GPT headers or entry arrays have been allocated
  22. // by GptLoad() because those fields might have failed validation checks.
  23. AllocAndClear(&drive->gpt.primary_header,
  24. drive->gpt.sector_bytes * GPT_HEADER_SECTORS);
  25. AllocAndClear(&drive->gpt.secondary_header,
  26. drive->gpt.sector_bytes * GPT_HEADER_SECTORS);
  27. drive->gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
  28. GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2);
  29. // Initialize a blank set
  30. if (!params->zap) {
  31. GptHeader *h = (GptHeader *)drive->gpt.primary_header;
  32. memcpy(h->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
  33. h->revision = GPT_HEADER_REVISION;
  34. h->size = sizeof(GptHeader);
  35. h->my_lba = GPT_PMBR_SECTORS; /* The second sector on drive. */
  36. h->alternate_lba = drive->gpt.gpt_drive_sectors - GPT_HEADER_SECTORS;
  37. if (CGPT_OK != GenerateGuid(&h->disk_uuid)) {
  38. Error("Unable to generate new GUID.\n");
  39. return -1;
  40. }
  41. /* Calculate number of entries */
  42. h->size_of_entry = sizeof(GptEntry);
  43. h->number_of_entries = MAX_NUMBER_OF_ENTRIES;
  44. if (drive->gpt.flags & GPT_FLAG_EXTERNAL) {
  45. // We might have smaller space for the GPT table. Scale accordingly.
  46. //
  47. // +------+------------+---------------+-----+--------------+-----------+
  48. // | PMBR | Prim. Head | Prim. Entries | ... | Sec. Entries | Sec. Head |
  49. // +------+------------+---------------+-----+--------------+-----------+
  50. //
  51. // Half the size of gpt_drive_sectors must be big enough to hold PMBR +
  52. // GPT Header + Entries Table, though the secondary structures do not
  53. // contain PMBR.
  54. size_t required_headers_size =
  55. (GPT_PMBR_SECTORS + GPT_HEADER_SECTORS) * drive->gpt.sector_bytes;
  56. size_t min_entries_size = MIN_NUMBER_OF_ENTRIES * h->size_of_entry;
  57. size_t required_min_size = required_headers_size + min_entries_size;
  58. size_t half_size =
  59. (drive->gpt.gpt_drive_sectors / 2) * drive->gpt.sector_bytes;
  60. if (half_size < required_min_size) {
  61. Error("Not enough space to store GPT structures. Required %d bytes.\n",
  62. required_min_size * 2);
  63. return -1;
  64. }
  65. size_t max_entries =
  66. (half_size - required_headers_size) / h->size_of_entry;
  67. if (h->number_of_entries > max_entries) {
  68. h->number_of_entries = max_entries;
  69. }
  70. }
  71. /* Then use number of entries to calculate entries_lba. */
  72. h->entries_lba = h->my_lba + GPT_HEADER_SECTORS;
  73. if (!(drive->gpt.flags & GPT_FLAG_EXTERNAL)) {
  74. h->entries_lba += params->padding;
  75. h->first_usable_lba = h->entries_lba + CalculateEntriesSectors(h);
  76. h->last_usable_lba = (drive->gpt.streaming_drive_sectors - GPT_HEADER_SECTORS -
  77. CalculateEntriesSectors(h) - 1);
  78. } else {
  79. h->first_usable_lba = params->padding;
  80. h->last_usable_lba = (drive->gpt.streaming_drive_sectors - 1);
  81. }
  82. size_t entries_size = h->number_of_entries * h->size_of_entry;
  83. AllocAndClear(&drive->gpt.primary_entries, entries_size);
  84. AllocAndClear(&drive->gpt.secondary_entries, entries_size);
  85. // Copy to secondary
  86. RepairHeader(&drive->gpt, MASK_PRIMARY);
  87. UpdateCrc(&drive->gpt);
  88. }
  89. return 0;
  90. }
  91. int CgptCreate(CgptCreateParams *params) {
  92. struct drive drive;
  93. if (params == NULL)
  94. return CGPT_FAILED;
  95. if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
  96. params->drive_size))
  97. return CGPT_FAILED;
  98. if (GptCreate(&drive, params))
  99. goto bad;
  100. // Write it all out
  101. return DriveClose(&drive, 1);
  102. bad:
  103. DriveClose(&drive, 0);
  104. return CGPT_FAILED;
  105. }