gpt_misc.h revision 7c2beb08380410ca6847abdac23e11ded2d1b625
1/* Copyright (c) 2013 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
6#ifndef VBOOT_REFERENCE_CGPT_MISC_H_
7#define VBOOT_REFERENCE_CGPT_MISC_H_
8
9#include "vboot_api.h"
10
11enum {
12	GPT_SUCCESS = 0,
13	GPT_ERROR_NO_VALID_KERNEL,
14	GPT_ERROR_INVALID_HEADERS,
15	GPT_ERROR_INVALID_ENTRIES,
16	GPT_ERROR_INVALID_SECTOR_SIZE,
17	GPT_ERROR_INVALID_SECTOR_NUMBER,
18	GPT_ERROR_INVALID_UPDATE_TYPE,
19	GPT_ERROR_CRC_CORRUPTED,
20	GPT_ERROR_OUT_OF_REGION,
21	GPT_ERROR_START_LBA_OVERLAP,
22	GPT_ERROR_END_LBA_OVERLAP,
23	GPT_ERROR_DUP_GUID,
24	GPT_ERROR_INVALID_FLASH_GEOMETRY,
25	GPT_ERROR_NO_SUCH_ENTRY,
26	/* Number of errors */
27	GPT_ERROR_COUNT
28};
29
30/* Bit masks for GptData.modified field. */
31#define GPT_MODIFIED_HEADER1 0x01
32#define GPT_MODIFIED_HEADER2 0x02
33#define GPT_MODIFIED_ENTRIES1 0x04
34#define GPT_MODIFIED_ENTRIES2 0x08
35
36/*
37 * Size of GptData.primary_entries and secondary_entries: 128 bytes/entry * 128
38 * entries.
39 */
40#define TOTAL_ENTRIES_SIZE 16384
41
42/*
43 * The 'update_type' of GptUpdateKernelEntry().  We expose TRY and BAD only
44 * because those are what verified boot needs.  For more precise control on GPT
45 * attribute bits, please refer to gpt_internal.h.
46 */
47enum {
48	/*
49	 * System will be trying to boot the currently selected kernel
50	 * partition.  Update its try count if necessary.
51	 */
52	GPT_UPDATE_ENTRY_TRY = 1,
53	/*
54	 * The currently selected kernel partition failed validation.  Mark
55	 * entry as invalid.
56	 */
57	GPT_UPDATE_ENTRY_BAD = 2,
58};
59
60typedef struct {
61	/* Fill in the following fields before calling GptInit() */
62	/* GPT primary header, from sector 1 of disk (size: 512 bytes) */
63	uint8_t *primary_header;
64	/* GPT secondary header, from last sector of disk (size: 512 bytes) */
65	uint8_t *secondary_header;
66	/* Primary GPT table, follows primary header (size: 16 KB) */
67	uint8_t *primary_entries;
68	/* Secondary GPT table, precedes secondary header (size: 16 KB) */
69	uint8_t *secondary_entries;
70	/* Size of a LBA sector, in bytes */
71	uint32_t sector_bytes;
72	/* Size of drive in LBA sectors, in sectors */
73	uint64_t drive_sectors;
74
75	/* Outputs */
76	/* Which inputs have been modified?  GPT_MODIFIED_* */
77	uint8_t modified;
78	/*
79	 * The current chromeos kernel index in partition table.  -1 means not
80	 * found on drive. Note that GPT partition numbers are traditionally
81	 * 1-based, but we're using a zero-based index here.
82	 */
83	int current_kernel;
84
85	/* Internal variables */
86	uint32_t valid_headers, valid_entries;
87	int current_priority;
88} GptData;
89
90/**
91 * Initializes the GPT data structure's internal state.
92 *
93 * The following fields must be filled before calling this function:
94 *
95 *   primary_header
96 *   secondary_header
97 *   primary_entries
98 *   secondary_entries
99 *   sector_bytes
100 *   drive_sectors
101 *
102 * On return the modified field may be set, if the GPT data has been modified
103 * and should be written to disk.
104 *
105 * Returns GPT_SUCCESS if successful, non-zero if error:
106 *   GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters
107 *                              recovery mode,
108 *   GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters
109 *                              recovery mode,
110 *   GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported,
111 *   GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too
112 *                                    small) */
113int GptInit(GptData *gpt);
114
115/**
116 * Allocate and read GPT data from the drive.  The sector_bytes and
117 * drive_sectors fields should be filled on input.  The primary and secondary
118 * header and entries are filled on output.
119 *
120 * Returns 0 if successful, 1 if error.
121 */
122int AllocAndReadGptData(VbExDiskHandle_t disk_handle, GptData *gptdata);
123
124/**
125 * Write any changes for the GPT data back to the drive, then free the buffers.
126 */
127int WriteAndFreeGptData(VbExDiskHandle_t disk_handle, GptData *gptdata);
128
129#endif  /* VBOOT_REFERENCE_CGPT_MISC_H_ */
130