blob: 4ff3a4e27551c89151b624f62d5928a5f2d00536 [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) */
Jeremy Kerre2826c72024-07-05 10:54:21 +0800536 extern const struct handler_type *const __start_handlers;
537 extern const struct handler_type *const __stop_handlers;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930538 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerre2826c72024-07-05 10:54:21 +0800539 size_t n_types;
540 int j = 0;
541 size_t i;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800542
Jeremy Kerre2826c72024-07-05 10:54:21 +0800543 n_types = &__stop_handlers - &__start_handlers;
544 console->handlers = calloc(n_types, sizeof(struct handler *));
545 if (!console->handlers) {
546 err(EXIT_FAILURE, "malloc(handlers)");
547 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800548
Jeremy Kerre2826c72024-07-05 10:54:21 +0800549 printf("%ld handler type%s\n", n_types, n_types == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800550
Jeremy Kerre2826c72024-07-05 10:54:21 +0800551 for (i = 0; i < n_types; i++) {
552 const struct handler_type *type = &__start_handlers[i];
553 struct handler *handler;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800554
Jeremy Kerre2826c72024-07-05 10:54:21 +0800555 /* Should be picked up at build time by
556 * console_handler_register, but check anyway
557 */
558 if (!type->init || !type->fini) {
559 errx(EXIT_FAILURE,
560 "invalid handler type %s: no init() / fini()",
561 type->name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930562 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800563
Jeremy Kerre2826c72024-07-05 10:54:21 +0800564 handler = type->init(type, console, config);
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800565
Jeremy Kerre2826c72024-07-05 10:54:21 +0800566 printf(" console '%s': handler %s [%sactive]\n",
567 console->console_id, type->name, handler ? "" : "in");
568
569 if (handler) {
570 handler->type = type;
571 console->handlers[j++] = handler;
572 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800573 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800574
575 console->n_handlers = j;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800576}
577
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800578static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800579{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800580 struct handler *handler;
581 int i;
582
583 for (i = 0; i < console->n_handlers; i++) {
584 handler = console->handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +0800585 handler->type->fini(handler);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800586 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800587
588 free(console->handlers);
589 console->handlers = NULL;
590 console->n_handlers = 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800591}
592
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800593static int get_current_time(struct timeval *tv)
594{
595 struct timespec t;
596 int rc;
597
598 /*
599 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
600 * local time changes. However, a struct timeval is more
601 * convenient for calculations, so convert to that.
602 */
603 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930604 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800605 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930606 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800607
608 tv->tv_sec = t.tv_sec;
609 tv->tv_usec = t.tv_nsec / 1000;
610
611 return 0;
612}
613
Andrew Jefferya72711a2023-04-18 18:19:41 +0930614struct ringbuffer_consumer *
615console_ringbuffer_consumer_register(struct console *console,
616 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800617{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800618 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800619}
620
Jeremy Kerr55c97122017-02-07 17:06:46 +0800621struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930622 struct handler *handler,
623 poller_event_fn_t poller_fn,
624 poller_timeout_fn_t timeout_fn, int fd,
625 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800626{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800627 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930628 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800629
630 poller = malloc(sizeof(*poller));
631 poller->remove = false;
632 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800633 poller->event_fn = poller_fn;
634 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800635 poller->data = data;
636
637 /* add one to our pollers array */
638 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930639 /*
640 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
641 * pointer type.
642 */
643 /* NOLINTBEGIN(bugprone-sizeof-expression) */
644 console->pollers = reallocarray(console->pollers, console->n_pollers,
645 sizeof(*console->pollers));
646 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800647
648 console->pollers[n] = poller;
649
650 /* increase pollfds array too */
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930651 console->pollfds = reallocarray(
652 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
653 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800654
655 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930656 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
657 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800658
659 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930660 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800661
662 return poller;
663}
664
Andrew Jefferya72711a2023-04-18 18:19:41 +0930665void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800666{
667 int i;
668
669 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930670 for (i = 0; i < console->n_pollers; i++) {
671 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800672 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930673 }
674 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800675
676 assert(i < console->n_pollers);
677
678 console->n_pollers--;
679
Andrew Jeffery91b52172023-04-19 12:42:14 +0930680 /*
681 * Remove the item from the pollers array...
682 *
683 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
684 * pointer type.
685 */
686 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930687 memmove(&console->pollers[i], &console->pollers[i + 1],
688 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800689
Andrew Jeffery91b52172023-04-19 12:42:14 +0930690 console->pollers = reallocarray(console->pollers, console->n_pollers,
691 sizeof(*console->pollers));
692 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800693
694 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930695 memmove(&console->pollfds[i], &console->pollfds[i + 1],
696 sizeof(*console->pollfds) *
697 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800698
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930699 console->pollfds = reallocarray(
700 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
701 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800702
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800703 free(poller);
704}
705
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800706void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930707 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800708{
709 int i;
710
711 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930712 for (i = 0; i < console->n_pollers; i++) {
713 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800714 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930715 }
716 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800717
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930718 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800719}
720
Andrew Jefferyfd048322023-04-18 12:02:01 +0930721void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930722 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800723{
724 struct timeval now;
725 int rc;
726
727 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930728 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800729 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930730 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800731
732 timeradd(&now, tv, &poller->timeout);
733}
734
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930735static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800736{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930737 struct timeval *earliest;
738 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800739 struct poller *poller;
740 int i;
741
742 earliest = NULL;
743
744 for (i = 0; i < console->n_pollers; i++) {
745 poller = console->pollers[i];
746
747 if (poller->timeout_fn && timerisset(&poller->timeout) &&
748 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930749 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800750 // poller is buffering data and needs the poll
751 // function to timeout.
752 earliest = &poller->timeout;
753 }
754 }
755
756 if (earliest) {
757 if (timercmp(earliest, cur_time, >)) {
758 /* recalculate the timeout period, time period has
759 * not elapsed */
760 timersub(earliest, cur_time, &interval);
761 return ((interval.tv_sec * 1000) +
762 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930763 } /* return from poll immediately */
764 return 0;
765
766 } /* poll indefinitely */
767 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800768}
769
770static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800771{
772 struct poller *poller;
773 struct pollfd *pollfd;
774 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930775 int i;
776 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800777
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800778 rc = 0;
779
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800780 /*
781 * Process poll events by iterating through the pollers and pollfds
782 * in-step, calling any pollers that we've found revents for.
783 */
784 for (i = 0; i < console->n_pollers; i++) {
785 poller = console->pollers[i];
786 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800787 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800788
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800789 /* process pending events... */
790 if (pollfd->revents) {
791 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930792 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930793 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800794 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930795 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800796 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930797 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800798 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800799
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800800 if ((prc == POLLER_OK) && poller->timeout_fn &&
801 timerisset(&poller->timeout) &&
802 timercmp(&poller->timeout, cur_time, <=)) {
803 /* One of the ringbuffer consumers is buffering the
804 data stream. The amount of idle time the consumer
805 desired has expired. Process the buffered data for
806 transmission. */
807 timerclear(&poller->timeout);
808 prc = poller->timeout_fn(poller->handler, poller->data);
809 if (prc == POLLER_EXIT) {
810 rc = -1;
811 } else if (prc == POLLER_REMOVE) {
812 poller->remove = true;
813 }
814 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800815 }
816
817 /**
818 * Process deferred removals; restarting each time we unregister, as
819 * the array will have changed
820 */
821 for (;;) {
822 bool removed = false;
823
824 for (i = 0; i < console->n_pollers; i++) {
825 poller = console->pollers[i];
826 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800827 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800828 removed = true;
829 break;
830 }
831 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930832 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800833 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930834 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800835 }
836
837 return rc;
838}
839
Jeremy Kerr769cee12016-03-15 17:53:56 +0800840static void sighandler(int signal)
841{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930842 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800843 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930844 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800845}
846
Alexander Hansen69257402024-06-07 15:17:09 +0200847static int run_console_iteration(struct console *console)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800848{
Alexander Hansen69257402024-06-07 15:17:09 +0200849 uint8_t buf[4096];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800850 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930851 long timeout;
852 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800853
Alexander Hansen69257402024-06-07 15:17:09 +0200854 if (console->rb->size < sizeof(buf)) {
855 fprintf(stderr, "Ringbuffer size should be greater than %zuB\n",
856 sizeof(buf));
857 return -1;
858 }
859
860 if (sigint) {
861 fprintf(stderr, "Received interrupt, exiting\n");
862 return -1;
863 }
864
865 rc = get_current_time(&tv);
866 if (rc) {
867 warn("Failed to read current time");
868 return -1;
869 }
870
871 timeout = get_poll_timeout(console, &tv);
872
873 rc = poll(console->pollfds, console->n_pollers + MAX_INTERNAL_POLLFD,
874 (int)timeout);
875
876 if (rc < 0) {
877 if (errno == EINTR) {
878 return 0;
879 }
880 warn("poll error");
881 return -1;
882 }
883
884 /* process internal fd first */
885 if (console->pollfds[console->n_pollers].revents) {
886 rc = read(console->tty.fd, buf, sizeof(buf));
887 if (rc <= 0) {
888 warn("Error reading from tty device");
889 return -1;
890 }
891 rc = ringbuffer_queue(console->rb, buf, rc);
892 if (rc) {
893 return -1;
894 }
895 }
896
897 if (console->pollfds[console->n_pollers + 1].revents) {
898 sd_bus_process(console->bus, NULL);
899 }
900
901 /* ... and then the pollers */
902 rc = call_pollers(console, &tv);
903 if (rc) {
904 return -1;
905 }
906 return 0;
907}
908
909int run_console(struct console *console)
910{
911 sighandler_t sighandler_save = signal(SIGINT, sighandler);
912 ssize_t rc = 0;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800913
Jeremy Kerrd831f962016-01-29 17:18:01 +0800914 for (;;) {
Alexander Hansen69257402024-06-07 15:17:09 +0200915 rc = run_console_iteration(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930916 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800917 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930918 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800919 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800920
921 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800922 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800923
924 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800925}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800926static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930927 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930928 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +0930929 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800930};
931
932int main(int argc, char **argv)
933{
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700934 size_t buffer_size = default_buffer_size;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800935 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500936 const char *config_tty_kname = NULL;
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700937 const char *buffer_size_str = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930938 const char *console_id = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800939 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800940 struct config *config;
941 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800942
Jeremy Kerrd831f962016-01-29 17:18:01 +0800943 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930944 int c;
945 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800946
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930947 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930948 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800949 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930950 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800951
952 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800953 case 'c':
954 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800955 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930956 case 'i':
957 console_id = optarg;
958 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800959 case 'h':
960 case '?':
961 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800962 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800963 }
964 }
965
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930966 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030967 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930968 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500969
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700970 config = config_init(config_filename);
971
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800972 console = malloc(sizeof(struct console));
973 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930974 console->pollfds =
975 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700976 buffer_size_str = config_get_value(config, "ringbuffer-size");
977 if (buffer_size_str) {
978 rc = config_parse_bytesize(buffer_size_str, &buffer_size);
979 if (rc) {
980 warn("Invalid ringbuffer-size. Default to %zukB",
981 buffer_size >> 10);
982 }
983 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800984 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800985
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930986 if (set_socket_info(console, config, console_id)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930987 rc = -1;
988 goto out_config_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500989 }
990
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700991 uart_routing_init(config);
992
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930993 rc = tty_init(console, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930994 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800995 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930996 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800997
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800998 dbus_init(console, config);
999
Jeremy Kerrd47963e2016-03-16 17:29:55 +08001000 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001001
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001002 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001003
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001004 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001005
Andrew Jeffery30ea6382023-05-03 16:56:10 +09301006 tty_fini(console);
1007
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001008out_config_fini:
1009 config_fini(config);
1010
Jeremy Kerr89ea8192016-03-15 17:57:43 +08001011 free(console->pollers);
1012 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001013 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001014
1015 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1016}