blob: f561d4f05832dbabd3dd145f434b38acebf88bf4 [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>
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
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010047MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
48
Andrew Boie7238f512017-03-02 13:39:06 -080049void os_heap_init(void);
50
51#if defined(CONFIG_ARM)
52struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060053 uint32_t msp;
54 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070055};
56
Emanuele Di Santofcfff582018-11-01 16:06:36 +010057extern void sys_clock_disable(void);
58
Andrew Boie7238f512017-03-02 13:39:06 -080059static void do_boot(struct boot_rsp *rsp)
60{
David Brown0d0652a2017-04-11 17:33:30 -060061 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040062 uintptr_t flash_base;
63 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080064
David Brown0d0652a2017-04-11 17:33:30 -060065 /* The beginning of the image is the ARM vector table, containing
66 * the initial stack pointer address and the reset vector
67 * consecutively. Manually set the stack pointer and jump into the
68 * reset vector
69 */
Marti Bolivareb940802017-05-01 23:15:29 -040070 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
71 assert(rc == 0);
72
73 vt = (struct arm_vector_table *)(flash_base +
74 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060075 rsp->br_hdr->ih_hdr_size);
76 irq_lock();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +020077#ifdef CONFIG_SYS_CLOCK_EXISTS
David Brown0d0652a2017-04-11 17:33:30 -060078 sys_clock_disable();
Andrzej Puzdrowski960a2d62019-08-12 16:28:26 +020079#endif
Jun Li01bef712019-05-08 21:55:54 -070080#ifdef CONFIG_USB
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +020081 /* Disable the USB to prevent it from firing interrupts */
82 usb_disable();
83#endif
David Brown57837b92018-03-13 15:56:38 -060084 __set_MSP(vt->msp);
David Brown0d0652a2017-04-11 17:33:30 -060085 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080086}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053087
88#elif defined(CONFIG_XTENSA)
89#define SRAM_BASE_ADDRESS 0xBE030000
90
91static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
92{
93 const struct flash_area *fap;
94 int area_id;
95 int rc;
96 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
97
98 BOOT_LOG_INF("Copying image to SRAM");
99
100 area_id = flash_area_id_from_image_slot(slot);
101 rc = flash_area_open(area_id, &fap);
102 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300103 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530104 goto done;
105 }
106
107 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
108 if (rc != 0) {
Fabio Utzigcac58f62019-09-06 08:52:35 -0300109 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530110 goto done;
111 }
112
113done:
114 flash_area_close(fap);
115}
116
117/* Entry point (.ResetVector) is at the very beginning of the image.
118 * Simply copy the image to a suitable location and jump there.
119 */
120static void do_boot(struct boot_rsp *rsp)
121{
122 void *start;
123
124 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
125 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
126
127 /* Copy from the flash to HP SRAM */
128 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
129
130 /* Jump to entry point */
131 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
132 ((void (*)(void))start)();
133}
134
Andrew Boie7238f512017-03-02 13:39:06 -0800135#else
136/* Default: Assume entry point is at the very beginning of the image. Simply
137 * lock interrupts and jump there. This is the right thing to do for X86 and
138 * possibly other platforms.
139 */
140static void do_boot(struct boot_rsp *rsp)
141{
Marti Bolivareb940802017-05-01 23:15:29 -0400142 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -0600143 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -0400144 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800145
Marti Bolivareb940802017-05-01 23:15:29 -0400146 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
147 assert(rc == 0);
148
149 start = (void *)(flash_base + rsp->br_image_off +
150 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -0800151
David Brown0d0652a2017-04-11 17:33:30 -0600152 /* Lock interrupts and dive into the entry point */
153 irq_lock();
154 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800155}
156#endif
David Brown5153bd62017-01-06 11:16:53 -0700157
158void main(void)
159{
David Brown0d0652a2017-04-11 17:33:30 -0600160 struct boot_rsp rsp;
161 int rc;
David Brown5153bd62017-01-06 11:16:53 -0700162
David Brown0d0652a2017-04-11 17:33:30 -0600163 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200164
David Brown0d0652a2017-04-11 17:33:30 -0600165 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700166
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530167#if (!defined(CONFIG_XTENSA) && defined(DT_FLASH_DEV_NAME))
Andrzej Puzdrowskif50054d2018-11-14 14:33:19 +0100168 if (!flash_device_get_binding(DT_FLASH_DEV_NAME)) {
169 BOOT_LOG_ERR("Flash device %s not found", DT_FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600170 while (1)
171 ;
172 }
Rajavardhan Gundi24321c32019-02-08 12:48:34 +0530173#elif (defined(CONFIG_XTENSA) && defined(DT_JEDEC_SPI_NOR_0_LABEL))
174 if (!flash_device_get_binding(DT_JEDEC_SPI_NOR_0_LABEL)) {
175 BOOT_LOG_ERR("Flash device %s not found", DT_JEDEC_SPI_NOR_0_LABEL);
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530176 while (1)
177 ;
178 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530179#endif
David Brown5153bd62017-01-06 11:16:53 -0700180
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200181#ifdef CONFIG_MCUBOOT_SERIAL
182
183 struct device *detect_port;
184 u32_t detect_value;
185
186 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
187 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
188
189 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
190 GPIO_DIR_IN | GPIO_PUD_PULL_UP);
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200191 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200192
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100193 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200194 &detect_value);
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200195 __ASSERT(rc == 0, "Error of the reading the detect pin.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200196
197 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
198 BOOT_LOG_INF("Enter the serial recovery mode");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300199 rc = boot_console_init();
Carles Cufie2a36122018-06-15 10:51:02 +0200200 __ASSERT(rc == 0, "Error initializing boot console.\n");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300201 boot_serial_start(&boot_funcs);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200202 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
203 }
204#endif
205
Rajavardhan Gundi51c9d702019-02-20 14:08:52 +0530206#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
207 BOOT_LOG_INF("Waiting for USB DFU");
208 wait_for_usb_dfu();
209 BOOT_LOG_INF("USB DFU wait time elapsed");
210#endif
211
David Brown0d0652a2017-04-11 17:33:30 -0600212 rc = boot_go(&rsp);
213 if (rc != 0) {
214 BOOT_LOG_ERR("Unable to find bootable image");
215 while (1)
216 ;
217 }
David Brown5153bd62017-01-06 11:16:53 -0700218
Marti Bolivar88f48d92017-05-01 22:30:02 -0400219 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
220 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200221
David Brown0d0652a2017-04-11 17:33:30 -0600222 BOOT_LOG_INF("Jumping to the first image slot");
223 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700224
David Brown0d0652a2017-04-11 17:33:30 -0600225 BOOT_LOG_ERR("Never should get here");
226 while (1)
227 ;
David Brown5153bd62017-01-06 11:16:53 -0700228}