blob: cec607782d221f8318cc0553c4e7b3ad667eb041 [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
Bill Richardson18e03702014-06-23 17:48:33 -070012static int GptCreate(struct drive *drive, CgptCreateParams *params) {
Albert Chaulk92f22e72013-04-02 13:20:52 -070013 // Erase the data
14 memset(drive->gpt.primary_header, 0,
Nam T. Nguyen88458d92014-08-28 10:58:47 -070015 drive->gpt.sector_bytes * GPT_HEADER_SECTORS);
Albert Chaulk92f22e72013-04-02 13:20:52 -070016 memset(drive->gpt.secondary_header, 0,
Nam T. Nguyen88458d92014-08-28 10:58:47 -070017 drive->gpt.sector_bytes * GPT_HEADER_SECTORS);
Albert Chaulk92f22e72013-04-02 13:20:52 -070018 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);
Nam T. Nguyen88458d92014-08-28 10:58:47 -070032 h->my_lba = GPT_PMBR_SECTORS; /* The second sector on drive. */
33 h->alternate_lba = drive->gpt.drive_sectors - GPT_HEADER_SECTORS;
34 h->entries_lba = h->my_lba + GPT_HEADER_SECTORS + params->padding;
Nam T. Nguyena2d72f72014-08-22 15:01:38 -070035 h->first_usable_lba = h->entries_lba + GPT_ENTRIES_SECTORS;
Nam T. Nguyen88458d92014-08-28 10:58:47 -070036 h->last_usable_lba = (drive->gpt.drive_sectors - GPT_HEADER_SECTORS -
Nam T. Nguyena2d72f72014-08-22 15:01:38 -070037 GPT_ENTRIES_SECTORS - 1);
Bill Richardson4cb54972014-06-20 14:33:00 -070038 if (CGPT_OK != GenerateGuid(&h->disk_uuid)) {
39 Error("Unable to generate new GUID.\n");
Albert Chaulk92f22e72013-04-02 13:20:52 -070040 return -1;
41 }
Albert Chaulk92f22e72013-04-02 13:20:52 -070042 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
Bill Richardson18e03702014-06-23 17:48:33 -070054static int MtdCreate(struct drive *drive, CgptCreateParams *params) {
Albert Chaulk92f22e72013-04-02 13:20:52 -070055 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 }
Albert Chaulk494646d2013-07-19 12:56:38 -070067 if (params->size) {
68 h->last_offset = params->size - 1;
69 drive->size = params->size;
70 drive->mtd.drive_sectors = drive->size / drive->mtd.sector_bytes;
71 } else if (!drive->mtd.drive_sectors) {
72 Error("MTD create with params->size == 0 && drive->mtd.drive_sectors == 0");
73 return -1;
74 }
Albert Chaulk92f22e72013-04-02 13:20:52 -070075
76 return 0;
77}
78
Bill Richardson3f806a22013-03-20 15:02:34 -070079int CgptCreate(CgptCreateParams *params) {
Jay Srinivasana0581432012-01-26 21:50:05 -080080 struct drive drive;
81
82 if (params == NULL)
83 return CGPT_FAILED;
84
Bill Richardson23429d32012-04-30 11:33:13 -070085 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR))
Jay Srinivasana0581432012-01-26 21:50:05 -080086 return CGPT_FAILED;
87
Albert Chaulk92f22e72013-04-02 13:20:52 -070088 if (drive.is_mtd) {
89 if (MtdCreate(&drive, params))
Jay Srinivasan5fac7572012-02-23 10:59:01 -080090 goto bad;
Albert Chaulk92f22e72013-04-02 13:20:52 -070091 } else {
92 if (GptCreate(&drive, params))
93 goto bad;
Jay Srinivasana0581432012-01-26 21:50:05 -080094 }
95
96 // Write it all out
97 return DriveClose(&drive, 1);
Jay Srinivasan5fac7572012-02-23 10:59:01 -080098
99bad:
100
101 DriveClose(&drive, 0);
102 return CGPT_FAILED;
Jay Srinivasana0581432012-01-26 21:50:05 -0800103}