blob: 8ee45a7eabb3b6aa7bf728bd50005f1bfa3f75aa [file] [log] [blame]
Randall Spanglerd1836442010-06-10 09:59:04 -07001/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Functions for loading a kernel from disk.
6 * (Firmware portion)
7 */
8
9#include "vboot_kernel.h"
10
11#include "boot_device.h"
12#include "cgptlib.h"
Bill Richardson5deb67f2010-07-23 17:22:25 -070013#include "cgptlib_internal.h"
Randall Spanglerd1836442010-06-10 09:59:04 -070014#include "load_kernel_fw.h"
15#include "rollback_index.h"
16#include "utility.h"
Randall Spangler83c88cf2010-06-11 16:14:18 -070017#include "vboot_common.h"
18
Randall Spanglerd1836442010-06-10 09:59:04 -070019#define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */
20
Randall Spangler83c88cf2010-06-11 16:14:18 -070021
22/* Allocates and reads GPT data from the drive. The sector_bytes and
23 * drive_sectors fields should be filled on input. The primary and
24 * secondary header and entries are filled on output.
25 *
26 * Returns 0 if successful, 1 if error. */
27int AllocAndReadGptData(GptData* gptdata) {
28
29 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
30
31 /* No data to be written yet */
32 gptdata->modified = 0;
33
34 /* Allocate all buffers */
35 gptdata->primary_header = (uint8_t*)Malloc(gptdata->sector_bytes);
36 gptdata->secondary_header = (uint8_t*)Malloc(gptdata->sector_bytes);
37 gptdata->primary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE);
38 gptdata->secondary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE);
39
40 if (gptdata->primary_header == NULL || gptdata->secondary_header == NULL ||
41 gptdata->primary_entries == NULL || gptdata->secondary_entries == NULL)
42 return 1;
43
44 /* Read data from the drive, skipping the protective MBR */
45 if (0 != BootDeviceReadLBA(1, 1, gptdata->primary_header))
46 return 1;
47 if (0 != BootDeviceReadLBA(2, entries_sectors, gptdata->primary_entries))
48 return 1;
49 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - entries_sectors - 1,
50 entries_sectors, gptdata->secondary_entries))
51 return 1;
52 if (0 != BootDeviceReadLBA(gptdata->drive_sectors - 1,
53 1, gptdata->secondary_header))
54 return 1;
55
56 return 0;
57}
58
59
60/* Writes any changes for the GPT data back to the drive, then frees
61 * the buffers.
62 *
63 * Returns 0 if successful, 1 if error. */
64int WriteAndFreeGptData(GptData* gptdata) {
65
66 uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes;
67
68 if (gptdata->primary_header) {
69 if (gptdata->modified & GPT_MODIFIED_HEADER1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070070 VBDEBUG(("Updating GPT header 1\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070071 if (0 != BootDeviceWriteLBA(1, 1, gptdata->primary_header))
72 return 1;
73 }
74 Free(gptdata->primary_header);
75 }
76
77 if (gptdata->primary_entries) {
78 if (gptdata->modified & GPT_MODIFIED_ENTRIES1) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070079 VBDEBUG(("Updating GPT entries 1\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070080 if (0 != BootDeviceWriteLBA(2, entries_sectors,
81 gptdata->primary_entries))
82 return 1;
83 }
84 Free(gptdata->primary_entries);
85 }
86
87 if (gptdata->secondary_entries) {
88 if (gptdata->modified & GPT_MODIFIED_ENTRIES2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070089 VBDEBUG(("Updating GPT header 2\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -070090 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - entries_sectors - 1,
91 entries_sectors, gptdata->secondary_entries))
92 return 1;
93 }
94 Free(gptdata->secondary_entries);
95 }
96
97 if (gptdata->secondary_header) {
98 if (gptdata->modified & GPT_MODIFIED_HEADER2) {
Randall Spanglere2ec9842010-06-23 21:17:07 -070099 VBDEBUG(("Updating GPT entries 2\n"));
Randall Spangler83c88cf2010-06-11 16:14:18 -0700100 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - 1, 1,
101 gptdata->secondary_header))
102 return 1;
103 }
104 Free(gptdata->secondary_header);
105 }
106
107 /* Success */
108 return 0;
109}
110
vbendeb3ecaf772010-06-24 16:19:53 -0700111/* disable MSVC warning on const logical expression (as in } while(0);) */
112__pragma(warning(disable: 4127))
Randall Spangler83c88cf2010-06-11 16:14:18 -0700113
Randall Spanglerbd529f02010-06-16 12:51:26 -0700114int LoadKernel(LoadKernelParams* params) {
Bill Richardsone2729402010-07-22 12:23:47 -0700115 VbPublicKey* kernel_subkey;
Randall Spanglerd1836442010-06-10 09:59:04 -0700116 GptData gpt;
117 uint64_t part_start, part_size;
Bill Richardsone2729402010-07-22 12:23:47 -0700118 uint64_t blba;
119 uint64_t kbuf_sectors;
Randall Spanglerd1836442010-06-10 09:59:04 -0700120 uint8_t* kbuf = NULL;
121 int found_partitions = 0;
122 int good_partition = -1;
Randall Spangler66680282010-08-16 12:33:44 -0700123 uint32_t tpm_version = 0;
124 uint64_t lowest_version = 0xFFFFFFFF;
Bill Richardsone2729402010-07-22 12:23:47 -0700125 int is_dev;
126 int is_rec;
127 int is_normal;
Randall Spangler7a786b72010-07-08 13:29:42 -0700128 uint32_t status;
Randall Spanglerd1836442010-06-10 09:59:04 -0700129
Bill Richardsone2729402010-07-22 12:23:47 -0700130 /* Sanity Checks */
131 if (!params ||
Bill Richardsone2729402010-07-22 12:23:47 -0700132 !params->bytes_per_lba ||
133 !params->ending_lba ||
134 !params->kernel_buffer ||
135 !params->kernel_buffer_size) {
136 VBDEBUG(("LoadKernel() called with invalid params\n"));
137 return LOAD_KERNEL_INVALID;
138 }
139
140 /* Initialization */
141 kernel_subkey = (VbPublicKey*)params->header_sign_key_blob;
142 blba = params->bytes_per_lba;
143 kbuf_sectors = KBUF_SIZE / blba;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700144 if (0 == kbuf_sectors) {
145 VBDEBUG(("LoadKernel() called with sector size > KBUF_SIZE\n"));
146 return LOAD_KERNEL_INVALID;
147 }
148
Bill Richardsone2729402010-07-22 12:23:47 -0700149 is_dev = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0);
150 is_rec = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0);
151 is_normal = (!is_dev && !is_rec);
152
Randall Spanglerd1836442010-06-10 09:59:04 -0700153 /* Clear output params in case we fail */
154 params->partition_number = 0;
155 params->bootloader_address = 0;
156 params->bootloader_size = 0;
157
Randall Spangler63dffcb2010-08-05 15:13:14 -0700158 /* Let the TPM know if we're in recovery mode */
159 if (is_rec) {
160 if (0 != RollbackKernelRecovery(is_dev)) {
161 VBDEBUG(("Error setting up TPM for recovery kernel\n"));
162 /* Ignore return code, since we need to boot recovery mode to
163 * fix the TPM. */
Randall Spangler10788382010-06-23 15:35:31 -0700164 }
Randall Spangler81d09962010-06-23 10:15:38 -0700165 }
166
Randall Spanglerd1836442010-06-10 09:59:04 -0700167 if (is_normal) {
168 /* Read current kernel key index from TPM. Assumes TPM is already
169 * initialized. */
Randall Spangler66680282010-08-16 12:33:44 -0700170 status = RollbackKernelRead(&tpm_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700171 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700172 VBDEBUG(("Unable to get kernel versions from TPM\n"));
Randall Spangler7a786b72010-07-08 13:29:42 -0700173 return (status == TPM_E_MUST_REBOOT ?
174 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spangler695cd162010-06-15 23:38:23 -0700175 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700176 }
177
178 do {
179 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700180 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700181 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700182 if (0 != AllocAndReadGptData(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700183 VBDEBUG(("Unable to read GPT data\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700184 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700185 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700186
187 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700188 if (GPT_SUCCESS != GptInit(&gpt)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700189 VBDEBUG(("Error parsing GPT\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700190 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700191 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700192
Randall Spanglerd1836442010-06-10 09:59:04 -0700193 /* Allocate kernel header buffers */
194 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
195 if (!kbuf)
196 break;
197
198 /* Loop over candidate kernel partitions */
199 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
200 VbKeyBlockHeader* key_block;
201 VbKernelPreambleHeader* preamble;
Randall Spangler741d2b22010-08-20 16:37:12 -0700202 RSAPublicKey* data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700203 uint64_t key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700204 uint64_t combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700205 uint64_t body_offset;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700206 uint64_t body_offset_sectors;
207 uint64_t body_sectors;
Randall Spanglerd1836442010-06-10 09:59:04 -0700208
Randall Spanglere2ec9842010-06-23 21:17:07 -0700209 VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
210 part_start, part_size));
Randall Spangler695cd162010-06-15 23:38:23 -0700211
Randall Spanglerd1836442010-06-10 09:59:04 -0700212 /* Found at least one kernel partition. */
213 found_partitions++;
214
215 /* Read the first part of the kernel partition */
Randall Spangler77ae3892010-09-09 17:37:51 -0700216 if (part_size < kbuf_sectors) {
217 VBDEBUG(("Partition too small to hold kernel.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700218 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700219 }
220
221 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) {
222 VBDEBUG(("Unable to read start of partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700223 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700224 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700225
Randall Spangler138acfe2010-08-17 15:45:21 -0700226 /* Verify the key block. In developer mode, we ignore the key
227 * and use only the SHA-512 hash to verify the key block. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700228 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler138acfe2010-08-17 15:45:21 -0700229 if ((0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey,
230 is_dev && !is_rec))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700231 VBDEBUG(("Verifying key block failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700232 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700233 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700234
Randall Spanglerae029d92010-07-19 18:26:35 -0700235 /* Check the key block flags against the current boot mode in normal
236 * and recovery modes (not in developer mode booting from SSD). */
237 if (is_rec || is_normal) {
238 if (!(key_block->key_block_flags &
239 (is_dev ? KEY_BLOCK_FLAG_DEVELOPER_1 :
240 KEY_BLOCK_FLAG_DEVELOPER_0))) {
241 VBDEBUG(("Developer flag mismatch.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700242 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700243 }
244 if (!(key_block->key_block_flags &
245 (is_rec ? KEY_BLOCK_FLAG_RECOVERY_1 :
246 KEY_BLOCK_FLAG_RECOVERY_0))) {
247 VBDEBUG(("Recovery flag mismatch.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700248 goto bad_kernel;
Randall Spanglerae029d92010-07-19 18:26:35 -0700249 }
Randall Spangler695cd162010-06-15 23:38:23 -0700250 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700251
252 /* Check for rollback of key version. Note this is implicitly
253 * skipped in recovery and developer modes because those set
254 * key_version=0 above. */
255 key_version = key_block->data_key.key_version;
Randall Spangler66680282010-08-16 12:33:44 -0700256 if (key_version < (tpm_version >> 16)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700257 VBDEBUG(("Key version too old.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700258 goto bad_kernel;
Randall Spangler695cd162010-06-15 23:38:23 -0700259 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700260
261 /* Get the key for preamble/data verification from the key block */
262 data_key = PublicKeyToRSA(&key_block->data_key);
Randall Spangler77ae3892010-09-09 17:37:51 -0700263 if (!data_key) {
264 VBDEBUG(("Data key bad.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700265 goto bad_kernel;
Randall Spangler77ae3892010-09-09 17:37:51 -0700266 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700267
268 /* Verify the preamble, which follows the key block */
269 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
Randall Spangler87c13d82010-07-19 10:35:40 -0700270 if ((0 != VerifyKernelPreamble(preamble,
Randall Spanglerd1836442010-06-10 09:59:04 -0700271 KBUF_SIZE - key_block->key_block_size,
272 data_key))) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700273 VBDEBUG(("Preamble verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700274 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700275 }
276
277 /* Check for rollback of kernel version. Note this is implicitly
Randall Spangler66680282010-08-16 12:33:44 -0700278 * skipped in recovery and developer modes because rollback_index
279 * sets those to 0 in those modes. */
280 combined_version = ((key_version << 16) |
281 (preamble->kernel_version & 0xFFFF));
282 if (combined_version < tpm_version) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700283 VBDEBUG(("Kernel version too low.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700284 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700285 }
286
Randall Spanglere2ec9842010-06-23 21:17:07 -0700287 VBDEBUG(("Kernel preamble is good.\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700288
Randall Spangler66680282010-08-16 12:33:44 -0700289 /* Check for lowest version from a valid header. */
290 if (lowest_version > combined_version)
291 lowest_version = combined_version;
Randall Spanglerd1836442010-06-10 09:59:04 -0700292
293 /* If we already have a good kernel, no need to read another
294 * one; we only needed to look at the versions to check for
Randall Spangler77ae3892010-09-09 17:37:51 -0700295 * rollback. So skip to the next kernel preamble. */
Randall Spanglerd1836442010-06-10 09:59:04 -0700296 if (-1 != good_partition)
Randall Spangler77ae3892010-09-09 17:37:51 -0700297 continue;
Randall Spanglerd1836442010-06-10 09:59:04 -0700298
299 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700300 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
301 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700302 VBDEBUG(("Wrong body load address.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700303 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700304 }
305
306 /* Verify kernel body starts at a multiple of the sector size. */
307 body_offset = key_block->key_block_size + preamble->preamble_size;
308 if (0 != body_offset % blba) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700309 VBDEBUG(("Kernel body not at multiple of sector size.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700310 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700311 }
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700312 body_offset_sectors = body_offset / blba;
313
314 /* Verify kernel body fits in the buffer */
315 body_sectors = (preamble->body_signature.data_size + blba - 1) / blba;
316 if (body_sectors * blba > params->kernel_buffer_size) {
317 VBDEBUG(("Kernel body doesn't fit in memory.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700318 goto bad_kernel;
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700319 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700320
321 /* Verify kernel body fits in the partition */
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700322 if (body_offset_sectors + body_sectors > part_size) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700323 VBDEBUG(("Kernel body doesn't fit in partition.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700324 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700325 }
326
327 /* Read the kernel data */
Randall Spangler6078ca32010-10-18 15:49:28 -0700328 VBPERFSTART("VB_RKD");
Randall Spangler4bb5e4b2010-08-19 09:05:22 -0700329 if (0 != BootDeviceReadLBA(part_start + body_offset_sectors,
330 body_sectors,
331 params->kernel_buffer)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700332 VBDEBUG(("Unable to read kernel data.\n"));
Randall Spangler6078ca32010-10-18 15:49:28 -0700333 VBPERFEND("VB_RKD");
Randall Spangler741d2b22010-08-20 16:37:12 -0700334 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700335 }
Randall Spangler6078ca32010-10-18 15:49:28 -0700336 VBPERFEND("VB_RKD");
Randall Spanglerd1836442010-06-10 09:59:04 -0700337
338 /* Verify kernel data */
339 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
Randall Spangler87c13d82010-07-19 10:35:40 -0700340 params->kernel_buffer_size,
Randall Spanglerd1836442010-06-10 09:59:04 -0700341 &preamble->body_signature, data_key)) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700342 VBDEBUG(("Kernel data verification failed.\n"));
Randall Spangler741d2b22010-08-20 16:37:12 -0700343 goto bad_kernel;
Randall Spanglerd1836442010-06-10 09:59:04 -0700344 }
345
346 /* Done with the kernel signing key, so can free it now */
347 RSAPublicKeyFree(data_key);
Randall Spangler741d2b22010-08-20 16:37:12 -0700348 data_key = NULL;
Randall Spanglerd1836442010-06-10 09:59:04 -0700349
350 /* If we're still here, the kernel is valid. */
351 /* Save the first good partition we find; that's the one we'll boot */
Randall Spanglere2ec9842010-06-23 21:17:07 -0700352 VBDEBUG(("Partiton is good.\n"));
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700353 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
354 * Adjust here, until cgptlib is fixed. */
355 good_partition = gpt.current_kernel + 1;
Randall Spanglerb9d60a52010-06-23 12:43:01 -0700356 params->partition_number = gpt.current_kernel + 1;
Bill Richardson5deb67f2010-07-23 17:22:25 -0700357 GetCurrentKernelUniqueGuid(&gpt, &params->partition_guid);
Randall Spangler63dffcb2010-08-05 15:13:14 -0700358 /* TODO: GetCurrentKernelUniqueGuid() should take a destination size, or
359 * the dest should be a struct, so we know it's big enough. */
Randall Spangler695cd162010-06-15 23:38:23 -0700360 params->bootloader_address = preamble->bootloader_address;
361 params->bootloader_size = preamble->bootloader_size;
Randall Spangler741d2b22010-08-20 16:37:12 -0700362
363 /* Update GPT to note this is the kernel we're trying */
364 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_TRY);
365
Randall Spangler695cd162010-06-15 23:38:23 -0700366 /* If we're in developer or recovery mode, there's no rollback
367 * protection, so we can stop at the first valid kernel. */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700368 if (!is_normal) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700369 VBDEBUG(("Boot_flags = !is_normal\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700370 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700371 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700372
Randall Spangler695cd162010-06-15 23:38:23 -0700373 /* Otherwise, we're in normal boot mode, so we do care about the
374 * key index in the TPM. If the good partition's key version is
375 * the same as the tpm, then the TPM doesn't need updating; we
376 * can stop now. Otherwise, we'll check all the other headers
377 * to see if they contain a newer key. */
Randall Spangler66680282010-08-16 12:33:44 -0700378 if (combined_version == tpm_version) {
379 VBDEBUG(("Same kernel version\n"));
Randall Spangler695cd162010-06-15 23:38:23 -0700380 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700381 }
Randall Spangler741d2b22010-08-20 16:37:12 -0700382
383 /* Continue, so that we skip the error handling code below */
384 continue;
385
386 bad_kernel:
387 /* Handle errors parsing this kernel */
388 if (NULL != data_key)
389 RSAPublicKeyFree(data_key);
390
391 VBDEBUG(("Marking kernel as invalid.\n"));
392 GptUpdateKernelEntry(&gpt, GPT_UPDATE_ENTRY_BAD);
393
394
Randall Spanglerd1836442010-06-10 09:59:04 -0700395 } /* while(GptNextKernelEntry) */
396 } while(0);
397
398 /* Free kernel buffer */
399 if (kbuf)
400 Free(kbuf);
401
402 /* Write and free GPT data */
403 WriteAndFreeGptData(&gpt);
404
405 /* Handle finding a good partition */
406 if (good_partition >= 0) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700407 VBDEBUG(("Good_partition >= 0\n"));
Randall Spanglerd1836442010-06-10 09:59:04 -0700408
409 /* See if we need to update the TPM */
410 if (is_normal) {
411 /* We only update the TPM in normal boot mode. In developer
412 * mode, the kernel is self-signed by the developer, so we can't
413 * trust the key version and wouldn't want to roll the TPM
414 * forward. In recovery mode, the TPM stays PP-unlocked, so
415 * anything we write gets blown away by the firmware when we go
416 * back to normal mode. */
Randall Spanglere2ec9842010-06-23 21:17:07 -0700417 VBDEBUG(("Boot_flags = is_normal\n"));
Randall Spangler66680282010-08-16 12:33:44 -0700418 if (lowest_version > tpm_version) {
419 status = RollbackKernelWrite((uint32_t)lowest_version);
Randall Spangler7a786b72010-07-08 13:29:42 -0700420 if (0 != status) {
Randall Spanglere2ec9842010-06-23 21:17:07 -0700421 VBDEBUG(("Error writing kernel versions to TPM.\n"));
Randall Spangler63dffcb2010-08-05 15:13:14 -0700422 return (status == TPM_E_MUST_REBOOT ?
423 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spangler10788382010-06-23 15:35:31 -0700424 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700425 }
426 }
427
Randall Spangler63dffcb2010-08-05 15:13:14 -0700428 /* Lock the kernel versions */
429 status = RollbackKernelLock();
430 if (0 != status) {
431 VBDEBUG(("Error locking kernel versions.\n"));
432 /* Don't reboot to recovery mode if we're already there */
433 if (!is_rec)
434 return (status == TPM_E_MUST_REBOOT ?
435 LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY);
Randall Spanglerd1836442010-06-10 09:59:04 -0700436 }
437
438 /* Success! */
439 return LOAD_KERNEL_SUCCESS;
440 }
441
Bill Richardson60563ee2010-08-30 15:15:38 -0700442 /* The BIOS may attempt to display different screens depending on whether
443 * we find an invalid kernel partition (return LOAD_KERNEL_INVALID) or not.
444 * But the flow is changing, so for now treating both cases as invalid gives
445 * slightly less confusing user feedback. Sigh.
446 */
447 return LOAD_KERNEL_INVALID;
Randall Spanglerd1836442010-06-10 09:59:04 -0700448}