blob: ee0b36b2705fc2cff604a83cf2aace62420ea33a [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 Bolivar4a97b4c2017-01-31 18:20:02 -050022#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
23#include "bootutil/bootutil_log.h"
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020024
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020025#if defined(MCUBOOT_TARGET_CONFIG)
26#include MCUBOOT_TARGET_CONFIG
David Brown5153bd62017-01-06 11:16:53 -070027#else
28#error "Board is currently not supported by bootloader"
29#endif
30
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020031#include "bootutil/image.h"
32#include "bootutil/bootutil.h"
33
David Brown5153bd62017-01-06 11:16:53 -070034struct device *boot_flash_device;
35
Andrew Boie7238f512017-03-02 13:39:06 -080036void os_heap_init(void);
37
38#if defined(CONFIG_ARM)
39struct arm_vector_table {
David Brown5153bd62017-01-06 11:16:53 -070040 uint32_t msp;
41 uint32_t reset;
42};
43
Andrew Boie7238f512017-03-02 13:39:06 -080044static void do_boot(struct boot_rsp *rsp)
45{
46 struct arm_vector_table *vt;
47
48 /* The beginning of the image is the ARM vector table, containing
49 * the initial stack pointer address and the reset vector
50 * consecutively. Manually set the stack pointer and jump into the
51 * reset vector
52 */
Marti Bolivar836768b2017-03-16 15:14:21 -040053 vt = (struct arm_vector_table *)(rsp->br_image_addr +
54 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080055 irq_lock();
Ricardo Salveti8e4d44d2017-02-27 23:00:31 -030056 sys_clock_disable();
Andrew Boie7238f512017-03-02 13:39:06 -080057 _MspSet(vt->msp);
58 ((void (*)(void))vt->reset)();
59}
60#else
61/* Default: Assume entry point is at the very beginning of the image. Simply
62 * lock interrupts and jump there. This is the right thing to do for X86 and
63 * possibly other platforms.
64 */
65static void do_boot(struct boot_rsp *rsp)
66{
67 void *start;
68
69 start = (void *)(rsp->br_image_addr + rsp->br_hdr->ih_hdr_size);
70
71 /* Lock interrupts and dive into the entry point */
72 irq_lock();
73 ((void (*)(void))start)();
74}
75#endif
David Brown5153bd62017-01-06 11:16:53 -070076
77void main(void)
78{
79 struct boot_rsp rsp;
David Brown5153bd62017-01-06 11:16:53 -070080 int rc;
81
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050082 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020083
David Brown5153bd62017-01-06 11:16:53 -070084 os_heap_init();
85
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020086 boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
David Brown5153bd62017-01-06 11:16:53 -070087 if (!boot_flash_device) {
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050088 BOOT_LOG_ERR("Flash device not found");
David Brown5153bd62017-01-06 11:16:53 -070089 while (1)
90 ;
91 }
92
93 rc = boot_go(&rsp);
94 if (rc != 0) {
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050095 BOOT_LOG_ERR("Unable to find bootable image");
David Brown5153bd62017-01-06 11:16:53 -070096 while (1)
97 ;
98 }
99
Marti Bolivar4a97b4c2017-01-31 18:20:02 -0500100 BOOT_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
Marti Bolivar4a97b4c2017-01-31 18:20:02 -0500101 BOOT_LOG_INF("Jumping to the first image slot");
Andrew Boie7238f512017-03-02 13:39:06 -0800102 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700103
Marti Bolivar4a97b4c2017-01-31 18:20:02 -0500104 BOOT_LOG_ERR("Never should get here");
David Brown5153bd62017-01-06 11:16:53 -0700105 while (1)
106 ;
107}