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 | |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 11 | #include <inttypes.h> /* For PRIu64 */ |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 12 | #include "boot_device.h" |
| 13 | #include "cgptlib.h" |
| 14 | #include "load_kernel_fw.h" |
| 15 | #include "rollback_index.h" |
| 16 | #include "utility.h" |
Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 17 | #include "vboot_common.h" |
| 18 | |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 19 | |
| 20 | #define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */ |
| 21 | |
Randall Spangler | 83c88cf | 2010-06-11 16:14:18 -0700 | [diff] [blame] | 22 | |
| 23 | /* Allocates and reads GPT data from the drive. The sector_bytes and |
| 24 | * drive_sectors fields should be filled on input. The primary and |
| 25 | * secondary header and entries are filled on output. |
| 26 | * |
| 27 | * Returns 0 if successful, 1 if error. */ |
| 28 | int AllocAndReadGptData(GptData* gptdata) { |
| 29 | |
| 30 | uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes; |
| 31 | |
| 32 | /* No data to be written yet */ |
| 33 | gptdata->modified = 0; |
| 34 | |
| 35 | /* Allocate all buffers */ |
| 36 | gptdata->primary_header = (uint8_t*)Malloc(gptdata->sector_bytes); |
| 37 | gptdata->secondary_header = (uint8_t*)Malloc(gptdata->sector_bytes); |
| 38 | gptdata->primary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE); |
| 39 | gptdata->secondary_entries = (uint8_t*)Malloc(TOTAL_ENTRIES_SIZE); |
| 40 | |
| 41 | if (gptdata->primary_header == NULL || gptdata->secondary_header == NULL || |
| 42 | gptdata->primary_entries == NULL || gptdata->secondary_entries == NULL) |
| 43 | return 1; |
| 44 | |
| 45 | /* Read data from the drive, skipping the protective MBR */ |
| 46 | if (0 != BootDeviceReadLBA(1, 1, gptdata->primary_header)) |
| 47 | return 1; |
| 48 | if (0 != BootDeviceReadLBA(2, entries_sectors, gptdata->primary_entries)) |
| 49 | return 1; |
| 50 | if (0 != BootDeviceReadLBA(gptdata->drive_sectors - entries_sectors - 1, |
| 51 | entries_sectors, gptdata->secondary_entries)) |
| 52 | return 1; |
| 53 | if (0 != BootDeviceReadLBA(gptdata->drive_sectors - 1, |
| 54 | 1, gptdata->secondary_header)) |
| 55 | return 1; |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /* Writes any changes for the GPT data back to the drive, then frees |
| 62 | * the buffers. |
| 63 | * |
| 64 | * Returns 0 if successful, 1 if error. */ |
| 65 | int WriteAndFreeGptData(GptData* gptdata) { |
| 66 | |
| 67 | uint64_t entries_sectors = TOTAL_ENTRIES_SIZE / gptdata->sector_bytes; |
| 68 | |
| 69 | if (gptdata->primary_header) { |
| 70 | if (gptdata->modified & GPT_MODIFIED_HEADER1) { |
| 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) { |
| 79 | if (0 != BootDeviceWriteLBA(2, entries_sectors, |
| 80 | gptdata->primary_entries)) |
| 81 | return 1; |
| 82 | } |
| 83 | Free(gptdata->primary_entries); |
| 84 | } |
| 85 | |
| 86 | if (gptdata->secondary_entries) { |
| 87 | if (gptdata->modified & GPT_MODIFIED_ENTRIES2) { |
| 88 | if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - entries_sectors - 1, |
| 89 | entries_sectors, gptdata->secondary_entries)) |
| 90 | return 1; |
| 91 | } |
| 92 | Free(gptdata->secondary_entries); |
| 93 | } |
| 94 | |
| 95 | if (gptdata->secondary_header) { |
| 96 | if (gptdata->modified & GPT_MODIFIED_HEADER2) { |
| 97 | if (0 != BootDeviceWriteLBA(gptdata->drive_sectors - 1, 1, |
| 98 | gptdata->secondary_header)) |
| 99 | return 1; |
| 100 | } |
| 101 | Free(gptdata->secondary_header); |
| 102 | } |
| 103 | |
| 104 | /* Success */ |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 109 | int LoadKernel2(LoadKernelParams* params) { |
| 110 | |
| 111 | VbPublicKey* kernel_subkey = (VbPublicKey*)params->header_sign_key_blob; |
| 112 | |
| 113 | GptData gpt; |
| 114 | uint64_t part_start, part_size; |
| 115 | uint64_t blba = params->bytes_per_lba; |
| 116 | uint64_t kbuf_sectors = KBUF_SIZE / blba; |
| 117 | uint8_t* kbuf = NULL; |
| 118 | int found_partitions = 0; |
| 119 | int good_partition = -1; |
| 120 | uint16_t tpm_key_version = 0; |
| 121 | uint16_t tpm_kernel_version = 0; |
| 122 | uint64_t lowest_key_version = 0xFFFF; |
| 123 | uint64_t lowest_kernel_version = 0xFFFF; |
| 124 | int is_dev = ((BOOT_FLAG_DEVELOPER & params->boot_flags) && |
| 125 | !(BOOT_FLAG_RECOVERY & params->boot_flags)); |
| 126 | int is_normal = (!(BOOT_FLAG_DEVELOPER & params->boot_flags) && |
| 127 | !(BOOT_FLAG_RECOVERY & params->boot_flags)); |
| 128 | |
| 129 | /* Clear output params in case we fail */ |
| 130 | params->partition_number = 0; |
| 131 | params->bootloader_address = 0; |
| 132 | params->bootloader_size = 0; |
| 133 | |
| 134 | if (is_normal) { |
| 135 | /* Read current kernel key index from TPM. Assumes TPM is already |
| 136 | * initialized. */ |
| 137 | if (0 != GetStoredVersions(KERNEL_VERSIONS, |
| 138 | &tpm_key_version, |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 139 | &tpm_kernel_version)) { |
| 140 | debug("Unable to get stored version from TPM\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 141 | return LOAD_KERNEL_RECOVERY; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 142 | } |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 143 | } else if (is_dev) { |
| 144 | /* In developer mode, we ignore the kernel subkey, and just use |
| 145 | * the SHA-512 hash to verify the key block. */ |
| 146 | kernel_subkey = NULL; |
| 147 | } |
| 148 | |
| 149 | do { |
| 150 | /* Read GPT data */ |
| 151 | gpt.sector_bytes = blba; |
| 152 | gpt.drive_sectors = params->ending_lba + 1; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 153 | if (0 != AllocAndReadGptData(&gpt)) { |
| 154 | debug("Unable to read GPT data\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 155 | break; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 156 | } |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 157 | |
| 158 | /* Initialize GPT library */ |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 159 | if (GPT_SUCCESS != GptInit(&gpt)) { |
| 160 | debug("Error parsing GPT\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 161 | break; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 162 | } |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 163 | |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 164 | /* Allocate kernel header buffers */ |
| 165 | kbuf = (uint8_t*)Malloc(KBUF_SIZE); |
| 166 | if (!kbuf) |
| 167 | break; |
| 168 | |
| 169 | /* Loop over candidate kernel partitions */ |
| 170 | while (GPT_SUCCESS == GptNextKernelEntry(&gpt, &part_start, &part_size)) { |
| 171 | VbKeyBlockHeader* key_block; |
| 172 | VbKernelPreambleHeader* preamble; |
| 173 | RSAPublicKey* data_key; |
| 174 | uint64_t key_version; |
| 175 | uint64_t body_offset; |
| 176 | |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 177 | debug("Found kernel entry at %" PRIu64 " size %" PRIu64 "\n", |
| 178 | part_start, part_size); |
| 179 | |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 180 | /* Found at least one kernel partition. */ |
| 181 | found_partitions++; |
| 182 | |
| 183 | /* Read the first part of the kernel partition */ |
| 184 | if (part_size < kbuf_sectors) |
| 185 | continue; |
| 186 | if (0 != BootDeviceReadLBA(part_start, kbuf_sectors, kbuf)) |
| 187 | continue; |
| 188 | |
| 189 | /* Verify the key block */ |
| 190 | key_block = (VbKeyBlockHeader*)kbuf; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 191 | if ((0 != KeyBlockVerify(key_block, KBUF_SIZE, kernel_subkey))) { |
| 192 | debug("Verifying key block failed.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 193 | continue; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 194 | } |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 195 | |
| 196 | /* Check the key block flags against the current boot mode */ |
| 197 | if (!(key_block->key_block_flags && |
| 198 | ((BOOT_FLAG_DEVELOPER & params->boot_flags) ? |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 199 | KEY_BLOCK_FLAG_DEVELOPER_1 : KEY_BLOCK_FLAG_DEVELOPER_0))) { |
| 200 | debug("Developer flag mismatch.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 201 | continue; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 202 | } |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 203 | if (!(key_block->key_block_flags && |
| 204 | ((BOOT_FLAG_RECOVERY & params->boot_flags) ? |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 205 | KEY_BLOCK_FLAG_RECOVERY_1 : KEY_BLOCK_FLAG_RECOVERY_0))) { |
| 206 | debug("Recovery flag mismatch.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 207 | continue; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 208 | } |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 209 | |
| 210 | /* Check for rollback of key version. Note this is implicitly |
| 211 | * skipped in recovery and developer modes because those set |
| 212 | * key_version=0 above. */ |
| 213 | key_version = key_block->data_key.key_version; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 214 | if (key_version < tpm_key_version) { |
| 215 | debug("Key version too old.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 216 | continue; |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 217 | } |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 218 | |
| 219 | /* Get the key for preamble/data verification from the key block */ |
| 220 | data_key = PublicKeyToRSA(&key_block->data_key); |
| 221 | if (!data_key) |
| 222 | continue; |
| 223 | |
| 224 | /* Verify the preamble, which follows the key block */ |
| 225 | preamble = (VbKernelPreambleHeader*)(kbuf + key_block->key_block_size); |
| 226 | if ((0 != VerifyKernelPreamble2(preamble, |
| 227 | KBUF_SIZE - key_block->key_block_size, |
| 228 | data_key))) { |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 229 | debug("Preamble verification failed.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 230 | RSAPublicKeyFree(data_key); |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | /* Check for rollback of kernel version. Note this is implicitly |
| 235 | * skipped in recovery and developer modes because those set |
| 236 | * key_version=0 and kernel_version=0 above. */ |
| 237 | if (key_version == tpm_key_version && |
| 238 | preamble->kernel_version < tpm_kernel_version) { |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 239 | debug("Kernel version too low.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 240 | RSAPublicKeyFree(data_key); |
| 241 | continue; |
| 242 | } |
| 243 | |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 244 | debug("Kernel preamble is good.\n"); |
| 245 | |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 246 | /* Check for lowest key version from a valid header. */ |
| 247 | if (lowest_key_version > key_version) { |
| 248 | lowest_key_version = key_version; |
| 249 | lowest_kernel_version = preamble->kernel_version; |
| 250 | } |
| 251 | else if (lowest_key_version == key_version && |
| 252 | lowest_kernel_version > preamble->kernel_version) { |
| 253 | lowest_kernel_version = preamble->kernel_version; |
| 254 | } |
| 255 | |
| 256 | /* If we already have a good kernel, no need to read another |
| 257 | * one; we only needed to look at the versions to check for |
| 258 | * rollback. */ |
| 259 | if (-1 != good_partition) |
| 260 | continue; |
| 261 | |
| 262 | /* Verify body load address matches what we expect */ |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 263 | if ((preamble->body_load_address != (size_t)params->kernel_buffer) && |
| 264 | !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) { |
| 265 | debug("Wrong body load address.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 266 | RSAPublicKeyFree(data_key); |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | /* Verify kernel body starts at a multiple of the sector size. */ |
| 271 | body_offset = key_block->key_block_size + preamble->preamble_size; |
| 272 | if (0 != body_offset % blba) { |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 273 | debug("Kernel body not at multiple of sector size.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 274 | RSAPublicKeyFree(data_key); |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | /* Verify kernel body fits in the partition */ |
| 279 | if (body_offset + preamble->body_signature.data_size > |
| 280 | part_size * blba) { |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 281 | debug("Kernel body doesn't fit in partition.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 282 | RSAPublicKeyFree(data_key); |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | /* Read the kernel data */ |
| 287 | if (0 != BootDeviceReadLBA( |
| 288 | part_start + (body_offset / blba), |
| 289 | (preamble->body_signature.data_size + blba - 1) / blba, |
| 290 | params->kernel_buffer)) { |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 291 | debug("Unable to read kernel data.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 292 | RSAPublicKeyFree(data_key); |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | /* Verify kernel data */ |
| 297 | if (0 != VerifyData((const uint8_t*)params->kernel_buffer, |
| 298 | &preamble->body_signature, data_key)) { |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 299 | debug("Kernel data verification failed.\n"); |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 300 | RSAPublicKeyFree(data_key); |
| 301 | continue; |
| 302 | } |
| 303 | |
| 304 | /* Done with the kernel signing key, so can free it now */ |
| 305 | RSAPublicKeyFree(data_key); |
| 306 | |
| 307 | /* If we're still here, the kernel is valid. */ |
| 308 | /* 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^] | 309 | debug("Partiton is good.\n"); |
| 310 | good_partition = gpt.current_kernel; |
| 311 | params->partition_number = gpt.current_kernel; |
| 312 | params->bootloader_address = preamble->bootloader_address; |
| 313 | params->bootloader_size = preamble->bootloader_size; |
| 314 | /* If we're in developer or recovery mode, there's no rollback |
| 315 | * protection, so we can stop at the first valid kernel. */ |
| 316 | if (!is_normal) |
| 317 | break; |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 318 | |
Randall Spangler | 695cd16 | 2010-06-15 23:38:23 -0700 | [diff] [blame^] | 319 | /* Otherwise, we're in normal boot mode, so we do care about the |
| 320 | * key index in the TPM. If the good partition's key version is |
| 321 | * the same as the tpm, then the TPM doesn't need updating; we |
| 322 | * can stop now. Otherwise, we'll check all the other headers |
| 323 | * to see if they contain a newer key. */ |
| 324 | if (key_version == tpm_key_version && |
| 325 | preamble->kernel_version == tpm_kernel_version) |
| 326 | break; |
Randall Spangler | d183644 | 2010-06-10 09:59:04 -0700 | [diff] [blame] | 327 | } /* while(GptNextKernelEntry) */ |
| 328 | } while(0); |
| 329 | |
| 330 | /* Free kernel buffer */ |
| 331 | if (kbuf) |
| 332 | Free(kbuf); |
| 333 | |
| 334 | /* Write and free GPT data */ |
| 335 | WriteAndFreeGptData(&gpt); |
| 336 | |
| 337 | /* Handle finding a good partition */ |
| 338 | if (good_partition >= 0) { |
| 339 | |
| 340 | /* See if we need to update the TPM */ |
| 341 | if (is_normal) { |
| 342 | /* We only update the TPM in normal boot mode. In developer |
| 343 | * mode, the kernel is self-signed by the developer, so we can't |
| 344 | * trust the key version and wouldn't want to roll the TPM |
| 345 | * forward. In recovery mode, the TPM stays PP-unlocked, so |
| 346 | * anything we write gets blown away by the firmware when we go |
| 347 | * back to normal mode. */ |
| 348 | if ((lowest_key_version > tpm_key_version) || |
| 349 | (lowest_key_version == tpm_key_version && |
| 350 | lowest_kernel_version > tpm_kernel_version)) { |
| 351 | if (0 != WriteStoredVersions(KERNEL_VERSIONS, |
| 352 | lowest_key_version, |
| 353 | lowest_kernel_version)) |
| 354 | return LOAD_KERNEL_RECOVERY; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (!(BOOT_FLAG_RECOVERY & params->boot_flags)) { |
| 359 | /* We can lock the TPM now, since we've decided which kernel we |
| 360 | * like. If we don't find a good kernel, we leave the TPM |
| 361 | * unlocked so we can try again on the next boot device. If no |
| 362 | * kernels are good, we'll reboot to recovery mode, so it's ok to |
| 363 | * leave the TPM unlocked in that case too. |
| 364 | * |
| 365 | * If we're already in recovery mode, we need to leave PP unlocked, |
| 366 | * so don't lock the kernel versions. */ |
| 367 | if (0 != LockKernelVersionsByLockingPP()) |
| 368 | return LOAD_KERNEL_RECOVERY; |
| 369 | } |
| 370 | |
| 371 | /* Success! */ |
| 372 | return LOAD_KERNEL_SUCCESS; |
| 373 | } |
| 374 | |
| 375 | // Handle error cases |
| 376 | if (found_partitions) |
| 377 | return LOAD_KERNEL_INVALID; |
| 378 | else |
| 379 | return LOAD_KERNEL_NOT_FOUND; |
| 380 | } |