blob: 01922cba9016780fd309d5054b58f64897566738 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
David Vincze225c58f2019-12-09 17:32:48 +01003 * Copyright (c) 2017-2020 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"
David Vincze225c58f2019-12-09 17:32:48 +010027#include "flash_map_backend/flash_map_backend.h"
28#include "boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020029#include "security_cnt.h"
David Vincze225c58f2019-12-09 17:32:48 +010030#include "boot_hal.h"
David Vincze99f1b362019-12-12 16:17:35 +010031#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
David Vincze73dfbc52019-10-11 13:54:58 +020032#include "uart_stdout.h"
33#endif
Tamas Banf824e742019-10-25 21:22:26 +010034#if defined(CRYPTO_HW_ACCELERATOR) || \
35 defined(CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING)
Raef Coles0e82adc2019-10-17 15:06:26 +010036#include "crypto_hw.h"
Tamas Banf824e742019-10-25 21:22:26 +010037#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +000038
Tamas Ban581034a2017-12-19 19:54:37 +000039/* Avoids the semihosting issue */
40#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
41__asm(" .global __ARM_use_no_argv\n");
42#endif
43
David Hu5cc9a3f2019-06-14 13:10:40 +080044#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +020045/* Macros to pick linker symbols */
46#define REGION(a, b, c) a##b##c
47#define REGION_NAME(a, b, c) REGION(a, b, c)
48#define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
49
50REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +080051#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +020052
Tamas Banc3828852018-02-01 12:24:16 +000053/* Flash device name must be specified by target */
54extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
Tamas Banf70ef8c2017-12-19 15:35:09 +000055
Tamas Banbd3f7512018-01-26 15:45:03 +000056#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000
57/* Static buffer to be used by mbedtls for memory allocation */
58static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN];
Tamas Banf70ef8c2017-12-19 15:35:09 +000059
Tamas Banf70ef8c2017-12-19 15:35:09 +000060struct arm_vector_table {
61 uint32_t msp;
62 uint32_t reset;
63};
64
Tamas Band4bf3472019-09-06 12:59:56 +010065/*!
66 * \brief Chain-loading the next image in the boot sequence.
67 *
68 * This function calls the Reset_Handler of the next image in the boot sequence,
69 * usually it is the secure firmware. Before passing the execution to next image
70 * there is conditional rule to remove the secrets from the memory. This must be
71 * done if the following conditions are satisfied:
72 * - Memory is shared between SW components at different stages of the trusted
73 * boot process.
74 * - There are secrets in the memory: KDF parameter, symmetric key,
75 * manufacturer sensitive code/data, etc.
76 */
77__attribute__((naked)) void boot_jump_to_next_image(uint32_t reset_handler_addr)
78{
79 __ASM volatile(
80 ".syntax unified \n"
81 "mov r7, r0 \n"
82 "bl boot_clear_bl2_ram_area \n" /* Clear RAM before jump */
83 "movs r0, #0 \n" /* Clear registers: R0-R12, */
84 "mov r1, r0 \n" /* except R7 */
85 "mov r2, r0 \n"
86 "mov r3, r0 \n"
87 "mov r4, r0 \n"
88 "mov r5, r0 \n"
89 "mov r6, r0 \n"
90 "mov r8, r0 \n"
91 "mov r9, r0 \n"
92 "mov r10, r0 \n"
93 "mov r11, r0 \n"
94 "mov r12, r0 \n"
95 "mov lr, r0 \n"
96 "bx r7 \n" /* Jump to Reset_handler */
97 );
98}
99
Tamas Banf70ef8c2017-12-19 15:35:09 +0000100static void do_boot(struct boot_rsp *rsp)
101{
Tamas Ban581034a2017-12-19 19:54:37 +0000102 /* Clang at O0, stores variables on the stack with SP relative addressing.
103 * When manually set the SP then the place of reset vector is lost.
104 * Static variables are stored in 'data' or 'bss' section, change of SP has
105 * no effect on them.
106 */
107 static struct arm_vector_table *vt;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000108 uintptr_t flash_base;
109 int rc;
110
111 /* The beginning of the image is the ARM vector table, containing
112 * the initial stack pointer address and the reset vector
113 * consecutively. Manually set the stack pointer and jump into the
114 * reset vector
115 */
116 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
117 assert(rc == 0);
118
Oliver Swedef9982442018-08-24 18:37:44 +0100119 if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) {
120 /* The image has been copied to SRAM, find the vector table
121 * at the load address instead of image's address in flash
122 */
123 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr +
124 rsp->br_hdr->ih_hdr_size);
125 } else {
126 /* Using the flash address as not executing in SRAM */
127 vt = (struct arm_vector_table *)(flash_base +
128 rsp->br_image_off +
129 rsp->br_hdr->ih_hdr_size);
130 }
131
David Vinczeb57989f2018-09-24 10:59:04 +0200132 rc = FLASH_DEV_NAME.Uninitialize();
133 if(rc != ARM_DRIVER_OK) {
134 BOOT_LOG_ERR("Error while uninitializing Flash Interface");
135 }
136
David Vincze99f1b362019-12-12 16:17:35 +0100137#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
David Vincze8da7f102018-09-24 10:53:46 +0200138 stdio_uninit();
David Vincze73dfbc52019-10-11 13:54:58 +0200139#endif
David Vincze8da7f102018-09-24 10:53:46 +0200140
David Hu5cc9a3f2019-06-14 13:10:40 +0800141#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200142 /* Restore the Main Stack Pointer Limit register's reset value
143 * before passing execution to runtime firmware to make the
144 * bootloader transparent to it.
145 */
146 __set_MSPLIM(0);
David Hu5cc9a3f2019-06-14 13:10:40 +0800147#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200148
Tamas Ban581034a2017-12-19 19:54:37 +0000149 __set_MSP(vt->msp);
150 __DSB();
151 __ISB();
152
Tamas Band4bf3472019-09-06 12:59:56 +0100153 boot_jump_to_next_image(vt->reset);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000154}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000155
Tamas Ban581034a2017-12-19 19:54:37 +0000156int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000157{
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 uint32_t msp_stack_bottom =
160 (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +0800161#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000162 struct boot_rsp rsp;
163 int rc;
164
David Hu5cc9a3f2019-06-14 13:10:40 +0800165#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200166 __set_MSPLIM(msp_stack_bottom);
David Hu5cc9a3f2019-06-14 13:10:40 +0800167#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200168
Andrei Narkevitchb0be4612020-01-27 17:26:19 -0800169 /* Perform platform specific initialization */
170 if (boot_platform_init() != 0) {
171 while (1)
172 ;
173 }
174
David Vincze99f1b362019-12-12 16:17:35 +0100175#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
Gabor Kerteszeb953f52018-07-17 13:36:28 +0200176 stdio_init();
David Vincze73dfbc52019-10-11 13:54:58 +0200177#endif
Tamas Ban581034a2017-12-19 19:54:37 +0000178
Tamas Banf70ef8c2017-12-19 15:35:09 +0000179 BOOT_LOG_INF("Starting bootloader");
180
Tamas Banbd3f7512018-01-26 15:45:03 +0000181 /* Initialise the mbedtls static memory allocator so that mbedtls allocates
182 * memory from the provided static buffer instead of from the heap.
183 */
184 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000185
Raef Coles0e82adc2019-10-17 15:06:26 +0100186#ifdef CRYPTO_HW_ACCELERATOR
187 rc = crypto_hw_accelerator_init();
188 if (rc) {
189 BOOT_LOG_ERR("Error while initializing cryptographic accelerator.");
190 while (1);
191 }
192#endif /* CRYPTO_HW_ACCELERATOR */
193
David Vinczec3e313a2020-01-06 17:31:11 +0100194#ifndef MCUBOOT_USE_UPSTREAM
David Vincze060968d2019-05-23 01:13:14 +0200195 rc = boot_nv_security_counter_init();
196 if (rc != 0) {
197 BOOT_LOG_ERR("Error while initializing the security counter");
198 while (1)
199 ;
200 }
David Vinczec3e313a2020-01-06 17:31:11 +0100201#endif /* !MCUBOOT_USE_UPSTREAM */
David Vincze060968d2019-05-23 01:13:14 +0200202
Tamas Banf70ef8c2017-12-19 15:35:09 +0000203 rc = boot_go(&rsp);
204 if (rc != 0) {
205 BOOT_LOG_ERR("Unable to find bootable image");
206 while (1)
207 ;
208 }
209
Raef Coles0e82adc2019-10-17 15:06:26 +0100210#ifdef CRYPTO_HW_ACCELERATOR
211 rc = crypto_hw_accelerator_finish();
212 if (rc) {
213 BOOT_LOG_ERR("Error while uninitializing cryptographic accelerator.");
214 while (1);
215 }
216#endif /* CRYPTO_HW_ACCELERATOR */
217
Tamas Banf824e742019-10-25 21:22:26 +0100218/* This is a workaround to program the TF-M related cryptographic keys
219 * to CC312 OTP memory. This functionality is independent from secure boot,
220 * this is usually done in the factory floor during chip manufacturing.
221 */
222#ifdef CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING
223 BOOT_LOG_INF("OTP provisioning started.");
224 rc = crypto_hw_accelerator_otp_provisioning();
225 if (rc) {
226 BOOT_LOG_ERR("OTP provisioning FAILED: 0x%X", rc);
227 while (1);
228 } else {
229 BOOT_LOG_INF("OTP provisioning succeeded. TF-M won't be loaded.");
230
231 /* We don't need to boot - the only aim is provisioning. */
232 while (1);
233 }
234#endif /* CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING */
235
Tamas Banf70ef8c2017-12-19 15:35:09 +0000236 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
237 rsp.br_image_off);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000238 BOOT_LOG_INF("Jumping to the first image slot");
239 do_boot(&rsp);
240
241 BOOT_LOG_ERR("Never should get here");
242 while (1)
243 ;
244}