console: Prefer currently selected serial console as stdio device

Adjust the scan for default console stdio device to prefer the
currently selected serial device. This is useful in combination
with CONFIG_SERIAL_PROBE_ALL=y, in which case the system would
instantiate all serial devices as stdio devices in the order in
which they are listed in control DT. The currently selected serial
device may not be the first device listed in DT, in which case the
current console_init_r() implementation unexpectedly switches to
another serial console after listing stderr using "Err:" line, and
just before showing U-Boot shell, which is not the desired behavior.

The scan now iterates over the entire list of stdio devices. If the
current iterator stdio device is the current serial device, or there
is no input or output stdio device assigned to the input or output
stream yet, then the current iterator stdio device is assigned to that
stream. This way, the first suitable stdio device is assigned to the
stream, but the current serial console stdio device can override that
assignment.

As a small optimization, if the current iterator stdio device is the
current serial device and both input and output streams as assigned,
then the loop can terminate, because the current serial device has a
chance to be used as a stdio device at this point.

Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Marek Vasut 2026-03-17 03:17:40 +01:00 committed by Tom Rini
parent 0da1866a8f
commit 32dc2866e0

View File

@ -1212,13 +1212,16 @@ int console_init_r(void)
list_for_each(pos, list) {
dev = list_entry(pos, struct stdio_dev, list);
if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
if ((dev->flags & DEV_FLAGS_INPUT) &&
(dev->priv == gd->cur_serial_dev || !inputdev))
inputdev = dev;
}
if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
if ((dev->flags & DEV_FLAGS_OUTPUT) &&
(dev->priv == gd->cur_serial_dev || !outputdev))
outputdev = dev;
}
if(inputdev && outputdev)
/* The current serial console is the preferred stdio. */
if (dev->priv == gd->cur_serial_dev && inputdev && outputdev)
break;
}