blob: ee2890cfcd38ecc70a1dee670c5b031fb0bdfe1e [file] [log] [blame]
Randall Spangler95c40312011-03-09 15:54:16 -08001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Randall Spanglerd1836442010-06-10 09:59:04 -07002 * 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 Spangler95c40312011-03-09 15:54:16 -080014#include "gbb_header.h"
Randall Spanglerd1836442010-06-10 09:59:04 -070015#include "load_kernel_fw.h"
16#include "rollback_index.h"
17#include "utility.h"
Randall Spangler83c88cf2010-06-11 16:14:18 -070018#include "vboot_common.h"
19
Randall Spanglerd1836442010-06-10 09:59:04 -070020#define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */
21
Randall Spangler640fb512011-03-03 10:11:17 -080022typedef enum BootMode {
23 kBootNormal, /* Normal firmware */
24 kBootDev, /* Dev firmware AND dev switch is on */
25 kBootRecovery /* Recovery firmware, regardless of dev switch position */
26} BootMode;
27
Randall Spangler83c88cf2010-06-11 16:14:18 -070028
29/* Allocates and reads GPT data from the drive. The sector_bytes and
30 * drive_sectors fields should be filled on input. The primary and
31 * secondary header and entries are filled on output.
32 *
33 * Returns 0 if successful, 1 if error. */
34int AllocAndReadGptData(GptData* gptdata) {
35
36 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
37
38 /* No data to be written yet */
39 gptdata->modified = 0;
40
41 /* Allocate all buffers */
42 gptdata->primary_header = (uint8_t*)Malloc(gptdata->sector_bytes);
43 gptdata->secondary_header = (uint8_t*)Malloc(gptdata->sector_bytes);
44 gptdata->primary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE);
45 gptdata->secondary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE);
46
47 if (gptdata->primary_header == NULL || gptdata->secondary_header == NULL ||
48 gptdata->primary_entries == NULL || gptdata->secondary_entries == NULL)
49 return 1;
50
51 /* Read data from the drive, skipping the protective MBR */
52 if (0 != BootDeviceReadLBA(1, 1, gptdata->primary_header))
53 return 1;
54 if (0 != BootDeviceReadLBA(2, entries_sectors, gptdata->primary_entries))
55 return 1;
56 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - entries_sectors - 1,
57 entries_sectors, gptdata->secondary_entries))
58 return 1;
59 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - 1,
60 1, gptdata->secondary_header))
61 return 1;
62
63 return 0;
64}
65
66
67/* Writes any changes for the GPT data back to the drive, then frees
68 * the buffers.
69 *
70 * Returns 0 if successful, 1 if error. */
71int WriteAndFreeGptData(GptData* gptdata) {
72
73 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
74
75 if (gptdata->primary_header) {
76 if (gptdata->modified & GPT_MODIFIED_HEADER1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070077 VBDEBUG(("Updating GPT header 1\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070078 if (0 != BootDeviceWriteLBA(1, 1, gptdata->primary_header))
79 return 1;
80 }
81 Free(gptdata->primary_header);
82 }
83
84 if (gptdata->primary_entries) {
85 if (gptdata->modified & GPT_MODIFIED_ENTRIES1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070086 VBDEBUG(("Updating GPT entries 1\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070087 if (0 != BootDeviceWriteLBA(2, entries_sectors,
88 gptdata->primary_entries))
89 return 1;
90 }
91 Free(gptdata->primary_entries);
92 }
93
94 if (gptdata->secondary_entries) {
95 if (gptdata->modified & GPT_MODIFIED_ENTRIES2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070096 VBDEBUG(("Updating GPT header 2\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070097 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - entries_sectors - 1,
98 entries_sectors, gptdata->secondary_entries))
99 return 1;
100 }
101 Free(gptdata->secondary_entries);
102 }
103
104 if (gptdata->secondary_header) {
105 if (gptdata->modified & GPT_MODIFIED_HEADER2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700106 VBDEBUG(("Updating GPT entries 2\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -0700107 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - 1, 1,
108 gptdata->secondary_header))
109 return 1;
110 }
111 Free(gptdata->secondary_header);
112 }
113
114 /* Success */
115 return 0;
116}
117
vbendeb3ecaf772010-06-24 16:19:53 -0700118/* disable MSVC warning on const logical expression (as in } while(0);) */
119__pragma(warning(disable: 4127))
Randall Spangler83c88cf2010-06-11 16:14:18 -0700120
Randall Spanglerbd529f02010-06-16 12:51:26 -0700121int LoadKernel(LoadKernelParams* params) {
Randall Spangler95c40312011-03-09 15:54:16 -0800122 VbSharedDataHeader* shared = (VbSharedDataHeader*)params->shared_data_blob;
Randall Spangler640fb512011-03-03 10:11:17 -0800123 VbNvContext* vnc = params->nv_context;
Randall Spangler95c40312011-03-09 15:54:16 -0800124 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)params->gbb_data;
Bill Richardsone2729402010-07-22 12:23:47 -0700125 VbPublicKey* kernel_subkey;
Randall Spanglerd1836442010-06-10 09:59:04 -0700126 GptData gpt;
127 uint64_t part_start, part_size;
Bill Richardsone2729402010-07-22 12:23:47 -0700128 uint64_t blba;
129 uint64_t kbuf_sectors;
Randall Spanglerd1836442010-06-10 09:59:04 -0700130 uint8_t* kbuf = NULL;
131 int found_partitions = 0;
132 int good_partition = -1;
Randall Spangler640fb512011-03-03 10:11:17 -0800133 int good_partition_key_block_valid = 0;
Randall Spangler66680282010-08-16 12:33:44 -0700134 uint32_t tpm_version = 0;
135 uint64_t lowest_version = 0xFFFFFFFF;
Randall Spangler640fb512011-03-03 10:11:17 -0800136 int rec_switch, dev_switch;
137 BootMode boot_mode;
Randall Spangler7a786b72010-07-08 13:29:42 -0700138 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700139
Randall Spangler640fb512011-03-03 10:11:17 -0800140 /* TODO: differentiate between finding an invalid kernel (found_partitions>0)
141 * and not finding one at all. Right now we treat them the same, and return
142 * LOAD_KERNEL_INVALID for both. */
143 int retval = LOAD_KERNEL_INVALID;
144 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED;
145
146 /* Setup NV storage */
147 VbNvSetup(vnc);
148
Bill Richardsone2729402010-07-22 12:23:47 -0700149 /* Sanity Checks */
150 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700151 !params->bytes_per_lba ||
152 !params->ending_lba ||
153 !params->kernel_buffer ||
154 !params->kernel_buffer_size) {
155 VBDEBUG(("LoadKernel() called with invalid params\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800156 goto LoadKernelExit;
Bill Richardsone2729402010-07-22 12:23:47 -0700157 }
158
159 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700160 blba = params->bytes_per_lba;
161 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700162 if (0 == kbuf_sectors) {
163 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800164 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700165 }
166
Randall Spangler640fb512011-03-03 10:11:17 -0800167 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
168 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
169
170 if (rec_switch)
171 boot_mode = kBootRecovery;
Randall Spangler8478ece2011-03-03 11:00:17 -0800172 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags) {
Randall Spangler640fb512011-03-03 10:11:17 -0800173 if (!dev_switch) {
174 /* Dev firmware should be signed such that it never boots with the dev
175 * switch is off; so something is terribly wrong. */
176 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
177 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
178 goto LoadKernelExit;
179 }
180 boot_mode = kBootDev;
Randall Spangler8478ece2011-03-03 11:00:17 -0800181 } else {
Randall Spangler640fb512011-03-03 10:11:17 -0800182 /* Normal firmware */
183 boot_mode = kBootNormal;
184 dev_switch = 0; /* Always do a fully verified boot */
Randall Spanglera8e0f942011-02-14 11:12:09 -0800185 }
Bill Richardsone2729402010-07-22 12:23:47 -0700186
Randall Spanglerd1836442010-06-10 09:59:04 -0700187 /* Clear output params in case we fail */
188 params->partition_number = 0;
189 params->bootloader_address = 0;
190 params->bootloader_size = 0;
191
Randall Spangler640fb512011-03-03 10:11:17 -0800192 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800193 /* Initialize the shared data structure, since LoadFirmware() didn't do it
194 * for us. */
195 if (0 != VbSharedDataInit(shared, params->shared_data_size)) {
196 /* Error initializing the shared data, but we can keep going. We just
197 * can't use the shared data. */
198 VBDEBUG(("Shared data init error\n"));
199 params->shared_data_size = 0;
200 shared = NULL;
201 }
202
203 /* Use the recovery key to verify the kernel */
204 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
205
206 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800207 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700208 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
209 /* Ignore return code, since we need to boot recovery mode to
210 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700211 }
Randall Spangler640fb512011-03-03 10:11:17 -0800212 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800213 /* Use the kernel subkey passed from LoadFirmware(). */
214 kernel_subkey = &shared->kernel_subkey;
215
Randall Spanglerd1836442010-06-10 09:59:04 -0700216 /* Read current kernel key index from TPM. Assumes TPM is already
217 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700218 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700219 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700220 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800221 if (status == TPM_E_MUST_REBOOT)
222 retval = LOAD_KERNEL_REBOOT;
223 else
224 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
225 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700226 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700227 }
228
229 do {
230 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700231 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700232 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700233 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700234 VBDEBUG(("Unable to read GPT data\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700235 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700236 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700237
238 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700239 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700240 VBDEBUG(("Error parsing GPT\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700241 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700242 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700243
Randall Spanglerd1836442010-06-10 09:59:04 -0700244 /* Allocate kernel header buffers */
245 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
246 if (!kbuf)
247 break;
248
249 /* Loop over candidate kernel partitions */
250 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
251 VbKeyBlockHeader* key_block;
252 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700253 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700254 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700255 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700256 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700257 uint64_t body_offset_sectors;
258 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800259 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700260
Randall Spanglere2ec9842010-06-23 21:17:07 -0700261 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
262 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700263
Randall Spanglerd1836442010-06-10 09:59:04 -0700264 /* Found at least one kernel partition. */
265 found_partitions++;
266
Randall Spangler640fb512011-03-03 10:11:17 -0800267 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700268 if (part_size < kbuf_sectors) {
269 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700270 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700271 }
272
273 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) {
274 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700275 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700276 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700277
Randall Spangler640fb512011-03-03 10:11:17 -0800278 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700279 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800280 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
281 VBDEBUG(("Verifying key block signature failed.\n"));
282 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700283
Randall Spangler640fb512011-03-03 10:11:17 -0800284 /* If we're not in developer mode, this kernel is bad. */
285 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700286 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800287
288 /* In developer mode, we can continue if the SHA-512 hash of the key
289 * block is valid. */
290 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
291 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700292 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700293 }
Randall Spangler695cd162010-06-15 23:38:23 -0700294 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700295
Randall Spangler640fb512011-03-03 10:11:17 -0800296 /* Check the key block flags against the current boot mode. */
297 if (!(key_block->key_block_flags &
298 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
299 KEY_BLOCK_FLAG_DEVELOPER_0))) {
300 VBDEBUG(("Key block developer flag mismatch.\n"));
301 key_block_valid = 0;
302 }
303 if (!(key_block->key_block_flags &
304 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
305 KEY_BLOCK_FLAG_RECOVERY_0))) {
306 VBDEBUG(("Key block recovery flag mismatch.\n"));
307 key_block_valid = 0;
308 }
309
310 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700311 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800312 if (kBootRecovery != boot_mode) {
313 if (key_version < (tpm_version >> 16)) {
314 VBDEBUG(("Key version too old.\n"));
315 key_block_valid = 0;
316 }
317 }
318
319 /* If we're not in developer mode, require the key block to be valid. */
320 if (kBootDev != boot_mode && !key_block_valid) {
321 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700322 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700323 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700324
Randall Spangler640fb512011-03-03 10:11:17 -0800325 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700326 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700327 if (!data_key) {
328 VBDEBUG(("Data key bad.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700329 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700330 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700331
332 /* Verify the preamble, which follows the key block */
333 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700334 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700335 KBUF_SIZE - key_block->key_block_size,
336 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700337 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700338 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700339 }
340
Randall Spangler640fb512011-03-03 10:11:17 -0800341 /* If the key block is valid and we're not in recovery mode, check for
342 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700343 combined_version = ((key_version << 16) |
344 (preamble->kernel_version & 0xFFFF));
Randall Spangler640fb512011-03-03 10:11:17 -0800345 if (key_block_valid && kBootRecovery != boot_mode) {
346 if (combined_version < tpm_version) {
347 VBDEBUG(("Kernel version too low.\n"));
348 /* If we're not in developer mode, kernel version must be valid. */
349 if (kBootDev != boot_mode)
350 goto bad_kernel;
351 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700352 }
353
Randall Spanglere2ec9842010-06-23 21:17:07 -0700354 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700355
Randall Spangler66680282010-08-16 12:33:44 -0700356 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800357 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700358 lowest_version = combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700359
360 /* If we already have a good kernel, no need to read another
361 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700362 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700363 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700364 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700365
366 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700367 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
368 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700369 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700370 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700371 }
372
373 /* Verify kernel body starts at a multiple of the sector size. */
374 body_offset = key_block->key_block_size + preamble->preamble_size;
375 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700376 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700377 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700378 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700379 body_offset_sectors = body_offset / blba;
380
381 /* Verify kernel body fits in the buffer */
382 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
383 if (body_sectors * blba > params->kernel_buffer_size) {
384 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700385 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700386 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700387
388 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700389 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700390 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700391 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700392 }
393
394 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700395 VBPERFSTART("VB_RKD");
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700396 if (0 != BootDeviceReadLBA(part_start + body_offset_sectors,
397 body_sectors,
398 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700399 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700400 VBPERFEND("VB_RKD");
Randall Spangler741d2b22010-08-20 16:37:12 -0700401 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700402 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700403 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700404
405 /* Verify kernel data */
406 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700407 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700408 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700409 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700410 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700411 }
412
413 /* Done with the kernel signing key, so can free it now */
414 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700415 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700416
417 /* If we're still here, the kernel is valid. */
418 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800419 VBDEBUG(("Partition is good.\n"));
420 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700421 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
422 * Adjust here, until cgptlib is fixed. */
423 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700424 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700425 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700426 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
427 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700428 params->bootloader_address = preamble->bootloader_address;
429 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700430
431 /* Update GPT to note this is the kernel we're trying */
432 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
433
Randall Spangler640fb512011-03-03 10:11:17 -0800434 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
435 * there's no rollback protection, so we can stop at the first valid
436 * kernel. */
437 if (kBootRecovery == boot_mode || !key_block_valid) {
438 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700439 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700440 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700441
Randall Spangler640fb512011-03-03 10:11:17 -0800442 /* Otherwise, we do care about the key index in the TPM. If the good
443 * partition's key version is the same as the tpm, then the TPM doesn't
444 * need updating; we can stop now. Otherwise, we'll check all the other
445 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700446 if (combined_version == tpm_version) {
447 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700448 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700449 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700450
451 /* Continue, so that we skip the error handling code below */
452 continue;
453
454 bad_kernel:
455 /* Handle errors parsing this kernel */
456 if (NULL != data_key)
457 RSAPublicKeyFree(data_key);
458
459 VBDEBUG(("Marking kernel as invalid.\n"));
460 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
461
462
Randall Spanglerd1836442010-06-10 09:59:04 -0700463 } /* while(GptNextKernelEntry) */
464 } while(0);
465
466 /* Free kernel buffer */
467 if (kbuf)
468 Free(kbuf);
469
470 /* Write and free GPT data */
471 WriteAndFreeGptData(&gpt);
472
473 /* Handle finding a good partition */
474 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700475 VBDEBUG(("Good_partition >= 0\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700476
477 /* See if we need to update the TPM */
Randall Spanglerc324fbf2011-03-04 12:19:25 -0800478 if (kBootRecovery != boot_mode && good_partition_key_block_valid) {
Randall Spangler640fb512011-03-03 10:11:17 -0800479 /* We only update the TPM in normal and developer boot modes. In
480 * developer mode, we only advanced lowest_version for kernels with valid
481 * key blocks, and didn't count self-signed key blocks. In recovery
482 * mode, the TPM stays PP-unlocked, so anything we write gets blown away
483 * by the firmware when we go back to normal mode. */
484 VBDEBUG(("Boot_flags = not recovery\n"));
Randall Spangler66680282010-08-16 12:33:44 -0700485 if (lowest_version > tpm_version) {
486 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700487 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700488 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800489 if (status == TPM_E_MUST_REBOOT)
490 retval = LOAD_KERNEL_REBOOT;
491 else
492 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
493 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700494 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700495 }
496 }
497
Randall Spangler63dffcb2010-08-05 15:13:14 -0700498 /* Lock the kernel versions */
499 status = RollbackKernelLock();
500 if (0 != status) {
501 VBDEBUG(("Error locking kernel versions.\n"));
502 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800503 if (kBootRecovery != boot_mode) {
504 if (status == TPM_E_MUST_REBOOT)
505 retval = LOAD_KERNEL_REBOOT;
506 else
507 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
508 goto LoadKernelExit;
509 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700510 }
511
512 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800513 retval = LOAD_KERNEL_SUCCESS;
Randall Spanglerd1836442010-06-10 09:59:04 -0700514 }
515
Randall Spangler640fb512011-03-03 10:11:17 -0800516LoadKernelExit:
517
518 /* Save whether the good partition's key block was fully verified */
519 VbNvSet(vnc, VBNV_FW_VERIFIED_KERNEL_KEY, good_partition_key_block_valid);
520
521 /* Store recovery request, if any, then tear down non-volatile storage */
522 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
523 recovery : VBNV_RECOVERY_NOT_REQUESTED);
524 VbNvTeardown(vnc);
525
Randall Spangler95c40312011-03-09 15:54:16 -0800526 /* Store how much shared data we used, if any */
527 if (shared)
528 params->shared_data_size = shared->data_used;
529
Randall Spangler640fb512011-03-03 10:11:17 -0800530 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700531}