blob: 4c4ea8feddf1295d9c0d74fd0e5da90129802381 [file] [log] [blame]
Dave Chapman71cdf002007-10-20 09:11:34 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007 Dave Chapman
11 *
12 * USB code based on ifp-line - http://ifp-driver.sourceforge.net
13 *
14 * ifp-line is (C) Pavel Kriz, Jun Yamishiro and Joe Roback and
15 * licensed under the GPL (v2)
16 *
17 *
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.
20 *
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
23 *
24 ****************************************************************************/
25
26
27#include <stdio.h>
28#include <inttypes.h>
29#include <usb.h>
30#include <string.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <unistd.h>
34#include <fcntl.h>
35
36#define VERSION "0.1"
37
Dave Chapman28c5ba82007-10-27 07:53:42 +000038#define MAX_FIRMWARESIZE (10*1024*1024) /* Arbitrary limit (for safety) */
Dave Chapman71cdf002007-10-20 09:11:34 +000039
40struct device_t
41{
42 char* name;
43 char* label;
Dave Chapman11251f42007-10-21 11:00:28 +000044 uint16_t productid;
Dave Chapman71cdf002007-10-20 09:11:34 +000045 uint32_t loadaddr;
Dave Chapman2c816d12007-10-20 21:00:34 +000046 uint32_t sdcfg;
Dave Chapman71cdf002007-10-20 09:11:34 +000047};
48
49static struct device_t devices[] =
50{
Dave Chapman738a8972007-10-27 11:55:38 +000051 {"c100", "Sansa C100 series", 0xb021, 0x20000000, 0x42e97010 },
Dave Chapman28c5ba82007-10-27 07:53:42 +000052 {"cowond2", "Cowon D2", 0xb011, 0x20000000, 0xa2e92010 },
Dave Chapman11251f42007-10-21 11:00:28 +000053 {"iaudio6", "iAudio 6", 0xb021, 0x20000000, 0x62e97010 },
54 {"iaudio7", "iAudio 7", 0xb021, 0x20000000, 0x62e97010 },
Dave Chapman28c5ba82007-10-27 07:53:42 +000055 {"logikdax", "Logik DAX 1GB DAB/MP3 player", 0xb021, 0x20000000, 0x52e97410 }
Dave Chapman71cdf002007-10-20 09:11:34 +000056};
57
58#define NUM_DEVICES ((sizeof(devices) / sizeof(struct device_t)))
59
60int find_device(char* devname)
61{
62 unsigned int i = 0;
63
64 while ((i < NUM_DEVICES) && (strcmp(devices[i].name,devname)))
65 i++;
66
67 if (i==NUM_DEVICES)
68 return -1;
69 else
70 return i;
71}
72
73void print_devices(void)
74{
75 unsigned int i;
76
77 printf("Valid devices are:\n");
78 for (i=0; i<NUM_DEVICES; i++)
79 {
80 printf(" %10s - %s\n",devices[i].name,devices[i].label);
81 }
82}
83
84/* USB IDs for USB Boot Mode */
85#define TCC_VENDORID 0x140e
Dave Chapman71cdf002007-10-20 09:11:34 +000086
87#define TCC_BULK_TO 1
88#define TOUT 5000
89#define PACKET_SIZE 64 /* Number of bytes to send in one write */
90
91#ifndef MAX
92#define MAX(a,b) (((a)>(b))?(a):(b))
93#endif
94
95static void put_int32le(uint32_t x, char* p)
96{
97 p[0] = x & 0xff;
98 p[1] = (x >> 8) & 0xff;
99 p[2] = (x >> 16) & 0xff;
100 p[3] = (x >> 24) & 0xff;
101}
102
103int upload_app(usb_dev_handle* dh, int device, char* p, int len)
104{
105 char buf[PACKET_SIZE];
106 int err;
107 int i;
108
Dave Chapman2c816d12007-10-20 21:00:34 +0000109 /* Send the header - Destination address, length and SDCFG value */
Dave Chapman71cdf002007-10-20 09:11:34 +0000110 memset(buf, 0, PACKET_SIZE);
111
112 put_int32le(0xf0000000, buf); /* Unknown - always the same */
113 put_int32le(len / PACKET_SIZE, buf + 4);
114 put_int32le(devices[device].loadaddr, buf + 8);
Dave Chapman2c816d12007-10-20 21:00:34 +0000115 put_int32le(devices[device].sdcfg, buf + 12);
Dave Chapman71cdf002007-10-20 09:11:34 +0000116
117 err = usb_bulk_write(dh, TCC_BULK_TO, buf, PACKET_SIZE, TOUT);
118
119 if (err < 0)
120 {
121 fprintf(stderr,"[ERR] Error writing header\n");
122 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
123 return -1;
124 }
125
126 /* Now send the data, PACKET_SIZE bytes at a time. */
127
128 for (i=0 ; i < (len / PACKET_SIZE) ; i++)
129 {
130 err = usb_bulk_write(dh, TCC_BULK_TO, p, PACKET_SIZE, TOUT);
131
132 if (err < 0)
133 {
134 fprintf(stderr,"[ERR] Error writing data\n");
135 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
136 return -1;
137 }
138
139 p += PACKET_SIZE;
140 }
141
142 return 0;
143}
144
145
146/* The main function */
147
148void do_patching(int device, char* buf, int len)
149{
150 struct usb_bus *busses;
151 struct usb_bus *bus;
152 struct usb_device *tmp_dev;
153 struct usb_device *dev = NULL;
154 usb_dev_handle *dh;
155 int err;
156
157 fprintf(stderr,"[INFO] Searching for TCC device...\n");
158
159 usb_init();
160 if(usb_find_busses() < 0) {
161 fprintf(stderr, "[ERR] Could not find any USB busses.\n");
162 return;
163 }
164
165 if (usb_find_devices() < 0) {
166 fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n");
167 return;
168 }
169
170 /* C calling convention, it's not nice to use global stuff */
171 busses = usb_get_busses();
172
173 for (bus = busses; bus; bus = bus->next) {
174 for (tmp_dev = bus->devices; tmp_dev; tmp_dev = tmp_dev->next) {
175 //printf("Found Vendor %04x Product %04x\n",tmp_dev->descriptor.idVendor, tmp_dev->descriptor.idProduct);
176 if (tmp_dev->descriptor.idVendor == TCC_VENDORID &&
Dave Chapman11251f42007-10-21 11:00:28 +0000177 tmp_dev->descriptor.idProduct == devices[device].productid) {
Dave Chapman71cdf002007-10-20 09:11:34 +0000178
179 dev = tmp_dev;
180 goto found;
181
182 }
183 }
184 }
185
186 if (dev == NULL) {
187 fprintf(stderr, "[ERR] TCC device not found.\n");
188 fprintf(stderr, "[ERR] Ensure your TCC device is in USB boot mode and run tcctool again.\n");
189 return;
190 }
191
192found:
193 if ( (dh = usb_open(dev)) == NULL) {
194 fprintf(stderr,"[ERR] Unable to open TCC device.\n");
195 return;
196 }
197
198 err = usb_set_configuration(dh, 1);
199
200 if (err < 0) {
201 fprintf(stderr, "[ERR] usb_set_configuration failed (%d)\n", err);
202 usb_close(dh);
203 return;
204 }
205
206 /* "must be called" written in the libusb documentation */
207 err = usb_claim_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
208 if (err < 0) {
209 fprintf(stderr, "[ERR] Unable to claim interface (%d)\n", err);
210 usb_close(dh);
211 return;
212 }
213
214 fprintf(stderr,"[INFO] Found TCC device, uploading application.\n");
215
216 /* Now we can transfer the application to the device. */
217
218 if (upload_app(dh, device, buf, len) < 0)
219 {
220 fprintf(stderr,"[ERR] Upload of application failed.\n");
221 }
222 else
223 {
224 fprintf(stderr,"[INFO] Patching application uploaded successfully!\n");
225 }
226
227 /* release claimed interface */
228 usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
229
230 usb_close(dh);
231}
232
233off_t filesize(int fd) {
234 struct stat buf;
235
236 if (fstat(fd,&buf) < 0) {
237 perror("[ERR] Checking filesize of input file");
238 return -1;
239 } else {
240 return(buf.st_size);
241 }
242}
243
244void print_usage(void)
245{
246 printf("Usage: tcctool -d devicename firmware.bin\n");
247}
248
249int main(int argc, char* argv[])
250{
251 char* buf;
252 int n,len;
253 int fd;
254 int device;
255
256 printf("tcctool v" VERSION " - (C) 2007 Dave Chapman\n");
257 printf("This is free software; see the source for copying conditions. There is NO\n");
258 printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
259
260 if (argc != 4)
261 {
262 print_usage();
Dave Chapman28c5ba82007-10-27 07:53:42 +0000263 print_devices();
Dave Chapman71cdf002007-10-20 09:11:34 +0000264 return 1;
265 }
266
267 if (strcmp(argv[1],"-d"))
268 {
269 print_usage();
Dave Chapman28c5ba82007-10-27 07:53:42 +0000270 print_devices();
Dave Chapman71cdf002007-10-20 09:11:34 +0000271 return 2;
272 }
273
274 device = find_device(argv[2]);
275
276 if (device < 0)
277 {
278 printf("[ERR] Unknown device \"%s\"\n",argv[2]);
279 print_devices();
280 return 3;
281 }
282
283 printf("[INFO] Using device \"%s\"\n",devices[device].label);
284 fd = open(argv[3], O_RDONLY);
285 if (fd < 0)
286 {
287 printf("[ERR] Could not open %s\n", argv[3]);
288 return 4;
289 }
290
291 len = filesize(fd);
292
293 if (len > MAX_FIRMWARESIZE)
294 {
295 printf("[ERR] Firmware file too big\n");
296 close(fd);
297 return 5;
298 }
299
300 buf = malloc(len);
301 if (buf == NULL)
302 {
303 printf("[ERR] Could not allocate memory.\n");
304 close(fd);
305 return 6;
306 }
307
308 n = read(fd, buf, len);
309 if (n != len)
310 {
311 printf("[ERR] Short read.\n");
312 close(fd);
313 return 7;
314 }
315 close(fd);
316
317 do_patching(device, buf, len);
318
319 return 0;
320}