blob: 3341fa2557f0bb7f5d20994bbed98e69b63500ca [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;
Bill Richardson4cb54972014-06-20 14:33:00 -070036 if (CGPT_OK != GenerateGuid(&h->disk_uuid)) {
37 Error("Unable to generate new GUID.\n");
Albert Chaulk92f22e72013-04-02 13:20:52 -070038 return -1;
39 }
Albert Chaulk92f22e72013-04-02 13:20:52 -070040 h->entries_lba = 2;
41 h->number_of_entries = 128;
42 h->size_of_entry = sizeof(GptEntry);
43
44 // Copy to secondary
45 RepairHeader(&drive->gpt, MASK_PRIMARY);
46
47 UpdateCrc(&drive->gpt);
48 }
49
50 return 0;
51}
52
53int MtdCreate(struct drive *drive, CgptCreateParams *params) {
54 MtdDiskLayout *h = &drive->mtd.primary;
55 memset(h, 0, sizeof(*h));
56 drive->mtd.modified = 1;
57
58 if (!params->zap) {
59 // Prep basic parameters
60 memcpy(h->signature, MTD_DRIVE_SIGNATURE, sizeof(h->signature));
61 h->size = sizeof(*h);
Albert Chaulk289b6042013-06-25 11:30:46 -070062 h->first_offset = 0;
63 h->last_offset = (drive->mtd.drive_sectors * drive->mtd.sector_bytes) - 1;
Albert Chaulk92f22e72013-04-02 13:20:52 -070064 h->crc32 = MtdHeaderCrc(h);
65 }
Albert Chaulk494646d2013-07-19 12:56:38 -070066 if (params->size) {
67 h->last_offset = params->size - 1;
68 drive->size = params->size;
69 drive->mtd.drive_sectors = drive->size / drive->mtd.sector_bytes;
70 } else if (!drive->mtd.drive_sectors) {
71 Error("MTD create with params->size == 0 && drive->mtd.drive_sectors == 0");
72 return -1;
73 }
Albert Chaulk92f22e72013-04-02 13:20:52 -070074
75 return 0;
76}
77
Bill Richardson3f806a22013-03-20 15:02:34 -070078int CgptCreate(CgptCreateParams *params) {
Jay Srinivasana0581432012-01-26 21:50:05 -080079 struct drive drive;
80
81 if (params == NULL)
82 return CGPT_FAILED;
83
Bill Richardson23429d32012-04-30 11:33:13 -070084 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR))
Jay Srinivasana0581432012-01-26 21:50:05 -080085 return CGPT_FAILED;
86
Albert Chaulk92f22e72013-04-02 13:20:52 -070087 if (drive.is_mtd) {
88 if (MtdCreate(&drive, params))
Jay Srinivasan5fac7572012-02-23 10:59:01 -080089 goto bad;
Albert Chaulk92f22e72013-04-02 13:20:52 -070090 } else {
91 if (GptCreate(&drive, params))
92 goto bad;
Jay Srinivasana0581432012-01-26 21:50:05 -080093 }
94
95 // Write it all out
96 return DriveClose(&drive, 1);
Jay Srinivasan5fac7572012-02-23 10:59:01 -080097
98bad:
99
100 DriveClose(&drive, 0);
101 return CGPT_FAILED;
Jay Srinivasana0581432012-01-26 21:50:05 -0800102}