blob: 1674e133beba922febe4fdbec5a4b177f9514f0f [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
Sherry Zhangc7baf592021-07-15 14:54:17 +08003 * Copyright (c) 2017-2021 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 Banbd3f7512018-01-26 15:45:03 +000022#include "mbedtls/memory_buffer_alloc.h"
David Vincze7d591a62020-09-23 18:25:56 +020023#include "bootutil/security_cnt.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000024#include "bootutil/bootutil_log.h"
25#include "bootutil/image.h"
26#include "bootutil/bootutil.h"
Raef Coles8efad882020-07-10 09:46:00 +010027#include "bootutil/boot_record.h"
Tamas Ban1bfc9da2020-07-09 13:55:38 +010028#include "bootutil/fault_injection_hardening.h"
David Vincze7d591a62020-09-23 18:25:56 +020029#include "flash_map_backend/flash_map_backend.h"
David Vincze225c58f2019-12-09 17:32:48 +010030#include "boot_hal.h"
David Vincze73dfbc52019-10-11 13:54:58 +020031#include "uart_stdout.h"
Raef Colesaefbe082021-06-18 08:53:43 +010032#include "tfm_plat_otp.h"
33#include "tfm_plat_provisioning.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000034
Tamas Ban581034a2017-12-19 19:54:37 +000035/* Avoids the semihosting issue */
36#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
37__asm(" .global __ARM_use_no_argv\n");
38#endif
39
Balint Matyi5c476312020-03-31 13:15:39 +010040#ifdef MCUBOOT_ENCRYPT_RSA
Sherry Zhangc7baf592021-07-15 14:54:17 +080041#define BL2_MBEDTLS_MEM_BUF_LEN 0x3000
Balint Matyi5c476312020-03-31 13:15:39 +010042#else
Tamas Banbd3f7512018-01-26 15:45:03 +000043#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000
Balint Matyi5c476312020-03-31 13:15:39 +010044#endif
45
Tamas Banbd3f7512018-01-26 15:45:03 +000046/* Static buffer to be used by mbedtls for memory allocation */
47static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN];
Tamas Banf70ef8c2017-12-19 15:35:09 +000048
Tamas Banf70ef8c2017-12-19 15:35:09 +000049static void do_boot(struct boot_rsp *rsp)
50{
Michel Jaouen3ecd6222020-06-17 18:58:00 +020051 struct boot_arm_vector_table *vt;
Tamas Banf70ef8c2017-12-19 15:35:09 +000052 uintptr_t flash_base;
53 int rc;
54
55 /* The beginning of the image is the ARM vector table, containing
56 * the initial stack pointer address and the reset vector
57 * consecutively. Manually set the stack pointer and jump into the
58 * reset vector
59 */
60 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
61 assert(rc == 0);
62
Oliver Swedef9982442018-08-24 18:37:44 +010063 if (rsp->br_hdr->ih_flags & IMAGE_F_RAM_LOAD) {
64 /* The image has been copied to SRAM, find the vector table
65 * at the load address instead of image's address in flash
66 */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020067 vt = (struct boot_arm_vector_table *)(rsp->br_hdr->ih_load_addr +
Oliver Swedef9982442018-08-24 18:37:44 +010068 rsp->br_hdr->ih_hdr_size);
69 } else {
70 /* Using the flash address as not executing in SRAM */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020071 vt = (struct boot_arm_vector_table *)(flash_base +
Oliver Swedef9982442018-08-24 18:37:44 +010072 rsp->br_image_off +
73 rsp->br_hdr->ih_hdr_size);
74 }
David Vinczeb57989f2018-09-24 10:59:04 +020075
David Vincze99f1b362019-12-12 16:17:35 +010076#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
David Vincze8da7f102018-09-24 10:53:46 +020077 stdio_uninit();
David Vincze73dfbc52019-10-11 13:54:58 +020078#endif
David Vincze7d591a62020-09-23 18:25:56 +020079
Michel Jaouen3ecd6222020-06-17 18:58:00 +020080 /* This function never returns, because it calls the secure application
David Vincze7d591a62020-09-23 18:25:56 +020081 * Reset_Handler().
David Vinczee0a3c2f2019-05-15 16:45:14 +020082 */
Michel Jaouen3ecd6222020-06-17 18:58:00 +020083 boot_platform_quit(vt);
Tamas Banf70ef8c2017-12-19 15:35:09 +000084}
Tamas Banf70ef8c2017-12-19 15:35:09 +000085
Tamas Ban581034a2017-12-19 19:54:37 +000086int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +000087{
88 struct boot_rsp rsp;
Tamas Ban1bfc9da2020-07-09 13:55:38 +010089 fih_int fih_rc = FIH_FAILURE;
Raef Colesaefbe082021-06-18 08:53:43 +010090 enum tfm_plat_err_t plat_err;
Tamas Banf70ef8c2017-12-19 15:35:09 +000091
David Vincze7d591a62020-09-23 18:25:56 +020092 /* Initialise the mbedtls static memory allocator so that mbedtls allocates
93 * memory from the provided static buffer instead of from the heap.
94 */
95 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN);
96
97#if MCUBOOT_LOG_LEVEL > MCUBOOT_LOG_LEVEL_OFF
98 stdio_init();
David Hu5cc9a3f2019-06-14 13:10:40 +080099#endif
David Vinczee0a3c2f2019-05-15 16:45:14 +0200100
Andrei Narkevitchb0be4612020-01-27 17:26:19 -0800101 /* Perform platform specific initialization */
102 if (boot_platform_init() != 0) {
Tamas Ban37aedb52020-10-01 10:54:48 +0100103 BOOT_LOG_ERR("Platform init failed");
Tamas Ban1bfc9da2020-07-09 13:55:38 +0100104 FIH_PANIC;
Andrei Narkevitchb0be4612020-01-27 17:26:19 -0800105 }
106
Tamas Banf70ef8c2017-12-19 15:35:09 +0000107 BOOT_LOG_INF("Starting bootloader");
108
Raef Colesaefbe082021-06-18 08:53:43 +0100109 plat_err = tfm_plat_otp_init();
110 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
111 BOOT_LOG_ERR("OTP system initialization failed");
112 FIH_PANIC;
113 }
114
115 if (tfm_plat_provisioning_is_required()) {
116 plat_err = tfm_plat_provisioning_perform();
117 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
118 BOOT_LOG_ERR("Provisioning failed");
119 FIH_PANIC;
120 }
121 } else {
122 tfm_plat_provisioning_check_for_dummy_keys();
123 }
124
Tamas Ban1bfc9da2020-07-09 13:55:38 +0100125 FIH_CALL(boot_nv_security_counter_init, fih_rc);
126 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vincze060968d2019-05-23 01:13:14 +0200127 BOOT_LOG_ERR("Error while initializing the security counter");
Tamas Ban1bfc9da2020-07-09 13:55:38 +0100128 FIH_PANIC;
David Vincze060968d2019-05-23 01:13:14 +0200129 }
130
Tamas Ban1bfc9da2020-07-09 13:55:38 +0100131 FIH_CALL(boot_go, fih_rc, &rsp);
132 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000133 BOOT_LOG_ERR("Unable to find bootable image");
Tamas Ban1bfc9da2020-07-09 13:55:38 +0100134 FIH_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000135 }
136
137 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
138 rsp.br_image_off);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000139 BOOT_LOG_INF("Jumping to the first image slot");
140 do_boot(&rsp);
141
142 BOOT_LOG_ERR("Never should get here");
Tamas Ban1bfc9da2020-07-09 13:55:38 +0100143 FIH_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000144}