blob: 94963b1925cfbc7465a1cdb2443153a4a492fb9f [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"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020030#include "flash_map_backend/flash_map_backend.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
40#if defined(CONFIG_ARM)
41struct arm_vector_table {
David Brown0d0652a2017-04-11 17:33:30 -060042 uint32_t msp;
43 uint32_t reset;
David Brown5153bd62017-01-06 11:16:53 -070044};
45
Andrew Boie7238f512017-03-02 13:39:06 -080046static void do_boot(struct boot_rsp *rsp)
47{
David Brown0d0652a2017-04-11 17:33:30 -060048 struct arm_vector_table *vt;
Marti Bolivareb940802017-05-01 23:15:29 -040049 uintptr_t flash_base;
50 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080051
David Brown0d0652a2017-04-11 17:33:30 -060052 /* The beginning of the image is the ARM vector table, containing
53 * the initial stack pointer address and the reset vector
54 * consecutively. Manually set the stack pointer and jump into the
55 * reset vector
56 */
Marti Bolivareb940802017-05-01 23:15:29 -040057 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
58 assert(rc == 0);
59
60 vt = (struct arm_vector_table *)(flash_base +
61 rsp->br_image_off +
David Brown0d0652a2017-04-11 17:33:30 -060062 rsp->br_hdr->ih_hdr_size);
63 irq_lock();
64 sys_clock_disable();
David Brown57837b92018-03-13 15:56:38 -060065 __set_MSP(vt->msp);
David Brown0d0652a2017-04-11 17:33:30 -060066 ((void (*)(void))vt->reset)();
Andrew Boie7238f512017-03-02 13:39:06 -080067}
68#else
69/* Default: Assume entry point is at the very beginning of the image. Simply
70 * lock interrupts and jump there. This is the right thing to do for X86 and
71 * possibly other platforms.
72 */
73static void do_boot(struct boot_rsp *rsp)
74{
Marti Bolivareb940802017-05-01 23:15:29 -040075 uintptr_t flash_base;
David Brown0d0652a2017-04-11 17:33:30 -060076 void *start;
Marti Bolivareb940802017-05-01 23:15:29 -040077 int rc;
Andrew Boie7238f512017-03-02 13:39:06 -080078
Marti Bolivareb940802017-05-01 23:15:29 -040079 rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
80 assert(rc == 0);
81
82 start = (void *)(flash_base + rsp->br_image_off +
83 rsp->br_hdr->ih_hdr_size);
Andrew Boie7238f512017-03-02 13:39:06 -080084
David Brown0d0652a2017-04-11 17:33:30 -060085 /* Lock interrupts and dive into the entry point */
86 irq_lock();
87 ((void (*)(void))start)();
Andrew Boie7238f512017-03-02 13:39:06 -080088}
89#endif
David Brown5153bd62017-01-06 11:16:53 -070090
91void main(void)
92{
David Brown0d0652a2017-04-11 17:33:30 -060093 struct boot_rsp rsp;
94 int rc;
David Brown5153bd62017-01-06 11:16:53 -070095
David Brown0d0652a2017-04-11 17:33:30 -060096 BOOT_LOG_INF("Starting bootloader");
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020097
David Brown0d0652a2017-04-11 17:33:30 -060098 os_heap_init();
David Brown5153bd62017-01-06 11:16:53 -070099
Marti Bolivar310445b2018-04-10 10:23:47 -0400100 boot_flash_device = device_get_binding(FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600101 if (!boot_flash_device) {
Marti Bolivar310445b2018-04-10 10:23:47 -0400102 BOOT_LOG_ERR("Flash device %s not found", FLASH_DEV_NAME);
David Brown0d0652a2017-04-11 17:33:30 -0600103 while (1)
104 ;
105 }
David Brown5153bd62017-01-06 11:16:53 -0700106
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200107#ifdef CONFIG_MCUBOOT_SERIAL
108
109 struct device *detect_port;
110 u32_t detect_value;
111
112 detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
113 __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
114
115 rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
116 GPIO_DIR_IN | GPIO_PUD_PULL_UP);
117 __ASSERT(rc, "Error of boot detect pin initialization.\n");
118
119 rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
120 &detect_value);
121 __ASSERT(rc, "Error of the reading the detect pin.\n");
122
123 if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
124 BOOT_LOG_INF("Enter the serial recovery mode");
125 boot_serial_start(CONFIG_BOOT_MAX_LINE_INPUT_LEN + 1);
126 __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
127 }
128#endif
129
David Brown0d0652a2017-04-11 17:33:30 -0600130 rc = boot_go(&rsp);
131 if (rc != 0) {
132 BOOT_LOG_ERR("Unable to find bootable image");
133 while (1)
134 ;
135 }
David Brown5153bd62017-01-06 11:16:53 -0700136
Marti Bolivar88f48d92017-05-01 22:30:02 -0400137 BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
138 rsp.br_image_off);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200139
David Brown0d0652a2017-04-11 17:33:30 -0600140 BOOT_LOG_INF("Jumping to the first image slot");
141 do_boot(&rsp);
David Brown5153bd62017-01-06 11:16:53 -0700142
David Brown0d0652a2017-04-11 17:33:30 -0600143 BOOT_LOG_ERR("Never should get here");
144 while (1)
145 ;
David Brown5153bd62017-01-06 11:16:53 -0700146}