blob: d2c241994b099c04714159166fcf6e5b26d475b1 [file] [log] [blame]
Jeremy Kerrd831f962016-01-29 17:18:01 +08001/**
2 * Console server process for OpenBMC
3 *
Jeremy Kerr9326d772016-03-17 17:15:02 +08004 * Copyright © 2016 IBM Corporation
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.
Jeremy Kerrd831f962016-01-29 17:18:01 +080017 */
18
Jeremy Kerr329a35f2016-03-10 15:36:01 +080019#include <assert.h>
Jeremy Kerr769cee12016-03-15 17:53:56 +080020#include <errno.h>
21#include <signal.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080022#include <stdint.h>
23#include <stdbool.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <fcntl.h>
27#include <unistd.h>
28#include <err.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080029#include <string.h>
30#include <getopt.h>
Zev Weiss7dc08ba2023-09-12 20:49:14 -070031#include <glob.h>
Jeremy Kerr17217842016-01-29 18:44:21 +080032#include <limits.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080033#include <time.h>
Jeremy Kerr54e95692016-03-24 17:07:56 +080034#include <termios.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080035
36#include <sys/types.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080037#include <sys/time.h>
Ninad Palsuleb14ca192023-03-31 09:49:00 -050038#include <sys/socket.h>
Joel Stanley87e344c2016-09-01 00:00:51 +093039#include <poll.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080040
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080041#include "console-server.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080042
Andrew Jeffery30ea6382023-05-03 16:56:10 +093043#define DEV_PTS_PATH "/dev/pts"
44
Medicine Yeh7f2bfb92024-03-10 23:19:41 -070045/* default size of the shared backlog ringbuffer */
46const size_t default_buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080047
Jeremy Kerr769cee12016-03-15 17:53:56 +080048/* state shared with the signal handler */
49static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080050
Jeremy Kerrd831f962016-01-29 17:18:01 +080051static void usage(const char *progname)
52{
53 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093054 "usage: %s [options] <DEVICE>\n"
55 "\n"
56 "Options:\n"
Andrew Jeffery954be0f2023-05-03 21:01:59 +093057 " --config <FILE>\tUse FILE for configuration\n"
58 " --console-id <NAME>\tUse NAME in the UNIX domain socket address\n"
Andrew Jefferya72711a2023-04-18 18:19:41 +093059 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080060 progname);
61}
62
Andrew Jeffery30ea6382023-05-03 16:56:10 +093063/* populates console->tty.dev and console->tty.sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080064static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080065{
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093066 char *tty_class_device_link = NULL;
67 char *tty_path_input_real = NULL;
68 char *tty_device_tty_dir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093069 char *tty_vuart_lpc_addr = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093070 char *tty_device_reldir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093071 char *tty_sysfs_devnode = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093072 char *tty_kname_real = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093073 char *tty_path_input = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +080074 int rc;
75
Andrew Jeffery30ea6382023-05-03 16:56:10 +093076 console->tty.type = TTY_DEVICE_UNDEFINED;
77
78 assert(console->tty.kname);
79 if (!strlen(console->tty.kname)) {
80 warnx("TTY kname must not be empty");
81 rc = -1;
82 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093083 }
Jeremy Kerr17217842016-01-29 18:44:21 +080084
Andrew Jeffery30ea6382023-05-03 16:56:10 +093085 if (console->tty.kname[0] == '/') {
86 tty_path_input = strdup(console->tty.kname);
87 if (!tty_path_input) {
88 rc = -1;
89 goto out_free;
90 }
91 } else {
92 rc = asprintf(&tty_path_input, "/dev/%s", console->tty.kname);
93 if (rc < 0) {
94 goto out_free;
95 }
96 }
97
98 /* udev may rename the tty name with a symbol link, try to resolve */
Yi Li45ad7672016-10-18 23:21:19 +080099 tty_path_input_real = realpath(tty_path_input, NULL);
100 if (!tty_path_input_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930101 warn("Can't find realpath for %s", tty_path_input);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930102 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800103 goto out_free;
104 }
105
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930106 /*
107 * Allow hooking obmc-console-server up to PTYs for testing
108 *
109 * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html
110 */
111 if (!strncmp(DEV_PTS_PATH, tty_path_input_real, strlen(DEV_PTS_PATH))) {
112 console->tty.type = TTY_DEVICE_PTY;
113 console->tty.dev = strdup(console->tty.kname);
114 rc = console->tty.dev ? 0 : -1;
115 goto out_free;
116 }
117
Yi Li45ad7672016-10-18 23:21:19 +0800118 tty_kname_real = basename(tty_path_input_real);
119 if (!tty_kname_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930120 warn("Can't find real name for %s", console->tty.kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930121 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800122 goto out_free;
123 }
124
Andrew Jefferya72711a2023-04-18 18:19:41 +0930125 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
126 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930127 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800128 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930129 }
Yi Li45ad7672016-10-18 23:21:19 +0800130
Jeremy Kerr17217842016-01-29 18:44:21 +0800131 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800132 if (!tty_device_tty_dir) {
133 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930134 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800135 goto out_free;
136 }
137
138 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930139 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800140 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930141 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800142
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930143 tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
144 if (!tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800145 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930146 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800147
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930148 rc = asprintf(&console->tty.dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930149 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800150 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930151 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800152
Oskar Senft955d1402023-09-27 22:43:22 -0400153 // Default to non-VUART
154 console->tty.type = TTY_DEVICE_UART;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930155
Oskar Senft955d1402023-09-27 22:43:22 -0400156 /* Arbitrarily pick an attribute to differentiate UART vs VUART */
157 if (tty_sysfs_devnode) {
158 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address",
159 tty_sysfs_devnode);
160 if (rc < 0) {
161 goto out_free;
162 }
163
164 rc = access(tty_vuart_lpc_addr, F_OK);
165 if (!rc) {
166 console->tty.type = TTY_DEVICE_VUART;
167 console->tty.vuart.sysfs_devnode =
168 strdup(tty_sysfs_devnode);
169 }
170 }
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930171
Jeremy Kerr17217842016-01-29 18:44:21 +0800172 rc = 0;
173
174out_free:
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930175 free(tty_vuart_lpc_addr);
Jeremy Kerr17217842016-01-29 18:44:21 +0800176 free(tty_class_device_link);
177 free(tty_device_tty_dir);
178 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800179 free(tty_path_input);
180 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800181 return rc;
182}
183
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800184static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930185 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800186{
187 char *path;
188 FILE *fp;
189 int rc;
190
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930191 assert(console->tty.type == TTY_DEVICE_VUART);
192
193 if (!console->tty.vuart.sysfs_devnode) {
194 return -1;
195 }
196
197 rc = asprintf(&path, "%s/%s", console->tty.vuart.sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930198 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800199 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930200 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800201
202 fp = fopen(path, "w");
203 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930204 warn("Can't access attribute %s on device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930205 console->tty.kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800206 rc = -1;
207 goto out_free;
208 }
209 setvbuf(fp, NULL, _IONBF, 0);
210
211 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930212 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930213 warn("Error writing to %s attribute of device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930214 console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930215 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800216 fclose(fp);
217
Jeremy Kerr957818b2016-03-08 14:35:15 +0800218out_free:
219 free(path);
220 return rc;
221}
222
Jeremy Kerrd831f962016-01-29 17:18:01 +0800223/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700224 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800225 */
Ninad Palsulee258e512023-04-26 22:17:57 -0500226void tty_init_termios(struct console *console)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800227{
228 struct termios termios;
229 int rc;
230
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930231 rc = tcgetattr(console->tty.fd, &termios);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800232 if (rc) {
233 warn("Can't read tty termios");
234 return;
235 }
236
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930237 if (console->tty.type == TTY_DEVICE_UART && console->tty.uart.baud) {
238 if (cfsetspeed(&termios, console->tty.uart.baud) < 0) {
239 warn("Couldn't set speeds for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930240 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700241 }
242
243 /* Set console to raw mode: we don't want any processing to occur on
244 * the underlying terminal input/output.
245 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800246 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700247
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930248 rc = tcsetattr(console->tty.fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930249 if (rc) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930250 warn("Can't set terminal options for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930251 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800252}
253
254/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800255 * Open and initialise the serial device
256 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930257static void tty_init_vuart_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800258{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930259 assert(console->tty.type == TTY_DEVICE_VUART);
260
261 if (console->tty.vuart.sirq) {
262 tty_set_sysfs_attr(console, "sirq", console->tty.vuart.sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930263 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800264
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930265 if (console->tty.vuart.lpc_addr) {
266 tty_set_sysfs_attr(console, "lpc_address",
267 console->tty.vuart.lpc_addr);
268 }
269}
270
271static int tty_init_io(struct console *console)
272{
273 console->tty.fd = open(console->tty.dev, O_RDWR);
274 if (console->tty.fd <= 0) {
275 warn("Can't open tty %s", console->tty.dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800276 return -1;
277 }
278
279 /* Disable character delay. We may want to later enable this when
280 * we detect larger amounts of data
281 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930282 fcntl(console->tty.fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800283
Jeremy Kerr54e95692016-03-24 17:07:56 +0800284 tty_init_termios(console);
285
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930286 console->pollfds[console->n_pollers].fd = console->tty.fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800287 console->pollfds[console->n_pollers].events = POLLIN;
288
Jeremy Kerrd831f962016-01-29 17:18:01 +0800289 return 0;
290}
291
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930292static int tty_init_vuart(struct console *console, struct config *config)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800293{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930294 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800295 const char *val;
296 char *endp;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930297
298 assert(console->tty.type == TTY_DEVICE_VUART);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800299
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800300 val = config_get_value(config, "lpc-address");
301 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930302 errno = 0;
303 parsed = strtoul(val, &endp, 0);
304 if (parsed == ULONG_MAX && errno == ERANGE) {
305 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
306 val);
307 return -1;
308 }
309
310 if (parsed > UINT16_MAX) {
311 warn("Invalid LPC address '%s'", val);
312 return -1;
313 }
314
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930315 console->tty.vuart.lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800316 if (endp == optarg) {
317 warn("Invalid LPC address: '%s'", val);
318 return -1;
319 }
320 }
321
322 val = config_get_value(config, "sirq");
323 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930324 errno = 0;
325 parsed = strtoul(val, &endp, 0);
326 if (parsed == ULONG_MAX && errno == ERANGE) {
327 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
328 val);
329 }
330
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930331 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930332 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930333 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930334
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930335 console->tty.vuart.sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930336 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800337 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930338 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800339 }
340
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930341 return 0;
342}
343
344static int tty_init(struct console *console, struct config *config,
345 const char *tty_arg)
346{
347 const char *val;
348 int rc;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700349
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930350 if (tty_arg) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930351 console->tty.kname = tty_arg;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930352 } else if ((val = config_get_value(config, "upstream-tty"))) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930353 console->tty.kname = val;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930354 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800355 warnx("Error: No TTY device specified");
356 return -1;
357 }
358
359 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930360 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800361 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930362 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800363
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930364 switch (console->tty.type) {
365 case TTY_DEVICE_VUART:
366 rc = tty_init_vuart(console, config);
367 if (rc) {
368 return rc;
369 }
370
371 tty_init_vuart_io(console);
372 break;
373 case TTY_DEVICE_UART:
374 val = config_get_value(config, "baud");
375 if (val) {
376 if (config_parse_baud(&console->tty.uart.baud, val)) {
377 warnx("Invalid baud rate: '%s'", val);
378 }
379 }
380 break;
381 case TTY_DEVICE_PTY:
382 break;
383 case TTY_DEVICE_UNDEFINED:
384 default:
385 warnx("Cannot configure unrecognised TTY device");
386 return -1;
387 }
388
389 return tty_init_io(console);
390}
391
392static void tty_fini(struct console *console)
393{
394 if (console->tty.type == TTY_DEVICE_VUART) {
395 free(console->tty.vuart.sysfs_devnode);
396 }
397 free(console->tty.dev);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800398}
399
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700400static int write_to_path(const char *path, const char *data)
401{
402 int rc = 0;
403 FILE *f = fopen(path, "w");
404 if (!f) {
405 return -1;
406 }
407
408 if (fprintf(f, "%s", data) < 0) {
409 rc = -1;
410 }
411
412 if (fclose(f)) {
413 rc = -1;
414 }
415
416 return rc;
417}
418
419#define ASPEED_UART_ROUTING_PATTERN \
420 "/sys/bus/platform/drivers/aspeed-uart-routing/*.uart-routing"
421
422static void uart_routing_init(struct config *config)
423{
424 const char *muxcfg;
425 const char *p;
426 size_t buflen;
427 char *sink;
428 char *source;
429 char *muxdir;
430 char *path;
431 glob_t globbuf;
432
433 muxcfg = config_get_value(config, "aspeed-uart-routing");
434 if (!muxcfg) {
435 return;
436 }
437
438 /* Find the driver's sysfs directory */
439 if (glob(ASPEED_UART_ROUTING_PATTERN, GLOB_ERR | GLOB_NOSORT, NULL,
440 &globbuf) != 0) {
441 warn("Couldn't find uart-routing driver directory, cannot apply config");
442 return;
443 }
444 if (globbuf.gl_pathc != 1) {
445 warnx("Found %zd uart-routing driver directories, cannot apply config",
446 globbuf.gl_pathc);
447 goto out_free_glob;
448 }
449 muxdir = globbuf.gl_pathv[0];
450
451 /*
452 * Rather than faff about tracking a bunch of separate buffer sizes,
453 * just use one (worst-case) size for all of them -- +2 for a trailing
454 * NUL and a '/' separator to construct the sysfs file path.
455 */
456 buflen = strlen(muxdir) + strlen(muxcfg) + 2;
457
458 sink = malloc(buflen);
459 source = malloc(buflen);
460 path = malloc(buflen);
461 if (!path || !sink || !source) {
462 warnx("Out of memory applying uart routing config");
463 goto out_free_bufs;
464 }
465
466 p = muxcfg;
467 while (*p) {
468 ssize_t bytes_scanned;
469
470 if (sscanf(p, " %[^:/ \t]:%[^: \t] %zn", sink, source,
471 &bytes_scanned) != 2) {
472 warnx("Invalid syntax in aspeed uart config: '%s' not applied",
473 p);
474 break;
475 }
476 p += bytes_scanned;
477
478 /*
479 * Check that the sink name looks reasonable before proceeding
480 * (there are other writable files in the same directory that
481 * we shouldn't be touching, such as 'driver_override' and
482 * 'uevent').
483 */
484 if (strncmp(sink, "io", strlen("io")) != 0 &&
485 strncmp(sink, "uart", strlen("uart")) != 0) {
486 warnx("Skipping invalid uart routing name '%s' (must be ioN or uartN)",
487 sink);
488 continue;
489 }
490
491 snprintf(path, buflen, "%s/%s", muxdir, sink);
492 if (write_to_path(path, source)) {
493 warn("Failed to apply uart-routing config '%s:%s'",
494 sink, source);
495 }
496 }
497
498out_free_bufs:
499 free(path);
500 free(source);
501 free(sink);
502out_free_glob:
503 globfree(&globbuf);
504}
505
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800506int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800507{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930508 return write_buf_to_fd(console->tty.fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800509}
510
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500511/* Prepare a socket name */
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930512static int set_socket_info(struct console *console, struct config *config,
513 const char *console_id)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500514{
515 ssize_t len;
516
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500517 /* Get console id */
518 console->console_id = config_resolve_console_id(config, console_id);
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930519
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500520 /* Get the socket name/path */
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500521 len = console_socket_path(console->socket_name, console->console_id);
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500522 if (len < 0) {
523 warn("Failed to set socket path: %s", strerror(errno));
524 return EXIT_FAILURE;
525 }
526
527 /* Socket name is not a null terminated string hence save the length */
528 console->socket_name_len = len;
529
530 return 0;
531}
532
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800533static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800534{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930535 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
536 extern struct handler *__start_handlers;
537 extern struct handler *__stop_handlers;
538 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800539 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930540 int i;
541 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800542
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800543 console->n_handlers = &__stop_handlers - &__start_handlers;
544 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800545
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930546 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930547 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800548
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800549 for (i = 0; i < console->n_handlers; i++) {
550 handler = console->handlers[i];
551
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800552 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930553 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800554 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930555 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800556
557 handler->active = rc == 0;
558
559 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930560 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800561 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800562}
563
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800564static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800565{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800566 struct handler *handler;
567 int i;
568
569 for (i = 0; i < console->n_handlers; i++) {
570 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930571 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800572 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930573 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800574 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800575}
576
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800577static int get_current_time(struct timeval *tv)
578{
579 struct timespec t;
580 int rc;
581
582 /*
583 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
584 * local time changes. However, a struct timeval is more
585 * convenient for calculations, so convert to that.
586 */
587 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930588 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800589 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930590 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800591
592 tv->tv_sec = t.tv_sec;
593 tv->tv_usec = t.tv_nsec / 1000;
594
595 return 0;
596}
597
Andrew Jefferya72711a2023-04-18 18:19:41 +0930598struct ringbuffer_consumer *
599console_ringbuffer_consumer_register(struct console *console,
600 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800601{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800602 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800603}
604
Jeremy Kerr55c97122017-02-07 17:06:46 +0800605struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930606 struct handler *handler,
607 poller_event_fn_t poller_fn,
608 poller_timeout_fn_t timeout_fn, int fd,
609 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800610{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800611 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930612 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800613
614 poller = malloc(sizeof(*poller));
615 poller->remove = false;
616 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800617 poller->event_fn = poller_fn;
618 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800619 poller->data = data;
620
621 /* add one to our pollers array */
622 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930623 /*
624 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
625 * pointer type.
626 */
627 /* NOLINTBEGIN(bugprone-sizeof-expression) */
628 console->pollers = reallocarray(console->pollers, console->n_pollers,
629 sizeof(*console->pollers));
630 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800631
632 console->pollers[n] = poller;
633
634 /* increase pollfds array too */
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930635 console->pollfds = reallocarray(
636 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
637 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800638
639 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930640 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
641 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800642
643 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930644 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800645
646 return poller;
647}
648
Andrew Jefferya72711a2023-04-18 18:19:41 +0930649void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800650{
651 int i;
652
653 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930654 for (i = 0; i < console->n_pollers; i++) {
655 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800656 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930657 }
658 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800659
660 assert(i < console->n_pollers);
661
662 console->n_pollers--;
663
Andrew Jeffery91b52172023-04-19 12:42:14 +0930664 /*
665 * Remove the item from the pollers array...
666 *
667 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
668 * pointer type.
669 */
670 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930671 memmove(&console->pollers[i], &console->pollers[i + 1],
672 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800673
Andrew Jeffery91b52172023-04-19 12:42:14 +0930674 console->pollers = reallocarray(console->pollers, console->n_pollers,
675 sizeof(*console->pollers));
676 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800677
678 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930679 memmove(&console->pollfds[i], &console->pollfds[i + 1],
680 sizeof(*console->pollfds) *
681 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800682
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930683 console->pollfds = reallocarray(
684 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
685 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800686
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800687 free(poller);
688}
689
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800690void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930691 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800692{
693 int i;
694
695 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930696 for (i = 0; i < console->n_pollers; i++) {
697 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800698 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930699 }
700 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800701
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930702 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800703}
704
Andrew Jefferyfd048322023-04-18 12:02:01 +0930705void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930706 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800707{
708 struct timeval now;
709 int rc;
710
711 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930712 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800713 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930714 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800715
716 timeradd(&now, tv, &poller->timeout);
717}
718
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930719static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800720{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930721 struct timeval *earliest;
722 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800723 struct poller *poller;
724 int i;
725
726 earliest = NULL;
727
728 for (i = 0; i < console->n_pollers; i++) {
729 poller = console->pollers[i];
730
731 if (poller->timeout_fn && timerisset(&poller->timeout) &&
732 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930733 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800734 // poller is buffering data and needs the poll
735 // function to timeout.
736 earliest = &poller->timeout;
737 }
738 }
739
740 if (earliest) {
741 if (timercmp(earliest, cur_time, >)) {
742 /* recalculate the timeout period, time period has
743 * not elapsed */
744 timersub(earliest, cur_time, &interval);
745 return ((interval.tv_sec * 1000) +
746 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930747 } /* return from poll immediately */
748 return 0;
749
750 } /* poll indefinitely */
751 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800752}
753
754static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800755{
756 struct poller *poller;
757 struct pollfd *pollfd;
758 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930759 int i;
760 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800761
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800762 rc = 0;
763
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800764 /*
765 * Process poll events by iterating through the pollers and pollfds
766 * in-step, calling any pollers that we've found revents for.
767 */
768 for (i = 0; i < console->n_pollers; i++) {
769 poller = console->pollers[i];
770 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800771 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800772
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800773 /* process pending events... */
774 if (pollfd->revents) {
775 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930776 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930777 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800778 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930779 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800780 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930781 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800782 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800783
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800784 if ((prc == POLLER_OK) && poller->timeout_fn &&
785 timerisset(&poller->timeout) &&
786 timercmp(&poller->timeout, cur_time, <=)) {
787 /* One of the ringbuffer consumers is buffering the
788 data stream. The amount of idle time the consumer
789 desired has expired. Process the buffered data for
790 transmission. */
791 timerclear(&poller->timeout);
792 prc = poller->timeout_fn(poller->handler, poller->data);
793 if (prc == POLLER_EXIT) {
794 rc = -1;
795 } else if (prc == POLLER_REMOVE) {
796 poller->remove = true;
797 }
798 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800799 }
800
801 /**
802 * Process deferred removals; restarting each time we unregister, as
803 * the array will have changed
804 */
805 for (;;) {
806 bool removed = false;
807
808 for (i = 0; i < console->n_pollers; i++) {
809 poller = console->pollers[i];
810 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800811 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800812 removed = true;
813 break;
814 }
815 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930816 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800817 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930818 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800819 }
820
821 return rc;
822}
823
Jeremy Kerr769cee12016-03-15 17:53:56 +0800824static void sighandler(int signal)
825{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930826 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800827 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930828 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800829}
830
Alexander Hansen69257402024-06-07 15:17:09 +0200831static int run_console_iteration(struct console *console)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800832{
Alexander Hansen69257402024-06-07 15:17:09 +0200833 uint8_t buf[4096];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800834 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930835 long timeout;
836 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800837
Alexander Hansen69257402024-06-07 15:17:09 +0200838 if (console->rb->size < sizeof(buf)) {
839 fprintf(stderr, "Ringbuffer size should be greater than %zuB\n",
840 sizeof(buf));
841 return -1;
842 }
843
844 if (sigint) {
845 fprintf(stderr, "Received interrupt, exiting\n");
846 return -1;
847 }
848
849 rc = get_current_time(&tv);
850 if (rc) {
851 warn("Failed to read current time");
852 return -1;
853 }
854
855 timeout = get_poll_timeout(console, &tv);
856
857 rc = poll(console->pollfds, console->n_pollers + MAX_INTERNAL_POLLFD,
858 (int)timeout);
859
860 if (rc < 0) {
861 if (errno == EINTR) {
862 return 0;
863 }
864 warn("poll error");
865 return -1;
866 }
867
868 /* process internal fd first */
869 if (console->pollfds[console->n_pollers].revents) {
870 rc = read(console->tty.fd, buf, sizeof(buf));
871 if (rc <= 0) {
872 warn("Error reading from tty device");
873 return -1;
874 }
875 rc = ringbuffer_queue(console->rb, buf, rc);
876 if (rc) {
877 return -1;
878 }
879 }
880
881 if (console->pollfds[console->n_pollers + 1].revents) {
882 sd_bus_process(console->bus, NULL);
883 }
884
885 /* ... and then the pollers */
886 rc = call_pollers(console, &tv);
887 if (rc) {
888 return -1;
889 }
890 return 0;
891}
892
893int run_console(struct console *console)
894{
895 sighandler_t sighandler_save = signal(SIGINT, sighandler);
896 ssize_t rc = 0;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800897
Jeremy Kerrd831f962016-01-29 17:18:01 +0800898 for (;;) {
Alexander Hansen69257402024-06-07 15:17:09 +0200899 rc = run_console_iteration(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930900 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800901 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930902 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800903 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800904
905 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800906 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800907
908 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800909}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800910static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930911 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930912 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +0930913 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800914};
915
916int main(int argc, char **argv)
917{
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700918 size_t buffer_size = default_buffer_size;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800919 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500920 const char *config_tty_kname = NULL;
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700921 const char *buffer_size_str = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930922 const char *console_id = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800923 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800924 struct config *config;
925 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800926
Jeremy Kerrd831f962016-01-29 17:18:01 +0800927 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930928 int c;
929 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800930
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930931 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930932 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800933 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930934 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800935
936 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800937 case 'c':
938 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800939 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930940 case 'i':
941 console_id = optarg;
942 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800943 case 'h':
944 case '?':
945 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800946 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800947 }
948 }
949
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930950 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030951 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930952 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500953
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700954 config = config_init(config_filename);
955
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800956 console = malloc(sizeof(struct console));
957 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930958 console->pollfds =
959 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700960 buffer_size_str = config_get_value(config, "ringbuffer-size");
961 if (buffer_size_str) {
962 rc = config_parse_bytesize(buffer_size_str, &buffer_size);
963 if (rc) {
964 warn("Invalid ringbuffer-size. Default to %zukB",
965 buffer_size >> 10);
966 }
967 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800968 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800969
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930970 if (set_socket_info(console, config, console_id)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930971 rc = -1;
972 goto out_config_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500973 }
974
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700975 uart_routing_init(config);
976
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930977 rc = tty_init(console, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930978 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800979 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930980 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800981
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800982 dbus_init(console, config);
983
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800984 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800985
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800986 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800987
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800988 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800989
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930990 tty_fini(console);
991
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800992out_config_fini:
993 config_fini(config);
994
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800995 free(console->pollers);
996 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800997 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800998
999 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1000}