From 10b9f33142dcdc2be58503dbc62c1ba193bf63ff Mon Sep 17 00:00:00 2001 From: Shawn Nock Date: Tue, 4 Jun 2019 12:20:14 -0400 Subject: [PATCH] Integrates fixkey workflow into the normal update workflow Normal upload via umcumgr can update the bootloader if needed without additional steps. --- src/image.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/image.c b/src/image.c index a77832e..af33a6f 100644 --- a/src/image.c +++ b/src/image.c @@ -122,6 +122,7 @@ static void cbor_require_type(CborValue *it, CborType type) { } static char *get_version(void) { + serial_flush(serial_fd); int rc = write(serial_fd, IMAGE_LIST_CMD, sizeof(IMAGE_LIST_CMD) - 1); if (rc < sizeof(IMAGE_LIST_CMD) - 1) { fprintf(stderr, "Failed to send command: %s\n", strerror(errno)); @@ -188,9 +189,6 @@ static char *get_version(void) { cbor_value_dup_text_string(&version_tag, &version, &len, NULL); break; } - if (!version) { - printf("No applicaiton image found\n"); - } free(decoded_buf); return version; } @@ -247,6 +245,7 @@ static void wrap_and_send_pkt(uint8_t *data, size_t len) { } void do_upload(char *filename){ + serial_flush(serial_fd); size_t pos = 0, len; uint8_t const *payload; if (filename) { @@ -259,11 +258,13 @@ void do_upload(char *filename){ struct stat st; fstat(fd, &st); len = st.st_size; + uint8_t *payload_mut = calloc(1, len); if (!payload_mut) { fprintf(stderr, "Failed alloc %s:%d", __FILE__, __LINE__); exit(9); } + size_t bytes_read = 0; while (bytes_read < len) { errno = 0; @@ -375,28 +376,30 @@ void do_upload(char *filename){ pos += actual_size; free(out); } - printf("\nSuccess.\n"); - exit(0); + printf("\n"); } void fix_and_upload(char *filename) { char *version = get_version(); - if (!strcmp(version, "0.3.2.0") ) { + if (version && !strcmp(version, "0.3.2.0") ) { // Upload bootloader update payload printf("Bootloader update required, uploading...\n"); do_upload(NULL); // Reset into controller + printf("Starting Controller\n"); gpio_ble_req_controller(); + sleep(1); // Update bootloader + printf("Triggering bootloader upgrade\n"); write(serial_fd, fixkey_msg, sizeof(fixkey_msg)); - if (fsync(serial_fd) < 0) { - fprintf(stderr, "fsync of serial device failed.\n"); - } + sleep(3); // Reset into new bootloader + printf("Reseting into new bootloader\n"); gpio_ble_req_bootloader(); + usleep(50000); } // Do the update @@ -413,8 +416,13 @@ void image_main(int fd, int argc, char **argv){ } if (!strcmp(argv[0], "version")) { char *version = get_version(); - printf("%s\n", version); - free(version); + + if (version) { + printf("%s\n", version); + free(version); + } else { + printf("No application image found.\n"); + } } else if (!strcmp(argv[0], "upload")) { if (argc < 2) { fprintf(stderr, "umcumgr image upload \n");