6 typedef unsigned char u8;
7 typedef unsigned short u16;
8 typedef unsigned int u32;
9 #define array_size(x) (sizeof(x) / sizeof(x[0]))
12 unsigned short vendor;
13 unsigned short product;
16 { 0x03eb, 0x202a, "16MX+U Game Device" },
17 { 0x03eb, 0x202b, "32MX+U Game Device" },
18 { 0x03eb, 0x202c, "16MX+US Game Device" },
19 { 0x03eb, 0x202d, "32MX+UF Game Device" },
22 /*****************************************************************************/
24 #define CMD_ATM_READY 0x22
25 #define CMD_SEC_GET_NAME 'G' /* filename r/w */
26 #define CMD_SEC_PUT_NAME 'P'
27 #define CMD_SEC_DEVID 'L' /* read flash device ID */
28 #define CMD_SEC_ERASE 'E'
29 #define CMD_SEC_READY 'C' /* is flash ready? */
32 #define CTL_DATA_BUS 0x55
33 #define CTL_ADDR_BUS 0xAA
35 #define W_COUNTER 0xA0
36 #define W_CNT_WRITE 0x01
37 #define W_CNT_READ 0x00
39 #define FILENAME_ROM0 0
40 #define FILENAME_ROM1 1
41 #define FILENAME_RAM 2
55 u8 addrb2; /* most significant */
91 static const page_table_t p_AM29LV320DB[] =
93 { 0x000000, 0x00ffff, 0x002000 },
94 { 0x010000, 0x3fffff, 0x010000 },
95 { 0x000000, 0x000000, 0x000000 },
98 static const page_table_t p_AM29LV320DT[] =
100 { 0x000000, 0x3effff, 0x010000 },
101 { 0x3f0000, 0x3fffff, 0x002000 },
102 { 0x000000, 0x000000, 0x000000 },
105 /*****************************************************************************/
107 static void prepare_cmd(dev_cmd_t *dev_cmd, u8 cmd)
109 memset(dev_cmd, 0, sizeof(*dev_cmd));
111 memcpy(dev_cmd->magic, "USBC", 4);
112 dev_cmd->magic2 = 0x67; /* MySCSICommand, EXCOMMAND */
113 dev_cmd->mx_cmd = cmd;
116 static int write_data(struct usb_dev_handle *dev, void *data, int len)
118 int ret = usb_bulk_write(dev, 0x03, data, len, 2000);
120 fprintf(stderr, "failed to write:\n");
121 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
122 } else if (ret != len)
123 printf("write_cmd: wrote only %d of %d bytes\n", ret, len);
128 static int write_cmd(struct usb_dev_handle *dev, dev_cmd_t *cmd)
130 return write_data(dev, cmd, sizeof(*cmd));
133 static int read_data(struct usb_dev_handle *dev, void *buff, int size)
135 int ret = usb_bulk_read(dev, 0x82, buff, size, 2000);
137 fprintf(stderr, "failed to read:\n");
138 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
139 } else if (ret != size)
140 printf("read_data: read only %d of %d bytes\n", ret, size);
145 static int read_info(struct usb_dev_handle *device, u8 ctl_id)
151 prepare_cmd(&cmd, CMD_ATM_READY);
152 cmd.dev_info.which_device = ctl_id;
153 memset(&info, 0, sizeof(info));
155 ret = write_cmd(device, &cmd);
159 ret = read_data(device, &info, sizeof(info));
163 printf(" firmware version: %X.%X.%X%c\n", info.firmware_ver[0],
164 info.firmware_ver[1], info.firmware_ver[2], info.firmware_ver[3]);
165 printf(" bootloader version: %X.%X.%X%c\n", info.bootloader_ver[0],
166 info.bootloader_ver[1], info.bootloader_ver[2], info.bootloader_ver[3]);
167 info.names[sizeof(info.names) - 1] = 0;
168 printf(" device name: %s\n", info.names);
173 static int read_filename(struct usb_dev_handle *dev, char *dst, int len, u8 which)
179 prepare_cmd(&cmd, CMD_SEC_GET_NAME);
180 cmd.filename.which = which;
181 memset(buff, 0, sizeof(buff));
183 ret = write_cmd(dev, &cmd);
187 ret = read_data(dev, buff, 64);
191 strncpy(dst, buff, len);
197 static int write_filename(struct usb_dev_handle *dev, const char *fname, u8 which)
206 strncpy(buff, fname, len);
209 prepare_cmd(&cmd, CMD_SEC_PUT_NAME);
210 cmd.filename.which = which;
212 ret = write_cmd(dev, &cmd);
216 return write_data(dev, buff, len + 1);
219 static int read_w_counter(struct usb_dev_handle *dev, u32 *val)
225 prepare_cmd(&cmd, CMD_ATM_READY);
226 cmd.write_cnt.cmd = W_COUNTER;
227 cmd.write_cnt.action = W_CNT_READ;
229 ret = write_cmd(dev, &cmd);
233 ret = read_data(dev, buff, sizeof(buff));
241 static int read_flash_rom_id(struct usb_dev_handle *dev, u32 *val)
247 prepare_cmd(&cmd, CMD_SEC_DEVID);
248 cmd.write_flag = 1; /* XXX why? */
249 cmd.rom_id.dev_id = 1;
250 cmd.rom_id.which = 0;
252 ret = write_cmd(dev, &cmd);
256 ret = read_data(dev, buff, sizeof(buff));
260 *val = *(u16 *)buff << 16;
262 cmd.rom_id.which = 0;
263 ret = write_cmd(dev, &cmd);
267 ret = read_data(dev, buff, sizeof(buff));
271 *val |= *(u16 *)buff;
275 static const page_table_t *get_page_table(u32 rom_id)
279 return p_AM29LV320DB;
281 return p_AM29LV320DT;
283 fprintf(stderr, "unrecognized ROM id: %08x\n", rom_id);
289 static int get_page_size(const page_table_t *table, u32 addr, u32 *size)
291 const page_table_t *t;
293 for (t = table; t->end_addr != 0; t++) {
294 if (addr >= t->start_addr && addr <= t->end_addr) {
295 *size = t->page_size;
300 if (addr == t[-1].end_addr + 1)
303 fprintf(stderr, "get_page_size: failed on addr %06x\n", addr);
307 static int erase_sector(struct usb_dev_handle *dev, u32 addr)
313 prepare_cmd(&cmd, CMD_SEC_ERASE);
315 cmd.rom_rw.addrb2 = addr >> 16;
316 cmd.rom_rw.addrb1 = addr >> 8;
317 cmd.rom_rw.addrb0 = addr;
319 ret = write_cmd(dev, &cmd);
323 ret = read_data(dev, buff, sizeof(buff));
327 prepare_cmd(&cmd, CMD_SEC_READY);
328 cmd.rom_rw.addrb2 = addr >> 16;
329 cmd.rom_rw.addrb1 = addr >> 8;
330 cmd.rom_rw.addrb0 = addr;
332 for (i = 0; i < 100; i++) {
333 ret = write_cmd(dev, &cmd);
337 ret = read_data(dev, buff, sizeof(buff));
341 if (ret > 4 && buff[4] == 1)
347 printf("i = %d\n", i);
351 static usb_dev_handle *get_device(void)
353 struct usb_dev_handle *handle;
354 struct usb_device *dev;
358 ret = usb_find_busses();
360 fprintf(stderr, "Can't find USB busses\n");
364 ret = usb_find_devices();
366 fprintf(stderr, "Can't find USB devices\n");
370 bus = usb_get_busses();
371 for (; bus; bus = bus->next)
373 for (dev = bus->devices; dev; dev = dev->next)
375 for (i = 0; i < array_size(g_devices); i++)
377 if (dev->descriptor.idVendor == g_devices[i].vendor &&
378 dev->descriptor.idProduct == g_devices[i].product)
384 fprintf(stderr, "device not found.\n");
388 printf("found %s.\n", g_devices[i].name);
390 handle = usb_open(dev);
391 if (handle == NULL) {
392 fprintf(stderr, "failed to open device:\n");
393 fprintf(stderr, "%s\n", usb_strerror());
397 ret = usb_set_configuration(handle, 1);
399 fprintf(stderr, "couldn't set configuration for /*/bus/usb/%s/%s:\n",
400 bus->dirname, dev->filename);
401 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
405 ret = usb_claim_interface(handle, 0);
407 fprintf(stderr, "couldn't claim /*/bus/usb/%s/%s:\n",
408 bus->dirname, dev->filename);
409 fprintf(stderr, "%s (%d)\n", usb_strerror(), ret);
416 static void release_device(struct usb_dev_handle *device)
418 usb_release_interface(device, 0);
422 int main(int argc, char *argv[])
424 struct usb_dev_handle *device;
431 device = get_device();
435 printf("data bus controller:\n");
436 ret = read_info(device, CTL_DATA_BUS);
440 printf("address bus controller:\n");
441 ret = read_info(device, CTL_ADDR_BUS);
445 ret = read_filename(device, fname, sizeof(fname), FILENAME_ROM0);
448 printf("ROM filename: %s\n", fname);
450 ret = read_filename(device, fname, sizeof(fname), FILENAME_RAM);
453 printf("SRAM filename: %s\n", fname);
455 ret = read_w_counter(device, &counter);
458 printf("flash writes: %u\n", counter);
460 ret = read_flash_rom_id(device, &rom_id);
463 printf("flash rom id: %08x\n", rom_id);
466 release_device(device);