blob: 26461c17ef673ad91cd6f96b9481182ff571b9e0 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
David Vinczee0a3c2f2019-05-15 16:45:14 +02003 * Copyright (c) 2017-2019 Arm Limited.
Tamas Banf70ef8c2017-12-19 15:35:09 +00004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <assert.h>
Tamas Ban581034a2017-12-19 19:54:37 +000019#include "bl2_util.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000020#include "target.h"
Kevin Pengbc5e5aa2019-10-16 10:55:17 +080021#include "tfm_hal_device_header.h"
Tamas Banc3828852018-02-01 12:24:16 +000022#include "Driver_Flash.h"
Tamas Banbd3f7512018-01-26 15:45:03 +000023#include "mbedtls/memory_buffer_alloc.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000024#include "bootutil/bootutil_log.h"
25#include "bootutil/image.h"
26#include "bootutil/bootutil.h"
27#include "flash_map/flash_map.h"
Tamas Bana9de4a62018-09-18 08:09:45 +010028#include "bl2/include/boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020029#include "security_cnt.h"
Tamas Band4bf3472019-09-06 12:59:56 +010030#include "bl2/include/boot_hal.h"
David Vincze73dfbc52019-10-11 13:54:58 +020031#if BOOT_LOG_LEVEL > BOOT_LOG_LEVEL_OFF
32#include "uart_stdout.h"
33#endif
Raef Coles0e82adc2019-10-17 15:06:26 +010034#ifdef CRYPTO_HW_ACCELERATOR
35#include "crypto_hw.h"
36#endif /* CRYPTO_HW_ACCELERATOR */
Tamas Banf70ef8c2017-12-19 15:35:09 +000037
Tamas Ban581034a2017-12-19 19:54:37 +000038/* Avoids the semihosting issue */
39#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
40__asm(" .global __ARM_use_no_argv\n");
41#endif
42
David Hu5cc9a3f2019-06-14 13:10:40 +080043#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +020044/* Macros to pick linker symbols */
45#define REGION(a, b, c) a##b##c
46#define REGION_NAME(a, b, c) REGION(a, b, c)
47#define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
48
49REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +080050#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +020051
Tamas Banc3828852018-02-01 12:24:16 +000052/* Flash device name must be specified by target */
53extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
Tamas Banf70ef8c2017-12-19 15:35:09 +000054
Tamas Banbd3f7512018-01-26 15:45:03 +000055#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000
56/* Static buffer to be used by mbedtls for memory allocation */
57static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN];
Tamas Banf70ef8c2017-12-19 15:35:09 +000058
Tamas Banf70ef8c2017-12-19 15:35:09 +000059struct arm_vector_table {
60 uint32_t msp;
61 uint32_t reset;
62};
63
Tamas Band4bf3472019-09-06 12:59:56 +010064/*!
65 * \brief Chain-loading the next image in the boot sequence.
66 *
67 * This function calls the Reset_Handler of the next image in the boot sequence,
68 * usually it is the secure firmware. Before passing the execution to next image
69 * there is conditional rule to remove the secrets from the memory. This must be
70 * done if the following conditions are satisfied:
71 * - Memory is shared between SW components at different stages of the trusted
72 * boot process.
73 * - There are secrets in the memory: KDF parameter, symmetric key,
74 * manufacturer sensitive code/data, etc.
75 */
76__attribute__((naked)) void boot_jump_to_next_image(uint32_t reset_handler_addr)
77{
78 __ASM volatile(
79 ".syntax unified \n"
80 "mov r7, r0 \n"
81 "bl boot_clear_bl2_ram_area \n" /* Clear RAM before jump */
82 "movs r0, #0 \n" /* Clear registers: R0-R12, */
83 "mov r1, r0 \n" /* except R7 */
84 "mov r2, r0 \n"
85 "mov r3, r0 \n"
86 "mov r4, r0 \n"
87 "mov r5, r0 \n"
88 "mov r6, r0 \n"
89 "mov r8, r0 \n"
90 "mov r9, r0 \n"
91 "mov r10, r0 \n"
92 "mov r11, r0 \n"
93 "mov r12, r0 \n"
94 "mov lr, r0 \n"
95 "bx r7 \n" /* Jump to Reset_handler */
96 );
97}
98
Tamas Banf70ef8c2017-12-19 15:35:09 +000099static void do_boot(struct boot_rsp *rsp)
100{
Tamas Ban581034a2017-12-19 19:54:37 +0000101 /* Clang at O0, stores variables on the stack with SP relative addressing.
102 * When manually set the SP then the place of reset vector is lost.
103 * Static variables are stored in 'data' or 'bss' section, change of SP has
104 * no effect on them.
105 */
106 static struct arm_vector_table *vt;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000107 uintptr_t flash_base;
108 int rc;
109
110 /* The beginning of the image is the ARM vector table, containing
111 * the initial stack pointer address and the reset vector
112 * consecutively. Manually set the stack pointer and jump into the
113 * reset vector
114 */
115 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
116 assert(rc == 0);
117
Oliver Swedef9982442018-08-24 18:37:44 +0100118 if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) {
119 /* The image has been copied to SRAM, find the vector table
120 * at the load address instead of image's address in flash
121 */
122 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr +
123 rsp->br_hdr->ih_hdr_size);
124 } else {
125 /* Using the flash address as not executing in SRAM */
126 vt = (struct arm_vector_table *)(flash_base +
127 rsp->br_image_off +
128 rsp->br_hdr->ih_hdr_size);
129 }
130
David Vinczeb57989f2018-09-24 10:59:04 +0200131 rc = FLASH_DEV_NAME.Uninitialize();
132 if(rc != ARM_DRIVER_OK) {
133 BOOT_LOG_ERR("Error while uninitializing Flash Interface");
134 }
135
David Vincze73dfbc52019-10-11 13:54:58 +0200136#if BOOT_LOG_LEVEL > BOOT_LOG_LEVEL_OFF
David Vincze8da7f102018-09-24 10:53:46 +0200137 stdio_uninit();
David Vincze73dfbc52019-10-11 13:54:58 +0200138#endif
David Vincze8da7f102018-09-24 10:53:46 +0200139
David Hu5cc9a3f2019-06-14 13:10:40 +0800140#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200141 /* Restore the Main Stack Pointer Limit register's reset value
142 * before passing execution to runtime firmware to make the
143 * bootloader transparent to it.
144 */
145 __set_MSPLIM(0);
David Hu5cc9a3f2019-06-14 13:10:40 +0800146#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200147
Tamas Ban581034a2017-12-19 19:54:37 +0000148 __set_MSP(vt->msp);
149 __DSB();
150 __ISB();
151
Tamas Band4bf3472019-09-06 12:59:56 +0100152 boot_jump_to_next_image(vt->reset);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000153}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000154
Tamas Ban581034a2017-12-19 19:54:37 +0000155int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000156{
David Hu5cc9a3f2019-06-14 13:10:40 +0800157#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200158 uint32_t msp_stack_bottom =
159 (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +0800160#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000161 struct boot_rsp rsp;
162 int rc;
163
David Hu5cc9a3f2019-06-14 13:10:40 +0800164#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200165 __set_MSPLIM(msp_stack_bottom);
David Hu5cc9a3f2019-06-14 13:10:40 +0800166#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200167
David Vincze73dfbc52019-10-11 13:54:58 +0200168#if BOOT_LOG_LEVEL > BOOT_LOG_LEVEL_OFF
Gabor Kerteszeb953f52018-07-17 13:36:28 +0200169 stdio_init();
David Vincze73dfbc52019-10-11 13:54:58 +0200170#endif
Tamas Ban581034a2017-12-19 19:54:37 +0000171
Tamas Banf70ef8c2017-12-19 15:35:09 +0000172 BOOT_LOG_INF("Starting bootloader");
173
Tamas Banbd3f7512018-01-26 15:45:03 +0000174 /* Initialise the mbedtls static memory allocator so that mbedtls allocates
175 * memory from the provided static buffer instead of from the heap.
176 */
177 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000178
Raef Coles0e82adc2019-10-17 15:06:26 +0100179#ifdef CRYPTO_HW_ACCELERATOR
180 rc = crypto_hw_accelerator_init();
181 if (rc) {
182 BOOT_LOG_ERR("Error while initializing cryptographic accelerator.");
183 while (1);
184 }
185#endif /* CRYPTO_HW_ACCELERATOR */
186
David Vinczeb57989f2018-09-24 10:59:04 +0200187 rc = FLASH_DEV_NAME.Initialize(NULL);
188 if(rc != ARM_DRIVER_OK) {
189 BOOT_LOG_ERR("Error while initializing Flash Interface");
190 while (1)
191 ;
192 }
David Vincze26e8c8a2018-08-28 16:59:41 +0200193
David Vincze060968d2019-05-23 01:13:14 +0200194 rc = boot_nv_security_counter_init();
195 if (rc != 0) {
196 BOOT_LOG_ERR("Error while initializing the security counter");
197 while (1)
198 ;
199 }
200
Tamas Banf70ef8c2017-12-19 15:35:09 +0000201 rc = boot_go(&rsp);
202 if (rc != 0) {
203 BOOT_LOG_ERR("Unable to find bootable image");
204 while (1)
205 ;
206 }
207
Raef Coles0e82adc2019-10-17 15:06:26 +0100208#ifdef CRYPTO_HW_ACCELERATOR
209 rc = crypto_hw_accelerator_finish();
210 if (rc) {
211 BOOT_LOG_ERR("Error while uninitializing cryptographic accelerator.");
212 while (1);
213 }
214#endif /* CRYPTO_HW_ACCELERATOR */
215
Tamas Banf70ef8c2017-12-19 15:35:09 +0000216 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
217 rsp.br_image_off);
Tamas Ban581034a2017-12-19 19:54:37 +0000218 flash_area_warn_on_open();
Tamas Banf70ef8c2017-12-19 15:35:09 +0000219 BOOT_LOG_INF("Jumping to the first image slot");
220 do_boot(&rsp);
221
222 BOOT_LOG_ERR("Never should get here");
223 while (1)
224 ;
225}