blob: eab6122566fa93a2c90d051e1c5f13c75d10290b [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
5#include "cgpt.h"
6
7#include <string.h>
8
9#include "cgptlib_internal.h"
10#include "cgpt_params.h"
11
12int cgpt_create(CgptCreateParams *params) {
13 struct drive drive;
14
15 if (params == NULL)
16 return CGPT_FAILED;
17
Jay Srinivasan250549d2012-02-16 17:40:45 -080018 if (CGPT_OK != DriveOpen(params->drive_name, &drive))
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;
45 uuid_generate((uint8_t *)&h->disk_uuid);
46 h->entries_lba = 2;
47 h->number_of_entries = 128;
48 h->size_of_entry = sizeof(GptEntry);
49
50 // Copy to secondary
51 RepairHeader(&drive.gpt, MASK_PRIMARY);
52
53 UpdateCrc(&drive.gpt);
54 }
55
56 // Write it all out
57 return DriveClose(&drive, 1);
58}