xref: /rk3399_rockchip-uboot/board/freescale/common/fsl_chain_of_trust.c (revision 382bee57f19b4454e2015bc19a010bc2d0ab9337)
10a6b2714SAneesh Bansal /*
20a6b2714SAneesh Bansal  * Copyright 2015 Freescale Semiconductor, Inc.
30a6b2714SAneesh Bansal  *
40a6b2714SAneesh Bansal  * SPDX-License-Identifier:	GPL-2.0+
50a6b2714SAneesh Bansal  */
60a6b2714SAneesh Bansal 
70a6b2714SAneesh Bansal #include <common.h>
89d922450SSimon Glass #include <dm.h>
90a6b2714SAneesh Bansal #include <fsl_validate.h>
108f01397bSSumit Garg #include <fsl_secboot_err.h>
110a6b2714SAneesh Bansal #include <fsl_sfp.h>
128f01397bSSumit Garg #include <dm/root.h>
138f01397bSSumit Garg 
14028ac8c7SSumit Garg #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_FRAMEWORK)
15028ac8c7SSumit Garg #include <spl.h>
16028ac8c7SSumit Garg #endif
17028ac8c7SSumit Garg 
188f01397bSSumit Garg #ifdef CONFIG_ADDR_MAP
198f01397bSSumit Garg #include <asm/mmu.h>
208f01397bSSumit Garg #endif
218f01397bSSumit Garg 
228f01397bSSumit Garg #ifdef CONFIG_FSL_CORENET
238f01397bSSumit Garg #include <asm/fsl_pamu.h>
248f01397bSSumit Garg #endif
250a6b2714SAneesh Bansal 
2673fb5838SYork Sun #ifdef CONFIG_ARCH_LS1021A
270a6b2714SAneesh Bansal #include <asm/arch/immap_ls102xa.h>
280a6b2714SAneesh Bansal #endif
290a6b2714SAneesh Bansal 
300a6b2714SAneesh Bansal #if defined(CONFIG_MPC85xx)
310a6b2714SAneesh Bansal #define CONFIG_DCFG_ADDR	CONFIG_SYS_MPC85xx_GUTS_ADDR
320a6b2714SAneesh Bansal #else
330a6b2714SAneesh Bansal #define CONFIG_DCFG_ADDR	CONFIG_SYS_FSL_GUTS_ADDR
340a6b2714SAneesh Bansal #endif
350a6b2714SAneesh Bansal 
360a6b2714SAneesh Bansal #ifdef CONFIG_SYS_FSL_CCSR_GUR_LE
370a6b2714SAneesh Bansal #define gur_in32(a)       in_le32(a)
380a6b2714SAneesh Bansal #else
390a6b2714SAneesh Bansal #define gur_in32(a)       in_be32(a)
400a6b2714SAneesh Bansal #endif
410a6b2714SAneesh Bansal 
420a6b2714SAneesh Bansal /* Check the Boot Mode. If Secure, return 1 else return 0 */
fsl_check_boot_mode_secure(void)430a6b2714SAneesh Bansal int fsl_check_boot_mode_secure(void)
440a6b2714SAneesh Bansal {
450a6b2714SAneesh Bansal 	uint32_t val;
460a6b2714SAneesh Bansal 	struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
470a6b2714SAneesh Bansal 	struct ccsr_gur __iomem *gur = (void *)(CONFIG_DCFG_ADDR);
480a6b2714SAneesh Bansal 
490a6b2714SAneesh Bansal 	val = sfp_in32(&sfp_regs->ospr) & ITS_MASK;
500a6b2714SAneesh Bansal 	if (val == ITS_MASK)
510a6b2714SAneesh Bansal 		return 1;
520a6b2714SAneesh Bansal 
530a6b2714SAneesh Bansal #if defined(CONFIG_FSL_CORENET) || !defined(CONFIG_MPC85xx)
540a6b2714SAneesh Bansal 	/* For PBL based platforms check the SB_EN bit in RCWSR */
550a6b2714SAneesh Bansal 	val = gur_in32(&gur->rcwsr[RCW_SB_EN_REG_INDEX - 1]) & RCW_SB_EN_MASK;
560a6b2714SAneesh Bansal 	if (val == RCW_SB_EN_MASK)
570a6b2714SAneesh Bansal 		return 1;
580a6b2714SAneesh Bansal #endif
590a6b2714SAneesh Bansal 
600a6b2714SAneesh Bansal #if defined(CONFIG_MPC85xx) && !defined(CONFIG_FSL_CORENET)
610a6b2714SAneesh Bansal 	/* For Non-PBL Platforms, check the Device Status register 2*/
620a6b2714SAneesh Bansal 	val = gur_in32(&gur->pordevsr2) & MPC85xx_PORDEVSR2_SBC_MASK;
630a6b2714SAneesh Bansal 	if (val != MPC85xx_PORDEVSR2_SBC_MASK)
640a6b2714SAneesh Bansal 		return 1;
650a6b2714SAneesh Bansal 
660a6b2714SAneesh Bansal #endif
670a6b2714SAneesh Bansal 	return 0;
680a6b2714SAneesh Bansal }
69d0412885SAneesh Bansal 
708f01397bSSumit Garg #ifndef CONFIG_SPL_BUILD
fsl_setenv_chain_of_trust(void)71d0412885SAneesh Bansal int fsl_setenv_chain_of_trust(void)
72d0412885SAneesh Bansal {
73d0412885SAneesh Bansal 	/* Check Boot Mode
74d0412885SAneesh Bansal 	 * If Boot Mode is Non-Secure, no changes are required
75d0412885SAneesh Bansal 	 */
76d0412885SAneesh Bansal 	if (fsl_check_boot_mode_secure() == 0)
77d0412885SAneesh Bansal 		return 0;
78d0412885SAneesh Bansal 
79d0412885SAneesh Bansal 	/* If Boot mode is Secure, set the environment variables
80d0412885SAneesh Bansal 	 * bootdelay = 0 (To disable Boot Prompt)
81d0412885SAneesh Bansal 	 * bootcmd = CONFIG_CHAIN_BOOT_CMD (Validate and execute Boot script)
82d0412885SAneesh Bansal 	 */
83*382bee57SSimon Glass 	env_set("bootdelay", "0");
8476bbf1c6SSumit Garg 
8576bbf1c6SSumit Garg #ifdef CONFIG_ARM
86*382bee57SSimon Glass 	env_set("secureboot", "y");
8776bbf1c6SSumit Garg #else
88*382bee57SSimon Glass 	env_set("bootcmd", CONFIG_CHAIN_BOOT_CMD);
8976bbf1c6SSumit Garg #endif
9076bbf1c6SSumit Garg 
91d0412885SAneesh Bansal 	return 0;
92d0412885SAneesh Bansal }
938f01397bSSumit Garg #endif
948f01397bSSumit Garg 
958f01397bSSumit Garg #ifdef CONFIG_SPL_BUILD
spl_validate_uboot(uint32_t hdr_addr,uintptr_t img_addr)968f01397bSSumit Garg void spl_validate_uboot(uint32_t hdr_addr, uintptr_t img_addr)
978f01397bSSumit Garg {
988f01397bSSumit Garg 	int res;
998f01397bSSumit Garg 
1008f01397bSSumit Garg 	/*
1018f01397bSSumit Garg 	 * Check Boot Mode
1028f01397bSSumit Garg 	 * If Boot Mode is Non-Secure, skip validation
1038f01397bSSumit Garg 	 */
1048f01397bSSumit Garg 	if (fsl_check_boot_mode_secure() == 0)
1058f01397bSSumit Garg 		return;
1068f01397bSSumit Garg 
1078f01397bSSumit Garg 	printf("SPL: Validating U-Boot image\n");
1088f01397bSSumit Garg 
1098f01397bSSumit Garg #ifdef CONFIG_ADDR_MAP
1108f01397bSSumit Garg 	init_addr_map();
1118f01397bSSumit Garg #endif
1128f01397bSSumit Garg 
1138f01397bSSumit Garg #ifdef CONFIG_FSL_CORENET
1148f01397bSSumit Garg 	if (pamu_init() < 0)
1158f01397bSSumit Garg 		fsl_secboot_handle_error(ERROR_ESBC_PAMU_INIT);
1168f01397bSSumit Garg #endif
1178f01397bSSumit Garg 
1188f01397bSSumit Garg #ifdef CONFIG_FSL_CAAM
1198f01397bSSumit Garg 	if (sec_init() < 0)
1208f01397bSSumit Garg 		fsl_secboot_handle_error(ERROR_ESBC_SEC_INIT);
1218f01397bSSumit Garg #endif
1228f01397bSSumit Garg 
1238f01397bSSumit Garg /*
1248f01397bSSumit Garg  * dm_init_and_scan() is called as part of common SPL framework, so no
1258f01397bSSumit Garg  * need to call it again but in case of powerpc platforms which currently
1268f01397bSSumit Garg  * do not use common SPL framework, so need to call this function here.
1278f01397bSSumit Garg  */
1288f01397bSSumit Garg #if defined(CONFIG_SPL_DM) && (!defined(CONFIG_SPL_FRAMEWORK))
129028ac8c7SSumit Garg 	dm_init_and_scan(true);
1308f01397bSSumit Garg #endif
1318f01397bSSumit Garg 	res = fsl_secboot_validate(hdr_addr, CONFIG_SPL_UBOOT_KEY_HASH,
1328f01397bSSumit Garg 				   &img_addr);
1338f01397bSSumit Garg 
1348f01397bSSumit Garg 	if (res == 0)
1358f01397bSSumit Garg 		printf("SPL: Validation of U-boot successful\n");
1368f01397bSSumit Garg }
137028ac8c7SSumit Garg 
138028ac8c7SSumit Garg #ifdef CONFIG_SPL_FRAMEWORK
139028ac8c7SSumit Garg /* Override weak funtion defined in SPL framework to enable validation
140028ac8c7SSumit Garg  * of main u-boot image before jumping to u-boot image.
141028ac8c7SSumit Garg  */
jump_to_image_no_args(struct spl_image_info * spl_image)142028ac8c7SSumit Garg void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
143028ac8c7SSumit Garg {
144028ac8c7SSumit Garg 	typedef void __noreturn (*image_entry_noargs_t)(void);
145028ac8c7SSumit Garg 	uint32_t hdr_addr;
146028ac8c7SSumit Garg 
147028ac8c7SSumit Garg 	image_entry_noargs_t image_entry =
148028ac8c7SSumit Garg 		(image_entry_noargs_t)(unsigned long)spl_image->entry_point;
149028ac8c7SSumit Garg 
150028ac8c7SSumit Garg 	hdr_addr = (spl_image->entry_point + spl_image->size -
151028ac8c7SSumit Garg 			CONFIG_U_BOOT_HDR_SIZE);
152028ac8c7SSumit Garg 	spl_validate_uboot(hdr_addr, (uintptr_t)spl_image->entry_point);
153028ac8c7SSumit Garg 	/*
154028ac8c7SSumit Garg 	 * In case of failure in validation, spl_validate_uboot would
155028ac8c7SSumit Garg 	 * not return back in case of Production environment with ITS=1.
156028ac8c7SSumit Garg 	 * Thus U-Boot will not start.
157028ac8c7SSumit Garg 	 * In Development environment (ITS=0 and SB_EN=1), the function
158028ac8c7SSumit Garg 	 * may return back in case of non-fatal failures.
159028ac8c7SSumit Garg 	 */
160028ac8c7SSumit Garg 
1614386feb7STom Rini 	debug("image entry point: 0x%lX\n", spl_image->entry_point);
162028ac8c7SSumit Garg 	image_entry();
163028ac8c7SSumit Garg }
164028ac8c7SSumit Garg #endif /* ifdef CONFIG_SPL_FRAMEWORK */
1658f01397bSSumit Garg #endif /* ifdef CONFIG_SPL_BUILD */
166