1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Maurus Cuelenaere
12 * based on tcctool.c by Dave Chapman
14 * USB code based on ifp-line - http://ifp-driver.sourceforge.net
16 * ifp-line is (C) Pavel Kriz, Jun Yamishiro and Joe Roback and
17 * licensed under the GPL (v2)
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version 2
23 * of the License, or (at your option) any later version.
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
26 * KIND, either express or implied.
28 ****************************************************************************/
34 #include <sys/types.h>
41 #include "jz_xloader.h"
45 #define MAX_FIRMWARESIZE (64*1024*1024) /* Arbitrary limit (for safety) */
47 /* For win32 compatibility: */
52 /* USB IDs for USB Boot Mode */
56 #define EP_BULK_TO 0x01
59 enum USB_JZ4740_REQUEST
108 enum DATA_STRUCTURE_OB
121 int filesize(FILE* fd
)
125 fseek(fd
, 0, SEEK_END
);
127 fseek(fd
, 0, SEEK_SET
);
132 bool file_exists(const char* filename
)
134 FILE* fp
= fopen(filename
, "r");
146 #define SEND_COMMAND(cmd, arg) err = usb_control_msg(dh, USB_ENDPOINT_OUT | USB_TYPE_VENDOR, (cmd), (arg)>>16, (arg)&0xFFFF, NULL, 0, TOUT);\
149 fprintf(stderr,"\n[ERR] Error sending control message (%d, %s)\n", err, usb_strerror()); \
153 #define GET_CPU_INFO(s) err = usb_control_msg(dh, USB_ENDPOINT_IN | USB_TYPE_VENDOR, VR_GET_CPU_INFO, 0, 0, (s), 8, TOUT); \
156 fprintf(stderr,"\n[ERR] Error sending control message (%d, %s)\n", err, usb_strerror()); \
160 #define SEND_DATA(ptr, size) err = usb_bulk_write(dh, USB_ENDPOINT_OUT | EP_BULK_TO, ((char*)(ptr)), (size), TOUT); \
163 fprintf(stderr,"\n[ERR] Error writing data\n"); \
164 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err)); \
168 #define GET_DATA(ptr, size) err = usb_bulk_read(dh, USB_ENDPOINT_IN | EP_BULK_TO, ((char*)(ptr)), (size), TOUT); \
171 fprintf(stderr,"\n[ERR] Error writing data\n"); \
172 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err)); \
175 int upload_data(usb_dev_handle
* dh
, int address
, unsigned char* p
, int len
)
179 unsigned char* tmp_buf
;
181 fprintf(stderr
, "[INFO] GET_CPU_INFO: ");
184 fprintf(stderr
, "%s\n", buf
);
186 fprintf(stderr
, "[INFO] SET_DATA_ADDRESS to 0x%x...", address
);
187 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
188 fprintf(stderr
, " Done!\n");
190 fprintf(stderr
, "[INFO] Sending data...");
191 /* Must not split the file in several packages! */
193 fprintf(stderr
, " Done!\n");
195 fprintf(stderr
, "[INFO] Verifying data...");
196 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
197 SEND_COMMAND(VR_SET_DATA_LENGTH
, len
);
198 tmp_buf
= malloc(len
);
201 fprintf(stderr
, "\n[ERR] Could not allocate memory.\n");
204 GET_DATA(tmp_buf
, len
);
205 if (memcmp(tmp_buf
, p
, len
) != 0)
206 fprintf(stderr
, "\n[WARN] Sent data isn't the same as received data...\n");
208 fprintf(stderr
, " Done!\n");
214 int boot(usb_dev_handle
* dh
, int address
, bool stage2
)
218 fprintf(stderr
, "[INFO] Booting device STAGE%d...", (stage2
? 2 : 1));
219 SEND_COMMAND((stage2
? VR_PROGRAM_START2
: VR_PROGRAM_START1
), address
);
220 fprintf(stderr
, " Done!\n");
225 int upload_app(usb_dev_handle
* dh
, int address
, unsigned char* p
, int len
, bool stage2
)
227 int err
= upload_data(dh
, address
, p
, len
);
230 err
= boot(dh
, address
, stage2
);
232 fprintf(stderr
, "[INFO] Done!\n");
238 int read_data(usb_dev_handle
* dh
, int address
, unsigned char *p
, int len
)
243 fprintf(stderr
, "[INFO] GET_CPU_INFO: ");
246 fprintf(stderr
, "%s\n", buf
);
248 fprintf(stderr
, "[INFO] Reading data...");
249 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
250 SEND_COMMAND(VR_SET_DATA_LENGTH
, len
);
252 fprintf(stderr
, " Done!\n");
256 unsigned int read_reg(usb_dev_handle
* dh
, int address
, int size
)
259 unsigned char buf
[4];
261 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
262 SEND_COMMAND(VR_SET_DATA_LENGTH
, size
);
268 return (buf
[1] << 8) | buf
[0];
270 return (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
275 int set_reg(usb_dev_handle
* dh
, int address
, unsigned int val
, int size
)
278 unsigned char buf
[4];
283 buf
[1] = (val
>> 8) & 0xff;
286 buf
[2] = (val
>> 16) & 0xff;
287 buf
[3] = (val
>> 24) & 0xff;
291 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
292 SEND_DATA(buf
, size
);
296 #define or_reg(dh, adr, val, size) set_reg(dh, adr, (read_reg(dh, adr, size) | (val)), size);
297 #define and_reg(dh, adr, val, size) set_reg(dh, adr, (read_reg(dh, adr, size) & (val)), size);
298 #define bc_reg(dh, adr, val, size) set_reg(dh, adr, (read_reg(dh, adr, size) & ~(val)), size);
299 #define xor_reg(dh, adr, val, size) set_reg(dh, adr, (read_reg(dh, adr, size) ^ (val)), size);
301 #define TEST(m, size) fprintf(stderr, "%s -> %x\n", #m, read_reg(dh, m, size));
302 int test_device(usb_dev_handle
* dh
)
310 fprintf(stderr
, "\n");
322 fprintf(stderr
, "\n");
323 TEST(GPIO_PXPIN(0), 4);
324 TEST(GPIO_PXPIN(1), 4);
325 TEST(GPIO_PXPIN(2), 4);
326 TEST(GPIO_PXPIN(3), 4);
328 fprintf(stderr
, "\n");
331 fprintf(stderr
, "\n");
335 TEST(SADC_BATDAT
, 2);
338 fprintf(stderr
, "\n");
347 unsigned int read_file(const char *name
, unsigned char **buffer
)
352 fd
= fopen(name
, "rb");
355 fprintf(stderr
, "[ERR] Could not open %s\n", name
);
361 *buffer
= (unsigned char*)malloc(len
);
364 fprintf(stderr
, "[ERR] Could not allocate memory.\n");
369 n
= fread(*buffer
, 1, len
, fd
);
372 fprintf(stderr
, "[ERR] Short read.\n");
380 #define _GET_CPU fprintf(stderr, "[INFO] GET_CPU_INFO:"); \
383 fprintf(stderr, " %s\n", cpu);
384 #define _SET_ADDR(a) fprintf(stderr, "[INFO] Set address to 0x%x...", a); \
385 SEND_COMMAND(VR_SET_DATA_ADDRESS, a); \
386 fprintf(stderr, " Done!\n");
387 #define _SEND_FILE(a) fsize = read_file(a, &buffer); \
390 fprintf(stderr, "[INFO] Sending file %s: %d bytes...", a, fsize); \
391 SEND_DATA(buffer, fsize); \
393 fprintf(stderr, " Done!\n");
394 #define _VERIFY_DATA(a,c) fprintf(stderr, "[INFO] Verifying data (%s)...", a); \
395 fsize = read_file(a, &buffer); \
398 buffer2 = (unsigned char*)malloc(fsize); \
399 SEND_COMMAND(VR_SET_DATA_ADDRESS, c); \
400 SEND_COMMAND(VR_SET_DATA_LENGTH, fsize); \
401 GET_DATA(buffer2, fsize); \
402 if(memcmp(buffer, buffer2, fsize) != 0) \
403 fprintf(stderr, "\n[WARN] Sent data isn't the same as received data...\n"); \
405 fprintf(stderr, " Done!\n"); \
408 #define _STAGE1(a) fprintf(stderr, "[INFO] Stage 1 at 0x%x\n", a); \
409 SEND_COMMAND(VR_PROGRAM_START1, a);
410 #define _STAGE2(a) fprintf(stderr, "[INFO] Stage 2 at 0x%x\n", a); \
411 SEND_COMMAND(VR_PROGRAM_START2, a);
412 #define _FLUSH fprintf(stderr, "[INFO] Flushing caches...\n"); \
413 SEND_COMMAND(VR_FLUSH_CACHES, 0);
415 #define _SLEEP(x) Sleep(x*1000);
417 #define _SLEEP(x) sleep(x);
419 int mimic_of(usb_dev_handle
*dh
, bool vx767
)
422 unsigned char *buffer
, *buffer2
;
425 fprintf(stderr
, "[INFO] Start!\n");
427 _SET_ADDR(0x8000 << 16);
430 _VERIFY_DATA("1.bin", 0x8000 << 16);
431 _STAGE1(0x8000 << 16);
433 _VERIFY_DATA("2.bin", 0xB3020060);
439 _SET_ADDR(0x8000 << 16);
442 _VERIFY_DATA("3.bin", 0x8000 << 16);
447 _SET_ADDR(0x80D0 << 16);
450 _VERIFY_DATA("4.bin", 0x80D0 << 16);
455 _SET_ADDR(0x80E0 << 16);
458 _VERIFY_DATA("5.bin", 0x80E0 << 16);
463 _SET_ADDR(0x80004000);
466 _VERIFY_DATA("6.bin", 0x80004000);
471 _SET_ADDR(0x80FD << 16);
474 _VERIFY_DATA("7.bin", 0x80FD << 16);
479 _VERIFY_DATA("8.bin", 0x80004004);
480 _VERIFY_DATA("9.bin", 0x80004008);
483 _SET_ADDR(0x80E0 << 16);
484 _SEND_FILE("10.bin");
486 _VERIFY_DATA("10.bin", 0x80E0 << 16);
498 fprintf(stderr
, "[INFO] Done!\n");
502 int send_rockbox(usb_dev_handle
*dh
, const char* filename
)
505 unsigned char *buffer
;
507 fprintf(stderr
, "[INFO] Start!\n");
508 if(file_exists("jz_xloader.bin"))
510 fprintf(stderr
, "[INFO] Using jz_xloader.bin\n");
511 fsize
= read_file("jz_xloader.bin", &buffer
);
512 upload_data(dh
, 0x080000000, buffer
, fsize
);
517 fprintf(stderr
, "[INFO] Using built-in jz_xloader.bin\n");
518 upload_data(dh
, 0x080000000, jz_xloader
, LEN_jz_xloader
);
520 boot(dh
, 0x080000000, false);
523 fsize
= read_file(filename
, &buffer
);
524 upload_data(dh
, 0x080004000, buffer
, fsize
);
526 boot(dh
, 0x080004000, true);
528 fprintf(stderr
, "[INFO] Done!\n");
533 #define SEND_NAND_COMMAND(cs, cmd, option) SEND_COMMAND(VR_NAND_OPS, ((cmd&0xF)|((cs&0xFF)<<4)|((option&0xFF)<<12)) );
534 #define LENGTH 1024*1024*5
535 int nand_dump(usb_dev_handle
*dh
)
540 unsigned char* buffer
;
542 fd
= fopen("nand_dump.bin", "wb");
545 fprintf(stderr
, "[ERR] Could not open nand_dump.bin\n");
549 buffer
= (unsigned char*)malloc(LENGTH
);
552 fprintf(stderr
, "[ERR] Could not allocate memory.\n");
557 SEND_NAND_COMMAND(0, NAND_INIT
, 0);
559 fprintf(stderr, "[INFO] Querying NAND...\n");
560 SEND_NAND_COMMAND(0, NAND_QUERY, 0);
562 printf("[INFO] %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
564 SEND_COMMAND(VR_SET_DATA_ADDRESS
, 0);
565 SEND_COMMAND(VR_SET_DATA_LENGTH
, LENGTH
);
566 SEND_NAND_COMMAND(0, NAND_READ
, NO_OOB
);
568 fprintf(stderr
, "[INFO] Reading data...\n");
569 err
= usb_bulk_read(dh
, USB_ENDPOINT_IN
| EP_BULK_TO
, (char*)buffer
, LENGTH
, TOUT
);
572 fprintf(stderr
,"\n[ERR] Error writing data\n");
573 fprintf(stderr
,"[ERR] Bulk write error (%d, %s)\n", err
, strerror(-err
));
579 n
= fwrite(buffer
, 1, LENGTH
, fd
);
582 fprintf(stderr
, "[ERR] Short write.\n");
593 #define ROM_LENGTH 0x1000*16
594 int rom_dump(usb_dev_handle
*dh
)
599 unsigned char* buffer
;
601 fd
= fopen("rom_dump.bin", "wb");
604 fprintf(stderr
, "[ERR] Could not open rom_dump.bin\n");
608 buffer
= (unsigned char*)malloc(ROM_LENGTH
);
611 fprintf(stderr
, "[ERR] Could not allocate memory.\n");
616 SEND_COMMAND(VR_SET_DATA_ADDRESS
, 0x1FC00000);
617 SEND_COMMAND(VR_SET_DATA_LENGTH
, ROM_LENGTH
);
619 fprintf(stderr
, "[INFO] Reading data...\n");
620 err
= usb_bulk_read(dh
, USB_ENDPOINT_IN
| EP_BULK_TO
, (char*)buffer
, ROM_LENGTH
, TOUT
);
621 if (err
!= ROM_LENGTH
)
623 fprintf(stderr
,"\n[ERR] Error writing data\n");
624 fprintf(stderr
,"[ERR] Bulk write error (%d, %s)\n", err
, strerror(-err
));
630 n
= fwrite(buffer
, 1, ROM_LENGTH
, fd
);
633 fprintf(stderr
, "[ERR] Short write.\n");
644 int jzconnect(int address
, unsigned char* buf
, int len
, int func
)
647 struct usb_device
*tmp_dev
;
648 struct usb_device
*dev
= NULL
;
652 fprintf(stderr
,"[INFO] Searching for device...\n");
655 if(usb_find_busses() < 0)
657 fprintf(stderr
, "[ERR] Could not find any USB busses.\n");
661 if (usb_find_devices() < 0)
663 fprintf(stderr
, "[ERR] USB devices not found(nor hubs!).\n");
667 for (bus
= usb_get_busses(); bus
; bus
= bus
->next
)
669 for (tmp_dev
= bus
->devices
; tmp_dev
; tmp_dev
= tmp_dev
->next
)
671 if (tmp_dev
->descriptor
.idVendor
== VID
&&
672 tmp_dev
->descriptor
.idProduct
== PID
)
680 fprintf(stderr
, "[ERR] Device not found.\n");
681 fprintf(stderr
, "[ERR] Ensure your device is in USB boot mode and run usbtool again.\n");
685 if ( (dh
= usb_open(dev
)) == NULL
)
687 fprintf(stderr
,"[ERR] Unable to open device.\n");
691 /* usb_set_configuration() calls are already done in Linux */
693 err
= usb_set_configuration(dh
, 1);
697 fprintf(stderr
, "[ERR] usb_set_configuration failed (%d, %s)\n", err
, usb_strerror());
703 /* "must be called" written in the libusb documentation */
704 err
= usb_claim_interface(dh
, 0);
707 fprintf(stderr
, "[ERR] Unable to claim interface (%d, %s)\n", err
, usb_strerror());
712 fprintf(stderr
,"[INFO] Found device, uploading application.\n");
714 /* Now we can transfer the application to the device. */
720 err
= upload_app(dh
, address
, buf
, len
, (func
== 5));
723 err
= read_data(dh
, address
, buf
, len
);
726 err
= test_device(dh
);
730 err
= mimic_of(dh
, (func
== 7));
739 err
= send_rockbox(dh
, (char*)buf
);
743 /* release claimed interface */
744 usb_release_interface(dh
, 0);
751 void print_usage(void)
754 fprintf(stderr
, "Usage: usbtool.exe <CMD> [FILE] [ADDRESS] [LEN]\n");
756 fprintf(stderr
, "Usage: usbtool <CMD> [FILE] [ADDRESS] [LEN]\n");
759 fprintf(stderr
, "\t[ADDRESS] has to be in 0xHEXADECIMAL format\n");
760 fprintf(stderr
, "\tCMD:\n");
761 fprintf(stderr
, "\t\t 1 -> upload file to specified address and boot from it\n");
762 fprintf(stderr
, "\t\t 2 -> read data from [ADDRESS] with length [LEN] to [FILE]\n");
763 fprintf(stderr
, "\t\t 3 -> read device status\n");
764 fprintf(stderr
, "\t\t 5 -> same as 1 but do a stage 2 boot\n");
765 fprintf(stderr
, "\t\t 6 -> mimic VX747 OF fw recovery\n");
766 fprintf(stderr
, "\t\t 7 -> mimic VX767 OF fw recovery\n");
767 fprintf(stderr
, "\t\t 8 -> do a NAND dump\n");
768 fprintf(stderr
, "\t\t 9 -> do a ROM dump\n");
769 fprintf(stderr
, "\t\t10 -> send Rockbox bootloader at [FILE] to SDRAM\n");
772 fprintf(stderr
, "\nExample:\n\t usbtool.exe 1 fw.bin 0x80000000\n");
773 fprintf(stderr
, "\t usbtool.exe 2 save.bin 0x81000000 1024\n");
775 fprintf(stderr
, "\nExample:\n\t usbtool 1 fw.bin 0x80000000\n");
776 fprintf(stderr
, "\t usbtool 2 save.bin 0x81000000 1024\n");
780 int main(int argc
, char* argv
[])
783 int n
, len
, address
, cmd
=0;
786 fprintf(stderr
, "USBtool v" VERSION
" - (C) 2008 Maurus Cuelenaere\n");
787 fprintf(stderr
, "This is free software; see the source for copying conditions. There is NO\n");
788 fprintf(stderr
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
791 sscanf(argv
[1], "%d", &cmd
);
796 if (strcmp(argv
[3], "-1") == 0)
797 address
= 0x80000000;
800 if (sscanf(argv
[3], "0x%x", &address
) <= 0)
807 fd
= fopen(argv
[2], "rb");
810 fprintf(stderr
, "[ERR] Could not open %s\n", argv
[2]);
816 if (len
> MAX_FIRMWARESIZE
)
818 fprintf(stderr
, "[ERR] Firmware file too big\n");
826 fprintf(stderr
, "[ERR] Could not allocate memory.\n");
831 n
= fread(buf
, 1, len
, fd
);
834 fprintf(stderr
, "[ERR] Short read.\n");
840 fprintf(stderr
, "[INFO] File size: %d bytes\n", n
);
842 return jzconnect(address
, buf
, len
, cmd
);
844 if (sscanf(argv
[3], "0x%x", &address
) <= 0)
850 fd
= fopen(argv
[2], "wb");
853 fprintf(stderr
, "[ERR] Could not open %s\n", argv
[2]);
857 sscanf(argv
[4], "%d", &len
);
862 fprintf(stderr
, "[ERR] Could not allocate memory.\n");
867 int err
= jzconnect(address
, buf
, len
, 2);
869 n
= fwrite(buf
, 1, len
, fd
);
872 fprintf(stderr
, "[ERR] Short write.\n");
886 if(!file_exists(argv
[2]))
891 return jzconnect(address
, (unsigned char*)argv
[2], 0, 10);
897 return jzconnect(address
, NULL
, 0, cmd
);