2 * Copyright (c) 2009, GraÅžvydas Ignotas
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the organization nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 typedef unsigned char u8;
33 typedef unsigned short u16;
34 typedef unsigned int u32;
35 #define array_size(x) (sizeof(x) / sizeof(x[0]))
38 unsigned short vendor;
39 unsigned short product;
42 { 0x03eb, 0x202a, "16MX+U Game Device" },
43 { 0x03eb, 0x202b, "32MX+U Game Device" },
44 { 0x03eb, 0x202c, "16MX+US Game Device" },
45 { 0x03eb, 0x202d, "32MX+UF Game Device" },
48 #define VERSION "0.91"
50 #define IO_BLK_SIZE 0x2000 /* 8K */
51 #define IO_RAM_BLK_SIZE 256
53 #define CMD_ATM_READY 0x22
54 #define CMD_SEC_GET_NAME 'G' /* filename r/w */
55 #define CMD_SEC_PUT_NAME 'P'
56 #define CMD_SEC_DEVID 'L' /* read flash device ID */
57 #define CMD_SEC_ERASE 'E'
58 #define CMD_SEC_READY 'C' /* is flash ready? */
59 #define CMD_SEC_READ 'R'
60 #define CMD_SEC_WRITE 'W'
61 #define CMD_SEC_RAM_READ 'D' /* not implemented? */
62 #define CMD_SEC_RAM_WRITE 'U'
63 #define CMD_SEC_COMPAT '$' /* set RAM mode */
66 #define CTL_DATA_BUS 0x55
67 #define CTL_ADDR_BUS 0xAA
69 #define W_COUNTER 0xA0
70 #define W_CNT_WRITE 0x01
71 #define W_CNT_READ 0x00
73 #define FILENAME_ROM0 0
74 #define FILENAME_ROM1 1
75 #define FILENAME_RAM 2
77 /* windows app sets to 0x80 on init
78 * checkboxes use 0x41 0x42 0x43 (?)
79 * r,w Ram/ROM uses 0x23/0x21
81 #define C_MODE_4M_NORAM 0x41 /* RAM always off */
82 #define C_MODE_4M_RAM 0x42 /* RAM switched by game */
83 #define C_MODE_2M_RAM 0x43
84 #define C_RAM_TMP_OFF 0x21
85 #define C_RAM_TMP_ON 0x23
99 u8 addrb2; /* most significant (BE) */
102 u8 param; /* 64 byte usb packets for i/o */
126 u8 bootloader_ver[4];
136 static const page_table_t p_AM29LV320DB[] =
138 { 0x000000, 0x00ffff, 0x002000 },
139 { 0x010000, 0x3fffff, 0x010000 },
140 { 0x000000, 0x000000, 0x000000 },
143 static const page_table_t p_AM29LV320DT[] =
145 { 0x000000, 0x3effff, 0x010000 },
146 { 0x3f0000, 0x3fffff, 0x002000 },
147 { 0x000000, 0x000000, 0x000000 },
150 static const page_table_t p_2x_16[] =
152 { 0x000000, 0x003fff, 0x004000 },
153 { 0x004000, 0x007fff, 0x002000 },
154 { 0x008000, 0x00ffff, 0x008000 },
155 { 0x010000, 0x1fffff, 0x010000 },
156 { 0x200000, 0x203fff, 0x004000 },
157 { 0x204000, 0x207fff, 0x002000 },
158 { 0x208000, 0x20ffff, 0x008000 },
159 { 0x210000, 0x3fffff, 0x010000 },
160 { 0x000000, 0x000000, 0x000000 },
163 /*****************************************************************************/
165 static void prepare_cmd(dev_cmd_t *dev_cmd, u8 cmd)
167 memset(dev_cmd, 0, sizeof(*dev_cmd));
169 memcpy(dev_cmd->magic, "USBC", 4);
170 dev_cmd->magic2 = 0x67; /* MySCSICommand, EXCOMMAND */
171 dev_cmd->mx_cmd = cmd;
174 static int write_data(struct usb_dev_handle *dev, void *data, int len)
176 int ret = usb_bulk_write(dev, 0x03, data, len, 2000);
178 fprintf(stderr, "failed to write:\n");
179 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
180 } else if (ret != len)
181 printf("write_cmd: wrote only %d of %d bytes\n", ret, len);
186 static int write_cmd(struct usb_dev_handle *dev, dev_cmd_t *cmd)
188 return write_data(dev, cmd, sizeof(*cmd));
191 static int read_data(struct usb_dev_handle *dev, void *buff, int size)
193 int ret = usb_bulk_read(dev, 0x82, buff, size, 2000);
195 fprintf(stderr, "failed to read:\n");
196 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
199 else if (ret != size)
200 printf("read_data: read only %d of %d bytes\n", ret, size);
205 static int read_info(struct usb_dev_handle *device, u8 ctl_id, dev_info_t *info)
210 prepare_cmd(&cmd, CMD_ATM_READY);
211 cmd.dev_info.which_device = ctl_id;
212 memset(info, 0, sizeof(*info));
214 ret = write_cmd(device, &cmd);
218 ret = read_data(device, info, sizeof(*info));
225 static void printf_info(dev_info_t *info)
227 printf(" firmware version: %X.%X.%X%c\n", info->firmware_ver[0],
228 info->firmware_ver[1], info->firmware_ver[2], info->firmware_ver[3]);
229 printf(" bootloader version: %X.%X.%X%c\n", info->bootloader_ver[0],
230 info->bootloader_ver[1], info->bootloader_ver[2], info->bootloader_ver[3]);
231 info->names[sizeof(info->names) - 1] = 0;
232 printf(" device name: %s\n", info->names);
235 static void print_progress(u32 done, u32 total)
239 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
240 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); /* 20 */
241 printf("\b\b\b\b\b\b");
242 printf("%06x/%06x |", done, total);
245 for (i = step; i <= total; i += step)
246 printf("%c", done >= i ? '=' : '-');
247 printf("| %3d%%", done * 100 / total);
251 static int read_filename(struct usb_dev_handle *dev, char *dst, int len, u8 which)
257 prepare_cmd(&cmd, CMD_SEC_GET_NAME);
258 cmd.filename.which = which;
259 memset(buff, 0, sizeof(buff));
261 ret = write_cmd(dev, &cmd);
265 ret = read_data(dev, buff, 64);
269 strncpy(dst, buff, len);
275 static int write_filename(struct usb_dev_handle *dev, const char *fname, u8 which)
284 strncpy(buff, fname, len);
287 prepare_cmd(&cmd, CMD_SEC_PUT_NAME);
288 cmd.filename.which = which;
290 ret = write_cmd(dev, &cmd);
294 return write_data(dev, buff, len + 1);
297 static int read_erase_counter(struct usb_dev_handle *dev, u32 *val)
299 dev_info_t dummy_info;
304 /* must perform dummy info read here,
305 * or else device hangs after close (firmware bug?) */
306 ret = read_info(dev, CTL_DATA_BUS, &dummy_info);
310 prepare_cmd(&cmd, CMD_ATM_READY);
311 cmd.write_cnt.cmd = W_COUNTER;
312 cmd.write_cnt.action = W_CNT_READ;
314 ret = write_cmd(dev, &cmd);
318 ret = read_data(dev, buff, sizeof(buff));
326 static int read_flash_rom_id(struct usb_dev_handle *dev, int is_second, u32 *val)
332 prepare_cmd(&cmd, CMD_SEC_DEVID);
333 cmd.rom_id.which = is_second ? 0x10 : 0;
334 cmd.rom_id.dev_id = 0;
336 ret = write_cmd(dev, &cmd);
340 ret = read_data(dev, buff, sizeof(buff));
344 *val = *(u16 *)buff << 16;
346 cmd.rom_id.dev_id = 1;
347 ret = write_cmd(dev, &cmd);
351 ret = read_data(dev, buff, sizeof(buff));
355 *val |= *(u16 *)buff;
359 static const page_table_t *get_page_table(u32 rom_id)
363 return p_AM29LV320DB;
365 return p_AM29LV320DT;
370 fprintf(stderr, "unrecognized ROM id: %08x\n", rom_id);
376 static int get_page_size(const page_table_t *table, u32 addr, u32 *size)
378 const page_table_t *t;
380 for (t = table; t->end_addr != 0; t++) {
381 if (addr >= t->start_addr && addr <= t->end_addr) {
382 *size = t->page_size;
387 if (addr == t[-1].end_addr + 1)
388 return 1; /* no more */
390 fprintf(stderr, "get_page_size: failed on addr %06x\n", addr);
394 static int set_ram_mode(struct usb_dev_handle *dev, u8 mode)
400 prepare_cmd(&cmd, CMD_SEC_COMPAT);
402 cmd.mode.which = mode;
404 ret = write_cmd(dev, &cmd);
408 ret = read_data(dev, buff, sizeof(buff));
412 fprintf(stderr, "warning: failed to set RAM mode\n");
417 * - bytes must be multiple of 64
418 * - bytes must be less than 16k
419 * - must perform even number of reads, or dev hangs on exit (firmware bug?) */
420 static int rw_dev_block(struct usb_dev_handle *dev, u32 addr, void *buffer, int bytes, int mx_cmd)
425 prepare_cmd(&cmd, mx_cmd);
426 if (mx_cmd == CMD_SEC_WRITE || mx_cmd == CMD_SEC_RAM_WRITE)
428 cmd.rom_rw.addrb2 = addr >> (16 + 1);
429 cmd.rom_rw.addrb1 = addr >> (8 + 1);
430 cmd.rom_rw.addrb0 = addr >> 1;
431 cmd.rom_rw.param = bytes / 64;
432 if (mx_cmd == CMD_SEC_WRITE || mx_cmd == CMD_SEC_RAM_WRITE)
433 cmd.rom_rw.param2 = 1; /* ? */
435 ret = write_cmd(dev, &cmd);
441 if (mx_cmd == CMD_SEC_WRITE || mx_cmd == CMD_SEC_RAM_WRITE)
442 ret = write_data(dev, buffer, bytes);
444 ret = read_data(dev, buffer, bytes);
449 fprintf(stderr, "rw_dev_block warning: done only %d/%d bytes\n", ret, bytes);
454 static int read_write_rom(struct usb_dev_handle *dev, u32 addr, void *buffer, int bytes, int is_write)
456 int mx_cmd = is_write ? CMD_SEC_WRITE : CMD_SEC_READ;
457 int total_bytes = bytes;
463 fprintf(stderr, "read_write_rom: can't handle odd address %06x, "
464 "LSb will be ignored\n", addr);
466 fprintf(stderr, "read_write_rom: byte count must be multiple of 64, "
467 "last %d bytes will not be handled\n", bytes & 63);
469 set_ram_mode(dev, C_RAM_TMP_OFF);
471 printf("%s flash ROM...\n", is_write ? "writing to" : "reading");
473 /* do i/o in blocks */
474 for (count = 0; bytes >= IO_BLK_SIZE; count++) {
475 print_progress(buff - (u8 *)buffer, total_bytes);
477 ret = rw_dev_block(dev, addr, buff, IO_BLK_SIZE, mx_cmd);
482 bytes -= IO_BLK_SIZE;
484 print_progress(buff - (u8 *)buffer, total_bytes);
488 ret = rw_dev_block(dev, addr, buff, bytes, mx_cmd);
490 print_progress(total_bytes, total_bytes);
493 if ((count & 1) && !is_write)
494 /* work around rw_dev_block() limitation 3 (works for reads only?) */
495 rw_dev_block(dev, 0, dummy, sizeof(dummy), 0);
501 static int read_write_ram(struct usb_dev_handle *dev, void *buffer, int bytes, int is_write)
503 int mx_cmd = is_write ? CMD_SEC_RAM_WRITE : CMD_SEC_READ;
504 int total_bytes = bytes;
509 if (bytes % IO_RAM_BLK_SIZE)
510 fprintf(stderr, "read_write_ram: byte count must be multiple of %d, "
511 "last %d bytes will not be handled\n", IO_RAM_BLK_SIZE,
512 bytes % IO_RAM_BLK_SIZE);
514 set_ram_mode(dev, C_RAM_TMP_ON);
516 printf("%s RAM...\n", is_write ? "writing to" : "reading");
518 /* do i/o in blocks */
519 while (bytes >= IO_RAM_BLK_SIZE) {
520 print_progress(buff - (u8 *)buffer, total_bytes);
522 ret = rw_dev_block(dev, addr, buff, IO_RAM_BLK_SIZE, mx_cmd);
525 buff += IO_RAM_BLK_SIZE;
526 addr += IO_RAM_BLK_SIZE;
527 bytes -= IO_RAM_BLK_SIZE;
529 print_progress(buff - (u8 *)buffer, total_bytes);
531 /* only D0-D7 connected.. */
532 for (i = 0; i < total_bytes; i += 2)
533 ((u8 *)buffer)[i] = 0;
540 static int increment_erase_cnt(struct usb_dev_handle *dev)
547 ret = read_erase_counter(dev, &cnt);
551 if (cnt == (u32)-1) {
552 fprintf(stderr, "flash erase counter maxed out!\n");
553 fprintf(stderr, "(wow, did you really erase so many times?)\n");
559 prepare_cmd(&cmd, CMD_ATM_READY);
560 cmd.write_cnt.cmd = W_COUNTER;
561 cmd.write_cnt.action = W_CNT_WRITE;
562 cmd.write_cnt.b3 = cnt >> 24;
563 cmd.write_cnt.b2 = cnt >> 16;
564 cmd.write_cnt.b1 = cnt >> 8;
565 cmd.write_cnt.b0 = cnt;
567 ret = write_cmd(dev, &cmd);
571 ret = read_data(dev, buff, sizeof(buff));
578 static int erase_page(struct usb_dev_handle *dev, u32 addr, int whole)
584 prepare_cmd(&cmd, CMD_SEC_ERASE);
586 cmd.rom_rw.addrb2 = addr >> (16 + 1);
587 cmd.rom_rw.addrb1 = addr >> (8 + 1);
588 cmd.rom_rw.addrb0 = addr >> 1;
589 cmd.rom_rw.param = whole ? 0x10 : 0;
591 ret = write_cmd(dev, &cmd);
595 ret = read_data(dev, buff, sizeof(buff));
599 prepare_cmd(&cmd, CMD_SEC_READY);
600 cmd.rom_rw.addrb2 = addr >> (16 + 1);
601 cmd.rom_rw.addrb1 = addr >> (8 + 1);
602 cmd.rom_rw.addrb0 = addr >> 1;
604 for (i = 0; i < 100; i++) {
605 ret = write_cmd(dev, &cmd);
609 ret = read_data(dev, buff, sizeof(buff));
613 if (ret > 4 && buff[4] == 1)
616 usleep((whole ? 600 : 20) * 1000);
620 fprintf(stderr, "\ntimeout waiting for erase to complete\n");
627 static int erase_seq(struct usb_dev_handle *dev, u32 size)
629 const page_table_t *table;
630 u32 addr, page_size = 0;
631 u32 rom0_id, rom1_id;
634 ret = read_flash_rom_id(dev, 0, &rom0_id);
638 ret = read_flash_rom_id(dev, 1, &rom1_id);
642 if (rom0_id != rom1_id)
643 fprintf(stderr, "Warning: flash ROM ids differ: %08x %08x\n",
646 table = get_page_table(rom0_id);
650 ret = increment_erase_cnt(dev);
652 fprintf(stderr, "warning: coun't increase erase counter\n");
654 printf("erasing flash... (erase count=%u)\n", ret);
656 for (addr = 0, count = 0; addr < size; addr += page_size, count++) {
657 print_progress(addr, size);
659 ret = erase_page(dev, addr, 0);
663 ret = get_page_size(table, addr, &page_size);
670 /* must submit even number of erase commands (fw bug?) */
671 erase_page(dev, 0, 0);
673 print_progress(addr, size);
679 static int erase_all(struct usb_dev_handle *dev, u32 size)
683 ret = increment_erase_cnt(dev);
685 fprintf(stderr, "warning: couldn't increase erase counter\n");
687 printf("erasing flash0, count=%u ...", ret);
690 ret = erase_page(dev, 0xaaa, 1);
694 if (size > 0x200000) {
696 printf("erasing flash1...");
699 ret = erase_page(dev, 0x200aaa, 1);
706 static int print_device_info(struct usb_dev_handle *dev)
708 u32 counter, rom0_id, rom1_id;
712 printf("data bus controller:\n");
713 ret = read_info(dev, CTL_DATA_BUS, &info);
718 printf("address bus controller:\n");
719 ret = read_info(dev, CTL_ADDR_BUS, &info);
724 ret = read_erase_counter(dev, &counter);
727 printf("flash erase count: %u\n", counter);
729 ret = read_flash_rom_id(dev, 0, &rom0_id);
732 printf("flash rom0 id: %08x\n", rom0_id);
734 ret = read_flash_rom_id(dev, 1, &rom1_id);
737 printf("flash rom1 id: %08x\n", rom1_id);
742 static int print_game_info(struct usb_dev_handle *dev)
747 ret = read_filename(dev, fname, sizeof(fname), FILENAME_ROM0);
750 printf("ROM filename: %s\n", fname);
752 ret = read_filename(dev, fname, sizeof(fname), FILENAME_RAM);
755 printf("SRAM filename: %s\n", fname);
760 static int read_file(const char *fname, void **buff_out, int *size, int limit)
766 file = fopen(fname, "rb");
768 fprintf(stderr, "can't open file: %s\n", fname);
772 fseek(file, 0, SEEK_END);
773 file_size = ftell(file);
774 fseek(file, 0, SEEK_SET);
775 if (file_size > limit)
776 fprintf(stderr, "warning: input file \"%s\" too large\n", fname);
778 fprintf(stderr, "bad/empty file: %s\n", fname);
782 data = malloc(file_size);
784 fprintf(stderr, "low memory\n");
788 ret = fread(data, 1, file_size, file);
789 if (ret != file_size) {
790 fprintf(stderr, "failed to read file: %s", fname);
805 static int write_file(const char *fname, void *buff, int size)
810 file = fopen(fname, "wb");
812 fprintf(stderr, "can't open for writing: %s\n", fname);
816 ret = fwrite(buff, 1, size, file);
818 fprintf(stderr, "write failed to %s", fname);
821 printf("saved to \"%s\".\n", fname);
827 static usb_dev_handle *get_device(void)
829 struct usb_dev_handle *handle;
830 struct usb_device *dev;
834 ret = usb_find_busses();
836 fprintf(stderr, "Can't find USB busses\n");
840 ret = usb_find_devices();
842 fprintf(stderr, "Can't find USB devices\n");
846 bus = usb_get_busses();
847 for (; bus; bus = bus->next)
849 for (dev = bus->devices; dev; dev = dev->next)
851 for (i = 0; i < array_size(g_devices); i++)
853 if (dev->descriptor.idVendor == g_devices[i].vendor &&
854 dev->descriptor.idProduct == g_devices[i].product)
860 fprintf(stderr, "device not found.\n");
864 printf("found %s.\n", g_devices[i].name);
866 handle = usb_open(dev);
867 if (handle == NULL) {
868 fprintf(stderr, "failed to open device:\n");
869 fprintf(stderr, "%s\n", usb_strerror());
873 ret = usb_set_configuration(handle, 1);
875 fprintf(stderr, "couldn't set configuration for /*/bus/usb/%s/%s:\n",
876 bus->dirname, dev->filename);
877 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
881 ret = usb_claim_interface(handle, 0);
883 fprintf(stderr, "couldn't claim /*/bus/usb/%s/%s:\n",
884 bus->dirname, dev->filename);
885 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
892 static void release_device(struct usb_dev_handle *device)
894 usb_release_interface(device, 0);
898 static void usage(const char *app_name)
900 printf("Flasher tool for MX game devices\n"
901 "written by Grazvydas \"notaz\" Ignotas\n");
902 printf("v" VERSION " (" __DATE__ ")\n\n");
904 "%s [-i] [-g] [-e] [-r [file]] [-w <file>] ...\n"
905 " -i print some info about connected device\n"
906 " -g print some info about game ROM inside device\n"
907 " -e[1] erase whole flash ROM in device, '1' uses different erase method\n"
908 " -m[1-3] set MX mode: 2M+RAM, 4M no RAM, 4M+RAM\n"
909 " -f skip file check\n"
910 " -r [file] copy game image from device to file; can autodetect filename\n"
911 " -w <file> write file to device; also does erase\n"
912 " -sr [file] read save RAM to file\n"
913 " -sw <file> write save RAM file to device\n"
914 " -sc clear save RAM\n"
915 " -v with -w or -sw: verify written file\n",
919 int main(int argc, char *argv[])
921 char *r_fname = NULL, *w_fname = NULL, *sr_fname = NULL, *sw_fname = NULL;
922 void *r_fdata = NULL, *w_fdata = NULL, *sr_fdata = NULL, *sw_fdata = NULL;
923 int do_read_ram = 0, do_clear_ram = 0, do_verify = 0, do_check = 1;
924 int pr_dev_info = 0, pr_rom_info = 0, do_read = 0, mx_mode = 0;
925 int erase_method = 0, do_erase_size = 0;
926 int w_fsize = 0, sw_fsize = 0;
927 struct usb_dev_handle *device;
931 for (i = 1; i < argc; i++)
933 if (argv[i][0] != '-')
936 switch (argv[i][1]) {
944 do_erase_size = 0x400000;
945 if (argv[i][2] == '1')
955 mx_mode = argv[i][2];
959 if (argv[i+1] && argv[i+1][0] != '-')
963 if (argv[i+1] && argv[i+1][0] != '-')
969 switch (argv[i][2]) {
972 if (argv[i+1] && argv[i+1][0] != '-')
973 sr_fname = argv[++i];
976 if (argv[i+1] && argv[i+1][0] != '-')
977 sw_fname = argv[++i];
994 if (i <= 1 || i < argc) {
1000 if (w_fname != NULL) {
1001 /* check extension */
1002 ret = strlen(w_fname);
1003 if (do_check && (w_fname[ret - 4] == '.' || w_fname[ret - 3] == '.' ||
1004 w_fname[ret - 2] == '.') &&
1005 strcasecmp(&w_fname[ret - 4], ".gen") != 0 &&
1006 strcasecmp(&w_fname[ret - 4], ".bin") != 0) {
1007 fprintf(stderr, "\"%s\" doesn't look like a game ROM, aborting "
1008 "(use -f to disable this check)\n", w_fname);
1012 ret = read_file(w_fname, &w_fdata, &w_fsize, 0x400000);
1016 /* align size to 64 */
1017 ret = (w_fsize + 63) & ~63;
1018 if (w_fsize != ret) {
1019 printf("ROM image size is %d, padding to %d\n", w_fsize, ret);
1020 w_fdata = realloc(w_fdata, ret);
1021 if (w_fdata == NULL) {
1022 fprintf(stderr, "low mem\n");
1025 memset((char *)w_fdata + w_fsize, 0, ret - w_fsize);
1029 if (do_erase_size < w_fsize)
1030 do_erase_size = w_fsize;
1032 if (sw_fname != NULL) {
1033 ret = read_file(sw_fname, &sw_fdata, &sw_fsize, 0x8000*2);
1037 if (sw_fdata != NULL || do_clear_ram) {
1038 if (sw_fsize < 0x8000*2) {
1039 sw_fdata = realloc(sw_fdata, 0x8000*2);
1040 if (sw_fdata == NULL) {
1041 fprintf(stderr, "low mem\n");
1044 memset((u8 *)sw_fdata + sw_fsize, 0, 0x8000*2 - sw_fsize);
1046 sw_fsize = 0x8000*2;
1048 if (w_fname == NULL && sw_fname == NULL && do_verify) {
1049 fprintf(stderr, "warning: -w or -sw not specified, -v ignored.\n");
1056 device = get_device();
1062 ret = print_device_info(device);
1068 ret = print_game_info(device);
1074 if (mx_mode || w_fsize > 0x200000) {
1077 printf("MX mode set to ");
1080 printf("2M with RAM.\n");
1081 mx_mode = C_MODE_2M_RAM;
1084 printf("4M, no RAM.\n");
1085 mx_mode = C_MODE_4M_NORAM;
1088 printf("4M with RAM.\n");
1089 mx_mode = C_MODE_4M_RAM;
1092 set_ram_mode(device, mx_mode);
1096 if (do_erase_size != 0) {
1098 ret = erase_all(device, do_erase_size);
1100 ret = erase_seq(device, do_erase_size);
1106 if (w_fdata != NULL) {
1109 ret = read_write_rom(device, 0, w_fdata, w_fsize, 1);
1113 p = strrchr(w_fname, '/');
1114 p = (p == NULL) ? w_fname : p + 1;
1116 ret = write_filename(device, p, FILENAME_ROM0);
1118 fprintf(stderr, "warning: failed to save ROM filename\n");
1122 if (sw_fdata != NULL) {
1125 ret = read_write_ram(device, sw_fdata, sw_fsize, 1);
1129 memset(fname_buff, 0, sizeof(fname_buff));
1131 if (sw_fname != NULL) {
1132 p = strrchr(sw_fname, '/');
1133 p = (p == NULL) ? sw_fname : p + 1;
1134 } else if (w_fname != NULL) {
1135 t = strrchr(w_fname, '/');
1136 t = (t == NULL) ? w_fname : t + 1;
1138 strncpy(fname_buff, t, sizeof(fname_buff));
1139 fname_buff[sizeof(fname_buff) - 1] = 0;
1140 ret = strlen(fname_buff);
1141 if (ret > 4 && fname_buff[ret - 4] == '.')
1142 strcpy(&fname_buff[ret - 4], ".srm");
1145 ret = write_filename(device, p, FILENAME_RAM);
1147 fprintf(stderr, "warning: failed to save RAM filename\n");
1151 if (do_read && r_fname == NULL) {
1152 ret = read_filename(device, fname_buff, sizeof(fname_buff), FILENAME_ROM0);
1155 r_fname = fname_buff;
1156 if (r_fname[0] == 0)
1157 r_fname = "rom.gen";
1160 if (r_fname != NULL || do_verify) {
1161 r_fdata = malloc(0x400000);
1162 if (r_fdata == NULL) {
1163 fprintf(stderr, "low mem\n");
1167 ret = read_write_rom(device, 0, r_fdata, 0x400000, 0);
1172 if (r_fname != NULL)
1173 write_file(r_fname, r_fdata, 0x400000);
1176 if (do_read_ram && sr_fname == NULL) {
1177 ret = read_filename(device, fname_buff, sizeof(fname_buff), FILENAME_RAM);
1180 sr_fname = fname_buff;
1181 if (sr_fname[0] == 0)
1182 sr_fname = "rom.srm";
1185 if (sr_fname != NULL || do_verify) {
1186 sr_fdata = malloc(0x8000*2);
1187 if (sr_fdata == NULL) {
1188 fprintf(stderr, "low mem\n");
1192 ret = read_write_ram(device, sr_fdata, 0x8000*2, 0);
1197 if (sr_fname != NULL)
1198 write_file(sr_fname, sr_fdata, 0x8000*2);
1201 if (do_verify && w_fdata != NULL && r_fdata != NULL) {
1202 ret = memcmp(w_fdata, r_fdata, w_fsize);
1204 printf("flash verification passed.\n");
1206 printf("flash verification FAILED!\n");
1209 if (do_verify && sw_fdata != NULL && sr_fdata != NULL) {
1210 ret = memcmp(sw_fdata, sr_fdata, 0x8000*2);
1212 printf("RAM verification passed.\n");
1214 printf("RAM verification FAILED!\n");
1217 printf("all done.\n");
1221 if (w_fdata != NULL)
1223 if (r_fdata != NULL)
1226 release_device(device);