#include #include #include #include "output.h" #include "jtaglib.h" #include "sr_global.h" #include "mspdebug_wrapper.h" #define BLOCK_SIZE 512 /* bytes */ static struct jtdev sr_jtdev; int flash_and_reset(size_t img_start, size_t img_len, ssize_t (*read_block)(int addr, size_t len, uint8_t *out)) { union { uint8_t bytes[BLOCK_SIZE]; uint16_t words[BLOCK_SIZE/2]; } block; /* Initialize JTAG connection */ unsigned int jtag_id = jtag_init(&sr_jtdev); if (sr_jtdev.failed) return -EPIPE; if (jtag_id != 0x89 && jtag_id != 0x91) return -EINVAL; /* Clear flash */ jtag_erase_flash(&sr_jtdev, JTAG_ERASE_MAIN, 0); if (sr_jtdev.failed) return -EPIPE; /* Write flash */ for (size_t p = img_start; p < img_start + img_len; p += BLOCK_SIZE) { ssize_t nin = read_block(p, BLOCK_SIZE, block.bytes); if (nin < 0) return nin; if (nin & 1) { /* pad unaligned */ block.bytes[nin] = 0; nin ++; } /* Convert to little-endian */ for (ssize_t i=0; i