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