blob: b0256c5118a56a2c2bb8f214009f5f4c7adc4ddd [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 Richardson3f806a22013-03-20 15:02:34 -070012int CgptCreate(CgptCreateParams *params) {
Jay Srinivasana0581432012-01-26 21:50:05 -080013 struct drive drive;
14
15 if (params == NULL)
16 return CGPT_FAILED;
17
Bill Richardson23429d32012-04-30 11:33:13 -070018 if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR))
Jay Srinivasana0581432012-01-26 21:50:05 -080019 return CGPT_FAILED;
20
21 // Erase the data
22 memset(drive.gpt.primary_header, 0,
23 drive.gpt.sector_bytes * GPT_HEADER_SECTOR);
24 memset(drive.gpt.secondary_header, 0,
25 drive.gpt.sector_bytes * GPT_HEADER_SECTOR);
26 memset(drive.gpt.primary_entries, 0,
27 drive.gpt.sector_bytes * GPT_ENTRIES_SECTORS);
28 memset(drive.gpt.secondary_entries, 0,
29 drive.gpt.sector_bytes * GPT_ENTRIES_SECTORS);
30
31 drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 |
32 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2);
33
34 // Initialize a blank set
35 if (!params->zap)
36 {
37 GptHeader *h = (GptHeader *)drive.gpt.primary_header;
38 memcpy(h->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
39 h->revision = GPT_HEADER_REVISION;
40 h->size = sizeof(GptHeader);
41 h->my_lba = 1;
42 h->alternate_lba = drive.gpt.drive_sectors - 1;
43 h->first_usable_lba = 1 + 1 + GPT_ENTRIES_SECTORS;
44 h->last_usable_lba = drive.gpt.drive_sectors - 1 - GPT_ENTRIES_SECTORS - 1;
Jay Srinivasan5fac7572012-02-23 10:59:01 -080045 if (!uuid_generator) {
46 Error("Unable to generate new GUID. uuid_generator not set.\n");
47 goto bad;
48 }
49 (*uuid_generator)((uint8_t *)&h->disk_uuid);
Jay Srinivasana0581432012-01-26 21:50:05 -080050 h->entries_lba = 2;
51 h->number_of_entries = 128;
52 h->size_of_entry = sizeof(GptEntry);
53
54 // Copy to secondary
55 RepairHeader(&drive.gpt, MASK_PRIMARY);
56
57 UpdateCrc(&drive.gpt);
58 }
59
60 // Write it all out
61 return DriveClose(&drive, 1);
Jay Srinivasan5fac7572012-02-23 10:59:01 -080062
63bad:
64
65 DriveClose(&drive, 0);
66 return CGPT_FAILED;
Jay Srinivasana0581432012-01-26 21:50:05 -080067}