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