diff options
Diffstat (limited to 'fastboot')
| -rw-r--r-- | fastboot/Android.mk | 6 | ||||
| -rw-r--r-- | fastboot/bootimg.c | 20 | ||||
| -rw-r--r-- | fastboot/engine.c | 132 | ||||
| -rw-r--r-- | fastboot/fastboot.c | 8 | ||||
| -rw-r--r-- | fastboot/fastboot.h | 6 | ||||
| -rw-r--r-- | fastboot/protocol.c | 14 | ||||
| -rw-r--r-- | fastboot/usb.h | 10 | ||||
| -rw-r--r-- | fastboot/usb_linux.c | 69 | ||||
| -rw-r--r-- | fastboot/usb_osx.c | 31 | ||||
| -rw-r--r-- | fastboot/usb_windows.c | 12 | ||||
| -rw-r--r-- | fastboot/usbtest.c | 16 | ||||
| -rw-r--r-- | fastboot/util_linux.c | 6 | ||||
| -rw-r--r-- | fastboot/util_osx.c | 4 | ||||
| -rw-r--r-- | fastboot/util_windows.c | 4 |
14 files changed, 195 insertions, 143 deletions
diff --git a/fastboot/Android.mk b/fastboot/Android.mk index 8c130ff..089b9bb 100644 --- a/fastboot/Android.mk +++ b/fastboot/Android.mk @@ -50,6 +50,12 @@ endif LOCAL_STATIC_LIBRARIES := $(EXTRA_STATIC_LIBS) libzipfile libunz libext4_utils libz +ifneq ($(HOST_OS),windows) +ifeq ($(HAVE_SELINUX), true) +LOCAL_STATIC_LIBRARIES += libselinux +endif # HAVE_SELINUX +endif # HOST_OS != windows + include $(BUILD_HOST_EXECUTABLE) $(call dist-for-goals,dist_files,$(LOCAL_BUILT_MODULE)) diff --git a/fastboot/bootimg.c b/fastboot/bootimg.c index e5aea4e..9e0e45c 100644 --- a/fastboot/bootimg.c +++ b/fastboot/bootimg.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -48,23 +48,23 @@ boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned second_actual; unsigned page_mask; boot_img_hdr *hdr; - + page_mask = page_size - 1; - + kernel_actual = (kernel_size + page_mask) & (~page_mask); ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask); second_actual = (second_size + page_mask) & (~page_mask); - + *bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual; - + hdr = calloc(*bootimg_size, 1); - + if(hdr == 0) { return hdr; } memcpy(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE); - + hdr->kernel_size = kernel_size; hdr->ramdisk_size = ramdisk_size; hdr->second_size = second_size; @@ -74,9 +74,9 @@ boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, hdr->tags_addr = base + 0x00000100; hdr->page_size = page_size; - memcpy(hdr->magic + page_size, + memcpy(hdr->magic + page_size, kernel, kernel_size); - memcpy(hdr->magic + page_size + kernel_actual, + memcpy(hdr->magic + page_size + kernel_actual, ramdisk, ramdisk_size); memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, second, second_size); diff --git a/fastboot/engine.c b/fastboot/engine.c index 89cff3f..a1b6539 100644 --- a/fastboot/engine.c +++ b/fastboot/engine.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -35,12 +35,17 @@ #include <stdarg.h> #include <stdbool.h> #include <string.h> -#include <sys/mman.h> #include <sys/stat.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> +#ifdef USE_MINGW +#include <fcntl.h> +#else +#include <sys/mman.h> +#endif + extern struct fs_info info; #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) @@ -61,7 +66,7 @@ char *mkmsg(const char *fmt, ...) va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); - + s = strdup(buf); if (s == 0) die("out of memory"); return s; @@ -77,7 +82,7 @@ typedef struct Action Action; #define CMD_SIZE 64 -struct Action +struct Action { unsigned op; Action *next; @@ -104,7 +109,7 @@ struct image_data { }; void generate_ext4_image(struct image_data *image); -void munmap_image(struct image_data *image); +void cleanup_image(struct image_data *image); struct generator { char *fs_type; @@ -122,7 +127,7 @@ struct generator { */ void (*cleanup)(struct image_data *image); } generators[] = { - { "ext4", generate_ext4_image, munmap_image } + { "ext4", generate_ext4_image, cleanup_image } }; static int cb_default(Action *a, int status, char *resp) @@ -176,9 +181,58 @@ void fb_queue_erase(const char *ptn) a->msg = mkmsg("erasing '%s'", ptn); } -void munmap_image(struct image_data *image) +/* Loads file content into buffer. Returns NULL on error. */ +static void *load_buffer(int fd, off_t size) +{ + void *buffer; + +#ifdef USE_MINGW + ssize_t count = 0; + + // mmap is more efficient but mingw does not support it. + // In this case we read whole image into memory buffer. + buffer = malloc(size); + if (!buffer) { + perror("malloc"); + return NULL; + } + + lseek(fd, 0, SEEK_SET); + while(count < size) { + ssize_t actually_read = read(fd, (char*)buffer+count, size-count); + + if (actually_read == 0) { + break; + } + if (actually_read < 0) { + if (errno == EINTR) { + continue; + } + perror("read"); + free(buffer); + return NULL; + } + + count += actually_read; + } +#else + buffer = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); + if (buffer == MAP_FAILED) { + perror("mmap"); + return NULL; + } +#endif + + return buffer; +} + +void cleanup_image(struct image_data *image) { +#ifdef USE_MINGW + free(image->buffer); +#else munmap(image->buffer, image->image_size); +#endif } void generate_ext4_image(struct image_data *image) @@ -186,28 +240,35 @@ void generate_ext4_image(struct image_data *image) int fd; struct stat st; +#ifdef USE_MINGW + /* Ideally we should use tmpfile() here, the same as with unix version. + * But unfortunately it is not portable as it is not clear whether this + * function opens file in TEXT or BINARY mode. + * + * There are also some reports it is buggy: + * http://pdplab.it.uom.gr/teaching/gcc_manuals/gnulib.html#tmpfile + * http://www.mega-nerd.com/erikd/Blog/Windiots/tmpfile.html + */ + char *filename = tempnam(getenv("TEMP"), "fastboot-format.img"); + fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0644); + unlink(filename); +#else fd = fileno(tmpfile()); - /* reset ext4fs info so we can be called multiple times */ +#endif reset_ext4fs_info(); info.len = image->partition_size; - make_ext4fs_internal(fd, NULL, NULL, 0, 0, 1, 0, 0, 0); + make_ext4fs_internal(fd, NULL, NULL, NULL, 0, 1, 0, 0, 0, NULL); fstat(fd, &st); image->image_size = st.st_size; - - image->buffer = mmap(NULL, image->image_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (image->buffer == MAP_FAILED) { - perror("mmap"); - image->buffer = NULL; - } + image->buffer = load_buffer(fd, st.st_size); close(fd); } -int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported) +int fb_format(Action *a, usb_handle *usb) { const char *partition = a->cmd; - char query[256]; char response[FB_RESPONSE_SZ+1]; int status = 0; struct image_data image; @@ -217,16 +278,9 @@ int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported) char cmd[CMD_SIZE]; response[FB_RESPONSE_SZ] = '\0'; - snprintf(query, sizeof(query), "getvar:partition-type:%s", partition); - status = fb_command_response(usb, query, response); + snprintf(cmd, sizeof(cmd), "getvar:partition-type:%s", partition); + status = fb_command_response(usb, cmd, response); if (status) { - if (skip_if_not_supported) { - fprintf(stderr, - "Erase successful, but not automatically formatting.\n"); - fprintf(stderr, - "Can't determine partition type.\n"); - return 0; - } fprintf(stderr,"FAILED (%s)\n", fb_get_error()); return status; } @@ -238,28 +292,15 @@ int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported) } } if (!generator) { - if (skip_if_not_supported) { - fprintf(stderr, - "Erase successful, but not automatically formatting.\n"); - fprintf(stderr, - "File system type %s not supported.\n", response); - return 0; - } fprintf(stderr,"Formatting is not supported for filesystem with type '%s'.\n", response); return -1; } response[FB_RESPONSE_SZ] = '\0'; - snprintf(query, sizeof(query), "getvar:partition-size:%s", partition); - status = fb_command_response(usb, query, response); + snprintf(cmd, sizeof(cmd), "getvar:partition-size:%s", partition); + status = fb_command_response(usb, cmd, response); if (status) { - if (skip_if_not_supported) { - fprintf(stderr, - "Erase successful, but not automatically formatting.\n"); - fprintf(stderr, "Unable to get partition size\n."); - return 0; - } fprintf(stderr,"FAILED (%s)\n", fb_get_error()); return status; } @@ -278,7 +319,7 @@ int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported) if (status) goto cleanup; fprintf(stderr, "writing '%s'...\n", partition); - snprintf(cmd, CMD_SIZE, "flash:%s", partition); + snprintf(cmd, sizeof(cmd), "flash:%s", partition); status = fb_command(usb, cmd); if (status) goto cleanup; @@ -288,12 +329,11 @@ cleanup: return status; } -void fb_queue_format(const char *partition, int skip_if_not_supported) +void fb_queue_format(const char *partition) { Action *a; a = queue_action(OP_FORMAT, partition); - a->data = (void*)skip_if_not_supported; a->msg = mkmsg("formatting '%s' partition", partition); } @@ -507,7 +547,7 @@ int fb_execute_queue(usb_handle *usb) } else if (a->op == OP_NOTICE) { fprintf(stderr,"%s\n",(char*)a->data); } else if (a->op == OP_FORMAT) { - status = fb_format(a, usb, (int)a->data); + status = fb_format(a, usb); status = a->func(a, status, status ? fb_get_error() : ""); if (status) break; } else { diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c index f1b675f..b96d93b 100644 --- a/fastboot/fastboot.c +++ b/fastboot/fastboot.c @@ -89,6 +89,8 @@ char *find_item(const char *item, const char *product) fn = "system.img"; } else if(!strcmp(item,"userdata")) { fn = "userdata.img"; + } else if(!strcmp(item,"cache")) { + fn = "cache.img"; } else if(!strcmp(item,"info")) { fn = "android-info.txt"; } else { @@ -151,6 +153,7 @@ int match_fastboot(usb_ifc_info *info) { if(!(vendor_id && (info->dev_vendor == vendor_id)) && (info->dev_vendor != 0x18d1) && // Google + (info->dev_vendor != 0x8087) && // Intel (info->dev_vendor != 0x0451) && (info->dev_vendor != 0x0502) && (info->dev_vendor != 0x0fce) && // Sony Ericsson @@ -159,6 +162,7 @@ int match_fastboot(usb_ifc_info *info) (info->dev_vendor != 0x0955) && // Nvidia (info->dev_vendor != 0x413c) && // DELL (info->dev_vendor != 0x2314) && // INQ Mobile + (info->dev_vendor != 0x0b05) && // Asus (info->dev_vendor != 0x0bb4)) // HTC return -1; if(info->ifc_class != 0xff) return -1; @@ -650,7 +654,7 @@ int main(int argc, char **argv) skip(2); } else if(!strcmp(*argv, "format")) { require(2); - fb_queue_format(argv[1], 0); + fb_queue_format(argv[1]); skip(2); } else if(!strcmp(*argv, "signature")) { require(2); @@ -740,9 +744,7 @@ int main(int argc, char **argv) if (wants_wipe) { fb_queue_erase("userdata"); - fb_queue_format("userdata", 1); fb_queue_erase("cache"); - fb_queue_format("cache", 1); } if (wants_reboot) { fb_queue_reboot(); diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h index 6d036f8..c249a8f 100644 --- a/fastboot/fastboot.h +++ b/fastboot/fastboot.h @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -43,7 +43,7 @@ char *fb_get_error(void); /* engine.c - high level command queue engine */ void fb_queue_flash(const char *ptn, void *data, unsigned sz);; void fb_queue_erase(const char *ptn); -void fb_queue_format(const char *ptn, int skip_if_not_supported); +void fb_queue_format(const char *ptn); void fb_queue_require(const char *prod, const char *var, int invert, unsigned nvalues, const char **value); void fb_queue_display(const char *var, const char *prettyname); diff --git a/fastboot/protocol.c b/fastboot/protocol.c index 3948363..e871113 100644 --- a/fastboot/protocol.c +++ b/fastboot/protocol.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -40,7 +40,7 @@ char *fb_get_error(void) return ERROR; } -static int check_response(usb_handle *usb, unsigned size, +static int check_response(usb_handle *usb, unsigned size, unsigned data_okay, char *response) { unsigned char status[65]; @@ -106,7 +106,7 @@ static int _command_send(usb_handle *usb, const char *cmd, { int cmdsize = strlen(cmd); int r; - + if(response) { response[0] = 0; } @@ -145,7 +145,7 @@ static int _command_send(usb_handle *usb, const char *cmd, return -1; } } - + r = check_response(usb, 0, 0, 0); if(r < 0) { return -1; @@ -168,10 +168,10 @@ int fb_download_data(usb_handle *usb, const void *data, unsigned size) { char cmd[64]; int r; - + sprintf(cmd, "download:%08x", size); r = _command_send(usb, cmd, data, size, 0); - + if(r < 0) { return -1; } else { diff --git a/fastboot/usb.h b/fastboot/usb.h index 7a7d55b..d504ee2 100644 --- a/fastboot/usb.h +++ b/fastboot/usb.h @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -42,20 +42,20 @@ struct usb_ifc_info unsigned char dev_class; unsigned char dev_subclass; unsigned char dev_protocol; - + unsigned char ifc_class; unsigned char ifc_subclass; unsigned char ifc_protocol; unsigned char has_bulk_in; unsigned char has_bulk_out; - + unsigned char writable; char serial_number[256]; char device_path[256]; }; - + typedef int (*ifc_match_func)(usb_ifc_info *ifc); usb_handle *usb_open(ifc_match_func callback); diff --git a/fastboot/usb_linux.c b/fastboot/usb_linux.c index 19be51e..b7a9ca3 100644 --- a/fastboot/usb_linux.c +++ b/fastboot/usb_linux.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -67,7 +67,7 @@ */ #define MAX_USBFS_BULK_SIZE (16 * 1024) -struct usb_handle +struct usb_handle { char fname[64]; int desc; @@ -86,12 +86,12 @@ static inline int badname(const char *name) static int check(void *_desc, int len, unsigned type, int size) { unsigned char *desc = _desc; - + if(len < size) return -1; if(desc[0] < size) return -1; if(desc[0] > len) return -1; if(desc[1] != type) return -1; - + return 0; } @@ -104,7 +104,7 @@ static int filter_usb_device(int fd, char *ptr, int len, int writable, struct usb_interface_descriptor *ifc; struct usb_endpoint_descriptor *ept; struct usb_ifc_info info; - + int in, out; unsigned i; unsigned e; @@ -117,35 +117,38 @@ static int filter_usb_device(int fd, char *ptr, int len, int writable, dev = (void*) ptr; len -= dev->bLength; ptr += dev->bLength; - + if(check(ptr, len, USB_DT_CONFIG, USB_DT_CONFIG_SIZE)) return -1; cfg = (void*) ptr; len -= cfg->bLength; ptr += cfg->bLength; - + info.dev_vendor = dev->idVendor; info.dev_product = dev->idProduct; info.dev_class = dev->bDeviceClass; info.dev_subclass = dev->bDeviceSubClass; info.dev_protocol = dev->bDeviceProtocol; info.writable = writable; - + // read device serial number (if there is one) info.serial_number[0] = 0; if (dev->iSerialNumber) { struct usbdevfs_ctrltransfer ctrl; - __u16 buffer[128]; + // Keep it short enough because some bootloaders are borked if the URB len is > 255 + // 128 is too big by 1. + __u16 buffer[127]; memset(buffer, 0, sizeof(buffer)); ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE; ctrl.bRequest = USB_REQ_GET_DESCRIPTOR; ctrl.wValue = (USB_DT_STRING << 8) | dev->iSerialNumber; - ctrl.wIndex = 0; + //language ID (en-us) for serial number string + ctrl.wIndex = 0x0409; ctrl.wLength = sizeof(buffer); ctrl.data = buffer; - ctrl.timeout = 50; + ctrl.timeout = 50; result = ioctl(fd, USBDEVFS_CONTROL, &ctrl); if (result > 0) { @@ -200,23 +203,23 @@ static int filter_usb_device(int fd, char *ptr, int len, int writable, ifc = (void*) ptr; len -= ifc->bLength; ptr += ifc->bLength; - + in = -1; out = -1; info.ifc_class = ifc->bInterfaceClass; info.ifc_subclass = ifc->bInterfaceSubClass; info.ifc_protocol = ifc->bInterfaceProtocol; - + for(e = 0; e < ifc->bNumEndpoints; e++) { if(check(ptr, len, USB_DT_ENDPOINT, USB_DT_ENDPOINT_SIZE)) return -1; ept = (void*) ptr; len -= ept->bLength; ptr += ept->bLength; - + if((ept->bmAttributes & 0x03) != 0x02) continue; - + if(ept->bEndpointAddress & 0x80) { in = ept->bEndpointAddress; } else { @@ -226,7 +229,7 @@ static int filter_usb_device(int fd, char *ptr, int len, int writable, info.has_bulk_in = (in != -1); info.has_bulk_out = (out != -1); - + if(callback(&info) == 0) { *ept_in_id = in; *ept_out_id = out; @@ -244,25 +247,25 @@ static usb_handle *find_usb_device(const char *base, ifc_match_func callback) char busname[64], devname[64]; char desc[1024]; int n, in, out, ifc; - + DIR *busdir, *devdir; struct dirent *de; int fd; int writable; - + busdir = opendir(base); if(busdir == 0) return 0; while((de = readdir(busdir)) && (usb == 0)) { if(badname(de->d_name)) continue; - + sprintf(busname, "%s/%s", base, de->d_name); devdir = opendir(busname); if(devdir == 0) continue; - + // DBG("[ scanning %s ]\n", busname); while((de = readdir(devdir)) && (usb == 0)) { - + if(badname(de->d_name)) continue; sprintf(devname, "%s/%s", busname, de->d_name); @@ -278,7 +281,7 @@ static usb_handle *find_usb_device(const char *base, ifc_match_func callback) } n = read(fd, desc, sizeof(desc)); - + if(filter_usb_device(fd, desc, n, writable, callback, &in, &out, &ifc) == 0) { usb = calloc(1, sizeof(usb_handle)); @@ -315,13 +318,13 @@ int usb_write(usb_handle *h, const void *_data, int len) if(h->ep_out == 0) { return -1; } - + if(len == 0) { bulk.ep = h->ep_out; bulk.len = 0; bulk.data = data; bulk.timeout = 0; - + n = ioctl(h->desc, USBDEVFS_BULK, &bulk); if(n != 0) { fprintf(stderr,"ERROR: n = %d, errno = %d (%s)\n", @@ -330,16 +333,16 @@ int usb_write(usb_handle *h, const void *_data, int len) } return 0; } - + while(len > 0) { int xfer; xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; - + bulk.ep = h->ep_out; bulk.len = xfer; bulk.data = data; bulk.timeout = 0; - + n = ioctl(h->desc, USBDEVFS_BULK, &bulk); if(n != xfer) { DBG("ERROR: n = %d, errno = %d (%s)\n", @@ -365,10 +368,10 @@ int usb_read(usb_handle *h, void *_data, int len) if(h->ep_in == 0) { return -1; } - + while(len > 0) { int xfer = (len > MAX_USBFS_BULK_SIZE) ? MAX_USBFS_BULK_SIZE : len; - + bulk.ep = h->ep_in; bulk.len = xfer; bulk.data = data; @@ -391,12 +394,12 @@ int usb_read(usb_handle *h, void *_data, int len) count += n; len -= n; data += n; - + if(n < xfer) { break; } } - + return count; } @@ -415,7 +418,7 @@ void usb_kick(usb_handle *h) int usb_close(usb_handle *h) { int fd; - + fd = h->desc; h->desc = -1; if(fd >= 0) { diff --git a/fastboot/usb_osx.c b/fastboot/usb_osx.c index 605c8e2..1548ba8 100644 --- a/fastboot/usb_osx.c +++ b/fastboot/usb_osx.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -55,7 +55,7 @@ struct usb_handle int success; ifc_match_func callback; usb_ifc_info info; - + UInt8 bulkIn; UInt8 bulkOut; IOUSBInterfaceInterface190 **interface; @@ -132,7 +132,7 @@ static int try_interfaces(IOUSBDeviceInterface182 **dev, usb_handle *handle) { // continue so we can try the next interface continue; } - + /* * Now open the interface. This will cause the pipes * associated with the endpoints in the interface descriptor @@ -149,7 +149,7 @@ static int try_interfaces(IOUSBDeviceInterface182 **dev, usb_handle *handle) { * use the interface. Maybe something needs to be done about * this situation. */ - + kr = (*interface)->USBInterfaceOpen(interface); if (kr != 0) { @@ -158,7 +158,7 @@ static int try_interfaces(IOUSBDeviceInterface182 **dev, usb_handle *handle) { // continue so we can try the next interface continue; } - + // Get the number of endpoints associated with this interface. kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints); @@ -242,10 +242,10 @@ static int try_interfaces(IOUSBDeviceInterface182 **dev, usb_handle *handle) { ERR("could not clear output pipe; result %x, ignoring....\n", kr); } } - + return 0; } - + next_interface: (*interface)->USBInterfaceClose(interface); (*interface)->Release(interface); @@ -285,7 +285,7 @@ static int try_device(io_service_t device, usb_handle *handle) { goto error; } - /* + /* * We don't need the intermediate interface after the device interface * is created. */ @@ -339,14 +339,15 @@ static int try_device(io_service_t device, usb_handle *handle) { req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); req.bRequest = kUSBRqGetDescriptor; req.wValue = (kUSBStringDesc << 8) | serialIndex; - req.wIndex = 0; + //language ID (en-us) for serial number string + req.wIndex = 0x0409; req.pData = buffer; req.wLength = sizeof(buffer); kr = (*dev)->DeviceRequest(dev, &req); if (kr == kIOReturnSuccess && req.wLenDone > 0) { int i, count; - + // skip first word, and copy the rest to the serial string, changing shorts to bytes. count = (req.wLenDone - 1) / 2; for (i = 0; i < count; i++) @@ -371,8 +372,8 @@ static int try_device(io_service_t device, usb_handle *handle) { if (dev != NULL) { (*dev)->Release(dev); } - - return -1; + + return -1; } @@ -406,7 +407,7 @@ static int init_usb(ifc_match_func callback, usb_handle **handle) { ERR("Could not create iterator."); return -1; } - + for (;;) { if (! IOIteratorIsValid(iterator)) { /* @@ -416,7 +417,7 @@ static int init_usb(ifc_match_func callback, usb_handle **handle) { IOIteratorReset(iterator); continue; } - + io_service_t device = IOIteratorNext(iterator); if (device == 0) { diff --git a/fastboot/usb_windows.c b/fastboot/usb_windows.c index 10317bf..7aa36b2 100644 --- a/fastboot/usb_windows.c +++ b/fastboot/usb_windows.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -51,13 +51,13 @@ struct usb_handle { /// Handle to USB interface ADBAPIHANDLE adb_interface; - + /// Handle to USB read pipe (endpoint) ADBAPIHANDLE adb_read_pipe; - + /// Handle to USB write pipe (endpoint) ADBAPIHANDLE adb_write_pipe; - + /// Interface name char* interface_name; }; @@ -303,7 +303,7 @@ int recognized_device(usb_handle* handle, ifc_match_func callback) { info.ifc_subclass = interf_desc.bInterfaceSubClass; info.ifc_protocol = interf_desc.bInterfaceProtocol; info.writable = 1; - + // read serial number (if there is one) unsigned long serial_number_len = sizeof(info.serial_number); if (!AdbGetSerialNumber(handle->adb_interface, info.serial_number, diff --git a/fastboot/usbtest.c b/fastboot/usbtest.c index e34d7e6..b8fb9e2 100644 --- a/fastboot/usbtest.c +++ b/fastboot/usbtest.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -38,11 +38,11 @@ static unsigned arg_size = 4096; static unsigned arg_count = 4096; -long long NOW(void) +long long NOW(void) { struct timeval tv; gettimeofday(&tv, 0); - + return (((long long) tv.tv_sec) * ((long long) 1000000)) + (((long long) tv.tv_usec)); } @@ -110,7 +110,7 @@ int test_zero(usb_handle *usb) int i; unsigned char buf[4096]; long long t0, t1; - + t0 = NOW(); for(i = 0; i < arg_count; i++) { if(usb_read(usb, buf, arg_size) != arg_size) { @@ -123,7 +123,7 @@ int test_zero(usb_handle *usb) return 0; } -struct +struct { const char *cmd; ifc_match_func match; @@ -179,12 +179,12 @@ int main(int argc, char **argv) { usb_handle *usb; int i; - + if(argc < 2) return usage(); if(argc > 2) { - if(process_args(argc - 2, argv + 2)) + if(process_args(argc - 2, argv + 2)) return -1; } diff --git a/fastboot/util_linux.c b/fastboot/util_linux.c index 912e16f..91c3776 100644 --- a/fastboot/util_linux.c +++ b/fastboot/util_linux.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF @@ -38,7 +38,7 @@ void get_my_path(char *path) { char proc[64]; char *x; - + sprintf(proc, "/proc/%d/exe", getpid()); int err = readlink(proc, path, PATH_MAX - 1); diff --git a/fastboot/util_osx.c b/fastboot/util_osx.c index b43e316..26b832a 100644 --- a/fastboot/util_osx.c +++ b/fastboot/util_osx.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF diff --git a/fastboot/util_windows.c b/fastboot/util_windows.c index 37077a4..c3d545c 100644 --- a/fastboot/util_windows.c +++ b/fastboot/util_windows.c @@ -9,7 +9,7 @@ * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the + * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS @@ -19,7 +19,7 @@ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
