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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1a108c740 nxboot: support watchdog ping during FW update
1a108c740 is described below

commit 1a108c740fe1f8465602db2f008f2282066f7ceb
Author: Michal Lenc <[email protected]>
AuthorDate: Thu Nov 12 18:56:00 2026 +0100

    nxboot: support watchdog ping during FW update
    
    The application might need to ping the watchdog during update. Add
    support for pinging if NXBOOT_WATCHDOG is enabled.
    
    This has no effect on current usage, the behavior is the same
    if NXBOOT_WATCHDOG is disabled.
    
    Signed-off-by: Michal Lenc <[email protected]>
---
 boot/nxboot/Kconfig          | 20 +++++++++++++++
 boot/nxboot/include/nxboot.h |  6 +++++
 boot/nxboot/loader/boot.c    | 12 +++++++++
 boot/nxboot/nxboot_main.c    | 61 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 99 insertions(+)

diff --git a/boot/nxboot/Kconfig b/boot/nxboot/Kconfig
index f8222e0f0..68b38772c 100644
--- a/boot/nxboot/Kconfig
+++ b/boot/nxboot/Kconfig
@@ -154,6 +154,26 @@ config NXBOOT_PRINTF_PROGRESS_PERCENT
 
 endchoice
 
+config NXBOOT_WATCHDOG
+       bool "Ping watchdog during FW update"
+       default n
+       depends on WATCHDOG
+       ---help---
+               This may be needed if the device uses watchdog. Selecting this 
option
+               ensures the watchdog ping is sent during FW update process. 
There
+               isn't periodic ping timing, but rather pings during key parts of
+               the update process (typically between chunk writes to the 
memory).
+
+if NXBOOT_WATCHDOG
+
+config NXBOOT_WATCHDOG_DEVPATH
+       string "Watchdog device path"
+       default "/dev/watchdog0"
+       ---help---
+               Path to the device driver the bootloader application will open.
+
+endif # NXBOOT_WATCHDOG
+
 endif # NXBOOT_BOOTLOADER
 
 endif # BOOT_NXBOOT
diff --git a/boot/nxboot/include/nxboot.h b/boot/nxboot/include/nxboot.h
index 3d92e9753..8cb08aee5 100644
--- a/boot/nxboot/include/nxboot.h
+++ b/boot/nxboot/include/nxboot.h
@@ -174,6 +174,12 @@ void nxboot_progress(int type, ...);
 #define nxboot_progress(type, ...) do {} while (0)
 #endif
 
+#ifdef CONFIG_NXBOOT_WATCHDOG
+int nxboot_ping_watchdog(void);
+#else
+#define nxboot_ping_watchdog(void) do {} while (0)
+#endif
+
 /****************************************************************************
  * Name: nxboot_get_state
  *
diff --git a/boot/nxboot/loader/boot.c b/boot/nxboot/loader/boot.c
index b2b350e42..fea411cf2 100644
--- a/boot/nxboot/loader/boot.c
+++ b/boot/nxboot/loader/boot.c
@@ -103,6 +103,7 @@ static uint32_t calculate_crc(int fd, struct 
nxboot_img_header *header)
 #ifdef CONFIG_NXBOOT_PRINTF_PROGRESS_PERCENT
   total_size = remain;
 #endif
+  nxboot_ping_watchdog();
   while (remain > 0)
     {
       readsiz = remain > info.blocksize ? info.blocksize : remain;
@@ -211,6 +212,7 @@ static int copy_partition(int from, int where, struct 
nxboot_state *state,
 
   while (remain > 0)
     {
+      nxboot_ping_watchdog();
       readsiz = remain > blocksize ? blocksize : remain;
       if (flash_partition_read(from, buf, readsiz, off) < 0)
         {
@@ -245,6 +247,8 @@ static bool validate_image(int fd)
 {
   struct nxboot_img_header header;
 
+  nxboot_ping_watchdog();
+
   get_image_header(fd, &header);
   if (!validate_image_header(&header))
     {
@@ -344,6 +348,8 @@ static int perform_update(struct nxboot_state *state, bool 
check_only)
   int tertiary;
   bool primary_valid;
 
+  nxboot_ping_watchdog();
+
   primary = flash_partition_open(CONFIG_NXBOOT_PRIMARY_SLOT_PATH);
   if (primary < 0)
     {
@@ -549,6 +555,12 @@ int nxboot_get_state(struct nxboot_state *state)
 
   memset(state, 0, sizeof *state);
 
+  /* Getting the state should be fast enough to avoid the need of pinging
+   * the watchdog in it multiple times
+   */
+
+  nxboot_ping_watchdog();
+
   primary = flash_partition_open(CONFIG_NXBOOT_PRIMARY_SLOT_PATH);
   if (primary < 0)
     {
diff --git a/boot/nxboot/nxboot_main.c b/boot/nxboot/nxboot_main.c
index 1670ca207..4265e3362 100644
--- a/boot/nxboot/nxboot_main.c
+++ b/boot/nxboot/nxboot_main.c
@@ -34,6 +34,12 @@
 #include <nxboot.h>
 #include <sys/boardctl.h>
 
+#ifdef CONFIG_NXBOOT_WATCHDOG
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <nuttx/timers/watchdog.h>
+#endif
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -49,6 +55,10 @@ static bool g_progress_percent_started = false;
 #endif
 #endif
 
+#ifdef CONFIG_NXBOOT_WATCHDOG
+static int wdog_fd = -1;
+#endif
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -95,6 +105,37 @@ static const char *progress_msgs[] =
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: nxboot_ping_watchdog
+ *
+ * Description:
+ *   If enabled, this function pings the watchdog when called.
+ *
+ * Returned Value:
+ *   Zero on success, negative value on error.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NXBOOT_WATCHDOG
+int nxboot_ping_watchdog(void)
+{
+  int ret;
+
+  if (wdog_fd < 0)
+    {
+      return wdog_fd;
+    }
+
+  ret = ioctl(wdog_fd, WDIOC_KEEPALIVE, 0);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "Failed to ping watchdog: %d", errno);
+    }
+
+  return ret;
+}
+#endif
+
 /****************************************************************************
  * Name: nxboot_progress
  *
@@ -278,6 +319,17 @@ int main(int argc, FAR char *argv[])
   syslog(LOG_INFO, "*** nxboot ***\n");
   nxboot_progress(nxboot_info, startup_msg);
 
+#ifdef CONFIG_NXBOOT_WATCHDOG
+  wdog_fd = open(CONFIG_NXBOOT_WATCHDOG_DEVPATH, O_RDONLY);
+  if (wdog_fd < 0)
+    {
+      syslog(LOG_ERR, "Failed to open watchdog device \"%s\": %d",
+             CONFIG_NXBOOT_WATCHDOG_DEVPATH, errno);
+    }
+#endif
+
+  nxboot_ping_watchdog();
+
 #ifdef CONFIG_NXBOOT_SWRESET_ONLY
   check_only = true;
   ret = boardctl(BOARDIOC_RESET_CAUSE, (uintptr_t)&cause);
@@ -310,6 +362,8 @@ int main(int argc, FAR char *argv[])
   syslog(LOG_INFO, "Found bootable image, boot from primary.\n");
   nxboot_progress(nxboot_info, found_bootable_image);
 
+  nxboot_ping_watchdog();
+
 #ifdef CONFIG_NXBOOT_COPY_TO_RAM
   syslog(LOG_INFO, "Copying image to RAM.\n");
   nxboot_ramcopy();
@@ -320,6 +374,13 @@ int main(int argc, FAR char *argv[])
   info.path        = CONFIG_NXBOOT_PRIMARY_SLOT_PATH;
   info.header_size = CONFIG_NXBOOT_HEADER_SIZE;
 
+#ifdef CONFIG_NXBOOT_WATCHDOG
+  if (wdog_fd >= 0)
+    {
+      close(wdog_fd);
+    }
+#endif
+
   ret = boardctl(BOARDIOC_BOOT_IMAGE, (uintptr_t)&info);
 
   /* Only get here if the board boot fails */

Reply via email to