Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* TODO: some more #ifdef's to avoid unneeded code for stage 1 / stage 2 */ |
| 8 | |
| 9 | #ifdef CCDM_ID_DEBUG |
| 10 | #define DEBUG |
| 11 | #endif |
| 12 | |
| 13 | #include <common.h> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 14 | #include <bootstage.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 15 | #include <command.h> |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 16 | #include <dm.h> |
Simon Glass | 6eaea25 | 2019-08-01 09:46:48 -0600 | [diff] [blame] | 17 | #include <env.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 18 | #include <hang.h> |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 19 | #include <malloc.h> |
| 20 | #include <fs.h> |
| 21 | #include <i2c.h> |
| 22 | #include <mmc.h> |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 23 | #include <tpm-v1.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 24 | #include <u-boot/crc.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 25 | #include <u-boot/sha1.h> |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 26 | #include <asm/byteorder.h> |
| 27 | #include <asm/unaligned.h> |
| 28 | #include <pca9698.h> |
| 29 | |
| 30 | #undef CCDM_FIRST_STAGE |
| 31 | #undef CCDM_SECOND_STAGE |
| 32 | #undef CCDM_AUTO_FIRST_STAGE |
| 33 | |
| 34 | #ifdef CONFIG_DEVELOP |
| 35 | #define CCDM_DEVELOP |
| 36 | #endif |
| 37 | |
| 38 | #ifdef CONFIG_TRAILBLAZER |
| 39 | #define CCDM_FIRST_STAGE |
| 40 | #undef CCDM_SECOND_STAGE |
| 41 | #else |
| 42 | #undef CCDM_FIRST_STAGE |
| 43 | #define CCDM_SECOND_STAGE |
| 44 | #endif |
| 45 | |
| 46 | #if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \ |
| 47 | !defined(CCCM_FIRST_STAGE) |
| 48 | #define CCDM_AUTO_FIRST_STAGE |
| 49 | #endif |
| 50 | |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 51 | /* CCDM specific contants */ |
| 52 | enum { |
| 53 | /* NV indices */ |
| 54 | NV_COMMON_DATA_INDEX = 0x40000001, |
| 55 | /* magics for key blob chains */ |
| 56 | MAGIC_KEY_PROGRAM = 0x68726500, |
| 57 | MAGIC_HMAC = 0x68616300, |
| 58 | MAGIC_END_OF_CHAIN = 0x00000000, |
| 59 | /* sizes */ |
| 60 | NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t), |
| 61 | }; |
| 62 | |
| 63 | /* other constants */ |
| 64 | enum { |
| 65 | ESDHC_BOOT_IMAGE_SIG_OFS = 0x40, |
| 66 | ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48, |
| 67 | ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50, |
| 68 | ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58, |
| 69 | ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60, |
| 70 | }; |
| 71 | |
Dirk Eibach | e674bf1 | 2014-07-03 09:28:16 +0200 | [diff] [blame] | 72 | enum { |
| 73 | I2C_SOC_0 = 0, |
| 74 | I2C_SOC_1 = 1, |
| 75 | }; |
| 76 | |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 77 | struct key_program { |
| 78 | uint32_t magic; |
| 79 | uint32_t code_crc; |
| 80 | uint32_t code_size; |
| 81 | uint8_t code[]; |
| 82 | }; |
| 83 | |
| 84 | struct h_reg { |
| 85 | bool valid; |
| 86 | uint8_t digest[20]; |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | enum access_mode { |
| 91 | HREG_NONE = 0, |
| 92 | HREG_RD = 1, |
| 93 | HREG_WR = 2, |
| 94 | HREG_RDWR = 3, |
| 95 | }; |
| 96 | |
| 97 | /* register constants */ |
| 98 | enum { |
| 99 | FIX_HREG_DEVICE_ID_HASH = 0, |
| 100 | FIX_HREG_SELF_HASH = 1, |
| 101 | FIX_HREG_STAGE2_HASH = 2, |
| 102 | FIX_HREG_VENDOR = 3, |
| 103 | COUNT_FIX_HREGS |
| 104 | }; |
| 105 | |
| 106 | |
| 107 | /* hre opcodes */ |
| 108 | enum { |
| 109 | /* opcodes w/o data */ |
| 110 | HRE_NOP = 0x00, |
| 111 | HRE_SYNC = HRE_NOP, |
| 112 | HRE_CHECK0 = 0x01, |
| 113 | /* opcodes w/o data, w/ sync dst */ |
| 114 | /* opcodes w/ data */ |
| 115 | HRE_LOAD = 0x81, |
| 116 | /* opcodes w/data, w/sync dst */ |
| 117 | HRE_XOR = 0xC1, |
| 118 | HRE_AND = 0xC2, |
| 119 | HRE_OR = 0xC3, |
| 120 | HRE_EXTEND = 0xC4, |
| 121 | HRE_LOADKEY = 0xC5, |
| 122 | }; |
| 123 | |
| 124 | /* hre errors */ |
| 125 | enum { |
| 126 | HRE_E_OK = 0, |
| 127 | HRE_E_TPM_FAILURE, |
| 128 | HRE_E_INVALID_HREG, |
| 129 | }; |
| 130 | |
| 131 | static uint64_t device_id; |
| 132 | static uint64_t device_cl; |
| 133 | static uint64_t device_type; |
| 134 | |
| 135 | static uint32_t platform_key_handle; |
| 136 | |
| 137 | static void(*bl2_entry)(void); |
| 138 | |
| 139 | static struct h_reg pcr_hregs[24]; |
| 140 | static struct h_reg fix_hregs[COUNT_FIX_HREGS]; |
| 141 | static struct h_reg var_hregs[8]; |
| 142 | static uint32_t hre_tpm_err; |
| 143 | static int hre_err = HRE_E_OK; |
| 144 | |
| 145 | #define IS_PCR_HREG(spec) ((spec) & 0x20) |
| 146 | #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08) |
| 147 | #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10) |
| 148 | #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7)) |
| 149 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 150 | static int get_tpm(struct udevice **devp) |
| 151 | { |
| 152 | int rc; |
| 153 | |
| 154 | rc = uclass_first_device_err(UCLASS_TPM, devp); |
| 155 | if (rc) { |
| 156 | printf("Could not find TPM (ret=%d)\n", rc); |
| 157 | return CMD_RET_FAILURE; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 163 | static const uint8_t vendor[] = "Guntermann & Drunck"; |
| 164 | |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 165 | /** |
| 166 | * @brief read a bunch of data from MMC into memory. |
| 167 | * |
| 168 | * @param mmc pointer to the mmc structure to use. |
| 169 | * @param src offset where the data starts on MMC/SD device (in bytes). |
| 170 | * @param dst pointer to the location where the read data should be stored. |
| 171 | * @param size number of bytes to read from the MMC/SD device. |
| 172 | * @return number of bytes read or -1 on error. |
| 173 | */ |
| 174 | static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size) |
| 175 | { |
| 176 | int result = 0; |
| 177 | u32 blk_len, ofs; |
| 178 | ulong block_no, n, cnt; |
| 179 | u8 *tmp_buf = NULL; |
| 180 | |
| 181 | if (size <= 0) |
| 182 | goto end; |
| 183 | |
| 184 | blk_len = mmc->read_bl_len; |
| 185 | tmp_buf = malloc(blk_len); |
| 186 | if (!tmp_buf) |
| 187 | goto failure; |
| 188 | block_no = src / blk_len; |
| 189 | ofs = src % blk_len; |
| 190 | |
| 191 | if (ofs) { |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 192 | n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1, |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 193 | tmp_buf); |
| 194 | if (!n) |
| 195 | goto failure; |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 196 | result = min(size, (int)(blk_len - ofs)); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 197 | memcpy(dst, tmp_buf + ofs, result); |
| 198 | dst += result; |
| 199 | size -= result; |
| 200 | } |
| 201 | cnt = size / blk_len; |
| 202 | if (cnt) { |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 203 | n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt, |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 204 | dst); |
| 205 | if (n != cnt) |
| 206 | goto failure; |
| 207 | size -= cnt * blk_len; |
| 208 | result += cnt * blk_len; |
| 209 | dst += cnt * blk_len; |
| 210 | block_no += cnt; |
| 211 | } |
| 212 | if (size) { |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 213 | n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1, |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 214 | tmp_buf); |
| 215 | if (!n) |
| 216 | goto failure; |
| 217 | memcpy(dst, tmp_buf, size); |
| 218 | result += size; |
| 219 | } |
| 220 | goto end; |
| 221 | failure: |
| 222 | result = -1; |
| 223 | end: |
| 224 | if (tmp_buf) |
| 225 | free(tmp_buf); |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @brief returns a location where the 2nd stage bootloader can be(/ is) placed. |
| 231 | * |
| 232 | * @return pointer to the location for/of the 2nd stage bootloader |
| 233 | */ |
| 234 | static u8 *get_2nd_stage_bl_location(ulong target_addr) |
| 235 | { |
| 236 | ulong addr; |
| 237 | #ifdef CCDM_SECOND_STAGE |
Simon Glass | 22c34c2 | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 238 | addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 239 | #else |
| 240 | addr = target_addr; |
| 241 | #endif |
| 242 | return (u8 *)(addr); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | #ifdef CCDM_SECOND_STAGE |
| 247 | /** |
| 248 | * @brief returns a location where the image can be(/ is) placed. |
| 249 | * |
| 250 | * @return pointer to the location for/of the image |
| 251 | */ |
| 252 | static u8 *get_image_location(void) |
| 253 | { |
| 254 | ulong addr; |
| 255 | /* TODO use other area? */ |
Simon Glass | 22c34c2 | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 256 | addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 257 | return (u8 *)(addr); |
| 258 | } |
| 259 | #endif |
| 260 | |
| 261 | /** |
| 262 | * @brief get the size of a given (TPM) NV area |
| 263 | * @param index NV index of the area to get size for |
| 264 | * @param size pointer to the size |
| 265 | * @return 0 on success, != 0 on error |
| 266 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 267 | static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 268 | { |
| 269 | uint32_t err; |
| 270 | uint8_t info[72]; |
| 271 | uint8_t *ptr; |
| 272 | uint16_t v16; |
| 273 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 274 | err = tpm_get_capability(tpm, TPM_CAP_NV_INDEX, index, |
| 275 | info, sizeof(info)); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 276 | if (err) { |
| 277 | printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n", |
| 278 | index, err); |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | /* skip tag and nvIndex */ |
| 283 | ptr = info + 6; |
| 284 | /* skip 2 pcr info fields */ |
| 285 | v16 = get_unaligned_be16(ptr); |
| 286 | ptr += 2 + v16 + 1 + 20; |
| 287 | v16 = get_unaligned_be16(ptr); |
| 288 | ptr += 2 + v16 + 1 + 20; |
| 289 | /* skip permission and flags */ |
| 290 | ptr += 6 + 3; |
| 291 | |
| 292 | *size = get_unaligned_be32(ptr); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @brief search for a key by usage auth and pub key hash. |
| 298 | * @param auth usage auth of the key to search for |
| 299 | * @param pubkey_digest (SHA1) hash of the pub key structure of the key |
| 300 | * @param[out] handle the handle of the key iff found |
| 301 | * @return 0 if key was found in TPM; != 0 if not. |
| 302 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 303 | static int find_key(struct udevice *tpm, const uint8_t auth[20], |
| 304 | const uint8_t pubkey_digest[20], uint32_t *handle) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 305 | { |
| 306 | uint16_t key_count; |
| 307 | uint32_t key_handles[10]; |
| 308 | uint8_t buf[288]; |
| 309 | uint8_t *ptr; |
| 310 | uint32_t err; |
| 311 | uint8_t digest[20]; |
| 312 | size_t buf_len; |
| 313 | unsigned int i; |
| 314 | |
| 315 | /* fetch list of already loaded keys in the TPM */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 316 | err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf, |
| 317 | sizeof(buf)); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 318 | if (err) |
| 319 | return -1; |
| 320 | key_count = get_unaligned_be16(buf); |
| 321 | ptr = buf + 2; |
| 322 | for (i = 0; i < key_count; ++i, ptr += 4) |
| 323 | key_handles[i] = get_unaligned_be32(ptr); |
| 324 | |
| 325 | /* now search a(/ the) key which we can access with the given auth */ |
| 326 | for (i = 0; i < key_count; ++i) { |
| 327 | buf_len = sizeof(buf); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 328 | err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf, |
| 329 | &buf_len); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 330 | if (err && err != TPM_AUTHFAIL) |
| 331 | return -1; |
| 332 | if (err) |
| 333 | continue; |
| 334 | sha1_csum(buf, buf_len, digest); |
| 335 | if (!memcmp(digest, pubkey_digest, 20)) { |
| 336 | *handle = key_handles[i]; |
| 337 | return 0; |
| 338 | } |
| 339 | } |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * @brief read CCDM common data from TPM NV |
| 345 | * @return 0 if CCDM common data was found and read, !=0 if something failed. |
| 346 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 347 | static int read_common_data(struct udevice *tpm) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 348 | { |
| 349 | uint32_t size; |
| 350 | uint32_t err; |
| 351 | uint8_t buf[256]; |
| 352 | sha1_context ctx; |
| 353 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 354 | if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) || |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 355 | size < NV_COMMON_DATA_MIN_SIZE) |
| 356 | return 1; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 357 | err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX, |
| 358 | buf, min(sizeof(buf), size)); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 359 | if (err) { |
| 360 | printf("tpm_nv_read_value() failed: %u\n", err); |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | device_id = get_unaligned_be64(buf); |
| 365 | device_cl = get_unaligned_be64(buf + 8); |
| 366 | device_type = get_unaligned_be64(buf + 16); |
| 367 | |
| 368 | sha1_starts(&ctx); |
| 369 | sha1_update(&ctx, buf, 24); |
| 370 | sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest); |
| 371 | fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true; |
| 372 | |
| 373 | platform_key_handle = get_unaligned_be32(buf + 24); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * @brief compute hash of bootloader itself. |
| 380 | * @param[out] dst hash register where the hash should be stored |
| 381 | * @return 0 on success, != 0 on failure. |
| 382 | * |
| 383 | * @note MUST be called at a time where the boot loader is accessible at the |
| 384 | * configured location (; so take care when code is reallocated). |
| 385 | */ |
| 386 | static int compute_self_hash(struct h_reg *dst) |
| 387 | { |
| 388 | sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE, |
| 389 | CONFIG_SYS_MONITOR_LEN, dst->digest); |
| 390 | dst->valid = true; |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | int ccdm_compute_self_hash(void) |
| 395 | { |
| 396 | if (!fix_hregs[FIX_HREG_SELF_HASH].valid) |
| 397 | compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * @brief compute the hash of the 2nd stage boot loader (on SD card) |
| 403 | * @param[out] dst hash register to store the computed hash |
| 404 | * @return 0 on success, != 0 on failure |
| 405 | * |
| 406 | * Determines the size and location of the 2nd stage boot loader on SD card, |
| 407 | * loads the 2nd stage boot loader and computes the (SHA1) hash value. |
| 408 | * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at |
| 409 | * the desired memory location and the variable @a bl2_entry is set. |
| 410 | * |
| 411 | * @note This sets the variable @a bl2_entry to the entry point when the |
| 412 | * 2nd stage boot loader is loaded at its configured memory location. |
| 413 | */ |
| 414 | static int compute_second_stage_hash(struct h_reg *dst) |
| 415 | { |
| 416 | int result = 0; |
| 417 | u32 code_len, code_offset, target_addr, exec_entry; |
| 418 | struct mmc *mmc; |
| 419 | u8 *load_addr = NULL; |
| 420 | u8 buf[128]; |
| 421 | |
| 422 | mmc = find_mmc_device(0); |
| 423 | if (!mmc) |
| 424 | goto failure; |
| 425 | mmc_init(mmc); |
| 426 | |
| 427 | if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0) |
| 428 | goto failure; |
| 429 | |
| 430 | code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS); |
| 431 | code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS); |
| 432 | target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS); |
| 433 | exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS); |
| 434 | |
| 435 | load_addr = get_2nd_stage_bl_location(target_addr); |
| 436 | if (load_addr == (u8 *)target_addr) |
| 437 | bl2_entry = (void(*)(void))exec_entry; |
| 438 | |
| 439 | if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0) |
| 440 | goto failure; |
| 441 | |
| 442 | sha1_csum(load_addr, code_len, dst->digest); |
| 443 | dst->valid = true; |
| 444 | |
| 445 | goto end; |
| 446 | failure: |
| 447 | result = 1; |
| 448 | bl2_entry = NULL; |
| 449 | end: |
| 450 | return result; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * @brief get pointer to hash register by specification |
| 455 | * @param spec specification of a hash register |
| 456 | * @return pointer to hash register or NULL if @a spec does not qualify a |
| 457 | * valid hash register; NULL else. |
| 458 | */ |
| 459 | static struct h_reg *get_hreg(uint8_t spec) |
| 460 | { |
| 461 | uint8_t idx; |
| 462 | |
| 463 | idx = HREG_IDX(spec); |
| 464 | if (IS_FIX_HREG(spec)) { |
| 465 | if (idx < ARRAY_SIZE(fix_hregs)) |
| 466 | return fix_hregs + idx; |
| 467 | hre_err = HRE_E_INVALID_HREG; |
| 468 | } else if (IS_PCR_HREG(spec)) { |
| 469 | if (idx < ARRAY_SIZE(pcr_hregs)) |
| 470 | return pcr_hregs + idx; |
| 471 | hre_err = HRE_E_INVALID_HREG; |
| 472 | } else if (IS_VAR_HREG(spec)) { |
| 473 | if (idx < ARRAY_SIZE(var_hregs)) |
| 474 | return var_hregs + idx; |
| 475 | hre_err = HRE_E_INVALID_HREG; |
| 476 | } |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @brief get pointer of a hash register by specification and usage. |
| 482 | * @param spec specification of a hash register |
| 483 | * @param mode access mode (read or write or read/write) |
| 484 | * @return pointer to hash register if found and valid; NULL else. |
| 485 | * |
| 486 | * This func uses @a get_reg() to determine the hash register for a given spec. |
| 487 | * If a register is found it is validated according to the desired access mode. |
| 488 | * The value of automatic registers (PCR register and fixed registers) is |
| 489 | * loaded or computed on read access. |
| 490 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 491 | static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec, |
| 492 | enum access_mode mode) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 493 | { |
| 494 | struct h_reg *result; |
| 495 | |
| 496 | result = get_hreg(spec); |
| 497 | if (!result) |
| 498 | return NULL; |
| 499 | |
| 500 | if (mode & HREG_WR) { |
| 501 | if (IS_FIX_HREG(spec)) { |
| 502 | hre_err = HRE_E_INVALID_HREG; |
| 503 | return NULL; |
| 504 | } |
| 505 | } |
| 506 | if (mode & HREG_RD) { |
| 507 | if (!result->valid) { |
| 508 | if (IS_PCR_HREG(spec)) { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 509 | hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec), |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 510 | result->digest, 20); |
| 511 | result->valid = (hre_tpm_err == TPM_SUCCESS); |
| 512 | } else if (IS_FIX_HREG(spec)) { |
| 513 | switch (HREG_IDX(spec)) { |
| 514 | case FIX_HREG_DEVICE_ID_HASH: |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 515 | read_common_data(tpm); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 516 | break; |
| 517 | case FIX_HREG_SELF_HASH: |
| 518 | ccdm_compute_self_hash(); |
| 519 | break; |
| 520 | case FIX_HREG_STAGE2_HASH: |
| 521 | compute_second_stage_hash(result); |
| 522 | break; |
| 523 | case FIX_HREG_VENDOR: |
| 524 | memcpy(result->digest, vendor, 20); |
| 525 | result->valid = true; |
| 526 | break; |
| 527 | } |
| 528 | } else { |
| 529 | result->valid = true; |
| 530 | } |
| 531 | } |
| 532 | if (!result->valid) { |
| 533 | hre_err = HRE_E_INVALID_HREG; |
| 534 | return NULL; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | return result; |
| 539 | } |
| 540 | |
| 541 | static void *compute_and(void *_dst, const void *_src, size_t n) |
| 542 | { |
| 543 | uint8_t *dst = _dst; |
| 544 | const uint8_t *src = _src; |
| 545 | size_t i; |
| 546 | |
| 547 | for (i = n; i-- > 0; ) |
| 548 | *dst++ &= *src++; |
| 549 | |
| 550 | return _dst; |
| 551 | } |
| 552 | |
| 553 | static void *compute_or(void *_dst, const void *_src, size_t n) |
| 554 | { |
| 555 | uint8_t *dst = _dst; |
| 556 | const uint8_t *src = _src; |
| 557 | size_t i; |
| 558 | |
| 559 | for (i = n; i-- > 0; ) |
| 560 | *dst++ |= *src++; |
| 561 | |
| 562 | return _dst; |
| 563 | } |
| 564 | |
| 565 | static void *compute_xor(void *_dst, const void *_src, size_t n) |
| 566 | { |
| 567 | uint8_t *dst = _dst; |
| 568 | const uint8_t *src = _src; |
| 569 | size_t i; |
| 570 | |
| 571 | for (i = n; i-- > 0; ) |
| 572 | *dst++ ^= *src++; |
| 573 | |
| 574 | return _dst; |
| 575 | } |
| 576 | |
| 577 | static void *compute_extend(void *_dst, const void *_src, size_t n) |
| 578 | { |
| 579 | uint8_t digest[20]; |
| 580 | sha1_context ctx; |
| 581 | |
| 582 | sha1_starts(&ctx); |
| 583 | sha1_update(&ctx, _dst, n); |
| 584 | sha1_update(&ctx, _src, n); |
| 585 | sha1_finish(&ctx, digest); |
| 586 | memcpy(_dst, digest, min(n, sizeof(digest))); |
| 587 | |
| 588 | return _dst; |
| 589 | } |
| 590 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 591 | static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg, |
| 592 | struct h_reg *dst_reg, const void *key, |
| 593 | size_t key_size) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 594 | { |
| 595 | uint32_t parent_handle; |
| 596 | uint32_t key_handle; |
| 597 | |
| 598 | if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid) |
| 599 | return -1; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 600 | if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle)) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 601 | return -1; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 602 | hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size, |
| 603 | src_reg->digest, &key_handle); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 604 | if (hre_tpm_err) { |
| 605 | hre_err = HRE_E_TPM_FAILURE; |
| 606 | return -1; |
| 607 | } |
| 608 | /* TODO remember key handle somehow? */ |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * @brief executes the next opcode on the hash register engine. |
| 615 | * @param[in,out] ip pointer to the opcode (instruction pointer) |
| 616 | * @param[in,out] code_size (remaining) size of the code |
| 617 | * @return new instruction pointer on success, NULL on error. |
| 618 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 619 | static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip, |
| 620 | size_t *code_size) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 621 | { |
| 622 | bool dst_modified = false; |
| 623 | uint32_t ins; |
| 624 | uint8_t opcode; |
| 625 | uint8_t src_spec; |
| 626 | uint8_t dst_spec; |
| 627 | uint16_t data_size; |
| 628 | struct h_reg *src_reg, *dst_reg; |
| 629 | uint8_t buf[20]; |
| 630 | const uint8_t *src_buf, *data; |
| 631 | uint8_t *ptr; |
| 632 | int i; |
| 633 | void * (*bin_func)(void *, const void *, size_t); |
| 634 | |
| 635 | if (*code_size < 4) |
| 636 | return NULL; |
| 637 | |
| 638 | ins = get_unaligned_be32(*ip); |
| 639 | opcode = **ip; |
| 640 | data = *ip + 4; |
| 641 | src_spec = (ins >> 18) & 0x3f; |
| 642 | dst_spec = (ins >> 12) & 0x3f; |
| 643 | data_size = (ins & 0x7ff); |
| 644 | |
| 645 | debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins, |
| 646 | opcode, src_spec, dst_spec, data_size); |
| 647 | |
| 648 | if ((opcode & 0x80) && (data_size + 4) > *code_size) |
| 649 | return NULL; |
| 650 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 651 | src_reg = access_hreg(tpm, src_spec, HREG_RD); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 652 | if (hre_err || hre_tpm_err) |
| 653 | return NULL; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 654 | dst_reg = access_hreg(tpm, dst_spec, |
| 655 | (opcode & 0x40) ? HREG_RDWR : HREG_WR); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 656 | if (hre_err || hre_tpm_err) |
| 657 | return NULL; |
| 658 | |
| 659 | switch (opcode) { |
| 660 | case HRE_NOP: |
| 661 | goto end; |
| 662 | case HRE_CHECK0: |
| 663 | if (src_reg) { |
| 664 | for (i = 0; i < 20; ++i) { |
| 665 | if (src_reg->digest[i]) |
| 666 | return NULL; |
| 667 | } |
| 668 | } |
| 669 | break; |
| 670 | case HRE_LOAD: |
| 671 | bin_func = memcpy; |
| 672 | goto do_bin_func; |
| 673 | case HRE_XOR: |
| 674 | bin_func = compute_xor; |
| 675 | goto do_bin_func; |
| 676 | case HRE_AND: |
| 677 | bin_func = compute_and; |
| 678 | goto do_bin_func; |
| 679 | case HRE_OR: |
| 680 | bin_func = compute_or; |
| 681 | goto do_bin_func; |
| 682 | case HRE_EXTEND: |
| 683 | bin_func = compute_extend; |
| 684 | do_bin_func: |
| 685 | if (!dst_reg) |
| 686 | return NULL; |
| 687 | if (src_reg) { |
| 688 | src_buf = src_reg->digest; |
| 689 | } else { |
| 690 | if (!data_size) { |
| 691 | memset(buf, 0, 20); |
| 692 | src_buf = buf; |
| 693 | } else if (data_size == 1) { |
| 694 | memset(buf, *data, 20); |
| 695 | src_buf = buf; |
| 696 | } else if (data_size >= 20) { |
| 697 | src_buf = data; |
| 698 | } else { |
| 699 | src_buf = buf; |
| 700 | for (ptr = (uint8_t *)src_buf, i = 20; i > 0; |
| 701 | i -= data_size, ptr += data_size) |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 702 | memcpy(ptr, data, |
| 703 | min_t(size_t, i, data_size)); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | bin_func(dst_reg->digest, src_buf, 20); |
| 707 | dst_reg->valid = true; |
| 708 | dst_modified = true; |
| 709 | break; |
| 710 | case HRE_LOADKEY: |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 711 | if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size)) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 712 | return NULL; |
| 713 | break; |
| 714 | default: |
| 715 | return NULL; |
| 716 | } |
| 717 | |
| 718 | if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 719 | hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec), |
| 720 | dst_reg->digest, dst_reg->digest); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 721 | if (hre_tpm_err) { |
| 722 | hre_err = HRE_E_TPM_FAILURE; |
| 723 | return NULL; |
| 724 | } |
| 725 | } |
| 726 | end: |
| 727 | *ip += 4; |
| 728 | *code_size -= 4; |
| 729 | if (opcode & 0x80) { |
| 730 | *ip += data_size; |
| 731 | *code_size -= data_size; |
| 732 | } |
| 733 | |
| 734 | return *ip; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * @brief runs a program on the hash register engine. |
| 739 | * @param code pointer to the (HRE) code. |
| 740 | * @param code_size size of the code (in bytes). |
| 741 | * @return 0 on success, != 0 on failure. |
| 742 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 743 | static int hre_run_program(struct udevice *tpm, const uint8_t *code, |
| 744 | size_t code_size) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 745 | { |
| 746 | size_t code_left; |
| 747 | const uint8_t *ip = code; |
| 748 | |
| 749 | code_left = code_size; |
| 750 | hre_tpm_err = 0; |
| 751 | hre_err = HRE_E_OK; |
| 752 | while (code_left > 0) |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 753 | if (!hre_execute_op(tpm, &ip, &code_left)) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 754 | return -1; |
| 755 | |
| 756 | return hre_err; |
| 757 | } |
| 758 | |
| 759 | static int check_hmac(struct key_program *hmac, |
| 760 | const uint8_t *data, size_t data_size) |
| 761 | { |
| 762 | uint8_t key[20], computed_hmac[20]; |
| 763 | uint32_t type; |
| 764 | |
| 765 | type = get_unaligned_be32(hmac->code); |
| 766 | if (type != 0) |
| 767 | return 1; |
| 768 | memset(key, 0, sizeof(key)); |
| 769 | compute_extend(key, pcr_hregs[1].digest, 20); |
| 770 | compute_extend(key, pcr_hregs[2].digest, 20); |
| 771 | compute_extend(key, pcr_hregs[3].digest, 20); |
| 772 | compute_extend(key, pcr_hregs[4].digest, 20); |
| 773 | |
| 774 | sha1_hmac(key, sizeof(key), data, data_size, computed_hmac); |
| 775 | |
| 776 | return memcmp(computed_hmac, hmac->code + 4, 20); |
| 777 | } |
| 778 | |
| 779 | static int verify_program(struct key_program *prg) |
| 780 | { |
| 781 | uint32_t crc; |
| 782 | crc = crc32(0, prg->code, prg->code_size); |
| 783 | |
| 784 | if (crc != prg->code_crc) { |
| 785 | printf("HRC crc mismatch: %08x != %08x\n", |
| 786 | crc, prg->code_crc); |
| 787 | return 1; |
| 788 | } |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE) |
| 793 | static struct key_program *load_sd_key_program(void) |
| 794 | { |
| 795 | u32 code_len, code_offset; |
| 796 | struct mmc *mmc; |
| 797 | u8 buf[128]; |
| 798 | struct key_program *result = NULL, *hmac = NULL; |
| 799 | struct key_program header; |
| 800 | |
| 801 | mmc = find_mmc_device(0); |
| 802 | if (!mmc) |
| 803 | return NULL; |
| 804 | mmc_init(mmc); |
| 805 | |
| 806 | if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0) |
| 807 | goto failure; |
| 808 | |
| 809 | code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS); |
| 810 | code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS); |
| 811 | |
| 812 | code_offset += code_len; |
| 813 | /* TODO: the following needs to be the size of the 2nd stage env */ |
| 814 | code_offset += CONFIG_ENV_SIZE; |
| 815 | |
| 816 | if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0) |
| 817 | goto failure; |
| 818 | |
| 819 | header.magic = get_unaligned_be32(buf); |
| 820 | header.code_crc = get_unaligned_be32(buf + 4); |
| 821 | header.code_size = get_unaligned_be32(buf + 8); |
| 822 | |
| 823 | if (header.magic != MAGIC_KEY_PROGRAM) |
| 824 | goto failure; |
| 825 | |
| 826 | result = malloc(sizeof(struct key_program) + header.code_size); |
| 827 | if (!result) |
| 828 | goto failure; |
| 829 | *result = header; |
| 830 | |
| 831 | printf("load key program chunk from SD card (%u bytes) ", |
| 832 | header.code_size); |
| 833 | code_offset += 12; |
| 834 | if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size) |
| 835 | < 0) |
| 836 | goto failure; |
| 837 | code_offset += header.code_size; |
| 838 | puts("\n"); |
| 839 | |
| 840 | if (verify_program(result)) |
| 841 | goto failure; |
| 842 | |
| 843 | if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0) |
| 844 | goto failure; |
| 845 | |
| 846 | header.magic = get_unaligned_be32(buf); |
| 847 | header.code_crc = get_unaligned_be32(buf + 4); |
| 848 | header.code_size = get_unaligned_be32(buf + 8); |
| 849 | |
| 850 | if (header.magic == MAGIC_HMAC) { |
| 851 | puts("check integrity\n"); |
| 852 | hmac = malloc(sizeof(struct key_program) + header.code_size); |
| 853 | if (!hmac) |
| 854 | goto failure; |
| 855 | *hmac = header; |
| 856 | code_offset += 12; |
| 857 | if (ccdm_mmc_read(mmc, code_offset, hmac->code, |
| 858 | hmac->code_size) < 0) |
| 859 | goto failure; |
| 860 | if (verify_program(hmac)) |
| 861 | goto failure; |
| 862 | if (check_hmac(hmac, result->code, result->code_size)) { |
| 863 | puts("key program integrity could not be verified\n"); |
| 864 | goto failure; |
| 865 | } |
| 866 | puts("key program verified\n"); |
| 867 | } |
| 868 | |
| 869 | goto end; |
| 870 | failure: |
| 871 | if (result) |
| 872 | free(result); |
| 873 | result = NULL; |
| 874 | end: |
| 875 | if (hmac) |
| 876 | free(hmac); |
| 877 | |
| 878 | return result; |
| 879 | } |
| 880 | #endif |
| 881 | |
| 882 | #ifdef CCDM_SECOND_STAGE |
| 883 | /** |
| 884 | * @brief load a key program from file system. |
| 885 | * @param ifname interface of the file system |
| 886 | * @param dev_part_str device part of the file system |
| 887 | * @param fs_type tyep of the file system |
| 888 | * @param path path of the file to load. |
| 889 | * @return the loaded structure or NULL on failure. |
| 890 | */ |
| 891 | static struct key_program *load_key_chunk(const char *ifname, |
| 892 | const char *dev_part_str, int fs_type, |
| 893 | const char *path) |
| 894 | { |
| 895 | struct key_program *result = NULL; |
| 896 | struct key_program header; |
| 897 | uint32_t crc; |
| 898 | uint8_t buf[12]; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 899 | loff_t i; |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 900 | |
| 901 | if (fs_set_blk_dev(ifname, dev_part_str, fs_type)) |
| 902 | goto failure; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 903 | if (fs_read(path, (ulong)buf, 0, 12, &i) < 0) |
| 904 | goto failure; |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 905 | if (i < 12) |
| 906 | goto failure; |
| 907 | header.magic = get_unaligned_be32(buf); |
| 908 | header.code_crc = get_unaligned_be32(buf + 4); |
| 909 | header.code_size = get_unaligned_be32(buf + 8); |
| 910 | |
| 911 | if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM) |
| 912 | goto failure; |
| 913 | |
| 914 | result = malloc(sizeof(struct key_program) + header.code_size); |
| 915 | if (!result) |
| 916 | goto failure; |
| 917 | if (fs_set_blk_dev(ifname, dev_part_str, fs_type)) |
| 918 | goto failure; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 919 | if (fs_read(path, (ulong)result, 0, |
| 920 | sizeof(struct key_program) + header.code_size, &i) < 0) |
| 921 | goto failure; |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 922 | if (i <= 0) |
| 923 | goto failure; |
| 924 | *result = header; |
| 925 | |
| 926 | crc = crc32(0, result->code, result->code_size); |
| 927 | |
| 928 | if (crc != result->code_crc) { |
| 929 | printf("%s: HRC crc mismatch: %08x != %08x\n", |
| 930 | path, crc, result->code_crc); |
| 931 | goto failure; |
| 932 | } |
| 933 | goto end; |
| 934 | failure: |
| 935 | if (result) { |
| 936 | free(result); |
| 937 | result = NULL; |
| 938 | } |
| 939 | end: |
| 940 | return result; |
| 941 | } |
| 942 | #endif |
| 943 | |
| 944 | #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE) |
Tom Rini | 910c793 | 2017-06-16 13:06:26 -0400 | [diff] [blame] | 945 | static const uint8_t prg_stage1_prepare[] = { |
| 946 | 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */ |
| 947 | 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */ |
| 948 | 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */ |
| 949 | 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */ |
| 950 | 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */ |
| 951 | 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */ |
| 952 | 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */ |
| 953 | 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */ |
| 954 | 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */ |
| 955 | 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */ |
| 956 | }; |
| 957 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 958 | static int first_stage_actions(struct udevice *tpm) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 959 | { |
| 960 | int result = 0; |
| 961 | struct key_program *sd_prg = NULL; |
| 962 | |
| 963 | puts("CCDM S1: start actions\n"); |
| 964 | #ifndef CCDM_SECOND_STAGE |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 965 | if (tpm_continue_self_test(tpm)) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 966 | goto failure; |
| 967 | #else |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 968 | tpm_continue_self_test(tpm); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 969 | #endif |
| 970 | mdelay(37); |
| 971 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 972 | if (hre_run_program(tpm, prg_stage1_prepare, |
| 973 | sizeof(prg_stage1_prepare))) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 974 | goto failure; |
| 975 | |
| 976 | sd_prg = load_sd_key_program(); |
| 977 | if (sd_prg) { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 978 | if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size)) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 979 | goto failure; |
| 980 | puts("SD code run successfully\n"); |
| 981 | } else { |
| 982 | puts("no key program found on SD\n"); |
| 983 | goto failure; |
| 984 | } |
| 985 | goto end; |
| 986 | failure: |
| 987 | result = 1; |
| 988 | end: |
| 989 | if (sd_prg) |
| 990 | free(sd_prg); |
| 991 | printf("CCDM S1: actions done (%d)\n", result); |
| 992 | return result; |
| 993 | } |
| 994 | #endif |
| 995 | |
| 996 | #ifdef CCDM_FIRST_STAGE |
| 997 | static int first_stage_init(void) |
| 998 | { |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 999 | struct udevice *tpm; |
| 1000 | int ret; |
| 1001 | |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1002 | puts("CCDM S1\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1003 | ret = get_tpm(&tpm); |
| 1004 | if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR)) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1005 | return 1; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1006 | ret = first_stage_actions(tpm); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1007 | #ifndef CCDM_SECOND_STAGE |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1008 | if (!ret) { |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1009 | if (bl2_entry) |
| 1010 | (*bl2_entry)(); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1011 | ret = 1; |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1012 | } |
| 1013 | #endif |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1014 | return ret; |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1015 | } |
| 1016 | #endif |
| 1017 | |
| 1018 | #ifdef CCDM_SECOND_STAGE |
Tom Rini | 09868d4 | 2017-05-08 22:14:26 -0400 | [diff] [blame] | 1019 | static const uint8_t prg_stage2_prepare[] = { |
| 1020 | 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */ |
| 1021 | 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */ |
| 1022 | 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */ |
| 1023 | 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */ |
| 1024 | 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */ |
| 1025 | }; |
| 1026 | |
| 1027 | static const uint8_t prg_stage2_success[] = { |
| 1028 | 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */ |
| 1029 | 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */ |
| 1030 | 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */ |
| 1031 | 0xe4, 0xd2, 0x81, 0xe0, /* data */ |
| 1032 | }; |
| 1033 | |
| 1034 | static const uint8_t prg_stage_fail[] = { |
| 1035 | 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */ |
| 1036 | 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */ |
| 1037 | 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */ |
| 1038 | 0xea, 0xdf, 0x14, 0x4b, /* data */ |
| 1039 | 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */ |
| 1040 | 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */ |
| 1041 | }; |
| 1042 | |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1043 | static int second_stage_init(void) |
| 1044 | { |
| 1045 | static const char mac_suffix[] = ".mac"; |
| 1046 | bool did_first_stage_run = true; |
| 1047 | int result = 0; |
| 1048 | char *cptr, *mmcdev = NULL; |
| 1049 | struct key_program *hmac_blob = NULL; |
| 1050 | const char *image_path = "/ccdm.itb"; |
| 1051 | char *mac_path = NULL; |
| 1052 | ulong image_addr; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1053 | loff_t image_size; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1054 | struct udevice *tpm; |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1055 | uint32_t err; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1056 | int ret; |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1057 | |
| 1058 | printf("CCDM S2\n"); |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1059 | ret = get_tpm(&tpm); |
| 1060 | if (ret || tpm_init(tpm)) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1061 | return 1; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1062 | err = tpm_startup(tpm, TPM_ST_CLEAR); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1063 | if (err != TPM_INVALID_POSTINIT) |
| 1064 | did_first_stage_run = false; |
| 1065 | |
| 1066 | #ifdef CCDM_AUTO_FIRST_STAGE |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1067 | if (!did_first_stage_run && first_stage_actions(tpm)) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1068 | goto failure; |
| 1069 | #else |
| 1070 | if (!did_first_stage_run) |
| 1071 | goto failure; |
| 1072 | #endif |
| 1073 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1074 | if (hre_run_program(tpm, prg_stage2_prepare, |
| 1075 | sizeof(prg_stage2_prepare))) |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1076 | goto failure; |
| 1077 | |
| 1078 | /* run "prepboot" from env to get "mmcdev" set */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1079 | cptr = env_get("prepboot"); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1080 | if (cptr && !run_command(cptr, 0)) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1081 | mmcdev = env_get("mmcdev"); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1082 | if (!mmcdev) |
| 1083 | goto failure; |
| 1084 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 1085 | cptr = env_get("ramdiskimage"); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1086 | if (cptr) |
| 1087 | image_path = cptr; |
| 1088 | |
| 1089 | mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1); |
| 1090 | if (mac_path == NULL) |
| 1091 | goto failure; |
| 1092 | strcpy(mac_path, image_path); |
| 1093 | strcat(mac_path, mac_suffix); |
| 1094 | |
| 1095 | /* read image from mmcdev (ccdm.itb) */ |
| 1096 | image_addr = (ulong)get_image_location(); |
| 1097 | if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT)) |
| 1098 | goto failure; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1099 | if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0) |
| 1100 | goto failure; |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1101 | if (image_size <= 0) |
| 1102 | goto failure; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1103 | printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1104 | |
| 1105 | hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path); |
| 1106 | if (!hmac_blob) { |
| 1107 | puts("failed to load mac file\n"); |
| 1108 | goto failure; |
| 1109 | } |
| 1110 | if (verify_program(hmac_blob)) { |
| 1111 | puts("corrupted mac file\n"); |
| 1112 | goto failure; |
| 1113 | } |
| 1114 | if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) { |
| 1115 | puts("image integrity could not be verified\n"); |
| 1116 | goto failure; |
| 1117 | } |
| 1118 | puts("CCDM image OK\n"); |
| 1119 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1120 | hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success)); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1121 | |
| 1122 | goto end; |
| 1123 | failure: |
| 1124 | result = 1; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 1125 | hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail)); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1126 | end: |
| 1127 | if (hmac_blob) |
| 1128 | free(hmac_blob); |
| 1129 | if (mac_path) |
| 1130 | free(mac_path); |
| 1131 | |
| 1132 | return result; |
| 1133 | } |
| 1134 | #endif |
| 1135 | |
| 1136 | int show_self_hash(void) |
| 1137 | { |
| 1138 | struct h_reg *hash_ptr; |
| 1139 | #ifdef CCDM_SECOND_STAGE |
| 1140 | struct h_reg hash; |
| 1141 | |
| 1142 | hash_ptr = &hash; |
| 1143 | if (compute_self_hash(hash_ptr)) |
| 1144 | return 1; |
| 1145 | #else |
| 1146 | hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH]; |
| 1147 | #endif |
| 1148 | puts("self hash: "); |
| 1149 | if (hash_ptr && hash_ptr->valid) |
| 1150 | print_buffer(0, hash_ptr->digest, 1, 20, 20); |
| 1151 | else |
| 1152 | puts("INVALID\n"); |
| 1153 | |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * @brief let the system hang. |
| 1159 | * |
| 1160 | * Called on error. |
| 1161 | * Will stop the boot process; display a message and signal the error condition |
| 1162 | * by blinking the "status" and the "finder" LED of the controller board. |
| 1163 | * |
| 1164 | * @note the develop version runs the blink cycle 2 times and then returns. |
| 1165 | * The release version never returns. |
| 1166 | */ |
| 1167 | static void ccdm_hang(void) |
| 1168 | { |
| 1169 | static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */ |
| 1170 | static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */ |
| 1171 | u64 f, s; |
| 1172 | int i; |
| 1173 | #ifdef CCDM_DEVELOP |
| 1174 | int j; |
| 1175 | #endif |
| 1176 | |
Dirk Eibach | e674bf1 | 2014-07-03 09:28:16 +0200 | [diff] [blame] | 1177 | I2C_SET_BUS(I2C_SOC_0); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1178 | pca9698_direction_output(0x22, 0, 0); /* Finder */ |
| 1179 | pca9698_direction_output(0x22, 4, 0); /* Status */ |
| 1180 | |
| 1181 | puts("### ERROR ### Please RESET the board ###\n"); |
| 1182 | bootstage_error(BOOTSTAGE_ID_NEED_RESET); |
| 1183 | #ifdef CCDM_DEVELOP |
| 1184 | puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n"); |
| 1185 | puts("** but we continue since this is a DEVELOP version **\n"); |
| 1186 | puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n"); |
| 1187 | for (j = 2; j-- > 0;) { |
| 1188 | putc('#'); |
| 1189 | #else |
| 1190 | for (;;) { |
| 1191 | #endif |
| 1192 | f = f0; |
| 1193 | s = s0; |
| 1194 | for (i = 54; i-- > 0;) { |
| 1195 | pca9698_set_value(0x22, 0, !(f & 1)); |
| 1196 | pca9698_set_value(0x22, 4, (s & 1)); |
| 1197 | f >>= 1; |
| 1198 | s >>= 1; |
| 1199 | mdelay(120); |
| 1200 | } |
| 1201 | } |
| 1202 | puts("\ncontinue...\n"); |
| 1203 | } |
| 1204 | |
| 1205 | int startup_ccdm_id_module(void) |
| 1206 | { |
| 1207 | int result = 0; |
| 1208 | unsigned int orig_i2c_bus; |
| 1209 | |
Dirk Eibach | e674bf1 | 2014-07-03 09:28:16 +0200 | [diff] [blame] | 1210 | orig_i2c_bus = i2c_get_bus_num(); |
| 1211 | i2c_set_bus_num(I2C_SOC_1); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1212 | |
| 1213 | /* goto end; */ |
| 1214 | |
| 1215 | #ifdef CCDM_DEVELOP |
| 1216 | show_self_hash(); |
| 1217 | #endif |
| 1218 | #ifdef CCDM_FIRST_STAGE |
| 1219 | result = first_stage_init(); |
| 1220 | if (result) { |
| 1221 | puts("1st stage init failed\n"); |
| 1222 | goto failure; |
| 1223 | } |
| 1224 | #endif |
| 1225 | #ifdef CCDM_SECOND_STAGE |
| 1226 | result = second_stage_init(); |
| 1227 | if (result) { |
| 1228 | puts("2nd stage init failed\n"); |
| 1229 | goto failure; |
| 1230 | } |
| 1231 | #endif |
| 1232 | |
| 1233 | goto end; |
| 1234 | failure: |
| 1235 | result = 1; |
| 1236 | end: |
Dirk Eibach | e674bf1 | 2014-07-03 09:28:16 +0200 | [diff] [blame] | 1237 | i2c_set_bus_num(orig_i2c_bus); |
Dirk Eibach | 762d3df | 2013-06-26 15:55:17 +0200 | [diff] [blame] | 1238 | if (result) |
| 1239 | ccdm_hang(); |
| 1240 | |
| 1241 | return result; |
| 1242 | } |