blob: a1ddd176239346db49ea7d6e65d076e377ff92c6 [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 {
Randall Spangler17c71262011-03-18 11:24:27 -070024 kBootRecovery = 0, /* Recovery firmware, regardless of dev switch position */
25 kBootNormal = 1, /* Normal firmware */
26 kBootDev = 2 /* Dev firmware AND dev switch is on */
Randall Spangler640fb512011-03-03 10:11:17 -080027} 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 Spangler17c71262011-03-18 11:24:27 -0700124 VbSharedDataKernelCall* shcall = NULL;
Randall Spangler640fb512011-03-03 10:11:17 -0800125 VbNvContext* vnc = params->nv_context;
Randall Spangler95c40312011-03-09 15:54:16 -0800126 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)params->gbb_data;
Bill Richardsone2729402010-07-22 12:23:47 -0700127 VbPublicKey* kernel_subkey;
Randall Spanglerd1836442010-06-10 09:59:04 -0700128 GptData gpt;
129 uint64_t part_start, part_size;
Bill Richardsone2729402010-07-22 12:23:47 -0700130 uint64_t blba;
131 uint64_t kbuf_sectors;
Randall Spanglerd1836442010-06-10 09:59:04 -0700132 uint8_t* kbuf = NULL;
133 int found_partitions = 0;
134 int good_partition = -1;
Randall Spangler640fb512011-03-03 10:11:17 -0800135 int good_partition_key_block_valid = 0;
Randall Spangler66680282010-08-16 12:33:44 -0700136 uint32_t tpm_version = 0;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700137 uint64_t lowest_version = LOWEST_TPM_VERSION;
Randall Spangler640fb512011-03-03 10:11:17 -0800138 int rec_switch, dev_switch;
139 BootMode boot_mode;
Randall Spangler99ca3462011-03-15 15:28:31 -0700140 uint32_t test_err = 0;
Randall Spangler7a786b72010-07-08 13:29:42 -0700141 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700142
Randall Spangler02223552011-03-15 19:20:25 -0700143 int retval = LOAD_KERNEL_RECOVERY;
Randall Spangler640fb512011-03-03 10:11:17 -0800144 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED;
Randall Spanglerad6824b2011-03-16 19:07:33 -0700145 uint64_t timer_enter = VbGetTimer();
Randall Spangler640fb512011-03-03 10:11:17 -0800146
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 Spanglerad6824b2011-03-16 19:07:33 -0700160 /* Clear output params in case we fail */
161 params->partition_number = 0;
162 params->bootloader_address = 0;
163 params->bootloader_size = 0;
164
Randall Spangler17c71262011-03-18 11:24:27 -0700165 /* Calculate switch positions and boot mode */
166 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
167 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
168 if (rec_switch)
169 boot_mode = kBootRecovery;
170 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags)
171 boot_mode = kBootDev;
172 else {
173 /* Normal firmware */
174 boot_mode = kBootNormal;
175 dev_switch = 0; /* Always do a fully verified boot */
176 }
177
178 if (kBootRecovery == boot_mode) {
179 /* Initialize the shared data structure, since LoadFirmware() didn't do it
180 * for us. */
181 if (0 != VbSharedDataInit(shared, params->shared_data_size)) {
182 /* Error initializing the shared data, but we can keep going. We just
183 * can't use the shared data. */
184 VBDEBUG(("Shared data init error\n"));
185 params->shared_data_size = 0;
186 shared = NULL;
187 }
188 }
189
190 if (shared) {
191 /* Set up tracking for this call. This wraps around if called many times,
192 * so we need to initialize the call entry each time. */
193 shcall = shared->lk_calls + (shared->lk_call_count
194 & (VBSD_MAX_KERNEL_CALLS - 1));
195 Memset(shcall, 0, sizeof(VbSharedDataKernelCall));
196 shcall->boot_flags = (uint32_t)params->boot_flags;
197 shcall->boot_mode = boot_mode;
198 shcall->sector_size = (uint32_t)params->bytes_per_lba;
199 shcall->sector_count = params->ending_lba + 1;
200 shared->lk_call_count++;
201 }
202
Randall Spangler99ca3462011-03-15 15:28:31 -0700203 /* Handle test errors */
204 VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err);
205 if (VBNV_TEST_ERROR_LOAD_KERNEL == test_err) {
206 /* Get error code */
207 VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err);
Randall Spangler17c71262011-03-18 11:24:27 -0700208 if (shcall)
209 shcall->test_error_num = (uint8_t)test_err;
Randall Spangler99ca3462011-03-15 15:28:31 -0700210 /* Clear test params so we don't repeat the error */
211 VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0);
212 VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0);
213 /* Handle error codes */
214 switch (test_err) {
215 case LOAD_KERNEL_RECOVERY:
216 recovery = VBNV_RECOVERY_RW_TEST_LK;
217 goto LoadKernelExit;
218 case LOAD_KERNEL_NOT_FOUND:
219 case LOAD_KERNEL_INVALID:
220 case LOAD_KERNEL_REBOOT:
221 retval = test_err;
222 goto LoadKernelExit;
223 default:
224 break;
225 }
226 }
227
Bill Richardsone2729402010-07-22 12:23:47 -0700228 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700229 blba = params->bytes_per_lba;
230 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700231 if (0 == kbuf_sectors) {
232 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800233 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700234 }
235
Randall Spangler17c71262011-03-18 11:24:27 -0700236 if (kBootDev == boot_mode && !dev_switch) {
Randall Spanglerc90e7e82011-05-17 15:27:42 -0700237 /* Dev firmware should be signed such that it never boots with the dev
238 * switch is off; so something is terribly wrong. */
239 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700240 if (shcall)
241 shcall->check_result = VBSD_LKC_CHECK_DEV_SWITCH_MISMATCH;
Randall Spanglerc90e7e82011-05-17 15:27:42 -0700242 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
243 goto LoadKernelExit;
244 }
Bill Richardsone2729402010-07-22 12:23:47 -0700245
Randall Spangler640fb512011-03-03 10:11:17 -0800246 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800247 /* Use the recovery key to verify the kernel */
248 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
249
250 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800251 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700252 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700253 if (shcall)
254 shcall->flags |= VBSD_LK_FLAG_REC_TPM_INIT_ERROR;
Randall Spangler63dffcb2010-08-05 15:13:14 -0700255 /* Ignore return code, since we need to boot recovery mode to
256 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700257 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700258
259 /* Read the key indices from the TPM; ignore any errors */
260 if (shared) {
261 RollbackFirmwareRead(&shared->fw_version_tpm);
262 RollbackKernelRead(&shared->kernel_version_tpm);
263 }
Randall Spangler640fb512011-03-03 10:11:17 -0800264 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800265 /* Use the kernel subkey passed from LoadFirmware(). */
266 kernel_subkey = &shared->kernel_subkey;
267
Randall Spanglerd1836442010-06-10 09:59:04 -0700268 /* Read current kernel key index from TPM. Assumes TPM is already
269 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700270 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700271 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700272 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800273 if (status == TPM_E_MUST_REBOOT)
274 retval = LOAD_KERNEL_REBOOT;
275 else
276 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
277 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700278 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700279 if (shared)
280 shared->kernel_version_tpm = tpm_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700281 }
282
283 do {
284 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700285 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700286 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700287 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700288 VBDEBUG(("Unable to read GPT data\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700289 if (shcall)
290 shcall->check_result = VBSD_LKC_CHECK_GPT_READ_ERROR;
Randall Spanglerd1836442010-06-10 09:59:04 -0700291 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700292 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700293
294 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700295 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700296 VBDEBUG(("Error parsing GPT\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700297 if (shcall)
298 shcall->check_result = VBSD_LKC_CHECK_GPT_PARSE_ERROR;
Randall Spanglerd1836442010-06-10 09:59:04 -0700299 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700300 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700301
Randall Spanglerd1836442010-06-10 09:59:04 -0700302 /* Allocate kernel header buffers */
303 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
304 if (!kbuf)
305 break;
306
307 /* Loop over candidate kernel partitions */
308 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
Randall Spangler17c71262011-03-18 11:24:27 -0700309 VbSharedDataKernelPart* shpart = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700310 VbKeyBlockHeader* key_block;
311 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700312 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700313 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700314 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700315 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700316 uint64_t body_offset_sectors;
317 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800318 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700319
Randall Spanglere2ec9842010-06-23 21:17:07 -0700320 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
321 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700322
Randall Spangler17c71262011-03-18 11:24:27 -0700323 if (shcall) {
324 /* Set up tracking for this partition. This wraps around if called
325 * many times, so initialize the partition entry each time. */
326 shpart = shcall->parts + (shcall->kernel_parts_found
327 & (VBSD_MAX_KERNEL_PARTS - 1));
328 Memset(shpart, 0, sizeof(VbSharedDataKernelPart));
329 shpart->sector_start = part_start;
330 shpart->sector_count = part_size;
331 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
332 * Adjust here, until cgptlib is fixed. */
333 shpart->gpt_index = (uint8_t)(gpt.current_kernel + 1);
334 shcall->kernel_parts_found++;
335 }
336
Randall Spanglerd1836442010-06-10 09:59:04 -0700337 /* Found at least one kernel partition. */
338 found_partitions++;
339
Randall Spangler640fb512011-03-03 10:11:17 -0800340 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700341 if (part_size < kbuf_sectors) {
342 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700343 if (shpart)
344 shpart->check_result = VBSD_LKP_CHECK_TOO_SMALL;
Randall Spangler741d2b22010-08-20 16:37:12 -0700345 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700346 }
347
348 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) {
349 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700350 if (shpart)
351 shpart->check_result = VBSD_LKP_CHECK_READ_START;
Randall Spangler741d2b22010-08-20 16:37:12 -0700352 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700353 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700354
Randall Spangler640fb512011-03-03 10:11:17 -0800355 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700356 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800357 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
358 VBDEBUG(("Verifying key block signature failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700359 if (shpart)
360 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_SIG;
361
Randall Spangler640fb512011-03-03 10:11:17 -0800362 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700363
Randall Spangler640fb512011-03-03 10:11:17 -0800364 /* If we're not in developer mode, this kernel is bad. */
365 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700366 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800367
368 /* In developer mode, we can continue if the SHA-512 hash of the key
369 * block is valid. */
370 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
371 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700372 if (shpart)
373 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_HASH;
Randall Spangler741d2b22010-08-20 16:37:12 -0700374 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700375 }
Randall Spangler695cd162010-06-15 23:38:23 -0700376 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700377
Randall Spangler640fb512011-03-03 10:11:17 -0800378 /* Check the key block flags against the current boot mode. */
379 if (!(key_block->key_block_flags &
380 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
381 KEY_BLOCK_FLAG_DEVELOPER_0))) {
382 VBDEBUG(("Key block developer flag mismatch.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700383 if (shpart)
384 shpart->check_result = VBSD_LKP_CHECK_DEV_MISMATCH;
Randall Spangler640fb512011-03-03 10:11:17 -0800385 key_block_valid = 0;
386 }
387 if (!(key_block->key_block_flags &
388 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
389 KEY_BLOCK_FLAG_RECOVERY_0))) {
390 VBDEBUG(("Key block recovery flag mismatch.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700391 if (shpart)
392 shpart->check_result = VBSD_LKP_CHECK_REC_MISMATCH;
Randall Spangler640fb512011-03-03 10:11:17 -0800393 key_block_valid = 0;
394 }
395
396 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700397 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800398 if (kBootRecovery != boot_mode) {
399 if (key_version < (tpm_version >> 16)) {
400 VBDEBUG(("Key version too old.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700401 if (shpart)
402 shpart->check_result = VBSD_LKP_CHECK_KEY_ROLLBACK;
Randall Spangler640fb512011-03-03 10:11:17 -0800403 key_block_valid = 0;
404 }
405 }
406
407 /* If we're not in developer mode, require the key block to be valid. */
408 if (kBootDev != boot_mode && !key_block_valid) {
409 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700410 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700411 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700412
Randall Spangler640fb512011-03-03 10:11:17 -0800413 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700414 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700415 if (!data_key) {
416 VBDEBUG(("Data key bad.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700417 if (shpart)
418 shpart->check_result = VBSD_LKP_CHECK_DATA_KEY_PARSE;
Randall Spangler741d2b22010-08-20 16:37:12 -0700419 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700420 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700421
422 /* Verify the preamble, which follows the key block */
423 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700424 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700425 KBUF_SIZE - key_block->key_block_size,
426 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700427 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700428 if (shpart)
429 shpart->check_result = VBSD_LKP_CHECK_VERIFY_PREAMBLE;
Randall Spangler741d2b22010-08-20 16:37:12 -0700430 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700431 }
432
Randall Spangler640fb512011-03-03 10:11:17 -0800433 /* If the key block is valid and we're not in recovery mode, check for
434 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700435 combined_version = ((key_version << 16) |
436 (preamble->kernel_version & 0xFFFF));
Randall Spangler17c71262011-03-18 11:24:27 -0700437 if (shpart)
438 shpart->combined_version = (uint32_t)combined_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800439 if (key_block_valid && kBootRecovery != boot_mode) {
440 if (combined_version < tpm_version) {
441 VBDEBUG(("Kernel version too low.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700442 if (shpart)
443 shpart->check_result = VBSD_LKP_CHECK_KERNEL_ROLLBACK;
Randall Spangler640fb512011-03-03 10:11:17 -0800444 /* If we're not in developer mode, kernel version must be valid. */
445 if (kBootDev != boot_mode)
446 goto bad_kernel;
447 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700448 }
449
Randall Spanglere2ec9842010-06-23 21:17:07 -0700450 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700451 if (shpart)
452 shpart->check_result = VBSD_LKP_CHECK_PREAMBLE_VALID;
Randall Spangler695cd162010-06-15 23:38:23 -0700453
Randall Spangler66680282010-08-16 12:33:44 -0700454 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800455 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700456 lowest_version = combined_version;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700457 else {
458 VBDEBUG(("Key block valid: %d\n", key_block_valid));
459 VBDEBUG(("Combined version: %" PRIu64 "\n", combined_version));
460 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700461
462 /* If we already have a good kernel, no need to read another
463 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700464 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700465 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700466 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700467
468 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700469 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
470 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700471 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700472 if (shpart)
473 shpart->check_result = VBSD_LKP_CHECK_BODY_ADDRESS;
Randall Spangler741d2b22010-08-20 16:37:12 -0700474 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700475 }
476
477 /* Verify kernel body starts at a multiple of the sector size. */
478 body_offset = key_block->key_block_size + preamble->preamble_size;
479 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700480 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700481 if (shpart)
482 shpart->check_result = VBSD_LKP_CHECK_BODY_OFFSET;
Randall Spangler741d2b22010-08-20 16:37:12 -0700483 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700484 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700485 body_offset_sectors = body_offset / blba;
486
487 /* Verify kernel body fits in the buffer */
488 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
489 if (body_sectors * blba > params->kernel_buffer_size) {
490 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700491 if (shpart)
492 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_MEM;
Randall Spangler741d2b22010-08-20 16:37:12 -0700493 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700494 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700495
496 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700497 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700498 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700499 if (shpart)
500 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_PART;
Randall Spangler741d2b22010-08-20 16:37:12 -0700501 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700502 }
503
504 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700505 VBPERFSTART("VB_RKD");
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700506 if (0 != BootDeviceReadLBA(part_start + body_offset_sectors,
507 body_sectors,
508 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700509 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700510 VBPERFEND("VB_RKD");
Randall Spangler17c71262011-03-18 11:24:27 -0700511 if (shpart)
512 shpart->check_result = VBSD_LKP_CHECK_READ_DATA;
Randall Spangler741d2b22010-08-20 16:37:12 -0700513 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700514 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700515 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700516
517 /* Verify kernel data */
518 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700519 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700520 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700521 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700522 if (shpart)
523 shpart->check_result = VBSD_LKP_CHECK_VERIFY_DATA;
Randall Spangler741d2b22010-08-20 16:37:12 -0700524 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700525 }
526
527 /* Done with the kernel signing key, so can free it now */
528 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700529 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700530
531 /* If we're still here, the kernel is valid. */
532 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800533 VBDEBUG(("Partition is good.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700534 if (shpart) {
535 shpart->check_result = VBSD_LKP_CHECK_KERNEL_GOOD;
536 if (key_block_valid)
537 shpart->flags |= VBSD_LKP_FLAG_KEY_BLOCK_VALID;
538 }
539
Randall Spangler640fb512011-03-03 10:11:17 -0800540 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700541 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
542 * Adjust here, until cgptlib is fixed. */
543 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700544 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700545 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700546 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
547 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700548 params->bootloader_address = preamble->bootloader_address;
549 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700550
551 /* Update GPT to note this is the kernel we're trying */
552 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
553
Randall Spangler640fb512011-03-03 10:11:17 -0800554 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
555 * there's no rollback protection, so we can stop at the first valid
556 * kernel. */
557 if (kBootRecovery == boot_mode || !key_block_valid) {
558 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700559 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700560 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700561
Randall Spangler640fb512011-03-03 10:11:17 -0800562 /* Otherwise, we do care about the key index in the TPM. If the good
563 * partition's key version is the same as the tpm, then the TPM doesn't
564 * need updating; we can stop now. Otherwise, we'll check all the other
565 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700566 if (combined_version == tpm_version) {
567 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700568 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700569 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700570
571 /* Continue, so that we skip the error handling code below */
572 continue;
573
574 bad_kernel:
575 /* Handle errors parsing this kernel */
576 if (NULL != data_key)
577 RSAPublicKeyFree(data_key);
578
579 VBDEBUG(("Marking kernel as invalid.\n"));
580 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
581
582
Randall Spanglerd1836442010-06-10 09:59:04 -0700583 } /* while(GptNextKernelEntry) */
584 } while(0);
585
586 /* Free kernel buffer */
587 if (kbuf)
588 Free(kbuf);
589
590 /* Write and free GPT data */
591 WriteAndFreeGptData(&gpt);
592
593 /* Handle finding a good partition */
594 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700595 VBDEBUG(("Good_partition >= 0\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700596 if (shcall)
597 shcall->check_result = VBSD_LKC_CHECK_GOOD_PARTITION;
Randall Spanglerd1836442010-06-10 09:59:04 -0700598
599 /* See if we need to update the TPM */
Randall Spangler700fc492011-04-17 10:48:10 -0700600 if ((kBootNormal == boot_mode) &&
601 !((1 == shared->firmware_index) && (shared->flags & VBSD_FWB_TRIED))) {
602 /* We only update the TPM in normal mode. We don't advance the
603 * TPM if we're trying a new firmware B, because that firmware
604 * may have a key change and roll forward the TPM too soon. */
605 VBDEBUG(("Checking if TPM kernel version needs advancing\n"));
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700606
607 if ((lowest_version > tpm_version) &&
608 (lowest_version != LOWEST_TPM_VERSION)) {
Randall Spangler66680282010-08-16 12:33:44 -0700609 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700610 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700611 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800612 if (status == TPM_E_MUST_REBOOT)
613 retval = LOAD_KERNEL_REBOOT;
614 else
615 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
616 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700617 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700618 if (shared)
619 shared->kernel_version_tpm = (uint32_t)lowest_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700620 }
621 }
622
Randall Spangler63dffcb2010-08-05 15:13:14 -0700623 /* Lock the kernel versions */
624 status = RollbackKernelLock();
625 if (0 != status) {
626 VBDEBUG(("Error locking kernel versions.\n"));
627 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800628 if (kBootRecovery != boot_mode) {
629 if (status == TPM_E_MUST_REBOOT)
630 retval = LOAD_KERNEL_REBOOT;
631 else
632 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
633 goto LoadKernelExit;
634 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700635 }
636
637 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800638 retval = LOAD_KERNEL_SUCCESS;
Randall Spangler02223552011-03-15 19:20:25 -0700639 } else {
Randall Spangler17c71262011-03-18 11:24:27 -0700640 if (shcall)
641 shcall->check_result = (found_partitions > 0
642 ? VBSD_LKC_CHECK_INVALID_PARTITIONS
643 : VBSD_LKC_CHECK_NO_PARTITIONS);
644
Randall Spangler02223552011-03-15 19:20:25 -0700645 /* TODO: differentiate between finding an invalid kernel
646 * (found_partitions>0) and not finding one at all. Right now we
647 * treat them the same, and return LOAD_KERNEL_INVALID for both. */
648 retval = LOAD_KERNEL_INVALID;
Randall Spanglerd1836442010-06-10 09:59:04 -0700649 }
650
Randall Spangler640fb512011-03-03 10:11:17 -0800651LoadKernelExit:
652
Randall Spangler640fb512011-03-03 10:11:17 -0800653 /* Store recovery request, if any, then tear down non-volatile storage */
654 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
655 recovery : VBNV_RECOVERY_NOT_REQUESTED);
656 VbNvTeardown(vnc);
657
Randall Spanglerad6824b2011-03-16 19:07:33 -0700658 if (shared) {
Randall Spangler17c71262011-03-18 11:24:27 -0700659 if (shcall)
660 shcall->return_code = (uint8_t)retval;
661
662 /* Save whether the good partition's key block was fully verified */
663 if (good_partition_key_block_valid)
664 shared->flags |= VBSD_KERNEL_KEY_VERIFIED;
665
Randall Spanglerad6824b2011-03-16 19:07:33 -0700666 /* Save timer values */
667 shared->timer_load_kernel_enter = timer_enter;
668 shared->timer_load_kernel_exit = VbGetTimer();
669 /* Store how much shared data we used, if any */
Randall Spangler95c40312011-03-09 15:54:16 -0800670 params->shared_data_size = shared->data_used;
Randall Spanglerad6824b2011-03-16 19:07:33 -0700671 }
Randall Spangler95c40312011-03-09 15:54:16 -0800672
Randall Spangler640fb512011-03-03 10:11:17 -0800673 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700674}