2012-08-08 17:02:31 +02:00
|
|
|
/*
|
|
|
|
|
0xFFFF - Open Free Fiasco Firmware Flasher
|
|
|
|
|
Copyright (C) 2012 Pali Rohár <pali.rohar@gmail.com>
|
|
|
|
|
|
|
|
|
|
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 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include <usb.h>
|
|
|
|
|
|
2012-08-09 18:25:48 +02:00
|
|
|
#include "global.h"
|
2012-08-08 17:02:31 +02:00
|
|
|
#include "device.h"
|
2012-08-09 16:13:43 +02:00
|
|
|
#include "usb-device.h"
|
2012-08-10 12:03:30 +02:00
|
|
|
#include "printf-utils.h"
|
2012-08-09 18:25:48 +02:00
|
|
|
|
2012-08-08 17:02:31 +02:00
|
|
|
static struct usb_flash_device usb_devices[] = {
|
|
|
|
|
{ 0x0421, 0x0105, 2, 1, -1, FLASH_NOLO, { DEVICE_SU_18, DEVICE_RX_44, DEVICE_RX_48, DEVICE_RX_51, 0 } },
|
|
|
|
|
{ 0x0421, 0x0106, 0, -1, -1, FLASH_COLD, { DEVICE_RX_51, 0 } },
|
|
|
|
|
{ 0x0421, 0x01c7, 0, -1, -1, FLASH_DISK, { DEVICE_RX_51, 0 } },
|
2012-08-10 15:39:55 +02:00
|
|
|
{ 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, 0 } },
|
2012-08-08 17:02:31 +02:00
|
|
|
{ 0x0421, 0x0431, 0, -1, -1, FLASH_DISK, { DEVICE_SU_18, DEVICE_RX_34, 0 } },
|
|
|
|
|
{ 0x0421, 0x3f00, 2, 1, -1, FLASH_NOLO, { DEVICE_RX_34, 0 } },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char * usb_flash_protocols[] = {
|
|
|
|
|
[FLASH_NOLO] = "NOLO",
|
|
|
|
|
[FLASH_COLD] = "Cold flashing",
|
|
|
|
|
[FLASH_MKII] = "Mkii protcol",
|
|
|
|
|
[FLASH_DISK] = "RAW disk",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char * usb_flash_protocol_to_string(enum usb_flash_protocol protocol) {
|
|
|
|
|
|
|
|
|
|
if ( protocol > sizeof(usb_flash_protocols) )
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return usb_flash_protocols[protocol];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 19:43:22 +02:00
|
|
|
static void usb_flash_device_info_print(const struct usb_flash_device * dev) {
|
2012-08-08 17:02:31 +02:00
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_ADD("USB device %s", device_to_string(dev->devices[0]));
|
2012-08-08 17:02:31 +02:00
|
|
|
|
|
|
|
|
for ( i = 1; dev->devices[i]; ++i )
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_ADD("/%s", device_to_string(dev->devices[i]));
|
2012-08-08 17:02:31 +02:00
|
|
|
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_ADD(" (%#04x:%#04x) in %s mode", dev->vendor, dev->product, usb_flash_protocol_to_string(dev->protocol));
|
2012-08-08 17:02:31 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) {
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
struct usb_device_info * ret = NULL;
|
|
|
|
|
|
|
|
|
|
for ( i = 0; usb_devices[i].vendor; ++i ) {
|
|
|
|
|
|
|
|
|
|
if ( dev->descriptor.idVendor == usb_devices[i].vendor && dev->descriptor.idProduct == usb_devices[i].product ) {
|
|
|
|
|
|
2012-08-10 21:23:19 +02:00
|
|
|
printf("\b\b ");
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_END();
|
|
|
|
|
PRINTF_ADD("Found ");
|
2012-08-09 19:43:22 +02:00
|
|
|
usb_flash_device_info_print(&usb_devices[i]);
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_END();
|
2012-08-08 17:02:31 +02:00
|
|
|
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_LINE("Opening USB...");
|
2012-08-08 17:02:31 +02:00
|
|
|
usb_dev_handle * udev = usb_open(dev);
|
|
|
|
|
if ( ! udev ) {
|
2012-08-10 12:01:14 +02:00
|
|
|
PRINTF_ERROR("usb_open failed");
|
2012-08-08 17:02:31 +02:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(LIBUSB_HAS_GET_DRIVER_NP) && defined(LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP)
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_LINE("Detaching kernel from USB interface...");
|
2012-08-08 17:02:31 +02:00
|
|
|
usb_detach_kernel_driver_np(udev, usb_devices[i].interface);
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_LINE("Claiming USB interface...");
|
2012-08-08 17:02:31 +02:00
|
|
|
if ( usb_claim_interface(udev, usb_devices[i].interface) < 0 ) {
|
2012-08-10 12:01:14 +02:00
|
|
|
PRINTF_ERROR("usb_claim_interface failed");
|
2012-08-08 17:02:31 +02:00
|
|
|
usb_close(udev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( usb_devices[i].alternate >= 0 ) {
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_LINE("Setting alternate USB interface...");
|
2012-08-08 17:02:31 +02:00
|
|
|
if ( usb_set_altinterface(udev, usb_devices[i].alternate) < 0 ) {
|
2012-08-10 12:01:14 +02:00
|
|
|
PRINTF_ERROR("usb_claim_interface failed");
|
2012-08-08 17:02:31 +02:00
|
|
|
usb_close(udev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( usb_devices[i].configuration >= 0 ) {
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_LINE("Setting USB configuration...");
|
2012-08-08 17:02:31 +02:00
|
|
|
if ( usb_set_configuration(udev, usb_devices[i].configuration) < 0 ) {
|
2012-08-10 12:01:14 +02:00
|
|
|
PRINTF_ERROR("usb_set_configuration failed");
|
2012-08-08 17:02:31 +02:00
|
|
|
usb_close(udev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 19:43:22 +02:00
|
|
|
ret = calloc(1, sizeof(struct usb_device_info));
|
2012-08-08 17:02:31 +02:00
|
|
|
if ( ! ret ) {
|
2012-08-09 18:25:48 +02:00
|
|
|
ALLOC_ERROR();
|
2012-08-08 17:02:31 +02:00
|
|
|
usb_close(udev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 19:43:22 +02:00
|
|
|
ret->detected_device = DEVICE_UNKNOWN;
|
2012-08-11 09:50:38 +02:00
|
|
|
ret->detected_hwrev = NULL;
|
2012-08-09 19:43:22 +02:00
|
|
|
ret->flash_device = &usb_devices[i];
|
|
|
|
|
ret->udev = udev;
|
2012-08-08 17:02:31 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct usb_device_info * usb_search_device(struct usb_device * dev, int level) {
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
struct usb_device_info * ret = NULL;
|
|
|
|
|
|
|
|
|
|
if ( ! dev )
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
ret = usb_device_is_valid(dev);
|
|
|
|
|
if ( ret )
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
for ( i = 0; i < dev->num_children; i++ ) {
|
|
|
|
|
ret = usb_search_device(dev->children[i], level + 1);
|
|
|
|
|
if ( ret )
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 18:29:13 +02:00
|
|
|
struct usb_device_info * usb_open_and_wait_for_device(void) {
|
2012-08-08 17:02:31 +02:00
|
|
|
|
|
|
|
|
struct usb_bus * bus;
|
|
|
|
|
struct usb_device_info * ret = NULL;
|
|
|
|
|
int i = 0;
|
|
|
|
|
static char progress[] = {'/','-','\\', '|'};
|
|
|
|
|
|
|
|
|
|
usb_init();
|
|
|
|
|
usb_find_busses();
|
|
|
|
|
|
|
|
|
|
while ( 1 ) {
|
|
|
|
|
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_LINE("Waiting for USB device... %c", progress[++i%sizeof(progress)]);
|
2012-08-08 17:02:31 +02:00
|
|
|
|
|
|
|
|
usb_find_devices();
|
|
|
|
|
|
|
|
|
|
for ( bus = usb_get_busses(); bus; bus = bus->next ) {
|
|
|
|
|
|
|
|
|
|
if ( bus->root_dev )
|
|
|
|
|
ret = usb_search_device(bus->root_dev, 0);
|
|
|
|
|
else {
|
|
|
|
|
struct usb_device *dev;
|
|
|
|
|
for ( dev = bus->devices; dev; dev = dev->next ) {
|
|
|
|
|
ret = usb_search_device(dev, 0);
|
|
|
|
|
if ( ret )
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ret )
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ret )
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
usleep(0xc350); // 0.5s
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-09 18:25:48 +02:00
|
|
|
PRINTF_BACK();
|
2012-08-08 17:02:31 +02:00
|
|
|
|
|
|
|
|
if ( ! ret )
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usb_close_device(struct usb_device_info * dev) {
|
|
|
|
|
|
2012-08-11 09:50:38 +02:00
|
|
|
usb_close(dev->udev);
|
|
|
|
|
free(dev->detected_hwrev);
|
2012-08-08 17:02:31 +02:00
|
|
|
free(dev);
|
|
|
|
|
|
|
|
|
|
}
|