With CONFIG_SYSTEMD_OF_WATCHDOG, the device tree bootargs fixup puts
systemd.watchdog_device= into global.linux.bootargs.dyn.watchdog only to
read it back right away via linux_bootargs_get(). This has side effects
beyond the fixup:

 - The variable is created whenever the fixup runs, so also for of_dump
   -F, oftree -s or of_diff after a boot attempt has enabled the
   watchdog, and stays around afterwards.
 - Its mere existence makes linux_bootargs_get() ignore the legacy
   bootargs environment variable, which is only used when no
   global.linux.bootargs.* variables exist.

Return the option from watchdog_build_bootargs() and append it to the
bootargs in of_write_bootargs() instead. Note this moves the option to
the end of the command line, while it was previously sorted among the
global.linux.bootargs.* variables.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <[email protected]>
---
 Documentation/user/booting-linux.rst |  2 ++
 common/Kconfig                       |  6 +++---
 common/oftree.c                      | 41 ++++++++++++++++++++----------------
 3 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/Documentation/user/booting-linux.rst 
b/Documentation/user/booting-linux.rst
index 0fbdc52c68..158aa8b313 100644
--- a/Documentation/user/booting-linux.rst
+++ b/Documentation/user/booting-linux.rst
@@ -124,6 +124,8 @@ interleaved with externally provided command-line arguments:
   * ``systemd.machine_id=`` controlled by 
:ref:`global.bootm.provide_machine_id 
<magicvar_global_bootm_provide_machine_id>`
   * ``systemd.hostname=`` controlled by :ref:`global.bootm.provide_hostname 
<magicvar_global_bootm_provide_hostname>`
   * ``barebox.security.policy=`` controlled by 
:ref:`global.bootm.provide_policy <magicvar_global_bootm_provide_policy>`
+  * ``systemd.watchdog_device=`` controlled by ``CONFIG_SYSTEMD_OF_WATCHDOG``,
+    only when booting with a device tree
 
 Creating root= options for the Kernel
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/common/Kconfig b/common/Kconfig
index 9adecfd2e4..da4cc8a5c9 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -1443,9 +1443,9 @@ config SYSTEMD_OF_WATCHDOG
        bool "inform devicetree-enabled kernel of used watchdog"
        depends on WATCHDOG && OFTREE && FLEXIBLE_BOOTARGS
        help
-         Sets the linux.bootargs.dyn.watchdog global variable with a value of
-         systemd.watchdog_device=/dev/WDOG if barebox succeeded in enabling
-         the watchdog WDOG prior to boot. WDOG is the alias of the watchdog
+         Appends systemd.watchdog_device=/dev/WDOG to the Kernel command line
+         in the device tree if barebox succeeded in enabling the watchdog
+         WDOG prior to boot. WDOG is the alias of the watchdog
          in the kernel device tree. If the kernel is booted without a device
          tree or with one that lacks aliases, nothing is added.
 
diff --git a/common/oftree.c b/common/oftree.c
index 7aaed70e40..5f9b2e50cb 100644
--- a/common/oftree.c
+++ b/common/oftree.c
@@ -160,22 +160,18 @@ static int of_fixup_bootargs_reset_source(struct 
device_node *root,
        return of_property_write_string(chosen, "reset-source-device", 
np->full_name);
 }
 
-static void watchdog_build_bootargs(struct watchdog *watchdog, struct 
device_node *root)
+static char *watchdog_build_bootargs(struct watchdog *watchdog, struct 
device_node *root)
 {
        int alias_id;
-       char *buf;
 
        if (!watchdog)
-               return;
+               return NULL;
 
        alias_id = watchdog_get_alias_id_from(watchdog, root);
        if (alias_id < 0)
-               return;
-
-       buf = xasprintf("systemd.watchdog_device=/dev/watchdog%d", alias_id);
+               return NULL;
 
-       globalvar_add_simple("linux.bootargs.dyn.watchdog", buf);
-       free(buf);
+       return xasprintf("systemd.watchdog_device=/dev/watchdog%d", alias_id);
 }
 
 static int bootargs_append = 0;
@@ -183,31 +179,40 @@ BAREBOX_MAGICVAR(global.linux.bootargs_append, "append to 
original oftree bootar
 
 static int of_write_bootargs(struct device_node *node)
 {
-       const char *str;
+       const char *str, *oldstr;
+       char *watchdog = NULL;
        char *buf = NULL;
        int ret;
 
        if (IS_ENABLED(CONFIG_SYSTEMD_OF_WATCHDOG))
-               watchdog_build_bootargs(boot_get_enabled_watchdog(), 
of_get_parent(node));
+               watchdog = watchdog_build_bootargs(boot_get_enabled_watchdog(),
+                                                  of_get_parent(node));
 
-       str = linux_bootargs_get();
-       if (!str)
-               return 0;
+       str = skip_spaces(linux_bootargs_get() ?: "");
+
+       if (watchdog) {
+               if (*str)
+                       str = buf = xasprintf("%s %s", str, watchdog);
+               else
+                       str = watchdog;
+       }
 
-       str = skip_spaces(str);
        if (strlen(str) == 0)
                return 0;
 
        if (bootargs_append) {
-               const char *oldstr;
-
                ret = of_property_read_string(node, "bootargs", &oldstr);
-               if (!ret)
-                       str = buf = xasprintf("%s %s", oldstr, str);
+               if (!ret) {
+                       char *tmp = xasprintf("%s %s", oldstr, str);
+
+                       free(buf);
+                       str = buf = tmp;
+               }
        }
 
        ret = of_property_write_string(node, "bootargs", str);
        free(buf);
+       free(watchdog);
        return ret;
 }
 

-- 
2.47.3


Reply via email to