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