Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 1 | // 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 Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 5 | |
| 6 | #include <string.h> |
| 7 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame^] | 8 | #include "cgpt.h" |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 9 | #include "cgptlib_internal.h" |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame^] | 10 | #include "vboot_host.h" |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 11 | |
Bill Richardson | 3f806a2 | 2013-03-20 15:02:34 -0700 | [diff] [blame] | 12 | int CgptCreate(CgptCreateParams *params) { |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 13 | struct drive drive; |
| 14 | |
| 15 | if (params == NULL) |
| 16 | return CGPT_FAILED; |
| 17 | |
Bill Richardson | 23429d3 | 2012-04-30 11:33:13 -0700 | [diff] [blame] | 18 | if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDWR)) |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 19 | 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 Srinivasan | 5fac757 | 2012-02-23 10:59:01 -0800 | [diff] [blame] | 45 | 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 Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 50 | 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 Srinivasan | 5fac757 | 2012-02-23 10:59:01 -0800 | [diff] [blame] | 62 | |
| 63 | bad: |
| 64 | |
| 65 | DriveClose(&drive, 0); |
| 66 | return CGPT_FAILED; |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame] | 67 | } |