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/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");