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 */
21 #include <u-boot/sha1.h>
22 #include <asm/byteorder.h>
23 #include <asm/unaligned.h>
26 #undef CCDM_FIRST_STAGE
27 #undef CCDM_SECOND_STAGE
28 #undef CCDM_AUTO_FIRST_STAGE
34 #ifdef CONFIG_TRAILBLAZER
35 #define CCDM_FIRST_STAGE
36 #undef CCDM_SECOND_STAGE
38 #undef CCDM_FIRST_STAGE
39 #define CCDM_SECOND_STAGE
42 #if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
43 !defined(CCCM_FIRST_STAGE)
44 #define CCDM_AUTO_FIRST_STAGE
47 /* CCDM specific contants */
50 NV_COMMON_DATA_INDEX = 0x40000001,
51 /* magics for key blob chains */
52 MAGIC_KEY_PROGRAM = 0x68726500,
53 MAGIC_HMAC = 0x68616300,
54 MAGIC_END_OF_CHAIN = 0x00000000,
56 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
61 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
62 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
63 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
64 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
65 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
93 /* register constants */
95 FIX_HREG_DEVICE_ID_HASH = 0,
96 FIX_HREG_SELF_HASH = 1,
97 FIX_HREG_STAGE2_HASH = 2,
105 /* opcodes w/o data */
109 /* opcodes w/o data, w/ sync dst */
110 /* opcodes w/ data */
112 /* opcodes w/data, w/sync dst */
127 static uint64_t device_id;
128 static uint64_t device_cl;
129 static uint64_t device_type;
131 static uint32_t platform_key_handle;
133 static void(*bl2_entry)(void);
135 static struct h_reg pcr_hregs[24];
136 static struct h_reg fix_hregs[COUNT_FIX_HREGS];
137 static struct h_reg var_hregs[8];
138 static uint32_t hre_tpm_err;
139 static int hre_err = HRE_E_OK;
141 #define IS_PCR_HREG(spec) ((spec) & 0x20)
142 #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
143 #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
144 #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
146 static int get_tpm(struct udevice **devp)
150 rc = uclass_first_device_err(UCLASS_TPM, devp);
152 printf("Could not find TPM (ret=%d)\n", rc);
153 return CMD_RET_FAILURE;
159 static const uint8_t vendor[] = "Guntermann & Drunck";
162 * @brief read a bunch of data from MMC into memory.
164 * @param mmc pointer to the mmc structure to use.
165 * @param src offset where the data starts on MMC/SD device (in bytes).
166 * @param dst pointer to the location where the read data should be stored.
167 * @param size number of bytes to read from the MMC/SD device.
168 * @return number of bytes read or -1 on error.
170 static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
174 ulong block_no, n, cnt;
180 blk_len = mmc->read_bl_len;
181 tmp_buf = malloc(blk_len);
184 block_no = src / blk_len;
188 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
192 result = min(size, (int)(blk_len - ofs));
193 memcpy(dst, tmp_buf + ofs, result);
197 cnt = size / blk_len;
199 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
203 size -= cnt * blk_len;
204 result += cnt * blk_len;
205 dst += cnt * blk_len;
209 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
213 memcpy(dst, tmp_buf, size);
226 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
228 * @return pointer to the location for/of the 2nd stage bootloader
230 static u8 *get_2nd_stage_bl_location(ulong target_addr)
233 #ifdef CCDM_SECOND_STAGE
234 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
242 #ifdef CCDM_SECOND_STAGE
244 * @brief returns a location where the image can be(/ is) placed.
246 * @return pointer to the location for/of the image
248 static u8 *get_image_location(void)
251 /* TODO use other area? */
252 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
258 * @brief get the size of a given (TPM) NV area
259 * @param index NV index of the area to get size for
260 * @param size pointer to the size
261 * @return 0 on success, != 0 on error
263 static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
270 err = tpm_get_capability(tpm, TPM_CAP_NV_INDEX, index,
273 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
278 /* skip tag and nvIndex */
280 /* skip 2 pcr info fields */
281 v16 = get_unaligned_be16(ptr);
282 ptr += 2 + v16 + 1 + 20;
283 v16 = get_unaligned_be16(ptr);
284 ptr += 2 + v16 + 1 + 20;
285 /* skip permission and flags */
288 *size = get_unaligned_be32(ptr);
293 * @brief search for a key by usage auth and pub key hash.
294 * @param auth usage auth of the key to search for
295 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
296 * @param[out] handle the handle of the key iff found
297 * @return 0 if key was found in TPM; != 0 if not.
299 static int find_key(struct udevice *tpm, const uint8_t auth[20],
300 const uint8_t pubkey_digest[20], uint32_t *handle)
303 uint32_t key_handles[10];
311 /* fetch list of already loaded keys in the TPM */
312 err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
316 key_count = get_unaligned_be16(buf);
318 for (i = 0; i < key_count; ++i, ptr += 4)
319 key_handles[i] = get_unaligned_be32(ptr);
321 /* now search a(/ the) key which we can access with the given auth */
322 for (i = 0; i < key_count; ++i) {
323 buf_len = sizeof(buf);
324 err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
326 if (err && err != TPM_AUTHFAIL)
330 sha1_csum(buf, buf_len, digest);
331 if (!memcmp(digest, pubkey_digest, 20)) {
332 *handle = key_handles[i];
340 * @brief read CCDM common data from TPM NV
341 * @return 0 if CCDM common data was found and read, !=0 if something failed.
343 static int read_common_data(struct udevice *tpm)
350 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
351 size < NV_COMMON_DATA_MIN_SIZE)
353 err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX,
354 buf, min(sizeof(buf), size));
356 printf("tpm_nv_read_value() failed: %u\n", err);
360 device_id = get_unaligned_be64(buf);
361 device_cl = get_unaligned_be64(buf + 8);
362 device_type = get_unaligned_be64(buf + 16);
365 sha1_update(&ctx, buf, 24);
366 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
367 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
369 platform_key_handle = get_unaligned_be32(buf + 24);
375 * @brief compute hash of bootloader itself.
376 * @param[out] dst hash register where the hash should be stored
377 * @return 0 on success, != 0 on failure.
379 * @note MUST be called at a time where the boot loader is accessible at the
380 * configured location (; so take care when code is reallocated).
382 static int compute_self_hash(struct h_reg *dst)
384 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
385 CONFIG_SYS_MONITOR_LEN, dst->digest);
390 int ccdm_compute_self_hash(void)
392 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
393 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
398 * @brief compute the hash of the 2nd stage boot loader (on SD card)
399 * @param[out] dst hash register to store the computed hash
400 * @return 0 on success, != 0 on failure
402 * Determines the size and location of the 2nd stage boot loader on SD card,
403 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
404 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
405 * the desired memory location and the variable @a bl2_entry is set.
407 * @note This sets the variable @a bl2_entry to the entry point when the
408 * 2nd stage boot loader is loaded at its configured memory location.
410 static int compute_second_stage_hash(struct h_reg *dst)
413 u32 code_len, code_offset, target_addr, exec_entry;
415 u8 *load_addr = NULL;
418 mmc = find_mmc_device(0);
423 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
426 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
427 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
428 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
429 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
431 load_addr = get_2nd_stage_bl_location(target_addr);
432 if (load_addr == (u8 *)target_addr)
433 bl2_entry = (void(*)(void))exec_entry;
435 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
438 sha1_csum(load_addr, code_len, dst->digest);
450 * @brief get pointer to hash register by specification
451 * @param spec specification of a hash register
452 * @return pointer to hash register or NULL if @a spec does not qualify a
453 * valid hash register; NULL else.
455 static struct h_reg *get_hreg(uint8_t spec)
459 idx = HREG_IDX(spec);
460 if (IS_FIX_HREG(spec)) {
461 if (idx < ARRAY_SIZE(fix_hregs))
462 return fix_hregs + idx;
463 hre_err = HRE_E_INVALID_HREG;
464 } else if (IS_PCR_HREG(spec)) {
465 if (idx < ARRAY_SIZE(pcr_hregs))
466 return pcr_hregs + idx;
467 hre_err = HRE_E_INVALID_HREG;
468 } else if (IS_VAR_HREG(spec)) {
469 if (idx < ARRAY_SIZE(var_hregs))
470 return var_hregs + idx;
471 hre_err = HRE_E_INVALID_HREG;
477 * @brief get pointer of a hash register by specification and usage.
478 * @param spec specification of a hash register
479 * @param mode access mode (read or write or read/write)
480 * @return pointer to hash register if found and valid; NULL else.
482 * This func uses @a get_reg() to determine the hash register for a given spec.
483 * If a register is found it is validated according to the desired access mode.
484 * The value of automatic registers (PCR register and fixed registers) is
485 * loaded or computed on read access.
487 static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
488 enum access_mode mode)
490 struct h_reg *result;
492 result = get_hreg(spec);
496 if (mode & HREG_WR) {
497 if (IS_FIX_HREG(spec)) {
498 hre_err = HRE_E_INVALID_HREG;
502 if (mode & HREG_RD) {
503 if (!result->valid) {
504 if (IS_PCR_HREG(spec)) {
505 hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec),
507 result->valid = (hre_tpm_err == TPM_SUCCESS);
508 } else if (IS_FIX_HREG(spec)) {
509 switch (HREG_IDX(spec)) {
510 case FIX_HREG_DEVICE_ID_HASH:
511 read_common_data(tpm);
513 case FIX_HREG_SELF_HASH:
514 ccdm_compute_self_hash();
516 case FIX_HREG_STAGE2_HASH:
517 compute_second_stage_hash(result);
519 case FIX_HREG_VENDOR:
520 memcpy(result->digest, vendor, 20);
521 result->valid = true;
525 result->valid = true;
528 if (!result->valid) {
529 hre_err = HRE_E_INVALID_HREG;
537 static void *compute_and(void *_dst, const void *_src, size_t n)
540 const uint8_t *src = _src;
543 for (i = n; i-- > 0; )
549 static void *compute_or(void *_dst, const void *_src, size_t n)
552 const uint8_t *src = _src;
555 for (i = n; i-- > 0; )
561 static void *compute_xor(void *_dst, const void *_src, size_t n)
564 const uint8_t *src = _src;
567 for (i = n; i-- > 0; )
573 static void *compute_extend(void *_dst, const void *_src, size_t n)
579 sha1_update(&ctx, _dst, n);
580 sha1_update(&ctx, _src, n);
581 sha1_finish(&ctx, digest);
582 memcpy(_dst, digest, min(n, sizeof(digest)));
587 static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
588 struct h_reg *dst_reg, const void *key,
591 uint32_t parent_handle;
594 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
596 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
598 hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size,
599 src_reg->digest, &key_handle);
601 hre_err = HRE_E_TPM_FAILURE;
604 /* TODO remember key handle somehow? */
610 * @brief executes the next opcode on the hash register engine.
611 * @param[in,out] ip pointer to the opcode (instruction pointer)
612 * @param[in,out] code_size (remaining) size of the code
613 * @return new instruction pointer on success, NULL on error.
615 static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
618 bool dst_modified = false;
624 struct h_reg *src_reg, *dst_reg;
626 const uint8_t *src_buf, *data;
629 void * (*bin_func)(void *, const void *, size_t);
634 ins = get_unaligned_be32(*ip);
637 src_spec = (ins >> 18) & 0x3f;
638 dst_spec = (ins >> 12) & 0x3f;
639 data_size = (ins & 0x7ff);
641 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
642 opcode, src_spec, dst_spec, data_size);
644 if ((opcode & 0x80) && (data_size + 4) > *code_size)
647 src_reg = access_hreg(tpm, src_spec, HREG_RD);
648 if (hre_err || hre_tpm_err)
650 dst_reg = access_hreg(tpm, dst_spec,
651 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
652 if (hre_err || hre_tpm_err)
660 for (i = 0; i < 20; ++i) {
661 if (src_reg->digest[i])
670 bin_func = compute_xor;
673 bin_func = compute_and;
676 bin_func = compute_or;
679 bin_func = compute_extend;
684 src_buf = src_reg->digest;
689 } else if (data_size == 1) {
690 memset(buf, *data, 20);
692 } else if (data_size >= 20) {
696 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
697 i -= data_size, ptr += data_size)
699 min_t(size_t, i, data_size));
702 bin_func(dst_reg->digest, src_buf, 20);
703 dst_reg->valid = true;
707 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
714 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
715 hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec),
716 dst_reg->digest, dst_reg->digest);
718 hre_err = HRE_E_TPM_FAILURE;
727 *code_size -= data_size;
734 * @brief runs a program on the hash register engine.
735 * @param code pointer to the (HRE) code.
736 * @param code_size size of the code (in bytes).
737 * @return 0 on success, != 0 on failure.
739 static int hre_run_program(struct udevice *tpm, const uint8_t *code,
743 const uint8_t *ip = code;
745 code_left = code_size;
748 while (code_left > 0)
749 if (!hre_execute_op(tpm, &ip, &code_left))
755 static int check_hmac(struct key_program *hmac,
756 const uint8_t *data, size_t data_size)
758 uint8_t key[20], computed_hmac[20];
761 type = get_unaligned_be32(hmac->code);
764 memset(key, 0, sizeof(key));
765 compute_extend(key, pcr_hregs[1].digest, 20);
766 compute_extend(key, pcr_hregs[2].digest, 20);
767 compute_extend(key, pcr_hregs[3].digest, 20);
768 compute_extend(key, pcr_hregs[4].digest, 20);
770 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
772 return memcmp(computed_hmac, hmac->code + 4, 20);
775 static int verify_program(struct key_program *prg)
778 crc = crc32(0, prg->code, prg->code_size);
780 if (crc != prg->code_crc) {
781 printf("HRC crc mismatch: %08x != %08x\n",
788 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
789 static struct key_program *load_sd_key_program(void)
791 u32 code_len, code_offset;
794 struct key_program *result = NULL, *hmac = NULL;
795 struct key_program header;
797 mmc = find_mmc_device(0);
802 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
805 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
806 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
808 code_offset += code_len;
809 /* TODO: the following needs to be the size of the 2nd stage env */
810 code_offset += CONFIG_ENV_SIZE;
812 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
815 header.magic = get_unaligned_be32(buf);
816 header.code_crc = get_unaligned_be32(buf + 4);
817 header.code_size = get_unaligned_be32(buf + 8);
819 if (header.magic != MAGIC_KEY_PROGRAM)
822 result = malloc(sizeof(struct key_program) + header.code_size);
827 printf("load key program chunk from SD card (%u bytes) ",
830 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
833 code_offset += header.code_size;
836 if (verify_program(result))
839 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
842 header.magic = get_unaligned_be32(buf);
843 header.code_crc = get_unaligned_be32(buf + 4);
844 header.code_size = get_unaligned_be32(buf + 8);
846 if (header.magic == MAGIC_HMAC) {
847 puts("check integrity\n");
848 hmac = malloc(sizeof(struct key_program) + header.code_size);
853 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
854 hmac->code_size) < 0)
856 if (verify_program(hmac))
858 if (check_hmac(hmac, result->code, result->code_size)) {
859 puts("key program integrity could not be verified\n");
862 puts("key program verified\n");
878 #ifdef CCDM_SECOND_STAGE
880 * @brief load a key program from file system.
881 * @param ifname interface of the file system
882 * @param dev_part_str device part of the file system
883 * @param fs_type tyep of the file system
884 * @param path path of the file to load.
885 * @return the loaded structure or NULL on failure.
887 static struct key_program *load_key_chunk(const char *ifname,
888 const char *dev_part_str, int fs_type,
891 struct key_program *result = NULL;
892 struct key_program header;
897 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
899 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
903 header.magic = get_unaligned_be32(buf);
904 header.code_crc = get_unaligned_be32(buf + 4);
905 header.code_size = get_unaligned_be32(buf + 8);
907 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
910 result = malloc(sizeof(struct key_program) + header.code_size);
913 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
915 if (fs_read(path, (ulong)result, 0,
916 sizeof(struct key_program) + header.code_size, &i) < 0)
922 crc = crc32(0, result->code, result->code_size);
924 if (crc != result->code_crc) {
925 printf("%s: HRC crc mismatch: %08x != %08x\n",
926 path, crc, result->code_crc);
940 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
941 static const uint8_t prg_stage1_prepare[] = {
942 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
943 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
944 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
945 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
946 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
947 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
948 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
949 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
950 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
951 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
954 static int first_stage_actions(struct udevice *tpm)
957 struct key_program *sd_prg = NULL;
959 puts("CCDM S1: start actions\n");
960 #ifndef CCDM_SECOND_STAGE
961 if (tpm_continue_self_test(tpm))
964 tpm_continue_self_test(tpm);
968 if (hre_run_program(tpm, prg_stage1_prepare,
969 sizeof(prg_stage1_prepare)))
972 sd_prg = load_sd_key_program();
974 if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size))
976 puts("SD code run successfully\n");
978 puts("no key program found on SD\n");
987 printf("CCDM S1: actions done (%d)\n", result);
992 #ifdef CCDM_FIRST_STAGE
993 static int first_stage_init(void)
1000 if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR))
1002 ret = first_stage_actions(tpm);
1003 #ifndef CCDM_SECOND_STAGE
1014 #ifdef CCDM_SECOND_STAGE
1015 static const uint8_t prg_stage2_prepare[] = {
1016 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
1017 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
1018 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
1019 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
1020 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
1023 static const uint8_t prg_stage2_success[] = {
1024 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
1025 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1026 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1027 0xe4, 0xd2, 0x81, 0xe0, /* data */
1030 static const uint8_t prg_stage_fail[] = {
1031 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1032 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1033 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1034 0xea, 0xdf, 0x14, 0x4b, /* data */
1035 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1036 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1039 static int second_stage_init(void)
1041 static const char mac_suffix[] = ".mac";
1042 bool did_first_stage_run = true;
1044 char *cptr, *mmcdev = NULL;
1045 struct key_program *hmac_blob = NULL;
1046 const char *image_path = "/ccdm.itb";
1047 char *mac_path = NULL;
1050 struct udevice *tpm;
1054 printf("CCDM S2\n");
1055 ret = get_tpm(&tpm);
1056 if (ret || tpm_init(tpm))
1058 err = tpm_startup(tpm, TPM_ST_CLEAR);
1059 if (err != TPM_INVALID_POSTINIT)
1060 did_first_stage_run = false;
1062 #ifdef CCDM_AUTO_FIRST_STAGE
1063 if (!did_first_stage_run && first_stage_actions(tpm))
1066 if (!did_first_stage_run)
1070 if (hre_run_program(tpm, prg_stage2_prepare,
1071 sizeof(prg_stage2_prepare)))
1074 /* run "prepboot" from env to get "mmcdev" set */
1075 cptr = env_get("prepboot");
1076 if (cptr && !run_command(cptr, 0))
1077 mmcdev = env_get("mmcdev");
1081 cptr = env_get("ramdiskimage");
1085 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1086 if (mac_path == NULL)
1088 strcpy(mac_path, image_path);
1089 strcat(mac_path, mac_suffix);
1091 /* read image from mmcdev (ccdm.itb) */
1092 image_addr = (ulong)get_image_location();
1093 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1095 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1097 if (image_size <= 0)
1099 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
1101 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1103 puts("failed to load mac file\n");
1106 if (verify_program(hmac_blob)) {
1107 puts("corrupted mac file\n");
1110 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1111 puts("image integrity could not be verified\n");
1114 puts("CCDM image OK\n");
1116 hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success));
1121 hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail));
1132 int show_self_hash(void)
1134 struct h_reg *hash_ptr;
1135 #ifdef CCDM_SECOND_STAGE
1139 if (compute_self_hash(hash_ptr))
1142 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1144 puts("self hash: ");
1145 if (hash_ptr && hash_ptr->valid)
1146 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1154 * @brief let the system hang.
1157 * Will stop the boot process; display a message and signal the error condition
1158 * by blinking the "status" and the "finder" LED of the controller board.
1160 * @note the develop version runs the blink cycle 2 times and then returns.
1161 * The release version never returns.
1163 static void ccdm_hang(void)
1165 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1166 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1173 I2C_SET_BUS(I2C_SOC_0);
1174 pca9698_direction_output(0x22, 0, 0); /* Finder */
1175 pca9698_direction_output(0x22, 4, 0); /* Status */
1177 puts("### ERROR ### Please RESET the board ###\n");
1178 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1180 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1181 puts("** but we continue since this is a DEVELOP version **\n");
1182 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1183 for (j = 2; j-- > 0;) {
1190 for (i = 54; i-- > 0;) {
1191 pca9698_set_value(0x22, 0, !(f & 1));
1192 pca9698_set_value(0x22, 4, (s & 1));
1198 puts("\ncontinue...\n");
1201 int startup_ccdm_id_module(void)
1204 unsigned int orig_i2c_bus;
1206 orig_i2c_bus = i2c_get_bus_num();
1207 i2c_set_bus_num(I2C_SOC_1);
1214 #ifdef CCDM_FIRST_STAGE
1215 result = first_stage_init();
1217 puts("1st stage init failed\n");
1221 #ifdef CCDM_SECOND_STAGE
1222 result = second_stage_init();
1224 puts("2nd stage init failed\n");
1233 i2c_set_bus_num(orig_i2c_bus);