blob: c428d5e736585b136f18b902599234c87c9d9108 [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 Ban581034a2017-12-19 19:54:37 +000020#include "bl2_util.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000021#include "target.h"
Kevin Pengbc5e5aa2019-10-16 10:55:17 +080022#include "tfm_hal_device_header.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#include "bootutil/bootutil_log.h"
26#include "bootutil/image.h"
27#include "bootutil/bootutil.h"
David Vincze225c58f2019-12-09 17:32:48 +010028#include "flash_map_backend/flash_map_backend.h"
29#include "boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020030#include "security_cnt.h"
David Vincze225c58f2019-12-09 17:32:48 +010031#include "boot_hal.h"
TTornblom83d96372019-11-19 12:53:16 +010032#include "region.h"
David Vincze99f1b362019-12-12 16:17:35 +010033#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
David Vincze73dfbc52019-10-11 13:54:58 +020034#include "uart_stdout.h"
35#endif
Tamas Banf824e742019-10-25 21:22:26 +010036#if defined(CRYPTO_HW_ACCELERATOR) || \
37 defined(CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING)
Raef Coles0e82adc2019-10-17 15:06:26 +010038#include "crypto_hw.h"
Tamas Banf824e742019-10-25 21:22:26 +010039#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +000040
Tamas Ban581034a2017-12-19 19:54:37 +000041/* Avoids the semihosting issue */
42#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
43__asm(" .global __ARM_use_no_argv\n");
44#endif
45
David Hu5cc9a3f2019-06-14 13:10:40 +080046#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +020047REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +080048#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +020049
Tamas Banc3828852018-02-01 12:24:16 +000050/* Flash device name must be specified by target */
51extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
Tamas Banf70ef8c2017-12-19 15:35:09 +000052
Balint Matyi5c476312020-03-31 13:15:39 +010053#ifdef MCUBOOT_ENCRYPT_RSA
54#define BL2_MBEDTLS_MEM_BUF_LEN 0x225C
55#else
Tamas Banbd3f7512018-01-26 15:45:03 +000056#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000
Balint Matyi5c476312020-03-31 13:15:39 +010057#endif
58
Tamas Banbd3f7512018-01-26 15:45:03 +000059/* Static buffer to be used by mbedtls for memory allocation */
60static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN];
Tamas Banf70ef8c2017-12-19 15:35:09 +000061
Tamas Banf70ef8c2017-12-19 15:35:09 +000062static void do_boot(struct boot_rsp *rsp)
63{
Michel Jaouen3ecd6222020-06-17 18:58:00 +020064 struct boot_arm_vector_table *vt;
Tamas Banf70ef8c2017-12-19 15:35:09 +000065 uintptr_t flash_base;
66 int rc;
67
68 /* The beginning of the image is the ARM vector table, containing
69 * the initial stack pointer address and the reset vector
70 * consecutively. Manually set the stack pointer and jump into the
71 * reset vector
72 */
73 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
74 assert(rc == 0);
75
Oliver Swedef9982442018-08-24 18:37:44 +010076 if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) {
77 /* The image has been copied to SRAM, find the vector table
78 * at the load address instead of image's address in flash
79 */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020080 vt = (struct boot_arm_vector_table *)(rsp->br_hdr->ih_load_addr +
Oliver Swedef9982442018-08-24 18:37:44 +010081 rsp->br_hdr->ih_hdr_size);
82 } else {
83 /* Using the flash address as not executing in SRAM */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020084 vt = (struct boot_arm_vector_table *)(flash_base +
Oliver Swedef9982442018-08-24 18:37:44 +010085 rsp->br_image_off +
86 rsp->br_hdr->ih_hdr_size);
87 }
David Vinczeb57989f2018-09-24 10:59:04 +020088 rc = FLASH_DEV_NAME.Uninitialize();
89 if(rc != ARM_DRIVER_OK) {
90 BOOT_LOG_ERR("Error while uninitializing Flash Interface");
91 }
92
David Vincze99f1b362019-12-12 16:17:35 +010093#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
David Vincze8da7f102018-09-24 10:53:46 +020094 stdio_uninit();
David Vincze73dfbc52019-10-11 13:54:58 +020095#endif
Michel Jaouen3ecd6222020-06-17 18:58:00 +020096 /* This function never returns, because it calls the secure application
97 * Reset_Handler()
David Vinczee0a3c2f2019-05-15 16:45:14 +020098 */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020099 boot_platform_quit(vt);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000100}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000101
Tamas Ban581034a2017-12-19 19:54:37 +0000102int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000103{
David Hu5cc9a3f2019-06-14 13:10:40 +0800104#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200105 uint32_t msp_stack_bottom =
106 (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +0800107#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000108 struct boot_rsp rsp;
109 int rc;
110
David Hu5cc9a3f2019-06-14 13:10:40 +0800111#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200112 __set_MSPLIM(msp_stack_bottom);
David Hu5cc9a3f2019-06-14 13:10:40 +0800113#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200114
Andrei Narkevitchb0be4612020-01-27 17:26:19 -0800115 /* Perform platform specific initialization */
116 if (boot_platform_init() != 0) {
117 while (1)
118 ;
119 }
120
David Vincze99f1b362019-12-12 16:17:35 +0100121#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
Gabor Kerteszeb953f52018-07-17 13:36:28 +0200122 stdio_init();
David Vincze73dfbc52019-10-11 13:54:58 +0200123#endif
Tamas Ban581034a2017-12-19 19:54:37 +0000124
Tamas Banf70ef8c2017-12-19 15:35:09 +0000125 BOOT_LOG_INF("Starting bootloader");
126
Tamas Banbd3f7512018-01-26 15:45:03 +0000127 /* Initialise the mbedtls static memory allocator so that mbedtls allocates
128 * memory from the provided static buffer instead of from the heap.
129 */
130 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000131
Raef Coles0e82adc2019-10-17 15:06:26 +0100132#ifdef CRYPTO_HW_ACCELERATOR
133 rc = crypto_hw_accelerator_init();
134 if (rc) {
135 BOOT_LOG_ERR("Error while initializing cryptographic accelerator.");
136 while (1);
137 }
138#endif /* CRYPTO_HW_ACCELERATOR */
139
David Vincze060968d2019-05-23 01:13:14 +0200140 rc = boot_nv_security_counter_init();
141 if (rc != 0) {
142 BOOT_LOG_ERR("Error while initializing the security counter");
143 while (1)
144 ;
145 }
146
Tamas Banf70ef8c2017-12-19 15:35:09 +0000147 rc = boot_go(&rsp);
148 if (rc != 0) {
149 BOOT_LOG_ERR("Unable to find bootable image");
150 while (1)
151 ;
152 }
153
Raef Coles0e82adc2019-10-17 15:06:26 +0100154#ifdef CRYPTO_HW_ACCELERATOR
155 rc = crypto_hw_accelerator_finish();
156 if (rc) {
157 BOOT_LOG_ERR("Error while uninitializing cryptographic accelerator.");
158 while (1);
159 }
160#endif /* CRYPTO_HW_ACCELERATOR */
161
Tamas Banf824e742019-10-25 21:22:26 +0100162/* This is a workaround to program the TF-M related cryptographic keys
163 * to CC312 OTP memory. This functionality is independent from secure boot,
164 * this is usually done in the factory floor during chip manufacturing.
165 */
166#ifdef CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING
167 BOOT_LOG_INF("OTP provisioning started.");
168 rc = crypto_hw_accelerator_otp_provisioning();
169 if (rc) {
170 BOOT_LOG_ERR("OTP provisioning FAILED: 0x%X", rc);
171 while (1);
172 } else {
173 BOOT_LOG_INF("OTP provisioning succeeded. TF-M won't be loaded.");
174
175 /* We don't need to boot - the only aim is provisioning. */
176 while (1);
177 }
178#endif /* CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING */
179
Tamas Banf70ef8c2017-12-19 15:35:09 +0000180 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
181 rsp.br_image_off);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000182 BOOT_LOG_INF("Jumping to the first image slot");
183 do_boot(&rsp);
184
185 BOOT_LOG_ERR("Never should get here");
186 while (1)
187 ;
188}