blob: 0efc5a802823ff141a1c1bddd45530ce325b278b [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>
Rafał Kuźnia505c6e62020-07-17 13:18:15 +020025#include <linker/linker-defs.h>
David Brown5153bd62017-01-06 11:16:53 -070026
Marti Bolivar51181cf2017-03-20 11:03:41 -040027#include "target.h"
28
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050029#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020030#include "bootutil/image.h"
31#include "bootutil/bootutil.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020032#include "flash_map_backend/flash_map_backend.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020033
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020034#ifdef CONFIG_MCUBOOT_SERIAL
Marko Kiiskila149b4572018-06-06 14:18:54 +030035#include "boot_serial/boot_serial.h"
36#include "serial_adapter/serial_adapter.h"
37
38const struct boot_uart_funcs boot_funcs = {
39 .read = console_read,
40 .write = console_write
41};
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020042#endif
43
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +053044#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
45#include <usb/class/usb_dfu.h>
46#endif
47
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010048#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
49#include <arm_cleanup.h>
50#endif
51
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010052#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE)
53#ifdef CONFIG_LOG_PROCESS_THREAD
54#warning "The log internal thread for log processing can't transfer the log"\
55 "well for MCUBoot."
56#else
57#include <logging/log_ctrl.h>
58
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010059#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010060
61/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010062K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010063struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010064volatile bool boot_log_stop = false;
65K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010066
67/* log processing need to be initalized by the application */
68#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010069#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010070#endif /* CONFIG_LOG_PROCESS_THREAD */
71#else
72/* synchronous log mode doesn't need to be initalized by the application */
73#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010074#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010075#endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */
76
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010077#ifdef CONFIG_SOC_FAMILY_NRF
78#include <hal/nrf_power.h>
79
80static inline bool boot_skip_serial_recovery()
81{
Fabio Utzig63b4eac2019-12-17 09:59:47 -030082#if NRF_POWER_HAS_RESETREAS
Kumar Gala0813efe2020-05-27 12:25:41 -050083 uint32_t rr = nrf_power_resetreas_get(NRF_POWER);
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010084
85 return !(rr == 0 || (rr & NRF_POWER_RESETREAS_RESETPIN_MASK));
86#else
87 return false;
88#endif
89}
90#else
91static inline bool boot_skip_serial_recovery()
92{
93 return false;
94}
95#endif
96
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010097MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
98
Andrew Boie7238f512017-03-02 13:39:06 -080099void os_heap_init(void);
100
101#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200102
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200103#ifdef CONFIG_SW_VECTOR_RELAY
104extern void *_vector_table_pointer;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200105#endif
106
Andrew Boie7238f512017-03-02 13:39:06 -0800107struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600108 uint32_t msp;
109 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700110};
111
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100112extern void sys_clock_disable(void);
113
Andrew Boie7238f512017-03-02 13:39:06 -0800114static void do_boot(struct boot_rsp *rsp)
115{
David Brown0d0652a2017-04-11 17:33:30 -0600116 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -0400117 uintptr_t flash_base;
118 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800119
David Brown0d0652a2017-04-11 17:33:30 -0600120 /* The beginning of the image is the ARM vector table, containing
121 * the initial stack pointer address and the reset vector
122 * consecutively. Manually set the stack pointer and jump into the
123 * reset vector
124 */
Marti Bolivareb940802017-05-01 23:15:29 -0400125 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
126 assert(rc == 0);
127
128 vt = (struct arm_vector_table *)(flash_base +
129 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600130 rsp->br_hdr->ih_hdr_size);
Arvid Rosén9ed399c2020-08-19 08:57:11 +0200131
132#ifdef CONFIG_CPU_CORTEX_M7
133 /* Disable instruction cache and data cache before chain-load the application */
134 SCB_DisableDCache();
135 SCB_DisableICache();
136#endif
137
David Brown0d0652a2017-04-11 17:33:30 -0600138 irq_lock();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +0200139#ifdef CONFIG_SYS_CLOCK_EXISTS
David Brown0d0652a2017-04-11 17:33:30 -0600140 sys_clock_disable();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +0200141#endif
Jun Li01bef712019-05-08 21:55:54 -0700142#ifdef CONFIG_USB
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200143 /* Disable the USB to prevent it from firing interrupts */
144 usb_disable();
145#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100146#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
147 cleanup_arm_nvic(); /* cleanup NVIC registers */
148#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200149
150#ifdef CONFIG_BOOT_INTR_VEC_RELOC
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200151#if defined(CONFIG_SW_VECTOR_RELAY)
152 _vector_table_pointer = vt;
153#ifdef CONFIG_CPU_CORTEX_M_HAS_VTOR
154 SCB->VTOR = (uint32_t)__vector_relay_table;
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200155#endif
Rafał Kuźnia505c6e62020-07-17 13:18:15 +0200156#elif defined(CONFIG_CPU_CORTEX_M_HAS_VTOR)
157 SCB->VTOR = (uint32_t)vt;
158#endif /* CONFIG_SW_VECTOR_RELAY */
159#else /* CONFIG_BOOT_INTR_VEC_RELOC */
160#if defined(CONFIG_CPU_CORTEX_M_HAS_VTOR) && defined(CONFIG_SW_VECTOR_RELAY)
161 _vector_table_pointer = _vector_start;
162 SCB->VTOR = (uint32_t)__vector_relay_table;
163#endif
164#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200165
David Brown57837b92018-03-13 15:56:38 -0600166 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100167#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
168 __set_CONTROL(0x00); /* application will configures core on its own */
169#endif
David Brown0d0652a2017-04-11 17:33:30 -0600170 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800171}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530172
173#elif defined(CONFIG_XTENSA)
174#define SRAM_BASE_ADDRESS 0xBE030000
175
176static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
177{
178 const struct flash_area *fap;
179 int area_id;
180 int rc;
181 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
182
183 BOOT_LOG_INF("Copying image to SRAM");
184
185 area_id = flash_area_id_from_image_slot(slot);
186 rc = flash_area_open(area_id, &fap);
187 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300188 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530189 goto done;
190 }
191
192 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
193 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300194 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530195 goto done;
196 }
197
198done:
199 flash_area_close(fap);
200}
201
202/* Entry point (.ResetVector) is at the very beginning of the image.
203 * Simply copy the image to a suitable location and jump there.
204 */
205static void do_boot(struct boot_rsp *rsp)
206{
207 void *start;
208
209 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
210 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
211
212 /* Copy from the flash to HP SRAM */
213 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
214
215 /* Jump to entry point */
216 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
217 ((void (*)(void))start)();
218}
219
Andrew Boie7238f512017-03-02 13:39:06 -0800220#else
221/* Default: Assume entry point is at the very beginning of the image. Simply
222 * lock interrupts and jump there. This is the right thing to do for X86 and
223 * possibly other platforms.
224 */
225static void do_boot(struct boot_rsp *rsp)
226{
Marti Bolivareb940802017-05-01 23:15:29 -0400227 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -0600228 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -0400229 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800230
Marti Bolivareb940802017-05-01 23:15:29 -0400231 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
232 assert(rc == 0);
233
234 start = (void *)(flash_base + rsp->br_image_off +
235 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -0800236
David Brown0d0652a2017-04-11 17:33:30 -0600237 /* Lock interrupts and dive into the entry point */
238 irq_lock();
239 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800240}
241#endif
David Brown5153bd62017-01-06 11:16:53 -0700242
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100243#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
244 !defined(CONFIG_LOG_PROCESS_THREAD)
245/* The log internal thread for log processing can't transfer log well as has too
246 * low priority.
247 * Dedicated thread for log processing below uses highest application
248 * priority. This allows to transmit all logs without adding k_sleep/k_yield
249 * anywhere else int the code.
250 */
251
252/* most simple log processing theread */
253void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
254{
255 (void)dummy1;
256 (void)dummy2;
257 (void)dummy3;
258
259 log_init();
260
261 while (1) {
262 if (log_process(false) == false) {
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100263 if (boot_log_stop) {
264 break;
265 }
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100266 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
267 }
268 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100269
270 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100271}
272
273void zephyr_boot_log_start(void)
274{
275 /* start logging thread */
276 k_thread_create(&boot_log_thread, boot_log_stack,
277 K_THREAD_STACK_SIZEOF(boot_log_stack),
278 boot_log_thread_func, NULL, NULL, NULL,
279 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
280 BOOT_LOG_PROCESSING_INTERVAL);
281
282 k_thread_name_set(&boot_log_thread, "logging");
283}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100284
285void zephyr_boot_log_stop(void)
286{
287 boot_log_stop = true;
288
289 /* wait until log procesing thread expired
290 * This can be reworked using a thread_join() API once a such will be
291 * available in zephyr.
292 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
293 */
294 (void)k_sem_take(&boot_log_sem, K_FOREVER);
295}
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100296#endif/* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
297 !defined(CONFIG_LOG_PROCESS_THREAD) */
298
David Brown5153bd62017-01-06 11:16:53 -0700299void main(void)
300{
David Brown0d0652a2017-04-11 17:33:30 -0600301 struct boot_rsp rsp;
302 int rc;
David Brown5153bd62017-01-06 11:16:53 -0700303
David Brown0d0652a2017-04-11 17:33:30 -0600304 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200305
David Brown0d0652a2017-04-11 17:33:30 -0600306 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700307
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100308 ZEPHYR_BOOT_LOG_START();
309
Kumar Gala32b61f32020-04-08 12:02:03 -0500310#if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL))
311 if (!flash_device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) {
312 BOOT_LOG_ERR("Flash device %s not found",
313 DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
David Brown0d0652a2017-04-11 17:33:30 -0600314 while (1)
315 ;
316 }
Kumar Gala9a5b9512020-04-08 12:06:21 -0500317#elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL))
318 if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) {
319 BOOT_LOG_ERR("Flash device %s not found", JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530320 while (1)
321 ;
322 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530323#endif
David Brown5153bd62017-01-06 11:16:53 -0700324
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200325#ifdef CONFIG_MCUBOOT_SERIAL
326
327 struct device *detect_port;
Kumar Gala0813efe2020-05-27 12:25:41 -0500328 uint32_t detect_value = !CONFIG_BOOT_SERIAL_DETECT_PIN_VAL;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200329
330 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
331 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
332
Peter Bigot36e90292020-01-30 05:36:47 -0600333 /* The default presence value is 0 which would normally be
334 * active-low, but historically the raw value was checked so we'll
335 * use the raw interface.
336 */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200337 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Peter Bigot36e90292020-01-30 05:36:47 -0600338#ifdef GPIO_INPUT
339 GPIO_INPUT | GPIO_PULL_UP
340#else
341 GPIO_DIR_IN | GPIO_PUD_PULL_UP
342#endif
Andrzej Puzdrowski5d96bd22020-02-20 10:35:33 +0100343 );
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200344 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200345
Peter Bigot36e90292020-01-30 05:36:47 -0600346#ifdef GPIO_INPUT
347 rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN);
348 detect_value = rc;
349#else
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100350 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200351 &detect_value);
Peter Bigot36e90292020-01-30 05:36:47 -0600352#endif
353 __ASSERT(rc >= 0, "Error of the reading the detect pin.\n");
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100354 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL &&
355 !boot_skip_serial_recovery()) {
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200356 BOOT_LOG_INF("Enter the serial recovery mode");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300357 rc = boot_console_init();
Carles Cufie2a36122018-06-15 10:51:02 +0200358 __ASSERT(rc == 0, "Error initializing boot console.\n");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300359 boot_serial_start(&boot_funcs);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200360 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
361 }
362#endif
363
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530364#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100365 rc = usb_enable(NULL);
366 if (rc) {
367 BOOT_LOG_ERR("Cannot enable USB");
368 } else {
369 BOOT_LOG_INF("Waiting for USB DFU");
370 wait_for_usb_dfu();
371 BOOT_LOG_INF("USB DFU wait time elapsed");
372 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530373#endif
374
David Brown0d0652a2017-04-11 17:33:30 -0600375 rc = boot_go(&rsp);
376 if (rc != 0) {
377 BOOT_LOG_ERR("Unable to find bootable image");
378 while (1)
379 ;
380 }
David Brown5153bd62017-01-06 11:16:53 -0700381
Marti Bolivar88f48d92017-05-01 22:30:02 -0400382 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
383 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200384
David Brown0d0652a2017-04-11 17:33:30 -0600385 BOOT_LOG_INF("Jumping to the first image slot");
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100386 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600387 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700388
David Brown0d0652a2017-04-11 17:33:30 -0600389 BOOT_LOG_ERR("Never should get here");
390 while (1)
391 ;
David Brown5153bd62017-01-06 11:16:53 -0700392}