boot_serial; text size reduction.
 - Remove dependency to sprintf()
 - Remove dependency to cborattr
 - mynewt: replace console with more direct interface to uart
 - mynewt: settings to reduce included os code

Signed-off-by: Marko Kiiskila <marko@runtime.io>
diff --git a/boot/mynewt/src/main.c b/boot/mynewt/src/main.c
index 083d03e..b504896 100755
--- a/boot/mynewt/src/main.c
+++ b/boot/mynewt/src/main.c
@@ -23,7 +23,8 @@
 #include <stddef.h>
 #include <inttypes.h>
 #include <stdio.h>
-#include "syscfg/syscfg.h"
+
+#include <syscfg/syscfg.h>
 #include <flash_map_backend/flash_map_backend.h>
 #include <os/os.h>
 #include <bsp/bsp.h>
@@ -36,6 +37,9 @@
 #include <hal/hal_nvreg.h>
 #include <boot_serial/boot_serial.h>
 #endif
+#if defined(MCUBOOT_SERIAL) && MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0
+#include <boot_uart/boot_uart.h>
+#endif
 #include <console/console.h>
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
@@ -61,6 +65,70 @@
 }
 
 #ifdef MCUBOOT_SERIAL
+#if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0
+
+/** Don't include null-terminator in comparison. */
+#define BOOT_SERIAL_DETECT_STRING_LEN \
+    (sizeof MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING) - 1)
+
+/**
+ * Listens on the UART for the management string.  Blocks for up to
+ * BOOT_SERIAL_DETECT_TIMEOUT milliseconds.
+ *
+ * @return                      true if the management string was received;
+ *                              false if the management string was not received
+ *                                  before the UART listen timeout expired.
+ */
+static bool
+serial_detect_uart_string(void)
+{
+    uint32_t start_tick;
+    char buf[BOOT_SERIAL_DETECT_STRING_LEN] = { 0 };
+    char ch;
+    int newline;
+    int rc;
+
+    /* Calculate the timeout duration in OS cputime ticks. */
+    static const uint32_t timeout_dur =
+        MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) /
+        (1000.0 / MYNEWT_VAL(OS_CPUTIME_FREQ));
+
+    rc = boot_serial_uart_open();
+    assert(rc == 0);
+
+    start_tick = os_cputime_get32();
+
+    while (1) {
+        /* Read a single character from the UART. */
+        rc = boot_serial_uart_read(&ch, 1, &newline);
+        if (rc > 0) {
+            /* Eliminate the oldest character in the buffer to make room for
+             * the new one.
+             */
+            memmove(buf, buf + 1, BOOT_SERIAL_DETECT_STRING_LEN - 1);
+            buf[BOOT_SERIAL_DETECT_STRING_LEN - 1] = ch;
+
+            /* If the full management string has been received, indicate that
+             * the serial boot loader should start.
+             */
+            rc = memcmp(buf,
+                        MYNEWT_VAL(BOOT_SERIAL_DETECT_STRING),
+                        BOOT_SERIAL_DETECT_STRING_LEN);
+            if (rc == 0) {
+                boot_serial_uart_close();
+                return true;
+            }
+        }
+
+        /* Abort the listen on timeout. */
+        if (os_cputime_get32() >= start_tick + timeout_dur) {
+            boot_serial_uart_close();
+            return false;
+        }
+    }
+}
+#endif
+
 static void
 serial_boot_detect(void)
 {
@@ -99,7 +167,7 @@
      * download commands from serial.
      */
 #if MYNEWT_VAL(BOOT_SERIAL_DETECT_TIMEOUT) != 0
-    if (boot_serial_detect_uart_string()) {
+    if (serial_detect_uart_string()) {
         boot_serial_start(BOOT_SERIAL_INPUT_MAX);
         assert(0);
     }