blob: e2e609fbecf7e713497ae1e4c0333b1542f80d73 [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>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060019#include <drivers/gpio.h>
Andrzej Puzdrowskif1d189c2019-12-12 09:34:11 +010020#include <sys/__assert.h>
Peter Bigot54c1e3f2020-01-25 05:50:12 -060021#include <drivers/flash.h>
Andrzej Puzdrowskif99a4c72019-06-28 15:16:35 +020022#include <drivers/timer/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
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053043#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
44#include <usb/class/usb_dfu.h>
45#endif
46
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010047#ifdef CONFIG_SOC_FAMILY_NRF
48#include <hal/nrf_power.h>
49
50static inline bool boot_skip_serial_recovery()
51{
Fabio Utzig63b4eac2019-12-17 09:59:47 -030052#if NRF_POWER_HAS_RESETREAS
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010053 u32_t rr = nrf_power_resetreas_get(NRF_POWER);
54
55 return !(rr == 0 || (rr & NRF_POWER_RESETREAS_RESETPIN_MASK));
56#else
57 return false;
58#endif
59}
60#else
61static inline bool boot_skip_serial_recovery()
62{
63 return false;
64}
65#endif
66
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010067MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
68
Andrew Boie7238f512017-03-02 13:39:06 -080069void os_heap_init(void);
70
71#if defined(CONFIG_ARM)
72struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060073 uint32_t msp;
74 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070075};
76
Emanuele Di Santofcfff582018-11-01 16:06:36 +010077extern void sys_clock_disable(void);
78
Andrew Boie7238f512017-03-02 13:39:06 -080079static void do_boot(struct boot_rsp *rsp)
80{
David Brown0d0652a2017-04-11 17:33:30 -060081 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040082 uintptr_t flash_base;
83 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080084
David Brown0d0652a2017-04-11 17:33:30 -060085 /* The beginning of the image is the ARM vector table, containing
86 * the initial stack pointer address and the reset vector
87 * consecutively. Manually set the stack pointer and jump into the
88 * reset vector
89 */
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 vt = (struct arm_vector_table *)(flash_base +
94 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060095 rsp->br_hdr->ih_hdr_size);
96 irq_lock();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +020097#ifdef CONFIG_SYS_CLOCK_EXISTS
David Brown0d0652a2017-04-11 17:33:30 -060098 sys_clock_disable();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +020099#endif
Jun Li01bef712019-05-08 21:55:54 -0700100#ifdef CONFIG_USB
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200101 /* Disable the USB to prevent it from firing interrupts */
102 usb_disable();
103#endif
David Brown57837b92018-03-13 15:56:38 -0600104 __set_MSP(vt->msp);
David Brown0d0652a2017-04-11 17:33:30 -0600105 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800106}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530107
108#elif defined(CONFIG_XTENSA)
109#define SRAM_BASE_ADDRESS 0xBE030000
110
111static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
112{
113 const struct flash_area *fap;
114 int area_id;
115 int rc;
116 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
117
118 BOOT_LOG_INF("Copying image to SRAM");
119
120 area_id = flash_area_id_from_image_slot(slot);
121 rc = flash_area_open(area_id, &fap);
122 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300123 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530124 goto done;
125 }
126
127 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
128 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300129 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530130 goto done;
131 }
132
133done:
134 flash_area_close(fap);
135}
136
137/* Entry point (.ResetVector) is at the very beginning of the image.
138 * Simply copy the image to a suitable location and jump there.
139 */
140static void do_boot(struct boot_rsp *rsp)
141{
142 void *start;
143
144 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
145 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
146
147 /* Copy from the flash to HP SRAM */
148 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
149
150 /* Jump to entry point */
151 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
152 ((void (*)(void))start)();
153}
154
Andrew Boie7238f512017-03-02 13:39:06 -0800155#else
156/* Default: Assume entry point is at the very beginning of the image. Simply
157 * lock interrupts and jump there. This is the right thing to do for X86 and
158 * possibly other platforms.
159 */
160static void do_boot(struct boot_rsp *rsp)
161{
Marti Bolivareb940802017-05-01 23:15:29 -0400162 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -0600163 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -0400164 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800165
Marti Bolivareb940802017-05-01 23:15:29 -0400166 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
167 assert(rc == 0);
168
169 start = (void *)(flash_base + rsp->br_image_off +
170 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -0800171
David Brown0d0652a2017-04-11 17:33:30 -0600172 /* Lock interrupts and dive into the entry point */
173 irq_lock();
174 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800175}
176#endif
David Brown5153bd62017-01-06 11:16:53 -0700177
178void main(void)
179{
David Brown0d0652a2017-04-11 17:33:30 -0600180 struct boot_rsp rsp;
181 int rc;
David Brown5153bd62017-01-06 11:16:53 -0700182
David Brown0d0652a2017-04-11 17:33:30 -0600183 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200184
David Brown0d0652a2017-04-11 17:33:30 -0600185 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700186
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530187#if (!defined(CONFIG_XTENSA) && defined(DT_FLASH_DEV_NAME))
Andrzej Puzdrowskif50054d2018-11-14 14:33:19 +0100188 if (!flash_device_get_binding(DT_FLASH_DEV_NAME)) {
189 BOOT_LOG_ERR("Flash device %s not found", DT_FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600190 while (1)
191 ;
192 }
Rajavardhan Gundi24321c32019-02-08 12:48:34 +0530193#elif (defined(CONFIG_XTENSA) && defined(DT_JEDEC_SPI_NOR_0_LABEL))
194 if (!flash_device_get_binding(DT_JEDEC_SPI_NOR_0_LABEL)) {
195 BOOT_LOG_ERR("Flash device %s not found", DT_JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530196 while (1)
197 ;
198 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530199#endif
David Brown5153bd62017-01-06 11:16:53 -0700200
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200201#ifdef CONFIG_MCUBOOT_SERIAL
202
203 struct device *detect_port;
Peter Bigot36e90292020-01-30 05:36:47 -0600204 u32_t detect_value = !CONFIG_BOOT_SERIAL_DETECT_PIN_VAL;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200205
206 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
207 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
208
Peter Bigot36e90292020-01-30 05:36:47 -0600209 /* The default presence value is 0 which would normally be
210 * active-low, but historically the raw value was checked so we'll
211 * use the raw interface.
212 */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200213 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Peter Bigot36e90292020-01-30 05:36:47 -0600214#ifdef GPIO_INPUT
215 GPIO_INPUT | GPIO_PULL_UP
216#else
217 GPIO_DIR_IN | GPIO_PUD_PULL_UP
218#endif
Andrzej Puzdrowski5d96bd22020-02-20 10:35:33 +0100219 );
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200220 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200221
Peter Bigot36e90292020-01-30 05:36:47 -0600222#ifdef GPIO_INPUT
223 rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN);
224 detect_value = rc;
225#else
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100226 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200227 &detect_value);
Peter Bigot36e90292020-01-30 05:36:47 -0600228#endif
229 __ASSERT(rc >= 0, "Error of the reading the detect pin.\n");
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100230 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL &&
231 !boot_skip_serial_recovery()) {
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200232 BOOT_LOG_INF("Enter the serial recovery mode");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300233 rc = boot_console_init();
Carles Cufie2a36122018-06-15 10:51:02 +0200234 __ASSERT(rc == 0, "Error initializing boot console.\n");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300235 boot_serial_start(&boot_funcs);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200236 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
237 }
238#endif
239
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530240#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100241 rc = usb_enable(NULL);
242 if (rc) {
243 BOOT_LOG_ERR("Cannot enable USB");
244 } else {
245 BOOT_LOG_INF("Waiting for USB DFU");
246 wait_for_usb_dfu();
247 BOOT_LOG_INF("USB DFU wait time elapsed");
248 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530249#endif
250
David Brown0d0652a2017-04-11 17:33:30 -0600251 rc = boot_go(&rsp);
252 if (rc != 0) {
253 BOOT_LOG_ERR("Unable to find bootable image");
254 while (1)
255 ;
256 }
David Brown5153bd62017-01-06 11:16:53 -0700257
Marti Bolivar88f48d92017-05-01 22:30:02 -0400258 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
259 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200260
David Brown0d0652a2017-04-11 17:33:30 -0600261 BOOT_LOG_INF("Jumping to the first image slot");
262 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700263
David Brown0d0652a2017-04-11 17:33:30 -0600264 BOOT_LOG_ERR("Never should get here");
265 while (1)
266 ;
David Brown5153bd62017-01-06 11:16:53 -0700267}