blob: 50da9864f3bd945727eb6aee975c8002dd4541e1 [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
35struct vector_table {
36 uint32_t msp;
37 uint32_t reset;
38};
39
40void os_heap_init(void);
41
42void main(void)
43{
44 struct boot_rsp rsp;
45 struct vector_table *vt;
46 int rc;
47
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050048 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020049
David Brown5153bd62017-01-06 11:16:53 -070050 os_heap_init();
51
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020052 boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
David Brown5153bd62017-01-06 11:16:53 -070053 if (!boot_flash_device) {
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050054 BOOT_LOG_ERR("Flash device not found");
David Brown5153bd62017-01-06 11:16:53 -070055 while (1)
56 ;
57 }
58
59 rc = boot_go(&rsp);
60 if (rc != 0) {
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050061 BOOT_LOG_ERR("Unable to find bootable image");
David Brown5153bd62017-01-06 11:16:53 -070062 while (1)
63 ;
64 }
65
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050066 BOOT_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
David Brown5153bd62017-01-06 11:16:53 -070067 vt = (struct vector_table *)(rsp.br_image_addr +
68 rsp.br_hdr->ih_hdr_size);
69 irq_lock();
70 _MspSet(vt->msp);
71
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050072 BOOT_LOG_INF("Jumping to the first image slot");
David Brown5153bd62017-01-06 11:16:53 -070073 ((void (*)(void))vt->reset)();
74
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050075 BOOT_LOG_ERR("Never should get here");
David Brown5153bd62017-01-06 11:16:53 -070076 while (1)
77 ;
78}