blob: 7ffb315bc9352b2644c913323bfc890ffa2f12f5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Aneesh Bansalc4713ec2016-01-22 16:37:25 +05302/*
3 * Copyright 2015 Freescale Semiconductor, Inc.
Aneesh Bansalc4713ec2016-01-22 16:37:25 +05304 */
5
6#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06007#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06008#include <env.h>
Ovidiu Panait7dbb0212022-01-01 19:13:29 +02009#include <init.h>
Aneesh Bansalc4713ec2016-01-22 16:37:25 +053010#include <fsl_validate.h>
Sumit Gargf6d96cb2016-07-14 12:27:51 -040011#include <fsl_secboot_err.h>
Aneesh Bansalc4713ec2016-01-22 16:37:25 +053012#include <fsl_sfp.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Sumit Gargf6d96cb2016-07-14 12:27:51 -040014#include <dm/root.h>
15
Sumit Gargbdddd6e2016-06-14 13:52:38 -040016#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_FRAMEWORK)
17#include <spl.h>
18#endif
19
Sumit Gargf6d96cb2016-07-14 12:27:51 -040020#ifdef CONFIG_FSL_CORENET
21#include <asm/fsl_pamu.h>
22#endif
Aneesh Bansalc4713ec2016-01-22 16:37:25 +053023
York Sunc4f047c2017-03-27 11:41:03 -070024#ifdef CONFIG_ARCH_LS1021A
Aneesh Bansalc4713ec2016-01-22 16:37:25 +053025#include <asm/arch/immap_ls102xa.h>
26#endif
27
28#if defined(CONFIG_MPC85xx)
29#define CONFIG_DCFG_ADDR CONFIG_SYS_MPC85xx_GUTS_ADDR
30#else
31#define CONFIG_DCFG_ADDR CONFIG_SYS_FSL_GUTS_ADDR
32#endif
33
34#ifdef CONFIG_SYS_FSL_CCSR_GUR_LE
35#define gur_in32(a) in_le32(a)
36#else
37#define gur_in32(a) in_be32(a)
38#endif
39
40/* Check the Boot Mode. If Secure, return 1 else return 0 */
41int fsl_check_boot_mode_secure(void)
42{
43 uint32_t val;
44 struct ccsr_sfp_regs *sfp_regs = (void *)(CONFIG_SYS_SFP_ADDR);
45 struct ccsr_gur __iomem *gur = (void *)(CONFIG_DCFG_ADDR);
46
47 val = sfp_in32(&sfp_regs->ospr) & ITS_MASK;
48 if (val == ITS_MASK)
49 return 1;
50
51#if defined(CONFIG_FSL_CORENET) || !defined(CONFIG_MPC85xx)
52 /* For PBL based platforms check the SB_EN bit in RCWSR */
53 val = gur_in32(&gur->rcwsr[RCW_SB_EN_REG_INDEX - 1]) & RCW_SB_EN_MASK;
54 if (val == RCW_SB_EN_MASK)
55 return 1;
56#endif
57
58#if defined(CONFIG_MPC85xx) && !defined(CONFIG_FSL_CORENET)
59 /* For Non-PBL Platforms, check the Device Status register 2*/
60 val = gur_in32(&gur->pordevsr2) & MPC85xx_PORDEVSR2_SBC_MASK;
61 if (val != MPC85xx_PORDEVSR2_SBC_MASK)
62 return 1;
63
64#endif
65 return 0;
66}
Aneesh Bansal39d5b3b2016-01-22 16:37:26 +053067
Sumit Gargf6d96cb2016-07-14 12:27:51 -040068#ifndef CONFIG_SPL_BUILD
Aneesh Bansal39d5b3b2016-01-22 16:37:26 +053069int fsl_setenv_chain_of_trust(void)
70{
71 /* Check Boot Mode
72 * If Boot Mode is Non-Secure, no changes are required
73 */
74 if (fsl_check_boot_mode_secure() == 0)
75 return 0;
76
77 /* If Boot mode is Secure, set the environment variables
78 * bootdelay = 0 (To disable Boot Prompt)
79 * bootcmd = CONFIG_CHAIN_BOOT_CMD (Validate and execute Boot script)
80 */
Udit Agarwal9bca6622019-06-11 09:37:49 +000081 env_set("bootdelay", "-2");
Sumit Garg9cbcc4d2017-06-05 23:51:51 +053082
83#ifdef CONFIG_ARM
Simon Glass6a38e412017-08-03 12:22:09 -060084 env_set("secureboot", "y");
Sumit Garg9cbcc4d2017-06-05 23:51:51 +053085#else
Simon Glass6a38e412017-08-03 12:22:09 -060086 env_set("bootcmd", CONFIG_CHAIN_BOOT_CMD);
Sumit Garg9cbcc4d2017-06-05 23:51:51 +053087#endif
88
Aneesh Bansal39d5b3b2016-01-22 16:37:26 +053089 return 0;
90}
Sumit Gargf6d96cb2016-07-14 12:27:51 -040091#endif
92
93#ifdef CONFIG_SPL_BUILD
94void spl_validate_uboot(uint32_t hdr_addr, uintptr_t img_addr)
95{
96 int res;
97
98 /*
99 * Check Boot Mode
100 * If Boot Mode is Non-Secure, skip validation
101 */
102 if (fsl_check_boot_mode_secure() == 0)
103 return;
104
105 printf("SPL: Validating U-Boot image\n");
106
107#ifdef CONFIG_ADDR_MAP
108 init_addr_map();
109#endif
110
111#ifdef CONFIG_FSL_CORENET
112 if (pamu_init() < 0)
113 fsl_secboot_handle_error(ERROR_ESBC_PAMU_INIT);
114#endif
115
116#ifdef CONFIG_FSL_CAAM
117 if (sec_init() < 0)
118 fsl_secboot_handle_error(ERROR_ESBC_SEC_INIT);
119#endif
120
121/*
122 * dm_init_and_scan() is called as part of common SPL framework, so no
123 * need to call it again but in case of powerpc platforms which currently
124 * do not use common SPL framework, so need to call this function here.
125 */
126#if defined(CONFIG_SPL_DM) && (!defined(CONFIG_SPL_FRAMEWORK))
Sumit Gargbdddd6e2016-06-14 13:52:38 -0400127 dm_init_and_scan(true);
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400128#endif
129 res = fsl_secboot_validate(hdr_addr, CONFIG_SPL_UBOOT_KEY_HASH,
130 &img_addr);
131
132 if (res == 0)
133 printf("SPL: Validation of U-boot successful\n");
134}
Sumit Gargbdddd6e2016-06-14 13:52:38 -0400135
136#ifdef CONFIG_SPL_FRAMEWORK
137/* Override weak funtion defined in SPL framework to enable validation
138 * of main u-boot image before jumping to u-boot image.
139 */
140void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
141{
142 typedef void __noreturn (*image_entry_noargs_t)(void);
143 uint32_t hdr_addr;
144
145 image_entry_noargs_t image_entry =
146 (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
147
148 hdr_addr = (spl_image->entry_point + spl_image->size -
149 CONFIG_U_BOOT_HDR_SIZE);
150 spl_validate_uboot(hdr_addr, (uintptr_t)spl_image->entry_point);
151 /*
152 * In case of failure in validation, spl_validate_uboot would
153 * not return back in case of Production environment with ITS=1.
154 * Thus U-Boot will not start.
155 * In Development environment (ITS=0 and SB_EN=1), the function
156 * may return back in case of non-fatal failures.
157 */
158
Tom Rinif1c2fc02017-01-11 10:45:48 -0500159 debug("image entry point: 0x%lX\n", spl_image->entry_point);
Sumit Gargbdddd6e2016-06-14 13:52:38 -0400160 image_entry();
161}
162#endif /* ifdef CONFIG_SPL_FRAMEWORK */
Sumit Gargf6d96cb2016-07-14 12:27:51 -0400163#endif /* ifdef CONFIG_SPL_BUILD */