blob: 26c9121037d4e7ed708cb2d1a54d90eefa85fb7f [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;
Randall Spanglere49e8af2011-07-08 13:03:32 -0700148 uint64_t timer_enter = VbExGetTimer();
Randall Spangler640fb512011-03-03 10:11:17 -0800149
150 /* Setup NV storage */
151 VbNvSetup(vnc);
152
Bill Richardsone2729402010-07-22 12:23:47 -0700153 /* Sanity Checks */
154 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700155 !params->bytes_per_lba ||
156 !params->ending_lba ||
157 !params->kernel_buffer ||
158 !params->kernel_buffer_size) {
159 VBDEBUG(("LoadKernel() called with invalid params\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800160 goto LoadKernelExit;
Bill Richardsone2729402010-07-22 12:23:47 -0700161 }
162
Randall Spanglerad6824b2011-03-16 19:07:33 -0700163 /* Clear output params in case we fail */
164 params->partition_number = 0;
165 params->bootloader_address = 0;
166 params->bootloader_size = 0;
167
Randall Spangler17c71262011-03-18 11:24:27 -0700168 /* Calculate switch positions and boot mode */
169 rec_switch = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
170 dev_switch = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
171 if (rec_switch)
172 boot_mode = kBootRecovery;
173 else if (BOOT_FLAG_DEV_FIRMWARE & params->boot_flags)
174 boot_mode = kBootDev;
175 else {
176 /* Normal firmware */
177 boot_mode = kBootNormal;
178 dev_switch = 0; /* Always do a fully verified boot */
179 }
180
181 if (kBootRecovery == boot_mode) {
182 /* Initialize the shared data structure, since LoadFirmware() didn't do it
183 * for us. */
184 if (0 != VbSharedDataInit(shared, params->shared_data_size)) {
185 /* Error initializing the shared data, but we can keep going. We just
186 * can't use the shared data. */
187 VBDEBUG(("Shared data init error\n"));
188 params->shared_data_size = 0;
189 shared = NULL;
190 }
191 }
192
193 if (shared) {
194 /* Set up tracking for this call. This wraps around if called many times,
195 * so we need to initialize the call entry each time. */
196 shcall = shared->lk_calls + (shared->lk_call_count
197 & (VBSD_MAX_KERNEL_CALLS - 1));
198 Memset(shcall, 0, sizeof(VbSharedDataKernelCall));
199 shcall->boot_flags = (uint32_t)params->boot_flags;
200 shcall->boot_mode = boot_mode;
201 shcall->sector_size = (uint32_t)params->bytes_per_lba;
202 shcall->sector_count = params->ending_lba + 1;
203 shared->lk_call_count++;
204 }
205
Randall Spangler99ca3462011-03-15 15:28:31 -0700206 /* Handle test errors */
207 VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err);
208 if (VBNV_TEST_ERROR_LOAD_KERNEL == test_err) {
209 /* Get error code */
210 VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err);
Randall Spangler17c71262011-03-18 11:24:27 -0700211 if (shcall)
212 shcall->test_error_num = (uint8_t)test_err;
Randall Spangler99ca3462011-03-15 15:28:31 -0700213 /* Clear test params so we don't repeat the error */
214 VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0);
215 VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0);
216 /* Handle error codes */
217 switch (test_err) {
218 case LOAD_KERNEL_RECOVERY:
219 recovery = VBNV_RECOVERY_RW_TEST_LK;
220 goto LoadKernelExit;
221 case LOAD_KERNEL_NOT_FOUND:
222 case LOAD_KERNEL_INVALID:
223 case LOAD_KERNEL_REBOOT:
224 retval = test_err;
225 goto LoadKernelExit;
226 default:
227 break;
228 }
229 }
230
Bill Richardsone2729402010-07-22 12:23:47 -0700231 /* Initialization */
Bill Richardsone2729402010-07-22 12:23:47 -0700232 blba = params->bytes_per_lba;
233 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700234 if (0 == kbuf_sectors) {
235 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800236 goto LoadKernelExit;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700237 }
238
Randall Spangler17c71262011-03-18 11:24:27 -0700239 if (kBootDev == boot_mode && !dev_switch) {
Randall Spanglerc90e7e82011-05-17 15:27:42 -0700240 /* Dev firmware should be signed such that it never boots with the dev
241 * switch is off; so something is terribly wrong. */
242 VBDEBUG(("LoadKernel() called with dev firmware but dev switch off\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700243 if (shcall)
244 shcall->check_result = VBSD_LKC_CHECK_DEV_SWITCH_MISMATCH;
Randall Spanglerc90e7e82011-05-17 15:27:42 -0700245 recovery = VBNV_RECOVERY_RW_DEV_MISMATCH;
246 goto LoadKernelExit;
247 }
Bill Richardsone2729402010-07-22 12:23:47 -0700248
Randall Spangler640fb512011-03-03 10:11:17 -0800249 if (kBootRecovery == boot_mode) {
Randall Spangler95c40312011-03-09 15:54:16 -0800250 /* Use the recovery key to verify the kernel */
251 kernel_subkey = (VbPublicKey*)((uint8_t*)gbb + gbb->recovery_key_offset);
252
253 /* Let the TPM know if we're in recovery mode */
Randall Spangler640fb512011-03-03 10:11:17 -0800254 if (0 != RollbackKernelRecovery(dev_switch)) {
Randall Spangler63dffcb2010-08-05 15:13:14 -0700255 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700256 if (shcall)
257 shcall->flags |= VBSD_LK_FLAG_REC_TPM_INIT_ERROR;
Randall Spangler63dffcb2010-08-05 15:13:14 -0700258 /* Ignore return code, since we need to boot recovery mode to
259 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700260 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700261
262 /* Read the key indices from the TPM; ignore any errors */
263 if (shared) {
264 RollbackFirmwareRead(&shared->fw_version_tpm);
265 RollbackKernelRead(&shared->kernel_version_tpm);
266 }
Randall Spangler640fb512011-03-03 10:11:17 -0800267 } else {
Randall Spangler95c40312011-03-09 15:54:16 -0800268 /* Use the kernel subkey passed from LoadFirmware(). */
269 kernel_subkey = &shared->kernel_subkey;
270
Randall Spanglerd1836442010-06-10 09:59:04 -0700271 /* Read current kernel key index from TPM. Assumes TPM is already
272 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700273 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700274 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700275 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800276 if (status == TPM_E_MUST_REBOOT)
277 retval = LOAD_KERNEL_REBOOT;
278 else
279 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
280 goto LoadKernelExit;
Randall Spangler695cd162010-06-15 23:38:23 -0700281 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700282 if (shared)
283 shared->kernel_version_tpm = tpm_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700284 }
285
286 do {
287 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700288 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700289 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler1b1998d2011-07-01 16:12:47 -0700290 if (0 != AllocAndReadGptData(params->disk_handle, &gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700291 VBDEBUG(("Unable to read GPT data\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700292 if (shcall)
293 shcall->check_result = VBSD_LKC_CHECK_GPT_READ_ERROR;
Randall Spanglerd1836442010-06-10 09:59:04 -0700294 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700295 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700296
297 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700298 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700299 VBDEBUG(("Error parsing GPT\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700300 if (shcall)
301 shcall->check_result = VBSD_LKC_CHECK_GPT_PARSE_ERROR;
Randall Spanglerd1836442010-06-10 09:59:04 -0700302 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700303 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700304
Randall Spanglerd1836442010-06-10 09:59:04 -0700305 /* Allocate kernel header buffers */
Randall Spanglere49e8af2011-07-08 13:03:32 -0700306 kbuf = (uint8_t*)VbExMalloc(KBUF_SIZE);
Randall Spanglerd1836442010-06-10 09:59:04 -0700307 if (!kbuf)
308 break;
309
310 /* Loop over candidate kernel partitions */
311 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
Randall Spangler17c71262011-03-18 11:24:27 -0700312 VbSharedDataKernelPart* shpart = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700313 VbKeyBlockHeader* key_block;
314 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700315 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700316 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700317 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700318 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700319 uint64_t body_offset_sectors;
320 uint64_t body_sectors;
Randall Spangler640fb512011-03-03 10:11:17 -0800321 int key_block_valid = 1;
Randall Spanglerd1836442010-06-10 09:59:04 -0700322
Randall Spanglere2ec9842010-06-23 21:17:07 -0700323 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
324 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700325
Randall Spangler17c71262011-03-18 11:24:27 -0700326 if (shcall) {
327 /* Set up tracking for this partition. This wraps around if called
328 * many times, so initialize the partition entry each time. */
329 shpart = shcall->parts + (shcall->kernel_parts_found
330 & (VBSD_MAX_KERNEL_PARTS - 1));
331 Memset(shpart, 0, sizeof(VbSharedDataKernelPart));
332 shpart->sector_start = part_start;
333 shpart->sector_count = part_size;
334 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
335 * Adjust here, until cgptlib is fixed. */
336 shpart->gpt_index = (uint8_t)(gpt.current_kernel + 1);
337 shcall->kernel_parts_found++;
338 }
339
Randall Spanglerd1836442010-06-10 09:59:04 -0700340 /* Found at least one kernel partition. */
341 found_partitions++;
342
Randall Spangler640fb512011-03-03 10:11:17 -0800343 /* Read the first part of the kernel partition. */
Randall Spangler77ae3892010-09-09 17:37:51 -0700344 if (part_size < kbuf_sectors) {
345 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700346 if (shpart)
347 shpart->check_result = VBSD_LKP_CHECK_TOO_SMALL;
Randall Spangler741d2b22010-08-20 16:37:12 -0700348 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700349 }
350
Randall Spangler1b1998d2011-07-01 16:12:47 -0700351 if (0 != VbExDiskRead(params->disk_handle, part_start, kbuf_sectors,
352 kbuf)) {
Randall Spangler77ae3892010-09-09 17:37:51 -0700353 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700354 if (shpart)
355 shpart->check_result = VBSD_LKP_CHECK_READ_START;
Randall Spangler741d2b22010-08-20 16:37:12 -0700356 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700357 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700358
Randall Spangler640fb512011-03-03 10:11:17 -0800359 /* Verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700360 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler640fb512011-03-03 10:11:17 -0800361 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 0)) {
362 VBDEBUG(("Verifying key block signature failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700363 if (shpart)
364 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_SIG;
365
Randall Spangler640fb512011-03-03 10:11:17 -0800366 key_block_valid = 0;
Randall Spanglerd1836442010-06-10 09:59:04 -0700367
Randall Spangler640fb512011-03-03 10:11:17 -0800368 /* If we're not in developer mode, this kernel is bad. */
369 if (kBootDev != boot_mode)
Randall Spangler741d2b22010-08-20 16:37:12 -0700370 goto bad_kernel;
Randall Spangler640fb512011-03-03 10:11:17 -0800371
372 /* In developer mode, we can continue if the SHA-512 hash of the key
373 * block is valid. */
374 if (0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey, 1)) {
375 VBDEBUG(("Verifying key block hash failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700376 if (shpart)
377 shpart->check_result = VBSD_LKP_CHECK_KEY_BLOCK_HASH;
Randall Spangler741d2b22010-08-20 16:37:12 -0700378 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700379 }
Randall Spangler695cd162010-06-15 23:38:23 -0700380 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700381
Randall Spangler640fb512011-03-03 10:11:17 -0800382 /* Check the key block flags against the current boot mode. */
383 if (!(key_block->key_block_flags &
384 (dev_switch ? KEY_BLOCK_FLAG_DEVELOPER_1 :
385 KEY_BLOCK_FLAG_DEVELOPER_0))) {
386 VBDEBUG(("Key block developer flag mismatch.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700387 if (shpart)
388 shpart->check_result = VBSD_LKP_CHECK_DEV_MISMATCH;
Randall Spangler640fb512011-03-03 10:11:17 -0800389 key_block_valid = 0;
390 }
391 if (!(key_block->key_block_flags &
392 (rec_switch ? KEY_BLOCK_FLAG_RECOVERY_1 :
393 KEY_BLOCK_FLAG_RECOVERY_0))) {
394 VBDEBUG(("Key block recovery flag mismatch.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700395 if (shpart)
396 shpart->check_result = VBSD_LKP_CHECK_REC_MISMATCH;
Randall Spangler640fb512011-03-03 10:11:17 -0800397 key_block_valid = 0;
398 }
399
400 /* Check for rollback of key version except in recovery mode. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700401 key_version = key_block->data_key.key_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800402 if (kBootRecovery != boot_mode) {
403 if (key_version < (tpm_version >> 16)) {
404 VBDEBUG(("Key version too old.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700405 if (shpart)
406 shpart->check_result = VBSD_LKP_CHECK_KEY_ROLLBACK;
Randall Spangler640fb512011-03-03 10:11:17 -0800407 key_block_valid = 0;
408 }
409 }
410
411 /* If we're not in developer mode, require the key block to be valid. */
412 if (kBootDev != boot_mode && !key_block_valid) {
413 VBDEBUG(("Key block is invalid.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700414 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700415 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700416
Randall Spangler640fb512011-03-03 10:11:17 -0800417 /* Get the key for preamble/data verification from the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700418 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700419 if (!data_key) {
420 VBDEBUG(("Data key bad.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700421 if (shpart)
422 shpart->check_result = VBSD_LKP_CHECK_DATA_KEY_PARSE;
Randall Spangler741d2b22010-08-20 16:37:12 -0700423 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700424 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700425
426 /* Verify the preamble, which follows the key block */
427 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700428 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700429 KBUF_SIZE - key_block->key_block_size,
430 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700431 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700432 if (shpart)
433 shpart->check_result = VBSD_LKP_CHECK_VERIFY_PREAMBLE;
Randall Spangler741d2b22010-08-20 16:37:12 -0700434 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700435 }
436
Randall Spangler640fb512011-03-03 10:11:17 -0800437 /* If the key block is valid and we're not in recovery mode, check for
438 * rollback of the kernel version. */
Randall Spangler66680282010-08-16 12:33:44 -0700439 combined_version = ((key_version << 16) |
440 (preamble->kernel_version & 0xFFFF));
Randall Spangler17c71262011-03-18 11:24:27 -0700441 if (shpart)
442 shpart->combined_version = (uint32_t)combined_version;
Randall Spangler640fb512011-03-03 10:11:17 -0800443 if (key_block_valid && kBootRecovery != boot_mode) {
444 if (combined_version < tpm_version) {
445 VBDEBUG(("Kernel version too low.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700446 if (shpart)
447 shpart->check_result = VBSD_LKP_CHECK_KERNEL_ROLLBACK;
Randall Spangler640fb512011-03-03 10:11:17 -0800448 /* If we're not in developer mode, kernel version must be valid. */
449 if (kBootDev != boot_mode)
450 goto bad_kernel;
451 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700452 }
453
Randall Spanglere2ec9842010-06-23 21:17:07 -0700454 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700455 if (shpart)
456 shpart->check_result = VBSD_LKP_CHECK_PREAMBLE_VALID;
Randall Spangler695cd162010-06-15 23:38:23 -0700457
Randall Spangler66680282010-08-16 12:33:44 -0700458 /* Check for lowest version from a valid header. */
Randall Spangler640fb512011-03-03 10:11:17 -0800459 if (key_block_valid && lowest_version > combined_version)
Randall Spangler66680282010-08-16 12:33:44 -0700460 lowest_version = combined_version;
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700461 else {
462 VBDEBUG(("Key block valid: %d\n", key_block_valid));
463 VBDEBUG(("Combined version: %" PRIu64 "\n", combined_version));
464 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700465
466 /* If we already have a good kernel, no need to read another
467 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700468 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700469 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700470 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700471
472 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700473 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
474 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700475 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700476 if (shpart)
477 shpart->check_result = VBSD_LKP_CHECK_BODY_ADDRESS;
Randall Spangler741d2b22010-08-20 16:37:12 -0700478 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700479 }
480
481 /* Verify kernel body starts at a multiple of the sector size. */
482 body_offset = key_block->key_block_size + preamble->preamble_size;
483 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700484 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700485 if (shpart)
486 shpart->check_result = VBSD_LKP_CHECK_BODY_OFFSET;
Randall Spangler741d2b22010-08-20 16:37:12 -0700487 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700488 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700489 body_offset_sectors = body_offset / blba;
490
491 /* Verify kernel body fits in the buffer */
492 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
493 if (body_sectors * blba > params->kernel_buffer_size) {
494 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700495 if (shpart)
496 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_MEM;
Randall Spangler741d2b22010-08-20 16:37:12 -0700497 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700498 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700499
500 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700501 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700502 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700503 if (shpart)
504 shpart->check_result = VBSD_LKP_CHECK_BODY_EXCEEDS_PART;
Randall Spangler741d2b22010-08-20 16:37:12 -0700505 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700506 }
507
508 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700509 VBPERFSTART("VB_RKD");
Randall Spangler1b1998d2011-07-01 16:12:47 -0700510 if (0 != VbExDiskRead(params->disk_handle,
511 part_start + body_offset_sectors,
512 body_sectors, params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700513 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700514 VBPERFEND("VB_RKD");
Randall Spangler17c71262011-03-18 11:24:27 -0700515 if (shpart)
516 shpart->check_result = VBSD_LKP_CHECK_READ_DATA;
Randall Spangler741d2b22010-08-20 16:37:12 -0700517 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700518 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700519 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700520
521 /* Verify kernel data */
522 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700523 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700524 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700525 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700526 if (shpart)
527 shpart->check_result = VBSD_LKP_CHECK_VERIFY_DATA;
Randall Spangler741d2b22010-08-20 16:37:12 -0700528 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700529 }
530
531 /* Done with the kernel signing key, so can free it now */
532 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700533 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700534
535 /* If we're still here, the kernel is valid. */
536 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler640fb512011-03-03 10:11:17 -0800537 VBDEBUG(("Partition is good.\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700538 if (shpart) {
539 shpart->check_result = VBSD_LKP_CHECK_KERNEL_GOOD;
540 if (key_block_valid)
541 shpart->flags |= VBSD_LKP_FLAG_KEY_BLOCK_VALID;
542 }
543
Randall Spangler640fb512011-03-03 10:11:17 -0800544 good_partition_key_block_valid = key_block_valid;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700545 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
546 * Adjust here, until cgptlib is fixed. */
547 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700548 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700549 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700550 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
551 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700552 params->bootloader_address = preamble->bootloader_address;
553 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700554
555 /* Update GPT to note this is the kernel we're trying */
556 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
557
Randall Spangler640fb512011-03-03 10:11:17 -0800558 /* If we're in recovery mode or we're about to boot a dev-signed kernel,
559 * there's no rollback protection, so we can stop at the first valid
560 * kernel. */
561 if (kBootRecovery == boot_mode || !key_block_valid) {
562 VBDEBUG(("In recovery mode or dev-signed kernel\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700563 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700564 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700565
Randall Spangler640fb512011-03-03 10:11:17 -0800566 /* Otherwise, we do care about the key index in the TPM. If the good
567 * partition's key version is the same as the tpm, then the TPM doesn't
568 * need updating; we can stop now. Otherwise, we'll check all the other
569 * headers to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700570 if (combined_version == tpm_version) {
571 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700572 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700573 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700574
575 /* Continue, so that we skip the error handling code below */
576 continue;
577
578 bad_kernel:
579 /* Handle errors parsing this kernel */
580 if (NULL != data_key)
581 RSAPublicKeyFree(data_key);
582
583 VBDEBUG(("Marking kernel as invalid.\n"));
584 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
585
586
Randall Spanglerd1836442010-06-10 09:59:04 -0700587 } /* while(GptNextKernelEntry) */
588 } while(0);
589
590 /* Free kernel buffer */
591 if (kbuf)
Randall Spanglere49e8af2011-07-08 13:03:32 -0700592 VbExFree(kbuf);
Randall Spanglerd1836442010-06-10 09:59:04 -0700593
594 /* Write and free GPT data */
Randall Spangler1b1998d2011-07-01 16:12:47 -0700595 WriteAndFreeGptData(params->disk_handle, &gpt);
Randall Spanglerd1836442010-06-10 09:59:04 -0700596
597 /* Handle finding a good partition */
598 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700599 VBDEBUG(("Good_partition >= 0\n"));
Randall Spangler17c71262011-03-18 11:24:27 -0700600 if (shcall)
601 shcall->check_result = VBSD_LKC_CHECK_GOOD_PARTITION;
Randall Spanglerd1836442010-06-10 09:59:04 -0700602
603 /* See if we need to update the TPM */
Randall Spangler700fc492011-04-17 10:48:10 -0700604 if ((kBootNormal == boot_mode) &&
605 !((1 == shared->firmware_index) && (shared->flags & VBSD_FWB_TRIED))) {
606 /* We only update the TPM in normal mode. We don't advance the
607 * TPM if we're trying a new firmware B, because that firmware
608 * may have a key change and roll forward the TPM too soon. */
609 VBDEBUG(("Checking if TPM kernel version needs advancing\n"));
Stefan Reinauer55db6a62011-03-15 16:23:41 -0700610
611 if ((lowest_version > tpm_version) &&
612 (lowest_version != LOWEST_TPM_VERSION)) {
Randall Spangler66680282010-08-16 12:33:44 -0700613 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700614 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700615 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler640fb512011-03-03 10:11:17 -0800616 if (status == TPM_E_MUST_REBOOT)
617 retval = LOAD_KERNEL_REBOOT;
618 else
619 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
620 goto LoadKernelExit;
Randall Spangler10788382010-06-23 15:35:31 -0700621 }
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700622 if (shared)
623 shared->kernel_version_tpm = (uint32_t)lowest_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700624 }
625 }
626
Randall Spangler63dffcb2010-08-05 15:13:14 -0700627 /* Lock the kernel versions */
628 status = RollbackKernelLock();
629 if (0 != status) {
630 VBDEBUG(("Error locking kernel versions.\n"));
631 /* Don't reboot to recovery mode if we're already there */
Randall Spangler640fb512011-03-03 10:11:17 -0800632 if (kBootRecovery != boot_mode) {
633 if (status == TPM_E_MUST_REBOOT)
634 retval = LOAD_KERNEL_REBOOT;
635 else
636 recovery = VBNV_RECOVERY_RW_TPM_ERROR;
637 goto LoadKernelExit;
638 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700639 }
640
641 /* Success! */
Randall Spangler640fb512011-03-03 10:11:17 -0800642 retval = LOAD_KERNEL_SUCCESS;
Randall Spangler02223552011-03-15 19:20:25 -0700643 } else {
Randall Spangler17c71262011-03-18 11:24:27 -0700644 if (shcall)
645 shcall->check_result = (found_partitions > 0
646 ? VBSD_LKC_CHECK_INVALID_PARTITIONS
647 : VBSD_LKC_CHECK_NO_PARTITIONS);
648
Randall Spangler02223552011-03-15 19:20:25 -0700649 /* TODO: differentiate between finding an invalid kernel
650 * (found_partitions>0) and not finding one at all. Right now we
651 * treat them the same, and return LOAD_KERNEL_INVALID for both. */
652 retval = LOAD_KERNEL_INVALID;
Randall Spanglerd1836442010-06-10 09:59:04 -0700653 }
654
Randall Spangler640fb512011-03-03 10:11:17 -0800655LoadKernelExit:
656
Randall Spangler640fb512011-03-03 10:11:17 -0800657 /* Store recovery request, if any, then tear down non-volatile storage */
658 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ?
659 recovery : VBNV_RECOVERY_NOT_REQUESTED);
660 VbNvTeardown(vnc);
661
Randall Spanglerad6824b2011-03-16 19:07:33 -0700662 if (shared) {
Randall Spangler17c71262011-03-18 11:24:27 -0700663 if (shcall)
664 shcall->return_code = (uint8_t)retval;
665
666 /* Save whether the good partition's key block was fully verified */
667 if (good_partition_key_block_valid)
668 shared->flags |= VBSD_KERNEL_KEY_VERIFIED;
669
Randall Spanglerad6824b2011-03-16 19:07:33 -0700670 /* Save timer values */
671 shared->timer_load_kernel_enter = timer_enter;
Randall Spanglere49e8af2011-07-08 13:03:32 -0700672 shared->timer_load_kernel_exit = VbExGetTimer();
Randall Spanglerad6824b2011-03-16 19:07:33 -0700673 /* Store how much shared data we used, if any */
Randall Spangler95c40312011-03-09 15:54:16 -0800674 params->shared_data_size = shared->data_used;
Randall Spanglerad6824b2011-03-16 19:07:33 -0700675 }
Randall Spangler95c40312011-03-09 15:54:16 -0800676
Randall Spangler640fb512011-03-03 10:11:17 -0800677 return retval;
Randall Spanglerd1836442010-06-10 09:59:04 -0700678}