blob: 7d3c0595ee1b06dce6e00398d523811980b584af [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. */
47 h->alternate_lba = drive->gpt.drive_sectors - GPT_HEADER_SECTORS;
48 h->entries_lba = h->my_lba + GPT_HEADER_SECTORS + params->padding;
Nam T. Nguyena2d72f72014-08-22 15:01:38 -070049 h->first_usable_lba = h->entries_lba + GPT_ENTRIES_SECTORS;
Nam T. Nguyen88458d92014-08-28 10:58:47 -070050 h->last_usable_lba = (drive->gpt.drive_sectors - GPT_HEADER_SECTORS -
Nam T. Nguyena2d72f72014-08-22 15:01:38 -070051 GPT_ENTRIES_SECTORS - 1);
Bill Richardson4cb54972014-06-20 14:33:00 -070052 if (CGPT_OK != GenerateGuid(&h->disk_uuid)) {
53 Error("Unable to generate new GUID.\n");
Albert Chaulk92f22e72013-04-02 13:20:52 -070054 return -1;
55 }
Albert Chaulk92f22e72013-04-02 13:20:52 -070056 h->number_of_entries = 128;
57 h->size_of_entry = sizeof(GptEntry);
58
59 // Copy to secondary
60 RepairHeader(&drive->gpt, MASK_PRIMARY);
61
62 UpdateCrc(&drive->gpt);
63 }
64
65 return 0;
66}
67
Bill Richardson18e03702014-06-23 17:48:33 -070068static int MtdCreate(struct drive *drive, CgptCreateParams *params) {
Albert Chaulk92f22e72013-04-02 13:20:52 -070069 MtdDiskLayout *h = &drive->mtd.primary;
70 memset(h, 0, sizeof(*h));
71 drive->mtd.modified = 1;
72
73 if (!params->zap) {
74 // Prep basic parameters
75 memcpy(h->signature, MTD_DRIVE_SIGNATURE, sizeof(h->signature));
76 h->size = sizeof(*h);
Albert Chaulk289b6042013-06-25 11:30:46 -070077 h->first_offset = 0;
78 h->last_offset = (drive->mtd.drive_sectors * drive->mtd.sector_bytes) - 1;
Albert Chaulk92f22e72013-04-02 13:20:52 -070079 h->crc32 = MtdHeaderCrc(h);
80 }
Albert Chaulk494646d2013-07-19 12:56:38 -070081 if (params->size) {
82 h->last_offset = params->size - 1;
83 drive->size = params->size;
84 drive->mtd.drive_sectors = drive->size / drive->mtd.sector_bytes;
85 } else if (!drive->mtd.drive_sectors) {
86 Error("MTD create with params->size == 0 && drive->mtd.drive_sectors == 0");
87 return -1;
88 }
Albert Chaulk92f22e72013-04-02 13:20:52 -070089
90 return 0;
91}
92
Bill Richardson3f806a22013-03-20 15:02:34 -070093int CgptCreate(CgptCreateParams *params) {
Jay Srinivasana0581432012-01-26 21:50:05 -080094 struct drive drive;
95
96 if (params == NULL)
97 return CGPT_FAILED;
98
Bill Richardson23429d32012-04-30 11:33:13 -070099 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR))
Jay Srinivasana0581432012-01-26 21:50:05 -0800100 return CGPT_FAILED;
101
Albert Chaulk92f22e72013-04-02 13:20:52 -0700102 if (drive.is_mtd) {
103 if (MtdCreate(&drive, params))
Jay Srinivasan5fac7572012-02-23 10:59:01 -0800104 goto bad;
Albert Chaulk92f22e72013-04-02 13:20:52 -0700105 } else {
106 if (GptCreate(&drive, params))
107 goto bad;
Jay Srinivasana0581432012-01-26 21:50:05 -0800108 }
109
110 // Write it all out
111 return DriveClose(&drive, 1);
Jay Srinivasan5fac7572012-02-23 10:59:01 -0800112
113bad:
114
115 DriveClose(&drive, 0);
116 return CGPT_FAILED;
Jay Srinivasana0581432012-01-26 21:50:05 -0800117}