blob: 83fb3fed31c61643d96756d0aafac27343d30a9c [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;
144
145 /* Setup NV storage */
146 VbNvSetup(vnc);
147
Bill Richardsone2729402010-07-22 12:23:47 -0700148 /* Sanity Checks */
149 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700150 !params->bytes_per_lba ||
151 !params->ending_lba ||
152 !params->kernel_buffer ||
153 !params->kernel_buffer_size) {
154 VBDEBUG(("LoadKernel() called with invalid params\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800155 goto LoadKernelExit;
Bill Richardsone2729402010-07-22 12:23:47 -0700156 }
157
Randall Spangler99ca3462011-03-15 15:28:31 -0700158 /* Handle test errors */
159 VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err);
160 if (VBNV_TEST_ERROR_LOAD_KERNEL == test_err) {
161 /* Get error code */
162 VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err);
163 /* Clear test params so we don't repeat the error */
164 VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0);
165 VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0);
166 /* Handle error codes */
167 switch (test_err) {
168 case LOAD_KERNEL_RECOVERY:
169 recovery = VBNV_RECOVERY_RW_TEST_LK;
170 goto LoadKernelExit;
171 case LOAD_KERNEL_NOT_FOUND:
172 case LOAD_KERNEL_INVALID:
173 case LOAD_KERNEL_REBOOT:
174 retval = test_err;
175 goto LoadKernelExit;
176 default:
177 break;
178 }
179 }
180
Bill Richardsone2729402010-07-22 12:23:47 -0700181 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700182 blba = params->bytes_per_lba;
183 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700184 if (0 == kbuf_sectors) {
185 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800186 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700187 }
188
Randall Spangler640fb512011-03-03 10:11:17 -0800189 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
190 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
191
192 if (rec_switch)
193 boot_mode = kBootRecovery;
Randall Spangler8478ece2011-03-03 11:00:17 -0800194 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags) {
Randall Spangler640fb512011-03-03 10:11:17 -0800195 if (!dev_switch) {
196 /* Dev firmware should be signed such that it never boots with the dev
197 * switch is off; so something is terribly wrong. */
198 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
199 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
200 goto LoadKernelExit;
201 }
202 boot_mode = kBootDev;
Randall Spangler8478ece2011-03-03 11:00:17 -0800203 } else {
Randall Spangler640fb512011-03-03 10:11:17 -0800204 /* Normal firmware */
205 boot_mode = kBootNormal;
206 dev_switch = 0; /* Always do a fully verified boot */
Randall Spanglera8e0f942011-02-14 11:12:09 -0800207 }
Bill Richardsone2729402010-07-22 12:23:47 -0700208
Randall Spanglerd1836442010-06-10 09:59:04 -0700209 /* Clear output params in case we fail */
210 params->partition_number = 0;
211 params->bootloader_address = 0;
212 params->bootloader_size = 0;
213
Randall Spangler640fb512011-03-03 10:11:17 -0800214 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800215 /* Initialize the shared data structure, since LoadFirmware() didn't do it
216 * for us. */
217 if (0 != VbSharedDataInit(shared, params->shared_data_size)) {
218 /* Error initializing the shared data, but we can keep going. We just
219 * can't use the shared data. */
220 VBDEBUG(("Shared data init error\n"));
221 params->shared_data_size = 0;
222 shared = NULL;
223 }
224
225 /* Use the recovery key to verify the kernel */
226 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
227
228 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800229 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700230 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
231 /* Ignore return code, since we need to boot recovery mode to
232 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700233 }
Randall Spangler640fb512011-03-03 10:11:17 -0800234 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800235 /* Use the kernel subkey passed from LoadFirmware(). */
236 kernel_subkey = &shared->kernel_subkey;
237
Randall Spanglerd1836442010-06-10 09:59:04 -0700238 /* Read current kernel key index from TPM. Assumes TPM is already
239 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700240 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700241 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700242 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800243 if (status == TPM_E_MUST_REBOOT)
244 retval = LOAD_KERNEL_REBOOT;
245 else
246 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
247 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700248 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700249 }
250
251 do {
252 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700253 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700254 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700255 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700256 VBDEBUG(("Unable to read GPT data\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700257 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700258 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700259
260 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700261 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700262 VBDEBUG(("Error parsing GPT\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700263 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700264 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700265
Randall Spanglerd1836442010-06-10 09:59:04 -0700266 /* Allocate kernel header buffers */
267 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
268 if (!kbuf)
269 break;
270
271 /* Loop over candidate kernel partitions */
272 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
273 VbKeyBlockHeader* key_block;
274 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700275 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700276 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700277 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700278 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700279 uint64_t body_offset_sectors;
280 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800281 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700282
Randall Spanglere2ec9842010-06-23 21:17:07 -0700283 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
284 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700285
Randall Spanglerd1836442010-06-10 09:59:04 -0700286 /* Found at least one kernel partition. */
287 found_partitions++;
288
Randall Spangler640fb512011-03-03 10:11:17 -0800289 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700290 if (part_size < kbuf_sectors) {
291 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700292 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700293 }
294
295 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) {
296 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700297 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700298 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700299
Randall Spangler640fb512011-03-03 10:11:17 -0800300 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700301 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800302 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
303 VBDEBUG(("Verifying key block signature failed.\n"));
304 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700305
Randall Spangler640fb512011-03-03 10:11:17 -0800306 /* If we're not in developer mode, this kernel is bad. */
307 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700308 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800309
310 /* In developer mode, we can continue if the SHA-512 hash of the key
311 * block is valid. */
312 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
313 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700314 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700315 }
Randall Spangler695cd162010-06-15 23:38:23 -0700316 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700317
Randall Spangler640fb512011-03-03 10:11:17 -0800318 /* Check the key block flags against the current boot mode. */
319 if (!(key_block->key_block_flags &
320 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
321 KEY_BLOCK_FLAG_DEVELOPER_0))) {
322 VBDEBUG(("Key block developer flag mismatch.\n"));
323 key_block_valid = 0;
324 }
325 if (!(key_block->key_block_flags &
326 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
327 KEY_BLOCK_FLAG_RECOVERY_0))) {
328 VBDEBUG(("Key block recovery flag mismatch.\n"));
329 key_block_valid = 0;
330 }
331
332 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700333 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800334 if (kBootRecovery != boot_mode) {
335 if (key_version < (tpm_version >> 16)) {
336 VBDEBUG(("Key version too old.\n"));
337 key_block_valid = 0;
338 }
339 }
340
341 /* If we're not in developer mode, require the key block to be valid. */
342 if (kBootDev != boot_mode && !key_block_valid) {
343 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700344 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700345 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700346
Randall Spangler640fb512011-03-03 10:11:17 -0800347 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700348 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700349 if (!data_key) {
350 VBDEBUG(("Data key bad.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700351 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700352 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700353
354 /* Verify the preamble, which follows the key block */
355 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700356 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700357 KBUF_SIZE - key_block->key_block_size,
358 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700359 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700360 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700361 }
362
Randall Spangler640fb512011-03-03 10:11:17 -0800363 /* If the key block is valid and we're not in recovery mode, check for
364 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700365 combined_version = ((key_version << 16) |
366 (preamble->kernel_version & 0xFFFF));
Randall Spangler640fb512011-03-03 10:11:17 -0800367 if (key_block_valid && kBootRecovery != boot_mode) {
368 if (combined_version < tpm_version) {
369 VBDEBUG(("Kernel version too low.\n"));
370 /* If we're not in developer mode, kernel version must be valid. */
371 if (kBootDev != boot_mode)
372 goto bad_kernel;
373 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700374 }
375
Randall Spanglere2ec9842010-06-23 21:17:07 -0700376 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700377
Randall Spangler66680282010-08-16 12:33:44 -0700378 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800379 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700380 lowest_version = combined_version;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700381 else {
382 VBDEBUG(("Key block valid: %d\n", key_block_valid));
383 VBDEBUG(("Combined version: %" PRIu64 "\n", combined_version));
384 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700385
386 /* If we already have a good kernel, no need to read another
387 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700388 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700389 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700390 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700391
392 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700393 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
394 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700395 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700396 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700397 }
398
399 /* Verify kernel body starts at a multiple of the sector size. */
400 body_offset = key_block->key_block_size + preamble->preamble_size;
401 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700402 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700403 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700404 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700405 body_offset_sectors = body_offset / blba;
406
407 /* Verify kernel body fits in the buffer */
408 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
409 if (body_sectors * blba > params->kernel_buffer_size) {
410 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700411 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700412 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700413
414 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700415 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700416 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700417 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700418 }
419
420 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700421 VBPERFSTART("VB_RKD");
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700422 if (0 != BootDeviceReadLBA(part_start + body_offset_sectors,
423 body_sectors,
424 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700425 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700426 VBPERFEND("VB_RKD");
Randall Spangler741d2b22010-08-20 16:37:12 -0700427 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700428 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700429 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700430
431 /* Verify kernel data */
432 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700433 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700434 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700435 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700436 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700437 }
438
439 /* Done with the kernel signing key, so can free it now */
440 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700441 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700442
443 /* If we're still here, the kernel is valid. */
444 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800445 VBDEBUG(("Partition is good.\n"));
446 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700447 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
448 * Adjust here, until cgptlib is fixed. */
449 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700450 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700451 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700452 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
453 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700454 params->bootloader_address = preamble->bootloader_address;
455 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700456
457 /* Update GPT to note this is the kernel we're trying */
458 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
459
Randall Spangler640fb512011-03-03 10:11:17 -0800460 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
461 * there's no rollback protection, so we can stop at the first valid
462 * kernel. */
463 if (kBootRecovery == boot_mode || !key_block_valid) {
464 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700465 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700466 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700467
Randall Spangler640fb512011-03-03 10:11:17 -0800468 /* Otherwise, we do care about the key index in the TPM. If the good
469 * partition's key version is the same as the tpm, then the TPM doesn't
470 * need updating; we can stop now. Otherwise, we'll check all the other
471 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700472 if (combined_version == tpm_version) {
473 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700474 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700475 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700476
477 /* Continue, so that we skip the error handling code below */
478 continue;
479
480 bad_kernel:
481 /* Handle errors parsing this kernel */
482 if (NULL != data_key)
483 RSAPublicKeyFree(data_key);
484
485 VBDEBUG(("Marking kernel as invalid.\n"));
486 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
487
488
Randall Spanglerd1836442010-06-10 09:59:04 -0700489 } /* while(GptNextKernelEntry) */
490 } while(0);
491
492 /* Free kernel buffer */
493 if (kbuf)
494 Free(kbuf);
495
496 /* Write and free GPT data */
497 WriteAndFreeGptData(&gpt);
498
499 /* Handle finding a good partition */
500 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700501 VBDEBUG(("Good_partition >= 0\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700502
503 /* See if we need to update the TPM */
Randall Spanglerc324fbf2011-03-04 12:19:25 -0800504 if (kBootRecovery != boot_mode && good_partition_key_block_valid) {
Randall Spangler640fb512011-03-03 10:11:17 -0800505 /* We only update the TPM in normal and developer boot modes. In
506 * developer mode, we only advanced lowest_version for kernels with valid
507 * key blocks, and didn't count self-signed key blocks. In recovery
508 * mode, the TPM stays PP-unlocked, so anything we write gets blown away
509 * by the firmware when we go back to normal mode. */
510 VBDEBUG(("Boot_flags = not recovery\n"));
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700511
512 if ((lowest_version > tpm_version) &&
513 (lowest_version != LOWEST_TPM_VERSION)) {
Randall Spangler66680282010-08-16 12:33:44 -0700514 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700515 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700516 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800517 if (status == TPM_E_MUST_REBOOT)
518 retval = LOAD_KERNEL_REBOOT;
519 else
520 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
521 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700522 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700523 }
524 }
525
Randall Spangler63dffcb2010-08-05 15:13:14 -0700526 /* Lock the kernel versions */
527 status = RollbackKernelLock();
528 if (0 != status) {
529 VBDEBUG(("Error locking kernel versions.\n"));
530 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800531 if (kBootRecovery != boot_mode) {
532 if (status == TPM_E_MUST_REBOOT)
533 retval = LOAD_KERNEL_REBOOT;
534 else
535 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
536 goto LoadKernelExit;
537 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700538 }
539
540 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800541 retval = LOAD_KERNEL_SUCCESS;
Randall Spangler02223552011-03-15 19:20:25 -0700542 } else {
543 /* TODO: differentiate between finding an invalid kernel
544 * (found_partitions>0) and not finding one at all. Right now we
545 * treat them the same, and return LOAD_KERNEL_INVALID for both. */
546 retval = LOAD_KERNEL_INVALID;
Randall Spanglerd1836442010-06-10 09:59:04 -0700547 }
548
Randall Spangler640fb512011-03-03 10:11:17 -0800549LoadKernelExit:
550
551 /* Save whether the good partition's key block was fully verified */
552 VbNvSet(vnc, VBNV_FW_VERIFIED_KERNEL_KEY, good_partition_key_block_valid);
553
554 /* Store recovery request, if any, then tear down non-volatile storage */
555 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
556 recovery : VBNV_RECOVERY_NOT_REQUESTED);
557 VbNvTeardown(vnc);
558
Randall Spangler95c40312011-03-09 15:54:16 -0800559 /* Store how much shared data we used, if any */
560 if (shared)
561 params->shared_data_size = shared->data_used;
562
Randall Spangler640fb512011-03-03 10:11:17 -0800563 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700564}