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 */
14 #include <bootstage.h>
25 #include <u-boot/crc.h>
26 #include <u-boot/sha1.h>
27 #include <asm/byteorder.h>
28 #include <asm/unaligned.h>
31 #undef CCDM_FIRST_STAGE
32 #undef CCDM_SECOND_STAGE
33 #undef CCDM_AUTO_FIRST_STAGE
39 #ifdef CONFIG_TRAILBLAZER
40 #define CCDM_FIRST_STAGE
41 #undef CCDM_SECOND_STAGE
43 #undef CCDM_FIRST_STAGE
44 #define CCDM_SECOND_STAGE
47 #if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
48 !defined(CCCM_FIRST_STAGE)
49 #define CCDM_AUTO_FIRST_STAGE
52 /* CCDM specific contants */
55 NV_COMMON_DATA_INDEX = 0x40000001,
56 /* magics for key blob chains */
57 MAGIC_KEY_PROGRAM = 0x68726500,
58 MAGIC_HMAC = 0x68616300,
59 MAGIC_END_OF_CHAIN = 0x00000000,
61 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
66 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
67 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
68 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
69 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
70 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
98 /* register constants */
100 FIX_HREG_DEVICE_ID_HASH = 0,
101 FIX_HREG_SELF_HASH = 1,
102 FIX_HREG_STAGE2_HASH = 2,
110 /* opcodes w/o data */
114 /* opcodes w/o data, w/ sync dst */
115 /* opcodes w/ data */
117 /* opcodes w/data, w/sync dst */
132 static uint64_t device_id;
133 static uint64_t device_cl;
134 static uint64_t device_type;
136 static uint32_t platform_key_handle;
138 static void(*bl2_entry)(void);
140 static struct h_reg pcr_hregs[24];
141 static struct h_reg fix_hregs[COUNT_FIX_HREGS];
142 static struct h_reg var_hregs[8];
143 static uint32_t hre_tpm_err;
144 static int hre_err = HRE_E_OK;
146 #define IS_PCR_HREG(spec) ((spec) & 0x20)
147 #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
148 #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
149 #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
151 static int get_tpm(struct udevice **devp)
155 rc = uclass_first_device_err(UCLASS_TPM, devp);
157 printf("Could not find TPM (ret=%d)\n", rc);
158 return CMD_RET_FAILURE;
164 static const uint8_t vendor[] = "Guntermann & Drunck";
167 * @brief read a bunch of data from MMC into memory.
169 * @param mmc pointer to the mmc structure to use.
170 * @param src offset where the data starts on MMC/SD device (in bytes).
171 * @param dst pointer to the location where the read data should be stored.
172 * @param size number of bytes to read from the MMC/SD device.
173 * @return number of bytes read or -1 on error.
175 static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
179 ulong block_no, n, cnt;
185 blk_len = mmc->read_bl_len;
186 tmp_buf = malloc(blk_len);
189 block_no = src / blk_len;
193 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
197 result = min(size, (int)(blk_len - ofs));
198 memcpy(dst, tmp_buf + ofs, result);
202 cnt = size / blk_len;
204 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
208 size -= cnt * blk_len;
209 result += cnt * blk_len;
210 dst += cnt * blk_len;
214 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
218 memcpy(dst, tmp_buf, size);
231 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
233 * @return pointer to the location for/of the 2nd stage bootloader
235 static u8 *get_2nd_stage_bl_location(ulong target_addr)
238 #ifdef CCDM_SECOND_STAGE
239 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
247 #ifdef CCDM_SECOND_STAGE
249 * @brief returns a location where the image can be(/ is) placed.
251 * @return pointer to the location for/of the image
253 static u8 *get_image_location(void)
256 /* TODO use other area? */
257 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
263 * @brief get the size of a given (TPM) NV area
264 * @param index NV index of the area to get size for
265 * @param size pointer to the size
266 * @return 0 on success, != 0 on error
268 static int get_tpm_nv_size(struct udevice *tpm, uint32_t index, uint32_t *size)
275 err = tpm_get_capability(tpm, TPM_CAP_NV_INDEX, index,
278 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
283 /* skip tag and nvIndex */
285 /* skip 2 pcr info fields */
286 v16 = get_unaligned_be16(ptr);
287 ptr += 2 + v16 + 1 + 20;
288 v16 = get_unaligned_be16(ptr);
289 ptr += 2 + v16 + 1 + 20;
290 /* skip permission and flags */
293 *size = get_unaligned_be32(ptr);
298 * @brief search for a key by usage auth and pub key hash.
299 * @param auth usage auth of the key to search for
300 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
301 * @param[out] handle the handle of the key iff found
302 * @return 0 if key was found in TPM; != 0 if not.
304 static int find_key(struct udevice *tpm, const uint8_t auth[20],
305 const uint8_t pubkey_digest[20], uint32_t *handle)
308 uint32_t key_handles[10];
316 /* fetch list of already loaded keys in the TPM */
317 err = tpm_get_capability(tpm, TPM_CAP_HANDLE, TPM_RT_KEY, buf,
321 key_count = get_unaligned_be16(buf);
323 for (i = 0; i < key_count; ++i, ptr += 4)
324 key_handles[i] = get_unaligned_be32(ptr);
326 /* now search a(/ the) key which we can access with the given auth */
327 for (i = 0; i < key_count; ++i) {
328 buf_len = sizeof(buf);
329 err = tpm_get_pub_key_oiap(tpm, key_handles[i], auth, buf,
331 if (err && err != TPM_AUTHFAIL)
335 sha1_csum(buf, buf_len, digest);
336 if (!memcmp(digest, pubkey_digest, 20)) {
337 *handle = key_handles[i];
345 * @brief read CCDM common data from TPM NV
346 * @return 0 if CCDM common data was found and read, !=0 if something failed.
348 static int read_common_data(struct udevice *tpm)
355 if (get_tpm_nv_size(tpm, NV_COMMON_DATA_INDEX, &size) ||
356 size < NV_COMMON_DATA_MIN_SIZE)
358 err = tpm_nv_read_value(tpm, NV_COMMON_DATA_INDEX,
359 buf, min(sizeof(buf), size));
361 printf("tpm_nv_read_value() failed: %u\n", err);
365 device_id = get_unaligned_be64(buf);
366 device_cl = get_unaligned_be64(buf + 8);
367 device_type = get_unaligned_be64(buf + 16);
370 sha1_update(&ctx, buf, 24);
371 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
372 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
374 platform_key_handle = get_unaligned_be32(buf + 24);
380 * @brief compute hash of bootloader itself.
381 * @param[out] dst hash register where the hash should be stored
382 * @return 0 on success, != 0 on failure.
384 * @note MUST be called at a time where the boot loader is accessible at the
385 * configured location (; so take care when code is reallocated).
387 static int compute_self_hash(struct h_reg *dst)
389 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
390 CONFIG_SYS_MONITOR_LEN, dst->digest);
395 int ccdm_compute_self_hash(void)
397 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
398 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
403 * @brief compute the hash of the 2nd stage boot loader (on SD card)
404 * @param[out] dst hash register to store the computed hash
405 * @return 0 on success, != 0 on failure
407 * Determines the size and location of the 2nd stage boot loader on SD card,
408 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
409 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
410 * the desired memory location and the variable @a bl2_entry is set.
412 * @note This sets the variable @a bl2_entry to the entry point when the
413 * 2nd stage boot loader is loaded at its configured memory location.
415 static int compute_second_stage_hash(struct h_reg *dst)
418 u32 code_len, code_offset, target_addr, exec_entry;
420 u8 *load_addr = NULL;
423 mmc = find_mmc_device(0);
428 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
431 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
432 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
433 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
434 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
436 load_addr = get_2nd_stage_bl_location(target_addr);
437 if (load_addr == (u8 *)target_addr)
438 bl2_entry = (void(*)(void))exec_entry;
440 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
443 sha1_csum(load_addr, code_len, dst->digest);
455 * @brief get pointer to hash register by specification
456 * @param spec specification of a hash register
457 * @return pointer to hash register or NULL if @a spec does not qualify a
458 * valid hash register; NULL else.
460 static struct h_reg *get_hreg(uint8_t spec)
464 idx = HREG_IDX(spec);
465 if (IS_FIX_HREG(spec)) {
466 if (idx < ARRAY_SIZE(fix_hregs))
467 return fix_hregs + idx;
468 hre_err = HRE_E_INVALID_HREG;
469 } else if (IS_PCR_HREG(spec)) {
470 if (idx < ARRAY_SIZE(pcr_hregs))
471 return pcr_hregs + idx;
472 hre_err = HRE_E_INVALID_HREG;
473 } else if (IS_VAR_HREG(spec)) {
474 if (idx < ARRAY_SIZE(var_hregs))
475 return var_hregs + idx;
476 hre_err = HRE_E_INVALID_HREG;
482 * @brief get pointer of a hash register by specification and usage.
483 * @param spec specification of a hash register
484 * @param mode access mode (read or write or read/write)
485 * @return pointer to hash register if found and valid; NULL else.
487 * This func uses @a get_reg() to determine the hash register for a given spec.
488 * If a register is found it is validated according to the desired access mode.
489 * The value of automatic registers (PCR register and fixed registers) is
490 * loaded or computed on read access.
492 static struct h_reg *access_hreg(struct udevice *tpm, uint8_t spec,
493 enum access_mode mode)
495 struct h_reg *result;
497 result = get_hreg(spec);
501 if (mode & HREG_WR) {
502 if (IS_FIX_HREG(spec)) {
503 hre_err = HRE_E_INVALID_HREG;
507 if (mode & HREG_RD) {
508 if (!result->valid) {
509 if (IS_PCR_HREG(spec)) {
510 hre_tpm_err = tpm_pcr_read(tpm, HREG_IDX(spec),
512 result->valid = (hre_tpm_err == TPM_SUCCESS);
513 } else if (IS_FIX_HREG(spec)) {
514 switch (HREG_IDX(spec)) {
515 case FIX_HREG_DEVICE_ID_HASH:
516 read_common_data(tpm);
518 case FIX_HREG_SELF_HASH:
519 ccdm_compute_self_hash();
521 case FIX_HREG_STAGE2_HASH:
522 compute_second_stage_hash(result);
524 case FIX_HREG_VENDOR:
525 memcpy(result->digest, vendor, 20);
526 result->valid = true;
530 result->valid = true;
533 if (!result->valid) {
534 hre_err = HRE_E_INVALID_HREG;
542 static void *compute_and(void *_dst, const void *_src, size_t n)
545 const uint8_t *src = _src;
548 for (i = n; i-- > 0; )
554 static void *compute_or(void *_dst, const void *_src, size_t n)
557 const uint8_t *src = _src;
560 for (i = n; i-- > 0; )
566 static void *compute_xor(void *_dst, const void *_src, size_t n)
569 const uint8_t *src = _src;
572 for (i = n; i-- > 0; )
578 static void *compute_extend(void *_dst, const void *_src, size_t n)
584 sha1_update(&ctx, _dst, n);
585 sha1_update(&ctx, _src, n);
586 sha1_finish(&ctx, digest);
587 memcpy(_dst, digest, min(n, sizeof(digest)));
592 static int hre_op_loadkey(struct udevice *tpm, struct h_reg *src_reg,
593 struct h_reg *dst_reg, const void *key,
596 uint32_t parent_handle;
599 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
601 if (find_key(tpm, src_reg->digest, dst_reg->digest, &parent_handle))
603 hre_tpm_err = tpm_load_key2_oiap(tpm, parent_handle, key, key_size,
604 src_reg->digest, &key_handle);
606 hre_err = HRE_E_TPM_FAILURE;
609 /* TODO remember key handle somehow? */
615 * @brief executes the next opcode on the hash register engine.
616 * @param[in,out] ip pointer to the opcode (instruction pointer)
617 * @param[in,out] code_size (remaining) size of the code
618 * @return new instruction pointer on success, NULL on error.
620 static const uint8_t *hre_execute_op(struct udevice *tpm, const uint8_t **ip,
623 bool dst_modified = false;
629 struct h_reg *src_reg, *dst_reg;
631 const uint8_t *src_buf, *data;
634 void * (*bin_func)(void *, const void *, size_t);
639 ins = get_unaligned_be32(*ip);
642 src_spec = (ins >> 18) & 0x3f;
643 dst_spec = (ins >> 12) & 0x3f;
644 data_size = (ins & 0x7ff);
646 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
647 opcode, src_spec, dst_spec, data_size);
649 if ((opcode & 0x80) && (data_size + 4) > *code_size)
652 src_reg = access_hreg(tpm, src_spec, HREG_RD);
653 if (hre_err || hre_tpm_err)
655 dst_reg = access_hreg(tpm, dst_spec,
656 (opcode & 0x40) ? HREG_RDWR : HREG_WR);
657 if (hre_err || hre_tpm_err)
665 for (i = 0; i < 20; ++i) {
666 if (src_reg->digest[i])
675 bin_func = compute_xor;
678 bin_func = compute_and;
681 bin_func = compute_or;
684 bin_func = compute_extend;
689 src_buf = src_reg->digest;
694 } else if (data_size == 1) {
695 memset(buf, *data, 20);
697 } else if (data_size >= 20) {
701 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
702 i -= data_size, ptr += data_size)
704 min_t(size_t, i, data_size));
707 bin_func(dst_reg->digest, src_buf, 20);
708 dst_reg->valid = true;
712 if (hre_op_loadkey(tpm, src_reg, dst_reg, data, data_size))
719 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
720 hre_tpm_err = tpm_extend(tpm, HREG_IDX(dst_spec),
721 dst_reg->digest, dst_reg->digest);
723 hre_err = HRE_E_TPM_FAILURE;
732 *code_size -= data_size;
739 * @brief runs a program on the hash register engine.
740 * @param code pointer to the (HRE) code.
741 * @param code_size size of the code (in bytes).
742 * @return 0 on success, != 0 on failure.
744 static int hre_run_program(struct udevice *tpm, const uint8_t *code,
748 const uint8_t *ip = code;
750 code_left = code_size;
753 while (code_left > 0)
754 if (!hre_execute_op(tpm, &ip, &code_left))
760 static int check_hmac(struct key_program *hmac,
761 const uint8_t *data, size_t data_size)
763 uint8_t key[20], computed_hmac[20];
766 type = get_unaligned_be32(hmac->code);
769 memset(key, 0, sizeof(key));
770 compute_extend(key, pcr_hregs[1].digest, 20);
771 compute_extend(key, pcr_hregs[2].digest, 20);
772 compute_extend(key, pcr_hregs[3].digest, 20);
773 compute_extend(key, pcr_hregs[4].digest, 20);
775 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
777 return memcmp(computed_hmac, hmac->code + 4, 20);
780 static int verify_program(struct key_program *prg)
783 crc = crc32(0, prg->code, prg->code_size);
785 if (crc != prg->code_crc) {
786 printf("HRC crc mismatch: %08x != %08x\n",
793 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
794 static struct key_program *load_sd_key_program(void)
796 u32 code_len, code_offset;
799 struct key_program *result = NULL, *hmac = NULL;
800 struct key_program header;
802 mmc = find_mmc_device(0);
807 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
810 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
811 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
813 code_offset += code_len;
814 /* TODO: the following needs to be the size of the 2nd stage env */
815 code_offset += CONFIG_ENV_SIZE;
817 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
820 header.magic = get_unaligned_be32(buf);
821 header.code_crc = get_unaligned_be32(buf + 4);
822 header.code_size = get_unaligned_be32(buf + 8);
824 if (header.magic != MAGIC_KEY_PROGRAM)
827 result = malloc(sizeof(struct key_program) + header.code_size);
832 printf("load key program chunk from SD card (%u bytes) ",
835 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
838 code_offset += header.code_size;
841 if (verify_program(result))
844 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
847 header.magic = get_unaligned_be32(buf);
848 header.code_crc = get_unaligned_be32(buf + 4);
849 header.code_size = get_unaligned_be32(buf + 8);
851 if (header.magic == MAGIC_HMAC) {
852 puts("check integrity\n");
853 hmac = malloc(sizeof(struct key_program) + header.code_size);
858 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
859 hmac->code_size) < 0)
861 if (verify_program(hmac))
863 if (check_hmac(hmac, result->code, result->code_size)) {
864 puts("key program integrity could not be verified\n");
867 puts("key program verified\n");
883 #ifdef CCDM_SECOND_STAGE
885 * @brief load a key program from file system.
886 * @param ifname interface of the file system
887 * @param dev_part_str device part of the file system
888 * @param fs_type tyep of the file system
889 * @param path path of the file to load.
890 * @return the loaded structure or NULL on failure.
892 static struct key_program *load_key_chunk(const char *ifname,
893 const char *dev_part_str, int fs_type,
896 struct key_program *result = NULL;
897 struct key_program header;
902 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
904 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
908 header.magic = get_unaligned_be32(buf);
909 header.code_crc = get_unaligned_be32(buf + 4);
910 header.code_size = get_unaligned_be32(buf + 8);
912 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
915 result = malloc(sizeof(struct key_program) + header.code_size);
918 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
920 if (fs_read(path, (ulong)result, 0,
921 sizeof(struct key_program) + header.code_size, &i) < 0)
927 crc = crc32(0, result->code, result->code_size);
929 if (crc != result->code_crc) {
930 printf("%s: HRC crc mismatch: %08x != %08x\n",
931 path, crc, result->code_crc);
945 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
946 static const uint8_t prg_stage1_prepare[] = {
947 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
948 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
949 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
950 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
951 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
952 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
953 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
954 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
955 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
956 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
959 static int first_stage_actions(struct udevice *tpm)
962 struct key_program *sd_prg = NULL;
964 puts("CCDM S1: start actions\n");
965 #ifndef CCDM_SECOND_STAGE
966 if (tpm_continue_self_test(tpm))
969 tpm_continue_self_test(tpm);
973 if (hre_run_program(tpm, prg_stage1_prepare,
974 sizeof(prg_stage1_prepare)))
977 sd_prg = load_sd_key_program();
979 if (hre_run_program(tpm, sd_prg->code, sd_prg->code_size))
981 puts("SD code run successfully\n");
983 puts("no key program found on SD\n");
992 printf("CCDM S1: actions done (%d)\n", result);
997 #ifdef CCDM_FIRST_STAGE
998 static int first_stage_init(void)
1000 struct udevice *tpm;
1004 ret = get_tpm(&tpm);
1005 if (ret || tpm_init(tpm) || tpm_startup(tpm, TPM_ST_CLEAR))
1007 ret = first_stage_actions(tpm);
1008 #ifndef CCDM_SECOND_STAGE
1019 #ifdef CCDM_SECOND_STAGE
1020 static const uint8_t prg_stage2_prepare[] = {
1021 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
1022 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
1023 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
1024 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
1025 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
1028 static const uint8_t prg_stage2_success[] = {
1029 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
1030 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1031 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1032 0xe4, 0xd2, 0x81, 0xe0, /* data */
1035 static const uint8_t prg_stage_fail[] = {
1036 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1037 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1038 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1039 0xea, 0xdf, 0x14, 0x4b, /* data */
1040 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1041 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1044 static int second_stage_init(void)
1046 static const char mac_suffix[] = ".mac";
1047 bool did_first_stage_run = true;
1049 char *cptr, *mmcdev = NULL;
1050 struct key_program *hmac_blob = NULL;
1051 const char *image_path = "/ccdm.itb";
1052 char *mac_path = NULL;
1055 struct udevice *tpm;
1059 printf("CCDM S2\n");
1060 ret = get_tpm(&tpm);
1061 if (ret || tpm_init(tpm))
1063 err = tpm_startup(tpm, TPM_ST_CLEAR);
1064 if (err != TPM_INVALID_POSTINIT)
1065 did_first_stage_run = false;
1067 #ifdef CCDM_AUTO_FIRST_STAGE
1068 if (!did_first_stage_run && first_stage_actions(tpm))
1071 if (!did_first_stage_run)
1075 if (hre_run_program(tpm, prg_stage2_prepare,
1076 sizeof(prg_stage2_prepare)))
1079 /* run "prepboot" from env to get "mmcdev" set */
1080 cptr = env_get("prepboot");
1081 if (cptr && !run_command(cptr, 0))
1082 mmcdev = env_get("mmcdev");
1086 cptr = env_get("ramdiskimage");
1090 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1091 if (mac_path == NULL)
1093 strcpy(mac_path, image_path);
1094 strcat(mac_path, mac_suffix);
1096 /* read image from mmcdev (ccdm.itb) */
1097 image_addr = (ulong)get_image_location();
1098 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1100 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1102 if (image_size <= 0)
1104 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
1106 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1108 puts("failed to load mac file\n");
1111 if (verify_program(hmac_blob)) {
1112 puts("corrupted mac file\n");
1115 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1116 puts("image integrity could not be verified\n");
1119 puts("CCDM image OK\n");
1121 hre_run_program(tpm, prg_stage2_success, sizeof(prg_stage2_success));
1126 hre_run_program(tpm, prg_stage_fail, sizeof(prg_stage_fail));
1137 int show_self_hash(void)
1139 struct h_reg *hash_ptr;
1140 #ifdef CCDM_SECOND_STAGE
1144 if (compute_self_hash(hash_ptr))
1147 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1149 puts("self hash: ");
1150 if (hash_ptr && hash_ptr->valid)
1151 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1159 * @brief let the system hang.
1162 * Will stop the boot process; display a message and signal the error condition
1163 * by blinking the "status" and the "finder" LED of the controller board.
1165 * @note the develop version runs the blink cycle 2 times and then returns.
1166 * The release version never returns.
1168 static void ccdm_hang(void)
1170 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1171 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1178 I2C_SET_BUS(I2C_SOC_0);
1179 pca9698_direction_output(0x22, 0, 0); /* Finder */
1180 pca9698_direction_output(0x22, 4, 0); /* Status */
1182 puts("### ERROR ### Please RESET the board ###\n");
1183 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1185 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1186 puts("** but we continue since this is a DEVELOP version **\n");
1187 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1188 for (j = 2; j-- > 0;) {
1195 for (i = 54; i-- > 0;) {
1196 pca9698_set_value(0x22, 0, !(f & 1));
1197 pca9698_set_value(0x22, 4, (s & 1));
1203 puts("\ncontinue...\n");
1206 int startup_ccdm_id_module(void)
1209 unsigned int orig_i2c_bus;
1211 orig_i2c_bus = i2c_get_bus_num();
1212 i2c_set_bus_num(I2C_SOC_1);
1219 #ifdef CCDM_FIRST_STAGE
1220 result = first_stage_init();
1222 puts("1st stage init failed\n");
1226 #ifdef CCDM_SECOND_STAGE
1227 result = second_stage_init();
1229 puts("2nd stage init failed\n");
1238 i2c_set_bus_num(orig_i2c_bus);