1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Dave Chapman
12 * USB code based on ifp-line - http://ifp-driver.sourceforge.net
14 * ifp-line is (C) Pavel Kriz, Jun Yamishiro and Joe Roback and
15 * licensed under the GPL (v2)
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2
21 * of the License, or (at your option) any later version.
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>
37 #define MAX_FIRMWARESIZE (10*1024*1024) /* Arbitrary limit (for safety) */
39 /* For win32 compatibility: */
53 static struct device_t devices
[] =
55 {"c100", "Sansa C100 series", 0xb021, 0x20000000, 0x42e97010 },
56 {"m200", "Sansa M200 series", 0xb021, 0x20000000, 0x42e97010 },
57 {"cowond2", "Cowon D2", 0xb011, 0x20000000, 0xa2e92010 },
58 {"cowons9", "Cowon S9", 0xb057, 0x20000000, 0xa1102800 },
59 {"iaudio6", "iAudio 6", 0xb021, 0x20000000, 0x62e97010 },
60 {"iaudio7", "iAudio 7", 0xb021, 0x20000000, 0x62e97010 },
61 {"logikdax", "Logik DAX 1GB DAB/MP3 player", 0xb021, 0x20000000, 0x52e97410 },
62 {"x20", "iRiver X20", 0xb051, 0x20000000, 0x02e92010 },
63 {"ypp2", "Samsung YP-P2", 0xb011, 0x20000000, 0x22e92010 },
64 {"ypk3", "Samsung YP-K3", 0xb021, 0x20000000, 0x62e92018 },
67 #define NUM_DEVICES ((sizeof(devices) / sizeof(struct device_t)))
69 int find_device(char* devname
)
73 while ((i
< NUM_DEVICES
) && (strcmp(devices
[i
].name
,devname
)))
82 void print_devices(void)
86 printf("Valid devices are:\n");
87 for (i
=0; i
<NUM_DEVICES
; i
++)
89 printf(" %10s - %s\n",devices
[i
].name
,devices
[i
].label
);
93 /* USB IDs for USB Boot Mode */
94 #define TCC_VENDORID 0x140e
98 #define PACKET_SIZE 64 /* Number of bytes to send in one write */
101 #define MAX(a,b) (((a)>(b))?(a):(b))
104 static void put_int32le(uint32_t x
, char* p
)
107 p
[1] = (x
>> 8) & 0xff;
108 p
[2] = (x
>> 16) & 0xff;
109 p
[3] = (x
>> 24) & 0xff;
112 int upload_app(usb_dev_handle
* dh
, int device
, char* p
, int len
)
114 char buf
[PACKET_SIZE
];
118 /* Send the header - Destination address, length and SDCFG value */
119 memset(buf
, 0, PACKET_SIZE
);
121 put_int32le(0xf0000000, buf
); /* Unknown - always the same */
122 put_int32le(len
/ PACKET_SIZE
, buf
+ 4);
123 put_int32le(devices
[device
].loadaddr
, buf
+ 8);
124 put_int32le(devices
[device
].sdcfg
, buf
+ 12);
126 err
= usb_bulk_write(dh
, TCC_BULK_TO
, buf
, PACKET_SIZE
, TOUT
);
130 fprintf(stderr
,"[ERR] Error writing header\n");
131 fprintf(stderr
,"[ERR] Bulk write error (%d, %s)\n", err
, strerror(-err
));
135 /* Now send the data, PACKET_SIZE bytes at a time. */
137 for (i
=0 ; i
< (len
/ PACKET_SIZE
) ; i
++)
139 err
= usb_bulk_write(dh
, TCC_BULK_TO
, p
, PACKET_SIZE
, TOUT
);
143 fprintf(stderr
,"[ERR] Error writing data\n");
144 fprintf(stderr
,"[ERR] Bulk write error (%d, %s)\n", err
, strerror(-err
));
155 /* The main function */
157 int do_patching(int device
, char* buf
, int len
)
159 struct usb_bus
*busses
;
161 struct usb_device
*tmp_dev
;
162 struct usb_device
*dev
= NULL
;
166 fprintf(stderr
,"[INFO] Searching for TCC device...\n");
169 if(usb_find_busses() < 0) {
170 fprintf(stderr
, "[ERR] Could not find any USB busses.\n");
174 if (usb_find_devices() < 0) {
175 fprintf(stderr
, "[ERR] USB devices not found(nor hubs!).\n");
179 /* C calling convention, it's not nice to use global stuff */
180 busses
= usb_get_busses();
182 for (bus
= busses
; bus
; bus
= bus
->next
) {
183 for (tmp_dev
= bus
->devices
; tmp_dev
; tmp_dev
= tmp_dev
->next
) {
184 //printf("Found Vendor %04x Product %04x\n",tmp_dev->descriptor.idVendor, tmp_dev->descriptor.idProduct);
185 if (tmp_dev
->descriptor
.idVendor
== TCC_VENDORID
&&
186 tmp_dev
->descriptor
.idProduct
== devices
[device
].productid
) {
196 fprintf(stderr
, "[ERR] TCC device not found.\n");
197 fprintf(stderr
, "[ERR] Ensure your TCC device is in USB boot mode and run tcctool again.\n");
202 if ( (dh
= usb_open(dev
)) == NULL
) {
203 fprintf(stderr
,"[ERR] Unable to open TCC device.\n");
207 err
= usb_set_configuration(dh
, 1);
210 fprintf(stderr
, "[ERR] usb_set_configuration failed (%d)\n", err
);
215 /* "must be called" written in the libusb documentation */
216 err
= usb_claim_interface(dh
, dev
->config
->interface
->altsetting
->bInterfaceNumber
);
218 fprintf(stderr
, "[ERR] Unable to claim interface (%d)\n", err
);
223 fprintf(stderr
,"[INFO] Found TCC device, uploading application.\n");
225 /* Now we can transfer the application to the device. */
227 if ( (err
= upload_app(dh
, device
, buf
, len
)) < 0)
229 fprintf(stderr
,"[ERR] Upload of application failed.\n");
233 fprintf(stderr
,"[INFO] Patching application uploaded successfully!\n");
236 /* release claimed interface */
237 usb_release_interface(dh
, dev
->config
->interface
->altsetting
->bInterfaceNumber
);
240 return err
< 0 ? -1: 0;
243 off_t
filesize(int fd
) {
246 if (fstat(fd
,&buf
) < 0) {
247 perror("[ERR] Checking filesize of input file");
254 void print_usage(void)
256 printf("Usage: tcctool -d devicename firmware.bin\n");
259 int main(int argc
, char* argv
[])
262 int n
,len
,padded_len
;
266 printf("tcctool " VERSION
" - (C) 2007-2010 Dave Chapman\n");
267 printf("This is free software; see the source for copying conditions. There is NO\n");
268 printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
277 if (strcmp(argv
[1],"-d"))
284 device
= find_device(argv
[2]);
288 printf("[ERR] Unknown device \"%s\"\n",argv
[2]);
293 printf("[INFO] Using device \"%s\"\n",devices
[device
].label
);
294 fd
= open(argv
[3], O_RDONLY
|O_BINARY
);
297 printf("[ERR] Could not open %s\n", argv
[3]);
303 if (len
> MAX_FIRMWARESIZE
)
305 printf("[ERR] Firmware file too big\n");
310 /* Round len up to multiple of PACKET_SIZE */
311 padded_len
= (len
+ PACKET_SIZE
) & ~(PACKET_SIZE
-1);
313 buf
= malloc(padded_len
);
316 printf("[ERR] Could not allocate memory.\n");
321 n
= read(fd
, buf
, len
);
324 printf("[ERR] Short read.\n");
330 if (do_patching(device
, buf
, padded_len
))