From c27279dc26a26b8ad054882d4c5f86a85526fd16 Mon Sep 17 00:00:00 2001
From: Yoonji Park <koreapyj@dcmys.kr>
Date: Mon, 6 Jun 2022 07:44:48 +0000
Subject: [PATCH] arm: arm64: of: add CMDLINE_OVERRIDE to support chosen/bootargs-override

Add a CONFIG_CMDLINE_OVERRIDE Kconfig option for both ARM and ARM64 and
handle the corresponding "bootargs-override" property in
early_init_dt_scan_chosen(). When the property is present in the DT
chosen node its value fully replaces the kernel command line, which is
useful on devices where the bootloader sets bootargs that cannot be
edited.

Signed-off-by: Yoonji Park <koreapyj@dcmys.kr>
---
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -1490,6 +1490,14 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEN
 
 endchoice
 
+config CMDLINE_OVERRIDE
+	bool "Use alternative cmdline from device tree"
+	help
+	  Some bootloaders may have uneditable bootargs. While CMDLINE_FORCE can
+	  be used, this is not a good option for kernels that are shared across
+	  devices. This setting enables using "chosen/bootargs-override" as the
+	  cmdline if it exists in the device tree.
+
 config CMDLINE
 	string "Default kernel command string"
 	default ""
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -1120,6 +1120,17 @@ int __init early_init_dt_scan_chosen(cha
 	if (p != NULL && l > 0)
 		strlcat(cmdline, p, min_t(int, strlen(cmdline) + (int)l, COMMAND_LINE_SIZE));
 
+    /* CONFIG_CMDLINE_OVERRIDE is used to fallback to a different
+     * device tree option of chosen/bootargs-override. This is
+     * helpful on boards where u-boot sets bootargs, and is unable
+     * to be modified.
+     */
+#ifdef CONFIG_CMDLINE_OVERRIDE
+	p = of_get_flat_dt_prop(node, "bootargs-override", &l);
+	if (p != NULL && l > 0)
+		strscpy(cmdline, p, min((int)l, COMMAND_LINE_SIZE));
+#endif
+
 handle_cmdline:
 	/*
 	 * CONFIG_CMDLINE is meant to be a default in case nothing else
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -2398,6 +2398,14 @@ config CMDLINE_FORCE
 
 endchoice
 
+config CMDLINE_OVERRIDE
+	bool "Use alternative cmdline from device tree"
+	help
+	  Some bootloaders may have uneditable bootargs. While CMDLINE_FORCE can
+	  be used, this is not a good option for kernels that are shared across
+	  devices. This setting enables using "chosen/bootargs-override" as the
+	  cmdline if it exists in the device tree.
+
 config EFI_STUB
 	bool
 
