From 50f987eb483d064e7f3f73d37b68de3d8326cfcf Mon Sep 17 00:00:00 2001 From: Shawn Nock Date: Tue, 18 Jun 2019 14:44:47 -0400 Subject: [PATCH] Adds in optional fast_mode for later c3w bootloaders --- src/error.c | 2 +- src/gpio.c | 8 +++++++- src/image.c | 48 +++++++++++++++++++++++++++++++++++++---------- src/image.h | 1 + src/main.c | 7 ++++++- src/serial_util.c | 4 ++-- 6 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/error.c b/src/error.c index 12f2c65..6961c6d 100644 --- a/src/error.c +++ b/src/error.c @@ -28,6 +28,6 @@ void _bail_out(err_t code, const char *function, const char *file, unsigned line if (code < ERR_LAST) { err_string = error_strings[code]; } - fprintf(stderr, "Fatal Error in %s @ %s:%u: %s\n", function, file, line, err_string); + fprintf(stderr, "Fatal Error in %s() @ %s:%u: %s\n", function, file, line, err_string); exit(code); } \ No newline at end of file diff --git a/src/gpio.c b/src/gpio.c index 3727a9e..e732e29 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -21,7 +21,7 @@ static int gpio_open(char const *gpio) { sprintf(gpio_path, fmt_str, gpio); int fd = open(gpio_path, O_WRONLY); if (fd < 0) { - fprintf(stderr, "Failed to open %s: %s", gpio_path, strerror(errno)); + fprintf(stderr, "Failed to open %s: %s\n", gpio_path, strerror(errno)); return -1; } return fd; @@ -29,6 +29,9 @@ static int gpio_open(char const *gpio) { void gpio_ble_reset(void) { int fd = gpio_open(EM_BLE_RESET_GPIO); + if (fd < 0) { + return; + } if (write(fd, "0", 1) < 1) { fprintf(stderr, "Failed to assert reset line\n"); } @@ -45,6 +48,9 @@ void gpio_ble_reset(void) { void gpio_ble_req_bootloader(void) { int fd = gpio_open(EM_BLE_REQ_GPIO); + if (fd < 0) { + return; + } if (write(fd, "0", 1) < 1) { fprintf(stderr, "Failed to assert bootloader_req\n"); } diff --git a/src/image.c b/src/image.c index 0b23b20..5eafc70 100644 --- a/src/image.c +++ b/src/image.c @@ -27,8 +27,14 @@ #include "sha256.h" #endif -#define IMAGE_CHUNK_SIZE 124 //216 // 124 for identical output to newtmgr -#define MACRO_CHUNK_SIZE 302 //432 // 302 "" + +/* These values produce output identical to mcumgr *if* DO_UPLOAD_SHA is defined */ +#define IMAGE_CHUNK_SIZE 124 // 124 for identical output to newtmgr +#define MACRO_CHUNK_SIZE 302 // 302 "" + +/* Fast mode doesn't support original EM bootloader, but is much faster with later bootloaders */ +#define IMAGE_CHUNK_SIZE_FAST 216 +#define MACRO_CHUNK_SIZE_FAST 432 /* * Larger values above greatly increase speed, but seem to break older @@ -40,6 +46,16 @@ static int serial_fd = -1; +static bool g_fast_mode = false; + +void image_set_fast_mode(void) { + g_fast_mode = true; +} + +static bool image_get_fast_mode(void) { + return g_fast_mode; +} + void image_help(void){ BAIL_OUT(ERR_USAGE); } @@ -191,7 +207,7 @@ static char *get_version(void) { return version; } -static void wrap_and_send_pkt(uint8_t *data, size_t len) { +static void wrap_and_send_pkt(uint8_t *data, size_t len, bool fast_mode) { assert(data != NULL); assert(len != 0); static uint8_t seq = 67; @@ -235,7 +251,13 @@ static void wrap_and_send_pkt(uint8_t *data, size_t len) { } size_t remaining = out_buf + out_len - pos; - size_t to_write = remaining > IMAGE_CHUNK_SIZE ? IMAGE_CHUNK_SIZE : remaining; + size_t to_write; + if (fast_mode) { + to_write = remaining > IMAGE_CHUNK_SIZE_FAST ? IMAGE_CHUNK_SIZE_FAST : remaining; + } else { + to_write = remaining > IMAGE_CHUNK_SIZE ? IMAGE_CHUNK_SIZE : remaining; + } + write(serial_fd, pos, to_write); write(serial_fd, "\n", 1); pos += to_write; @@ -244,7 +266,7 @@ static void wrap_and_send_pkt(uint8_t *data, size_t len) { fsync(serial_fd); } -void do_upload(char *filename){ +void do_upload(char *filename, bool fast_mode){ serial_flush(serial_fd); size_t pos = 0, len; uint8_t const *payload; @@ -279,12 +301,18 @@ void do_upload(char *filename){ len = fixkey_payload_len; } + size_t base_chunk_len; + if (fast_mode) { + base_chunk_len = MACRO_CHUNK_SIZE_FAST; + } else { + base_chunk_len = MACRO_CHUNK_SIZE; + } while (pos < len){ size_t data_chunk_len; if (pos == 0) { - data_chunk_len = MACRO_CHUNK_SIZE; + data_chunk_len = base_chunk_len; } else { - data_chunk_len = MACRO_CHUNK_SIZE + 45; + data_chunk_len = base_chunk_len + 45; } CborEncoder root, map; uint8_t cbor_buf[BOOT_SERIAL_OUT_MAX*2]; @@ -325,7 +353,7 @@ void do_upload(char *filename){ cbor_ensure_success(cbor_encoder_close_container(&root, &map)); size_t cbor_size = cbor_encoder_get_buffer_size(&root, cbor_buf); - wrap_and_send_pkt(cbor_buf, cbor_size); + wrap_and_send_pkt(cbor_buf, cbor_size, fast_mode); uint8_t resp_buf[BOOT_SERIAL_IN_MAX*2]; size_t in_len; @@ -378,7 +406,7 @@ void fix_and_upload(char *filename) { if (version && !strcmp(version, "0.3.2.0") ) { // Upload bootloader update payload printf("Bootloader update required, uploading...\n"); - do_upload(NULL); + do_upload(NULL, false); // Reset into controller printf("Starting Controller\n"); @@ -398,7 +426,7 @@ void fix_and_upload(char *filename) { // Do the update printf("Writing %s into slot 1\n", filename); - do_upload(filename); + do_upload(filename, image_get_fast_mode()); } void image_main(int fd, int argc, char **argv){ diff --git a/src/image.h b/src/image.h index c4ca07e..c112b33 100644 --- a/src/image.h +++ b/src/image.h @@ -10,5 +10,6 @@ #define IMAGE_LIST_CMD "\6\tAAsAAAABAAFCAKD1Mw==\n" void image_main(int, int, char **); +void image_set_fast_mode(void); #endif //UMCUMGR_IMAGE_H diff --git a/src/main.c b/src/main.c index 00ee575..ba1b5ee 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "image.h" #include "serial_util.h" @@ -14,13 +15,17 @@ int main(int argc, char **argv) { int c; char *speed = NULL; char *serial_port = DEFAULT_SERIAL_PORT; - while ((c = getopt(argc, argv, "c:s:")) != -1) { + bool fast_mode = false; + while ((c = getopt(argc, argv, "c:s:f")) != -1) { switch (c) { case 'c': serial_port = optarg; break; case 's': speed = optarg; + case 'f': + image_set_fast_mode(); + break; default: break; } diff --git a/src/serial_util.c b/src/serial_util.c index f71ce36..090b648 100644 --- a/src/serial_util.c +++ b/src/serial_util.c @@ -9,6 +9,7 @@ #include #include #include "serial_util.h" +#include "error.h" #define DEFAULT_SPEED B115200 @@ -45,9 +46,8 @@ int serial_init(char const *serial_port, char const *speed) { int serial_fd = open(serial_port, O_RDWR | O_NOCTTY | O_NDELAY); if (serial_fd < 0) { fprintf(stderr, "Unable to open %s: %s\n", serial_port, strerror(errno)); - return 1; + exit(ERR_SERIAL_PORT); } - //printf("Opened %s\n", serial_port); struct termios t_options;