xref: /rk3399_ARM-atf/bl2/bl2_main.c (revision ae770fedf459d5643125d29f48659e3e936ebd2d)
1 /*
2  * Copyright (c) 2013-2024, 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  ******************************************************************************/
41 void bl2_el3_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
42 		   u_register_t arg3)
43 {
44 	/* Enable early console if EARLY_CONSOLE flag is enabled */
45 	plat_setup_early_console();
46 
47 	/* Perform early platform-specific setup */
48 	bl2_el3_early_platform_setup(arg0, arg1, arg2, arg3);
49 
50 	/* Perform late platform-specific setup */
51 	bl2_el3_plat_arch_setup();
52 
53 #if CTX_INCLUDE_PAUTH_REGS
54 	/*
55 	 * Assert that the ARMv8.3-PAuth registers are present or an access
56 	 * fault will be triggered when they are being saved or restored.
57 	 */
58 	assert(is_armv8_3_pauth_present());
59 #endif /* CTX_INCLUDE_PAUTH_REGS */
60 }
61 #else /* RESET_TO_BL2 */
62 
63 /*******************************************************************************
64  * Setup function for BL2 when RESET_TO_BL2=0
65  ******************************************************************************/
66 void bl2_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
67 	       u_register_t arg3)
68 {
69 	/* Enable early console if EARLY_CONSOLE flag is enabled */
70 	plat_setup_early_console();
71 
72 	/* Perform early platform-specific setup */
73 	bl2_early_platform_setup2(arg0, arg1, arg2, arg3);
74 
75 	/* Perform late platform-specific setup */
76 	bl2_plat_arch_setup();
77 
78 #if CTX_INCLUDE_PAUTH_REGS
79 	/*
80 	 * Assert that the ARMv8.3-PAuth registers are present or an access
81 	 * fault will be triggered when they are being saved or restored.
82 	 */
83 	assert(is_armv8_3_pauth_present());
84 #endif /* CTX_INCLUDE_PAUTH_REGS */
85 }
86 #endif /* RESET_TO_BL2 */
87 
88 /*******************************************************************************
89  * The only thing to do in BL2 is to load further images and pass control to
90  * next BL. The memory occupied by BL2 will be reclaimed by BL3x stages. BL2
91  * runs entirely in S-EL1.
92  ******************************************************************************/
93 void bl2_main(void)
94 {
95 	entry_point_info_t *next_bl_ep_info;
96 
97 #if ENABLE_RUNTIME_INSTRUMENTATION
98 	PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_ENTRY, PMF_CACHE_MAINT);
99 #endif
100 
101 	NOTICE("BL2: %s\n", version_string);
102 	NOTICE("BL2: %s\n", build_message);
103 
104 	/* Perform remaining generic architectural setup in S-EL1 */
105 	bl2_arch_setup();
106 
107 #if PSA_FWU_SUPPORT
108 	fwu_init();
109 #endif /* PSA_FWU_SUPPORT */
110 
111 	crypto_mod_init();
112 
113 	/* Initialize authentication module */
114 	auth_mod_init();
115 
116 	/* Initialize the Measured Boot backend */
117 	bl2_plat_mboot_init();
118 
119 	/* Initialize boot source */
120 	bl2_plat_preload_setup();
121 
122 	/* Load the subsequent bootloader images. */
123 	next_bl_ep_info = bl2_load_images();
124 
125 	/* Teardown the Measured Boot backend */
126 	bl2_plat_mboot_finish();
127 
128 #if !BL2_RUNS_AT_EL3
129 #ifndef __aarch64__
130 	/*
131 	 * For AArch32 state BL1 and BL2 share the MMU setup.
132 	 * Given that BL2 does not map BL1 regions, MMU needs
133 	 * to be disabled in order to go back to BL1.
134 	 */
135 	disable_mmu_icache_secure();
136 #endif /* !__aarch64__ */
137 
138 #if ENABLE_PAUTH
139 	/*
140 	 * Disable pointer authentication before running next boot image
141 	 */
142 	pauth_disable_el1();
143 #endif /* ENABLE_PAUTH */
144 
145 #if ENABLE_RUNTIME_INSTRUMENTATION
146 	PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_EXIT, PMF_CACHE_MAINT);
147 #endif
148 
149 	console_flush();
150 
151 	/*
152 	 * Run next BL image via an SMC to BL1. Information on how to pass
153 	 * control to the BL32 (if present) and BL33 software images will
154 	 * be passed to next BL image as an argument.
155 	 */
156 	smc(BL1_SMC_RUN_IMAGE, (unsigned long)next_bl_ep_info, 0, 0, 0, 0, 0, 0);
157 #else /* if BL2_RUNS_AT_EL3 */
158 
159 	NOTICE("BL2: Booting " NEXT_IMAGE "\n");
160 	print_entry_point_info(next_bl_ep_info);
161 #if ENABLE_RUNTIME_INSTRUMENTATION
162 	PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_EXIT, PMF_CACHE_MAINT);
163 #endif
164 	console_flush();
165 
166 #if ENABLE_PAUTH
167 	/*
168 	 * Disable pointer authentication before running next boot image
169 	 */
170 	pauth_disable_el3();
171 #endif /* ENABLE_PAUTH */
172 
173 	bl2_run_next_image(next_bl_ep_info);
174 #endif /* BL2_RUNS_AT_EL3 */
175 }
176