blob: ca88fbd62a8573c19884d45f89deb0213005b5b7 [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
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020021#define SYS_LOG_DOMAIN "BOOTLOADER"
22#define SYS_LOG_LEVEL SYS_LOG_LEVEL_INFO
23#include <logging/sys_log.h>
24
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
36struct vector_table {
37 uint32_t msp;
38 uint32_t reset;
39};
40
41void os_heap_init(void);
42
43void main(void)
44{
45 struct boot_rsp rsp;
46 struct vector_table *vt;
47 int rc;
48
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020049 SYS_LOG_INF("Starting bootloader");
50
David Brown5153bd62017-01-06 11:16:53 -070051 os_heap_init();
52
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020053 boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
David Brown5153bd62017-01-06 11:16:53 -070054 if (!boot_flash_device) {
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020055 SYS_LOG_ERR("Flash device not found");
David Brown5153bd62017-01-06 11:16:53 -070056 while (1)
57 ;
58 }
59
60 rc = boot_go(&rsp);
61 if (rc != 0) {
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020062 SYS_LOG_ERR("Unable to find bootable image");
David Brown5153bd62017-01-06 11:16:53 -070063 while (1)
64 ;
65 }
66
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020067 SYS_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
David Brown5153bd62017-01-06 11:16:53 -070068 vt = (struct vector_table *)(rsp.br_image_addr +
69 rsp.br_hdr->ih_hdr_size);
70 irq_lock();
71 _MspSet(vt->msp);
72
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020073 SYS_LOG_INF("Jumping to the first image slot");
David Brown5153bd62017-01-06 11:16:53 -070074 ((void (*)(void))vt->reset)();
75
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020076 SYS_LOG_ERR("Never should get here");
David Brown5153bd62017-01-06 11:16:53 -070077 while (1)
78 ;
79}