1 // SPDX-License-Identifier: GPL-2.0+
11 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
12 void iomux_printdevs(const int console)
15 struct stdio_dev *dev;
17 for_each_console_dev(i, console, dev)
18 printf("%s ", dev->name);
22 int iomux_match_device(struct stdio_dev **set, const int n, struct stdio_dev *sdev)
26 for (i = 0; i < n; i++)
32 /* This tries to preserve the old list if an error occurs. */
33 int iomux_doenv(const int console, const char *arg)
35 char *console_args, *temp, **start;
36 int i, j, io_flag, cs_idx, repeat;
37 struct stdio_dev **cons_set, **old_set;
38 struct stdio_dev *dev;
40 console_args = strdup(arg);
41 if (console_args == NULL)
44 * Check whether a comma separated list of devices was
45 * entered and count how many devices were entered.
46 * The array start[] has pointers to the beginning of
47 * each device name (up to MAX_CONSARGS devices).
49 * Have to do this twice - once to count the number of
50 * commas and then again to populate start.
55 /* There's always one entry more than the number of commas. */
58 temp = strchr(temp, ',');
64 start = (char **)malloc(i * sizeof(char *));
70 start[0] = console_args;
72 temp = strchr(start[i++], ',');
78 cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
79 if (cons_set == NULL) {
85 io_flag = stdio_file_to_flags(console);
94 for (j = 0; j < i; j++) {
96 * Check whether the device exists and is valid.
97 * console_assign() also calls console_search_dev(),
98 * but I need the pointer to the device.
100 dev = console_search_dev(io_flag, start[j]);
104 * Prevent multiple entries for a device.
106 repeat = iomux_match_device(cons_set, cs_idx, dev);
110 * Try assigning the specified device.
111 * This could screw up the console settings for apps.
113 if (console_assign(console, start[j]) < 0)
115 cons_set[cs_idx++] = dev;
119 /* failed to set any console */
125 old_set = console_devices[console];
126 repeat = cd_count[console];
128 console_devices[console] = cons_set;
129 cd_count[console] = cs_idx;
131 /* Stop dropped consoles */
132 for (i = 0; i < repeat; i++) {
133 j = iomux_match_device(cons_set, cs_idx, old_set[i]);
135 console_stop(console, old_set[i]);
142 int iomux_replace_device(const int console, const char *old, const char *new)
144 struct stdio_dev *dev;
145 char *arg = NULL; /* Initial empty list */
146 int size = 1; /* For NUL terminator */
149 for_each_console_dev(i, console, dev) {
150 const char *name = strcmp(dev->name, old) ? dev->name : new;
153 /* Append name with a ',' (comma) separator */
154 tmp = realloc(arg, size + strlen(name) + 1);
168 size = strlen(tmp) + 1;
171 ret = iomux_doenv(console, arg);
178 #endif /* CONSOLE_MUX */