2007-10-20 09:11:34 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Dave Chapman
|
|
|
|
*
|
|
|
|
* USB code based on ifp-line - http://ifp-driver.sourceforge.net
|
|
|
|
*
|
|
|
|
* ifp-line is (C) Pavel Kriz, Jun Yamishiro and Joe Roback and
|
|
|
|
* licensed under the GPL (v2)
|
|
|
|
*
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-10-20 09:11:34 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <usb.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2007-10-27 07:53:42 +00:00
|
|
|
#define MAX_FIRMWARESIZE (10*1024*1024) /* Arbitrary limit (for safety) */
|
2007-10-20 09:11:34 +00:00
|
|
|
|
2007-10-31 17:52:50 +00:00
|
|
|
/* For win32 compatibility: */
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2007-10-20 09:11:34 +00:00
|
|
|
struct device_t
|
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
char* label;
|
2007-10-21 11:00:28 +00:00
|
|
|
uint16_t productid;
|
2007-10-20 09:11:34 +00:00
|
|
|
uint32_t loadaddr;
|
2007-10-20 21:00:34 +00:00
|
|
|
uint32_t sdcfg;
|
2007-10-20 09:11:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct device_t devices[] =
|
|
|
|
{
|
2007-10-27 11:55:38 +00:00
|
|
|
{"c100", "Sansa C100 series", 0xb021, 0x20000000, 0x42e97010 },
|
2009-05-07 20:49:03 +00:00
|
|
|
{"m200", "Sansa M200 series", 0xb021, 0x20000000, 0x42e97010 },
|
2007-10-27 07:53:42 +00:00
|
|
|
{"cowond2", "Cowon D2", 0xb011, 0x20000000, 0xa2e92010 },
|
2010-11-10 18:55:59 +00:00
|
|
|
{"cowonj3", "Cowon J3", 0xb057, 0x20000000, 0xaaac7800 },
|
2010-05-02 09:42:51 +00:00
|
|
|
{"cowons9", "Cowon S9", 0xb057, 0x20000000, 0xa1102800 },
|
2007-10-21 11:00:28 +00:00
|
|
|
{"iaudio6", "iAudio 6", 0xb021, 0x20000000, 0x62e97010 },
|
|
|
|
{"iaudio7", "iAudio 7", 0xb021, 0x20000000, 0x62e97010 },
|
2008-02-07 20:53:20 +00:00
|
|
|
{"logikdax", "Logik DAX 1GB DAB/MP3 player", 0xb021, 0x20000000, 0x52e97410 },
|
2008-10-05 00:36:12 +00:00
|
|
|
{"x20", "iRiver X20", 0xb051, 0x20000000, 0x02e92010 },
|
2008-02-23 20:47:40 +00:00
|
|
|
{"ypp2", "Samsung YP-P2", 0xb011, 0x20000000, 0x22e92010 },
|
2008-02-23 20:50:26 +00:00
|
|
|
{"ypk3", "Samsung YP-K3", 0xb021, 0x20000000, 0x62e92018 },
|
2007-10-20 09:11:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NUM_DEVICES ((sizeof(devices) / sizeof(struct device_t)))
|
|
|
|
|
|
|
|
int find_device(char* devname)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
|
|
while ((i < NUM_DEVICES) && (strcmp(devices[i].name,devname)))
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i==NUM_DEVICES)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_devices(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
printf("Valid devices are:\n");
|
|
|
|
for (i=0; i<NUM_DEVICES; i++)
|
|
|
|
{
|
|
|
|
printf(" %10s - %s\n",devices[i].name,devices[i].label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* USB IDs for USB Boot Mode */
|
|
|
|
#define TCC_VENDORID 0x140e
|
|
|
|
|
|
|
|
#define TCC_BULK_TO 1
|
|
|
|
#define TOUT 5000
|
|
|
|
#define PACKET_SIZE 64 /* Number of bytes to send in one write */
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a,b) (((a)>(b))?(a):(b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void put_int32le(uint32_t x, char* p)
|
|
|
|
{
|
|
|
|
p[0] = x & 0xff;
|
|
|
|
p[1] = (x >> 8) & 0xff;
|
|
|
|
p[2] = (x >> 16) & 0xff;
|
|
|
|
p[3] = (x >> 24) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
int upload_app(usb_dev_handle* dh, int device, char* p, int len)
|
|
|
|
{
|
|
|
|
char buf[PACKET_SIZE];
|
|
|
|
int err;
|
|
|
|
int i;
|
|
|
|
|
2007-10-20 21:00:34 +00:00
|
|
|
/* Send the header - Destination address, length and SDCFG value */
|
2007-10-20 09:11:34 +00:00
|
|
|
memset(buf, 0, PACKET_SIZE);
|
|
|
|
|
|
|
|
put_int32le(0xf0000000, buf); /* Unknown - always the same */
|
|
|
|
put_int32le(len / PACKET_SIZE, buf + 4);
|
|
|
|
put_int32le(devices[device].loadaddr, buf + 8);
|
2007-10-20 21:00:34 +00:00
|
|
|
put_int32le(devices[device].sdcfg, buf + 12);
|
2007-10-20 09:11:34 +00:00
|
|
|
|
|
|
|
err = usb_bulk_write(dh, TCC_BULK_TO, buf, PACKET_SIZE, TOUT);
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"[ERR] Error writing header\n");
|
|
|
|
fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now send the data, PACKET_SIZE bytes at a time. */
|
|
|
|
|
|
|
|
for (i=0 ; i < (len / PACKET_SIZE) ; i++)
|
|
|
|
{
|
|
|
|
err = usb_bulk_write(dh, TCC_BULK_TO, p, PACKET_SIZE, TOUT);
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"[ERR] Error writing data\n");
|
|
|
|
fprintf(stderr,"[ERR] Bulk write error (%d, %s)\n", err, strerror(-err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += PACKET_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The main function */
|
|
|
|
|
2008-10-13 11:31:35 +00:00
|
|
|
int do_patching(int device, char* buf, int len)
|
2007-10-20 09:11:34 +00:00
|
|
|
{
|
|
|
|
struct usb_bus *busses;
|
|
|
|
struct usb_bus *bus;
|
|
|
|
struct usb_device *tmp_dev;
|
|
|
|
struct usb_device *dev = NULL;
|
|
|
|
usb_dev_handle *dh;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Searching for TCC device...\n");
|
|
|
|
|
|
|
|
usb_init();
|
|
|
|
if(usb_find_busses() < 0) {
|
|
|
|
fprintf(stderr, "[ERR] Could not find any USB busses.\n");
|
2008-10-13 11:31:35 +00:00
|
|
|
return -1;
|
2007-10-20 09:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (usb_find_devices() < 0) {
|
|
|
|
fprintf(stderr, "[ERR] USB devices not found(nor hubs!).\n");
|
2008-10-13 11:31:35 +00:00
|
|
|
return -1;
|
2007-10-20 09:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* C calling convention, it's not nice to use global stuff */
|
|
|
|
busses = usb_get_busses();
|
|
|
|
|
|
|
|
for (bus = busses; bus; bus = bus->next) {
|
|
|
|
for (tmp_dev = bus->devices; tmp_dev; tmp_dev = tmp_dev->next) {
|
|
|
|
//printf("Found Vendor %04x Product %04x\n",tmp_dev->descriptor.idVendor, tmp_dev->descriptor.idProduct);
|
|
|
|
if (tmp_dev->descriptor.idVendor == TCC_VENDORID &&
|
2007-10-21 11:00:28 +00:00
|
|
|
tmp_dev->descriptor.idProduct == devices[device].productid) {
|
2007-10-20 09:11:34 +00:00
|
|
|
|
|
|
|
dev = tmp_dev;
|
|
|
|
goto found;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev == NULL) {
|
|
|
|
fprintf(stderr, "[ERR] TCC device not found.\n");
|
|
|
|
fprintf(stderr, "[ERR] Ensure your TCC device is in USB boot mode and run tcctool again.\n");
|
2008-10-13 11:31:35 +00:00
|
|
|
return -1;
|
2007-10-20 09:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
found:
|
|
|
|
if ( (dh = usb_open(dev)) == NULL) {
|
|
|
|
fprintf(stderr,"[ERR] Unable to open TCC device.\n");
|
2008-10-13 11:31:35 +00:00
|
|
|
return -1;
|
2007-10-20 09:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = usb_set_configuration(dh, 1);
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "[ERR] usb_set_configuration failed (%d)\n", err);
|
|
|
|
usb_close(dh);
|
2008-10-13 11:31:35 +00:00
|
|
|
return -1;
|
2007-10-20 09:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* "must be called" written in the libusb documentation */
|
|
|
|
err = usb_claim_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
|
|
|
|
if (err < 0) {
|
|
|
|
fprintf(stderr, "[ERR] Unable to claim interface (%d)\n", err);
|
|
|
|
usb_close(dh);
|
2008-10-13 11:31:35 +00:00
|
|
|
return -1;
|
2007-10-20 09:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,"[INFO] Found TCC device, uploading application.\n");
|
|
|
|
|
|
|
|
/* Now we can transfer the application to the device. */
|
|
|
|
|
2008-10-13 11:31:35 +00:00
|
|
|
if ( (err = upload_app(dh, device, buf, len)) < 0)
|
2007-10-20 09:11:34 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,"[ERR] Upload of application failed.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr,"[INFO] Patching application uploaded successfully!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release claimed interface */
|
|
|
|
usb_release_interface(dh, dev->config->interface->altsetting->bInterfaceNumber);
|
|
|
|
usb_close(dh);
|
2008-10-13 11:31:35 +00:00
|
|
|
|
|
|
|
return err < 0 ? -1: 0;
|
2007-10-20 09:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t filesize(int fd) {
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if (fstat(fd,&buf) < 0) {
|
|
|
|
perror("[ERR] Checking filesize of input file");
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return(buf.st_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_usage(void)
|
|
|
|
{
|
|
|
|
printf("Usage: tcctool -d devicename firmware.bin\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
char* buf;
|
2008-10-12 18:10:39 +00:00
|
|
|
int n,len,padded_len;
|
2007-10-20 09:11:34 +00:00
|
|
|
int fd;
|
|
|
|
int device;
|
|
|
|
|
2010-05-02 09:54:14 +00:00
|
|
|
printf("tcctool " VERSION " - (C) 2007-2010 Dave Chapman\n");
|
2007-10-20 09:11:34 +00:00
|
|
|
printf("This is free software; see the source for copying conditions. There is NO\n");
|
|
|
|
printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
|
|
|
|
|
|
|
|
if (argc != 4)
|
|
|
|
{
|
|
|
|
print_usage();
|
2007-10-27 07:53:42 +00:00
|
|
|
print_devices();
|
2007-10-20 09:11:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1],"-d"))
|
|
|
|
{
|
|
|
|
print_usage();
|
2007-10-27 07:53:42 +00:00
|
|
|
print_devices();
|
2007-10-20 09:11:34 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
device = find_device(argv[2]);
|
|
|
|
|
|
|
|
if (device < 0)
|
|
|
|
{
|
|
|
|
printf("[ERR] Unknown device \"%s\"\n",argv[2]);
|
|
|
|
print_devices();
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("[INFO] Using device \"%s\"\n",devices[device].label);
|
2007-10-31 17:52:50 +00:00
|
|
|
fd = open(argv[3], O_RDONLY|O_BINARY);
|
2007-10-20 09:11:34 +00:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
printf("[ERR] Could not open %s\n", argv[3]);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = filesize(fd);
|
|
|
|
|
|
|
|
if (len > MAX_FIRMWARESIZE)
|
|
|
|
{
|
|
|
|
printf("[ERR] Firmware file too big\n");
|
|
|
|
close(fd);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2008-10-12 18:10:39 +00:00
|
|
|
/* Round len up to multiple of PACKET_SIZE */
|
|
|
|
padded_len = (len + PACKET_SIZE) & ~(PACKET_SIZE-1);
|
|
|
|
|
|
|
|
buf = malloc(padded_len);
|
2007-10-20 09:11:34 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
printf("[ERR] Could not allocate memory.\n");
|
|
|
|
close(fd);
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = read(fd, buf, len);
|
|
|
|
if (n != len)
|
|
|
|
{
|
|
|
|
printf("[ERR] Short read.\n");
|
|
|
|
close(fd);
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2008-10-13 11:31:35 +00:00
|
|
|
if (do_patching(device, buf, padded_len))
|
|
|
|
{
|
|
|
|
return 8;
|
|
|
|
}
|
2007-10-20 09:11:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|