blob: a0a76566748930022be6c6cfafd14bce3ce35f99 [file] [log] [blame]
Randall Spanglerd1836442010-06-10 09:59:04 -07001/* Copyright (c) 2010 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 * Functions for loading a kernel from disk.
6 * (Firmware portion)
7 */
8
9#include "vboot_kernel.h"
10
11#include "boot_device.h"
12#include "cgptlib.h"
13#include "load_kernel_fw.h"
14#include "rollback_index.h"
15#include "utility.h"
Randall Spangler83c88cf2010-06-11 16:14:18 -070016#include "vboot_common.h"
17
Randall Spanglerd1836442010-06-10 09:59:04 -070018
19#define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */
20
Randall Spangler83c88cf2010-06-11 16:14:18 -070021
22/* Allocates and reads GPT data from the drive. The sector_bytes and
23 * drive_sectors fields should be filled on input. The primary and
24 * secondary header and entries are filled on output.
25 *
26 * Returns 0 if successful, 1 if error. */
27int AllocAndReadGptData(GptData* gptdata) {
28
29 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
30
31 /* No data to be written yet */
32 gptdata->modified = 0;
33
34 /* Allocate all buffers */
35 gptdata->primary_header = (uint8_t*)Malloc(gptdata->sector_bytes);
36 gptdata->secondary_header = (uint8_t*)Malloc(gptdata->sector_bytes);
37 gptdata->primary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE);
38 gptdata->secondary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE);
39
40 if (gptdata->primary_header == NULL || gptdata->secondary_header == NULL ||
41 gptdata->primary_entries == NULL || gptdata->secondary_entries == NULL)
42 return 1;
43
44 /* Read data from the drive, skipping the protective MBR */
45 if (0 != BootDeviceReadLBA(1, 1, gptdata->primary_header))
46 return 1;
47 if (0 != BootDeviceReadLBA(2, entries_sectors, gptdata->primary_entries))
48 return 1;
49 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - entries_sectors - 1,
50 entries_sectors, gptdata->secondary_entries))
51 return 1;
52 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - 1,
53 1, gptdata->secondary_header))
54 return 1;
55
56 return 0;
57}
58
59
60/* Writes any changes for the GPT data back to the drive, then frees
61 * the buffers.
62 *
63 * Returns 0 if successful, 1 if error. */
64int WriteAndFreeGptData(GptData* gptdata) {
65
66 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
67
68 if (gptdata->primary_header) {
69 if (gptdata->modified & GPT_MODIFIED_HEADER1) {
Randall Spangler81d09962010-06-23 10:15:38 -070070 debug("Updating GPT header 1\n");
Randall Spangler83c88cf2010-06-11 16:14:18 -070071 if (0 != BootDeviceWriteLBA(1, 1, gptdata->primary_header))
72 return 1;
73 }
74 Free(gptdata->primary_header);
75 }
76
77 if (gptdata->primary_entries) {
78 if (gptdata->modified & GPT_MODIFIED_ENTRIES1) {
Randall Spangler81d09962010-06-23 10:15:38 -070079 debug("Updating GPT entries 1\n");
Randall Spangler83c88cf2010-06-11 16:14:18 -070080 if (0 != BootDeviceWriteLBA(2, entries_sectors,
81 gptdata->primary_entries))
82 return 1;
83 }
84 Free(gptdata->primary_entries);
85 }
86
87 if (gptdata->secondary_entries) {
88 if (gptdata->modified & GPT_MODIFIED_ENTRIES2) {
Randall Spangler81d09962010-06-23 10:15:38 -070089 debug("Updating GPT header 2\n");
Randall Spangler83c88cf2010-06-11 16:14:18 -070090 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - entries_sectors - 1,
91 entries_sectors, gptdata->secondary_entries))
92 return 1;
93 }
94 Free(gptdata->secondary_entries);
95 }
96
97 if (gptdata->secondary_header) {
98 if (gptdata->modified & GPT_MODIFIED_HEADER2) {
Randall Spangler81d09962010-06-23 10:15:38 -070099 debug("Updating GPT entries 2\n");
Randall Spangler83c88cf2010-06-11 16:14:18 -0700100 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - 1, 1,
101 gptdata->secondary_header))
102 return 1;
103 }
104 Free(gptdata->secondary_header);
105 }
106
107 /* Success */
108 return 0;
109}
110
111
Randall Spanglerbd529f02010-06-16 12:51:26 -0700112int LoadKernel(LoadKernelParams* params) {
Randall Spanglerd1836442010-06-10 09:59:04 -0700113
114 VbPublicKey* kernel_subkey = (VbPublicKey*)params->header_sign_key_blob;
115
116 GptData gpt;
117 uint64_t part_start, part_size;
118 uint64_t blba = params->bytes_per_lba;
119 uint64_t kbuf_sectors = KBUF_SIZE / blba;
120 uint8_t* kbuf = NULL;
121 int found_partitions = 0;
122 int good_partition = -1;
123 uint16_t tpm_key_version = 0;
124 uint16_t tpm_kernel_version = 0;
125 uint64_t lowest_key_version = 0xFFFF;
126 uint64_t lowest_kernel_version = 0xFFFF;
127 int is_dev = ((BOOT_FLAG_DEVELOPER & params->boot_flags) &&
128 !(BOOT_FLAG_RECOVERY & params->boot_flags));
129 int is_normal = (!(BOOT_FLAG_DEVELOPER & params->boot_flags) &&
130 !(BOOT_FLAG_RECOVERY & params->boot_flags));
131
132 /* Clear output params in case we fail */
133 params->partition_number = 0;
134 params->bootloader_address = 0;
135 params->bootloader_size = 0;
136
Randall Spangler10788382010-06-23 15:35:31 -0700137 /* Let the TPM know if we're in recovery mode */
138 if (BOOT_FLAG_RECOVERY & params->boot_flags) {
139 if (0 != RollbackKernelRecovery(BOOT_FLAG_DEVELOPER & params->boot_flags
140 ? 1 : 0)) {
141 debug("Error setting up TPM for recovery kernel\n");
142 return LOAD_KERNEL_RECOVERY;
143 }
Randall Spangler81d09962010-06-23 10:15:38 -0700144 }
145
Randall Spanglerd1836442010-06-10 09:59:04 -0700146 if (is_normal) {
147 /* Read current kernel key index from TPM. Assumes TPM is already
148 * initialized. */
Randall Spangler10788382010-06-23 15:35:31 -0700149 if (0 != RollbackKernelRead(&tpm_key_version, &tpm_kernel_version)) {
150 debug("Unable to get kernel versions from TPM\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700151 return LOAD_KERNEL_RECOVERY;
Randall Spangler695cd162010-06-15 23:38:23 -0700152 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700153 } else if (is_dev) {
154 /* In developer mode, we ignore the kernel subkey, and just use
155 * the SHA-512 hash to verify the key block. */
156 kernel_subkey = NULL;
157 }
158
159 do {
160 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700161 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700162 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700163 if (0 != AllocAndReadGptData(&gpt)) {
164 debug("Unable to read GPT data\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700165 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700166 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700167
168 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700169 if (GPT_SUCCESS != GptInit(&gpt)) {
170 debug("Error parsing GPT\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700171 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700172 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700173
Randall Spanglerd1836442010-06-10 09:59:04 -0700174 /* Allocate kernel header buffers */
175 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
176 if (!kbuf)
177 break;
178
179 /* Loop over candidate kernel partitions */
180 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
181 VbKeyBlockHeader* key_block;
182 VbKernelPreambleHeader* preamble;
183 RSAPublicKey* data_key;
184 uint64_t key_version;
185 uint64_t body_offset;
186
Randall Spangler695cd162010-06-15 23:38:23 -0700187 debug("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
188 part_start, part_size);
189
Randall Spanglerd1836442010-06-10 09:59:04 -0700190 /* Found at least one kernel partition. */
191 found_partitions++;
192
193 /* Read the first part of the kernel partition */
194 if (part_size < kbuf_sectors)
195 continue;
196 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf))
197 continue;
198
199 /* Verify the key block */
200 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler695cd162010-06-15 23:38:23 -0700201 if ((0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey))) {
202 debug("Verifying key block failed.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700203 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700204 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700205
206 /* Check the key block flags against the current boot mode */
207 if (!(key_block->key_block_flags &&
208 ((BOOT_FLAG_DEVELOPER & params->boot_flags) ?
Randall Spangler695cd162010-06-15 23:38:23 -0700209 KEY_BLOCK_FLAG_DEVELOPER_1 : KEY_BLOCK_FLAG_DEVELOPER_0))) {
210 debug("Developer flag mismatch.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700211 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700212 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700213 if (!(key_block->key_block_flags &&
214 ((BOOT_FLAG_RECOVERY & params->boot_flags) ?
Randall Spangler695cd162010-06-15 23:38:23 -0700215 KEY_BLOCK_FLAG_RECOVERY_1 : KEY_BLOCK_FLAG_RECOVERY_0))) {
216 debug("Recovery flag mismatch.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700217 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700218 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700219
220 /* Check for rollback of key version. Note this is implicitly
221 * skipped in recovery and developer modes because those set
222 * key_version=0 above. */
223 key_version = key_block->data_key.key_version;
Randall Spangler695cd162010-06-15 23:38:23 -0700224 if (key_version < tpm_key_version) {
225 debug("Key version too old.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700226 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700227 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700228
229 /* Get the key for preamble/data verification from the key block */
230 data_key = PublicKeyToRSA(&key_block->data_key);
231 if (!data_key)
232 continue;
233
234 /* Verify the preamble, which follows the key block */
235 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
236 if ((0 != VerifyKernelPreamble2(preamble,
237 KBUF_SIZE - key_block->key_block_size,
238 data_key))) {
Randall Spangler695cd162010-06-15 23:38:23 -0700239 debug("Preamble verification failed.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700240 RSAPublicKeyFree(data_key);
241 continue;
242 }
243
244 /* Check for rollback of kernel version. Note this is implicitly
245 * skipped in recovery and developer modes because those set
246 * key_version=0 and kernel_version=0 above. */
247 if (key_version == tpm_key_version &&
248 preamble->kernel_version < tpm_kernel_version) {
Randall Spangler695cd162010-06-15 23:38:23 -0700249 debug("Kernel version too low.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700250 RSAPublicKeyFree(data_key);
251 continue;
252 }
253
Randall Spangler695cd162010-06-15 23:38:23 -0700254 debug("Kernel preamble is good.\n");
255
Randall Spanglerd1836442010-06-10 09:59:04 -0700256 /* Check for lowest key version from a valid header. */
257 if (lowest_key_version > key_version) {
258 lowest_key_version = key_version;
259 lowest_kernel_version = preamble->kernel_version;
260 }
261 else if (lowest_key_version == key_version &&
262 lowest_kernel_version > preamble->kernel_version) {
263 lowest_kernel_version = preamble->kernel_version;
264 }
265
266 /* If we already have a good kernel, no need to read another
267 * one; we only needed to look at the versions to check for
268 * rollback. */
269 if (-1 != good_partition)
270 continue;
271
272 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700273 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
274 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
275 debug("Wrong body load address.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700276 RSAPublicKeyFree(data_key);
277 continue;
278 }
279
280 /* Verify kernel body starts at a multiple of the sector size. */
281 body_offset = key_block->key_block_size + preamble->preamble_size;
282 if (0 != body_offset % blba) {
Randall Spangler695cd162010-06-15 23:38:23 -0700283 debug("Kernel body not at multiple of sector size.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700284 RSAPublicKeyFree(data_key);
285 continue;
286 }
287
288 /* Verify kernel body fits in the partition */
289 if (body_offset + preamble->body_signature.data_size >
290 part_size * blba) {
Randall Spangler695cd162010-06-15 23:38:23 -0700291 debug("Kernel body doesn't fit in partition.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700292 RSAPublicKeyFree(data_key);
293 continue;
294 }
295
296 /* Read the kernel data */
297 if (0 != BootDeviceReadLBA(
298 part_start + (body_offset / blba),
299 (preamble->body_signature.data_size + blba - 1) / blba,
300 params->kernel_buffer)) {
Randall Spangler695cd162010-06-15 23:38:23 -0700301 debug("Unable to read kernel data.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700302 RSAPublicKeyFree(data_key);
303 continue;
304 }
305
306 /* Verify kernel data */
307 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
308 &preamble->body_signature, data_key)) {
Randall Spangler695cd162010-06-15 23:38:23 -0700309 debug("Kernel data verification failed.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700310 RSAPublicKeyFree(data_key);
311 continue;
312 }
313
314 /* Done with the kernel signing key, so can free it now */
315 RSAPublicKeyFree(data_key);
316
317 /* If we're still here, the kernel is valid. */
318 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler695cd162010-06-15 23:38:23 -0700319 debug("Partiton is good.\n");
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700320 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
321 * Adjust here, until cgptlib is fixed. */
322 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700323 params->partition_number = gpt.current_kernel + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700324 params->bootloader_address = preamble->bootloader_address;
325 params->bootloader_size = preamble->bootloader_size;
326 /* If we're in developer or recovery mode, there's no rollback
327 * protection, so we can stop at the first valid kernel. */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700328 if (!is_normal) {
329 debug("Boot_flags = !is_normal\n");
Randall Spangler695cd162010-06-15 23:38:23 -0700330 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700331 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700332
Randall Spangler695cd162010-06-15 23:38:23 -0700333 /* Otherwise, we're in normal boot mode, so we do care about the
334 * key index in the TPM. If the good partition's key version is
335 * the same as the tpm, then the TPM doesn't need updating; we
336 * can stop now. Otherwise, we'll check all the other headers
337 * to see if they contain a newer key. */
338 if (key_version == tpm_key_version &&
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700339 preamble->kernel_version == tpm_kernel_version) {
340 debug("Same key version\n");
Randall Spangler695cd162010-06-15 23:38:23 -0700341 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700342 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700343 } /* while(GptNextKernelEntry) */
344 } while(0);
345
346 /* Free kernel buffer */
347 if (kbuf)
348 Free(kbuf);
349
350 /* Write and free GPT data */
351 WriteAndFreeGptData(&gpt);
352
353 /* Handle finding a good partition */
354 if (good_partition >= 0) {
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700355 debug("Good_partition >= 0\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700356
357 /* See if we need to update the TPM */
358 if (is_normal) {
359 /* We only update the TPM in normal boot mode. In developer
360 * mode, the kernel is self-signed by the developer, so we can't
361 * trust the key version and wouldn't want to roll the TPM
362 * forward. In recovery mode, the TPM stays PP-unlocked, so
363 * anything we write gets blown away by the firmware when we go
364 * back to normal mode. */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700365 debug("Boot_flags = is_normal\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700366 if ((lowest_key_version > tpm_key_version) ||
367 (lowest_key_version == tpm_key_version &&
368 lowest_kernel_version > tpm_kernel_version)) {
Randall Spangler10788382010-06-23 15:35:31 -0700369 if (0 != RollbackKernelWrite((uint16_t)lowest_key_version,
370 (uint16_t)lowest_kernel_version)) {
371 debug("Error writing kernel versions to TPM.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700372 return LOAD_KERNEL_RECOVERY;
Randall Spangler10788382010-06-23 15:35:31 -0700373 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700374 }
375 }
376
Randall Spangler10788382010-06-23 15:35:31 -0700377 /* Lock the kernel versions, since we're about to boot the kernel */
378 if (0 != RollbackKernelLock()) {
379 debug("Error locking kernel versions.\n");
380 return LOAD_KERNEL_RECOVERY;
Randall Spanglerd1836442010-06-10 09:59:04 -0700381 }
382
383 /* Success! */
384 return LOAD_KERNEL_SUCCESS;
385 }
386
387 // Handle error cases
388 if (found_partitions)
389 return LOAD_KERNEL_INVALID;
390 else
391 return LOAD_KERNEL_NOT_FOUND;
392}