blob: f4bc4ab79f19b6fb2792d724caf787c346c6ce28 [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>
Ricardo Salveti8e4d44d2017-02-27 23:00:31 -030022#include <drivers/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
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010043MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
44
Andrew Boie7238f512017-03-02 13:39:06 -080045void os_heap_init(void);
46
47#if defined(CONFIG_ARM)
48struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060049 uint32_t msp;
50 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070051};
52
Emanuele Di Santofcfff582018-11-01 16:06:36 +010053extern void sys_clock_disable(void);
54
Andrew Boie7238f512017-03-02 13:39:06 -080055static void do_boot(struct boot_rsp *rsp)
56{
David Brown0d0652a2017-04-11 17:33:30 -060057 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040058 uintptr_t flash_base;
59 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080060
David Brown0d0652a2017-04-11 17:33:30 -060061 /* The beginning of the image is the ARM vector table, containing
62 * the initial stack pointer address and the reset vector
63 * consecutively. Manually set the stack pointer and jump into the
64 * reset vector
65 */
Marti Bolivareb940802017-05-01 23:15:29 -040066 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
67 assert(rc == 0);
68
69 vt = (struct arm_vector_table *)(flash_base +
70 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060071 rsp->br_hdr->ih_hdr_size);
72 irq_lock();
73 sys_clock_disable();
Emanuele Di Santoc4bf7802018-07-20 11:39:57 +020074#ifdef CONFIG_BOOT_SERIAL_CDC_ACM
75 /* Disable the USB to prevent it from firing interrupts */
76 usb_disable();
77#endif
David Brown57837b92018-03-13 15:56:38 -060078 __set_MSP(vt->msp);
David Brown0d0652a2017-04-11 17:33:30 -060079 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080080}
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +053081
82#elif defined(CONFIG_XTENSA)
83#define SRAM_BASE_ADDRESS 0xBE030000
84
85static void copy_img_to_SRAM(int slot, unsigned int hdr_offset)
86{
87 const struct flash_area *fap;
88 int area_id;
89 int rc;
90 unsigned char *dst = (unsigned char *)(SRAM_BASE_ADDRESS + hdr_offset);
91
92 BOOT_LOG_INF("Copying image to SRAM");
93
94 area_id = flash_area_id_from_image_slot(slot);
95 rc = flash_area_open(area_id, &fap);
96 if (rc != 0) {
97 BOOT_LOG_ERR("flash_area_open failed with %d\n", rc);
98 goto done;
99 }
100
101 rc = flash_area_read(fap, hdr_offset, dst, fap->fa_size - hdr_offset);
102 if (rc != 0) {
103 BOOT_LOG_ERR("flash_area_read failed with %d\n", rc);
104 goto done;
105 }
106
107done:
108 flash_area_close(fap);
109}
110
111/* Entry point (.ResetVector) is at the very beginning of the image.
112 * Simply copy the image to a suitable location and jump there.
113 */
114static void do_boot(struct boot_rsp *rsp)
115{
116 void *start;
117
118 BOOT_LOG_INF("br_image_off = 0x%x\n", rsp->br_image_off);
119 BOOT_LOG_INF("ih_hdr_size = 0x%x\n", rsp->br_hdr->ih_hdr_size);
120
121 /* Copy from the flash to HP SRAM */
122 copy_img_to_SRAM(0, rsp->br_hdr->ih_hdr_size);
123
124 /* Jump to entry point */
125 start = (void *)(SRAM_BASE_ADDRESS + rsp->br_hdr->ih_hdr_size);
126 ((void (*)(void))start)();
127}
128
Andrew Boie7238f512017-03-02 13:39:06 -0800129#else
130/* Default: Assume entry point is at the very beginning of the image. Simply
131 * lock interrupts and jump there. This is the right thing to do for X86 and
132 * possibly other platforms.
133 */
134static void do_boot(struct boot_rsp *rsp)
135{
Marti Bolivareb940802017-05-01 23:15:29 -0400136 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -0600137 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -0400138 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -0800139
Marti Bolivareb940802017-05-01 23:15:29 -0400140 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
141 assert(rc == 0);
142
143 start = (void *)(flash_base + rsp->br_image_off +
144 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -0800145
David Brown0d0652a2017-04-11 17:33:30 -0600146 /* Lock interrupts and dive into the entry point */
147 irq_lock();
148 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -0800149}
150#endif
David Brown5153bd62017-01-06 11:16:53 -0700151
152void main(void)
153{
David Brown0d0652a2017-04-11 17:33:30 -0600154 struct boot_rsp rsp;
155 int rc;
David Brown5153bd62017-01-06 11:16:53 -0700156
David Brown0d0652a2017-04-11 17:33:30 -0600157 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -0200158
David Brown0d0652a2017-04-11 17:33:30 -0600159 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700160
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530161#if (!defined(CONFIG_XTENSA) && defined(DT_FLASH_DEV_NAME))
Andrzej Puzdrowskif50054d2018-11-14 14:33:19 +0100162 if (!flash_device_get_binding(DT_FLASH_DEV_NAME)) {
163 BOOT_LOG_ERR("Flash device %s not found", DT_FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600164 while (1)
165 ;
166 }
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +0530167#elif (defined(CONFIG_XTENSA) && defined(DT_SPI_NOR_DRV_NAME))
168 if (!flash_device_get_binding(DT_SPI_NOR_DRV_NAME)) {
169 BOOT_LOG_ERR("Flash device %s not found", DT_SPI_NOR_DRV_NAME);
170 while (1)
171 ;
172 }
Rajavardhan Gundic3353b22018-12-06 17:24:10 +0530173#endif
David Brown5153bd62017-01-06 11:16:53 -0700174
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200175#ifdef CONFIG_MCUBOOT_SERIAL
176
177 struct device *detect_port;
178 u32_t detect_value;
179
180 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
181 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
182
183 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
184 GPIO_DIR_IN | GPIO_PUD_PULL_UP);
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200185 __ASSERT(rc == 0, "Error of boot detect pin initialization.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200186
Emanuele Di Santofcfff582018-11-01 16:06:36 +0100187 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200188 &detect_value);
Andrzej Puzdrowski82d73952018-06-13 14:55:51 +0200189 __ASSERT(rc == 0, "Error of the reading the detect pin.\n");
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200190
191 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
192 BOOT_LOG_INF("Enter the serial recovery mode");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300193 rc = boot_console_init();
Carles Cufie2a36122018-06-15 10:51:02 +0200194 __ASSERT(rc == 0, "Error initializing boot console.\n");
Marko Kiiskila149b4572018-06-06 14:18:54 +0300195 boot_serial_start(&boot_funcs);
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200196 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
197 }
198#endif
199
David Brown0d0652a2017-04-11 17:33:30 -0600200 rc = boot_go(&rsp);
201 if (rc != 0) {
202 BOOT_LOG_ERR("Unable to find bootable image");
203 while (1)
204 ;
205 }
David Brown5153bd62017-01-06 11:16:53 -0700206
Marti Bolivar88f48d92017-05-01 22:30:02 -0400207 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
208 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200209
David Brown0d0652a2017-04-11 17:33:30 -0600210 BOOT_LOG_INF("Jumping to the first image slot");
211 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700212
David Brown0d0652a2017-04-11 17:33:30 -0600213 BOOT_LOG_ERR("Never should get here");
214 while (1)
215 ;
David Brown5153bd62017-01-06 11:16:53 -0700216}