Jamie McCrae | 433b848 | 2023-08-16 07:33:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
| 3 | * Copyright (c) 2020 Arm Limited |
| 4 | * Copyright (c) 2021-2023 Nordic Semiconductor ASA |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <zephyr/kernel.h> |
| 21 | #include <zephyr/devicetree.h> |
| 22 | #include <zephyr/drivers/gpio.h> |
| 23 | #include <zephyr/sys/__assert.h> |
| 24 | #include <zephyr/drivers/flash.h> |
| 25 | #include <zephyr/drivers/timer/system_timer.h> |
| 26 | #include <zephyr/usb/usb_device.h> |
| 27 | #include <soc.h> |
| 28 | #include <zephyr/linker/linker-defs.h> |
| 29 | |
| 30 | #include "target.h" |
| 31 | |
Jamie McCrae | 215345f | 2023-08-16 07:37:18 +0100 | [diff] [blame^] | 32 | #if defined(CONFIG_BOOT_SERIAL_PIN_RESET) || defined(CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET) |
Jamie McCrae | 433b848 | 2023-08-16 07:33:24 +0100 | [diff] [blame] | 33 | #include <zephyr/drivers/hwinfo.h> |
| 34 | #endif |
| 35 | |
Jamie McCrae | 215345f | 2023-08-16 07:37:18 +0100 | [diff] [blame^] | 36 | #if defined(CONFIG_BOOT_SERIAL_BOOT_MODE) || defined(CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE) |
Jamie McCrae | 433b848 | 2023-08-16 07:33:24 +0100 | [diff] [blame] | 37 | #include <zephyr/retention/bootmode.h> |
| 38 | #endif |
| 39 | |
| 40 | /* Validate serial recovery configuration */ |
| 41 | #ifdef CONFIG_MCUBOOT_SERIAL |
| 42 | #if !defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) && \ |
| 43 | !defined(CONFIG_BOOT_SERIAL_WAIT_FOR_DFU) && \ |
| 44 | !defined(CONFIG_BOOT_SERIAL_BOOT_MODE) && \ |
| 45 | !defined(CONFIG_BOOT_SERIAL_NO_APPLICATION) && \ |
| 46 | !defined(CONFIG_BOOT_SERIAL_PIN_RESET) |
| 47 | #error "Serial recovery selected without an entrance mode set" |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Jamie McCrae | 215345f | 2023-08-16 07:37:18 +0100 | [diff] [blame^] | 51 | /* Validate firmware loader configuration */ |
| 52 | #ifdef CONFIG_BOOT_FIRMWARE_LOADER |
| 53 | #if !defined(CONFIG_BOOT_FIRMWARE_LOADER_ENTRANCE_GPIO) && \ |
| 54 | !defined(CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE) && \ |
| 55 | !defined(CONFIG_BOOT_FIRMWARE_LOADER_NO_APPLICATION) && \ |
| 56 | !defined(CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET) |
| 57 | #error "Firmware loader selected without an entrance mode set" |
| 58 | #endif |
| 59 | #endif |
| 60 | |
Jamie McCrae | 433b848 | 2023-08-16 07:33:24 +0100 | [diff] [blame] | 61 | #ifdef CONFIG_MCUBOOT_INDICATION_LED |
| 62 | |
| 63 | /* |
| 64 | * The led0 devicetree alias is optional. If present, we'll use it |
| 65 | * to turn on the LED whenever the button is pressed. |
| 66 | */ |
| 67 | #if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0)) |
| 68 | #define LED0_NODE DT_ALIAS(mcuboot_led0) |
| 69 | #elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0)) |
| 70 | #warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead" |
| 71 | #define LED0_NODE DT_ALIAS(bootloader_led0) |
| 72 | #endif |
| 73 | |
| 74 | #if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios) |
| 75 | static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios); |
| 76 | #else |
| 77 | /* A build error here means your board isn't set up to drive an LED. */ |
| 78 | #error "Unsupported board: led0 devicetree alias is not defined" |
| 79 | #endif |
| 80 | |
| 81 | void io_led_init(void) |
| 82 | { |
| 83 | if (!device_is_ready(led0.port)) { |
| 84 | BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n"); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | gpio_pin_configure_dt(&led0, GPIO_OUTPUT); |
| 89 | gpio_pin_set_dt(&led0, 0); |
| 90 | } |
| 91 | #endif /* CONFIG_MCUBOOT_INDICATION_LED */ |
| 92 | |
Jamie McCrae | 215345f | 2023-08-16 07:37:18 +0100 | [diff] [blame^] | 93 | #if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO) || \ |
| 94 | defined(CONFIG_BOOT_FIRMWARE_LOADER_ENTRANCE_GPIO) |
Jamie McCrae | 433b848 | 2023-08-16 07:33:24 +0100 | [diff] [blame] | 95 | |
| 96 | #if defined(CONFIG_MCUBOOT_SERIAL) |
| 97 | #define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY |
Jamie McCrae | 215345f | 2023-08-16 07:37:18 +0100 | [diff] [blame^] | 98 | #elif defined(CONFIG_BOOT_FIRMWARE_LOADER) |
| 99 | #define BUTTON_0_DETECT_DELAY CONFIG_BOOT_FIRMWARE_LOADER_DETECT_DELAY |
Jamie McCrae | 433b848 | 2023-08-16 07:33:24 +0100 | [diff] [blame] | 100 | #else |
| 101 | #define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY |
| 102 | #endif |
| 103 | |
| 104 | #define BUTTON_0_NODE DT_ALIAS(mcuboot_button0) |
| 105 | |
| 106 | #if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios) |
| 107 | static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios); |
| 108 | #else |
| 109 | #error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'" |
| 110 | #endif |
| 111 | |
| 112 | bool io_detect_pin(void) |
| 113 | { |
| 114 | int rc; |
| 115 | int pin_active; |
| 116 | |
| 117 | if (!device_is_ready(button0.port)) { |
| 118 | __ASSERT(false, "GPIO device is not ready.\n"); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | rc = gpio_pin_configure_dt(&button0, GPIO_INPUT); |
| 123 | __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n"); |
| 124 | |
| 125 | rc = gpio_pin_get_dt(&button0); |
| 126 | pin_active = rc; |
| 127 | |
| 128 | __ASSERT(rc >= 0, "Failed to read boot detect pin.\n"); |
| 129 | |
| 130 | if (pin_active) { |
| 131 | if (BUTTON_0_DETECT_DELAY > 0) { |
| 132 | #ifdef CONFIG_MULTITHREADING |
| 133 | k_sleep(K_MSEC(50)); |
| 134 | #else |
| 135 | k_busy_wait(50000); |
| 136 | #endif |
| 137 | |
| 138 | /* Get the uptime for debounce purposes. */ |
| 139 | int64_t timestamp = k_uptime_get(); |
| 140 | |
| 141 | for(;;) { |
| 142 | rc = gpio_pin_get_dt(&button0); |
| 143 | pin_active = rc; |
| 144 | __ASSERT(rc >= 0, "Failed to read boot detect pin.\n"); |
| 145 | |
| 146 | /* Get delta from when this started */ |
| 147 | uint32_t delta = k_uptime_get() - timestamp; |
| 148 | |
| 149 | /* If not pressed OR if pressed > debounce period, stop. */ |
| 150 | if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) { |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | /* Delay 1 ms */ |
| 155 | #ifdef CONFIG_MULTITHREADING |
| 156 | k_sleep(K_MSEC(1)); |
| 157 | #else |
| 158 | k_busy_wait(1000); |
| 159 | #endif |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return (bool)pin_active; |
| 165 | } |
| 166 | #endif |
| 167 | |
Jamie McCrae | 215345f | 2023-08-16 07:37:18 +0100 | [diff] [blame^] | 168 | #if defined(CONFIG_BOOT_SERIAL_PIN_RESET) || defined(CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET) |
Jamie McCrae | 433b848 | 2023-08-16 07:33:24 +0100 | [diff] [blame] | 169 | bool io_detect_pin_reset(void) |
| 170 | { |
| 171 | uint32_t reset_cause; |
| 172 | int rc; |
| 173 | |
| 174 | rc = hwinfo_get_reset_cause(&reset_cause); |
| 175 | |
| 176 | if (rc == 0 && reset_cause == RESET_PIN) { |
| 177 | (void)hwinfo_clear_reset_cause(); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | #endif |
| 184 | |
Jamie McCrae | 215345f | 2023-08-16 07:37:18 +0100 | [diff] [blame^] | 185 | #if defined(CONFIG_BOOT_SERIAL_BOOT_MODE) || defined(CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE) |
Jamie McCrae | 433b848 | 2023-08-16 07:33:24 +0100 | [diff] [blame] | 186 | bool io_detect_boot_mode(void) |
| 187 | { |
| 188 | int32_t boot_mode; |
| 189 | |
| 190 | boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER); |
| 191 | |
| 192 | if (boot_mode == 1) { |
| 193 | /* Boot mode to stay in bootloader, clear status and enter serial |
| 194 | * recovery mode |
| 195 | */ |
| 196 | bootmode_clear(); |
| 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | return false; |
| 202 | } |
| 203 | #endif |