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