enable serial recovery functionality on the zephyr mcuboot

This patch introduced serial bootloader functionality ported
from mynewt targets tree.

For achieving this following changes were applied:
- Modified boot_serial module for using, zephyr-os modules
  (crc driver, mbedtls-base64 library) and the zephyr serial adapter module
  introduced recently.
- Added service of boot serial recovery mode to main.
- Adapted the input parser to using static buffers.

Default serial-boot-pin configuration was added for nrf52_pca10040
and nrf52840_pca10056 boards.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
diff --git a/boot/zephyr/CMakeLists.txt b/boot/zephyr/CMakeLists.txt
index 0b9eabb..e303421 100644
--- a/boot/zephyr/CMakeLists.txt
+++ b/boot/zephyr/CMakeLists.txt
@@ -6,6 +6,9 @@
 
 cmake_minimum_required(VERSION 3.8.2)
 
+
+set(KCONFIG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../Kconfig)
+
 ########################
 # Configuration choices.
 ########################
@@ -168,3 +171,12 @@
   target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/sha256.c")
   target_sources(app PRIVATE "${TINYCRYPT_DIR}/source/utils.c")
 endif()
+
+if (CONFIG_MCUBOOT_SERIAL)
+zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
+
+add_subdirectory(${BOOT_DIR}/boot_serial ./boot/boot_serial)
+add_subdirectory(${BOOT_DIR}/zephyr/tinycbor)
+add_subdirectory(${BOOT_DIR}/zephyr/cborattr)
+
+endif()
diff --git a/boot/zephyr/include/config-boot.h b/boot/zephyr/include/config-boot.h
index a81a02f..72cca68 100644
--- a/boot/zephyr/include/config-boot.h
+++ b/boot/zephyr/include/config-boot.h
@@ -29,6 +29,11 @@
 #ifndef MBEDTLS_CONFIG_BOOT_H
 #define MBEDTLS_CONFIG_BOOT_H
 
+#ifdef CONFIG_MCUBOOT_SERIAL
+/* Mcuboot uses mbedts-base64 for serial protocol encoding. */
+#define MBEDTLS_BASE64_C
+#endif
+
 /* TODO: Configure this between app and target.  Really, we want the
  * config to come from the app. */
 #define CONFIG_BOOT_VERIFY_RSA_SIGNATURE
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index cdd4139..1159e34 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -16,6 +16,8 @@
 
 #include <assert.h>
 #include <zephyr.h>
+#include <gpio.h>
+#include <misc/__assert.h>
 #include <flash.h>
 #include <asm_inline.h>
 #include <drivers/system_timer.h>
@@ -28,6 +30,10 @@
 #include "bootutil/bootutil.h"
 #include "flash_map/flash_map.h"
 
+#ifdef CONFIG_MCUBOOT_SERIAL
+#include <boot_serial/boot_serial.h>
+#endif
+
 struct device *boot_flash_device;
 
 void os_heap_init(void);
@@ -101,6 +107,29 @@
             ;
     }
 
+#ifdef CONFIG_MCUBOOT_SERIAL
+
+    struct device *detect_port;
+    u32_t detect_value;
+
+    detect_port = device_get_binding(CONFIG_BOOT_SERIAL_DETECT_PORT);
+    __ASSERT(detect_port, "Error: Bad port for boot serial detection.\n");
+
+    rc = gpio_pin_configure(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN,
+                            GPIO_DIR_IN | GPIO_PUD_PULL_UP);
+    __ASSERT(rc, "Error of boot detect pin initialization.\n");
+
+    rc = gpio_pin_read(detect_port, CONFIG_BOOT_SERIAL_DETECT_PIN, 
+                       &detect_value);
+    __ASSERT(rc, "Error of the reading the detect pin.\n");
+
+    if (detect_value == CONFIG_BOOT_SERIAL_DETECT_PIN_VAL) {
+        BOOT_LOG_INF("Enter the serial recovery mode");
+        boot_serial_start(CONFIG_BOOT_MAX_LINE_INPUT_LEN + 1);
+        __ASSERT(0, "Bootloader serial process was terminated unexpectedly.\n");
+    }
+#endif
+
     rc = boot_go(&rsp);
     if (rc != 0) {
         BOOT_LOG_ERR("Unable to find bootable image");