From 351bfbd055aae28cd545bf3513a80f266ce841a2 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:24:02 +0100 Subject: mkii: Implement device initialization --- src/mkii.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 119 insertions(+), 9 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index 81e860d..e51943b 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -17,25 +17,125 @@ */ +#include +#include +#include + +#include + #include "mkii.h" #include "global.h" #include "image.h" #include "device.h" #include "usb-device.h" +#define MKII_PING 0x00000000 +#define MKII_GET_PROTOCOL 0x01010000 +#define MKII_TELL_PROTOCOL 0x02020000 +#define MKII_GET_DEVICE 0x01030000 +#define MKII_GET_HWREV 0x01040000 +#define MKII_GET_IMAGES 0x01050000 +#define MKII_REBOOT 0x0C060000 +#define MKII_INIT_SEND 0x03010000 +#define MKII_SEND_IMAGE 0x04020000 + +struct mkii_message { + uint32_t header; + uint16_t size; + uint32_t type; + char data[]; +} __attribute__((__packed__)); + +static int mkii_send_receive(usb_dev_handle * udev, uint32_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { + + int ret; + + in_msg->header = 0x8810001B; + in_msg->size = htons(data_size + 4); + in_msg->type = type; + + ret = usb_bulk_write(udev, 1, (const char *)in_msg, data_size + sizeof(*in_msg), 5000); + if ( ret < 0 ) + return ret; + if ( (size_t)ret != data_size + sizeof(*in_msg) ) + return -1; + + ret = usb_bulk_read(udev, 129, (char *)out_msg, out_size, 5000); + if ( ret < 0 ) + return ret; + + if ( out_msg->header != 0x8800101B ) + return -1; + + if ( out_msg->type != (type | 0x20000000) ) + return -1; + + if ( (size_t)ret < sizeof(*out_msg) ) + return -1; + + if ( ntohs(out_msg->size) != ret - sizeof(*out_msg) + 4 ) + return -1; + + return ret - sizeof(*out_msg); + +} + int mkii_init(struct usb_device_info * dev) { - ERROR("Mk II protocol is not implemented yet"); - (void)dev; - return -1; + char buf[2048]; + struct mkii_message * msg; + enum device device; + int ret; + + printf("Initializing Mk II protocol...\n"); + + msg = (struct mkii_message *)buf; + + ret = mkii_send_receive(dev->udev, MKII_PING, msg, 0, msg, sizeof(buf)); + if ( ret != 0 ) + return -1; + + memcpy(msg->data, "/update/protocol_version", sizeof("/update/protocol_version")-1); + ret = mkii_send_receive(dev->udev, MKII_GET_PROTOCOL, msg, sizeof("/update/protocol_version")-1, msg, sizeof(buf)); + if ( ret != 2 || msg->data[0] != 0 || msg->data[1] != 0x32 ) + return -1; + + memcpy(msg->data, "/update/host_protocol_version\x00\x32", sizeof("/update/host_protocol_version\x00\x32")-1); + ret = mkii_send_receive(dev->udev, MKII_TELL_PROTOCOL, msg, sizeof("/update/host_protocol_version\x00\x32")-1, msg, sizeof(buf)); + if ( ret != 1 || msg->data[0] != 0 ) + return -1; + + device = mkii_get_device(dev); + + if ( ! dev->device ) + dev->device = device; + + if ( dev->device && device && dev->device != device ) { + ERROR("Device mishmash, expected %s, got %s", device_to_string(dev->device), device_to_string(device)); + return -1; + } + + dev->hwrev = mkii_get_hwrev(dev); + + return 0; } enum device mkii_get_device(struct usb_device_info * dev) { - ERROR("Not implemented yet"); - (void)dev; - return DEVICE_UNKNOWN; + char buf[2048]; + struct mkii_message * msg; + int ret; + + msg = (struct mkii_message *)buf; + + memcpy(msg->data, "/device/product_code", sizeof("/device/product_code")-1); + ret = mkii_send_receive(dev->udev, MKII_GET_DEVICE, msg, sizeof("/device/product_code")-1, msg, sizeof(buf)); + if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) + return DEVICE_UNKNOWN; + + msg->data[ret] = 0; + return device_from_string((char *)msg->data+1); } @@ -128,9 +228,19 @@ int mkii_set_rd_flags(struct usb_device_info * dev, const char * flags) { int16_t mkii_get_hwrev(struct usb_device_info * dev) { - ERROR("Not implemented yet"); - (void)dev; - return -1; + char buf[2048]; + struct mkii_message * msg; + int ret; + + msg = (struct mkii_message *)buf; + + memcpy(msg->data, "/device/hw_build", sizeof("/device/hw_build")-1); + ret = mkii_send_receive(dev->udev, MKII_GET_DEVICE, msg, sizeof("/device/hw_build")-1, msg, sizeof(buf)); + if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) + return -1; + + msg->data[ret] = 0; + return atoi(msg->data+1); } -- cgit v1.2.3 From 3dc90e643c6d19d0ef577db9ffaaf8fe995cd5b4 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:24:55 +0100 Subject: mkii: Implement device reboot --- src/mkii.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index e51943b..d345891 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -150,9 +150,20 @@ int mkii_flash_image(struct usb_device_info * dev, struct image * image) { int mkii_reboot_device(struct usb_device_info * dev) { - ERROR("Not implemented yet"); - (void)dev; - return -1; + char buf[2048]; + struct mkii_message * msg; + int ret; + + msg = (struct mkii_message *)buf; + + printf("Rebooting device...\n"); + + memcpy(msg->data, "reboot", sizeof("reboot")-1); + ret = mkii_send_receive(dev->udev, MKII_GET_DEVICE, msg, sizeof("reboot")-1, msg, sizeof(buf)); + if ( ret != 1 || msg->data[0] != 0 ) + return -1; + + return 0; } -- cgit v1.2.3 From ea9f9d5a69a003cb793b9175963325501c6f0132 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:25:25 +0100 Subject: mkii: Unfinished code for image flashing --- src/mkii.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 2 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index d345891..0214c7a 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -141,11 +141,145 @@ enum device mkii_get_device(struct usb_device_info * dev) { int mkii_flash_image(struct usb_device_info * dev, struct image * image) { + char buf1[512]; + char buf[2048]; + struct mkii_message * msg1; + struct mkii_message * msg; + char * ptr; + const char * type; + uint8_t len; + uint16_t hash; + uint32_t size; + int ret; + ERROR("Not implemented yet"); - (void)dev; - (void)image; return -1; + msg = (struct mkii_message *)buf; + msg1 = (struct mkii_message *)buf1; + ptr = msg->data; + + /* Signature */ + memcpy(ptr, "\x2E\x19\x01\x01", 4); + ptr += 4; + + /* Space */ + memcpy(ptr, "\x00", 1); + ptr += 1; + + /* Hash */ + hash = htons(image->hash); + memcpy(ptr, &hash, 2); + ptr += 2; + + /* Type */ + type = image_type_to_string(image->type); + if ( ! type ) + ERROR_RETURN("Unknown image type", -1); + memset(ptr, 0, 12); + strncpy(ptr, type, 12); + ptr += 12; + + /* Size */ + size = htonl(image->size); + memcpy(ptr, &size, 4); + ptr += 4; + + /* Space */ + memcpy(ptr, "\x00\x00\x00\x00", 4); + ptr += 4; + + /* Device & hwrev */ + if ( image->devices ) { + + int i; + uint8_t len; + char buf[9]; + char ** bufs = NULL; + struct device_list * device = image->devices; + + while ( device ) { + if ( device->device == dev->device && hwrev_is_valid(device->hwrevs, dev->hwrev) ) + break; + device = device->next; + } + + if ( device ) + bufs = device_list_alloc_to_bufs(device); + + if ( bufs ) { + + memset(buf, 0, sizeof(buf)); + snprintf(buf, 8+1, "%d", dev->hwrev); + + for ( i = 0; bufs[i]; ++i ) { + len = ((uint8_t*)bufs[i])[0]; + if ( memmem(bufs[i]+1, len, buf, strlen(buf)) ) + break; + } + + if ( bufs[i] ) { + /* Device & hwrev string header */ + memcpy(ptr, "\x32", 1); + ptr += 1; + /* Device & hwrev string size */ + memcpy(ptr, &len, 1); + ptr += 1; + /* Device & hwrev string */ + memcpy(ptr, bufs[i]+1, len); + ptr += len; + } + + free(bufs); + + } + + } + + /* Version */ + if ( image->version ) { + len = strnlen(image->version, 255) + 1; + /* Version string header */ + memcpy(ptr, "\x31", 1); + ptr += 1; + /* Version string size */ + memcpy(ptr, &len, 1); + ptr += 1; + /* Version string */ + memcpy(ptr, image->version, len); + ptr += len; + } + + /* append layout subsection */ + if ( image->layout ) { + len = strlen(image->layout); + /* Layout header */ + memcpy(ptr, "\x33", 1); + ptr += 1; + /* Layout size */ + memcpy(ptr, &len, 1); + ptr += 1; + /* Layout string */ + memcpy(ptr, image->layout, len); + ptr += len; + } + + /* end */ + memcpy(ptr, "\x00", 1); + ptr += 1; + + ret = mkii_send_receive(dev->udev, MKII_INIT_SEND, msg1, 0, msg1, sizeof(buf1)); + if ( ret != 1 || msg1->data[0] != 0 ) + return -1; + + ret = mkii_send_receive(dev->udev, MKII_SEND_IMAGE, msg, ptr - msg->data, msg, sizeof(buf)); + if ( ret != 9 ) + return -1; + + /* TODO: send image itself */ + + return 0; + } int mkii_reboot_device(struct usb_device_info * dev) { -- cgit v1.2.3 From 78fc280577e15e8d40745caf0d5bfc8f30c505e8 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:26:19 +0100 Subject: usb-device: Enable Mk II protocol --- src/usb-device.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/usb-device.c b/src/usb-device.c index bf1241e..a391c02 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -43,7 +43,7 @@ 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 } }, */ -/* { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, 0 } }, */ + { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, 0 } }, /* { 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 } }, }; @@ -51,7 +51,7 @@ static struct usb_flash_device usb_devices[] = { static const char * usb_flash_protocols[] = { [FLASH_NOLO] = "NOLO", [FLASH_COLD] = "Cold flashing", -/* [FLASH_MKII] = "Mk II protocol", */ + [FLASH_MKII] = "Mk II protocol", /* [FLASH_DISK] = "RAW disk", */ }; -- cgit v1.2.3 From feb2d3ac6a35070c760374fcc569a7736cf71076 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:26:50 +0100 Subject: usb-device: Rename usb_switch_to_mkii to usb_switch_to_update --- src/usb-device.c | 4 +++- src/usb-device.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/usb-device.c b/src/usb-device.c index a391c02..536f986 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -369,7 +369,7 @@ void usb_switch_to_cold(struct usb_device_info * dev) { } -void usb_switch_to_mkii(struct usb_device_info * dev) { +void usb_switch_to_update(struct usb_device_info * dev) { printf("\nSwitching to Update mode...\n"); @@ -377,6 +377,8 @@ void usb_switch_to_mkii(struct usb_device_info * dev) { leave_cold_flash(dev); else if ( dev->flash_device->protocol == FLASH_NOLO ) nolo_boot_device(dev, "update"); + else if ( dev->flash_device->protocol == FLASH_MKII ) + mkii_reboot_device(dev); else if ( dev->flash_device->protocol == FLASH_DISK ) printf_and_wait("Unplug USB cable, turn device off, press ENTER and plug USB cable again"); diff --git a/src/usb-device.h b/src/usb-device.h index 20a073c..983af51 100644 --- a/src/usb-device.h +++ b/src/usb-device.h @@ -56,7 +56,7 @@ void usb_close_device(struct usb_device_info * dev); void usb_switch_to_nolo(struct usb_device_info * dev); void usb_switch_to_cold(struct usb_device_info * dev); -void usb_switch_to_mkii(struct usb_device_info * dev); +void usb_switch_to_update(struct usb_device_info * dev); void usb_switch_to_disk(struct usb_device_info * dev); #endif -- cgit v1.2.3 From 1a59034571348ceffe3367f489ee04df53d8dca3 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:27:21 +0100 Subject: operations: Fix code to work also with Mk II protocol --- src/operations.c | 152 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 85 insertions(+), 67 deletions(-) diff --git a/src/operations.c b/src/operations.c index 2c28228..7ac5626 100644 --- a/src/operations.c +++ b/src/operations.c @@ -178,10 +178,9 @@ int dev_flash_image(struct device_info * dev, struct image * image) { return nolo_flash_image(dev->usb, image); else if ( protocol == FLASH_MKII ) return mkii_flash_image(dev->usb, image); - else { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -230,10 +229,9 @@ int dev_boot_device(struct device_info * dev, const char * cmdline) { if ( protocol == FLASH_NOLO ) return nolo_boot_device(dev->usb, cmdline); - else { - usb_switch_to_nolo(dev->usb); - return 0; - } + + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -292,13 +290,13 @@ int dev_set_root_device(struct device_info * dev, int device) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_root_device(dev->usb, device); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -313,7 +311,9 @@ int dev_get_usb_host_mode(struct device_info * dev) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_usb_host_mode(dev->usb); } @@ -329,13 +329,13 @@ int dev_set_usb_host_mode(struct device_info * dev, int enable) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_usb_host_mode(dev->usb, enable); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -350,7 +350,9 @@ int dev_get_rd_mode(struct device_info * dev) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_rd_mode(dev->usb); } @@ -366,13 +368,13 @@ int dev_set_rd_mode(struct device_info * dev, int enable) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_rd_mode(dev->usb, enable); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -387,7 +389,9 @@ int dev_get_rd_flags(struct device_info * dev, char * flags, size_t size) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_rd_flags(dev->usb, flags, size); } @@ -403,13 +407,13 @@ int dev_set_rd_flags(struct device_info * dev, const char * flags) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_rd_flags(dev->usb, flags); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -424,8 +428,12 @@ int16_t dev_get_hwrev(struct device_info * dev) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_hwrev(dev->usb); + else if ( protocol == FLASH_MKII ) + return mkii_get_hwrev(dev->usb); } @@ -440,13 +448,13 @@ int dev_set_hwrev(struct device_info * dev, int16_t hwrev) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_hwrev(dev->usb, hwrev); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -461,7 +469,9 @@ int dev_get_kernel_ver(struct device_info * dev, char * ver, size_t size) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_kernel_ver(dev->usb, ver, size); } @@ -477,13 +487,13 @@ int dev_set_kernel_ver(struct device_info * dev, const char * ver) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_kernel_ver(dev->usb, ver); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -498,7 +508,9 @@ int dev_get_initfs_ver(struct device_info * dev, char * ver, size_t size) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_initfs_ver(dev->usb, ver, size); } @@ -514,13 +526,13 @@ int dev_set_initfs_ver(struct device_info * dev, const char * ver) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_initfs_ver(dev->usb, ver); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -535,7 +547,9 @@ int dev_get_nolo_ver(struct device_info * dev, char * ver, size_t size) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_nolo_ver(dev->usb, ver, size); } @@ -551,13 +565,13 @@ int dev_set_nolo_ver(struct device_info * dev, const char * ver) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_nolo_ver(dev->usb, ver); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -572,7 +586,9 @@ int dev_get_sw_ver(struct device_info * dev, char * ver, size_t size) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_sw_ver(dev->usb, ver, size); } @@ -588,13 +604,13 @@ int dev_set_sw_ver(struct device_info * dev, const char * ver) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_sw_ver(dev->usb, ver); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } @@ -609,7 +625,9 @@ int dev_get_content_ver(struct device_info * dev, char * ver, size_t size) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_get_content_ver(dev->usb, ver, size); } @@ -625,13 +643,13 @@ int dev_set_content_ver(struct device_info * dev, const char * ver) { if ( dev->method == METHOD_USB ) { - if ( dev->usb->flash_device->protocol == FLASH_NOLO ) + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_NOLO ) return nolo_set_content_ver(dev->usb, ver); - if ( dev->usb->flash_device->protocol == FLASH_COLD ) { - usb_switch_to_nolo(dev->usb); - return -EAGAIN; - } + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } -- cgit v1.2.3 From c23e8df2607d8b1bc882379000e1990bc055b39d Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:31:28 +0100 Subject: usb-device: Enable also RAW disk --- src/usb-device.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/usb-device.c b/src/usb-device.c index 536f986..0ef55b4 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -42,9 +42,9 @@ 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 } }, */ + { 0x0421, 0x01c7, 0, -1, -1, FLASH_DISK, { DEVICE_RX_51, 0 } }, { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, 0 } }, -/* { 0x0421, 0x0431, 0, -1, -1, FLASH_DISK, { DEVICE_SU_18, DEVICE_RX_34, 0 } }, */ + { 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 } }, }; @@ -52,7 +52,7 @@ static const char * usb_flash_protocols[] = { [FLASH_NOLO] = "NOLO", [FLASH_COLD] = "Cold flashing", [FLASH_MKII] = "Mk II protocol", -/* [FLASH_DISK] = "RAW disk", */ + [FLASH_DISK] = "RAW disk", }; const char * usb_flash_protocol_to_string(enum usb_flash_protocol protocol) { -- cgit v1.2.3 From ea980459f1a903052f831510e03c08109f2157c4 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:31:49 +0100 Subject: operations: Enable RAW disk --- src/operations.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/operations.c b/src/operations.c index 7ac5626..2b2de2d 100644 --- a/src/operations.c +++ b/src/operations.c @@ -110,8 +110,8 @@ enum device dev_get_device(struct device_info * dev) { return nolo_get_device(dev->usb); else if ( protocol == FLASH_MKII ) return mkii_get_device(dev->usb); -/* else if ( protocol == FLASH_DISK ) - return disk_get_device(dev->usb);*/ + else if ( protocol == FLASH_DISK ) + return disk_get_device(dev->usb); } -- cgit v1.2.3 From bc4aeefab5c80308ef96ea33e3085c72094b622b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:51:04 +0100 Subject: usb-device: Do not claim USB interface in raw disk mode --- src/usb-device.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/usb-device.c b/src/usb-device.c index 0ef55b4..1f9b403 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -42,9 +42,9 @@ 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 } }, + { 0x0421, 0x01c7, -1, -1, -1, FLASH_DISK, { DEVICE_RX_51, 0 } }, { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, 0 } }, - { 0x0421, 0x0431, 0, -1, -1, FLASH_DISK, { DEVICE_SU_18, DEVICE_RX_34, 0 } }, + { 0x0421, 0x0431, -1, -1, -1, FLASH_DISK, { DEVICE_SU_18, DEVICE_RX_34, 0 } }, { 0x0421, 0x3f00, 2, 1, -1, FLASH_NOLO, { DEVICE_RX_34, 0 } }, }; @@ -90,6 +90,9 @@ static void usb_reattach_kernel_driver(usb_dev_handle * udev, int interface) { .data = NULL, }; + if ( interface < 0 ) + return; + usb_release_interface(udev, interface); ioctl(*((int *)udev), _IOWR('U', 18, command), &command); #endif @@ -159,18 +162,22 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { usb_descriptor_info_print(udev, dev, product, sizeof(product)); + if ( usb_devices[i].interface >= 0 ) { + #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP - PRINTF_LINE("Detaching kernel from USB interface..."); - usb_detach_kernel_driver_np(udev, usb_devices[i].interface); + PRINTF_LINE("Detaching kernel from USB interface..."); + usb_detach_kernel_driver_np(udev, usb_devices[i].interface); #endif - PRINTF_LINE("Claiming USB interface..."); - if ( usb_claim_interface(udev, usb_devices[i].interface) < 0 ) { - PRINTF_ERROR("usb_claim_interface failed"); - fprintf(stderr, "\n"); - usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); - return NULL; + PRINTF_LINE("Claiming USB interface..."); + if ( usb_claim_interface(udev, usb_devices[i].interface) < 0 ) { + PRINTF_ERROR("usb_claim_interface failed"); + fprintf(stderr, "\n"); + usb_reattach_kernel_driver(udev, usb_devices[i].interface); + usb_close(udev); + return NULL; + } + } if ( usb_devices[i].alternate >= 0 ) { -- cgit v1.2.3 From 5611e8a68cbf2fe97e26b55caac0f48f88ae8edb Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:51:48 +0100 Subject: operations: When reboot is not supported use usb_switch_to_nolo() --- src/operations.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/operations.c b/src/operations.c index 2b2de2d..ae06b1d 100644 --- a/src/operations.c +++ b/src/operations.c @@ -255,8 +255,8 @@ int dev_reboot_device(struct device_info * dev) { else if ( protocol == FLASH_MKII ) return mkii_reboot_device(dev->usb); else { - ERROR("Rebooting device in RAW disk mode is not supported"); - return -1; + usb_switch_to_nolo(dev->usb); + return -EAGAIN; } } -- cgit v1.2.3 From eb23177014ccfdd96d604f4c6e1cc4bfa5c24ffe Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 18 Nov 2014 23:52:14 +0100 Subject: disk: Implement init and get_device functions --- src/disk.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/disk.c b/src/disk.c index eb63211..d6390c4 100644 --- a/src/disk.c +++ b/src/disk.c @@ -37,17 +37,14 @@ static char global_buf[1 << 22]; /* 4MB */ int disk_init(struct usb_device_info * dev) { - ERROR("RAW mode is not implemented yet"); (void)dev; - return -1; + return 0; } enum device disk_get_device(struct usb_device_info * dev) { - ERROR("Not implemented yet"); - (void)dev; - return DEVICE_UNKNOWN; + return dev->device; } -- cgit v1.2.3 From e00331fb42561669c198cbe3715b1dafdb53bee9 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 15:01:30 +0100 Subject: disk: Implement new functions for raw disk access --- src/disk.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/disk.h | 3 + 2 files changed, 189 insertions(+) diff --git a/src/disk.c b/src/disk.c index d6390c4..76e2634 100644 --- a/src/disk.c +++ b/src/disk.c @@ -35,6 +35,192 @@ static char global_buf[1 << 22]; /* 4MB */ +int disk_open_dev(int maj, int min, int partition, int readonly) { + + int fd; + struct stat st; + DIR * dir; + struct dirent * dirent; + int found; + size_t len; + char blkdev[1024]; + + dir = opendir("/dev/"); + if ( ! dir ) { + ERROR_INFO("Cannot open '/dev/' directory"); + return -1; + } + + found = 0; + + while ( ( dirent = readdir(dir) ) ) { + + if ( snprintf(blkdev, sizeof(blkdev), "/dev/%s", dirent->d_name) <= 0 ) + continue; + + if ( stat(blkdev, &st) != 0 ) + continue; + + if ( ! S_ISBLK(st.st_mode) ) + continue; + + if ( makedev(maj, min) != st.st_rdev ) + continue; + + found = 1; + break; + + } + + closedir(dir); + + if ( ! found ) { + ERROR("Cannot find block device with id %d:%d", maj, min); + return -1; + } + + printf("Found block device %s with id %d:%d\n", blkdev, maj, min); + + if ( partition == -1 ) { + + /* Check if block device does not have partitions */ + + len = strlen(blkdev); + if ( sizeof(blkdev) <= len+2 ) { + ERROR("Block device name is too long"); + return -1; + } + + memcpy(blkdev+len, "p1", 3); + if ( stat(blkdev, &st) == 0 ) { + ERROR("Block device has partitions"); + return -1; + } + + memcpy(blkdev+len, "1", 2); + if ( stat(blkdev, &st) == 0 ) { + ERROR("Block device has partitions"); + return -1; + } + + blkdev[len] = 0; + + } else if ( partition > 0 ) { + + /* Select partition */ + + len = strlen(blkdev); + if ( sizeof(blkdev) <= len+2 ) { + ERROR("Block device name is too long"); + return -1; + } + + memcpy(blkdev+len, "p1", 3); + if ( stat(blkdev, &st) != 0 || ! S_ISBLK(st.st_mode) ) { + memcpy(blkdev+len, "1", 2); + if ( stat(blkdev, &st) != 0 || ! S_ISBLK(st.st_mode) ) { + ERROR("Block device has partitions"); + return -1; + } + } + + printf("Found block device %s for partition %d\n", blkdev, partition); + + } + + fd = open(blkdev, (readonly ? O_RDONLY : O_RDWR) | O_EXCL); + + if ( fd < 0 ) { + ERROR_INFO("Cannot open block device %s", blkdev); + return -1; + } + + return fd; + +} + +int disk_dump_dev(int fd, const char * file) { + + int fd2; + int ret; + char * path; + uint64_t blksize; + size_t need, readed; + ssize_t size; + struct statvfs buf; + + printf("Dump block device to file %s...\n", file); + + if ( ioctl(fd, BLKGETSIZE64, &blksize) != 0 ) { + ERROR_INFO("Cannot get size of block device"); + return -1; + } + + if ( blksize > ULLONG_MAX ) { + ERROR("Block device is too big"); + return -1; + } + + if ( blksize == 0 ) { + ERROR("Block device has zero size"); + return -1; + } + + path = strdup(file); + if ( ! path ) { + ALLOC_ERROR(); + return -1; + } + + ret = statvfs(dirname(path), &buf); + + free(path); + + if ( ret == 0 && buf.f_bsize * buf.f_bfree < blksize ) { + ERROR("Not enough free space (have: %llu, need: %llu)", (unsigned long long int)(buf.f_bsize) * buf.f_bfree, (unsigned long long int)blksize); + return -1; + } + + fd2 = creat(file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + + if ( fd2 < 0 ) { + ERROR_INFO("Cannot create file %s", file); + return -1; + } + + readed = 0; + printf_progressbar(0, blksize); + + while ( readed < blksize ) { + need = blksize - readed; + if ( need > sizeof(global_buf) ) + need = sizeof(global_buf); + size = read(fd, global_buf, need); + if ( size == 0 ) + break; + if ( write(fd2, global_buf, size) != size ) { + PRINTF_ERROR("Dumping image failed"); + close(fd2); + return -1; + } + readed += size; + printf_progressbar(readed, blksize); + } + + close(fd2); + return 0; + +} + +int disk_flash_dev(int fd, const char * file) { + + ERROR("Not implemented yet"); + (void)fd; + (void)file; + return -1; + +} + int disk_init(struct usb_device_info * dev) { (void)dev; diff --git a/src/disk.h b/src/disk.h index d67b208..bb61642 100644 --- a/src/disk.h +++ b/src/disk.h @@ -29,6 +29,9 @@ enum device disk_get_device(struct usb_device_info * dev); int disk_flash_raw(const char * blkdev, const char * file); int disk_dump_raw(const char * blkdev, const char * file); +int disk_open_dev(int maj, int min, int partition, int readonly); +int disk_dump_dev(int fd, const char * file); +int disk_flash_dev(int fd, const char * file); int disk_flash_image(struct usb_device_info * dev, struct image * image); int disk_dump_image(struct usb_device_info * dev, enum image_type image, const char * file); -- cgit v1.2.3 From e30e08136a7ce083bbaff194be77bbe380d7c9f3 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 15:02:10 +0100 Subject: local: Use new disk functions --- src/local.c | 157 +++++++++++++++++++++++++----------------------------------- 1 file changed, 66 insertions(+), 91 deletions(-) diff --git a/src/local.c b/src/local.c index c7f3ad1..08c6b8b 100644 --- a/src/local.c +++ b/src/local.c @@ -282,143 +282,118 @@ static struct nanddump_device nanddump[] = { #undef NANDDUMP -int local_dump_image(enum image_type image, const char * file) { +static void local_find_internal_mydocs(int * maj, int * min) { - int ret = -1; - int fd = -1; - unsigned char * addr = NULL; - off_t nlen, len; - int align; + int fd; DIR * dir; DIR * dir2; FILE * f; struct dirent * dirent; struct dirent * dirent2; - struct stat st; - int maj, min; char buf[1024]; - char blk[1024]; - printf("Dump %s image to file %s...\n", image_type_to_string(image), file); + /* Find min & maj id for block device MyDocs (mmc device, partition 1) */ - if ( image == IMAGE_MMC ) { + dir = opendir("/sys/class/mmc_host/"); + if ( ! dir ) { + ERROR("Cannot find MyDocs mmc device: Opening '/sys/class/mmc_host/' failed"); + return; + } - maj = -1; - min = -1; + while ( ( dirent = readdir(dir) ) ) { - /* Find block device in /dev/ for MyDocs (mmc device, partition 1) */ + if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name) <= 0 ) + continue; - dir = opendir("/sys/class/mmc_host/"); - if ( ! dir ) { - ERROR("Cannot find MyDocs mmc device: Opening '/sys/class/mmc_host/' failed"); - goto clean; - } + fd = open(buf, O_RDONLY); + if ( fd < 0 ) + continue; - while ( ( dirent = readdir(dir) ) ) { + memset(buf, 0, sizeof(buf)); + if ( read(fd, buf, sizeof(buf)-1) < 0 ) + buf[0] = 0; + close(fd); + fd = -1; - if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name) <= 0 ) - continue; + if ( strncmp(buf, "internal", sizeof("internal")-1) != 0 ) + continue; - fd = open(buf, O_RDONLY); - if ( fd < 0 ) - continue; + if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/", dirent->d_name, dirent->d_name) <= 0 ) + continue; - memset(buf, 0, sizeof(buf)); - if ( read(fd, buf, sizeof(buf)-1) < 0 ) - buf[0] = 0; - close(fd); + dir2 = opendir(buf); + if ( ! dir2 ) + continue; - if ( strncmp(buf, "internal", sizeof("internal")-1) != 0 ) - continue; + while ( ( dirent2 = readdir(dir2) ) ) { - if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/", dirent->d_name, dirent->d_name) <= 0 ) + if ( strncmp(dirent2->d_name, "block:mmcblk", sizeof("block:mmcblk")-1) != 0 ) continue; - dir2 = opendir(buf); - if ( ! dir2 ) + if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/%s/dev", dirent->d_name, dirent->d_name, dirent2->d_name) <= 0 ) continue; - while ( ( dirent2 = readdir(dir2) ) ) { - - if ( strncmp(dirent2->d_name, "block:mmcblk", sizeof("block:mmcblk")-1) != 0 ) - continue; - - if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/%s:0001/%s/dev", dirent->d_name, dirent->d_name, dirent2->d_name) <= 0 ) - continue; - - f = fopen(buf, "r"); - if ( ! f ) - continue; - - if ( fscanf(f, "%d:%d", &maj, &min) != 2 ) { - maj = -1; - min = -1; - fclose(f); - continue; - } + f = fopen(buf, "r"); + if ( ! f ) + continue; + if ( fscanf(f, "%d:%d", maj, min) != 2 ) { + *maj = -1; + *min = -1; fclose(f); - break; - + continue; } - closedir(dir2); - - if ( maj != -1 && min != -1 ) - break; + fclose(f); + break; } - closedir(dir); + closedir(dir2); - if ( maj == -1 || min == -1 ) { - ERROR("Cannot find MyDocs mmc device: Slot 'internal' was not found"); - goto clean; - } - - VERBOSE("Detected internal mmc device: major=%d minor=%d\n", maj, min); + if ( *maj != -1 && *min != -1 ) + break; - blk[0] = 0; + } - dir = opendir("/dev/"); - if ( ! dir ) { - ERROR("Cannot find MyDocs mmc device: Opening '/dev/' failed"); - goto clean; - } + closedir(dir); - while ( ( dirent = readdir(dir) ) ) { +} - if ( snprintf(buf, sizeof(buf), "/dev/%s", dirent->d_name) <= 0 ) - continue; +int local_dump_image(enum image_type image, const char * file) { - if ( stat(buf, &st) != 0 ) - continue; + int ret = -1; + int fd = -1; + unsigned char * addr = NULL; + off_t nlen, len; + int align; + int maj, min; - if ( ! S_ISBLK(st.st_mode) ) - continue; + printf("Dump %s image to file %s...\n", image_type_to_string(image), file); - if ( makedev(maj, min) != st.st_rdev ) - continue; + if ( image == IMAGE_MMC ) { - strcpy(blk, buf); - break; + maj = -1; + min = -1; + local_find_internal_mydocs(&maj, &min); + if ( maj == -1 || min == -1 ) { + ERROR("Cannot find MyDocs mmc device: Slot 'internal' was not found"); + goto clean; } - closedir(dir); + VERBOSE("Detected internal MyDocs mmc device: major=%d minor=%d\n", maj, min); - if ( ! blk[0] ) { - ERROR("Cannot find MyDocs mmc device: Block device in /dev/ was not found"); + fd = disk_open_dev(maj, min, 1, 1); + if ( fd < 0 ) { + ERROR("Cannot open MyDocs mmc device in /dev/"); goto clean; } - VERBOSE("Detected internal mmc device: '%s'\n", blk); + ret = disk_dump_dev(fd, file); - strncat(blk, "p1", sizeof(blk)-strlen(blk)-1); - - printf("Using MyDocs mmc device: '%s'\n", blk); - - ret = disk_dump_raw(blk, file); + close(fd); + fd = -1; } else { -- cgit v1.2.3 From e66015b8806321835f15434bf9025ca2b1b03a53 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 15:03:18 +0100 Subject: disk: Remove old disk functions (they are not used anymore) --- src/disk.c | 108 ------------------------------------------------------------- src/disk.h | 2 -- 2 files changed, 110 deletions(-) diff --git a/src/disk.c b/src/disk.c index 76e2634..de3d0a0 100644 --- a/src/disk.c +++ b/src/disk.c @@ -234,15 +234,6 @@ enum device disk_get_device(struct usb_device_info * dev) { } -int disk_flash_raw(const char * blkdev, const char * file) { - - ERROR("Not implemented yet"); - (void)blkdev; - (void)file; - return -1; - -} - int disk_flash_image(struct usb_device_info * dev, struct image * image) { ERROR("Not implemented yet"); @@ -252,105 +243,6 @@ int disk_flash_image(struct usb_device_info * dev, struct image * image) { } -int disk_dump_raw(const char * blkdev, const char * file) { - - int fd1, fd2; - int ret; - char * path; - uint64_t blksize; - size_t need, readed; - ssize_t size; - struct stat st; - struct statvfs buf; - - printf("Dump block device %s to file %s...\n", blkdev, file); - - if ( stat(blkdev, &st) != 0 ) { - ERROR_INFO("Cannot stat block device %s", blkdev); - return -1; - } - - if ( ! S_ISBLK(st.st_mode) ) { - ERROR("Invalid block device %s", blkdev); - return -1; - } - - fd1 = open(blkdev, O_RDONLY); - - if ( fd1 < 0 ) { - ERROR_INFO("Cannot open block device %s", blkdev); - return -1; - } - - if ( ioctl(fd1, BLKGETSIZE64, &blksize) != 0 ) { - ERROR_INFO("Cannot get size of block device %s", blkdev); - close(fd1); - return -1; - } - - if ( blksize > ULLONG_MAX ) { - ERROR("Block device %s is too big", blkdev); - close(fd1); - return -1; - } - - if ( blksize == 0 ) { - ERROR("Block device %s has zero size", blkdev); - close(fd1); - return -1; - } - - path = strdup(file); - if ( ! path ) { - ALLOC_ERROR(); - close(fd1); - return -1; - } - - ret = statvfs(dirname(path), &buf); - - free(path); - - if ( ret == 0 && buf.f_bsize * buf.f_bfree < blksize ) { - ERROR("Not enough free space (have: %llu, need: %llu)", (unsigned long long int)(buf.f_bsize) * buf.f_bfree, (unsigned long long int)blksize); - close(fd1); - return -1; - } - - fd2 = creat(file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - - if ( fd2 < 0 ) { - ERROR_INFO("Cannot create file %s", file); - close(fd1); - return -1; - } - - readed = 0; - printf_progressbar(0, blksize); - - while ( readed < blksize ) { - need = blksize - readed; - if ( need > sizeof(global_buf) ) - need = sizeof(global_buf); - size = read(fd1, global_buf, need); - if ( size == 0 ) - break; - if ( write(fd2, global_buf, size) != size ) { - PRINTF_ERROR("Dumping image failed"); - close(fd1); - close(fd2); - return -1; - } - readed += size; - printf_progressbar(readed, blksize); - } - - close(fd1); - close(fd2); - return 0; - -} - int disk_dump_image(struct usb_device_info * dev, enum image_type image, const char * file) { ERROR("Not implemented yet"); diff --git a/src/disk.h b/src/disk.h index bb61642..d5f367d 100644 --- a/src/disk.h +++ b/src/disk.h @@ -27,8 +27,6 @@ int disk_init(struct usb_device_info * dev); enum device disk_get_device(struct usb_device_info * dev); -int disk_flash_raw(const char * blkdev, const char * file); -int disk_dump_raw(const char * blkdev, const char * file); int disk_open_dev(int maj, int min, int partition, int readonly); int disk_dump_dev(int fd, const char * file); int disk_flash_dev(int fd, const char * file); -- cgit v1.2.3 From 5d20747c30be7e5d246db702a0bdb45d69e62bfe Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 16:31:13 +0100 Subject: local: Ignore '.' and '..' when listing directory --- src/local.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/local.c b/src/local.c index 08c6b8b..f2cdba4 100644 --- a/src/local.c +++ b/src/local.c @@ -302,6 +302,9 @@ static void local_find_internal_mydocs(int * maj, int * min) { while ( ( dirent = readdir(dir) ) ) { + if ( strncmp(dirent->d_name, ".", sizeof(".")) == 0 || strncmp(dirent->d_name, "..", sizeof("..")) == 0 ) + continue; + if ( snprintf(buf, sizeof(buf), "/sys/class/mmc_host/%s/slot_name", dirent->d_name) <= 0 ) continue; -- cgit v1.2.3 From 75c87a1583834b725c89ec963fcd3ee3c4ea4284 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 16:31:29 +0100 Subject: disk: Ignore '.' and '..' when listing directory --- src/disk.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/disk.c b/src/disk.c index de3d0a0..1a63ae1 100644 --- a/src/disk.c +++ b/src/disk.c @@ -55,6 +55,9 @@ int disk_open_dev(int maj, int min, int partition, int readonly) { while ( ( dirent = readdir(dir) ) ) { + if ( strncmp(dirent->d_name, ".", sizeof(".")) == 0 || strncmp(dirent->d_name, "..", sizeof("..")) == 0 ) + continue; + if ( snprintf(blkdev, sizeof(blkdev), "/dev/%s", dirent->d_name) <= 0 ) continue; -- cgit v1.2.3 From bebb608bc842060a58c1ad71ac0f22b6cd597364 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 16:32:30 +0100 Subject: usb-device: Add data info to struct usb_device --- src/usb-device.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/usb-device.h b/src/usb-device.h index 983af51..850c550 100644 --- a/src/usb-device.h +++ b/src/usb-device.h @@ -48,6 +48,7 @@ struct usb_device_info { int16_t hwrev; const struct usb_flash_device * flash_device; usb_dev_handle * udev; + int data; }; const char * usb_flash_protocol_to_string(enum usb_flash_protocol protocol); -- cgit v1.2.3 From 5fd07554487c4383103372f4e12bf8f74d5f8171 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 16:33:28 +0100 Subject: disk: Find emmc device in disk_init() --- src/disk.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/src/disk.c b/src/disk.c index 1a63ae1..30d2d29 100644 --- a/src/disk.c +++ b/src/disk.c @@ -226,7 +226,94 @@ int disk_flash_dev(int fd, const char * file) { int disk_init(struct usb_device_info * dev) { - (void)dev; + int fd; + int maj; + int min; + + maj = -1; + min = -1; + + FILE * f; + DIR * dir; + struct dirent * dirent; + char buf[1024]; + unsigned int devnum; + unsigned int busnum; + + struct usb_device * device; + + device = usb_device(dev->udev); + if ( ! device || ! device->bus ) { + ERROR_INFO("Cannot read usb devnum and busnum"); + return -1; + } + + dir = opendir("/sys/dev/block/"); + if ( ! dir ) { + ERROR_INFO("Cannot open '/sys/dev/block/' directory"); + return -1; + } + + while ( ( dirent = readdir(dir) ) ) { + + if ( strncmp(dirent->d_name, ".", sizeof(".")) == 0 || strncmp(dirent->d_name, "..", sizeof("..")) == 0 ) + continue; + + if ( snprintf(buf, sizeof(buf), "/sys/dev/block/%s/device/../../../../busnum", dirent->d_name) <= 0 ) + continue; + + f = fopen(buf, "r"); + if ( ! f ) + continue; + + if ( fscanf(f, "%u", &busnum) != 1 ) { + fclose(f); + continue; + } + + fclose(f); + + if ( snprintf(buf, sizeof(buf), "/sys/dev/block/%s/device/../../../../devnum", dirent->d_name) <= 0 ) + continue; + + f = fopen(buf, "r"); + if ( ! f ) + continue; + + if ( fscanf(f, "%u", &devnum) != 1 ) { + fclose(f); + continue; + } + + fclose(f); + + if ( devnum != device->devnum || device->bus->location != busnum ) + continue; + + if ( sscanf(dirent->d_name, "%d:%d", &maj, &min) != 2 ) { + maj = -1; + min = -1; + continue; + } + + break; + + } + + closedir(dir); + + if ( maj == -1 || min == -1 ) { + ERROR("Cannot find id for mmc block disk device"); + return -1; + } + + /* TODO: change 1 to 0 when disk_flash_dev will be implemented */ + fd = disk_open_dev(maj, min, -1, 1); + + if ( fd < 0 ) + return -1; + + dev->data = fd; return 0; } -- cgit v1.2.3 From 93680de9442cbe5dbf1bcf871d1505e2f5da7817 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 16:33:42 +0100 Subject: disk: Implement disk_dump_image() --- src/disk.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/disk.c b/src/disk.c index 30d2d29..efd2441 100644 --- a/src/disk.c +++ b/src/disk.c @@ -335,11 +335,10 @@ int disk_flash_image(struct usb_device_info * dev, struct image * image) { int disk_dump_image(struct usb_device_info * dev, enum image_type image, const char * file) { - ERROR("Not implemented yet"); - (void)dev; - (void)image; - (void)file; - return -1; + if ( image != IMAGE_MMC ) + ERROR_RETURN("Only mmc images are supported", -1); + + return disk_dump_dev(dev->data, file); } -- cgit v1.2.3 From d3eb042a1c1ba330f41fc61cfc270145b97f486d Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 16:41:05 +0100 Subject: operations: Enable dumping mmc images via USB disk protocol --- src/operations.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/operations.c b/src/operations.c index ae06b1d..6696fe1 100644 --- a/src/operations.c +++ b/src/operations.c @@ -194,8 +194,15 @@ int dev_dump_image(struct device_info * dev, enum image_type image, const char * return local_dump_image(image, file); if ( dev->method == METHOD_USB ) { - ERROR("Dump image via USB is not supported"); + + enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; + + if ( protocol == FLASH_DISK ) + return disk_dump_image(dev->usb, image, file); + + ERROR("Dump image via USB not in Mass Storage Mode is not supported"); return -1; + } return -1; -- cgit v1.2.3 From 8c305104a0da9da7f3703b648bea194314a94a5e Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 17:14:39 +0100 Subject: mkii: Check for supported image types --- src/mkii.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/mkii.c b/src/mkii.c index 0214c7a..47b0c66 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -86,6 +86,9 @@ int mkii_init(struct usb_device_info * dev) { struct mkii_message * msg; enum device device; int ret; + char * newptr; + char * ptr; + enum image_type type; printf("Initializing Mk II protocol...\n"); @@ -117,6 +120,34 @@ int mkii_init(struct usb_device_info * dev) { dev->hwrev = mkii_get_hwrev(dev); + memcpy(msg->data, "/update/supported_images", sizeof("/update/supported_images")-1); + ret = mkii_send_receive(dev->udev, MKII_GET_IMAGES, msg, sizeof("/update/supported_images")-1, msg, sizeof(buf)); + if ( ret < 2 || msg->data[0] != 0 ) { + ERROR("Cannot get supported image types"); + return -1; + } + + msg->data[ret] = 0; + ptr = msg->data + 1; + + printf("Supported images by current device configuration:"); + + while ( ptr && *ptr ) { + newptr = strchr(ptr, ','); + if ( newptr ) { + *newptr = 0; + ++newptr; + } + type = image_type_from_string(ptr); + if ( type != IMAGE_UNKNOWN ) { + dev->data |= (1 << type); + printf(" %s", ptr); + } + ptr = newptr; + } + + printf("\n"); + return 0; } @@ -155,6 +186,11 @@ int mkii_flash_image(struct usb_device_info * dev, struct image * image) { ERROR("Not implemented yet"); return -1; + if ( ! ( dev->data & (1 << image->type) ) ) { + ERROR("Flashing image %s is not supported in current device configuration", image_type_to_string(image->type)); + return -1; + } + msg = (struct mkii_message *)buf; msg1 = (struct mkii_message *)buf1; ptr = msg->data; -- cgit v1.2.3 From 7023b4b290cb92e15411331ae930e76e12f6834b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 17:15:16 +0100 Subject: mkii: Check for Update and PC Suite mode --- src/mkii.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/mkii.c b/src/mkii.c index 47b0c66..e3738bc 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -148,6 +148,13 @@ int mkii_init(struct usb_device_info * dev) { printf("\n"); + memset(buf, 0, sizeof(buf)); + usb_get_string_simple(dev->udev, usb_device(dev->udev)->config[dev->flash_device->configuration].iConfiguration, buf, sizeof(buf)); + if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) + dev->data |= (1 << 31); + + printf("Device is in: %s mode\n", (dev->data & (1<<31)) ? "Update" : "PC Suite"); + return 0; } -- cgit v1.2.3 From 6372323ee8d3f60d0668e0ed4f98cf8c8e06b9fa Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 17:16:10 +0100 Subject: usb-device: Difference between Update and PC Suite mode --- src/usb-device.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/usb-device.c b/src/usb-device.c index 1f9b403..943303b 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -384,7 +384,7 @@ void usb_switch_to_update(struct usb_device_info * dev) { leave_cold_flash(dev); else if ( dev->flash_device->protocol == FLASH_NOLO ) nolo_boot_device(dev, "update"); - else if ( dev->flash_device->protocol == FLASH_MKII ) + else if ( dev->flash_device->protocol == FLASH_MKII && ! ( dev->data & ( 1 << 31 ) ) ) mkii_reboot_device(dev); else if ( dev->flash_device->protocol == FLASH_DISK ) printf_and_wait("Unplug USB cable, turn device off, press ENTER and plug USB cable again"); @@ -400,7 +400,11 @@ void usb_switch_to_disk(struct usb_device_info * dev) { else if ( dev->flash_device->protocol == FLASH_NOLO ) { nolo_boot_device(dev, NULL); printf_and_wait("Wait until device start, choose USB Mass Storage Mode and press ENTER"); - } else if ( dev->flash_device->protocol == FLASH_MKII ) - mkii_reboot_device(dev); + } else if ( dev->flash_device->protocol == FLASH_MKII ) { + if ( dev->data & ( 1 << 31 ) ) + mkii_reboot_device(dev); + else + printf_and_wait("Unplug USB cable, plug again, choose USB Mass Storage Mode and press ENTER"); + } } -- cgit v1.2.3 From f1095777c6c92f284ba2a8eb8b08a21476e4cb5f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 17:16:44 +0100 Subject: operations: Check for supported image types in dev_flash_image --- src/operations.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/operations.c b/src/operations.c index 6696fe1..10ad975 100644 --- a/src/operations.c +++ b/src/operations.c @@ -174,10 +174,15 @@ int dev_flash_image(struct device_info * dev, struct image * image) { enum usb_flash_protocol protocol = dev->usb->flash_device->protocol; - if ( protocol == FLASH_NOLO ) - return nolo_flash_image(dev->usb, image); - else if ( protocol == FLASH_MKII ) - return mkii_flash_image(dev->usb, image); + if ( protocol == FLASH_NOLO ) { + if ( image->type != IMAGE_MMC ) + return nolo_flash_image(dev->usb, image); + usb_switch_to_update(dev->usb); + return -EAGAIN; + } else if ( protocol == FLASH_MKII ) { + if ( dev->usb->data & (1 << image->type) ) + return mkii_flash_image(dev->usb, image); + } usb_switch_to_nolo(dev->usb); return -EAGAIN; -- cgit v1.2.3 From 8369f94195142fc2da816f401ee7843f90677e54 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 17:47:47 +0100 Subject: mkii: Implement mkii_get_sw_ver --- src/mkii.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index e3738bc..bcf6a3a 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -500,11 +500,21 @@ int mkii_set_nolo_ver(struct usb_device_info * dev, const char * ver) { int mkii_get_sw_ver(struct usb_device_info * dev, char * ver, size_t size) { - ERROR("Not implemented yet"); - (void)dev; - (void)ver; - (void)size; - return -1; + char buf[2048]; + struct mkii_message * msg; + int ret; + + msg = (struct mkii_message *)buf; + + memcpy(msg->data, "/version/sw_release", sizeof("/version/sw_release")-1); + ret = mkii_send_receive(dev->udev, MKII_GET_DEVICE, msg, sizeof("/version/sw_release")-1, msg, sizeof(buf)); + if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) + return -1; + + msg->data[ret] = 0; + strncpy(ver, msg->data+1, size); + ver[size-1] = 0; + return strlen(ver); } -- cgit v1.2.3 From 2c155350999f8cb89b2912050431403d199350f8 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 21 Nov 2014 17:48:01 +0100 Subject: operations: Enable mkii_get_sw_ver --- src/operations.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/operations.c b/src/operations.c index 10ad975..85d1e3d 100644 --- a/src/operations.c +++ b/src/operations.c @@ -602,6 +602,8 @@ int dev_get_sw_ver(struct device_info * dev, char * ver, size_t size) { if ( protocol == FLASH_NOLO ) return nolo_get_sw_ver(dev->usb, ver, size); + else if ( protocol == FLASH_MKII ) + return mkii_get_sw_ver(dev->usb, ver, size); } -- cgit v1.2.3 From 8b5c94507896e6f125d97c16fca55f8929fc734d Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 14:25:41 +0100 Subject: mkii: Fix mkii_get_hwrev --- src/mkii.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mkii.c b/src/mkii.c index bcf6a3a..0f7125a 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -423,7 +423,7 @@ int16_t mkii_get_hwrev(struct usb_device_info * dev) { msg = (struct mkii_message *)buf; memcpy(msg->data, "/device/hw_build", sizeof("/device/hw_build")-1); - ret = mkii_send_receive(dev->udev, MKII_GET_DEVICE, msg, sizeof("/device/hw_build")-1, msg, sizeof(buf)); + ret = mkii_send_receive(dev->udev, MKII_GET_HWREV, msg, sizeof("/device/hw_build")-1, msg, sizeof(buf)); if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) return -1; -- cgit v1.2.3 From c87687128fea5e309fd4651a0d42abde74e405ed Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 14:25:50 +0100 Subject: mkii: Fix mkii_reboot_device --- src/mkii.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index 0f7125a..0abde6a 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -335,8 +335,8 @@ int mkii_reboot_device(struct usb_device_info * dev) { printf("Rebooting device...\n"); - memcpy(msg->data, "reboot", sizeof("reboot")-1); - ret = mkii_send_receive(dev->udev, MKII_GET_DEVICE, msg, sizeof("reboot")-1, msg, sizeof(buf)); + memcpy(msg->data, "reboot", sizeof("reboot")); + ret = mkii_send_receive(dev->udev, MKII_REBOOT, msg, sizeof("reboot"), msg, sizeof(buf)); if ( ret != 1 || msg->data[0] != 0 ) return -1; -- cgit v1.2.3 From 349a9d40ef03343c0f3bea08911b277c855dd1f1 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 14:26:14 +0100 Subject: mkii: Fix mode information --- src/mkii.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mkii.c b/src/mkii.c index 0abde6a..248461c 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -153,7 +153,7 @@ int mkii_init(struct usb_device_info * dev) { if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) dev->data |= (1 << 31); - printf("Device is in: %s mode\n", (dev->data & (1<<31)) ? "Update" : "PC Suite"); + printf("Mode: %s\n", (dev->data & (1<<31)) ? "Update" : "PC Suite"); return 0; -- cgit v1.2.3 From e8f9a7917478b6cd41b91eedac6026d9b350a0cf Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 15:07:50 +0100 Subject: doc/mkii: Update info about protocol --- doc/mkii | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 83 insertions(+), 17 deletions(-) diff --git a/doc/mkii b/doc/mkii index f18f5b3..0d3ecc2 100644 --- a/doc/mkii +++ b/doc/mkii @@ -1,4 +1,4 @@ - Copyright (C) 2012 Pali Rohár + Copyright (C) 2012-2014 Pali Rohár Mk II protocol is the only protocol which can be used to flash eMMC images. NOLO does not support eMMC, so flashing eMMC is done in Maemo system. NOLO @@ -27,6 +27,10 @@ Over usb are used only these functions for communication: usb_bulk_write (ep=1, timeout=5000) usb_bulk_read (ep=129, timeout=5000) +And this function for sending raw data: + + usb_bulk_write (ep=2, timeout=1000) + For every (request) message which is send by host, server send back response. Format of message every message is same. It has 6 bytes header and (at least) 4 bytes body. @@ -40,51 +44,113 @@ HEADER BODY - 4 bytes -- type of message + 2 bytes -- unknown (always zero) + 1 byte -- or. num of message (starting with zero) + 1 byte -- type of message N bytes -- data Reply message data always starts with char 0x00 (except pong response). +Message types: + +0x00 - PING +0x01 - GET +0x02 - TELL +0x0C - REBOOT + +0x20 - RESPONCE + Here are some sniffed messages from Nokia RX-51. First two messages seems to must be always protocol version exchange (first host ask for protocol version of server and then host send its protocol version). On RX-51 is used version "2". Ping: - req_type = 0x00000000 - res_type = 0x20000000 + req_type = 0x00 + res_type = 0x20 Get protocol version: - req_type = 0x01010000 + req_type = 0x01 req_data = "/update/protocol_version" - res_type = 0x21010000 + res_type = 0x21 res_data = 0x00 "2" Tell our protocol version: - req_type = 0x02020000 + req_type = 0x02 req_data = "/update/host_protocol_version" 0x00 "2" - res_type = 0x22020000 + res_type = 0x22 res_data = 0x00 Get device: - req_type = 0x01030000 + req_type = 0x01 req_data = "/device/product_code" - res_type = 0x21030000 + res_type = 0x21 res_data = 0x00 "RX-51" Get hwrev: - req_type = 0x01040000 + req_type = 0x01 req_data = "/device/hw_build" - res_type = 0x21040000 + res_type = 0x21 res_data = 0x00 "2101" Get image types: - req_type = 0x01050000 + req_type = 0x01 req_data = "/update/supported_images" - res_type = 0x21050000 + res_type = 0x21 res_data = 0x00 "xloader,secondary,kernel,mmc,cmt-2nd,cmt-algo,cmt-mcusw" Reboot device: - req_type = 0x0C060000 - req_data = "reboot" - res_type = 0x2C060000 + req_type = 0x0C + req_data = "reboot" 0x00 + res_type = 0x2C res_data = 0x00 + + Send image (mmc): + req_type = 0x03 + res_type = 0x23 + res_data = 0x00 + + req_type = 0x04 + req_data = fiasco subimage header + res_type = 0x24 + res_data = 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 + + req_type = 0x05 + req_data = 0x00 0x00 0x00 0x00 "usb:raw" + res_type = 0x25 + res_data = 0x00 + + req_type = 0x06 + req_data = 0x00 0x00 0x00 0x00 + res_type = 0x26 + res_data = 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 + + req_type = 0x0B + req_data = 0x00 0x00 0x00 0x64 + res_type = 0x2B + res_data = 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x00 + + req_type = 0x08 + req_data = 0x00 0x00 0x00 0x00 0x00 0x10 0x00 0x00 + res_type = 0x28 + res_data = 0x00 + + (raw data on ep=2 size=1048576) + + req_type = 0x06 + req_data = 0x00 0x00 0x00 0x00 + res_type = 0x26 + res_data = 0x00 0x00 0x00 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x0F 0x9F 0x2C 0x00 0x00 + + req_type = 0x0B + req_data = 0x00 0x00 0x00 0x64 + res_type = 0x2B + res_data = 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x01 0xF0 0x00 0x00 0x00 + + req_type = 0x08 + req_data = 0x00 0x00 0x00 0x00 0x00 0x10 0x00 0x00 + res_type = 0x28 + res_data = 0x00 + + (raw data on ep=2 size=1048576) + + ... -- cgit v1.2.3 From a9b1332f8f0edb0b98113d6b959e0066bac8505f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 15:09:01 +0100 Subject: mkii: Update code with to reflect documentation --- src/mkii.c | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index 248461c..7e2ea26 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -29,29 +29,34 @@ #include "device.h" #include "usb-device.h" -#define MKII_PING 0x00000000 -#define MKII_GET_PROTOCOL 0x01010000 -#define MKII_TELL_PROTOCOL 0x02020000 -#define MKII_GET_DEVICE 0x01030000 -#define MKII_GET_HWREV 0x01040000 -#define MKII_GET_IMAGES 0x01050000 -#define MKII_REBOOT 0x0C060000 -#define MKII_INIT_SEND 0x03010000 -#define MKII_SEND_IMAGE 0x04020000 +#define MKII_OUT 0x8810001B +#define MKII_IN 0x8800101B + +#define MKII_PING 0x00 +#define MKII_GET 0x01 +#define MKII_TELL 0x02 +#define MKII_REBOOT 0x0C +#define MKII_RESPONCE 0x20 struct mkii_message { uint32_t header; uint16_t size; - uint32_t type; + uint16_t zero; + uint8_t num; + uint8_t type; char data[]; } __attribute__((__packed__)); -static int mkii_send_receive(usb_dev_handle * udev, uint32_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { + +static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { int ret; + static uint8_t number = 0; - in_msg->header = 0x8810001B; + in_msg->header = MKII_OUT; in_msg->size = htons(data_size + 4); + in_msg->zero = 0; + in_msg->num = number++; in_msg->type = type; ret = usb_bulk_write(udev, 1, (const char *)in_msg, data_size + sizeof(*in_msg), 5000); @@ -64,10 +69,10 @@ static int mkii_send_receive(usb_dev_handle * udev, uint32_t type, struct mkii_m if ( ret < 0 ) return ret; - if ( out_msg->header != 0x8800101B ) + if ( out_msg->header != MKII_IN ) return -1; - if ( out_msg->type != (type | 0x20000000) ) + if ( out_msg->type != (type | MKII_RESPONCE) ) return -1; if ( (size_t)ret < sizeof(*out_msg) ) @@ -99,12 +104,12 @@ int mkii_init(struct usb_device_info * dev) { return -1; memcpy(msg->data, "/update/protocol_version", sizeof("/update/protocol_version")-1); - ret = mkii_send_receive(dev->udev, MKII_GET_PROTOCOL, msg, sizeof("/update/protocol_version")-1, msg, sizeof(buf)); + ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/update/protocol_version")-1, msg, sizeof(buf)); if ( ret != 2 || msg->data[0] != 0 || msg->data[1] != 0x32 ) return -1; memcpy(msg->data, "/update/host_protocol_version\x00\x32", sizeof("/update/host_protocol_version\x00\x32")-1); - ret = mkii_send_receive(dev->udev, MKII_TELL_PROTOCOL, msg, sizeof("/update/host_protocol_version\x00\x32")-1, msg, sizeof(buf)); + ret = mkii_send_receive(dev->udev, MKII_TELL, msg, sizeof("/update/host_protocol_version\x00\x32")-1, msg, sizeof(buf)); if ( ret != 1 || msg->data[0] != 0 ) return -1; @@ -121,7 +126,7 @@ int mkii_init(struct usb_device_info * dev) { dev->hwrev = mkii_get_hwrev(dev); memcpy(msg->data, "/update/supported_images", sizeof("/update/supported_images")-1); - ret = mkii_send_receive(dev->udev, MKII_GET_IMAGES, msg, sizeof("/update/supported_images")-1, msg, sizeof(buf)); + ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/update/supported_images")-1, msg, sizeof(buf)); if ( ret < 2 || msg->data[0] != 0 ) { ERROR("Cannot get supported image types"); return -1; @@ -168,7 +173,7 @@ enum device mkii_get_device(struct usb_device_info * dev) { msg = (struct mkii_message *)buf; memcpy(msg->data, "/device/product_code", sizeof("/device/product_code")-1); - ret = mkii_send_receive(dev->udev, MKII_GET_DEVICE, msg, sizeof("/device/product_code")-1, msg, sizeof(buf)); + ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/device/product_code")-1, msg, sizeof(buf)); if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) return DEVICE_UNKNOWN; @@ -311,11 +316,11 @@ int mkii_flash_image(struct usb_device_info * dev, struct image * image) { memcpy(ptr, "\x00", 1); ptr += 1; - ret = mkii_send_receive(dev->udev, MKII_INIT_SEND, msg1, 0, msg1, sizeof(buf1)); + ret = mkii_send_receive(dev->udev, 0x03, msg1, 0, msg1, sizeof(buf1)); if ( ret != 1 || msg1->data[0] != 0 ) return -1; - ret = mkii_send_receive(dev->udev, MKII_SEND_IMAGE, msg, ptr - msg->data, msg, sizeof(buf)); + ret = mkii_send_receive(dev->udev, 0x04, msg, ptr - msg->data, msg, sizeof(buf)); if ( ret != 9 ) return -1; @@ -423,7 +428,7 @@ int16_t mkii_get_hwrev(struct usb_device_info * dev) { msg = (struct mkii_message *)buf; memcpy(msg->data, "/device/hw_build", sizeof("/device/hw_build")-1); - ret = mkii_send_receive(dev->udev, MKII_GET_HWREV, msg, sizeof("/device/hw_build")-1, msg, sizeof(buf)); + ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/device/hw_build")-1, msg, sizeof(buf)); if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) return -1; @@ -507,7 +512,7 @@ int mkii_get_sw_ver(struct usb_device_info * dev, char * ver, size_t size) { msg = (struct mkii_message *)buf; memcpy(msg->data, "/version/sw_release", sizeof("/version/sw_release")-1); - ret = mkii_send_receive(dev->udev, MKII_GET_DEVICE, msg, sizeof("/version/sw_release")-1, msg, sizeof(buf)); + ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/version/sw_release")-1, msg, sizeof(buf)); if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) return -1; -- cgit v1.2.3 From bc9f42f5595bf554e9af7d2cfc0bb9dfb8fb255f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 15:46:03 +0100 Subject: all: Use nanosleep (via SLEEP) instead usleep --- src/cold-flash.c | 16 ++++++++-------- src/global.h | 3 +++ src/nolo.c | 2 +- src/usb-device.c | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/cold-flash.c b/src/cold-flash.c index 8874b32..eb2ca67 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -189,13 +189,13 @@ static int send_2nd(usb_dev_handle * udev, struct image * image) { printf("Sending OMAP peripheral boot message...\n"); ret = usb_bulk_write(udev, WRITE_DEV, (char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), WRITE_TIMEOUT); - usleep(5000); + SLEEP(5000); if ( ret != sizeof(omap_peripheral_msg) ) ERROR_RETURN("Sending OMAP peripheral boot message failed", -1); printf("Sending 2nd X-Loader image size...\n"); ret = usb_bulk_write(udev, WRITE_DEV, (char *)&image->size, 4, WRITE_TIMEOUT); - usleep(5000); + SLEEP(5000); if ( ret != 4 ) ERROR_RETURN("Sending 2nd X-Loader image size failed", -1); @@ -215,7 +215,7 @@ static int send_2nd(usb_dev_handle * udev, struct image * image) { readed += ret; printf_progressbar(readed, image->size); } - usleep(50000); + SLEEP(50000); return 0; @@ -232,7 +232,7 @@ static int send_secondary(usb_dev_handle * udev, struct image * image) { printf("Sending X-Loader init message...\n"); ret = usb_bulk_write(udev, WRITE_DEV, (char *)&init_msg, sizeof(init_msg), WRITE_TIMEOUT); - usleep(5000); + SLEEP(5000); if ( ret != sizeof(init_msg) ) ERROR_RETURN("Sending X-Loader init message failed", -1); @@ -257,7 +257,7 @@ static int send_secondary(usb_dev_handle * udev, struct image * image) { readed += ret; printf_progressbar(readed, image->size); } - usleep(5000); + SLEEP(5000); printf("Waiting for X-Loader response...\n"); ret = usb_bulk_read(udev, READ_DEV, (char *)&buffer, 4, READ_TIMEOUT); /* 4 bytes - dummy value */ @@ -295,7 +295,7 @@ static int ping_timeout(usb_dev_handle * udev) { break; } - usleep(5000); + SLEEP(5000); --try_read; } @@ -405,11 +405,11 @@ int leave_cold_flash(struct usb_device_info * dev) { printf("Sending OMAP memory boot message...\n"); ret = usb_bulk_write(dev->udev, WRITE_DEV, (char *)&omap_memory_msg, sizeof(omap_memory_msg), WRITE_TIMEOUT); - usleep(5000); + SLEEP(5000); if ( ret != sizeof(omap_memory_msg) ) ERROR_RETURN("Sending OMAP memory boot message failed", -1); - usleep(250000); + SLEEP(250000); return 0; } diff --git a/src/global.h b/src/global.h index e1f0365..991d3b3 100644 --- a/src/global.h +++ b/src/global.h @@ -5,6 +5,7 @@ #include #include #include +#include extern int simulate; extern int noverify; @@ -21,4 +22,6 @@ extern int verbose; #define ALLOC_ERROR() do { ERROR("Cannot allocate memory"); } while (0) #define ALLOC_ERROR_RETURN(...) do { ALLOC_ERROR(); return __VA_ARGS__; } while (0) +#define SLEEP(usec) do { struct timespec _t = { 0, (usec) }; nanosleep(&_t, NULL); } while (0) + #endif diff --git a/src/nolo.c b/src/nolo.c index e437ef7..6953f49 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -537,7 +537,7 @@ int nolo_flash_image(struct usb_device_info * dev, struct image * image) { } - usleep(0xc350); // 0.5s + SLEEP(0xc350); // 0.5s } diff --git a/src/usb-device.c b/src/usb-device.c index 943303b..4fb011a 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -325,7 +325,7 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { if ( ret ) break; - usleep(0xc350); // 0.5s + SLEEP(0xc350); // 0.5s } -- cgit v1.2.3 From 52efd5a404f3c8ee64b62ed55c954a151e61153a Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 15:47:21 +0100 Subject: nolo: Check for return buffer size --- src/nolo.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/nolo.c b/src/nolo.c index 6953f49..6dce8a2 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -124,6 +124,9 @@ static int nolo_identify_string(struct usb_device_info * dev, const char * str, if ( ret < 0 ) NOLO_ERROR_RETURN("NOLO_IDENTIFY failed", -1); + if ( (size_t)ret > sizeof(buf) ) + ret = sizeof(buf); + ptr = memmem(buf, ret, str, strlen(str)); if ( ! ptr ) ERROR_RETURN("Substring was not found", -1); @@ -167,6 +170,9 @@ static int nolo_get_string(struct usb_device_info * dev, char * str, char * out, if ( ( ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET_STRING, 0, 0, out, size-1, 2000) ) < 0 ) return -1; + if ( (size_t)ret > size-1 ) + ret = size-1; + out[size-1] = 0; out[ret] = 0; return strlen(out); -- cgit v1.2.3 From db4c7c19512504ee1391728248ae08c966a60b43 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:29:53 +0100 Subject: all: Use internal MEMMEM instead gnu memem It is used only on small strings, so it is fast enough --- src/global.h | 13 +++++++++++++ src/mkii.c | 2 +- src/nolo.c | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/global.h b/src/global.h index 991d3b3..4cff79f 100644 --- a/src/global.h +++ b/src/global.h @@ -24,4 +24,17 @@ extern int verbose; #define SLEEP(usec) do { struct timespec _t = { 0, (usec) }; nanosleep(&_t, NULL); } while (0) +static inline void * MEMMEM(void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { + for ( size_t i = 0; i < haystacklen; ++i ) { + for ( size_t j = 0; j < needlelen; ++j ) { + if ( ((char *)haystack)[i] != ((const char *)needle)[j] ) + break; + if ( j != needlelen - 1 ) + continue; + return (char *)haystack + i; + } + } + return NULL; +} + #endif diff --git a/src/mkii.c b/src/mkii.c index 7e2ea26..889dae7 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -262,7 +262,7 @@ int mkii_flash_image(struct usb_device_info * dev, struct image * image) { for ( i = 0; bufs[i]; ++i ) { len = ((uint8_t*)bufs[i])[0]; - if ( memmem(bufs[i]+1, len, buf, strlen(buf)) ) + if ( MEMMEM(bufs[i]+1, len, buf, strlen(buf)) ) break; } diff --git a/src/nolo.c b/src/nolo.c index 6dce8a2..5e51a36 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -127,7 +127,7 @@ static int nolo_identify_string(struct usb_device_info * dev, const char * str, if ( (size_t)ret > sizeof(buf) ) ret = sizeof(buf); - ptr = memmem(buf, ret, str, strlen(str)); + ptr = MEMMEM(buf, ret, str, strlen(str)); if ( ! ptr ) ERROR_RETURN("Substring was not found", -1); @@ -326,7 +326,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i for ( i = 0; bufs[i]; ++i ) { len = ((uint8_t*)bufs[i])[0]; - if ( memmem(bufs[i]+1, len, buf, strlen(buf)) ) + if ( MEMMEM(bufs[i]+1, len, buf, strlen(buf)) ) break; } -- cgit v1.2.3 From 15219cb99ce185571638f8b6732300dd32e225bd Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:30:32 +0100 Subject: main: Add missing extern variables for getopt --- src/main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.c b/src/main.c index 0552593..b224e5c 100644 --- a/src/main.c +++ b/src/main.c @@ -36,6 +36,9 @@ #include "device.h" #include "operations.h" +extern char *optarg; +extern int optind, opterr, optopt; + static void show_title(void) { printf("0xFFFF v%s // Open Free Fiasco Firmware Flasher\n", VERSION); } -- cgit v1.2.3 From d61815f40eef2583adc23a5c34d519847de26ca2 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:30:54 +0100 Subject: mkii: Fix argument for usb_bulk_write --- src/mkii.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mkii.c b/src/mkii.c index 889dae7..5030a89 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -59,7 +59,7 @@ static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_me in_msg->num = number++; in_msg->type = type; - ret = usb_bulk_write(udev, 1, (const char *)in_msg, data_size + sizeof(*in_msg), 5000); + ret = usb_bulk_write(udev, 1, (char *)in_msg, data_size + sizeof(*in_msg), 5000); if ( ret < 0 ) return ret; if ( (size_t)ret != data_size + sizeof(*in_msg) ) -- cgit v1.2.3 From 5633a067102ac35a623258bb71cb5d6396f50986 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:32:36 +0100 Subject: local: Enable N900 code only for ARM and enable Linux code only on Linux --- src/local.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/local.c b/src/local.c index f2cdba4..5dc14ee 100644 --- a/src/local.c +++ b/src/local.c @@ -55,6 +55,8 @@ static int root_device = -1; #define local_cal_read(cal, str, ptr, len) ( cal_read_block(cal, str, &ptr, &len, 0) == 0 && ptr ) #define local_cal_readcopy(cal, str, dest) do { void * ptr; unsigned long int len; if ( local_cal_read(cal, str, ptr, len) ) local_cal_copy(dest, ptr, len); } while ( 0 ) +#if defined(__linux__) && defined(__arm__) + static void local_cal_parse(void) { struct cal * cal = NULL; @@ -109,16 +111,22 @@ static void local_cal_parse(void) { } +#endif + int local_init(void) { +#if defined(__linux__) && defined(__arm__) char buf[1024]; char * ptr; char * ptr2; FILE * file; +#endif if ( failed ) return -1; +#if defined(__linux__) && defined(__arm__) + file = fopen("/proc/cpuinfo", "r"); if ( ! file ) { failed = 1; @@ -174,9 +182,12 @@ int local_init(void) { } + fclose(file); + +#endif + failed = 1; printf("Not a local device\n"); - fclose(file); return -1; } @@ -284,6 +295,8 @@ static struct nanddump_device nanddump[] = { static void local_find_internal_mydocs(int * maj, int * min) { +#ifdef __linux__ + int fd; DIR * dir; DIR * dir2; @@ -361,6 +374,8 @@ static void local_find_internal_mydocs(int * maj, int * min) { closedir(dir); +#endif + } int local_dump_image(enum image_type image, const char * file) { -- cgit v1.2.3 From ef1960cc32a524ab003abea53bb54e2a39fa6421 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:33:07 +0100 Subject: disk: Enable Linux code only on Linux --- src/disk.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/disk.c b/src/disk.c index efd2441..86394f4 100644 --- a/src/disk.c +++ b/src/disk.c @@ -18,13 +18,16 @@ #include #include +#include #include #include -#include #include +#ifdef __linux__ +#include #include +#endif #include "disk.h" #include "global.h" @@ -37,6 +40,8 @@ static char global_buf[1 << 22]; /* 4MB */ int disk_open_dev(int maj, int min, int partition, int readonly) { +#ifdef __linux__ + int fd; struct stat st; DIR * dir; @@ -140,6 +145,17 @@ int disk_open_dev(int maj, int min, int partition, int readonly) { return fd; +#else + + ERROR("Not implemented yet"); + (void)min; + (void)maj; + (void)partition; + (void)readonly; + return -1; + +#endif + } int disk_dump_dev(int fd, const char * file) { @@ -154,11 +170,25 @@ int disk_dump_dev(int fd, const char * file) { printf("Dump block device to file %s...\n", file); +#ifdef __linux__ + if ( ioctl(fd, BLKGETSIZE64, &blksize) != 0 ) { ERROR_INFO("Cannot get size of block device"); return -1; } +#else + + blksize = lseek(fd, 0, SEEK_END); + if ( blksize == (off_t)-1 ) { + ERROR_INFO("Cannot get size of block device"); + return -1; + } + + lseek(fd, 0, SEEK_SET); + +#endif + if ( blksize > ULLONG_MAX ) { ERROR("Block device is too big"); return -1; @@ -226,6 +256,8 @@ int disk_flash_dev(int fd, const char * file) { int disk_init(struct usb_device_info * dev) { +#ifdef __linux__ + int fd; int maj; int min; @@ -316,6 +348,14 @@ int disk_init(struct usb_device_info * dev) { dev->data = fd; return 0; +#else + + ERROR("Not implemented yet"); + (void)dev; + return -1; + +#endif + } enum device disk_get_device(struct usb_device_info * dev) { -- cgit v1.2.3 From 448365b5889747ee04ac8d8628b730aa477ca89e Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:33:21 +0100 Subject: cal: Enable Linux code only on Linux --- src/cal.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/cal.c b/src/cal.c index 3a46abd..99de241 100644 --- a/src/cal.c +++ b/src/cal.c @@ -33,8 +33,10 @@ #include #include +#ifdef __linux__ #include #include +#endif #include "cal.h" @@ -68,7 +70,9 @@ int cal_init_file(const char * file, struct cal ** cal_out) { void * mem = NULL; struct cal * cal = NULL; struct stat st; +#ifdef __linux__ mtd_info_t mtd_info; +#endif if ( stat(file, &st) != 0 ) return -1; @@ -81,17 +85,30 @@ int cal_init_file(const char * file, struct cal ** cal_out) { if ( S_ISREG(st.st_mode) ) size = st.st_size; else if ( S_ISBLK(st.st_mode) ) { +#ifdef __linux__ if ( ioctl(fd, BLKGETSIZE64, &blksize) != 0 ) goto err; +#else + blksize = lseek(fd, 0, SEEK_END); + if ( blksize == (off_t)-1 ) + goto err; + lseek(fd, 0, SEEK_SET); +#endif if ( blksize > SSIZE_MAX ) goto err; size = blksize; - } else if ( S_ISCHR(st.st_mode) && major(st.st_rdev) == 90 ) { - if ( ioctl(fd, MEMGETINFO, &mtd_info) != 0 ) - goto err; - size = mtd_info.size; } else { +#ifdef __linux__ + if ( S_ISCHR(st.st_mode) && major(st.st_rdev) == 90 ) { + if ( ioctl(fd, MEMGETINFO, &mtd_info) != 0 ) + goto err; + size = mtd_info.size; + } else { + goto err; + } +#else goto err; +#endif } if ( size == 0 || size > MAX_SIZE ) -- cgit v1.2.3 From c86e130ab1af4b6161b2a71a2449d2e02109800f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:35:01 +0100 Subject: Makefile: Now 0xFFFF is Posix compatible, enable -D_POSIX_C_SOURCE=200809L --- src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index b436ee6..cad52e3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,7 +7,7 @@ CC = gcc CROSS_CC = $(CROSS_COMPILE)$(CC) HOST_CC = $(HOST_COMPILE)$(CC) -CPPFLAGS += -DVERSION=\"$(VERSION)\" -D_GNU_SOURCE +CPPFLAGS += -DVERSION=\"$(VERSION)\" -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L CFLAGS += -W -Wall -O2 -pedantic -std=c99 LIBS += -lusb -- cgit v1.2.3 From bb90caec1be476ce4093e72029537c8ccd38f5b3 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:49:39 +0100 Subject: all: Fix overflow in shift operators --- src/disk.c | 2 +- src/mkii.c | 8 ++++---- src/operations.c | 2 +- src/usb-device.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/disk.c b/src/disk.c index 86394f4..7370f13 100644 --- a/src/disk.c +++ b/src/disk.c @@ -36,7 +36,7 @@ #include "usb-device.h" #include "printf-utils.h" -static char global_buf[1 << 22]; /* 4MB */ +static char global_buf[1UL << 22]; /* 4MB */ int disk_open_dev(int maj, int min, int partition, int readonly) { diff --git a/src/mkii.c b/src/mkii.c index 5030a89..e2fb23f 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -145,7 +145,7 @@ int mkii_init(struct usb_device_info * dev) { } type = image_type_from_string(ptr); if ( type != IMAGE_UNKNOWN ) { - dev->data |= (1 << type); + dev->data |= (1UL << type); printf(" %s", ptr); } ptr = newptr; @@ -156,9 +156,9 @@ int mkii_init(struct usb_device_info * dev) { memset(buf, 0, sizeof(buf)); usb_get_string_simple(dev->udev, usb_device(dev->udev)->config[dev->flash_device->configuration].iConfiguration, buf, sizeof(buf)); if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) - dev->data |= (1 << 31); + dev->data |= (1UL << 31); - printf("Mode: %s\n", (dev->data & (1<<31)) ? "Update" : "PC Suite"); + printf("Mode: %s\n", (dev->data & (1UL << 31)) ? "Update" : "PC Suite"); return 0; @@ -198,7 +198,7 @@ int mkii_flash_image(struct usb_device_info * dev, struct image * image) { ERROR("Not implemented yet"); return -1; - if ( ! ( dev->data & (1 << image->type) ) ) { + if ( ! ( dev->data & (1UL << image->type) ) ) { ERROR("Flashing image %s is not supported in current device configuration", image_type_to_string(image->type)); return -1; } diff --git a/src/operations.c b/src/operations.c index 85d1e3d..59bf212 100644 --- a/src/operations.c +++ b/src/operations.c @@ -180,7 +180,7 @@ int dev_flash_image(struct device_info * dev, struct image * image) { usb_switch_to_update(dev->usb); return -EAGAIN; } else if ( protocol == FLASH_MKII ) { - if ( dev->usb->data & (1 << image->type) ) + if ( dev->usb->data & (1UL << image->type) ) return mkii_flash_image(dev->usb, image); } diff --git a/src/usb-device.c b/src/usb-device.c index 4fb011a..a4b1f8b 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -384,7 +384,7 @@ void usb_switch_to_update(struct usb_device_info * dev) { leave_cold_flash(dev); else if ( dev->flash_device->protocol == FLASH_NOLO ) nolo_boot_device(dev, "update"); - else if ( dev->flash_device->protocol == FLASH_MKII && ! ( dev->data & ( 1 << 31 ) ) ) + else if ( dev->flash_device->protocol == FLASH_MKII && ! ( dev->data & ( 1UL << 31 ) ) ) mkii_reboot_device(dev); else if ( dev->flash_device->protocol == FLASH_DISK ) printf_and_wait("Unplug USB cable, turn device off, press ENTER and plug USB cable again"); @@ -401,7 +401,7 @@ void usb_switch_to_disk(struct usb_device_info * dev) { nolo_boot_device(dev, NULL); printf_and_wait("Wait until device start, choose USB Mass Storage Mode and press ENTER"); } else if ( dev->flash_device->protocol == FLASH_MKII ) { - if ( dev->data & ( 1 << 31 ) ) + if ( dev->data & ( 1UL << 31 ) ) mkii_reboot_device(dev); else printf_and_wait("Unplug USB cable, plug again, choose USB Mass Storage Mode and press ENTER"); -- cgit v1.2.3 From 33f1dc8b55b4dd871e0a4a734c111ff751aaac03 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 16:53:08 +0100 Subject: local: Fix argument for mmap/munmap --- src/local.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/local.c b/src/local.c index 5dc14ee..71d6d9d 100644 --- a/src/local.c +++ b/src/local.c @@ -442,7 +442,7 @@ int local_dump_image(enum image_type image, const char * file) { if ( len == (off_t)-1 || len == 0 ) goto clean; - addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); + addr = (unsigned char *)mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); if ( addr == MAP_FAILED ) addr = NULL; @@ -477,7 +477,7 @@ int local_dump_image(enum image_type image, const char * file) { clean: if ( addr ) - munmap(addr, len); + munmap((void *)addr, len); if ( fd >= 0 ) close(fd); -- cgit v1.2.3 From ef678ecf6c72c3df72b28685f83186228c5afd9b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 26 Nov 2014 17:00:10 +0100 Subject: fiasco: Remove not reached code --- src/fiasco.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/fiasco.c b/src/fiasco.c index a2cd55a..cb890e0 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -234,8 +234,6 @@ struct fiasco * fiasco_alloc_from_file(const char * file) { } - return fiasco; - } void fiasco_free(struct fiasco * fiasco) { -- cgit v1.2.3 From f965980e2b309921e0b869c05177f1ee3937a845 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 15 Jan 2015 09:42:01 +0100 Subject: all: Fix MEMMEM implementation --- src/global.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global.h b/src/global.h index 4cff79f..a26c88b 100644 --- a/src/global.h +++ b/src/global.h @@ -27,7 +27,7 @@ extern int verbose; static inline void * MEMMEM(void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { for ( size_t i = 0; i < haystacklen; ++i ) { for ( size_t j = 0; j < needlelen; ++j ) { - if ( ((char *)haystack)[i] != ((const char *)needle)[j] ) + if ( ((char *)haystack)[i+j] != ((const char *)needle)[j] ) break; if ( j != needlelen - 1 ) continue; -- cgit v1.2.3 From 289d2f22b237e901853566c8588800ac9f189847 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 15 Jan 2015 09:42:40 +0100 Subject: image: Fix detecting new ARM U-Boot images --- src/image.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/image.c b/src/image.c index 48d80d8..5424f9a 100644 --- a/src/image.c +++ b/src/image.c @@ -502,7 +502,7 @@ enum image_type image_type_from_data(struct image * image) { return IMAGE_2ND; else if ( memcmp(buf+36, "\x18\x28\x6f\x01", 4) == 0 ) /* ARM Linux kernel magic number */ return IMAGE_KERNEL; - else if ( memcmp(buf, "\x14\x00\x00\xea", 4) == 0 ) /* ARM U-Boot - instruction branch +0x50 */ + else if ( memcmp(buf+1, "\x00\x00\xea", 3) == 0 ) /* ARM U-Boot - instruction branch */ return IMAGE_KERNEL; else if ( memcmp(buf, "UBI#", 4) == 0 ) /* UBI EC header */ return IMAGE_ROOTFS; -- cgit v1.2.3 From 94a2fe0c17cac00c679cd08f9d8c3f6e78fa09a4 Mon Sep 17 00:00:00 2001 From: Dhole Date: Wed, 24 Jun 2015 17:46:28 +0200 Subject: mangen: Set build date externally Patch build system to allow the build date in the man entry to be set externally. In Debian it will be set to the latest debian/changelog entry for reproducibility. --- src/Makefile | 4 +++- src/mangen.c | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Makefile b/src/Makefile index cad52e3..6b8ef08 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,11 +3,13 @@ include ../config.mk PREFIX ?= /usr/local INSTALL ?= install +BUILD_DATE ?= $(shell LC_ALL=C date '+%b %e %Y') + CC = gcc CROSS_CC = $(CROSS_COMPILE)$(CC) HOST_CC = $(HOST_COMPILE)$(CC) -CPPFLAGS += -DVERSION=\"$(VERSION)\" -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L +CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L CFLAGS += -W -Wall -O2 -pedantic -std=c99 LIBS += -lusb diff --git a/src/mangen.c b/src/mangen.c index d2bf89a..6f5a2fa 100644 --- a/src/mangen.c +++ b/src/mangen.c @@ -25,6 +25,10 @@ #define INFO NAME " \\- Open Free Fiasco Firmware Flasher, version " VERSION #define DESCRIPTION "0xFFFF is Open Free Fiasco Firmware Flasher for Maemo devices. It support generating and unpacking FIASCO images on local computer. Useful for editing Maemo firmware package for future flash. It support via USB flashing any image type to Maemo device and also \"cold\" flashing which means flashing dead device with erased bootloader. There is support for booting kernel via USB without flashing to NAND and also changing configuration of Maemo device (enable/disable R&D mode, changing HW revision strings, ...).\n\n0xFFFF is alternative tool to proprietary Nokia flasher-3.5 and fiasco-gen. 0xFFFF generate compatible FIASCO images and also accept FIASCO images generated by Nokia fiasco-gen." +#ifndef BUILD_DATE +#define BUILD_DATE __DATE__ +#endif + int main() { FILE * pipe; @@ -34,7 +38,7 @@ int main() { if ( ! pipe ) return 1; - puts(".TH " NAME " 1 \"" __DATE__ "\" \"" NAME " " VERSION "\""); + puts(".TH " NAME " 1 \"" BUILD_DATE "\" \"" NAME " " VERSION "\""); puts(""); puts(".SH NAME"); -- cgit v1.2.3 From 3f406cc9addbe7155bc05518715bdd292154fbb6 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 24 Jun 2015 18:30:07 +0200 Subject: fiasco: Fix checking for header of next image --- src/fiasco.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fiasco.c b/src/fiasco.c index cb890e0..79bde74 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -122,7 +122,7 @@ struct fiasco * fiasco_alloc_from_file(const char * file) { READ_OR_RETURN(fiasco, buf, 7); /* Header of next image */ - if ( ! buf[0] == 0x54 && buf[2] == 0x2E && buf[3] == 0x19 && buf[4] == 0x01 && buf[5] == 0x01 && buf[6] == 0x00 ) { + if ( ! ( buf[0] == 0x54 && buf[2] == 0x2E && buf[3] == 0x19 && buf[4] == 0x01 && buf[5] == 0x01 && buf[6] == 0x00 ) ) { ERROR("Invalid next image header"); return fiasco; } -- cgit v1.2.3 From 75ea4bfbd8218a11370f72ececa864b09eb1c2a3 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 24 Jun 2015 19:09:34 +0200 Subject: image: Fix condition for detecting device --- src/image.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/image.c b/src/image.c index 5424f9a..0b16de1 100644 --- a/src/image.c +++ b/src/image.c @@ -76,7 +76,7 @@ static void image_missing_values_from_name(struct image * image, const char * na image->type = image_type_from_string(type); free(type); - if ( ! image->devices || image->devices->device || image->devices->device == DEVICE_ANY ) { + if ( ! image->devices || ! image->devices->device || image->devices->device == DEVICE_ANY ) { new_device = device_from_string(device); if ( new_device ) { if ( ! image->devices ) image->devices = calloc(1, sizeof(struct device_list)); -- cgit v1.2.3 From 05f7048580dcd74c8d1145fc207372cce36d991b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 12:33:22 +0100 Subject: local: Call nanddump without -i param Param -i was removed in recent versions of mtd-utils. And in older versions it did nothing. --- src/local.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/local.c b/src/local.c index 71d6d9d..3b2b1cd 100644 --- a/src/local.c +++ b/src/local.c @@ -227,13 +227,13 @@ static int local_nanddump(const char * file, int mtd, int offset, int length) { return 1; } - size = snprintf(NULL, 0, "nanddump -i -o -b -s %d -l %d -f %s /dev/mtd%dro", offset, length, file, mtd); + size = snprintf(NULL, 0, "nanddump -o -b -s %d -l %d -f %s /dev/mtd%dro", offset, length, file, mtd); command = malloc(size+1); if ( ! command ) return 1; - snprintf(command, size+1, "nanddump -i -o -b -s %d -l %d -f %s /dev/mtd%dro", offset, length, file, mtd); + snprintf(command, size+1, "nanddump -o -b -s %d -l %d -f %s /dev/mtd%dro", offset, length, file, mtd); ret = system(command); -- cgit v1.2.3 From ccef3c6daa6f72326df429bfb3821860f036e206 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 12:53:57 +0100 Subject: README: Update 0xFFFF website --- README | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README b/README index 8bbecca..76367fa 100644 --- a/README +++ b/README @@ -29,10 +29,7 @@ bootloaders to break this tool. USE IT AT YOUR OWN RISK. PLEASE. Read carefully all the documentation inside doc/* for more information before building or using the flasher to avoid questions, problems or so. -Feel free to join to the mailing list and visit the homepage for more info: - - 0xffff@lists.nopcode.org - http://www.nopcode.org/0xFFFF + https://github.com/pali/0xFFFF -----------8<--------------------------------------------------------------- -- cgit v1.2.3 From 79af7395bf1c95b19e62e26d9f8525a1d6bc79b6 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 13:27:17 +0100 Subject: fiasco: In simulate mode do not close invalid not open fd --- src/fiasco.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/fiasco.c b/src/fiasco.c index 79bde74..0a3f509 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -443,7 +443,9 @@ int fiasco_write_to_file(struct fiasco * fiasco, const char * file) { } - close(fd); + if ( ! simulate ) + close(fd); + printf("\nDone\n\n"); return 0; -- cgit v1.2.3 From 1ee86c05f4520e859a335ac7e84488aba9033b09 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 13:29:28 +0100 Subject: fiasco: Fix memory leak for device_hwrevs_bufs variable --- src/fiasco.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/fiasco.c b/src/fiasco.c index 0a3f509..7917385 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -38,7 +38,8 @@ #define FIASCO_WRITE_ERROR(file, fd, ...) do { ERROR_INFO_STR(file, __VA_ARGS__); if ( fd >= 0 ) close(fd); return -1; } while (0) #define READ_OR_FAIL(fiasco, buf, size) do { if ( read(fiasco->fd, buf, size) != size ) { FIASCO_READ_ERROR(fiasco, "Cannot read %d bytes", size); } } while (0) #define READ_OR_RETURN(fiasco, buf, size) do { if ( read(fiasco->fd, buf, size) != size ) return fiasco; } while (0) -#define WRITE_OR_FAIL(file, fd, buf, size) do { if ( ! simulate ) { if ( write(fd, buf, size) != (ssize_t)size ) { FIASCO_WRITE_ERROR(file, fd, "Cannot write %d bytes", size); } } } while (0) +#define WRITE_OR_FAIL_FREE(file, fd, buf, size, var) do { if ( ! simulate ) { if ( write(fd, buf, size) != (ssize_t)size ) { free(var); FIASCO_WRITE_ERROR(file, fd, "Cannot write %d bytes", size); } } } while (0) +#define WRITE_OR_FAIL(file, fd, buf, size) WRITE_OR_FAIL_FREE(file, fd, buf, size, NULL) struct fiasco * fiasco_alloc_empty(void) { @@ -351,12 +352,6 @@ int fiasco_write_to_file(struct fiasco * fiasco, const char * file) { type = image_type_to_string(image->type); - device_hwrevs_bufs = device_list_alloc_to_bufs(image->devices); - - device_count = 0; - if ( device_hwrevs_bufs && device_hwrevs_bufs[0] ) - for ( ; device_hwrevs_bufs[device_count]; ++device_count ); - if ( ! type ) FIASCO_WRITE_ERROR(file, fd, "Unknown image type"); @@ -366,10 +361,16 @@ int fiasco_write_to_file(struct fiasco * fiasco, const char * file) { if ( image->layout && strlen(image->layout) > UINT8_MAX ) FIASCO_WRITE_ERROR(file, fd, "Image layout is too long"); + device_hwrevs_bufs = device_list_alloc_to_bufs(image->devices); + + device_count = 0; + if ( device_hwrevs_bufs && device_hwrevs_bufs[0] ) + for ( ; device_hwrevs_bufs[device_count]; ++device_count ); + printf("Writing image header...\n"); /* signature */ - WRITE_OR_FAIL(file, fd, "T", 1); + WRITE_OR_FAIL_FREE(file, fd, "T", 1, device_hwrevs_bufs); /* number of subsections */ length8 = device_count+1; @@ -377,41 +378,40 @@ int fiasco_write_to_file(struct fiasco * fiasco, const char * file) { ++length8; if ( image->layout ) ++length8; - WRITE_OR_FAIL(file, fd, &length8, 1); + WRITE_OR_FAIL_FREE(file, fd, &length8, 1, device_hwrevs_bufs); /* unknown */ - WRITE_OR_FAIL(file, fd, "\x2e\x19\x01\x01\x00", 5); + WRITE_OR_FAIL_FREE(file, fd, "\x2e\x19\x01\x01\x00", 5, device_hwrevs_bufs); /* checksum */ hash = htons(image->hash); - WRITE_OR_FAIL(file, fd, &hash, 2); + WRITE_OR_FAIL_FREE(file, fd, &hash, 2, device_hwrevs_bufs); /* image type name */ memset(buf, 0, 12); strncpy((char *)buf, type, 12); - WRITE_OR_FAIL(file, fd, buf, 12); + WRITE_OR_FAIL_FREE(file, fd, buf, 12, device_hwrevs_bufs); /* image size */ size = htonl(image->size); - WRITE_OR_FAIL(file, fd, &size, 4); + WRITE_OR_FAIL_FREE(file, fd, &size, 4, device_hwrevs_bufs); /* unknown */ - WRITE_OR_FAIL(file, fd, "\x00\x00\x00\x00", 4); + WRITE_OR_FAIL_FREE(file, fd, "\x00\x00\x00\x00", 4, device_hwrevs_bufs); /* append version subsection */ if ( image->version ) { - WRITE_OR_FAIL(file, fd, "1", 1); /* 1 - version */ + WRITE_OR_FAIL_FREE(file, fd, "1", 1, device_hwrevs_bufs); /* 1 - version */ length8 = strlen(image->version)+1; - WRITE_OR_FAIL(file, fd, &length8, 1); - WRITE_OR_FAIL(file, fd, image->version, length8); + WRITE_OR_FAIL_FREE(file, fd, &length8, 1, device_hwrevs_bufs); + WRITE_OR_FAIL_FREE(file, fd, image->version, length8, device_hwrevs_bufs); } /* append device & hwrevs subsection */ for ( i = 0; i < device_count; ++i ) { - WRITE_OR_FAIL(file, fd, "2", 1); /* 2 - device & hwrevs */ - WRITE_OR_FAIL(file, fd, &device_hwrevs_bufs[i][0], 1); - WRITE_OR_FAIL(file, fd, device_hwrevs_bufs[i]+1, ((uint8_t *)(device_hwrevs_bufs[i]))[0]); - /* FIXME: memory leak: device_hwrevs_bufs */ + WRITE_OR_FAIL_FREE(file, fd, "2", 1, device_hwrevs_bufs); /* 2 - device & hwrevs */ + WRITE_OR_FAIL_FREE(file, fd, &device_hwrevs_bufs[i][0], 1, device_hwrevs_bufs); + WRITE_OR_FAIL_FREE(file, fd, device_hwrevs_bufs[i]+1, ((uint8_t *)(device_hwrevs_bufs[i]))[0], device_hwrevs_bufs); } free(device_hwrevs_bufs); -- cgit v1.2.3 From b7f80de25d701ebbfbcb03ac73c2bb4de0979d90 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 14:29:18 +0100 Subject: all: Check for return value of lseek --- src/cal.c | 3 ++- src/disk.c | 5 ++++- src/fiasco.c | 5 ++++- src/image.c | 38 +++++++++++++++++++++++++++++++++----- src/main.c | 5 ++++- 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/cal.c b/src/cal.c index 99de241..102ffab 100644 --- a/src/cal.c +++ b/src/cal.c @@ -92,7 +92,8 @@ int cal_init_file(const char * file, struct cal ** cal_out) { blksize = lseek(fd, 0, SEEK_END); if ( blksize == (off_t)-1 ) goto err; - lseek(fd, 0, SEEK_SET); + if ( lseek(fd, 0, SEEK_SET) == (off_t)-1 ) + goto err; #endif if ( blksize > SSIZE_MAX ) goto err; diff --git a/src/disk.c b/src/disk.c index 7370f13..f8a51f8 100644 --- a/src/disk.c +++ b/src/disk.c @@ -185,7 +185,10 @@ int disk_dump_dev(int fd, const char * file) { return -1; } - lseek(fd, 0, SEEK_SET); + if ( lseek(fd, 0, SEEK_SET) == (off_t)-1 ) { + ERROR_INFO("Cannot seek to begin of block device"); + return -1; + } #endif diff --git a/src/fiasco.c b/src/fiasco.c index 7917385..ba6d262 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -218,6 +218,8 @@ struct fiasco * fiasco_alloc_from_file(const char * file) { READ_OR_RETURN(fiasco, buf, 1); offset = lseek(fiasco->fd, 0, SEEK_CUR); + if ( offset == (off_t)-1 ) + FIASCO_READ_ERROR(fiasco, "Cannot get offset of file"); VERBOSE(" version: %s\n", version); VERBOSE(" device: %s\n", device); @@ -231,7 +233,8 @@ struct fiasco * fiasco_alloc_from_file(const char * file) { fiasco_add_image(fiasco, image); - lseek(fiasco->fd, offset+length, SEEK_SET); + if ( lseek(fiasco->fd, offset+length, SEEK_SET) == (off_t)-1 ) + FIASCO_READ_ERROR(fiasco, "Cannot seek to next image in file"); } diff --git a/src/image.c b/src/image.c index 0b16de1..0daebf3 100644 --- a/src/image.c +++ b/src/image.c @@ -247,6 +247,7 @@ static struct image * image_alloc(void) { struct image * image_alloc_from_file(const char * file, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout) { + off_t offset; struct image * image = image_alloc(); if ( ! image ) return NULL; @@ -259,11 +260,26 @@ struct image * image_alloc_from_file(const char * file, const char * type, const return NULL; } - image->size = lseek(image->fd, 0, SEEK_END); + offset = lseek(image->fd, 0, SEEK_END); + if ( offset == (off_t)-1 ) { + ERROR_INFO("Cannot seek to end of file %s", file); + close(image->fd); + free(image); + return NULL; + } + + image->size = offset; image->offset = 0; image->cur = 0; image->orig_filename = strdup(file); - lseek(image->fd, 0, SEEK_SET); + + if ( lseek(image->fd, 0, SEEK_SET) == (off_t)-1 ) { + ERROR_INFO("Cannot seek to begin of file %s", file); + close(image->fd); + free(image->orig_filename); + free(image); + return NULL; + } if ( image_append(image, type, device, hwrevs, version, layout) < 0 ) return NULL; @@ -331,17 +347,22 @@ void image_free(struct image * image) { void image_seek(struct image * image, size_t whence) { + off_t offset; + if ( whence > image->size ) return; if ( whence >= image->size - image->align ) { - lseek(image->fd, image->size - image->align - 1, SEEK_SET); + offset = lseek(image->fd, image->size - image->align - 1, SEEK_SET); image->acur = whence - ( image->size - image->align ); } else { - lseek(image->fd, image->offset + whence, SEEK_SET); + offset = lseek(image->fd, image->offset + whence, SEEK_SET); image->acur = 0; } + if ( offset == (off_t)-1 ) + ERROR_INFO("Seek in file %s failed", (image->orig_filename ? image->orig_filename : "(unknown)")); + IMAGE_STORE_CUR(image); } @@ -350,6 +371,7 @@ size_t image_read(struct image * image, void * buf, size_t count) { size_t cur; ssize_t ret; + off_t offset; size_t new_count = 0; size_t ret_count = 0; @@ -376,7 +398,13 @@ size_t image_read(struct image * image, void * buf, size_t count) { if ( ret_count == count ) return ret_count; - cur = lseek(image->fd, 0, SEEK_CUR) - image->offset; + offset = lseek(image->fd, 0, SEEK_CUR); + if ( offset == (off_t)-1 ) { + ERROR_INFO("Cannot get offset of file %s", (image->orig_filename ? image->orig_filename : "(unknown)")); + return 0; + } + + cur = offset - image->offset; if ( image->align && cur == image->size - image->align && image->acur < image->align ) { diff --git a/src/main.c b/src/main.c index b224e5c..a6994b8 100644 --- a/src/main.c +++ b/src/main.c @@ -212,7 +212,10 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) { ERROR_INFO("Cannot get size of file %s", layout_file); exit(1); } - lseek(fd, 0, SEEK_SET); + if ( lseek(fd, 0, SEEK_SET) == (off_t)-1 ) { + ERROR_INFO("Cannot seek to begin of file %s", layout_file); + exit(1); + } layout = malloc(len+1); if ( ! layout ) { ALLOC_ERROR(); -- cgit v1.2.3 From b48b9b558c89783f812e6afbcdecb9d2a2153ad4 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 14:31:28 +0100 Subject: fiasco: Remove wrong checks for 'name' and 'swver' Members 'name' and 'swver' of fiasco are arrays, not pointers --- src/fiasco.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fiasco.c b/src/fiasco.c index ba6d262..2d5a253 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -289,10 +289,10 @@ int fiasco_write_to_file(struct fiasco * fiasco, const char * file) { if ( ! fiasco->first ) FIASCO_WRITE_ERROR(file, fd, "Nothing to write"); - if ( fiasco->name && strlen(fiasco->name)+1 > UINT8_MAX ) + if ( strlen(fiasco->name)+1 > UINT8_MAX ) FIASCO_WRITE_ERROR(file, fd, "Fiasco name string is too long"); - if ( fiasco->swver && strlen(fiasco->swver)+1 > UINT8_MAX ) + if ( strlen(fiasco->swver)+1 > UINT8_MAX ) FIASCO_WRITE_ERROR(file, fd, "SW version string is too long"); if ( ! simulate ) { -- cgit v1.2.3 From 3c1ab9934d91dfe04ecc29146625ffd3e900a38e Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 14:32:43 +0100 Subject: main: Check for SW rel version length --- src/main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main.c b/src/main.c index a6994b8..cea36f7 100644 --- a/src/main.c +++ b/src/main.c @@ -834,9 +834,16 @@ int main(int argc, char **argv) { char * swver = strchr(fiasco_gen_arg, '%'); if ( swver ) *(swver++) = 0; + if ( swver && strlen(swver) >= sizeof(fiasco_out->swver) ) { + ERROR("SW rel version is too long"); + ret = 1; + goto clean; + } fiasco_out = fiasco_alloc_empty(); if ( ! fiasco_out ) { ERROR("Cannot write images to fiasco file %s", fiasco_gen_arg); + ret = 1; + goto clean; } else { if ( swver ) strcpy(fiasco_out->swver, swver); -- cgit v1.2.3 From c258289b7448ad0ddb04dbd4367323ed8bb8ba1a Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 14:33:30 +0100 Subject: image: Check for return value fo image_read() in image_type_from_data() --- src/image.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/image.c b/src/image.c index 0daebf3..23d79d8 100644 --- a/src/image.c +++ b/src/image.c @@ -511,40 +511,41 @@ static const char * image_types[] = { enum image_type image_type_from_data(struct image * image) { unsigned char buf[512]; + size_t size; memset(buf, 0, sizeof(buf)); image_seek(image, 0); - image_read(image, buf, sizeof(buf)); + size = image_read(image, buf, sizeof(buf)); - if ( memcmp(buf+52, "2NDAPE", 6) == 0 ) + if ( size >= 58 && memcmp(buf+52, "2NDAPE", 6) == 0 ) return IMAGE_2ND; - else if ( memcmp(buf+20, "2ND", 3) == 0 ) + else if ( size >= 23 && memcmp(buf+20, "2ND", 3) == 0 ) return IMAGE_2ND; - else if ( memcmp(buf+4, "NOLOScnd", 8) == 0 ) + else if ( size >= 8 && memcmp(buf+4, "NOLOScnd", 8) == 0 ) return IMAGE_SECONDARY; - else if ( memcmp(buf+20, "X-LOADER", 8) == 0 ) + else if ( size >= 28 && memcmp(buf+20, "X-LOADER", 8) == 0 ) return IMAGE_XLOADER; - else if ( memcmp(buf+12, "NOLOXldr", 8) == 0 ) + else if ( size >= 20 && memcmp(buf+12, "NOLOXldr", 8) == 0 ) return IMAGE_XLOADER; - else if ( memcmp(buf+4, "NOLOXldr", 8) == 0 ) + else if ( size >= 12 && memcmp(buf+4, "NOLOXldr", 8) == 0 ) return IMAGE_2ND; - else if ( memcmp(buf+36, "\x18\x28\x6f\x01", 4) == 0 ) /* ARM Linux kernel magic number */ + else if ( size >= 40 && memcmp(buf+36, "\x18\x28\x6f\x01", 4) == 0 ) /* ARM Linux kernel magic number */ return IMAGE_KERNEL; - else if ( memcmp(buf+1, "\x00\x00\xea", 3) == 0 ) /* ARM U-Boot - instruction branch */ + else if ( size >= 4 && memcmp(buf+1, "\x00\x00\xea", 3) == 0 ) /* ARM U-Boot - instruction branch */ return IMAGE_KERNEL; - else if ( memcmp(buf, "UBI#", 4) == 0 ) /* UBI EC header */ + else if ( size >= 4 && memcmp(buf, "UBI#", 4) == 0 ) /* UBI EC header */ return IMAGE_ROOTFS; - else if ( memcmp(buf+510, "\x55\xaa", 2) == 0 ) /* FAT boot sector signature */ + else if ( size >= 512 && memcmp(buf+510, "\x55\xaa", 2) == 0 ) /* FAT boot sector signature */ return IMAGE_MMC; - else if ( memcmp(buf, "\xb0\x00\x01\x03\x9d\x00\x00\x00", 8) == 0 ) + else if ( size >= 8 && memcmp(buf, "\xb0\x00\x01\x03\x9d\x00\x00\x00", 8) == 0 ) return IMAGE_CMT_2ND; - else if ( memcmp(buf, "\xb1\x00\x00\x00\x82\x00\x00\x00", 8) == 0 ) + else if ( size >= 8 && memcmp(buf, "\xb1\x00\x00\x00\x82\x00\x00\x00", 8) == 0 ) return IMAGE_CMT_ALGO; - else if ( memcmp(buf, "\xb2\x00\x00\x01\x44\x00\x00\x00", 8) == 0 ) + else if ( size >= 8 && memcmp(buf, "\xb2\x00\x00\x01\x44\x00\x00\x00", 8) == 0 ) return IMAGE_CMT_MCUSW; - else if ( memcmp(buf, "\x45\x3d\xcd\x28", 4) == 0 ) /* CRAMFS MAGIC */ + else if ( size >= 4 && memcmp(buf, "\x45\x3d\xcd\x28", 4) == 0 ) /* CRAMFS MAGIC */ return IMAGE_INITFS; - else if ( memcmp(buf, "\x85\x19", 2) == 0 ) { /* JFFS2 MAGIC */ + else if ( size >= 2 && memcmp(buf, "\x85\x19", 2) == 0 ) { /* JFFS2 MAGIC */ if ( image->size < 0x300000 ) return IMAGE_INITFS; else -- cgit v1.2.3 From f36a762168726fc79529906b5a14a65836f7f609 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 17:26:23 +0100 Subject: fiasco: Fix code for append device & hwrevs subsection Fix possible use-after-free in WRITE_OR_FAIL_FREE() macro --- src/fiasco.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/fiasco.c b/src/fiasco.c index 2d5a253..caa0a9f 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -405,7 +405,7 @@ int fiasco_write_to_file(struct fiasco * fiasco, const char * file) { /* append version subsection */ if ( image->version ) { WRITE_OR_FAIL_FREE(file, fd, "1", 1, device_hwrevs_bufs); /* 1 - version */ - length8 = strlen(image->version)+1; + length8 = strlen(image->version)+1; /* +1 for NULL term */ WRITE_OR_FAIL_FREE(file, fd, &length8, 1, device_hwrevs_bufs); WRITE_OR_FAIL_FREE(file, fd, image->version, length8, device_hwrevs_bufs); } @@ -413,15 +413,16 @@ int fiasco_write_to_file(struct fiasco * fiasco, const char * file) { /* append device & hwrevs subsection */ for ( i = 0; i < device_count; ++i ) { WRITE_OR_FAIL_FREE(file, fd, "2", 1, device_hwrevs_bufs); /* 2 - device & hwrevs */ - WRITE_OR_FAIL_FREE(file, fd, &device_hwrevs_bufs[i][0], 1, device_hwrevs_bufs); - WRITE_OR_FAIL_FREE(file, fd, device_hwrevs_bufs[i]+1, ((uint8_t *)(device_hwrevs_bufs[i]))[0], device_hwrevs_bufs); + length8 = ((uint8_t *)(device_hwrevs_bufs[i]))[0]; + WRITE_OR_FAIL_FREE(file, fd, &length8, 1, device_hwrevs_bufs); + WRITE_OR_FAIL_FREE(file, fd, device_hwrevs_bufs[i]+1, length8, device_hwrevs_bufs); } free(device_hwrevs_bufs); /* append layout subsection */ if ( image->layout ) { - length8 = strlen(image->layout); WRITE_OR_FAIL(file, fd, "3", 1); /* 3 - layout */ + length8 = strlen(image->layout); WRITE_OR_FAIL(file, fd, &length8, 1); WRITE_OR_FAIL(file, fd, image->layout, length8); } -- cgit v1.2.3 From 055621d3a2aefceb6427a4a67fd00be146fe7193 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Mon, 11 Jan 2016 08:24:37 +0100 Subject: all: free() is defined in stdlib.h Without including newer gcc releases throw warnings about undefined free(). --- src/disk.c | 1 + src/operations.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/disk.c b/src/disk.c index f8a51f8..956d67b 100644 --- a/src/disk.c +++ b/src/disk.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include diff --git a/src/operations.c b/src/operations.c index 59bf212..2d26ad5 100644 --- a/src/operations.c +++ b/src/operations.c @@ -17,6 +17,8 @@ */ +#include + #include "global.h" #include "device.h" #include "usb-device.h" -- cgit v1.2.3 From d4342a8cdffb15d3863154f1b9f7fcbee0a6e3ee Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Mon, 11 Jan 2016 08:25:35 +0100 Subject: disk: dirent.h is required for DIR Without include newer gcc releases throw warnings about undefined "DIR". --- src/disk.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/disk.c b/src/disk.c index 956d67b..daa3711 100644 --- a/src/disk.c +++ b/src/disk.c @@ -28,6 +28,7 @@ #ifdef __linux__ #include #include +#include #endif #include "disk.h" -- cgit v1.2.3 From 5f0897d372064739709af61aec0a136b37fd55be Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 13 Jan 2016 22:28:36 +0100 Subject: doc: Remove nanddump param -i also from doc --- doc/dumping | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/dumping b/doc/dumping index 2d7ce25..4a629cd 100644 --- a/doc/dumping +++ b/doc/dumping @@ -30,10 +30,9 @@ Technical details: For dumping mtd partition is used tool nanddump. Here is example how to dump kernel image without padding to file zImage: - $ nanddump -i -o -b -s 0x00000800 -l 0x001FF800 -f zImage /dev/mtd2 + $ nanddump -o -b -s 0x00000800 -l 0x001FF800 -f zImage /dev/mtd2 Params means: --i - "Ignore errors" -o - "Omit oob data" -b - "Omit bad blocks" -s - "Start address" -- cgit v1.2.3 From 4a4005b46dceb174211d2de1764133c26f5b7128 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Fri, 15 Jan 2016 15:43:24 +0100 Subject: libusb-sniff: add header for getenv() --- src/libusb-sniff.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libusb-sniff.c b/src/libusb-sniff.c index ce66772..eeb6ec6 100644 --- a/src/libusb-sniff.c +++ b/src/libusb-sniff.c @@ -23,6 +23,7 @@ #define _GNU_SOURCE #include +#include #include #include -- cgit v1.2.3 From 57b0c6d0c47d6495392161ddd9030b2a2d5c9e6c Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Fri, 15 Jan 2016 15:44:13 +0100 Subject: libusb-sniff: avoid usb.h include usb.h is only required for the definition of usb_dev_handle, which is never used, so it can be defined as anonymous struct instead. This has the advantage, that libusb is not required on the system compiling the library. --- src/libusb-sniff.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libusb-sniff.c b/src/libusb-sniff.c index eeb6ec6..a901556 100644 --- a/src/libusb-sniff.c +++ b/src/libusb-sniff.c @@ -25,7 +25,9 @@ #include #include #include -#include + +struct usb_dev_handle; +typedef struct usb_dev_handle usb_dev_handle; static char to_ascii(char c) { -- cgit v1.2.3 From 6482e4d2b7a2cb641b7d9461db11d3b0578f8e70 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 23 Jan 2016 16:03:01 +0100 Subject: libusb-sniff: Use POSIX.1-2003 workaround for dlsym() assignment which is compatible with C99 --- src/libusb-sniff.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libusb-sniff.c b/src/libusb-sniff.c index a901556..ecf4b2c 100644 --- a/src/libusb-sniff.c +++ b/src/libusb-sniff.c @@ -71,7 +71,7 @@ int usb_bulk_write(usb_dev_handle * dev, int ep, const char * bytes, int size, i static int (*real_usb_bulk_write)(usb_dev_handle * dev, int ep, const char * bytes, int size, int timeout) = NULL; if ( ! real_usb_bulk_write ) - real_usb_bulk_write = dlsym(RTLD_NEXT, "usb_bulk_write"); + *(void **)(&real_usb_bulk_write) = dlsym(RTLD_NEXT, "usb_bulk_write"); if ( ! getenv("USBSNIFF_SKIP_WRITE") ) { @@ -95,7 +95,7 @@ int usb_bulk_read(usb_dev_handle * dev, int ep, char * bytes, int size, int time int ret; if ( ! real_usb_bulk_read ) - real_usb_bulk_read = dlsym(RTLD_NEXT, "usb_bulk_read"); + *(void **)(&real_usb_bulk_read) = dlsym(RTLD_NEXT, "usb_bulk_read"); ret = real_usb_bulk_read(dev, ep, bytes, size, timeout); @@ -123,7 +123,7 @@ int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value int ret; if ( ! real_usb_control_msg ) - real_usb_control_msg = dlsym(RTLD_NEXT, "usb_control_msg"); + *(void **)(&real_usb_control_msg) = dlsym(RTLD_NEXT, "usb_control_msg"); if ( requesttype == 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) { @@ -162,7 +162,7 @@ int usb_set_configuration(usb_dev_handle *dev, int configuration) { static int (*real_usb_set_configuration)(usb_dev_handle *dev, int configuration) = NULL; if ( ! real_usb_set_configuration ) - real_usb_set_configuration = dlsym(RTLD_NEXT, "usb_set_configuration"); + *(void **)(&real_usb_set_configuration) = dlsym(RTLD_NEXT, "usb_set_configuration"); printf("\n==== usb_set_configuration (configuration=%d) ====\n", configuration); @@ -175,7 +175,7 @@ int usb_claim_interface(usb_dev_handle *dev, int interface) { static int (*real_usb_claim_interface)(usb_dev_handle *dev, int interface) = NULL; if ( ! real_usb_claim_interface ) - real_usb_claim_interface = dlsym(RTLD_NEXT, "usb_claim_interface"); + *(void **)(&real_usb_claim_interface) = dlsym(RTLD_NEXT, "usb_claim_interface"); printf("\n==== usb_claim_interface (interface=%d) ====\n", interface); @@ -188,7 +188,7 @@ int usb_set_altinterface(usb_dev_handle *dev, int alternate) { static int (*real_usb_set_altinterface)(usb_dev_handle *dev, int alternate) = NULL; if ( ! real_usb_set_altinterface ) - real_usb_set_altinterface = dlsym(RTLD_NEXT, "usb_set_altinterface"); + *(void **)(&real_usb_set_altinterface) = dlsym(RTLD_NEXT, "usb_set_altinterface"); printf("\n==== usb_set_altinterface (alternate=%d) ====\n", alternate); -- cgit v1.2.3 From d5028a12ca539638a6ae5d923e7dba0e4f30a45a Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Mon, 11 Jan 2016 08:41:58 +0100 Subject: all: Switch to libusb-1.0 libusb 0.1 is deprecated. This updates 0xFFFF to use the newer libusb 1.0 API instead. --- src/Makefile | 2 +- src/cold-flash.c | 85 +++++++++++++++++--------------- src/disk.c | 14 ++++-- src/mkii.c | 29 +++++++---- src/nolo.c | 60 +++++++++++++---------- src/usb-device.c | 144 ++++++++++++++++++++++--------------------------------- src/usb-device.h | 4 +- 7 files changed, 170 insertions(+), 168 deletions(-) diff --git a/src/Makefile b/src/Makefile index 6b8ef08..6682436 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,7 +11,7 @@ HOST_CC = $(HOST_COMPILE)$(CC) CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L CFLAGS += -W -Wall -O2 -pedantic -std=c99 -LIBS += -lusb +LIBS += -lusb-1.0 DEPENDS = Makefile ../config.mk diff --git a/src/cold-flash.c b/src/cold-flash.c index eb2ca67..1c9d95b 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -21,10 +21,10 @@ #include #include #include -#include -#include "global.h" +#include +#include "global.h" #include "cold-flash.h" #include "image.h" #include "usb-device.h" @@ -168,37 +168,41 @@ struct xloader_msg xloader_msg_create(uint32_t type, struct image * image) { } -static int read_asic(usb_dev_handle * udev, uint8_t * asic_buffer, int size, int asic_size) { +static int read_asic(libusb_device_handle * udev, uint8_t * asic_buffer, int size, int asic_size) { - int ret; + int ret, transferred; printf("Waiting for ASIC ID...\n"); - ret = usb_bulk_read(udev, READ_DEV, (char *)asic_buffer, size, READ_TIMEOUT); - if ( ret != asic_size ) + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)asic_buffer, size, &transferred, READ_TIMEOUT); + if ( ret < 0 ) + ERROR_RETURN("Cannot read ASIC ID", -1); + if ( transferred != asic_size ) ERROR_RETURN("Invalid size of ASIC ID", -1); return 0; } -static int send_2nd(usb_dev_handle * udev, struct image * image) { +static int send_2nd(libusb_device_handle * udev, struct image * image) { uint8_t buffer[1024]; uint32_t need, readed; - int ret; + int ret, transferred; printf("Sending OMAP peripheral boot message...\n"); - ret = usb_bulk_write(udev, WRITE_DEV, (char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), WRITE_TIMEOUT); - SLEEP(5000); - if ( ret != sizeof(omap_peripheral_msg) ) + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != sizeof(omap_peripheral_msg) ) ERROR_RETURN("Sending OMAP peripheral boot message failed", -1); - printf("Sending 2nd X-Loader image size...\n"); - ret = usb_bulk_write(udev, WRITE_DEV, (char *)&image->size, 4, WRITE_TIMEOUT); SLEEP(5000); - if ( ret != 4 ) + + printf("Sending 2nd X-Loader image size...\n"); + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&image->size, 4, &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != 4 ) ERROR_RETURN("Sending 2nd X-Loader image size failed", -1); + SLEEP(5000); + printf("Sending 2nd X-Loader image...\n"); printf_progressbar(0, image->size); image_seek(image, 0); @@ -210,35 +214,37 @@ static int send_2nd(usb_dev_handle * udev, struct image * image) { ret = image_read(image, buffer, need); if ( ret == 0 ) break; - if ( usb_bulk_write(udev, WRITE_DEV, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) + if ( libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed", -1); - readed += ret; + if ( ret != transferred ) + PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed (incomplete bulk transfer)", -1); + readed += transferred; printf_progressbar(readed, image->size); } - SLEEP(50000); + SLEEP(50000); return 0; } -static int send_secondary(usb_dev_handle * udev, struct image * image) { +static int send_secondary(libusb_device_handle * udev, struct image * image) { struct xloader_msg init_msg; uint8_t buffer[1024]; uint32_t need, readed; - int ret; + int ret, transferred; init_msg = xloader_msg_create(XLOADER_MSG_TYPE_SEND, image); printf("Sending X-Loader init message...\n"); - ret = usb_bulk_write(udev, WRITE_DEV, (char *)&init_msg, sizeof(init_msg), WRITE_TIMEOUT); - SLEEP(5000); - if ( ret != sizeof(init_msg) ) + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&init_msg, sizeof(init_msg), &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != sizeof(init_msg) ) ERROR_RETURN("Sending X-Loader init message failed", -1); printf("Waiting for X-Loader response...\n"); - ret = usb_bulk_read(udev, READ_DEV, (char *)&buffer, 4, READ_TIMEOUT); /* 4 bytes - dummy value */ - if ( ret != 4 ) + SLEEP(5000); + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ + if ( ret < 0 || transferred != 4 ) ERROR_RETURN("No response", -1); printf("Sending Secondary image...\n"); @@ -252,25 +258,27 @@ static int send_secondary(usb_dev_handle * udev, struct image * image) { ret = image_read(image, buffer, need); if ( ret == 0 ) break; - if ( usb_bulk_write(udev, WRITE_DEV, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) + if ( libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) PRINTF_ERROR_RETURN("Sending Secondary image failed", -1); - readed += ret; + if ( ret != transferred ) + PRINTF_ERROR_RETURN("Sending Secondary image failed (incomplete bulk transfer)", -1); + readed += transferred; printf_progressbar(readed, image->size); } - SLEEP(5000); printf("Waiting for X-Loader response...\n"); - ret = usb_bulk_read(udev, READ_DEV, (char *)&buffer, 4, READ_TIMEOUT); /* 4 bytes - dummy value */ - if ( ret != 4 ) + SLEEP(5000); + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ + if ( ret < 0 || transferred != 4 ) ERROR_RETURN("No response", -1); return 0; } -static int ping_timeout(usb_dev_handle * udev) { +static int ping_timeout(libusb_device_handle * udev) { - int ret; + int ret, transferred; int pong = 0; int try_ping = 10; @@ -280,16 +288,16 @@ static int ping_timeout(usb_dev_handle * udev) { int try_read = 4; printf("Sending X-Loader ping message\n"); - ret = usb_bulk_write(udev, WRITE_DEV, (char *)&ping_msg, sizeof(ping_msg), WRITE_TIMEOUT); - if ( ret != sizeof(ping_msg) ) + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&ping_msg, sizeof(ping_msg), &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != sizeof(ping_msg) ) ERROR_RETURN("Sending X-Loader ping message failed", -1); printf("Waiting for X-Loader pong response...\n"); while ( try_read > 0 ) { uint32_t ping_read; - ret = usb_bulk_read(udev, READ_DEV, (char *)&ping_read, sizeof(ping_read), READ_TIMEOUT); - if ( ret == sizeof(ping_read) ) { + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&ping_read, sizeof(ping_read), &transferred, READ_TIMEOUT); + if ( ret == 0 && transferred == sizeof(ping_read) ) { printf("Got it\n"); pong = 1; break; @@ -401,12 +409,11 @@ int cold_flash(struct usb_device_info * dev, struct image * x2nd, struct image * int leave_cold_flash(struct usb_device_info * dev) { - int ret; + int ret, transferred; printf("Sending OMAP memory boot message...\n"); - ret = usb_bulk_write(dev->udev, WRITE_DEV, (char *)&omap_memory_msg, sizeof(omap_memory_msg), WRITE_TIMEOUT); - SLEEP(5000); - if ( ret != sizeof(omap_memory_msg) ) + ret = libusb_bulk_transfer(dev->udev, WRITE_DEV, (unsigned char *)&omap_memory_msg, sizeof(omap_memory_msg), &transferred, WRITE_TIMEOUT); + if ( ret < 0 || transferred != sizeof(omap_memory_msg) ) ERROR_RETURN("Sending OMAP memory boot message failed", -1); SLEEP(250000); diff --git a/src/disk.c b/src/disk.c index daa3711..5988305 100644 --- a/src/disk.c +++ b/src/disk.c @@ -277,14 +277,20 @@ int disk_init(struct usb_device_info * dev) { unsigned int devnum; unsigned int busnum; - struct usb_device * device; + uint8_t usbdevnum; + uint8_t usbbusnum; - device = usb_device(dev->udev); - if ( ! device || ! device->bus ) { + struct libusb_device * device; + + device = libusb_get_device(dev->udev); + if ( ! device ) { ERROR_INFO("Cannot read usb devnum and busnum"); return -1; } + usbbusnum = libusb_get_bus_number(device); + usbdevnum = libusb_get_port_number(device); + dir = opendir("/sys/dev/block/"); if ( ! dir ) { ERROR_INFO("Cannot open '/sys/dev/block/' directory"); @@ -324,7 +330,7 @@ int disk_init(struct usb_device_info * dev) { fclose(f); - if ( devnum != device->devnum || device->bus->location != busnum ) + if ( devnum != usbdevnum || usbbusnum != busnum ) continue; if ( sscanf(dirent->d_name, "%d:%d", &maj, &min) != 2 ) { diff --git a/src/mkii.c b/src/mkii.c index e2fb23f..742c1da 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -29,6 +29,9 @@ #include "device.h" #include "usb-device.h" +#define READ_DEV 0x81 +#define WRITE_DEV 0x01 + #define MKII_OUT 0x8810001B #define MKII_IN 0x8800101B @@ -48,9 +51,9 @@ struct mkii_message { } __attribute__((__packed__)); -static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { +static int mkii_send_receive(libusb_device_handle * udev, uint8_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { - int ret; + int ret, transferred; static uint8_t number = 0; in_msg->header = MKII_OUT; @@ -59,15 +62,17 @@ static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_me in_msg->num = number++; in_msg->type = type; - ret = usb_bulk_write(udev, 1, (char *)in_msg, data_size + sizeof(*in_msg), 5000); + ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)in_msg, data_size + sizeof(*in_msg), &transferred, 5000); if ( ret < 0 ) return ret; - if ( (size_t)ret != data_size + sizeof(*in_msg) ) + if ( (size_t)transferred != data_size + sizeof(*in_msg) ) return -1; - ret = usb_bulk_read(udev, 129, (char *)out_msg, out_size, 5000); + ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)out_msg, out_size, &transferred, 5000); if ( ret < 0 ) return ret; + if ( (size_t)transferred < sizeof(*out_msg) ) + return -1; if ( out_msg->header != MKII_IN ) return -1; @@ -75,13 +80,13 @@ static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_me if ( out_msg->type != (type | MKII_RESPONCE) ) return -1; - if ( (size_t)ret < sizeof(*out_msg) ) + if ( (size_t)transferred < sizeof(*out_msg) ) return -1; - if ( ntohs(out_msg->size) != ret - sizeof(*out_msg) + 4 ) + if ( ntohs(out_msg->size) != transferred - sizeof(*out_msg) + 4 ) return -1; - return ret - sizeof(*out_msg); + return transferred - sizeof(*out_msg); } @@ -94,6 +99,8 @@ int mkii_init(struct usb_device_info * dev) { char * newptr; char * ptr; enum image_type type; + struct libusb_device *udev; + struct libusb_config_descriptor *desc; printf("Initializing Mk II protocol...\n"); @@ -154,7 +161,11 @@ int mkii_init(struct usb_device_info * dev) { printf("\n"); memset(buf, 0, sizeof(buf)); - usb_get_string_simple(dev->udev, usb_device(dev->udev)->config[dev->flash_device->configuration].iConfiguration, buf, sizeof(buf)); + + udev = libusb_get_device(dev->udev); + ret = libusb_get_config_descriptor(udev, dev->flash_device->configuration, &desc); + if ( ret == 0 ) + libusb_get_string_descriptor_ascii(dev->udev, desc->iConfiguration, (unsigned char*)buf, sizeof(buf)); if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) dev->data |= (1UL << 31); diff --git a/src/nolo.c b/src/nolo.c index 5e51a36..e94a652 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -22,13 +22,16 @@ #include #include -#include +#include #include "nolo.h" #include "image.h" #include "global.h" #include "printf-utils.h" +#define READ_DEV 0x81 +#define WRITE_DEV 0x01 + /* Request type */ #define NOLO_WRITE 64 #define NOLO_QUERY 192 @@ -90,7 +93,7 @@ static void nolo_error_log(struct usb_device_info * dev, int only_clear) { memset(buf, 0, sizeof(buf)); - ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_ERROR_LOG, 0, 0, buf, sizeof(buf), 2000); + ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_ERROR_LOG, 0, 0, (unsigned char *)buf, sizeof(buf), 2000); if ( ret < 0 ) break; @@ -120,7 +123,7 @@ static int nolo_identify_string(struct usb_device_info * dev, const char * str, memset(buf, 0, sizeof(buf)); - ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_IDENTIFY, 0, 0, (char *)buf, sizeof(buf), 2000); + ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_IDENTIFY, 0, 0, (unsigned char *)buf, sizeof(buf), 2000); if ( ret < 0 ) NOLO_ERROR_RETURN("NOLO_IDENTIFY failed", -1); @@ -150,10 +153,10 @@ static int nolo_set_string(struct usb_device_info * dev, char * str, char * arg) if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, str, strlen(str), 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, (unsigned char*)str, strlen(str), 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_STRING failed", -1); - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET_STRING, 0, 0, arg, strlen(arg), 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET_STRING, 0, 0, (unsigned char*)arg, strlen(arg), 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_SET_STRING failed", -1); return 0; @@ -164,10 +167,10 @@ static int nolo_get_string(struct usb_device_info * dev, char * str, char * out, int ret = 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, str, strlen(str), 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, (unsigned char*)str, strlen(str), 2000) < 0 ) return -1; - if ( ( ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET_STRING, 0, 0, out, size-1, 2000) ) < 0 ) + if ( ( ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET_STRING, 0, 0, (unsigned char*)out, size-1, 2000) ) < 0 ) return -1; if ( (size_t)ret > size-1 ) @@ -211,7 +214,7 @@ int nolo_init(struct usb_device_info * dev) { printf("Initializing NOLO...\n"); while ( val != 0 ) - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_STATUS, 0, 0, (char *)&val, 4, 2000) == -1 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_STATUS, 0, 0, (unsigned char *)&val, 4, 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_STATUS failed", -1); /* clear error log */ @@ -256,6 +259,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i uint32_t readed; int request; int ret; + int transferred; if ( flash ) printf("Send and flash image:\n"); @@ -370,7 +374,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i printf("Sending image header...\n"); if ( ! simulate ) { - if ( usb_control_msg(dev->udev, NOLO_WRITE, request, 0, 0, buf, ptr-buf, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, request, 0, 0, (unsigned char*)buf, ptr-buf, 2000) < 0 ) NOLO_ERROR_RETURN("Sending image header failed", -1); } @@ -389,19 +393,23 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i if ( ret == 0 ) break; if ( ! simulate ) { - if ( usb_bulk_write(dev->udev, 2, buf, ret, 5000) != ret ) { + if ( libusb_bulk_transfer(dev->udev, WRITE_DEV, (unsigned char*)buf, ret, &transferred, 5000) < 0 ) { PRINTF_END(); NOLO_ERROR_RETURN("Sending image failed", -1); } + if ( transferred != ret ) { + PRINTF_END(); + NOLO_ERROR_RETURN("Sending image was incomplete!", -1); + } } - readed += ret; + readed += transferred; printf_progressbar(readed, image->size); } if ( flash ) { printf("Finishing flashing...\n"); if ( ! simulate ) { - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SEND_FLASH_FINISH, 0, 0, NULL, 0, 30000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SEND_FLASH_FINISH, 0, 0, NULL, 0, 30000) < 0 ) NOLO_ERROR_RETURN("Finishing failed", -1); } } @@ -458,7 +466,7 @@ int nolo_flash_image(struct usb_device_info * dev, struct image * image) { printf("Flashing image...\n"); if ( ! simulate ) { - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_FLASH_IMAGE, 0, index, NULL, 0, 10000) ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_FLASH_IMAGE, 0, index, NULL, 0, 10000) < 0 ) NOLO_ERROR_RETURN("Flashing failed", -1); } @@ -579,7 +587,7 @@ int nolo_boot_device(struct usb_device_info * dev, const char * cmdline) { cmdline = NULL; } - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_BOOT, mode, 0, (char *)cmdline, size, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_BOOT, mode, 0, (unsigned char *)cmdline, size, 2000) < 0 ) NOLO_ERROR_RETURN("Booting failed", -1); return 0; @@ -589,7 +597,7 @@ int nolo_boot_device(struct usb_device_info * dev, const char * cmdline) { int nolo_reboot_device(struct usb_device_info * dev) { printf("Rebooting device...\n"); - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_REBOOT, 0, 0, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_REBOOT, 0, 0, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_REBOOT failed", -1); return 0; @@ -598,7 +606,7 @@ int nolo_reboot_device(struct usb_device_info * dev) { int nolo_get_root_device(struct usb_device_info * dev) { uint8_t device = 0; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ROOT_DEVICE, (char *)&device, 1, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ROOT_DEVICE, (unsigned char *)&device, 1, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get root device", -1); return device; @@ -609,7 +617,7 @@ int nolo_set_root_device(struct usb_device_info * dev, int device) { printf("Setting root device to %d...\n", device); if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, device, NOLO_ROOT_DEVICE, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, device, NOLO_ROOT_DEVICE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot set root device", -1); return 0; @@ -618,7 +626,7 @@ int nolo_set_root_device(struct usb_device_info * dev, int device) { int nolo_get_usb_host_mode(struct usb_device_info * dev) { uint32_t enabled = 0; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_USB_HOST_MODE, (void *)&enabled, 4, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_USB_HOST_MODE, (unsigned char *)&enabled, 4, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get USB host mode status", -1); return enabled ? 1 : 0; @@ -629,7 +637,7 @@ int nolo_set_usb_host_mode(struct usb_device_info * dev, int enable) { printf("%s USB host mode...\n", enable ? "Enabling" : "Disabling"); if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_USB_HOST_MODE, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_USB_HOST_MODE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot change USB host mode status", -1); return 0; @@ -638,7 +646,7 @@ int nolo_set_usb_host_mode(struct usb_device_info * dev, int enable) { int nolo_get_rd_mode(struct usb_device_info * dev) { uint8_t enabled = 0; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_RD_MODE, (char *)&enabled, 1, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_RD_MODE, (unsigned char *)&enabled, 1, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get R&D mode status", -1); return enabled ? 1 : 0; @@ -649,7 +657,7 @@ int nolo_set_rd_mode(struct usb_device_info * dev, int enable) { printf("%s R&D mode...\n", enable ? "Enabling" : "Disabling"); if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_RD_MODE, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_RD_MODE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot change R&D mode status", -1); return 0; @@ -662,7 +670,7 @@ int nolo_get_rd_flags(struct usb_device_info * dev, char * flags, size_t size) { uint16_t add_flags = 0; char * ptr = flags; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ADD_RD_FLAGS, (char *)&add_flags, 2, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ADD_RD_FLAGS, (unsigned char *)&add_flags, 2, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get R&D flags", -1); if ( add_flags & NOLO_RD_FLAG_NO_OMAP_WD ) @@ -762,10 +770,10 @@ int nolo_set_rd_flags(struct usb_device_info * dev, const char * flags) { if ( simulate ) return 0; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, add_flags, NOLO_ADD_RD_FLAGS, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, add_flags, NOLO_ADD_RD_FLAGS, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot add R&D flags", -1); - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, del_flags, NOLO_DEL_RD_FLAGS, NULL, 0, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, del_flags, NOLO_DEL_RD_FLAGS, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot del R&D flags", -1); return 0; @@ -825,7 +833,7 @@ int nolo_get_nolo_ver(struct usb_device_info * dev, char * ver, size_t size) { uint32_t version = 0; - if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET_NOLO_VERSION, 0, 0, (char *)&version, 4, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET_NOLO_VERSION, 0, 0, (unsigned char *)&version, 4, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get NOLO version", -1); if ( (version & 255) > 1 ) @@ -884,7 +892,7 @@ int nolo_set_sw_ver(struct usb_device_info * dev, const char * ver) { memcpy(ptr, ver, len); ptr += len; - if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET_SW_RELEASE, 0, 0, buf, ptr-buf, 2000) < 0 ) + if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET_SW_RELEASE, 0, 0, (unsigned char*)buf, ptr-buf, 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_SET_SW_RELEASE failed", -1); return 0; diff --git a/src/usb-device.c b/src/usb-device.c index a4b1f8b..9f5c18d 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -25,11 +25,7 @@ #include #include -#include - -#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP -#include -#endif +#include #include "global.h" #include "device.h" @@ -77,38 +73,31 @@ static void usb_flash_device_info_print(const struct usb_flash_device * dev) { } -static void usb_reattach_kernel_driver(usb_dev_handle * udev, int interface) { - -#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP - struct { - int ifno; - int ioctl_code; - void * data; - } command = { - .ifno = interface, - .ioctl_code = _IO('U', 23), - .data = NULL, - }; +static void usb_reattach_kernel_driver(libusb_device_handle * udev, int interface) { - if ( interface < 0 ) - return; - - usb_release_interface(udev, interface); - ioctl(*((int *)udev), _IOWR('U', 18, command), &command); -#endif + PRINTF_LINE("Reattach kernel driver to USB interface..."); + PRINTF_END(); + libusb_release_interface(udev, interface); + libusb_attach_kernel_driver(udev, interface); } -static void usb_descriptor_info_print(usb_dev_handle * udev, struct usb_device * dev, char * product, size_t size) { +static void usb_descriptor_info_print(libusb_device_handle * udev, struct libusb_device * dev, char * product, size_t size) { + struct libusb_device_descriptor desc; char buf[1024]; char buf2[1024]; unsigned int x; int ret; int i; + if ( libusb_get_device_descriptor(dev, &desc) < 0 ) { + PRINTF_LINE("libusb_get_device_descriptor() failed"); + PRINTF_END(); + return; + } memset(buf, 0, sizeof(buf)); - usb_get_string_simple(udev, dev->descriptor.iProduct, buf, sizeof(buf)); + libusb_get_string_descriptor_ascii(udev, desc.iProduct, (unsigned char *)buf, sizeof(buf)); PRINTF_LINE("USB device product string: %s", buf[0] ? buf : "(not detected)"); PRINTF_END(); @@ -117,7 +106,7 @@ static void usb_descriptor_info_print(usb_dev_handle * udev, struct usb_device * memset(buf, 0, sizeof(buf)); memset(buf2, 0, sizeof(buf2)); - ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, buf, sizeof(buf)); + ret = libusb_get_string_descriptor_ascii(udev, desc.iSerialNumber, (unsigned char *)buf, sizeof(buf)); if ( ! isalnum(buf[0]) ) buf[0] = 0; for ( i = 0; i < ret; i+=2 ) { @@ -136,15 +125,23 @@ static void usb_descriptor_info_print(usb_dev_handle * udev, struct usb_device * } -static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { +static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) { - int i; + int err, i; char product[1024]; + libusb_device_handle * udev; struct usb_device_info * ret = NULL; + struct libusb_device_descriptor desc; + + if ( libusb_get_device_descriptor(dev, &desc) < 0 ) { + PRINTF_LINE("libusb_get_device_descriptor failed"); + PRINTF_END(); + return NULL; + } for ( i = 0; usb_devices[i].vendor; ++i ) { - if ( dev->descriptor.idVendor == usb_devices[i].vendor && dev->descriptor.idProduct == usb_devices[i].product ) { + if ( desc.idVendor == usb_devices[i].vendor && desc.idProduct == usb_devices[i].product ) { printf("\b\b "); PRINTF_END(); @@ -153,9 +150,10 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { PRINTF_END(); PRINTF_LINE("Opening USB..."); - usb_dev_handle * udev = usb_open(dev); - if ( ! udev ) { - PRINTF_ERROR("usb_open failed"); + + err = libusb_open(dev, &udev); + if ( err < 0 ) { + PRINTF_ERROR("libusb_open failed"); fprintf(stderr, "\n"); return NULL; } @@ -164,17 +162,15 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { if ( usb_devices[i].interface >= 0 ) { -#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP PRINTF_LINE("Detaching kernel from USB interface..."); - usb_detach_kernel_driver_np(udev, usb_devices[i].interface); -#endif + libusb_detach_kernel_driver(udev, usb_devices[i].interface); PRINTF_LINE("Claiming USB interface..."); - if ( usb_claim_interface(udev, usb_devices[i].interface) < 0 ) { - PRINTF_ERROR("usb_claim_interface failed"); + if ( libusb_claim_interface(udev, usb_devices[i].interface) < 0 ) { + PRINTF_ERROR("libusb_claim_interface failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); return NULL; } @@ -182,22 +178,22 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { if ( usb_devices[i].alternate >= 0 ) { PRINTF_LINE("Setting alternate USB interface..."); - if ( usb_set_altinterface(udev, usb_devices[i].alternate) < 0 ) { - PRINTF_ERROR("usb_claim_interface failed"); + if ( libusb_set_interface_alt_setting(udev, usb_devices[i].interface, usb_devices[i].alternate) < 0 ) { + PRINTF_ERROR("libusb_claim_interface failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); return NULL; } } if ( usb_devices[i].configuration >= 0 ) { PRINTF_LINE("Setting USB configuration..."); - if ( usb_set_configuration(udev, usb_devices[i].configuration) < 0 ) { - PRINTF_ERROR("usb_set_configuration failed"); + if ( libusb_set_configuration(udev, usb_devices[i].configuration) < 0 ) { + PRINTF_ERROR("libusb_set_configuration failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); return NULL; } } @@ -206,7 +202,7 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { if ( ! ret ) { ALLOC_ERROR(); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); return NULL; } @@ -232,7 +228,7 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { ERROR("Device mishmash"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - usb_close(udev); + libusb_close(udev); free(ret); return NULL; } @@ -249,28 +245,6 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { } -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; - -} - static volatile sig_atomic_t signal_quit; static void signal_handler(int signum) { @@ -282,14 +256,18 @@ static void signal_handler(int signum) { struct usb_device_info * usb_open_and_wait_for_device(void) { - struct usb_bus * bus; + libusb_device **devs; + libusb_device **dev; struct usb_device_info * ret = NULL; int i = 0; void (*prev)(int); static char progress[] = {'/','-','\\', '|'}; - usb_init(); - usb_find_busses(); + if ( libusb_init(NULL) < 0 ) { + PRINTF_LINE("libusb_init failed"); + PRINTF_END(); + return NULL; + } PRINTF_BACK(); printf("\n"); @@ -302,24 +280,16 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { PRINTF_LINE("Waiting for USB device... %c", progress[++i%sizeof(progress)]); - 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 ( libusb_get_device_list(NULL, &devs) < 0 ) { + PRINTF_LINE("Listing USB devices failed"); + PRINTF_END(); + break; + } + for ( dev = devs; *dev != NULL; ++dev ) { + ret = usb_device_is_valid(*dev); if ( ret ) break; - } if ( ret ) @@ -345,7 +315,7 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { void usb_close_device(struct usb_device_info * dev) { usb_reattach_kernel_driver(dev->udev, dev->flash_device->interface); - usb_close(dev->udev); + libusb_close(dev->udev); free(dev); } diff --git a/src/usb-device.h b/src/usb-device.h index 850c550..9b2bcc4 100644 --- a/src/usb-device.h +++ b/src/usb-device.h @@ -20,7 +20,7 @@ #ifndef USB_DEVICE_H #define USB_DEVICE_H -#include +#include #include "device.h" @@ -47,7 +47,7 @@ struct usb_device_info { enum device device; int16_t hwrev; const struct usb_flash_device * flash_device; - usb_dev_handle * udev; + libusb_device_handle * udev; int data; }; -- cgit v1.2.3 From ed4bdab6e72b8034da218be46db8774fd471655a Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Fri, 15 Jan 2016 15:46:23 +0100 Subject: libusb-sniff: add libusb 1.0 support This adds additional support for libusb-1.0 to the sniffing library. Afterwards it can be used with libusb 1.0 and libusb 0.1 based binaries. --- src/libusb-sniff.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/src/libusb-sniff.c b/src/libusb-sniff.c index ecf4b2c..0ac1259 100644 --- a/src/libusb-sniff.c +++ b/src/libusb-sniff.c @@ -27,7 +27,9 @@ #include struct usb_dev_handle; +struct libusb_device_handle; typedef struct usb_dev_handle usb_dev_handle; +typedef struct libusb_device_handle libusb_device_handle; static char to_ascii(char c) { @@ -117,6 +119,54 @@ int usb_bulk_read(usb_dev_handle * dev, int ep, char * bytes, int size, int time } +int libusb_bulk_transfer(libusb_device_handle *dev, unsigned char ep, unsigned char *bytes, int size, int *actual_length, unsigned int timeout) { + + static int (*real_libusb_bulk_transfer)(libusb_device_handle *dev, unsigned char ep, unsigned char *bytes, int size, int *actual_length, unsigned int timeout) = NULL; + int ret; + + if ( ! real_libusb_bulk_transfer ) + *(void **)(&real_libusb_bulk_transfer) = dlsym(RTLD_NEXT, "libusb_bulk_transfer"); + + if ( ep == 0x81 ) { + + ret = real_libusb_bulk_transfer(dev, ep, bytes, size, actual_length, timeout); + + if ( ! getenv("USBSNIFF_SKIP_READ") ) { + + printf("\n==== usb_bulk_read (ep=%d size=%d timeout=%d) ret = %d ====\n", ep, size, timeout, (ret < 0) ? ret : *actual_length); + if ( ret == 0 ) { + dump_bytes((char*) bytes, *actual_length); + printf("====\n"); + } + + if ( getenv("USBSNIFF_WAIT") ) { + printf("Press ENTER"); fflush(stdout); getchar(); + } + + } + + return ret; + + } else { + + if ( ! getenv("USBSNIFF_SKIP_WRITE") ) { + + printf("\n==== usb_bulk_write (ep=%d size=%d timeout=%d) ====\n", ep, size, timeout); + dump_bytes((char*) bytes, size); + printf("====\n"); + + if ( getenv("USBSNIFF_WAIT") ) { + printf("Press ENTER"); fflush(stdout); getchar(); + } + + } + + return real_libusb_bulk_transfer(dev, ep, bytes, size, actual_length, timeout); + + } + +} + int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout) { static int (*real_usb_control_msg)(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout) = NULL; @@ -157,6 +207,46 @@ int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value } +int libusb_control_msg(libusb_device_handle *dev, int requesttype, int request, int value, int index, unsigned char *bytes, int size, int timeout) { + + static int (*real_usb_control_msg)(libusb_device_handle *dev, int requesttype, int request, int value, int index, unsigned char *bytes, int size, int timeout) = NULL; + int ret; + + if ( ! real_usb_control_msg ) + *(void **)(&real_usb_control_msg) = dlsym(RTLD_NEXT, "libusb_control_msg"); + + if ( requesttype == 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) { + + printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ====\n", requesttype, request, value, index, size, timeout); + dump_bytes((char*) bytes, size); + printf("====\n"); + + if ( getenv("USBSNIFF_WAIT") ) { + printf("Press ENTER"); fflush(stdout); getchar(); + } + + } + + ret = real_usb_control_msg(dev, requesttype, request, value, index, bytes, size, timeout); + + if ( requesttype != 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) { + + printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ret = %d ====\n", requesttype, request, value, index, size, timeout, ret); + if ( ret > 0 ) { + dump_bytes((char*) bytes, ret); + printf("====\n"); + } + + if ( getenv("USBSNIFF_WAIT") ) { + printf("Press ENTER"); fflush(stdout); getchar(); + } + + } + + return ret; + +} + int usb_set_configuration(usb_dev_handle *dev, int configuration) { static int (*real_usb_set_configuration)(usb_dev_handle *dev, int configuration) = NULL; @@ -170,6 +260,19 @@ int usb_set_configuration(usb_dev_handle *dev, int configuration) { } +int libusb_set_configuration(libusb_device_handle *dev, int configuration) { + + static int (*real_usb_set_configuration)(libusb_device_handle *dev, int configuration) = NULL; + + if ( ! real_usb_set_configuration ) + *(void **)(&real_usb_set_configuration) = dlsym(RTLD_NEXT, "libusb_set_configuration"); + + printf("\n==== usb_set_configuration (configuration=%d) ====\n", configuration); + + return real_usb_set_configuration(dev, configuration); + +} + int usb_claim_interface(usb_dev_handle *dev, int interface) { static int (*real_usb_claim_interface)(usb_dev_handle *dev, int interface) = NULL; @@ -183,6 +286,19 @@ int usb_claim_interface(usb_dev_handle *dev, int interface) { } +int libusb_claim_interface(libusb_device_handle *dev, int interface) { + + static int (*real_usb_claim_interface)(libusb_device_handle *dev, int interface) = NULL; + + if ( ! real_usb_claim_interface ) + *(void **)(&real_usb_claim_interface) = dlsym(RTLD_NEXT, "libusb_claim_interface"); + + printf("\n==== usb_claim_interface (interface=%d) ====\n", interface); + + return real_usb_claim_interface(dev, interface); + +} + int usb_set_altinterface(usb_dev_handle *dev, int alternate) { static int (*real_usb_set_altinterface)(usb_dev_handle *dev, int alternate) = NULL; @@ -195,3 +311,16 @@ int usb_set_altinterface(usb_dev_handle *dev, int alternate) { return real_usb_set_altinterface(dev, alternate); } + +int libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface, int alternate) { + + static int (*real_usb_set_altinterface)(libusb_device_handle *dev, int interface, int alternate) = NULL; + + if ( ! real_usb_set_altinterface ) + *(void **)(&real_usb_set_altinterface) = dlsym(RTLD_NEXT, "libusb_set_interface_alt_setting"); + + printf("\n==== usb_set_altinterface (alternate=%d) ====\n", alternate); + + return real_usb_set_altinterface(dev, interface, alternate); + +} -- cgit v1.2.3 From 62fa041f865cb3593e07b2caa514b61e266e851e Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 23 Jan 2016 16:06:51 +0100 Subject: all: Rename variable readed to sent --- src/cold-flash.c | 34 +++++++++++++++++----------------- src/disk.c | 12 ++++++------ src/nolo.c | 12 ++++++------ 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/cold-flash.c b/src/cold-flash.c index 1c9d95b..4f01457 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -138,7 +138,7 @@ struct xloader_msg { struct xloader_msg xloader_msg_create(uint32_t type, struct image * image) { struct xloader_msg msg; - uint32_t need, readed; + uint32_t need, sent; int ret; uint8_t buffer[1024]; @@ -149,16 +149,16 @@ struct xloader_msg xloader_msg_create(uint32_t type, struct image * image) { if ( image ) { msg.size = image->size; image_seek(image, 0); - readed = 0; - while ( readed < image->size ) { - need = image->size - readed; + sent = 0; + while ( sent < image->size ) { + need = image->size - sent; if ( need > sizeof(buffer) ) need = sizeof(buffer); ret = image_read(image, buffer, need); if ( ret == 0 ) break; msg.crc1 = crc32(buffer, ret, msg.crc1); - readed += ret; + sent += ret; } } @@ -186,7 +186,7 @@ static int read_asic(libusb_device_handle * udev, uint8_t * asic_buffer, int siz static int send_2nd(libusb_device_handle * udev, struct image * image) { uint8_t buffer[1024]; - uint32_t need, readed; + uint32_t need, sent; int ret, transferred; printf("Sending OMAP peripheral boot message...\n"); @@ -206,9 +206,9 @@ static int send_2nd(libusb_device_handle * udev, struct image * image) { printf("Sending 2nd X-Loader image...\n"); printf_progressbar(0, image->size); image_seek(image, 0); - readed = 0; - while ( readed < image->size ) { - need = image->size - readed; + sent = 0; + while ( sent < image->size ) { + need = image->size - sent; if ( need > sizeof(buffer) ) need = sizeof(buffer); ret = image_read(image, buffer, need); @@ -218,8 +218,8 @@ static int send_2nd(libusb_device_handle * udev, struct image * image) { PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed", -1); if ( ret != transferred ) PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed (incomplete bulk transfer)", -1); - readed += transferred; - printf_progressbar(readed, image->size); + sent += transferred; + printf_progressbar(sent, image->size); } SLEEP(50000); @@ -231,7 +231,7 @@ static int send_secondary(libusb_device_handle * udev, struct image * image) { struct xloader_msg init_msg; uint8_t buffer[1024]; - uint32_t need, readed; + uint32_t need, sent; int ret, transferred; init_msg = xloader_msg_create(XLOADER_MSG_TYPE_SEND, image); @@ -250,9 +250,9 @@ static int send_secondary(libusb_device_handle * udev, struct image * image) { printf("Sending Secondary image...\n"); printf_progressbar(0, image->size); image_seek(image, 0); - readed = 0; - while ( readed < image->size ) { - need = image->size - readed; + sent = 0; + while ( sent < image->size ) { + need = image->size - sent; if ( need > sizeof(buffer) ) need = sizeof(buffer); ret = image_read(image, buffer, need); @@ -262,8 +262,8 @@ static int send_secondary(libusb_device_handle * udev, struct image * image) { PRINTF_ERROR_RETURN("Sending Secondary image failed", -1); if ( ret != transferred ) PRINTF_ERROR_RETURN("Sending Secondary image failed (incomplete bulk transfer)", -1); - readed += transferred; - printf_progressbar(readed, image->size); + sent += transferred; + printf_progressbar(sent, image->size); } printf("Waiting for X-Loader response...\n"); diff --git a/src/disk.c b/src/disk.c index 5988305..78b315a 100644 --- a/src/disk.c +++ b/src/disk.c @@ -166,7 +166,7 @@ int disk_dump_dev(int fd, const char * file) { int ret; char * path; uint64_t blksize; - size_t need, readed; + size_t need, sent; ssize_t size; struct statvfs buf; @@ -226,11 +226,11 @@ int disk_dump_dev(int fd, const char * file) { return -1; } - readed = 0; + sent = 0; printf_progressbar(0, blksize); - while ( readed < blksize ) { - need = blksize - readed; + while ( sent < blksize ) { + need = blksize - sent; if ( need > sizeof(global_buf) ) need = sizeof(global_buf); size = read(fd, global_buf, need); @@ -241,8 +241,8 @@ int disk_dump_dev(int fd, const char * file) { close(fd2); return -1; } - readed += size; - printf_progressbar(readed, blksize); + sent += size; + printf_progressbar(sent, blksize); } close(fd2); diff --git a/src/nolo.c b/src/nolo.c index e94a652..91bcf3f 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -256,7 +256,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i uint16_t hash; uint32_t size; uint32_t need; - uint32_t readed; + uint32_t sent; int request; int ret; int transferred; @@ -384,9 +384,9 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i printf("Sending image...\n"); printf_progressbar(0, image->size); image_seek(image, 0); - readed = 0; - while ( readed < image->size ) { - need = image->size - readed; + sent = 0; + while ( sent < image->size ) { + need = image->size - sent; if ( need > sizeof(buf) ) need = sizeof(buf); ret = image_read(image, buf, need); @@ -402,8 +402,8 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i NOLO_ERROR_RETURN("Sending image was incomplete!", -1); } } - readed += transferred; - printf_progressbar(readed, image->size); + sent += ret; + printf_progressbar(sent, image->size); } if ( flash ) { -- cgit v1.2.3 From fc987674dd92a50687d970b02e072fff19391175 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 23 Jan 2016 16:07:02 +0100 Subject: all: Code style fixes --- src/cal.c | 4 ++-- src/cold-flash.c | 4 ++-- src/main.c | 10 +++++----- src/operations.c | 2 +- src/printf-utils.c | 14 +++++++------- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/cal.c b/src/cal.c index 102ffab..60a2de1 100644 --- a/src/cal.c +++ b/src/cal.c @@ -224,10 +224,10 @@ static int64_t find_section(void *start, uint64_t count, int want_index, const c memcpy(sectname, hdr->name, sizeof(hdr->name)); if ( want_index == INDEX_LAST ) { - if ((int)hdr->index <= previous_index) + if ( (int)hdr->index <= previous_index ) goto next; } else { - if (want_index >= 0 && want_index != hdr->index) + if ( want_index >= 0 && want_index != hdr->index ) goto next; } diff --git a/src/cold-flash.c b/src/cold-flash.c index 4f01457..89b055a 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -49,7 +49,7 @@ static void crc32_gentab(void) { for ( j = 8; j > 0; j-- ) { - if (crc & 1) + if ( crc & 1 ) crc = (crc >> 1) ^ poly; else crc >>= 1; @@ -316,7 +316,7 @@ static int ping_timeout(libusb_device_handle * udev) { } - if (pong) + if ( pong ) return 0; else return -1; diff --git a/src/main.c b/src/main.c index cea36f7..e658f06 100644 --- a/src/main.c +++ b/src/main.c @@ -168,7 +168,7 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) { } layout_file = strchr(arg, '%'); - if (layout_file) + if ( layout_file ) *(layout_file++) = 0; type = NULL; @@ -178,16 +178,16 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) { layout = NULL; file = strrchr(arg, ':'); - if (file) { + if ( file ) { *(file++) = 0; type = strrchr(arg, ':'); - if (type) { + if ( type ) { *(type++) = 0; version = strrchr(arg, ':'); - if (version) { + if ( version ) { *(version++) = 0; hwrevs = strchr(arg, ':'); - if (hwrevs) + if ( hwrevs ) *(hwrevs++) = 0; device = arg; } else { diff --git a/src/operations.c b/src/operations.c index 2d26ad5..8773b95 100644 --- a/src/operations.c +++ b/src/operations.c @@ -82,7 +82,7 @@ struct device_info * dev_detect(void) { } clean: - if (usb) + if ( usb ) usb_close_device(usb); free(dev); return NULL; diff --git a/src/printf-utils.c b/src/printf-utils.c index 3520d0b..d43c063 100644 --- a/src/printf-utils.c +++ b/src/printf-utils.c @@ -39,20 +39,20 @@ void printf_progressbar(unsigned long long part, unsigned long long total) { int tmp, cols = 80; /* percentage calculation */ - pc = total==0?100:(int)(part*100/total); - (pc<0)?pc=0:(pc>100)?pc=100:0; + pc = total == 0 ? 100 : (int)(part*100/total); + ( pc < 0 ) ? pc = 0 : ( pc > 100 ) ? pc = 100 : 0; PRINTF_BACK(); PRINTF_ADD("\x1b[K %3d%% [", pc); - if (columns) + if ( columns ) cols = atoi(columns); - if (cols > 115) + if ( cols > 115 ) cols = 115; cols-=15; - for(tmp=cols*pc/100;tmp;tmp--) PRINTF_ADD("#"); - for(tmp=cols-(cols*pc/100);tmp;tmp--) PRINTF_ADD("-"); + for ( tmp = cols*pc/100; tmp; tmp-- ) PRINTF_ADD("#"); + for ( tmp = cols-(cols*pc/100); tmp; tmp-- ) PRINTF_ADD("-"); PRINTF_ADD("]"); - if (part == total) PRINTF_END(); + if ( part == total ) PRINTF_END(); fflush(stdout); } -- cgit v1.2.3 From 2e9748e6a5cf70aa893caa5bcbb67ebd431d2b62 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 23 Jan 2016 16:26:40 +0100 Subject: cold-flash: Comment unused omap variables --- src/cold-flash.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cold-flash.c b/src/cold-flash.c index 89b055a..459efdb 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -85,6 +85,8 @@ static uint32_t crc32(unsigned char * bytes, size_t size, uint32_t crc) { /* Omap Peripheral boot message */ static const uint32_t omap_peripheral_msg = 0xF0030002; +/* Unused */ +#if 0 /* Omap Void (no device) boot message */ static const uint32_t omap_void_msg = 0xF0030006; @@ -117,6 +119,7 @@ static const uint32_t omap_hsusb_msg = 0xF0031106; /* Omap next device boot message */ static const uint32_t omap_next_msg = 0xFFFFFFFF; +#endif /* Omap memory boot message */ static const uint32_t omap_memory_msg = 0; -- cgit v1.2.3 From 2504f9e55339648584a3cfc4c203dd0b936998be Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 23 Jan 2016 16:27:35 +0100 Subject: travis: Update travis script for clang and libusb-1.0 --- .travis.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3e46a37..65fbec7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,16 @@ language: c compiler: - gcc + - clang before_install: - sudo apt-get update -qq - - sudo apt-get install -qq -y libusb-dev -script: if [ ${COVERITY_SCAN_BRANCH} != 1 ]; then make ; fi + - sudo apt-get install -qq -y libusb-1.0-0-dev +script: if [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make ; fi + +matrix: + exclude: + - compiler: clang + - env: COVERITY_SCAN_BRANCH=1 env: global: @@ -16,5 +22,5 @@ addons: name: "pali/0xFFFF" description: "Build submitted via Travis CI" notification_email: pali.rohar@gmail.com - build_command: "make" + build_command: make branch_pattern: master -- cgit v1.2.3 From 96de6a6215e5292e2ae86533eab32661bdd2d673 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 23 Jan 2016 16:38:17 +0100 Subject: usb-device: Fix memory leak when listing devices --- src/usb-device.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/usb-device.c b/src/usb-device.c index 9f5c18d..5d54284 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -292,6 +292,8 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { break; } + libusb_free_device_list(devs, 1); + if ( ret ) break; @@ -316,6 +318,7 @@ void usb_close_device(struct usb_device_info * dev) { usb_reattach_kernel_driver(dev->udev, dev->flash_device->interface); libusb_close(dev->udev); + libusb_exit(NULL); free(dev); } -- cgit v1.2.3 From 09920cf8abd698dc3d296b275c34336dfbf8213a Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 23 Jan 2016 16:46:05 +0100 Subject: config.mk: Version is from git --- config.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.mk b/config.mk index 822eb02..bb0ca40 100644 --- a/config.mk +++ b/config.mk @@ -1,4 +1,4 @@ -VERSION = 0.6.1 +VERSION = 0.6.1+git PREFIX = /usr/local # NetBSD stuff -- cgit v1.2.3 From abcf2562689a46386b45eaa34da44010054244ae Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 12 Feb 2016 22:27:35 +0100 Subject: disk: Use correct libusb function which returns Linux devnum value It is libusb_get_device_address() and not libusb_get_port_number() --- src/disk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/disk.c b/src/disk.c index 78b315a..96a8d8a 100644 --- a/src/disk.c +++ b/src/disk.c @@ -289,7 +289,7 @@ int disk_init(struct usb_device_info * dev) { } usbbusnum = libusb_get_bus_number(device); - usbdevnum = libusb_get_port_number(device); + usbdevnum = libusb_get_device_address(device); dir = opendir("/sys/dev/block/"); if ( ! dir ) { -- cgit v1.2.3 From bc7894b99dc4902b365b5f2e5bb258f471fe5582 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 21 Feb 2016 20:52:21 +0100 Subject: all: Fix usb endpoints after conversion to libusb 1.0 Endpoint in nolo.c for libusb_bulk_transfer must be 0x02, not 0x01. --- src/cold-flash.c | 24 +++++++++++------------- src/mkii.c | 7 ++----- src/nolo.c | 5 +---- src/usb-device.h | 4 ++++ 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/cold-flash.c b/src/cold-flash.c index 459efdb..a6a11b6 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -30,8 +30,6 @@ #include "usb-device.h" #include "printf-utils.h" -#define READ_DEV 0x81 -#define WRITE_DEV 0x01 #define READ_TIMEOUT 500 #define WRITE_TIMEOUT 3000 @@ -176,7 +174,7 @@ static int read_asic(libusb_device_handle * udev, uint8_t * asic_buffer, int siz int ret, transferred; printf("Waiting for ASIC ID...\n"); - ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)asic_buffer, size, &transferred, READ_TIMEOUT); + ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)asic_buffer, size, &transferred, READ_TIMEOUT); if ( ret < 0 ) ERROR_RETURN("Cannot read ASIC ID", -1); if ( transferred != asic_size ) @@ -193,14 +191,14 @@ static int send_2nd(libusb_device_handle * udev, struct image * image) { int ret, transferred; printf("Sending OMAP peripheral boot message...\n"); - ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), &transferred, WRITE_TIMEOUT); + ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), &transferred, WRITE_TIMEOUT); if ( ret < 0 || transferred != sizeof(omap_peripheral_msg) ) ERROR_RETURN("Sending OMAP peripheral boot message failed", -1); SLEEP(5000); printf("Sending 2nd X-Loader image size...\n"); - ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&image->size, 4, &transferred, WRITE_TIMEOUT); + ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)&image->size, 4, &transferred, WRITE_TIMEOUT); if ( ret < 0 || transferred != 4 ) ERROR_RETURN("Sending 2nd X-Loader image size failed", -1); @@ -217,7 +215,7 @@ static int send_2nd(libusb_device_handle * udev, struct image * image) { ret = image_read(image, buffer, need); if ( ret == 0 ) break; - if ( libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) + if ( libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed", -1); if ( ret != transferred ) PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed (incomplete bulk transfer)", -1); @@ -240,13 +238,13 @@ static int send_secondary(libusb_device_handle * udev, struct image * image) { init_msg = xloader_msg_create(XLOADER_MSG_TYPE_SEND, image); printf("Sending X-Loader init message...\n"); - ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&init_msg, sizeof(init_msg), &transferred, WRITE_TIMEOUT); + ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)&init_msg, sizeof(init_msg), &transferred, WRITE_TIMEOUT); if ( ret < 0 || transferred != sizeof(init_msg) ) ERROR_RETURN("Sending X-Loader init message failed", -1); printf("Waiting for X-Loader response...\n"); SLEEP(5000); - ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ + ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ if ( ret < 0 || transferred != 4 ) ERROR_RETURN("No response", -1); @@ -261,7 +259,7 @@ static int send_secondary(libusb_device_handle * udev, struct image * image) { ret = image_read(image, buffer, need); if ( ret == 0 ) break; - if ( libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) + if ( libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) PRINTF_ERROR_RETURN("Sending Secondary image failed", -1); if ( ret != transferred ) PRINTF_ERROR_RETURN("Sending Secondary image failed (incomplete bulk transfer)", -1); @@ -271,7 +269,7 @@ static int send_secondary(libusb_device_handle * udev, struct image * image) { printf("Waiting for X-Loader response...\n"); SLEEP(5000); - ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ + ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ if ( ret < 0 || transferred != 4 ) ERROR_RETURN("No response", -1); @@ -291,7 +289,7 @@ static int ping_timeout(libusb_device_handle * udev) { int try_read = 4; printf("Sending X-Loader ping message\n"); - ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)&ping_msg, sizeof(ping_msg), &transferred, WRITE_TIMEOUT); + ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)&ping_msg, sizeof(ping_msg), &transferred, WRITE_TIMEOUT); if ( ret < 0 || transferred != sizeof(ping_msg) ) ERROR_RETURN("Sending X-Loader ping message failed", -1); @@ -299,7 +297,7 @@ static int ping_timeout(libusb_device_handle * udev) { while ( try_read > 0 ) { uint32_t ping_read; - ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)&ping_read, sizeof(ping_read), &transferred, READ_TIMEOUT); + ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)&ping_read, sizeof(ping_read), &transferred, READ_TIMEOUT); if ( ret == 0 && transferred == sizeof(ping_read) ) { printf("Got it\n"); pong = 1; @@ -415,7 +413,7 @@ int leave_cold_flash(struct usb_device_info * dev) { int ret, transferred; printf("Sending OMAP memory boot message...\n"); - ret = libusb_bulk_transfer(dev->udev, WRITE_DEV, (unsigned char *)&omap_memory_msg, sizeof(omap_memory_msg), &transferred, WRITE_TIMEOUT); + ret = libusb_bulk_transfer(dev->udev, USB_WRITE_EP, (unsigned char *)&omap_memory_msg, sizeof(omap_memory_msg), &transferred, WRITE_TIMEOUT); if ( ret < 0 || transferred != sizeof(omap_memory_msg) ) ERROR_RETURN("Sending OMAP memory boot message failed", -1); diff --git a/src/mkii.c b/src/mkii.c index 742c1da..3217557 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -29,9 +29,6 @@ #include "device.h" #include "usb-device.h" -#define READ_DEV 0x81 -#define WRITE_DEV 0x01 - #define MKII_OUT 0x8810001B #define MKII_IN 0x8800101B @@ -62,13 +59,13 @@ static int mkii_send_receive(libusb_device_handle * udev, uint8_t type, struct m in_msg->num = number++; in_msg->type = type; - ret = libusb_bulk_transfer(udev, WRITE_DEV, (unsigned char *)in_msg, data_size + sizeof(*in_msg), &transferred, 5000); + ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)in_msg, data_size + sizeof(*in_msg), &transferred, 5000); if ( ret < 0 ) return ret; if ( (size_t)transferred != data_size + sizeof(*in_msg) ) return -1; - ret = libusb_bulk_transfer(udev, READ_DEV, (unsigned char *)out_msg, out_size, &transferred, 5000); + ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)out_msg, out_size, &transferred, 5000); if ( ret < 0 ) return ret; if ( (size_t)transferred < sizeof(*out_msg) ) diff --git a/src/nolo.c b/src/nolo.c index 91bcf3f..4f5ec27 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -29,9 +29,6 @@ #include "global.h" #include "printf-utils.h" -#define READ_DEV 0x81 -#define WRITE_DEV 0x01 - /* Request type */ #define NOLO_WRITE 64 #define NOLO_QUERY 192 @@ -393,7 +390,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i if ( ret == 0 ) break; if ( ! simulate ) { - if ( libusb_bulk_transfer(dev->udev, WRITE_DEV, (unsigned char*)buf, ret, &transferred, 5000) < 0 ) { + if ( libusb_bulk_transfer(dev->udev, USB_WRITE_DATA_EP, (unsigned char*)buf, ret, &transferred, 5000) < 0 ) { PRINTF_END(); NOLO_ERROR_RETURN("Sending image failed", -1); } diff --git a/src/usb-device.h b/src/usb-device.h index 9b2bcc4..910a8c9 100644 --- a/src/usb-device.h +++ b/src/usb-device.h @@ -22,6 +22,10 @@ #include +#define USB_READ_EP (LIBUSB_ENDPOINT_IN | 0x1) +#define USB_WRITE_EP (LIBUSB_ENDPOINT_OUT | 0x1) +#define USB_WRITE_DATA_EP (LIBUSB_ENDPOINT_OUT | 0x2) + #include "device.h" enum usb_flash_protocol { -- cgit v1.2.3 From 6952d730827165f9596b6c65380f5aeb4e6c985b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 23 Feb 2016 18:15:55 +0100 Subject: libusb-sniff: Fix libusb_control_transfer function --- src/libusb-sniff.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/libusb-sniff.c b/src/libusb-sniff.c index 0ac1259..ea7ef48 100644 --- a/src/libusb-sniff.c +++ b/src/libusb-sniff.c @@ -24,6 +24,7 @@ #define _GNU_SOURCE #include #include +#include #include struct usb_dev_handle; @@ -207,17 +208,17 @@ int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value } -int libusb_control_msg(libusb_device_handle *dev, int requesttype, int request, int value, int index, unsigned char *bytes, int size, int timeout) { +int libusb_control_transfer(libusb_device_handle *dev, uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, unsigned char *bytes, uint16_t size, unsigned int timeout) { - static int (*real_usb_control_msg)(libusb_device_handle *dev, int requesttype, int request, int value, int index, unsigned char *bytes, int size, int timeout) = NULL; + static int (*real_libusb_control_transfer)(libusb_device_handle *dev, uint8_t requesttype, uint8_t request, uint16_t value, uint16_t index, unsigned char *bytes, uint16_t size, unsigned int timeout) = NULL; int ret; - if ( ! real_usb_control_msg ) - *(void **)(&real_usb_control_msg) = dlsym(RTLD_NEXT, "libusb_control_msg"); + if ( ! real_libusb_control_transfer ) + *(void **)(&real_libusb_control_transfer) = dlsym(RTLD_NEXT, "libusb_control_transfer"); if ( requesttype == 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) { - printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ====\n", requesttype, request, value, index, size, timeout); + printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ====\n", (int)requesttype, (int)request, (int)value, (int)index, (int)size, (int)timeout); dump_bytes((char*) bytes, size); printf("====\n"); @@ -227,11 +228,11 @@ int libusb_control_msg(libusb_device_handle *dev, int requesttype, int request, } - ret = real_usb_control_msg(dev, requesttype, request, value, index, bytes, size, timeout); + ret = real_libusb_control_transfer(dev, requesttype, request, value, index, bytes, size, timeout); if ( requesttype != 64 && ! getenv("USBSNIFF_SKIP_CONTROL") ) { - printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ret = %d ====\n", requesttype, request, value, index, size, timeout, ret); + printf("\n==== usb_control_msg(requesttype=%d, request=%d, value=%d, index=%d, size=%d, timeout=%d) ret = %d ====\n", (int)requesttype, (int)request, (int)value, (int)index, (int)size, (int)timeout, ret); if ( ret > 0 ) { dump_bytes((char*) bytes, ret); printf("====\n"); -- cgit v1.2.3 From aeb9e897dbf80cb341ace040267378beefed55ca Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 23 Feb 2016 18:23:04 +0100 Subject: usb-device: Autodetect Nokia 770 device from product string --- src/usb-device.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/usb-device.c b/src/usb-device.c index 5d54284..22d30f5 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -206,7 +206,9 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) return NULL; } - if ( strstr(product, "N900") ) + if ( strncmp(product, "Nokia 770", sizeof("Nokia 770")-1) == 0 ) + ret->device = DEVICE_SU_18; + else if ( strstr(product, "N900") ) ret->device = DEVICE_RX_51; else ret->device = DEVICE_UNKNOWN; -- cgit v1.2.3 From c90c6319fe3f7dd7e50dc52bc363a04289eb165f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 23 Feb 2016 18:38:48 +0100 Subject: main: Initialize buf before calling dev_get_rd_flags() --- src/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.c b/src/main.c index e658f06..3c24820 100644 --- a/src/main.c +++ b/src/main.c @@ -1000,6 +1000,7 @@ int main(int argc, char **argv) { printf("\n"); if ( ret == 1 ) { + buf[0] = 0; ret = dev_get_rd_flags(dev, buf, sizeof(buf)); printf("R&D flags: "); if ( ret < 0 ) -- cgit v1.2.3 From a1623bcb7e57029f5e4ed2aaef889df0859ea47f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 9 Mar 2016 00:07:52 +0100 Subject: image: Fix detection of jffs2 initfs images Looks like that some official initfs images are bigger then 3MB, so set limit to 10MB --- src/image.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/image.c b/src/image.c index 23d79d8..f31d6c7 100644 --- a/src/image.c +++ b/src/image.c @@ -546,7 +546,7 @@ enum image_type image_type_from_data(struct image * image) { else if ( size >= 4 && memcmp(buf, "\x45\x3d\xcd\x28", 4) == 0 ) /* CRAMFS MAGIC */ return IMAGE_INITFS; else if ( size >= 2 && memcmp(buf, "\x85\x19", 2) == 0 ) { /* JFFS2 MAGIC */ - if ( image->size < 0x300000 ) + if ( image->size < 0x1000000 ) return IMAGE_INITFS; else return IMAGE_ROOTFS; -- cgit v1.2.3 From 98451c5851bbdeeac1fca0a2bbd57e75e346bd69 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 9 Mar 2016 00:15:25 +0100 Subject: usb-device: Add FLASH_DISK usb ids for all supported devices --- src/usb-device.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/usb-device.c b/src/usb-device.c index 22d30f5..4b64fae 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -36,11 +36,14 @@ #include "mkii.h" static struct usb_flash_device usb_devices[] = { + { 0x0421, 0x0096, -1, -1, -1, FLASH_DISK, { DEVICE_RX_44, 0 } }, { 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, -1, -1, -1, FLASH_DISK, { DEVICE_RX_51, 0 } }, + { 0x0421, 0x0189, -1, -1, -1, FLASH_DISK, { DEVICE_RX_48, 0 } }, + { 0x0421, 0x01c7, -1, -1, -1, FLASH_DISK, { DEVICE_RX_51, 0 } }, { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, 0 } }, - { 0x0421, 0x0431, -1, -1, -1, FLASH_DISK, { DEVICE_SU_18, DEVICE_RX_34, 0 } }, + { 0x0421, 0x0431, -1, -1, -1, FLASH_DISK, { DEVICE_SU_18, DEVICE_RX_34, 0 } }, + { 0x0421, 0x04c3, -1, -1, -1, FLASH_DISK, { DEVICE_RX_34, 0 } }, { 0x0421, 0x3f00, 2, 1, -1, FLASH_NOLO, { DEVICE_RX_34, 0 } }, }; -- cgit v1.2.3 From c560f3642c20eeeabd882a671741ac7a3b087e02 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 9 Mar 2016 00:15:59 +0100 Subject: usb-device: Finish usb device detection based on product strings --- src/usb-device.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/usb-device.c b/src/usb-device.c index 4b64fae..e0c4074 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -209,22 +209,37 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) return NULL; } - if ( strncmp(product, "Nokia 770", sizeof("Nokia 770")-1) == 0 ) + if ( strcmp(product, "Nokia 770") == 0 || strcmp(product, "Nokia 770 (Update mode)") == 0 ) ret->device = DEVICE_SU_18; - else if ( strstr(product, "N900") ) + else if ( strcmp(product, "Nokia N800 Internet Tablet") == 0 || strcmp(product, "Nokia N800 (Update mode)") == 0 ) + ret->device = DEVICE_RX_34; + else if ( strcmp(product, "Nokia N810 Internet Tablet") == 0 || strcmp(product, "Nokia N810 (Update mode)") == 0 ) + ret->device = DEVICE_RX_44; + else if ( strcmp(product, "Nokia N810 Internet Tablet WiMAX Edition") == 0 || strcmp(product, "Nokia-RX48 (Update mode)") == 0 ) + ret->device = DEVICE_RX_48; + else if ( strcmp(product, "N900 (Storage Mode)") == 0 || strcmp(product, "Nokia N900 (Update mode)") == 0 || strcmp(product, "N900 (PC-Suite Mode)") == 0 ) ret->device = DEVICE_RX_51; + else if ( strcmp(product, "Nokia USB ROM") == 0 ) + ret->device = DEVICE_ANY; else ret->device = DEVICE_UNKNOWN; - /* TODO: Autodetect more devices */ - if ( device_to_string(ret->device) ) PRINTF_LINE("Detected USB device: %s", device_to_string(ret->device)); else PRINTF_LINE("Detected USB device: (not detected)"); PRINTF_END(); - if ( ret->device ) { + if ( ! noverify && ret->device == DEVICE_UNKNOWN ) { + ERROR("Device detection failed"); + fprintf(stderr, "\n"); + usb_reattach_kernel_driver(udev, usb_devices[i].interface); + libusb_close(udev); + free(ret); + return NULL; + } + + if ( ! noverify && ret->device != DEVICE_ANY ) { enum device * device; for ( device = usb_devices[i].devices; *device; ++device ) if ( *device == ret->device ) -- cgit v1.2.3 From 96e4fa5995f461a1cd33c6313172133dc3f3a15b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 12 Mar 2016 11:36:50 +0100 Subject: mkii: Fix memory leak, call libusb_free_config_descriptor() --- src/mkii.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mkii.c b/src/mkii.c index 3217557..a3766a2 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -165,6 +165,8 @@ int mkii_init(struct usb_device_info * dev) { libusb_get_string_descriptor_ascii(dev->udev, desc->iConfiguration, (unsigned char*)buf, sizeof(buf)); if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) dev->data |= (1UL << 31); + if ( ret == 0 ) + libusb_free_config_descriptor(desc); printf("Mode: %s\n", (dev->data & (1UL << 31)) ? "Update" : "PC Suite"); -- cgit v1.2.3 From 7b3e9bcf000551ceb64a8607138e6d0125aea6c6 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 12 Mar 2016 11:38:00 +0100 Subject: mkii: Use libusb_get_active_config_descriptor() instead libusb_get_config_descriptor() This will fix problem that it was used incorrect config index --- src/mkii.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mkii.c b/src/mkii.c index a3766a2..7e307b8 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -160,7 +160,7 @@ int mkii_init(struct usb_device_info * dev) { memset(buf, 0, sizeof(buf)); udev = libusb_get_device(dev->udev); - ret = libusb_get_config_descriptor(udev, dev->flash_device->configuration, &desc); + ret = libusb_get_active_config_descriptor(udev, &desc); if ( ret == 0 ) libusb_get_string_descriptor_ascii(dev->udev, desc->iConfiguration, (unsigned char*)buf, sizeof(buf)); if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) -- cgit v1.2.3 From 54805eb0409bb9fda8cef33b9a5ae513d430999b Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 12 Mar 2016 12:01:27 +0100 Subject: mkii: Define MKII_UPDATE_MODE instead hardcoded constant --- src/mkii.c | 4 ++-- src/mkii.h | 2 ++ src/usb-device.c | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index 7e307b8..aa02432 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -164,11 +164,11 @@ int mkii_init(struct usb_device_info * dev) { if ( ret == 0 ) libusb_get_string_descriptor_ascii(dev->udev, desc->iConfiguration, (unsigned char*)buf, sizeof(buf)); if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) - dev->data |= (1UL << 31); + dev->data |= MKII_UPDATE_MODE; if ( ret == 0 ) libusb_free_config_descriptor(desc); - printf("Mode: %s\n", (dev->data & (1UL << 31)) ? "Update" : "PC Suite"); + printf("Mode: %s\n", (dev->data & MKII_UPDATE_MODE) ? "Update" : "PC Suite"); return 0; diff --git a/src/mkii.h b/src/mkii.h index d4d5c31..11fbd2a 100644 --- a/src/mkii.h +++ b/src/mkii.h @@ -24,6 +24,8 @@ #include "device.h" #include "usb-device.h" +#define MKII_UPDATE_MODE (1UL << 31) + int mkii_init(struct usb_device_info * dev); enum device mkii_get_device(struct usb_device_info * dev); diff --git a/src/usb-device.c b/src/usb-device.c index e0c4074..895e56f 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -377,7 +377,7 @@ void usb_switch_to_update(struct usb_device_info * dev) { leave_cold_flash(dev); else if ( dev->flash_device->protocol == FLASH_NOLO ) nolo_boot_device(dev, "update"); - else if ( dev->flash_device->protocol == FLASH_MKII && ! ( dev->data & ( 1UL << 31 ) ) ) + else if ( dev->flash_device->protocol == FLASH_MKII && ! ( dev->data & MKII_UPDATE_MODE ) ) mkii_reboot_device(dev); else if ( dev->flash_device->protocol == FLASH_DISK ) printf_and_wait("Unplug USB cable, turn device off, press ENTER and plug USB cable again"); @@ -394,7 +394,7 @@ void usb_switch_to_disk(struct usb_device_info * dev) { nolo_boot_device(dev, NULL); printf_and_wait("Wait until device start, choose USB Mass Storage Mode and press ENTER"); } else if ( dev->flash_device->protocol == FLASH_MKII ) { - if ( dev->data & ( 1UL << 31 ) ) + if ( dev->data & MKII_UPDATE_MODE ) mkii_reboot_device(dev); else printf_and_wait("Unplug USB cable, plug again, choose USB Mass Storage Mode and press ENTER"); -- cgit v1.2.3 From d2e7b3d2425c9202d10a7659ed8b5d4398b03ace Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 12 Mar 2016 12:47:41 +0100 Subject: mkii: Implement rebooting to Update mode --- src/mkii.c | 18 ++++++++++++++---- src/mkii.h | 2 +- src/operations.c | 4 +++- src/usb-device.c | 8 ++++---- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index aa02432..7b36e43 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -340,18 +340,28 @@ int mkii_flash_image(struct usb_device_info * dev, struct image * image) { } -int mkii_reboot_device(struct usb_device_info * dev) { +int mkii_reboot_device(struct usb_device_info * dev, int update) { char buf[2048]; struct mkii_message * msg; + const char * str; + int len; int ret; msg = (struct mkii_message *)buf; - printf("Rebooting device...\n"); + if ( update ) { + printf("Rebooting device to Update mode...\n"); + len = sizeof("reboot=update"); + str = "reboot=update"; + } else { + printf("Rebooting device...\n"); + len = sizeof("reboot"); + str = "reboot"; + } - memcpy(msg->data, "reboot", sizeof("reboot")); - ret = mkii_send_receive(dev->udev, MKII_REBOOT, msg, sizeof("reboot"), msg, sizeof(buf)); + memcpy(msg->data, str, len); + ret = mkii_send_receive(dev->udev, MKII_REBOOT, msg, len, msg, sizeof(buf)); if ( ret != 1 || msg->data[0] != 0 ) return -1; diff --git a/src/mkii.h b/src/mkii.h index 11fbd2a..c15c61b 100644 --- a/src/mkii.h +++ b/src/mkii.h @@ -31,7 +31,7 @@ int mkii_init(struct usb_device_info * dev); enum device mkii_get_device(struct usb_device_info * dev); int mkii_flash_image(struct usb_device_info * dev, struct image * image); -int mkii_reboot_device(struct usb_device_info * dev); +int mkii_reboot_device(struct usb_device_info * dev, int update); int mkii_get_root_device(struct usb_device_info * dev); int mkii_set_root_device(struct usb_device_info * dev, int device); diff --git a/src/operations.c b/src/operations.c index 8773b95..1fd4766 100644 --- a/src/operations.c +++ b/src/operations.c @@ -243,6 +243,8 @@ int dev_boot_device(struct device_info * dev, const char * cmdline) { if ( protocol == FLASH_NOLO ) return nolo_boot_device(dev->usb, cmdline); + else if ( protocol == FLASH_MKII && strcmp(cmdline, "update") == 0 ) + return mkii_reboot_device(dev->usb, 1); usb_switch_to_nolo(dev->usb); return -EAGAIN; @@ -267,7 +269,7 @@ int dev_reboot_device(struct device_info * dev) { else if ( protocol == FLASH_NOLO ) return nolo_reboot_device(dev->usb); else if ( protocol == FLASH_MKII ) - return mkii_reboot_device(dev->usb); + return mkii_reboot_device(dev->usb, 0); else { usb_switch_to_nolo(dev->usb); return -EAGAIN; diff --git a/src/usb-device.c b/src/usb-device.c index 895e56f..670eb9e 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -350,7 +350,7 @@ void usb_switch_to_nolo(struct usb_device_info * dev) { if ( dev->flash_device->protocol == FLASH_COLD ) leave_cold_flash(dev); else if ( dev->flash_device->protocol == FLASH_MKII ) - mkii_reboot_device(dev); + mkii_reboot_device(dev, 0); else if ( dev->flash_device->protocol == FLASH_DISK ) printf_and_wait("Unplug USB cable, turn device off, press ENTER and plug USB cable again"); @@ -363,7 +363,7 @@ void usb_switch_to_cold(struct usb_device_info * dev) { if ( dev->flash_device->protocol == FLASH_NOLO ) nolo_reboot_device(dev); else if ( dev->flash_device->protocol == FLASH_MKII ) - mkii_reboot_device(dev); + mkii_reboot_device(dev, 0); else if ( dev->flash_device->protocol == FLASH_DISK ) printf_and_wait("Unplug USB cable, turn device off, press ENTER and plug USB cable again"); @@ -378,7 +378,7 @@ void usb_switch_to_update(struct usb_device_info * dev) { else if ( dev->flash_device->protocol == FLASH_NOLO ) nolo_boot_device(dev, "update"); else if ( dev->flash_device->protocol == FLASH_MKII && ! ( dev->data & MKII_UPDATE_MODE ) ) - mkii_reboot_device(dev); + mkii_reboot_device(dev, 1); else if ( dev->flash_device->protocol == FLASH_DISK ) printf_and_wait("Unplug USB cable, turn device off, press ENTER and plug USB cable again"); @@ -395,7 +395,7 @@ void usb_switch_to_disk(struct usb_device_info * dev) { printf_and_wait("Wait until device start, choose USB Mass Storage Mode and press ENTER"); } else if ( dev->flash_device->protocol == FLASH_MKII ) { if ( dev->data & MKII_UPDATE_MODE ) - mkii_reboot_device(dev); + mkii_reboot_device(dev, 0); else printf_and_wait("Unplug USB cable, plug again, choose USB Mass Storage Mode and press ENTER"); } -- cgit v1.2.3 From cf95375177c0ee8d96a87b77d64c999070d6e805 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 12 May 2016 00:12:55 +0200 Subject: disk: Fix detection of MyDocs partition in DISK RAW mode for non RX-51 devices --- src/disk.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/disk.c b/src/disk.c index 96a8d8a..4a34068 100644 --- a/src/disk.c +++ b/src/disk.c @@ -264,11 +264,15 @@ int disk_init(struct usb_device_info * dev) { #ifdef __linux__ int fd; - int maj; - int min; + int maj1; + int maj2; + int min1; + int min2; - maj = -1; - min = -1; + maj1 = -1; + maj2 = -1; + min1 = -1; + min2 = -1; FILE * f; DIR * dir; @@ -333,25 +337,39 @@ int disk_init(struct usb_device_info * dev) { if ( devnum != usbdevnum || usbbusnum != busnum ) continue; - if ( sscanf(dirent->d_name, "%d:%d", &maj, &min) != 2 ) { - maj = -1; - min = -1; + if ( sscanf(dirent->d_name, "%d:%d", &maj2, &min2) != 2 ) { + maj2 = -1; + min2 = -1; continue; } - break; + if ( maj1 != -1 && min1 != -1 && maj2 != -1 && min2 != -1 ) + break; + + maj1 = maj2; + min1 = min2; + maj2 = -1; + min2 = -1; } closedir(dir); - if ( maj == -1 || min == -1 ) { + if ( maj1 == -1 || min1 == -1 ) { ERROR("Cannot find id for mmc block disk device"); return -1; } /* TODO: change 1 to 0 when disk_flash_dev will be implemented */ - fd = disk_open_dev(maj, min, -1, 1); + + /* RX-51 exports MyDocs in first usb device and just first partion, so host system see whole device without MBR table */ + if ( dev->device == DEVICE_RX_51 ) + fd = disk_open_dev(maj1, min1, -1, 1); + /* Other devices can export SD card as first partition and export whole mmc device, so host system will see MBR table */ + else if ( maj2 != -1 && min2 != -1 ) + fd = disk_open_dev(maj2, min2, 1, 1); + else + fd = disk_open_dev(maj1, min1, 1, 1); if ( fd < 0 ) return -1; -- cgit v1.2.3 From 8a433f2d7af63438dc2ea9fa8d787177caf71e0e Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Thu, 12 May 2016 00:18:02 +0200 Subject: usb-device: Do not reattach kernel driver if USB interface is not used --- src/usb-device.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/usb-device.c b/src/usb-device.c index 670eb9e..a7fdf6b 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -78,6 +78,9 @@ static void usb_flash_device_info_print(const struct usb_flash_device * dev) { static void usb_reattach_kernel_driver(libusb_device_handle * udev, int interface) { + if ( interface < 0 ) + return; + PRINTF_LINE("Reattach kernel driver to USB interface..."); PRINTF_END(); libusb_release_interface(udev, interface); -- cgit v1.2.3 From 61d77eed68615d601716e7ca68203e038b17acd4 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Mon, 27 Jun 2016 17:24:05 +0200 Subject: mkii: Call /version/sw_release only for protocol version 2 Looks like command /version/sw_release is supported only by protocol version 2. --- src/mkii.c | 30 +++++++++++++++++++----------- src/mkii.h | 1 + 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/mkii.c b/src/mkii.c index 7b36e43..b43c551 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -105,17 +105,24 @@ int mkii_init(struct usb_device_info * dev) { ret = mkii_send_receive(dev->udev, MKII_PING, msg, 0, msg, sizeof(buf)); if ( ret != 0 ) - return -1; + ERROR_RETURN("Cannot ping device", -1); memcpy(msg->data, "/update/protocol_version", sizeof("/update/protocol_version")-1); ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/update/protocol_version")-1, msg, sizeof(buf)); - if ( ret != 2 || msg->data[0] != 0 || msg->data[1] != 0x32 ) - return -1; + if ( ret < 2 || msg->data[0] != 0 ) + ERROR_RETURN("Cannot get Mk II protocol version", -1); + + msg->data[ret] = 0; + + if ( ret == 2 && msg->data[1] == 0x32 ) + dev->data |= MKII_SUPPORT_SW_RELEASE; + + printf("Detected Mk II protocol version: %s\n", msg->data); memcpy(msg->data, "/update/host_protocol_version\x00\x32", sizeof("/update/host_protocol_version\x00\x32")-1); ret = mkii_send_receive(dev->udev, MKII_TELL, msg, sizeof("/update/host_protocol_version\x00\x32")-1, msg, sizeof(buf)); if ( ret != 1 || msg->data[0] != 0 ) - return -1; + ERROR_RETURN("Cannot send our protocol version", -1); device = mkii_get_device(dev); @@ -131,10 +138,8 @@ int mkii_init(struct usb_device_info * dev) { memcpy(msg->data, "/update/supported_images", sizeof("/update/supported_images")-1); ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/update/supported_images")-1, msg, sizeof(buf)); - if ( ret < 2 || msg->data[0] != 0 ) { - ERROR("Cannot get supported image types"); - return -1; - } + if ( ret < 2 || msg->data[0] != 0 ) + ERROR_RETURN("Cannot get supported image types", -1); msg->data[ret] = 0; ptr = msg->data + 1; @@ -363,7 +368,7 @@ int mkii_reboot_device(struct usb_device_info * dev, int update) { memcpy(msg->data, str, len); ret = mkii_send_receive(dev->udev, MKII_REBOOT, msg, len, msg, sizeof(buf)); if ( ret != 1 || msg->data[0] != 0 ) - return -1; + ERROR_RETURN("Cannot send reboot command", -1); return 0; @@ -450,7 +455,7 @@ int16_t mkii_get_hwrev(struct usb_device_info * dev) { memcpy(msg->data, "/device/hw_build", sizeof("/device/hw_build")-1); ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/device/hw_build")-1, msg, sizeof(buf)); if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) - return -1; + ERROR_RETURN("Cannot get hw revision", -1); msg->data[ret] = 0; return atoi(msg->data+1); @@ -529,12 +534,15 @@ int mkii_get_sw_ver(struct usb_device_info * dev, char * ver, size_t size) { struct mkii_message * msg; int ret; + if ( ! ( dev->data & MKII_SUPPORT_SW_RELEASE ) ) + return -1; + msg = (struct mkii_message *)buf; memcpy(msg->data, "/version/sw_release", sizeof("/version/sw_release")-1); ret = mkii_send_receive(dev->udev, MKII_GET, msg, sizeof("/version/sw_release")-1, msg, sizeof(buf)); if ( ret < 2 || msg->data[0] != 0 || msg->data[1] == 0 ) - return -1; + ERROR_RETURN("Cannot get sw release", -1); msg->data[ret] = 0; strncpy(ver, msg->data+1, size); diff --git a/src/mkii.h b/src/mkii.h index c15c61b..2731eed 100644 --- a/src/mkii.h +++ b/src/mkii.h @@ -24,6 +24,7 @@ #include "device.h" #include "usb-device.h" +#define MKII_SUPPORT_SW_RELEASE (1UL << 30) #define MKII_UPDATE_MODE (1UL << 31) int mkii_init(struct usb_device_info * dev); -- cgit v1.2.3 From ea7f2a441a68bc4496733a2fcabc6bf8d2962d04 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Mon, 27 Jun 2016 17:24:51 +0200 Subject: all: Add support for Nokia N950 (RM-680) --- src/device.c | 2 ++ src/device.h | 1 + src/usb-device.c | 8 +++++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/device.c b/src/device.c index 2ac5cb6..d50d129 100644 --- a/src/device.c +++ b/src/device.c @@ -31,6 +31,7 @@ static const char * devices[] = { [DEVICE_RX_44] = "RX-44", [DEVICE_RX_48] = "RX-48", [DEVICE_RX_51] = "RX-51", + [DEVICE_RM_680] = "RM-680", }; enum device device_from_string(const char * device) { @@ -63,6 +64,7 @@ static const char * long_devices[] = { [DEVICE_RX_44] = "Nokia N810", [DEVICE_RX_48] = "Nokia N810 Wimax", [DEVICE_RX_51] = "Nokia N900", + [DEVICE_RM_680] = "Nokia N950", }; const char * device_to_long_string(enum device device) { diff --git a/src/device.h b/src/device.h index 4ea3f0f..9b58dbe 100644 --- a/src/device.h +++ b/src/device.h @@ -30,6 +30,7 @@ enum device { DEVICE_RX_44, /* Nokia N810 */ DEVICE_RX_48, /* Nokia N810 WiMax */ DEVICE_RX_51, /* Nokia N900 */ + DEVICE_RM_680, /* Nokia N950 */ DEVICE_COUNT, }; diff --git a/src/usb-device.c b/src/usb-device.c index a7fdf6b..3ca4579 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -37,11 +37,11 @@ static struct usb_flash_device usb_devices[] = { { 0x0421, 0x0096, -1, -1, -1, FLASH_DISK, { DEVICE_RX_44, 0 } }, - { 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, 0x0105, 2, 1, -1, FLASH_NOLO, { DEVICE_SU_18, DEVICE_RX_44, DEVICE_RX_48, DEVICE_RX_51, DEVICE_RM_680, 0 } }, + { 0x0421, 0x0106, 0, -1, -1, FLASH_COLD, { DEVICE_RX_51, DEVICE_RM_680, 0 } }, { 0x0421, 0x0189, -1, -1, -1, FLASH_DISK, { DEVICE_RX_48, 0 } }, { 0x0421, 0x01c7, -1, -1, -1, FLASH_DISK, { DEVICE_RX_51, 0 } }, - { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, 0 } }, + { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, DEVICE_RM_680, 0 } }, { 0x0421, 0x0431, -1, -1, -1, FLASH_DISK, { DEVICE_SU_18, DEVICE_RX_34, 0 } }, { 0x0421, 0x04c3, -1, -1, -1, FLASH_DISK, { DEVICE_RX_34, 0 } }, { 0x0421, 0x3f00, 2, 1, -1, FLASH_NOLO, { DEVICE_RX_34, 0 } }, @@ -222,6 +222,8 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) ret->device = DEVICE_RX_48; else if ( strcmp(product, "N900 (Storage Mode)") == 0 || strcmp(product, "Nokia N900 (Update mode)") == 0 || strcmp(product, "N900 (PC-Suite Mode)") == 0 ) ret->device = DEVICE_RX_51; + else if ( strcmp(product, "Sync Mode") == 0 ) + ret->device = DEVICE_RM_680; else if ( strcmp(product, "Nokia USB ROM") == 0 ) ret->device = DEVICE_ANY; else -- cgit v1.2.3 From 676da477510cb1966a74273f89c8aa64654a1d49 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 6 Dec 2016 22:39:44 +0100 Subject: all: Revert libusb-1.0 support Listing of usb devices with libusb-1.0 is too slow and not usable for flashing or cold-flashing Nokia N900. Old libusb (0.1) does not have this problem and works perfectly. Problem for libusb-1.0 was reported at least two times into libusb-devel mailing list, but upstream developers are totally ignore it. One message is in archive: https://sourceforge.net/p/libusb/mailman/message/34985373/ Because of that there is just one option: use tested and working libusb 0.1 library instead some experimental and non-working libusb-1.0. --- .travis.yml | 2 +- src/Makefile | 2 +- src/cold-flash.c | 70 ++++++++++++-------------- src/disk.c | 14 ++---- src/mkii.c | 28 ++++------- src/nolo.c | 55 ++++++++++----------- src/usb-device.c | 148 +++++++++++++++++++++++++++++++++---------------------- src/usb-device.h | 10 ++-- 8 files changed, 166 insertions(+), 163 deletions(-) diff --git a/.travis.yml b/.travis.yml index 65fbec7..b623f9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ compiler: - clang before_install: - sudo apt-get update -qq - - sudo apt-get install -qq -y libusb-1.0-0-dev + - sudo apt-get install -qq -y libusb-dev script: if [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make ; fi matrix: diff --git a/src/Makefile b/src/Makefile index 6682436..6b8ef08 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,7 +11,7 @@ HOST_CC = $(HOST_COMPILE)$(CC) CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L CFLAGS += -W -Wall -O2 -pedantic -std=c99 -LIBS += -lusb-1.0 +LIBS += -lusb DEPENDS = Makefile ../config.mk diff --git a/src/cold-flash.c b/src/cold-flash.c index a6a11b6..b4fe4e6 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -22,7 +22,7 @@ #include #include -#include +#include #include "global.h" #include "cold-flash.h" @@ -169,37 +169,35 @@ struct xloader_msg xloader_msg_create(uint32_t type, struct image * image) { } -static int read_asic(libusb_device_handle * udev, uint8_t * asic_buffer, int size, int asic_size) { +static int read_asic(usb_dev_handle * udev, uint8_t * asic_buffer, int size, int asic_size) { - int ret, transferred; + int ret; printf("Waiting for ASIC ID...\n"); - ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)asic_buffer, size, &transferred, READ_TIMEOUT); - if ( ret < 0 ) - ERROR_RETURN("Cannot read ASIC ID", -1); - if ( transferred != asic_size ) + ret = usb_bulk_read(udev, USB_READ_EP, (char *)asic_buffer, size, READ_TIMEOUT); + if ( ret != asic_size ) ERROR_RETURN("Invalid size of ASIC ID", -1); return 0; } -static int send_2nd(libusb_device_handle * udev, struct image * image) { +static int send_2nd(usb_dev_handle * udev, struct image * image) { uint8_t buffer[1024]; uint32_t need, sent; - int ret, transferred; + int ret; printf("Sending OMAP peripheral boot message...\n"); - ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), &transferred, WRITE_TIMEOUT); - if ( ret < 0 || transferred != sizeof(omap_peripheral_msg) ) + ret = usb_bulk_write(udev, USB_WRITE_EP, (char *)&omap_peripheral_msg, sizeof(omap_peripheral_msg), WRITE_TIMEOUT); + if ( ret != sizeof(omap_peripheral_msg) ) ERROR_RETURN("Sending OMAP peripheral boot message failed", -1); SLEEP(5000); printf("Sending 2nd X-Loader image size...\n"); - ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)&image->size, 4, &transferred, WRITE_TIMEOUT); - if ( ret < 0 || transferred != 4 ) + ret = usb_bulk_write(udev, USB_WRITE_EP, (char *)&image->size, 4, WRITE_TIMEOUT); + if ( ret != 4 ) ERROR_RETURN("Sending 2nd X-Loader image size failed", -1); SLEEP(5000); @@ -215,11 +213,9 @@ static int send_2nd(libusb_device_handle * udev, struct image * image) { ret = image_read(image, buffer, need); if ( ret == 0 ) break; - if ( libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) + if ( usb_bulk_write(udev, USB_WRITE_EP, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed", -1); - if ( ret != transferred ) - PRINTF_ERROR_RETURN("Sending 2nd X-Loader image failed (incomplete bulk transfer)", -1); - sent += transferred; + sent += ret; printf_progressbar(sent, image->size); } @@ -228,24 +224,24 @@ static int send_2nd(libusb_device_handle * udev, struct image * image) { } -static int send_secondary(libusb_device_handle * udev, struct image * image) { +static int send_secondary(usb_dev_handle * udev, struct image * image) { struct xloader_msg init_msg; uint8_t buffer[1024]; uint32_t need, sent; - int ret, transferred; + int ret; init_msg = xloader_msg_create(XLOADER_MSG_TYPE_SEND, image); printf("Sending X-Loader init message...\n"); - ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)&init_msg, sizeof(init_msg), &transferred, WRITE_TIMEOUT); - if ( ret < 0 || transferred != sizeof(init_msg) ) + ret = usb_bulk_write(udev, USB_WRITE_EP, (char *)&init_msg, sizeof(init_msg), WRITE_TIMEOUT); + if ( ret != sizeof(init_msg) ) ERROR_RETURN("Sending X-Loader init message failed", -1); printf("Waiting for X-Loader response...\n"); SLEEP(5000); - ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ - if ( ret < 0 || transferred != 4 ) + ret = usb_bulk_read(udev, USB_READ_EP, (char *)&buffer, 4, READ_TIMEOUT); /* 4 bytes - dummy value */ + if ( ret != 4 ) ERROR_RETURN("No response", -1); printf("Sending Secondary image...\n"); @@ -259,27 +255,25 @@ static int send_secondary(libusb_device_handle * udev, struct image * image) { ret = image_read(image, buffer, need); if ( ret == 0 ) break; - if ( libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)buffer, ret, &transferred, WRITE_TIMEOUT) < 0 ) + if ( usb_bulk_write(udev, USB_WRITE_EP, (char *)buffer, ret, WRITE_TIMEOUT) != ret ) PRINTF_ERROR_RETURN("Sending Secondary image failed", -1); - if ( ret != transferred ) - PRINTF_ERROR_RETURN("Sending Secondary image failed (incomplete bulk transfer)", -1); - sent += transferred; + sent += ret; printf_progressbar(sent, image->size); } printf("Waiting for X-Loader response...\n"); SLEEP(5000); - ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)&buffer, 4, &transferred, READ_TIMEOUT); /* 4 bytes - dummy value */ - if ( ret < 0 || transferred != 4 ) + ret = usb_bulk_read(udev, USB_READ_EP, (char *)&buffer, 4, READ_TIMEOUT); /* 4 bytes - dummy value */ + if ( ret != 4 ) ERROR_RETURN("No response", -1); return 0; } -static int ping_timeout(libusb_device_handle * udev) { +static int ping_timeout(usb_dev_handle * udev) { - int ret, transferred; + int ret; int pong = 0; int try_ping = 10; @@ -289,16 +283,16 @@ static int ping_timeout(libusb_device_handle * udev) { int try_read = 4; printf("Sending X-Loader ping message\n"); - ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)&ping_msg, sizeof(ping_msg), &transferred, WRITE_TIMEOUT); - if ( ret < 0 || transferred != sizeof(ping_msg) ) + ret = usb_bulk_write(udev, USB_WRITE_EP, (char *)&ping_msg, sizeof(ping_msg), WRITE_TIMEOUT); + if ( ret != sizeof(ping_msg) ) ERROR_RETURN("Sending X-Loader ping message failed", -1); printf("Waiting for X-Loader pong response...\n"); while ( try_read > 0 ) { uint32_t ping_read; - ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)&ping_read, sizeof(ping_read), &transferred, READ_TIMEOUT); - if ( ret == 0 && transferred == sizeof(ping_read) ) { + ret = usb_bulk_read(udev, USB_READ_EP, (char *)&ping_read, sizeof(ping_read), READ_TIMEOUT); + if ( ret == sizeof(ping_read) ) { printf("Got it\n"); pong = 1; break; @@ -410,11 +404,11 @@ int cold_flash(struct usb_device_info * dev, struct image * x2nd, struct image * int leave_cold_flash(struct usb_device_info * dev) { - int ret, transferred; + int ret; printf("Sending OMAP memory boot message...\n"); - ret = libusb_bulk_transfer(dev->udev, USB_WRITE_EP, (unsigned char *)&omap_memory_msg, sizeof(omap_memory_msg), &transferred, WRITE_TIMEOUT); - if ( ret < 0 || transferred != sizeof(omap_memory_msg) ) + ret = usb_bulk_write(dev->udev, USB_WRITE_EP, (char *)&omap_memory_msg, sizeof(omap_memory_msg), WRITE_TIMEOUT); + if ( ret != sizeof(omap_memory_msg) ) ERROR_RETURN("Sending OMAP memory boot message failed", -1); SLEEP(250000); diff --git a/src/disk.c b/src/disk.c index 4a34068..b8e9f51 100644 --- a/src/disk.c +++ b/src/disk.c @@ -281,20 +281,14 @@ int disk_init(struct usb_device_info * dev) { unsigned int devnum; unsigned int busnum; - uint8_t usbdevnum; - uint8_t usbbusnum; + struct usb_device * device; - struct libusb_device * device; - - device = libusb_get_device(dev->udev); - if ( ! device ) { + device = usb_device(dev->udev); + if ( ! device || ! device->bus ) { ERROR_INFO("Cannot read usb devnum and busnum"); return -1; } - usbbusnum = libusb_get_bus_number(device); - usbdevnum = libusb_get_device_address(device); - dir = opendir("/sys/dev/block/"); if ( ! dir ) { ERROR_INFO("Cannot open '/sys/dev/block/' directory"); @@ -334,7 +328,7 @@ int disk_init(struct usb_device_info * dev) { fclose(f); - if ( devnum != usbdevnum || usbbusnum != busnum ) + if ( devnum != device->devnum || device->bus->location != busnum ) continue; if ( sscanf(dirent->d_name, "%d:%d", &maj2, &min2) != 2 ) { diff --git a/src/mkii.c b/src/mkii.c index b43c551..0c6e655 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -48,9 +48,9 @@ struct mkii_message { } __attribute__((__packed__)); -static int mkii_send_receive(libusb_device_handle * udev, uint8_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { +static int mkii_send_receive(usb_dev_handle * udev, uint8_t type, struct mkii_message * in_msg, size_t data_size, struct mkii_message * out_msg, size_t out_size) { - int ret, transferred; + int ret; static uint8_t number = 0; in_msg->header = MKII_OUT; @@ -59,17 +59,15 @@ static int mkii_send_receive(libusb_device_handle * udev, uint8_t type, struct m in_msg->num = number++; in_msg->type = type; - ret = libusb_bulk_transfer(udev, USB_WRITE_EP, (unsigned char *)in_msg, data_size + sizeof(*in_msg), &transferred, 5000); + ret = usb_bulk_write(udev, USB_WRITE_EP, (char *)in_msg, data_size + sizeof(*in_msg), 5000); if ( ret < 0 ) return ret; - if ( (size_t)transferred != data_size + sizeof(*in_msg) ) + if ( (size_t)ret != data_size + sizeof(*in_msg) ) return -1; - ret = libusb_bulk_transfer(udev, USB_READ_EP, (unsigned char *)out_msg, out_size, &transferred, 5000); + ret = usb_bulk_read(udev, USB_READ_EP, (char *)out_msg, out_size, 5000); if ( ret < 0 ) return ret; - if ( (size_t)transferred < sizeof(*out_msg) ) - return -1; if ( out_msg->header != MKII_IN ) return -1; @@ -77,13 +75,13 @@ static int mkii_send_receive(libusb_device_handle * udev, uint8_t type, struct m if ( out_msg->type != (type | MKII_RESPONCE) ) return -1; - if ( (size_t)transferred < sizeof(*out_msg) ) + if ( (size_t)ret < sizeof(*out_msg) ) return -1; - if ( ntohs(out_msg->size) != transferred - sizeof(*out_msg) + 4 ) + if ( ntohs(out_msg->size) != ret - sizeof(*out_msg) + 4 ) return -1; - return transferred - sizeof(*out_msg); + return ret - sizeof(*out_msg); } @@ -96,8 +94,6 @@ int mkii_init(struct usb_device_info * dev) { char * newptr; char * ptr; enum image_type type; - struct libusb_device *udev; - struct libusb_config_descriptor *desc; printf("Initializing Mk II protocol...\n"); @@ -163,15 +159,9 @@ int mkii_init(struct usb_device_info * dev) { printf("\n"); memset(buf, 0, sizeof(buf)); - - udev = libusb_get_device(dev->udev); - ret = libusb_get_active_config_descriptor(udev, &desc); - if ( ret == 0 ) - libusb_get_string_descriptor_ascii(dev->udev, desc->iConfiguration, (unsigned char*)buf, sizeof(buf)); + usb_get_string_simple(dev->udev, usb_device(dev->udev)->config[dev->flash_device->configuration].iConfiguration, buf, sizeof(buf)); if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) dev->data |= MKII_UPDATE_MODE; - if ( ret == 0 ) - libusb_free_config_descriptor(desc); printf("Mode: %s\n", (dev->data & MKII_UPDATE_MODE) ? "Update" : "PC Suite"); diff --git a/src/nolo.c b/src/nolo.c index 4f5ec27..1dd9d61 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -22,7 +22,7 @@ #include #include -#include +#include #include "nolo.h" #include "image.h" @@ -90,7 +90,7 @@ static void nolo_error_log(struct usb_device_info * dev, int only_clear) { memset(buf, 0, sizeof(buf)); - ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_ERROR_LOG, 0, 0, (unsigned char *)buf, sizeof(buf), 2000); + ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_ERROR_LOG, 0, 0, buf, sizeof(buf), 2000); if ( ret < 0 ) break; @@ -120,7 +120,7 @@ static int nolo_identify_string(struct usb_device_info * dev, const char * str, memset(buf, 0, sizeof(buf)); - ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_IDENTIFY, 0, 0, (unsigned char *)buf, sizeof(buf), 2000); + ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_IDENTIFY, 0, 0, (char *)buf, sizeof(buf), 2000); if ( ret < 0 ) NOLO_ERROR_RETURN("NOLO_IDENTIFY failed", -1); @@ -150,10 +150,10 @@ static int nolo_set_string(struct usb_device_info * dev, char * str, char * arg) if ( simulate ) return 0; - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, (unsigned char*)str, strlen(str), 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, str, strlen(str), 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_STRING failed", -1); - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET_STRING, 0, 0, (unsigned char*)arg, strlen(arg), 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET_STRING, 0, 0, arg, strlen(arg), 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_SET_STRING failed", -1); return 0; @@ -164,10 +164,10 @@ static int nolo_get_string(struct usb_device_info * dev, char * str, char * out, int ret = 0; - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, (unsigned char*)str, strlen(str), 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_STRING, 0, 0, str, strlen(str), 2000) < 0 ) return -1; - if ( ( ret = libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET_STRING, 0, 0, (unsigned char*)out, size-1, 2000) ) < 0 ) + if ( ( ret = usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET_STRING, 0, 0, out, size-1, 2000) ) < 0 ) return -1; if ( (size_t)ret > size-1 ) @@ -211,7 +211,7 @@ int nolo_init(struct usb_device_info * dev) { printf("Initializing NOLO...\n"); while ( val != 0 ) - if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_STATUS, 0, 0, (unsigned char *)&val, 4, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_STATUS, 0, 0, (char *)&val, 4, 2000) == -1 ) NOLO_ERROR_RETURN("NOLO_STATUS failed", -1); /* clear error log */ @@ -256,7 +256,6 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i uint32_t sent; int request; int ret; - int transferred; if ( flash ) printf("Send and flash image:\n"); @@ -371,7 +370,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i printf("Sending image header...\n"); if ( ! simulate ) { - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, request, 0, 0, (unsigned char*)buf, ptr-buf, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, request, 0, 0, buf, ptr-buf, 2000) < 0 ) NOLO_ERROR_RETURN("Sending image header failed", -1); } @@ -390,14 +389,10 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i if ( ret == 0 ) break; if ( ! simulate ) { - if ( libusb_bulk_transfer(dev->udev, USB_WRITE_DATA_EP, (unsigned char*)buf, ret, &transferred, 5000) < 0 ) { + if ( usb_bulk_write(dev->udev, USB_WRITE_DATA_EP, buf, ret, 5000) != ret ) { PRINTF_END(); NOLO_ERROR_RETURN("Sending image failed", -1); } - if ( transferred != ret ) { - PRINTF_END(); - NOLO_ERROR_RETURN("Sending image was incomplete!", -1); - } } sent += ret; printf_progressbar(sent, image->size); @@ -406,7 +401,7 @@ static int nolo_send_image(struct usb_device_info * dev, struct image * image, i if ( flash ) { printf("Finishing flashing...\n"); if ( ! simulate ) { - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SEND_FLASH_FINISH, 0, 0, NULL, 0, 30000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SEND_FLASH_FINISH, 0, 0, NULL, 0, 30000) < 0 ) NOLO_ERROR_RETURN("Finishing failed", -1); } } @@ -463,7 +458,7 @@ int nolo_flash_image(struct usb_device_info * dev, struct image * image) { printf("Flashing image...\n"); if ( ! simulate ) { - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_FLASH_IMAGE, 0, index, NULL, 0, 10000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_FLASH_IMAGE, 0, index, NULL, 0, 10000) ) NOLO_ERROR_RETURN("Flashing failed", -1); } @@ -584,7 +579,7 @@ int nolo_boot_device(struct usb_device_info * dev, const char * cmdline) { cmdline = NULL; } - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_BOOT, mode, 0, (unsigned char *)cmdline, size, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_BOOT, mode, 0, (char *)cmdline, size, 2000) < 0 ) NOLO_ERROR_RETURN("Booting failed", -1); return 0; @@ -594,7 +589,7 @@ int nolo_boot_device(struct usb_device_info * dev, const char * cmdline) { int nolo_reboot_device(struct usb_device_info * dev) { printf("Rebooting device...\n"); - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_REBOOT, 0, 0, NULL, 0, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_REBOOT, 0, 0, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_REBOOT failed", -1); return 0; @@ -603,7 +598,7 @@ int nolo_reboot_device(struct usb_device_info * dev) { int nolo_get_root_device(struct usb_device_info * dev) { uint8_t device = 0; - if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ROOT_DEVICE, (unsigned char *)&device, 1, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ROOT_DEVICE, (char *)&device, 1, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get root device", -1); return device; @@ -614,7 +609,7 @@ int nolo_set_root_device(struct usb_device_info * dev, int device) { printf("Setting root device to %d...\n", device); if ( simulate ) return 0; - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, device, NOLO_ROOT_DEVICE, NULL, 0, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, device, NOLO_ROOT_DEVICE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot set root device", -1); return 0; @@ -623,7 +618,7 @@ int nolo_set_root_device(struct usb_device_info * dev, int device) { int nolo_get_usb_host_mode(struct usb_device_info * dev) { uint32_t enabled = 0; - if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_USB_HOST_MODE, (unsigned char *)&enabled, 4, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_USB_HOST_MODE, (void *)&enabled, 4, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get USB host mode status", -1); return enabled ? 1 : 0; @@ -634,7 +629,7 @@ int nolo_set_usb_host_mode(struct usb_device_info * dev, int enable) { printf("%s USB host mode...\n", enable ? "Enabling" : "Disabling"); if ( simulate ) return 0; - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_USB_HOST_MODE, NULL, 0, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_USB_HOST_MODE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot change USB host mode status", -1); return 0; @@ -643,7 +638,7 @@ int nolo_set_usb_host_mode(struct usb_device_info * dev, int enable) { int nolo_get_rd_mode(struct usb_device_info * dev) { uint8_t enabled = 0; - if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_RD_MODE, (unsigned char *)&enabled, 1, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_RD_MODE, (char *)&enabled, 1, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get R&D mode status", -1); return enabled ? 1 : 0; @@ -654,7 +649,7 @@ int nolo_set_rd_mode(struct usb_device_info * dev, int enable) { printf("%s R&D mode...\n", enable ? "Enabling" : "Disabling"); if ( simulate ) return 0; - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_RD_MODE, NULL, 0, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, enable, NOLO_RD_MODE, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot change R&D mode status", -1); return 0; @@ -667,7 +662,7 @@ int nolo_get_rd_flags(struct usb_device_info * dev, char * flags, size_t size) { uint16_t add_flags = 0; char * ptr = flags; - if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ADD_RD_FLAGS, (unsigned char *)&add_flags, 2, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET, 0, NOLO_ADD_RD_FLAGS, (char *)&add_flags, 2, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get R&D flags", -1); if ( add_flags & NOLO_RD_FLAG_NO_OMAP_WD ) @@ -767,10 +762,10 @@ int nolo_set_rd_flags(struct usb_device_info * dev, const char * flags) { if ( simulate ) return 0; - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, add_flags, NOLO_ADD_RD_FLAGS, NULL, 0, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, add_flags, NOLO_ADD_RD_FLAGS, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot add R&D flags", -1); - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET, del_flags, NOLO_DEL_RD_FLAGS, NULL, 0, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET, del_flags, NOLO_DEL_RD_FLAGS, NULL, 0, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot del R&D flags", -1); return 0; @@ -830,7 +825,7 @@ int nolo_get_nolo_ver(struct usb_device_info * dev, char * ver, size_t size) { uint32_t version = 0; - if ( libusb_control_transfer(dev->udev, NOLO_QUERY, NOLO_GET_NOLO_VERSION, 0, 0, (unsigned char *)&version, 4, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_QUERY, NOLO_GET_NOLO_VERSION, 0, 0, (char *)&version, 4, 2000) < 0 ) NOLO_ERROR_RETURN("Cannot get NOLO version", -1); if ( (version & 255) > 1 ) @@ -889,7 +884,7 @@ int nolo_set_sw_ver(struct usb_device_info * dev, const char * ver) { memcpy(ptr, ver, len); ptr += len; - if ( libusb_control_transfer(dev->udev, NOLO_WRITE, NOLO_SET_SW_RELEASE, 0, 0, (unsigned char*)buf, ptr-buf, 2000) < 0 ) + if ( usb_control_msg(dev->udev, NOLO_WRITE, NOLO_SET_SW_RELEASE, 0, 0, buf, ptr-buf, 2000) < 0 ) NOLO_ERROR_RETURN("NOLO_SET_SW_RELEASE failed", -1); return 0; diff --git a/src/usb-device.c b/src/usb-device.c index 3ca4579..d3aa707 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -25,7 +25,13 @@ #include #include -#include +#include + +#ifdef __linux__ +#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP +#include +#endif +#endif #include "global.h" #include "device.h" @@ -76,34 +82,42 @@ static void usb_flash_device_info_print(const struct usb_flash_device * dev) { } -static void usb_reattach_kernel_driver(libusb_device_handle * udev, int interface) { +static void usb_reattach_kernel_driver(usb_dev_handle * udev, int interface) { + +#ifdef __linux__ +#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP + struct { + int ifno; + int ioctl_code; + void * data; + } command = { + .ifno = interface, + .ioctl_code = _IO('U', 23), + .data = NULL, + }; if ( interface < 0 ) return; PRINTF_LINE("Reattach kernel driver to USB interface..."); PRINTF_END(); - libusb_release_interface(udev, interface); - libusb_attach_kernel_driver(udev, interface); + usb_release_interface(udev, interface); + ioctl(*((int *)udev), _IOWR('U', 18, command), &command); +#endif +#endif } -static void usb_descriptor_info_print(libusb_device_handle * udev, struct libusb_device * dev, char * product, size_t size) { +static void usb_descriptor_info_print(usb_dev_handle * udev, struct usb_device * dev, char * product, size_t size) { - struct libusb_device_descriptor desc; char buf[1024]; char buf2[1024]; unsigned int x; int ret; int i; - if ( libusb_get_device_descriptor(dev, &desc) < 0 ) { - PRINTF_LINE("libusb_get_device_descriptor() failed"); - PRINTF_END(); - return; - } memset(buf, 0, sizeof(buf)); - libusb_get_string_descriptor_ascii(udev, desc.iProduct, (unsigned char *)buf, sizeof(buf)); + usb_get_string_simple(udev, dev->descriptor.iProduct, buf, sizeof(buf)); PRINTF_LINE("USB device product string: %s", buf[0] ? buf : "(not detected)"); PRINTF_END(); @@ -112,7 +126,7 @@ static void usb_descriptor_info_print(libusb_device_handle * udev, struct libusb memset(buf, 0, sizeof(buf)); memset(buf2, 0, sizeof(buf2)); - ret = libusb_get_string_descriptor_ascii(udev, desc.iSerialNumber, (unsigned char *)buf, sizeof(buf)); + ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, buf, sizeof(buf)); if ( ! isalnum(buf[0]) ) buf[0] = 0; for ( i = 0; i < ret; i+=2 ) { @@ -131,23 +145,15 @@ static void usb_descriptor_info_print(libusb_device_handle * udev, struct libusb } -static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) { +static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { - int err, i; + int i; char product[1024]; - libusb_device_handle * udev; struct usb_device_info * ret = NULL; - struct libusb_device_descriptor desc; - - if ( libusb_get_device_descriptor(dev, &desc) < 0 ) { - PRINTF_LINE("libusb_get_device_descriptor failed"); - PRINTF_END(); - return NULL; - } for ( i = 0; usb_devices[i].vendor; ++i ) { - if ( desc.idVendor == usb_devices[i].vendor && desc.idProduct == usb_devices[i].product ) { + if ( dev->descriptor.idVendor == usb_devices[i].vendor && dev->descriptor.idProduct == usb_devices[i].product ) { printf("\b\b "); PRINTF_END(); @@ -156,10 +162,9 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) PRINTF_END(); PRINTF_LINE("Opening USB..."); - - err = libusb_open(dev, &udev); - if ( err < 0 ) { - PRINTF_ERROR("libusb_open failed"); + usb_dev_handle * udev = usb_open(dev); + if ( ! udev ) { + PRINTF_ERROR("usb_open failed"); fprintf(stderr, "\n"); return NULL; } @@ -168,15 +173,17 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) if ( usb_devices[i].interface >= 0 ) { +#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP PRINTF_LINE("Detaching kernel from USB interface..."); - libusb_detach_kernel_driver(udev, usb_devices[i].interface); + usb_detach_kernel_driver_np(udev, usb_devices[i].interface); +#endif PRINTF_LINE("Claiming USB interface..."); - if ( libusb_claim_interface(udev, usb_devices[i].interface) < 0 ) { - PRINTF_ERROR("libusb_claim_interface failed"); + if ( usb_claim_interface(udev, usb_devices[i].interface) < 0 ) { + PRINTF_ERROR("usb_claim_interface failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - libusb_close(udev); + usb_close(udev); return NULL; } @@ -184,22 +191,22 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) if ( usb_devices[i].alternate >= 0 ) { PRINTF_LINE("Setting alternate USB interface..."); - if ( libusb_set_interface_alt_setting(udev, usb_devices[i].interface, usb_devices[i].alternate) < 0 ) { - PRINTF_ERROR("libusb_claim_interface failed"); + if ( usb_set_altinterface(udev, usb_devices[i].alternate) < 0 ) { + PRINTF_ERROR("usb_claim_interface failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - libusb_close(udev); + usb_close(udev); return NULL; } } if ( usb_devices[i].configuration >= 0 ) { PRINTF_LINE("Setting USB configuration..."); - if ( libusb_set_configuration(udev, usb_devices[i].configuration) < 0 ) { - PRINTF_ERROR("libusb_set_configuration failed"); + if ( usb_set_configuration(udev, usb_devices[i].configuration) < 0 ) { + PRINTF_ERROR("usb_set_configuration failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - libusb_close(udev); + usb_close(udev); return NULL; } } @@ -208,7 +215,7 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) if ( ! ret ) { ALLOC_ERROR(); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - libusb_close(udev); + usb_close(udev); return NULL; } @@ -239,7 +246,7 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) ERROR("Device detection failed"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - libusb_close(udev); + usb_close(udev); free(ret); return NULL; } @@ -253,7 +260,7 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) ERROR("Device mishmash"); fprintf(stderr, "\n"); usb_reattach_kernel_driver(udev, usb_devices[i].interface); - libusb_close(udev); + usb_close(udev); free(ret); return NULL; } @@ -270,6 +277,28 @@ static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) } +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; + +} + static volatile sig_atomic_t signal_quit; static void signal_handler(int signum) { @@ -281,18 +310,14 @@ static void signal_handler(int signum) { struct usb_device_info * usb_open_and_wait_for_device(void) { - libusb_device **devs; - libusb_device **dev; + struct usb_bus * bus; struct usb_device_info * ret = NULL; int i = 0; void (*prev)(int); static char progress[] = {'/','-','\\', '|'}; - if ( libusb_init(NULL) < 0 ) { - PRINTF_LINE("libusb_init failed"); - PRINTF_END(); - return NULL; - } + usb_init(); + usb_find_busses(); PRINTF_BACK(); printf("\n"); @@ -305,19 +330,25 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { PRINTF_LINE("Waiting for USB device... %c", progress[++i%sizeof(progress)]); - if ( libusb_get_device_list(NULL, &devs) < 0 ) { - PRINTF_LINE("Listing USB devices failed"); - PRINTF_END(); - break; - } + 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; + } + } - for ( dev = devs; *dev != NULL; ++dev ) { - ret = usb_device_is_valid(*dev); if ( ret ) break; - } - libusb_free_device_list(devs, 1); + } if ( ret ) break; @@ -342,8 +373,7 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { void usb_close_device(struct usb_device_info * dev) { usb_reattach_kernel_driver(dev->udev, dev->flash_device->interface); - libusb_close(dev->udev); - libusb_exit(NULL); + usb_close(dev->udev); free(dev); } diff --git a/src/usb-device.h b/src/usb-device.h index 910a8c9..101fbcd 100644 --- a/src/usb-device.h +++ b/src/usb-device.h @@ -20,11 +20,11 @@ #ifndef USB_DEVICE_H #define USB_DEVICE_H -#include +#include -#define USB_READ_EP (LIBUSB_ENDPOINT_IN | 0x1) -#define USB_WRITE_EP (LIBUSB_ENDPOINT_OUT | 0x1) -#define USB_WRITE_DATA_EP (LIBUSB_ENDPOINT_OUT | 0x2) +#define USB_READ_EP (USB_ENDPOINT_IN | 0x1) +#define USB_WRITE_EP (USB_ENDPOINT_OUT | 0x1) +#define USB_WRITE_DATA_EP (USB_ENDPOINT_OUT | 0x2) #include "device.h" @@ -51,7 +51,7 @@ struct usb_device_info { enum device device; int16_t hwrev; const struct usb_flash_device * flash_device; - libusb_device_handle * udev; + usb_dev_handle * udev; int data; }; -- cgit v1.2.3 From 5e79d651caee6b7a81097ae432b829e3da952261 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 6 Dec 2016 23:10:58 +0100 Subject: all: Compile globally without _GNU_SOURCE, define _BSD_SOURCE or _GNU_SOURCE where needed --- src/Makefile | 2 +- src/cal.c | 6 ++++++ src/disk.c | 6 ++++++ src/libusb-sniff.c | 4 ++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index 6b8ef08..94a8790 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,7 +9,7 @@ CC = gcc CROSS_CC = $(CROSS_COMPILE)$(CC) HOST_CC = $(HOST_COMPILE)$(CC) -CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L +CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_POSIX_C_SOURCE=200809L CFLAGS += -W -Wall -O2 -pedantic -std=c99 LIBS += -lusb diff --git a/src/cal.c b/src/cal.c index 60a2de1..a90546f 100644 --- a/src/cal.c +++ b/src/cal.c @@ -20,6 +20,12 @@ /* This is simple CAL parser form Calvaria */ +#ifdef __linux__ +#ifndef _BSD_SOURCE +#define _BSD_SOURCE +#endif +#endif + #include #include #include diff --git a/src/disk.c b/src/disk.c index b8e9f51..11df4af 100644 --- a/src/disk.c +++ b/src/disk.c @@ -16,6 +16,12 @@ along with this program. If not, see . */ +#ifdef __linux__ +#ifndef _BSD_SOURCE +#define _BSD_SOURCE +#endif +#endif + #include #include #include diff --git a/src/libusb-sniff.c b/src/libusb-sniff.c index ea7ef48..91f7872 100644 --- a/src/libusb-sniff.c +++ b/src/libusb-sniff.c @@ -21,7 +21,11 @@ /* usage: sudo USBSNIFF_WAIT=1 LD_PRELOAD=./libusb-sniff.so flasher-3.5 ... */ /* usage: sudo USBSNIFF_SKIP_READ=1 USBSNIFF_SKIP_WRITE=1 LD_PRELOAD=./libusb-sniff.so flasher-3.5 ... */ +/* Enable RTLD_NEXT for glibc */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif + #include #include #include -- cgit v1.2.3 From 9a5f94004611c901b6914f86721185acd69fc27f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 6 Dec 2016 23:11:47 +0100 Subject: usb-device: Check that we are not using broken libusb-1.0 library --- src/Makefile | 2 +- src/usb-device.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index 94a8790..6813608 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,7 +11,7 @@ HOST_CC = $(HOST_COMPILE)$(CC) CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_POSIX_C_SOURCE=200809L CFLAGS += -W -Wall -O2 -pedantic -std=c99 -LIBS += -lusb +LIBS += -lusb -ldl DEPENDS = Makefile ../config.mk diff --git a/src/usb-device.c b/src/usb-device.c index d3aa707..2913198 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -17,6 +17,11 @@ */ +/* Enable RTLD_DEFAULT for glibc */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + #include #include #include @@ -24,6 +29,7 @@ #include #include #include +#include #include @@ -316,6 +322,9 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { void (*prev)(int); static char progress[] = {'/','-','\\', '|'}; + if ( dlsym(RTLD_DEFAULT, "libusb_init") ) + ERROR_RETURN("You are trying to use broken libusb-1.0 library (either directly or via wrapper) which has slow listing of usb devices. It cannot be used for flashing or cold-flashing. Please use libusb 0.1.", NULL); + usb_init(); usb_find_busses(); -- cgit v1.2.3 From 9ea8f2bb3872ff91e3413c5bf8c099da7b617831 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 6 Dec 2016 23:13:12 +0100 Subject: cal: Style fix --- src/cal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cal.c b/src/cal.c index a90546f..d284f9f 100644 --- a/src/cal.c +++ b/src/cal.c @@ -194,8 +194,8 @@ static int is_header(void *data, size_t size) { return 0; if ( memcmp(hdr->magic, HDR_MAGIC, sizeof(hdr->magic)) != 0 ) - return 0; + return 1; } -- cgit v1.2.3 From 0650cdf51f374e8ab2e54324fe84c86d5a80cd72 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 6 Dec 2016 23:13:48 +0100 Subject: cold-flash: Show OMAP chip revision --- src/cold-flash.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cold-flash.c b/src/cold-flash.c index b4fe4e6..9b92d79 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -323,6 +323,7 @@ int init_cold_flash(struct usb_device_info * dev) { uint8_t asic_buffer[127]; int asic_size = 69; const char * chip = NULL; + int revision; int i; if ( dev->flash_device->protocol != FLASH_COLD ) @@ -346,11 +347,11 @@ int init_cold_flash(struct usb_device_info * dev) { if ( asic_buffer[0] != 0x05 ) ERROR_RETURN("Invalid ASIC ID", -1); - /* ID Subblock - header */ + /* 1. ID Subblock - header */ if ( memcmp(asic_buffer+1, "\x01\x05\x01", 3) != 0 ) ERROR_RETURN("Invalid ASIC ID", -1); - /* ID Subblock - OMAP chip version (check for OMAP3430 or 3630) */ + /* 1. ID Subblock - OMAP chip version (check for OMAP3430 or 3630) */ if ( memcmp(asic_buffer+4, "\x34\x30\x07", 3) == 0 ) chip = "OMAP3430"; else if ( memcmp(asic_buffer+4, "\x36\x30\x07", 3) == 0 ) @@ -358,23 +359,26 @@ int init_cold_flash(struct usb_device_info * dev) { else ERROR_RETURN("Invalid ASIC ID", -1); - /* Reserved1 - header */ + /* 1. ID Subblock - OMAP chip revision */ + revision = asic_buffer[7]; + + /* 2. Secure Mode Subblock - header */ if ( memcmp(asic_buffer+8, "\x13\x02\x01", 3) != 0 ) ERROR_RETURN("Invalid ASIC ID", -1); - /* 2nd ID Subblock - header */ + /* 3. 2nd ID Subblock - header */ if ( memcmp(asic_buffer+12, "\x12\x15\x01", 3) != 0 ) ERROR_RETURN("Invalid ASIC ID", -1); - /* Reserved2 - header */ + /* 4. Root Key Hash Subblock - header */ if ( memcmp(asic_buffer+35, "\x14\x15\x01", 3) != 0 ) ERROR_RETURN("Invalid ASIC ID", -1); - /* Checksum subblock - header */ + /* 5. Checksum subblock - header */ if ( memcmp(asic_buffer+58, "\x15\x09\x01", 3) != 0 ) ERROR_RETURN("Invalid ASIC ID", -1); - printf("Detected %s chip\n", chip); + printf("Detected %s chip (revision %d)\n", chip, revision); return 0; -- cgit v1.2.3 From 15dbd14d7acf494e63a87e4c8ae0991dd8281b77 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Tue, 6 Dec 2016 23:42:41 +0100 Subject: doc: Update fiasco documentation --- doc/fiasco | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/fiasco b/doc/fiasco index f7462dd..941a2da 100644 --- a/doc/fiasco +++ b/doc/fiasco @@ -37,12 +37,16 @@ FW HEADER IMAGE 1 byte = 0x54 -- signature - 1 byte -- number of subsection blocks + 1 - 5 bytes -- unknown (always 0x2e 0x19 0x01 0x01 0x00) + 1 byte -- number of subsection blocks + (start of data block) + 1 byte -- type of subsection block (data - always 0x2e) + 1 byte -- length of subsection block (data - always 25) + 3 bytes -- unknown (always 0x01 0x01 0x00) 2 bytes -- checksum for the image contents (xorpair) (big endian) 12 bytes -- image name type (first byte is FF if is the last image) 4 bytes -- length of image data (big endian) 4 bytes -- unknown (always 0x00 0x00 0x00 0x00) + (end of data block) block { 1 byte -- type of subsection block '1' - version -- cgit v1.2.3 From 19e2b22193776e8edc3b45679e3fcc7b0bf8bb5f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 7 Dec 2016 00:39:36 +0100 Subject: image: Remove dead code in image_align() --- src/image.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/image.c b/src/image.c index f31d6c7..9cb3e41 100644 --- a/src/image.c +++ b/src/image.c @@ -214,16 +214,13 @@ static int image_append(struct image * image, const char * type, const char * de static void image_align(struct image * image) { - size_t align = 0; + size_t align; if ( image->type == IMAGE_MMC ) align = 8; else align = 7; - if ( align == 0 ) - return; - if ( ( image->size & ( ( 1ULL << align ) - 1 ) ) == 0 ) return; -- cgit v1.2.3 From f2a51b5041f01858a0154fb06fc9d7054f94ccbb Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 7 Dec 2016 11:38:07 +0100 Subject: disk: Do not fail when empty block device is exported --- src/disk.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/disk.c b/src/disk.c index 11df4af..50dac45 100644 --- a/src/disk.c +++ b/src/disk.c @@ -35,6 +35,7 @@ #include #include #include +#include #endif #include "disk.h" @@ -147,7 +148,8 @@ int disk_open_dev(int maj, int min, int partition, int readonly) { fd = open(blkdev, (readonly ? O_RDONLY : O_RDWR) | O_EXCL); if ( fd < 0 ) { - ERROR_INFO("Cannot open block device %s", blkdev); + if ( errno != ENOMEDIUM ) + ERROR_INFO("Cannot open block device %s", blkdev); return -1; } @@ -371,7 +373,7 @@ int disk_init(struct usb_device_info * dev) { else fd = disk_open_dev(maj1, min1, 1, 1); - if ( fd < 0 ) + if ( fd < 0 && errno != ENOMEDIUM ) return -1; dev->data = fd; -- cgit v1.2.3 From 188fa70c809b9790dc359c34554eb972e87ccd99 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 7 Dec 2016 11:55:26 +0100 Subject: cold-flash: Increase timeout after leaving cold flash mode Switching to NOLO takes about one second and NOLO itself wait another second. So it should be OK. --- src/cold-flash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cold-flash.c b/src/cold-flash.c index 9b92d79..fc2e86b 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -415,7 +415,7 @@ int leave_cold_flash(struct usb_device_info * dev) { if ( ret != sizeof(omap_memory_msg) ) ERROR_RETURN("Sending OMAP memory boot message failed", -1); - SLEEP(250000); + SLEEP(1000000); return 0; } -- cgit v1.2.3 From 4ccf8fb369119f3e1532f061a7cf14dec5b3279f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 7 Dec 2016 11:55:54 +0100 Subject: usb-device: Do not print verbose message "Reattach kernel driver to USB interface..." --- src/usb-device.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/usb-device.c b/src/usb-device.c index 2913198..9363179 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -105,8 +105,6 @@ static void usb_reattach_kernel_driver(usb_dev_handle * udev, int interface) { if ( interface < 0 ) return; - PRINTF_LINE("Reattach kernel driver to USB interface..."); - PRINTF_END(); usb_release_interface(udev, interface); ioctl(*((int *)udev), _IOWR('U', 18, command), &command); #endif -- cgit v1.2.3 From 3d71caab8ffda2f6f001a75090e0843361c86ca6 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 7 Dec 2016 11:56:37 +0100 Subject: usb-device: Do not reattach kernel driver after closing cold flash mode --- src/usb-device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/usb-device.c b/src/usb-device.c index 9363179..391574a 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -379,7 +379,8 @@ struct usb_device_info * usb_open_and_wait_for_device(void) { void usb_close_device(struct usb_device_info * dev) { - usb_reattach_kernel_driver(dev->udev, dev->flash_device->interface); + if ( dev->flash_device->protocol != FLASH_COLD ) + usb_reattach_kernel_driver(dev->udev, dev->flash_device->interface); usb_close(dev->udev); free(dev); -- cgit v1.2.3 From 8f57f7ef61fe2f439422d5dc313d2005d8adc370 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 10 Dec 2016 14:13:14 +0100 Subject: mkii: Fix detection of Update mode --- src/mkii.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mkii.c b/src/mkii.c index 0c6e655..6c8a0d6 100644 --- a/src/mkii.c +++ b/src/mkii.c @@ -159,7 +159,8 @@ int mkii_init(struct usb_device_info * dev) { printf("\n"); memset(buf, 0, sizeof(buf)); - usb_get_string_simple(dev->udev, usb_device(dev->udev)->config[dev->flash_device->configuration].iConfiguration, buf, sizeof(buf)); + if ( usb_device(dev->udev)->descriptor.bNumConfigurations >= 1 ) + usb_get_string_simple(dev->udev, usb_device(dev->udev)->config[0].iConfiguration, buf, sizeof(buf)); if ( strncmp(buf, "Firmware Upgrade Configuration", sizeof("Firmware Upgrade Configuration")) == 0 ) dev->data |= MKII_UPDATE_MODE; -- cgit v1.2.3 From 6ec64f4992ccd5e4ff42e9578d6c306a5b688a35 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 10 Dec 2016 14:13:41 +0100 Subject: operations: Fix NULL pointer dereference in dev_boot_device This will fix a crash when calling "0xFFFF -b" from Mk II Update mode. --- src/operations.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/operations.c b/src/operations.c index 1fd4766..cd7ef44 100644 --- a/src/operations.c +++ b/src/operations.c @@ -243,7 +243,7 @@ int dev_boot_device(struct device_info * dev, const char * cmdline) { if ( protocol == FLASH_NOLO ) return nolo_boot_device(dev->usb, cmdline); - else if ( protocol == FLASH_MKII && strcmp(cmdline, "update") == 0 ) + else if ( protocol == FLASH_MKII && cmdline && strcmp(cmdline, "update") == 0 ) return mkii_reboot_device(dev->usb, 1); usb_switch_to_nolo(dev->usb); -- cgit v1.2.3 From 7522a9fbc72f14c5cb7fbaa54fb57526c5221d85 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 16 Dec 2016 15:39:46 +0100 Subject: all: Fix compilation when u_int*_t are not defined and used by usb.h --- src/cold-flash.c | 2 -- src/nolo.c | 2 -- src/usb-device.c | 14 ++++++-------- src/usb-device.h | 7 +++++++ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/cold-flash.c b/src/cold-flash.c index fc2e86b..6338670 100644 --- a/src/cold-flash.c +++ b/src/cold-flash.c @@ -22,8 +22,6 @@ #include #include -#include - #include "global.h" #include "cold-flash.h" #include "image.h" diff --git a/src/nolo.c b/src/nolo.c index 1dd9d61..9faea56 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -22,8 +22,6 @@ #include #include -#include - #include "nolo.h" #include "image.h" #include "global.h" diff --git a/src/usb-device.c b/src/usb-device.c index 391574a..24ce4be 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -31,14 +31,6 @@ #include #include -#include - -#ifdef __linux__ -#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP -#include -#endif -#endif - #include "global.h" #include "device.h" #include "usb-device.h" @@ -47,6 +39,12 @@ #include "cold-flash.h" #include "mkii.h" +#ifdef __linux__ +#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP +#include +#endif +#endif + static struct usb_flash_device usb_devices[] = { { 0x0421, 0x0096, -1, -1, -1, FLASH_DISK, { DEVICE_RX_44, 0 } }, { 0x0421, 0x0105, 2, 1, -1, FLASH_NOLO, { DEVICE_SU_18, DEVICE_RX_44, DEVICE_RX_48, DEVICE_RX_51, DEVICE_RM_680, 0 } }, diff --git a/src/usb-device.h b/src/usb-device.h index 101fbcd..082bf73 100644 --- a/src/usb-device.h +++ b/src/usb-device.h @@ -20,6 +20,13 @@ #ifndef USB_DEVICE_H #define USB_DEVICE_H +#include + +/* u_int*_t types are not defined without _GNU_SOURCE but usb.h needs them */ +#define u_int8_t uint8_t +#define u_int16_t uint16_t +#define u_int32_t uint32_t + #include #define USB_READ_EP (USB_ENDPOINT_IN | 0x1) -- cgit v1.2.3 From 430930537cf483847419bcfacce00ff3f75b8db8 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 17 Dec 2016 01:51:33 +0100 Subject: all: Use #include for major() instead #define _BSD_SOURCE --- src/cal.c | 7 +------ src/disk.c | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/cal.c b/src/cal.c index d284f9f..c6af8e2 100644 --- a/src/cal.c +++ b/src/cal.c @@ -20,12 +20,6 @@ /* This is simple CAL parser form Calvaria */ -#ifdef __linux__ -#ifndef _BSD_SOURCE -#define _BSD_SOURCE -#endif -#endif - #include #include #include @@ -42,6 +36,7 @@ #ifdef __linux__ #include #include +#include #endif #include "cal.h" diff --git a/src/disk.c b/src/disk.c index 50dac45..8a16ca3 100644 --- a/src/disk.c +++ b/src/disk.c @@ -16,12 +16,6 @@ along with this program. If not, see . */ -#ifdef __linux__ -#ifndef _BSD_SOURCE -#define _BSD_SOURCE -#endif -#endif - #include #include #include @@ -32,6 +26,7 @@ #include #ifdef __linux__ +#include #include #include #include -- cgit v1.2.3 From e36423e8c2140a0a5b679f77be907fe9d4501d4f Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 17 Dec 2016 01:52:50 +0100 Subject: all: Fix printing fsblkcnt_t and off_t types --- src/local.c | 2 +- src/main.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/local.c b/src/local.c index 3b2b1cd..628c49e 100644 --- a/src/local.c +++ b/src/local.c @@ -223,7 +223,7 @@ static int local_nanddump(const char * file, int mtd, int offset, int length) { free(path); if ( ret == 0 && buf.f_bsize * buf.f_bfree < (long unsigned int)length ) { - ERROR("Not enough free space (have: %lu, need: %d)", buf.f_bsize * buf.f_bfree, length); + ERROR("Not enough free space (have: %ju, need: %d)", (intmax_t)(buf.f_bsize * buf.f_bfree), length); return 1; } diff --git a/src/main.c b/src/main.c index 3c24820..40f367a 100644 --- a/src/main.c +++ b/src/main.c @@ -222,7 +222,7 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) { exit(1); } if ( read(fd, layout, len) != len ) { - ERROR_INFO("Cannot read %lu bytes from layout file %s", len, layout_file); + ERROR_INFO("Cannot read %ju bytes from layout file %s", (intmax_t)len, layout_file); exit(1); } layout[len] = 0; -- cgit v1.2.3 From b8fb95b2aab338830357db86c7c8f7f85a545eb5 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 17 Dec 2016 01:53:16 +0100 Subject: all: Enable Large File Support --- src/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index 6813608..be4ade6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,7 +9,7 @@ CC = gcc CROSS_CC = $(CROSS_COMPILE)$(CC) HOST_CC = $(HOST_COMPILE)$(CC) -CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_POSIX_C_SOURCE=200809L +CPPFLAGS += -DVERSION=\"$(VERSION)\" -DBUILD_DATE="\"$(BUILD_DATE)\"" -D_POSIX_C_SOURCE=200809L -D_FILE_OFFSET_BITS=64 CFLAGS += -W -Wall -O2 -pedantic -std=c99 LIBS += -lusb -ldl -- cgit v1.2.3 From bc0f663e927f5f8667fcbfa48bd948eb2640ee07 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 17 Dec 2016 03:00:12 +0100 Subject: cal: Fix check for lseek errors --- src/cal.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cal.c b/src/cal.c index c6af8e2..2edd0b5 100644 --- a/src/cal.c +++ b/src/cal.c @@ -73,6 +73,8 @@ int cal_init_file(const char * file, struct cal ** cal_out) { struct stat st; #ifdef __linux__ mtd_info_t mtd_info; +#else + off_t lsize = 0; #endif if ( stat(file, &st) != 0 ) @@ -90,11 +92,12 @@ int cal_init_file(const char * file, struct cal ** cal_out) { if ( ioctl(fd, BLKGETSIZE64, &blksize) != 0 ) goto err; #else - blksize = lseek(fd, 0, SEEK_END); - if ( blksize == (off_t)-1 ) + lsize = lseek(fd, 0, SEEK_END); + if ( lsize == (off_t)-1 ) goto err; if ( lseek(fd, 0, SEEK_SET) == (off_t)-1 ) goto err; + blksize = lsize; #endif if ( blksize > SSIZE_MAX ) goto err; -- cgit v1.2.3 From d4ff1d93281aaf942e1bd9430a1bf61d84c608a4 Mon Sep 17 00:00:00 2001 From: Aaro Koskinen Date: Sat, 17 Dec 2016 17:46:35 +0100 Subject: usb-device: Add another product string for RM-680 --- src/usb-device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/usb-device.c b/src/usb-device.c index 24ce4be..2e2b6c4 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -231,7 +231,7 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { ret->device = DEVICE_RX_48; else if ( strcmp(product, "N900 (Storage Mode)") == 0 || strcmp(product, "Nokia N900 (Update mode)") == 0 || strcmp(product, "N900 (PC-Suite Mode)") == 0 ) ret->device = DEVICE_RX_51; - else if ( strcmp(product, "Sync Mode") == 0 ) + else if ( strcmp(product, "Sync Mode") == 0 || strcmp(product, "N950 (Update mode)") == 0 ) ret->device = DEVICE_RM_680; else if ( strcmp(product, "Nokia USB ROM") == 0 ) ret->device = DEVICE_ANY; -- cgit v1.2.3 From 645e280d690d1e67406f4fb924c04910440a3420 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 17 Dec 2016 18:00:07 +0100 Subject: cal: Fix memory leak --- src/cal.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cal.c b/src/cal.c index 2edd0b5..8a5d720 100644 --- a/src/cal.c +++ b/src/cal.c @@ -46,7 +46,6 @@ #define HDR_MAGIC "ConF" struct cal { - int fd; ssize_t size; void * mem; }; @@ -132,16 +131,18 @@ int cal_init_file(const char * file, struct cal ** cal_out) { if ( ! cal ) goto err; - cal->fd = fd; cal->mem = mem; cal->size = size; + close(fd); + *cal_out = cal; return 0; err: close(fd); free(mem); + free(cal); return -1; } -- cgit v1.2.3 From bd59c47ec374cac2051dda20b5a58f84b5c81664 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Wed, 21 Dec 2016 18:25:25 +0100 Subject: usb-device: Detect Nokia N950 in disk mode --- src/usb-device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/usb-device.c b/src/usb-device.c index 2e2b6c4..4c3e762 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -52,6 +52,7 @@ static struct usb_flash_device usb_devices[] = { { 0x0421, 0x0189, -1, -1, -1, FLASH_DISK, { DEVICE_RX_48, 0 } }, { 0x0421, 0x01c7, -1, -1, -1, FLASH_DISK, { DEVICE_RX_51, 0 } }, { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, DEVICE_RM_680, 0 } }, + { 0x0421, 0x03d1, -1, -1, -1, FLASH_DISK, { DEVICE_RM_680, 0 } }, { 0x0421, 0x0431, -1, -1, -1, FLASH_DISK, { DEVICE_SU_18, DEVICE_RX_34, 0 } }, { 0x0421, 0x04c3, -1, -1, -1, FLASH_DISK, { DEVICE_RX_34, 0 } }, { 0x0421, 0x3f00, 2, 1, -1, FLASH_NOLO, { DEVICE_RX_34, 0 } }, @@ -231,7 +232,7 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { ret->device = DEVICE_RX_48; else if ( strcmp(product, "N900 (Storage Mode)") == 0 || strcmp(product, "Nokia N900 (Update mode)") == 0 || strcmp(product, "N900 (PC-Suite Mode)") == 0 ) ret->device = DEVICE_RX_51; - else if ( strcmp(product, "Sync Mode") == 0 || strcmp(product, "N950 (Update mode)") == 0 ) + else if ( strcmp(product, "Nokia N950") == 0 || strcmp(product, "Sync Mode") == 0 || strcmp(product, "N950 (Update mode)") == 0 ) ret->device = DEVICE_RM_680; else if ( strcmp(product, "Nokia USB ROM") == 0 ) ret->device = DEVICE_ANY; -- cgit v1.2.3 From 0a1ac2521966221c38f654ca59128c7be70fb708 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 23 Dec 2016 10:12:27 +0100 Subject: cal: Move sys/ioctl.h to __linux__ --- src/cal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cal.c b/src/cal.c index 8a5d720..265f929 100644 --- a/src/cal.c +++ b/src/cal.c @@ -27,13 +27,13 @@ #include #include -#include #include #include #include #include #ifdef __linux__ +#include #include #include #include -- cgit v1.2.3 From b7c28b2c1bfab4783d6da431bef8288e32fdb136 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 23 Dec 2016 10:13:07 +0100 Subject: cal: SSIZE_MAX is not required to be defined --- src/cal.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cal.c b/src/cal.c index 265f929..8fa7e9f 100644 --- a/src/cal.c +++ b/src/cal.c @@ -98,8 +98,10 @@ int cal_init_file(const char * file, struct cal ** cal_out) { goto err; blksize = lsize; #endif +#ifdef SSIZE_MAX if ( blksize > SSIZE_MAX ) goto err; +#endif size = blksize; } else { #ifdef __linux__ -- cgit v1.2.3 From 82de19bee10d24737eca3b3ada1dd5863f492442 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 23 Dec 2016 10:13:23 +0100 Subject: disk: Check for ENOMEDIUM when partition is required --- src/disk.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/disk.c b/src/disk.c index 8a16ca3..63f7a52 100644 --- a/src/disk.c +++ b/src/disk.c @@ -131,8 +131,18 @@ int disk_open_dev(int maj, int min, int partition, int readonly) { if ( stat(blkdev, &st) != 0 || ! S_ISBLK(st.st_mode) ) { memcpy(blkdev+len, "1", 2); if ( stat(blkdev, &st) != 0 || ! S_ISBLK(st.st_mode) ) { - ERROR("Block device has partitions"); - return -1; + blkdev[len] = 0; + fd = open(blkdev, O_RDONLY); + if ( fd < 0 ) { + if ( errno != ENOMEDIUM ) { + ERROR_INFO("Cannot open block device %s", blkdev); + return -1; + } + } else { + close(fd); + ERROR("Block device does not have partitions"); + return -1; + } } } -- cgit v1.2.3 From f7bad18a565515421329931acf2669c5e75a1fde Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 23 Dec 2016 10:13:42 +0100 Subject: disk: Fix for RM-680 --- src/disk.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/disk.c b/src/disk.c index 63f7a52..6bdd974 100644 --- a/src/disk.c +++ b/src/disk.c @@ -369,8 +369,8 @@ int disk_init(struct usb_device_info * dev) { /* TODO: change 1 to 0 when disk_flash_dev will be implemented */ - /* RX-51 exports MyDocs in first usb device and just first partion, so host system see whole device without MBR table */ - if ( dev->device == DEVICE_RX_51 ) + /* RX-51 and RM-680 export MyDocs in first usb device and just first partion, so host system see whole device without MBR table */ + if ( dev->device == DEVICE_RX_51 || dev->device == DEVICE_RM_680 ) fd = disk_open_dev(maj1, min1, -1, 1); /* Other devices can export SD card as first partition and export whole mmc device, so host system will see MBR table */ else if ( maj2 != -1 && min2 != -1 ) -- cgit v1.2.3 From 117d0bb2d04df5a6cb0289f00afe61671376f0ec Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 24 Dec 2016 18:06:57 +0100 Subject: cal: cal_read_block allocate memory --- src/cal.c | 13 ++++++++++--- src/local.c | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/cal.c b/src/cal.c index 8fa7e9f..25bb9ee 100644 --- a/src/cal.c +++ b/src/cal.c @@ -264,6 +264,7 @@ int cal_read_block(struct cal * cal, const char * name, void ** ptr, unsigned lo uint64_t filelen = cal->size; uint8_t * data = cal->mem; struct header * hdr; + void * offset; find_offset = find_section(data, filelen, INDEX_LAST, name); if ( find_offset < 0 ) @@ -277,12 +278,18 @@ int cal_read_block(struct cal * cal, const char * name, void ** ptr, unsigned lo if ( crc32(0, hdr, sizeof(*hdr) - 4) != hdr->hdrsum ) return -1; - *ptr = data + find_offset + sizeof(struct header); - *len = hdr->length; + offset = data + find_offset + sizeof(struct header); + + if ( crc32(0, offset, hdr->length) != hdr->datasum ) + return -1; - if ( crc32(0, *ptr, *len) != hdr->datasum ) + *ptr = malloc(hdr->length); + if (!ptr) return -1; + memcpy(*ptr, offset, hdr->length); + *len = hdr->length; + return 0; } diff --git a/src/local.c b/src/local.c index 628c49e..3813910 100644 --- a/src/local.c +++ b/src/local.c @@ -53,7 +53,7 @@ static int root_device = -1; #define min(a, b) (a < b ? a : b) #define local_cal_copy(dest, from, len) strncpy(dest, from, min(len, sizeof(dest)-1)) #define local_cal_read(cal, str, ptr, len) ( cal_read_block(cal, str, &ptr, &len, 0) == 0 && ptr ) -#define local_cal_readcopy(cal, str, dest) do { void * ptr; unsigned long int len; if ( local_cal_read(cal, str, ptr, len) ) local_cal_copy(dest, ptr, len); } while ( 0 ) +#define local_cal_readcopy(cal, str, dest) do { void * ptr; unsigned long int len; if ( local_cal_read(cal, str, ptr, len) ) { local_cal_copy(dest, ptr, len); free(ptr); } } while ( 0 ) #if defined(__linux__) && defined(__arm__) -- cgit v1.2.3 From b4314bce152c52ee49ccc9f9a318390ec686bf2a Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 24 Dec 2016 18:22:02 +0100 Subject: cal: Fix pointer check --- src/cal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cal.c b/src/cal.c index 25bb9ee..4d07e5e 100644 --- a/src/cal.c +++ b/src/cal.c @@ -284,7 +284,7 @@ int cal_read_block(struct cal * cal, const char * name, void ** ptr, unsigned lo return -1; *ptr = malloc(hdr->length); - if (!ptr) + if (!*ptr) return -1; memcpy(*ptr, offset, hdr->length); -- cgit v1.2.3 From 08485a0c094cf021e5abedd001741da9d0fd1474 Mon Sep 17 00:00:00 2001 From: Aaro Koskinen Date: Sat, 24 Dec 2016 18:23:28 +0100 Subject: all: Add basic support for Nokia N9 --- src/device.c | 2 ++ src/device.h | 1 + src/usb-device.c | 6 ++++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/device.c b/src/device.c index d50d129..db2c6a7 100644 --- a/src/device.c +++ b/src/device.c @@ -32,6 +32,7 @@ static const char * devices[] = { [DEVICE_RX_48] = "RX-48", [DEVICE_RX_51] = "RX-51", [DEVICE_RM_680] = "RM-680", + [DEVICE_RM_696] = "RM-696", }; enum device device_from_string(const char * device) { @@ -65,6 +66,7 @@ static const char * long_devices[] = { [DEVICE_RX_48] = "Nokia N810 Wimax", [DEVICE_RX_51] = "Nokia N900", [DEVICE_RM_680] = "Nokia N950", + [DEVICE_RM_696] = "Nokia N9", }; const char * device_to_long_string(enum device device) { diff --git a/src/device.h b/src/device.h index 9b58dbe..8d38e39 100644 --- a/src/device.h +++ b/src/device.h @@ -31,6 +31,7 @@ enum device { DEVICE_RX_48, /* Nokia N810 WiMax */ DEVICE_RX_51, /* Nokia N900 */ DEVICE_RM_680, /* Nokia N950 */ + DEVICE_RM_696, /* Nokia N9 */ DEVICE_COUNT, }; diff --git a/src/usb-device.c b/src/usb-device.c index 4c3e762..5379b82 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -47,8 +47,8 @@ static struct usb_flash_device usb_devices[] = { { 0x0421, 0x0096, -1, -1, -1, FLASH_DISK, { DEVICE_RX_44, 0 } }, - { 0x0421, 0x0105, 2, 1, -1, FLASH_NOLO, { DEVICE_SU_18, DEVICE_RX_44, DEVICE_RX_48, DEVICE_RX_51, DEVICE_RM_680, 0 } }, - { 0x0421, 0x0106, 0, -1, -1, FLASH_COLD, { DEVICE_RX_51, DEVICE_RM_680, 0 } }, + { 0x0421, 0x0105, 2, 1, -1, FLASH_NOLO, { DEVICE_SU_18, DEVICE_RX_44, DEVICE_RX_48, DEVICE_RX_51, DEVICE_RM_680, DEVICE_RM_696, 0 } }, + { 0x0421, 0x0106, 0, -1, -1, FLASH_COLD, { DEVICE_RX_51, DEVICE_RM_680, DEVICE_RM_696, 0 } }, { 0x0421, 0x0189, -1, -1, -1, FLASH_DISK, { DEVICE_RX_48, 0 } }, { 0x0421, 0x01c7, -1, -1, -1, FLASH_DISK, { DEVICE_RX_51, 0 } }, { 0x0421, 0x01c8, 1, 1, -1, FLASH_MKII, { DEVICE_RX_51, DEVICE_RM_680, 0 } }, @@ -234,6 +234,8 @@ static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) { ret->device = DEVICE_RX_51; else if ( strcmp(product, "Nokia N950") == 0 || strcmp(product, "Sync Mode") == 0 || strcmp(product, "N950 (Update mode)") == 0 ) ret->device = DEVICE_RM_680; + else if ( strcmp(product, "N9 (Update mode)") == 0 || strcmp(product, "Nxy (Update mode)") == 0 ) + ret->device = DEVICE_RM_696; else if ( strcmp(product, "Nokia USB ROM") == 0 ) ret->device = DEVICE_ANY; else -- cgit v1.2.3 From 955ee1bd1e85fcef9cfcd9ed956f85a0efb43dfa Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 24 Dec 2016 18:23:43 +0100 Subject: TODO: Update state --- TODO | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index 915a005..292b372 100644 --- a/TODO +++ b/TODO @@ -4,11 +4,9 @@ cal: cold-flash: * Detect device from asic id -device: - * Support for Nokia N950 and Nokia N9 - disk: - * Everything (work with eMMC images in USB Mass storage mode) + * Support for flashing mmc images + * Badblock checking fiasco: * Support for Harmattan images @@ -18,17 +16,11 @@ image: local: * Support for flashing (on device) + * Write versions + * Badblock checking mkii: - * Everything (flashing protocol for eMMC images) + * Support for flashing nolo: - * Support for dumping images (it is possible?) * Implement functions for setting nolo, kernel, initfs and content versions (it is possible?) - -operations: - * Enable FLASH_DISK protocol when implemented - -usb-device: - * Autodetect devices from product string - * Enable FLASH_DISK and FLASH_MKII protocols when implemented -- cgit v1.2.3 From 9bf886c04169cd2ee6bb0916f3422d4d54e158f4 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 25 Dec 2016 11:46:12 +0100 Subject: Release version 0.7 --- config.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.mk b/config.mk index bb0ca40..1d5f3ad 100644 --- a/config.mk +++ b/config.mk @@ -1,4 +1,4 @@ -VERSION = 0.6.1+git +VERSION = 0.7 PREFIX = /usr/local # NetBSD stuff -- cgit v1.2.3