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/boot_serial/src/CMakeLists.txt b/boot/boot_serial/src/CMakeLists.txt
new file mode 100644
index 0000000..48608e8
--- /dev/null
+++ b/boot/boot_serial/src/CMakeLists.txt
@@ -0,0 +1,6 @@
+zephyr_sources(boot_serial.c)
+
+#mbedtls-base64 library header
+zephyr_include_directories($ENV{ZEPHYR_BASE}/ext/lib/crypto/mbedtls/include)
+
+zephyr_include_directories(${BOOT_DIR}/bootutil/include)
diff --git a/boot/boot_serial/src/boot_serial.c b/boot/boot_serial/src/boot_serial.c
index ac935c1..db6388e 100644
--- a/boot/boot_serial/src/boot_serial.c
+++ b/boot/boot_serial/src/boot_serial.c
@@ -24,24 +24,35 @@
#include "sysflash/sysflash.h"
+#ifdef __ZEPHYR__
+#include <misc/reboot.h>
+#include <misc/byteorder.h>
+#include <misc/__assert.h>
+#include <flash.h>
+#include <crc16.h>
+#include <serial_adapter/serial_adapter.h>
+
+#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
+#include "bootutil/bootutil_log.h"
+#include "mbedtls/base64.h"
+#else
#include <bsp/bsp.h>
-
-#include <flash_map/flash_map.h>
-#include <hal/hal_flash.h>
#include <hal/hal_system.h>
-
#include <os/endian.h>
-#include <os/os.h>
-#include <os/os_malloc.h>
#include <os/os_cputime.h>
-
#include <console/console.h>
+#include <crc/crc16.h>
+#include <base64/base64.h>
+#endif /* __ZEPHYR__ */
#include <tinycbor/cbor.h>
#include <tinycbor/cbor_buf_reader.h>
#include <cborattr/cborattr.h>
-#include <base64/base64.h>
-#include <crc/crc16.h>
+
+#include <flash_map/flash_map.h>
+#include <hal/hal_flash.h>
+#include <os/os.h>
+#include <os/os_malloc.h>
#include <bootutil/image.h>
@@ -50,6 +61,20 @@
#define BOOT_SERIAL_OUT_MAX 48
+#ifdef __ZEPHYR__
+/* mbedtls-base64 lib encodes data to null-terminated string */
+#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
+
+#define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */
+#define CRC_CITT_POLYMINAL 0x1021
+
+#define ntohs(x) sys_be16_to_cpu(x)
+#define htons(x) sys_cpu_to_be16(x)
+
+static char in_buf[CONFIG_BOOT_MAX_LINE_INPUT_LEN + 1];
+static char dec_buf[CONFIG_BOOT_MAX_LINE_INPUT_LEN + 1];
+#endif
+
static uint32_t curr_off;
static uint32_t img_size;
static struct nmgr_hdr *bs_hdr;
@@ -229,6 +254,7 @@
curr_off += img_blen;
out:
+ BOOT_LOG_INF("RX: 0x%x", rc);
cbor_encoder_create_map(&bs_root, &bs_rsp, CborIndefiniteLength);
cbor_encode_text_stringz(&bs_rsp, "rc");
cbor_encode_int(&bs_rsp, rc);
@@ -264,8 +290,13 @@
cbor_encoder_close_container(&bs_root, &bs_rsp);
boot_serial_output();
+#ifdef __ZEPHYR__
+ k_sleep(250);
+ sys_reboot(SYS_REBOOT_COLD);
+#else
os_cputime_delay_usecs(250000);
hal_system_reset();
+#endif
}
/*
@@ -288,10 +319,10 @@
buf += sizeof(*hdr);
len -= sizeof(*hdr);
-
bs_writer.bytes_written = 0;
cbor_encoder_init(&bs_root, &bs_writer, 0);
+
/*
* Limited support for commands.
*/
@@ -339,8 +370,14 @@
bs_hdr->nh_len = htons(len);
bs_hdr->nh_group = htons(bs_hdr->nh_group);
+#ifdef __ZEPHYR__
+ crc = crc16((u8_t *)bs_hdr, sizeof(*bs_hdr), CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC,
+ false);
+ crc = crc16(data, len, CRC_CITT_POLYMINAL, crc, true);
+#else
crc = crc16_ccitt(CRC16_INITIAL_CRC, bs_hdr, sizeof(*bs_hdr));
crc = crc16_ccitt(crc, data, len);
+#endif
crc = htons(crc);
console_write(pkt_start, sizeof(pkt_start));
@@ -356,9 +393,17 @@
totlen += len;
memcpy(&buf[totlen], &crc, sizeof(crc));
totlen += sizeof(crc);
+#ifdef __ZEPHYR__
+ size_t enc_len;
+ mbedtls_base64_encode(encoded_buf, sizeof(encoded_buf), &enc_len, buf,
+ totlen);
+ totlen = enc_len;
+#else
totlen = base64_encode(buf, totlen, encoded_buf, 1);
+#endif
console_write(encoded_buf, totlen);
console_write("\n", 1);
+ BOOT_LOG_INF("TX");
}
/*
@@ -370,7 +415,13 @@
int rc;
uint16_t crc;
uint16_t len;
-
+#ifdef __ZEPHYR__
+ int err;
+ err = mbedtls_base64_decode( &out[*out_off], maxout, &rc, in, inlen - 2);
+ if (err) {
+ return -1;
+ }
+#else
if (*out_off + base64_decode_len(in) >= maxout) {
return -1;
}
@@ -378,6 +429,7 @@
if (rc < 0) {
return -1;
}
+#endif
*out_off += rc;
if (*out_off > sizeof(uint16_t)) {
@@ -385,7 +437,11 @@
len = min(len, *out_off - sizeof(uint16_t));
out += sizeof(uint16_t);
+#ifdef __ZEPHYR__
+ crc = crc16(out, len, CRC_CITT_POLYMINAL, CRC16_INITIAL_CRC, true);
+#else
crc = crc16_ccitt(CRC16_INITIAL_CRC, out, len);
+#endif
if (crc || len <= sizeof(crc)) {
return 0;
}
@@ -411,12 +467,18 @@
int dec_off;
int full_line;
+#ifdef __ZEPHYR__
+ rc = boot_console_init();
+ buf = in_buf;
+ dec = dec_buf;
+ assert(max_input <= sizeof(in_buf) && max_input <= sizeof(dec_buf));
+#else
rc = console_init(NULL);
assert(rc == 0);
console_echo(0);
-
buf = os_malloc(max_input);
dec = os_malloc(max_input);
+#endif
assert(buf && dec);
off = 0;