|
| 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 | + |
| 32 | +#if defined(CONFIG_BOOT_SERIAL_PIN_RESET) |
| 33 | +#include <zephyr/drivers/hwinfo.h> |
| 34 | +#endif |
| 35 | + |
| 36 | +#if defined(CONFIG_BOOT_SERIAL_BOOT_MODE) |
| 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 | + |
| 51 | +#ifdef CONFIG_MCUBOOT_INDICATION_LED |
| 52 | + |
| 53 | +/* |
| 54 | + * The led0 devicetree alias is optional. If present, we'll use it |
| 55 | + * to turn on the LED whenever the button is pressed. |
| 56 | + */ |
| 57 | +#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0)) |
| 58 | +#define LED0_NODE DT_ALIAS(mcuboot_led0) |
| 59 | +#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0)) |
| 60 | +#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead" |
| 61 | +#define LED0_NODE DT_ALIAS(bootloader_led0) |
| 62 | +#endif |
| 63 | + |
| 64 | +#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios) |
| 65 | +static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios); |
| 66 | +#else |
| 67 | +/* A build error here means your board isn't set up to drive an LED. */ |
| 68 | +#error "Unsupported board: led0 devicetree alias is not defined" |
| 69 | +#endif |
| 70 | + |
| 71 | +void io_led_init(void) |
| 72 | +{ |
| 73 | + if (!device_is_ready(led0.port)) { |
| 74 | + BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + gpio_pin_configure_dt(&led0, GPIO_OUTPUT); |
| 79 | + gpio_pin_set_dt(&led0, 0); |
| 80 | +} |
| 81 | +#endif /* CONFIG_MCUBOOT_INDICATION_LED */ |
| 82 | + |
| 83 | +#if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO) |
| 84 | + |
| 85 | +#if defined(CONFIG_MCUBOOT_SERIAL) |
| 86 | +#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY |
| 87 | +#else |
| 88 | +#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY |
| 89 | +#endif |
| 90 | + |
| 91 | +#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0) |
| 92 | + |
| 93 | +#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios) |
| 94 | +static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios); |
| 95 | +#else |
| 96 | +#error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'" |
| 97 | +#endif |
| 98 | + |
| 99 | +bool io_detect_pin(void) |
| 100 | +{ |
| 101 | + int rc; |
| 102 | + int pin_active; |
| 103 | + |
| 104 | + if (!device_is_ready(button0.port)) { |
| 105 | + __ASSERT(false, "GPIO device is not ready.\n"); |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + rc = gpio_pin_configure_dt(&button0, GPIO_INPUT); |
| 110 | + __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n"); |
| 111 | + |
| 112 | + rc = gpio_pin_get_dt(&button0); |
| 113 | + pin_active = rc; |
| 114 | + |
| 115 | + __ASSERT(rc >= 0, "Failed to read boot detect pin.\n"); |
| 116 | + |
| 117 | + if (pin_active) { |
| 118 | + if (BUTTON_0_DETECT_DELAY > 0) { |
| 119 | +#ifdef CONFIG_MULTITHREADING |
| 120 | + k_sleep(K_MSEC(50)); |
| 121 | +#else |
| 122 | + k_busy_wait(50000); |
| 123 | +#endif |
| 124 | + |
| 125 | + /* Get the uptime for debounce purposes. */ |
| 126 | + int64_t timestamp = k_uptime_get(); |
| 127 | + |
| 128 | + for(;;) { |
| 129 | + rc = gpio_pin_get_dt(&button0); |
| 130 | + pin_active = rc; |
| 131 | + __ASSERT(rc >= 0, "Failed to read boot detect pin.\n"); |
| 132 | + |
| 133 | + /* Get delta from when this started */ |
| 134 | + uint32_t delta = k_uptime_get() - timestamp; |
| 135 | + |
| 136 | + /* If not pressed OR if pressed > debounce period, stop. */ |
| 137 | + if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) { |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + /* Delay 1 ms */ |
| 142 | +#ifdef CONFIG_MULTITHREADING |
| 143 | + k_sleep(K_MSEC(1)); |
| 144 | +#else |
| 145 | + k_busy_wait(1000); |
| 146 | +#endif |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return (bool)pin_active; |
| 152 | +} |
| 153 | +#endif |
| 154 | + |
| 155 | +#if defined(CONFIG_BOOT_SERIAL_PIN_RESET) |
| 156 | +bool io_detect_pin_reset(void) |
| 157 | +{ |
| 158 | + uint32_t reset_cause; |
| 159 | + int rc; |
| 160 | + |
| 161 | + rc = hwinfo_get_reset_cause(&reset_cause); |
| 162 | + |
| 163 | + if (rc == 0 && reset_cause == RESET_PIN) { |
| 164 | + (void)hwinfo_clear_reset_cause(); |
| 165 | + return true; |
| 166 | + } |
| 167 | + |
| 168 | + return false; |
| 169 | +} |
| 170 | +#endif |
| 171 | + |
| 172 | +#if defined(CONFIG_BOOT_SERIAL_BOOT_MODE) |
| 173 | +bool io_detect_boot_mode(void) |
| 174 | +{ |
| 175 | + int32_t boot_mode; |
| 176 | + |
| 177 | + boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER); |
| 178 | + |
| 179 | + if (boot_mode == 1) { |
| 180 | + /* Boot mode to stay in bootloader, clear status and enter serial |
| 181 | + * recovery mode |
| 182 | + */ |
| 183 | + bootmode_clear(); |
| 184 | + |
| 185 | + return true; |
| 186 | + } |
| 187 | + |
| 188 | + return false; |
| 189 | +} |
| 190 | +#endif |
0 commit comments