blob: b63cc78358f2d280458f136e4befa22c3c13654e [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
David Vinczee0a3c2f2019-05-15 16:45:14 +02003 * Copyright (c) 2017-2019 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"
Tamas Ban581034a2017-12-19 19:54:37 +000021#include "cmsis.h"
22#include "uart_stdout.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#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
26#include "bootutil/bootutil_log.h"
27#include "bootutil/image.h"
28#include "bootutil/bootutil.h"
29#include "flash_map/flash_map.h"
Tamas Bana9de4a62018-09-18 08:09:45 +010030#include "bl2/include/boot_record.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000031
Tamas Ban581034a2017-12-19 19:54:37 +000032/* Avoids the semihosting issue */
33#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
34__asm(" .global __ARM_use_no_argv\n");
35#endif
36
David Vinczee0a3c2f2019-05-15 16:45:14 +020037/* Macros to pick linker symbols */
38#define REGION(a, b, c) a##b##c
39#define REGION_NAME(a, b, c) REGION(a, b, c)
40#define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
41
42REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
43
Tamas Banc3828852018-02-01 12:24:16 +000044/* Flash device name must be specified by target */
45extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
Tamas Banf70ef8c2017-12-19 15:35:09 +000046
Tamas Banbd3f7512018-01-26 15:45:03 +000047#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000
48/* Static buffer to be used by mbedtls for memory allocation */
49static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN];
Tamas Banf70ef8c2017-12-19 15:35:09 +000050
Tamas Banf70ef8c2017-12-19 15:35:09 +000051struct arm_vector_table {
52 uint32_t msp;
53 uint32_t reset;
54};
55
56static void do_boot(struct boot_rsp *rsp)
57{
Tamas Ban581034a2017-12-19 19:54:37 +000058 /* Clang at O0, stores variables on the stack with SP relative addressing.
59 * When manually set the SP then the place of reset vector is lost.
60 * Static variables are stored in 'data' or 'bss' section, change of SP has
61 * no effect on them.
62 */
63 static struct 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 */
79 vt = (struct arm_vector_table *)(rsp->br_hdr->ih_load_addr +
80 rsp->br_hdr->ih_hdr_size);
81 } else {
82 /* Using the flash address as not executing in SRAM */
83 vt = (struct arm_vector_table *)(flash_base +
84 rsp->br_image_off +
85 rsp->br_hdr->ih_hdr_size);
86 }
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 Vincze8da7f102018-09-24 10:53:46 +020093 stdio_uninit();
94
David Vinczee0a3c2f2019-05-15 16:45:14 +020095 /* Restore the Main Stack Pointer Limit register's reset value
96 * before passing execution to runtime firmware to make the
97 * bootloader transparent to it.
98 */
99 __set_MSPLIM(0);
100
Tamas Ban581034a2017-12-19 19:54:37 +0000101 __set_MSP(vt->msp);
102 __DSB();
103 __ISB();
104
Tamas Banf70ef8c2017-12-19 15:35:09 +0000105 ((void (*)(void))vt->reset)();
106}
Tamas Banf70ef8c2017-12-19 15:35:09 +0000107
Tamas Ban581034a2017-12-19 19:54:37 +0000108int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000109{
David Vinczee0a3c2f2019-05-15 16:45:14 +0200110 uint32_t msp_stack_bottom =
111 (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000112 struct boot_rsp rsp;
113 int rc;
114
David Vinczee0a3c2f2019-05-15 16:45:14 +0200115 __set_MSPLIM(msp_stack_bottom);
116
Gabor Kerteszeb953f52018-07-17 13:36:28 +0200117 stdio_init();
Tamas Ban581034a2017-12-19 19:54:37 +0000118
Tamas Banf70ef8c2017-12-19 15:35:09 +0000119 BOOT_LOG_INF("Starting bootloader");
120
Tamas Banbd3f7512018-01-26 15:45:03 +0000121 /* Initialise the mbedtls static memory allocator so that mbedtls allocates
122 * memory from the provided static buffer instead of from the heap.
123 */
124 mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000125
David Vinczeb57989f2018-09-24 10:59:04 +0200126 rc = FLASH_DEV_NAME.Initialize(NULL);
127 if(rc != ARM_DRIVER_OK) {
128 BOOT_LOG_ERR("Error while initializing Flash Interface");
129 while (1)
130 ;
131 }
David Vincze26e8c8a2018-08-28 16:59:41 +0200132
Tamas Banf70ef8c2017-12-19 15:35:09 +0000133 rc = boot_go(&rsp);
134 if (rc != 0) {
135 BOOT_LOG_ERR("Unable to find bootable image");
136 while (1)
137 ;
138 }
139
140 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
141 rsp.br_image_off);
Tamas Ban581034a2017-12-19 19:54:37 +0000142 flash_area_warn_on_open();
Tamas Banf70ef8c2017-12-19 15:35:09 +0000143 BOOT_LOG_INF("Jumping to the first image slot");
144 do_boot(&rsp);
145
146 BOOT_LOG_ERR("Never should get here");
147 while (1)
148 ;
149}