Cold Flasher - Remove duplicate usb code, prepair for flashing form fiasco images
This commit is contained in:
1 parent
3f0d6f85d3
commit
418f3cfd4b
1 file changed
+89
-141
+89
-141
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
cold-flasher-rx51.c - Cold flasher for Nokia RX51
|
cold-flash.c - Cold flashing
|
||||||
Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
|
Copyright (C) 2011-2012 Pali Rohár <pali.rohar@gmail.com>
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* compile: gcc cold-flasher-rx51.c -o cold-flasher-rx51 -W -Wall -O2 -lusb */
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -30,9 +28,6 @@
|
|||||||
#define READ_TIMEOUT 500
|
#define READ_TIMEOUT 500
|
||||||
#define WRITE_TIMEOUT 3000
|
#define WRITE_TIMEOUT 3000
|
||||||
|
|
||||||
#define USB_VENDOR 0x0421
|
|
||||||
#define USB_PRODUCT 0x0106
|
|
||||||
|
|
||||||
static uint32_t tab[256];
|
static uint32_t tab[256];
|
||||||
|
|
||||||
static void crc32_gentab(void) {
|
static void crc32_gentab(void) {
|
||||||
@@ -77,7 +72,7 @@ static uint32_t crc32(unsigned char * bytes, size_t size, uint32_t crc) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Omap download message - same as in pusb.c */
|
/* Omap download message */
|
||||||
static const uint8_t omap_download_msg[4] = { 0x02, 0x00, 0x03, 0xF0 };
|
static const uint8_t omap_download_msg[4] = { 0x02, 0x00, 0x03, 0xF0 };
|
||||||
|
|
||||||
/* Structure of X-Loader message */
|
/* Structure of X-Loader message */
|
||||||
@@ -91,20 +86,31 @@ struct xloader_msg {
|
|||||||
#define XLOADER_MSG_TYPE_PING 0x6301326E
|
#define XLOADER_MSG_TYPE_PING 0x6301326E
|
||||||
#define XLOADER_MSG_TYPE_SEND 0x6302326E
|
#define XLOADER_MSG_TYPE_SEND 0x6302326E
|
||||||
|
|
||||||
struct xloader_msg xloader_msg_create(uint32_t type, uint32_t size, FILE * file) {
|
struct xloader_msg xloader_msg_create(uint32_t type, FILE * file, size_t offset, uint32_t size) {
|
||||||
|
|
||||||
struct xloader_msg msg;
|
struct xloader_msg msg;
|
||||||
uint8_t buffer[1024];
|
uint8_t buffer[1024];
|
||||||
|
size_t need, readed;
|
||||||
uint32_t crc = 0;
|
uint32_t crc = 0;
|
||||||
|
int ret;
|
||||||
|
|
||||||
memcpy(msg.type, &type, 4);
|
memcpy(msg.type, &type, 4);
|
||||||
memcpy(msg.size, &size, 4);
|
memcpy(msg.size, &size, 4);
|
||||||
|
|
||||||
while ( file && ( size = fread(buffer, 1, sizeof(buffer), file) ) > 0 )
|
if ( file ) {
|
||||||
crc = crc32(buffer, size, crc);
|
fseek(file, offset, SEEK_SET);
|
||||||
|
readed = 0;
|
||||||
if ( file )
|
while ( readed < size ) {
|
||||||
rewind(file);
|
need = size - readed;
|
||||||
|
if ( need > sizeof(buffer) )
|
||||||
|
need = sizeof(buffer);
|
||||||
|
ret = fread(buffer, 1, need, file);
|
||||||
|
if ( ret < 0 )
|
||||||
|
break;
|
||||||
|
crc = crc32(buffer, ret, crc);
|
||||||
|
readed += ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
memcpy(msg.crc1, &crc, 4);
|
memcpy(msg.crc1, &crc, 4);
|
||||||
|
|
||||||
@@ -115,74 +121,11 @@ struct xloader_msg xloader_msg_create(uint32_t type, uint32_t size, FILE * file)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct usb_device * search_device(struct usb_device *dev, int level) {
|
|
||||||
|
|
||||||
int i;
|
|
||||||
struct usb_device * ret = NULL;
|
|
||||||
|
|
||||||
if ( ! dev )
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if ( ( dev->descriptor.idVendor == USB_VENDOR ) && ( dev->descriptor.idProduct == USB_PRODUCT ) )
|
|
||||||
return dev;
|
|
||||||
|
|
||||||
for ( i = 0; i < dev->num_children; i++ ) {
|
|
||||||
ret = search_device(dev->children[i], level + 1);
|
|
||||||
if ( ret )
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static usb_dev_handle * find_device(void) {
|
|
||||||
|
|
||||||
struct usb_bus * bus;
|
|
||||||
struct usb_device * ret = NULL;
|
|
||||||
|
|
||||||
usb_init();
|
|
||||||
usb_find_busses();
|
|
||||||
|
|
||||||
printf("Waiting for USB device\n");
|
|
||||||
|
|
||||||
while ( ! ret ) {
|
|
||||||
|
|
||||||
usb_find_devices();
|
|
||||||
|
|
||||||
for ( bus = usb_get_busses(); bus; bus = bus->next ) {
|
|
||||||
|
|
||||||
if ( bus->root_dev )
|
|
||||||
ret = search_device(bus->root_dev, 0);
|
|
||||||
else {
|
|
||||||
struct usb_device *dev;
|
|
||||||
for ( dev = bus->devices; dev; dev = dev->next ) {
|
|
||||||
ret = search_device(dev, 0);
|
|
||||||
if ( ret )
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ret )
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ret )
|
|
||||||
return usb_open(ret);
|
|
||||||
else
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int read_asic(usb_dev_handle * udev) {
|
static int read_asic(usb_dev_handle * udev) {
|
||||||
|
|
||||||
uint8_t asic_buffer[127];
|
uint8_t asic_buffer[127];
|
||||||
int asic_size = 69;
|
int asic_size = 69;
|
||||||
int i;
|
int i, ret;
|
||||||
int ret;
|
|
||||||
|
|
||||||
printf("Waiting for ASIC ID...\n");
|
printf("Waiting for ASIC ID...\n");
|
||||||
ret = usb_bulk_read(udev, READ_DEV, (char *)asic_buffer, sizeof(asic_buffer), READ_TIMEOUT);
|
ret = usb_bulk_read(udev, READ_DEV, (char *)asic_buffer, sizeof(asic_buffer), READ_TIMEOUT);
|
||||||
@@ -200,16 +143,15 @@ static int read_asic(usb_dev_handle * udev) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int send_2nd(usb_dev_handle * udev, FILE * file) {
|
static int send_2nd(usb_dev_handle * udev, FILE * file, size_t offset, size_t size) {
|
||||||
|
|
||||||
uint8_t buffer[1024];
|
uint8_t buffer[1024];
|
||||||
uint32_t size32;
|
uint32_t size32;
|
||||||
size_t size;
|
size_t need, readed;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
fseek(file, 0, SEEK_END);
|
size32 = size;
|
||||||
size32 = size = ftell(file);
|
fseek(file, offset, SEEK_SET);
|
||||||
rewind(file);
|
|
||||||
|
|
||||||
if ( size == 0 ) {
|
if ( size == 0 ) {
|
||||||
fprintf(stderr, "2nd X-Loader image has zero size\n");
|
fprintf(stderr, "2nd X-Loader image has zero size\n");
|
||||||
@@ -238,11 +180,19 @@ static int send_2nd(usb_dev_handle * udev, FILE * file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("Sending 2nd X-Loader image...\n");
|
printf("Sending 2nd X-Loader image...\n");
|
||||||
while ( ( ret = fread(buffer, 1, sizeof(buffer), file) ) > 0 ) {
|
readed = 0;
|
||||||
|
while ( readed < size ) {
|
||||||
|
need = size - readed;
|
||||||
|
if ( need > sizeof(buffer) )
|
||||||
|
need = sizeof(buffer);
|
||||||
|
ret = fread(buffer, 1, need, file);
|
||||||
|
if ( ret < 0 )
|
||||||
|
break;
|
||||||
if ( usb_bulk_write(udev, WRITE_DEV, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) {
|
if ( usb_bulk_write(udev, WRITE_DEV, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) {
|
||||||
fprintf(stderr, "Error while sending 2nd X-Loader image\n");
|
fprintf(stderr, "Error while sending 2nd X-Loader image\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
readed += ret;
|
||||||
}
|
}
|
||||||
usleep(50000);
|
usleep(50000);
|
||||||
|
|
||||||
@@ -250,17 +200,16 @@ static int send_2nd(usb_dev_handle * udev, FILE * file) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int send_secondary(usb_dev_handle * udev, FILE * file) {
|
static int send_secondary(usb_dev_handle * udev, FILE * file, size_t offset, size_t size) {
|
||||||
|
|
||||||
struct xloader_msg init_msg;
|
struct xloader_msg init_msg;
|
||||||
uint8_t buffer[1024];
|
uint8_t buffer[1024];
|
||||||
uint32_t size32;
|
uint32_t size32;
|
||||||
size_t size;
|
size_t need, readed;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
fseek(file, 0, SEEK_END);
|
size32 = size;
|
||||||
size32 = size = ftell(file);
|
fseek(file, offset, SEEK_SET);
|
||||||
rewind(file);
|
|
||||||
|
|
||||||
if ( size == 0 ) {
|
if ( size == 0 ) {
|
||||||
fprintf(stderr, "2nd Secondary image has zero size\n");
|
fprintf(stderr, "2nd Secondary image has zero size\n");
|
||||||
@@ -272,7 +221,7 @@ static int send_secondary(usb_dev_handle * udev, FILE * file) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
init_msg = xloader_msg_create(XLOADER_MSG_TYPE_SEND, size32, file);
|
init_msg = xloader_msg_create(XLOADER_MSG_TYPE_SEND, file, offset, size32);
|
||||||
|
|
||||||
printf("Sending X-Loader init message...\n");
|
printf("Sending X-Loader init message...\n");
|
||||||
ret = usb_bulk_write(udev, WRITE_DEV, (char *)&init_msg, sizeof(init_msg), WRITE_TIMEOUT);
|
ret = usb_bulk_write(udev, WRITE_DEV, (char *)&init_msg, sizeof(init_msg), WRITE_TIMEOUT);
|
||||||
@@ -290,11 +239,19 @@ static int send_secondary(usb_dev_handle * udev, FILE * file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("Sending Secondary image...\n");
|
printf("Sending Secondary image...\n");
|
||||||
while ( ( ret = fread(buffer, 1, sizeof(buffer), file) ) > 0 ) {
|
readed = 0;
|
||||||
|
while ( readed < size ) {
|
||||||
|
need = size - readed;
|
||||||
|
if ( need > sizeof(buffer) )
|
||||||
|
need = sizeof(buffer);
|
||||||
|
ret = fread(buffer, 1, need, file);
|
||||||
|
if ( ret < 0 )
|
||||||
|
break;
|
||||||
if ( usb_bulk_write(udev, WRITE_DEV, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) {
|
if ( usb_bulk_write(udev, WRITE_DEV, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) {
|
||||||
fprintf(stderr, "Error while sending Secondary image\n");
|
fprintf(stderr, "Error while sending Secondary image\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
readed += ret;
|
||||||
}
|
}
|
||||||
usleep(5000);
|
usleep(5000);
|
||||||
|
|
||||||
@@ -317,7 +274,7 @@ static int ping_timeout(usb_dev_handle * udev) {
|
|||||||
|
|
||||||
while ( try_ping > 0 ) {
|
while ( try_ping > 0 ) {
|
||||||
|
|
||||||
struct xloader_msg ping_msg = xloader_msg_create(XLOADER_MSG_TYPE_PING, 0, NULL);
|
struct xloader_msg ping_msg = xloader_msg_create(XLOADER_MSG_TYPE_PING, NULL, 0, 0);
|
||||||
int try_read = 4;
|
int try_read = 4;
|
||||||
|
|
||||||
printf("Sending X-Loader ping message\n");
|
printf("Sending X-Loader ping message\n");
|
||||||
@@ -358,83 +315,74 @@ static int ping_timeout(usb_dev_handle * udev) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void usage(char * arg) {
|
int cold_flash(usb_dev_handle * udev, const char * x2nd, const char * secondary) {
|
||||||
printf("Usage: \n");
|
|
||||||
printf("%s 2nd.bin secondary.bin\n", arg);
|
|
||||||
printf(" * Send 2nd X-Loader (2nd.bin) via USB OMAP download command, boot it and wait\n");
|
|
||||||
printf(" * Send Secondary NOLO (secondary.bin) via USB X-Loader commands and flash it into NAND\n");
|
|
||||||
printf("%s -s secondary.bin\n", arg);
|
|
||||||
printf(" * Expect that 2nd X-Loader is already booted, send only secondary.bin and flash it\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
|
||||||
|
|
||||||
|
size_t size1, size2;
|
||||||
FILE * file1 = NULL;
|
FILE * file1 = NULL;
|
||||||
FILE * file2 = NULL;
|
FILE * file2 = NULL;
|
||||||
int secondary = 0;
|
int ret = 1;
|
||||||
usb_dev_handle * udev = NULL;
|
|
||||||
|
|
||||||
if ( argc != 3 ) {
|
file1 = fopen(x2nd, "r");
|
||||||
usage(argv[0]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( strncmp(argv[1], "-s", 2) == 0 )
|
|
||||||
secondary = 1;
|
|
||||||
|
|
||||||
if ( ! secondary ) {
|
|
||||||
|
|
||||||
file1 = fopen(argv[1], "r");
|
|
||||||
if ( ! file1 ) {
|
if ( ! file1 ) {
|
||||||
fprintf(stderr, "Cannot open 2nd X-Loader file '%s': %s\n", argv[1], strerror(errno));
|
fprintf(stderr, "Cannot open 2nd X-Loader file '%s': %s\n", x2nd, strerror(errno));
|
||||||
return 1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
file2 = fopen(secondary, "r");
|
||||||
|
|
||||||
file2 = fopen(argv[2], "r");
|
|
||||||
if ( ! file2 ) {
|
if ( ! file2 ) {
|
||||||
fprintf(stderr, "Cannot open Secondary file '%s': %s\n", argv[2], strerror(errno));
|
fprintf(stderr, "Cannot open Secondary file '%s': %s\n", secondary, strerror(errno));
|
||||||
return 1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
udev = find_device();
|
fseek(file1, 0, SEEK_END);
|
||||||
if ( ! udev ) {
|
size1 = ftell(file1);
|
||||||
fprintf(stderr, "Cannot find USB device '0x%x:0x%x'\n", USB_VENDOR, USB_PRODUCT);
|
|
||||||
return 1;
|
fseek(file2, 0, SEEK_END);
|
||||||
|
size2 = ftell(file2);
|
||||||
|
|
||||||
|
if ( usb_set_configuration(udev, 1) < 0 ) {
|
||||||
|
fprintf(stderr, "usb_set_configuration failed: %s\n", strerror(errno));
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Found USB device\n");
|
if ( usb_claim_interface(udev, 1) < 0 ) {
|
||||||
|
fprintf(stderr, "usb_claim_interface failed: %s\n", strerror(errno));
|
||||||
usb_set_configuration(udev, 1);
|
goto cleanup;
|
||||||
usb_claim_interface(udev, 1);
|
}
|
||||||
|
|
||||||
if ( ! secondary ) {
|
|
||||||
|
|
||||||
if ( ! read_asic(udev) ) {
|
if ( ! read_asic(udev) ) {
|
||||||
fprintf(stderr, "Reading ASIC ID failed\n");
|
fprintf(stderr, "Reading ASIC ID failed\n");
|
||||||
return 1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! send_2nd(udev, file1) ) {
|
if ( ! send_2nd(udev, file1, size1, 0) ) {
|
||||||
fprintf(stderr, "Sending 2nd X-Loader image failed\n");
|
fprintf(stderr, "Sending 2nd X-Loader image failed\n");
|
||||||
return 1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! ping_timeout(udev) ) {
|
if ( ! ping_timeout(udev) ) {
|
||||||
fprintf(stderr, "Sending X-Loader ping failed\n");
|
fprintf(stderr, "Sending X-Loader ping failed\n");
|
||||||
return 1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
if ( ! send_secondary(udev, file2, size2, 0) ) {
|
||||||
|
|
||||||
if ( ! send_secondary(udev, file2) ) {
|
|
||||||
fprintf(stderr, "Sending Secondary image failed\n");
|
fprintf(stderr, "Sending Secondary image failed\n");
|
||||||
return 1;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Done\n");
|
printf("Done\n");
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
return 0;
|
cleanup:
|
||||||
|
if (file1)
|
||||||
|
fclose(file1);
|
||||||
|
if (file2)
|
||||||
|
fclose(file2);
|
||||||
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*int fiasco_cold_flash(usb_dev_handle * udev, const char * device, const char * hw_rev, const char * fiasco) {
|
||||||
|
|
||||||
|
|
||||||
|
}*/
|
||||||
Reference in new issue
Block a user