mirror of
https://github.com/raphnet/gc_n64_usb-v3
synced 2024-12-21 23:08:53 -05:00
Tools
This commit is contained in:
parent
8a8b59f19b
commit
04f3bd8d08
@ -1,14 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "gcn64lib.h"
|
||||
#include "gc2n64_adapter.h"
|
||||
#include "hexdump.h"
|
||||
#include "ihex.h"
|
||||
|
||||
int gc2n64_adapter_echotest(gcn64_hdl_t hdl, int verbose)
|
||||
{
|
||||
unsigned char cmd[35];
|
||||
unsigned char buf[64];
|
||||
int i, n;
|
||||
int times;
|
||||
|
||||
cmd[0] = 'R';
|
||||
cmd[1] = 0x00; // echo
|
||||
@ -21,17 +25,16 @@ int gc2n64_adapter_echotest(gcn64_hdl_t hdl, int verbose)
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
if (verbose) {
|
||||
printf(" Sent [%d]: ", 35); printHexBuf(cmd, 35);
|
||||
printf("Received [%d]: ", n); printHexBuf(buf, n);
|
||||
if (!memcmp(cmd, buf, 35)) {
|
||||
printf("Test OK\n");
|
||||
} else {
|
||||
if ((n != 35) || memcmp(cmd, buf, 35)) {
|
||||
printf("Test failed\n");
|
||||
printf(" Sent [%d]: ", 35); printHexBuf(cmd, 35);
|
||||
printf("Received [%d]: ", n); printHexBuf(buf, n);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return memcmp(cmd, buf, 32);
|
||||
return (n!= 35) || memcmp(cmd, buf, 35);
|
||||
}
|
||||
|
||||
int gc2n64_adapter_getMapping(gcn64_hdl_t hdl, int id)
|
||||
@ -138,6 +141,55 @@ int gc2n64_adapter_getInfo(gcn64_hdl_t hdl, struct gc2n64_adapter_info *inf)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gc2n64_adapter_boot_isBusy(gcn64_hdl_t hdl)
|
||||
{
|
||||
unsigned char buf[64];
|
||||
int n;
|
||||
|
||||
buf[0] = 'R';
|
||||
buf[1] = 0xf9;
|
||||
|
||||
n = gcn64lib_rawSiCommand(hdl, 0, buf, 2, buf, sizeof(buf));
|
||||
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
|
||||
}
|
||||
|
||||
int gc2n64_adapter_boot_waitNotBusy(gcn64_hdl_t hdl, int verbose)
|
||||
{
|
||||
char spinner[4] = { '|','/','-','\\' };
|
||||
int busy, no_reply_count=0;
|
||||
int c=0;
|
||||
|
||||
while ((busy = gc2n64_adapter_boot_isBusy(hdl)))
|
||||
{
|
||||
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++;
|
||||
usleep(50000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gc2n64_adapter_boot_eraseAll(gcn64_hdl_t hdl)
|
||||
{
|
||||
unsigned char buf[64];
|
||||
@ -150,8 +202,13 @@ int gc2n64_adapter_boot_eraseAll(gcn64_hdl_t hdl)
|
||||
if (n<0)
|
||||
return n;
|
||||
|
||||
if (n != 3) {
|
||||
fprintf(stderr, "Invalid answer\n");
|
||||
if (n != 1) {
|
||||
fprintf(stderr, "Invalid answer. %d bytes received.\n", n);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (buf[0] != 0x00) {
|
||||
fprintf(stderr, "eraseAll request NACK!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -197,8 +254,221 @@ int gc2n64_adapter_dumpFlash(gcn64_hdl_t hdl)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gc2n64_adapter_updateFirmware(gcn64_hdl_t hdl, const char *hexfile)
|
||||
int gc2n64_adapter_enterBootloader(gcn64_hdl_t hdl)
|
||||
{
|
||||
unsigned char buf[64];
|
||||
int n;
|
||||
|
||||
buf[0] = 'R';
|
||||
buf[1] = 0xff;
|
||||
|
||||
n = gcn64lib_rawSiCommand(hdl, 0, buf, 4 + 32, buf, sizeof(buf));
|
||||
if (n<0)
|
||||
return n;
|
||||
|
||||
// No answer since the effect is immediate.
|
||||
usleep(100000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gc2n64_adapter_bootApplication(gcn64_hdl_t hdl)
|
||||
{
|
||||
unsigned char buf[64];
|
||||
int n;
|
||||
|
||||
buf[0] = 'R';
|
||||
buf[1] = 0xfe;
|
||||
|
||||
n = gcn64lib_rawSiCommand(hdl, 0, buf, 4 + 32, buf, sizeof(buf));
|
||||
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
|
||||
int gc2n64_adapter_sendFirmwareBlocks(gcn64_hdl_t hdl, unsigned char *firmware, int len)
|
||||
{
|
||||
unsigned char buf[64];
|
||||
int i, block_id;
|
||||
int n;
|
||||
|
||||
for (i=0; i<len; i+=32) {
|
||||
block_id = i / 32;
|
||||
|
||||
printf("Block %d / %d\r", block_id+1, len / 32); fflush(stdout);
|
||||
|
||||
buf[0] = 'R';
|
||||
buf[1] = 0xf2;
|
||||
buf[2] = block_id >> 8;
|
||||
buf[3] = block_id & 0xff;
|
||||
memcpy(buf + 4, firmware+i, 32);
|
||||
|
||||
n = gcn64lib_rawSiCommand(hdl, 0, buf, 4 + 32, buf, sizeof(buf));
|
||||
if (n<0)
|
||||
return n;
|
||||
|
||||
if (n < 1) {
|
||||
fprintf(stderr, "Invalid upload block answer\n");
|
||||
// return -1;
|
||||
goto hope;
|
||||
}
|
||||
|
||||
if (n == 1) {
|
||||
fprintf(stderr, "upload block: Busy\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (n != 4) {
|
||||
fprintf(stderr, "Invalid upload block answer 2\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// [0] ACK (should be 0x00)
|
||||
// [1] Need to poll?
|
||||
// [2] Page address
|
||||
// [3] Page address
|
||||
|
||||
if (buf[0] != 0x00) {
|
||||
fprintf(stderr, "Invalid upload block answer 3\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (buf[1]) {
|
||||
hope:
|
||||
if (gc2n64_adapter_boot_waitNotBusy(hdl, 1)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gc2n64_adapter_verifyFirmware(gcn64_hdl_t hdl, unsigned char *firmware, int len)
|
||||
{
|
||||
unsigned char buf[32];
|
||||
int i;
|
||||
|
||||
for (i=0; i<len; i+=32) {
|
||||
|
||||
gc2n64_adapter_boot_readBlock(hdl, i/32, buf);
|
||||
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);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gc2n64_adapter_waitForBootloader(gcn64_hdl_t hdl, int timeout_s)
|
||||
{
|
||||
struct gc2n64_adapter_info inf;
|
||||
int i;
|
||||
int n;
|
||||
|
||||
for (i=0; i<=timeout_s; i++) {
|
||||
n = gc2n64_adapter_getInfo(hdl, &inf);
|
||||
// Errors (caused by timeouts) are just ignored since they are expected.
|
||||
if (n == 0) {
|
||||
if (inf.in_bootloader)
|
||||
return 0;
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
#define FIRMWARE_BUF_SIZE 0x10000
|
||||
int gc2n64_adapter_updateFirmware(gcn64_hdl_t hdl, const char *hexfile)
|
||||
{
|
||||
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");
|
||||
gc2n64_adapter_getInfo(hdl, &inf);
|
||||
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");
|
||||
gc2n64_adapter_enterBootloader(hdl);
|
||||
printf("waiting for bootloader...\n");
|
||||
res = gc2n64_adapter_waitForBootloader(hdl, 5);
|
||||
if (res<0) {
|
||||
fprintf(stderr, "Bootloader did not start?\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////
|
||||
printf("gc2n64 firmware update, step [4/7] : Erase current firmware... "); fflush(stdout);
|
||||
gc2n64_adapter_boot_eraseAll(hdl);
|
||||
|
||||
if (gc2n64_adapter_boot_waitNotBusy(hdl, 1)) {
|
||||
return -1;
|
||||
}
|
||||
printf("Ok\n");
|
||||
|
||||
////////////////////
|
||||
printf("gc2n64 firmware update, step [5/7] : Write new firmware...\n");
|
||||
res = gc2n64_adapter_sendFirmwareBlocks(hdl, buf, inf.bootldr.bootloader_start_address);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("gc2n64 firmware update, step [6/7] : Verify firmware...\n");
|
||||
res = gc2n64_adapter_verifyFirmware(hdl, buf, max_addr);
|
||||
if (res < 0) {
|
||||
printf("Verify failed : Update failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("gc2n64 firmware update, step [7/7] : Launch new firmware.\n");
|
||||
gc2n64_adapter_bootApplication(hdl);
|
||||
|
||||
err:
|
||||
free(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
@ -27,11 +27,16 @@ struct gc2n64_adapter_info {
|
||||
int gc2n64_adapter_echotest(gcn64_hdl_t hdl, int verbosee);
|
||||
int gc2n64_adapter_getInfo(gcn64_hdl_t hdl, struct gc2n64_adapter_info *inf);
|
||||
void gc2n64_adapter_printInfo(struct gc2n64_adapter_info *inf);
|
||||
|
||||
int gc2n64_adapter_boot_eraseAll(gcn64_hdl_t hdl);
|
||||
int gc2n64_adapter_boot_readBlock(gcn64_hdl_t hdl, unsigned int block_id, unsigned char dst[32]);
|
||||
int gc2n64_adapter_dumpFlash(gcn64_hdl_t hdl);
|
||||
int gc2n64_adapter_updateFirmware(gcn64_hdl_t hdl, const char *hexfile);
|
||||
|
||||
int gc2n64_adapter_enterBootloader(gcn64_hdl_t hdl);
|
||||
int gc2n64_adapter_bootApplication(gcn64_hdl_t hdl);
|
||||
int gc2n64_adapter_sendFirmwareBlocks(gcn64_hdl_t hdl, unsigned char *firmware, int len);
|
||||
int gc2n64_adapter_verifyFirmware(gcn64_hdl_t hdl, unsigned char *firmware, int len);
|
||||
int gc2n64_adapter_waitForBootloader(gcn64_hdl_t hdl, int timeout_s);
|
||||
|
||||
#endif // _gc2n64_adapter_h__
|
||||
|
||||
|
50
tool/main.c
50
tool/main.c
@ -44,6 +44,7 @@ static void printUsage(void)
|
||||
printf(" -s serial Operate on specified device (required unless -f is specified)\n");
|
||||
printf(" -f, --force If no serial is specified, use first device detected.\n");
|
||||
printf(" -o, --outfile file Output file for read operations (eg: --n64-mempak-dump)\n");
|
||||
printf(" --nonstop Continue testing forever or until an error occurs.\n");
|
||||
printf("\n");
|
||||
printf("Configuration commands:\n");
|
||||
printf(" --set_serial serial Assign a new device serial number\n");
|
||||
@ -66,9 +67,14 @@ static void printUsage(void)
|
||||
printf("\n");
|
||||
printf("GC to N64 adapter commands: (For GC to N64 adapter connected to GC/N64 to USB adapter)\n");
|
||||
printf(" --gc_to_n64_info Display info on adapter (version, config, etc)\n");
|
||||
printf(" --gc_to_n64_echotest Perform a communication test\n");
|
||||
printf(" --gc_to_n64_update file.hex Update GC to N64 adapter firmware\n");
|
||||
printf("\n");
|
||||
printf("GC to N64 adapter, development/debug commands:\n");
|
||||
printf(" --gc_to_n64_echotest Perform a communication test (usable with --nonstop)\n");
|
||||
printf(" --gc_to_n64_dump Display the firmware content in hex.\n");
|
||||
printf(" --gc_to_n64_enter_bootloader Jump to the bootloader.\n");
|
||||
printf(" --gc_to_n64_boot_application Exit bootloader and start application.\n");
|
||||
printf("\n");
|
||||
printf("Development/Experimental/Research commands: (use at your own risk)\n");
|
||||
printf(" --si_8bit_scan Try all possible 1-byte commands, to see which one a controller responds to.\n");
|
||||
printf(" --si_16bit_scan Try all possible 2-byte commands, to see which one a controller responds to.\n");
|
||||
@ -94,6 +100,10 @@ static void printUsage(void)
|
||||
#define OPT_GC_TO_N64_INFO 313
|
||||
#define OPT_GC_TO_N64_TEST 314
|
||||
#define OPT_GC_TO_N64_UPDATE 315
|
||||
#define OPT_GC_TO_N64_DUMP 316
|
||||
#define OPT_GC_TO_N64_ENTER_BOOTLOADER 317
|
||||
#define OPT_GC_TO_N64_BOOT_APPLICATION 318
|
||||
#define OPT_NONSTOP 319
|
||||
|
||||
struct option longopts[] = {
|
||||
{ "help", 0, NULL, 'h' },
|
||||
@ -118,6 +128,10 @@ struct option longopts[] = {
|
||||
{ "gc_to_n64_info", 0, NULL, OPT_GC_TO_N64_INFO },
|
||||
{ "gc_to_n64_echotest", 0, NULL, OPT_GC_TO_N64_TEST },
|
||||
{ "gc_to_n64_update", 1, NULL, OPT_GC_TO_N64_UPDATE },
|
||||
{ "gc_to_n64_dump", 0, NULL, OPT_GC_TO_N64_DUMP },
|
||||
{ "gc_to_n64_enter_bootloader", 0, NULL, OPT_GC_TO_N64_ENTER_BOOTLOADER },
|
||||
{ "gc_to_n64_boot_application", 0, NULL, OPT_GC_TO_N64_BOOT_APPLICATION },
|
||||
{ "nonstop", 0, NULL, OPT_NONSTOP },
|
||||
{ },
|
||||
};
|
||||
|
||||
@ -151,6 +165,7 @@ int main(int argc, char **argv)
|
||||
struct gcn64_info inf;
|
||||
struct gcn64_info *selected_device = NULL;
|
||||
int verbose = 0, use_first = 0, serial_specified = 0;
|
||||
int nonstop = 0;
|
||||
int cmd_list = 0;
|
||||
#define TARGET_SERIAL_CHARS 128
|
||||
wchar_t target_serial[TARGET_SERIAL_CHARS];
|
||||
@ -187,6 +202,9 @@ int main(int argc, char **argv)
|
||||
outfile = optarg;
|
||||
printf("Output file: %s\n", outfile);
|
||||
break;
|
||||
case OPT_NONSTOP:
|
||||
nonstop = 1;
|
||||
break;
|
||||
case '?':
|
||||
fprintf(stderr, "Unrecognized argument. Try -h\n");
|
||||
return -1;
|
||||
@ -355,15 +373,39 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
|
||||
case OPT_GC_TO_N64_TEST:
|
||||
n = gc2n64_adapter_echotest(hdl, 1);
|
||||
if (n != 0) {
|
||||
return -1;
|
||||
{
|
||||
int i=0;
|
||||
|
||||
do {
|
||||
n = gc2n64_adapter_echotest(hdl, 1);
|
||||
if (n != 0) {
|
||||
printf("Test failed\n");
|
||||
return -1;
|
||||
}
|
||||
usleep(1000 * (i));
|
||||
i++;
|
||||
if (i>100)
|
||||
i=0;
|
||||
} while (nonstop);
|
||||
printf("Test ok\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case OPT_GC_TO_N64_UPDATE:
|
||||
gc2n64_adapter_updateFirmware(hdl, optarg);
|
||||
break;
|
||||
|
||||
case OPT_GC_TO_N64_DUMP:
|
||||
gc2n64_adapter_dumpFlash(hdl);
|
||||
break;
|
||||
|
||||
case OPT_GC_TO_N64_ENTER_BOOTLOADER:
|
||||
gc2n64_adapter_enterBootloader(hdl);
|
||||
break;
|
||||
|
||||
case OPT_GC_TO_N64_BOOT_APPLICATION:
|
||||
gc2n64_adapter_bootApplication(hdl);
|
||||
break;
|
||||
}
|
||||
|
||||
if (do_exchange) {
|
||||
|
Loading…
Reference in New Issue
Block a user