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 /* 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 const uint8_t vendor
[] = "Guntermann & Drunck";
148 * @brief read a bunch of data from MMC into memory.
150 * @param mmc pointer to the mmc structure to use.
151 * @param src offset where the data starts on MMC/SD device (in bytes).
152 * @param dst pointer to the location where the read data should be stored.
153 * @param size number of bytes to read from the MMC/SD device.
154 * @return number of bytes read or -1 on error.
156 static int ccdm_mmc_read(struct mmc
*mmc
, u64 src
, u8
*dst
, int size
)
160 ulong block_no
, n
, cnt
;
166 blk_len
= mmc
->read_bl_len
;
167 tmp_buf
= malloc(blk_len
);
170 block_no
= src
/ blk_len
;
174 n
= mmc
->block_dev
.block_read(&mmc
->block_dev
, block_no
++, 1,
178 result
= min(size
, (int)(blk_len
- ofs
));
179 memcpy(dst
, tmp_buf
+ ofs
, result
);
183 cnt
= size
/ blk_len
;
185 n
= mmc
->block_dev
.block_read(&mmc
->block_dev
, block_no
, cnt
,
189 size
-= cnt
* blk_len
;
190 result
+= cnt
* blk_len
;
191 dst
+= cnt
* blk_len
;
195 n
= mmc
->block_dev
.block_read(&mmc
->block_dev
, block_no
++, 1,
199 memcpy(dst
, tmp_buf
, size
);
212 * @brief returns a location where the 2nd stage bootloader can be(/ is) placed.
214 * @return pointer to the location for/of the 2nd stage bootloader
216 static u8
*get_2nd_stage_bl_location(ulong target_addr
)
219 #ifdef CCDM_SECOND_STAGE
220 addr
= env_get_ulong("loadaddr", 16, CONFIG_LOADADDR
);
228 #ifdef CCDM_SECOND_STAGE
230 * @brief returns a location where the image can be(/ is) placed.
232 * @return pointer to the location for/of the image
234 static u8
*get_image_location(void)
237 /* TODO use other area? */
238 addr
= env_get_ulong("loadaddr", 16, CONFIG_LOADADDR
);
244 * @brief get the size of a given (TPM) NV area
245 * @param index NV index of the area to get size for
246 * @param size pointer to the size
247 * @return 0 on success, != 0 on error
249 static int get_tpm_nv_size(uint32_t index
, uint32_t *size
)
256 err
= tpm_get_capability(TPM_CAP_NV_INDEX
, index
,
259 printf("tpm_get_capability(CAP_NV_INDEX, %08x) failed: %u\n",
264 /* skip tag and nvIndex */
266 /* skip 2 pcr info fields */
267 v16
= get_unaligned_be16(ptr
);
268 ptr
+= 2 + v16
+ 1 + 20;
269 v16
= get_unaligned_be16(ptr
);
270 ptr
+= 2 + v16
+ 1 + 20;
271 /* skip permission and flags */
274 *size
= get_unaligned_be32(ptr
);
279 * @brief search for a key by usage auth and pub key hash.
280 * @param auth usage auth of the key to search for
281 * @param pubkey_digest (SHA1) hash of the pub key structure of the key
282 * @param[out] handle the handle of the key iff found
283 * @return 0 if key was found in TPM; != 0 if not.
285 static int find_key(const uint8_t auth
[20], const uint8_t pubkey_digest
[20],
289 uint32_t key_handles
[10];
297 /* fetch list of already loaded keys in the TPM */
298 err
= tpm_get_capability(TPM_CAP_HANDLE
, TPM_RT_KEY
, buf
, sizeof(buf
));
301 key_count
= get_unaligned_be16(buf
);
303 for (i
= 0; i
< key_count
; ++i
, ptr
+= 4)
304 key_handles
[i
] = get_unaligned_be32(ptr
);
306 /* now search a(/ the) key which we can access with the given auth */
307 for (i
= 0; i
< key_count
; ++i
) {
308 buf_len
= sizeof(buf
);
309 err
= tpm_get_pub_key_oiap(key_handles
[i
], auth
, buf
, &buf_len
);
310 if (err
&& err
!= TPM_AUTHFAIL
)
314 sha1_csum(buf
, buf_len
, digest
);
315 if (!memcmp(digest
, pubkey_digest
, 20)) {
316 *handle
= key_handles
[i
];
324 * @brief read CCDM common data from TPM NV
325 * @return 0 if CCDM common data was found and read, !=0 if something failed.
327 static int read_common_data(void)
334 if (get_tpm_nv_size(NV_COMMON_DATA_INDEX
, &size
) ||
335 size
< NV_COMMON_DATA_MIN_SIZE
)
337 err
= tpm_nv_read_value(NV_COMMON_DATA_INDEX
,
338 buf
, min(sizeof(buf
), size
));
340 printf("tpm_nv_read_value() failed: %u\n", err
);
344 device_id
= get_unaligned_be64(buf
);
345 device_cl
= get_unaligned_be64(buf
+ 8);
346 device_type
= get_unaligned_be64(buf
+ 16);
349 sha1_update(&ctx
, buf
, 24);
350 sha1_finish(&ctx
, fix_hregs
[FIX_HREG_DEVICE_ID_HASH
].digest
);
351 fix_hregs
[FIX_HREG_DEVICE_ID_HASH
].valid
= true;
353 platform_key_handle
= get_unaligned_be32(buf
+ 24);
359 * @brief compute hash of bootloader itself.
360 * @param[out] dst hash register where the hash should be stored
361 * @return 0 on success, != 0 on failure.
363 * @note MUST be called at a time where the boot loader is accessible at the
364 * configured location (; so take care when code is reallocated).
366 static int compute_self_hash(struct h_reg
*dst
)
368 sha1_csum((const uint8_t *)CONFIG_SYS_MONITOR_BASE
,
369 CONFIG_SYS_MONITOR_LEN
, dst
->digest
);
374 int ccdm_compute_self_hash(void)
376 if (!fix_hregs
[FIX_HREG_SELF_HASH
].valid
)
377 compute_self_hash(&fix_hregs
[FIX_HREG_SELF_HASH
]);
382 * @brief compute the hash of the 2nd stage boot loader (on SD card)
383 * @param[out] dst hash register to store the computed hash
384 * @return 0 on success, != 0 on failure
386 * Determines the size and location of the 2nd stage boot loader on SD card,
387 * loads the 2nd stage boot loader and computes the (SHA1) hash value.
388 * Within the 1st stage boot loader, the 2nd stage boot loader is loaded at
389 * the desired memory location and the variable @a bl2_entry is set.
391 * @note This sets the variable @a bl2_entry to the entry point when the
392 * 2nd stage boot loader is loaded at its configured memory location.
394 static int compute_second_stage_hash(struct h_reg
*dst
)
397 u32 code_len
, code_offset
, target_addr
, exec_entry
;
399 u8
*load_addr
= NULL
;
402 mmc
= find_mmc_device(0);
407 if (ccdm_mmc_read(mmc
, 0, buf
, sizeof(buf
)) < 0)
410 code_offset
= *(u32
*)(buf
+ ESDHC_BOOT_IMAGE_ADDR_OFS
);
411 code_len
= *(u32
*)(buf
+ ESDHC_BOOT_IMAGE_SIZE_OFS
);
412 target_addr
= *(u32
*)(buf
+ ESDHC_BOOT_IMAGE_TARGET_OFS
);
413 exec_entry
= *(u32
*)(buf
+ ESDHC_BOOT_IMAGE_ENTRY_OFS
);
415 load_addr
= get_2nd_stage_bl_location(target_addr
);
416 if (load_addr
== (u8
*)target_addr
)
417 bl2_entry
= (void(*)(void))exec_entry
;
419 if (ccdm_mmc_read(mmc
, code_offset
, load_addr
, code_len
) < 0)
422 sha1_csum(load_addr
, code_len
, dst
->digest
);
434 * @brief get pointer to hash register by specification
435 * @param spec specification of a hash register
436 * @return pointer to hash register or NULL if @a spec does not qualify a
437 * valid hash register; NULL else.
439 static struct h_reg
*get_hreg(uint8_t spec
)
443 idx
= HREG_IDX(spec
);
444 if (IS_FIX_HREG(spec
)) {
445 if (idx
< ARRAY_SIZE(fix_hregs
))
446 return fix_hregs
+ idx
;
447 hre_err
= HRE_E_INVALID_HREG
;
448 } else if (IS_PCR_HREG(spec
)) {
449 if (idx
< ARRAY_SIZE(pcr_hregs
))
450 return pcr_hregs
+ idx
;
451 hre_err
= HRE_E_INVALID_HREG
;
452 } else if (IS_VAR_HREG(spec
)) {
453 if (idx
< ARRAY_SIZE(var_hregs
))
454 return var_hregs
+ idx
;
455 hre_err
= HRE_E_INVALID_HREG
;
461 * @brief get pointer of a hash register by specification and usage.
462 * @param spec specification of a hash register
463 * @param mode access mode (read or write or read/write)
464 * @return pointer to hash register if found and valid; NULL else.
466 * This func uses @a get_reg() to determine the hash register for a given spec.
467 * If a register is found it is validated according to the desired access mode.
468 * The value of automatic registers (PCR register and fixed registers) is
469 * loaded or computed on read access.
471 static struct h_reg
*access_hreg(uint8_t spec
, enum access_mode mode
)
473 struct h_reg
*result
;
475 result
= get_hreg(spec
);
479 if (mode
& HREG_WR
) {
480 if (IS_FIX_HREG(spec
)) {
481 hre_err
= HRE_E_INVALID_HREG
;
485 if (mode
& HREG_RD
) {
486 if (!result
->valid
) {
487 if (IS_PCR_HREG(spec
)) {
488 hre_tpm_err
= tpm_pcr_read(HREG_IDX(spec
),
490 result
->valid
= (hre_tpm_err
== TPM_SUCCESS
);
491 } else if (IS_FIX_HREG(spec
)) {
492 switch (HREG_IDX(spec
)) {
493 case FIX_HREG_DEVICE_ID_HASH
:
496 case FIX_HREG_SELF_HASH
:
497 ccdm_compute_self_hash();
499 case FIX_HREG_STAGE2_HASH
:
500 compute_second_stage_hash(result
);
502 case FIX_HREG_VENDOR
:
503 memcpy(result
->digest
, vendor
, 20);
504 result
->valid
= true;
508 result
->valid
= true;
511 if (!result
->valid
) {
512 hre_err
= HRE_E_INVALID_HREG
;
520 static void *compute_and(void *_dst
, const void *_src
, size_t n
)
523 const uint8_t *src
= _src
;
526 for (i
= n
; i
-- > 0; )
532 static void *compute_or(void *_dst
, const void *_src
, size_t n
)
535 const uint8_t *src
= _src
;
538 for (i
= n
; i
-- > 0; )
544 static void *compute_xor(void *_dst
, const void *_src
, size_t n
)
547 const uint8_t *src
= _src
;
550 for (i
= n
; i
-- > 0; )
556 static void *compute_extend(void *_dst
, const void *_src
, size_t n
)
562 sha1_update(&ctx
, _dst
, n
);
563 sha1_update(&ctx
, _src
, n
);
564 sha1_finish(&ctx
, digest
);
565 memcpy(_dst
, digest
, min(n
, sizeof(digest
)));
570 static int hre_op_loadkey(struct h_reg
*src_reg
, struct h_reg
*dst_reg
,
571 const void *key
, size_t key_size
)
573 uint32_t parent_handle
;
576 if (!src_reg
|| !dst_reg
|| !src_reg
->valid
|| !dst_reg
->valid
)
578 if (find_key(src_reg
->digest
, dst_reg
->digest
, &parent_handle
))
580 hre_tpm_err
= tpm_load_key2_oiap(parent_handle
, key
, key_size
,
581 src_reg
->digest
, &key_handle
);
583 hre_err
= HRE_E_TPM_FAILURE
;
586 /* TODO remember key handle somehow? */
592 * @brief executes the next opcode on the hash register engine.
593 * @param[in,out] ip pointer to the opcode (instruction pointer)
594 * @param[in,out] code_size (remaining) size of the code
595 * @return new instruction pointer on success, NULL on error.
597 static const uint8_t *hre_execute_op(const uint8_t **ip
, size_t *code_size
)
599 bool dst_modified
= false;
605 struct h_reg
*src_reg
, *dst_reg
;
607 const uint8_t *src_buf
, *data
;
610 void * (*bin_func
)(void *, const void *, size_t);
615 ins
= get_unaligned_be32(*ip
);
618 src_spec
= (ins
>> 18) & 0x3f;
619 dst_spec
= (ins
>> 12) & 0x3f;
620 data_size
= (ins
& 0x7ff);
622 debug("HRE: ins=%08x (op=%02x, s=%02x, d=%02x, L=%d)\n", ins
,
623 opcode
, src_spec
, dst_spec
, data_size
);
625 if ((opcode
& 0x80) && (data_size
+ 4) > *code_size
)
628 src_reg
= access_hreg(src_spec
, HREG_RD
);
629 if (hre_err
|| hre_tpm_err
)
631 dst_reg
= access_hreg(dst_spec
, (opcode
& 0x40) ? HREG_RDWR
: HREG_WR
);
632 if (hre_err
|| hre_tpm_err
)
640 for (i
= 0; i
< 20; ++i
) {
641 if (src_reg
->digest
[i
])
650 bin_func
= compute_xor
;
653 bin_func
= compute_and
;
656 bin_func
= compute_or
;
659 bin_func
= compute_extend
;
664 src_buf
= src_reg
->digest
;
669 } else if (data_size
== 1) {
670 memset(buf
, *data
, 20);
672 } else if (data_size
>= 20) {
676 for (ptr
= (uint8_t *)src_buf
, i
= 20; i
> 0;
677 i
-= data_size
, ptr
+= data_size
)
679 min_t(size_t, i
, data_size
));
682 bin_func(dst_reg
->digest
, src_buf
, 20);
683 dst_reg
->valid
= true;
687 if (hre_op_loadkey(src_reg
, dst_reg
, data
, data_size
))
694 if (dst_reg
&& dst_modified
&& IS_PCR_HREG(dst_spec
)) {
695 hre_tpm_err
= tpm_extend(HREG_IDX(dst_spec
), dst_reg
->digest
,
698 hre_err
= HRE_E_TPM_FAILURE
;
707 *code_size
-= data_size
;
714 * @brief runs a program on the hash register engine.
715 * @param code pointer to the (HRE) code.
716 * @param code_size size of the code (in bytes).
717 * @return 0 on success, != 0 on failure.
719 static int hre_run_program(const uint8_t *code
, size_t code_size
)
722 const uint8_t *ip
= code
;
724 code_left
= code_size
;
727 while (code_left
> 0)
728 if (!hre_execute_op(&ip
, &code_left
))
734 static int check_hmac(struct key_program
*hmac
,
735 const uint8_t *data
, size_t data_size
)
737 uint8_t key
[20], computed_hmac
[20];
740 type
= get_unaligned_be32(hmac
->code
);
743 memset(key
, 0, sizeof(key
));
744 compute_extend(key
, pcr_hregs
[1].digest
, 20);
745 compute_extend(key
, pcr_hregs
[2].digest
, 20);
746 compute_extend(key
, pcr_hregs
[3].digest
, 20);
747 compute_extend(key
, pcr_hregs
[4].digest
, 20);
749 sha1_hmac(key
, sizeof(key
), data
, data_size
, computed_hmac
);
751 return memcmp(computed_hmac
, hmac
->code
+ 4, 20);
754 static int verify_program(struct key_program
*prg
)
757 crc
= crc32(0, prg
->code
, prg
->code_size
);
759 if (crc
!= prg
->code_crc
) {
760 printf("HRC crc mismatch: %08x != %08x\n",
767 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
768 static struct key_program
*load_sd_key_program(void)
770 u32 code_len
, code_offset
;
773 struct key_program
*result
= NULL
, *hmac
= NULL
;
774 struct key_program header
;
776 mmc
= find_mmc_device(0);
781 if (ccdm_mmc_read(mmc
, 0, buf
, sizeof(buf
)) <= 0)
784 code_offset
= *(u32
*)(buf
+ ESDHC_BOOT_IMAGE_ADDR_OFS
);
785 code_len
= *(u32
*)(buf
+ ESDHC_BOOT_IMAGE_SIZE_OFS
);
787 code_offset
+= code_len
;
788 /* TODO: the following needs to be the size of the 2nd stage env */
789 code_offset
+= CONFIG_ENV_SIZE
;
791 if (ccdm_mmc_read(mmc
, code_offset
, buf
, 4*3) < 0)
794 header
.magic
= get_unaligned_be32(buf
);
795 header
.code_crc
= get_unaligned_be32(buf
+ 4);
796 header
.code_size
= get_unaligned_be32(buf
+ 8);
798 if (header
.magic
!= MAGIC_KEY_PROGRAM
)
801 result
= malloc(sizeof(struct key_program
) + header
.code_size
);
806 printf("load key program chunk from SD card (%u bytes) ",
809 if (ccdm_mmc_read(mmc
, code_offset
, result
->code
, header
.code_size
)
812 code_offset
+= header
.code_size
;
815 if (verify_program(result
))
818 if (ccdm_mmc_read(mmc
, code_offset
, buf
, 4*3) < 0)
821 header
.magic
= get_unaligned_be32(buf
);
822 header
.code_crc
= get_unaligned_be32(buf
+ 4);
823 header
.code_size
= get_unaligned_be32(buf
+ 8);
825 if (header
.magic
== MAGIC_HMAC
) {
826 puts("check integrity\n");
827 hmac
= malloc(sizeof(struct key_program
) + header
.code_size
);
832 if (ccdm_mmc_read(mmc
, code_offset
, hmac
->code
,
833 hmac
->code_size
) < 0)
835 if (verify_program(hmac
))
837 if (check_hmac(hmac
, result
->code
, result
->code_size
)) {
838 puts("key program integrity could not be verified\n");
841 puts("key program verified\n");
857 #ifdef CCDM_SECOND_STAGE
859 * @brief load a key program from file system.
860 * @param ifname interface of the file system
861 * @param dev_part_str device part of the file system
862 * @param fs_type tyep of the file system
863 * @param path path of the file to load.
864 * @return the loaded structure or NULL on failure.
866 static struct key_program
*load_key_chunk(const char *ifname
,
867 const char *dev_part_str
, int fs_type
,
870 struct key_program
*result
= NULL
;
871 struct key_program header
;
876 if (fs_set_blk_dev(ifname
, dev_part_str
, fs_type
))
878 if (fs_read(path
, (ulong
)buf
, 0, 12, &i
) < 0)
882 header
.magic
= get_unaligned_be32(buf
);
883 header
.code_crc
= get_unaligned_be32(buf
+ 4);
884 header
.code_size
= get_unaligned_be32(buf
+ 8);
886 if (header
.magic
!= MAGIC_HMAC
&& header
.magic
!= MAGIC_KEY_PROGRAM
)
889 result
= malloc(sizeof(struct key_program
) + header
.code_size
);
892 if (fs_set_blk_dev(ifname
, dev_part_str
, fs_type
))
894 if (fs_read(path
, (ulong
)result
, 0,
895 sizeof(struct key_program
) + header
.code_size
, &i
) < 0)
901 crc
= crc32(0, result
->code
, result
->code_size
);
903 if (crc
!= result
->code_crc
) {
904 printf("%s: HRC crc mismatch: %08x != %08x\n",
905 path
, crc
, result
->code_crc
);
919 #if defined(CCDM_FIRST_STAGE) || (defined CCDM_AUTO_FIRST_STAGE)
920 static const uint8_t prg_stage1_prepare
[] = {
921 0x00, 0x20, 0x00, 0x00, /* opcode: SYNC f0 */
922 0x00, 0x24, 0x00, 0x00, /* opcode: SYNC f1 */
923 0x01, 0x80, 0x00, 0x00, /* opcode: CHECK0 PCR0 */
924 0x81, 0x22, 0x00, 0x00, /* opcode: LOAD PCR0, f0 */
925 0x01, 0x84, 0x00, 0x00, /* opcode: CHECK0 PCR1 */
926 0x81, 0x26, 0x10, 0x00, /* opcode: LOAD PCR1, f1 */
927 0x01, 0x88, 0x00, 0x00, /* opcode: CHECK0 PCR2 */
928 0x81, 0x2a, 0x20, 0x00, /* opcode: LOAD PCR2, f2 */
929 0x01, 0x8c, 0x00, 0x00, /* opcode: CHECK0 PCR3 */
930 0x81, 0x2e, 0x30, 0x00, /* opcode: LOAD PCR3, f3 */
933 static int first_stage_actions(void)
936 struct key_program
*sd_prg
= NULL
;
938 puts("CCDM S1: start actions\n");
939 #ifndef CCDM_SECOND_STAGE
940 if (tpm_continue_self_test())
943 tpm_continue_self_test();
947 if (hre_run_program(prg_stage1_prepare
, sizeof(prg_stage1_prepare
)))
950 sd_prg
= load_sd_key_program();
952 if (hre_run_program(sd_prg
->code
, sd_prg
->code_size
))
954 puts("SD code run successfully\n");
956 puts("no key program found on SD\n");
965 printf("CCDM S1: actions done (%d)\n", result
);
970 #ifdef CCDM_FIRST_STAGE
971 static int first_stage_init(void)
975 if (tpm_init() || tpm_startup(TPM_ST_CLEAR
))
977 res
= first_stage_actions();
978 #ifndef CCDM_SECOND_STAGE
989 #ifdef CCDM_SECOND_STAGE
990 static const uint8_t prg_stage2_prepare
[] = {
991 0x00, 0x80, 0x00, 0x00, /* opcode: SYNC PCR0 */
992 0x00, 0x84, 0x00, 0x00, /* opcode: SYNC PCR1 */
993 0x00, 0x88, 0x00, 0x00, /* opcode: SYNC PCR2 */
994 0x00, 0x8c, 0x00, 0x00, /* opcode: SYNC PCR3 */
995 0x00, 0x90, 0x00, 0x00, /* opcode: SYNC PCR4 */
998 static const uint8_t prg_stage2_success
[] = {
999 0x81, 0x02, 0x40, 0x14, /* opcode: LOAD PCR4, #<20B data> */
1000 0x48, 0xfd, 0x95, 0x17, 0xe7, 0x54, 0x6b, 0x68, /* data */
1001 0x92, 0x31, 0x18, 0x05, 0xf8, 0x58, 0x58, 0x3c, /* data */
1002 0xe4, 0xd2, 0x81, 0xe0, /* data */
1005 static const uint8_t prg_stage_fail
[] = {
1006 0x81, 0x01, 0x00, 0x14, /* opcode: LOAD v0, #<20B data> */
1007 0xc0, 0x32, 0xad, 0xc1, 0xff, 0x62, 0x9c, 0x9b, /* data */
1008 0x66, 0xf2, 0x27, 0x49, 0xad, 0x66, 0x7e, 0x6b, /* data */
1009 0xea, 0xdf, 0x14, 0x4b, /* data */
1010 0x81, 0x42, 0x30, 0x00, /* opcode: LOAD PCR3, v0 */
1011 0x81, 0x42, 0x40, 0x00, /* opcode: LOAD PCR4, v0 */
1014 static int second_stage_init(void)
1016 static const char mac_suffix
[] = ".mac";
1017 bool did_first_stage_run
= true;
1019 char *cptr
, *mmcdev
= NULL
;
1020 struct key_program
*hmac_blob
= NULL
;
1021 const char *image_path
= "/ccdm.itb";
1022 char *mac_path
= NULL
;
1027 printf("CCDM S2\n");
1030 err
= tpm_startup(TPM_ST_CLEAR
);
1031 if (err
!= TPM_INVALID_POSTINIT
)
1032 did_first_stage_run
= false;
1034 #ifdef CCDM_AUTO_FIRST_STAGE
1035 if (!did_first_stage_run
&& first_stage_actions())
1038 if (!did_first_stage_run
)
1042 if (hre_run_program(prg_stage2_prepare
, sizeof(prg_stage2_prepare
)))
1045 /* run "prepboot" from env to get "mmcdev" set */
1046 cptr
= env_get("prepboot");
1047 if (cptr
&& !run_command(cptr
, 0))
1048 mmcdev
= env_get("mmcdev");
1052 cptr
= env_get("ramdiskimage");
1056 mac_path
= malloc(strlen(image_path
) + strlen(mac_suffix
) + 1);
1057 if (mac_path
== NULL
)
1059 strcpy(mac_path
, image_path
);
1060 strcat(mac_path
, mac_suffix
);
1062 /* read image from mmcdev (ccdm.itb) */
1063 image_addr
= (ulong
)get_image_location();
1064 if (fs_set_blk_dev("mmc", mmcdev
, FS_TYPE_EXT
))
1066 if (fs_read(image_path
, image_addr
, 0, 0, &image_size
) < 0)
1068 if (image_size
<= 0)
1070 printf("CCDM image found on %s, %lld bytes\n", mmcdev
, image_size
);
1072 hmac_blob
= load_key_chunk("mmc", mmcdev
, FS_TYPE_EXT
, mac_path
);
1074 puts("failed to load mac file\n");
1077 if (verify_program(hmac_blob
)) {
1078 puts("corrupted mac file\n");
1081 if (check_hmac(hmac_blob
, (u8
*)image_addr
, image_size
)) {
1082 puts("image integrity could not be verified\n");
1085 puts("CCDM image OK\n");
1087 hre_run_program(prg_stage2_success
, sizeof(prg_stage2_success
));
1092 hre_run_program(prg_stage_fail
, sizeof(prg_stage_fail
));
1103 int show_self_hash(void)
1105 struct h_reg
*hash_ptr
;
1106 #ifdef CCDM_SECOND_STAGE
1110 if (compute_self_hash(hash_ptr
))
1113 hash_ptr
= &fix_hregs
[FIX_HREG_SELF_HASH
];
1115 puts("self hash: ");
1116 if (hash_ptr
&& hash_ptr
->valid
)
1117 print_buffer(0, hash_ptr
->digest
, 1, 20, 20);
1125 * @brief let the system hang.
1128 * Will stop the boot process; display a message and signal the error condition
1129 * by blinking the "status" and the "finder" LED of the controller board.
1131 * @note the develop version runs the blink cycle 2 times and then returns.
1132 * The release version never returns.
1134 static void ccdm_hang(void)
1136 static const u64 f0
= 0x0ba3bb8ba2e880; /* blink code "finder" LED */
1137 static const u64 s0
= 0x00f0f0f0f0f0f0; /* blink code "status" LED */
1144 I2C_SET_BUS(I2C_SOC_0
);
1145 pca9698_direction_output(0x22, 0, 0); /* Finder */
1146 pca9698_direction_output(0x22, 4, 0); /* Status */
1148 puts("### ERROR ### Please RESET the board ###\n");
1149 bootstage_error(BOOTSTAGE_ID_NEED_RESET
);
1151 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1152 puts("** but we continue since this is a DEVELOP version **\n");
1153 puts("*** ERROR ******** THIS WOULD HANG ******** ERROR ***\n");
1154 for (j
= 2; j
-- > 0;) {
1161 for (i
= 54; i
-- > 0;) {
1162 pca9698_set_value(0x22, 0, !(f
& 1));
1163 pca9698_set_value(0x22, 4, (s
& 1));
1169 puts("\ncontinue...\n");
1172 int startup_ccdm_id_module(void)
1175 unsigned int orig_i2c_bus
;
1177 orig_i2c_bus
= i2c_get_bus_num();
1178 i2c_set_bus_num(I2C_SOC_1
);
1185 #ifdef CCDM_FIRST_STAGE
1186 result
= first_stage_init();
1188 puts("1st stage init failed\n");
1192 #ifdef CCDM_SECOND_STAGE
1193 result
= second_stage_init();
1195 puts("2nd stage init failed\n");
1204 i2c_set_bus_num(orig_i2c_bus
);