blob: e9fc75e0255dcc7e50f1c05089719dc436029168 [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
Balint Matyi2fe04922020-02-18 12:27:38 +000018#include "mcuboot_config/mcuboot_config.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000019#include <assert.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"
Raef Coles8efad882020-07-10 09:46:00 +010028#include "bootutil/boot_record.h"
Balint Matyi69e2d2e2020-07-08 10:53:54 +010029#include "bootutil/security_cnt.h"
David Vincze225c58f2019-12-09 17:32:48 +010030#include "boot_hal.h"
TTornblom83d96372019-11-19 12:53:16 +010031#include "region.h"
David Vincze99f1b362019-12-12 16:17:35 +010032#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
David Vincze73dfbc52019-10-11 13:54:58 +020033#include "uart_stdout.h"
34#endif
Tamas Banf824e742019-10-25 21:22:26 +010035#if defined(CRYPTO_HW_ACCELERATOR) || \
36 defined(CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING)
Raef Coles0e82adc2019-10-17 15:06:26 +010037#include "crypto_hw.h"
Tamas Banf824e742019-10-25 21:22:26 +010038#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +000039
Tamas Ban581034a2017-12-19 19:54:37 +000040/* Avoids the semihosting issue */
41#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
42__asm(" .global __ARM_use_no_argv\n");
43#endif
44
David Hu5cc9a3f2019-06-14 13:10:40 +080045#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +020046REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +080047#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +020048
Tamas Banc3828852018-02-01 12:24:16 +000049/* Flash device name must be specified by target */
50extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
Tamas Banf70ef8c2017-12-19 15:35:09 +000051
Balint Matyi5c476312020-03-31 13:15:39 +010052#ifdef MCUBOOT_ENCRYPT_RSA
53#define BL2_MBEDTLS_MEM_BUF_LEN 0x225C
54#else
Tamas Banbd3f7512018-01-26 15:45:03 +000055#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000
Balint Matyi5c476312020-03-31 13:15:39 +010056#endif
57
Tamas Banbd3f7512018-01-26 15:45:03 +000058/* Static buffer to be used by mbedtls for memory allocation */
59static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN];
Tamas Banf70ef8c2017-12-19 15:35:09 +000060
Tamas Banf70ef8c2017-12-19 15:35:09 +000061static void do_boot(struct boot_rsp *rsp)
62{
Michel Jaouen3ecd6222020-06-17 18:58:00 +020063 struct boot_arm_vector_table *vt;
Tamas Banf70ef8c2017-12-19 15:35:09 +000064 uintptr_t flash_base;
65 int rc;
66
67 /* The beginning of the image is the ARM vector table, containing
68 * the initial stack pointer address and the reset vector
69 * consecutively. Manually set the stack pointer and jump into the
70 * reset vector
71 */
72 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
73 assert(rc == 0);
74
Oliver Swedef9982442018-08-24 18:37:44 +010075 if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) {
76 /* The image has been copied to SRAM, find the vector table
77 * at the load address instead of image's address in flash
78 */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020079 vt = (struct boot_arm_vector_table *)(rsp->br_hdr->ih_load_addr +
Oliver Swedef9982442018-08-24 18:37:44 +010080 rsp->br_hdr->ih_hdr_size);
81 } else {
82 /* Using the flash address as not executing in SRAM */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020083 vt = (struct boot_arm_vector_table *)(flash_base +
Oliver Swedef9982442018-08-24 18:37:44 +010084 rsp->br_image_off +
85 rsp->br_hdr->ih_hdr_size);
86 }
David Vinczeb57989f2018-09-24 10:59:04 +020087 rc = FLASH_DEV_NAME.Uninitialize();
88 if(rc != ARM_DRIVER_OK) {
89 BOOT_LOG_ERR("Error while uninitializing Flash Interface");
90 }
91
David Vincze99f1b362019-12-12 16:17:35 +010092#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
David Vincze8da7f102018-09-24 10:53:46 +020093 stdio_uninit();
David Vincze73dfbc52019-10-11 13:54:58 +020094#endif
Michel Jaouen3ecd6222020-06-17 18:58:00 +020095 /* This function never returns, because it calls the secure application
96 * Reset_Handler()
David Vinczee0a3c2f2019-05-15 16:45:14 +020097 */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020098 boot_platform_quit(vt);
Tamas Banf70ef8c2017-12-19 15:35:09 +000099}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000100
Tamas Ban581034a2017-12-19 19:54:37 +0000101int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000102{
David Hu5cc9a3f2019-06-14 13:10:40 +0800103#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200104 uint32_t msp_stack_bottom =
105 (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +0800106#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000107 struct boot_rsp rsp;
108 int rc;
109
David Hu5cc9a3f2019-06-14 13:10:40 +0800110#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200111 __set_MSPLIM(msp_stack_bottom);
David Hu5cc9a3f2019-06-14 13:10:40 +0800112#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200113
Andrei Narkevitchb0be4612020-01-27 17:26:19 -0800114 /* Perform platform specific initialization */
115 if (boot_platform_init() != 0) {
116 while (1)
117 ;
118 }
119
David Vincze99f1b362019-12-12 16:17:35 +0100120#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
Gabor Kerteszeb953f52018-07-17 13:36:28 +0200121 stdio_init();
David Vincze73dfbc52019-10-11 13:54:58 +0200122#endif
Tamas Ban581034a2017-12-19 19:54:37 +0000123
Tamas Banf70ef8c2017-12-19 15:35:09 +0000124 BOOT_LOG_INF("Starting bootloader");
125
Tamas Banbd3f7512018-01-26 15:45:03 +0000126 /* Initialise the mbedtls static memory allocator so that mbedtls allocates
127 * memory from the provided static buffer instead of from the heap.
128 */
129 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000130
Raef Coles0e82adc2019-10-17 15:06:26 +0100131#ifdef CRYPTO_HW_ACCELERATOR
132 rc = crypto_hw_accelerator_init();
133 if (rc) {
134 BOOT_LOG_ERR("Error while initializing cryptographic accelerator.");
135 while (1);
136 }
137#endif /* CRYPTO_HW_ACCELERATOR */
138
David Vincze060968d2019-05-23 01:13:14 +0200139 rc = boot_nv_security_counter_init();
140 if (rc != 0) {
141 BOOT_LOG_ERR("Error while initializing the security counter");
142 while (1)
143 ;
144 }
145
Tamas Banf70ef8c2017-12-19 15:35:09 +0000146 rc = boot_go(&rsp);
147 if (rc != 0) {
148 BOOT_LOG_ERR("Unable to find bootable image");
149 while (1)
150 ;
151 }
152
Raef Coles0e82adc2019-10-17 15:06:26 +0100153#ifdef CRYPTO_HW_ACCELERATOR
154 rc = crypto_hw_accelerator_finish();
155 if (rc) {
156 BOOT_LOG_ERR("Error while uninitializing cryptographic accelerator.");
157 while (1);
158 }
159#endif /* CRYPTO_HW_ACCELERATOR */
160
Tamas Banf824e742019-10-25 21:22:26 +0100161/* This is a workaround to program the TF-M related cryptographic keys
162 * to CC312 OTP memory. This functionality is independent from secure boot,
163 * this is usually done in the factory floor during chip manufacturing.
164 */
165#ifdef CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING
166 BOOT_LOG_INF("OTP provisioning started.");
167 rc = crypto_hw_accelerator_otp_provisioning();
168 if (rc) {
169 BOOT_LOG_ERR("OTP provisioning FAILED: 0x%X", rc);
170 while (1);
171 } else {
172 BOOT_LOG_INF("OTP provisioning succeeded. TF-M won't be loaded.");
173
174 /* We don't need to boot - the only aim is provisioning. */
175 while (1);
176 }
177#endif /* CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING */
178
Tamas Banf70ef8c2017-12-19 15:35:09 +0000179 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
180 rsp.br_image_off);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000181 BOOT_LOG_INF("Jumping to the first image slot");
182 do_boot(&rsp);
183
184 BOOT_LOG_ERR("Never should get here");
185 while (1)
186 ;
187}