blob: f037a2b6ff41e222b246e26a90488e52040edcb3 [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 Brown5153bd62017-01-06 11:16:53 -070035 uint32_t msp;
36 uint32_t reset;
37};
38
Andrew Boie7238f512017-03-02 13:39:06 -080039static void do_boot(struct boot_rsp *rsp)
40{
41 struct arm_vector_table *vt;
42
43 /* 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 */
Marti Bolivar836768b2017-03-16 15:14:21 -040048 vt = (struct arm_vector_table *)(rsp->br_image_addr +
49 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080050 irq_lock();
Ricardo Salveti8e4d44d2017-02-27 23:00:31 -030051 sys_clock_disable();
Andrew Boie7238f512017-03-02 13:39:06 -080052 _MspSet(vt->msp);
53 ((void (*)(void))vt->reset)();
54}
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{
62 void *start;
63
64 start = (void *)(rsp->br_image_addr + rsp->br_hdr->ih_hdr_size);
65
66 /* Lock interrupts and dive into the entry point */
67 irq_lock();
68 ((void (*)(void))start)();
69}
70#endif
David Brown5153bd62017-01-06 11:16:53 -070071
72void main(void)
73{
74 struct boot_rsp rsp;
David Brown5153bd62017-01-06 11:16:53 -070075 int rc;
76
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050077 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020078
David Brown5153bd62017-01-06 11:16:53 -070079 os_heap_init();
80
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020081 boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
David Brown5153bd62017-01-06 11:16:53 -070082 if (!boot_flash_device) {
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050083 BOOT_LOG_ERR("Flash device not found");
David Brown5153bd62017-01-06 11:16:53 -070084 while (1)
85 ;
86 }
87
88 rc = boot_go(&rsp);
89 if (rc != 0) {
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050090 BOOT_LOG_ERR("Unable to find bootable image");
David Brown5153bd62017-01-06 11:16:53 -070091 while (1)
92 ;
93 }
94
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050095 BOOT_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050096 BOOT_LOG_INF("Jumping to the first image slot");
Andrew Boie7238f512017-03-02 13:39:06 -080097 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -070098
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050099 BOOT_LOG_ERR("Never should get here");
David Brown5153bd62017-01-06 11:16:53 -0700100 while (1)
101 ;
102}