blob: 84f6615c946f29015b79b18b23c084f64aab58c8 [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 */
Stefan Reinauer55db6a62011-03-15 16:23:41 -070021#define LOWEST_TPM_VERSION 0xffffffff
Randall Spanglerd1836442010-06-10 09:59:04 -070022
Randall Spangler640fb512011-03-03 10:11:17 -080023typedef enum BootMode {
24 kBootNormal, /* Normal firmware */
25 kBootDev, /* Dev firmware AND dev switch is on */
26 kBootRecovery /* Recovery firmware, regardless of dev switch position */
27} BootMode;
28
Randall Spangler83c88cf2010-06-11 16:14:18 -070029
30/* Allocates and reads GPT data from the drive. The sector_bytes and
31 * drive_sectors fields should be filled on input. The primary and
32 * secondary header and entries are filled on output.
33 *
34 * Returns 0 if successful, 1 if error. */
35int AllocAndReadGptData(GptData* gptdata) {
36
37 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
38
39 /* No data to be written yet */
40 gptdata->modified = 0;
41
42 /* Allocate all buffers */
43 gptdata->primary_header = (uint8_t*)Malloc(gptdata->sector_bytes);
44 gptdata->secondary_header = (uint8_t*)Malloc(gptdata->sector_bytes);
45 gptdata->primary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE);
46 gptdata->secondary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE);
47
48 if (gptdata->primary_header == NULL || gptdata->secondary_header == NULL ||
49 gptdata->primary_entries == NULL || gptdata->secondary_entries == NULL)
50 return 1;
51
52 /* Read data from the drive, skipping the protective MBR */
53 if (0 != BootDeviceReadLBA(1, 1, gptdata->primary_header))
54 return 1;
55 if (0 != BootDeviceReadLBA(2, entries_sectors, gptdata->primary_entries))
56 return 1;
57 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - entries_sectors - 1,
58 entries_sectors, gptdata->secondary_entries))
59 return 1;
60 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - 1,
61 1, gptdata->secondary_header))
62 return 1;
63
64 return 0;
65}
66
67
68/* Writes any changes for the GPT data back to the drive, then frees
69 * the buffers.
70 *
71 * Returns 0 if successful, 1 if error. */
72int WriteAndFreeGptData(GptData* gptdata) {
73
74 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
75
76 if (gptdata->primary_header) {
77 if (gptdata->modified & GPT_MODIFIED_HEADER1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070078 VBDEBUG(("Updating GPT header 1\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070079 if (0 != BootDeviceWriteLBA(1, 1, gptdata->primary_header))
80 return 1;
81 }
82 Free(gptdata->primary_header);
83 }
84
85 if (gptdata->primary_entries) {
86 if (gptdata->modified & GPT_MODIFIED_ENTRIES1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070087 VBDEBUG(("Updating GPT entries 1\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070088 if (0 != BootDeviceWriteLBA(2, entries_sectors,
89 gptdata->primary_entries))
90 return 1;
91 }
92 Free(gptdata->primary_entries);
93 }
94
95 if (gptdata->secondary_entries) {
96 if (gptdata->modified & GPT_MODIFIED_ENTRIES2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070097 VBDEBUG(("Updating GPT header 2\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070098 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - entries_sectors - 1,
99 entries_sectors, gptdata->secondary_entries))
100 return 1;
101 }
102 Free(gptdata->secondary_entries);
103 }
104
105 if (gptdata->secondary_header) {
106 if (gptdata->modified & GPT_MODIFIED_HEADER2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700107 VBDEBUG(("Updating GPT entries 2\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -0700108 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - 1, 1,
109 gptdata->secondary_header))
110 return 1;
111 }
112 Free(gptdata->secondary_header);
113 }
114
115 /* Success */
116 return 0;
117}
118
vbendeb3ecaf772010-06-24 16:19:53 -0700119/* disable MSVC warning on const logical expression (as in } while(0);) */
120__pragma(warning(disable: 4127))
Randall Spangler83c88cf2010-06-11 16:14:18 -0700121
Randall Spanglerbd529f02010-06-16 12:51:26 -0700122int LoadKernel(LoadKernelParams* params) {
Randall Spangler95c40312011-03-09 15:54:16 -0800123 VbSharedDataHeader* shared = (VbSharedDataHeader*)params->shared_data_blob;
Randall Spangler640fb512011-03-03 10:11:17 -0800124 VbNvContext* vnc = params->nv_context;
Randall Spangler95c40312011-03-09 15:54:16 -0800125 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)params->gbb_data;
Bill Richardsone2729402010-07-22 12:23:47 -0700126 VbPublicKey* kernel_subkey;
Randall Spanglerd1836442010-06-10 09:59:04 -0700127 GptData gpt;
128 uint64_t part_start, part_size;
Bill Richardsone2729402010-07-22 12:23:47 -0700129 uint64_t blba;
130 uint64_t kbuf_sectors;
Randall Spanglerd1836442010-06-10 09:59:04 -0700131 uint8_t* kbuf = NULL;
132 int found_partitions = 0;
133 int good_partition = -1;
Randall Spangler640fb512011-03-03 10:11:17 -0800134 int good_partition_key_block_valid = 0;
Randall Spangler66680282010-08-16 12:33:44 -0700135 uint32_t tpm_version = 0;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700136 uint64_t lowest_version = LOWEST_TPM_VERSION;
Randall Spangler640fb512011-03-03 10:11:17 -0800137 int rec_switch, dev_switch;
138 BootMode boot_mode;
Randall Spangler99ca3462011-03-15 15:28:31 -0700139 uint32_t test_err = 0;
Randall Spangler7a786b72010-07-08 13:29:42 -0700140 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700141
Randall Spangler640fb512011-03-03 10:11:17 -0800142 /* TODO: differentiate between finding an invalid kernel (found_partitions>0)
143 * and not finding one at all. Right now we treat them the same, and return
144 * LOAD_KERNEL_INVALID for both. */
145 int retval = LOAD_KERNEL_INVALID;
146 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED;
147
148 /* Setup NV storage */
149 VbNvSetup(vnc);
150
Bill Richardsone2729402010-07-22 12:23:47 -0700151 /* Sanity Checks */
152 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700153 !params->bytes_per_lba ||
154 !params->ending_lba ||
155 !params->kernel_buffer ||
156 !params->kernel_buffer_size) {
157 VBDEBUG(("LoadKernel() called with invalid params\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800158 goto LoadKernelExit;
Bill Richardsone2729402010-07-22 12:23:47 -0700159 }
160
Randall Spangler99ca3462011-03-15 15:28:31 -0700161 /* Handle test errors */
162 VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err);
163 if (VBNV_TEST_ERROR_LOAD_KERNEL == test_err) {
164 /* Get error code */
165 VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err);
166 /* Clear test params so we don't repeat the error */
167 VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0);
168 VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0);
169 /* Handle error codes */
170 switch (test_err) {
171 case LOAD_KERNEL_RECOVERY:
172 recovery = VBNV_RECOVERY_RW_TEST_LK;
173 goto LoadKernelExit;
174 case LOAD_KERNEL_NOT_FOUND:
175 case LOAD_KERNEL_INVALID:
176 case LOAD_KERNEL_REBOOT:
177 retval = test_err;
178 goto LoadKernelExit;
179 default:
180 break;
181 }
182 }
183
Bill Richardsone2729402010-07-22 12:23:47 -0700184 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700185 blba = params->bytes_per_lba;
186 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700187 if (0 == kbuf_sectors) {
188 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800189 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700190 }
191
Randall Spangler640fb512011-03-03 10:11:17 -0800192 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
193 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
194
195 if (rec_switch)
196 boot_mode = kBootRecovery;
Randall Spangler8478ece2011-03-03 11:00:17 -0800197 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags) {
Randall Spangler640fb512011-03-03 10:11:17 -0800198 if (!dev_switch) {
199 /* Dev firmware should be signed such that it never boots with the dev
200 * switch is off; so something is terribly wrong. */
201 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
202 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
203 goto LoadKernelExit;
204 }
205 boot_mode = kBootDev;
Randall Spangler8478ece2011-03-03 11:00:17 -0800206 } else {
Randall Spangler640fb512011-03-03 10:11:17 -0800207 /* Normal firmware */
208 boot_mode = kBootNormal;
209 dev_switch = 0; /* Always do a fully verified boot */
Randall Spanglera8e0f942011-02-14 11:12:09 -0800210 }
Bill Richardsone2729402010-07-22 12:23:47 -0700211
Randall Spanglerd1836442010-06-10 09:59:04 -0700212 /* Clear output params in case we fail */
213 params->partition_number = 0;
214 params->bootloader_address = 0;
215 params->bootloader_size = 0;
216
Randall Spangler640fb512011-03-03 10:11:17 -0800217 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800218 /* Initialize the shared data structure, since LoadFirmware() didn't do it
219 * for us. */
220 if (0 != VbSharedDataInit(shared, params->shared_data_size)) {
221 /* Error initializing the shared data, but we can keep going. We just
222 * can't use the shared data. */
223 VBDEBUG(("Shared data init error\n"));
224 params->shared_data_size = 0;
225 shared = NULL;
226 }
227
228 /* Use the recovery key to verify the kernel */
229 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
230
231 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800232 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700233 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
234 /* Ignore return code, since we need to boot recovery mode to
235 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700236 }
Randall Spangler640fb512011-03-03 10:11:17 -0800237 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800238 /* Use the kernel subkey passed from LoadFirmware(). */
239 kernel_subkey = &shared->kernel_subkey;
240
Randall Spanglerd1836442010-06-10 09:59:04 -0700241 /* Read current kernel key index from TPM. Assumes TPM is already
242 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700243 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700244 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700245 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800246 if (status == TPM_E_MUST_REBOOT)
247 retval = LOAD_KERNEL_REBOOT;
248 else
249 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
250 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700251 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700252 }
253
254 do {
255 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700256 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700257 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700258 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700259 VBDEBUG(("Unable to read GPT data\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700260 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700261 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700262
263 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700264 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700265 VBDEBUG(("Error parsing GPT\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700266 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700267 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700268
Randall Spanglerd1836442010-06-10 09:59:04 -0700269 /* Allocate kernel header buffers */
270 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
271 if (!kbuf)
272 break;
273
274 /* Loop over candidate kernel partitions */
275 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
276 VbKeyBlockHeader* key_block;
277 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700278 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700279 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700280 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700281 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700282 uint64_t body_offset_sectors;
283 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800284 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700285
Randall Spanglere2ec9842010-06-23 21:17:07 -0700286 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
287 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700288
Randall Spanglerd1836442010-06-10 09:59:04 -0700289 /* Found at least one kernel partition. */
290 found_partitions++;
291
Randall Spangler640fb512011-03-03 10:11:17 -0800292 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700293 if (part_size < kbuf_sectors) {
294 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700295 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700296 }
297
298 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) {
299 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700300 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700301 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700302
Randall Spangler640fb512011-03-03 10:11:17 -0800303 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700304 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800305 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
306 VBDEBUG(("Verifying key block signature failed.\n"));
307 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700308
Randall Spangler640fb512011-03-03 10:11:17 -0800309 /* If we're not in developer mode, this kernel is bad. */
310 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700311 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800312
313 /* In developer mode, we can continue if the SHA-512 hash of the key
314 * block is valid. */
315 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
316 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700317 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700318 }
Randall Spangler695cd162010-06-15 23:38:23 -0700319 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700320
Randall Spangler640fb512011-03-03 10:11:17 -0800321 /* Check the key block flags against the current boot mode. */
322 if (!(key_block->key_block_flags &
323 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
324 KEY_BLOCK_FLAG_DEVELOPER_0))) {
325 VBDEBUG(("Key block developer flag mismatch.\n"));
326 key_block_valid = 0;
327 }
328 if (!(key_block->key_block_flags &
329 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
330 KEY_BLOCK_FLAG_RECOVERY_0))) {
331 VBDEBUG(("Key block recovery flag mismatch.\n"));
332 key_block_valid = 0;
333 }
334
335 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700336 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800337 if (kBootRecovery != boot_mode) {
338 if (key_version < (tpm_version >> 16)) {
339 VBDEBUG(("Key version too old.\n"));
340 key_block_valid = 0;
341 }
342 }
343
344 /* If we're not in developer mode, require the key block to be valid. */
345 if (kBootDev != boot_mode && !key_block_valid) {
346 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700347 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700348 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700349
Randall Spangler640fb512011-03-03 10:11:17 -0800350 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700351 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700352 if (!data_key) {
353 VBDEBUG(("Data key bad.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700354 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700355 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700356
357 /* Verify the preamble, which follows the key block */
358 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700359 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700360 KBUF_SIZE - key_block->key_block_size,
361 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700362 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700363 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700364 }
365
Randall Spangler640fb512011-03-03 10:11:17 -0800366 /* If the key block is valid and we're not in recovery mode, check for
367 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700368 combined_version = ((key_version << 16) |
369 (preamble->kernel_version & 0xFFFF));
Randall Spangler640fb512011-03-03 10:11:17 -0800370 if (key_block_valid && kBootRecovery != boot_mode) {
371 if (combined_version < tpm_version) {
372 VBDEBUG(("Kernel version too low.\n"));
373 /* If we're not in developer mode, kernel version must be valid. */
374 if (kBootDev != boot_mode)
375 goto bad_kernel;
376 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700377 }
378
Randall Spanglere2ec9842010-06-23 21:17:07 -0700379 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700380
Randall Spangler66680282010-08-16 12:33:44 -0700381 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800382 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700383 lowest_version = combined_version;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700384 else {
385 VBDEBUG(("Key block valid: %d\n", key_block_valid));
386 VBDEBUG(("Combined version: %" PRIu64 "\n", combined_version));
387 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700388
389 /* If we already have a good kernel, no need to read another
390 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700391 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700392 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700393 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700394
395 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700396 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
397 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700398 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700399 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700400 }
401
402 /* Verify kernel body starts at a multiple of the sector size. */
403 body_offset = key_block->key_block_size + preamble->preamble_size;
404 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700405 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700406 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700407 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700408 body_offset_sectors = body_offset / blba;
409
410 /* Verify kernel body fits in the buffer */
411 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
412 if (body_sectors * blba > params->kernel_buffer_size) {
413 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700414 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700415 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700416
417 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700418 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700419 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700420 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700421 }
422
423 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700424 VBPERFSTART("VB_RKD");
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700425 if (0 != BootDeviceReadLBA(part_start + body_offset_sectors,
426 body_sectors,
427 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700428 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700429 VBPERFEND("VB_RKD");
Randall Spangler741d2b22010-08-20 16:37:12 -0700430 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700431 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700432 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700433
434 /* Verify kernel data */
435 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700436 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700437 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700438 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700439 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700440 }
441
442 /* Done with the kernel signing key, so can free it now */
443 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700444 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700445
446 /* If we're still here, the kernel is valid. */
447 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800448 VBDEBUG(("Partition is good.\n"));
449 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700450 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
451 * Adjust here, until cgptlib is fixed. */
452 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700453 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700454 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700455 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
456 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700457 params->bootloader_address = preamble->bootloader_address;
458 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700459
460 /* Update GPT to note this is the kernel we're trying */
461 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
462
Randall Spangler640fb512011-03-03 10:11:17 -0800463 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
464 * there's no rollback protection, so we can stop at the first valid
465 * kernel. */
466 if (kBootRecovery == boot_mode || !key_block_valid) {
467 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700468 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700469 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700470
Randall Spangler640fb512011-03-03 10:11:17 -0800471 /* Otherwise, we do care about the key index in the TPM. If the good
472 * partition's key version is the same as the tpm, then the TPM doesn't
473 * need updating; we can stop now. Otherwise, we'll check all the other
474 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700475 if (combined_version == tpm_version) {
476 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700477 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700478 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700479
480 /* Continue, so that we skip the error handling code below */
481 continue;
482
483 bad_kernel:
484 /* Handle errors parsing this kernel */
485 if (NULL != data_key)
486 RSAPublicKeyFree(data_key);
487
488 VBDEBUG(("Marking kernel as invalid.\n"));
489 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
490
491
Randall Spanglerd1836442010-06-10 09:59:04 -0700492 } /* while(GptNextKernelEntry) */
493 } while(0);
494
495 /* Free kernel buffer */
496 if (kbuf)
497 Free(kbuf);
498
499 /* Write and free GPT data */
500 WriteAndFreeGptData(&gpt);
501
502 /* Handle finding a good partition */
503 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700504 VBDEBUG(("Good_partition >= 0\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700505
506 /* See if we need to update the TPM */
Randall Spanglerc324fbf2011-03-04 12:19:25 -0800507 if (kBootRecovery != boot_mode && good_partition_key_block_valid) {
Randall Spangler640fb512011-03-03 10:11:17 -0800508 /* We only update the TPM in normal and developer boot modes. In
509 * developer mode, we only advanced lowest_version for kernels with valid
510 * key blocks, and didn't count self-signed key blocks. In recovery
511 * mode, the TPM stays PP-unlocked, so anything we write gets blown away
512 * by the firmware when we go back to normal mode. */
513 VBDEBUG(("Boot_flags = not recovery\n"));
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700514
515 if ((lowest_version > tpm_version) &&
516 (lowest_version != LOWEST_TPM_VERSION)) {
Randall Spangler66680282010-08-16 12:33:44 -0700517 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700518 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700519 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800520 if (status == TPM_E_MUST_REBOOT)
521 retval = LOAD_KERNEL_REBOOT;
522 else
523 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
524 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700525 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700526 }
527 }
528
Randall Spangler63dffcb2010-08-05 15:13:14 -0700529 /* Lock the kernel versions */
530 status = RollbackKernelLock();
531 if (0 != status) {
532 VBDEBUG(("Error locking kernel versions.\n"));
533 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800534 if (kBootRecovery != boot_mode) {
535 if (status == TPM_E_MUST_REBOOT)
536 retval = LOAD_KERNEL_REBOOT;
537 else
538 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
539 goto LoadKernelExit;
540 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700541 }
542
543 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800544 retval = LOAD_KERNEL_SUCCESS;
Randall Spanglerd1836442010-06-10 09:59:04 -0700545 }
546
Randall Spangler640fb512011-03-03 10:11:17 -0800547LoadKernelExit:
548
549 /* Save whether the good partition's key block was fully verified */
550 VbNvSet(vnc, VBNV_FW_VERIFIED_KERNEL_KEY, good_partition_key_block_valid);
551
552 /* Store recovery request, if any, then tear down non-volatile storage */
553 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
554 recovery : VBNV_RECOVERY_NOT_REQUESTED);
555 VbNvTeardown(vnc);
556
Randall Spangler95c40312011-03-09 15:54:16 -0800557 /* Store how much shared data we used, if any */
558 if (shared)
559 params->shared_data_size = shared->data_used;
560
Randall Spangler640fb512011-03-03 10:11:17 -0800561 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700562}