blob: 517ec6002884ebc00919de8cbae35ce3e8b193bb [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 Puzdrowski3f092bd2020-02-17 13:25:32 +010047#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE)
48#ifdef CONFIG_LOG_PROCESS_THREAD
49#warning "The log internal thread for log processing can't transfer the log"\
50 "well for MCUBoot."
51#else
52#include <logging/log_ctrl.h>
53
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010054#define BOOT_LOG_PROCESSING_INTERVAL 30 /* [ms] */
55
56/* log are processing in custom routine */
Andrzej Puzdrowskiaf148532020-02-25 12:51:26 +010057K_THREAD_STACK_DEFINE(boot_log_stack, CONFIG_MCUBOOT_LOG_THREAD_STACK_SIZE);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010058struct k_thread boot_log_thread;
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010059volatile bool boot_log_stop = false;
60K_SEM_DEFINE(boot_log_sem, 1, 1);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010061
62/* log processing need to be initalized by the application */
63#define ZEPHYR_BOOT_LOG_START() zephyr_boot_log_start()
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010064#define ZEPHYR_BOOT_LOG_STOP() zephyr_boot_log_stop()
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010065#endif /* CONFIG_LOG_PROCESS_THREAD */
66#else
67/* synchronous log mode doesn't need to be initalized by the application */
68#define ZEPHYR_BOOT_LOG_START() do { } while (false)
Andrzej Puzdrowski84591632020-02-24 11:50:19 +010069#define ZEPHYR_BOOT_LOG_STOP() do { } while (false)
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +010070#endif /* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) */
71
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010072#ifdef CONFIG_SOC_FAMILY_NRF
73#include <hal/nrf_power.h>
74
75static inline bool boot_skip_serial_recovery()
76{
Fabio Utzig63b4eac2019-12-17 09:59:47 -030077#if NRF_POWER_HAS_RESETREAS
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +010078 u32_t rr = nrf_power_resetreas_get(NRF_POWER);
79
80 return !(rr == 0 || (rr & NRF_POWER_RESETREAS_RESETPIN_MASK));
81#else
82 return false;
83#endif
84}
85#else
86static inline bool boot_skip_serial_recovery()
87{
88 return false;
89}
90#endif
91
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010092MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
93
Andrew Boie7238f512017-03-02 13:39:06 -080094void os_heap_init(void);
95
96#if defined(CONFIG_ARM)
97struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060098 uint32_t msp;
99 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -0700100};
101
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100102extern void sys_clock_disable(void);
103
Andrew Boie7238f512017-03-02 13:39:06 -0800104static void do_boot(struct boot_rsp *rsp)
105{
David Brown0d0652a2017-04-11 17:33:30 -0600106 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -0400107 uintptr_t flash_base;
108 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800109
David Brown0d0652a2017-04-11 17:33:30 -0600110 /* The beginning of the image is the ARM vector table, containing
111 * the initial stack pointer address and the reset vector
112 * consecutively. Manually set the stack pointer and jump into the
113 * reset vector
114 */
Marti Bolivareb940802017-05-01 23:15:29 -0400115 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
116 assert(rc == 0);
117
118 vt = (struct arm_vector_table *)(flash_base +
119 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -0600120 rsp->br_hdr->ih_hdr_size);
121 irq_lock();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +0200122#ifdef CONFIG_SYS_CLOCK_EXISTS
David Brown0d0652a2017-04-11 17:33:30 -0600123 sys_clock_disable();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +0200124#endif
Jun Li01bef712019-05-08 21:55:54 -0700125#ifdef CONFIG_USB
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +0200126 /* Disable the USB to prevent it from firing interrupts */
127 usb_disable();
128#endif
David Brown57837b92018-03-13 15:56:38 -0600129 __set_MSP(vt->msp);
David Brown0d0652a2017-04-11 17:33:30 -0600130 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -0800131}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530132
133#elif defined(CONFIG_XTENSA)
134#define SRAM_BASE_ADDRESS 0xBE030000
135
136static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
137{
138 const struct flash_area *fap;
139 int area_id;
140 int rc;
141 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
142
143 BOOT_LOG_INF("Copying image to SRAM");
144
145 area_id = flash_area_id_from_image_slot(slot);
146 rc = flash_area_open(area_id, &fap);
147 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300148 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530149 goto done;
150 }
151
152 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
153 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300154 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530155 goto done;
156 }
157
158done:
159 flash_area_close(fap);
160}
161
162/* Entry point (.ResetVector) is at the very beginning of the image.
163 * Simply copy the image to a suitable location and jump there.
164 */
165static void do_boot(struct boot_rsp *rsp)
166{
167 void *start;
168
169 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
170 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
171
172 /* Copy from the flash to HP SRAM */
173 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
174
175 /* Jump to entry point */
176 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
177 ((void (*)(void))start)();
178}
179
Andrew Boie7238f512017-03-02 13:39:06 -0800180#else
181/* Default: Assume entry point is at the very beginning of the image. Simply
182 * lock interrupts and jump there. This is the right thing to do for X86 and
183 * possibly other platforms.
184 */
185static void do_boot(struct boot_rsp *rsp)
186{
Marti Bolivareb940802017-05-01 23:15:29 -0400187 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -0600188 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -0400189 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800190
Marti Bolivareb940802017-05-01 23:15:29 -0400191 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
192 assert(rc == 0);
193
194 start = (void *)(flash_base + rsp->br_image_off +
195 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -0800196
David Brown0d0652a2017-04-11 17:33:30 -0600197 /* Lock interrupts and dive into the entry point */
198 irq_lock();
199 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800200}
201#endif
David Brown5153bd62017-01-06 11:16:53 -0700202
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100203#if defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
204 !defined(CONFIG_LOG_PROCESS_THREAD)
205/* The log internal thread for log processing can't transfer log well as has too
206 * low priority.
207 * Dedicated thread for log processing below uses highest application
208 * priority. This allows to transmit all logs without adding k_sleep/k_yield
209 * anywhere else int the code.
210 */
211
212/* most simple log processing theread */
213void boot_log_thread_func(void *dummy1, void *dummy2, void *dummy3)
214{
215 (void)dummy1;
216 (void)dummy2;
217 (void)dummy3;
218
219 log_init();
220
221 while (1) {
222 if (log_process(false) == false) {
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100223 if (boot_log_stop) {
224 break;
225 }
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100226 k_sleep(BOOT_LOG_PROCESSING_INTERVAL);
227 }
228 }
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100229
230 k_sem_give(&boot_log_sem);
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100231}
232
233void zephyr_boot_log_start(void)
234{
235 /* start logging thread */
236 k_thread_create(&boot_log_thread, boot_log_stack,
237 K_THREAD_STACK_SIZEOF(boot_log_stack),
238 boot_log_thread_func, NULL, NULL, NULL,
239 K_HIGHEST_APPLICATION_THREAD_PRIO, 0,
240 BOOT_LOG_PROCESSING_INTERVAL);
241
242 k_thread_name_set(&boot_log_thread, "logging");
243}
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100244
245void zephyr_boot_log_stop(void)
246{
247 boot_log_stop = true;
248
249 /* wait until log procesing thread expired
250 * This can be reworked using a thread_join() API once a such will be
251 * available in zephyr.
252 * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
253 */
254 (void)k_sem_take(&boot_log_sem, K_FOREVER);
255}
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100256#endif/* defined(CONFIG_LOG) && !defined(CONFIG_LOG_IMMEDIATE) &&\
257 !defined(CONFIG_LOG_PROCESS_THREAD) */
258
David Brown5153bd62017-01-06 11:16:53 -0700259void main(void)
260{
David Brown0d0652a2017-04-11 17:33:30 -0600261 struct boot_rsp rsp;
262 int rc;
David Brown5153bd62017-01-06 11:16:53 -0700263
David Brown0d0652a2017-04-11 17:33:30 -0600264 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200265
David Brown0d0652a2017-04-11 17:33:30 -0600266 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700267
Andrzej Puzdrowski3f092bd2020-02-17 13:25:32 +0100268 ZEPHYR_BOOT_LOG_START();
269
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530270#if (!defined(CONFIG_XTENSA) && defined(DT_FLASH_DEV_NAME))
Andrzej Puzdrowskif50054d2018-11-14 14:33:19 +0100271 if (!flash_device_get_binding(DT_FLASH_DEV_NAME)) {
272 BOOT_LOG_ERR("Flash device %s not found", DT_FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600273 while (1)
274 ;
275 }
Rajavardhan Gundi24321c32019-02-08 12:48:34 +0530276#elif (defined(CONFIG_XTENSA) && defined(DT_JEDEC_SPI_NOR_0_LABEL))
277 if (!flash_device_get_binding(DT_JEDEC_SPI_NOR_0_LABEL)) {
278 BOOT_LOG_ERR("Flash device %s not found", DT_JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530279 while (1)
280 ;
281 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530282#endif
David Brown5153bd62017-01-06 11:16:53 -0700283
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200284#ifdef CONFIG_MCUBOOT_SERIAL
285
286 struct device *detect_port;
Peter Bigot36e90292020-01-30 05:36:47 -0600287 u32_t detect_value = !CONFIG_BOOT_SERIAL_DETECT_PIN_VAL;
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200288
289 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
290 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
291
Peter Bigot36e90292020-01-30 05:36:47 -0600292 /* The default presence value is 0 which would normally be
293 * active-low, but historically the raw value was checked so we'll
294 * use the raw interface.
295 */
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200296 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Peter Bigot36e90292020-01-30 05:36:47 -0600297#ifdef GPIO_INPUT
298 GPIO_INPUT | GPIO_PULL_UP
299#else
300 GPIO_DIR_IN | GPIO_PUD_PULL_UP
301#endif
Andrzej Puzdrowski5d96bd22020-02-20 10:35:33 +0100302 );
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200303 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200304
Peter Bigot36e90292020-01-30 05:36:47 -0600305#ifdef GPIO_INPUT
306 rc = gpio_pin_get_raw(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN);
307 detect_value = rc;
308#else
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100309 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200310 &detect_value);
Peter Bigot36e90292020-01-30 05:36:47 -0600311#endif
312 __ASSERT(rc >= 0, "Error of the reading the detect pin.\n");
Jon Helge Nistade58f9bd2019-11-25 15:59:52 +0100313 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL &&
314 !boot_skip_serial_recovery()) {
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200315 BOOT_LOG_INF("Enter the serial recovery mode");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300316 rc = boot_console_init();
Carles Cufie2a36122018-06-15 10:51:02 +0200317 __ASSERT(rc == 0, "Error initializing boot console.\n");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300318 boot_serial_start(&boot_funcs);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200319 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
320 }
321#endif
322
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530323#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
Andrzej Puzdrowskiac1f1ff2020-02-05 08:40:12 +0100324 rc = usb_enable(NULL);
325 if (rc) {
326 BOOT_LOG_ERR("Cannot enable USB");
327 } else {
328 BOOT_LOG_INF("Waiting for USB DFU");
329 wait_for_usb_dfu();
330 BOOT_LOG_INF("USB DFU wait time elapsed");
331 }
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530332#endif
333
David Brown0d0652a2017-04-11 17:33:30 -0600334 rc = boot_go(&rsp);
335 if (rc != 0) {
336 BOOT_LOG_ERR("Unable to find bootable image");
337 while (1)
338 ;
339 }
David Brown5153bd62017-01-06 11:16:53 -0700340
Marti Bolivar88f48d92017-05-01 22:30:02 -0400341 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
342 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200343
David Brown0d0652a2017-04-11 17:33:30 -0600344 BOOT_LOG_INF("Jumping to the first image slot");
Andrzej Puzdrowski84591632020-02-24 11:50:19 +0100345 ZEPHYR_BOOT_LOG_STOP();
David Brown0d0652a2017-04-11 17:33:30 -0600346 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700347
David Brown0d0652a2017-04-11 17:33:30 -0600348 BOOT_LOG_ERR("Never should get here");
349 while (1)
350 ;
David Brown5153bd62017-01-06 11:16:53 -0700351}