blob: 97d32d344782cd7973034234078d31c336d13200 [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 Spangler1b1998d2011-07-01 16:12:47 -07009
Randall Spanglerd1836442010-06-10 09:59:04 -070010#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 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. */
Randall Spangler1b1998d2011-07-01 16:12:47 -070035int AllocAndReadGptData(VbExDiskHandle_t disk_handle, GptData* gptdata) {
Randall Spangler83c88cf2010-06-11 16:14:18 -070036
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 */
Randall Spanglere49e8af2011-07-08 13:03:32 -070043 gptdata->primary_header = (uint8_t*)VbExMalloc(gptdata->sector_bytes);
44 gptdata->secondary_header = (uint8_t*)VbExMalloc(gptdata->sector_bytes);
45 gptdata->primary_entries = (uint8_t*)VbExMalloc(TOTAL_ENTRIES_SIZE);
46 gptdata->secondary_entries = (uint8_t*)VbExMalloc(TOTAL_ENTRIES_SIZE);
Randall Spangler83c88cf2010-06-11 16:14:18 -070047
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 */
Randall Spangler1b1998d2011-07-01 16:12:47 -070053 if (0 != VbExDiskRead(disk_handle, 1, 1, gptdata->primary_header))
Randall Spangler83c88cf2010-06-11 16:14:18 -070054 return 1;
Randall Spangler1b1998d2011-07-01 16:12:47 -070055 if (0 != VbExDiskRead(disk_handle, 2, entries_sectors,
56 gptdata->primary_entries))
Randall Spangler83c88cf2010-06-11 16:14:18 -070057 return 1;
Randall Spangler1b1998d2011-07-01 16:12:47 -070058 if (0 != VbExDiskRead(disk_handle,
59 gptdata->drive_sectors - entries_sectors - 1,
60 entries_sectors, gptdata->secondary_entries))
Randall Spangler83c88cf2010-06-11 16:14:18 -070061 return 1;
Randall Spangler1b1998d2011-07-01 16:12:47 -070062 if (0 != VbExDiskRead(disk_handle, gptdata->drive_sectors - 1, 1,
63 gptdata->secondary_header))
Randall Spangler83c88cf2010-06-11 16:14:18 -070064 return 1;
65
66 return 0;
67}
68
69
70/* Writes any changes for the GPT data back to the drive, then frees
71 * the buffers.
72 *
73 * Returns 0 if successful, 1 if error. */
Randall Spangler1b1998d2011-07-01 16:12:47 -070074int WriteAndFreeGptData(VbExDiskHandle_t disk_handle, GptData* gptdata) {
Randall Spangler83c88cf2010-06-11 16:14:18 -070075
76 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
77
78 if (gptdata->primary_header) {
79 if (gptdata->modified & GPT_MODIFIED_HEADER1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070080 VBDEBUG(("Updating GPT header 1\n"));
Randall Spangler1b1998d2011-07-01 16:12:47 -070081 if (0 != VbExDiskWrite(disk_handle, 1, 1, gptdata->primary_header))
Randall Spangler83c88cf2010-06-11 16:14:18 -070082 return 1;
83 }
Randall Spanglere49e8af2011-07-08 13:03:32 -070084 VbExFree(gptdata->primary_header);
Randall Spangler83c88cf2010-06-11 16:14:18 -070085 }
86
87 if (gptdata->primary_entries) {
88 if (gptdata->modified & GPT_MODIFIED_ENTRIES1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070089 VBDEBUG(("Updating GPT entries 1\n"));
Randall Spangler1b1998d2011-07-01 16:12:47 -070090 if (0 != VbExDiskWrite(disk_handle, 2, entries_sectors,
91 gptdata->primary_entries))
Randall Spangler83c88cf2010-06-11 16:14:18 -070092 return 1;
93 }
Randall Spanglere49e8af2011-07-08 13:03:32 -070094 VbExFree(gptdata->primary_entries);
Randall Spangler83c88cf2010-06-11 16:14:18 -070095 }
96
97 if (gptdata->secondary_entries) {
98 if (gptdata->modified & GPT_MODIFIED_ENTRIES2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070099 VBDEBUG(("Updating GPT header 2\n"));
Randall Spangler1b1998d2011-07-01 16:12:47 -0700100 if (0 != VbExDiskWrite(disk_handle,
101 gptdata->drive_sectors - entries_sectors - 1,
102 entries_sectors, gptdata->secondary_entries))
Randall Spangler83c88cf2010-06-11 16:14:18 -0700103 return 1;
104 }
Randall Spanglere49e8af2011-07-08 13:03:32 -0700105 VbExFree(gptdata->secondary_entries);
Randall Spangler83c88cf2010-06-11 16:14:18 -0700106 }
107
108 if (gptdata->secondary_header) {
109 if (gptdata->modified & GPT_MODIFIED_HEADER2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700110 VBDEBUG(("Updating GPT entries 2\n"));
Randall Spangler1b1998d2011-07-01 16:12:47 -0700111 if (0 != VbExDiskWrite(disk_handle, gptdata->drive_sectors - 1, 1,
112 gptdata->secondary_header))
Randall Spangler83c88cf2010-06-11 16:14:18 -0700113 return 1;
114 }
Randall Spanglere49e8af2011-07-08 13:03:32 -0700115 VbExFree(gptdata->secondary_header);
Randall Spangler83c88cf2010-06-11 16:14:18 -0700116 }
117
118 /* Success */
119 return 0;
120}
121
vbendeb3ecaf772010-06-24 16:19:53 -0700122/* disable MSVC warning on const logical expression (as in } while(0);) */
123__pragma(warning(disable: 4127))
Randall Spangler83c88cf2010-06-11 16:14:18 -0700124
Randall Spanglerbd529f02010-06-16 12:51:26 -0700125int LoadKernel(LoadKernelParams* params) {
Randall Spangler95c40312011-03-09 15:54:16 -0800126 VbSharedDataHeader* shared = (VbSharedDataHeader*)params->shared_data_blob;
Randall Spangler17c71262011-03-18 11:24:27 -0700127 VbSharedDataKernelCall* shcall = NULL;
Randall Spangler640fb512011-03-03 10:11:17 -0800128 VbNvContext* vnc = params->nv_context;
Randall Spangler95c40312011-03-09 15:54:16 -0800129 GoogleBinaryBlockHeader* gbb = (GoogleBinaryBlockHeader*)params->gbb_data;
Bill Richardsone2729402010-07-22 12:23:47 -0700130 VbPublicKey* kernel_subkey;
Randall Spanglerd1836442010-06-10 09:59:04 -0700131 GptData gpt;
132 uint64_t part_start, part_size;
Bill Richardsone2729402010-07-22 12:23:47 -0700133 uint64_t blba;
134 uint64_t kbuf_sectors;
Randall Spanglerd1836442010-06-10 09:59:04 -0700135 uint8_t* kbuf = NULL;
136 int found_partitions = 0;
137 int good_partition = -1;
Randall Spangler640fb512011-03-03 10:11:17 -0800138 int good_partition_key_block_valid = 0;
Randall Spangler66680282010-08-16 12:33:44 -0700139 uint32_t tpm_version = 0;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700140 uint64_t lowest_version = LOWEST_TPM_VERSION;
Randall Spangler640fb512011-03-03 10:11:17 -0800141 int rec_switch, dev_switch;
142 BootMode boot_mode;
Randall Spangler99ca3462011-03-15 15:28:31 -0700143 uint32_t test_err = 0;
Randall Spangler7a786b72010-07-08 13:29:42 -0700144 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700145
Randall Spangler02223552011-03-15 19:20:25 -0700146 int retval = LOAD_KERNEL_RECOVERY;
Randall Spangler640fb512011-03-03 10:11:17 -0800147 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED;
148
149 /* Setup NV storage */
150 VbNvSetup(vnc);
151
Bill Richardsone2729402010-07-22 12:23:47 -0700152 /* Sanity Checks */
153 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700154 !params->bytes_per_lba ||
155 !params->ending_lba ||
156 !params->kernel_buffer ||
157 !params->kernel_buffer_size) {
158 VBDEBUG(("LoadKernel() called with invalid params\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800159 goto LoadKernelExit;
Bill Richardsone2729402010-07-22 12:23:47 -0700160 }
161
Randall Spanglerad6824b2011-03-16 19:07:33 -0700162 /* Clear output params in case we fail */
163 params->partition_number = 0;
164 params->bootloader_address = 0;
165 params->bootloader_size = 0;
166
Randall Spangler17c71262011-03-18 11:24:27 -0700167 /* Calculate switch positions and boot mode */
168 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
169 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
170 if (rec_switch)
171 boot_mode = kBootRecovery;
172 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags)
173 boot_mode = kBootDev;
174 else {
175 /* Normal firmware */
176 boot_mode = kBootNormal;
177 dev_switch = 0; /* Always do a fully verified boot */
178 }
179
Randall Spangler96191122011-07-08 14:01:54 -0700180 /* Set up tracking for this call. This wraps around if called many times,
181 * so we need to initialize the call entry each time. */
182 shcall = shared->lk_calls + (shared->lk_call_count
183 & (VBSD_MAX_KERNEL_CALLS - 1));
184 Memset(shcall, 0, sizeof(VbSharedDataKernelCall));
185 shcall->boot_flags = (uint32_t)params->boot_flags;
186 shcall->boot_mode = boot_mode;
187 shcall->sector_size = (uint32_t)params->bytes_per_lba;
188 shcall->sector_count = params->ending_lba + 1;
189 shared->lk_call_count++;
Randall Spangler17c71262011-03-18 11:24:27 -0700190
Randall Spangler99ca3462011-03-15 15:28:31 -0700191 /* Handle test errors */
192 VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err);
193 if (VBNV_TEST_ERROR_LOAD_KERNEL == test_err) {
194 /* Get error code */
195 VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err);
Randall Spangler96191122011-07-08 14:01:54 -0700196 shcall->test_error_num = (uint8_t)test_err;
Randall Spangler99ca3462011-03-15 15:28:31 -0700197 /* Clear test params so we don't repeat the error */
198 VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0);
199 VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0);
200 /* Handle error codes */
201 switch (test_err) {
202 case LOAD_KERNEL_RECOVERY:
203 recovery = VBNV_RECOVERY_RW_TEST_LK;
204 goto LoadKernelExit;
205 case LOAD_KERNEL_NOT_FOUND:
206 case LOAD_KERNEL_INVALID:
207 case LOAD_KERNEL_REBOOT:
208 retval = test_err;
209 goto LoadKernelExit;
210 default:
211 break;
212 }
213 }
214
Bill Richardsone2729402010-07-22 12:23:47 -0700215 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700216 blba = params->bytes_per_lba;
217 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700218 if (0 == kbuf_sectors) {
219 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800220 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700221 }
222
Randall Spangler17c71262011-03-18 11:24:27 -0700223 if (kBootDev == boot_mode && !dev_switch) {
Randall Spangler96191122011-07-08 14:01:54 -0700224 /* Dev firmware should be signed such that it never boots with the dev
225 * switch is off; so something is terribly wrong. */
Randall Spanglerc90e7e82011-05-17 15:27:42 -0700226 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700227 shcall->check_result = VBSD_LKC_CHECK_DEV_SWITCH_MISMATCH;
Randall Spanglerc90e7e82011-05-17 15:27:42 -0700228 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
229 goto LoadKernelExit;
230 }
Bill Richardsone2729402010-07-22 12:23:47 -0700231
Randall Spangler640fb512011-03-03 10:11:17 -0800232 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800233 /* Use the recovery key to verify the kernel */
234 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
235
236 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800237 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700238 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700239 shcall->flags |= VBSD_LK_FLAG_REC_TPM_INIT_ERROR;
Randall Spangler63dffcb2010-08-05 15:13:14 -0700240 /* Ignore return code, since we need to boot recovery mode to
241 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700242 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700243
244 /* Read the key indices from the TPM; ignore any errors */
Randall Spangler96191122011-07-08 14:01:54 -0700245 RollbackFirmwareRead(&shared->fw_version_tpm);
246 RollbackKernelRead(&shared->kernel_version_tpm);
Randall Spangler640fb512011-03-03 10:11:17 -0800247 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800248 /* Use the kernel subkey passed from LoadFirmware(). */
249 kernel_subkey = &shared->kernel_subkey;
250
Randall Spanglerd1836442010-06-10 09:59:04 -0700251 /* Read current kernel key index from TPM. Assumes TPM is already
252 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700253 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700254 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700255 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800256 if (status == TPM_E_MUST_REBOOT)
257 retval = LOAD_KERNEL_REBOOT;
258 else
259 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
260 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700261 }
Randall Spangler96191122011-07-08 14:01:54 -0700262 shared->kernel_version_tpm = tpm_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700263 }
264
265 do {
266 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700267 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700268 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler1b1998d2011-07-01 16:12:47 -0700269 if (0 != AllocAndReadGptData(params->disk_handle, &gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700270 VBDEBUG(("Unable to read GPT data\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700271 shcall->check_result = VBSD_LKC_CHECK_GPT_READ_ERROR;
Randall Spanglerd1836442010-06-10 09:59:04 -0700272 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700273 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700274
275 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700276 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700277 VBDEBUG(("Error parsing GPT\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700278 shcall->check_result = VBSD_LKC_CHECK_GPT_PARSE_ERROR;
Randall Spanglerd1836442010-06-10 09:59:04 -0700279 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700280 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700281
Randall Spanglerd1836442010-06-10 09:59:04 -0700282 /* Allocate kernel header buffers */
Randall Spanglere49e8af2011-07-08 13:03:32 -0700283 kbuf = (uint8_t*)VbExMalloc(KBUF_SIZE);
Randall Spanglerd1836442010-06-10 09:59:04 -0700284 if (!kbuf)
285 break;
286
287 /* Loop over candidate kernel partitions */
288 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
Randall Spangler17c71262011-03-18 11:24:27 -0700289 VbSharedDataKernelPart* shpart = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700290 VbKeyBlockHeader* key_block;
291 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700292 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700293 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700294 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700295 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700296 uint64_t body_offset_sectors;
297 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800298 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700299
Randall Spanglere2ec9842010-06-23 21:17:07 -0700300 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
301 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700302
Randall Spangler96191122011-07-08 14:01:54 -0700303 /* Set up tracking for this partition. This wraps around if called
304 * many times, so initialize the partition entry each time. */
305 shpart = shcall->parts + (shcall->kernel_parts_found
306 & (VBSD_MAX_KERNEL_PARTS - 1));
307 Memset(shpart, 0, sizeof(VbSharedDataKernelPart));
308 shpart->sector_start = part_start;
309 shpart->sector_count = part_size;
310 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
311 * Adjust here, until cgptlib is fixed. */
312 shpart->gpt_index = (uint8_t)(gpt.current_kernel + 1);
313 shcall->kernel_parts_found++;
Randall Spangler17c71262011-03-18 11:24:27 -0700314
Randall Spanglerd1836442010-06-10 09:59:04 -0700315 /* Found at least one kernel partition. */
316 found_partitions++;
317
Randall Spangler640fb512011-03-03 10:11:17 -0800318 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700319 if (part_size < kbuf_sectors) {
320 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700321 shpart->check_result = VBSD_LKP_CHECK_TOO_SMALL;
Randall Spangler741d2b22010-08-20 16:37:12 -0700322 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700323 }
324
Randall Spangler1b1998d2011-07-01 16:12:47 -0700325 if (0 != VbExDiskRead(params->disk_handle, part_start, kbuf_sectors,
326 kbuf)) {
Randall Spangler77ae3892010-09-09 17:37:51 -0700327 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700328 shpart->check_result = VBSD_LKP_CHECK_READ_START;
Randall Spangler741d2b22010-08-20 16:37:12 -0700329 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700330 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700331
Randall Spangler640fb512011-03-03 10:11:17 -0800332 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700333 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800334 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
335 VBDEBUG(("Verifying key block signature failed.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700336 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_SIG;
Randall Spangler17c71262011-03-18 11:24:27 -0700337
Randall Spangler640fb512011-03-03 10:11:17 -0800338 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700339
Randall Spangler640fb512011-03-03 10:11:17 -0800340 /* If we're not in developer mode, this kernel is bad. */
341 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700342 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800343
344 /* In developer mode, we can continue if the SHA-512 hash of the key
345 * block is valid. */
346 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
347 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700348 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_HASH;
Randall Spangler741d2b22010-08-20 16:37:12 -0700349 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700350 }
Randall Spangler695cd162010-06-15 23:38:23 -0700351 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700352
Randall Spangler640fb512011-03-03 10:11:17 -0800353 /* Check the key block flags against the current boot mode. */
354 if (!(key_block->key_block_flags &
355 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
356 KEY_BLOCK_FLAG_DEVELOPER_0))) {
357 VBDEBUG(("Key block developer flag mismatch.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700358 shpart->check_result = VBSD_LKP_CHECK_DEV_MISMATCH;
Randall Spangler640fb512011-03-03 10:11:17 -0800359 key_block_valid = 0;
360 }
361 if (!(key_block->key_block_flags &
362 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
363 KEY_BLOCK_FLAG_RECOVERY_0))) {
364 VBDEBUG(("Key block recovery flag mismatch.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700365 shpart->check_result = VBSD_LKP_CHECK_REC_MISMATCH;
Randall Spangler640fb512011-03-03 10:11:17 -0800366 key_block_valid = 0;
367 }
368
369 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700370 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800371 if (kBootRecovery != boot_mode) {
372 if (key_version < (tpm_version >> 16)) {
373 VBDEBUG(("Key version too old.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700374 shpart->check_result = VBSD_LKP_CHECK_KEY_ROLLBACK;
Randall Spangler640fb512011-03-03 10:11:17 -0800375 key_block_valid = 0;
376 }
377 }
378
379 /* If we're not in developer mode, require the key block to be valid. */
380 if (kBootDev != boot_mode && !key_block_valid) {
381 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700382 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700383 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700384
Randall Spangler640fb512011-03-03 10:11:17 -0800385 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700386 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700387 if (!data_key) {
388 VBDEBUG(("Data key bad.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700389 shpart->check_result = VBSD_LKP_CHECK_DATA_KEY_PARSE;
Randall Spangler741d2b22010-08-20 16:37:12 -0700390 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700391 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700392
393 /* Verify the preamble, which follows the key block */
394 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700395 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700396 KBUF_SIZE - key_block->key_block_size,
397 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700398 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700399 shpart->check_result = VBSD_LKP_CHECK_VERIFY_PREAMBLE;
Randall Spangler741d2b22010-08-20 16:37:12 -0700400 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700401 }
402
Randall Spangler640fb512011-03-03 10:11:17 -0800403 /* If the key block is valid and we're not in recovery mode, check for
404 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700405 combined_version = ((key_version << 16) |
406 (preamble->kernel_version & 0xFFFF));
Randall Spangler96191122011-07-08 14:01:54 -0700407 shpart->combined_version = (uint32_t)combined_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800408 if (key_block_valid && kBootRecovery != boot_mode) {
409 if (combined_version < tpm_version) {
410 VBDEBUG(("Kernel version too low.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700411 shpart->check_result = VBSD_LKP_CHECK_KERNEL_ROLLBACK;
Randall Spangler640fb512011-03-03 10:11:17 -0800412 /* If we're not in developer mode, kernel version must be valid. */
413 if (kBootDev != boot_mode)
414 goto bad_kernel;
415 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700416 }
417
Randall Spanglere2ec9842010-06-23 21:17:07 -0700418 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700419 shpart->check_result = VBSD_LKP_CHECK_PREAMBLE_VALID;
Randall Spangler695cd162010-06-15 23:38:23 -0700420
Randall Spangler66680282010-08-16 12:33:44 -0700421 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800422 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700423 lowest_version = combined_version;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700424 else {
425 VBDEBUG(("Key block valid: %d\n", key_block_valid));
426 VBDEBUG(("Combined version: %" PRIu64 "\n", combined_version));
427 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700428
429 /* If we already have a good kernel, no need to read another
430 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700431 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700432 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700433 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700434
435 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700436 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
437 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700438 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700439 shpart->check_result = VBSD_LKP_CHECK_BODY_ADDRESS;
Randall Spangler741d2b22010-08-20 16:37:12 -0700440 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700441 }
442
443 /* Verify kernel body starts at a multiple of the sector size. */
444 body_offset = key_block->key_block_size + preamble->preamble_size;
445 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700446 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700447 shpart->check_result = VBSD_LKP_CHECK_BODY_OFFSET;
Randall Spangler741d2b22010-08-20 16:37:12 -0700448 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700449 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700450 body_offset_sectors = body_offset / blba;
451
452 /* Verify kernel body fits in the buffer */
453 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
454 if (body_sectors * blba > params->kernel_buffer_size) {
455 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700456 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_MEM;
Randall Spangler741d2b22010-08-20 16:37:12 -0700457 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700458 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700459
460 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700461 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700462 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700463 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_PART;
Randall Spangler741d2b22010-08-20 16:37:12 -0700464 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700465 }
466
467 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700468 VBPERFSTART("VB_RKD");
Randall Spangler1b1998d2011-07-01 16:12:47 -0700469 if (0 != VbExDiskRead(params->disk_handle,
470 part_start + body_offset_sectors,
471 body_sectors, params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700472 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700473 VBPERFEND("VB_RKD");
Randall Spangler96191122011-07-08 14:01:54 -0700474 shpart->check_result = VBSD_LKP_CHECK_READ_DATA;
Randall Spangler741d2b22010-08-20 16:37:12 -0700475 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700476 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700477 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700478
479 /* Verify kernel data */
480 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700481 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700482 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700483 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700484 shpart->check_result = VBSD_LKP_CHECK_VERIFY_DATA;
Randall Spangler741d2b22010-08-20 16:37:12 -0700485 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700486 }
487
488 /* Done with the kernel signing key, so can free it now */
489 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700490 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700491
492 /* If we're still here, the kernel is valid. */
493 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800494 VBDEBUG(("Partition is good.\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700495 shpart->check_result = VBSD_LKP_CHECK_KERNEL_GOOD;
496 if (key_block_valid)
497 shpart->flags |= VBSD_LKP_FLAG_KEY_BLOCK_VALID;
Randall Spangler17c71262011-03-18 11:24:27 -0700498
Randall Spangler640fb512011-03-03 10:11:17 -0800499 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700500 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
501 * Adjust here, until cgptlib is fixed. */
502 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700503 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700504 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700505 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
506 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700507 params->bootloader_address = preamble->bootloader_address;
508 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700509
510 /* Update GPT to note this is the kernel we're trying */
511 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
512
Randall Spangler640fb512011-03-03 10:11:17 -0800513 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
514 * there's no rollback protection, so we can stop at the first valid
515 * kernel. */
516 if (kBootRecovery == boot_mode || !key_block_valid) {
517 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700518 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700519 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700520
Randall Spangler640fb512011-03-03 10:11:17 -0800521 /* Otherwise, we do care about the key index in the TPM. If the good
522 * partition's key version is the same as the tpm, then the TPM doesn't
523 * need updating; we can stop now. Otherwise, we'll check all the other
524 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700525 if (combined_version == tpm_version) {
526 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700527 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700528 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700529
530 /* Continue, so that we skip the error handling code below */
531 continue;
532
533 bad_kernel:
534 /* Handle errors parsing this kernel */
535 if (NULL != data_key)
536 RSAPublicKeyFree(data_key);
537
538 VBDEBUG(("Marking kernel as invalid.\n"));
539 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
540
541
Randall Spanglerd1836442010-06-10 09:59:04 -0700542 } /* while(GptNextKernelEntry) */
543 } while(0);
544
545 /* Free kernel buffer */
546 if (kbuf)
Randall Spanglere49e8af2011-07-08 13:03:32 -0700547 VbExFree(kbuf);
Randall Spanglerd1836442010-06-10 09:59:04 -0700548
549 /* Write and free GPT data */
Randall Spangler1b1998d2011-07-01 16:12:47 -0700550 WriteAndFreeGptData(params->disk_handle, &gpt);
Randall Spanglerd1836442010-06-10 09:59:04 -0700551
552 /* Handle finding a good partition */
553 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700554 VBDEBUG(("Good_partition >= 0\n"));
Randall Spangler96191122011-07-08 14:01:54 -0700555 shcall->check_result = VBSD_LKC_CHECK_GOOD_PARTITION;
Randall Spanglerd1836442010-06-10 09:59:04 -0700556
557 /* See if we need to update the TPM */
Randall Spangler700fc492011-04-17 10:48:10 -0700558 if ((kBootNormal == boot_mode) &&
559 !((1 == shared->firmware_index) && (shared->flags & VBSD_FWB_TRIED))) {
560 /* We only update the TPM in normal mode. We don't advance the
561 * TPM if we're trying a new firmware B, because that firmware
562 * may have a key change and roll forward the TPM too soon. */
563 VBDEBUG(("Checking if TPM kernel version needs advancing\n"));
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700564
565 if ((lowest_version > tpm_version) &&
566 (lowest_version != LOWEST_TPM_VERSION)) {
Randall Spangler66680282010-08-16 12:33:44 -0700567 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700568 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700569 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800570 if (status == TPM_E_MUST_REBOOT)
571 retval = LOAD_KERNEL_REBOOT;
572 else
573 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
574 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700575 }
Randall Spangler96191122011-07-08 14:01:54 -0700576 shared->kernel_version_tpm = (uint32_t)lowest_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700577 }
578 }
579
Randall Spangler63dffcb2010-08-05 15:13:14 -0700580 /* Lock the kernel versions */
581 status = RollbackKernelLock();
582 if (0 != status) {
583 VBDEBUG(("Error locking kernel versions.\n"));
584 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800585 if (kBootRecovery != boot_mode) {
586 if (status == TPM_E_MUST_REBOOT)
587 retval = LOAD_KERNEL_REBOOT;
588 else
589 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
590 goto LoadKernelExit;
591 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700592 }
593
594 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800595 retval = LOAD_KERNEL_SUCCESS;
Randall Spangler02223552011-03-15 19:20:25 -0700596 } else {
Randall Spangler96191122011-07-08 14:01:54 -0700597 shcall->check_result = (found_partitions > 0
598 ? VBSD_LKC_CHECK_INVALID_PARTITIONS
599 : VBSD_LKC_CHECK_NO_PARTITIONS);
Randall Spangler17c71262011-03-18 11:24:27 -0700600
Randall Spangler02223552011-03-15 19:20:25 -0700601 /* TODO: differentiate between finding an invalid kernel
602 * (found_partitions>0) and not finding one at all. Right now we
603 * treat them the same, and return LOAD_KERNEL_INVALID for both. */
604 retval = LOAD_KERNEL_INVALID;
Randall Spanglerd1836442010-06-10 09:59:04 -0700605 }
606
Randall Spangler640fb512011-03-03 10:11:17 -0800607LoadKernelExit:
608
Randall Spangler640fb512011-03-03 10:11:17 -0800609 /* Store recovery request, if any, then tear down non-volatile storage */
610 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
611 recovery : VBNV_RECOVERY_NOT_REQUESTED);
612 VbNvTeardown(vnc);
613
Randall Spangler96191122011-07-08 14:01:54 -0700614 shcall->return_code = (uint8_t)retval;
Randall Spangler17c71262011-03-18 11:24:27 -0700615
Randall Spangler96191122011-07-08 14:01:54 -0700616 /* Save whether the good partition's key block was fully verified */
617 if (good_partition_key_block_valid)
618 shared->flags |= VBSD_KERNEL_KEY_VERIFIED;
Randall Spangler17c71262011-03-18 11:24:27 -0700619
Randall Spangler96191122011-07-08 14:01:54 -0700620 /* Store how much shared data we used, if any */
621 params->shared_data_size = shared->data_used;
Randall Spangler95c40312011-03-09 15:54:16 -0800622
Randall Spangler640fb512011-03-03 10:11:17 -0800623 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700624}