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 * All files in this archive are subject to the GNU General Public License.
21 * See the file COPYING in the source tree root for full license agreement.
23 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
24 * KIND, either express or implied.
26 ****************************************************************************/
32 #include <sys/types.h>
40 #define MAX_FIRMWARESIZE (64*1024*1024) /* Arbitrary limit (for safety) */
42 /* For win32 compatibility: */
47 /* USB IDs for USB Boot Mode */
51 #define EP_BULK_TO 0x01
55 #define MAX(a,b) (((a)>(b))?(a):(b))
58 enum USB_JZ4740_REQUEST
108 enum DATA_STRUCTURE_OB
121 #define SEND_COMMAND(cmd, arg) err = usb_control_msg(dh, USB_ENDPOINT_OUT | USB_TYPE_VENDOR, cmd, arg>>16, arg&0xFFFF, NULL, 0, TOUT);\
124 fprintf(stderr,"\n[ERR] Error sending control message (%d, %s)\n", err, usb_strerror()); \
128 #define GET_CPU_INFO(s) err = usb_control_msg(dh, USB_ENDPOINT_IN | USB_TYPE_VENDOR, VR_GET_CPU_INFO, 0, 0, buf, 8, TOUT); \
131 fprintf(stderr,"\n[ERR] Error sending control message (%d, %s)\n", err, usb_strerror()); \
135 #define SEND_DATA(ptr, size) err = usb_bulk_write(dh, USB_ENDPOINT_OUT | EP_BULK_TO, ptr, size, TOUT); \
138 fprintf(stderr,"\n[ERR] Error writing data\n"); \
139 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err)); \
143 #define GET_DATA(ptr, size) err = usb_bulk_read(dh, USB_ENDPOINT_IN | EP_BULK_TO, ptr, size, TOUT); \
146 fprintf(stderr,"\n[ERR] Error writing data\n"); \
147 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err)); \
151 int upload_app(usb_dev_handle
* dh
, int address
, unsigned char* p
, int len
)
155 unsigned char* tmp_buf
;
157 fprintf(stderr
, "[INFO] GET_CPU_INFO: ");
160 fprintf(stderr
, "%s\n", buf
);
162 fprintf(stderr
, "[INFO] SET_DATA_ADDRESS to 0x%x...", address
);
163 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
164 fprintf(stderr
, " Done!\n");
166 fprintf(stderr
, "[INFO] Sending data...");
167 /* Must not split the file in several packages! */
169 fprintf(stderr
, " Done!\n");
171 fprintf(stderr
, "[INFO] Verifying data...");
172 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
173 SEND_COMMAND(VR_SET_DATA_LENGTH
, len
);
174 tmp_buf
= malloc(len
);
177 fprintf(stderr
, "\n[ERR] Could not allocate memory.\n");
180 GET_DATA(tmp_buf
, len
);
181 if (memcmp(tmp_buf
, p
, len
) != 0)
183 fprintf(stderr
, "\n[ERR] Sent data isn't the same as received data...\n");
188 fprintf(stderr
, " Done !\n");
190 fprintf(stderr
, "[INFO] Booting device...");
191 SEND_COMMAND(VR_PROGRAM_START1
, address
);
192 fprintf(stderr
, " Done!\n");
197 int read_data(usb_dev_handle
* dh
, int address
, unsigned char *p
, int len
)
202 fprintf(stderr
, "[INFO] GET_CPU_INFO: ");
205 fprintf(stderr
, "%s\n", buf
);
207 fprintf(stderr
, "[INFO] Reading data...");
208 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
209 SEND_COMMAND(VR_SET_DATA_LENGTH
, len
);
211 fprintf(stderr
, " Done!\n");
215 unsigned int read_reg(usb_dev_handle
* dh
, int address
)
218 unsigned char buf
[4];
220 SEND_COMMAND(VR_SET_DATA_ADDRESS
, address
);
221 SEND_COMMAND(VR_SET_DATA_LENGTH
, 4);
224 return (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
227 #define TEST(m) fprintf(stderr, "%s -> %x\n", #m, read_reg(dh, m));
228 int test_device(usb_dev_handle
* dh
)
236 fprintf(stderr
, "\n");
248 fprintf(stderr
, "\n");
256 void jzconnect(int address
, unsigned char* buf
, int len
, int func
)
259 struct usb_device
*tmp_dev
;
260 struct usb_device
*dev
= NULL
;
264 fprintf(stderr
,"[INFO] Searching for device...\n");
267 if(usb_find_busses() < 0)
269 fprintf(stderr
, "[ERR] Could not find any USB busses.\n");
273 if (usb_find_devices() < 0)
275 fprintf(stderr
, "[ERR] USB devices not found(nor hubs!).\n");
279 for (bus
= usb_get_busses(); bus
; bus
= bus
->next
)
281 for (tmp_dev
= bus
->devices
; tmp_dev
; tmp_dev
= tmp_dev
->next
)
283 //printf("Found Vendor %04x Product %04x\n",tmp_dev->descriptor.idVendor, tmp_dev->descriptor.idProduct);
284 if (tmp_dev
->descriptor
.idVendor
== VID
&&
285 tmp_dev
->descriptor
.idProduct
== PID
)
296 fprintf(stderr
, "[ERR] Device not found.\n");
297 fprintf(stderr
, "[ERR] Ensure your device is in USB boot mode and run usbtool again.\n");
302 if ( (dh
= usb_open(dev
)) == NULL
)
304 fprintf(stderr
,"[ERR] Unable to open device.\n");
308 err
= usb_set_configuration(dh
, 1);
312 fprintf(stderr
, "[ERR] usb_set_configuration failed (%d, %s)\n", err
, usb_strerror());
317 /* "must be called" written in the libusb documentation */
318 err
= usb_claim_interface(dh
, 0);
321 fprintf(stderr
, "[ERR] Unable to claim interface (%d, %s)\n", err
, usb_strerror());
326 fprintf(stderr
,"[INFO] Found device, uploading application.\n");
328 /* Now we can transfer the application to the device. */
333 err
= upload_app(dh
, address
, buf
, len
);
336 err
= read_data(dh
, address
, buf
, len
);
339 err
= test_device(dh
);
343 /* release claimed interface */
344 usb_release_interface(dh
, 0);
349 int filesize(FILE* fd
)
352 fseek(fd
, 0, SEEK_END
);
354 fseek(fd
, 0, SEEK_SET
);
358 void print_usage(void)
361 fprintf(stderr
, "Usage: usbtool.exe [CMD] [FILE] [ADDRESS] [LEN]\n");
363 fprintf(stderr
, "Usage: usbtool [CMD] [FILE] [ADDRESS] [LEN]\n");
365 fprintf(stderr
, "\t[ADDRESS] has to be in 0xHEXADECIMAL format\n");
366 fprintf(stderr
, "\t[CMD]:\n\t\t1 -> upload file to specified address and boot from it\n\t\t2 -> read data from [ADDRESS] with length [LEN] to [FILE]\n");
367 fprintf(stderr
, "\t\t3 -> read device status\n");
369 fprintf(stderr
, "\nExample:\n\t usbtool.exe 1 fw.bin 0x80000000");
370 fprintf(stderr
, "\n\t usbtool.exe 2 save.bin 0x81000000 1024");
372 fprintf(stderr
, "\nExample:\n\t usbtool 1 fw.bin 0x80000000");
373 fprintf(stderr
, "\n\t usbtool 2 save.bin 0x81000000 1024");
377 int main(int argc
, char* argv
[])
380 int n
, len
, address
, cmd
=0;
383 fprintf(stderr
, "USBtool v" VERSION
" - (C) 2008 Maurus Cuelenaere\n");
384 fprintf(stderr
, "This is free software; see the source for copying conditions. There is NO\n");
385 fprintf(stderr
, "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
388 sscanf(argv
[1], "%d", &cmd
);
392 if (sscanf(argv
[3], "0x%x", &address
) <= 0)
398 fd
= fopen(argv
[2], "rb");
401 fprintf(stderr
, "[ERR] Could not open %s\n", argv
[2]);
407 if (len
> MAX_FIRMWARESIZE
)
409 fprintf(stderr
, "[ERR] Firmware file too big\n");
417 fprintf(stderr
, "[ERR] Could not allocate memory.\n");
422 n
= fread(buf
, 1, len
, fd
);
425 fprintf(stderr
, "[ERR] Short read.\n");
431 fprintf(stderr
, "[INFO] File size: %d bytes\n", n
);
433 jzconnect(address
, buf
, len
, 1);
436 if (sscanf(argv
[3], "0x%x", &address
) <= 0)
442 fd
= fopen(argv
[2], "wb");
445 fprintf(stderr
, "[ERR] Could not open %s\n", argv
[2]);
449 sscanf(argv
[4], "%d", &len
);
454 fprintf(stderr
, "[ERR] Could not allocate memory.\n");
459 jzconnect(address
, buf
, len
, 2);
461 n
= fwrite(buf
, 1, len
, fd
);
464 fprintf(stderr
, "[ERR] Short write.\n");
471 jzconnect(address
, NULL
, 0, 3);