1 /*
2  * Copyright (c) 2017-2023 Nordic Semiconductor ASA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <zephyr/drivers/uart.h>
19 #include <assert.h>
20 #include <string.h>
21 #include <zephyr/kernel.h>
22 #include "bootutil/bootutil_log.h"
23 #include <zephyr/usb/usb_device.h>
24 
25 #if defined(CONFIG_BOOT_SERIAL_UART) && defined(CONFIG_UART_CONSOLE) && \
26     (!DT_HAS_CHOSEN(zephyr_uart_mcumgr) ||                              \
27      DT_SAME_NODE(DT_CHOSEN(zephyr_uart_mcumgr), DT_CHOSEN(zephyr_console)))
28 #error Zephyr UART console must be disabled if serial_adapter module is used.
29 #endif
30 
31 #if defined(CONFIG_BOOT_SERIAL_CDC_ACM) && \
32     defined(CONFIG_UART_CONSOLE) && (!DT_HAS_CHOSEN(zephyr_uart_mcumgr) || \
33     DT_SAME_NODE(DT_CHOSEN(zephyr_uart_mcumgr), DT_CHOSEN(zephyr_console)))
34 #error Zephyr UART console must be disabled if CDC ACM is enabled and MCUmgr \
35        has not been redirected to other UART with DTS chosen zephyr,uart-mcumgr.
36 #endif
37 
38 #if defined(CONFIG_BOOT_SERIAL_CDC_ACM) && CONFIG_MAIN_THREAD_PRIORITY < 0
39 #error CONFIG_MAIN_THREAD_PRIORITY must be preemptible to support USB CDC ACM \
40        (0 or above)
41 #endif
42 
43 BOOT_LOG_MODULE_REGISTER(serial_adapter);
44 
45 /** @brief Console input representation
46  *
47  * This struct is used to represent an input line from a serial interface.
48  */
49 struct line_input {
50 	/** Required to use sys_slist */
51 	sys_snode_t node;
52 
53 	int len;
54 	/** Buffer where the input line is recorded */
55 	char line[CONFIG_BOOT_MAX_LINE_INPUT_LEN];
56 };
57 
58 static struct device const *uart_dev;
59 static struct line_input line_bufs[CONFIG_BOOT_LINE_BUFS];
60 
61 static sys_slist_t avail_queue;
62 static sys_slist_t lines_queue;
63 
64 static uint16_t cur;
65 
66 static int boot_uart_fifo_getline(char **line);
67 static int boot_uart_fifo_init(void);
68 
69 int
console_out(int c)70 console_out(int c)
71 {
72 	uart_poll_out(uart_dev, c);
73 
74 	return c;
75 }
76 
77 void
console_write(const char * str,int cnt)78 console_write(const char *str, int cnt)
79 {
80 	int i;
81 
82 	for (i = 0; i < cnt; i++) {
83 		if (console_out((int)str[i]) == EOF) {
84 			break;
85 		}
86 	}
87 }
88 
89 int
console_read(char * str,int str_size,int * newline)90 console_read(char *str, int str_size, int *newline)
91 {
92 	char *line;
93 	int len;
94 
95 	len = boot_uart_fifo_getline(&line);
96 
97 	if (line == NULL) {
98 		*newline = 0;
99 		return 0;
100 	}
101 
102 	if (len > str_size - 1) {
103 		len = str_size - 1;
104 	}
105 
106 	memcpy(str, line, len);
107 	str[len] = '\0';
108 	*newline = 1;
109 	return len + 1;
110 }
111 
112 int
boot_console_init(void)113 boot_console_init(void)
114 {
115 	int i;
116 
117 	/* Zephyr UART handler takes an empty buffer from avail_queue,
118 	 * stores UART input in it until EOL, and then puts it into
119 	 * lines_queue.
120 	 */
121 	sys_slist_init(&avail_queue);
122 	sys_slist_init(&lines_queue);
123 
124 	for (i = 0; i < ARRAY_SIZE(line_bufs); i++) {
125 		sys_slist_append(&avail_queue, &line_bufs[i].node);
126 	}
127 
128 	return boot_uart_fifo_init();
129 }
130 
131 static void
boot_uart_fifo_callback(const struct device * dev,void * user_data)132 boot_uart_fifo_callback(const struct device *dev, void *user_data)
133 {
134 	static struct line_input *cmd;
135 	uint8_t byte;
136 	int rx;
137 
138 	uart_irq_update(uart_dev);
139 
140 	if (!uart_irq_rx_ready(uart_dev)) {
141 		return;
142 	}
143 
144 	while (true) {
145 		rx = uart_fifo_read(uart_dev, &byte, 1);
146 		if (rx != 1) {
147 			break;
148 		}
149 
150 		if (!cmd) {
151 			sys_snode_t *node;
152 
153 			node = sys_slist_get(&avail_queue);
154 			if (!node) {
155 				BOOT_LOG_ERR("Not enough memory to store"
156 					     " incoming data!");
157 				return;
158 			}
159 			cmd = CONTAINER_OF(node, struct line_input, node);
160 		}
161 
162 		if (cur < CONFIG_BOOT_MAX_LINE_INPUT_LEN) {
163 			cmd->line[cur++] = byte;
164 		}
165 
166 		if (byte ==  '\n') {
167 			cmd->len = cur;
168 			sys_slist_append(&lines_queue, &cmd->node);
169 			cur = 0;
170 			cmd = NULL;
171 		}
172 	}
173 }
174 
175 static int
boot_uart_fifo_getline(char ** line)176 boot_uart_fifo_getline(char **line)
177 {
178 	static struct line_input *cmd;
179 	sys_snode_t *node;
180 	int key;
181 
182 	key = irq_lock();
183 	/* Recycle cmd buffer returned previous time */
184 	if (cmd != NULL) {
185 		if (sys_slist_peek_tail(&avail_queue) != &cmd->node) {
186 			sys_slist_append(&avail_queue, &cmd->node);
187 		}
188 	}
189 
190 	node = sys_slist_get(&lines_queue);
191 	irq_unlock(key);
192 
193 	if (node == NULL) {
194 		cmd = NULL;
195 		*line = NULL;
196 
197 		return 0;
198 	}
199 
200 	cmd = CONTAINER_OF(node, struct line_input, node);
201 	*line = cmd->line;
202 	return cmd->len;
203 }
204 
205 static int
boot_uart_fifo_init(void)206 boot_uart_fifo_init(void)
207 {
208 #if defined(CONFIG_BOOT_SERIAL_UART)
209 
210 #if DT_HAS_CHOSEN(zephyr_uart_mcumgr)
211 	uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_uart_mcumgr));
212 #else
213 	uart_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
214 #endif
215 
216 #elif defined(CONFIG_BOOT_SERIAL_CDC_ACM)
217         uart_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
218 #else
219 #error No serial recovery device selected
220 #endif
221 
222 
223 	if (!device_is_ready(uart_dev)) {
224 		return (-1);
225 	}
226 
227 #if CONFIG_BOOT_SERIAL_CDC_ACM
228 	int rc = usb_enable(NULL);
229 	if (rc) {
230 		return (-1);
231 	}
232 #endif
233 
234 	uart_irq_callback_set(uart_dev, boot_uart_fifo_callback);
235 
236 	/* Drain the fifo */
237 	if (uart_irq_rx_ready(uart_dev)) {
238 		uint8_t c;
239 
240 		while (uart_fifo_read(uart_dev, &c, 1)) {
241 			;
242 		}
243 	}
244 
245 	cur = 0;
246 
247 	uart_irq_rx_enable(uart_dev);
248 
249 	return 0;
250 }
251