blob: 9791f264d2619fa28ea5de873bd8e51ca184746c [file] [log] [blame]
David Brown5153bd62017-01-06 11:16:53 -07001/*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <zephyr.h>
David Brown5153bd62017-01-06 11:16:53 -070018#include <flash.h>
19#include <asm_inline.h>
Ricardo Salveti8e4d44d2017-02-27 23:00:31 -030020#include <drivers/system_timer.h>
David Brown5153bd62017-01-06 11:16:53 -070021
Marti Bolivar51181cf2017-03-20 11:03:41 -040022#include "target.h"
23
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050024#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
25#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020026#include "bootutil/image.h"
27#include "bootutil/bootutil.h"
28
David Brown5153bd62017-01-06 11:16:53 -070029struct device *boot_flash_device;
30
Andrew Boie7238f512017-03-02 13:39:06 -080031void os_heap_init(void);
32
33#if defined(CONFIG_ARM)
34struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060035 uint32_t msp;
36 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070037};
38
Andrew Boie7238f512017-03-02 13:39:06 -080039static void do_boot(struct boot_rsp *rsp)
40{
David Brown0d0652a2017-04-11 17:33:30 -060041 struct arm_vector_table *vt;
Andrew Boie7238f512017-03-02 13:39:06 -080042
David Brown0d0652a2017-04-11 17:33:30 -060043 /* The beginning of the image is the ARM vector table, containing
44 * the initial stack pointer address and the reset vector
45 * consecutively. Manually set the stack pointer and jump into the
46 * reset vector
47 */
48 vt = (struct arm_vector_table *)(rsp->br_image_addr +
49 rsp->br_hdr->ih_hdr_size);
50 irq_lock();
51 sys_clock_disable();
52 _MspSet(vt->msp);
53 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080054}
55#else
56/* Default: Assume entry point is at the very beginning of the image. Simply
57 * lock interrupts and jump there. This is the right thing to do for X86 and
58 * possibly other platforms.
59 */
60static void do_boot(struct boot_rsp *rsp)
61{
David Brown0d0652a2017-04-11 17:33:30 -060062 void *start;
Andrew Boie7238f512017-03-02 13:39:06 -080063
David Brown0d0652a2017-04-11 17:33:30 -060064 start = (void *)(rsp->br_image_addr + rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080065
David Brown0d0652a2017-04-11 17:33:30 -060066 /* Lock interrupts and dive into the entry point */
67 irq_lock();
68 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -080069}
70#endif
David Brown5153bd62017-01-06 11:16:53 -070071
72void main(void)
73{
David Brown0d0652a2017-04-11 17:33:30 -060074 struct boot_rsp rsp;
75 int rc;
David Brown5153bd62017-01-06 11:16:53 -070076
David Brown0d0652a2017-04-11 17:33:30 -060077 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020078
David Brown0d0652a2017-04-11 17:33:30 -060079 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -070080
David Brown0d0652a2017-04-11 17:33:30 -060081 boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
82 if (!boot_flash_device) {
83 BOOT_LOG_ERR("Flash device not found");
84 while (1)
85 ;
86 }
David Brown5153bd62017-01-06 11:16:53 -070087
David Brown0d0652a2017-04-11 17:33:30 -060088 rc = boot_go(&rsp);
89 if (rc != 0) {
90 BOOT_LOG_ERR("Unable to find bootable image");
91 while (1)
92 ;
93 }
David Brown5153bd62017-01-06 11:16:53 -070094
David Brown0d0652a2017-04-11 17:33:30 -060095 BOOT_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
96 BOOT_LOG_INF("Jumping to the first image slot");
97 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -070098
David Brown0d0652a2017-04-11 17:33:30 -060099 BOOT_LOG_ERR("Never should get here");
100 while (1)
101 ;
David Brown5153bd62017-01-06 11:16:53 -0700102}