| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include "cgpt.h" |
| 9 |
#include "cgptlib_internal.h" |
| 10 |
#include "vboot_host.h" |
| 11 |
|
| 12 |
static void AllocAndClear(uint8_t **buf, uint64_t size) { |
| 13 |
if (*buf) { |
| 14 |
memset(*buf, 0, size); |
| 15 |
} else { |
| 16 |
*buf = calloc(1, size); |
| 17 |
if (!*buf) { |
| 18 |
Error("Cannot allocate %" PRIu64 " bytes.\n", size); |
| 19 |
abort(); |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
static int GptCreate(struct drive *drive, CgptCreateParams *params) { |
| 25 |
|
| 26 |
if (!memcmp(((GptHeader*)drive->gpt.primary_header)->signature, |
| 27 |
GPT_HEADER_SIGNATURE_IGNORED, GPT_HEADER_SIGNATURE_SIZE)) { |
| 28 |
drive->gpt.ignored |= MASK_PRIMARY; |
| 29 |
Warning("Primary GPT was marked ignored, will not overwrite.\n"); |
| 30 |
} |
| 31 |
|
| 32 |
if (!memcmp(((GptHeader*)drive->gpt.secondary_header)->signature, |
| 33 |
GPT_HEADER_SIGNATURE_IGNORED, GPT_HEADER_SIGNATURE_SIZE)) { |
| 34 |
drive->gpt.ignored |= MASK_SECONDARY; |
| 35 |
Warning("Secondary GPT was marked ignored, will not overwrite.\n"); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
AllocAndClear(&drive->gpt.primary_header, |
| 42 |
drive->gpt.sector_bytes * GPT_HEADER_SECTORS); |
| 43 |
AllocAndClear(&drive->gpt.secondary_header, |
| 44 |
drive->gpt.sector_bytes * GPT_HEADER_SECTORS); |
| 45 |
|
| 46 |
drive->gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 | |
| 47 |
GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2); |
| 48 |
|
| 49 |
|
| 50 |
if (!params->zap) { |
| 51 |
GptHeader *h = (GptHeader *)drive->gpt.primary_header; |
| 52 |
memcpy(h->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE); |
| 53 |
h->revision = GPT_HEADER_REVISION; |
| 54 |
h->size = sizeof(GptHeader); |
| 55 |
h->my_lba = GPT_PMBR_SECTORS; |
| 56 |
h->alternate_lba = drive->gpt.gpt_drive_sectors - GPT_HEADER_SECTORS; |
| 57 |
if (CGPT_OK != GenerateGuid(&h->disk_uuid)) { |
| 58 |
Error("Unable to generate new GUID.\n"); |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
h->size_of_entry = sizeof(GptEntry); |
| 64 |
h->number_of_entries = MAX_NUMBER_OF_ENTRIES; |
| 65 |
if (drive->gpt.flags & GPT_FLAG_EXTERNAL) { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
size_t required_headers_size = |
| 76 |
(GPT_PMBR_SECTORS + GPT_HEADER_SECTORS) * drive->gpt.sector_bytes; |
| 77 |
size_t min_entries_size = MIN_NUMBER_OF_ENTRIES * h->size_of_entry; |
| 78 |
size_t required_min_size = required_headers_size + min_entries_size; |
| 79 |
size_t half_size = |
| 80 |
(drive->gpt.gpt_drive_sectors / 2) * drive->gpt.sector_bytes; |
| 81 |
if (half_size < required_min_size) { |
| 82 |
Error("Not enough space to store GPT structures. Required %zu bytes.\n", |
| 83 |
required_min_size * 2); |
| 84 |
return -1; |
| 85 |
} |
| 86 |
size_t max_entries = |
| 87 |
(half_size - required_headers_size) / h->size_of_entry; |
| 88 |
if (h->number_of_entries > max_entries) { |
| 89 |
h->number_of_entries = max_entries; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
h->entries_lba = h->my_lba + GPT_HEADER_SECTORS; |
| 95 |
if (!(drive->gpt.flags & GPT_FLAG_EXTERNAL)) { |
| 96 |
h->entries_lba += params->padding; |
| 97 |
h->first_usable_lba = h->entries_lba + CalculateEntriesSectors(h, |
| 98 |
drive->gpt.sector_bytes); |
| 99 |
h->last_usable_lba = |
| 100 |
(drive->gpt.streaming_drive_sectors - GPT_HEADER_SECTORS - |
| 101 |
CalculateEntriesSectors(h, drive->gpt.sector_bytes) - 1); |
| 102 |
} else { |
| 103 |
h->first_usable_lba = params->padding; |
| 104 |
h->last_usable_lba = (drive->gpt.streaming_drive_sectors - 1); |
| 105 |
} |
| 106 |
|
| 107 |
size_t entries_size = h->number_of_entries * h->size_of_entry; |
| 108 |
AllocAndClear(&drive->gpt.primary_entries, entries_size); |
| 109 |
AllocAndClear(&drive->gpt.secondary_entries, entries_size); |
| 110 |
|
| 111 |
|
| 112 |
RepairHeader(&drive->gpt, MASK_PRIMARY); |
| 113 |
|
| 114 |
UpdateCrc(&drive->gpt); |
| 115 |
} |
| 116 |
|
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
int CgptCreate(CgptCreateParams *params) { |
| 121 |
struct drive drive; |
| 122 |
|
| 123 |
if (params == NULL) |
| 124 |
return CGPT_FAILED; |
| 125 |
|
| 126 |
if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR, |
| 127 |
params->drive_size)) |
| 128 |
return CGPT_FAILED; |
| 129 |
|
| 130 |
if (GptCreate(&drive, params)) |
| 131 |
goto bad; |
| 132 |
|
| 133 |
|
| 134 |
return DriveClose(&drive, 1); |
| 135 |
|
| 136 |
bad: |
| 137 |
|
| 138 |
DriveClose(&drive, 0); |
| 139 |
return CGPT_FAILED; |
| 140 |
} |