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 */
19 #include <u-boot/sha1.h>
20 #include <asm/byteorder.h>
21 #include <asm/unaligned.h>
24 #undef CCDM_FIRST_STAGE
25 #undef CCDM_SECOND_STAGE
26 #undef CCDM_AUTO_FIRST_STAGE
32 #ifdef CONFIG_TRAILBLAZER
33 #define CCDM_FIRST_STAGE
34 #undef CCDM_SECOND_STAGE
36 #undef CCDM_FIRST_STAGE
37 #define CCDM_SECOND_STAGE
40 #if defined(CCDM_DEVELOP) && defined(CCDM_SECOND_STAGE) && \
41 !defined(CCCM_FIRST_STAGE)
42 #define CCDM_AUTO_FIRST_STAGE
45 /* CCDM specific contants */
48 NV_COMMON_DATA_INDEX = 0x40000001,
49 /* magics for key blob chains */
50 MAGIC_KEY_PROGRAM = 0x68726500,
51 MAGIC_HMAC = 0x68616300,
52 MAGIC_END_OF_CHAIN = 0x00000000,
54 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
59 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
60 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
61 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
62 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
63 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
91 /* register constants */
93 FIX_HREG_DEVICE_ID_HASH = 0,
94 FIX_HREG_SELF_HASH = 1,
95 FIX_HREG_STAGE2_HASH = 2,
103 /* opcodes w/o data */
107 /* opcodes w/o data, w/ sync dst */
108 /* opcodes w/ data */
110 /* opcodes w/data, w/sync dst */
125 static uint64_t device_id;
126 static uint64_t device_cl;
127 static uint64_t device_type;
129 static uint32_t platform_key_handle;
131 static void(*bl2_entry)(void);
133 static struct h_reg pcr_hregs[24];
134 static struct h_reg fix_hregs[COUNT_FIX_HREGS];
135 static struct h_reg var_hregs[8];
136 static uint32_t hre_tpm_err;
137 static int hre_err = HRE_E_OK;
139 #define IS_PCR_HREG(spec) ((spec) & 0x20)
140 #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
141 #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
142 #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
144 static const uint8_t vendor[] = "Guntermann & Drunck";
147 * @brief read a bunch of data from MMC into memory.
149 * @param mmc pointer to the mmc structure to use.
150 * @param src offset where the data starts on MMC/SD device (in bytes).
151 * @param dst pointer to the location where the read data should be stored.
152 * @param size number of bytes to read from the MMC/SD device.
153 * @return number of bytes read or -1 on error.
155 static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
159 ulong block_no, n, cnt;
165 blk_len = mmc->read_bl_len;
166 tmp_buf = malloc(blk_len);
169 block_no = src / blk_len;
173 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
177 result = min(size, (int)(blk_len - ofs));
178 memcpy(dst, tmp_buf + ofs, result);
182 cnt = size / blk_len;
184 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
188 size -= cnt * blk_len;
189 result += cnt * blk_len;
190 dst += cnt * blk_len;
194 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
198 memcpy(dst, tmp_buf, size);
211 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
213 * @return pointer to the location for/of the 2nd stage bootloader
215 static u8 *get_2nd_stage_bl_location(ulong target_addr)
218 #ifdef CCDM_SECOND_STAGE
219 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
227 #ifdef CCDM_SECOND_STAGE
229 * @brief returns a location where the image can be(/ is) placed.
231 * @return pointer to the location for/of the image
233 static u8 *get_image_location(void)
236 /* TODO use other area? */
237 addr = env_get_ulong("loadaddr", 16, CONFIG_LOADADDR);
243 * @brief get the size of a given (TPM) NV area
244 * @param index NV index of the area to get size for
245 * @param size pointer to the size
246 * @return 0 on success, != 0 on error
248 static int get_tpm_nv_size(uint32_t index, uint32_t *size)
255 err = tpm_get_capability(TPM_CAP_NV_INDEX, index,
258 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
263 /* skip tag and nvIndex */
265 /* skip 2 pcr info fields */
266 v16 = get_unaligned_be16(ptr);
267 ptr += 2 + v16 + 1 + 20;
268 v16 = get_unaligned_be16(ptr);
269 ptr += 2 + v16 + 1 + 20;
270 /* skip permission and flags */
273 *size = get_unaligned_be32(ptr);
278 * @brief search for a key by usage auth and pub key hash.
279 * @param auth usage auth of the key to search for
280 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
281 * @param[out] handle the handle of the key iff found
282 * @return 0 if key was found in TPM; != 0 if not.
284 static int find_key(const uint8_t auth[20], const uint8_t pubkey_digest[20],
288 uint32_t key_handles[10];
296 /* fetch list of already loaded keys in the TPM */
297 err = tpm_get_capability(TPM_CAP_HANDLE, TPM_RT_KEY, buf, sizeof(buf));
300 key_count = get_unaligned_be16(buf);
302 for (i = 0; i < key_count; ++i, ptr += 4)
303 key_handles[i] = get_unaligned_be32(ptr);
305 /* now search a(/ the) key which we can access with the given auth */
306 for (i = 0; i < key_count; ++i) {
307 buf_len = sizeof(buf);
308 err = tpm_get_pub_key_oiap(key_handles[i], auth, buf, &buf_len);
309 if (err && err != TPM_AUTHFAIL)
313 sha1_csum(buf, buf_len, digest);
314 if (!memcmp(digest, pubkey_digest, 20)) {
315 *handle = key_handles[i];
323 * @brief read CCDM common data from TPM NV
324 * @return 0 if CCDM common data was found and read, !=0 if something failed.
326 static int read_common_data(void)
333 if (get_tpm_nv_size(NV_COMMON_DATA_INDEX, &size) ||
334 size < NV_COMMON_DATA_MIN_SIZE)
336 err = tpm_nv_read_value(NV_COMMON_DATA_INDEX,
337 buf, min(sizeof(buf), size));
339 printf("tpm_nv_read_value() failed: %u\n", err);
343 device_id = get_unaligned_be64(buf);
344 device_cl = get_unaligned_be64(buf + 8);
345 device_type = get_unaligned_be64(buf + 16);
348 sha1_update(&ctx, buf, 24);
349 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
350 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
352 platform_key_handle = get_unaligned_be32(buf + 24);
358 * @brief compute hash of bootloader itself.
359 * @param[out] dst hash register where the hash should be stored
360 * @return 0 on success, != 0 on failure.
362 * @note MUST be called at a time where the boot loader is accessible at the
363 * configured location (; so take care when code is reallocated).
365 static int compute_self_hash(struct h_reg *dst)
367 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
368 CONFIG_SYS_MONITOR_LEN, dst->digest);
373 int ccdm_compute_self_hash(void)
375 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
376 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
381 * @brief compute the hash of the 2nd stage boot loader (on SD card)
382 * @param[out] dst hash register to store the computed hash
383 * @return 0 on success, != 0 on failure
385 * Determines the size and location of the 2nd stage boot loader on SD card,
386 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
387 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
388 * the desired memory location and the variable @a bl2_entry is set.
390 * @note This sets the variable @a bl2_entry to the entry point when the
391 * 2nd stage boot loader is loaded at its configured memory location.
393 static int compute_second_stage_hash(struct h_reg *dst)
396 u32 code_len, code_offset, target_addr, exec_entry;
398 u8 *load_addr = NULL;
401 mmc = find_mmc_device(0);
406 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
409 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
410 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
411 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
412 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
414 load_addr = get_2nd_stage_bl_location(target_addr);
415 if (load_addr == (u8 *)target_addr)
416 bl2_entry = (void(*)(void))exec_entry;
418 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
421 sha1_csum(load_addr, code_len, dst->digest);
433 * @brief get pointer to hash register by specification
434 * @param spec specification of a hash register
435 * @return pointer to hash register or NULL if @a spec does not qualify a
436 * valid hash register; NULL else.
438 static struct h_reg *get_hreg(uint8_t spec)
442 idx = HREG_IDX(spec);
443 if (IS_FIX_HREG(spec)) {
444 if (idx < ARRAY_SIZE(fix_hregs))
445 return fix_hregs + idx;
446 hre_err = HRE_E_INVALID_HREG;
447 } else if (IS_PCR_HREG(spec)) {
448 if (idx < ARRAY_SIZE(pcr_hregs))
449 return pcr_hregs + idx;
450 hre_err = HRE_E_INVALID_HREG;
451 } else if (IS_VAR_HREG(spec)) {
452 if (idx < ARRAY_SIZE(var_hregs))
453 return var_hregs + idx;
454 hre_err = HRE_E_INVALID_HREG;
460 * @brief get pointer of a hash register by specification and usage.
461 * @param spec specification of a hash register
462 * @param mode access mode (read or write or read/write)
463 * @return pointer to hash register if found and valid; NULL else.
465 * This func uses @a get_reg() to determine the hash register for a given spec.
466 * If a register is found it is validated according to the desired access mode.
467 * The value of automatic registers (PCR register and fixed registers) is
468 * loaded or computed on read access.
470 static struct h_reg *access_hreg(uint8_t spec, enum access_mode mode)
472 struct h_reg *result;
474 result = get_hreg(spec);
478 if (mode & HREG_WR) {
479 if (IS_FIX_HREG(spec)) {
480 hre_err = HRE_E_INVALID_HREG;
484 if (mode & HREG_RD) {
485 if (!result->valid) {
486 if (IS_PCR_HREG(spec)) {
487 hre_tpm_err = tpm_pcr_read(HREG_IDX(spec),
489 result->valid = (hre_tpm_err == TPM_SUCCESS);
490 } else if (IS_FIX_HREG(spec)) {
491 switch (HREG_IDX(spec)) {
492 case FIX_HREG_DEVICE_ID_HASH:
495 case FIX_HREG_SELF_HASH:
496 ccdm_compute_self_hash();
498 case FIX_HREG_STAGE2_HASH:
499 compute_second_stage_hash(result);
501 case FIX_HREG_VENDOR:
502 memcpy(result->digest, vendor, 20);
503 result->valid = true;
507 result->valid = true;
510 if (!result->valid) {
511 hre_err = HRE_E_INVALID_HREG;
519 static void *compute_and(void *_dst, const void *_src, size_t n)
522 const uint8_t *src = _src;
525 for (i = n; i-- > 0; )
531 static void *compute_or(void *_dst, const void *_src, size_t n)
534 const uint8_t *src = _src;
537 for (i = n; i-- > 0; )
543 static void *compute_xor(void *_dst, const void *_src, size_t n)
546 const uint8_t *src = _src;
549 for (i = n; i-- > 0; )
555 static void *compute_extend(void *_dst, const void *_src, size_t n)
561 sha1_update(&ctx, _dst, n);
562 sha1_update(&ctx, _src, n);
563 sha1_finish(&ctx, digest);
564 memcpy(_dst, digest, min(n, sizeof(digest)));
569 static int hre_op_loadkey(struct h_reg *src_reg, struct h_reg *dst_reg,
570 const void *key, size_t key_size)
572 uint32_t parent_handle;
575 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
577 if (find_key(src_reg->digest, dst_reg->digest, &parent_handle))
579 hre_tpm_err = tpm_load_key2_oiap(parent_handle, key, key_size,
580 src_reg->digest, &key_handle);
582 hre_err = HRE_E_TPM_FAILURE;
585 /* TODO remember key handle somehow? */
591 * @brief executes the next opcode on the hash register engine.
592 * @param[in,out] ip pointer to the opcode (instruction pointer)
593 * @param[in,out] code_size (remaining) size of the code
594 * @return new instruction pointer on success, NULL on error.
596 static const uint8_t *hre_execute_op(const uint8_t **ip, size_t *code_size)
598 bool dst_modified = false;
604 struct h_reg *src_reg, *dst_reg;
606 const uint8_t *src_buf, *data;
609 void * (*bin_func)(void *, const void *, size_t);
614 ins = get_unaligned_be32(*ip);
617 src_spec = (ins >> 18) & 0x3f;
618 dst_spec = (ins >> 12) & 0x3f;
619 data_size = (ins & 0x7ff);
621 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
622 opcode, src_spec, dst_spec, data_size);
624 if ((opcode & 0x80) && (data_size + 4) > *code_size)
627 src_reg = access_hreg(src_spec, HREG_RD);
628 if (hre_err || hre_tpm_err)
630 dst_reg = access_hreg(dst_spec, (opcode & 0x40) ? HREG_RDWR : HREG_WR);
631 if (hre_err || hre_tpm_err)
639 for (i = 0; i < 20; ++i) {
640 if (src_reg->digest[i])
649 bin_func = compute_xor;
652 bin_func = compute_and;
655 bin_func = compute_or;
658 bin_func = compute_extend;
663 src_buf = src_reg->digest;
668 } else if (data_size == 1) {
669 memset(buf, *data, 20);
671 } else if (data_size >= 20) {
675 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
676 i -= data_size, ptr += data_size)
678 min_t(size_t, i, data_size));
681 bin_func(dst_reg->digest, src_buf, 20);
682 dst_reg->valid = true;
686 if (hre_op_loadkey(src_reg, dst_reg, data, data_size))
693 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
694 hre_tpm_err = tpm_extend(HREG_IDX(dst_spec), dst_reg->digest,
697 hre_err = HRE_E_TPM_FAILURE;
706 *code_size -= data_size;
713 * @brief runs a program on the hash register engine.
714 * @param code pointer to the (HRE) code.
715 * @param code_size size of the code (in bytes).
716 * @return 0 on success, != 0 on failure.
718 static int hre_run_program(const uint8_t *code, size_t code_size)
721 const uint8_t *ip = code;
723 code_left = code_size;
726 while (code_left > 0)
727 if (!hre_execute_op(&ip, &code_left))
733 static int check_hmac(struct key_program *hmac,
734 const uint8_t *data, size_t data_size)
736 uint8_t key[20], computed_hmac[20];
739 type = get_unaligned_be32(hmac->code);
742 memset(key, 0, sizeof(key));
743 compute_extend(key, pcr_hregs[1].digest, 20);
744 compute_extend(key, pcr_hregs[2].digest, 20);
745 compute_extend(key, pcr_hregs[3].digest, 20);
746 compute_extend(key, pcr_hregs[4].digest, 20);
748 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
750 return memcmp(computed_hmac, hmac->code + 4, 20);
753 static int verify_program(struct key_program *prg)
756 crc = crc32(0, prg->code, prg->code_size);
758 if (crc != prg->code_crc) {
759 printf("HRC crc mismatch: %08x != %08x\n",
766 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
767 static struct key_program *load_sd_key_program(void)
769 u32 code_len, code_offset;
772 struct key_program *result = NULL, *hmac = NULL;
773 struct key_program header;
775 mmc = find_mmc_device(0);
780 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
783 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
784 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
786 code_offset += code_len;
787 /* TODO: the following needs to be the size of the 2nd stage env */
788 code_offset += CONFIG_ENV_SIZE;
790 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
793 header.magic = get_unaligned_be32(buf);
794 header.code_crc = get_unaligned_be32(buf + 4);
795 header.code_size = get_unaligned_be32(buf + 8);
797 if (header.magic != MAGIC_KEY_PROGRAM)
800 result = malloc(sizeof(struct key_program) + header.code_size);
805 printf("load key program chunk from SD card (%u bytes) ",
808 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
811 code_offset += header.code_size;
814 if (verify_program(result))
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_HMAC) {
825 puts("check integrity\n");
826 hmac = malloc(sizeof(struct key_program) + header.code_size);
831 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
832 hmac->code_size) < 0)
834 if (verify_program(hmac))
836 if (check_hmac(hmac, result->code, result->code_size)) {
837 puts("key program integrity could not be verified\n");
840 puts("key program verified\n");
856 #ifdef CCDM_SECOND_STAGE
858 * @brief load a key program from file system.
859 * @param ifname interface of the file system
860 * @param dev_part_str device part of the file system
861 * @param fs_type tyep of the file system
862 * @param path path of the file to load.
863 * @return the loaded structure or NULL on failure.
865 static struct key_program *load_key_chunk(const char *ifname,
866 const char *dev_part_str, int fs_type,
869 struct key_program *result = NULL;
870 struct key_program header;
875 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
877 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
881 header.magic = get_unaligned_be32(buf);
882 header.code_crc = get_unaligned_be32(buf + 4);
883 header.code_size = get_unaligned_be32(buf + 8);
885 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
888 result = malloc(sizeof(struct key_program) + header.code_size);
891 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
893 if (fs_read(path, (ulong)result, 0,
894 sizeof(struct key_program) + header.code_size, &i) < 0)
900 crc = crc32(0, result->code, result->code_size);
902 if (crc != result->code_crc) {
903 printf("%s: HRC crc mismatch: %08x != %08x\n",
904 path, crc, result->code_crc);
918 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
919 static const uint8_t prg_stage1_prepare[] = {
920 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
921 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
922 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
923 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
924 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
925 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
926 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
927 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
928 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
929 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
932 static int first_stage_actions(void)
935 struct key_program *sd_prg = NULL;
937 puts("CCDM S1: start actions\n");
938 #ifndef CCDM_SECOND_STAGE
939 if (tpm_continue_self_test())
942 tpm_continue_self_test();
946 if (hre_run_program(prg_stage1_prepare, sizeof(prg_stage1_prepare)))
949 sd_prg = load_sd_key_program();
951 if (hre_run_program(sd_prg->code, sd_prg->code_size))
953 puts("SD code run successfully\n");
955 puts("no key program found on SD\n");
964 printf("CCDM S1: actions done (%d)\n", result);
969 #ifdef CCDM_FIRST_STAGE
970 static int first_stage_init(void)
974 if (tpm_init() || tpm_startup(TPM_ST_CLEAR))
976 res = first_stage_actions();
977 #ifndef CCDM_SECOND_STAGE
988 #ifdef CCDM_SECOND_STAGE
989 static const uint8_t prg_stage2_prepare[] = {
990 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
991 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
992 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
993 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
994 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
997 static const uint8_t prg_stage2_success[] = {
998 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
999 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1000 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1001 0xe4, 0xd2, 0x81, 0xe0, /* data */
1004 static const uint8_t prg_stage_fail[] = {
1005 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1006 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1007 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1008 0xea, 0xdf, 0x14, 0x4b, /* data */
1009 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1010 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1013 static int second_stage_init(void)
1015 static const char mac_suffix[] = ".mac";
1016 bool did_first_stage_run = true;
1018 char *cptr, *mmcdev = NULL;
1019 struct key_program *hmac_blob = NULL;
1020 const char *image_path = "/ccdm.itb";
1021 char *mac_path = NULL;
1026 printf("CCDM S2\n");
1029 err = tpm_startup(TPM_ST_CLEAR);
1030 if (err != TPM_INVALID_POSTINIT)
1031 did_first_stage_run = false;
1033 #ifdef CCDM_AUTO_FIRST_STAGE
1034 if (!did_first_stage_run && first_stage_actions())
1037 if (!did_first_stage_run)
1041 if (hre_run_program(prg_stage2_prepare, sizeof(prg_stage2_prepare)))
1044 /* run "prepboot" from env to get "mmcdev" set */
1045 cptr = env_get("prepboot");
1046 if (cptr && !run_command(cptr, 0))
1047 mmcdev = env_get("mmcdev");
1051 cptr = env_get("ramdiskimage");
1055 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1056 if (mac_path == NULL)
1058 strcpy(mac_path, image_path);
1059 strcat(mac_path, mac_suffix);
1061 /* read image from mmcdev (ccdm.itb) */
1062 image_addr = (ulong)get_image_location();
1063 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1065 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1067 if (image_size <= 0)
1069 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
1071 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1073 puts("failed to load mac file\n");
1076 if (verify_program(hmac_blob)) {
1077 puts("corrupted mac file\n");
1080 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1081 puts("image integrity could not be verified\n");
1084 puts("CCDM image OK\n");
1086 hre_run_program(prg_stage2_success, sizeof(prg_stage2_success));
1091 hre_run_program(prg_stage_fail, sizeof(prg_stage_fail));
1102 int show_self_hash(void)
1104 struct h_reg *hash_ptr;
1105 #ifdef CCDM_SECOND_STAGE
1109 if (compute_self_hash(hash_ptr))
1112 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1114 puts("self hash: ");
1115 if (hash_ptr && hash_ptr->valid)
1116 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1124 * @brief let the system hang.
1127 * Will stop the boot process; display a message and signal the error condition
1128 * by blinking the "status" and the "finder" LED of the controller board.
1130 * @note the develop version runs the blink cycle 2 times and then returns.
1131 * The release version never returns.
1133 static void ccdm_hang(void)
1135 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1136 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1143 I2C_SET_BUS(I2C_SOC_0);
1144 pca9698_direction_output(0x22, 0, 0); /* Finder */
1145 pca9698_direction_output(0x22, 4, 0); /* Status */
1147 puts("### ERROR ### Please RESET the board ###\n");
1148 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1150 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1151 puts("** but we continue since this is a DEVELOP version **\n");
1152 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1153 for (j = 2; j-- > 0;) {
1160 for (i = 54; i-- > 0;) {
1161 pca9698_set_value(0x22, 0, !(f & 1));
1162 pca9698_set_value(0x22, 4, (s & 1));
1168 puts("\ncontinue...\n");
1171 int startup_ccdm_id_module(void)
1174 unsigned int orig_i2c_bus;
1176 orig_i2c_bus = i2c_get_bus_num();
1177 i2c_set_bus_num(I2C_SOC_1);
1184 #ifdef CCDM_FIRST_STAGE
1185 result = first_stage_init();
1187 puts("1st stage init failed\n");
1191 #ifdef CCDM_SECOND_STAGE
1192 result = second_stage_init();
1194 puts("2nd stage init failed\n");
1203 i2c_set_bus_num(orig_i2c_bus);