blob: ddbceed6e39cc7b281afa57d66b0644117f23851 [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 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) {
Randall Spanglerd1836442010-06-10 09:59:04 -0700115
116 VbPublicKey* kernel_subkey = (VbPublicKey*)params->header_sign_key_blob;
117
118 GptData gpt;
119 uint64_t part_start, part_size;
120 uint64_t blba = params->bytes_per_lba;
121 uint64_t kbuf_sectors = KBUF_SIZE / blba;
122 uint8_t* kbuf = NULL;
123 int found_partitions = 0;
124 int good_partition = -1;
125 uint16_t tpm_key_version = 0;
126 uint16_t tpm_kernel_version = 0;
127 uint64_t lowest_key_version = 0xFFFF;
128 uint64_t lowest_kernel_version = 0xFFFF;
Randall Spanglerc4a011a2010-06-29 19:08:43 -0700129 int is_dev = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
130 int is_rec = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700131 int is_normal = (!is_dev && !is_rec);
Randall Spangler7a786b72010-07-08 13:29:42 -0700132 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700133
134 /* Clear output params in case we fail */
135 params->partition_number = 0;
136 params->bootloader_address = 0;
137 params->bootloader_size = 0;
138
Randall Spangler10788382010-06-23 15:35:31 -0700139 /* Let the TPM know if we're in recovery mode */
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700140 if (is_rec) {
141 if (0 != RollbackKernelRecovery(is_dev ? 1 : 0)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700142 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700143 /* Ignore return code, since we need to boot recovery mode to
144 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700145 }
Randall Spangler81d09962010-06-23 10:15:38 -0700146 }
147
Randall Spanglerd1836442010-06-10 09:59:04 -0700148 if (is_normal) {
149 /* Read current kernel key index from TPM. Assumes TPM is already
150 * initialized. */
Randall Spangler7a786b72010-07-08 13:29:42 -0700151 status = RollbackKernelRead(&tpm_key_version, &tpm_kernel_version);
152 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700153 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler7a786b72010-07-08 13:29:42 -0700154 return (status == TPM_E_MUST_REBOOT ?
155 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spangler695cd162010-06-15 23:38:23 -0700156 }
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700157 } else if (is_dev && !is_rec) {
Randall Spanglerd1836442010-06-10 09:59:04 -0700158 /* In developer mode, we ignore the kernel subkey, and just use
159 * the SHA-512 hash to verify the key block. */
160 kernel_subkey = NULL;
161 }
162
163 do {
164 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700165 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700166 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700167 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700168 VBDEBUG(("Unable to read GPT data\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700169 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700170 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700171
172 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700173 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700174 VBDEBUG(("Error parsing GPT\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700175 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700176 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700177
Randall Spanglerd1836442010-06-10 09:59:04 -0700178 /* Allocate kernel header buffers */
179 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
180 if (!kbuf)
181 break;
182
183 /* Loop over candidate kernel partitions */
184 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
185 VbKeyBlockHeader* key_block;
186 VbKernelPreambleHeader* preamble;
187 RSAPublicKey* data_key;
188 uint64_t key_version;
189 uint64_t body_offset;
190
Randall Spanglere2ec9842010-06-23 21:17:07 -0700191 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
192 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700193
Randall Spanglerd1836442010-06-10 09:59:04 -0700194 /* Found at least one kernel partition. */
195 found_partitions++;
196
197 /* Read the first part of the kernel partition */
198 if (part_size < kbuf_sectors)
199 continue;
200 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf))
201 continue;
202
203 /* Verify the key block */
204 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler695cd162010-06-15 23:38:23 -0700205 if ((0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700206 VBDEBUG(("Verifying key block failed.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700207 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700208 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700209
210 /* Check the key block flags against the current boot mode */
Randall Spanglerc4a011a2010-06-29 19:08:43 -0700211 if (!(key_block->key_block_flags &
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700212 (is_dev ? KEY_BLOCK_FLAG_DEVELOPER_1 :
213 KEY_BLOCK_FLAG_DEVELOPER_0))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700214 VBDEBUG(("Developer flag mismatch.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700215 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700216 }
Randall Spanglerc4a011a2010-06-29 19:08:43 -0700217 if (!(key_block->key_block_flags &
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700218 (is_rec ? KEY_BLOCK_FLAG_RECOVERY_1 :
219 KEY_BLOCK_FLAG_RECOVERY_0))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700220 VBDEBUG(("Recovery flag mismatch.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700221 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700222 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700223
224 /* Check for rollback of key version. Note this is implicitly
225 * skipped in recovery and developer modes because those set
226 * key_version=0 above. */
227 key_version = key_block->data_key.key_version;
Randall Spangler695cd162010-06-15 23:38:23 -0700228 if (key_version < tpm_key_version) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700229 VBDEBUG(("Key version too old.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700230 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700231 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700232
233 /* Get the key for preamble/data verification from the key block */
234 data_key = PublicKeyToRSA(&key_block->data_key);
235 if (!data_key)
236 continue;
237
238 /* Verify the preamble, which follows the key block */
239 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
240 if ((0 != VerifyKernelPreamble2(preamble,
241 KBUF_SIZE - key_block->key_block_size,
242 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700243 VBDEBUG(("Preamble verification failed.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700244 RSAPublicKeyFree(data_key);
245 continue;
246 }
247
248 /* Check for rollback of kernel version. Note this is implicitly
249 * skipped in recovery and developer modes because those set
250 * key_version=0 and kernel_version=0 above. */
251 if (key_version == tpm_key_version &&
252 preamble->kernel_version < tpm_kernel_version) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700253 VBDEBUG(("Kernel version too low.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700254 RSAPublicKeyFree(data_key);
255 continue;
256 }
257
Randall Spanglere2ec9842010-06-23 21:17:07 -0700258 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700259
Randall Spanglerd1836442010-06-10 09:59:04 -0700260 /* Check for lowest key version from a valid header. */
261 if (lowest_key_version > key_version) {
262 lowest_key_version = key_version;
263 lowest_kernel_version = preamble->kernel_version;
264 }
265 else if (lowest_key_version == key_version &&
266 lowest_kernel_version > preamble->kernel_version) {
267 lowest_kernel_version = preamble->kernel_version;
268 }
269
270 /* If we already have a good kernel, no need to read another
271 * one; we only needed to look at the versions to check for
272 * rollback. */
273 if (-1 != good_partition)
274 continue;
275
276 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700277 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
278 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700279 VBDEBUG(("Wrong body load address.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700280 RSAPublicKeyFree(data_key);
281 continue;
282 }
283
284 /* Verify kernel body starts at a multiple of the sector size. */
285 body_offset = key_block->key_block_size + preamble->preamble_size;
286 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700287 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700288 RSAPublicKeyFree(data_key);
289 continue;
290 }
291
292 /* Verify kernel body fits in the partition */
293 if (body_offset + preamble->body_signature.data_size >
294 part_size * blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700295 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700296 RSAPublicKeyFree(data_key);
297 continue;
298 }
299
300 /* Read the kernel data */
301 if (0 != BootDeviceReadLBA(
302 part_start + (body_offset / blba),
303 (preamble->body_signature.data_size + blba - 1) / blba,
304 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700305 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700306 RSAPublicKeyFree(data_key);
307 continue;
308 }
309
310 /* Verify kernel data */
311 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
312 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700313 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700314 RSAPublicKeyFree(data_key);
315 continue;
316 }
317
318 /* Done with the kernel signing key, so can free it now */
319 RSAPublicKeyFree(data_key);
320
321 /* If we're still here, the kernel is valid. */
322 /* Save the first good partition we find; that's the one we'll boot */
Randall Spanglere2ec9842010-06-23 21:17:07 -0700323 VBDEBUG(("Partiton is good.\n"));
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700324 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
325 * Adjust here, until cgptlib is fixed. */
326 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700327 params->partition_number = gpt.current_kernel + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700328 params->bootloader_address = preamble->bootloader_address;
329 params->bootloader_size = preamble->bootloader_size;
330 /* If we're in developer or recovery mode, there's no rollback
331 * protection, so we can stop at the first valid kernel. */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700332 if (!is_normal) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700333 VBDEBUG(("Boot_flags = !is_normal\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700334 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700335 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700336
Randall Spangler695cd162010-06-15 23:38:23 -0700337 /* Otherwise, we're in normal boot mode, so we do care about the
338 * key index in the TPM. If the good partition's key version is
339 * the same as the tpm, then the TPM doesn't need updating; we
340 * can stop now. Otherwise, we'll check all the other headers
341 * to see if they contain a newer key. */
342 if (key_version == tpm_key_version &&
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700343 preamble->kernel_version == tpm_kernel_version) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700344 VBDEBUG(("Same key version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700345 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700346 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700347 } /* while(GptNextKernelEntry) */
348 } while(0);
349
350 /* Free kernel buffer */
351 if (kbuf)
352 Free(kbuf);
353
354 /* Write and free GPT data */
355 WriteAndFreeGptData(&gpt);
356
357 /* Handle finding a good partition */
358 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700359 VBDEBUG(("Good_partition >= 0\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700360
361 /* See if we need to update the TPM */
362 if (is_normal) {
363 /* We only update the TPM in normal boot mode. In developer
364 * mode, the kernel is self-signed by the developer, so we can't
365 * trust the key version and wouldn't want to roll the TPM
366 * forward. In recovery mode, the TPM stays PP-unlocked, so
367 * anything we write gets blown away by the firmware when we go
368 * back to normal mode. */
Randall Spanglere2ec9842010-06-23 21:17:07 -0700369 VBDEBUG(("Boot_flags = is_normal\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700370 if ((lowest_key_version > tpm_key_version) ||
371 (lowest_key_version == tpm_key_version &&
372 lowest_kernel_version > tpm_kernel_version)) {
Randall Spangler7a786b72010-07-08 13:29:42 -0700373
374 status = RollbackKernelWrite((uint16_t)lowest_key_version,
375 (uint16_t)lowest_kernel_version);
376 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700377 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler7a786b72010-07-08 13:29:42 -0700378 return (status == TPM_E_MUST_REBOOT ?
379 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spangler10788382010-06-23 15:35:31 -0700380 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700381 }
382 }
383
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700384 /* Lock the kernel versions */
Randall Spangler7a786b72010-07-08 13:29:42 -0700385 status = RollbackKernelLock();
386 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700387 VBDEBUG(("Error locking kernel versions.\n"));
Randall Spanglerd6aad3a2010-06-24 14:01:34 -0700388 /* Don't reboot to recovery mode if we're already there */
389 if (!is_rec)
Randall Spangler7a786b72010-07-08 13:29:42 -0700390 return (status == TPM_E_MUST_REBOOT ?
391 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spanglerd1836442010-06-10 09:59:04 -0700392 }
393
394 /* Success! */
395 return LOAD_KERNEL_SUCCESS;
396 }
397
398 // Handle error cases
399 if (found_partitions)
400 return LOAD_KERNEL_INVALID;
401 else
402 return LOAD_KERNEL_NOT_FOUND;
403}