blob: f86c4c288c00f8071b0654f7ffcb4970602a12a3 [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"
Tamas Ban581034a2017-12-19 19:54:37 +000021#include "cmsis.h"
22#include "uart_stdout.h"
Tamas Banc3828852018-02-01 12:24:16 +000023#include "Driver_Flash.h"
Tamas Banbd3f7512018-01-26 15:45:03 +000024#include "mbedtls/memory_buffer_alloc.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000025#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
26#include "bootutil/bootutil_log.h"
27#include "bootutil/image.h"
28#include "bootutil/bootutil.h"
29#include "flash_map/flash_map.h"
Tamas Bana9de4a62018-09-18 08:09:45 +010030#include "bl2/include/boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020031#include "security_cnt.h"
Tamas Band4bf3472019-09-06 12:59:56 +010032#include "bl2/include/boot_hal.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000033
Tamas Ban581034a2017-12-19 19:54:37 +000034/* Avoids the semihosting issue */
35#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
36__asm(" .global __ARM_use_no_argv\n");
37#endif
38
David Hu5cc9a3f2019-06-14 13:10:40 +080039#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +020040/* Macros to pick linker symbols */
41#define REGION(a, b, c) a##b##c
42#define REGION_NAME(a, b, c) REGION(a, b, c)
43#define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
44
45REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +080046#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +020047
Tamas Banc3828852018-02-01 12:24:16 +000048/* Flash device name must be specified by target */
49extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
Tamas Banf70ef8c2017-12-19 15:35:09 +000050
Tamas Banbd3f7512018-01-26 15:45:03 +000051#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000
52/* Static buffer to be used by mbedtls for memory allocation */
53static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN];
Tamas Banf70ef8c2017-12-19 15:35:09 +000054
Tamas Banf70ef8c2017-12-19 15:35:09 +000055struct arm_vector_table {
56 uint32_t msp;
57 uint32_t reset;
58};
59
Tamas Band4bf3472019-09-06 12:59:56 +010060/*!
61 * \brief Chain-loading the next image in the boot sequence.
62 *
63 * This function calls the Reset_Handler of the next image in the boot sequence,
64 * usually it is the secure firmware. Before passing the execution to next image
65 * there is conditional rule to remove the secrets from the memory. This must be
66 * done if the following conditions are satisfied:
67 * - Memory is shared between SW components at different stages of the trusted
68 * boot process.
69 * - There are secrets in the memory: KDF parameter, symmetric key,
70 * manufacturer sensitive code/data, etc.
71 */
72__attribute__((naked)) void boot_jump_to_next_image(uint32_t reset_handler_addr)
73{
74 __ASM volatile(
75 ".syntax unified \n"
76 "mov r7, r0 \n"
77 "bl boot_clear_bl2_ram_area \n" /* Clear RAM before jump */
78 "movs r0, #0 \n" /* Clear registers: R0-R12, */
79 "mov r1, r0 \n" /* except R7 */
80 "mov r2, r0 \n"
81 "mov r3, r0 \n"
82 "mov r4, r0 \n"
83 "mov r5, r0 \n"
84 "mov r6, r0 \n"
85 "mov r8, r0 \n"
86 "mov r9, r0 \n"
87 "mov r10, r0 \n"
88 "mov r11, r0 \n"
89 "mov r12, r0 \n"
90 "mov lr, r0 \n"
91 "bx r7 \n" /* Jump to Reset_handler */
92 );
93}
94
Tamas Banf70ef8c2017-12-19 15:35:09 +000095static void do_boot(struct boot_rsp *rsp)
96{
Tamas Ban581034a2017-12-19 19:54:37 +000097 /* Clang at O0, stores variables on the stack with SP relative addressing.
98 * When manually set the SP then the place of reset vector is lost.
99 * Static variables are stored in 'data' or 'bss' section, change of SP has
100 * no effect on them.
101 */
102 static struct arm_vector_table *vt;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000103 uintptr_t flash_base;
104 int rc;
105
106 /* The beginning of the image is the ARM vector table, containing
107 * the initial stack pointer address and the reset vector
108 * consecutively. Manually set the stack pointer and jump into the
109 * reset vector
110 */
111 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
112 assert(rc == 0);
113
Oliver Swedef9982442018-08-24 18:37:44 +0100114 if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) {
115 /* The image has been copied to SRAM, find the vector table
116 * at the load address instead of image's address in flash
117 */
118 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr +
119 rsp->br_hdr->ih_hdr_size);
120 } else {
121 /* Using the flash address as not executing in SRAM */
122 vt = (struct arm_vector_table *)(flash_base +
123 rsp->br_image_off +
124 rsp->br_hdr->ih_hdr_size);
125 }
126
David Vinczeb57989f2018-09-24 10:59:04 +0200127 rc = FLASH_DEV_NAME.Uninitialize();
128 if(rc != ARM_DRIVER_OK) {
129 BOOT_LOG_ERR("Error while uninitializing Flash Interface");
130 }
131
David Vincze8da7f102018-09-24 10:53:46 +0200132 stdio_uninit();
133
David Hu5cc9a3f2019-06-14 13:10:40 +0800134#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200135 /* Restore the Main Stack Pointer Limit register's reset value
136 * before passing execution to runtime firmware to make the
137 * bootloader transparent to it.
138 */
139 __set_MSPLIM(0);
David Hu5cc9a3f2019-06-14 13:10:40 +0800140#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200141
Tamas Ban581034a2017-12-19 19:54:37 +0000142 __set_MSP(vt->msp);
143 __DSB();
144 __ISB();
145
Tamas Band4bf3472019-09-06 12:59:56 +0100146 boot_jump_to_next_image(vt->reset);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000147}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000148
Tamas Ban581034a2017-12-19 19:54:37 +0000149int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000150{
David Hu5cc9a3f2019-06-14 13:10:40 +0800151#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200152 uint32_t msp_stack_bottom =
153 (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +0800154#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000155 struct boot_rsp rsp;
156 int rc;
157
David Hu5cc9a3f2019-06-14 13:10:40 +0800158#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200159 __set_MSPLIM(msp_stack_bottom);
David Hu5cc9a3f2019-06-14 13:10:40 +0800160#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200161
Gabor Kerteszeb953f52018-07-17 13:36:28 +0200162 stdio_init();
Tamas Ban581034a2017-12-19 19:54:37 +0000163
Tamas Banf70ef8c2017-12-19 15:35:09 +0000164 BOOT_LOG_INF("Starting bootloader");
165
Tamas Banbd3f7512018-01-26 15:45:03 +0000166 /* Initialise the mbedtls static memory allocator so that mbedtls allocates
167 * memory from the provided static buffer instead of from the heap.
168 */
169 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000170
David Vinczeb57989f2018-09-24 10:59:04 +0200171 rc = FLASH_DEV_NAME.Initialize(NULL);
172 if(rc != ARM_DRIVER_OK) {
173 BOOT_LOG_ERR("Error while initializing Flash Interface");
174 while (1)
175 ;
176 }
David Vincze26e8c8a2018-08-28 16:59:41 +0200177
David Vincze060968d2019-05-23 01:13:14 +0200178 rc = boot_nv_security_counter_init();
179 if (rc != 0) {
180 BOOT_LOG_ERR("Error while initializing the security counter");
181 while (1)
182 ;
183 }
184
Tamas Banf70ef8c2017-12-19 15:35:09 +0000185 rc = boot_go(&rsp);
186 if (rc != 0) {
187 BOOT_LOG_ERR("Unable to find bootable image");
188 while (1)
189 ;
190 }
191
192 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
193 rsp.br_image_off);
Tamas Ban581034a2017-12-19 19:54:37 +0000194 flash_area_warn_on_open();
Tamas Banf70ef8c2017-12-19 15:35:09 +0000195 BOOT_LOG_INF("Jumping to the first image slot");
196 do_boot(&rsp);
197
198 BOOT_LOG_ERR("Never should get here");
199 while (1)
200 ;
201}