static void lvlhsh_free(void *mem, void *p) { nxt_free(p); }
int main (int argc, char **argv) { const char *filename; tx_data_t data; nxt_link_t nxt; uint8_t buffer[USB_BUFSIZE]; size_t read, sent; ssize_t total; FILE *ro_data; if (argc < 2) { fprintf(stderr, "Provide the ROM image as first parameter.\n"); exit(1); } filename = argv[1]; // FIXME this should be parametrized. Basically it's the address of // the first unlocked flash memory location. data.start_address = 0x108000; total = data.length = get_ro_size(filename); if (total == -1) { fprintf(stderr, "Invalid ROM image\n"); exit(1); } if (nxt_init(&nxt)) { fprintf(stderr, "Initialization failed\n"); exit(1); } ro_data = fopen(filename, "rb"); if (ro_data == NULL) { fprintf(stderr, "Error in opening %s\n", filename); nxt_free(&nxt); } fprintf(stdout, "Sync sent: %d bytes\n" " start=%p\n" " length=%d\n", (int)nxt_send(&nxt, (void *)&data, sizeof(tx_data_t)), (void *)(uintmax_t)data.start_address, data.length); while ((read = fread((void *)buffer, 1, USB_BUFSIZE, ro_data)) > 0) { sent = nxt_send(&nxt, (void *)buffer, read); data.length -= sent; data.start_address += sent; display_percentage(total, &data); if (sent == -1) { fprintf(stderr, "Send failed: %s\r", nxt_libusb_strerr(&nxt)); break; } usleep(DELAY_uSECS); } fprintf(stdout, "\nCompleted\n"); if (ferror(ro_data)) { fprintf(stderr, "File error in %s\n", filename); } fclose(ro_data); nxt_free(&nxt); exit(0); }