blob: faa9c48a7204da331204446767a4cefebf877c4f [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 Spangler99ca3462011-03-15 15:28:31 -0700138 uint32_t test_err = 0;
Randall Spangler7a786b72010-07-08 13:29:42 -0700139 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700140
Randall Spangler640fb512011-03-03 10:11:17 -0800141 /* TODO: differentiate between finding an invalid kernel (found_partitions>0)
142 * and not finding one at all. Right now we treat them the same, and return
143 * LOAD_KERNEL_INVALID for both. */
144 int retval = LOAD_KERNEL_INVALID;
145 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED;
146
147 /* Setup NV storage */
148 VbNvSetup(vnc);
149
Bill Richardsone2729402010-07-22 12:23:47 -0700150 /* Sanity Checks */
151 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700152 !params->bytes_per_lba ||
153 !params->ending_lba ||
154 !params->kernel_buffer ||
155 !params->kernel_buffer_size) {
156 VBDEBUG(("LoadKernel() called with invalid params\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800157 goto LoadKernelExit;
Bill Richardsone2729402010-07-22 12:23:47 -0700158 }
159
Randall Spangler99ca3462011-03-15 15:28:31 -0700160 /* Handle test errors */
161 VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err);
162 if (VBNV_TEST_ERROR_LOAD_KERNEL == test_err) {
163 /* Get error code */
164 VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err);
165 /* Clear test params so we don't repeat the error */
166 VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0);
167 VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0);
168 /* Handle error codes */
169 switch (test_err) {
170 case LOAD_KERNEL_RECOVERY:
171 recovery = VBNV_RECOVERY_RW_TEST_LK;
172 goto LoadKernelExit;
173 case LOAD_KERNEL_NOT_FOUND:
174 case LOAD_KERNEL_INVALID:
175 case LOAD_KERNEL_REBOOT:
176 retval = test_err;
177 goto LoadKernelExit;
178 default:
179 break;
180 }
181 }
182
Bill Richardsone2729402010-07-22 12:23:47 -0700183 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700184 blba = params->bytes_per_lba;
185 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700186 if (0 == kbuf_sectors) {
187 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800188 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700189 }
190
Randall Spangler640fb512011-03-03 10:11:17 -0800191 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
192 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
193
194 if (rec_switch)
195 boot_mode = kBootRecovery;
Randall Spangler8478ece2011-03-03 11:00:17 -0800196 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags) {
Randall Spangler640fb512011-03-03 10:11:17 -0800197 if (!dev_switch) {
198 /* Dev firmware should be signed such that it never boots with the dev
199 * switch is off; so something is terribly wrong. */
200 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
201 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
202 goto LoadKernelExit;
203 }
204 boot_mode = kBootDev;
Randall Spangler8478ece2011-03-03 11:00:17 -0800205 } else {
Randall Spangler640fb512011-03-03 10:11:17 -0800206 /* Normal firmware */
207 boot_mode = kBootNormal;
208 dev_switch = 0; /* Always do a fully verified boot */
Randall Spanglera8e0f942011-02-14 11:12:09 -0800209 }
Bill Richardsone2729402010-07-22 12:23:47 -0700210
Randall Spanglerd1836442010-06-10 09:59:04 -0700211 /* Clear output params in case we fail */
212 params->partition_number = 0;
213 params->bootloader_address = 0;
214 params->bootloader_size = 0;
215
Randall Spangler640fb512011-03-03 10:11:17 -0800216 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800217 /* Initialize the shared data structure, since LoadFirmware() didn't do it
218 * for us. */
219 if (0 != VbSharedDataInit(shared, params->shared_data_size)) {
220 /* Error initializing the shared data, but we can keep going. We just
221 * can't use the shared data. */
222 VBDEBUG(("Shared data init error\n"));
223 params->shared_data_size = 0;
224 shared = NULL;
225 }
226
227 /* Use the recovery key to verify the kernel */
228 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
229
230 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800231 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700232 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
233 /* Ignore return code, since we need to boot recovery mode to
234 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700235 }
Randall Spangler640fb512011-03-03 10:11:17 -0800236 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800237 /* Use the kernel subkey passed from LoadFirmware(). */
238 kernel_subkey = &shared->kernel_subkey;
239
Randall Spanglerd1836442010-06-10 09:59:04 -0700240 /* Read current kernel key index from TPM. Assumes TPM is already
241 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700242 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700243 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700244 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800245 if (status == TPM_E_MUST_REBOOT)
246 retval = LOAD_KERNEL_REBOOT;
247 else
248 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
249 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700250 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700251 }
252
253 do {
254 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700255 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700256 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700257 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700258 VBDEBUG(("Unable to read GPT data\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700259 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700260 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700261
262 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700263 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700264 VBDEBUG(("Error parsing GPT\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700265 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700266 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700267
Randall Spanglerd1836442010-06-10 09:59:04 -0700268 /* Allocate kernel header buffers */
269 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
270 if (!kbuf)
271 break;
272
273 /* Loop over candidate kernel partitions */
274 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
275 VbKeyBlockHeader* key_block;
276 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700277 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700278 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700279 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700280 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700281 uint64_t body_offset_sectors;
282 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800283 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700284
Randall Spanglere2ec9842010-06-23 21:17:07 -0700285 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
286 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700287
Randall Spanglerd1836442010-06-10 09:59:04 -0700288 /* Found at least one kernel partition. */
289 found_partitions++;
290
Randall Spangler640fb512011-03-03 10:11:17 -0800291 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700292 if (part_size < kbuf_sectors) {
293 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700294 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700295 }
296
297 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) {
298 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700299 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700300 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700301
Randall Spangler640fb512011-03-03 10:11:17 -0800302 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700303 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800304 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
305 VBDEBUG(("Verifying key block signature failed.\n"));
306 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700307
Randall Spangler640fb512011-03-03 10:11:17 -0800308 /* If we're not in developer mode, this kernel is bad. */
309 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700310 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800311
312 /* In developer mode, we can continue if the SHA-512 hash of the key
313 * block is valid. */
314 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
315 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700316 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700317 }
Randall Spangler695cd162010-06-15 23:38:23 -0700318 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700319
Randall Spangler640fb512011-03-03 10:11:17 -0800320 /* Check the key block flags against the current boot mode. */
321 if (!(key_block->key_block_flags &
322 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
323 KEY_BLOCK_FLAG_DEVELOPER_0))) {
324 VBDEBUG(("Key block developer flag mismatch.\n"));
325 key_block_valid = 0;
326 }
327 if (!(key_block->key_block_flags &
328 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
329 KEY_BLOCK_FLAG_RECOVERY_0))) {
330 VBDEBUG(("Key block recovery flag mismatch.\n"));
331 key_block_valid = 0;
332 }
333
334 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700335 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800336 if (kBootRecovery != boot_mode) {
337 if (key_version < (tpm_version >> 16)) {
338 VBDEBUG(("Key version too old.\n"));
339 key_block_valid = 0;
340 }
341 }
342
343 /* If we're not in developer mode, require the key block to be valid. */
344 if (kBootDev != boot_mode && !key_block_valid) {
345 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700346 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700347 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700348
Randall Spangler640fb512011-03-03 10:11:17 -0800349 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700350 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700351 if (!data_key) {
352 VBDEBUG(("Data key bad.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700353 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700354 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700355
356 /* Verify the preamble, which follows the key block */
357 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700358 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700359 KBUF_SIZE - key_block->key_block_size,
360 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700361 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700362 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700363 }
364
Randall Spangler640fb512011-03-03 10:11:17 -0800365 /* If the key block is valid and we're not in recovery mode, check for
366 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700367 combined_version = ((key_version << 16) |
368 (preamble->kernel_version & 0xFFFF));
Randall Spangler640fb512011-03-03 10:11:17 -0800369 if (key_block_valid && kBootRecovery != boot_mode) {
370 if (combined_version < tpm_version) {
371 VBDEBUG(("Kernel version too low.\n"));
372 /* If we're not in developer mode, kernel version must be valid. */
373 if (kBootDev != boot_mode)
374 goto bad_kernel;
375 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700376 }
377
Randall Spanglere2ec9842010-06-23 21:17:07 -0700378 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700379
Randall Spangler66680282010-08-16 12:33:44 -0700380 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800381 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700382 lowest_version = combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700383
384 /* If we already have a good kernel, no need to read another
385 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700386 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700387 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700388 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700389
390 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700391 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
392 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700393 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700394 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700395 }
396
397 /* Verify kernel body starts at a multiple of the sector size. */
398 body_offset = key_block->key_block_size + preamble->preamble_size;
399 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700400 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700401 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700402 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700403 body_offset_sectors = body_offset / blba;
404
405 /* Verify kernel body fits in the buffer */
406 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
407 if (body_sectors * blba > params->kernel_buffer_size) {
408 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700409 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700410 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700411
412 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700413 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700414 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700415 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700416 }
417
418 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700419 VBPERFSTART("VB_RKD");
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700420 if (0 != BootDeviceReadLBA(part_start + body_offset_sectors,
421 body_sectors,
422 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700423 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700424 VBPERFEND("VB_RKD");
Randall Spangler741d2b22010-08-20 16:37:12 -0700425 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700426 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700427 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700428
429 /* Verify kernel data */
430 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700431 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700432 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700433 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700434 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700435 }
436
437 /* Done with the kernel signing key, so can free it now */
438 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700439 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700440
441 /* If we're still here, the kernel is valid. */
442 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800443 VBDEBUG(("Partition is good.\n"));
444 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700445 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
446 * Adjust here, until cgptlib is fixed. */
447 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700448 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700449 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700450 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
451 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700452 params->bootloader_address = preamble->bootloader_address;
453 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700454
455 /* Update GPT to note this is the kernel we're trying */
456 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
457
Randall Spangler640fb512011-03-03 10:11:17 -0800458 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
459 * there's no rollback protection, so we can stop at the first valid
460 * kernel. */
461 if (kBootRecovery == boot_mode || !key_block_valid) {
462 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700463 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700464 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700465
Randall Spangler640fb512011-03-03 10:11:17 -0800466 /* Otherwise, we do care about the key index in the TPM. If the good
467 * partition's key version is the same as the tpm, then the TPM doesn't
468 * need updating; we can stop now. Otherwise, we'll check all the other
469 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700470 if (combined_version == tpm_version) {
471 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700472 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700473 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700474
475 /* Continue, so that we skip the error handling code below */
476 continue;
477
478 bad_kernel:
479 /* Handle errors parsing this kernel */
480 if (NULL != data_key)
481 RSAPublicKeyFree(data_key);
482
483 VBDEBUG(("Marking kernel as invalid.\n"));
484 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
485
486
Randall Spanglerd1836442010-06-10 09:59:04 -0700487 } /* while(GptNextKernelEntry) */
488 } while(0);
489
490 /* Free kernel buffer */
491 if (kbuf)
492 Free(kbuf);
493
494 /* Write and free GPT data */
495 WriteAndFreeGptData(&gpt);
496
497 /* Handle finding a good partition */
498 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700499 VBDEBUG(("Good_partition >= 0\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700500
501 /* See if we need to update the TPM */
Randall Spanglerc324fbf2011-03-04 12:19:25 -0800502 if (kBootRecovery != boot_mode && good_partition_key_block_valid) {
Randall Spangler640fb512011-03-03 10:11:17 -0800503 /* We only update the TPM in normal and developer boot modes. In
504 * developer mode, we only advanced lowest_version for kernels with valid
505 * key blocks, and didn't count self-signed key blocks. In recovery
506 * mode, the TPM stays PP-unlocked, so anything we write gets blown away
507 * by the firmware when we go back to normal mode. */
508 VBDEBUG(("Boot_flags = not recovery\n"));
Randall Spangler66680282010-08-16 12:33:44 -0700509 if (lowest_version > tpm_version) {
510 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700511 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700512 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800513 if (status == TPM_E_MUST_REBOOT)
514 retval = LOAD_KERNEL_REBOOT;
515 else
516 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
517 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700518 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700519 }
520 }
521
Randall Spangler63dffcb2010-08-05 15:13:14 -0700522 /* Lock the kernel versions */
523 status = RollbackKernelLock();
524 if (0 != status) {
525 VBDEBUG(("Error locking kernel versions.\n"));
526 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800527 if (kBootRecovery != boot_mode) {
528 if (status == TPM_E_MUST_REBOOT)
529 retval = LOAD_KERNEL_REBOOT;
530 else
531 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
532 goto LoadKernelExit;
533 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700534 }
535
536 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800537 retval = LOAD_KERNEL_SUCCESS;
Randall Spanglerd1836442010-06-10 09:59:04 -0700538 }
539
Randall Spangler640fb512011-03-03 10:11:17 -0800540LoadKernelExit:
541
542 /* Save whether the good partition's key block was fully verified */
543 VbNvSet(vnc, VBNV_FW_VERIFIED_KERNEL_KEY, good_partition_key_block_valid);
544
545 /* Store recovery request, if any, then tear down non-volatile storage */
546 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
547 recovery : VBNV_RECOVERY_NOT_REQUESTED);
548 VbNvTeardown(vnc);
549
Randall Spangler95c40312011-03-09 15:54:16 -0800550 /* Store how much shared data we used, if any */
551 if (shared)
552 params->shared_data_size = shared->data_used;
553
Randall Spangler640fb512011-03-03 10:11:17 -0800554 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700555}