blob: 054c4668e5ee6c17f94ee509a2840a7d31829275 [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
Andrew Boie7238f512017-03-02 13:39:06 -080041static void do_boot(struct boot_rsp *rsp)
42{
David Brown0d0652a2017-04-11 17:33:30 -060043 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040044 uintptr_t flash_base;
45 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080046
David Brown0d0652a2017-04-11 17:33:30 -060047 /* 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 */
Marti Bolivareb940802017-05-01 23:15:29 -040052 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
53 assert(rc == 0);
54
55 vt = (struct arm_vector_table *)(flash_base +
56 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060057 rsp->br_hdr->ih_hdr_size);
58 irq_lock();
59 sys_clock_disable();
60 _MspSet(vt->msp);
61 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080062}
63#else
64/* Default: Assume entry point is at the very beginning of the image. Simply
65 * lock interrupts and jump there. This is the right thing to do for X86 and
66 * possibly other platforms.
67 */
68static void do_boot(struct boot_rsp *rsp)
69{
Marti Bolivareb940802017-05-01 23:15:29 -040070 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -060071 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -040072 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080073
Marti Bolivareb940802017-05-01 23:15:29 -040074 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
75 assert(rc == 0);
76
77 start = (void *)(flash_base + rsp->br_image_off +
78 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080079
David Brown0d0652a2017-04-11 17:33:30 -060080 /* Lock interrupts and dive into the entry point */
81 irq_lock();
82 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -080083}
84#endif
David Brown5153bd62017-01-06 11:16:53 -070085
86void main(void)
87{
David Brown0d0652a2017-04-11 17:33:30 -060088 struct boot_rsp rsp;
89 int rc;
David Brown5153bd62017-01-06 11:16:53 -070090
David Brown0d0652a2017-04-11 17:33:30 -060091 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020092
David Brown0d0652a2017-04-11 17:33:30 -060093 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -070094
David Brown0d0652a2017-04-11 17:33:30 -060095 boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
96 if (!boot_flash_device) {
97 BOOT_LOG_ERR("Flash device not found");
98 while (1)
99 ;
100 }
David Brown5153bd62017-01-06 11:16:53 -0700101
David Brown0d0652a2017-04-11 17:33:30 -0600102 rc = boot_go(&rsp);
103 if (rc != 0) {
104 BOOT_LOG_ERR("Unable to find bootable image");
105 while (1)
106 ;
107 }
David Brown5153bd62017-01-06 11:16:53 -0700108
Marti Bolivar88f48d92017-05-01 22:30:02 -0400109 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
110 rsp.br_image_off);
David Brown0d0652a2017-04-11 17:33:30 -0600111 BOOT_LOG_INF("Jumping to the first image slot");
112 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700113
David Brown0d0652a2017-04-11 17:33:30 -0600114 BOOT_LOG_ERR("Never should get here");
115 while (1)
116 ;
David Brown5153bd62017-01-06 11:16:53 -0700117}