Use O_BINARY when reading the firmware file - for win32 compatibility
[Rockbox.git] / utils / tcctool / tcctool.c
blobbda077a4593d1abc6f0bfc3aba495ef90cfe9833
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
26 #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>
35 #define VERSION "0.1"
37 #define MAX_FIRMWARESIZE (10*1024*1024) /* Arbitrary limit (for safety) */
39 /* For win32 compatibility: */
40 #ifndef O_BINARY
41 #define O_BINARY 0
42 #endif
44 struct device_t
46 char* name;
47 char* label;
48 uint16_t productid;
49 uint32_t loadaddr;
50 uint32_t sdcfg;
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)
66 unsigned int i = 0;
68 while ((i < NUM_DEVICES) && (strcmp(devices[i].name,devname)))
69 i++;
71 if (i==NUM_DEVICES)
72 return -1;
73 else
74 return i;
77 void print_devices(void)
79 unsigned int i;
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
91 #define TCC_BULK_TO 1
92 #define TOUT 5000
93 #define PACKET_SIZE 64 /* Number of bytes to send in one write */
95 #ifndef MAX
96 #define MAX(a,b) (((a)>(b))?(a):(b))
97 #endif
99 static void put_int32le(uint32_t x, char* p)
101 p[0] = x & 0xff;
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];
110 int err;
111 int i;
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);
123 if (err < 0)
125 fprintf(stderr,"[ERR] Error writing header\n");
126 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
127 return -1;
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);
136 if (err < 0)
138 fprintf(stderr,"[ERR] Error writing data\n");
139 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
140 return -1;
143 p += PACKET_SIZE;
146 return 0;
150 /* The main function */
152 void do_patching(int device, char* buf, int len)
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;
161 fprintf(stderr,"[INFO] Searching for TCC device...\n");
163 usb_init();
164 if(usb_find_busses() < 0) {
165 fprintf(stderr, "[ERR] Could not find any USB busses.\n");
166 return;
169 if (usb_find_devices() < 0) {
170 fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n");
171 return;
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) {
183 dev = tmp_dev;
184 goto found;
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;
196 found:
197 if ( (dh = usb_open(dev)) == NULL) {
198 fprintf(stderr,"[ERR] Unable to open TCC device.\n");
199 return;
202 err = usb_set_configuration(dh, 1);
204 if (err < 0) {
205 fprintf(stderr, "[ERR] usb_set_configuration failed (%d)\n", err);
206 usb_close(dh);
207 return;
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;
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");
226 else
228 fprintf(stderr,"[INFO] Patching application uploaded successfully!\n");
231 /* release claimed interface */
232 usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
234 usb_close(dh);
237 off_t filesize(int fd) {
238 struct stat buf;
240 if (fstat(fd,&buf) < 0) {
241 perror("[ERR] Checking filesize of input file");
242 return -1;
243 } else {
244 return(buf.st_size);
248 void print_usage(void)
250 printf("Usage: tcctool -d devicename firmware.bin\n");
253 int main(int argc, char* argv[])
255 char* buf;
256 int n,len;
257 int fd;
258 int device;
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");
264 if (argc != 4)
266 print_usage();
267 print_devices();
268 return 1;
271 if (strcmp(argv[1],"-d"))
273 print_usage();
274 print_devices();
275 return 2;
278 device = find_device(argv[2]);
280 if (device < 0)
282 printf("[ERR] Unknown device \"%s\"\n",argv[2]);
283 print_devices();
284 return 3;
287 printf("[INFO] Using device \"%s\"\n",devices[device].label);
288 fd = open(argv[3], O_RDONLY|O_BINARY);
289 if (fd < 0)
291 printf("[ERR] Could not open %s\n", argv[3]);
292 return 4;
295 len = filesize(fd);
297 if (len > MAX_FIRMWARESIZE)
299 printf("[ERR] Firmware file too big\n");
300 close(fd);
301 return 5;
304 buf = malloc(len);
305 if (buf == NULL)
307 printf("[ERR] Could not allocate memory.\n");
308 close(fd);
309 return 6;
312 n = read(fd, buf, len);
313 if (n != len)
315 printf("[ERR] Short read.\n");
316 close(fd);
317 return 7;
319 close(fd);
321 do_patching(device, buf, len);
323 return 0;