blob: 952e52801ef90d024164be990ce06f8d72900c4f [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
Tamas Banc3828852018-02-01 12:24:16 +00003 * Copyright (c) 2017-2018 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 Banf70ef8c2017-12-19 15:35:09 +000024
25#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"
30
Tamas Ban581034a2017-12-19 19:54:37 +000031/* Avoids the semihosting issue */
32#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
33__asm(" .global __ARM_use_no_argv\n");
34#endif
35
Tamas Banc3828852018-02-01 12:24:16 +000036/* Flash device name must be specified by target */
37extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
Tamas Banf70ef8c2017-12-19 15:35:09 +000038
39void os_heap_init(void);
40
Tamas Banf70ef8c2017-12-19 15:35:09 +000041struct arm_vector_table {
42 uint32_t msp;
43 uint32_t reset;
44};
45
46static void do_boot(struct boot_rsp *rsp)
47{
Tamas Ban581034a2017-12-19 19:54:37 +000048 /* Clang at O0, stores variables on the stack with SP relative addressing.
49 * When manually set the SP then the place of reset vector is lost.
50 * Static variables are stored in 'data' or 'bss' section, change of SP has
51 * no effect on them.
52 */
53 static struct arm_vector_table *vt;
Tamas Banf70ef8c2017-12-19 15:35:09 +000054 uintptr_t flash_base;
55 int rc;
56
57 /* The beginning of the image is the ARM vector table, containing
58 * the initial stack pointer address and the reset vector
59 * consecutively. Manually set the stack pointer and jump into the
60 * reset vector
61 */
62 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
63 assert(rc == 0);
64
65 vt = (struct arm_vector_table *)(flash_base +
66 rsp->br_image_off +
67 rsp->br_hdr->ih_hdr_size);
Tamas Ban581034a2017-12-19 19:54:37 +000068 __disable_irq();
69 __set_MSP(vt->msp);
70 __DSB();
71 __ISB();
72
Tamas Banf70ef8c2017-12-19 15:35:09 +000073 ((void (*)(void))vt->reset)();
74}
Tamas Banf70ef8c2017-12-19 15:35:09 +000075
Tamas Ban581034a2017-12-19 19:54:37 +000076int main(void)
Tamas Banf70ef8c2017-12-19 15:35:09 +000077{
78 struct boot_rsp rsp;
79 int rc;
80
Tamas Ban581034a2017-12-19 19:54:37 +000081 uart_init(UART0_CHANNEL);
82
Tamas Banf70ef8c2017-12-19 15:35:09 +000083 BOOT_LOG_INF("Starting bootloader");
84
85 os_heap_init();
86
Tamas Banc3828852018-02-01 12:24:16 +000087 /* Initialize Flash driver */
88 FLASH_DEV_NAME.Initialize(NULL);
Tamas Banf70ef8c2017-12-19 15:35:09 +000089 rc = boot_go(&rsp);
90 if (rc != 0) {
91 BOOT_LOG_ERR("Unable to find bootable image");
92 while (1)
93 ;
94 }
95
96 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
97 rsp.br_image_off);
Tamas Ban581034a2017-12-19 19:54:37 +000098 flash_area_warn_on_open();
Tamas Banf70ef8c2017-12-19 15:35:09 +000099 BOOT_LOG_INF("Jumping to the first image slot");
100 do_boot(&rsp);
101
102 BOOT_LOG_ERR("Never should get here");
103 while (1)
104 ;
105}