blob: b7dd03ec983356d7dd47deecb9e036968db41ea0 [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
Tamas Banbd3f7512018-01-26 15:45:03 +000053#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000
54/* Static buffer to be used by mbedtls for memory allocation */
55static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN];
Tamas Banf70ef8c2017-12-19 15:35:09 +000056
Tamas Banf70ef8c2017-12-19 15:35:09 +000057static void do_boot(struct boot_rsp *rsp)
58{
Michel Jaouen3ecd6222020-06-17 18:58:00 +020059 struct boot_arm_vector_table *vt;
Tamas Banf70ef8c2017-12-19 15:35:09 +000060 uintptr_t flash_base;
61 int rc;
62
63 /* The beginning of the image is the ARM vector table, containing
64 * the initial stack pointer address and the reset vector
65 * consecutively. Manually set the stack pointer and jump into the
66 * reset vector
67 */
68 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
69 assert(rc == 0);
70
Oliver Swedef9982442018-08-24 18:37:44 +010071 if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) {
72 /* The image has been copied to SRAM, find the vector table
73 * at the load address instead of image's address in flash
74 */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020075 vt = (struct boot_arm_vector_table *)(rsp->br_hdr->ih_load_addr +
Oliver Swedef9982442018-08-24 18:37:44 +010076 rsp->br_hdr->ih_hdr_size);
77 } else {
78 /* Using the flash address as not executing in SRAM */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020079 vt = (struct boot_arm_vector_table *)(flash_base +
Oliver Swedef9982442018-08-24 18:37:44 +010080 rsp->br_image_off +
81 rsp->br_hdr->ih_hdr_size);
82 }
David Vinczeb57989f2018-09-24 10:59:04 +020083 rc = FLASH_DEV_NAME.Uninitialize();
84 if(rc != ARM_DRIVER_OK) {
85 BOOT_LOG_ERR("Error while uninitializing Flash Interface");
86 }
87
David Vincze99f1b362019-12-12 16:17:35 +010088#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
David Vincze8da7f102018-09-24 10:53:46 +020089 stdio_uninit();
David Vincze73dfbc52019-10-11 13:54:58 +020090#endif
Michel Jaouen3ecd6222020-06-17 18:58:00 +020091 /* This function never returns, because it calls the secure application
92 * Reset_Handler()
David Vinczee0a3c2f2019-05-15 16:45:14 +020093 */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020094 boot_platform_quit(vt);
Tamas Banf70ef8c2017-12-19 15:35:09 +000095}
Tamas Banf70ef8c2017-12-19 15:35:09 +000096
Tamas Ban581034a2017-12-19 19:54:37 +000097int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +000098{
David Hu5cc9a3f2019-06-14 13:10:40 +080099#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200100 uint32_t msp_stack_bottom =
101 (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
David Hu5cc9a3f2019-06-14 13:10:40 +0800102#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000103 struct boot_rsp rsp;
104 int rc;
105
David Hu5cc9a3f2019-06-14 13:10:40 +0800106#if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Vinczee0a3c2f2019-05-15 16:45:14 +0200107 __set_MSPLIM(msp_stack_bottom);
David Hu5cc9a3f2019-06-14 13:10:40 +0800108#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200109
Andrei Narkevitchb0be4612020-01-27 17:26:19 -0800110 /* Perform platform specific initialization */
111 if (boot_platform_init() != 0) {
112 while (1)
113 ;
114 }
115
David Vincze99f1b362019-12-12 16:17:35 +0100116#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
Gabor Kerteszeb953f52018-07-17 13:36:28 +0200117 stdio_init();
David Vincze73dfbc52019-10-11 13:54:58 +0200118#endif
Tamas Ban581034a2017-12-19 19:54:37 +0000119
Tamas Banf70ef8c2017-12-19 15:35:09 +0000120 BOOT_LOG_INF("Starting bootloader");
121
Tamas Banbd3f7512018-01-26 15:45:03 +0000122 /* Initialise the mbedtls static memory allocator so that mbedtls allocates
123 * memory from the provided static buffer instead of from the heap.
124 */
125 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000126
Raef Coles0e82adc2019-10-17 15:06:26 +0100127#ifdef CRYPTO_HW_ACCELERATOR
128 rc = crypto_hw_accelerator_init();
129 if (rc) {
130 BOOT_LOG_ERR("Error while initializing cryptographic accelerator.");
131 while (1);
132 }
133#endif /* CRYPTO_HW_ACCELERATOR */
134
David Vinczec3e313a2020-01-06 17:31:11 +0100135#ifndef MCUBOOT_USE_UPSTREAM
David Vincze060968d2019-05-23 01:13:14 +0200136 rc = boot_nv_security_counter_init();
137 if (rc != 0) {
138 BOOT_LOG_ERR("Error while initializing the security counter");
139 while (1)
140 ;
141 }
David Vinczec3e313a2020-01-06 17:31:11 +0100142#endif /* !MCUBOOT_USE_UPSTREAM */
David Vincze060968d2019-05-23 01:13:14 +0200143
Tamas Banf70ef8c2017-12-19 15:35:09 +0000144 rc = boot_go(&rsp);
145 if (rc != 0) {
146 BOOT_LOG_ERR("Unable to find bootable image");
147 while (1)
148 ;
149 }
150
Raef Coles0e82adc2019-10-17 15:06:26 +0100151#ifdef CRYPTO_HW_ACCELERATOR
152 rc = crypto_hw_accelerator_finish();
153 if (rc) {
154 BOOT_LOG_ERR("Error while uninitializing cryptographic accelerator.");
155 while (1);
156 }
157#endif /* CRYPTO_HW_ACCELERATOR */
158
Tamas Banf824e742019-10-25 21:22:26 +0100159/* This is a workaround to program the TF-M related cryptographic keys
160 * to CC312 OTP memory. This functionality is independent from secure boot,
161 * this is usually done in the factory floor during chip manufacturing.
162 */
163#ifdef CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING
164 BOOT_LOG_INF("OTP provisioning started.");
165 rc = crypto_hw_accelerator_otp_provisioning();
166 if (rc) {
167 BOOT_LOG_ERR("OTP provisioning FAILED: 0x%X", rc);
168 while (1);
169 } else {
170 BOOT_LOG_INF("OTP provisioning succeeded. TF-M won't be loaded.");
171
172 /* We don't need to boot - the only aim is provisioning. */
173 while (1);
174 }
175#endif /* CRYPTO_HW_ACCELERATOR_OTP_PROVISIONING */
176
Tamas Banf70ef8c2017-12-19 15:35:09 +0000177 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
178 rsp.br_image_off);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000179 BOOT_LOG_INF("Jumping to the first image slot");
180 do_boot(&rsp);
181
182 BOOT_LOG_ERR("Never should get here");
183 while (1)
184 ;
185}