1 /* Copyright 2012 The ChromiumOS Authors
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <string.h>
7
8 #include "cgpt.h"
9 #include "cgptlib_internal.h"
10 #include "vboot_host.h"
11
AllocAndClear(uint8_t ** buf,uint64_t size)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
GptCreate(struct drive * drive,CgptCreateParams * params)24 static int GptCreate(struct drive *drive, CgptCreateParams *params) {
25 // Do not replace any existing IGNOREME GPT headers.
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 // Allocate and/or erase the data.
39 // We cannot assume the GPT headers or entry arrays have been allocated
40 // by GptLoad() because those fields might have failed validation checks.
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 // Initialize a blank set
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; /* The second sector on drive. */
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 /* Calculate number of entries */
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 // We might have smaller space for the GPT table. Scale accordingly.
67 //
68 // +------+------------+---------------+-----+--------------+-----------+
69 // | PMBR | Prim. Head | Prim. Entries | ... | Sec. Entries | Sec. Head |
70 // +------+------------+---------------+-----+--------------+-----------+
71 //
72 // Half the size of gpt_drive_sectors must be big enough to hold PMBR +
73 // GPT Header + Entries Table, though the secondary structures do not
74 // contain PMBR.
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 /* Then use number of entries to calculate entries_lba. */
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 // Copy to secondary
112 RepairHeader(&drive->gpt, MASK_PRIMARY);
113
114 UpdateCrc(&drive->gpt);
115 }
116
117 return 0;
118 }
119
CgptCreate(CgptCreateParams * params)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 // Write it all out
134 return DriveClose(&drive, 1);
135
136 bad:
137
138 DriveClose(&drive, 0);
139 return CGPT_FAILED;
140 }
141