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 * All files in this archive are subject to the GNU General Public License.
19 * See the file COPYING in the source tree root for full license agreement.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
30 #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 {"cowond2", "Cowon D2", 0xb011, 0x20000000, 0xa2e92010 },
57 {"iaudio6", "iAudio 6", 0xb021, 0x20000000, 0x62e97010 },
58 {"iaudio7", "iAudio 7", 0xb021, 0x20000000, 0x62e97010 },
59 {"logikdax", "Logik DAX 1GB DAB/MP3 player", 0xb021, 0x20000000, 0x52e97410 }
62 #define NUM_DEVICES ((sizeof(devices) / sizeof(struct device_t)))
64 int find_device(char* devname
)
68 while ((i
< NUM_DEVICES
) && (strcmp(devices
[i
].name
,devname
)))
77 void print_devices(void)
81 printf("Valid devices are:\n");
82 for (i
=0; i
<NUM_DEVICES
; i
++)
84 printf(" %10s - %s\n",devices
[i
].name
,devices
[i
].label
);
88 /* USB IDs for USB Boot Mode */
89 #define TCC_VENDORID 0x140e
93 #define PACKET_SIZE 64 /* Number of bytes to send in one write */
96 #define MAX(a,b) (((a)>(b))?(a):(b))
99 static void put_int32le(uint32_t x
, char* p
)
102 p
[1] = (x
>> 8) & 0xff;
103 p
[2] = (x
>> 16) & 0xff;
104 p
[3] = (x
>> 24) & 0xff;
107 int upload_app(usb_dev_handle
* dh
, int device
, char* p
, int len
)
109 char buf
[PACKET_SIZE
];
113 /* Send the header - Destination address, length and SDCFG value */
114 memset(buf
, 0, PACKET_SIZE
);
116 put_int32le(0xf0000000, buf
); /* Unknown - always the same */
117 put_int32le(len
/ PACKET_SIZE
, buf
+ 4);
118 put_int32le(devices
[device
].loadaddr
, buf
+ 8);
119 put_int32le(devices
[device
].sdcfg
, buf
+ 12);
121 err
= usb_bulk_write(dh
, TCC_BULK_TO
, buf
, PACKET_SIZE
, TOUT
);
125 fprintf(stderr
,"[ERR] Error writing header\n");
126 fprintf(stderr
,"[ERR] Bulk write error (%d, %s)\n", err
, strerror(-err
));
130 /* Now send the data, PACKET_SIZE bytes at a time. */
132 for (i
=0 ; i
< (len
/ PACKET_SIZE
) ; i
++)
134 err
= usb_bulk_write(dh
, TCC_BULK_TO
, p
, PACKET_SIZE
, TOUT
);
138 fprintf(stderr
,"[ERR] Error writing data\n");
139 fprintf(stderr
,"[ERR] Bulk write error (%d, %s)\n", err
, strerror(-err
));
150 /* The main function */
152 void do_patching(int device
, char* buf
, int len
)
154 struct usb_bus
*busses
;
156 struct usb_device
*tmp_dev
;
157 struct usb_device
*dev
= NULL
;
161 fprintf(stderr
,"[INFO] Searching for TCC device...\n");
164 if(usb_find_busses() < 0) {
165 fprintf(stderr
, "[ERR] Could not find any USB busses.\n");
169 if (usb_find_devices() < 0) {
170 fprintf(stderr
, "[ERR] USB devices not found(nor hubs!).\n");
174 /* C calling convention, it's not nice to use global stuff */
175 busses
= usb_get_busses();
177 for (bus
= busses
; bus
; bus
= bus
->next
) {
178 for (tmp_dev
= bus
->devices
; tmp_dev
; tmp_dev
= tmp_dev
->next
) {
179 //printf("Found Vendor %04x Product %04x\n",tmp_dev->descriptor.idVendor, tmp_dev->descriptor.idProduct);
180 if (tmp_dev
->descriptor
.idVendor
== TCC_VENDORID
&&
181 tmp_dev
->descriptor
.idProduct
== devices
[device
].productid
) {
191 fprintf(stderr
, "[ERR] TCC device not found.\n");
192 fprintf(stderr
, "[ERR] Ensure your TCC device is in USB boot mode and run tcctool again.\n");
197 if ( (dh
= usb_open(dev
)) == NULL
) {
198 fprintf(stderr
,"[ERR] Unable to open TCC device.\n");
202 err
= usb_set_configuration(dh
, 1);
205 fprintf(stderr
, "[ERR] usb_set_configuration failed (%d)\n", err
);
210 /* "must be called" written in the libusb documentation */
211 err
= usb_claim_interface(dh
, dev
->config
->interface
->altsetting
->bInterfaceNumber
);
213 fprintf(stderr
, "[ERR] Unable to claim interface (%d)\n", err
);
218 fprintf(stderr
,"[INFO] Found TCC device, uploading application.\n");
220 /* Now we can transfer the application to the device. */
222 if (upload_app(dh
, device
, buf
, len
) < 0)
224 fprintf(stderr
,"[ERR] Upload of application failed.\n");
228 fprintf(stderr
,"[INFO] Patching application uploaded successfully!\n");
231 /* release claimed interface */
232 usb_release_interface(dh
, dev
->config
->interface
->altsetting
->bInterfaceNumber
);
237 off_t
filesize(int fd
) {
240 if (fstat(fd
,&buf
) < 0) {
241 perror("[ERR] Checking filesize of input file");
248 void print_usage(void)
250 printf("Usage: tcctool -d devicename firmware.bin\n");
253 int main(int argc
, char* argv
[])
260 printf("tcctool v" VERSION
" - (C) 2007 Dave Chapman\n");
261 printf("This is free software; see the source for copying conditions. There is NO\n");
262 printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
271 if (strcmp(argv
[1],"-d"))
278 device
= find_device(argv
[2]);
282 printf("[ERR] Unknown device \"%s\"\n",argv
[2]);
287 printf("[INFO] Using device \"%s\"\n",devices
[device
].label
);
288 fd
= open(argv
[3], O_RDONLY
|O_BINARY
);
291 printf("[ERR] Could not open %s\n", argv
[3]);
297 if (len
> MAX_FIRMWARESIZE
)
299 printf("[ERR] Firmware file too big\n");
307 printf("[ERR] Could not allocate memory.\n");
312 n
= read(fd
, buf
, len
);
315 printf("[ERR] Short read.\n");
321 do_patching(device
, buf
, len
);