[PATCH procd] inittab: detect active console from kernel if no console= specified

Mathew McBride matt at traverse.com.au
Fri Mar 5 00:54:15 GMT 2021


The default serial console can be set in the device tree
using the linux,stdout-path parameter (or equivalent from ACPI).

This is important for universal booting (EFI/EBBR) on ARM platforms
where the default console can be different (e.g ttyS0 vs ttyAMA0).

Signed-off-by: Mathew McBride <matt at traverse.com.au>
---
 inittab.c     |  8 ++++++++
 utils/utils.c | 22 ++++++++++++++++++++++
 utils/utils.h |  1 +
 3 files changed, 31 insertions(+)

diff --git a/inittab.c b/inittab.c
index 45118f4..2c2270c 100644
--- a/inittab.c
+++ b/inittab.c
@@ -183,7 +183,15 @@ static void askconsole(struct init_action *a)
 	char line[256], *tty, *split;
 	int i;
 
+	/* First, try console= on the kernel command line,
+	 * then fallback to /sys/class/tty/console/active,
+	 * which should work when linux,stdout-path (or equivalent)
+	 * is in the device tree
+	 */
 	tty = get_cmdline_val("console", line, sizeof(line));
+	if (tty == NULL) {
+		tty = get_active_console(line, sizeof(line));
+	}
 	if (tty != NULL) {
 		split = strchr(tty, ',');
 		if (split != NULL)
diff --git a/utils/utils.c b/utils/utils.c
index 8d76129..e90feec 100644
--- a/utils/utils.c
+++ b/utils/utils.c
@@ -135,6 +135,28 @@ blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
 	return true;
 }
 
+char *get_active_console(char *out, int len)
+{
+	char line[CMDLINE_SIZE + 1];
+	int fd = open("/sys/class/tty/console/active", O_RDONLY);
+	ssize_t r = read(fd, line, sizeof(line) - 1);
+
+	close(fd);
+
+	if (r <= 0)
+		return NULL;
+
+	/* The active file is terminated by a newline which we need to strip */
+	char *newline = strtok(line, "\n");
+
+	if (newline != NULL) {
+		strncpy(out, newline, len);
+		return out;
+	}
+
+	return NULL;
+}
+
 char* get_cmdline_val(const char* name, char* out, int len)
 {
 	char line[CMDLINE_SIZE + 1], *c, *sptr;
diff --git a/utils/utils.h b/utils/utils.h
index 908c314..216323e 100644
--- a/utils/utils.h
+++ b/utils/utils.h
@@ -52,6 +52,7 @@ void blobmsg_list_free(struct blobmsg_list *list);
 bool blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2);
 void blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src);
 char* get_cmdline_val(const char* name, char* out, int len);
+char *get_active_console(char *out, int len);
 
 int patch_fd(const char *device, int fd, int flags);
 int patch_stdio(const char *device);
-- 
2.30.0




More information about the openwrt-devel mailing list