blob: e6d54d3c7d2772bf9c4a645b3c43db6747d8eff2 [file] [log] [blame]
Jamie McCrae433b8482023-08-16 07:33:24 +01001/*
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"
Piotr Dymacz8c6c6702023-12-05 14:05:38 +010031#include "bootutil/bootutil_log.h"
Jamie McCrae433b8482023-08-16 07:33:24 +010032
Jamie McCrae215345f2023-08-16 07:37:18 +010033#if defined(CONFIG_BOOT_SERIAL_PIN_RESET) || defined(CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET)
Jamie McCrae433b8482023-08-16 07:33:24 +010034#include <zephyr/drivers/hwinfo.h>
35#endif
36
Jamie McCrae215345f2023-08-16 07:37:18 +010037#if defined(CONFIG_BOOT_SERIAL_BOOT_MODE) || defined(CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE)
Jamie McCrae433b8482023-08-16 07:33:24 +010038#include <zephyr/retention/bootmode.h>
39#endif
40
41/* Validate serial recovery configuration */
42#ifdef CONFIG_MCUBOOT_SERIAL
43#if !defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) && \
44 !defined(CONFIG_BOOT_SERIAL_WAIT_FOR_DFU) && \
45 !defined(CONFIG_BOOT_SERIAL_BOOT_MODE) && \
46 !defined(CONFIG_BOOT_SERIAL_NO_APPLICATION) && \
47 !defined(CONFIG_BOOT_SERIAL_PIN_RESET)
48#error "Serial recovery selected without an entrance mode set"
49#endif
50#endif
51
Jamie McCrae215345f2023-08-16 07:37:18 +010052/* Validate firmware loader configuration */
53#ifdef CONFIG_BOOT_FIRMWARE_LOADER
54#if !defined(CONFIG_BOOT_FIRMWARE_LOADER_ENTRANCE_GPIO) && \
55 !defined(CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE) && \
56 !defined(CONFIG_BOOT_FIRMWARE_LOADER_NO_APPLICATION) && \
57 !defined(CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET)
58#error "Firmware loader selected without an entrance mode set"
59#endif
60#endif
61
Jamie McCrae433b8482023-08-16 07:33:24 +010062#ifdef CONFIG_MCUBOOT_INDICATION_LED
63
64/*
65 * The led0 devicetree alias is optional. If present, we'll use it
66 * to turn on the LED whenever the button is pressed.
67 */
68#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
69#define LED0_NODE DT_ALIAS(mcuboot_led0)
70#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
71#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
72#define LED0_NODE DT_ALIAS(bootloader_led0)
73#endif
74
75#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
76static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
77#else
78/* A build error here means your board isn't set up to drive an LED. */
79#error "Unsupported board: led0 devicetree alias is not defined"
80#endif
81
Piotr Dymacz8c6c6702023-12-05 14:05:38 +010082BOOT_LOG_MODULE_DECLARE(mcuboot);
83
Jamie McCrae433b8482023-08-16 07:33:24 +010084void io_led_init(void)
85{
86 if (!device_is_ready(led0.port)) {
87 BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
88 return;
89 }
90
91 gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
92 gpio_pin_set_dt(&led0, 0);
93}
94#endif /* CONFIG_MCUBOOT_INDICATION_LED */
95
Jamie McCrae215345f2023-08-16 07:37:18 +010096#if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO) || \
97 defined(CONFIG_BOOT_FIRMWARE_LOADER_ENTRANCE_GPIO)
Jamie McCrae433b8482023-08-16 07:33:24 +010098
99#if defined(CONFIG_MCUBOOT_SERIAL)
100#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
Jamie McCrae215345f2023-08-16 07:37:18 +0100101#elif defined(CONFIG_BOOT_FIRMWARE_LOADER)
102#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_FIRMWARE_LOADER_DETECT_DELAY
Jamie McCrae433b8482023-08-16 07:33:24 +0100103#else
104#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
105#endif
106
107#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
108
109#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
110static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
111#else
112#error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'"
113#endif
114
115bool io_detect_pin(void)
116{
117 int rc;
118 int pin_active;
119
120 if (!device_is_ready(button0.port)) {
121 __ASSERT(false, "GPIO device is not ready.\n");
122 return false;
123 }
124
125 rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
126 __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
127
128 rc = gpio_pin_get_dt(&button0);
129 pin_active = rc;
130
131 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
132
133 if (pin_active) {
134 if (BUTTON_0_DETECT_DELAY > 0) {
135#ifdef CONFIG_MULTITHREADING
136 k_sleep(K_MSEC(50));
137#else
138 k_busy_wait(50000);
139#endif
140
141 /* Get the uptime for debounce purposes. */
142 int64_t timestamp = k_uptime_get();
143
144 for(;;) {
145 rc = gpio_pin_get_dt(&button0);
146 pin_active = rc;
147 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
148
149 /* Get delta from when this started */
150 uint32_t delta = k_uptime_get() - timestamp;
151
152 /* If not pressed OR if pressed > debounce period, stop. */
153 if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
154 break;
155 }
156
157 /* Delay 1 ms */
158#ifdef CONFIG_MULTITHREADING
159 k_sleep(K_MSEC(1));
160#else
161 k_busy_wait(1000);
162#endif
163 }
164 }
165 }
166
167 return (bool)pin_active;
168}
169#endif
170
Jamie McCrae215345f2023-08-16 07:37:18 +0100171#if defined(CONFIG_BOOT_SERIAL_PIN_RESET) || defined(CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET)
Jamie McCrae433b8482023-08-16 07:33:24 +0100172bool io_detect_pin_reset(void)
173{
174 uint32_t reset_cause;
175 int rc;
176
177 rc = hwinfo_get_reset_cause(&reset_cause);
178
179 if (rc == 0 && reset_cause == RESET_PIN) {
180 (void)hwinfo_clear_reset_cause();
181 return true;
182 }
183
184 return false;
185}
186#endif
187
Jamie McCrae215345f2023-08-16 07:37:18 +0100188#if defined(CONFIG_BOOT_SERIAL_BOOT_MODE) || defined(CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE)
Jamie McCrae433b8482023-08-16 07:33:24 +0100189bool io_detect_boot_mode(void)
190{
191 int32_t boot_mode;
192
193 boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER);
194
195 if (boot_mode == 1) {
196 /* Boot mode to stay in bootloader, clear status and enter serial
197 * recovery mode
198 */
199 bootmode_clear();
200
201 return true;
202 }
203
204 return false;
205}
206#endif