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> |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 34 | #include <sysinit/sysinit.h> |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 35 | #ifdef MCUBOOT_SERIAL |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 36 | #include <hal/hal_gpio.h> |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 37 | #include <hal/hal_nvreg.h> |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 38 | #include <boot_serial/boot_serial.h> |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 39 | #endif |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame^] | 40 | #if defined(MCUBOOT_SERIAL) && MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0 |
| 41 | #include <boot_uart/boot_uart.h> |
| 42 | #endif |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 43 | #include <console/console.h> |
| 44 | #include "bootutil/image.h" |
| 45 | #include "bootutil/bootutil.h" |
Fabio Utzig | 94912c5 | 2018-05-07 08:38:23 -0300 | [diff] [blame] | 46 | #include "bootutil/bootutil_log.h" |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 47 | |
Fabio Utzig | 12f819f | 2018-03-29 16:20:53 -0300 | [diff] [blame] | 48 | #define BOOT_AREA_DESC_MAX (256) |
| 49 | #define AREA_DESC_MAX (BOOT_AREA_DESC_MAX) |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 50 | |
Fabio Utzig | 19356bf | 2017-05-11 16:19:36 -0300 | [diff] [blame] | 51 | #ifdef MCUBOOT_SERIAL |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 52 | #define BOOT_SERIAL_INPUT_MAX (512) |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 53 | #endif |
| 54 | |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 55 | /* |
| 56 | * Temporary flash_device_base() implementation. |
| 57 | * |
| 58 | * TODO: remove this when mynewt needs to support flash_device_base() |
| 59 | * for devices with nonzero base addresses. |
| 60 | */ |
| 61 | int flash_device_base(uint8_t fd_id, uintptr_t *ret) |
| 62 | { |
| 63 | *ret = 0; |
| 64 | return 0; |
| 65 | } |
| 66 | |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 67 | #ifdef MCUBOOT_SERIAL |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame^] | 68 | #if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0 |
| 69 | |
| 70 | /** Don't include null-terminator in comparison. */ |
| 71 | #define BOOT_SERIAL_DETECT_STRING_LEN \ |
| 72 | (sizeof MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING) - 1) |
| 73 | |
| 74 | /** |
| 75 | * Listens on the UART for the management string. Blocks for up to |
| 76 | * BOOT_SERIAL_DETECT_TIMEOUT milliseconds. |
| 77 | * |
| 78 | * @return true if the management string was received; |
| 79 | * false if the management string was not received |
| 80 | * before the UART listen timeout expired. |
| 81 | */ |
| 82 | static bool |
| 83 | serial_detect_uart_string(void) |
| 84 | { |
| 85 | uint32_t start_tick; |
| 86 | char buf[BOOT_SERIAL_DETECT_STRING_LEN] = { 0 }; |
| 87 | char ch; |
| 88 | int newline; |
| 89 | int rc; |
| 90 | |
| 91 | /* Calculate the timeout duration in OS cputime ticks. */ |
| 92 | static const uint32_t timeout_dur = |
| 93 | MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) / |
| 94 | (1000.0 / MYNEWT_VAL(OS_CPUTIME_FREQ)); |
| 95 | |
| 96 | rc = boot_serial_uart_open(); |
| 97 | assert(rc == 0); |
| 98 | |
| 99 | start_tick = os_cputime_get32(); |
| 100 | |
| 101 | while (1) { |
| 102 | /* Read a single character from the UART. */ |
| 103 | rc = boot_serial_uart_read(&ch, 1, &newline); |
| 104 | if (rc > 0) { |
| 105 | /* Eliminate the oldest character in the buffer to make room for |
| 106 | * the new one. |
| 107 | */ |
| 108 | memmove(buf, buf + 1, BOOT_SERIAL_DETECT_STRING_LEN - 1); |
| 109 | buf[BOOT_SERIAL_DETECT_STRING_LEN - 1] = ch; |
| 110 | |
| 111 | /* If the full management string has been received, indicate that |
| 112 | * the serial boot loader should start. |
| 113 | */ |
| 114 | rc = memcmp(buf, |
| 115 | MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING), |
| 116 | BOOT_SERIAL_DETECT_STRING_LEN); |
| 117 | if (rc == 0) { |
| 118 | boot_serial_uart_close(); |
| 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* Abort the listen on timeout. */ |
| 124 | if (os_cputime_get32() >= start_tick + timeout_dur) { |
| 125 | boot_serial_uart_close(); |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | #endif |
| 131 | |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 132 | static void |
| 133 | serial_boot_detect(void) |
| 134 | { |
| 135 | /* |
| 136 | * Read retained register and compare with expected magic value. |
| 137 | * If it matches, await for download commands from serial. |
| 138 | */ |
| 139 | #if MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX) != -1 |
| 140 | if (hal_nvreg_read(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX)) == |
| 141 | MYNEWT_VAL(BOOT_SERIAL_NVREG_MAGIC)) { |
| 142 | |
| 143 | hal_nvreg_write(MYNEWT_VAL(BOOT_SERIAL_NVREG_INDEX), 0); |
| 144 | |
| 145 | boot_serial_start(BOOT_SERIAL_INPUT_MAX); |
| 146 | assert(0); |
| 147 | } |
| 148 | |
| 149 | #endif |
| 150 | |
| 151 | /* |
| 152 | * Configure a GPIO as input, and compare it against expected value. |
| 153 | * If it matches, await for download commands from serial. |
| 154 | */ |
| 155 | #if MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN) != -1 |
| 156 | hal_gpio_init_in(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN), |
| 157 | MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_CFG)); |
| 158 | if (hal_gpio_read(MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN)) == |
| 159 | MYNEWT_VAL(BOOT_SERIAL_DETECT_PIN_VAL)) { |
| 160 | boot_serial_start(BOOT_SERIAL_INPUT_MAX); |
| 161 | assert(0); |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | /* |
| 166 | * Listen for management pattern in UART input. If detected, await for |
| 167 | * download commands from serial. |
| 168 | */ |
| 169 | #if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0 |
Marko Kiiskila | ce50ab0 | 2018-06-06 11:33:33 +0300 | [diff] [blame^] | 170 | if (serial_detect_uart_string()) { |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 171 | boot_serial_start(BOOT_SERIAL_INPUT_MAX); |
| 172 | assert(0); |
| 173 | } |
| 174 | #endif |
| 175 | } |
| 176 | #endif |
| 177 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 178 | int |
| 179 | main(void) |
| 180 | { |
| 181 | struct boot_rsp rsp; |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 182 | uintptr_t flash_base; |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 183 | int rc; |
| 184 | |
Andrzej Kaczmarek | c49099c | 2018-02-16 17:10:51 +0100 | [diff] [blame] | 185 | hal_bsp_init(); |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 186 | |
Fabio Utzig | 94912c5 | 2018-05-07 08:38:23 -0300 | [diff] [blame] | 187 | #if defined(MCUBOOT_SERIAL) || defined(MCUBOOT_HAVE_LOGGING) |
Fabio Utzig | 9f7c3d2 | 2018-02-16 14:55:28 -0200 | [diff] [blame] | 188 | /* initialize uart without os */ |
| 189 | os_dev_initialize_all(OS_DEV_INIT_PRIMARY); |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 190 | os_dev_initialize_all(OS_DEV_INIT_SECONDARY); |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 191 | sysinit(); |
Fabio Utzig | 1d46c94 | 2018-02-26 10:38:00 -0300 | [diff] [blame] | 192 | console_blocking_mode(); |
Marko Kiiskila | 316d361 | 2018-06-05 12:03:27 +0300 | [diff] [blame] | 193 | #if defined(MCUBOOT_SERIAL) |
| 194 | serial_boot_detect(); |
| 195 | #endif |
Fabio Utzig | 94912c5 | 2018-05-07 08:38:23 -0300 | [diff] [blame] | 196 | #else |
| 197 | flash_map_init(); |
| 198 | #endif |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 199 | |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 200 | rc = boot_go(&rsp); |
| 201 | assert(rc == 0); |
| 202 | |
Fabio Utzig | b00d648 | 2017-06-20 19:28:22 -0300 | [diff] [blame] | 203 | rc = flash_device_base(rsp.br_flash_dev_id, &flash_base); |
Marti Bolivar | eb94080 | 2017-05-01 23:15:29 -0400 | [diff] [blame] | 204 | assert(rc == 0); |
| 205 | |
| 206 | hal_system_start((void *)(flash_base + rsp.br_image_off + |
| 207 | rsp.br_hdr->ih_hdr_size)); |
Christopher Collins | cf18a03 | 2017-02-06 15:10:45 -0800 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |