| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Data structure and API definitions for a verified boot kernel image. | 5 * API definitions for a generating and manipulating verified boot kernel images
. |
| 6 * (Userland portion.) |
| 6 */ | 7 */ |
| 7 | 8 |
| 8 #ifndef VBOOT_REFERENCE_KERNEL_IMAGE_H_ | 9 #ifndef VBOOT_REFERENCE_KERNEL_IMAGE_H_ |
| 9 #define VBOOT_REFERENCE_KERNEL_IMAGE_H_ | 10 #define VBOOT_REFERENCE_KERNEL_IMAGE_H_ |
| 10 | 11 |
| 11 #include <inttypes.h> | 12 #include "kernel_image_fw.h" |
| 12 | |
| 13 #include "rsa.h" | |
| 14 #include "sha.h" | |
| 15 | |
| 16 #define KERNEL_MAGIC "CHROMEOS" | |
| 17 #define KERNEL_MAGIC_SIZE 8 | |
| 18 #define KERNEL_CMD_LINE_SIZE 4096 | |
| 19 | |
| 20 #define DEV_MODE_ENABLED 1 | |
| 21 #define DEV_MODE_DISABLED 0 | |
| 22 | |
| 23 /* Kernel config file options according to the Chrome OS drive map design. */ | |
| 24 typedef struct kconfig_options { | |
| 25 uint32_t version[2]; /* Configuration file version. */ | |
| 26 uint8_t cmd_line[KERNEL_CMD_LINE_SIZE]; /* Kernel command line option string | |
| 27 * terminated by a NULL character. */ | |
| 28 uint64_t kernel_len; /* Size of the kernel. */ | |
| 29 uint64_t kernel_load_addr; /* Load address in memory for the kernel image */ | |
| 30 uint64_t kernel_entry_addr; /* Address to jump to after kernel is loaded. */ | |
| 31 } kconfig_options; | |
| 32 | |
| 33 | |
| 34 typedef struct KernelImage { | |
| 35 uint8_t magic[KERNEL_MAGIC_SIZE]; | |
| 36 /* Key header */ | |
| 37 uint16_t header_version; /* Header version. */ | |
| 38 uint16_t header_len; /* Length of the header. */ | |
| 39 uint16_t firmware_sign_algorithm; /* Signature algorithm used by the firmware | |
| 40 * signing key (used to sign this kernel | |
| 41 * header. */ | |
| 42 uint16_t kernel_sign_algorithm; /* Signature algorithm used by the kernel | |
| 43 * signing key. */ | |
| 44 uint16_t kernel_key_version; /* Key Version# for preventing rollbacks. */ | |
| 45 uint8_t* kernel_sign_key; /* Pre-processed public half of signing key. */ | |
| 46 /* TODO(gauravsh): Do we need a choice of digest algorithms for the header | |
| 47 * checksum? */ | |
| 48 uint8_t header_checksum[SHA512_DIGEST_SIZE]; /* SHA-512 Crytographic hash of | |
| 49 * the concatenation of the | |
| 50 * header fields, i.e. | |
| 51 * [header_len, | |
| 52 * firmware_sign_algorithm, | |
| 53 * sign_algorithm, sign_key, | |
| 54 * key_version] */ | |
| 55 | |
| 56 uint8_t* kernel_key_signature; /* Signature of the header above. */ | |
| 57 | |
| 58 uint16_t kernel_version; /* Kernel Version# for preventing rollbacks. */ | |
| 59 kconfig_options options; /* Other kernel/bootloader options. */ | |
| 60 | |
| 61 uint8_t* config_signature; /* Signature of the kernel config file. */ | |
| 62 | |
| 63 /* The kernel signature comes first as it may allow us to parallelize | |
| 64 * the kernel data fetch and RSA public key operation. | |
| 65 */ | |
| 66 uint8_t* kernel_signature; /* Signature on the concatenation of | |
| 67 * [kernel_version], [options] and | |
| 68 * [kernel_data]. */ | |
| 69 uint8_t* kernel_data; /* Actual kernel data. */ | |
| 70 | |
| 71 } KernelImage; | |
| 72 | 13 |
| 73 /* Allocate and return a new KernelImage structure. */ | 14 /* Allocate and return a new KernelImage structure. */ |
| 74 KernelImage* KernelImageNew(void); | 15 KernelImage* KernelImageNew(void); |
| 75 | 16 |
| 76 /* Deep free the contents of [image]. */ | 17 /* Deep free the contents of [image]. */ |
| 77 void KernelImageFree(KernelImage* image); | 18 void KernelImageFree(KernelImage* image); |
| 78 | 19 |
| 79 /* Read kernel data from file named [input_file]. | 20 /* Read kernel data from file named [input_file]. |
| 80 * | 21 * |
| 81 * Returns a filled up KernelImage on success, NULL on error. | 22 * Returns a filled up KernelImage on success, NULL on error. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 * Return 1 on success, 0 on error. | 59 * Return 1 on success, 0 on error. |
| 119 */ | 60 */ |
| 120 int WriteKernelImage(const char* input_file, | 61 int WriteKernelImage(const char* input_file, |
| 121 const KernelImage* image); | 62 const KernelImage* image); |
| 122 | 63 |
| 123 /* Pretty print the contents of [image]. Only headers and metadata information | 64 /* Pretty print the contents of [image]. Only headers and metadata information |
| 124 * is printed. | 65 * is printed. |
| 125 */ | 66 */ |
| 126 void PrintKernelImage(const KernelImage* image); | 67 void PrintKernelImage(const KernelImage* image); |
| 127 | 68 |
| 128 /* Error Codes for VerifyFirmware. */ | |
| 129 #define VERIFY_KERNEL_SUCCESS 0 | |
| 130 #define VERIFY_KERNEL_INVALID_IMAGE 1 | |
| 131 #define VERIFY_KERNEL_KEY_SIGNATURE_FAILED 2 | |
| 132 #define VERIFY_KERNEL_INVALID_ALGORITHM 3 | |
| 133 #define VERIFY_KERNEL_CONFIG_SIGNATURE_FAILED 4 | |
| 134 #define VERIFY_KERNEL_SIGNATURE_FAILED 5 | |
| 135 #define VERIFY_KERNEL_WRONG_MAGIC 6 | |
| 136 #define VERIFY_KERNEL_MAX 7 /* Generic catch-all. */ | |
| 137 | |
| 138 extern char* kVerifyKernelErrors[VERIFY_KERNEL_MAX]; | |
| 139 | |
| 140 /* Checks for the sanity of the kernel header pointed by [kernel_header_blob]. | |
| 141 * If [dev_mode] is enabled, also checks the firmware key signature using the | |
| 142 * pre-processed public firmware signing key [firmware_sign_key_blob]. | |
| 143 * | |
| 144 * On success, put firmware signature algorithm in [firmware_algorithm], | |
| 145 * kernel signature algorithm in [kernel_algorithm], kernel header | |
| 146 * length in [header_len], and return 0. | |
| 147 * Else, return error code on failure. | |
| 148 */ | |
| 149 int VerifyKernelHeader(const uint8_t* firmware_sign_key_blob, | |
| 150 const uint8_t* kernel_header_blob, | |
| 151 const int dev_mode, | |
| 152 int* firmware_algorithm, | |
| 153 int* kernel_algorithm, | |
| 154 int* header_len); | |
| 155 | |
| 156 /* Checks the kernel config (analogous to preamble for firmware) signature on | |
| 157 * kernel config pointed by [kernel_config_blob] using the signing key | |
| 158 * [kernel_sign_key]. | |
| 159 * | |
| 160 * On success, put kernel length into [kernel_len], and return 0. | |
| 161 * Else, return error code on failure. | |
| 162 */ | |
| 163 int VerifyKernelConfig(RSAPublicKey* kernel_sign_key, | |
| 164 const uint8_t* kernel_config_blob, | |
| 165 int algorithm, | |
| 166 uint64_t* kernel_len); | |
| 167 | |
| 168 /* Checks the signature on the kernel data at location [kernel_data_start]. | |
| 169 * The length of the actual kernel data is kernel _len and it is assumed to | |
| 170 * be prepended with the signature whose size depends on the signature_algorithm | |
| 171 * [algorithm]. | |
| 172 * | |
| 173 * Return 0 on success, error code on failure. | |
| 174 */ | |
| 175 int VerifyKernelData(RSAPublicKey* kernel_sign_key, | |
| 176 const uint8_t* kernel_config_start, | |
| 177 const uint8_t* kernel_data_start, | |
| 178 uint64_t kernel_len, | |
| 179 int algorithm); | |
| 180 | |
| 181 /* Performs a chained verify of the kernel blob [kernel_blob]. If | |
| 182 * [dev_mode] is 0 [inactive], then the pre-processed public signing key | |
| 183 * [root_key_blob] is used to verify the signature of the signing key, | |
| 184 * else the check is skipped. | |
| 185 * | |
| 186 * TODO(gauravsh): Does the dev mode only effect the R/W firmware verification, | |
| 187 * or kernel verification, or both? | |
| 188 * | |
| 189 * Returns 0 on success, error code on failure. | |
| 190 * | |
| 191 * NOTE: The length of the kernel blob is derived from reading the fields | |
| 192 * in the first few bytes of the buffer. This might look risky but in firmware | |
| 193 * land, the start address of the kernel_blob will always be fixed depending | |
| 194 * on the memory map on the particular platform. In addition, the signature on | |
| 195 * length itself is checked early in the verification process for extra safety. | |
| 196 */ | |
| 197 int VerifyKernel(const uint8_t* signing_key_blob, | |
| 198 const uint8_t* kernel_blob, | |
| 199 const int dev_mode); | |
| 200 | |
| 201 /* Performs a chained verify of the kernel [image]. If [dev_mode] is | 69 /* Performs a chained verify of the kernel [image]. If [dev_mode] is |
| 202 * 0 (inactive), then the [firmware_signing_key] is used to verify the signature | 70 * 0 (inactive), then the [firmware_signing_key] is used to verify the signature |
| 203 * of the signing key, else the check is skipped. | 71 * of the signing key, else the check is skipped. |
| 204 * | 72 * |
| 205 * Returns 0 on success, error code on failure. | 73 * Returns 0 on success, error code on failure. |
| 206 */ | 74 */ |
| 207 int VerifyKernelImage(const RSAPublicKey* firmware_signing_key, | 75 int VerifyKernelImage(const RSAPublicKey* firmware_signing_key, |
| 208 const KernelImage* image, | 76 const KernelImage* image, |
| 209 int dev_mode); | 77 int dev_mode); |
| 210 | 78 |
| 211 | 79 |
| 212 /* Maps error codes from VerifyKernel*() to error description. */ | 80 /* Maps error codes from VerifyKernel*() to error description. */ |
| 213 const char* VerifyKernelErrorString(int error); | 81 const char* VerifyKernelErrorString(int error); |
| 214 | 82 |
| 215 /* Add a kernel signing key signature to the key header to a kernel image | 83 /* Add a kernel signing key signature to the key header to a kernel image |
| 216 * [image] using the private key in file [firmware_key_file]. | 84 * [image] using the private key in file [firmware_key_file]. |
| 217 * | 85 * |
| 218 * Return 1 on success, 0 on failure. | 86 * Return 1 on success, 0 on failure. |
| 219 */ | 87 */ |
| 220 int AddKernelKeySignature(KernelImage* image, const char* firmware_key_file); | 88 int AddKernelKeySignature(KernelImage* image, const char* firmware_key_file); |
| 221 | 89 |
| 222 /* Add a kernel and kernel config signature to a kernel image [image] | 90 /* Add a kernel and kernel config signature to a kernel image [image] |
| 223 * using the private signing key in file [kernel_sigining_key_file]. | 91 * using the private signing key in file [kernel_sigining_key_file]. |
| 224 * | 92 * |
| 225 * Return 1 on success, 0 on failure. | 93 * Return 1 on success, 0 on failure. |
| 226 */ | 94 */ |
| 227 int AddKernelSignature(KernelImage* image, | 95 int AddKernelSignature(KernelImage* image, |
| 228 const char* kernel_sigining_key_file); | 96 const char* kernel_sigining_key_file); |
| 229 | 97 |
| 230 /* Returns the logical version of a kernel blob which is calculated as | |
| 231 * (kernel_key_version << 16 | kernel_version). */ | |
| 232 uint32_t GetLogicalKernelVersion(uint8_t* kernel_blob); | |
| 233 | |
| 234 #define BOOT_KERNEL_A_CONTINUE 1 | |
| 235 #define BOOT_KERNEL_B_CONTINUE 2 | |
| 236 #define BOOT_KERNEL_RECOVERY_CONTINUE 3 | |
| 237 | |
| 238 /* Contains information about the kernel paritition | |
| 239 * gleaned from the GPT partition table. | |
| 240 * | |
| 241 * Based on the Chromium OS Drive Map design document by | |
| 242 * rspangler@chromium.org. | |
| 243 * | |
| 244 */ | |
| 245 typedef struct kernel_entry { | |
| 246 uint8_t* kernel_blob; /* Pointer to actual kernel. */ | |
| 247 uint8_t boot_priority; /* 15 = highest, 1 = lowest, 0 = not bootable. */ | |
| 248 uint8_t boot_tries_remaining; /* Used when boot_priority = 0. */ | |
| 249 uint8_t boot_success_flag; /* Set to 1 on successful boot by AU. */ | |
| 250 } kernel_entry; | |
| 251 | |
| 252 void PrintKernelEntry(kernel_entry* entry); | 98 void PrintKernelEntry(kernel_entry* entry); |
| 253 | 99 |
| 254 /* This function is the driver used by the RW firmware to | |
| 255 * determine which copy of the kernel to boot from. It performs | |
| 256 * the requisite priority and remaining tries checking for a specific | |
| 257 * kernel partition, does rollback index checking, including updating | |
| 258 * if required. | |
| 259 * | |
| 260 * Returns the code path to follow. It is one of: | |
| 261 * BOOT_KERNEL_A_CONTINUE Boot from Kenrel A | |
| 262 * BOOT_KERNEL_B_CONTINUE Boot from Kernel B | |
| 263 * BOOT_KERNEL_RECOVERY_CONTINUE Jump to recovery mode | |
| 264 */ | |
| 265 int VerifyKernelDriver_f(uint8_t* firmware_key_blob, | |
| 266 kernel_entry* kernelA, | |
| 267 kernel_entry* kernelB, | |
| 268 int dev_mode); | |
| 269 | |
| 270 #endif /* VBOOT_REFERENCE_KERNEL_IMAGE_H_ */ | 100 #endif /* VBOOT_REFERENCE_KERNEL_IMAGE_H_ */ |
| OLD | NEW |