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