Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 1 | /* |
| 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 Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame] | 20 | #include "mcuboot_config/mcuboot_config.h" |
Fabio Utzig | 1a2e41a | 2017-11-17 12:13:09 -0200 | [diff] [blame] | 21 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 22 | #include <assert.h> |
| 23 | #include <stddef.h> |
| 24 | #include <inttypes.h> |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 25 | #include <stdio.h> |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 26 | |
| 27 | #include <syscfg/syscfg.h> |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 28 | #include <flash_map_backend/flash_map_backend.h> |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 29 | #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 Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 34 | #include <hal/hal_watchdog.h> |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 35 | #include <sysinit/sysinit.h> |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 36 | #ifdef MCUBOOT_SERIAL |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 37 | #include <hal/hal_gpio.h> |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 38 | #include <hal/hal_nvreg.h> |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 39 | #include <boot_serial/boot_serial.h> |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 40 | #endif |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 41 | #if defined(MCUBOOT_SERIAL) |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 42 | #include <boot_uart/boot_uart.h> |
| 43 | #endif |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 44 | #include <console/console.h> |
| 45 | #include "bootutil/image.h" |
| 46 | #include "bootutil/bootutil.h" |
Fabio Utzig | 94912c5 | 2018-05-07 08:38:23 -0300 | [diff] [blame] | 47 | #include "bootutil/bootutil_log.h" |
Tamas Ban | 76177e1 | 2020-09-30 08:01:05 +0100 | [diff] [blame] | 48 | #include "bootutil/fault_injection_hardening.h" |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 49 | |
Fabio Utzig | e47ccd6 | 2019-10-18 10:55:22 -0300 | [diff] [blame] | 50 | #if MYNEWT_VAL(BOOT_CUSTOM_START) |
| 51 | void boot_custom_start(uintptr_t flash_base, struct boot_rsp *rsp); |
| 52 | #endif |
| 53 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 54 | #if defined(MCUBOOT_SERIAL) |
| 55 | #define BOOT_SERIAL_REPORT_DUR \ |
| 56 | (MYNEWT_VAL(OS_CPUTIME_FREQ) / MYNEWT_VAL(BOOT_SERIAL_REPORT_FREQ)) |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 57 | #define BOOT_SERIAL_INPUT_MAX (512) |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 58 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 59 | static int boot_read(char *str, int cnt, int *newline); |
| 60 | static const struct boot_uart_funcs boot_uart_funcs = { |
| 61 | .read = boot_read, |
| 62 | .write = boot_uart_write |
| 63 | }; |
| 64 | |
| 65 | static int |
| 66 | boot_read(char *str, int cnt, int *newline) |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 67 | { |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 68 | #if MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN) != -1 |
| 69 | static uint32_t tick = 0; |
| 70 | |
| 71 | if (tick == 0) { |
| 72 | /* |
| 73 | * Configure GPIO line as output. This is a pin we toggle at the |
| 74 | * given frequency. |
| 75 | */ |
| 76 | hal_gpio_init_out(MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN), 0); |
| 77 | tick = os_cputime_get32(); |
| 78 | } else { |
| 79 | if (os_cputime_get32() - tick > BOOT_SERIAL_REPORT_DUR) { |
| 80 | hal_gpio_toggle(MYNEWT_VAL(BOOT_SERIAL_REPORT_PIN)); |
| 81 | tick = os_cputime_get32(); |
| 82 | } |
| 83 | } |
| 84 | #endif |
| 85 | hal_watchdog_tickle(); |
| 86 | |
| 87 | return boot_uart_read(str, cnt, newline); |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 90 | #if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0 |
| 91 | |
| 92 | /** Don't include null-terminator in comparison. */ |
| 93 | #define BOOT_SERIAL_DETECT_STRING_LEN \ |
| 94 | (sizeof MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING) - 1) |
| 95 | |
| 96 | /** |
| 97 | * Listens on the UART for the management string. Blocks for up to |
| 98 | * BOOT_SERIAL_DETECT_TIMEOUT milliseconds. |
| 99 | * |
| 100 | * @return true if the management string was received; |
| 101 | * false if the management string was not received |
| 102 | * before the UART listen timeout expired. |
| 103 | */ |
| 104 | static bool |
| 105 | serial_detect_uart_string(void) |
| 106 | { |
| 107 | uint32_t start_tick; |
| 108 | char buf[BOOT_SERIAL_DETECT_STRING_LEN] = { 0 }; |
| 109 | char ch; |
| 110 | int newline; |
| 111 | int rc; |
| 112 | |
| 113 | /* Calculate the timeout duration in OS cputime ticks. */ |
| 114 | static const uint32_t timeout_dur = |
| 115 | MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) / |
| 116 | (1000.0 / MYNEWT_VAL(OS_CPUTIME_FREQ)); |
| 117 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 118 | rc = boot_uart_open(); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 119 | assert(rc == 0); |
| 120 | |
| 121 | start_tick = os_cputime_get32(); |
| 122 | |
| 123 | while (1) { |
| 124 | /* Read a single character from the UART. */ |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 125 | rc = boot_uart_read(&ch, 1, &newline); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 126 | if (rc > 0) { |
| 127 | /* Eliminate the oldest character in the buffer to make room for |
| 128 | * the new one. |
| 129 | */ |
| 130 | memmove(buf, buf + 1, BOOT_SERIAL_DETECT_STRING_LEN - 1); |
| 131 | buf[BOOT_SERIAL_DETECT_STRING_LEN - 1] = ch; |
| 132 | |
| 133 | /* If the full management string has been received, indicate that |
| 134 | * the serial boot loader should start. |
| 135 | */ |
| 136 | rc = memcmp(buf, |
| 137 | MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING), |
| 138 | BOOT_SERIAL_DETECT_STRING_LEN); |
| 139 | if (rc == 0) { |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 140 | boot_uart_close(); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* Abort the listen on timeout. */ |
| 146 | if (os_cputime_get32() >= start_tick + timeout_dur) { |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 147 | boot_uart_close(); |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | #endif |
| 153 | |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 154 | static void |
| 155 | serial_boot_detect(void) |
| 156 | { |
| 157 | /* |
| 158 | * Read retained register and compare with expected magic value. |
| 159 | * If it matches, await for download commands from serial. |
| 160 | */ |
| 161 | #if MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX) != -1 |
| 162 | if (hal_nvreg_read(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX)) == |
| 163 | MYNEWT_VAL(BOOT_SERIAL_NVREG_MAGIC)) { |
| 164 | |
| 165 | hal_nvreg_write(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX), 0); |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 166 | goto serial_boot; |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 167 | } |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 168 | #endif |
| 169 | |
| 170 | /* |
| 171 | * Configure a GPIO as input, and compare it against expected value. |
| 172 | * If it matches, await for download commands from serial. |
| 173 | */ |
| 174 | #if MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN) != -1 |
| 175 | hal_gpio_init_in(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN), |
| 176 | MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_CFG)); |
| 177 | if (hal_gpio_read(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN)) == |
| 178 | MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_VAL)) { |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 179 | goto serial_boot; |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 180 | } |
| 181 | #endif |
| 182 | |
| 183 | /* |
| 184 | * Listen for management pattern in UART input. If detected, await for |
| 185 | * download commands from serial. |
| 186 | */ |
| 187 | #if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0 |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame] | 188 | if (serial_detect_uart_string()) { |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 189 | goto serial_boot; |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 190 | } |
| 191 | #endif |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 192 | return; |
| 193 | serial_boot: |
| 194 | boot_uart_open(); |
| 195 | boot_serial_start(&boot_uart_funcs); |
| 196 | assert(0); |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 197 | } |
| 198 | #endif |
| 199 | |
Marko Kiiskila | 149b457 | 2018-06-06 14:18:54 +0300 | [diff] [blame] | 200 | /* |
| 201 | * Temporary flash_device_base() implementation. |
| 202 | * |
| 203 | * TODO: remove this when mynewt needs to support flash_device_base() |
| 204 | * for devices with nonzero base addresses. |
| 205 | */ |
| 206 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 207 | { |
| 208 | *ret = 0; |
| 209 | return 0; |
| 210 | } |
| 211 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 212 | int |
| 213 | main(void) |
| 214 | { |
| 215 | struct boot_rsp rsp; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 216 | uintptr_t flash_base; |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 217 | int rc; |
Tamas Ban | 76177e1 | 2020-09-30 08:01:05 +0100 | [diff] [blame] | 218 | fih_int fih_rc = FIH_FAILURE; |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 219 | |
Andrzej Kaczmarek | c49099c | 2018-02-16 17:10:51 +0100 | [diff] [blame] | 220 | hal_bsp_init(); |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 221 | |
Fabio Utzig | 0f29c48 | 2018-07-26 14:53:36 -0300 | [diff] [blame] | 222 | #if !MYNEWT_VAL(OS_SCHEDULING) && MYNEWT_VAL(WATCHDOG_INTERVAL) |
| 223 | rc = hal_watchdog_init(MYNEWT_VAL(WATCHDOG_INTERVAL)); |
| 224 | assert(rc == 0); |
| 225 | #endif |
| 226 | |
Fabio Utzig | af1e02e | 2019-06-14 08:56:41 -0300 | [diff] [blame] | 227 | #if defined(MCUBOOT_SERIAL) || defined(MCUBOOT_HAVE_LOGGING) || \ |
| 228 | MYNEWT_VAL(CRYPTO) || MYNEWT_VAL(HASH) |
Fabio Utzig | ad0e9b8 | 2019-02-18 16:13:01 -0300 | [diff] [blame] | 229 | /* initialize uart/crypto without os */ |
Fabio Utzig | 9f7c3d2 | 2018-02-16 14:55:28 -0200 | [diff] [blame] | 230 | os_dev_initialize_all(OS_DEV_INIT_PRIMARY); |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 231 | os_dev_initialize_all(OS_DEV_INIT_SECONDARY); |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 232 | sysinit(); |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 233 | console_blocking_mode(); |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 234 | #if defined(MCUBOOT_SERIAL) |
| 235 | serial_boot_detect(); |
Fabio Utzig | 3b69d6f | 2018-06-25 13:59:36 -0300 | [diff] [blame] | 236 | hal_timer_deinit(MYNEWT_VAL(OS_CPUTIME_TIMER_NUM)); |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 237 | #endif |
Fabio Utzig | 94912c5 | 2018-05-07 08:38:23 -0300 | [diff] [blame] | 238 | #else |
| 239 | flash_map_init(); |
| 240 | #endif |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 241 | |
Tamas Ban | 76177e1 | 2020-09-30 08:01:05 +0100 | [diff] [blame] | 242 | FIH_CALL(boot_go, fih_rc, &rsp); |
| 243 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
| 244 | assert(fih_int_decode(fih_rc) == FIH_POSITIVE_VALUE); |
| 245 | FIH_PANIC; |
| 246 | } |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 247 | |
Fabio Utzig | b00d648 | 2017-06-20 19:28:22 -0300 | [diff] [blame] | 248 | rc = flash_device_base(rsp.br_flash_dev_id, &flash_base); |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 249 | assert(rc == 0); |
| 250 | |
Fabio Utzig | e47ccd6 | 2019-10-18 10:55:22 -0300 | [diff] [blame] | 251 | #if MYNEWT_VAL(BOOT_CUSTOM_START) |
| 252 | boot_custom_start(flash_base, &rsp); |
| 253 | #else |
Jerzy Kasenberg | 1d0467e | 2020-11-21 23:45:34 +0100 | [diff] [blame^] | 254 | hal_bsp_deinit(); |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 255 | hal_system_start((void *)(flash_base + rsp.br_image_off + |
| 256 | rsp.br_hdr->ih_hdr_size)); |
Fabio Utzig | e47ccd6 | 2019-10-18 10:55:22 -0300 | [diff] [blame] | 257 | #endif |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 258 | |
| 259 | return 0; |
| 260 | } |