blob: 3280ba5e17c1bc35e3dc70eed423e2e2468e4b55 [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"
Bill Richardson5deb67f2010-07-23 17:22:25 -070013#include "cgptlib_internal.h"
Randall Spanglerd1836442010-06-10 09:59:04 -070014#include "load_kernel_fw.h"
15#include "rollback_index.h"
16#include "utility.h"
Randall Spangler83c88cf2010-06-11 16:14:18 -070017#include "vboot_common.h"
18
Randall Spanglerd1836442010-06-10 09:59:04 -070019#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 Spanglere2ec9842010-06-23 21:17:07 -070070 VBDEBUG(("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 Spanglere2ec9842010-06-23 21:17:07 -070079 VBDEBUG(("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 Spanglere2ec9842010-06-23 21:17:07 -070089 VBDEBUG(("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 Spanglere2ec9842010-06-23 21:17:07 -070099 VBDEBUG(("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
vbendeb3ecaf772010-06-24 16:19:53 -0700111/* disable MSVC warning on const logical expression (as in } while(0);) */
112__pragma(warning(disable: 4127))
Randall Spangler83c88cf2010-06-11 16:14:18 -0700113
Randall Spanglerbd529f02010-06-16 12:51:26 -0700114int LoadKernel(LoadKernelParams* params) {
Bill Richardsone2729402010-07-22 12:23:47 -0700115 VbPublicKey* kernel_subkey;
Randall Spanglerd1836442010-06-10 09:59:04 -0700116 GptData gpt;
117 uint64_t part_start, part_size;
Bill Richardsone2729402010-07-22 12:23:47 -0700118 uint64_t blba;
119 uint64_t kbuf_sectors;
Randall Spanglerd1836442010-06-10 09:59:04 -0700120 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;
Bill Richardsone2729402010-07-22 12:23:47 -0700127 int is_dev;
128 int is_rec;
129 int is_normal;
Randall Spangler7a786b72010-07-08 13:29:42 -0700130 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700131
Bill Richardsone2729402010-07-22 12:23:47 -0700132 /* Sanity Checks */
133 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700134 !params->bytes_per_lba ||
135 !params->ending_lba ||
136 !params->kernel_buffer ||
137 !params->kernel_buffer_size) {
138 VBDEBUG(("LoadKernel() called with invalid params\n"));
139 return LOAD_KERNEL_INVALID;
140 }
141
142 /* Initialization */
143 kernel_subkey = (VbPublicKey*)params->header_sign_key_blob;
144 blba = params->bytes_per_lba;
145 kbuf_sectors = KBUF_SIZE / blba;
146 is_dev = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
147 is_rec = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
148 is_normal = (!is_dev && !is_rec);
149
Randall Spanglerd1836442010-06-10 09:59:04 -0700150 /* Clear output params in case we fail */
151 params->partition_number = 0;
152 params->bootloader_address = 0;
153 params->bootloader_size = 0;
154
Randall Spangler63dffcb2010-08-05 15:13:14 -0700155 /* Let the TPM know if we're in recovery mode */
156 if (is_rec) {
157 if (0 != RollbackKernelRecovery(is_dev)) {
158 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
159 /* Ignore return code, since we need to boot recovery mode to
160 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700161 }
Randall Spangler81d09962010-06-23 10:15:38 -0700162 }
163
Randall Spanglerd1836442010-06-10 09:59:04 -0700164 if (is_normal) {
165 /* Read current kernel key index from TPM. Assumes TPM is already
166 * initialized. */
Randall Spangler7a786b72010-07-08 13:29:42 -0700167 status = RollbackKernelRead(&tpm_key_version, &tpm_kernel_version);
168 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700169 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler7a786b72010-07-08 13:29:42 -0700170 return (status == TPM_E_MUST_REBOOT ?
171 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spangler695cd162010-06-15 23:38:23 -0700172 }
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700173 } else if (is_dev && !is_rec) {
Randall Spanglerd1836442010-06-10 09:59:04 -0700174 /* In developer mode, we ignore the kernel subkey, and just use
175 * the SHA-512 hash to verify the key block. */
176 kernel_subkey = NULL;
177 }
178
179 do {
180 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700181 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700182 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700183 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700184 VBDEBUG(("Unable to read GPT data\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700185 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700186 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700187
188 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700189 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700190 VBDEBUG(("Error parsing GPT\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700191 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700192 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700193
Randall Spanglerd1836442010-06-10 09:59:04 -0700194 /* Allocate kernel header buffers */
195 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
196 if (!kbuf)
197 break;
198
199 /* Loop over candidate kernel partitions */
200 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
201 VbKeyBlockHeader* key_block;
202 VbKernelPreambleHeader* preamble;
203 RSAPublicKey* data_key;
204 uint64_t key_version;
205 uint64_t body_offset;
206
Randall Spanglere2ec9842010-06-23 21:17:07 -0700207 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
208 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700209
Randall Spanglerd1836442010-06-10 09:59:04 -0700210 /* Found at least one kernel partition. */
211 found_partitions++;
212
213 /* Read the first part of the kernel partition */
214 if (part_size < kbuf_sectors)
215 continue;
216 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf))
217 continue;
218
219 /* Verify the key block */
220 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler695cd162010-06-15 23:38:23 -0700221 if ((0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700222 VBDEBUG(("Verifying key block failed.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700223 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700224 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700225
Randall Spanglerae029d92010-07-19 18:26:35 -0700226 /* Check the key block flags against the current boot mode in normal
227 * and recovery modes (not in developer mode booting from SSD). */
228 if (is_rec || is_normal) {
229 if (!(key_block->key_block_flags &
230 (is_dev ? KEY_BLOCK_FLAG_DEVELOPER_1 :
231 KEY_BLOCK_FLAG_DEVELOPER_0))) {
232 VBDEBUG(("Developer flag mismatch.\n"));
233 continue;
234 }
235 if (!(key_block->key_block_flags &
236 (is_rec ? KEY_BLOCK_FLAG_RECOVERY_1 :
237 KEY_BLOCK_FLAG_RECOVERY_0))) {
238 VBDEBUG(("Recovery flag mismatch.\n"));
239 continue;
240 }
Randall Spangler695cd162010-06-15 23:38:23 -0700241 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700242
243 /* Check for rollback of key version. Note this is implicitly
244 * skipped in recovery and developer modes because those set
245 * key_version=0 above. */
246 key_version = key_block->data_key.key_version;
Randall Spangler695cd162010-06-15 23:38:23 -0700247 if (key_version < tpm_key_version) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700248 VBDEBUG(("Key version too old.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700249 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700250 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700251
252 /* Get the key for preamble/data verification from the key block */
253 data_key = PublicKeyToRSA(&key_block->data_key);
254 if (!data_key)
255 continue;
256
257 /* Verify the preamble, which follows the key block */
258 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700259 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700260 KBUF_SIZE - key_block->key_block_size,
261 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700262 VBDEBUG(("Preamble verification failed.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700263 RSAPublicKeyFree(data_key);
264 continue;
265 }
266
267 /* Check for rollback of kernel version. Note this is implicitly
268 * skipped in recovery and developer modes because those set
269 * key_version=0 and kernel_version=0 above. */
270 if (key_version == tpm_key_version &&
271 preamble->kernel_version < tpm_kernel_version) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700272 VBDEBUG(("Kernel version too low.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700273 RSAPublicKeyFree(data_key);
274 continue;
275 }
276
Randall Spanglere2ec9842010-06-23 21:17:07 -0700277 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700278
Randall Spanglerd1836442010-06-10 09:59:04 -0700279 /* Check for lowest key version from a valid header. */
280 if (lowest_key_version > key_version) {
281 lowest_key_version = key_version;
282 lowest_kernel_version = preamble->kernel_version;
283 }
284 else if (lowest_key_version == key_version &&
285 lowest_kernel_version > preamble->kernel_version) {
286 lowest_kernel_version = preamble->kernel_version;
287 }
288
289 /* If we already have a good kernel, no need to read another
290 * one; we only needed to look at the versions to check for
291 * rollback. */
292 if (-1 != good_partition)
293 continue;
294
295 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700296 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
297 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700298 VBDEBUG(("Wrong body load address.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700299 RSAPublicKeyFree(data_key);
300 continue;
301 }
302
303 /* Verify kernel body starts at a multiple of the sector size. */
304 body_offset = key_block->key_block_size + preamble->preamble_size;
305 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700306 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700307 RSAPublicKeyFree(data_key);
308 continue;
309 }
310
311 /* Verify kernel body fits in the partition */
312 if (body_offset + preamble->body_signature.data_size >
313 part_size * blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700314 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700315 RSAPublicKeyFree(data_key);
316 continue;
317 }
318
319 /* Read the kernel data */
320 if (0 != BootDeviceReadLBA(
321 part_start + (body_offset / blba),
322 (preamble->body_signature.data_size + blba - 1) / blba,
323 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700324 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700325 RSAPublicKeyFree(data_key);
326 continue;
327 }
328
329 /* Verify kernel data */
330 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700331 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700332 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700333 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700334 RSAPublicKeyFree(data_key);
335 continue;
336 }
337
338 /* Done with the kernel signing key, so can free it now */
339 RSAPublicKeyFree(data_key);
340
341 /* If we're still here, the kernel is valid. */
342 /* Save the first good partition we find; that's the one we'll boot */
Randall Spanglere2ec9842010-06-23 21:17:07 -0700343 VBDEBUG(("Partiton is good.\n"));
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700344 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
345 * Adjust here, until cgptlib is fixed. */
346 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700347 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700348 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700349 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
350 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700351 params->bootloader_address = preamble->bootloader_address;
352 params->bootloader_size = preamble->bootloader_size;
353 /* If we're in developer or recovery mode, there's no rollback
354 * protection, so we can stop at the first valid kernel. */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700355 if (!is_normal) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700356 VBDEBUG(("Boot_flags = !is_normal\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700357 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700358 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700359
Randall Spangler695cd162010-06-15 23:38:23 -0700360 /* Otherwise, we're in normal boot mode, so we do care about the
361 * key index in the TPM. If the good partition's key version is
362 * the same as the tpm, then the TPM doesn't need updating; we
363 * can stop now. Otherwise, we'll check all the other headers
364 * to see if they contain a newer key. */
365 if (key_version == tpm_key_version &&
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700366 preamble->kernel_version == tpm_kernel_version) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700367 VBDEBUG(("Same key version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700368 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700369 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700370 } /* while(GptNextKernelEntry) */
371 } while(0);
372
373 /* Free kernel buffer */
374 if (kbuf)
375 Free(kbuf);
376
377 /* Write and free GPT data */
378 WriteAndFreeGptData(&gpt);
379
380 /* Handle finding a good partition */
381 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700382 VBDEBUG(("Good_partition >= 0\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700383
384 /* See if we need to update the TPM */
385 if (is_normal) {
386 /* We only update the TPM in normal boot mode. In developer
387 * mode, the kernel is self-signed by the developer, so we can't
388 * trust the key version and wouldn't want to roll the TPM
389 * forward. In recovery mode, the TPM stays PP-unlocked, so
390 * anything we write gets blown away by the firmware when we go
391 * back to normal mode. */
Randall Spanglere2ec9842010-06-23 21:17:07 -0700392 VBDEBUG(("Boot_flags = is_normal\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700393 if ((lowest_key_version > tpm_key_version) ||
394 (lowest_key_version == tpm_key_version &&
395 lowest_kernel_version > tpm_kernel_version)) {
Randall Spangler7a786b72010-07-08 13:29:42 -0700396
397 status = RollbackKernelWrite((uint16_t)lowest_key_version,
398 (uint16_t)lowest_kernel_version);
399 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700400 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler63dffcb2010-08-05 15:13:14 -0700401 return (status == TPM_E_MUST_REBOOT ?
402 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spangler10788382010-06-23 15:35:31 -0700403 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700404 }
405 }
406
Randall Spangler63dffcb2010-08-05 15:13:14 -0700407 /* Lock the kernel versions */
408 status = RollbackKernelLock();
409 if (0 != status) {
410 VBDEBUG(("Error locking kernel versions.\n"));
411 /* Don't reboot to recovery mode if we're already there */
412 if (!is_rec)
413 return (status == TPM_E_MUST_REBOOT ?
414 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spanglerd1836442010-06-10 09:59:04 -0700415 }
416
417 /* Success! */
418 return LOAD_KERNEL_SUCCESS;
419 }
420
421 // Handle error cases
422 if (found_partitions)
423 return LOAD_KERNEL_INVALID;
424 else
425 return LOAD_KERNEL_NOT_FOUND;
426}