blob: 204874f6e03c434f23d7e614595679e0366bd029 [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 Spangler02223552011-03-15 19:20:25 -0700142 int retval = LOAD_KERNEL_RECOVERY;
Randall Spangler640fb512011-03-03 10:11:17 -0800143 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED;
Randall Spanglerad6824b2011-03-16 19:07:33 -0700144 uint64_t timer_enter = VbGetTimer();
Randall Spangler640fb512011-03-03 10:11:17 -0800145
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
Randall Spanglerad6824b2011-03-16 19:07:33 -0700159 /* Clear output params in case we fail */
160 params->partition_number = 0;
161 params->bootloader_address = 0;
162 params->bootloader_size = 0;
163
Randall Spangler99ca3462011-03-15 15:28:31 -0700164 /* Handle test errors */
165 VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err);
166 if (VBNV_TEST_ERROR_LOAD_KERNEL == test_err) {
167 /* Get error code */
168 VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err);
169 /* Clear test params so we don't repeat the error */
170 VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0);
171 VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0);
172 /* Handle error codes */
173 switch (test_err) {
174 case LOAD_KERNEL_RECOVERY:
175 recovery = VBNV_RECOVERY_RW_TEST_LK;
176 goto LoadKernelExit;
177 case LOAD_KERNEL_NOT_FOUND:
178 case LOAD_KERNEL_INVALID:
179 case LOAD_KERNEL_REBOOT:
180 retval = test_err;
181 goto LoadKernelExit;
182 default:
183 break;
184 }
185 }
186
Bill Richardsone2729402010-07-22 12:23:47 -0700187 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700188 blba = params->bytes_per_lba;
189 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700190 if (0 == kbuf_sectors) {
191 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800192 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700193 }
194
Randall Spangler640fb512011-03-03 10:11:17 -0800195 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
196 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
197
198 if (rec_switch)
199 boot_mode = kBootRecovery;
Randall Spangler8478ece2011-03-03 11:00:17 -0800200 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags) {
Randall Spangler640fb512011-03-03 10:11:17 -0800201 if (!dev_switch) {
202 /* Dev firmware should be signed such that it never boots with the dev
203 * switch is off; so something is terribly wrong. */
204 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
205 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
206 goto LoadKernelExit;
207 }
208 boot_mode = kBootDev;
Randall Spangler8478ece2011-03-03 11:00:17 -0800209 } else {
Randall Spangler640fb512011-03-03 10:11:17 -0800210 /* Normal firmware */
211 boot_mode = kBootNormal;
212 dev_switch = 0; /* Always do a fully verified boot */
Randall Spanglera8e0f942011-02-14 11:12:09 -0800213 }
Bill Richardsone2729402010-07-22 12:23:47 -0700214
Randall Spangler640fb512011-03-03 10:11:17 -0800215 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800216 /* Initialize the shared data structure, since LoadFirmware() didn't do it
217 * for us. */
218 if (0 != VbSharedDataInit(shared, params->shared_data_size)) {
219 /* Error initializing the shared data, but we can keep going. We just
220 * can't use the shared data. */
221 VBDEBUG(("Shared data init error\n"));
222 params->shared_data_size = 0;
223 shared = NULL;
224 }
225
226 /* Use the recovery key to verify the kernel */
227 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
228
229 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800230 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700231 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
232 /* Ignore return code, since we need to boot recovery mode to
233 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700234 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700235
236 /* Read the key indices from the TPM; ignore any errors */
237 if (shared) {
238 RollbackFirmwareRead(&shared->fw_version_tpm);
239 RollbackKernelRead(&shared->kernel_version_tpm);
240 }
Randall Spangler640fb512011-03-03 10:11:17 -0800241 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800242 /* Use the kernel subkey passed from LoadFirmware(). */
243 kernel_subkey = &shared->kernel_subkey;
244
Randall Spanglerd1836442010-06-10 09:59:04 -0700245 /* Read current kernel key index from TPM. Assumes TPM is already
246 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700247 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700248 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700249 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800250 if (status == TPM_E_MUST_REBOOT)
251 retval = LOAD_KERNEL_REBOOT;
252 else
253 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
254 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700255 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700256 if (shared)
257 shared->kernel_version_tpm = tpm_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700258 }
259
260 do {
261 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700262 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700263 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700264 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700265 VBDEBUG(("Unable to read GPT data\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
269 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700270 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700271 VBDEBUG(("Error parsing GPT\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700272 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700273 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700274
Randall Spanglerd1836442010-06-10 09:59:04 -0700275 /* Allocate kernel header buffers */
276 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
277 if (!kbuf)
278 break;
279
280 /* Loop over candidate kernel partitions */
281 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
282 VbKeyBlockHeader* key_block;
283 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700284 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700285 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700286 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700287 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700288 uint64_t body_offset_sectors;
289 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800290 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700291
Randall Spanglere2ec9842010-06-23 21:17:07 -0700292 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
293 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700294
Randall Spanglerd1836442010-06-10 09:59:04 -0700295 /* Found at least one kernel partition. */
296 found_partitions++;
297
Randall Spangler640fb512011-03-03 10:11:17 -0800298 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700299 if (part_size < kbuf_sectors) {
300 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700301 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700302 }
303
304 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) {
305 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700306 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700307 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700308
Randall Spangler640fb512011-03-03 10:11:17 -0800309 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700310 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800311 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
312 VBDEBUG(("Verifying key block signature failed.\n"));
313 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700314
Randall Spangler640fb512011-03-03 10:11:17 -0800315 /* If we're not in developer mode, this kernel is bad. */
316 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700317 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800318
319 /* In developer mode, we can continue if the SHA-512 hash of the key
320 * block is valid. */
321 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
322 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700323 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700324 }
Randall Spangler695cd162010-06-15 23:38:23 -0700325 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700326
Randall Spangler640fb512011-03-03 10:11:17 -0800327 /* Check the key block flags against the current boot mode. */
328 if (!(key_block->key_block_flags &
329 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
330 KEY_BLOCK_FLAG_DEVELOPER_0))) {
331 VBDEBUG(("Key block developer flag mismatch.\n"));
332 key_block_valid = 0;
333 }
334 if (!(key_block->key_block_flags &
335 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
336 KEY_BLOCK_FLAG_RECOVERY_0))) {
337 VBDEBUG(("Key block recovery flag mismatch.\n"));
338 key_block_valid = 0;
339 }
340
341 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700342 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800343 if (kBootRecovery != boot_mode) {
344 if (key_version < (tpm_version >> 16)) {
345 VBDEBUG(("Key version too old.\n"));
346 key_block_valid = 0;
347 }
348 }
349
350 /* If we're not in developer mode, require the key block to be valid. */
351 if (kBootDev != boot_mode && !key_block_valid) {
352 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700353 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700354 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700355
Randall Spangler640fb512011-03-03 10:11:17 -0800356 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700357 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700358 if (!data_key) {
359 VBDEBUG(("Data key bad.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700360 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700361 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700362
363 /* Verify the preamble, which follows the key block */
364 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700365 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700366 KBUF_SIZE - key_block->key_block_size,
367 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700368 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700369 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700370 }
371
Randall Spangler640fb512011-03-03 10:11:17 -0800372 /* If the key block is valid and we're not in recovery mode, check for
373 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700374 combined_version = ((key_version << 16) |
375 (preamble->kernel_version & 0xFFFF));
Randall Spangler640fb512011-03-03 10:11:17 -0800376 if (key_block_valid && kBootRecovery != boot_mode) {
377 if (combined_version < tpm_version) {
378 VBDEBUG(("Kernel version too low.\n"));
379 /* If we're not in developer mode, kernel version must be valid. */
380 if (kBootDev != boot_mode)
381 goto bad_kernel;
382 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700383 }
384
Randall Spanglere2ec9842010-06-23 21:17:07 -0700385 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700386
Randall Spangler66680282010-08-16 12:33:44 -0700387 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800388 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700389 lowest_version = combined_version;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700390 else {
391 VBDEBUG(("Key block valid: %d\n", key_block_valid));
392 VBDEBUG(("Combined version: %" PRIu64 "\n", combined_version));
393 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700394
395 /* If we already have a good kernel, no need to read another
396 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700397 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700398 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700399 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700400
401 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700402 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
403 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700404 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700405 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700406 }
407
408 /* Verify kernel body starts at a multiple of the sector size. */
409 body_offset = key_block->key_block_size + preamble->preamble_size;
410 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700411 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700412 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700413 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700414 body_offset_sectors = body_offset / blba;
415
416 /* Verify kernel body fits in the buffer */
417 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
418 if (body_sectors * blba > params->kernel_buffer_size) {
419 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700420 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700421 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700422
423 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700424 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700425 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700426 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700427 }
428
429 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700430 VBPERFSTART("VB_RKD");
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700431 if (0 != BootDeviceReadLBA(part_start + body_offset_sectors,
432 body_sectors,
433 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700434 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700435 VBPERFEND("VB_RKD");
Randall Spangler741d2b22010-08-20 16:37:12 -0700436 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700437 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700438 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700439
440 /* Verify kernel data */
441 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700442 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700443 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700444 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700445 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700446 }
447
448 /* Done with the kernel signing key, so can free it now */
449 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700450 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700451
452 /* If we're still here, the kernel is valid. */
453 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800454 VBDEBUG(("Partition is good.\n"));
455 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700456 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
457 * Adjust here, until cgptlib is fixed. */
458 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700459 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700460 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700461 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
462 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700463 params->bootloader_address = preamble->bootloader_address;
464 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700465
466 /* Update GPT to note this is the kernel we're trying */
467 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
468
Randall Spangler640fb512011-03-03 10:11:17 -0800469 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
470 * there's no rollback protection, so we can stop at the first valid
471 * kernel. */
472 if (kBootRecovery == boot_mode || !key_block_valid) {
473 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700474 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700475 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700476
Randall Spangler640fb512011-03-03 10:11:17 -0800477 /* Otherwise, we do care about the key index in the TPM. If the good
478 * partition's key version is the same as the tpm, then the TPM doesn't
479 * need updating; we can stop now. Otherwise, we'll check all the other
480 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700481 if (combined_version == tpm_version) {
482 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700483 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700484 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700485
486 /* Continue, so that we skip the error handling code below */
487 continue;
488
489 bad_kernel:
490 /* Handle errors parsing this kernel */
491 if (NULL != data_key)
492 RSAPublicKeyFree(data_key);
493
494 VBDEBUG(("Marking kernel as invalid.\n"));
495 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
496
497
Randall Spanglerd1836442010-06-10 09:59:04 -0700498 } /* while(GptNextKernelEntry) */
499 } while(0);
500
501 /* Free kernel buffer */
502 if (kbuf)
503 Free(kbuf);
504
505 /* Write and free GPT data */
506 WriteAndFreeGptData(&gpt);
507
508 /* Handle finding a good partition */
509 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700510 VBDEBUG(("Good_partition >= 0\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700511
512 /* See if we need to update the TPM */
Randall Spanglerc324fbf2011-03-04 12:19:25 -0800513 if (kBootRecovery != boot_mode && good_partition_key_block_valid) {
Randall Spangler640fb512011-03-03 10:11:17 -0800514 /* We only update the TPM in normal and developer boot modes. In
515 * developer mode, we only advanced lowest_version for kernels with valid
516 * key blocks, and didn't count self-signed key blocks. In recovery
517 * mode, the TPM stays PP-unlocked, so anything we write gets blown away
518 * by the firmware when we go back to normal mode. */
519 VBDEBUG(("Boot_flags = not recovery\n"));
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700520
521 if ((lowest_version > tpm_version) &&
522 (lowest_version != LOWEST_TPM_VERSION)) {
Randall Spangler66680282010-08-16 12:33:44 -0700523 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700524 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700525 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800526 if (status == TPM_E_MUST_REBOOT)
527 retval = LOAD_KERNEL_REBOOT;
528 else
529 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
530 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700531 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700532 if (shared)
533 shared->kernel_version_tpm = (uint32_t)lowest_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700534 }
535 }
536
Randall Spangler63dffcb2010-08-05 15:13:14 -0700537 /* Lock the kernel versions */
538 status = RollbackKernelLock();
539 if (0 != status) {
540 VBDEBUG(("Error locking kernel versions.\n"));
541 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800542 if (kBootRecovery != boot_mode) {
543 if (status == TPM_E_MUST_REBOOT)
544 retval = LOAD_KERNEL_REBOOT;
545 else
546 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
547 goto LoadKernelExit;
548 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700549 }
550
551 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800552 retval = LOAD_KERNEL_SUCCESS;
Randall Spangler02223552011-03-15 19:20:25 -0700553 } else {
554 /* TODO: differentiate between finding an invalid kernel
555 * (found_partitions>0) and not finding one at all. Right now we
556 * treat them the same, and return LOAD_KERNEL_INVALID for both. */
557 retval = LOAD_KERNEL_INVALID;
Randall Spanglerd1836442010-06-10 09:59:04 -0700558 }
559
Randall Spangler640fb512011-03-03 10:11:17 -0800560LoadKernelExit:
561
562 /* Save whether the good partition's key block was fully verified */
563 VbNvSet(vnc, VBNV_FW_VERIFIED_KERNEL_KEY, good_partition_key_block_valid);
564
565 /* Store recovery request, if any, then tear down non-volatile storage */
566 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
567 recovery : VBNV_RECOVERY_NOT_REQUESTED);
568 VbNvTeardown(vnc);
569
Randall Spanglerad6824b2011-03-16 19:07:33 -0700570 if (shared) {
571 /* Save timer values */
572 shared->timer_load_kernel_enter = timer_enter;
573 shared->timer_load_kernel_exit = VbGetTimer();
574 /* Store how much shared data we used, if any */
Randall Spangler95c40312011-03-09 15:54:16 -0800575 params->shared_data_size = shared->data_used;
Randall Spanglerad6824b2011-03-16 19:07:33 -0700576 }
Randall Spangler95c40312011-03-09 15:54:16 -0800577
Randall Spangler640fb512011-03-03 10:11:17 -0800578 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700579}