blob: 4ff73d7dea81df7dd2122b733c922775904216fb [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Boyan Karatotevf39b14c2025-03-26 15:30:42 +00002 * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
Alexei Fedorovf41355c2019-09-13 14:11:59 +01007#include <assert.h>
8
Achin Gupta4f6ad662013-10-25 09:08:21 +01009#include <arch_helpers.h>
Alexei Fedorovf41355c2019-09-13 14:11:59 +010010#include <arch_features.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <bl1/bl1.h>
12#include <bl2/bl2.h>
13#include <common/bl_common.h>
Chris Kay99b5b2e2024-03-08 16:08:31 +000014#include <common/build_message.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <common/debug.h>
16#include <drivers/auth/auth_mod.h>
Manish V Badarkhe92de80a2021-12-16 10:41:47 +000017#include <drivers/auth/crypto_mod.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <drivers/console.h>
Manish V Badarkhe99575e42021-06-25 23:28:59 +010019#include <drivers/fwu/fwu.h>
thagon01-arm6805e8d2023-07-12 10:43:58 -050020#include <lib/bootmarker_capture.h>
Alexei Fedorovf41355c2019-09-13 14:11:59 +010021#include <lib/extensions/pauth.h>
thagon01-arm6805e8d2023-07-12 10:43:58 -050022#include <lib/pmf/pmf.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <plat/common/platform.h>
24
Dan Handleybcd60ba2014-04-17 18:53:42 +010025#include "bl2_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010026
Julius Werner8e0ef0f2019-07-09 14:02:43 -070027#ifdef __aarch64__
Roberto Vargase0e99462017-10-30 14:43:43 +000028#define NEXT_IMAGE "BL31"
Julius Werner8e0ef0f2019-07-09 14:02:43 -070029#else
30#define NEXT_IMAGE "BL32"
Roberto Vargase0e99462017-10-30 14:43:43 +000031#endif
Vikram Kanigirida567432014-04-15 18:08:08 +010032
thagon01-arm6805e8d2023-07-12 10:43:58 -050033#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
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -060038#if RESET_TO_BL2
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010039/*******************************************************************************
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -060040 * Setup function for BL2 when RESET_TO_BL2=1
Antonio Nino Diaz6e4b0832019-01-31 10:48:47 +000041 ******************************************************************************/
Zelalem Aweke688fbf72021-07-09 11:37:10 -050042void bl2_el3_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
43 u_register_t arg3)
Antonio Nino Diaz6e4b0832019-01-31 10:48:47 +000044{
Yann Gautier5ae29c02024-01-16 19:39:31 +010045 /* Enable early console if EARLY_CONSOLE flag is enabled */
46 plat_setup_early_console();
47
Antonio Nino Diaz6e4b0832019-01-31 10:48:47 +000048 /* Perform early platform-specific setup */
Zelalem Aweke688fbf72021-07-09 11:37:10 -050049 bl2_el3_early_platform_setup(arg0, arg1, arg2, arg3);
Antonio Nino Diaz6e4b0832019-01-31 10:48:47 +000050
Antonio Nino Diaz6e4b0832019-01-31 10:48:47 +000051 /* Perform late platform-specific setup */
Zelalem Aweke688fbf72021-07-09 11:37:10 -050052 bl2_el3_plat_arch_setup();
Antonio Nino Diaz6e4b0832019-01-31 10:48:47 +000053}
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -060054#else /* RESET_TO_BL2 */
55
Antonio Nino Diaz4f29fb72019-01-31 17:40:44 +000056/*******************************************************************************
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -060057 * Setup function for BL2 when RESET_TO_BL2=0
Antonio Nino Diaz4f29fb72019-01-31 17:40:44 +000058 ******************************************************************************/
Zelalem Aweke688fbf72021-07-09 11:37:10 -050059void bl2_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
60 u_register_t arg3)
Antonio Nino Diaz4f29fb72019-01-31 17:40:44 +000061{
Yann Gautier5ae29c02024-01-16 19:39:31 +010062 /* Enable early console if EARLY_CONSOLE flag is enabled */
63 plat_setup_early_console();
64
Antonio Nino Diaz4f29fb72019-01-31 17:40:44 +000065 /* Perform early platform-specific setup */
Zelalem Aweke688fbf72021-07-09 11:37:10 -050066 bl2_early_platform_setup2(arg0, arg1, arg2, arg3);
Antonio Nino Diaz4f29fb72019-01-31 17:40:44 +000067
Antonio Nino Diaz4f29fb72019-01-31 17:40:44 +000068 /* Perform late platform-specific setup */
Zelalem Aweke688fbf72021-07-09 11:37:10 -050069 bl2_plat_arch_setup();
Antonio Nino Diaz4f29fb72019-01-31 17:40:44 +000070}
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -060071#endif /* RESET_TO_BL2 */
Antonio Nino Diaz6e4b0832019-01-31 10:48:47 +000072
73/*******************************************************************************
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010074 * The only thing to do in BL2 is to load further images and pass control to
Yatharth Kochar51f76f62016-09-12 16:10:33 +010075 * next BL. The memory occupied by BL2 will be reclaimed by BL3x stages. BL2
76 * runs entirely in S-EL1.
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010077 ******************************************************************************/
78void bl2_main(void)
79{
Yatharth Kochar51f76f62016-09-12 16:10:33 +010080 entry_point_info_t *next_bl_ep_info;
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010081
thagon01-arm6805e8d2023-07-12 10:43:58 -050082#if ENABLE_RUNTIME_INSTRUMENTATION
83 PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_ENTRY, PMF_CACHE_MAINT);
84#endif
85
Chris Kay99b5b2e2024-03-08 16:08:31 +000086 NOTICE("BL2: %s\n", build_version_string);
Dan Handley91b624e2014-07-29 17:14:00 +010087 NOTICE("BL2: %s\n", build_message);
88
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010089 /* Perform remaining generic architectural setup in S-EL1 */
90 bl2_arch_setup();
91
Manish V Badarkhe99575e42021-06-25 23:28:59 +010092#if PSA_FWU_SUPPORT
93 fwu_init();
94#endif /* PSA_FWU_SUPPORT */
95
Manish V Badarkhe92de80a2021-12-16 10:41:47 +000096 crypto_mod_init();
97
Juan Castillo9246ab82015-01-28 16:46:57 +000098 /* Initialize authentication module */
Juan Castilloa08a5e72015-05-19 11:54:12 +010099 auth_mod_init();
Juan Castillo9246ab82015-01-28 16:46:57 +0000100
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100101 /* Initialize the Measured Boot backend */
102 bl2_plat_mboot_init();
103
Alexei Fedorov71707b12020-07-13 14:06:47 +0100104 /* Initialize boot source */
Roberto Vargasbc1ae1f2017-09-26 12:53:01 +0100105 bl2_plat_preload_setup();
106
Yatharth Kochar51f76f62016-09-12 16:10:33 +0100107 /* Load the subsequent bootloader images. */
108 next_bl_ep_info = bl2_load_images();
Andrew Thoelkea55566d2014-05-28 22:22:55 +0100109
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100110 /* Teardown the Measured Boot backend */
111 bl2_plat_mboot_finish();
Alexei Fedorov71707b12020-07-13 14:06:47 +0100112
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -0600113#if !BL2_RUNS_AT_EL3
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700114#ifndef __aarch64__
Yatharth Kochardafb2472016-06-30 14:52:12 +0100115 /*
116 * For AArch32 state BL1 and BL2 share the MMU setup.
117 * Given that BL2 does not map BL1 regions, MMU needs
118 * to be disabled in order to go back to BL1.
119 */
120 disable_mmu_icache_secure();
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700121#endif /* !__aarch64__ */
Yatharth Kochardafb2472016-06-30 14:52:12 +0100122
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100123#if ENABLE_PAUTH
124 /*
125 * Disable pointer authentication before running next boot image
126 */
127 pauth_disable_el1();
128#endif /* ENABLE_PAUTH */
129
thagon01-arm6805e8d2023-07-12 10:43:58 -0500130#if ENABLE_RUNTIME_INSTRUMENTATION
131 PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_EXIT, PMF_CACHE_MAINT);
132#endif
133
134 console_flush();
135
Achin Gupta4f6ad662013-10-25 09:08:21 +0100136 /*
Yatharth Kochar51f76f62016-09-12 16:10:33 +0100137 * Run next BL image via an SMC to BL1. Information on how to pass
138 * control to the BL32 (if present) and BL33 software images will
139 * be passed to next BL image as an argument.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100140 */
Yatharth Kochar51f76f62016-09-12 16:10:33 +0100141 smc(BL1_SMC_RUN_IMAGE, (unsigned long)next_bl_ep_info, 0, 0, 0, 0, 0, 0);
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -0600142#else /* if BL2_RUNS_AT_EL3 */
143
Roberto Vargase0e99462017-10-30 14:43:43 +0000144 NOTICE("BL2: Booting " NEXT_IMAGE "\n");
145 print_entry_point_info(next_bl_ep_info);
thagon01-arm6805e8d2023-07-12 10:43:58 -0500146#if ENABLE_RUNTIME_INSTRUMENTATION
147 PMF_CAPTURE_TIMESTAMP(bl_svc, BL2_EXIT, PMF_CACHE_MAINT);
148#endif
Roberto Vargase0e99462017-10-30 14:43:43 +0000149 console_flush();
150
Alexei Fedorovf41355c2019-09-13 14:11:59 +0100151#if ENABLE_PAUTH
152 /*
153 * Disable pointer authentication before running next boot image
154 */
155 pauth_disable_el3();
156#endif /* ENABLE_PAUTH */
157
Roberto Vargase0e99462017-10-30 14:43:43 +0000158 bl2_run_next_image(next_bl_ep_info);
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -0600159#endif /* BL2_RUNS_AT_EL3 */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100160}