blob: 3d1f9d6d54fd963c0062a4c38576bf565402d29f [file] [log] [blame]
Christopher Collinscf18a032017-02-06 15:10:45 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Fabio Utzig1a2e41a2017-11-17 12:13:09 -020020#include "mcuboot_config/mcuboot_config.h"
Fabio Utzig1a2e41a2017-11-17 12:13:09 -020021
Christopher Collinscf18a032017-02-06 15:10:45 -080022#include <assert.h>
23#include <stddef.h>
24#include <inttypes.h>
Fabio Utzig1d46c942018-02-26 10:38:00 -030025#include <stdio.h>
Marko Kiiskilace50ab02018-06-06 11:33:33 +030026
27#include <syscfg/syscfg.h>
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020028#include <flash_map_backend/flash_map_backend.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080029#include <os/os.h>
30#include <bsp/bsp.h>
31#include <hal/hal_bsp.h>
32#include <hal/hal_system.h>
33#include <hal/hal_flash.h>
Marko Kiiskila149b4572018-06-06 14:18:54 +030034#include <hal/hal_watchdog.h>
Fabio Utzig1d46c942018-02-26 10:38:00 -030035#include <sysinit/sysinit.h>
Fabio Utzig19356bf2017-05-11 16:19:36 -030036#ifdef MCUBOOT_SERIAL
Christopher Collinscf18a032017-02-06 15:10:45 -080037#include <hal/hal_gpio.h>
Marko Kiiskila316d3612018-06-05 12:03:27 +030038#include <hal/hal_nvreg.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080039#include <boot_serial/boot_serial.h>
Christopher Collinscf18a032017-02-06 15:10:45 -080040#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +030041#if defined(MCUBOOT_SERIAL)
Marko Kiiskilace50ab02018-06-06 11:33:33 +030042#include <boot_uart/boot_uart.h>
43#endif
Christopher Collinscf18a032017-02-06 15:10:45 -080044#include <console/console.h>
45#include "bootutil/image.h"
46#include "bootutil/bootutil.h"
Fabio Utzig94912c52018-05-07 08:38:23 -030047#include "bootutil/bootutil_log.h"
Tamas Ban76177e12020-09-30 08:01:05 +010048#include "bootutil/fault_injection_hardening.h"
49#include "bootutil/fault_injection_hardening_delay_rng.h"
Christopher Collinscf18a032017-02-06 15:10:45 -080050
Fabio Utzige47ccd62019-10-18 10:55:22 -030051#if MYNEWT_VAL(BOOT_CUSTOM_START)
52void boot_custom_start(uintptr_t flash_base, struct boot_rsp *rsp);
53#endif
54
Marko Kiiskila149b4572018-06-06 14:18:54 +030055#if defined(MCUBOOT_SERIAL)
56#define BOOT_SERIAL_REPORT_DUR \
57 (MYNEWT_VAL(OS_CPUTIME_FREQ) / MYNEWT_VAL(BOOT_SERIAL_REPORT_FREQ))
Marko Kiiskila316d3612018-06-05 12:03:27 +030058#define BOOT_SERIAL_INPUT_MAX (512)
Christopher Collinscf18a032017-02-06 15:10:45 -080059
Marko Kiiskila149b4572018-06-06 14:18:54 +030060static int boot_read(char *str, int cnt, int *newline);
61static const struct boot_uart_funcs boot_uart_funcs = {
62 .read = boot_read,
63 .write = boot_uart_write
64};
65
66static int
67boot_read(char *str, int cnt, int *newline)
Marti Bolivareb940802017-05-01 23:15:29 -040068{
Marko Kiiskila149b4572018-06-06 14:18:54 +030069#if MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN) != -1
70 static uint32_t tick = 0;
71
72 if (tick == 0) {
73 /*
74 * Configure GPIO line as output. This is a pin we toggle at the
75 * given frequency.
76 */
77 hal_gpio_init_out(MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN), 0);
78 tick = os_cputime_get32();
79 } else {
80 if (os_cputime_get32() - tick > BOOT_SERIAL_REPORT_DUR) {
81 hal_gpio_toggle(MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN));
82 tick = os_cputime_get32();
83 }
84 }
85#endif
86 hal_watchdog_tickle();
87
88 return boot_uart_read(str, cnt, newline);
Marti Bolivareb940802017-05-01 23:15:29 -040089}
90
Marko Kiiskilace50ab02018-06-06 11:33:33 +030091#if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0
92
93/** Don't include null-terminator in comparison. */
94#define BOOT_SERIAL_DETECT_STRING_LEN \
95 (sizeof MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING) - 1)
96
97/**
98 * Listens on the UART for the management string. Blocks for up to
99 * BOOT_SERIAL_DETECT_TIMEOUT milliseconds.
100 *
101 * @return true if the management string was received;
102 * false if the management string was not received
103 * before the UART listen timeout expired.
104 */
105static bool
106serial_detect_uart_string(void)
107{
108 uint32_t start_tick;
109 char buf[BOOT_SERIAL_DETECT_STRING_LEN] = { 0 };
110 char ch;
111 int newline;
112 int rc;
113
114 /* Calculate the timeout duration in OS cputime ticks. */
115 static const uint32_t timeout_dur =
116 MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) /
117 (1000.0 / MYNEWT_VAL(OS_CPUTIME_FREQ));
118
Marko Kiiskila149b4572018-06-06 14:18:54 +0300119 rc = boot_uart_open();
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300120 assert(rc == 0);
121
122 start_tick = os_cputime_get32();
123
124 while (1) {
125 /* Read a single character from the UART. */
Marko Kiiskila149b4572018-06-06 14:18:54 +0300126 rc = boot_uart_read(&ch, 1, &newline);
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300127 if (rc > 0) {
128 /* Eliminate the oldest character in the buffer to make room for
129 * the new one.
130 */
131 memmove(buf, buf + 1, BOOT_SERIAL_DETECT_STRING_LEN - 1);
132 buf[BOOT_SERIAL_DETECT_STRING_LEN - 1] = ch;
133
134 /* If the full management string has been received, indicate that
135 * the serial boot loader should start.
136 */
137 rc = memcmp(buf,
138 MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING),
139 BOOT_SERIAL_DETECT_STRING_LEN);
140 if (rc == 0) {
Marko Kiiskila149b4572018-06-06 14:18:54 +0300141 boot_uart_close();
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300142 return true;
143 }
144 }
145
146 /* Abort the listen on timeout. */
147 if (os_cputime_get32() >= start_tick + timeout_dur) {
Marko Kiiskila149b4572018-06-06 14:18:54 +0300148 boot_uart_close();
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300149 return false;
150 }
151 }
152}
153#endif
154
Marko Kiiskila316d3612018-06-05 12:03:27 +0300155static void
156serial_boot_detect(void)
157{
158 /*
159 * Read retained register and compare with expected magic value.
160 * If it matches, await for download commands from serial.
161 */
162#if MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX) != -1
163 if (hal_nvreg_read(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX)) ==
164 MYNEWT_VAL(BOOT_SERIAL_NVREG_MAGIC)) {
165
166 hal_nvreg_write(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX), 0);
Marko Kiiskila149b4572018-06-06 14:18:54 +0300167 goto serial_boot;
Marko Kiiskila316d3612018-06-05 12:03:27 +0300168 }
Marko Kiiskila316d3612018-06-05 12:03:27 +0300169#endif
170
171 /*
172 * Configure a GPIO as input, and compare it against expected value.
173 * If it matches, await for download commands from serial.
174 */
175#if MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN) != -1
176 hal_gpio_init_in(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN),
177 MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_CFG));
178 if (hal_gpio_read(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN)) ==
179 MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_VAL)) {
Marko Kiiskila149b4572018-06-06 14:18:54 +0300180 goto serial_boot;
Marko Kiiskila316d3612018-06-05 12:03:27 +0300181 }
182#endif
183
184 /*
185 * Listen for management pattern in UART input. If detected, await for
186 * download commands from serial.
187 */
188#if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0
Marko Kiiskilace50ab02018-06-06 11:33:33 +0300189 if (serial_detect_uart_string()) {
Marko Kiiskila149b4572018-06-06 14:18:54 +0300190 goto serial_boot;
Marko Kiiskila316d3612018-06-05 12:03:27 +0300191 }
192#endif
Marko Kiiskila149b4572018-06-06 14:18:54 +0300193 return;
194serial_boot:
195 boot_uart_open();
196 boot_serial_start(&boot_uart_funcs);
197 assert(0);
Marko Kiiskila316d3612018-06-05 12:03:27 +0300198}
199#endif
200
Marko Kiiskila149b4572018-06-06 14:18:54 +0300201/*
202 * Temporary flash_device_base() implementation.
203 *
204 * TODO: remove this when mynewt needs to support flash_device_base()
205 * for devices with nonzero base addresses.
206 */
207int flash_device_base(uint8_t fd_id, uintptr_t *ret)
208{
209 *ret = 0;
210 return 0;
211}
212
Christopher Collinscf18a032017-02-06 15:10:45 -0800213int
214main(void)
215{
216 struct boot_rsp rsp;
Marti Bolivareb940802017-05-01 23:15:29 -0400217 uintptr_t flash_base;
Christopher Collinscf18a032017-02-06 15:10:45 -0800218 int rc;
Tamas Ban76177e12020-09-30 08:01:05 +0100219 fih_int fih_rc = FIH_FAILURE;
Christopher Collinscf18a032017-02-06 15:10:45 -0800220
Andrzej Kaczmarekc49099c2018-02-16 17:10:51 +0100221 hal_bsp_init();
Fabio Utzig1d46c942018-02-26 10:38:00 -0300222
Fabio Utzig0f29c482018-07-26 14:53:36 -0300223#if !MYNEWT_VAL(OS_SCHEDULING) && MYNEWT_VAL(WATCHDOG_INTERVAL)
224 rc = hal_watchdog_init(MYNEWT_VAL(WATCHDOG_INTERVAL));
225 assert(rc == 0);
226#endif
227
Fabio Utzigaf1e02e2019-06-14 08:56:41 -0300228#if defined(MCUBOOT_SERIAL) || defined(MCUBOOT_HAVE_LOGGING) || \
229 MYNEWT_VAL(CRYPTO) || MYNEWT_VAL(HASH)
Fabio Utzigad0e9b82019-02-18 16:13:01 -0300230 /* initialize uart/crypto without os */
Fabio Utzig9f7c3d22018-02-16 14:55:28 -0200231 os_dev_initialize_all(OS_DEV_INIT_PRIMARY);
Marko Kiiskila316d3612018-06-05 12:03:27 +0300232 os_dev_initialize_all(OS_DEV_INIT_SECONDARY);
Christopher Collinscf18a032017-02-06 15:10:45 -0800233 sysinit();
Fabio Utzig1d46c942018-02-26 10:38:00 -0300234 console_blocking_mode();
Marko Kiiskila316d3612018-06-05 12:03:27 +0300235#if defined(MCUBOOT_SERIAL)
236 serial_boot_detect();
Fabio Utzig3b69d6f2018-06-25 13:59:36 -0300237 hal_timer_deinit(MYNEWT_VAL(OS_CPUTIME_TIMER_NUM));
Marko Kiiskila316d3612018-06-05 12:03:27 +0300238#endif
Fabio Utzig94912c52018-05-07 08:38:23 -0300239#else
240 flash_map_init();
241#endif
Christopher Collinscf18a032017-02-06 15:10:45 -0800242
Tamas Ban76177e12020-09-30 08:01:05 +0100243 FIH_CALL(boot_go, fih_rc, &rsp);
244 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
245 assert(fih_int_decode(fih_rc) == FIH_POSITIVE_VALUE);
246 FIH_PANIC;
247 }
Christopher Collinscf18a032017-02-06 15:10:45 -0800248
Fabio Utzigb00d6482017-06-20 19:28:22 -0300249 rc = flash_device_base(rsp.br_flash_dev_id, &flash_base);
Marti Bolivareb940802017-05-01 23:15:29 -0400250 assert(rc == 0);
251
Fabio Utzige47ccd62019-10-18 10:55:22 -0300252#if MYNEWT_VAL(BOOT_CUSTOM_START)
253 boot_custom_start(flash_base, &rsp);
254#else
Marti Bolivareb940802017-05-01 23:15:29 -0400255 hal_system_start((void *)(flash_base + rsp.br_image_off +
256 rsp.br_hdr->ih_hdr_size));
Fabio Utzige47ccd62019-10-18 10:55:22 -0300257#endif
Christopher Collinscf18a032017-02-06 15:10:45 -0800258
259 return 0;
260}