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 "gpt.h"
10#include "vboot_api.h"
11
12enum {
13	GPT_SUCCESS = 0,
14	GPT_ERROR_NO_VALID_KERNEL,
15	GPT_ERROR_INVALID_HEADERS,
16	GPT_ERROR_INVALID_ENTRIES,
17	GPT_ERROR_INVALID_SECTOR_SIZE,
18	GPT_ERROR_INVALID_SECTOR_NUMBER,
19	GPT_ERROR_INVALID_UPDATE_TYPE,
20	GPT_ERROR_CRC_CORRUPTED,
21	GPT_ERROR_OUT_OF_REGION,
22	GPT_ERROR_START_LBA_OVERLAP,
23	GPT_ERROR_END_LBA_OVERLAP,
24	GPT_ERROR_DUP_GUID,
25	GPT_ERROR_INVALID_FLASH_GEOMETRY,
26	GPT_ERROR_NO_SUCH_ENTRY,
27	/* Number of errors */
28	GPT_ERROR_COUNT
29};
30
31/* Bit masks for GptData.modified field. */
32#define GPT_MODIFIED_HEADER1 0x01
33#define GPT_MODIFIED_HEADER2 0x02
34#define GPT_MODIFIED_ENTRIES1 0x04
35#define GPT_MODIFIED_ENTRIES2 0x08
36
37/*
38 * The 'update_type' of GptUpdateKernelEntry().  We expose TRY and BAD only
39 * because those are what verified boot needs.  For more precise control on GPT
40 * attribute bits, please refer to gpt_internal.h.
41 */
42enum {
43	/*
44	 * System will be trying to boot the currently selected kernel
45	 * partition.  Update its try count if necessary.
46	 */
47	GPT_UPDATE_ENTRY_TRY = 1,
48	/*
49	 * The currently selected kernel partition failed validation.  Mark
50	 * entry as invalid.
51	 */
52	GPT_UPDATE_ENTRY_BAD = 2,
53	/*
54	 * Used for fastboot mode. When an image is written to kernel partition,
55	 * its GPT entry is marked with S1,P1,T15.
56	 */
57	GPT_UPDATE_ENTRY_RESET = 3,
58	/*
59	 * Used for fastboot mode. When an image is written to kernel partition,
60	 * its GPT entry is marked with S0,P0,T0.
61	 */
62	GPT_UPDATE_ENTRY_INVALID = 4,
63};
64
65/* If this bit is 1, the GPT is stored in another from the streaming data */
66#define GPT_FLAG_EXTERNAL	0x1
67
68/*
69 * A note about stored_on_device and gpt_drive_sectors:
70 *
71 * This code is used by both the "cgpt" utility and depthcharge/vboot. ATM,
72 * depthcharge does not have logic to properly setup stored_on_device and
73 * gpt_drive_sectors, but it does do a memset(gpt, 0, sizeof(GptData)). And so,
74 * GPT_STORED_ON_DEVICE should be 0 to make stored_on_device compatible with
75 * present behavior. At the same time, in vboot_kernel:LoadKernel(), and
76 * cgpt_common:GptLoad(), we need to have simple shims to set gpt_drive_sectors
77 * to drive_sectors.
78 *
79 * TODO(namnguyen): Remove those shims when the firmware can set these fields.
80 */
81typedef struct {
82	/* Fill in the following fields before calling GptInit() */
83	/* GPT primary header, from sector 1 of disk (size: 512 bytes) */
84	uint8_t *primary_header;
85	/* GPT secondary header, from last sector of disk (size: 512 bytes) */
86	uint8_t *secondary_header;
87	/* Primary GPT table, follows primary header (size: 16 KB) */
88	uint8_t *primary_entries;
89	/* Secondary GPT table, precedes secondary header (size: 16 KB) */
90	uint8_t *secondary_entries;
91	/* Size of a LBA sector, in bytes */
92	uint32_t sector_bytes;
93	/* Size of drive (that the partitions are on) in LBA sectors */
94	uint64_t streaming_drive_sectors;
95	/* Size of the device that holds the GPT structures, 512-byte sectors */
96	uint64_t gpt_drive_sectors;
97	/* Flags */
98	uint32_t flags;
99
100	/* Outputs */
101	/* Which inputs have been modified?  GPT_MODIFIED_* */
102	uint8_t modified;
103	/*
104	 * The current chromeos kernel index in partition table.  -1 means not
105	 * found on drive. Note that GPT partition numbers are traditionally
106	 * 1-based, but we're using a zero-based index here.
107	 */
108	int current_kernel;
109
110	/* Internal variables */
111	uint32_t valid_headers, valid_entries;
112	int current_priority;
113} GptData;
114
115/**
116 * Initializes the GPT data structure's internal state.
117 *
118 * The following fields must be filled before calling this function:
119 *
120 *   primary_header
121 *   secondary_header
122 *   primary_entries
123 *   secondary_entries
124 *   sector_bytes
125 *   drive_sectors
126 *   stored_on_device
127 *   gpt_device_sectors
128 *
129 * On return the modified field may be set, if the GPT data has been modified
130 * and should be written to disk.
131 *
132 * Returns GPT_SUCCESS if successful, non-zero if error:
133 *   GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters
134 *                              recovery mode,
135 *   GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters
136 *                              recovery mode,
137 *   GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported,
138 *   GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too
139 *                                    small) */
140int GptInit(GptData *gpt);
141
142/**
143 * Return the nth instance of parition entry matching the partition type guid
144 * from the gpt table. Instance value starts from 0. If the entry is not found,
145 * it returns NULL.
146 */
147GptEntry *GptFindNthEntry(GptData *gpt, const Guid *guid, unsigned int n);
148
149/**
150 * Allocate and read GPT data from the drive.  The sector_bytes and
151 * drive_sectors fields should be filled on input.  The primary and secondary
152 * header and entries are filled on output.
153 *
154 * Returns 0 if successful, 1 if error.
155 */
156int AllocAndReadGptData(VbExDiskHandle_t disk_handle, GptData *gptdata);
157
158/**
159 * Write any changes for the GPT data back to the drive, then free the buffers.
160 */
161int WriteAndFreeGptData(VbExDiskHandle_t disk_handle, GptData *gptdata);
162
163/**
164 * Return 1 if the entry is unused, 0 if it is used.
165 */
166int IsUnusedEntry(const GptEntry *e);
167
168/**
169 * Return size(in lba) of a partition represented by given GPT entry.
170 */
171size_t GptGetEntrySizeLba(const GptEntry *e);
172
173/**
174 * Return size(in bytes) of a partition represented by given GPT entry.
175 */
176size_t GptGetEntrySizeBytes(const GptData *gpt, const GptEntry *e);
177
178/**
179 * Updates the kernel entry with the specified index, using the specified type
180 * of update (GPT_UPDATE_ENTRY_*).
181 *
182 * On return the modified field may be set, if the GPT data has been modified
183 * and should be written to disk.
184 *
185 * Returns GPT_SUCCESS if successful, else
186 *   GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given.
187 */
188int GptUpdateKernelWithEntry(GptData *gpt, GptEntry *e, uint32_t update_type);
189
190/**
191 * Updates the kernel entry identified by current_kernel field. If
192 * current_kernel is not set it returns an error.
193 *
194 * Returns GPT_SUCCESS if successful, else
195 *   GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given.
196 */
197int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type);
198
199#endif  /* VBOOT_REFERENCE_CGPT_MISC_H_ */
200