1 // SPDX-License-Identifier: GPL-2.0+
4 * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc
7 /* TODO: some more #ifdef's to avoid unneeded code for stage 1 / stage 2 */
20 #include <u-boot/sha1.h>
21 #include <asm/byteorder.h>
22 #include <asm/unaligned.h>
25 #undef CCDM_FIRST_STAGE
26 #undef CCDM_SECOND_STAGE
27 #undef CCDM_AUTO_FIRST_STAGE
33 #ifdef CONFIG_TRAILBLAZER
34 #define CCDM_FIRST_STAGE
35 #undef CCDM_SECOND_STAGE
37 #undef CCDM_FIRST_STAGE
38 #define CCDM_SECOND_STAGE
41 #if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
42 !defined(CCCM_FIRST_STAGE)
43 #define CCDM_AUTO_FIRST_STAGE
46 /* CCDM specific contants */
49 NV_COMMON_DATA_INDEX = 0x40000001,
50 /* magics for key blob chains */
51 MAGIC_KEY_PROGRAM = 0x68726500,
52 MAGIC_HMAC = 0x68616300,
53 MAGIC_END_OF_CHAIN = 0x00000000,
55 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
60 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
61 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
62 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
63 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
64 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
92 /* register constants */
94 FIX_HREG_DEVICE_ID_HASH = 0,
95 FIX_HREG_SELF_HASH = 1,
96 FIX_HREG_STAGE2_HASH = 2,
104 /* opcodes w/o data */
108 /* opcodes w/o data, w/ sync dst */
109 /* opcodes w/ data */
111 /* opcodes w/data, w/sync dst */
126 static uint64_t device_id;
127 static uint64_t device_cl;
128 static uint64_t device_type;
130 static uint32_t platform_key_handle;
132 static void(*bl2_entry)(void);
134 static struct h_reg pcr_hregs[24];
135 static struct h_reg fix_hregs[COUNT_FIX_HREGS];
136 static struct h_reg var_hregs[8];
137 static uint32_t hre_tpm_err;
138 static int hre_err = HRE_E_OK;
140 #define IS_PCR_HREG(spec) ((spec) & 0x20)
141 #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
142 #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
143 #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
145 static int get_tpm(struct udevice **devp)
149 rc = uclass_first_device_err(UCLASS_TPM, devp);
151 printf("Could not find TPM (ret=%d)\n", rc);
152 return CMD_RET_FAILURE;
158 static const uint8_t vendor[] = "Guntermann & Drunck";
161 * @brief read a bunch of data from MMC into memory.
163 * @param mmc pointer to the mmc structure to use.
164 * @param src offset where the data starts on MMC/SD device (in bytes).
165 * @param dst pointer to the location where the read data should be stored.
166 * @param size number of bytes to read from the MMC/SD device.
167 * @return number of bytes read or -1 on error.
169 static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
173 ulong block_no, n, cnt;
179 blk_len = mmc->read_bl_len;
180 tmp_buf = malloc(blk_len);
183 block_no = src / blk_len;
187 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
191 result = min(size, (int)(blk_len - ofs));
192 memcpy(dst, tmp_buf + ofs, result);
196 cnt = size / blk_len;
198 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
202 size -= cnt * blk_len;
203 result += cnt * blk_len;
204 dst += cnt * blk_len;
208 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
212 memcpy(dst, tmp_buf, size);
225 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
227 * @return pointer to the location for/of the 2nd stage bootloader
229 static u8 *get_2nd_stage_bl_location(ulong target_addr)
232 #ifdef CCDM_SECOND_STAGE
233 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
241 #ifdef CCDM_SECOND_STAGE
243 * @brief returns a location where the image can be(/ is) placed.
245 * @return pointer to the location for/of the image
247 static u8 *get_image_location(void)
250 /* TODO use other area? */
251 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
257 * @brief get the size of a given (TPM) NV area
258 * @param index NV index of the area to get size for
259 * @param size pointer to the size
260 * @return 0 on success, != 0 on error
262 static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
269 err = tpm_get_capability(tpm, TPM_CAP_NV_INDEX, index,
272 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
277 /* skip tag and nvIndex */
279 /* skip 2 pcr info fields */
280 v16 = get_unaligned_be16(ptr);
281 ptr += 2 + v16 + 1 + 20;
282 v16 = get_unaligned_be16(ptr);
283 ptr += 2 + v16 + 1 + 20;
284 /* skip permission and flags */
287 *size = get_unaligned_be32(ptr);
292 * @brief search for a key by usage auth and pub key hash.
293 * @param auth usage auth of the key to search for
294 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
295 * @param[out] handle the handle of the key iff found
296 * @return 0 if key was found in TPM; != 0 if not.
298 static int find_key(struct udevice *tpm, const uint8_t auth[20],
299 const uint8_t pubkey_digest[20], uint32_t *handle)
302 uint32_t key_handles[10];
310 /* fetch list of already loaded keys in the TPM */
311 err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
315 key_count = get_unaligned_be16(buf);
317 for (i = 0; i < key_count; ++i, ptr += 4)
318 key_handles[i] = get_unaligned_be32(ptr);
320 /* now search a(/ the) key which we can access with the given auth */
321 for (i = 0; i < key_count; ++i) {
322 buf_len = sizeof(buf);
323 err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
325 if (err && err != TPM_AUTHFAIL)
329 sha1_csum(buf, buf_len, digest);
330 if (!memcmp(digest, pubkey_digest, 20)) {
331 *handle = key_handles[i];
339 * @brief read CCDM common data from TPM NV
340 * @return 0 if CCDM common data was found and read, !=0 if something failed.
342 static int read_common_data(struct udevice *tpm)
349 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
350 size < NV_COMMON_DATA_MIN_SIZE)
352 err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX,
353 buf, min(sizeof(buf), size));
355 printf("tpm_nv_read_value() failed: %u\n", err);
359 device_id = get_unaligned_be64(buf);
360 device_cl = get_unaligned_be64(buf + 8);
361 device_type = get_unaligned_be64(buf + 16);
364 sha1_update(&ctx, buf, 24);
365 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
366 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
368 platform_key_handle = get_unaligned_be32(buf + 24);
374 * @brief compute hash of bootloader itself.
375 * @param[out] dst hash register where the hash should be stored
376 * @return 0 on success, != 0 on failure.
378 * @note MUST be called at a time where the boot loader is accessible at the
379 * configured location (; so take care when code is reallocated).
381 static int compute_self_hash(struct h_reg *dst)
383 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
384 CONFIG_SYS_MONITOR_LEN, dst->digest);
389 int ccdm_compute_self_hash(void)
391 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
392 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
397 * @brief compute the hash of the 2nd stage boot loader (on SD card)
398 * @param[out] dst hash register to store the computed hash
399 * @return 0 on success, != 0 on failure
401 * Determines the size and location of the 2nd stage boot loader on SD card,
402 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
403 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
404 * the desired memory location and the variable @a bl2_entry is set.
406 * @note This sets the variable @a bl2_entry to the entry point when the
407 * 2nd stage boot loader is loaded at its configured memory location.
409 static int compute_second_stage_hash(struct h_reg *dst)
412 u32 code_len, code_offset, target_addr, exec_entry;
414 u8 *load_addr = NULL;
417 mmc = find_mmc_device(0);
422 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
425 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
426 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
427 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
428 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
430 load_addr = get_2nd_stage_bl_location(target_addr);
431 if (load_addr == (u8 *)target_addr)
432 bl2_entry = (void(*)(void))exec_entry;
434 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
437 sha1_csum(load_addr, code_len, dst->digest);
449 * @brief get pointer to hash register by specification
450 * @param spec specification of a hash register
451 * @return pointer to hash register or NULL if @a spec does not qualify a
452 * valid hash register; NULL else.
454 static struct h_reg *get_hreg(uint8_t spec)
458 idx = HREG_IDX(spec);
459 if (IS_FIX_HREG(spec)) {
460 if (idx < ARRAY_SIZE(fix_hregs))
461 return fix_hregs + idx;
462 hre_err = HRE_E_INVALID_HREG;
463 } else if (IS_PCR_HREG(spec)) {
464 if (idx < ARRAY_SIZE(pcr_hregs))
465 return pcr_hregs + idx;
466 hre_err = HRE_E_INVALID_HREG;
467 } else if (IS_VAR_HREG(spec)) {
468 if (idx < ARRAY_SIZE(var_hregs))
469 return var_hregs + idx;
470 hre_err = HRE_E_INVALID_HREG;
476 * @brief get pointer of a hash register by specification and usage.
477 * @param spec specification of a hash register
478 * @param mode access mode (read or write or read/write)
479 * @return pointer to hash register if found and valid; NULL else.
481 * This func uses @a get_reg() to determine the hash register for a given spec.
482 * If a register is found it is validated according to the desired access mode.
483 * The value of automatic registers (PCR register and fixed registers) is
484 * loaded or computed on read access.
486 static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
487 enum access_mode mode)
489 struct h_reg *result;
491 result = get_hreg(spec);
495 if (mode & HREG_WR) {
496 if (IS_FIX_HREG(spec)) {
497 hre_err = HRE_E_INVALID_HREG;
501 if (mode & HREG_RD) {
502 if (!result->valid) {
503 if (IS_PCR_HREG(spec)) {
504 hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec),
506 result->valid = (hre_tpm_err == TPM_SUCCESS);
507 } else if (IS_FIX_HREG(spec)) {
508 switch (HREG_IDX(spec)) {
509 case FIX_HREG_DEVICE_ID_HASH:
510 read_common_data(tpm);
512 case FIX_HREG_SELF_HASH:
513 ccdm_compute_self_hash();
515 case FIX_HREG_STAGE2_HASH:
516 compute_second_stage_hash(result);
518 case FIX_HREG_VENDOR:
519 memcpy(result->digest, vendor, 20);
520 result->valid = true;
524 result->valid = true;
527 if (!result->valid) {
528 hre_err = HRE_E_INVALID_HREG;
536 static void *compute_and(void *_dst, const void *_src, size_t n)
539 const uint8_t *src = _src;
542 for (i = n; i-- > 0; )
548 static void *compute_or(void *_dst, const void *_src, size_t n)
551 const uint8_t *src = _src;
554 for (i = n; i-- > 0; )
560 static void *compute_xor(void *_dst, const void *_src, size_t n)
563 const uint8_t *src = _src;
566 for (i = n; i-- > 0; )
572 static void *compute_extend(void *_dst, const void *_src, size_t n)
578 sha1_update(&ctx, _dst, n);
579 sha1_update(&ctx, _src, n);
580 sha1_finish(&ctx, digest);
581 memcpy(_dst, digest, min(n, sizeof(digest)));
586 static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
587 struct h_reg *dst_reg, const void *key,
590 uint32_t parent_handle;
593 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
595 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
597 hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size,
598 src_reg->digest, &key_handle);
600 hre_err = HRE_E_TPM_FAILURE;
603 /* TODO remember key handle somehow? */
609 * @brief executes the next opcode on the hash register engine.
610 * @param[in,out] ip pointer to the opcode (instruction pointer)
611 * @param[in,out] code_size (remaining) size of the code
612 * @return new instruction pointer on success, NULL on error.
614 static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
617 bool dst_modified = false;
623 struct h_reg *src_reg, *dst_reg;
625 const uint8_t *src_buf, *data;
628 void * (*bin_func)(void *, const void *, size_t);
633 ins = get_unaligned_be32(*ip);
636 src_spec = (ins >> 18) & 0x3f;
637 dst_spec = (ins >> 12) & 0x3f;
638 data_size = (ins & 0x7ff);
640 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
641 opcode, src_spec, dst_spec, data_size);
643 if ((opcode & 0x80) && (data_size + 4) > *code_size)
646 src_reg = access_hreg(tpm, src_spec, HREG_RD);
647 if (hre_err || hre_tpm_err)
649 dst_reg = access_hreg(tpm, dst_spec,
650 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
651 if (hre_err || hre_tpm_err)
659 for (i = 0; i < 20; ++i) {
660 if (src_reg->digest[i])
669 bin_func = compute_xor;
672 bin_func = compute_and;
675 bin_func = compute_or;
678 bin_func = compute_extend;
683 src_buf = src_reg->digest;
688 } else if (data_size == 1) {
689 memset(buf, *data, 20);
691 } else if (data_size >= 20) {
695 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
696 i -= data_size, ptr += data_size)
698 min_t(size_t, i, data_size));
701 bin_func(dst_reg->digest, src_buf, 20);
702 dst_reg->valid = true;
706 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
713 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
714 hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec),
715 dst_reg->digest, dst_reg->digest);
717 hre_err = HRE_E_TPM_FAILURE;
726 *code_size -= data_size;
733 * @brief runs a program on the hash register engine.
734 * @param code pointer to the (HRE) code.
735 * @param code_size size of the code (in bytes).
736 * @return 0 on success, != 0 on failure.
738 static int hre_run_program(struct udevice *tpm, const uint8_t *code,
742 const uint8_t *ip = code;
744 code_left = code_size;
747 while (code_left > 0)
748 if (!hre_execute_op(tpm, &ip, &code_left))
754 static int check_hmac(struct key_program *hmac,
755 const uint8_t *data, size_t data_size)
757 uint8_t key[20], computed_hmac[20];
760 type = get_unaligned_be32(hmac->code);
763 memset(key, 0, sizeof(key));
764 compute_extend(key, pcr_hregs[1].digest, 20);
765 compute_extend(key, pcr_hregs[2].digest, 20);
766 compute_extend(key, pcr_hregs[3].digest, 20);
767 compute_extend(key, pcr_hregs[4].digest, 20);
769 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
771 return memcmp(computed_hmac, hmac->code + 4, 20);
774 static int verify_program(struct key_program *prg)
777 crc = crc32(0, prg->code, prg->code_size);
779 if (crc != prg->code_crc) {
780 printf("HRC crc mismatch: %08x != %08x\n",
787 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
788 static struct key_program *load_sd_key_program(void)
790 u32 code_len, code_offset;
793 struct key_program *result = NULL, *hmac = NULL;
794 struct key_program header;
796 mmc = find_mmc_device(0);
801 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
804 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
805 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
807 code_offset += code_len;
808 /* TODO: the following needs to be the size of the 2nd stage env */
809 code_offset += CONFIG_ENV_SIZE;
811 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
814 header.magic = get_unaligned_be32(buf);
815 header.code_crc = get_unaligned_be32(buf + 4);
816 header.code_size = get_unaligned_be32(buf + 8);
818 if (header.magic != MAGIC_KEY_PROGRAM)
821 result = malloc(sizeof(struct key_program) + header.code_size);
826 printf("load key program chunk from SD card (%u bytes) ",
829 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
832 code_offset += header.code_size;
835 if (verify_program(result))
838 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
841 header.magic = get_unaligned_be32(buf);
842 header.code_crc = get_unaligned_be32(buf + 4);
843 header.code_size = get_unaligned_be32(buf + 8);
845 if (header.magic == MAGIC_HMAC) {
846 puts("check integrity\n");
847 hmac = malloc(sizeof(struct key_program) + header.code_size);
852 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
853 hmac->code_size) < 0)
855 if (verify_program(hmac))
857 if (check_hmac(hmac, result->code, result->code_size)) {
858 puts("key program integrity could not be verified\n");
861 puts("key program verified\n");
877 #ifdef CCDM_SECOND_STAGE
879 * @brief load a key program from file system.
880 * @param ifname interface of the file system
881 * @param dev_part_str device part of the file system
882 * @param fs_type tyep of the file system
883 * @param path path of the file to load.
884 * @return the loaded structure or NULL on failure.
886 static struct key_program *load_key_chunk(const char *ifname,
887 const char *dev_part_str, int fs_type,
890 struct key_program *result = NULL;
891 struct key_program header;
896 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
898 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
902 header.magic = get_unaligned_be32(buf);
903 header.code_crc = get_unaligned_be32(buf + 4);
904 header.code_size = get_unaligned_be32(buf + 8);
906 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
909 result = malloc(sizeof(struct key_program) + header.code_size);
912 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
914 if (fs_read(path, (ulong)result, 0,
915 sizeof(struct key_program) + header.code_size, &i) < 0)
921 crc = crc32(0, result->code, result->code_size);
923 if (crc != result->code_crc) {
924 printf("%s: HRC crc mismatch: %08x != %08x\n",
925 path, crc, result->code_crc);
939 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
940 static const uint8_t prg_stage1_prepare[] = {
941 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
942 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
943 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
944 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
945 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
946 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
947 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
948 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
949 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
950 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
953 static int first_stage_actions(struct udevice *tpm)
956 struct key_program *sd_prg = NULL;
958 puts("CCDM S1: start actions\n");
959 #ifndef CCDM_SECOND_STAGE
960 if (tpm_continue_self_test(tpm))
963 tpm_continue_self_test(tpm);
967 if (hre_run_program(tpm, prg_stage1_prepare,
968 sizeof(prg_stage1_prepare)))
971 sd_prg = load_sd_key_program();
973 if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size))
975 puts("SD code run successfully\n");
977 puts("no key program found on SD\n");
986 printf("CCDM S1: actions done (%d)\n", result);
991 #ifdef CCDM_FIRST_STAGE
992 static int first_stage_init(void)
999 if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR))
1001 ret = first_stage_actions(tpm);
1002 #ifndef CCDM_SECOND_STAGE
1013 #ifdef CCDM_SECOND_STAGE
1014 static const uint8_t prg_stage2_prepare[] = {
1015 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
1016 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
1017 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
1018 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
1019 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
1022 static const uint8_t prg_stage2_success[] = {
1023 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
1024 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1025 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1026 0xe4, 0xd2, 0x81, 0xe0, /* data */
1029 static const uint8_t prg_stage_fail[] = {
1030 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1031 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1032 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1033 0xea, 0xdf, 0x14, 0x4b, /* data */
1034 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1035 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1038 static int second_stage_init(void)
1040 static const char mac_suffix[] = ".mac";
1041 bool did_first_stage_run = true;
1043 char *cptr, *mmcdev = NULL;
1044 struct key_program *hmac_blob = NULL;
1045 const char *image_path = "/ccdm.itb";
1046 char *mac_path = NULL;
1049 struct udevice *tpm;
1053 printf("CCDM S2\n");
1054 ret = get_tpm(&tpm);
1055 if (ret || tpm_init(tpm))
1057 err = tpm_startup(tpm, TPM_ST_CLEAR);
1058 if (err != TPM_INVALID_POSTINIT)
1059 did_first_stage_run = false;
1061 #ifdef CCDM_AUTO_FIRST_STAGE
1062 if (!did_first_stage_run && first_stage_actions(tpm))
1065 if (!did_first_stage_run)
1069 if (hre_run_program(tpm, prg_stage2_prepare,
1070 sizeof(prg_stage2_prepare)))
1073 /* run "prepboot" from env to get "mmcdev" set */
1074 cptr = env_get("prepboot");
1075 if (cptr && !run_command(cptr, 0))
1076 mmcdev = env_get("mmcdev");
1080 cptr = env_get("ramdiskimage");
1084 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1085 if (mac_path == NULL)
1087 strcpy(mac_path, image_path);
1088 strcat(mac_path, mac_suffix);
1090 /* read image from mmcdev (ccdm.itb) */
1091 image_addr = (ulong)get_image_location();
1092 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1094 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1096 if (image_size <= 0)
1098 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
1100 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1102 puts("failed to load mac file\n");
1105 if (verify_program(hmac_blob)) {
1106 puts("corrupted mac file\n");
1109 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1110 puts("image integrity could not be verified\n");
1113 puts("CCDM image OK\n");
1115 hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success));
1120 hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail));
1131 int show_self_hash(void)
1133 struct h_reg *hash_ptr;
1134 #ifdef CCDM_SECOND_STAGE
1138 if (compute_self_hash(hash_ptr))
1141 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1143 puts("self hash: ");
1144 if (hash_ptr && hash_ptr->valid)
1145 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1153 * @brief let the system hang.
1156 * Will stop the boot process; display a message and signal the error condition
1157 * by blinking the "status" and the "finder" LED of the controller board.
1159 * @note the develop version runs the blink cycle 2 times and then returns.
1160 * The release version never returns.
1162 static void ccdm_hang(void)
1164 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1165 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1172 I2C_SET_BUS(I2C_SOC_0);
1173 pca9698_direction_output(0x22, 0, 0); /* Finder */
1174 pca9698_direction_output(0x22, 4, 0); /* Status */
1176 puts("### ERROR ### Please RESET the board ###\n");
1177 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1179 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1180 puts("** but we continue since this is a DEVELOP version **\n");
1181 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1182 for (j = 2; j-- > 0;) {
1189 for (i = 54; i-- > 0;) {
1190 pca9698_set_value(0x22, 0, !(f & 1));
1191 pca9698_set_value(0x22, 4, (s & 1));
1197 puts("\ncontinue...\n");
1200 int startup_ccdm_id_module(void)
1203 unsigned int orig_i2c_bus;
1205 orig_i2c_bus = i2c_get_bus_num();
1206 i2c_set_bus_num(I2C_SOC_1);
1213 #ifdef CCDM_FIRST_STAGE
1214 result = first_stage_init();
1216 puts("1st stage init failed\n");
1220 #ifdef CCDM_SECOND_STAGE
1221 result = second_stage_init();
1223 puts("2nd stage init failed\n");
1232 i2c_set_bus_num(orig_i2c_bus);