| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 1 | /* 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 Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 16 | #include "vboot_common.h" | 
|  | 17 |  | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #define KBUF_SIZE 65536  /* Bytes to read at start of kernel partition */ | 
|  | 20 |  | 
| Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 21 |  | 
|  | 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. */ | 
|  | 27 | int 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. */ | 
|  | 64 | int 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 Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 70 | VBDEBUG(("Updating GPT header 1\n")); | 
| Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 71 | 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 Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 79 | VBDEBUG(("Updating GPT entries 1\n")); | 
| Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 80 | 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 Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 89 | VBDEBUG(("Updating GPT header 2\n")); | 
| Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 90 | 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 Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 99 | VBDEBUG(("Updating GPT entries 2\n")); | 
| Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 100 | 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 |  | 
| vbendeb | 3ecaf77 | 2010-06-24 16:19:53 -0700 | [diff] [blame] | 111 | /* disable MSVC warning on const logical expression (as in } while(0);) */ | 
|  | 112 | __pragma(warning(disable: 4127)) | 
| Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 113 |  | 
| Randall Spangler | bd529f0 | 2010-06-16 12:51:26 -0700 | [diff] [blame] | 114 | int LoadKernel(LoadKernelParams* params) { | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | VbPublicKey* kernel_subkey = (VbPublicKey*)params->header_sign_key_blob; | 
|  | 117 |  | 
|  | 118 | GptData gpt; | 
|  | 119 | uint64_t part_start, part_size; | 
|  | 120 | uint64_t blba = params->bytes_per_lba; | 
|  | 121 | uint64_t kbuf_sectors = KBUF_SIZE / blba; | 
|  | 122 | uint8_t* kbuf = NULL; | 
|  | 123 | int found_partitions = 0; | 
|  | 124 | int good_partition = -1; | 
|  | 125 | uint16_t tpm_key_version = 0; | 
|  | 126 | uint16_t tpm_kernel_version = 0; | 
|  | 127 | uint64_t lowest_key_version = 0xFFFF; | 
|  | 128 | uint64_t lowest_kernel_version = 0xFFFF; | 
| Randall Spangler | c4a011a | 2010-06-29 19:08:43 -0700 | [diff] [blame] | 129 | int is_dev = (BOOT_FLAG_DEVELOPER & params->boot_flags ? 1 : 0); | 
|  | 130 | int is_rec = (BOOT_FLAG_RECOVERY & params->boot_flags ? 1 : 0); | 
| Randall Spangler | d6aad3a | 2010-06-24 14:01:34 -0700 | [diff] [blame] | 131 | int is_normal = (!is_dev && !is_rec); | 
| Randall Spangler | 7a786b7 | 2010-07-08 13:29:42 -0700 | [diff] [blame] | 132 | uint32_t status; | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | /* Clear output params in case we fail */ | 
|  | 135 | params->partition_number = 0; | 
|  | 136 | params->bootloader_address = 0; | 
|  | 137 | params->bootloader_size = 0; | 
|  | 138 |  | 
| Randall Spangler | 3e1081f | 2010-07-19 10:04:21 -0700 | [diff] [blame] | 139 | if (!is_dev) { | 
|  | 140 | /* TODO: should use the TPM all the time; for now, only use when | 
|  | 141 | * not in developer mode. */ | 
|  | 142 | /* Let the TPM know if we're in recovery mode */ | 
|  | 143 | if (is_rec) { | 
|  | 144 | if (0 != RollbackKernelRecovery(is_dev ? 1 : 0)) { | 
|  | 145 | VBDEBUG(("Error setting up TPM for recovery kernel\n")); | 
|  | 146 | /* Ignore return code, since we need to boot recovery mode to | 
|  | 147 | * fix the TPM. */ | 
|  | 148 | } | 
| Randall Spangler | 1078838 | 2010-06-23 15:35:31 -0700 | [diff] [blame] | 149 | } | 
| Randall Spangler | 81d0996 | 2010-06-23 10:15:38 -0700 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 152 | if (is_normal) { | 
|  | 153 | /* Read current kernel key index from TPM.  Assumes TPM is already | 
|  | 154 | * initialized. */ | 
| Randall Spangler | 7a786b7 | 2010-07-08 13:29:42 -0700 | [diff] [blame] | 155 | status = RollbackKernelRead(&tpm_key_version, &tpm_kernel_version); | 
|  | 156 | if (0 != status) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 157 | VBDEBUG(("Unable to get kernel versions from TPM\n")); | 
| Randall Spangler | 7a786b7 | 2010-07-08 13:29:42 -0700 | [diff] [blame] | 158 | return (status == TPM_E_MUST_REBOOT ? | 
|  | 159 | LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY); | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 160 | } | 
| Randall Spangler | d6aad3a | 2010-06-24 14:01:34 -0700 | [diff] [blame] | 161 | } else if (is_dev && !is_rec) { | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 162 | /* In developer mode, we ignore the kernel subkey, and just use | 
|  | 163 | * the SHA-512 hash to verify the key block. */ | 
|  | 164 | kernel_subkey = NULL; | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | do { | 
|  | 168 | /* Read GPT data */ | 
| Randall Spangler | beb5bae | 2010-06-21 16:33:26 -0700 | [diff] [blame] | 169 | gpt.sector_bytes = (uint32_t)blba; | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 170 | gpt.drive_sectors = params->ending_lba + 1; | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 171 | if (0 != AllocAndReadGptData(&gpt)) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 172 | VBDEBUG(("Unable to read GPT data\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 173 | break; | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 174 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 175 |  | 
|  | 176 | /* Initialize GPT library */ | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 177 | if (GPT_SUCCESS != GptInit(&gpt)) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 178 | VBDEBUG(("Error parsing GPT\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 179 | break; | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 180 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 181 |  | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 182 | /* Allocate kernel header buffers */ | 
|  | 183 | kbuf = (uint8_t*)Malloc(KBUF_SIZE); | 
|  | 184 | if (!kbuf) | 
|  | 185 | break; | 
|  | 186 |  | 
|  | 187 | /* Loop over candidate kernel partitions */ | 
|  | 188 | while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) { | 
|  | 189 | VbKeyBlockHeader* key_block; | 
|  | 190 | VbKernelPreambleHeader* preamble; | 
|  | 191 | RSAPublicKey* data_key; | 
|  | 192 | uint64_t key_version; | 
|  | 193 | uint64_t body_offset; | 
|  | 194 |  | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 195 | VBDEBUG(("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n", | 
|  | 196 | part_start, part_size)); | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 197 |  | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 198 | /* Found at least one kernel partition. */ | 
|  | 199 | found_partitions++; | 
|  | 200 |  | 
|  | 201 | /* Read the first part of the kernel partition  */ | 
|  | 202 | if (part_size < kbuf_sectors) | 
|  | 203 | continue; | 
|  | 204 | if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) | 
|  | 205 | continue; | 
|  | 206 |  | 
|  | 207 | /* Verify the key block */ | 
|  | 208 | key_block = (VbKeyBlockHeader*)kbuf; | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 209 | if ((0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey))) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 210 | VBDEBUG(("Verifying key block failed.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 211 | continue; | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 212 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 213 |  | 
| Randall Spangler | ae029d9 | 2010-07-19 18:26:35 -0700 | [diff] [blame^] | 214 | /* Check the key block flags against the current boot mode in normal | 
|  | 215 | * and recovery modes (not in developer mode booting from SSD). */ | 
|  | 216 | if (is_rec || is_normal) { | 
|  | 217 | if (!(key_block->key_block_flags & | 
|  | 218 | (is_dev ? KEY_BLOCK_FLAG_DEVELOPER_1 : | 
|  | 219 | KEY_BLOCK_FLAG_DEVELOPER_0))) { | 
|  | 220 | VBDEBUG(("Developer flag mismatch.\n")); | 
|  | 221 | continue; | 
|  | 222 | } | 
|  | 223 | if (!(key_block->key_block_flags & | 
|  | 224 | (is_rec ? KEY_BLOCK_FLAG_RECOVERY_1 : | 
|  | 225 | KEY_BLOCK_FLAG_RECOVERY_0))) { | 
|  | 226 | VBDEBUG(("Recovery flag mismatch.\n")); | 
|  | 227 | continue; | 
|  | 228 | } | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 229 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | /* Check for rollback of key version.  Note this is implicitly | 
|  | 232 | * skipped in recovery and developer modes because those set | 
|  | 233 | * key_version=0 above. */ | 
|  | 234 | key_version = key_block->data_key.key_version; | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 235 | if (key_version < tpm_key_version) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 236 | VBDEBUG(("Key version too old.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 237 | continue; | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 238 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 239 |  | 
|  | 240 | /* Get the key for preamble/data verification from the key block */ | 
|  | 241 | data_key = PublicKeyToRSA(&key_block->data_key); | 
|  | 242 | if (!data_key) | 
|  | 243 | continue; | 
|  | 244 |  | 
|  | 245 | /* Verify the preamble, which follows the key block */ | 
|  | 246 | preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size); | 
| Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 247 | if ((0 != VerifyKernelPreamble(preamble, | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 248 | KBUF_SIZE - key_block->key_block_size, | 
|  | 249 | data_key))) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 250 | VBDEBUG(("Preamble verification failed.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 251 | RSAPublicKeyFree(data_key); | 
|  | 252 | continue; | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | /* Check for rollback of kernel version.  Note this is implicitly | 
|  | 256 | * skipped in recovery and developer modes because those set | 
|  | 257 | * key_version=0 and kernel_version=0 above. */ | 
|  | 258 | if (key_version == tpm_key_version && | 
|  | 259 | preamble->kernel_version < tpm_kernel_version) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 260 | VBDEBUG(("Kernel version too low.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 261 | RSAPublicKeyFree(data_key); | 
|  | 262 | continue; | 
|  | 263 | } | 
|  | 264 |  | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 265 | VBDEBUG(("Kernel preamble is good.\n")); | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 266 |  | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 267 | /* Check for lowest key version from a valid header. */ | 
|  | 268 | if (lowest_key_version > key_version) { | 
|  | 269 | lowest_key_version = key_version; | 
|  | 270 | lowest_kernel_version = preamble->kernel_version; | 
|  | 271 | } | 
|  | 272 | else if (lowest_key_version == key_version && | 
|  | 273 | lowest_kernel_version > preamble->kernel_version) { | 
|  | 274 | lowest_kernel_version = preamble->kernel_version; | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | /* If we already have a good kernel, no need to read another | 
|  | 278 | * one; we only needed to look at the versions to check for | 
|  | 279 | * rollback. */ | 
|  | 280 | if (-1 != good_partition) | 
|  | 281 | continue; | 
|  | 282 |  | 
|  | 283 | /* Verify body load address matches what we expect */ | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 284 | if ((preamble->body_load_address != (size_t)params->kernel_buffer) && | 
|  | 285 | !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 286 | VBDEBUG(("Wrong body load address.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 287 | RSAPublicKeyFree(data_key); | 
|  | 288 | continue; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | /* Verify kernel body starts at a multiple of the sector size. */ | 
|  | 292 | body_offset = key_block->key_block_size + preamble->preamble_size; | 
|  | 293 | if (0 != body_offset % blba) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 294 | VBDEBUG(("Kernel body not at multiple of sector size.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 295 | RSAPublicKeyFree(data_key); | 
|  | 296 | continue; | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | /* Verify kernel body fits in the partition */ | 
|  | 300 | if (body_offset + preamble->body_signature.data_size > | 
|  | 301 | part_size * blba) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 302 | VBDEBUG(("Kernel body doesn't fit in partition.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 303 | RSAPublicKeyFree(data_key); | 
|  | 304 | continue; | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | /* Read the kernel data */ | 
|  | 308 | if (0 != BootDeviceReadLBA( | 
|  | 309 | part_start + (body_offset / blba), | 
|  | 310 | (preamble->body_signature.data_size + blba - 1) / blba, | 
|  | 311 | params->kernel_buffer)) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 312 | VBDEBUG(("Unable to read kernel data.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 313 | RSAPublicKeyFree(data_key); | 
|  | 314 | continue; | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | /* Verify kernel data */ | 
|  | 318 | if (0 != VerifyData((const uint8_t*)params->kernel_buffer, | 
| Randall Spangler | 87c13d8 | 2010-07-19 10:35:40 -0700 | [diff] [blame] | 319 | params->kernel_buffer_size, | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 320 | &preamble->body_signature, data_key)) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 321 | VBDEBUG(("Kernel data verification failed.\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 322 | RSAPublicKeyFree(data_key); | 
|  | 323 | continue; | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | /* Done with the kernel signing key, so can free it now */ | 
|  | 327 | RSAPublicKeyFree(data_key); | 
|  | 328 |  | 
|  | 329 | /* If we're still here, the kernel is valid. */ | 
|  | 330 | /* Save the first good partition we find; that's the one we'll boot */ | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 331 | VBDEBUG(("Partiton is good.\n")); | 
| Randall Spangler | beb5bae | 2010-06-21 16:33:26 -0700 | [diff] [blame] | 332 | /* TODO: GPT partitions start at 1, but cgptlib starts them at 0. | 
|  | 333 | * Adjust here, until cgptlib is fixed. */ | 
|  | 334 | good_partition = gpt.current_kernel + 1; | 
| Randall Spangler | b9d60a5 | 2010-06-23 12:43:01 -0700 | [diff] [blame] | 335 | params->partition_number = gpt.current_kernel + 1; | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 336 | params->bootloader_address = preamble->bootloader_address; | 
|  | 337 | params->bootloader_size = preamble->bootloader_size; | 
|  | 338 | /* If we're in developer or recovery mode, there's no rollback | 
|  | 339 | * protection, so we can stop at the first valid kernel. */ | 
| Randall Spangler | beb5bae | 2010-06-21 16:33:26 -0700 | [diff] [blame] | 340 | if (!is_normal) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 341 | VBDEBUG(("Boot_flags = !is_normal\n")); | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 342 | break; | 
| Randall Spangler | beb5bae | 2010-06-21 16:33:26 -0700 | [diff] [blame] | 343 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 344 |  | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 345 | /* Otherwise, we're in normal boot mode, so we do care about the | 
|  | 346 | * key index in the TPM.  If the good partition's key version is | 
|  | 347 | * the same as the tpm, then the TPM doesn't need updating; we | 
|  | 348 | * can stop now.  Otherwise, we'll check all the other headers | 
|  | 349 | * to see if they contain a newer key. */ | 
|  | 350 | if (key_version == tpm_key_version && | 
| Randall Spangler | beb5bae | 2010-06-21 16:33:26 -0700 | [diff] [blame] | 351 | preamble->kernel_version == tpm_kernel_version) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 352 | VBDEBUG(("Same key version\n")); | 
| Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame] | 353 | break; | 
| Randall Spangler | beb5bae | 2010-06-21 16:33:26 -0700 | [diff] [blame] | 354 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 355 | } /* while(GptNextKernelEntry) */ | 
|  | 356 | } while(0); | 
|  | 357 |  | 
|  | 358 | /* Free kernel buffer */ | 
|  | 359 | if (kbuf) | 
|  | 360 | Free(kbuf); | 
|  | 361 |  | 
|  | 362 | /* Write and free GPT data */ | 
|  | 363 | WriteAndFreeGptData(&gpt); | 
|  | 364 |  | 
|  | 365 | /* Handle finding a good partition */ | 
|  | 366 | if (good_partition >= 0) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 367 | VBDEBUG(("Good_partition >= 0\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 368 |  | 
|  | 369 | /* See if we need to update the TPM */ | 
|  | 370 | if (is_normal) { | 
|  | 371 | /* We only update the TPM in normal boot mode.  In developer | 
|  | 372 | * mode, the kernel is self-signed by the developer, so we can't | 
|  | 373 | * trust the key version and wouldn't want to roll the TPM | 
|  | 374 | * forward.  In recovery mode, the TPM stays PP-unlocked, so | 
|  | 375 | * anything we write gets blown away by the firmware when we go | 
|  | 376 | * back to normal mode. */ | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 377 | VBDEBUG(("Boot_flags = is_normal\n")); | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 378 | if ((lowest_key_version > tpm_key_version) || | 
|  | 379 | (lowest_key_version == tpm_key_version && | 
|  | 380 | lowest_kernel_version > tpm_kernel_version)) { | 
| Randall Spangler | 7a786b7 | 2010-07-08 13:29:42 -0700 | [diff] [blame] | 381 |  | 
|  | 382 | status = RollbackKernelWrite((uint16_t)lowest_key_version, | 
|  | 383 | (uint16_t)lowest_kernel_version); | 
|  | 384 | if (0 != status) { | 
| Randall Spangler | e2ec984 | 2010-06-23 21:17:07 -0700 | [diff] [blame] | 385 | VBDEBUG(("Error writing kernel versions to TPM.\n")); | 
| Randall Spangler | 7a786b7 | 2010-07-08 13:29:42 -0700 | [diff] [blame] | 386 | return (status == TPM_E_MUST_REBOOT ? | 
|  | 387 | LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY); | 
| Randall Spangler | 1078838 | 2010-06-23 15:35:31 -0700 | [diff] [blame] | 388 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 389 | } | 
|  | 390 | } | 
|  | 391 |  | 
| Randall Spangler | 3e1081f | 2010-07-19 10:04:21 -0700 | [diff] [blame] | 392 | if (!is_dev) { | 
|  | 393 | /* TODO: should use the TPM all the time; for now, only use when | 
|  | 394 | * not in developer mode. */ | 
|  | 395 | /* Lock the kernel versions */ | 
|  | 396 | status = RollbackKernelLock(); | 
|  | 397 | if (0 != status) { | 
|  | 398 | VBDEBUG(("Error locking kernel versions.\n")); | 
|  | 399 | /* Don't reboot to recovery mode if we're already there */ | 
|  | 400 | if (!is_rec) | 
|  | 401 | return (status == TPM_E_MUST_REBOOT ? | 
|  | 402 | LOAD_KERNEL_REBOOT : LOAD_KERNEL_RECOVERY); | 
|  | 403 | } | 
| Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 404 | } | 
|  | 405 |  | 
|  | 406 | /* Success! */ | 
|  | 407 | return LOAD_KERNEL_SUCCESS; | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 | // Handle error cases | 
|  | 411 | if (found_partitions) | 
|  | 412 | return LOAD_KERNEL_INVALID; | 
|  | 413 | else | 
|  | 414 | return LOAD_KERNEL_NOT_FOUND; | 
|  | 415 | } |