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 },
60 {"ypp2", "Samsung YP-P2", 0xb011, 0x20000000, 0x22e92010 },
61 {"ypk3", "Samsung YP-K3", 0xb021, 0x20000000, 0x62e92018 },
64 #define NUM_DEVICES ((sizeof(devices) / sizeof(struct device_t)))
66 int find_device(char* devname
)
70 while ((i
< NUM_DEVICES
) && (strcmp(devices
[i
].name
,devname
)))
79 void print_devices(void)
83 printf("Valid devices are:\n");
84 for (i
=0; i
<NUM_DEVICES
; i
++)
86 printf(" %10s - %s\n",devices
[i
].name
,devices
[i
].label
);
90 /* USB IDs for USB Boot Mode */
91 #define TCC_VENDORID 0x140e
95 #define PACKET_SIZE 64 /* Number of bytes to send in one write */
98 #define MAX(a,b) (((a)>(b))?(a):(b))
101 static void put_int32le(uint32_t x
, char* p
)
104 p
[1] = (x
>> 8) & 0xff;
105 p
[2] = (x
>> 16) & 0xff;
106 p
[3] = (x
>> 24) & 0xff;
109 int upload_app(usb_dev_handle
* dh
, int device
, char* p
, int len
)
111 char buf
[PACKET_SIZE
];
115 /* Send the header - Destination address, length and SDCFG value */
116 memset(buf
, 0, PACKET_SIZE
);
118 put_int32le(0xf0000000, buf
); /* Unknown - always the same */
119 put_int32le(len
/ PACKET_SIZE
, buf
+ 4);
120 put_int32le(devices
[device
].loadaddr
, buf
+ 8);
121 put_int32le(devices
[device
].sdcfg
, buf
+ 12);
123 err
= usb_bulk_write(dh
, TCC_BULK_TO
, buf
, PACKET_SIZE
, TOUT
);
127 fprintf(stderr
,"[ERR] Error writing header\n");
128 fprintf(stderr
,"[ERR] Bulk write error (%d, %s)\n", err
, strerror(-err
));
132 /* Now send the data, PACKET_SIZE bytes at a time. */
134 for (i
=0 ; i
< (len
/ PACKET_SIZE
) ; i
++)
136 err
= usb_bulk_write(dh
, TCC_BULK_TO
, p
, PACKET_SIZE
, TOUT
);
140 fprintf(stderr
,"[ERR] Error writing data\n");
141 fprintf(stderr
,"[ERR] Bulk write error (%d, %s)\n", err
, strerror(-err
));
152 /* The main function */
154 void do_patching(int device
, char* buf
, int len
)
156 struct usb_bus
*busses
;
158 struct usb_device
*tmp_dev
;
159 struct usb_device
*dev
= NULL
;
163 fprintf(stderr
,"[INFO] Searching for TCC device...\n");
166 if(usb_find_busses() < 0) {
167 fprintf(stderr
, "[ERR] Could not find any USB busses.\n");
171 if (usb_find_devices() < 0) {
172 fprintf(stderr
, "[ERR] USB devices not found(nor hubs!).\n");
176 /* C calling convention, it's not nice to use global stuff */
177 busses
= usb_get_busses();
179 for (bus
= busses
; bus
; bus
= bus
->next
) {
180 for (tmp_dev
= bus
->devices
; tmp_dev
; tmp_dev
= tmp_dev
->next
) {
181 //printf("Found Vendor %04x Product %04x\n",tmp_dev->descriptor.idVendor, tmp_dev->descriptor.idProduct);
182 if (tmp_dev
->descriptor
.idVendor
== TCC_VENDORID
&&
183 tmp_dev
->descriptor
.idProduct
== devices
[device
].productid
) {
193 fprintf(stderr
, "[ERR] TCC device not found.\n");
194 fprintf(stderr
, "[ERR] Ensure your TCC device is in USB boot mode and run tcctool again.\n");
199 if ( (dh
= usb_open(dev
)) == NULL
) {
200 fprintf(stderr
,"[ERR] Unable to open TCC device.\n");
204 err
= usb_set_configuration(dh
, 1);
207 fprintf(stderr
, "[ERR] usb_set_configuration failed (%d)\n", err
);
212 /* "must be called" written in the libusb documentation */
213 err
= usb_claim_interface(dh
, dev
->config
->interface
->altsetting
->bInterfaceNumber
);
215 fprintf(stderr
, "[ERR] Unable to claim interface (%d)\n", err
);
220 fprintf(stderr
,"[INFO] Found TCC device, uploading application.\n");
222 /* Now we can transfer the application to the device. */
224 if (upload_app(dh
, device
, buf
, len
) < 0)
226 fprintf(stderr
,"[ERR] Upload of application failed.\n");
230 fprintf(stderr
,"[INFO] Patching application uploaded successfully!\n");
233 /* release claimed interface */
234 usb_release_interface(dh
, dev
->config
->interface
->altsetting
->bInterfaceNumber
);
239 off_t
filesize(int fd
) {
242 if (fstat(fd
,&buf
) < 0) {
243 perror("[ERR] Checking filesize of input file");
250 void print_usage(void)
252 printf("Usage: tcctool -d devicename firmware.bin\n");
255 int main(int argc
, char* argv
[])
262 printf("tcctool v" VERSION
" - (C) 2007 Dave Chapman\n");
263 printf("This is free software; see the source for copying conditions. There is NO\n");
264 printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
273 if (strcmp(argv
[1],"-d"))
280 device
= find_device(argv
[2]);
284 printf("[ERR] Unknown device \"%s\"\n",argv
[2]);
289 printf("[INFO] Using device \"%s\"\n",devices
[device
].label
);
290 fd
= open(argv
[3], O_RDONLY
|O_BINARY
);
293 printf("[ERR] Could not open %s\n", argv
[3]);
299 if (len
> MAX_FIRMWARESIZE
)
301 printf("[ERR] Firmware file too big\n");
309 printf("[ERR] Could not allocate memory.\n");
314 n
= read(fd
, buf
, len
);
317 printf("[ERR] Short read.\n");
323 do_patching(device
, buf
, len
);