blob: e5a1eb89b26198af913f4fefdaa80f8dd9b1d6b9 [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
Marti Bolivareb940802017-05-01 23:15:29 -040017#include <assert.h>
David Brown5153bd62017-01-06 11:16:53 -070018#include <zephyr.h>
David Brown5153bd62017-01-06 11:16:53 -070019#include <flash.h>
20#include <asm_inline.h>
Ricardo Salveti8e4d44d2017-02-27 23:00:31 -030021#include <drivers/system_timer.h>
David Brown5153bd62017-01-06 11:16:53 -070022
Marti Bolivar51181cf2017-03-20 11:03:41 -040023#include "target.h"
24
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050025#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
26#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020027#include "bootutil/image.h"
28#include "bootutil/bootutil.h"
Marti Bolivareb940802017-05-01 23:15:29 -040029#include "flash_map/flash_map.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020030
David Brown5153bd62017-01-06 11:16:53 -070031struct device *boot_flash_device;
32
Andrew Boie7238f512017-03-02 13:39:06 -080033void os_heap_init(void);
34
35#if defined(CONFIG_ARM)
36struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060037 uint32_t msp;
38 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070039};
40
Marti Bolivar83a3cef2017-05-05 11:59:49 -040041extern void zephyr_flash_area_warn_on_open(void);
42
Andrew Boie7238f512017-03-02 13:39:06 -080043static void do_boot(struct boot_rsp *rsp)
44{
David Brown0d0652a2017-04-11 17:33:30 -060045 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040046 uintptr_t flash_base;
47 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080048
David Brown0d0652a2017-04-11 17:33:30 -060049 /* The beginning of the image is the ARM vector table, containing
50 * the initial stack pointer address and the reset vector
51 * consecutively. Manually set the stack pointer and jump into the
52 * reset vector
53 */
Marti Bolivareb940802017-05-01 23:15:29 -040054 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
55 assert(rc == 0);
56
57 vt = (struct arm_vector_table *)(flash_base +
58 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060059 rsp->br_hdr->ih_hdr_size);
60 irq_lock();
61 sys_clock_disable();
62 _MspSet(vt->msp);
63 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080064}
65#else
66/* Default: Assume entry point is at the very beginning of the image. Simply
67 * lock interrupts and jump there. This is the right thing to do for X86 and
68 * possibly other platforms.
69 */
70static void do_boot(struct boot_rsp *rsp)
71{
Marti Bolivareb940802017-05-01 23:15:29 -040072 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -060073 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -040074 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080075
Marti Bolivareb940802017-05-01 23:15:29 -040076 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
77 assert(rc == 0);
78
79 start = (void *)(flash_base + rsp->br_image_off +
80 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080081
David Brown0d0652a2017-04-11 17:33:30 -060082 /* Lock interrupts and dive into the entry point */
83 irq_lock();
84 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -080085}
86#endif
David Brown5153bd62017-01-06 11:16:53 -070087
88void main(void)
89{
David Brown0d0652a2017-04-11 17:33:30 -060090 struct boot_rsp rsp;
91 int rc;
David Brown5153bd62017-01-06 11:16:53 -070092
David Brown0d0652a2017-04-11 17:33:30 -060093 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020094
David Brown0d0652a2017-04-11 17:33:30 -060095 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -070096
David Brown0d0652a2017-04-11 17:33:30 -060097 boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
98 if (!boot_flash_device) {
99 BOOT_LOG_ERR("Flash device not found");
100 while (1)
101 ;
102 }
David Brown5153bd62017-01-06 11:16:53 -0700103
David Brown0d0652a2017-04-11 17:33:30 -0600104 rc = boot_go(&rsp);
105 if (rc != 0) {
106 BOOT_LOG_ERR("Unable to find bootable image");
107 while (1)
108 ;
109 }
David Brown5153bd62017-01-06 11:16:53 -0700110
Marti Bolivar88f48d92017-05-01 22:30:02 -0400111 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
112 rsp.br_image_off);
Marti Bolivar83a3cef2017-05-05 11:59:49 -0400113 zephyr_flash_area_warn_on_open();
David Brown0d0652a2017-04-11 17:33:30 -0600114 BOOT_LOG_INF("Jumping to the first image slot");
115 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700116
David Brown0d0652a2017-04-11 17:33:30 -0600117 BOOT_LOG_ERR("Never should get here");
118 while (1)
119 ;
David Brown5153bd62017-01-06 11:16:53 -0700120}