Oops. Should commit patches correctly.
[Rockbox.git] / utils / tcctool / tcctool.c
blob82aed58243c0118a36e4b5ab3c8dcd81288c51df
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 },
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)
68 unsigned int i = 0;
70 while ((i < NUM_DEVICES) && (strcmp(devices[i].name,devname)))
71 i++;
73 if (i==NUM_DEVICES)
74 return -1;
75 else
76 return i;
79 void print_devices(void)
81 unsigned int i;
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
93 #define TCC_BULK_TO 1
94 #define TOUT 5000
95 #define PACKET_SIZE 64 /* Number of bytes to send in one write */
97 #ifndef MAX
98 #define MAX(a,b) (((a)>(b))?(a):(b))
99 #endif
101 static void put_int32le(uint32_t x, char* p)
103 p[0] = x & 0xff;
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];
112 int err;
113 int i;
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);
125 if (err < 0)
127 fprintf(stderr,"[ERR] Error writing header\n");
128 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
129 return -1;
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);
138 if (err < 0)
140 fprintf(stderr,"[ERR] Error writing data\n");
141 fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
142 return -1;
145 p += PACKET_SIZE;
148 return 0;
152 /* The main function */
154 void do_patching(int device, char* buf, int len)
156 struct usb_bus *busses;
157 struct usb_bus *bus;
158 struct usb_device *tmp_dev;
159 struct usb_device *dev = NULL;
160 usb_dev_handle *dh;
161 int err;
163 fprintf(stderr,"[INFO] Searching for TCC device...\n");
165 usb_init();
166 if(usb_find_busses() < 0) {
167 fprintf(stderr, "[ERR] Could not find any USB busses.\n");
168 return;
171 if (usb_find_devices() < 0) {
172 fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n");
173 return;
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) {
185 dev = tmp_dev;
186 goto found;
192 if (dev == NULL) {
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");
195 return;
198 found:
199 if ( (dh = usb_open(dev)) == NULL) {
200 fprintf(stderr,"[ERR] Unable to open TCC device.\n");
201 return;
204 err = usb_set_configuration(dh, 1);
206 if (err < 0) {
207 fprintf(stderr, "[ERR] usb_set_configuration failed (%d)\n", err);
208 usb_close(dh);
209 return;
212 /* "must be called" written in the libusb documentation */
213 err = usb_claim_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
214 if (err < 0) {
215 fprintf(stderr, "[ERR] Unable to claim interface (%d)\n", err);
216 usb_close(dh);
217 return;
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");
228 else
230 fprintf(stderr,"[INFO] Patching application uploaded successfully!\n");
233 /* release claimed interface */
234 usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
236 usb_close(dh);
239 off_t filesize(int fd) {
240 struct stat buf;
242 if (fstat(fd,&buf) < 0) {
243 perror("[ERR] Checking filesize of input file");
244 return -1;
245 } else {
246 return(buf.st_size);
250 void print_usage(void)
252 printf("Usage: tcctool -d devicename firmware.bin\n");
255 int main(int argc, char* argv[])
257 char* buf;
258 int n,len;
259 int fd;
260 int device;
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");
266 if (argc != 4)
268 print_usage();
269 print_devices();
270 return 1;
273 if (strcmp(argv[1],"-d"))
275 print_usage();
276 print_devices();
277 return 2;
280 device = find_device(argv[2]);
282 if (device < 0)
284 printf("[ERR] Unknown device \"%s\"\n",argv[2]);
285 print_devices();
286 return 3;
289 printf("[INFO] Using device \"%s\"\n",devices[device].label);
290 fd = open(argv[3], O_RDONLY|O_BINARY);
291 if (fd < 0)
293 printf("[ERR] Could not open %s\n", argv[3]);
294 return 4;
297 len = filesize(fd);
299 if (len > MAX_FIRMWARESIZE)
301 printf("[ERR] Firmware file too big\n");
302 close(fd);
303 return 5;
306 buf = malloc(len);
307 if (buf == NULL)
309 printf("[ERR] Could not allocate memory.\n");
310 close(fd);
311 return 6;
314 n = read(fd, buf, len);
315 if (n != len)
317 printf("[ERR] Short read.\n");
318 close(fd);
319 return 7;
321 close(fd);
323 do_patching(device, buf, len);
325 return 0;