3 * Reinhard Pfau, Guntermann & Drunck GmbH, reinhard.pfau@gdsys.cc
5 * SPDX-License-Identifier: GPL-2.0+
8 /* 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 /* enums from TCG specs */
48 /* capability areas */
49 TPM_CAP_NV_INDEX = 0x00000011,
50 TPM_CAP_HANDLE = 0x00000014,
52 TPM_RT_KEY = 0x00000001,
55 /* CCDM specific contants */
58 NV_COMMON_DATA_INDEX = 0x40000001,
59 /* magics for key blob chains */
60 MAGIC_KEY_PROGRAM = 0x68726500,
61 MAGIC_HMAC = 0x68616300,
62 MAGIC_END_OF_CHAIN = 0x00000000,
64 NV_COMMON_DATA_MIN_SIZE = 3 * sizeof(uint64_t) + 2 * sizeof(uint16_t),
69 ESDHC_BOOT_IMAGE_SIG_OFS = 0x40,
70 ESDHC_BOOT_IMAGE_SIZE_OFS = 0x48,
71 ESDHC_BOOT_IMAGE_ADDR_OFS = 0x50,
72 ESDHC_BOOT_IMAGE_TARGET_OFS = 0x58,
73 ESDHC_BOOT_IMAGE_ENTRY_OFS = 0x60,
101 /* register constants */
103 FIX_HREG_DEVICE_ID_HASH = 0,
104 FIX_HREG_SELF_HASH = 1,
105 FIX_HREG_STAGE2_HASH = 2,
113 /* opcodes w/o data */
117 /* opcodes w/o data, w/ sync dst */
118 /* opcodes w/ data */
120 /* opcodes w/data, w/sync dst */
135 static uint64_t device_id;
136 static uint64_t device_cl;
137 static uint64_t device_type;
139 static uint32_t platform_key_handle;
141 static void(*bl2_entry)(void);
143 static struct h_reg pcr_hregs[24];
144 static struct h_reg fix_hregs[COUNT_FIX_HREGS];
145 static struct h_reg var_hregs[8];
146 static uint32_t hre_tpm_err;
147 static int hre_err = HRE_E_OK;
149 #define IS_PCR_HREG(spec) ((spec) & 0x20)
150 #define IS_FIX_HREG(spec) (((spec) & 0x38) == 0x08)
151 #define IS_VAR_HREG(spec) (((spec) & 0x38) == 0x10)
152 #define HREG_IDX(spec) ((spec) & (IS_PCR_HREG(spec) ? 0x1f : 0x7))
155 static const uint8_t prg_stage1_prepare[] = {
156 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
157 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
158 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
159 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
160 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
161 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
162 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
163 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
164 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
165 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
168 static const uint8_t prg_stage2_prepare[] = {
169 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
170 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
171 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
172 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
173 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
176 static const uint8_t prg_stage2_success[] = {
177 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
178 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
179 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
180 0xe4, 0xd2, 0x81, 0xe0, /* data */
183 static const uint8_t prg_stage_fail[] = {
184 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
185 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
186 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
187 0xea, 0xdf, 0x14, 0x4b, /* data */
188 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
189 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
192 static const uint8_t vendor[] = "Guntermann & Drunck";
196 * @brief read a bunch of data from MMC into memory.
198 * @param mmc pointer to the mmc structure to use.
199 * @param src offset where the data starts on MMC/SD device (in bytes).
200 * @param dst pointer to the location where the read data should be stored.
201 * @param size number of bytes to read from the MMC/SD device.
202 * @return number of bytes read or -1 on error.
204 static int ccdm_mmc_read(struct mmc *mmc, u64 src, u8 *dst, int size)
208 ulong block_no, n, cnt;
214 blk_len = mmc->read_bl_len;
215 tmp_buf = malloc(blk_len);
218 block_no = src / blk_len;
222 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
226 result = min(size, (int)(blk_len - ofs));
227 memcpy(dst, tmp_buf + ofs, result);
231 cnt = size / blk_len;
233 n = mmc->block_dev.block_read(&mmc->block_dev, block_no, cnt,
237 size -= cnt * blk_len;
238 result += cnt * blk_len;
239 dst += cnt * blk_len;
243 n = mmc->block_dev.block_read(&mmc->block_dev, block_no++, 1,
247 memcpy(dst, tmp_buf, size);
260 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
262 * @return pointer to the location for/of the 2nd stage bootloader
264 static u8 *get_2nd_stage_bl_location(ulong target_addr)
267 #ifdef CCDM_SECOND_STAGE
268 addr = getenv_ulong("loadaddr", 16, CONFIG_LOADADDR);
276 #ifdef CCDM_SECOND_STAGE
278 * @brief returns a location where the image can be(/ is) placed.
280 * @return pointer to the location for/of the image
282 static u8 *get_image_location(void)
285 /* TODO use other area? */
286 addr = getenv_ulong("loadaddr", 16, CONFIG_LOADADDR);
292 * @brief get the size of a given (TPM) NV area
293 * @param index NV index of the area to get size for
294 * @param size pointer to the size
295 * @return 0 on success, != 0 on error
297 static int get_tpm_nv_size(uint32_t index, uint32_t *size)
304 err = tpm_get_capability(TPM_CAP_NV_INDEX, index,
307 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
312 /* skip tag and nvIndex */
314 /* skip 2 pcr info fields */
315 v16 = get_unaligned_be16(ptr);
316 ptr += 2 + v16 + 1 + 20;
317 v16 = get_unaligned_be16(ptr);
318 ptr += 2 + v16 + 1 + 20;
319 /* skip permission and flags */
322 *size = get_unaligned_be32(ptr);
327 * @brief search for a key by usage auth and pub key hash.
328 * @param auth usage auth of the key to search for
329 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
330 * @param[out] handle the handle of the key iff found
331 * @return 0 if key was found in TPM; != 0 if not.
333 static int find_key(const uint8_t auth[20], const uint8_t pubkey_digest[20],
337 uint32_t key_handles[10];
345 /* fetch list of already loaded keys in the TPM */
346 err = tpm_get_capability(TPM_CAP_HANDLE, TPM_RT_KEY, buf, sizeof(buf));
349 key_count = get_unaligned_be16(buf);
351 for (i = 0; i < key_count; ++i, ptr += 4)
352 key_handles[i] = get_unaligned_be32(ptr);
354 /* now search a(/ the) key which we can access with the given auth */
355 for (i = 0; i < key_count; ++i) {
356 buf_len = sizeof(buf);
357 err = tpm_get_pub_key_oiap(key_handles[i], auth, buf, &buf_len);
358 if (err && err != TPM_AUTHFAIL)
362 sha1_csum(buf, buf_len, digest);
363 if (!memcmp(digest, pubkey_digest, 20)) {
364 *handle = key_handles[i];
372 * @brief read CCDM common data from TPM NV
373 * @return 0 if CCDM common data was found and read, !=0 if something failed.
375 static int read_common_data(void)
382 if (get_tpm_nv_size(NV_COMMON_DATA_INDEX, &size) ||
383 size < NV_COMMON_DATA_MIN_SIZE)
385 err = tpm_nv_read_value(NV_COMMON_DATA_INDEX,
386 buf, min(sizeof(buf), size));
388 printf("tpm_nv_read_value() failed: %u\n", err);
392 device_id = get_unaligned_be64(buf);
393 device_cl = get_unaligned_be64(buf + 8);
394 device_type = get_unaligned_be64(buf + 16);
397 sha1_update(&ctx, buf, 24);
398 sha1_finish(&ctx, fix_hregs[FIX_HREG_DEVICE_ID_HASH].digest);
399 fix_hregs[FIX_HREG_DEVICE_ID_HASH].valid = true;
401 platform_key_handle = get_unaligned_be32(buf + 24);
407 * @brief compute hash of bootloader itself.
408 * @param[out] dst hash register where the hash should be stored
409 * @return 0 on success, != 0 on failure.
411 * @note MUST be called at a time where the boot loader is accessible at the
412 * configured location (; so take care when code is reallocated).
414 static int compute_self_hash(struct h_reg *dst)
416 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE,
417 CONFIG_SYS_MONITOR_LEN, dst->digest);
422 int ccdm_compute_self_hash(void)
424 if (!fix_hregs[FIX_HREG_SELF_HASH].valid)
425 compute_self_hash(&fix_hregs[FIX_HREG_SELF_HASH]);
430 * @brief compute the hash of the 2nd stage boot loader (on SD card)
431 * @param[out] dst hash register to store the computed hash
432 * @return 0 on success, != 0 on failure
434 * Determines the size and location of the 2nd stage boot loader on SD card,
435 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
436 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
437 * the desired memory location and the variable @a bl2_entry is set.
439 * @note This sets the variable @a bl2_entry to the entry point when the
440 * 2nd stage boot loader is loaded at its configured memory location.
442 static int compute_second_stage_hash(struct h_reg *dst)
445 u32 code_len, code_offset, target_addr, exec_entry;
447 u8 *load_addr = NULL;
450 mmc = find_mmc_device(0);
455 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) < 0)
458 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
459 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
460 target_addr = *(u32 *)(buf + ESDHC_BOOT_IMAGE_TARGET_OFS);
461 exec_entry = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ENTRY_OFS);
463 load_addr = get_2nd_stage_bl_location(target_addr);
464 if (load_addr == (u8 *)target_addr)
465 bl2_entry = (void(*)(void))exec_entry;
467 if (ccdm_mmc_read(mmc, code_offset, load_addr, code_len) < 0)
470 sha1_csum(load_addr, code_len, dst->digest);
482 * @brief get pointer to hash register by specification
483 * @param spec specification of a hash register
484 * @return pointer to hash register or NULL if @a spec does not qualify a
485 * valid hash register; NULL else.
487 static struct h_reg *get_hreg(uint8_t spec)
491 idx = HREG_IDX(spec);
492 if (IS_FIX_HREG(spec)) {
493 if (idx < ARRAY_SIZE(fix_hregs))
494 return fix_hregs + idx;
495 hre_err = HRE_E_INVALID_HREG;
496 } else if (IS_PCR_HREG(spec)) {
497 if (idx < ARRAY_SIZE(pcr_hregs))
498 return pcr_hregs + idx;
499 hre_err = HRE_E_INVALID_HREG;
500 } else if (IS_VAR_HREG(spec)) {
501 if (idx < ARRAY_SIZE(var_hregs))
502 return var_hregs + idx;
503 hre_err = HRE_E_INVALID_HREG;
509 * @brief get pointer of a hash register by specification and usage.
510 * @param spec specification of a hash register
511 * @param mode access mode (read or write or read/write)
512 * @return pointer to hash register if found and valid; NULL else.
514 * This func uses @a get_reg() to determine the hash register for a given spec.
515 * If a register is found it is validated according to the desired access mode.
516 * The value of automatic registers (PCR register and fixed registers) is
517 * loaded or computed on read access.
519 static struct h_reg *access_hreg(uint8_t spec, enum access_mode mode)
521 struct h_reg *result;
523 result = get_hreg(spec);
527 if (mode & HREG_WR) {
528 if (IS_FIX_HREG(spec)) {
529 hre_err = HRE_E_INVALID_HREG;
533 if (mode & HREG_RD) {
534 if (!result->valid) {
535 if (IS_PCR_HREG(spec)) {
536 hre_tpm_err = tpm_pcr_read(HREG_IDX(spec),
538 result->valid = (hre_tpm_err == TPM_SUCCESS);
539 } else if (IS_FIX_HREG(spec)) {
540 switch (HREG_IDX(spec)) {
541 case FIX_HREG_DEVICE_ID_HASH:
544 case FIX_HREG_SELF_HASH:
545 ccdm_compute_self_hash();
547 case FIX_HREG_STAGE2_HASH:
548 compute_second_stage_hash(result);
550 case FIX_HREG_VENDOR:
551 memcpy(result->digest, vendor, 20);
552 result->valid = true;
556 result->valid = true;
559 if (!result->valid) {
560 hre_err = HRE_E_INVALID_HREG;
568 static void *compute_and(void *_dst, const void *_src, size_t n)
571 const uint8_t *src = _src;
574 for (i = n; i-- > 0; )
580 static void *compute_or(void *_dst, const void *_src, size_t n)
583 const uint8_t *src = _src;
586 for (i = n; i-- > 0; )
592 static void *compute_xor(void *_dst, const void *_src, size_t n)
595 const uint8_t *src = _src;
598 for (i = n; i-- > 0; )
604 static void *compute_extend(void *_dst, const void *_src, size_t n)
610 sha1_update(&ctx, _dst, n);
611 sha1_update(&ctx, _src, n);
612 sha1_finish(&ctx, digest);
613 memcpy(_dst, digest, min(n, sizeof(digest)));
618 static int hre_op_loadkey(struct h_reg *src_reg, struct h_reg *dst_reg,
619 const void *key, size_t key_size)
621 uint32_t parent_handle;
624 if (!src_reg || !dst_reg || !src_reg->valid || !dst_reg->valid)
626 if (find_key(src_reg->digest, dst_reg->digest, &parent_handle))
628 hre_tpm_err = tpm_load_key2_oiap(parent_handle, key, key_size,
629 src_reg->digest, &key_handle);
631 hre_err = HRE_E_TPM_FAILURE;
634 /* TODO remember key handle somehow? */
640 * @brief executes the next opcode on the hash register engine.
641 * @param[in,out] ip pointer to the opcode (instruction pointer)
642 * @param[in,out] code_size (remaining) size of the code
643 * @return new instruction pointer on success, NULL on error.
645 static const uint8_t *hre_execute_op(const uint8_t **ip, size_t *code_size)
647 bool dst_modified = false;
653 struct h_reg *src_reg, *dst_reg;
655 const uint8_t *src_buf, *data;
658 void * (*bin_func)(void *, const void *, size_t);
663 ins = get_unaligned_be32(*ip);
666 src_spec = (ins >> 18) & 0x3f;
667 dst_spec = (ins >> 12) & 0x3f;
668 data_size = (ins & 0x7ff);
670 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins,
671 opcode, src_spec, dst_spec, data_size);
673 if ((opcode & 0x80) && (data_size + 4) > *code_size)
676 src_reg = access_hreg(src_spec, HREG_RD);
677 if (hre_err || hre_tpm_err)
679 dst_reg = access_hreg(dst_spec, (opcode & 0x40) ? HREG_RDWR : HREG_WR);
680 if (hre_err || hre_tpm_err)
688 for (i = 0; i < 20; ++i) {
689 if (src_reg->digest[i])
698 bin_func = compute_xor;
701 bin_func = compute_and;
704 bin_func = compute_or;
707 bin_func = compute_extend;
712 src_buf = src_reg->digest;
717 } else if (data_size == 1) {
718 memset(buf, *data, 20);
720 } else if (data_size >= 20) {
724 for (ptr = (uint8_t *)src_buf, i = 20; i > 0;
725 i -= data_size, ptr += data_size)
727 min_t(size_t, i, data_size));
730 bin_func(dst_reg->digest, src_buf, 20);
731 dst_reg->valid = true;
735 if (hre_op_loadkey(src_reg, dst_reg, data, data_size))
742 if (dst_reg && dst_modified && IS_PCR_HREG(dst_spec)) {
743 hre_tpm_err = tpm_extend(HREG_IDX(dst_spec), dst_reg->digest,
746 hre_err = HRE_E_TPM_FAILURE;
755 *code_size -= data_size;
762 * @brief runs a program on the hash register engine.
763 * @param code pointer to the (HRE) code.
764 * @param code_size size of the code (in bytes).
765 * @return 0 on success, != 0 on failure.
767 static int hre_run_program(const uint8_t *code, size_t code_size)
770 const uint8_t *ip = code;
772 code_left = code_size;
775 while (code_left > 0)
776 if (!hre_execute_op(&ip, &code_left))
782 static int check_hmac(struct key_program *hmac,
783 const uint8_t *data, size_t data_size)
785 uint8_t key[20], computed_hmac[20];
788 type = get_unaligned_be32(hmac->code);
791 memset(key, 0, sizeof(key));
792 compute_extend(key, pcr_hregs[1].digest, 20);
793 compute_extend(key, pcr_hregs[2].digest, 20);
794 compute_extend(key, pcr_hregs[3].digest, 20);
795 compute_extend(key, pcr_hregs[4].digest, 20);
797 sha1_hmac(key, sizeof(key), data, data_size, computed_hmac);
799 return memcmp(computed_hmac, hmac->code + 4, 20);
802 static int verify_program(struct key_program *prg)
805 crc = crc32(0, prg->code, prg->code_size);
807 if (crc != prg->code_crc) {
808 printf("HRC crc mismatch: %08x != %08x\n",
815 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
816 static struct key_program *load_sd_key_program(void)
818 u32 code_len, code_offset;
821 struct key_program *result = NULL, *hmac = NULL;
822 struct key_program header;
824 mmc = find_mmc_device(0);
829 if (ccdm_mmc_read(mmc, 0, buf, sizeof(buf)) <= 0)
832 code_offset = *(u32 *)(buf + ESDHC_BOOT_IMAGE_ADDR_OFS);
833 code_len = *(u32 *)(buf + ESDHC_BOOT_IMAGE_SIZE_OFS);
835 code_offset += code_len;
836 /* TODO: the following needs to be the size of the 2nd stage env */
837 code_offset += CONFIG_ENV_SIZE;
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_KEY_PROGRAM)
849 result = malloc(sizeof(struct key_program) + header.code_size);
854 printf("load key program chunk from SD card (%u bytes) ",
857 if (ccdm_mmc_read(mmc, code_offset, result->code, header.code_size)
860 code_offset += header.code_size;
863 if (verify_program(result))
866 if (ccdm_mmc_read(mmc, code_offset, buf, 4*3) < 0)
869 header.magic = get_unaligned_be32(buf);
870 header.code_crc = get_unaligned_be32(buf + 4);
871 header.code_size = get_unaligned_be32(buf + 8);
873 if (header.magic == MAGIC_HMAC) {
874 puts("check integrity\n");
875 hmac = malloc(sizeof(struct key_program) + header.code_size);
880 if (ccdm_mmc_read(mmc, code_offset, hmac->code,
881 hmac->code_size) < 0)
883 if (verify_program(hmac))
885 if (check_hmac(hmac, result->code, result->code_size)) {
886 puts("key program integrity could not be verified\n");
889 puts("key program verified\n");
905 #ifdef CCDM_SECOND_STAGE
907 * @brief load a key program from file system.
908 * @param ifname interface of the file system
909 * @param dev_part_str device part of the file system
910 * @param fs_type tyep of the file system
911 * @param path path of the file to load.
912 * @return the loaded structure or NULL on failure.
914 static struct key_program *load_key_chunk(const char *ifname,
915 const char *dev_part_str, int fs_type,
918 struct key_program *result = NULL;
919 struct key_program header;
924 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
926 if (fs_read(path, (ulong)buf, 0, 12, &i) < 0)
930 header.magic = get_unaligned_be32(buf);
931 header.code_crc = get_unaligned_be32(buf + 4);
932 header.code_size = get_unaligned_be32(buf + 8);
934 if (header.magic != MAGIC_HMAC && header.magic != MAGIC_KEY_PROGRAM)
937 result = malloc(sizeof(struct key_program) + header.code_size);
940 if (fs_set_blk_dev(ifname, dev_part_str, fs_type))
942 if (fs_read(path, (ulong)result, 0,
943 sizeof(struct key_program) + header.code_size, &i) < 0)
949 crc = crc32(0, result->code, result->code_size);
951 if (crc != result->code_crc) {
952 printf("%s: HRC crc mismatch: %08x != %08x\n",
953 path, crc, result->code_crc);
967 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
968 static int first_stage_actions(void)
971 struct key_program *sd_prg = NULL;
973 puts("CCDM S1: start actions\n");
974 #ifndef CCDM_SECOND_STAGE
975 if (tpm_continue_self_test())
978 tpm_continue_self_test();
982 if (hre_run_program(prg_stage1_prepare, sizeof(prg_stage1_prepare)))
985 sd_prg = load_sd_key_program();
987 if (hre_run_program(sd_prg->code, sd_prg->code_size))
989 puts("SD code run successfully\n");
991 puts("no key program found on SD\n");
1000 printf("CCDM S1: actions done (%d)\n", result);
1005 #ifdef CCDM_FIRST_STAGE
1006 static int first_stage_init(void)
1010 if (tpm_init() || tpm_startup(TPM_ST_CLEAR))
1012 res = first_stage_actions();
1013 #ifndef CCDM_SECOND_STAGE
1024 #ifdef CCDM_SECOND_STAGE
1025 static int second_stage_init(void)
1027 static const char mac_suffix[] = ".mac";
1028 bool did_first_stage_run = true;
1030 char *cptr, *mmcdev = NULL;
1031 struct key_program *hmac_blob = NULL;
1032 const char *image_path = "/ccdm.itb";
1033 char *mac_path = NULL;
1038 printf("CCDM S2\n");
1041 err = tpm_startup(TPM_ST_CLEAR);
1042 if (err != TPM_INVALID_POSTINIT)
1043 did_first_stage_run = false;
1045 #ifdef CCDM_AUTO_FIRST_STAGE
1046 if (!did_first_stage_run && first_stage_actions())
1049 if (!did_first_stage_run)
1053 if (hre_run_program(prg_stage2_prepare, sizeof(prg_stage2_prepare)))
1056 /* run "prepboot" from env to get "mmcdev" set */
1057 cptr = getenv("prepboot");
1058 if (cptr && !run_command(cptr, 0))
1059 mmcdev = getenv("mmcdev");
1063 cptr = getenv("ramdiskimage");
1067 mac_path = malloc(strlen(image_path) + strlen(mac_suffix) + 1);
1068 if (mac_path == NULL)
1070 strcpy(mac_path, image_path);
1071 strcat(mac_path, mac_suffix);
1073 /* read image from mmcdev (ccdm.itb) */
1074 image_addr = (ulong)get_image_location();
1075 if (fs_set_blk_dev("mmc", mmcdev, FS_TYPE_EXT))
1077 if (fs_read(image_path, image_addr, 0, 0, &image_size) < 0)
1079 if (image_size <= 0)
1081 printf("CCDM image found on %s, %lld bytes\n", mmcdev, image_size);
1083 hmac_blob = load_key_chunk("mmc", mmcdev, FS_TYPE_EXT, mac_path);
1085 puts("failed to load mac file\n");
1088 if (verify_program(hmac_blob)) {
1089 puts("corrupted mac file\n");
1092 if (check_hmac(hmac_blob, (u8 *)image_addr, image_size)) {
1093 puts("image integrity could not be verified\n");
1096 puts("CCDM image OK\n");
1098 hre_run_program(prg_stage2_success, sizeof(prg_stage2_success));
1103 hre_run_program(prg_stage_fail, sizeof(prg_stage_fail));
1114 int show_self_hash(void)
1116 struct h_reg *hash_ptr;
1117 #ifdef CCDM_SECOND_STAGE
1121 if (compute_self_hash(hash_ptr))
1124 hash_ptr = &fix_hregs[FIX_HREG_SELF_HASH];
1126 puts("self hash: ");
1127 if (hash_ptr && hash_ptr->valid)
1128 print_buffer(0, hash_ptr->digest, 1, 20, 20);
1136 * @brief let the system hang.
1139 * Will stop the boot process; display a message and signal the error condition
1140 * by blinking the "status" and the "finder" LED of the controller board.
1142 * @note the develop version runs the blink cycle 2 times and then returns.
1143 * The release version never returns.
1145 static void ccdm_hang(void)
1147 static const u64 f0 = 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1148 static const u64 s0 = 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1155 I2C_SET_BUS(I2C_SOC_0);
1156 pca9698_direction_output(0x22, 0, 0); /* Finder */
1157 pca9698_direction_output(0x22, 4, 0); /* Status */
1159 puts("### ERROR ### Please RESET the board ###\n");
1160 bootstage_error(BOOTSTAGE_ID_NEED_RESET);
1162 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1163 puts("** but we continue since this is a DEVELOP version **\n");
1164 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1165 for (j = 2; j-- > 0;) {
1172 for (i = 54; i-- > 0;) {
1173 pca9698_set_value(0x22, 0, !(f & 1));
1174 pca9698_set_value(0x22, 4, (s & 1));
1180 puts("\ncontinue...\n");
1183 int startup_ccdm_id_module(void)
1186 unsigned int orig_i2c_bus;
1188 orig_i2c_bus = i2c_get_bus_num();
1189 i2c_set_bus_num(I2C_SOC_1);
1196 #ifdef CCDM_FIRST_STAGE
1197 result = first_stage_init();
1199 puts("1st stage init failed\n");
1203 #ifdef CCDM_SECOND_STAGE
1204 result = second_stage_init();
1206 puts("2nd stage init failed\n");
1215 i2c_set_bus_num(orig_i2c_bus);