blob: 4e79f0a573cc7b7916c613cf96f600b7ea377f6c [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>
20
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050021#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
22#include "bootutil/bootutil_log.h"
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020023
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020024#if defined(MCUBOOT_TARGET_CONFIG)
25#include MCUBOOT_TARGET_CONFIG
David Brown5153bd62017-01-06 11:16:53 -070026#else
27#error "Board is currently not supported by bootloader"
28#endif
29
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020030#include "bootutil/image.h"
31#include "bootutil/bootutil.h"
32
David Brown5153bd62017-01-06 11:16:53 -070033struct device *boot_flash_device;
34
Andrew Boie7238f512017-03-02 13:39:06 -080035void os_heap_init(void);
36
37#if defined(CONFIG_ARM)
38struct arm_vector_table {
David Brown5153bd62017-01-06 11:16:53 -070039 uint32_t msp;
40 uint32_t reset;
41};
42
Andrew Boie7238f512017-03-02 13:39:06 -080043static void do_boot(struct boot_rsp *rsp)
44{
45 struct arm_vector_table *vt;
46
47 /* The beginning of the image is the ARM vector table, containing
48 * the initial stack pointer address and the reset vector
49 * consecutively. Manually set the stack pointer and jump into the
50 * reset vector
51 */
52 vt = (struct vector_table *)(rsp->br_image_addr +
53 rsp->br_hdr->ih_hdr_size);
54 irq_lock();
55 _MspSet(vt->msp);
56 ((void (*)(void))vt->reset)();
57}
58#else
59/* Default: Assume entry point is at the very beginning of the image. Simply
60 * lock interrupts and jump there. This is the right thing to do for X86 and
61 * possibly other platforms.
62 */
63static void do_boot(struct boot_rsp *rsp)
64{
65 void *start;
66
67 start = (void *)(rsp->br_image_addr + rsp->br_hdr->ih_hdr_size);
68
69 /* Lock interrupts and dive into the entry point */
70 irq_lock();
71 ((void (*)(void))start)();
72}
73#endif
David Brown5153bd62017-01-06 11:16:53 -070074
75void main(void)
76{
77 struct boot_rsp rsp;
David Brown5153bd62017-01-06 11:16:53 -070078 int rc;
79
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050080 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020081
David Brown5153bd62017-01-06 11:16:53 -070082 os_heap_init();
83
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020084 boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
David Brown5153bd62017-01-06 11:16:53 -070085 if (!boot_flash_device) {
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050086 BOOT_LOG_ERR("Flash device not found");
David Brown5153bd62017-01-06 11:16:53 -070087 while (1)
88 ;
89 }
90
91 rc = boot_go(&rsp);
92 if (rc != 0) {
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050093 BOOT_LOG_ERR("Unable to find bootable image");
David Brown5153bd62017-01-06 11:16:53 -070094 while (1)
95 ;
96 }
97
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050098 BOOT_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050099 BOOT_LOG_INF("Jumping to the first image slot");
Andrew Boie7238f512017-03-02 13:39:06 -0800100 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700101
Marti Bolivar4a97b4c2017-01-31 18:20:02 -0500102 BOOT_LOG_ERR("Never should get here");
David Brown5153bd62017-01-06 11:16:53 -0700103 while (1)
104 ;
105}