blob: ab96edf4bca87ea8715c81219d8a0e3a2d47daa8 [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
Randall Spanglerd1836442010-06-10 09:59:04 -07009#include "boot_device.h"
10#include "cgptlib.h"
Bill Richardson5deb67f2010-07-23 17:22:25 -070011#include "cgptlib_internal.h"
Randall Spangler95c40312011-03-09 15:54:16 -080012#include "gbb_header.h"
Randall Spanglerd1836442010-06-10 09:59:04 -070013#include "load_kernel_fw.h"
14#include "rollback_index.h"
15#include "utility.h"
Randall Spanglere49e8af2011-07-08 13:03:32 -070016#include "vboot_api.h"
Randall Spangler83c88cf2010-06-11 16:14:18 -070017#include "vboot_common.h"
Randall Spanglere49e8af2011-07-08 13:03:32 -070018#include "vboot_kernel.h"
19
Randall Spangler83c88cf2010-06-11 16:14:18 -070020
Randall Spanglerd1836442010-06-10 09:59:04 -070021#define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */
Stefan Reinauer55db6a62011-03-15 16:23:41 -070022#define LOWEST_TPM_VERSION 0xffffffff
Randall Spanglerd1836442010-06-10 09:59:04 -070023
Randall Spangler640fb512011-03-03 10:11:17 -080024typedef enum BootMode {
Randall Spangler17c71262011-03-18 11:24:27 -070025 kBootRecovery = 0, /* Recovery firmware, regardless of dev switch position */
26 kBootNormal = 1, /* Normal firmware */
27 kBootDev = 2 /* Dev firmware AND dev switch is on */
Randall Spangler640fb512011-03-03 10:11:17 -080028} BootMode;
29
Randall Spangler83c88cf2010-06-11 16:14:18 -070030
31/* Allocates and reads GPT data from the drive. The sector_bytes and
32 * drive_sectors fields should be filled on input. The primary and
33 * secondary header and entries are filled on output.
34 *
35 * Returns 0 if successful, 1 if error. */
36int AllocAndReadGptData(GptData* gptdata) {
37
38 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
39
40 /* No data to be written yet */
41 gptdata->modified = 0;
42
43 /* Allocate all buffers */
Randall Spanglere49e8af2011-07-08 13:03:32 -070044 gptdata->primary_header = (uint8_t*)VbExMalloc(gptdata->sector_bytes);
45 gptdata->secondary_header = (uint8_t*)VbExMalloc(gptdata->sector_bytes);
46 gptdata->primary_entries = (uint8_t*)VbExMalloc(TOTAL_ENTRIES_SIZE);
47 gptdata->secondary_entries = (uint8_t*)VbExMalloc(TOTAL_ENTRIES_SIZE);
Randall Spangler83c88cf2010-06-11 16:14:18 -070048
49 if (gptdata->primary_header == NULL || gptdata->secondary_header == NULL ||
50 gptdata->primary_entries == NULL || gptdata->secondary_entries == NULL)
51 return 1;
52
53 /* Read data from the drive, skipping the protective MBR */
54 if (0 != BootDeviceReadLBA(1, 1, gptdata->primary_header))
55 return 1;
56 if (0 != BootDeviceReadLBA(2, entries_sectors, gptdata->primary_entries))
57 return 1;
58 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - entries_sectors - 1,
59 entries_sectors, gptdata->secondary_entries))
60 return 1;
61 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - 1,
62 1, gptdata->secondary_header))
63 return 1;
64
65 return 0;
66}
67
68
69/* Writes any changes for the GPT data back to the drive, then frees
70 * the buffers.
71 *
72 * Returns 0 if successful, 1 if error. */
73int WriteAndFreeGptData(GptData* gptdata) {
74
75 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
76
77 if (gptdata->primary_header) {
78 if (gptdata->modified & GPT_MODIFIED_HEADER1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070079 VBDEBUG(("Updating GPT header 1\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070080 if (0 != BootDeviceWriteLBA(1, 1, gptdata->primary_header))
81 return 1;
82 }
Randall Spanglere49e8af2011-07-08 13:03:32 -070083 VbExFree(gptdata->primary_header);
Randall Spangler83c88cf2010-06-11 16:14:18 -070084 }
85
86 if (gptdata->primary_entries) {
87 if (gptdata->modified & GPT_MODIFIED_ENTRIES1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070088 VBDEBUG(("Updating GPT entries 1\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070089 if (0 != BootDeviceWriteLBA(2, entries_sectors,
90 gptdata->primary_entries))
91 return 1;
92 }
Randall Spanglere49e8af2011-07-08 13:03:32 -070093 VbExFree(gptdata->primary_entries);
Randall Spangler83c88cf2010-06-11 16:14:18 -070094 }
95
96 if (gptdata->secondary_entries) {
97 if (gptdata->modified & GPT_MODIFIED_ENTRIES2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070098 VBDEBUG(("Updating GPT header 2\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070099 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - entries_sectors - 1,
100 entries_sectors, gptdata->secondary_entries))
101 return 1;
102 }
Randall Spanglere49e8af2011-07-08 13:03:32 -0700103 VbExFree(gptdata->secondary_entries);
Randall Spangler83c88cf2010-06-11 16:14:18 -0700104 }
105
106 if (gptdata->secondary_header) {
107 if (gptdata->modified & GPT_MODIFIED_HEADER2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700108 VBDEBUG(("Updating GPT entries 2\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -0700109 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - 1, 1,
110 gptdata->secondary_header))
111 return 1;
112 }
Randall Spanglere49e8af2011-07-08 13:03:32 -0700113 VbExFree(gptdata->secondary_header);
Randall Spangler83c88cf2010-06-11 16:14:18 -0700114 }
115
116 /* Success */
117 return 0;
118}
119
vbendeb3ecaf772010-06-24 16:19:53 -0700120/* disable MSVC warning on const logical expression (as in } while(0);) */
121__pragma(warning(disable: 4127))
Randall Spangler83c88cf2010-06-11 16:14:18 -0700122
Randall Spanglerbd529f02010-06-16 12:51:26 -0700123int LoadKernel(LoadKernelParams* params) {
Randall Spangler95c40312011-03-09 15:54:16 -0800124 VbSharedDataHeader* shared = (VbSharedDataHeader*)params->shared_data_blob;
Randall Spangler17c71262011-03-18 11:24:27 -0700125 VbSharedDataKernelCall* shcall = NULL;
Randall Spangler640fb512011-03-03 10:11:17 -0800126 VbNvContext* vnc = params->nv_context;
Randall Spangler95c40312011-03-09 15:54:16 -0800127 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)params->gbb_data;
Bill Richardsone2729402010-07-22 12:23:47 -0700128 VbPublicKey* kernel_subkey;
Randall Spanglerd1836442010-06-10 09:59:04 -0700129 GptData gpt;
130 uint64_t part_start, part_size;
Bill Richardsone2729402010-07-22 12:23:47 -0700131 uint64_t blba;
132 uint64_t kbuf_sectors;
Randall Spanglerd1836442010-06-10 09:59:04 -0700133 uint8_t* kbuf = NULL;
134 int found_partitions = 0;
135 int good_partition = -1;
Randall Spangler640fb512011-03-03 10:11:17 -0800136 int good_partition_key_block_valid = 0;
Randall Spangler66680282010-08-16 12:33:44 -0700137 uint32_t tpm_version = 0;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700138 uint64_t lowest_version = LOWEST_TPM_VERSION;
Randall Spangler640fb512011-03-03 10:11:17 -0800139 int rec_switch, dev_switch;
140 BootMode boot_mode;
Randall Spangler99ca3462011-03-15 15:28:31 -0700141 uint32_t test_err = 0;
Randall Spangler7a786b72010-07-08 13:29:42 -0700142 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700143
Randall Spangler02223552011-03-15 19:20:25 -0700144 int retval = LOAD_KERNEL_RECOVERY;
Randall Spangler640fb512011-03-03 10:11:17 -0800145 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED;
Randall Spanglere49e8af2011-07-08 13:03:32 -0700146 uint64_t timer_enter = VbExGetTimer();
Randall Spangler640fb512011-03-03 10:11:17 -0800147
148 /* Setup NV storage */
149 VbNvSetup(vnc);
150
Bill Richardsone2729402010-07-22 12:23:47 -0700151 /* Sanity Checks */
152 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700153 !params->bytes_per_lba ||
154 !params->ending_lba ||
155 !params->kernel_buffer ||
156 !params->kernel_buffer_size) {
157 VBDEBUG(("LoadKernel() called with invalid params\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800158 goto LoadKernelExit;
Bill Richardsone2729402010-07-22 12:23:47 -0700159 }
160
Randall Spanglerad6824b2011-03-16 19:07:33 -0700161 /* Clear output params in case we fail */
162 params->partition_number = 0;
163 params->bootloader_address = 0;
164 params->bootloader_size = 0;
165
Randall Spangler17c71262011-03-18 11:24:27 -0700166 /* Calculate switch positions and boot mode */
167 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
168 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
169 if (rec_switch)
170 boot_mode = kBootRecovery;
171 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags)
172 boot_mode = kBootDev;
173 else {
174 /* Normal firmware */
175 boot_mode = kBootNormal;
176 dev_switch = 0; /* Always do a fully verified boot */
177 }
178
179 if (kBootRecovery == boot_mode) {
180 /* Initialize the shared data structure, since LoadFirmware() didn't do it
181 * for us. */
182 if (0 != VbSharedDataInit(shared, params->shared_data_size)) {
183 /* Error initializing the shared data, but we can keep going. We just
184 * can't use the shared data. */
185 VBDEBUG(("Shared data init error\n"));
186 params->shared_data_size = 0;
187 shared = NULL;
188 }
189 }
190
191 if (shared) {
192 /* Set up tracking for this call. This wraps around if called many times,
193 * so we need to initialize the call entry each time. */
194 shcall = shared->lk_calls + (shared->lk_call_count
195 & (VBSD_MAX_KERNEL_CALLS - 1));
196 Memset(shcall, 0, sizeof(VbSharedDataKernelCall));
197 shcall->boot_flags = (uint32_t)params->boot_flags;
198 shcall->boot_mode = boot_mode;
199 shcall->sector_size = (uint32_t)params->bytes_per_lba;
200 shcall->sector_count = params->ending_lba + 1;
201 shared->lk_call_count++;
202 }
203
Randall Spangler99ca3462011-03-15 15:28:31 -0700204 /* Handle test errors */
205 VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err);
206 if (VBNV_TEST_ERROR_LOAD_KERNEL == test_err) {
207 /* Get error code */
208 VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err);
Randall Spangler17c71262011-03-18 11:24:27 -0700209 if (shcall)
210 shcall->test_error_num = (uint8_t)test_err;
Randall Spangler99ca3462011-03-15 15:28:31 -0700211 /* Clear test params so we don't repeat the error */
212 VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0);
213 VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0);
214 /* Handle error codes */
215 switch (test_err) {
216 case LOAD_KERNEL_RECOVERY:
217 recovery = VBNV_RECOVERY_RW_TEST_LK;
218 goto LoadKernelExit;
219 case LOAD_KERNEL_NOT_FOUND:
220 case LOAD_KERNEL_INVALID:
221 case LOAD_KERNEL_REBOOT:
222 retval = test_err;
223 goto LoadKernelExit;
224 default:
225 break;
226 }
227 }
228
Bill Richardsone2729402010-07-22 12:23:47 -0700229 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700230 blba = params->bytes_per_lba;
231 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700232 if (0 == kbuf_sectors) {
233 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800234 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700235 }
236
Randall Spangler17c71262011-03-18 11:24:27 -0700237 if (kBootDev == boot_mode && !dev_switch) {
Randall Spanglerc90e7e82011-05-17 15:27:42 -0700238 /* Dev firmware should be signed such that it never boots with the dev
239 * switch is off; so something is terribly wrong. */
240 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700241 if (shcall)
242 shcall->check_result = VBSD_LKC_CHECK_DEV_SWITCH_MISMATCH;
Randall Spanglerc90e7e82011-05-17 15:27:42 -0700243 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
244 goto LoadKernelExit;
245 }
Bill Richardsone2729402010-07-22 12:23:47 -0700246
Randall Spangler640fb512011-03-03 10:11:17 -0800247 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800248 /* Use the recovery key to verify the kernel */
249 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
250
251 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800252 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700253 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700254 if (shcall)
255 shcall->flags |= VBSD_LK_FLAG_REC_TPM_INIT_ERROR;
Randall Spangler63dffcb2010-08-05 15:13:14 -0700256 /* Ignore return code, since we need to boot recovery mode to
257 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700258 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700259
260 /* Read the key indices from the TPM; ignore any errors */
261 if (shared) {
262 RollbackFirmwareRead(&shared->fw_version_tpm);
263 RollbackKernelRead(&shared->kernel_version_tpm);
264 }
Randall Spangler640fb512011-03-03 10:11:17 -0800265 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800266 /* Use the kernel subkey passed from LoadFirmware(). */
267 kernel_subkey = &shared->kernel_subkey;
268
Randall Spanglerd1836442010-06-10 09:59:04 -0700269 /* Read current kernel key index from TPM. Assumes TPM is already
270 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700271 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700272 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700273 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800274 if (status == TPM_E_MUST_REBOOT)
275 retval = LOAD_KERNEL_REBOOT;
276 else
277 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
278 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700279 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700280 if (shared)
281 shared->kernel_version_tpm = tpm_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700282 }
283
284 do {
285 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700286 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700287 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700288 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700289 VBDEBUG(("Unable to read GPT data\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700290 if (shcall)
291 shcall->check_result = VBSD_LKC_CHECK_GPT_READ_ERROR;
Randall Spanglerd1836442010-06-10 09:59:04 -0700292 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700293 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700294
295 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700296 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700297 VBDEBUG(("Error parsing GPT\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700298 if (shcall)
299 shcall->check_result = VBSD_LKC_CHECK_GPT_PARSE_ERROR;
Randall Spanglerd1836442010-06-10 09:59:04 -0700300 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700301 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700302
Randall Spanglerd1836442010-06-10 09:59:04 -0700303 /* Allocate kernel header buffers */
Randall Spanglere49e8af2011-07-08 13:03:32 -0700304 kbuf = (uint8_t*)VbExMalloc(KBUF_SIZE);
Randall Spanglerd1836442010-06-10 09:59:04 -0700305 if (!kbuf)
306 break;
307
308 /* Loop over candidate kernel partitions */
309 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
Randall Spangler17c71262011-03-18 11:24:27 -0700310 VbSharedDataKernelPart* shpart = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700311 VbKeyBlockHeader* key_block;
312 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700313 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700314 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700315 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700316 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700317 uint64_t body_offset_sectors;
318 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800319 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700320
Randall Spanglere2ec9842010-06-23 21:17:07 -0700321 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
322 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700323
Randall Spangler17c71262011-03-18 11:24:27 -0700324 if (shcall) {
325 /* Set up tracking for this partition. This wraps around if called
326 * many times, so initialize the partition entry each time. */
327 shpart = shcall->parts + (shcall->kernel_parts_found
328 & (VBSD_MAX_KERNEL_PARTS - 1));
329 Memset(shpart, 0, sizeof(VbSharedDataKernelPart));
330 shpart->sector_start = part_start;
331 shpart->sector_count = part_size;
332 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
333 * Adjust here, until cgptlib is fixed. */
334 shpart->gpt_index = (uint8_t)(gpt.current_kernel + 1);
335 shcall->kernel_parts_found++;
336 }
337
Randall Spanglerd1836442010-06-10 09:59:04 -0700338 /* Found at least one kernel partition. */
339 found_partitions++;
340
Randall Spangler640fb512011-03-03 10:11:17 -0800341 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700342 if (part_size < kbuf_sectors) {
343 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700344 if (shpart)
345 shpart->check_result = VBSD_LKP_CHECK_TOO_SMALL;
Randall Spangler741d2b22010-08-20 16:37:12 -0700346 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700347 }
348
349 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) {
350 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700351 if (shpart)
352 shpart->check_result = VBSD_LKP_CHECK_READ_START;
Randall Spangler741d2b22010-08-20 16:37:12 -0700353 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700354 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700355
Randall Spangler640fb512011-03-03 10:11:17 -0800356 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700357 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800358 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
359 VBDEBUG(("Verifying key block signature failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700360 if (shpart)
361 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_SIG;
362
Randall Spangler640fb512011-03-03 10:11:17 -0800363 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700364
Randall Spangler640fb512011-03-03 10:11:17 -0800365 /* If we're not in developer mode, this kernel is bad. */
366 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700367 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800368
369 /* In developer mode, we can continue if the SHA-512 hash of the key
370 * block is valid. */
371 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
372 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700373 if (shpart)
374 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_HASH;
Randall Spangler741d2b22010-08-20 16:37:12 -0700375 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700376 }
Randall Spangler695cd162010-06-15 23:38:23 -0700377 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700378
Randall Spangler640fb512011-03-03 10:11:17 -0800379 /* Check the key block flags against the current boot mode. */
380 if (!(key_block->key_block_flags &
381 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
382 KEY_BLOCK_FLAG_DEVELOPER_0))) {
383 VBDEBUG(("Key block developer flag mismatch.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700384 if (shpart)
385 shpart->check_result = VBSD_LKP_CHECK_DEV_MISMATCH;
Randall Spangler640fb512011-03-03 10:11:17 -0800386 key_block_valid = 0;
387 }
388 if (!(key_block->key_block_flags &
389 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
390 KEY_BLOCK_FLAG_RECOVERY_0))) {
391 VBDEBUG(("Key block recovery flag mismatch.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700392 if (shpart)
393 shpart->check_result = VBSD_LKP_CHECK_REC_MISMATCH;
Randall Spangler640fb512011-03-03 10:11:17 -0800394 key_block_valid = 0;
395 }
396
397 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700398 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800399 if (kBootRecovery != boot_mode) {
400 if (key_version < (tpm_version >> 16)) {
401 VBDEBUG(("Key version too old.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700402 if (shpart)
403 shpart->check_result = VBSD_LKP_CHECK_KEY_ROLLBACK;
Randall Spangler640fb512011-03-03 10:11:17 -0800404 key_block_valid = 0;
405 }
406 }
407
408 /* If we're not in developer mode, require the key block to be valid. */
409 if (kBootDev != boot_mode && !key_block_valid) {
410 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700411 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700412 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700413
Randall Spangler640fb512011-03-03 10:11:17 -0800414 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700415 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700416 if (!data_key) {
417 VBDEBUG(("Data key bad.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700418 if (shpart)
419 shpart->check_result = VBSD_LKP_CHECK_DATA_KEY_PARSE;
Randall Spangler741d2b22010-08-20 16:37:12 -0700420 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700421 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700422
423 /* Verify the preamble, which follows the key block */
424 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700425 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700426 KBUF_SIZE - key_block->key_block_size,
427 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700428 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700429 if (shpart)
430 shpart->check_result = VBSD_LKP_CHECK_VERIFY_PREAMBLE;
Randall Spangler741d2b22010-08-20 16:37:12 -0700431 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700432 }
433
Randall Spangler640fb512011-03-03 10:11:17 -0800434 /* If the key block is valid and we're not in recovery mode, check for
435 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700436 combined_version = ((key_version << 16) |
437 (preamble->kernel_version & 0xFFFF));
Randall Spangler17c71262011-03-18 11:24:27 -0700438 if (shpart)
439 shpart->combined_version = (uint32_t)combined_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800440 if (key_block_valid && kBootRecovery != boot_mode) {
441 if (combined_version < tpm_version) {
442 VBDEBUG(("Kernel version too low.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700443 if (shpart)
444 shpart->check_result = VBSD_LKP_CHECK_KERNEL_ROLLBACK;
Randall Spangler640fb512011-03-03 10:11:17 -0800445 /* If we're not in developer mode, kernel version must be valid. */
446 if (kBootDev != boot_mode)
447 goto bad_kernel;
448 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700449 }
450
Randall Spanglere2ec9842010-06-23 21:17:07 -0700451 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700452 if (shpart)
453 shpart->check_result = VBSD_LKP_CHECK_PREAMBLE_VALID;
Randall Spangler695cd162010-06-15 23:38:23 -0700454
Randall Spangler66680282010-08-16 12:33:44 -0700455 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800456 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700457 lowest_version = combined_version;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700458 else {
459 VBDEBUG(("Key block valid: %d\n", key_block_valid));
460 VBDEBUG(("Combined version: %" PRIu64 "\n", combined_version));
461 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700462
463 /* If we already have a good kernel, no need to read another
464 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700465 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700466 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700467 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700468
469 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700470 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
471 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700472 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700473 if (shpart)
474 shpart->check_result = VBSD_LKP_CHECK_BODY_ADDRESS;
Randall Spangler741d2b22010-08-20 16:37:12 -0700475 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700476 }
477
478 /* Verify kernel body starts at a multiple of the sector size. */
479 body_offset = key_block->key_block_size + preamble->preamble_size;
480 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700481 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700482 if (shpart)
483 shpart->check_result = VBSD_LKP_CHECK_BODY_OFFSET;
Randall Spangler741d2b22010-08-20 16:37:12 -0700484 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700485 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700486 body_offset_sectors = body_offset / blba;
487
488 /* Verify kernel body fits in the buffer */
489 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
490 if (body_sectors * blba > params->kernel_buffer_size) {
491 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700492 if (shpart)
493 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_MEM;
Randall Spangler741d2b22010-08-20 16:37:12 -0700494 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700495 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700496
497 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700498 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700499 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700500 if (shpart)
501 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_PART;
Randall Spangler741d2b22010-08-20 16:37:12 -0700502 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700503 }
504
505 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700506 VBPERFSTART("VB_RKD");
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700507 if (0 != BootDeviceReadLBA(part_start + body_offset_sectors,
508 body_sectors,
509 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700510 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700511 VBPERFEND("VB_RKD");
Randall Spangler17c71262011-03-18 11:24:27 -0700512 if (shpart)
513 shpart->check_result = VBSD_LKP_CHECK_READ_DATA;
Randall Spangler741d2b22010-08-20 16:37:12 -0700514 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700515 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700516 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700517
518 /* Verify kernel data */
519 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700520 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700521 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700522 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700523 if (shpart)
524 shpart->check_result = VBSD_LKP_CHECK_VERIFY_DATA;
Randall Spangler741d2b22010-08-20 16:37:12 -0700525 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700526 }
527
528 /* Done with the kernel signing key, so can free it now */
529 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700530 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700531
532 /* If we're still here, the kernel is valid. */
533 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800534 VBDEBUG(("Partition is good.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700535 if (shpart) {
536 shpart->check_result = VBSD_LKP_CHECK_KERNEL_GOOD;
537 if (key_block_valid)
538 shpart->flags |= VBSD_LKP_FLAG_KEY_BLOCK_VALID;
539 }
540
Randall Spangler640fb512011-03-03 10:11:17 -0800541 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700542 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
543 * Adjust here, until cgptlib is fixed. */
544 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700545 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700546 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700547 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
548 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700549 params->bootloader_address = preamble->bootloader_address;
550 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700551
552 /* Update GPT to note this is the kernel we're trying */
553 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
554
Randall Spangler640fb512011-03-03 10:11:17 -0800555 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
556 * there's no rollback protection, so we can stop at the first valid
557 * kernel. */
558 if (kBootRecovery == boot_mode || !key_block_valid) {
559 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700560 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700561 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700562
Randall Spangler640fb512011-03-03 10:11:17 -0800563 /* Otherwise, we do care about the key index in the TPM. If the good
564 * partition's key version is the same as the tpm, then the TPM doesn't
565 * need updating; we can stop now. Otherwise, we'll check all the other
566 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700567 if (combined_version == tpm_version) {
568 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700569 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700570 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700571
572 /* Continue, so that we skip the error handling code below */
573 continue;
574
575 bad_kernel:
576 /* Handle errors parsing this kernel */
577 if (NULL != data_key)
578 RSAPublicKeyFree(data_key);
579
580 VBDEBUG(("Marking kernel as invalid.\n"));
581 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
582
583
Randall Spanglerd1836442010-06-10 09:59:04 -0700584 } /* while(GptNextKernelEntry) */
585 } while(0);
586
587 /* Free kernel buffer */
588 if (kbuf)
Randall Spanglere49e8af2011-07-08 13:03:32 -0700589 VbExFree(kbuf);
Randall Spanglerd1836442010-06-10 09:59:04 -0700590
591 /* Write and free GPT data */
592 WriteAndFreeGptData(&gpt);
593
594 /* Handle finding a good partition */
595 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700596 VBDEBUG(("Good_partition >= 0\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700597 if (shcall)
598 shcall->check_result = VBSD_LKC_CHECK_GOOD_PARTITION;
Randall Spanglerd1836442010-06-10 09:59:04 -0700599
600 /* See if we need to update the TPM */
Randall Spangler700fc492011-04-17 10:48:10 -0700601 if ((kBootNormal == boot_mode) &&
602 !((1 == shared->firmware_index) && (shared->flags & VBSD_FWB_TRIED))) {
603 /* We only update the TPM in normal mode. We don't advance the
604 * TPM if we're trying a new firmware B, because that firmware
605 * may have a key change and roll forward the TPM too soon. */
606 VBDEBUG(("Checking if TPM kernel version needs advancing\n"));
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700607
608 if ((lowest_version > tpm_version) &&
609 (lowest_version != LOWEST_TPM_VERSION)) {
Randall Spangler66680282010-08-16 12:33:44 -0700610 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700611 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700612 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800613 if (status == TPM_E_MUST_REBOOT)
614 retval = LOAD_KERNEL_REBOOT;
615 else
616 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
617 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700618 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700619 if (shared)
620 shared->kernel_version_tpm = (uint32_t)lowest_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700621 }
622 }
623
Randall Spangler63dffcb2010-08-05 15:13:14 -0700624 /* Lock the kernel versions */
625 status = RollbackKernelLock();
626 if (0 != status) {
627 VBDEBUG(("Error locking kernel versions.\n"));
628 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800629 if (kBootRecovery != boot_mode) {
630 if (status == TPM_E_MUST_REBOOT)
631 retval = LOAD_KERNEL_REBOOT;
632 else
633 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
634 goto LoadKernelExit;
635 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700636 }
637
638 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800639 retval = LOAD_KERNEL_SUCCESS;
Randall Spangler02223552011-03-15 19:20:25 -0700640 } else {
Randall Spangler17c71262011-03-18 11:24:27 -0700641 if (shcall)
642 shcall->check_result = (found_partitions > 0
643 ? VBSD_LKC_CHECK_INVALID_PARTITIONS
644 : VBSD_LKC_CHECK_NO_PARTITIONS);
645
Randall Spangler02223552011-03-15 19:20:25 -0700646 /* TODO: differentiate between finding an invalid kernel
647 * (found_partitions>0) and not finding one at all. Right now we
648 * treat them the same, and return LOAD_KERNEL_INVALID for both. */
649 retval = LOAD_KERNEL_INVALID;
Randall Spanglerd1836442010-06-10 09:59:04 -0700650 }
651
Randall Spangler640fb512011-03-03 10:11:17 -0800652LoadKernelExit:
653
Randall Spangler640fb512011-03-03 10:11:17 -0800654 /* Store recovery request, if any, then tear down non-volatile storage */
655 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
656 recovery : VBNV_RECOVERY_NOT_REQUESTED);
657 VbNvTeardown(vnc);
658
Randall Spanglerad6824b2011-03-16 19:07:33 -0700659 if (shared) {
Randall Spangler17c71262011-03-18 11:24:27 -0700660 if (shcall)
661 shcall->return_code = (uint8_t)retval;
662
663 /* Save whether the good partition's key block was fully verified */
664 if (good_partition_key_block_valid)
665 shared->flags |= VBSD_KERNEL_KEY_VERIFIED;
666
Randall Spanglerad6824b2011-03-16 19:07:33 -0700667 /* Save timer values */
668 shared->timer_load_kernel_enter = timer_enter;
Randall Spanglere49e8af2011-07-08 13:03:32 -0700669 shared->timer_load_kernel_exit = VbExGetTimer();
Randall Spanglerad6824b2011-03-16 19:07:33 -0700670 /* Store how much shared data we used, if any */
Randall Spangler95c40312011-03-09 15:54:16 -0800671 params->shared_data_size = shared->data_used;
Randall Spanglerad6824b2011-03-16 19:07:33 -0700672 }
Randall Spangler95c40312011-03-09 15:54:16 -0800673
Randall Spangler640fb512011-03-03 10:11:17 -0800674 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700675}