1 /*
2  * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <arch_helpers.h>
10 #include <arch_features.h>
11 #include <bl1/bl1.h>
12 #include <bl2/bl2.h>
13 #include <common/bl_common.h>
14 #include <common/debug.h>
15 #include <drivers/auth/auth_mod.h>
16 #include <drivers/auth/crypto_mod.h>
17 #include <drivers/console.h>
18 #include <drivers/fwu/fwu.h>
19 #include <lib/bootmarker_capture.h>
20 #include <lib/extensions/pauth.h>
21 #include <lib/pmf/pmf.h>
22 #include <plat/common/platform.h>
23 
24 #include "bl2_private.h"
25 
26 #ifdef __aarch64__
27 #define NEXT_IMAGE	"BL31"
28 #else
29 #define NEXT_IMAGE	"BL32"
30 #endif
31 
32 #if ENABLE_RUNTIME_INSTRUMENTATION
33 	PMF_REGISTER_SERVICE(bl_svc, PMF_RT_INSTR_SVC_ID,
34 		BL_TOTAL_IDS, PMF_DUMP_ENABLE);
35 #endif
36 
37 #if RESET_TO_BL2
38 /*******************************************************************************
39  * Setup function for BL2 when RESET_TO_BL2=1
40  ******************************************************************************/
bl2_el3_setup(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)41 void bl2_el3_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
42 		   u_register_t arg3)
43 {
44 	/* Perform early platform-specific setup */
45 	bl2_el3_early_platform_setup(arg0, arg1, arg2, arg3);
46 
47 	/* Perform late platform-specific setup */
48 	bl2_el3_plat_arch_setup();
49 
50 #if CTX_INCLUDE_PAUTH_REGS
51 	/*
52 	 * Assert that the ARMv8.3-PAuth registers are present or an access
53 	 * fault will be triggered when they are being saved or restored.
54 	 */
55 	assert(is_armv8_3_pauth_present());
56 #endif /* CTX_INCLUDE_PAUTH_REGS */
57 }
58 #else /* RESET_TO_BL2 */
59 
60 /*******************************************************************************
61  * Setup function for BL2 when RESET_TO_BL2=0
62  ******************************************************************************/
bl2_setup(u_register_t arg0,u_register_t arg1,u_register_t arg2,u_register_t arg3)63 void bl2_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
64 	       u_register_t arg3)
65 {
66 	/* Perform early platform-specific setup */
67 	bl2_early_platform_setup2(arg0, arg1, arg2, arg3);
68 
69 	/* Perform late platform-specific setup */
70 	bl2_plat_arch_setup();
71 
72 #if CTX_INCLUDE_PAUTH_REGS
73 	/*
74 	 * Assert that the ARMv8.3-PAuth registers are present or an access
75 	 * fault will be triggered when they are being saved or restored.
76 	 */
77 	assert(is_armv8_3_pauth_present());
78 #endif /* CTX_INCLUDE_PAUTH_REGS */
79 }
80 #endif /* RESET_TO_BL2 */
81 
82 /*******************************************************************************
83  * The only thing to do in BL2 is to load further images and pass control to
84  * next BL. The memory occupied by BL2 will be reclaimed by BL3x stages. BL2
85  * runs entirely in S-EL1.
86  ******************************************************************************/
bl2_main(void)87 void bl2_main(void)
88 {
89 	entry_point_info_t *next_bl_ep_info;
90 
91 #if ENABLE_RUNTIME_INSTRUMENTATION
92 	PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_ENTRY, PMF_CACHE_MAINT);
93 #endif
94 
95 	NOTICE("BL2: %s\n", version_string);
96 	NOTICE("BL2: %s\n", build_message);
97 
98 	/* Perform remaining generic architectural setup in S-EL1 */
99 	bl2_arch_setup();
100 
101 #if PSA_FWU_SUPPORT
102 	fwu_init();
103 #endif /* PSA_FWU_SUPPORT */
104 
105 	crypto_mod_init();
106 
107 	/* Initialize authentication module */
108 	auth_mod_init();
109 
110 	/* Initialize the Measured Boot backend */
111 	bl2_plat_mboot_init();
112 
113 	/* Initialize boot source */
114 	bl2_plat_preload_setup();
115 
116 	/* Load the subsequent bootloader images. */
117 	next_bl_ep_info = bl2_load_images();
118 
119 	/* Teardown the Measured Boot backend */
120 	bl2_plat_mboot_finish();
121 
122 #if !BL2_RUNS_AT_EL3
123 #ifndef __aarch64__
124 	/*
125 	 * For AArch32 state BL1 and BL2 share the MMU setup.
126 	 * Given that BL2 does not map BL1 regions, MMU needs
127 	 * to be disabled in order to go back to BL1.
128 	 */
129 	disable_mmu_icache_secure();
130 #endif /* !__aarch64__ */
131 
132 #if ENABLE_PAUTH
133 	/*
134 	 * Disable pointer authentication before running next boot image
135 	 */
136 	pauth_disable_el1();
137 #endif /* ENABLE_PAUTH */
138 
139 #if ENABLE_RUNTIME_INSTRUMENTATION
140 	PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_EXIT, PMF_CACHE_MAINT);
141 #endif
142 
143 	console_flush();
144 
145 	/*
146 	 * Run next BL image via an SMC to BL1. Information on how to pass
147 	 * control to the BL32 (if present) and BL33 software images will
148 	 * be passed to next BL image as an argument.
149 	 */
150 	smc(BL1_SMC_RUN_IMAGE, (unsigned long)next_bl_ep_info, 0, 0, 0, 0, 0, 0);
151 #else /* if BL2_RUNS_AT_EL3 */
152 
153 	NOTICE("BL2: Booting " NEXT_IMAGE "\n");
154 	print_entry_point_info(next_bl_ep_info);
155 #if ENABLE_RUNTIME_INSTRUMENTATION
156 	PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_EXIT, PMF_CACHE_MAINT);
157 #endif
158 	console_flush();
159 
160 #if ENABLE_PAUTH
161 	/*
162 	 * Disable pointer authentication before running next boot image
163 	 */
164 	pauth_disable_el3();
165 #endif /* ENABLE_PAUTH */
166 
167 	bl2_run_next_image(next_bl_ep_info);
168 #endif /* BL2_RUNS_AT_EL3 */
169 }
170