summaryrefslogtreecommitdiffstats
path: root/fastboot
diff options
context:
space:
mode:
Diffstat (limited to 'fastboot')
-rw-r--r--fastboot/Android.mk23
-rw-r--r--fastboot/bootimg.c20
-rw-r--r--fastboot/engine.c305
-rw-r--r--fastboot/fastboot.c412
-rw-r--r--fastboot/fastboot.h14
-rw-r--r--fastboot/protocol.c199
-rw-r--r--fastboot/usb.h11
-rw-r--r--fastboot/usb_linux.c105
-rw-r--r--fastboot/usb_osx.c36
-rw-r--r--fastboot/usb_windows.c14
-rw-r--r--fastboot/usbtest.c16
-rw-r--r--fastboot/util_linux.c6
-rw-r--r--fastboot/util_osx.c4
-rw-r--r--fastboot/util_windows.c49
14 files changed, 1012 insertions, 202 deletions
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index 3c5538f..1189e1f 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -16,9 +16,11 @@ LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/../mkbootimg
-LOCAL_SRC_FILES := protocol.c engine.c bootimg.c fastboot.c
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/../mkbootimg \
+ $(LOCAL_PATH)/../../extras/ext4_utils
+LOCAL_SRC_FILES := protocol.c engine.c bootimg.c fastboot.c
LOCAL_MODULE := fastboot
+LOCAL_MODULE_TAGS := debug
ifeq ($(HOST_OS),linux)
LOCAL_SRC_FILES += usb_linux.c util_linux.c
@@ -47,10 +49,23 @@ ifeq ($(HOST_OS),windows)
LOCAL_C_INCLUDES += development/host/windows/usb/api
endif
-LOCAL_STATIC_LIBRARIES := $(EXTRA_STATIC_LIBS) libzipfile libunz
+LOCAL_STATIC_LIBRARIES := \
+ $(EXTRA_STATIC_LIBS) \
+ libzipfile \
+ libunz \
+ libext4_utils_host \
+ libsparse_host \
+ libz
+
+ifneq ($(HOST_OS),windows)
+LOCAL_STATIC_LIBRARIES += libselinux
+endif # HOST_OS != windows
include $(BUILD_HOST_EXECUTABLE)
-$(call dist-for-goals,dist_files,$(LOCAL_BUILT_MODULE))
+
+
+$(call dist-for-goals,dist_files sdk,$(LOCAL_BUILT_MODULE))
+
ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS)
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 6d94035..8d46991 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,20 +19,34 @@
* 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
* SUCH DAMAGE.
*/
+#include "fastboot.h"
+#include "make_ext4fs.h"
+
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <stdbool.h>
#include <string.h>
+#include <sys/stat.h>
#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
-#include "fastboot.h"
+#ifdef USE_MINGW
+#include <fcntl.h>
+#else
+#include <sys/mman.h>
+#endif
+
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
double now()
{
@@ -50,7 +64,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;
@@ -60,15 +74,19 @@ char *mkmsg(const char *fmt, ...)
#define OP_COMMAND 2
#define OP_QUERY 3
#define OP_NOTICE 4
+#define OP_FORMAT 5
+#define OP_DOWNLOAD_SPARSE 6
typedef struct Action Action;
-struct Action
+#define CMD_SIZE 64
+
+struct Action
{
unsigned op;
Action *next;
- char cmd[64];
+ char cmd[CMD_SIZE];
const char *prod;
void *data;
unsigned size;
@@ -82,6 +100,82 @@ struct Action
static Action *action_list = 0;
static Action *action_last = 0;
+
+struct image_data {
+ long long partition_size;
+ long long image_size; // real size of image file
+ void *buffer;
+};
+
+void generate_ext4_image(struct image_data *image);
+void cleanup_image(struct image_data *image);
+
+int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...)
+{
+ char cmd[CMD_SIZE] = "getvar:";
+ int getvar_len = strlen(cmd);
+ va_list args;
+
+ response[FB_RESPONSE_SZ] = '\0';
+ va_start(args, fmt);
+ vsnprintf(cmd + getvar_len, sizeof(cmd) - getvar_len, fmt, args);
+ va_end(args);
+ cmd[CMD_SIZE - 1] = '\0';
+ return fb_command_response(usb, cmd, response);
+}
+
+struct generator {
+ char *fs_type;
+
+ /* generate image and return it as image->buffer.
+ * size of the buffer returned as image->image_size.
+ *
+ * image->partition_size specifies what is the size of the
+ * file partition we generate image for.
+ */
+ void (*generate)(struct image_data *image);
+
+ /* it cleans the buffer allocated during image creation.
+ * this function probably does free() or munmap().
+ */
+ void (*cleanup)(struct image_data *image);
+} generators[] = {
+ { "ext4", generate_ext4_image, cleanup_image }
+};
+
+/* Return true if this partition is supported by the fastboot format command.
+ * It is also used to determine if we should first erase a partition before
+ * flashing it with an ext4 filesystem. See needs_erase()
+ *
+ * Not all devices report the filesystem type, so don't report any errors,
+ * just return false.
+ */
+int fb_format_supported(usb_handle *usb, const char *partition)
+{
+ char response[FB_RESPONSE_SZ+1];
+ struct generator *generator = NULL;
+ int status;
+ unsigned int i;
+
+ status = fb_getvar(usb, response, "partition-type:%s", partition);
+ if (status) {
+ return 0;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(generators); i++) {
+ if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
+ generator = &generators[i];
+ break;
+ }
+ }
+
+ if (generator) {
+ return 1;
+ }
+
+ return 0;
+}
+
static int cb_default(Action *a, int status, char *resp)
{
if (status) {
@@ -133,6 +227,177 @@ void fb_queue_erase(const char *ptn)
a->msg = mkmsg("erasing '%s'", ptn);
}
+/* 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)
+{
+ 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());
+#endif
+ make_ext4fs_sparse_fd(fd, image->partition_size, NULL, NULL);
+
+ fstat(fd, &st);
+ image->image_size = st.st_size;
+ image->buffer = load_buffer(fd, st.st_size);
+
+ close(fd);
+}
+
+int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported)
+{
+ const char *partition = a->cmd;
+ char response[FB_RESPONSE_SZ+1];
+ int status = 0;
+ struct image_data image;
+ struct generator *generator = NULL;
+ int fd;
+ unsigned i;
+ char cmd[CMD_SIZE];
+
+ status = fb_getvar(usb, response, "partition-type:%s", partition);
+ 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;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(generators); i++) {
+ if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
+ generator = &generators[i];
+ break;
+ }
+ }
+ 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;
+ }
+
+ status = fb_getvar(usb, response, "partition-size:%s", partition);
+ 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;
+ }
+ image.partition_size = strtoll(response, (char **)NULL, 16);
+
+ generator->generate(&image);
+ if (!image.buffer) {
+ fprintf(stderr,"Cannot generate image.\n");
+ return -1;
+ }
+
+ // Following piece of code is similar to fb_queue_flash() but executes
+ // actions directly without queuing
+ fprintf(stderr, "sending '%s' (%lli KB)...\n", partition, image.image_size/1024);
+ status = fb_download_data(usb, image.buffer, image.image_size);
+ if (status) goto cleanup;
+
+ fprintf(stderr, "writing '%s'...\n", partition);
+ snprintf(cmd, sizeof(cmd), "flash:%s", partition);
+ status = fb_command(usb, cmd);
+ if (status) goto cleanup;
+
+cleanup:
+ generator->cleanup(&image);
+
+ return status;
+}
+
+void fb_queue_format(const char *partition, int skip_if_not_supported)
+{
+ Action *a;
+
+ a = queue_action(OP_FORMAT, partition);
+ a->data = (void*)skip_if_not_supported;
+ a->msg = mkmsg("formatting '%s' partition", partition);
+}
+
void fb_queue_flash(const char *ptn, void *data, unsigned sz)
{
Action *a;
@@ -146,6 +411,19 @@ void fb_queue_flash(const char *ptn, void *data, unsigned sz)
a->msg = mkmsg("writing '%s'", ptn);
}
+void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
+{
+ Action *a;
+
+ a = queue_action(OP_DOWNLOAD_SPARSE, "");
+ a->data = s;
+ a->size = 0;
+ a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024);
+
+ a = queue_action(OP_COMMAND, "flash:%s", ptn);
+ a->msg = mkmsg("writing '%s'", ptn);
+}
+
static int match(char *str, const char **value, unsigned count)
{
const char *val;
@@ -316,6 +594,8 @@ int fb_execute_queue(usb_handle *usb)
int status = 0;
a = action_list;
+ if (!a)
+ return status;
resp[FB_RESPONSE_SZ] = 0;
double start = -1;
@@ -340,6 +620,14 @@ int fb_execute_queue(usb_handle *usb)
if (status) break;
} 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 = a->func(a, status, status ? fb_get_error() : "");
+ if (status) break;
+ } else if (a->op == OP_DOWNLOAD_SPARSE) {
+ status = fb_download_data_sparse(usb, a->data);
+ status = a->func(a, status, status ? fb_get_error() : "");
+ if (status) break;
} else {
die("bogus action");
}
@@ -348,3 +636,8 @@ int fb_execute_queue(usb_handle *usb)
fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
return status;
}
+
+int fb_queue_is_empty(void)
+{
+ return (action_list == NULL);
+}
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index fe0af7a..3de6d7d 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -26,22 +26,34 @@
* SUCH DAMAGE.
*/
+#define _LARGEFILE64_SOURCE
+
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <ctype.h>
+#include <getopt.h>
#include <sys/time.h>
+#include <sys/types.h>
+
#include <bootimg.h>
+#include <sparse/sparse.h>
#include <zipfile/zipfile.h>
#include "fastboot.h"
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
char cur_product[FB_RESPONSE_SZ + 1];
void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
@@ -58,6 +70,9 @@ static const char *product = 0;
static const char *cmdline = 0;
static int wipe_data = 0;
static unsigned short vendor_id = 0;
+static int long_listing = 0;
+static int64_t sparse_limit = -1;
+static int64_t target_sparse_limit = -1;
static unsigned base_addr = 0x10000000;
@@ -88,6 +103,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 {
@@ -114,12 +131,33 @@ char *find_item(const char *item, const char *product)
#ifdef _WIN32
void *load_file(const char *fn, unsigned *_sz);
+int64_t file_size(const char *fn);
#else
+#if defined(__APPLE__) && defined(__MACH__)
+#define lseek64 lseek
+#define off64_t off_t
+#endif
+
+int64_t file_size(const char *fn)
+{
+ off64_t off;
+ int fd;
+
+ fd = open(fn, O_RDONLY);
+ if (fd < 0) return -1;
+
+ off = lseek64(fd, 0, SEEK_END);
+ close(fd);
+
+ return off;
+}
+
void *load_file(const char *fn, unsigned *_sz)
{
char *data;
int sz;
int fd;
+ int errno_tmp;
data = 0;
fd = open(fn, O_RDONLY);
@@ -140,14 +178,21 @@ void *load_file(const char *fn, unsigned *_sz)
return data;
oops:
+ errno_tmp = errno;
close(fd);
if(data != 0) free(data);
+ errno = errno_tmp;
return 0;
}
#endif
int match_fastboot(usb_ifc_info *info)
{
+ return match_fastboot_with_serial(info, serial);
+}
+
+int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
+{
if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
(info->dev_vendor != 0x18d1) && // Google
(info->dev_vendor != 0x8087) && // Intel
@@ -158,20 +203,23 @@ int match_fastboot(usb_ifc_info *info)
(info->dev_vendor != 0x22b8) && // Motorola
(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;
if(info->ifc_subclass != 0x42) return -1;
if(info->ifc_protocol != 0x03) return -1;
- // require matching serial number if a serial number is specified
+ // require matching serial number or device path if requested
// at the command line with the -s option.
- if (serial && strcmp(serial, info->serial_number) != 0) return -1;
+ if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
+ strcmp(local_serial, info->device_path) != 0)) return -1;
return 0;
}
int list_devices_callback(usb_ifc_info *info)
{
- if (match_fastboot(info) == 0) {
+ if (match_fastboot_with_serial(info, NULL) == 0) {
char* serial = info->serial_number;
if (!info->writable) {
serial = "no permissions"; // like "adb devices"
@@ -180,7 +228,13 @@ int list_devices_callback(usb_ifc_info *info)
serial = "????????????";
}
// output compatible with "adb devices"
- printf("%s\tfastboot\n", serial);
+ if (!long_listing) {
+ printf("%s\tfastboot\n", serial);
+ } else if (!info->device_path) {
+ printf("%-22s fastboot\n", serial);
+ } else {
+ printf("%-22s fastboot %s\n", serial, info->device_path);
+ }
}
return -1;
@@ -222,6 +276,7 @@ void usage(void)
" flashall flash boot + recovery + system\n"
" flash <partition> [ <filename> ] write a file to a flash partition\n"
" erase <partition> erase a flash partition\n"
+ " format <partition> format a flash partition \n"
" getvar <variable> display a bootloader variable\n"
" boot <kernel> [ <ramdisk> ] download and boot kernel\n"
" flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
@@ -232,13 +287,20 @@ void usage(void)
" help show this help message\n"
"\n"
"options:\n"
- " -w erase userdata and cache\n"
- " -s <serial number> specify device serial number\n"
+ " -w erase userdata and cache (and format\n"
+ " if supported by partition type)\n"
+ " -u do not first erase partition before\n"
+ " formatting\n"
+ " -s <specific device> specify device serial number\n"
+ " or path to device port\n"
+ " -l with \"devices\", lists device paths\n"
" -p <product> specify product name\n"
" -c <cmdline> override kernel commandline\n"
" -i <vendor id> specify a custom USB vendor id\n"
" -b <base_addr> specify a custom kernel base address\n"
" -n <page size> specify the nand page size. default: 2048\n"
+ " -S <size>[K|M|G] automatically sparse files greater than\n"
+ " size. 0 to disable\n"
);
}
@@ -257,7 +319,7 @@ void *load_bootable_image(unsigned page_size, const char *kernel, const char *ra
kdata = load_file(kernel, &ksize);
if(kdata == 0) {
- fprintf(stderr, "cannot load '%s'\n", kernel);
+ fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
return 0;
}
@@ -277,7 +339,7 @@ void *load_bootable_image(unsigned page_size, const char *kernel, const char *ra
if(ramdisk) {
rdata = load_file(ramdisk, &rsize);
if(rdata == 0) {
- fprintf(stderr,"cannot load '%s'\n", ramdisk);
+ fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
return 0;
}
}
@@ -424,6 +486,122 @@ void queue_info_dump(void)
fb_queue_notice("--------------------------------------------");
}
+
+struct sparse_file **load_sparse_files(const char *fname, int max_size)
+{
+ int fd;
+ struct sparse_file *s;
+ int files;
+ struct sparse_file **out_s;
+
+ fd = open(fname, O_RDONLY | O_BINARY);
+ if (fd < 0) {
+ die("cannot open '%s'\n", fname);
+ }
+
+ s = sparse_file_import_auto(fd, false);
+ if (!s) {
+ die("cannot sparse read file '%s'\n", fname);
+ }
+
+ files = sparse_file_resparse(s, max_size, NULL, 0);
+ if (files < 0) {
+ die("Failed to resparse '%s'\n", fname);
+ }
+
+ out_s = calloc(sizeof(struct sparse_file *), files + 1);
+ if (!out_s) {
+ die("Failed to allocate sparse file array\n");
+ }
+
+ files = sparse_file_resparse(s, max_size, out_s, files);
+ if (files < 0) {
+ die("Failed to resparse '%s'\n", fname);
+ }
+
+ return out_s;
+}
+
+static int64_t get_target_sparse_limit(struct usb_handle *usb)
+{
+ int64_t limit = 0;
+ char response[FB_RESPONSE_SZ + 1];
+ int status = fb_getvar(usb, response, "max-download-size");
+
+ if (!status) {
+ limit = strtoul(response, NULL, 0);
+ if (limit > 0) {
+ fprintf(stderr, "target reported max download size of %lld bytes\n",
+ limit);
+ }
+ }
+
+ return limit;
+}
+
+static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
+{
+ int64_t limit;
+
+ if (sparse_limit == 0) {
+ return 0;
+ } else if (sparse_limit > 0) {
+ limit = sparse_limit;
+ } else {
+ if (target_sparse_limit == -1) {
+ target_sparse_limit = get_target_sparse_limit(usb);
+ }
+ if (target_sparse_limit > 0) {
+ limit = target_sparse_limit;
+ } else {
+ return 0;
+ }
+ }
+
+ if (size > limit) {
+ return limit;
+ }
+
+ return 0;
+}
+
+/* Until we get lazy inode table init working in make_ext4fs, we need to
+ * erase partitions of type ext4 before flashing a filesystem so no stale
+ * inodes are left lying around. Otherwise, e2fsck gets very upset.
+ */
+static int needs_erase(const char *part)
+{
+ /* The function fb_format_supported() currently returns the value
+ * we want, so just call it.
+ */
+ return fb_format_supported(usb, part);
+}
+
+void do_flash(usb_handle *usb, const char *pname, const char *fname)
+{
+ int64_t sz64;
+ void *data;
+ int64_t limit;
+
+ sz64 = file_size(fname);
+ limit = get_sparse_limit(usb, sz64);
+ if (limit) {
+ struct sparse_file **s = load_sparse_files(fname, limit);
+ if (s == NULL) {
+ die("cannot sparse load '%s'\n", fname);
+ }
+ while (*s) {
+ sz64 = sparse_file_len(*s, true, false);
+ fb_queue_flash_sparse(pname, *s++, sz64);
+ }
+ } else {
+ unsigned int sz;
+ data = load_file(fname, &sz);
+ if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
+ fb_queue_flash(pname, data, sz);
+ }
+}
+
void do_update_signature(zipfile_t zip, char *fn)
{
void *data;
@@ -434,7 +612,7 @@ void do_update_signature(zipfile_t zip, char *fn)
fb_queue_command("signature", "installing signature");
}
-void do_update(char *fn)
+void do_update(char *fn, int erase_first)
{
void *zdata;
unsigned zsize;
@@ -447,7 +625,7 @@ void do_update(char *fn)
fb_queue_query_save("product", cur_product, sizeof(cur_product));
zdata = load_file(fn, &zsize);
- if (zdata == 0) die("failed to load '%s'", fn);
+ if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
zip = init_zipfile(zdata, zsize);
if(zip == 0) die("failed to access zipdata in '%s'");
@@ -472,17 +650,26 @@ void do_update(char *fn)
data = unzip_file(zip, "boot.img", &sz);
if (data == 0) die("update package missing boot.img");
do_update_signature(zip, "boot.sig");
+ if (erase_first && needs_erase("boot")) {
+ fb_queue_erase("boot");
+ }
fb_queue_flash("boot", data, sz);
data = unzip_file(zip, "recovery.img", &sz);
if (data != 0) {
do_update_signature(zip, "recovery.sig");
+ if (erase_first && needs_erase("recovery")) {
+ fb_queue_erase("recovery");
+ }
fb_queue_flash("recovery", data, sz);
}
data = unzip_file(zip, "system.img", &sz);
if (data == 0) die("update package missing system.img");
do_update_signature(zip, "system.sig");
+ if (erase_first && needs_erase("system")) {
+ fb_queue_erase("system");
+ }
fb_queue_flash("system", data, sz);
}
@@ -504,7 +691,7 @@ void do_send_signature(char *fn)
fb_queue_command("signature", "installing signature");
}
-void do_flashall(void)
+void do_flashall(int erase_first)
{
char *fname;
void *data;
@@ -517,26 +704,35 @@ void do_flashall(void)
fname = find_item("info", product);
if (fname == 0) die("cannot find android-info.txt");
data = load_file(fname, &sz);
- if (data == 0) die("could not load android-info.txt");
+ if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
setup_requirements(data, sz);
fname = find_item("boot", product);
data = load_file(fname, &sz);
- if (data == 0) die("could not load boot.img");
+ if (data == 0) die("could not load boot.img: %s", strerror(errno));
do_send_signature(fname);
+ if (erase_first && needs_erase("boot")) {
+ fb_queue_erase("boot");
+ }
fb_queue_flash("boot", data, sz);
fname = find_item("recovery", product);
data = load_file(fname, &sz);
if (data != 0) {
do_send_signature(fname);
+ if (erase_first && needs_erase("recovery")) {
+ fb_queue_erase("recovery");
+ }
fb_queue_flash("recovery", data, sz);
}
fname = find_item("system", product);
data = load_file(fname, &sz);
- if (data == 0) die("could not load system.img");
+ if (data == 0) die("could not load system.img: %s", strerror(errno));
do_send_signature(fname);
+ if (erase_first && needs_erase("system")) {
+ fb_queue_erase("system");
+ }
fb_queue_flash("system", data, sz);
}
@@ -561,82 +757,168 @@ int do_oem_command(int argc, char **argv)
return 0;
}
+static int64_t parse_num(const char *arg)
+{
+ char *endptr;
+ unsigned long long num;
+
+ num = strtoull(arg, &endptr, 0);
+ if (endptr == arg) {
+ return -1;
+ }
+
+ if (*endptr == 'k' || *endptr == 'K') {
+ if (num >= (-1ULL) / 1024) {
+ return -1;
+ }
+ num *= 1024LL;
+ endptr++;
+ } else if (*endptr == 'm' || *endptr == 'M') {
+ if (num >= (-1ULL) / (1024 * 1024)) {
+ return -1;
+ }
+ num *= 1024LL * 1024LL;
+ endptr++;
+ } else if (*endptr == 'g' || *endptr == 'G') {
+ if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
+ return -1;
+ }
+ num *= 1024LL * 1024LL * 1024LL;
+ endptr++;
+ }
+
+ if (*endptr != '\0') {
+ return -1;
+ }
+
+ if (num > INT64_MAX) {
+ return -1;
+ }
+
+ return num;
+}
+
int main(int argc, char **argv)
{
int wants_wipe = 0;
int wants_reboot = 0;
int wants_reboot_bootloader = 0;
+ int erase_first = 1;
void *data;
unsigned sz;
unsigned page_size = 2048;
int status;
+ int c;
+ int r;
- skip(1);
- if (argc == 0) {
+ const struct option longopts = { 0, 0, 0, 0 };
+
+ serial = getenv("ANDROID_SERIAL");
+
+ while (1) {
+ c = getopt_long(argc, argv, "wub:n:s:S:lp:c:i:m:h", &longopts, NULL);
+ if (c < 0) {
+ break;
+ }
+
+ switch (c) {
+ case 'w':
+ wants_wipe = 1;
+ break;
+ case 'u':
+ erase_first = 0;
+ break;
+ case 'b':
+ base_addr = strtoul(optarg, 0, 16);
+ break;
+ case 'n':
+ page_size = (unsigned)strtoul(optarg, NULL, 0);
+ if (!page_size) die("invalid page size");
+ break;
+ case 's':
+ serial = optarg;
+ break;
+ case 'S':
+ sparse_limit = parse_num(optarg);
+ if (sparse_limit < 0) {
+ die("invalid sparse limit");
+ }
+ break;
+ case 'l':
+ long_listing = 1;
+ break;
+ case 'p':
+ product = optarg;
+ break;
+ case 'c':
+ cmdline = optarg;
+ break;
+ case 'i': {
+ char *endptr = NULL;
+ unsigned long val;
+
+ val = strtoul(optarg, &endptr, 0);
+ if (!endptr || *endptr != '\0' || (val & ~0xffff))
+ die("invalid vendor id '%s'", optarg);
+ vendor_id = (unsigned short)val;
+ break;
+ }
+ case 'h':
+ usage();
+ return 1;
+ case '?':
+ return 1;
+ default:
+ abort();
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc == 0 && !wants_wipe) {
usage();
return 1;
}
- if (!strcmp(*argv, "devices")) {
+ if (argc > 0 && !strcmp(*argv, "devices")) {
+ skip(1);
list_devices();
return 0;
}
- if (!strcmp(*argv, "help")) {
+ if (argc > 0 && !strcmp(*argv, "help")) {
usage();
return 0;
}
-
- serial = getenv("ANDROID_SERIAL");
+ usb = open_device();
while (argc > 0) {
- if(!strcmp(*argv, "-w")) {
- wants_wipe = 1;
- skip(1);
- } else if(!strcmp(*argv, "-b")) {
- require(2);
- base_addr = strtoul(argv[1], 0, 16);
- skip(2);
- } else if(!strcmp(*argv, "-n")) {
- require(2);
- page_size = (unsigned)strtoul(argv[1], NULL, 0);
- if (!page_size) die("invalid page size");
- skip(2);
- } else if(!strcmp(*argv, "-s")) {
- require(2);
- serial = argv[1];
- skip(2);
- } else if(!strcmp(*argv, "-p")) {
- require(2);
- product = argv[1];
- skip(2);
- } else if(!strcmp(*argv, "-c")) {
- require(2);
- cmdline = argv[1];
- skip(2);
- } else if(!strcmp(*argv, "-i")) {
- char *endptr = NULL;
- unsigned long val;
-
- require(2);
- val = strtoul(argv[1], &endptr, 0);
- if (!endptr || *endptr != '\0' || (val & ~0xffff))
- die("invalid vendor id '%s'", argv[1]);
- vendor_id = (unsigned short)val;
- skip(2);
- } else if(!strcmp(*argv, "getvar")) {
+ if(!strcmp(*argv, "getvar")) {
require(2);
fb_queue_display(argv[1], argv[1]);
skip(2);
} else if(!strcmp(*argv, "erase")) {
require(2);
+
+ if (fb_format_supported(usb, argv[1])) {
+ fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
+ }
+
fb_queue_erase(argv[1]);
skip(2);
+ } else if(!strcmp(*argv, "format")) {
+ require(2);
+ if (erase_first && needs_erase(argv[1])) {
+ fb_queue_erase(argv[1]);
+ }
+ fb_queue_format(argv[1], 0);
+ skip(2);
} else if(!strcmp(*argv, "signature")) {
require(2);
data = load_file(argv[1], &sz);
- if (data == 0) die("could not load '%s'", argv[1]);
+ if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
if (sz != 256) die("signature must be 256 bytes");
fb_queue_download("signature", data, sz);
fb_queue_command("signature", "installing signature");
@@ -678,9 +960,10 @@ int main(int argc, char **argv)
skip(2);
}
if (fname == 0) die("cannot determine image filename for '%s'", pname);
- data = load_file(fname, &sz);
- if (data == 0) die("cannot load '%s'\n", fname);
- fb_queue_flash(pname, data, sz);
+ if (erase_first && needs_erase(pname)) {
+ fb_queue_erase(pname);
+ }
+ do_flash(usb, pname, fname);
} else if(!strcmp(*argv, "flash:raw")) {
char *pname = argv[1];
char *kname = argv[2];
@@ -697,14 +980,14 @@ int main(int argc, char **argv)
fb_queue_flash(pname, data, sz);
} else if(!strcmp(*argv, "flashall")) {
skip(1);
- do_flashall();
+ do_flashall(erase_first);
wants_reboot = 1;
} else if(!strcmp(*argv, "update")) {
if (argc > 1) {
- do_update(argv[1]);
+ do_update(argv[1], erase_first);
skip(2);
} else {
- do_update("update.zip");
+ do_update("update.zip", erase_first);
skip(1);
}
wants_reboot = 1;
@@ -718,7 +1001,9 @@ 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();
@@ -726,7 +1011,8 @@ int main(int argc, char **argv)
fb_queue_command("reboot-bootloader", "rebooting into bootloader");
}
- usb = open_device();
+ if (fb_queue_is_empty())
+ return 0;
status = fb_execute_queue(usb);
return (status) ? 1 : 0;
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index 9e043fe..c1b2964 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
@@ -31,18 +31,25 @@
#include "usb.h"
+struct sparse_file;
+
/* protocol.c - fastboot protocol */
int fb_command(usb_handle *usb, const char *cmd);
int fb_command_response(usb_handle *usb, const char *cmd, char *response);
int fb_download_data(usb_handle *usb, const void *data, unsigned size);
+int fb_download_data_sparse(usb_handle *usb, struct sparse_file *s);
char *fb_get_error(void);
#define FB_COMMAND_SZ 64
#define FB_RESPONSE_SZ 64
/* engine.c - high level command queue engine */
-void fb_queue_flash(const char *ptn, void *data, unsigned sz);;
+int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...);
+int fb_format_supported(usb_handle *usb, const char *partition);
+void fb_queue_flash(const char *ptn, void *data, unsigned sz);
+void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz);
void fb_queue_erase(const char *ptn);
+void fb_queue_format(const char *ptn, int skip_if_not_supported);
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);
@@ -52,6 +59,7 @@ void fb_queue_command(const char *cmd, const char *msg);
void fb_queue_download(const char *name, void *data, unsigned size);
void fb_queue_notice(const char *notice);
int fb_execute_queue(usb_handle *usb);
+int fb_queue_is_empty(void);
/* util stuff */
void die(const char *fmt, ...);
diff --git a/fastboot/protocol.c b/fastboot/protocol.c
index 3948363..a0e0fd4 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,18 +19,25 @@
* 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
* SUCH DAMAGE.
*/
+#define min(a, b) \
+ ({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; })
+#define round_down(a, b) \
+ ({ typeof(a) _a = (a); typeof(b) _b = (b); _a - (_a % _b); })
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <sparse/sparse.h>
+
#include "fastboot.h"
static char ERROR[128];
@@ -40,8 +47,7 @@ char *fb_get_error(void)
return ERROR;
}
-static int check_response(usb_handle *usb, unsigned size,
- unsigned data_okay, char *response)
+static int check_response(usb_handle *usb, unsigned int size, char *response)
{
unsigned char status[65];
int r;
@@ -82,7 +88,7 @@ static int check_response(usb_handle *usb, unsigned size,
return -1;
}
- if(!memcmp(status, "DATA", 4) && data_okay){
+ if(!memcmp(status, "DATA", 4) && size > 0){
unsigned dsize = strtoul((char*) status + 4, 0, 16);
if(dsize > size) {
strcpy(ERROR, "data size too large");
@@ -100,13 +106,12 @@ static int check_response(usb_handle *usb, unsigned size,
return -1;
}
-static int _command_send(usb_handle *usb, const char *cmd,
- const void *data, unsigned size,
- char *response)
+static int _command_start(usb_handle *usb, const char *cmd, unsigned size,
+ char *response)
{
int cmdsize = strlen(cmd);
int r;
-
+
if(response) {
response[0] = 0;
}
@@ -122,56 +127,91 @@ static int _command_send(usb_handle *usb, const char *cmd,
return -1;
}
- if(data == 0) {
- return check_response(usb, size, 0, response);
+ return check_response(usb, size, response);
+}
+
+static int _command_data(usb_handle *usb, const void *data, unsigned size)
+{
+ int r;
+
+ r = usb_write(usb, data, size);
+ if(r < 0) {
+ sprintf(ERROR, "data transfer failure (%s)", strerror(errno));
+ usb_close(usb);
+ return -1;
+ }
+ if(r != ((int) size)) {
+ sprintf(ERROR, "data transfer failure (short transfer)");
+ usb_close(usb);
+ return -1;
}
- r = check_response(usb, size, 1, 0);
+ return r;
+}
+
+static int _command_end(usb_handle *usb)
+{
+ int r;
+ r = check_response(usb, 0, 0);
if(r < 0) {
return -1;
}
- size = r;
+ return 0;
+}
- if(size) {
- r = usb_write(usb, data, size);
- if(r < 0) {
- sprintf(ERROR, "data transfer failure (%s)", strerror(errno));
- usb_close(usb);
- return -1;
- }
- if(r != ((int) size)) {
- sprintf(ERROR, "data transfer failure (short transfer)");
- usb_close(usb);
- return -1;
- }
+static int _command_send(usb_handle *usb, const char *cmd,
+ const void *data, unsigned size,
+ char *response)
+{
+ int r;
+ if (size == 0) {
+ return -1;
+ }
+
+ r = _command_start(usb, cmd, size, response);
+ if (r < 0) {
+ return -1;
+ }
+
+ r = _command_data(usb, data, size);
+ if (r < 0) {
+ return -1;
}
-
- r = check_response(usb, 0, 0, 0);
+
+ r = _command_end(usb);
if(r < 0) {
return -1;
- } else {
- return size;
}
+
+ return size;
+}
+
+static int _command_send_no_data(usb_handle *usb, const char *cmd,
+ char *response)
+{
+ int r;
+
+ return _command_start(usb, cmd, 0, response);
}
int fb_command(usb_handle *usb, const char *cmd)
{
- return _command_send(usb, cmd, 0, 0, 0);
+ return _command_send_no_data(usb, cmd, 0);
}
int fb_command_response(usb_handle *usb, const char *cmd, char *response)
{
- return _command_send(usb, cmd, 0, 0, response);
+ return _command_send_no_data(usb, cmd, response);
}
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 {
@@ -179,3 +219,96 @@ int fb_download_data(usb_handle *usb, const void *data, unsigned size)
}
}
+#define USB_BUF_SIZE 512
+static char usb_buf[USB_BUF_SIZE];
+static int usb_buf_len;
+
+static int fb_download_data_sparse_write(void *priv, const void *data, int len)
+{
+ int r;
+ usb_handle *usb = priv;
+ int to_write;
+ const char *ptr = data;
+
+ if (usb_buf_len) {
+ to_write = min(USB_BUF_SIZE - usb_buf_len, len);
+
+ memcpy(usb_buf + usb_buf_len, ptr, to_write);
+ usb_buf_len += to_write;
+ ptr += to_write;
+ len -= to_write;
+ }
+
+ if (usb_buf_len == USB_BUF_SIZE) {
+ r = _command_data(usb, usb_buf, USB_BUF_SIZE);
+ if (r != USB_BUF_SIZE) {
+ return -1;
+ }
+ usb_buf_len = 0;
+ }
+
+ if (len > USB_BUF_SIZE) {
+ if (usb_buf_len > 0) {
+ sprintf(ERROR, "internal error: usb_buf not empty\n");
+ return -1;
+ }
+ to_write = round_down(len, USB_BUF_SIZE);
+ r = _command_data(usb, ptr, to_write);
+ if (r != to_write) {
+ return -1;
+ }
+ ptr += to_write;
+ len -= to_write;
+ }
+
+ if (len > 0) {
+ if (len > USB_BUF_SIZE) {
+ sprintf(ERROR, "internal error: too much left for usb_buf\n");
+ return -1;
+ }
+ memcpy(usb_buf, ptr, len);
+ usb_buf_len = len;
+ }
+
+ return 0;
+}
+
+static int fb_download_data_sparse_flush(usb_handle *usb)
+{
+ int r;
+
+ if (usb_buf_len > 0) {
+ r = _command_data(usb, usb_buf, usb_buf_len);
+ if (r != usb_buf_len) {
+ return -1;
+ }
+ usb_buf_len = 0;
+ }
+
+ return 0;
+}
+
+int fb_download_data_sparse(usb_handle *usb, struct sparse_file *s)
+{
+ char cmd[64];
+ int r;
+ int size = sparse_file_len(s, true, false);
+ if (size <= 0) {
+ return -1;
+ }
+
+ sprintf(cmd, "download:%08x", size);
+ r = _command_start(usb, cmd, size, 0);
+ if (r < 0) {
+ return -1;
+ }
+
+ r = sparse_file_callback(s, true, false, fb_download_data_sparse_write, usb);
+ if (r < 0) {
+ return -1;
+ }
+
+ fb_download_data_sparse_flush(usb);
+
+ return _command_end(usb);
+}
diff --git a/fastboot/usb.h b/fastboot/usb.h
index cc157d5..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,19 +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 cbc64e4..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
@@ -32,6 +32,7 @@
#include <string.h>
#include <sys/ioctl.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
@@ -66,7 +67,7 @@
*/
#define MAX_USBFS_BULK_SIZE (16 * 1024)
-struct usb_handle
+struct usb_handle
{
char fname[64];
int desc;
@@ -85,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;
}
@@ -103,36 +104,40 @@ 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;
+ struct stat st;
+ int result;
+
if(check(ptr, len, USB_DT_DEVICE, USB_DT_DEVICE_SIZE))
return -1;
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];
- int result;
+ // 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));
@@ -156,29 +161,65 @@ static int filter_usb_device(int fd, char *ptr, int len, int writable,
}
}
+ /* We need to get a path that represents a particular port on a particular
+ * hub. We are passed an fd that was obtained by opening an entry under
+ * /dev/bus/usb. Unfortunately, the names of those entries change each
+ * time devices are plugged and unplugged. So how to get a repeatable
+ * path? udevadm provided the inspiration. We can get the major and
+ * minor of the device file, read the symlink that can be found here:
+ * /sys/dev/char/<major>:<minor>
+ * and then use the last element of that path. As a concrete example, I
+ * have an Android device at /dev/bus/usb/001/027 so working with bash:
+ * $ ls -l /dev/bus/usb/001/027
+ * crw-rw-r-- 1 root plugdev 189, 26 Apr 9 11:03 /dev/bus/usb/001/027
+ * $ ls -l /sys/dev/char/189:26
+ * lrwxrwxrwx 1 root root 0 Apr 9 11:03 /sys/dev/char/189:26 ->
+ * ../../devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4.2/1-4.2.3
+ * So our device_path would be 1-4.2.3 which says my device is connected
+ * to port 3 of a hub on port 2 of a hub on port 4 of bus 1 (per
+ * http://www.linux-usb.org/FAQ.html).
+ */
+ info.device_path[0] = '\0';
+ result = fstat(fd, &st);
+ if (!result && S_ISCHR(st.st_mode)) {
+ char cdev[128];
+ char link[256];
+ char *slash;
+ ssize_t link_len;
+ snprintf(cdev, sizeof(cdev), "/sys/dev/char/%d:%d",
+ major(st.st_rdev), minor(st.st_rdev));
+ link_len = readlink(cdev, link, sizeof(link) - 1);
+ if (link_len > 0) {
+ link[link_len] = '\0';
+ slash = strrchr(link, '/');
+ if (slash)
+ snprintf(info.device_path, sizeof(info.device_path), "usb:%s", slash+1);
+ }
+ }
+
for(i = 0; i < cfg->bNumInterfaces; i++) {
if(check(ptr, len, USB_DT_INTERFACE, USB_DT_INTERFACE_SIZE))
return -1;
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 {
@@ -188,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;
@@ -206,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);
@@ -240,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));
@@ -277,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",
@@ -292,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",
@@ -327,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;
@@ -353,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;
}
@@ -377,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 6df5d2c..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);
@@ -264,6 +264,7 @@ static int try_device(io_service_t device, usb_handle *handle) {
SInt32 score;
HRESULT result;
UInt8 serialIndex;
+ UInt32 locationId;
// Create an intermediate plugin.
kr = IOCreatePlugInInterfaceForService(device,
@@ -284,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.
*/
@@ -322,6 +323,13 @@ static int try_device(io_service_t device, usb_handle *handle) {
goto error;
}
+ kr = (*dev)->GetLocationID(dev, &locationId);
+ if (kr != 0) {
+ ERR("GetLocationId");
+ goto error;
+ }
+ snprintf(handle->info.device_path, sizeof(handle->info.device_path), "usb:%lX", locationId);
+
kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
if (serialIndex > 0) {
@@ -339,7 +347,7 @@ static int try_device(io_service_t device, usb_handle *handle) {
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++)
@@ -364,8 +372,8 @@ static int try_device(io_service_t device, usb_handle *handle) {
if (dev != NULL) {
(*dev)->Release(dev);
}
-
- return -1;
+
+ return -1;
}
@@ -399,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)) {
/*
@@ -409,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 1050293..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,
@@ -311,6 +311,8 @@ int recognized_device(usb_handle* handle, ifc_match_func callback) {
info.serial_number[0] = 0;
}
+ info.device_path[0] = 0;
+
if (callback(&info) == 0) {
return 1;
}
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..9e029fd 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
@@ -36,6 +36,29 @@
#include <windows.h>
+int64_t file_size(const char *fn)
+{
+ HANDLE file;
+ char *data;
+ DWORD sz;
+
+ file = CreateFile( fn,
+ GENERIC_READ,
+ FILE_SHARE_READ,
+ NULL,
+ OPEN_EXISTING,
+ 0,
+ NULL );
+
+ if (file == INVALID_HANDLE_VALUE)
+ return -1;
+
+ sz = GetFileSize( file, NULL );
+ CloseHandle( file );
+
+ return sz;
+}
+
void get_my_path(char exe[PATH_MAX])
{
char* r;
@@ -52,7 +75,7 @@ void *load_file(const char *fn, unsigned *_sz)
{
HANDLE file;
char *data;
- DWORD file_size;
+ DWORD sz;
file = CreateFile( fn,
GENERIC_READ,
@@ -65,29 +88,29 @@ void *load_file(const char *fn, unsigned *_sz)
if (file == INVALID_HANDLE_VALUE)
return NULL;
- file_size = GetFileSize( file, NULL );
+ sz = GetFileSize( file, NULL );
data = NULL;
- if (file_size > 0) {
- data = (char*) malloc( file_size );
+ if (sz > 0) {
+ data = (char*) malloc( sz );
if (data == NULL) {
- fprintf(stderr, "load_file: could not allocate %ld bytes\n", file_size );
- file_size = 0;
+ fprintf(stderr, "load_file: could not allocate %ld bytes\n", sz );
+ sz = 0;
} else {
DWORD out_bytes;
- if ( !ReadFile( file, data, file_size, &out_bytes, NULL ) ||
- out_bytes != file_size )
+ if ( !ReadFile( file, data, sz, &out_bytes, NULL ) ||
+ out_bytes != sz )
{
- fprintf(stderr, "load_file: could not read %ld bytes from '%s'\n", file_size, fn);
+ fprintf(stderr, "load_file: could not read %ld bytes from '%s'\n", sz, fn);
free(data);
data = NULL;
- file_size = 0;
+ sz = 0;
}
}
}
CloseHandle( file );
- *_sz = (unsigned) file_size;
+ *_sz = (unsigned) sz;
return data;
}