From fd83e0ae7d31b3b5998639715681793845fd6bed Mon Sep 17 00:00:00 2001 From: Raphael Assenat Date: Sun, 23 Aug 2015 13:42:27 -0400 Subject: [PATCH] Add dump memcard to file --- tool/main.c | 11 ++++++++--- tool/mempak.c | 36 ++++++++++++++++++++++++++++++++++++ tool/mempak.h | 2 ++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/tool/main.c b/tool/main.c index 0a60ca9..ae9cb8a 100644 --- a/tool/main.c +++ b/tool/main.c @@ -26,6 +26,7 @@ #include "version.h" #include "gcn64.h" +#include "mempak.h" #include "../requests.h" #include "../gcn64_protocol.h" @@ -54,7 +55,7 @@ static void printUsage(void) printf(" --gc_getstatus Read GC controller status now (turns rumble OFF)\n"); printf(" --gc_getstatus_rumble Read GC controller status now (turns rumble ON)\n"); printf(" --n64_getcaps Get N64 controller capabilities (or status such as pak present)\n"); - printf(" --n64_mempak_dump Dump (display) N64 mempak contents\n"); + printf(" --n64_mempak_dump Dump N64 mempak contents (Use with --outfile to write to file)\n"); } #define OPT_OUTFILE 'o' @@ -81,7 +82,7 @@ struct option longopts[] = { { "n64_getcaps", 0, NULL, OPT_N64_GETCAPS }, { "n64_mempak_dump", 0, NULL, OPT_N64_MEMPAK_DUMP }, { "suspend_polling", 0, NULL, OPT_SUSPEND_POLLING }, - { "outfile", 0, NULL, OPT_OUTFILE }, + { "outfile", 1, NULL, OPT_OUTFILE }, { }, }; @@ -273,7 +274,11 @@ int main(int argc, char **argv) break; case OPT_N64_MEMPAK_DUMP: - mempak_dump(hdl); + if (outfile) { + mempak_dumpToFile(hdl, outfile); + } else { + mempak_dump(hdl); + } break; } diff --git a/tool/mempak.c b/tool/mempak.c index b4df0ca..66d087f 100644 --- a/tool/mempak.c +++ b/tool/mempak.c @@ -3,6 +3,7 @@ #include #include #include "gcn64.h" +#include "mempak.h" #include "../gcn64_protocol.h" #include "../requests.h" @@ -207,3 +208,38 @@ void mempak_dump(gcn64_hdl_t hdl) printf("\n"); } } + +#define NUM_COPIES 4 + +int mempak_dumpToFile(gcn64_hdl_t hdl, const char *out_filename) +{ + unsigned char cardbuf[0x8000]; + FILE *fptr; + int copies; + + printf("Init pak\n"); + mempak_init(hdl); + printf("Reading card...\n"); + mempak_readAll(hdl, cardbuf); + + printf("Writing to file '%s'\n", out_filename); + fptr = fopen(out_filename, "w"); + if (!fptr) { + perror("fopen"); + return -1; + } + + for (copies = 0; copies < NUM_COPIES; copies++) { + if (1 != fwrite(cardbuf, sizeof(cardbuf), 1, fptr)) { + perror("fwrite"); + fclose(fptr); + return -2; + } + } + + printf("Done\n"); + + fclose(fptr); + return 0; +} + diff --git a/tool/mempak.h b/tool/mempak.h index dc207bb..91ccddd 100644 --- a/tool/mempak.h +++ b/tool/mempak.h @@ -1,4 +1,6 @@ void mempak_dump(gcn64_hdl_t hdl); int mempak_readBlock(gcn64_hdl_t hdl, unsigned short addr, unsigned char dst[32]); +int mempak_dumpToFile(gcn64_hdl_t hdl, const char *out_filename); +