[PATCH 1/8] console: add new $global.bootm.earlycon parameter

Ahmad Fatoum a.fatoum at pengutronix.de
Mon May 23 02:25:19 PDT 2022


barebox already fixes up a suitable console= parameter if it can
determine one into the kernel command line. This doesn't help with early
Linux issues, which can instead be debugged by the earlycon mechanism.

For earlycon to work, the kernel DT must have a stdout-path or the user
needs to explicitly specify what driver to use and how to access it
(base address, optionally access_type).

Make this easier by just having barebox fix up the needed information
when $global.bootm.earlycon is true and the barebox serial driver
provides the needed information. If the serial driver doesn't, a plain
"earlycon" parameter will be fixed up.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/bootm.c    | 24 ++++++++++++++++++++++++
 common/console.c  | 17 +++++++++++++++++
 include/console.h |  2 ++
 3 files changed, 43 insertions(+)

diff --git a/common/bootm.c b/common/bootm.c
index 3c80e8bf949b..712e6ebe4911 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -41,6 +41,7 @@ static struct image_handler *bootm_find_handler(enum filetype filetype,
 }
 
 static int bootm_appendroot;
+static int bootm_earlycon;
 static int bootm_provide_machine_id;
 static int bootm_verbosity;
 
@@ -732,6 +733,26 @@ int bootm_boot(struct bootm_data *bootm_data)
 		}
 	}
 
+	if (bootm_earlycon) {
+		struct console_device *console;
+		const char *earlycon = NULL;
+
+		for_each_console(console) {
+			if (!(console->f_active & (CONSOLE_STDOUT | CONSOLE_STDERR)))
+				continue;
+
+			earlycon = dev_get_param(&console->class_dev, "linux.bootargs.earlycon");
+			if (earlycon)
+				break;
+		}
+
+		if (!earlycon)
+			earlycon = "earlycon";
+
+		pr_info("Adding \"%s\" to Kernel commandline\n", earlycon);
+		globalvar_add_simple("linux.bootargs.bootm.earlycon", earlycon);
+	}
+
 	if (bootm_data->provide_machine_id) {
 		const char *machine_id = getenv_nonempty("global.machine_id");
 		char *machine_id_bootarg;
@@ -798,6 +819,7 @@ err_out:
 	if (data->of_root_node && data->of_root_node != of_get_root_node())
 		of_delete_node(data->of_root_node);
 
+	globalvar_remove("linux.bootargs.bootm.earlycon");
 	globalvar_remove("linux.bootargs.bootm.appendroot");
 	free(data->os_header);
 	free(data->os_file);
@@ -897,6 +919,7 @@ static int bootm_init(void)
 	globalvar_add_simple("bootm.root_dev", NULL);
 	globalvar_add_simple("bootm.tee", NULL);
 	globalvar_add_simple_bool("bootm.appendroot", &bootm_appendroot);
+	globalvar_add_simple_bool("bootm.earlycon", &bootm_earlycon);
 	globalvar_add_simple_bool("bootm.provide_machine_id", &bootm_provide_machine_id);
 	if (IS_ENABLED(CONFIG_BOOTM_INITRD)) {
 		globalvar_add_simple("bootm.initrd", NULL);
@@ -936,6 +959,7 @@ BAREBOX_MAGICVAR(global.bootm.oftree, "bootm default oftree");
 BAREBOX_MAGICVAR(global.bootm.tee, "bootm default tee image");
 BAREBOX_MAGICVAR(global.bootm.verify, "bootm default verify level");
 BAREBOX_MAGICVAR(global.bootm.verbose, "bootm default verbosity level (0=quiet)");
+BAREBOX_MAGICVAR(global.bootm.earlycon, "Add earlycon option to Kernel for early log output");
 BAREBOX_MAGICVAR(global.bootm.appendroot, "Add root= option to Kernel to mount rootfs from the device the Kernel comes from (default, device can be overridden via global.bootm.root_dev)");
 BAREBOX_MAGICVAR(global.bootm.root_dev, "bootm default root device (overrides default device in global.bootm.appendroot)");
 BAREBOX_MAGICVAR(global.bootm.provide_machine_id, "If true, append systemd.machine_id=$global.machine_id to Kernel command line");
diff --git a/common/console.c b/common/console.c
index 8727b187cf7c..c442c2dde132 100644
--- a/common/console.c
+++ b/common/console.c
@@ -220,6 +220,21 @@ static void console_init_early(void)
 	initialized = CONSOLE_INITIALIZED_BUFFER;
 }
 
+static void console_add_earlycon_param(struct console_device *cdev, unsigned baudrate)
+{
+	char *str;
+
+	if (!cdev->linux_earlycon_name)
+		return;
+
+	str = basprintf("earlycon=%s,0x%lx,%dn8", cdev->linux_earlycon_name,
+			(ulong)cdev->phys_base, baudrate);
+
+	dev_add_param_fixed(&cdev->class_dev, "linux.bootargs.earlycon", str);
+
+	free(str);
+}
+
 static void console_set_stdoutpath(struct console_device *cdev, unsigned baudrate)
 {
 	int id;
@@ -332,6 +347,8 @@ int console_register(struct console_device *newcdev)
 		console_set_stdoutpath(newcdev, baudrate);
 	}
 
+	console_add_earlycon_param(newcdev, baudrate);
+
 	if (newcdev->setbrg) {
 		ret = newcdev->setbrg(newcdev, baudrate);
 		if (ret)
diff --git a/include/console.h b/include/console.h
index 5d5783ca6668..7fea8eeb9e7d 100644
--- a/include/console.h
+++ b/include/console.h
@@ -53,6 +53,8 @@ struct console_device {
 	unsigned int baudrate_param;
 
 	const char *linux_console_name;
+	const char *linux_earlycon_name;
+	void __iomem *phys_base;
 
 	struct cdev devfs;
 	struct cdev_operations fops;
-- 
2.30.2




More information about the barebox mailing list