blob: 6dda3bd7cddc611a3063294fab13751c325ea783 [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>
David Brown5153bd62017-01-06 11:16:53 -070023
Marti Bolivar51181cf2017-03-20 11:03:41 -040024#include "target.h"
25
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050026#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
27#include "bootutil/bootutil_log.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020028#include "bootutil/image.h"
29#include "bootutil/bootutil.h"
Marti Bolivareb940802017-05-01 23:15:29 -040030#include "flash_map/flash_map.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020031
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +020032#ifdef CONFIG_MCUBOOT_SERIAL
33#include <boot_serial/boot_serial.h>
34#endif
35
David Brown5153bd62017-01-06 11:16:53 -070036struct device *boot_flash_device;
37
Andrew Boie7238f512017-03-02 13:39:06 -080038void os_heap_init(void);
39
Marti Bolivar97d997a2017-09-14 14:36:44 -040040extern void zephyr_flash_area_warn_on_open(void);
41
Andrew Boie7238f512017-03-02 13:39:06 -080042#if defined(CONFIG_ARM)
43struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060044 uint32_t msp;
45 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070046};
47
Andrew Boie7238f512017-03-02 13:39:06 -080048static void do_boot(struct boot_rsp *rsp)
49{
David Brown0d0652a2017-04-11 17:33:30 -060050 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040051 uintptr_t flash_base;
52 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080053
David Brown0d0652a2017-04-11 17:33:30 -060054 /* The beginning of the image is the ARM vector table, containing
55 * the initial stack pointer address and the reset vector
56 * consecutively. Manually set the stack pointer and jump into the
57 * reset vector
58 */
Marti Bolivareb940802017-05-01 23:15:29 -040059 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
60 assert(rc == 0);
61
62 vt = (struct arm_vector_table *)(flash_base +
63 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060064 rsp->br_hdr->ih_hdr_size);
65 irq_lock();
66 sys_clock_disable();
David Brown57837b92018-03-13 15:56:38 -060067 __set_MSP(vt->msp);
David Brown0d0652a2017-04-11 17:33:30 -060068 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080069}
70#else
71/* Default: Assume entry point is at the very beginning of the image. Simply
72 * lock interrupts and jump there. This is the right thing to do for X86 and
73 * possibly other platforms.
74 */
75static void do_boot(struct boot_rsp *rsp)
76{
Marti Bolivareb940802017-05-01 23:15:29 -040077 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -060078 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -040079 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080080
Marti Bolivareb940802017-05-01 23:15:29 -040081 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
82 assert(rc == 0);
83
84 start = (void *)(flash_base + rsp->br_image_off +
85 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080086
David Brown0d0652a2017-04-11 17:33:30 -060087 /* Lock interrupts and dive into the entry point */
88 irq_lock();
89 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -080090}
91#endif
David Brown5153bd62017-01-06 11:16:53 -070092
93void main(void)
94{
David Brown0d0652a2017-04-11 17:33:30 -060095 struct boot_rsp rsp;
96 int rc;
David Brown5153bd62017-01-06 11:16:53 -070097
David Brown0d0652a2017-04-11 17:33:30 -060098 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020099
David Brown0d0652a2017-04-11 17:33:30 -0600100 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -0700101
Marti Bolivar310445b2018-04-10 10:23:47 -0400102 boot_flash_device = device_get_binding(FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600103 if (!boot_flash_device) {
Marti Bolivar310445b2018-04-10 10:23:47 -0400104 BOOT_LOG_ERR("Flash device %s not found", FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600105 while (1)
106 ;
107 }
David Brown5153bd62017-01-06 11:16:53 -0700108
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200109#ifdef CONFIG_MCUBOOT_SERIAL
110
111 struct device *detect_port;
112 u32_t detect_value;
113
114 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
115 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
116
117 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
118 GPIO_DIR_IN | GPIO_PUD_PULL_UP);
119 __ASSERT(rc, "Error of boot detect pin initialization.\n");
120
121 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
122 &detect_value);
123 __ASSERT(rc, "Error of the reading the detect pin.\n");
124
125 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
126 BOOT_LOG_INF("Enter the serial recovery mode");
127 boot_serial_start(CONFIG_BOOT_MAX_LINE_INPUT_LEN + 1);
128 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
129 }
130#endif
131
David Brown0d0652a2017-04-11 17:33:30 -0600132 rc = boot_go(&rsp);
133 if (rc != 0) {
134 BOOT_LOG_ERR("Unable to find bootable image");
135 while (1)
136 ;
137 }
David Brown5153bd62017-01-06 11:16:53 -0700138
Marti Bolivar88f48d92017-05-01 22:30:02 -0400139 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
140 rsp.br_image_off);
Marti Bolivar83a3cef2017-05-05 11:59:49 -0400141 zephyr_flash_area_warn_on_open();
David Brown0d0652a2017-04-11 17:33:30 -0600142 BOOT_LOG_INF("Jumping to the first image slot");
143 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700144
David Brown0d0652a2017-04-11 17:33:30 -0600145 BOOT_LOG_ERR("Never should get here");
146 while (1)
147 ;
David Brown5153bd62017-01-06 11:16:53 -0700148}