Skip to content

MCUBOOT_SERIAL_WAIT_FOR_DFU #2613

@FaustynaS

Description

@FaustynaS

Hi,
I noticed that the MCUBOOT_SERIAL_WAIT_FOR_DFU option might not work as expected. In the boot_serial_read_console function (boot_serial.c), the start time is assigned inside the while loop. If the elapsed time between setting the start time and the check_timeout part of the loop is less than 1 ms, the elapsed time will very often be 0, which can cause the device to get stuck in this loop for a very long time.

 ...
    while (timeout_in_ms > 0 || bs_entry) {
...
#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
        uint32_t start = k_uptime_get_32();
#endif
...
check_timeout:
        /* Subtract elapsed time */
#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
        elapsed_in_ms = (k_uptime_get_32() - start);
#endif
        timeout_in_ms -= elapsed_in_ms;
    }

My suggested change:

@@ -1514,6 +1521,11 @@ boot_serial_read_console(const struct boot_uart_funcs *f,int timeout_in_ms)
     max_input = sizeof(in_buf);
 
     off = 0;
+
+#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
+    uint32_t start = k_uptime_get_32();
+#endif
+
     while (timeout_in_ms > 0 || bs_entry) {
         /*
          * Don't enter CPU idle state here if timeout based serial recovery is
@@ -1527,9 +1539,8 @@ boot_serial_read_console(const struct boot_uart_funcs *f,int timeout_in_ms)
         }
 #endif
         MCUBOOT_WATCHDOG_FEED();
-#ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
-        uint32_t start = k_uptime_get_32();
-#endif
         rc = f->read(in_buf + off, sizeof(in_buf) - off, &full_line);
         if (rc <= 0 && !full_line) {
 #ifndef MCUBOOT_SERIAL_WAIT_FOR_DFU
@@ -1564,7 +1575,11 @@ boot_serial_read_console(const struct boot_uart_funcs *f,int timeout_in_ms)
 check_timeout:
         /* Subtract elapsed time */
 #ifdef MCUBOOT_SERIAL_WAIT_FOR_DFU
-        elapsed_in_ms = (k_uptime_get_32() - start);
+        uint32_t end_time = k_uptime_get_32();
+        elapsed_in_ms = end_time - start;
+        if (end_time != start) {
+            start = end_time;
+        }
 #endif
         timeout_in_ms -= elapsed_in_ms;
     }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions