blob: 09286b69785d3fcbc03438777e2f0d0e38865dc0 [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>
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020019#include <gpio.h>
20#include <misc/__assert.h>
David Brown5153bd62017-01-06 11:16:53 -070021#include <flash.h>
Ricardo Salveti8e4d44d2017-02-27 23:00:31 -030022#include <drivers/system_timer.h>
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +020023#include <usb/usb_device.h>
Evan Gates4632d8d2018-06-28 13:27:40 -070024#include <soc.h>
David Brown5153bd62017-01-06 11:16:53 -070025
Marti Bolivar51181cf2017-03-20 11:03:41 -040026#include "target.h"
27
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050028#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020029#include "bootutil/image.h"
30#include "bootutil/bootutil.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020031#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020032
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020033#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030034#include "boot_serial/boot_serial.h"
35#include "serial_adapter/serial_adapter.h"
36
37const struct boot_uart_funcs boot_funcs = {
38 .read = console_read,
39 .write = console_write
40};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020041#endif
42
Andrew Boie7238f512017-03-02 13:39:06 -080043void os_heap_init(void);
44
45#if defined(CONFIG_ARM)
46struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060047 uint32_t msp;
48 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070049};
50
Emanuele Di Santofcfff582018-11-01 16:06:36 +010051extern void sys_clock_disable(void);
52
Andrew Boie7238f512017-03-02 13:39:06 -080053static void do_boot(struct boot_rsp *rsp)
54{
David Brown0d0652a2017-04-11 17:33:30 -060055 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040056 uintptr_t flash_base;
57 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080058
David Brown0d0652a2017-04-11 17:33:30 -060059 /* The beginning of the image is the ARM vector table, containing
60 * the initial stack pointer address and the reset vector
61 * consecutively. Manually set the stack pointer and jump into the
62 * reset vector
63 */
Marti Bolivareb940802017-05-01 23:15:29 -040064 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
65 assert(rc == 0);
66
67 vt = (struct arm_vector_table *)(flash_base +
68 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060069 rsp->br_hdr->ih_hdr_size);
70 irq_lock();
71 sys_clock_disable();
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +020072#ifdef CONFIG_BOOT_SERIAL_CDC_ACM
73 /* Disable the USB to prevent it from firing interrupts */
74 usb_disable();
75#endif
David Brown57837b92018-03-13 15:56:38 -060076 __set_MSP(vt->msp);
David Brown0d0652a2017-04-11 17:33:30 -060077 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080078}
79#else
80/* Default: Assume entry point is at the very beginning of the image. Simply
81 * lock interrupts and jump there. This is the right thing to do for X86 and
82 * possibly other platforms.
83 */
84static void do_boot(struct boot_rsp *rsp)
85{
Marti Bolivareb940802017-05-01 23:15:29 -040086 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -060087 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -040088 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080089
Marti Bolivareb940802017-05-01 23:15:29 -040090 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
91 assert(rc == 0);
92
93 start = (void *)(flash_base + rsp->br_image_off +
94 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080095
David Brown0d0652a2017-04-11 17:33:30 -060096 /* Lock interrupts and dive into the entry point */
97 irq_lock();
98 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -080099}
100#endif
David Brown5153bd62017-01-06 11:16:53 -0700101
102void main(void)
103{
David Brown0d0652a2017-04-11 17:33:30 -0600104 struct boot_rsp rsp;
105 int rc;
David Brown5153bd62017-01-06 11:16:53 -0700106
David Brown0d0652a2017-04-11 17:33:30 -0600107 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200108
David Brown0d0652a2017-04-11 17:33:30 -0600109 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700110
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530111#ifdef DT_FLASH_DEV_NAME
Andrzej Puzdrowskif50054d2018-11-14 14:33:19 +0100112 if (!flash_device_get_binding(DT_FLASH_DEV_NAME)) {
113 BOOT_LOG_ERR("Flash device %s not found", DT_FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600114 while (1)
115 ;
116 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530117#endif
David Brown5153bd62017-01-06 11:16:53 -0700118
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200119#ifdef CONFIG_MCUBOOT_SERIAL
120
121 struct device *detect_port;
122 u32_t detect_value;
123
124 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
125 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
126
127 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
128 GPIO_DIR_IN | GPIO_PUD_PULL_UP);
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200129 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200130
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100131 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200132 &detect_value);
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200133 __ASSERT(rc == 0, "Error of the reading the detect pin.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200134
135 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
136 BOOT_LOG_INF("Enter the serial recovery mode");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300137 rc = boot_console_init();
Carles Cufie2a36122018-06-15 10:51:02 +0200138 __ASSERT(rc == 0, "Error initializing boot console.\n");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300139 boot_serial_start(&boot_funcs);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200140 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
141 }
142#endif
143
David Brown0d0652a2017-04-11 17:33:30 -0600144 rc = boot_go(&rsp);
145 if (rc != 0) {
146 BOOT_LOG_ERR("Unable to find bootable image");
147 while (1)
148 ;
149 }
David Brown5153bd62017-01-06 11:16:53 -0700150
Marti Bolivar88f48d92017-05-01 22:30:02 -0400151 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
152 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200153
David Brown0d0652a2017-04-11 17:33:30 -0600154 BOOT_LOG_INF("Jumping to the first image slot");
155 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700156
David Brown0d0652a2017-04-11 17:33:30 -0600157 BOOT_LOG_ERR("Never should get here");
158 while (1)
159 ;
David Brown5153bd62017-01-06 11:16:53 -0700160}