This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit fd35a052497e52643d51d00ed03d706f681f0294
Author: wangjianyu3 <[email protected]>
AuthorDate: Fri Oct 17 15:15:28 2025 +0800

    system/nxinit: Add resetcause for triggers
    
    Add built-in property sys.boot.reason, which allows action triggers to be
    executed on specific reset cause.
    
    For example:
      ```
      on property:sys.boot.reason=cpu_soft_reset(bootloader)
          echo "bootloader mode ..."
          start fastboot
      ```
    
    Signed-off-by: wangjianyu3 <[email protected]>
---
 system/nxinit/init.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)

diff --git a/system/nxinit/init.c b/system/nxinit/init.c
index 8f8be4e29..95f95b1b2 100644
--- a/system/nxinit/init.c
+++ b/system/nxinit/init.c
@@ -28,6 +28,8 @@
 
 #include <errno.h>
 #include <poll.h>
+#include <string.h>
+#include <sys/boardctl.h>
 #include <sys/param.h>
 #include <sys/wait.h>
 
@@ -107,6 +109,68 @@ static void reap_process(FAR struct service_manager_s *sm,
     }
 }
 
+#ifdef CONFIG_BOARDCTL_RESET_CAUSE
+static int init_boot_reason(FAR struct action_manager_s *am)
+{
+  static FAR const char *const resetcause[] =
+    {
+      [BOARDIOC_RESETCAUSE_NONE]        = "unkown",
+      [BOARDIOC_RESETCAUSE_SYS_CHIPPOR] = "cold",
+      [BOARDIOC_RESETCAUSE_SYS_RWDT]    = "watchdog",
+      [BOARDIOC_RESETCAUSE_SYS_BOR]     = "undervoltage",
+      [BOARDIOC_RESETCAUSE_CORE_SOFT]   = "warm",
+      [BOARDIOC_RESETCAUSE_CORE_DPSP]   = "warm",
+      [BOARDIOC_RESETCAUSE_CORE_MWDT]   = "watchdog",
+      [BOARDIOC_RESETCAUSE_CORE_RWDT]   = "watchdog",
+      [BOARDIOC_RESETCAUSE_CPU_MWDT]    = "watchdog",
+      [BOARDIOC_RESETCAUSE_CPU_SOFT]    = "warm",
+      [BOARDIOC_RESETCAUSE_CPU_RWDT]    = "watchdog",
+      [BOARDIOC_RESETCAUSE_PIN]         = "powerkey",
+      [BOARDIOC_RESETCAUSE_LOWPOWER]    = "lowpower",
+      [BOARDIOC_RESETCAUSE_UNKOWN]      = "unkown",
+    };
+
+  static FAR const char * const resetflag[] =
+    {
+      [BOARDIOC_SOFTRESETCAUSE_USER_REBOOT]             = "reboot",
+      [BOARDIOC_SOFTRESETCAUSE_PANIC]                   = "kernel_panic",
+      [BOARDIOC_SOFTRESETCAUSE_ENTER_BOOTLOADER]        = "bootloader",
+      [BOARDIOC_SOFTRESETCAUSE_ENTER_RECOVERY]          = "recovery",
+      [BOARDIOC_SOFTRESETCAUSE_RESTORE_FACTORY]         = "factory_reset",
+      [BOARDIOC_SOFTRESETCAUSE_RESTORE_FACTORY_INQUIRY] =
+        "factory_reset_inquiry",
+      [BOARDIOC_SOFTRESETCAUSE_THERMAL]                 = "thermal",
+    };
+
+  struct boardioc_reset_cause_s reset;
+  int ret;
+
+  memset(&reset, 0, sizeof(reset));
+  ret = boardctl(BOARDIOC_RESET_CAUSE, (uintptr_t)&reset);
+  if (ret < 0)
+    {
+      init_err("boardctl BOARDIOC_RESET_CAUSE failed: %d", ret);
+      return ret;
+    }
+
+  if (reset.cause >= nitems(resetcause))
+    {
+      reset.cause = nitems(resetcause) - 1;
+    }
+
+  if (reset.flag >= nitems(resetflag))
+    {
+      reset.flag = nitems(resetflag) - 1;
+    }
+
+  return init_property_set(am->prop, "sys.boot.reason",
+                           reset.cause != BOARDIOC_RESETCAUSE_CPU_SOFT ?
+                           resetcause[reset.cause] : resetflag[reset.flag]);
+}
+#else
+#  define init_boot_reason(am) (0)
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -185,6 +249,12 @@ int main(int argc, FAR char *argv[])
   init_dump_actions(&am.actions);
   init_dump_services(&sm.services);
 
+  r = init_boot_reason(&am);
+  if (r < 0)
+    {
+      goto out;
+    }
+
   init_action_add_event(&am, "boot");
 
   for (; ; )

Reply via email to