blob: 2b246cee67b964d7804840f5b6d2872260e4a4b1 [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
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +010047#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
48#include <arm_cleanup.h>
49#endif
50
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010051#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE)
52#ifdef CONFIG_LOG_PROCESS_THREAD
53#warning "The log internal thread for log processing can't transfer the log"\
54 "well for MCUBoot."
55#else
56#include <logging/log_ctrl.h>
57
Krzysztof Chruscinski821214e2020-03-24 07:50:32 +010058#define BOOT_LOG_PROCESSING_INTERVAL K_MSEC(30) /* [ms] */
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010059
60/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010061K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010062struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010063volatile bool boot_log_stop = false;
64K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010065
66/* log processing need to be initalized by the application */
67#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010068#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010069#endif /* CONFIG_LOG_PROCESS_THREAD */
70#else
71/* synchronous log mode doesn't need to be initalized by the application */
72#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010073#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010074#endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */
75
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010076#ifdef CONFIG_SOC_FAMILY_NRF
77#include <hal/nrf_power.h>
78
79static inline bool boot_skip_serial_recovery()
80{
Fabio Utzig63b4eac2019-12-17 09:59:47 -030081#if NRF_POWER_HAS_RESETREAS
Kumar Gala0813efe2020-05-27 12:25:41 -050082 uint32_t rr = nrf_power_resetreas_get(NRF_POWER);
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010083
84 return !(rr == 0 || (rr & NRF_POWER_RESETREAS_RESETPIN_MASK));
85#else
86 return false;
87#endif
88}
89#else
90static inline bool boot_skip_serial_recovery()
91{
92 return false;
93}
94#endif
95
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010096MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
97
Andrew Boie7238f512017-03-02 13:39:06 -080098void os_heap_init(void);
99
100#if defined(CONFIG_ARM)
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200101
102#ifdef CONFIG_BOOT_INTR_VEC_RELOC
103
104#ifdef CONFIG_SW_VECTOR_RELAY
105extern void *_vector_table_pointer;
106#define VTOR _vector_table_pointer
107#elif CONFIG_CPU_CORTEX_M_HAS_VTOR
108#define VTOR SCB->VTOR
109#endif
110
111#endif /* CONFIG_BOOT_INTR_VEC_RELOC */
112
Andrew Boie7238f512017-03-02 13:39:06 -0800113struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -0600114 uint32_t msp;
115 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700116};
117
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100118extern void sys_clock_disable(void);
119
Andrew Boie7238f512017-03-02 13:39:06 -0800120static void do_boot(struct boot_rsp *rsp)
121{
David Brown0d0652a2017-04-11 17:33:30 -0600122 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -0400123 uintptr_t flash_base;
124 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800125
David Brown0d0652a2017-04-11 17:33:30 -0600126 /* The beginning of the image is the ARM vector table, containing
127 * the initial stack pointer address and the reset vector
128 * consecutively. Manually set the stack pointer and jump into the
129 * reset vector
130 */
Marti Bolivareb940802017-05-01 23:15:29 -0400131 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
132 assert(rc == 0);
133
134 vt = (struct arm_vector_table *)(flash_base +
135 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600136 rsp->br_hdr->ih_hdr_size);
137 irq_lock();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +0200138#ifdef CONFIG_SYS_CLOCK_EXISTS
David Brown0d0652a2017-04-11 17:33:30 -0600139 sys_clock_disable();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +0200140#endif
Jun Li01bef712019-05-08 21:55:54 -0700141#ifdef CONFIG_USB
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200142 /* Disable the USB to prevent it from firing interrupts */
143 usb_disable();
144#endif
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100145#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
146 cleanup_arm_nvic(); /* cleanup NVIC registers */
147#endif
Rafał Kuźniad854bb62020-06-17 15:06:47 +0200148
149#ifdef CONFIG_BOOT_INTR_VEC_RELOC
150 VTOR = vt;
151#endif
152
David Brown57837b92018-03-13 15:56:38 -0600153 __set_MSP(vt->msp);
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100154#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
155 __set_CONTROL(0x00); /* application will configures core on its own */
156#endif
David Brown0d0652a2017-04-11 17:33:30 -0600157 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800158}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530159
160#elif defined(CONFIG_XTENSA)
161#define SRAM_BASE_ADDRESS 0xBE030000
162
163static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
164{
165 const struct flash_area *fap;
166 int area_id;
167 int rc;
168 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
169
170 BOOT_LOG_INF("Copying image to SRAM");
171
172 area_id = flash_area_id_from_image_slot(slot);
173 rc = flash_area_open(area_id, &fap);
174 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300175 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530176 goto done;
177 }
178
179 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
180 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300181 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530182 goto done;
183 }
184
185done:
186 flash_area_close(fap);
187}
188
189/* Entry point (.ResetVector) is at the very beginning of the image.
190 * Simply copy the image to a suitable location and jump there.
191 */
192static void do_boot(struct boot_rsp *rsp)
193{
194 void *start;
195
196 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
197 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
198
199 /* Copy from the flash to HP SRAM */
200 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
201
202 /* Jump to entry point */
203 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
204 ((void (*)(void))start)();
205}
206
Andrew Boie7238f512017-03-02 13:39:06 -0800207#else
208/* Default: Assume entry point is at the very beginning of the image. Simply
209 * lock interrupts and jump there. This is the right thing to do for X86 and
210 * possibly other platforms.
211 */
212static void do_boot(struct boot_rsp *rsp)
213{
Marti Bolivareb940802017-05-01 23:15:29 -0400214 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -0600215 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -0400216 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800217
Marti Bolivareb940802017-05-01 23:15:29 -0400218 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
219 assert(rc == 0);
220
221 start = (void *)(flash_base + rsp->br_image_off +
222 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -0800223
David Brown0d0652a2017-04-11 17:33:30 -0600224 /* Lock interrupts and dive into the entry point */
225 irq_lock();
226 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800227}
228#endif
David Brown5153bd62017-01-06 11:16:53 -0700229
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100230#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
231 !defined(CONFIG_LOG_PROCESS_THREAD)
232/* The log internal thread for log processing can't transfer log well as has too
233 * low priority.
234 * Dedicated thread for log processing below uses highest application
235 * priority. This allows to transmit all logs without adding k_sleep/k_yield
236 * anywhere else int the code.
237 */
238
239/* most simple log processing theread */
240void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
241{
242 (void)dummy1;
243 (void)dummy2;
244 (void)dummy3;
245
246 log_init();
247
248 while (1) {
249 if (log_process(false) == false) {
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100250 if (boot_log_stop) {
251 break;
252 }
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100253 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
254 }
255 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100256
257 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100258}
259
260void zephyr_boot_log_start(void)
261{
262 /* start logging thread */
263 k_thread_create(&boot_log_thread, boot_log_stack,
264 K_THREAD_STACK_SIZEOF(boot_log_stack),
265 boot_log_thread_func, NULL, NULL, NULL,
266 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
267 BOOT_LOG_PROCESSING_INTERVAL);
268
269 k_thread_name_set(&boot_log_thread, "logging");
270}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100271
272void zephyr_boot_log_stop(void)
273{
274 boot_log_stop = true;
275
276 /* wait until log procesing thread expired
277 * This can be reworked using a thread_join() API once a such will be
278 * available in zephyr.
279 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
280 */
281 (void)k_sem_take(&boot_log_sem, K_FOREVER);
282}
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100283#endif/* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
284 !defined(CONFIG_LOG_PROCESS_THREAD) */
285
David Brown5153bd62017-01-06 11:16:53 -0700286void main(void)
287{
David Brown0d0652a2017-04-11 17:33:30 -0600288 struct boot_rsp rsp;
289 int rc;
David Brown5153bd62017-01-06 11:16:53 -0700290
David Brown0d0652a2017-04-11 17:33:30 -0600291 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200292
David Brown0d0652a2017-04-11 17:33:30 -0600293 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700294
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100295 ZEPHYR_BOOT_LOG_START();
296
Kumar Gala32b61f32020-04-08 12:02:03 -0500297#if (!defined(CONFIG_XTENSA) && defined(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL))
298 if (!flash_device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL)) {
299 BOOT_LOG_ERR("Flash device %s not found",
300 DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
David Brown0d0652a2017-04-11 17:33:30 -0600301 while (1)
302 ;
303 }
Kumar Gala9a5b9512020-04-08 12:06:21 -0500304#elif (defined(CONFIG_XTENSA) && defined(JEDEC_SPI_NOR_0_LABEL))
305 if (!flash_device_get_binding(JEDEC_SPI_NOR_0_LABEL)) {
306 BOOT_LOG_ERR("Flash device %s not found", JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530307 while (1)
308 ;
309 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530310#endif
David Brown5153bd62017-01-06 11:16:53 -0700311
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200312#ifdef CONFIG_MCUBOOT_SERIAL
313
314 struct device *detect_port;
Kumar Gala0813efe2020-05-27 12:25:41 -0500315 uint32_t detect_value = !CONFIG_BOOT_SERIAL_DETECT_PIN_VAL;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200316
317 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
318 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
319
Peter Bigot36e90292020-01-30 05:36:47 -0600320 /* The default presence value is 0 which would normally be
321 * active-low, but historically the raw value was checked so we'll
322 * use the raw interface.
323 */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200324 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Peter Bigot36e90292020-01-30 05:36:47 -0600325#ifdef GPIO_INPUT
326 GPIO_INPUT | GPIO_PULL_UP
327#else
328 GPIO_DIR_IN | GPIO_PUD_PULL_UP
329#endif
Andrzej Puzdrowski5d96bd22020-02-20 10:35:33 +0100330 );
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200331 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200332
Peter Bigot36e90292020-01-30 05:36:47 -0600333#ifdef GPIO_INPUT
334 rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN);
335 detect_value = rc;
336#else
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100337 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200338 &detect_value);
Peter Bigot36e90292020-01-30 05:36:47 -0600339#endif
340 __ASSERT(rc >= 0, "Error of the reading the detect pin.\n");
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100341 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL &&
342 !boot_skip_serial_recovery()) {
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200343 BOOT_LOG_INF("Enter the serial recovery mode");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300344 rc = boot_console_init();
Carles Cufie2a36122018-06-15 10:51:02 +0200345 __ASSERT(rc == 0, "Error initializing boot console.\n");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300346 boot_serial_start(&boot_funcs);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200347 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
348 }
349#endif
350
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530351#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100352 rc = usb_enable(NULL);
353 if (rc) {
354 BOOT_LOG_ERR("Cannot enable USB");
355 } else {
356 BOOT_LOG_INF("Waiting for USB DFU");
357 wait_for_usb_dfu();
358 BOOT_LOG_INF("USB DFU wait time elapsed");
359 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530360#endif
361
David Brown0d0652a2017-04-11 17:33:30 -0600362 rc = boot_go(&rsp);
363 if (rc != 0) {
364 BOOT_LOG_ERR("Unable to find bootable image");
365 while (1)
366 ;
367 }
David Brown5153bd62017-01-06 11:16:53 -0700368
Marti Bolivar88f48d92017-05-01 22:30:02 -0400369 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
370 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200371
David Brown0d0652a2017-04-11 17:33:30 -0600372 BOOT_LOG_INF("Jumping to the first image slot");
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100373 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600374 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700375
David Brown0d0652a2017-04-11 17:33:30 -0600376 BOOT_LOG_ERR("Never should get here");
377 while (1)
378 ;
David Brown5153bd62017-01-06 11:16:53 -0700379}