diff --git a/src/fixkey-main.c b/src/fixkey-main.c index f76a6bb..e70f10f 100644 --- a/src/fixkey-main.c +++ b/src/fixkey-main.c @@ -2,19 +2,61 @@ // Created by nock on 2019-05-21. // -#include +#include #include #include #include #include -#include +#include +#include #include "fixkey-msg.h" #include "serial_util.h" #include "timer.h" #define DEFAULT_SERIAL_PORT "/dev/ttyUSB0" -#define RESPONSE_LEN 2 +#define MAX_RESPONSE_LEN 11 + +static void read_response(int serial_fd, uint8_t *dest, size_t *len) { + static uint8_t buf[MAX_RESPONSE_LEN] = {0}; + static uint8_t *p = buf; + timer_start(3); + while (true) { + int bytes_read = read(serial_fd, p, MAX_RESPONSE_LEN); + if (bytes_read < 0) { + if (errno == EAGAIN) { + if (timer_expired()) { + fprintf(stderr, "Timeout waiting for bootloader\n"); + exit(3); + } + usleep(250000); + continue; + } + fprintf(stderr, "Failed to read from serial port: %s\n", strerror(errno)); + exit(1); + } + p += bytes_read; + uint8_t *nl_p = memchr(buf, '\r', p-buf); + if (nl_p) { + *len = nl_p - buf; + memcpy(dest, buf, *len); + uint16_t new_buf_len = p - nl_p - 1; // +1 skips trailing \n + if (new_buf_len) { + p = buf; + return; + } + memcpy(buf, nl_p+2, new_buf_len); + p = buf+new_buf_len; + break; + } + } +} + +void dump_unexpected_response(uint8_t *buf, size_t len){ + uint8_t tmp[MAX_RESPONSE_LEN+1] = {0}; + memcpy(tmp, buf, len); + fprintf(stderr, "Rx'd unexpected response: %s\n", tmp); +} int main(int argc, char **argv) { int c; @@ -32,27 +74,27 @@ int main(int argc, char **argv) { write(serial_fd, fixkey_msg, sizeof(fixkey_msg)); - int bytes_read; - uint8_t buf[RESPONSE_LEN]; + size_t out_len; + uint8_t buf[MAX_RESPONSE_LEN]; - timer_start(3); - while ((bytes_read = read(serial_fd, buf, RESPONSE_LEN)) < RESPONSE_LEN) { - if (bytes_read < 0 && errno != EAGAIN) { - fprintf(stderr, "Failed to read response\n"); - return 2; - } - if (timer_expired()) { - fprintf(stderr, "Timeout waiting for response from bootloader\n"); - return 3; - } - usleep(250000); - } - if (!memcmp(buf, "ok", RESPONSE_LEN)) { - return 0; + read_response(serial_fd, buf, &out_len); + if (!memcmp(buf, "hi", 2)) { + printf("Received confirmation that payload is running"); } else { - uint8_t tmp[RESPONSE_LEN+1] = {0}; - memcpy(tmp, buf, RESPONSE_LEN); - fprintf(stderr, "Rx'd unexpected response: %s\n", tmp); + dump_unexpected_response(buf, out_len); } - return 1; + + read_response(serial_fd, buf, &out_len); + if (!memcmp(buf, "ok", 2)) { + printf("Payload reports success"); + return 0; + } else if (!memcmp(buf, "badisn", 6)) { + fprintf(stderr, "Payload reports unexpected flash contents"); + return 2; + } else if (!memcmp(buf, "patchfail", 9)) { + fprintf(stderr, "Payload reports that patch failed to apply"); + return 3; + } + dump_unexpected_response(buf, out_len); + return 4; } \ No newline at end of file diff --git a/src/image.c b/src/image.c index eb7dba2..7092fb9 100644 --- a/src/image.c +++ b/src/image.c @@ -25,6 +25,11 @@ #define IMAGE_CHUNK_SIZE 124 //216 // 124 for identical output to newtmgr #define MACRO_CHUNK_SIZE 302 //432 // 302 "" +/* + * Larger values above greatly increase speed, but seem to break older + * versions of mcuboot (like the one shipped with inital EM firmware) + */ + #define CRC16_INITIAL_CRC 0 /* what to seed crc16 with */ #define CRC_CITT_POLYMINAL 0x1021