blob: 2265c8f96c62ad2aa2bce5e007a989e6ce16cb60 [file] [log] [blame]
Jay Srinivasana0581432012-01-26 21:50:05 -08001// 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
Jay Srinivasana0581432012-01-26 21:50:05 -08005
6#include <string.h>
7
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include "cgpt.h"
Jay Srinivasana0581432012-01-26 21:50:05 -08009#include "cgptlib_internal.h"
Bill Richardson0c3ba242013-03-29 11:09:30 -070010#include "vboot_host.h"
Jay Srinivasana0581432012-01-26 21:50:05 -080011
Nam T. Nguyend92856d2014-10-16 15:02:40 -070012static 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 %u bytes.\n", size);
19 abort();
20 }
21 }
22}
23
Bill Richardson18e03702014-06-23 17:48:33 -070024static int GptCreate(struct drive *drive, CgptCreateParams *params) {
Nam T. Nguyend92856d2014-10-16 15:02:40 -070025 // Allocate and/or erase the data.
26 // We cannot assume the GPT headers or entry arrays have been allocated
27 // by GptLoad() because those fields might have failed validation checks.
28 AllocAndClear(&drive->gpt.primary_header,
29 drive->gpt.sector_bytes * GPT_HEADER_SECTORS);
30 AllocAndClear(&drive->gpt.secondary_header,
31 drive->gpt.sector_bytes * GPT_HEADER_SECTORS);
32 AllocAndClear(&drive->gpt.primary_entries,
33 drive->gpt.sector_bytes * GPT_ENTRIES_SECTORS);
34 AllocAndClear(&drive->gpt.secondary_entries,
35 drive->gpt.sector_bytes * GPT_ENTRIES_SECTORS);
Albert Chaulk92f22e72013-04-02 13:20:52 -070036
37 drive->gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
38 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2);
39
40 // Initialize a blank set
41 if (!params->zap) {
42 GptHeader *h = (GptHeader *)drive->gpt.primary_header;
43 memcpy(h->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
44 h->revision = GPT_HEADER_REVISION;
45 h->size = sizeof(GptHeader);
Nam T. Nguyen88458d92014-08-28 10:58:47 -070046 h->my_lba = GPT_PMBR_SECTORS; /* The second sector on drive. */
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070047 h->alternate_lba = drive->gpt.gpt_drive_sectors - GPT_HEADER_SECTORS;
48 h->entries_lba = h->my_lba + GPT_HEADER_SECTORS;
Dan Ehrenbergb3d38f52014-12-09 13:42:15 -080049 if (!(drive->gpt.flags & GPT_FLAG_EXTERNAL)) {
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070050 h->entries_lba += params->padding;
51 h->first_usable_lba = h->entries_lba + GPT_ENTRIES_SECTORS;
Dan Ehrenbergb3d38f52014-12-09 13:42:15 -080052 h->last_usable_lba = (drive->gpt.streaming_drive_sectors -
53 GPT_HEADER_SECTORS -
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070054 GPT_ENTRIES_SECTORS - 1);
55 } else {
Nam T. Nguyenab899592014-11-13 19:30:46 -080056 h->first_usable_lba = params->padding;
Dan Ehrenbergb3d38f52014-12-09 13:42:15 -080057 h->last_usable_lba = (drive->gpt.streaming_drive_sectors - 1);
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070058 }
Bill Richardson4cb54972014-06-20 14:33:00 -070059 if (CGPT_OK != GenerateGuid(&h->disk_uuid)) {
60 Error("Unable to generate new GUID.\n");
Albert Chaulk92f22e72013-04-02 13:20:52 -070061 return -1;
62 }
Albert Chaulk92f22e72013-04-02 13:20:52 -070063 h->size_of_entry = sizeof(GptEntry);
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070064 h->number_of_entries = TOTAL_ENTRIES_SIZE / h->size_of_entry;
Dan Ehrenbergb3d38f52014-12-09 13:42:15 -080065 if (drive->gpt.flags & GPT_FLAG_EXTERNAL) {
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070066 // We might have smaller space for the GPT table. Scale accordingly.
Nam T. Nguyenab899592014-11-13 19:30:46 -080067 size_t half_size_sectors = drive->gpt.gpt_drive_sectors / 2;
68 if (half_size_sectors < GPT_HEADER_SECTORS) {
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070069 Error("Not enough space for a GPT header.\n");
70 return -1;
71 }
Nam T. Nguyenab899592014-11-13 19:30:46 -080072 half_size_sectors -= GPT_HEADER_SECTORS;
73 size_t half_size = half_size_sectors * drive->gpt.sector_bytes;
74 if (half_size < (MIN_NUMBER_OF_ENTRIES * h->size_of_entry)) {
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070075 Error("Not enough space for minimum number of entries.\n");
76 return -1;
77 }
Nam T. Nguyenab899592014-11-13 19:30:46 -080078 if (128 > (half_size / h->size_of_entry)) {
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070079 h->number_of_entries = half_size / h->size_of_entry;
80 }
81 }
Albert Chaulk92f22e72013-04-02 13:20:52 -070082
83 // Copy to secondary
84 RepairHeader(&drive->gpt, MASK_PRIMARY);
85
86 UpdateCrc(&drive->gpt);
87 }
88
89 return 0;
90}
91
Bill Richardson3f806a22013-03-20 15:02:34 -070092int CgptCreate(CgptCreateParams *params) {
Jay Srinivasana0581432012-01-26 21:50:05 -080093 struct drive drive;
94
95 if (params == NULL)
96 return CGPT_FAILED;
97
Nam T. Nguyenab899592014-11-13 19:30:46 -080098 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR,
99 params->drive_size))
Jay Srinivasana0581432012-01-26 21:50:05 -0800100 return CGPT_FAILED;
101
Nam T. Nguyen8577b532014-11-25 13:26:53 -0800102 if (GptCreate(&drive, params))
103 goto bad;
Jay Srinivasana0581432012-01-26 21:50:05 -0800104
105 // Write it all out
106 return DriveClose(&drive, 1);
Jay Srinivasan5fac7572012-02-23 10:59:01 -0800107
108bad:
109
110 DriveClose(&drive, 0);
111 return CGPT_FAILED;
Jay Srinivasana0581432012-01-26 21:50:05 -0800112}