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 #include "bootutil/bootutil_log.h"
32 
33 #if defined(CONFIG_BOOT_SERIAL_PIN_RESET) || defined(CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET)
34 #include <zephyr/drivers/hwinfo.h>
35 #endif
36 
37 #if defined(CONFIG_BOOT_SERIAL_BOOT_MODE) || defined(CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE)
38 #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 
52 /* 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 
62 #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)
76 static 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 
82 BOOT_LOG_MODULE_DECLARE(mcuboot);
83 
io_led_init(void)84 void 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 
io_led_set(int value)95 void io_led_set(int value)
96 {
97     gpio_pin_set_dt(&led0, value);
98 }
99 #endif /* CONFIG_MCUBOOT_INDICATION_LED */
100 
101 #if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO) || \
102     defined(CONFIG_BOOT_FIRMWARE_LOADER_ENTRANCE_GPIO)
103 
104 #if defined(CONFIG_MCUBOOT_SERIAL)
105 #define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
106 #elif defined(CONFIG_BOOT_FIRMWARE_LOADER)
107 #define BUTTON_0_DETECT_DELAY CONFIG_BOOT_FIRMWARE_LOADER_DETECT_DELAY
108 #else
109 #define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
110 #endif
111 
112 #define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
113 
114 #if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
115 static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
116 #else
117 #error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'"
118 #endif
119 
io_detect_pin(void)120 bool io_detect_pin(void)
121 {
122     int rc;
123     int pin_active;
124 
125     if (!device_is_ready(button0.port)) {
126         __ASSERT(false, "GPIO device is not ready.\n");
127         return false;
128     }
129 
130     rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
131     __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
132 
133     rc = gpio_pin_get_dt(&button0);
134     pin_active = rc;
135 
136     __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
137 
138     if (pin_active) {
139         if (BUTTON_0_DETECT_DELAY > 0) {
140 #ifdef CONFIG_MULTITHREADING
141             k_sleep(K_MSEC(50));
142 #else
143             k_busy_wait(50000);
144 #endif
145 
146             /* Get the uptime for debounce purposes. */
147             int64_t timestamp = k_uptime_get();
148 
149             for(;;) {
150                 rc = gpio_pin_get_dt(&button0);
151                 pin_active = rc;
152                 __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
153 
154                 /* Get delta from when this started */
155                 uint32_t delta = k_uptime_get() -  timestamp;
156 
157                 /* If not pressed OR if pressed > debounce period, stop. */
158                 if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
159                     break;
160                 }
161 
162                 /* Delay 1 ms */
163 #ifdef CONFIG_MULTITHREADING
164                 k_sleep(K_MSEC(1));
165 #else
166                 k_busy_wait(1000);
167 #endif
168             }
169         }
170     }
171 
172     return (bool)pin_active;
173 }
174 #endif
175 
176 #if defined(CONFIG_BOOT_SERIAL_PIN_RESET) || defined(CONFIG_BOOT_FIRMWARE_LOADER_PIN_RESET)
io_detect_pin_reset(void)177 bool io_detect_pin_reset(void)
178 {
179     uint32_t reset_cause;
180     int rc;
181 
182     rc = hwinfo_get_reset_cause(&reset_cause);
183 
184     if (rc == 0 && reset_cause == RESET_PIN) {
185         (void)hwinfo_clear_reset_cause();
186         return true;
187     }
188 
189     return false;
190 }
191 #endif
192 
193 #if defined(CONFIG_BOOT_SERIAL_BOOT_MODE) || defined(CONFIG_BOOT_FIRMWARE_LOADER_BOOT_MODE)
io_detect_boot_mode(void)194 bool io_detect_boot_mode(void)
195 {
196     int32_t boot_mode;
197 
198     boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER);
199 
200     if (boot_mode == 1) {
201         /* Boot mode to stay in bootloader, clear status and enter serial
202          * recovery mode
203          */
204         bootmode_clear();
205 
206         return true;
207     }
208 
209     return false;
210 }
211 #endif
212