blob: bda077a4593d1abc6f0bfc3aba495ef90cfe9833 [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
Dave Chapman71cdf002007-10-20 09:11:34 +000026#include <stdio.h>
27#include <inttypes.h>
28#include <usb.h>
29#include <string.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <unistd.h>
33#include <fcntl.h>
34
35#define VERSION "0.1"
36
Dave Chapman28c5ba82007-10-27 07:53:42 +000037#define MAX_FIRMWARESIZE (10*1024*1024) /* Arbitrary limit (for safety) */
Dave Chapman71cdf002007-10-20 09:11:34 +000038
Dave Chapman164b8f62007-10-31 17:52:50 +000039/* For win32 compatibility: */
40#ifndef O_BINARY
41#define O_BINARY 0
42#endif
43
Dave Chapman71cdf002007-10-20 09:11:34 +000044struct device_t
45{
46 char* name;
47 char* label;
Dave Chapman11251f42007-10-21 11:00:28 +000048 uint16_t productid;
Dave Chapman71cdf002007-10-20 09:11:34 +000049 uint32_t loadaddr;
Dave Chapman2c816d12007-10-20 21:00:34 +000050 uint32_t sdcfg;
Dave Chapman71cdf002007-10-20 09:11:34 +000051};
52
53static struct device_t devices[] =
54{
Dave Chapman738a8972007-10-27 11:55:38 +000055 {"c100", "Sansa C100 series", 0xb021, 0x20000000, 0x42e97010 },
Dave Chapman28c5ba82007-10-27 07:53:42 +000056 {"cowond2", "Cowon D2", 0xb011, 0x20000000, 0xa2e92010 },
Dave Chapman11251f42007-10-21 11:00:28 +000057 {"iaudio6", "iAudio 6", 0xb021, 0x20000000, 0x62e97010 },
58 {"iaudio7", "iAudio 7", 0xb021, 0x20000000, 0x62e97010 },
Dave Chapman28c5ba82007-10-27 07:53:42 +000059 {"logikdax", "Logik DAX 1GB DAB/MP3 player", 0xb021, 0x20000000, 0x52e97410 }
Dave Chapman71cdf002007-10-20 09:11:34 +000060};
61
62#define NUM_DEVICES ((sizeof(devices) / sizeof(struct device_t)))
63
64int find_device(char* devname)
65{
66 unsigned int i = 0;
67
68 while ((i < NUM_DEVICES) && (strcmp(devices[i].name,devname)))
69 i++;
70
71 if (i==NUM_DEVICES)
72 return -1;
73 else
74 return i;
75}
76
77void print_devices(void)
78{
79 unsigned int i;
80
81 printf("Valid devices are:\n");
82 for (i=0; i<NUM_DEVICES; i++)
83 {
84 printf(" %10s - %s\n",devices[i].name,devices[i].label);
85 }
86}
87
88/* USB IDs for USB Boot Mode */
89#define TCC_VENDORID 0x140e
Dave Chapman71cdf002007-10-20 09:11:34 +000090
91#define TCC_BULK_TO 1
92#define TOUT 5000
93#define PACKET_SIZE 64 /* Number of bytes to send in one write */
94
95#ifndef MAX
96#define MAX(a,b) (((a)>(b))?(a):(b))
97#endif
98
99static void put_int32le(uint32_t x, char* p)
100{
101 p[0] = x & 0xff;
102 p[1] = (x >> 8) & 0xff;
103 p[2] = (x >> 16) & 0xff;
104 p[3] = (x >> 24) & 0xff;
105}
106
107int upload_app(usb_dev_handle* dh, int device, char* p, int len)
108{
109 char buf[PACKET_SIZE];
110 int err;
111 int i;
112
Dave Chapman2c816d12007-10-20 21:00:34 +0000113 /* Send the header - Destination address, length and SDCFG value */
Dave Chapman71cdf002007-10-20 09:11:34 +0000114 memset(buf, 0, PACKET_SIZE);
115
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);
Dave Chapman2c816d12007-10-20 21:00:34 +0000119 put_int32le(devices[device].sdcfg, buf + 12);
Dave Chapman71cdf002007-10-20 09:11:34 +0000120
121 err = usb_bulk_write(dh, TCC_BULK_TO, buf, PACKET_SIZE, TOUT);
122
123 if (err < 0)
124 {
125 fprintf(stderr,"[ERR] Error writing header\n");
126 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
127 return -1;
128 }
129
130 /* Now send the data, PACKET_SIZE bytes at a time. */
131
132 for (i=0 ; i < (len / PACKET_SIZE) ; i++)
133 {
134 err = usb_bulk_write(dh, TCC_BULK_TO, p, PACKET_SIZE, TOUT);
135
136 if (err < 0)
137 {
138 fprintf(stderr,"[ERR] Error writing data\n");
139 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
140 return -1;
141 }
142
143 p += PACKET_SIZE;
144 }
145
146 return 0;
147}
148
149
150/* The main function */
151
152void do_patching(int device, char* buf, int len)
153{
154 struct usb_bus *busses;
155 struct usb_bus *bus;
156 struct usb_device *tmp_dev;
157 struct usb_device *dev = NULL;
158 usb_dev_handle *dh;
159 int err;
160
161 fprintf(stderr,"[INFO] Searching for TCC device...\n");
162
163 usb_init();
164 if(usb_find_busses() < 0) {
165 fprintf(stderr, "[ERR] Could not find any USB busses.\n");
166 return;
167 }
168
169 if (usb_find_devices() < 0) {
170 fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n");
171 return;
172 }
173
174 /* C calling convention, it's not nice to use global stuff */
175 busses = usb_get_busses();
176
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 &&
Dave Chapman11251f42007-10-21 11:00:28 +0000181 tmp_dev->descriptor.idProduct == devices[device].productid) {
Dave Chapman71cdf002007-10-20 09:11:34 +0000182
183 dev = tmp_dev;
184 goto found;
185
186 }
187 }
188 }
189
190 if (dev == NULL) {
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");
193 return;
194 }
195
196found:
197 if ( (dh = usb_open(dev)) == NULL) {
198 fprintf(stderr,"[ERR] Unable to open TCC device.\n");
199 return;
200 }
201
202 err = usb_set_configuration(dh, 1);
203
204 if (err < 0) {
205 fprintf(stderr, "[ERR] usb_set_configuration failed (%d)\n", err);
206 usb_close(dh);
207 return;
208 }
209
210 /* "must be called" written in the libusb documentation */
211 err = usb_claim_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
212 if (err < 0) {
213 fprintf(stderr, "[ERR] Unable to claim interface (%d)\n", err);
214 usb_close(dh);
215 return;
216 }
217
218 fprintf(stderr,"[INFO] Found TCC device, uploading application.\n");
219
220 /* Now we can transfer the application to the device. */
221
222 if (upload_app(dh, device, buf, len) < 0)
223 {
224 fprintf(stderr,"[ERR] Upload of application failed.\n");
225 }
226 else
227 {
228 fprintf(stderr,"[INFO] Patching application uploaded successfully!\n");
229 }
230
231 /* release claimed interface */
232 usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
233
234 usb_close(dh);
235}
236
237off_t filesize(int fd) {
238 struct stat buf;
239
240 if (fstat(fd,&buf) < 0) {
241 perror("[ERR] Checking filesize of input file");
242 return -1;
243 } else {
244 return(buf.st_size);
245 }
246}
247
248void print_usage(void)
249{
250 printf("Usage: tcctool -d devicename firmware.bin\n");
251}
252
253int main(int argc, char* argv[])
254{
255 char* buf;
256 int n,len;
257 int fd;
258 int device;
259
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");
263
264 if (argc != 4)
265 {
266 print_usage();
Dave Chapman28c5ba82007-10-27 07:53:42 +0000267 print_devices();
Dave Chapman71cdf002007-10-20 09:11:34 +0000268 return 1;
269 }
270
271 if (strcmp(argv[1],"-d"))
272 {
273 print_usage();
Dave Chapman28c5ba82007-10-27 07:53:42 +0000274 print_devices();
Dave Chapman71cdf002007-10-20 09:11:34 +0000275 return 2;
276 }
277
278 device = find_device(argv[2]);
279
280 if (device < 0)
281 {
282 printf("[ERR] Unknown device \"%s\"\n",argv[2]);
283 print_devices();
284 return 3;
285 }
286
287 printf("[INFO] Using device \"%s\"\n",devices[device].label);
Dave Chapman164b8f62007-10-31 17:52:50 +0000288 fd = open(argv[3], O_RDONLY|O_BINARY);
Dave Chapman71cdf002007-10-20 09:11:34 +0000289 if (fd < 0)
290 {
291 printf("[ERR] Could not open %s\n", argv[3]);
292 return 4;
293 }
294
295 len = filesize(fd);
296
297 if (len > MAX_FIRMWARESIZE)
298 {
299 printf("[ERR] Firmware file too big\n");
300 close(fd);
301 return 5;
302 }
303
304 buf = malloc(len);
305 if (buf == NULL)
306 {
307 printf("[ERR] Could not allocate memory.\n");
308 close(fd);
309 return 6;
310 }
311
312 n = read(fd, buf, len);
313 if (n != len)
314 {
315 printf("[ERR] Short read.\n");
316 close(fd);
317 return 7;
318 }
319 close(fd);
320
321 do_patching(device, buf, len);
322
323 return 0;
324}