blob: e7cbadf8f2d03f82ea4d46642dba5fd7d13545db [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
Albert Chaulk92f22e72013-04-02 13:20:52 -070012int GptCreate(struct drive *drive, CgptCreateParams *params) {
13 // Erase the data
14 memset(drive->gpt.primary_header, 0,
15 drive->gpt.sector_bytes * GPT_HEADER_SECTOR);
16 memset(drive->gpt.secondary_header, 0,
17 drive->gpt.sector_bytes * GPT_HEADER_SECTOR);
18 memset(drive->gpt.primary_entries, 0,
19 drive->gpt.sector_bytes * GPT_ENTRIES_SECTORS);
20 memset(drive->gpt.secondary_entries, 0,
21 drive->gpt.sector_bytes * GPT_ENTRIES_SECTORS);
22
23 drive->gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
24 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2);
25
26 // Initialize a blank set
27 if (!params->zap) {
28 GptHeader *h = (GptHeader *)drive->gpt.primary_header;
29 memcpy(h->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
30 h->revision = GPT_HEADER_REVISION;
31 h->size = sizeof(GptHeader);
32 h->my_lba = 1;
33 h->alternate_lba = drive->gpt.drive_sectors - 1;
34 h->first_usable_lba = 1 + 1 + GPT_ENTRIES_SECTORS;
35 h->last_usable_lba = drive->gpt.drive_sectors - 1 - GPT_ENTRIES_SECTORS - 1;
36 if (!uuid_generator) {
37 Error("Unable to generate new GUID. uuid_generator not set.\n");
38 return -1;
39 }
40 (*uuid_generator)((uint8_t *)&h->disk_uuid);
41 h->entries_lba = 2;
42 h->number_of_entries = 128;
43 h->size_of_entry = sizeof(GptEntry);
44
45 // Copy to secondary
46 RepairHeader(&drive->gpt, MASK_PRIMARY);
47
48 UpdateCrc(&drive->gpt);
49 }
50
51 return 0;
52}
53
54int MtdCreate(struct drive *drive, CgptCreateParams *params) {
55 MtdDiskLayout *h = &drive->mtd.primary;
56 memset(h, 0, sizeof(*h));
57 drive->mtd.modified = 1;
58
59 if (!params->zap) {
60 // Prep basic parameters
61 memcpy(h->signature, MTD_DRIVE_SIGNATURE, sizeof(h->signature));
62 h->size = sizeof(*h);
Albert Chaulk289b6042013-06-25 11:30:46 -070063 h->first_offset = 0;
64 h->last_offset = (drive->mtd.drive_sectors * drive->mtd.sector_bytes) - 1;
Albert Chaulk92f22e72013-04-02 13:20:52 -070065 h->crc32 = MtdHeaderCrc(h);
66 }
67
68 return 0;
69}
70
Bill Richardson3f806a22013-03-20 15:02:34 -070071int CgptCreate(CgptCreateParams *params) {
Jay Srinivasana0581432012-01-26 21:50:05 -080072 struct drive drive;
73
74 if (params == NULL)
75 return CGPT_FAILED;
76
Bill Richardson23429d32012-04-30 11:33:13 -070077 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR))
Jay Srinivasana0581432012-01-26 21:50:05 -080078 return CGPT_FAILED;
79
Albert Chaulk92f22e72013-04-02 13:20:52 -070080 if (drive.is_mtd) {
81 if (MtdCreate(&drive, params))
Jay Srinivasan5fac7572012-02-23 10:59:01 -080082 goto bad;
Albert Chaulk92f22e72013-04-02 13:20:52 -070083 } else {
84 if (GptCreate(&drive, params))
85 goto bad;
Jay Srinivasana0581432012-01-26 21:50:05 -080086 }
87
88 // Write it all out
89 return DriveClose(&drive, 1);
Jay Srinivasan5fac7572012-02-23 10:59:01 -080090
91bad:
92
93 DriveClose(&drive, 0);
94 return CGPT_FAILED;
Jay Srinivasana0581432012-01-26 21:50:05 -080095}