blob: ca30bb5c8b963e3ab6de3957afb031a7aba67866 [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>
Evan Gates4632d8d2018-06-28 13:27:40 -070023#include <soc.h>
David Brown5153bd62017-01-06 11:16:53 -070024
Marti Bolivar51181cf2017-03-20 11:03:41 -040025#include "target.h"
26
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050027#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020028#include "bootutil/image.h"
29#include "bootutil/bootutil.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020030#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020031
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020032#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030033#include "boot_serial/boot_serial.h"
34#include "serial_adapter/serial_adapter.h"
35
36const struct boot_uart_funcs boot_funcs = {
37 .read = console_read,
38 .write = console_write
39};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020040#endif
41
Andrew Boie7238f512017-03-02 13:39:06 -080042void os_heap_init(void);
43
44#if defined(CONFIG_ARM)
45struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060046 uint32_t msp;
47 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070048};
49
Andrew Boie7238f512017-03-02 13:39:06 -080050static void do_boot(struct boot_rsp *rsp)
51{
David Brown0d0652a2017-04-11 17:33:30 -060052 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040053 uintptr_t flash_base;
54 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080055
David Brown0d0652a2017-04-11 17:33:30 -060056 /* The beginning of the image is the ARM vector table, containing
57 * the initial stack pointer address and the reset vector
58 * consecutively. Manually set the stack pointer and jump into the
59 * reset vector
60 */
Marti Bolivareb940802017-05-01 23:15:29 -040061 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
62 assert(rc == 0);
63
64 vt = (struct arm_vector_table *)(flash_base +
65 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060066 rsp->br_hdr->ih_hdr_size);
67 irq_lock();
68 sys_clock_disable();
David Brown57837b92018-03-13 15:56:38 -060069 __set_MSP(vt->msp);
David Brown0d0652a2017-04-11 17:33:30 -060070 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080071}
72#else
73/* Default: Assume entry point is at the very beginning of the image. Simply
74 * lock interrupts and jump there. This is the right thing to do for X86 and
75 * possibly other platforms.
76 */
77static void do_boot(struct boot_rsp *rsp)
78{
Marti Bolivareb940802017-05-01 23:15:29 -040079 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -060080 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -040081 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080082
Marti Bolivareb940802017-05-01 23:15:29 -040083 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
84 assert(rc == 0);
85
86 start = (void *)(flash_base + rsp->br_image_off +
87 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080088
David Brown0d0652a2017-04-11 17:33:30 -060089 /* Lock interrupts and dive into the entry point */
90 irq_lock();
91 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -080092}
93#endif
David Brown5153bd62017-01-06 11:16:53 -070094
95void main(void)
96{
David Brown0d0652a2017-04-11 17:33:30 -060097 struct boot_rsp rsp;
98 int rc;
David Brown5153bd62017-01-06 11:16:53 -070099
David Brown0d0652a2017-04-11 17:33:30 -0600100 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200101
David Brown0d0652a2017-04-11 17:33:30 -0600102 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700103
Emanuele Di Santo205c8c62018-07-20 11:42:31 +0200104 if (!flash_device_get_binding(FLASH_DEV_NAME)) {
Marti Bolivar310445b2018-04-10 10:23:47 -0400105 BOOT_LOG_ERR("Flash device %s not found", FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600106 while (1)
107 ;
108 }
David Brown5153bd62017-01-06 11:16:53 -0700109
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200110#ifdef CONFIG_MCUBOOT_SERIAL
111
112 struct device *detect_port;
113 u32_t detect_value;
114
115 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
116 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
117
118 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
119 GPIO_DIR_IN | GPIO_PUD_PULL_UP);
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200120 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200121
122 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
123 &detect_value);
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200124 __ASSERT(rc == 0, "Error of the reading the detect pin.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200125
126 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
127 BOOT_LOG_INF("Enter the serial recovery mode");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300128 rc = boot_console_init();
Carles Cufie2a36122018-06-15 10:51:02 +0200129 __ASSERT(rc == 0, "Error initializing boot console.\n");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300130 boot_serial_start(&boot_funcs);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200131 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
132 }
133#endif
134
David Brown0d0652a2017-04-11 17:33:30 -0600135 rc = boot_go(&rsp);
136 if (rc != 0) {
137 BOOT_LOG_ERR("Unable to find bootable image");
138 while (1)
139 ;
140 }
David Brown5153bd62017-01-06 11:16:53 -0700141
Marti Bolivar88f48d92017-05-01 22:30:02 -0400142 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
143 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200144
David Brown0d0652a2017-04-11 17:33:30 -0600145 BOOT_LOG_INF("Jumping to the first image slot");
146 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700147
David Brown0d0652a2017-04-11 17:33:30 -0600148 BOOT_LOG_ERR("Never should get here");
149 while (1)
150 ;
David Brown5153bd62017-01-06 11:16:53 -0700151}