xref: /OK3568_Linux_fs/u-boot/board/freescale/common/fsl_chain_of_trust.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2015 Freescale Semiconductor, Inc.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <dm.h>
9*4882a593Smuzhiyun #include <fsl_validate.h>
10*4882a593Smuzhiyun #include <fsl_secboot_err.h>
11*4882a593Smuzhiyun #include <fsl_sfp.h>
12*4882a593Smuzhiyun #include <dm/root.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_FRAMEWORK)
15*4882a593Smuzhiyun #include <spl.h>
16*4882a593Smuzhiyun #endif
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #ifdef CONFIG_ADDR_MAP
19*4882a593Smuzhiyun #include <asm/mmu.h>
20*4882a593Smuzhiyun #endif
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #ifdef CONFIG_FSL_CORENET
23*4882a593Smuzhiyun #include <asm/fsl_pamu.h>
24*4882a593Smuzhiyun #endif
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #ifdef CONFIG_ARCH_LS1021A
27*4882a593Smuzhiyun #include <asm/arch/immap_ls102xa.h>
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #if defined(CONFIG_MPC85xx)
31*4882a593Smuzhiyun #define CONFIG_DCFG_ADDR	CONFIG_SYS_MPC85xx_GUTS_ADDR
32*4882a593Smuzhiyun #else
33*4882a593Smuzhiyun #define CONFIG_DCFG_ADDR	CONFIG_SYS_FSL_GUTS_ADDR
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #ifdef CONFIG_SYS_FSL_CCSR_GUR_LE
37*4882a593Smuzhiyun #define gur_in32(a)       in_le32(a)
38*4882a593Smuzhiyun #else
39*4882a593Smuzhiyun #define gur_in32(a)       in_be32(a)
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* Check the Boot Mode. If Secure, return 1 else return 0 */
fsl_check_boot_mode_secure(void)43*4882a593Smuzhiyun int fsl_check_boot_mode_secure(void)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	uint32_t val;
46*4882a593Smuzhiyun 	struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
47*4882a593Smuzhiyun 	struct ccsr_gur __iomem *gur = (void *)(CONFIG_DCFG_ADDR);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	val = sfp_in32(&sfp_regs->ospr) & ITS_MASK;
50*4882a593Smuzhiyun 	if (val == ITS_MASK)
51*4882a593Smuzhiyun 		return 1;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #if defined(CONFIG_FSL_CORENET) || !defined(CONFIG_MPC85xx)
54*4882a593Smuzhiyun 	/* For PBL based platforms check the SB_EN bit in RCWSR */
55*4882a593Smuzhiyun 	val = gur_in32(&gur->rcwsr[RCW_SB_EN_REG_INDEX - 1]) & RCW_SB_EN_MASK;
56*4882a593Smuzhiyun 	if (val == RCW_SB_EN_MASK)
57*4882a593Smuzhiyun 		return 1;
58*4882a593Smuzhiyun #endif
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #if defined(CONFIG_MPC85xx) && !defined(CONFIG_FSL_CORENET)
61*4882a593Smuzhiyun 	/* For Non-PBL Platforms, check the Device Status register 2*/
62*4882a593Smuzhiyun 	val = gur_in32(&gur->pordevsr2) & MPC85xx_PORDEVSR2_SBC_MASK;
63*4882a593Smuzhiyun 	if (val != MPC85xx_PORDEVSR2_SBC_MASK)
64*4882a593Smuzhiyun 		return 1;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun 	return 0;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #ifndef CONFIG_SPL_BUILD
fsl_setenv_chain_of_trust(void)71*4882a593Smuzhiyun int fsl_setenv_chain_of_trust(void)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	/* Check Boot Mode
74*4882a593Smuzhiyun 	 * If Boot Mode is Non-Secure, no changes are required
75*4882a593Smuzhiyun 	 */
76*4882a593Smuzhiyun 	if (fsl_check_boot_mode_secure() == 0)
77*4882a593Smuzhiyun 		return 0;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/* If Boot mode is Secure, set the environment variables
80*4882a593Smuzhiyun 	 * bootdelay = 0 (To disable Boot Prompt)
81*4882a593Smuzhiyun 	 * bootcmd = CONFIG_CHAIN_BOOT_CMD (Validate and execute Boot script)
82*4882a593Smuzhiyun 	 */
83*4882a593Smuzhiyun 	env_set("bootdelay", "0");
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #ifdef CONFIG_ARM
86*4882a593Smuzhiyun 	env_set("secureboot", "y");
87*4882a593Smuzhiyun #else
88*4882a593Smuzhiyun 	env_set("bootcmd", CONFIG_CHAIN_BOOT_CMD);
89*4882a593Smuzhiyun #endif
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	return 0;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun #endif
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #ifdef CONFIG_SPL_BUILD
spl_validate_uboot(uint32_t hdr_addr,uintptr_t img_addr)96*4882a593Smuzhiyun void spl_validate_uboot(uint32_t hdr_addr, uintptr_t img_addr)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	int res;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	/*
101*4882a593Smuzhiyun 	 * Check Boot Mode
102*4882a593Smuzhiyun 	 * If Boot Mode is Non-Secure, skip validation
103*4882a593Smuzhiyun 	 */
104*4882a593Smuzhiyun 	if (fsl_check_boot_mode_secure() == 0)
105*4882a593Smuzhiyun 		return;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	printf("SPL: Validating U-Boot image\n");
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #ifdef CONFIG_ADDR_MAP
110*4882a593Smuzhiyun 	init_addr_map();
111*4882a593Smuzhiyun #endif
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun #ifdef CONFIG_FSL_CORENET
114*4882a593Smuzhiyun 	if (pamu_init() < 0)
115*4882a593Smuzhiyun 		fsl_secboot_handle_error(ERROR_ESBC_PAMU_INIT);
116*4882a593Smuzhiyun #endif
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #ifdef CONFIG_FSL_CAAM
119*4882a593Smuzhiyun 	if (sec_init() < 0)
120*4882a593Smuzhiyun 		fsl_secboot_handle_error(ERROR_ESBC_SEC_INIT);
121*4882a593Smuzhiyun #endif
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun  * dm_init_and_scan() is called as part of common SPL framework, so no
125*4882a593Smuzhiyun  * need to call it again but in case of powerpc platforms which currently
126*4882a593Smuzhiyun  * do not use common SPL framework, so need to call this function here.
127*4882a593Smuzhiyun  */
128*4882a593Smuzhiyun #if defined(CONFIG_SPL_DM) && (!defined(CONFIG_SPL_FRAMEWORK))
129*4882a593Smuzhiyun 	dm_init_and_scan(true);
130*4882a593Smuzhiyun #endif
131*4882a593Smuzhiyun 	res = fsl_secboot_validate(hdr_addr, CONFIG_SPL_UBOOT_KEY_HASH,
132*4882a593Smuzhiyun 				   &img_addr);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	if (res == 0)
135*4882a593Smuzhiyun 		printf("SPL: Validation of U-boot successful\n");
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun #ifdef CONFIG_SPL_FRAMEWORK
139*4882a593Smuzhiyun /* Override weak funtion defined in SPL framework to enable validation
140*4882a593Smuzhiyun  * of main u-boot image before jumping to u-boot image.
141*4882a593Smuzhiyun  */
jump_to_image_no_args(struct spl_image_info * spl_image)142*4882a593Smuzhiyun void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	typedef void __noreturn (*image_entry_noargs_t)(void);
145*4882a593Smuzhiyun 	uint32_t hdr_addr;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	image_entry_noargs_t image_entry =
148*4882a593Smuzhiyun 		(image_entry_noargs_t)(unsigned long)spl_image->entry_point;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	hdr_addr = (spl_image->entry_point + spl_image->size -
151*4882a593Smuzhiyun 			CONFIG_U_BOOT_HDR_SIZE);
152*4882a593Smuzhiyun 	spl_validate_uboot(hdr_addr, (uintptr_t)spl_image->entry_point);
153*4882a593Smuzhiyun 	/*
154*4882a593Smuzhiyun 	 * In case of failure in validation, spl_validate_uboot would
155*4882a593Smuzhiyun 	 * not return back in case of Production environment with ITS=1.
156*4882a593Smuzhiyun 	 * Thus U-Boot will not start.
157*4882a593Smuzhiyun 	 * In Development environment (ITS=0 and SB_EN=1), the function
158*4882a593Smuzhiyun 	 * may return back in case of non-fatal failures.
159*4882a593Smuzhiyun 	 */
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	debug("image entry point: 0x%lX\n", spl_image->entry_point);
162*4882a593Smuzhiyun 	image_entry();
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun #endif /* ifdef CONFIG_SPL_FRAMEWORK */
165*4882a593Smuzhiyun #endif /* ifdef CONFIG_SPL_BUILD */
166