blob: 57155897975d2ad8a49f3332ab86e5d0053a3ffc [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
Jeremy Kerrf733c852017-02-07 18:40:10 +080045/* size of the shared backlog ringbuffer */
Andrew Jeffery5db8c792023-04-18 21:48:24 +093046const size_t 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
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930153 /* Arbitrarily pick an attribute to differentiate UART vs VUART */
Andrew Jeffery7c02ae12023-06-05 18:45:38 +0930154 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address", tty_sysfs_devnode);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930155 if (rc < 0) {
156 goto out_free;
157 }
158
159 rc = access(tty_vuart_lpc_addr, F_OK);
160 console->tty.type = (!rc) ? TTY_DEVICE_VUART : TTY_DEVICE_UART;
161
Jeremy Kerr17217842016-01-29 18:44:21 +0800162 rc = 0;
163
164out_free:
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930165 free(tty_vuart_lpc_addr);
Jeremy Kerr17217842016-01-29 18:44:21 +0800166 free(tty_class_device_link);
167 free(tty_device_tty_dir);
168 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800169 free(tty_path_input);
170 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800171 return rc;
172}
173
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800174static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930175 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800176{
177 char *path;
178 FILE *fp;
179 int rc;
180
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930181 assert(console->tty.type == TTY_DEVICE_VUART);
182
183 if (!console->tty.vuart.sysfs_devnode) {
184 return -1;
185 }
186
187 rc = asprintf(&path, "%s/%s", console->tty.vuart.sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930188 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800189 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930190 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800191
192 fp = fopen(path, "w");
193 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930194 warn("Can't access attribute %s on device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930195 console->tty.kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800196 rc = -1;
197 goto out_free;
198 }
199 setvbuf(fp, NULL, _IONBF, 0);
200
201 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930202 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930203 warn("Error writing to %s attribute of device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930204 console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930205 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800206 fclose(fp);
207
Jeremy Kerr957818b2016-03-08 14:35:15 +0800208out_free:
209 free(path);
210 return rc;
211}
212
Jeremy Kerrd831f962016-01-29 17:18:01 +0800213/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700214 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800215 */
Ninad Palsulee258e512023-04-26 22:17:57 -0500216void tty_init_termios(struct console *console)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800217{
218 struct termios termios;
219 int rc;
220
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930221 rc = tcgetattr(console->tty.fd, &termios);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800222 if (rc) {
223 warn("Can't read tty termios");
224 return;
225 }
226
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930227 if (console->tty.type == TTY_DEVICE_UART && console->tty.uart.baud) {
228 if (cfsetspeed(&termios, console->tty.uart.baud) < 0) {
229 warn("Couldn't set speeds for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930230 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700231 }
232
233 /* Set console to raw mode: we don't want any processing to occur on
234 * the underlying terminal input/output.
235 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800236 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700237
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930238 rc = tcsetattr(console->tty.fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930239 if (rc) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930240 warn("Can't set terminal options for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930241 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800242}
243
244/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800245 * Open and initialise the serial device
246 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930247static void tty_init_vuart_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800248{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930249 assert(console->tty.type == TTY_DEVICE_VUART);
250
251 if (console->tty.vuart.sirq) {
252 tty_set_sysfs_attr(console, "sirq", console->tty.vuart.sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930253 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800254
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930255 if (console->tty.vuart.lpc_addr) {
256 tty_set_sysfs_attr(console, "lpc_address",
257 console->tty.vuart.lpc_addr);
258 }
259}
260
261static int tty_init_io(struct console *console)
262{
263 console->tty.fd = open(console->tty.dev, O_RDWR);
264 if (console->tty.fd <= 0) {
265 warn("Can't open tty %s", console->tty.dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800266 return -1;
267 }
268
269 /* Disable character delay. We may want to later enable this when
270 * we detect larger amounts of data
271 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930272 fcntl(console->tty.fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800273
Jeremy Kerr54e95692016-03-24 17:07:56 +0800274 tty_init_termios(console);
275
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930276 console->pollfds[console->n_pollers].fd = console->tty.fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800277 console->pollfds[console->n_pollers].events = POLLIN;
278
Jeremy Kerrd831f962016-01-29 17:18:01 +0800279 return 0;
280}
281
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930282static int tty_init_vuart(struct console *console, struct config *config)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800283{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930284 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800285 const char *val;
286 char *endp;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930287
288 assert(console->tty.type == TTY_DEVICE_VUART);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800289
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800290 val = config_get_value(config, "lpc-address");
291 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930292 errno = 0;
293 parsed = strtoul(val, &endp, 0);
294 if (parsed == ULONG_MAX && errno == ERANGE) {
295 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
296 val);
297 return -1;
298 }
299
300 if (parsed > UINT16_MAX) {
301 warn("Invalid LPC address '%s'", val);
302 return -1;
303 }
304
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930305 console->tty.vuart.lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800306 if (endp == optarg) {
307 warn("Invalid LPC address: '%s'", val);
308 return -1;
309 }
310 }
311
312 val = config_get_value(config, "sirq");
313 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930314 errno = 0;
315 parsed = strtoul(val, &endp, 0);
316 if (parsed == ULONG_MAX && errno == ERANGE) {
317 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
318 val);
319 }
320
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930321 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930322 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930323 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930324
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930325 console->tty.vuart.sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930326 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800327 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930328 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800329 }
330
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930331 return 0;
332}
333
334static int tty_init(struct console *console, struct config *config,
335 const char *tty_arg)
336{
337 const char *val;
338 int rc;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700339
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930340 if (tty_arg) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930341 console->tty.kname = tty_arg;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930342 } else if ((val = config_get_value(config, "upstream-tty"))) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930343 console->tty.kname = val;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930344 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800345 warnx("Error: No TTY device specified");
346 return -1;
347 }
348
349 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930350 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800351 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930352 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800353
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930354 switch (console->tty.type) {
355 case TTY_DEVICE_VUART:
356 rc = tty_init_vuart(console, config);
357 if (rc) {
358 return rc;
359 }
360
361 tty_init_vuart_io(console);
362 break;
363 case TTY_DEVICE_UART:
364 val = config_get_value(config, "baud");
365 if (val) {
366 if (config_parse_baud(&console->tty.uart.baud, val)) {
367 warnx("Invalid baud rate: '%s'", val);
368 }
369 }
370 break;
371 case TTY_DEVICE_PTY:
372 break;
373 case TTY_DEVICE_UNDEFINED:
374 default:
375 warnx("Cannot configure unrecognised TTY device");
376 return -1;
377 }
378
379 return tty_init_io(console);
380}
381
382static void tty_fini(struct console *console)
383{
384 if (console->tty.type == TTY_DEVICE_VUART) {
385 free(console->tty.vuart.sysfs_devnode);
386 }
387 free(console->tty.dev);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800388}
389
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700390static int write_to_path(const char *path, const char *data)
391{
392 int rc = 0;
393 FILE *f = fopen(path, "w");
394 if (!f) {
395 return -1;
396 }
397
398 if (fprintf(f, "%s", data) < 0) {
399 rc = -1;
400 }
401
402 if (fclose(f)) {
403 rc = -1;
404 }
405
406 return rc;
407}
408
409#define ASPEED_UART_ROUTING_PATTERN \
410 "/sys/bus/platform/drivers/aspeed-uart-routing/*.uart-routing"
411
412static void uart_routing_init(struct config *config)
413{
414 const char *muxcfg;
415 const char *p;
416 size_t buflen;
417 char *sink;
418 char *source;
419 char *muxdir;
420 char *path;
421 glob_t globbuf;
422
423 muxcfg = config_get_value(config, "aspeed-uart-routing");
424 if (!muxcfg) {
425 return;
426 }
427
428 /* Find the driver's sysfs directory */
429 if (glob(ASPEED_UART_ROUTING_PATTERN, GLOB_ERR | GLOB_NOSORT, NULL,
430 &globbuf) != 0) {
431 warn("Couldn't find uart-routing driver directory, cannot apply config");
432 return;
433 }
434 if (globbuf.gl_pathc != 1) {
435 warnx("Found %zd uart-routing driver directories, cannot apply config",
436 globbuf.gl_pathc);
437 goto out_free_glob;
438 }
439 muxdir = globbuf.gl_pathv[0];
440
441 /*
442 * Rather than faff about tracking a bunch of separate buffer sizes,
443 * just use one (worst-case) size for all of them -- +2 for a trailing
444 * NUL and a '/' separator to construct the sysfs file path.
445 */
446 buflen = strlen(muxdir) + strlen(muxcfg) + 2;
447
448 sink = malloc(buflen);
449 source = malloc(buflen);
450 path = malloc(buflen);
451 if (!path || !sink || !source) {
452 warnx("Out of memory applying uart routing config");
453 goto out_free_bufs;
454 }
455
456 p = muxcfg;
457 while (*p) {
458 ssize_t bytes_scanned;
459
460 if (sscanf(p, " %[^:/ \t]:%[^: \t] %zn", sink, source,
461 &bytes_scanned) != 2) {
462 warnx("Invalid syntax in aspeed uart config: '%s' not applied",
463 p);
464 break;
465 }
466 p += bytes_scanned;
467
468 /*
469 * Check that the sink name looks reasonable before proceeding
470 * (there are other writable files in the same directory that
471 * we shouldn't be touching, such as 'driver_override' and
472 * 'uevent').
473 */
474 if (strncmp(sink, "io", strlen("io")) != 0 &&
475 strncmp(sink, "uart", strlen("uart")) != 0) {
476 warnx("Skipping invalid uart routing name '%s' (must be ioN or uartN)",
477 sink);
478 continue;
479 }
480
481 snprintf(path, buflen, "%s/%s", muxdir, sink);
482 if (write_to_path(path, source)) {
483 warn("Failed to apply uart-routing config '%s:%s'",
484 sink, source);
485 }
486 }
487
488out_free_bufs:
489 free(path);
490 free(source);
491 free(sink);
492out_free_glob:
493 globfree(&globbuf);
494}
495
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800496int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800497{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930498 return write_buf_to_fd(console->tty.fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800499}
500
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500501/* Prepare a socket name */
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930502static int set_socket_info(struct console *console, struct config *config,
503 const char *console_id)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500504{
505 ssize_t len;
506
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500507 /* Get console id */
508 console->console_id = config_resolve_console_id(config, console_id);
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930509
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500510 /* Get the socket name/path */
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500511 len = console_socket_path(console->socket_name, console->console_id);
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500512 if (len < 0) {
513 warn("Failed to set socket path: %s", strerror(errno));
514 return EXIT_FAILURE;
515 }
516
517 /* Socket name is not a null terminated string hence save the length */
518 console->socket_name_len = len;
519
520 return 0;
521}
522
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800523static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800524{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930525 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
526 extern struct handler *__start_handlers;
527 extern struct handler *__stop_handlers;
528 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800529 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930530 int i;
531 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800532
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800533 console->n_handlers = &__stop_handlers - &__start_handlers;
534 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800535
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930536 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930537 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800538
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800539 for (i = 0; i < console->n_handlers; i++) {
540 handler = console->handlers[i];
541
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800542 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930543 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800544 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930545 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800546
547 handler->active = rc == 0;
548
549 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930550 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800551 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800552}
553
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800554static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800555{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800556 struct handler *handler;
557 int i;
558
559 for (i = 0; i < console->n_handlers; i++) {
560 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930561 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800562 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930563 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800564 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800565}
566
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800567static int get_current_time(struct timeval *tv)
568{
569 struct timespec t;
570 int rc;
571
572 /*
573 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
574 * local time changes. However, a struct timeval is more
575 * convenient for calculations, so convert to that.
576 */
577 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930578 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800579 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930580 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800581
582 tv->tv_sec = t.tv_sec;
583 tv->tv_usec = t.tv_nsec / 1000;
584
585 return 0;
586}
587
Andrew Jefferya72711a2023-04-18 18:19:41 +0930588struct ringbuffer_consumer *
589console_ringbuffer_consumer_register(struct console *console,
590 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800591{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800592 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800593}
594
Jeremy Kerr55c97122017-02-07 17:06:46 +0800595struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930596 struct handler *handler,
597 poller_event_fn_t poller_fn,
598 poller_timeout_fn_t timeout_fn, int fd,
599 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800600{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800601 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930602 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800603
604 poller = malloc(sizeof(*poller));
605 poller->remove = false;
606 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800607 poller->event_fn = poller_fn;
608 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800609 poller->data = data;
610
611 /* add one to our pollers array */
612 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930613 /*
614 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
615 * pointer type.
616 */
617 /* NOLINTBEGIN(bugprone-sizeof-expression) */
618 console->pollers = reallocarray(console->pollers, console->n_pollers,
619 sizeof(*console->pollers));
620 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800621
622 console->pollers[n] = poller;
623
624 /* increase pollfds array too */
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930625 console->pollfds = reallocarray(
626 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
627 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800628
629 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930630 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
631 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800632
633 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930634 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800635
636 return poller;
637}
638
Andrew Jefferya72711a2023-04-18 18:19:41 +0930639void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800640{
641 int i;
642
643 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930644 for (i = 0; i < console->n_pollers; i++) {
645 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800646 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930647 }
648 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800649
650 assert(i < console->n_pollers);
651
652 console->n_pollers--;
653
Andrew Jeffery91b52172023-04-19 12:42:14 +0930654 /*
655 * Remove the item from the pollers array...
656 *
657 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
658 * pointer type.
659 */
660 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930661 memmove(&console->pollers[i], &console->pollers[i + 1],
662 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800663
Andrew Jeffery91b52172023-04-19 12:42:14 +0930664 console->pollers = reallocarray(console->pollers, console->n_pollers,
665 sizeof(*console->pollers));
666 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800667
668 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930669 memmove(&console->pollfds[i], &console->pollfds[i + 1],
670 sizeof(*console->pollfds) *
671 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800672
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930673 console->pollfds = reallocarray(
674 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
675 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800676
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800677 free(poller);
678}
679
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800680void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930681 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800682{
683 int i;
684
685 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930686 for (i = 0; i < console->n_pollers; i++) {
687 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800688 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930689 }
690 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800691
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930692 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800693}
694
Andrew Jefferyfd048322023-04-18 12:02:01 +0930695void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930696 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800697{
698 struct timeval now;
699 int rc;
700
701 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930702 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800703 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930704 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800705
706 timeradd(&now, tv, &poller->timeout);
707}
708
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930709static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800710{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930711 struct timeval *earliest;
712 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800713 struct poller *poller;
714 int i;
715
716 earliest = NULL;
717
718 for (i = 0; i < console->n_pollers; i++) {
719 poller = console->pollers[i];
720
721 if (poller->timeout_fn && timerisset(&poller->timeout) &&
722 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930723 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800724 // poller is buffering data and needs the poll
725 // function to timeout.
726 earliest = &poller->timeout;
727 }
728 }
729
730 if (earliest) {
731 if (timercmp(earliest, cur_time, >)) {
732 /* recalculate the timeout period, time period has
733 * not elapsed */
734 timersub(earliest, cur_time, &interval);
735 return ((interval.tv_sec * 1000) +
736 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930737 } /* return from poll immediately */
738 return 0;
739
740 } /* poll indefinitely */
741 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800742}
743
744static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800745{
746 struct poller *poller;
747 struct pollfd *pollfd;
748 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930749 int i;
750 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800751
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800752 rc = 0;
753
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800754 /*
755 * Process poll events by iterating through the pollers and pollfds
756 * in-step, calling any pollers that we've found revents for.
757 */
758 for (i = 0; i < console->n_pollers; i++) {
759 poller = console->pollers[i];
760 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800761 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800762
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800763 /* process pending events... */
764 if (pollfd->revents) {
765 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930766 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930767 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800768 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930769 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800770 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930771 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800772 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800773
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800774 if ((prc == POLLER_OK) && poller->timeout_fn &&
775 timerisset(&poller->timeout) &&
776 timercmp(&poller->timeout, cur_time, <=)) {
777 /* One of the ringbuffer consumers is buffering the
778 data stream. The amount of idle time the consumer
779 desired has expired. Process the buffered data for
780 transmission. */
781 timerclear(&poller->timeout);
782 prc = poller->timeout_fn(poller->handler, poller->data);
783 if (prc == POLLER_EXIT) {
784 rc = -1;
785 } else if (prc == POLLER_REMOVE) {
786 poller->remove = true;
787 }
788 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800789 }
790
791 /**
792 * Process deferred removals; restarting each time we unregister, as
793 * the array will have changed
794 */
795 for (;;) {
796 bool removed = false;
797
798 for (i = 0; i < console->n_pollers; i++) {
799 poller = console->pollers[i];
800 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800801 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800802 removed = true;
803 break;
804 }
805 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930806 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800807 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930808 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800809 }
810
811 return rc;
812}
813
Jeremy Kerr769cee12016-03-15 17:53:56 +0800814static void sighandler(int signal)
815{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930816 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800817 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930818 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800819}
820
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800821int run_console(struct console *console)
822{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930823 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800824 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930825 long timeout;
826 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800827
828 rc = 0;
829
Jeremy Kerrd831f962016-01-29 17:18:01 +0800830 for (;;) {
831 uint8_t buf[4096];
832
Jeremy Kerr17641452017-02-07 22:09:13 +0800833 BUILD_ASSERT(sizeof(buf) <= buffer_size);
834
Jeremy Kerr769cee12016-03-15 17:53:56 +0800835 if (sigint) {
836 fprintf(stderr, "Received interrupt, exiting\n");
837 break;
838 }
839
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800840 rc = get_current_time(&tv);
841 if (rc) {
842 warn("Failed to read current time");
843 break;
844 }
845
846 timeout = get_poll_timeout(console, &tv);
847
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800848 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930849 console->n_pollers + MAX_INTERNAL_POLLFD,
850 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800851
Jeremy Kerrd831f962016-01-29 17:18:01 +0800852 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800853 if (errno == EINTR) {
854 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800855 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930856 warn("poll error");
857 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800858 }
859
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800860 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800861 if (console->pollfds[console->n_pollers].revents) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930862 rc = read(console->tty.fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800863 if (rc <= 0) {
864 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800865 rc = -1;
866 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800867 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800868 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930869 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800870 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930871 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800872 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800873
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800874 if (console->pollfds[console->n_pollers + 1].revents) {
875 sd_bus_process(console->bus, NULL);
876 }
877
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800878 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800879 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930880 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800881 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930882 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800883 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800884
885 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800886 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800887
888 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800889}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800890static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930891 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930892 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +0930893 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800894};
895
896int main(int argc, char **argv)
897{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800898 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500899 const char *config_tty_kname = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930900 const char *console_id = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800901 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800902 struct config *config;
903 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800904
Jeremy Kerrd831f962016-01-29 17:18:01 +0800905 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930906 int c;
907 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800908
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930909 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930910 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800911 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930912 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800913
914 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800915 case 'c':
916 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800917 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930918 case 'i':
919 console_id = optarg;
920 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800921 case 'h':
922 case '?':
923 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800924 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800925 }
926 }
927
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930928 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030929 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930930 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500931
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800932 console = malloc(sizeof(struct console));
933 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930934 console->pollfds =
935 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800936 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800937
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800938 config = config_init(config_filename);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800939
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930940 if (set_socket_info(console, config, console_id)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930941 rc = -1;
942 goto out_config_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500943 }
944
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700945 uart_routing_init(config);
946
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930947 rc = tty_init(console, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930948 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800949 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930950 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800951
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800952 dbus_init(console, config);
953
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800954 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800955
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800956 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800957
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800958 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800959
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930960 tty_fini(console);
961
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800962out_config_fini:
963 config_fini(config);
964
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800965 free(console->pollers);
966 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800967 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800968
969 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
970}