2015-10-03 10:47:53 -04:00
|
|
|
#include <stdio.h>
|
2015-10-03 22:12:23 -04:00
|
|
|
#include <stdlib.h>
|
2015-10-03 10:47:53 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include "gcn64lib.h"
|
|
|
|
#include "gc2n64_adapter.h"
|
|
|
|
#include "hexdump.h"
|
2015-10-03 22:12:23 -04:00
|
|
|
#include "ihex.h"
|
2015-10-05 21:12:52 -04:00
|
|
|
#include "delay.h"
|
2015-10-03 10:47:53 -04:00
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_echotest(gcn64_hdl_t hdl, int channel, int verbose)
|
2015-10-03 10:47:53 -04:00
|
|
|
{
|
2015-10-05 23:21:38 -04:00
|
|
|
unsigned char cmd[30];
|
|
|
|
unsigned char buf[30];
|
2015-10-03 10:47:53 -04:00
|
|
|
int i, n;
|
|
|
|
|
|
|
|
cmd[0] = 'R';
|
|
|
|
cmd[1] = 0x00; // echo
|
2015-10-05 23:21:38 -04:00
|
|
|
for (i=0; i<28; i++) {
|
2015-10-03 10:47:53 -04:00
|
|
|
cmd[i+2] = 'A'+i;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, cmd, sizeof(buf), buf, sizeof(buf));
|
2015-10-03 10:47:53 -04:00
|
|
|
if (n<0) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-10-03 22:12:23 -04:00
|
|
|
|
2015-10-03 10:47:53 -04:00
|
|
|
if (verbose) {
|
2015-10-05 23:21:38 -04:00
|
|
|
if ((n != sizeof(buf)) || memcmp(cmd, buf, sizeof(buf))) {
|
2015-10-03 10:47:53 -04:00
|
|
|
printf("Test failed\n");
|
2015-10-05 23:21:38 -04:00
|
|
|
printf(" Sent [%d]: ", (int)sizeof(cmd)); printHexBuf(cmd, sizeof(cmd));
|
2015-10-03 22:12:23 -04:00
|
|
|
printf("Received [%d]: ", n); printHexBuf(buf, n);
|
|
|
|
return -1;
|
2015-10-03 10:47:53 -04:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 23:21:38 -04:00
|
|
|
return (n!= sizeof(buf)) || memcmp(cmd, buf, sizeof(buf));
|
2015-10-03 10:47:53 -04:00
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_getMapping(gcn64_hdl_t hdl, int channel, int id)
|
2015-10-03 10:47:53 -04:00
|
|
|
{
|
|
|
|
unsigned char buf[64];
|
|
|
|
unsigned char cmd[4];
|
|
|
|
int n;
|
|
|
|
int mapping_size;
|
|
|
|
|
|
|
|
cmd[0] = 'R';
|
|
|
|
cmd[1] = 0x02; // Get mapping
|
|
|
|
cmd[2] = id;
|
|
|
|
cmd[3] = 0; // chunk 0 (size)
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, cmd, 4, buf, 4);
|
2015-10-03 10:47:53 -04:00
|
|
|
if (n<0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (n == 1) {
|
|
|
|
int i, pos;
|
|
|
|
mapping_size = buf[0];
|
|
|
|
printf("Mapping %d size: %d\n", id, mapping_size);
|
|
|
|
|
|
|
|
for (pos=0, i=0; pos<mapping_size; i++) {
|
|
|
|
cmd[0] = 'R';
|
|
|
|
cmd[1] = 0x02; // Get mapping
|
|
|
|
cmd[2] = id;
|
|
|
|
cmd[3] = i+1; // chunk 1 is first 32 byte block, 2nd is next 32 bytes, etc
|
|
|
|
printf("Getting block %d\n", i+1);
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, cmd, 4, buf + pos, 32);
|
2015-10-03 10:47:53 -04:00
|
|
|
if (n<0) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
printf("ret: %d\n", n);
|
|
|
|
if (n==0)
|
|
|
|
break;
|
|
|
|
pos += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Received %d bytes\n", pos);
|
|
|
|
printHexBuf(buf, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void gc2n64_adapter_printInfo(struct gc2n64_adapter_info *inf)
|
|
|
|
{
|
|
|
|
if (!inf->in_bootloader) {
|
|
|
|
printf("gc_to_n64 adapter info: {\n");
|
|
|
|
|
|
|
|
printf("\tDefault mapping id: %d\n", inf->app.default_mapping_id);
|
|
|
|
printf("\tDeadzone enabled: %d\n", inf->app.deadzone_enabled);
|
|
|
|
printf("\tOld v1.5 conversion: %d\n", inf->app.old_v1_5_conversion);
|
|
|
|
printf("\tFirmware version: %s\n", inf->app.version);
|
|
|
|
} else {
|
|
|
|
printf("gc_to_n64 adapter in bootloader mode: {\n");
|
|
|
|
|
|
|
|
printf("\tBootloader firmware version: %s\n", inf->bootldr.version);
|
|
|
|
printf("\tMCU page size: %d bytes\n", inf->bootldr.mcu_page_size);
|
|
|
|
printf("\tBootloader code start address: 0x%04x\n", inf->bootldr.bootloader_start_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_getInfo(gcn64_hdl_t hdl, int channel, struct gc2n64_adapter_info *inf)
|
2015-10-03 10:47:53 -04:00
|
|
|
{
|
2015-10-05 23:21:38 -04:00
|
|
|
unsigned char buf[32];
|
2015-10-03 10:47:53 -04:00
|
|
|
int n;
|
|
|
|
|
|
|
|
buf[0] = 'R';
|
|
|
|
buf[1] = 0x01; // Get device info
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, buf, 2, buf, sizeof(buf));
|
2015-10-03 10:47:53 -04:00
|
|
|
if (n<0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
if (!inf)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
inf->in_bootloader = buf[0];
|
|
|
|
|
|
|
|
if (!inf->in_bootloader) {
|
|
|
|
inf->app.default_mapping_id = buf[2];
|
|
|
|
inf->app.deadzone_enabled = buf[3];
|
|
|
|
inf->app.old_v1_5_conversion = buf[4];
|
|
|
|
inf->app.version[sizeof(inf->app.version)-1]=0;
|
|
|
|
strncpy(inf->app.version, (char*)buf+10, sizeof(inf->app.version)-1);
|
|
|
|
} else {
|
|
|
|
inf->bootldr.mcu_page_size = buf[1];
|
|
|
|
inf->bootldr.bootloader_start_address = buf[2] << 8 | buf[3];
|
|
|
|
inf->bootldr.version[sizeof(inf->bootldr.version)-1]=0;
|
|
|
|
strncpy(inf->bootldr.version, (char*)buf+10, sizeof(inf->bootldr.version)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
printf("No answer (old version?)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_boot_isBusy(gcn64_hdl_t hdl, int channel)
|
2015-10-03 22:12:23 -04:00
|
|
|
{
|
|
|
|
unsigned char buf[64];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
buf[0] = 'R';
|
|
|
|
buf[1] = 0xf9;
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, buf, 2, buf, 1);
|
2015-10-03 22:12:23 -04:00
|
|
|
if (n<0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (n != 1) {
|
|
|
|
return 2; // Busy inferred from lack of answer
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[0] != 0x00) {
|
|
|
|
return 1; // Busy
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // Idle
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_boot_waitNotBusy(gcn64_hdl_t hdl, int channel, int verbose)
|
2015-10-03 22:12:23 -04:00
|
|
|
{
|
|
|
|
char spinner[4] = { '|','/','-','\\' };
|
|
|
|
int busy, no_reply_count=0;
|
|
|
|
int c=0;
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
while ((busy = gc2n64_adapter_boot_isBusy(hdl, channel)))
|
2015-10-03 22:12:23 -04:00
|
|
|
{
|
|
|
|
if (busy < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (busy == 2) {
|
|
|
|
no_reply_count++;
|
|
|
|
if (no_reply_count > 200) {
|
|
|
|
fprintf(stderr, "Adapter answer timeout\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%c\b", spinner[c%4]); fflush(stdout);
|
|
|
|
c++;
|
2015-10-05 21:12:52 -04:00
|
|
|
_delay_us(50000);
|
2015-10-03 22:12:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_boot_eraseAll(gcn64_hdl_t hdl, int channel)
|
2015-10-03 10:47:53 -04:00
|
|
|
{
|
|
|
|
unsigned char buf[64];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
buf[0] = 'R';
|
|
|
|
buf[1] = 0xf0;
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, buf, 2, buf, 1);
|
2015-10-03 10:47:53 -04:00
|
|
|
if (n<0)
|
|
|
|
return n;
|
|
|
|
|
2015-10-03 22:12:23 -04:00
|
|
|
if (n != 1) {
|
|
|
|
fprintf(stderr, "Invalid answer. %d bytes received.\n", n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[0] != 0x00) {
|
|
|
|
fprintf(stderr, "eraseAll request NACK!\n");
|
2015-10-03 10:47:53 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_boot_readBlock(gcn64_hdl_t hdl, int channel, unsigned int block_id, unsigned char dst[32])
|
2015-10-03 10:47:53 -04:00
|
|
|
{
|
2015-10-05 23:21:38 -04:00
|
|
|
unsigned char buf[32];
|
2015-10-03 10:47:53 -04:00
|
|
|
int n;
|
|
|
|
|
|
|
|
buf[0] = 'R';
|
|
|
|
buf[1] = 0xf1;
|
|
|
|
buf[2] = block_id >> 8;
|
|
|
|
buf[3] = block_id & 0xff;
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, buf, 4, buf, sizeof(buf));
|
2015-10-03 10:47:53 -04:00
|
|
|
if (n<0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (n != 32) {
|
|
|
|
fprintf(stderr, "Invalid answer\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dst, buf, 32);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_dumpFlash(gcn64_hdl_t hdl, int channel)
|
2015-10-03 10:47:53 -04:00
|
|
|
{
|
|
|
|
int i;
|
2015-10-05 23:21:38 -04:00
|
|
|
unsigned char buf[0x10000];
|
|
|
|
struct gc2n64_adapter_info inf;
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
i = gc2n64_adapter_getInfo(hdl, channel, &inf);
|
2015-10-05 23:21:38 -04:00
|
|
|
if (i)
|
|
|
|
return i;
|
2015-10-03 10:47:53 -04:00
|
|
|
|
2015-10-05 23:21:38 -04:00
|
|
|
if (!inf.in_bootloader) {
|
|
|
|
fprintf(stderr, "dumpFlash: Nnot in bootloader\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Atmega168 : 16K
|
|
|
|
for (i=0; i<16*1024; i+= 32)
|
2015-10-03 10:47:53 -04:00
|
|
|
{
|
2015-10-07 23:40:41 -04:00
|
|
|
gc2n64_adapter_boot_readBlock(hdl, channel, i/32, buf + i);
|
2015-10-03 10:47:53 -04:00
|
|
|
printf("0x%04x: ", i);
|
|
|
|
printHexBuf(buf + i, 32);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_enterBootloader(gcn64_hdl_t hdl, int channel)
|
2015-10-03 22:12:23 -04:00
|
|
|
{
|
2015-10-05 23:21:38 -04:00
|
|
|
unsigned char buf[4];
|
2015-10-03 22:12:23 -04:00
|
|
|
int n;
|
2015-10-05 23:21:38 -04:00
|
|
|
int t = 100; // > 100ms timeout
|
2015-10-03 22:12:23 -04:00
|
|
|
|
|
|
|
buf[0] = 'R';
|
|
|
|
buf[1] = 0xff;
|
|
|
|
|
2015-10-05 23:21:38 -04:00
|
|
|
/* The bootloader starts the application automatically if it is
|
|
|
|
* installed. To prevent the application from being restarted right
|
|
|
|
* away when are entering the bootloader, the bootloader waits
|
|
|
|
* 50 ms at startup, and if it receives the 'enter bootloader' command
|
|
|
|
* within this window, the application is not started.
|
|
|
|
*
|
|
|
|
* Also, contrary to the application, the bootloader actually answers
|
|
|
|
* this command. So it doubles as a handshake to know the bootloader has
|
|
|
|
* started and is ready to receive instructions.
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
do {
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, buf, 2, buf, sizeof(buf));
|
2015-10-05 23:21:38 -04:00
|
|
|
if (n<0) {
|
2015-10-03 22:12:23 -04:00
|
|
|
return n;
|
2015-10-05 23:21:38 -04:00
|
|
|
}
|
|
|
|
_delay_us(1000);
|
|
|
|
t--;
|
|
|
|
if (!t) {
|
|
|
|
fprintf(stderr, "Timeout waiting for bootloader\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(n==0);
|
2015-10-03 22:12:23 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_bootApplication(gcn64_hdl_t hdl, int channel)
|
2015-10-03 22:12:23 -04:00
|
|
|
{
|
2015-10-05 23:21:38 -04:00
|
|
|
unsigned char buf[2];
|
2015-10-03 22:12:23 -04:00
|
|
|
int n;
|
|
|
|
|
|
|
|
buf[0] = 'R';
|
|
|
|
buf[1] = 0xfe;
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, buf, 2, buf, 1);
|
2015-10-03 22:12:23 -04:00
|
|
|
if (n<0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (n != 1) {
|
|
|
|
fprintf(stderr, "boot application: Invalid answer\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[0]) {
|
|
|
|
fprintf(stderr, "Boot nack\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: eraseAll needs to be performed first
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_sendFirmwareBlocks(gcn64_hdl_t hdl, int channel, unsigned char *firmware, int len)
|
2015-10-03 22:12:23 -04:00
|
|
|
{
|
|
|
|
unsigned char buf[64];
|
|
|
|
int i, block_id;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (i=0; i<len; i+=32) {
|
|
|
|
block_id = i / 32;
|
|
|
|
buf[0] = 'R';
|
|
|
|
buf[1] = 0xf2;
|
|
|
|
buf[2] = block_id >> 8;
|
|
|
|
buf[3] = block_id & 0xff;
|
|
|
|
memcpy(buf + 4, firmware+i, 32);
|
|
|
|
|
2015-10-05 23:21:38 -04:00
|
|
|
printf("Block %d / %d\r", block_id+1, len / 32); fflush(stdout);
|
2015-10-03 22:12:23 -04:00
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gcn64lib_rawSiCommand(hdl, channel, buf, 4 + 32, buf, 4);
|
2015-10-05 23:21:38 -04:00
|
|
|
if (n<0) {
|
2015-10-07 23:40:41 -04:00
|
|
|
fprintf(stderr, "\nRaw command failed\n");
|
2015-10-05 23:21:38 -04:00
|
|
|
return n;
|
2015-10-03 22:12:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n != 4) {
|
2015-10-05 23:21:38 -04:00
|
|
|
fprintf(stderr, "\nInvalid upload block answer\n");
|
2015-10-03 22:12:23 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [0] ACK (should be 0x00)
|
|
|
|
// [1] Need to poll?
|
2015-10-05 23:21:38 -04:00
|
|
|
// [2] Block ID high
|
|
|
|
// [3] Block ID low
|
2015-10-03 22:12:23 -04:00
|
|
|
|
|
|
|
if (buf[0] != 0x00) {
|
2015-10-05 23:21:38 -04:00
|
|
|
fprintf(stderr, "Busy\n");
|
2015-10-03 22:12:23 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[1]) {
|
2015-10-07 23:40:41 -04:00
|
|
|
if (gc2n64_adapter_boot_waitNotBusy(hdl, channel, 1)) {
|
2015-10-05 23:21:38 -04:00
|
|
|
fprintf(stderr, "Error waiting not busy\n");
|
2015-10-03 22:12:23 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-10-05 23:21:38 -04:00
|
|
|
|
|
|
|
// printf("\n");
|
|
|
|
// printf("Block ID: 0x%04x\n", (buf[2]<<8) | buf[3]);
|
2015-10-03 22:12:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_verifyFirmware(gcn64_hdl_t hdl, int channel, unsigned char *firmware, int len)
|
2015-10-03 10:47:53 -04:00
|
|
|
{
|
2015-10-03 22:12:23 -04:00
|
|
|
unsigned char buf[32];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<len; i+=32) {
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
gc2n64_adapter_boot_readBlock(hdl, channel, i/32, buf);
|
2015-10-03 22:12:23 -04:00
|
|
|
if (memcmp(buf, firmware + i, 32)) {
|
|
|
|
printf("\nMismatch in block address 0x%04x\n", i);
|
|
|
|
printf("Written: "); printHexBuf(firmware + i, 32);
|
|
|
|
printf(" Read: "); printHexBuf(buf, 32);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
printf("Block %d / %d ok\r", i/32 + 1, len / 32); fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 10:47:53 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_waitForBootloader(gcn64_hdl_t hdl, int channel, int timeout_s)
|
2015-10-03 22:12:23 -04:00
|
|
|
{
|
|
|
|
struct gc2n64_adapter_info inf;
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (i=0; i<=timeout_s; i++) {
|
2015-10-07 23:40:41 -04:00
|
|
|
n = gc2n64_adapter_getInfo(hdl, channel, &inf);
|
2015-10-03 22:12:23 -04:00
|
|
|
// Errors (caused by timeouts) are just ignored since they are expected.
|
|
|
|
if (n == 0) {
|
2015-10-05 23:21:38 -04:00
|
|
|
gc2n64_adapter_printInfo(&inf);
|
2015-10-03 22:12:23 -04:00
|
|
|
if (inf.in_bootloader)
|
2015-10-05 23:21:38 -04:00
|
|
|
return 0;
|
2015-10-03 22:12:23 -04:00
|
|
|
}
|
2015-10-05 21:12:52 -04:00
|
|
|
_delay_s(1);
|
2015-10-03 22:12:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FIRMWARE_BUF_SIZE 0x10000
|
2015-10-07 23:40:41 -04:00
|
|
|
int gc2n64_adapter_updateFirmware(gcn64_hdl_t hdl, int channel, const char *hexfile)
|
2015-10-03 22:12:23 -04:00
|
|
|
{
|
|
|
|
unsigned char *buf;
|
|
|
|
int max_addr;
|
|
|
|
int ret = 0, res;
|
|
|
|
struct gc2n64_adapter_info inf;
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
printf("gc2n64 firmware update, step [1/7] : Load .hex file...\n");
|
|
|
|
buf = malloc(FIRMWARE_BUF_SIZE);
|
|
|
|
if (!buf) {
|
|
|
|
perror("malloc");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(buf, 0xff, FIRMWARE_BUF_SIZE);
|
|
|
|
|
|
|
|
max_addr = load_ihex(hexfile, buf, FIRMWARE_BUF_SIZE);
|
|
|
|
if (max_addr < 0) {
|
|
|
|
fprintf(stderr, "Update failed : Could not load hex file\n");
|
|
|
|
ret = -1;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Firmware size: %d bytes\n", max_addr+1);
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
printf("gc2n64 firmware update, step [2/7] : Get adapter info...\n");
|
2015-10-07 23:40:41 -04:00
|
|
|
res = gc2n64_adapter_getInfo(hdl, channel, &inf);
|
2015-10-05 23:21:38 -04:00
|
|
|
if (res < 0) {
|
|
|
|
fprintf(stderr, "Failed to read adapter info\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-03 22:12:23 -04:00
|
|
|
gc2n64_adapter_printInfo(&inf);
|
|
|
|
|
|
|
|
if (inf.in_bootloader) {
|
|
|
|
printf("gc2n64 firmware update, step [3/7] : Enter bootloader... Skipped. Already in bootloader.\n");
|
|
|
|
} else {
|
|
|
|
printf("gc2n64 firmware update, step [3/7] : Enter bootloader...\n");
|
2015-10-07 23:40:41 -04:00
|
|
|
res = gc2n64_adapter_enterBootloader(hdl, channel);
|
2015-10-05 23:21:38 -04:00
|
|
|
if (res < 0) {
|
|
|
|
fprintf(stderr, "Failed to enter the bootloader\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-read the info structure, as we will need the bootloader start address.
|
2015-10-07 23:40:41 -04:00
|
|
|
res = gc2n64_adapter_getInfo(hdl, channel, &inf);
|
2015-10-05 23:21:38 -04:00
|
|
|
if (res < 0) {
|
|
|
|
fprintf(stderr, "Failed to read info after enterring bootloader\n");
|
2015-10-03 22:12:23 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
printf("gc2n64 firmware update, step [4/7] : Erase current firmware... "); fflush(stdout);
|
2015-10-07 23:40:41 -04:00
|
|
|
gc2n64_adapter_boot_eraseAll(hdl, channel);
|
2015-10-03 22:12:23 -04:00
|
|
|
|
2015-10-07 23:40:41 -04:00
|
|
|
if (gc2n64_adapter_boot_waitNotBusy(hdl, channel, 1)) {
|
2015-10-03 22:12:23 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
printf("Ok\n");
|
|
|
|
|
|
|
|
////////////////////
|
2015-10-05 23:21:38 -04:00
|
|
|
// We need to add a marker at the end of the application area (just before the
|
|
|
|
// bootloader) so the bootloader knows an application is installed.
|
|
|
|
if (max_addr >= inf.bootldr.bootloader_start_address - 4) {
|
|
|
|
fprintf(stderr, "No space for marker - application too large. Aborting\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf[inf.bootldr.bootloader_start_address - 4] = 0x12;
|
|
|
|
buf[inf.bootldr.bootloader_start_address - 3] = 0x34;
|
|
|
|
buf[inf.bootldr.bootloader_start_address - 2] = 0x56;
|
|
|
|
buf[inf.bootldr.bootloader_start_address - 1] = 0x78;
|
|
|
|
|
2015-10-03 22:12:23 -04:00
|
|
|
printf("gc2n64 firmware update, step [5/7] : Write new firmware...\n");
|
2015-10-05 23:21:38 -04:00
|
|
|
// Note: We write up to the bootloader, even if the firmware was shorter (it usually is).
|
|
|
|
// This is to make sure that the marker we placed at the end gets written.
|
2015-10-07 23:40:41 -04:00
|
|
|
res = gc2n64_adapter_sendFirmwareBlocks(hdl, channel, buf, inf.bootldr.bootloader_start_address);
|
2015-10-03 22:12:23 -04:00
|
|
|
if (res < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("gc2n64 firmware update, step [6/7] : Verify firmware...\n");
|
2015-10-07 23:40:41 -04:00
|
|
|
res = gc2n64_adapter_verifyFirmware(hdl, channel, buf, inf.bootldr.bootloader_start_address);
|
2015-10-03 22:12:23 -04:00
|
|
|
if (res < 0) {
|
|
|
|
printf("Verify failed : Update failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("gc2n64 firmware update, step [7/7] : Launch new firmware.\n");
|
2015-10-07 23:40:41 -04:00
|
|
|
gc2n64_adapter_bootApplication(hdl, channel);
|
2015-10-03 22:12:23 -04:00
|
|
|
|
|
|
|
err:
|
|
|
|
free(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|