[PATCH] console: Prefer currently selected serial console as stdio device

Marek Vasut marek.vasut+renesas at mailbox.org
Mon Mar 16 00:53:32 CET 2026


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 runs twice. The first round looks up the current serial
console device and tests whether it is suitable as a console. If
yes, then the scanning ends, otherwise the second round starts.
The second round of scanning simply picks up the first suitable
console device.

Signed-off-by: Marek Vasut <marek.vasut+renesas at mailbox.org>
---
Cc: Alexander Sverdlin <alexander.sverdlin at siemens.com>
Cc: Andre Przywara <andre.przywara at arm.com>
Cc: Simon Glass <sjg at chromium.org>
Cc: Tom Rini <trini at konsulko.com>
Cc: u-boot at lists.denx.de
---
 common/console.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/common/console.c b/common/console.c
index 48586fd2166..3de53c66094 100644
--- a/common/console.c
+++ b/common/console.c
@@ -1208,17 +1208,35 @@ int console_init_r(void)
 			outputdev = console_search_dev (DEV_FLAGS_OUTPUT, "serial");
 	}
 
-	/* Scan devices looking for input and output devices */
-	list_for_each(pos, list) {
-		dev = list_entry(pos, struct stdio_dev, list);
-
-		if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
-			inputdev = dev;
-		}
-		if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
-			outputdev = dev;
+	/*
+	 * Scan devices looking for input and output devices.
+	 * The scan runs twice. The first round looks up the current
+	 * serial console device and tests whether it is suitable as
+	 * a console. If yes, then the scanning ends, otherwise the
+	 * second round starts. The second round of scanning simply
+	 * picks up the first suitable console device. This scanning
+	 * method allows retaining the currently selected serial
+	 * console, especially in case DM contains multiple suitable
+	 * serial consoles and the currently selected one is not the
+	 * first stdio device in the list.
+	 */
+	for (i = 0; i < 1; i++) {
+		list_for_each(pos, list) {
+			dev = list_entry(pos, struct stdio_dev, list);
+			if (!i && dev->priv != gd->cur_serial_dev)
+				continue;
+
+			if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
+				inputdev = dev;
+			}
+			if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
+				outputdev = dev;
+			}
+			if (inputdev && outputdev)
+				break;
 		}
-		if(inputdev && outputdev)
+
+		if (inputdev && outputdev)
 			break;
 	}
 
-- 
2.51.0



More information about the U-Boot mailing list