blob: ea0fc295480e46c49921b98d52113d19386b7b38 [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"
13#include "load_kernel_fw.h"
14#include "rollback_index.h"
15#include "utility.h"
Randall Spangler83c88cf2010-06-11 16:14:18 -070016#include "vboot_common.h"
17
Randall Spanglerd1836442010-06-10 09:59:04 -070018
19#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) {
70 if (0 != BootDeviceWriteLBA(1, 1, gptdata->primary_header))
71 return 1;
72 }
73 Free(gptdata->primary_header);
74 }
75
76 if (gptdata->primary_entries) {
77 if (gptdata->modified & GPT_MODIFIED_ENTRIES1) {
78 if (0 != BootDeviceWriteLBA(2, entries_sectors,
79 gptdata->primary_entries))
80 return 1;
81 }
82 Free(gptdata->primary_entries);
83 }
84
85 if (gptdata->secondary_entries) {
86 if (gptdata->modified & GPT_MODIFIED_ENTRIES2) {
87 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - entries_sectors - 1,
88 entries_sectors, gptdata->secondary_entries))
89 return 1;
90 }
91 Free(gptdata->secondary_entries);
92 }
93
94 if (gptdata->secondary_header) {
95 if (gptdata->modified & GPT_MODIFIED_HEADER2) {
96 if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - 1, 1,
97 gptdata->secondary_header))
98 return 1;
99 }
100 Free(gptdata->secondary_header);
101 }
102
103 /* Success */
104 return 0;
105}
106
107
Randall Spanglerbd529f02010-06-16 12:51:26 -0700108int LoadKernel(LoadKernelParams* params) {
Randall Spanglerd1836442010-06-10 09:59:04 -0700109
110 VbPublicKey* kernel_subkey = (VbPublicKey*)params->header_sign_key_blob;
111
112 GptData gpt;
113 uint64_t part_start, part_size;
114 uint64_t blba = params->bytes_per_lba;
115 uint64_t kbuf_sectors = KBUF_SIZE / blba;
116 uint8_t* kbuf = NULL;
117 int found_partitions = 0;
118 int good_partition = -1;
119 uint16_t tpm_key_version = 0;
120 uint16_t tpm_kernel_version = 0;
121 uint64_t lowest_key_version = 0xFFFF;
122 uint64_t lowest_kernel_version = 0xFFFF;
123 int is_dev = ((BOOT_FLAG_DEVELOPER & params->boot_flags) &&
124 !(BOOT_FLAG_RECOVERY & params->boot_flags));
125 int is_normal = (!(BOOT_FLAG_DEVELOPER & params->boot_flags) &&
126 !(BOOT_FLAG_RECOVERY & params->boot_flags));
127
128 /* Clear output params in case we fail */
129 params->partition_number = 0;
130 params->bootloader_address = 0;
131 params->bootloader_size = 0;
132
133 if (is_normal) {
134 /* Read current kernel key index from TPM. Assumes TPM is already
135 * initialized. */
136 if (0 != GetStoredVersions(KERNEL_VERSIONS,
137 &tpm_key_version,
Randall Spangler695cd162010-06-15 23:38:23 -0700138 &tpm_kernel_version)) {
139 debug("Unable to get stored version from TPM\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700140 return LOAD_KERNEL_RECOVERY;
Randall Spangler695cd162010-06-15 23:38:23 -0700141 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700142 } else if (is_dev) {
143 /* In developer mode, we ignore the kernel subkey, and just use
144 * the SHA-512 hash to verify the key block. */
145 kernel_subkey = NULL;
146 }
147
148 do {
149 /* Read GPT data */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700150 gpt.sector_bytes = (uint32_t)blba;
Randall Spanglerd1836442010-06-10 09:59:04 -0700151 gpt.drive_sectors = params->ending_lba + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700152 if (0 != AllocAndReadGptData(&gpt)) {
153 debug("Unable to read GPT data\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700154 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700155 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700156
157 /* Initialize GPT library */
Randall Spangler695cd162010-06-15 23:38:23 -0700158 if (GPT_SUCCESS != GptInit(&gpt)) {
159 debug("Error parsing GPT\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700160 break;
Randall Spangler695cd162010-06-15 23:38:23 -0700161 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700162
Randall Spanglerd1836442010-06-10 09:59:04 -0700163 /* Allocate kernel header buffers */
164 kbuf = (uint8_t*)Malloc(KBUF_SIZE);
165 if (!kbuf)
166 break;
167
168 /* Loop over candidate kernel partitions */
169 while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) {
170 VbKeyBlockHeader* key_block;
171 VbKernelPreambleHeader* preamble;
172 RSAPublicKey* data_key;
173 uint64_t key_version;
174 uint64_t body_offset;
175
Randall Spangler695cd162010-06-15 23:38:23 -0700176 debug("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n",
177 part_start, part_size);
178
Randall Spanglerd1836442010-06-10 09:59:04 -0700179 /* Found at least one kernel partition. */
180 found_partitions++;
181
182 /* Read the first part of the kernel partition */
183 if (part_size < kbuf_sectors)
184 continue;
185 if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf))
186 continue;
187
188 /* Verify the key block */
189 key_block = (VbKeyBlockHeader*)kbuf;
Randall Spangler695cd162010-06-15 23:38:23 -0700190 if ((0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey))) {
191 debug("Verifying key block failed.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700192 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700193 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700194
195 /* Check the key block flags against the current boot mode */
196 if (!(key_block->key_block_flags &&
197 ((BOOT_FLAG_DEVELOPER & params->boot_flags) ?
Randall Spangler695cd162010-06-15 23:38:23 -0700198 KEY_BLOCK_FLAG_DEVELOPER_1 : KEY_BLOCK_FLAG_DEVELOPER_0))) {
199 debug("Developer flag mismatch.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700200 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700201 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700202 if (!(key_block->key_block_flags &&
203 ((BOOT_FLAG_RECOVERY & params->boot_flags) ?
Randall Spangler695cd162010-06-15 23:38:23 -0700204 KEY_BLOCK_FLAG_RECOVERY_1 : KEY_BLOCK_FLAG_RECOVERY_0))) {
205 debug("Recovery flag mismatch.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700206 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700207 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700208
209 /* Check for rollback of key version. Note this is implicitly
210 * skipped in recovery and developer modes because those set
211 * key_version=0 above. */
212 key_version = key_block->data_key.key_version;
Randall Spangler695cd162010-06-15 23:38:23 -0700213 if (key_version < tpm_key_version) {
214 debug("Key version too old.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700215 continue;
Randall Spangler695cd162010-06-15 23:38:23 -0700216 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700217
218 /* Get the key for preamble/data verification from the key block */
219 data_key = PublicKeyToRSA(&key_block->data_key);
220 if (!data_key)
221 continue;
222
223 /* Verify the preamble, which follows the key block */
224 preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size);
225 if ((0 != VerifyKernelPreamble2(preamble,
226 KBUF_SIZE - key_block->key_block_size,
227 data_key))) {
Randall Spangler695cd162010-06-15 23:38:23 -0700228 debug("Preamble verification failed.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700229 RSAPublicKeyFree(data_key);
230 continue;
231 }
232
233 /* Check for rollback of kernel version. Note this is implicitly
234 * skipped in recovery and developer modes because those set
235 * key_version=0 and kernel_version=0 above. */
236 if (key_version == tpm_key_version &&
237 preamble->kernel_version < tpm_kernel_version) {
Randall Spangler695cd162010-06-15 23:38:23 -0700238 debug("Kernel version too low.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700239 RSAPublicKeyFree(data_key);
240 continue;
241 }
242
Randall Spangler695cd162010-06-15 23:38:23 -0700243 debug("Kernel preamble is good.\n");
244
Randall Spanglerd1836442010-06-10 09:59:04 -0700245 /* Check for lowest key version from a valid header. */
246 if (lowest_key_version > key_version) {
247 lowest_key_version = key_version;
248 lowest_kernel_version = preamble->kernel_version;
249 }
250 else if (lowest_key_version == key_version &&
251 lowest_kernel_version > preamble->kernel_version) {
252 lowest_kernel_version = preamble->kernel_version;
253 }
254
255 /* If we already have a good kernel, no need to read another
256 * one; we only needed to look at the versions to check for
257 * rollback. */
258 if (-1 != good_partition)
259 continue;
260
261 /* Verify body load address matches what we expect */
Randall Spangler695cd162010-06-15 23:38:23 -0700262 if ((preamble->body_load_address != (size_t)params->kernel_buffer) &&
263 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) {
264 debug("Wrong body load address.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700265 RSAPublicKeyFree(data_key);
266 continue;
267 }
268
269 /* Verify kernel body starts at a multiple of the sector size. */
270 body_offset = key_block->key_block_size + preamble->preamble_size;
271 if (0 != body_offset % blba) {
Randall Spangler695cd162010-06-15 23:38:23 -0700272 debug("Kernel body not at multiple of sector size.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700273 RSAPublicKeyFree(data_key);
274 continue;
275 }
276
277 /* Verify kernel body fits in the partition */
278 if (body_offset + preamble->body_signature.data_size >
279 part_size * blba) {
Randall Spangler695cd162010-06-15 23:38:23 -0700280 debug("Kernel body doesn't fit in partition.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700281 RSAPublicKeyFree(data_key);
282 continue;
283 }
284
285 /* Read the kernel data */
286 if (0 != BootDeviceReadLBA(
287 part_start + (body_offset / blba),
288 (preamble->body_signature.data_size + blba - 1) / blba,
289 params->kernel_buffer)) {
Randall Spangler695cd162010-06-15 23:38:23 -0700290 debug("Unable to read kernel data.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700291 RSAPublicKeyFree(data_key);
292 continue;
293 }
294
295 /* Verify kernel data */
296 if (0 != VerifyData((const uint8_t*)params->kernel_buffer,
297 &preamble->body_signature, data_key)) {
Randall Spangler695cd162010-06-15 23:38:23 -0700298 debug("Kernel data verification failed.\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700299 RSAPublicKeyFree(data_key);
300 continue;
301 }
302
303 /* Done with the kernel signing key, so can free it now */
304 RSAPublicKeyFree(data_key);
305
306 /* If we're still here, the kernel is valid. */
307 /* Save the first good partition we find; that's the one we'll boot */
Randall Spangler695cd162010-06-15 23:38:23 -0700308 debug("Partiton is good.\n");
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700309 /* TODO: GPT partitions start at 1, but cgptlib starts them at 0.
310 * Adjust here, until cgptlib is fixed. */
311 good_partition = gpt.current_kernel + 1;
Randall Spangler695cd162010-06-15 23:38:23 -0700312 params->partition_number = gpt.current_kernel;
313 params->bootloader_address = preamble->bootloader_address;
314 params->bootloader_size = preamble->bootloader_size;
315 /* If we're in developer or recovery mode, there's no rollback
316 * protection, so we can stop at the first valid kernel. */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700317 if (!is_normal) {
318 debug("Boot_flags = !is_normal\n");
Randall Spangler695cd162010-06-15 23:38:23 -0700319 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700320 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700321
Randall Spangler695cd162010-06-15 23:38:23 -0700322 /* Otherwise, we're in normal boot mode, so we do care about the
323 * key index in the TPM. If the good partition's key version is
324 * the same as the tpm, then the TPM doesn't need updating; we
325 * can stop now. Otherwise, we'll check all the other headers
326 * to see if they contain a newer key. */
327 if (key_version == tpm_key_version &&
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700328 preamble->kernel_version == tpm_kernel_version) {
329 debug("Same key version\n");
Randall Spangler695cd162010-06-15 23:38:23 -0700330 break;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700331 }
Randall Spanglerd1836442010-06-10 09:59:04 -0700332 } /* while(GptNextKernelEntry) */
333 } while(0);
334
335 /* Free kernel buffer */
336 if (kbuf)
337 Free(kbuf);
338
339 /* Write and free GPT data */
340 WriteAndFreeGptData(&gpt);
341
342 /* Handle finding a good partition */
343 if (good_partition >= 0) {
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700344 debug("Good_partition >= 0\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700345
346 /* See if we need to update the TPM */
347 if (is_normal) {
348 /* We only update the TPM in normal boot mode. In developer
349 * mode, the kernel is self-signed by the developer, so we can't
350 * trust the key version and wouldn't want to roll the TPM
351 * forward. In recovery mode, the TPM stays PP-unlocked, so
352 * anything we write gets blown away by the firmware when we go
353 * back to normal mode. */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700354 debug("Boot_flags = is_normal\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700355 if ((lowest_key_version > tpm_key_version) ||
356 (lowest_key_version == tpm_key_version &&
357 lowest_kernel_version > tpm_kernel_version)) {
358 if (0 != WriteStoredVersions(KERNEL_VERSIONS,
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700359 (uint16_t)lowest_key_version,
360 (uint16_t)lowest_kernel_version))
Randall Spanglerd1836442010-06-10 09:59:04 -0700361 return LOAD_KERNEL_RECOVERY;
362 }
363 }
364
365 if (!(BOOT_FLAG_RECOVERY & params->boot_flags)) {
366 /* We can lock the TPM now, since we've decided which kernel we
367 * like. If we don't find a good kernel, we leave the TPM
368 * unlocked so we can try again on the next boot device. If no
369 * kernels are good, we'll reboot to recovery mode, so it's ok to
370 * leave the TPM unlocked in that case too.
371 *
372 * If we're already in recovery mode, we need to leave PP unlocked,
373 * so don't lock the kernel versions. */
Randall Spanglerbeb5bae2010-06-21 16:33:26 -0700374 debug("Lock kernel versions\n");
Randall Spanglerd1836442010-06-10 09:59:04 -0700375 if (0 != LockKernelVersionsByLockingPP())
376 return LOAD_KERNEL_RECOVERY;
377 }
378
379 /* Success! */
380 return LOAD_KERNEL_SUCCESS;
381 }
382
383 // Handle error cases
384 if (found_partitions)
385 return LOAD_KERNEL_INVALID;
386 else
387 return LOAD_KERNEL_NOT_FOUND;
388}