diff options
| author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 18:29:04 -0800 |
|---|---|---|
| committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 18:29:04 -0800 |
| commit | e54eebbf1a908d65ee8cf80bab62821c05666d70 (patch) | |
| tree | 4b825dc642cb6eb9a060e54bf8d69288fbee4904 /fastboot | |
| parent | a1e1c1b106423de09bc918502e7a51d4ffe5a4ae (diff) | |
| download | system_core-e54eebbf1a908d65ee8cf80bab62821c05666d70.tar.gz system_core-e54eebbf1a908d65ee8cf80bab62821c05666d70.tar.bz2 system_core-e54eebbf1a908d65ee8cf80bab62821c05666d70.zip | |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'fastboot')
| -rw-r--r-- | fastboot/Android.mk | 57 | ||||
| -rw-r--r-- | fastboot/bootimg.c | 85 | ||||
| -rw-r--r-- | fastboot/engine.c | 289 | ||||
| -rw-r--r-- | fastboot/engineering_key.p12 | bin | 2610 -> 0 bytes | |||
| -rw-r--r-- | fastboot/fastboot.c | 673 | ||||
| -rw-r--r-- | fastboot/fastboot.h | 57 | ||||
| -rwxr-xr-x | fastboot/genkey.sh | 25 | ||||
| -rwxr-xr-x | fastboot/p12topem.sh | 9 | ||||
| -rw-r--r-- | fastboot/protocol.c | 181 | ||||
| -rwxr-xr-x | fastboot/signfile.sh | 10 | ||||
| -rw-r--r-- | fastboot/usb.h | 64 | ||||
| -rw-r--r-- | fastboot/usb_linux.c | 373 | ||||
| -rw-r--r-- | fastboot/usb_osx.c | 538 | ||||
| -rw-r--r-- | fastboot/usb_windows.c | 375 | ||||
| -rw-r--r-- | fastboot/usbtest.c | 212 | ||||
| -rw-r--r-- | fastboot/util_linux.c | 52 | ||||
| -rw-r--r-- | fastboot/util_osx.c | 47 | ||||
| -rw-r--r-- | fastboot/util_windows.c | 93 |
18 files changed, 0 insertions, 3140 deletions
diff --git a/fastboot/Android.mk b/fastboot/Android.mk deleted file mode 100644 index 7a9d35f2..00000000 --- a/fastboot/Android.mk +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright (C) 2007 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -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_MODULE := fastboot - -ifeq ($(HOST_OS),linux) - LOCAL_SRC_FILES += usb_linux.c util_linux.c -endif - -ifeq ($(HOST_OS),darwin) - LOCAL_SRC_FILES += usb_osx.c util_osx.c - LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit \ - -framework Carbon -endif - -ifeq ($(HOST_OS),windows) - LOCAL_SRC_FILES += usb_windows.c util_windows.c - EXTRA_STATIC_LIBS := AdbWinApi - LOCAL_C_INCLUDES += /usr/include/w32api/ddk development/host/windows/usb/api - ifeq ($(strip $(USE_CYGWIN)),) - LOCAL_LDLIBS += -lws2_32 - USE_SYSDEPS_WIN32 := 1 - endif -endif - -LOCAL_STATIC_LIBRARIES := $(EXTRA_STATIC_LIBS) libzipfile libunz - -include $(BUILD_HOST_EXECUTABLE) -$(call dist-for-goals,droid,$(LOCAL_BUILT_MODULE)) - -ifeq ($(HOST_OS),linux) -include $(CLEAR_VARS) -LOCAL_SRC_FILES := usbtest.c usb_linux.c -LOCAL_MODULE := usbtest -include $(BUILD_HOST_EXECUTABLE) -endif - -ifeq ($(HOST_OS),windows) -$(LOCAL_INSTALLED_MODULE): $(HOST_OUT_EXECUTABLES)/AdbWinApi.dll -endif diff --git a/fastboot/bootimg.c b/fastboot/bootimg.c deleted file mode 100644 index 1d77b3c4..00000000 --- a/fastboot/bootimg.c +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <bootimg.h> - -void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline) -{ - strcpy((char*) h->cmdline, cmdline); -} - -boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, - void *ramdisk, unsigned ramdisk_size, - void *second, unsigned second_size, - unsigned page_size, - unsigned *bootimg_size) -{ - unsigned kernel_actual; - unsigned ramdisk_actual; - 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->kernel_addr = 0x10008000; - hdr->ramdisk_size = ramdisk_size; - hdr->ramdisk_addr = 0x11000000; - hdr->second_size = second_size; - hdr->second_addr = 0x10F00000; - - hdr->tags_addr = 0x10000100; - hdr->page_size = page_size; - - memcpy(hdr->magic + page_size, - kernel, kernel_size); - memcpy(hdr->magic + page_size + kernel_actual, - ramdisk, ramdisk_size); - memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, - second, second_size); - return hdr; -} diff --git a/fastboot/engine.c b/fastboot/engine.c deleted file mode 100644 index 4c7e197f..00000000 --- a/fastboot/engine.c +++ /dev/null @@ -1,289 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <stdlib.h> -#include <stdarg.h> -#include <string.h> - -#include "fastboot.h" - -char *mkmsg(const char *fmt, ...) -{ - char buf[256]; - char *s; - va_list ap; - - va_start(ap, fmt); - vsprintf(buf, fmt, ap); - va_end(ap); - - s = strdup(buf); - if (s == 0) die("out of memory"); - return s; -} - -#define OP_DOWNLOAD 1 -#define OP_COMMAND 2 -#define OP_QUERY 3 -#define OP_NOTICE 4 - -typedef struct Action Action; - -struct Action -{ - unsigned op; - Action *next; - - char cmd[64]; - void *data; - unsigned size; - - const char *msg; - int (*func)(Action *a, int status, char *resp); -}; - -static Action *action_list = 0; -static Action *action_last = 0; - -static int cb_default(Action *a, int status, char *resp) -{ - if (status) { - fprintf(stderr,"FAILED (%s)\n", resp); - } else { - fprintf(stderr,"OKAY\n"); - } - return status; -} - -static Action *queue_action(unsigned op, const char *fmt, ...) -{ - Action *a; - va_list ap; - - a = calloc(1, sizeof(Action)); - if (a == 0) die("out of memory"); - - va_start(ap, fmt); - vsprintf(a->cmd, fmt, ap); - va_end(ap); - - if (action_last) { - action_last->next = a; - } else { - action_list = a; - } - action_last = a; - a->op = op; - a->func = cb_default; - return a; -} - -void fb_queue_erase(const char *ptn) -{ - Action *a; - a = queue_action(OP_COMMAND, "erase:%s", ptn); - a->msg = mkmsg("erasing '%s'", ptn); -} - -void fb_queue_flash(const char *ptn, void *data, unsigned sz) -{ - Action *a; - - a = queue_action(OP_DOWNLOAD, ""); - a->data = data; - a->size = sz; - a->msg = mkmsg("sending '%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; - unsigned n; - int len; - - for (n = 0; n < count; n++) { - const char *val = value[n]; - int len = strlen(val); - int match; - - if ((len > 1) && (val[len-1] == '*')) { - len--; - match = !strncmp(val, str, len); - } else { - match = !strcmp(val, str); - } - - if (match) return 1; - } - - return 0; -} - - - -static int cb_check(Action *a, int status, char *resp, int invert) -{ - const char **value = a->data; - unsigned count = a->size; - unsigned n; - int yes; - - if (status) { - fprintf(stderr,"FAILED (%s)\n", resp); - return status; - } - - yes = match(resp, value, count); - if (invert) yes = !yes; - - if (yes) { - fprintf(stderr,"OKAY\n"); - return 0; - } - - fprintf(stderr,"FAILED\n\n"); - fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp); - fprintf(stderr,"Update %s '%s'", - invert ? "rejects" : "requires", value[0]); - for (n = 1; n < count; n++) { - fprintf(stderr," or '%s'", value[n]); - } - fprintf(stderr,".\n\n"); - return -1; -} - -static int cb_require(Action *a, int status, char *resp) -{ - return cb_check(a, status, resp, 0); -} - -static int cb_reject(Action *a, int status, char *resp) -{ - return cb_check(a, status, resp, 1); -} - -void fb_queue_require(const char *var, int invert, unsigned nvalues, const char **value) -{ - Action *a; - a = queue_action(OP_QUERY, "getvar:%s", var); - a->data = value; - a->size = nvalues; - a->msg = mkmsg("checking %s", var); - a->func = invert ? cb_reject : cb_require; - if (a->data == 0) die("out of memory"); -} - -static int cb_display(Action *a, int status, char *resp) -{ - if (status) { - fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp); - return status; - } - fprintf(stderr, "%s: %s\n", (char*) a->data, resp); - return 0; -} - -void fb_queue_display(const char *var, const char *prettyname) -{ - Action *a; - a = queue_action(OP_QUERY, "getvar:%s", var); - a->data = strdup(prettyname); - if (a->data == 0) die("out of memory"); - a->func = cb_display; -} - -static int cb_do_nothing(Action *a, int status, char *resp) -{ - fprintf(stderr,"\n"); - return 0; -} - -void fb_queue_reboot(void) -{ - Action *a = queue_action(OP_COMMAND, "reboot"); - a->func = cb_do_nothing; - a->msg = "rebooting"; -} - -void fb_queue_command(const char *cmd, const char *msg) -{ - Action *a = queue_action(OP_COMMAND, cmd); - a->msg = msg; -} - -void fb_queue_download(const char *name, void *data, unsigned size) -{ - Action *a = queue_action(OP_DOWNLOAD, ""); - a->data = data; - a->size = size; - a->msg = mkmsg("downloading '%s'", name); -} - -void fb_queue_notice(const char *notice) -{ - Action *a = queue_action(OP_NOTICE, ""); - a->data = (void*) notice; -} - -void fb_execute_queue(usb_handle *usb) -{ - Action *a; - char resp[FB_RESPONSE_SZ+1]; - int status; - - a = action_list; - resp[FB_RESPONSE_SZ] = 0; - - for (a = action_list; a; a = a->next) { - if (a->msg) { - fprintf(stderr,"%s... ",a->msg); - } - if (a->op == OP_DOWNLOAD) { - status = fb_download_data(usb, a->data, a->size); - status = a->func(a, status, status ? fb_get_error() : ""); - if (status) break; - } else if (a->op == OP_COMMAND) { - status = fb_command(usb, a->cmd); - status = a->func(a, status, status ? fb_get_error() : ""); - if (status) break; - } else if (a->op == OP_QUERY) { - status = fb_command_response(usb, a->cmd, resp); - status = a->func(a, status, status ? fb_get_error() : resp); - if (status) break; - } else if (a->op == OP_NOTICE) { - fprintf(stderr,"%s\n",(char*)a->data); - } else { - die("bogus action"); - } - } -} - diff --git a/fastboot/engineering_key.p12 b/fastboot/engineering_key.p12 Binary files differdeleted file mode 100644 index d8183b05..00000000 --- a/fastboot/engineering_key.p12 +++ /dev/null diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c deleted file mode 100644 index e220dbea..00000000 --- a/fastboot/fastboot.c +++ /dev/null @@ -1,673 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <stdlib.h> -#include <stdarg.h> -#include <string.h> -#include <errno.h> -#include <fcntl.h> -#include <unistd.h> -#include <limits.h> -#include <ctype.h> - -#include <sys/time.h> -#include <bootimg.h> -#include <zipfile/zipfile.h> - -#include "fastboot.h" - -static usb_handle *usb = 0; -static const char *serial = 0; -static const char *product = 0; -static const char *cmdline = 0; -static int wipe_data = 0; -static unsigned short vendor_id = 0; - -void die(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - fprintf(stderr,"error: "); - vfprintf(stderr, fmt, ap); - fprintf(stderr,"\n"); - va_end(ap); - exit(1); -} - -void get_my_path(char *path); - -char *find_item(const char *item, const char *product) -{ - char *dir; - char *fn; - char path[PATH_MAX + 128]; - - if(!strcmp(item,"boot")) { - fn = "boot.img"; - } else if(!strcmp(item,"recovery")) { - fn = "recovery.img"; - } else if(!strcmp(item,"system")) { - fn = "system.img"; - } else if(!strcmp(item,"userdata")) { - fn = "userdata.img"; - } else if(!strcmp(item,"info")) { - fn = "android-info.txt"; - } else { - fprintf(stderr,"unknown partition '%s'\n", item); - return 0; - } - - if(product) { - get_my_path(path); - sprintf(path + strlen(path), - "../../../target/product/%s/%s", product, fn); - return strdup(path); - } - - dir = getenv("ANDROID_PRODUCT_OUT"); - if((dir == 0) || (dir[0] == 0)) { - die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); - return 0; - } - - sprintf(path, "%s/%s", dir, fn); - return strdup(path); -} - -#ifdef _WIN32 -void *load_file(const char *fn, unsigned *_sz); -#else -void *load_file(const char *fn, unsigned *_sz) -{ - char *data; - int sz; - int fd; - - data = 0; - fd = open(fn, O_RDONLY); - if(fd < 0) return 0; - - sz = lseek(fd, 0, SEEK_END); - if(sz < 0) goto oops; - - if(lseek(fd, 0, SEEK_SET) != 0) goto oops; - - data = (char*) malloc(sz); - if(data == 0) goto oops; - - if(read(fd, data, sz) != sz) goto oops; - close(fd); - - if(_sz) *_sz = sz; - return data; - -oops: - close(fd); - if(data != 0) free(data); - return 0; -} -#endif - -int match_fastboot(usb_ifc_info *info) -{ - if(!(vendor_id && (info->dev_vendor == vendor_id)) && - (info->dev_vendor != 0x18d1) && - (info->dev_vendor != 0x0bb4)) 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 - // at the command line with the -s option. - if (serial && strcmp(serial, info->serial_number) != 0) return -1; - return 0; -} - -int list_devices_callback(usb_ifc_info *info) -{ - if (match_fastboot(info) == 0) { - char* serial = info->serial_number; - if (!serial[0]) { - serial = "????????????"; - } - // output compatible with "adb devices" - printf("%s\tfastboot\n", serial); - } - - return -1; -} - -usb_handle *open_device(void) -{ - static usb_handle *usb = 0; - int announce = 1; - - if(usb) return usb; - - for(;;) { - usb = usb_open(match_fastboot); - if(usb) return usb; - if(announce) { - announce = 0; - fprintf(stderr,"< waiting for device >\n"); - } - sleep(1); - } -} - -void list_devices(void) { - // We don't actually open a USB device here, - // just getting our callback called so we can - // list all the connected devices. - usb_open(list_devices_callback); -} - -void usage(void) -{ - fprintf(stderr, -/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ - "usage: fastboot [ <option> ] <command>\n" - "\n" - "commands:\n" - " update <filename> reflash device from update.zip\n" - " flashall flash boot + recovery + system\n" - " flash <partition> [ <filename> ] write a file to a flash partition\n" - " erase <partition> erase 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" - " devices list all connected devices\n" - " reboot reboot device normally\n" - " reboot-bootloader reboot device into bootloader\n" - "\n" - "options:\n" - " -w erase userdata and cache\n" - " -s <serial number> specify device serial number\n" - " -p <product> specify product name\n" - " -c <cmdline> override kernel commandline\n" - " -i <vendor id> specify a custom USB vendor id\n" - ); - exit(1); -} - -void *load_bootable_image(const char *kernel, const char *ramdisk, - unsigned *sz, const char *cmdline) -{ - void *kdata = 0, *rdata = 0; - unsigned ksize = 0, rsize = 0; - void *bdata; - unsigned bsize; - - if(kernel == 0) { - fprintf(stderr, "no image specified\n"); - return 0; - } - - kdata = load_file(kernel, &ksize); - if(kdata == 0) { - fprintf(stderr, "cannot load '%s'\n", kernel); - return 0; - } - - /* is this actually a boot image? */ - if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { - if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); - - if(ramdisk) { - fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); - return 0; - } - - *sz = ksize; - return kdata; - } - - if(ramdisk) { - rdata = load_file(ramdisk, &rsize); - if(rdata == 0) { - fprintf(stderr,"cannot load '%s'\n", ramdisk); - return 0; - } - } - - fprintf(stderr,"creating boot image...\n"); - bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, 2048, &bsize); - if(bdata == 0) { - fprintf(stderr,"failed to create boot.img\n"); - return 0; - } - if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); - fprintf(stderr,"creating boot image - %d bytes\n", bsize); - *sz = bsize; - - return bdata; -} - -void *unzip_file(zipfile_t zip, const char *name, unsigned *sz) -{ - void *data; - zipentry_t entry; - unsigned datasz; - - entry = lookup_zipentry(zip, name); - if (entry == NULL) { - fprintf(stderr, "archive does not contain '%s'\n", name); - return 0; - } - - *sz = get_zipentry_size(entry); - - datasz = *sz * 1.001; - data = malloc(datasz); - - if(data == 0) { - fprintf(stderr, "failed to allocate %d bytes\n", *sz); - return 0; - } - - if (decompress_zipentry(entry, data, datasz)) { - fprintf(stderr, "failed to unzip '%s' from archive\n", name); - free(data); - return 0; - } - - return data; -} - -static char *strip(char *s) -{ - int n; - while(*s && isspace(*s)) s++; - n = strlen(s); - while(n-- > 0) { - if(!isspace(s[n])) break; - s[n] = 0; - } - return s; -} - -#define MAX_OPTIONS 32 -static int setup_requirement_line(char *name) -{ - char *val[MAX_OPTIONS]; - const char **out; - unsigned n, count; - char *x; - int invert = 0; - - if (!strncmp(name, "reject ", 7)) { - name += 7; - invert = 1; - } else if (!strncmp(name, "require ", 8)) { - name += 8; - invert = 0; - } - - x = strchr(name, '='); - if (x == 0) return 0; - *x = 0; - val[0] = x + 1; - - for(count = 1; count < MAX_OPTIONS; count++) { - x = strchr(val[count - 1],'|'); - if (x == 0) break; - *x = 0; - val[count] = x + 1; - } - - name = strip(name); - for(n = 0; n < count; n++) val[n] = strip(val[n]); - - name = strip(name); - if (name == 0) return -1; - - /* work around an unfortunate name mismatch */ - if (!strcmp(name,"board")) name = "product"; - - out = malloc(sizeof(char*) * count); - if (out == 0) return -1; - - for(n = 0; n < count; n++) { - out[n] = strdup(strip(val[n])); - if (out[n] == 0) return -1; - } - - fb_queue_require(name, invert, n, out); - return 0; -} - -static void setup_requirements(char *data, unsigned sz) -{ - char *s; - - s = data; - while (sz-- > 0) { - if(*s == '\n') { - *s++ = 0; - if (setup_requirement_line(data)) { - die("out of memory"); - } - data = s; - } else { - s++; - } - } -} - -void queue_info_dump(void) -{ - fb_queue_notice("--------------------------------------------"); - fb_queue_display("version-bootloader", "Bootloader Version..."); - fb_queue_display("version-baseband", "Baseband Version....."); - fb_queue_display("serialno", "Serial Number........"); - fb_queue_notice("--------------------------------------------"); -} - -void do_update_signature(zipfile_t zip, char *fn) -{ - void *data; - unsigned sz; - data = unzip_file(zip, fn, &sz); - if (data == 0) return; - fb_queue_download("signature", data, sz); - fb_queue_command("signature", "installing signature"); -} - -void do_update(char *fn) -{ - void *zdata; - unsigned zsize; - void *data; - unsigned sz; - zipfile_t zip; - - queue_info_dump(); - - zdata = load_file(fn, &zsize); - if (zdata == 0) die("failed to load '%s'", fn); - - zip = init_zipfile(zdata, zsize); - if(zip == 0) die("failed to access zipdata in '%s'"); - - data = unzip_file(zip, "android-info.txt", &sz); - if (data == 0) { - char *tmp; - /* fallback for older zipfiles */ - data = unzip_file(zip, "android-product.txt", &sz); - if ((data == 0) || (sz < 1)) { - die("update package has no android-info.txt or android-product.txt"); - } - tmp = malloc(sz + 128); - if (tmp == 0) die("out of memory"); - sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data); - data = tmp; - sz = strlen(tmp); - } - - setup_requirements(data, sz); - - data = unzip_file(zip, "boot.img", &sz); - if (data == 0) die("update package missing boot.img"); - do_update_signature(zip, "boot.sig"); - fb_queue_flash("boot", data, sz); - - data = unzip_file(zip, "recovery.img", &sz); - if (data != 0) { - do_update_signature(zip, "recovery.sig"); - 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"); - fb_queue_flash("system", data, sz); -} - -void do_send_signature(char *fn) -{ - void *data; - unsigned sz; - char *xtn; - - xtn = strrchr(fn, '.'); - if (!xtn) return; - if (strcmp(xtn, ".img")) return; - - strcpy(xtn,".sig"); - data = load_file(fn, &sz); - strcpy(xtn,".img"); - if (data == 0) return; - fb_queue_download("signature", data, sz); - fb_queue_command("signature", "installing signature"); -} - -void do_flashall(void) -{ - char *fname; - void *data; - unsigned sz; - - queue_info_dump(); - - 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"); - setup_requirements(data, sz); - - fname = find_item("boot", product); - data = load_file(fname, &sz); - if (data == 0) die("could not load boot.img"); - do_send_signature(fname); - fb_queue_flash("boot", data, sz); - - fname = find_item("recovery", product); - data = load_file(fname, &sz); - if (data != 0) { - do_send_signature(fname); - 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"); - do_send_signature(fname); - fb_queue_flash("system", data, sz); -} - -#define skip(n) do { argc -= (n); argv += (n); } while (0) -#define require(n) do { if (argc < (n)) usage(); } while (0) - -int do_oem_command(int argc, char **argv) -{ - int i; - char command[256]; - if (argc <= 1) return 0; - - command[0] = 0; - while(1) { - strcat(command,*argv); - skip(1); - if(argc == 0) break; - strcat(command," "); - } - - fb_queue_command(command,""); - return 0; -} - -int main(int argc, char **argv) -{ - int wants_wipe = 0; - int wants_reboot = 0; - int wants_reboot_bootloader = 0; - void *data; - unsigned sz; - - skip(1); - if (argc == 0) { - usage(); - return 0; - } - - if (!strcmp(*argv, "devices")) { - list_devices(); - return 0; - } - - while (argc > 0) { - if(!strcmp(*argv, "-w")) { - wants_wipe = 1; - skip(1); - } 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")) { - require(2); - fb_queue_display(argv[1], argv[1]); - skip(2); - } else if(!strcmp(*argv, "erase")) { - require(2); - fb_queue_erase(argv[1]); - 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 (sz != 256) die("signature must be 256 bytes"); - fb_queue_download("signature", data, sz); - fb_queue_command("signature", "installing signature"); - skip(2); - } else if(!strcmp(*argv, "reboot")) { - wants_reboot = 1; - skip(1); - } else if(!strcmp(*argv, "reboot-bootloader")) { - wants_reboot_bootloader = 1; - skip(1); - } else if (!strcmp(*argv, "continue")) { - fb_queue_command("continue", "resuming boot"); - skip(1); - } else if(!strcmp(*argv, "boot")) { - char *kname = 0; - char *rname = 0; - skip(1); - if (argc > 0) { - kname = argv[0]; - skip(1); - } - if (argc > 0) { - rname = argv[0]; - skip(1); - } - data = load_bootable_image(kname, rname, &sz, cmdline); - if (data == 0) return 1; - fb_queue_download("boot.img", data, sz); - fb_queue_command("boot", "booting"); - } else if(!strcmp(*argv, "flash")) { - char *pname = argv[1]; - char *fname = 0; - require(2); - if (argc > 2) { - fname = argv[2]; - skip(3); - } else { - fname = find_item(pname, product); - 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); - } else if(!strcmp(*argv, "flash:raw")) { - char *pname = argv[1]; - char *kname = argv[2]; - char *rname = 0; - require(3); - if(argc > 3) { - rname = argv[3]; - skip(4); - } else { - skip(3); - } - data = load_bootable_image(kname, rname, &sz, cmdline); - if (data == 0) die("cannot load bootable image"); - fb_queue_flash(pname, data, sz); - } else if(!strcmp(*argv, "flashall")) { - skip(1); - do_flashall(); - wants_reboot = 1; - } else if(!strcmp(*argv, "update")) { - if (argc > 1) { - do_update(argv[1]); - skip(2); - } else { - do_update("update.zip"); - skip(1); - } - wants_reboot = 1; - } else if(!strcmp(*argv, "oem")) { - argc = do_oem_command(argc, argv); - } else { - usage(); - } - } - - if (wants_wipe) { - fb_queue_erase("userdata"); - fb_queue_erase("cache"); - } - if (wants_reboot) { - fb_queue_reboot(); - } else if (wants_reboot_bootloader) { - fb_queue_command("reboot-bootloader", "rebooting into bootloader"); - } - - usb = open_device(); - - fb_execute_queue(usb); - return 0; -} diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h deleted file mode 100644 index a36c569e..00000000 --- a/fastboot/fastboot.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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. - */ - -#ifndef _FASTBOOT_H_ -#define _FASTBOOT_H_ - -#include "usb.h" - -/* 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); -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);; -void fb_queue_erase(const char *ptn); -void fb_queue_require(const char *var, int invert, unsigned nvalues, const char **value); -void fb_queue_display(const char *var, const char *prettyname); -void fb_queue_reboot(void); -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); -void fb_execute_queue(usb_handle *usb); - -/* util stuff */ -void die(const char *fmt, ...); - -#endif diff --git a/fastboot/genkey.sh b/fastboot/genkey.sh deleted file mode 100755 index 011e902e..00000000 --- a/fastboot/genkey.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash - -if [ $# -ne 2 ] -then - echo "Usage: $0 alias \"pass phrase\"" - exit -1 -fi - -# Generate a 2048 bit RSA key with public exponent 3. -# Encrypt private key with provided password. -openssl genrsa -3 -out $1.pem -passout pass:"$2" 2048 - -# Create a self-signed cert for this key. -openssl req -new -x509 -key $1.pem -passin pass:"$2" \ - -out $1-cert.pem \ - -batch -days 10000 - -# Create a PKCS12 store containing the generated private key. -# Protect the keystore and the private key with the provided password. -openssl pkcs12 -export -in $1-cert.pem -inkey $1.pem -passin pass:"$2" \ - -out $1.p12 -name $1 -passout pass:"$2" - -rm $1.pem -rm $1-cert.pem - diff --git a/fastboot/p12topem.sh b/fastboot/p12topem.sh deleted file mode 100755 index f081eb53..00000000 --- a/fastboot/p12topem.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -if [ $# -ne 2 ] -then - echo "Usage: $0 alias passphrase" - exit -1 -fi - -openssl pkcs12 -passin pass:"$2" -passout pass:"$2" -in $1.p12 -out $1.pem diff --git a/fastboot/protocol.c b/fastboot/protocol.c deleted file mode 100644 index c788a12f..00000000 --- a/fastboot/protocol.c +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> - -#include "fastboot.h" - -static char ERROR[128]; - -char *fb_get_error(void) -{ - return ERROR; -} - -static int check_response(usb_handle *usb, unsigned size, - unsigned data_okay, char *response) -{ - unsigned char status[65]; - int r; - - for(;;) { - r = usb_read(usb, status, 64); - if(r < 0) { - sprintf(ERROR, "status read failed (%s)", strerror(errno)); - usb_close(usb); - return -1; - } - status[r] = 0; - - if(r < 4) { - sprintf(ERROR, "status malformed (%d bytes)", r); - usb_close(usb); - return -1; - } - - if(!memcmp(status, "INFO", 4)) { - fprintf(stderr,"%s\n", status); - continue; - } - - if(!memcmp(status, "OKAY", 4)) { - if(response) { - strcpy(response, (char*) status + 4); - } - return 0; - } - - if(!memcmp(status, "FAIL", 4)) { - if(r > 4) { - sprintf(ERROR, "remote: %s", status + 4); - } else { - strcpy(ERROR, "remote failure"); - } - return -1; - } - - if(!memcmp(status, "DATA", 4) && data_okay){ - unsigned dsize = strtoul((char*) status + 4, 0, 16); - if(dsize > size) { - strcpy(ERROR, "data size too large"); - usb_close(usb); - return -1; - } - return dsize; - } - - strcpy(ERROR,"unknown status code"); - usb_close(usb); - break; - } - - return -1; -} - -static int _command_send(usb_handle *usb, const char *cmd, - const void *data, unsigned size, - char *response) -{ - int cmdsize = strlen(cmd); - int r; - - if(response) { - response[0] = 0; - } - - if(cmdsize > 64) { - sprintf(ERROR,"command too large"); - return -1; - } - - if(usb_write(usb, cmd, cmdsize) != cmdsize) { - sprintf(ERROR,"command write failed (%s)", strerror(errno)); - usb_close(usb); - return -1; - } - - if(data == 0) { - return check_response(usb, size, 0, response); - } - - r = check_response(usb, size, 1, 0); - if(r < 0) { - return -1; - } - size = r; - - 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; - } - } - - r = check_response(usb, 0, 0, 0); - if(r < 0) { - return -1; - } else { - return size; - } -} - -int fb_command(usb_handle *usb, const char *cmd) -{ - return _command_send(usb, cmd, 0, 0, 0); -} - -int fb_command_response(usb_handle *usb, const char *cmd, char *response) -{ - return _command_send(usb, cmd, 0, 0, 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 { - return 0; - } -} - diff --git a/fastboot/signfile.sh b/fastboot/signfile.sh deleted file mode 100755 index 3188d2d9..00000000 --- a/fastboot/signfile.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -if [ $# -ne 3 ] -then - echo "Usage: $0 alias filename passpharse" - exit -1 -fi - -openssl dgst -passin pass:"$3" -binary -sha1 -sign $1.pem $2 > $2.sign - diff --git a/fastboot/usb.h b/fastboot/usb.h deleted file mode 100644 index f3ec5bfd..00000000 --- a/fastboot/usb.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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. - */ - -#ifndef _USB_H_ -#define _USB_H_ - -typedef struct usb_handle usb_handle; - -typedef struct usb_ifc_info usb_ifc_info; - -struct usb_ifc_info -{ - /* from device descriptor */ - unsigned short dev_vendor; - unsigned short dev_product; - - 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; - - char serial_number[256]; -}; - -typedef int (*ifc_match_func)(usb_ifc_info *ifc); - -usb_handle *usb_open(ifc_match_func callback); -int usb_close(usb_handle *h); -int usb_read(usb_handle *h, void *_data, int len); -int usb_write(usb_handle *h, const void *_data, int len); - - -#endif diff --git a/fastboot/usb_linux.c b/fastboot/usb_linux.c deleted file mode 100644 index 06c62b84..00000000 --- a/fastboot/usb_linux.c +++ /dev/null @@ -1,373 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> - -#include <sys/ioctl.h> -#include <sys/types.h> -#include <dirent.h> -#include <fcntl.h> -#include <errno.h> -#include <pthread.h> -#include <ctype.h> - -#include <linux/usbdevice_fs.h> -#include <linux/usbdevice_fs.h> -#include <linux/version.h> -#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20) -#include <linux/usb/ch9.h> -#else -#include <linux/usb_ch9.h> -#endif -#include <asm/byteorder.h> - -#include "usb.h" - -#if TRACE_USB -#define DBG1(x...) fprintf(stderr, x) -#define DBG(x...) fprintf(stderr, x) -#else -#define DBG(x...) -#define DBG1(x...) -#endif - -struct usb_handle -{ - char fname[64]; - int desc; - unsigned char ep_in; - unsigned char ep_out; -}; - -static inline int badname(const char *name) -{ - while(*name) { - if(!isdigit(*name++)) return 1; - } - return 0; -} - -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; -} - -static int filter_usb_device(int fd, char *ptr, int len, ifc_match_func callback, - int *ept_in_id, int *ept_out_id, int *ifc_id) -{ - struct usb_device_descriptor *dev; - struct usb_config_descriptor *cfg; - struct usb_interface_descriptor *ifc; - struct usb_endpoint_descriptor *ept; - struct usb_ifc_info info; - - int in, out; - unsigned i; - unsigned e; - - 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; - - // 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; - - 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; - ctrl.wLength = sizeof(buffer); - ctrl.data = buffer; - - result = ioctl(fd, USBDEVFS_CONTROL, &ctrl); - if (result > 0) { - int i; - // skip first word, and copy the rest to the serial string, changing shorts to bytes. - result /= 2; - for (i = 1; i < result; i++) - info.serial_number[i - 1] = buffer[i]; - info.serial_number[i - 1] = 0; - } - } - - 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 { - out = ept->bEndpointAddress; - } - } - - info.has_bulk_in = (in != -1); - info.has_bulk_out = (out != -1); - - if(callback(&info) == 0) { - *ept_in_id = in; - *ept_out_id = out; - *ifc_id = ifc->bInterfaceNumber; - return 0; - } - } - - return -1; -} - -static usb_handle *find_usb_device(const char *base, ifc_match_func callback) -{ - usb_handle *usb = 0; - char busname[64], devname[64]; - char desc[1024]; - int n, in, out, ifc; - - DIR *busdir, *devdir; - struct dirent *de; - int fd; - - 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); - -// DBG("[ scanning %s ]\n", devname); - if((fd = open(devname, O_RDWR)) < 0) { - continue; - } - - n = read(fd, desc, sizeof(desc)); - - if(filter_usb_device(fd, desc, n, callback, &in, &out, &ifc) == 0){ - usb = calloc(1, sizeof(usb_handle)); - strcpy(usb->fname, devname); - usb->ep_in = in; - usb->ep_out = out; - usb->desc = fd; - - n = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &ifc); - if(n != 0) { - close(fd); - free(usb); - usb = 0; - continue; - } - } else { - close(fd); - } - } - closedir(devdir); - } - closedir(busdir); - - return usb; -} - -int usb_write(usb_handle *h, const void *_data, int len) -{ - unsigned char *data = (unsigned char*) _data; - unsigned count = 0; - struct usbdevfs_bulktransfer bulk; - int n; - - 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", - n, errno, strerror(errno)); - return -1; - } - return 0; - } - - while(len > 0) { - int xfer; - xfer = (len > 4096) ? 4096 : 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", - n, errno, strerror(errno)); - return -1; - } - - count += xfer; - len -= xfer; - data += xfer; - } - - return count; -} - -int usb_read(usb_handle *h, void *_data, int len) -{ - unsigned char *data = (unsigned char*) _data; - unsigned count = 0; - struct usbdevfs_bulktransfer bulk; - int n; - - if(h->ep_in == 0) { - return -1; - } - - while(len > 0) { - int xfer = (len > 4096) ? 4096 : len; - - bulk.ep = h->ep_in; - bulk.len = xfer; - bulk.data = data; - bulk.timeout = 0; - - DBG("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname); - n = ioctl(h->desc, USBDEVFS_BULK, &bulk); - DBG("[ usb read %d ] = %d, fname=%s\n", xfer, n, h->fname); - - if(n < 0) { - DBG1("ERROR: n = %d, errno = %d (%s)\n", - n, errno, strerror(errno)); - return -1; - } - - count += n; - len -= n; - data += n; - - if(n < xfer) { - break; - } - } - - return count; -} - -void usb_kick(usb_handle *h) -{ - int fd; - - fd = h->desc; - h->desc = -1; - if(fd >= 0) { - close(fd); - DBG("[ usb closed %d ]\n", fd); - } -} - -int usb_close(usb_handle *h) -{ - int fd; - - fd = h->desc; - h->desc = -1; - if(fd >= 0) { - close(fd); - DBG("[ usb closed %d ]\n", fd); - } - - return 0; -} - -usb_handle *usb_open(ifc_match_func callback) -{ - return find_usb_device("/dev/bus/usb", callback); -} - - diff --git a/fastboot/usb_osx.c b/fastboot/usb_osx.c deleted file mode 100644 index d6a82609..00000000 --- a/fastboot/usb_osx.c +++ /dev/null @@ -1,538 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <CoreFoundation/CoreFoundation.h> -#include <IOKit/IOKitLib.h> -#include <IOKit/IOCFPlugIn.h> -#include <IOKit/usb/IOUSBLib.h> -#include <IOKit/IOMessage.h> -#include <mach/mach_port.h> - -#include "usb.h" - - -/* - * Internal helper functions and associated definitions. - */ - -#if TRACE_USB -#define WARN(x...) fprintf(stderr, x) -#else -#define WARN(x...) -#endif - -#define ERR(x...) fprintf(stderr, "ERROR: " x) - -/** An open usb device */ -struct usb_handle -{ - int success; - ifc_match_func callback; - usb_ifc_info info; - - UInt8 bulkIn; - UInt8 bulkOut; - IOUSBInterfaceInterface190 **interface; - unsigned int zero_mask; -}; - -/** Try out all the interfaces and see if there's a match. Returns 0 on - * success, -1 on failure. */ -static int try_interfaces(IOUSBDeviceInterface **dev, usb_handle *handle) { - IOReturn kr; - IOUSBFindInterfaceRequest request; - io_iterator_t iterator; - io_service_t usbInterface; - IOCFPlugInInterface **plugInInterface; - IOUSBInterfaceInterface190 **interface = NULL; - HRESULT result; - SInt32 score; - UInt8 interfaceNumEndpoints; - UInt8 endpoint; - UInt8 configuration; - - // Placing the constant KIOUSBFindInterfaceDontCare into the following - // fields of the IOUSBFindInterfaceRequest structure will allow us to - // find all of the interfaces - request.bInterfaceClass = kIOUSBFindInterfaceDontCare; - request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare; - request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare; - request.bAlternateSetting = kIOUSBFindInterfaceDontCare; - - // SetConfiguration will kill an existing UMS connection, so let's - // not do this if not necessary. - configuration = 0; - (*dev)->GetConfiguration(dev, &configuration); - if (configuration != 1) - (*dev)->SetConfiguration(dev, 1); - - // Get an iterator for the interfaces on the device - kr = (*dev)->CreateInterfaceIterator(dev, &request, &iterator); - - if (kr != 0) { - ERR("Couldn't create a device interface iterator: (%08x)\n", kr); - return -1; - } - - while ((usbInterface = IOIteratorNext(iterator))) { - // Create an intermediate plugin - kr = IOCreatePlugInInterfaceForService( - usbInterface, - kIOUSBInterfaceUserClientTypeID, - kIOCFPlugInInterfaceID, - &plugInInterface, - &score); - - // No longer need the usbInterface object now that we have the plugin - (void) IOObjectRelease(usbInterface); - - if ((kr != 0) || (!plugInInterface)) { - WARN("Unable to create plugin (%08x)\n", kr); - continue; - } - - // Now create the interface interface for the interface - result = (*plugInInterface)->QueryInterface( - plugInInterface, - CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), - (LPVOID) &interface); - - // No longer need the intermediate plugin - (*plugInInterface)->Release(plugInInterface); - - if (result || !interface) { - ERR("Couldn't create interface interface: (%08x)\n", - (unsigned int) result); - // 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 - * to be instantiated. - */ - - /* - * TODO: Earlier comments here indicated that it was a bad - * idea to just open any interface, because opening "mass - * storage endpoints" is bad. However, the only way to find - * out if an interface does bulk in or out is to open it, and - * the framework in this application wants to be told about - * bulk in / out before deciding whether it actually wants to - * use the interface. Maybe something needs to be done about - * this situation. - */ - - kr = (*interface)->USBInterfaceOpen(interface); - - if (kr != 0) { - WARN("Could not open interface: (%08x)\n", kr); - (void) (*interface)->Release(interface); - // continue so we can try the next interface - continue; - } - - // Get the number of endpoints associated with this interface. - kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints); - - if (kr != 0) { - ERR("Unable to get number of endpoints: (%08x)\n", kr); - goto next_interface; - } - - // Get interface class, subclass and protocol - if ((*interface)->GetInterfaceClass(interface, &handle->info.ifc_class) != 0 || - (*interface)->GetInterfaceSubClass(interface, &handle->info.ifc_subclass) != 0 || - (*interface)->GetInterfaceProtocol(interface, &handle->info.ifc_protocol) != 0) - { - ERR("Unable to get interface class, subclass and protocol\n"); - goto next_interface; - } - - handle->info.has_bulk_in = 0; - handle->info.has_bulk_out = 0; - - // Iterate over the endpoints for this interface and see if there - // are any that do bulk in/out. - for (endpoint = 0; endpoint <= interfaceNumEndpoints; endpoint++) { - UInt8 transferType; - UInt16 maxPacketSize; - UInt8 interval; - UInt8 number; - UInt8 direction; - - kr = (*interface)->GetPipeProperties(interface, endpoint, - &direction, - &number, &transferType, &maxPacketSize, &interval); - - if (kr == 0) { - if (transferType != kUSBBulk) { - continue; - } - - if (direction == kUSBIn) { - handle->info.has_bulk_in = 1; - handle->bulkIn = endpoint; - } else if (direction == kUSBOut) { - handle->info.has_bulk_out = 1; - handle->bulkOut = endpoint; - } - - if (handle->info.ifc_protocol == 0x01) { - handle->zero_mask = maxPacketSize - 1; - } - } else { - ERR("could not get pipe properties\n"); - } - - if (handle->info.has_bulk_in && handle->info.has_bulk_out) { - break; - } - } - - if (handle->callback(&handle->info) == 0) { - handle->interface = interface; - handle->success = 1; - - /* - * Clear both the endpoints, because it has been observed - * that the Mac may otherwise (incorrectly) start out with - * them in bad state. - */ - - if (handle->info.has_bulk_in) { - kr = (*interface)->ClearPipeStallBothEnds(interface, - handle->bulkIn); - if (kr != 0) { - ERR("could not clear input pipe; result %d", kr); - return -1; - } - } - - if (handle->info.has_bulk_out) { - kr = (*interface)->ClearPipeStallBothEnds(interface, - handle->bulkOut); - if (kr != 0) { - ERR("could not clear output pipe; result %d", kr); - return -1; - } - } - - return 0; - } - -next_interface: - (*interface)->USBInterfaceClose(interface); - (*interface)->Release(interface); - } - - return 0; -} - -/** Try out the given device and see if there's a match. Returns 0 on - * success, -1 on failure. - */ -static int try_device(io_service_t device, usb_handle *handle) { - kern_return_t kr; - IOCFPlugInInterface **plugin = NULL; - IOUSBDeviceInterface182 **dev = NULL; - SInt32 score; - HRESULT result; - UInt8 serialIndex; - - // Create an intermediate plugin. - kr = IOCreatePlugInInterfaceForService(device, - kIOUSBDeviceUserClientTypeID, - kIOCFPlugInInterfaceID, - &plugin, &score); - - if ((kr != 0) || (plugin == NULL)) { - ERR("Unable to create a plug-in (%08x)\n", kr); - goto error; - } - - // Now create the device interface. - result = (*plugin)->QueryInterface(plugin, - CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID) &dev); - if ((result != 0) || (dev == NULL)) { - ERR("Couldn't create a device interface (%08x)\n", (int) result); - goto error; - } - - /* - * We don't need the intermediate interface after the device interface - * is created. - */ - IODestroyPlugInInterface(plugin); - - // So, we have a device, finally. Grab its vitals. - - kr = (*dev)->GetDeviceVendor(dev, &handle->info.dev_vendor); - if (kr != 0) { - ERR("GetDeviceVendor"); - goto error; - } - - kr = (*dev)->GetDeviceProduct(dev, &handle->info.dev_product); - if (kr != 0) { - ERR("GetDeviceProduct"); - goto error; - } - - kr = (*dev)->GetDeviceClass(dev, &handle->info.dev_class); - if (kr != 0) { - ERR("GetDeviceClass"); - goto error; - } - - kr = (*dev)->GetDeviceSubClass(dev, &handle->info.dev_subclass); - if (kr != 0) { - ERR("GetDeviceSubClass"); - goto error; - } - - kr = (*dev)->GetDeviceProtocol(dev, &handle->info.dev_protocol); - if (kr != 0) { - ERR("GetDeviceProtocol"); - goto error; - } - - kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex); - - if (serialIndex > 0) { - IOUSBDevRequest req; - UInt16 buffer[256]; - - req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); - req.bRequest = kUSBRqGetDescriptor; - req.wValue = (kUSBStringDesc << 8) | serialIndex; - req.wIndex = 0; - 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++) - handle->info.serial_number[i] = buffer[i + 1]; - handle->info.serial_number[i] = 0; - } - } else { - // device has no serial number - handle->info.serial_number[0] = 0; - } - - if (try_interfaces(dev, handle)) { - goto error; - } - - (*dev)->Release(dev); - return 0; - - error: - - if (dev != NULL) { - (*dev)->Release(dev); - } - - return -1; -} - - -/** Initializes the USB system. Returns 0 on success, -1 on error. */ -static int init_usb(ifc_match_func callback, usb_handle **handle) { - int ret = -1; - CFMutableDictionaryRef matchingDict; - kern_return_t result; - io_iterator_t iterator; - usb_handle h; - - h.success = 0; - h.callback = callback; - - /* - * Create our matching dictionary to find appropriate devices. - * IOServiceAddMatchingNotification consumes the reference, so we - * do not need to release it. - */ - matchingDict = IOServiceMatching(kIOUSBDeviceClassName); - - if (matchingDict == NULL) { - ERR("Couldn't create USB matching dictionary.\n"); - return -1; - } - - result = IOServiceGetMatchingServices( - kIOMasterPortDefault, matchingDict, &iterator); - - if (result != 0) { - ERR("Could not create iterator."); - return -1; - } - - for (;;) { - if (! IOIteratorIsValid(iterator)) { - /* - * Apple documentation advises resetting the iterator if - * it should become invalid during iteration. - */ - IOIteratorReset(iterator); - continue; - } - - io_service_t device = IOIteratorNext(iterator); - - if (device == 0) { - break; - } - - usb_ifc_info info; - - if (try_device(device, &h) != 0) { - IOObjectRelease(device); - ret = -1; - break; - } - - if (h.success) { - *handle = calloc(1, sizeof(usb_handle)); - memcpy(*handle, &h, sizeof(usb_handle)); - ret = 0; - break; - } - - IOObjectRelease(device); - } - - IOObjectRelease(iterator); - - return ret; -} - - - -/* - * Definitions of this file's public functions. - */ - -usb_handle *usb_open(ifc_match_func callback) { - usb_handle *handle = NULL; - - if (init_usb(callback, &handle) < 0) { - /* Something went wrong initializing USB. */ - return NULL; - } - - return handle; -} - -int usb_close(usb_handle *h) { - /* TODO: Something better here? */ - return 0; -} - -int usb_read(usb_handle *h, void *data, int len) { - IOReturn result; - UInt32 numBytes = len; - - if (len == 0) { - return 0; - } - - if (h == NULL) { - return -1; - } - - if (h->interface == NULL) { - ERR("usb_read interface was null\n"); - return -1; - } - - if (h->bulkIn == 0) { - ERR("bulkIn endpoint not assigned\n"); - return -1; - } - - result = (*h->interface)->ReadPipe( - h->interface, h->bulkIn, data, &numBytes); - - if (result == 0) { - return (int) numBytes; - } else { - ERR("usb_read failed with status %x\n", result); - } - - return -1; -} - -int usb_write(usb_handle *h, const void *data, int len) { - IOReturn result; - - if (len == 0) { - return 0; - } - - if (h == NULL) { - return -1; - } - - if (h->interface == NULL) { - ERR("usb_write interface was null\n"); - return -1; - } - - if (h->bulkOut == 0) { - ERR("bulkOut endpoint not assigned\n"); - return -1; - } - - result = (*h->interface)->WritePipe( - h->interface, h->bulkOut, (void *)data, len); - - #if 0 - if ((result == 0) && (h->zero_mask)) { - /* we need 0-markers and our transfer */ - if(!(len & h->zero_mask)) { - result = (*h->interface)->WritePipe( - h->interface, h->bulkOut, (void *)data, 0); - } - } - #endif - - if (result != 0) { - ERR("usb_write failed with status %x\n", result); - return -1; - } - - return len; -} diff --git a/fastboot/usb_windows.c b/fastboot/usb_windows.c deleted file mode 100644 index 9c0a9cbf..00000000 --- a/fastboot/usb_windows.c +++ /dev/null @@ -1,375 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <windows.h> -#include <winerror.h> -#include <errno.h> -#include <usb100.h> -#include <adb_api.h> -#include <stdio.h> - -#include "usb.h" - -//#define TRACE_USB 1 -#if TRACE_USB -#define DBG(x...) fprintf(stderr, x) -#else -#define DBG(x...) -#endif - - -/** Structure usb_handle describes our connection to the usb device via - AdbWinApi.dll. This structure is returned from usb_open() routine and - is expected in each subsequent call that is accessing the device. -*/ -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; -}; - -/// Class ID assigned to the device by androidusb.sys -static const GUID usb_class_id = ANDROID_USB_CLASS_ID; - - -/// Checks if interface (device) matches certain criteria -int recognized_device(usb_handle* handle, ifc_match_func callback); - -/// Opens usb interface (device) by interface (device) name. -usb_handle* do_usb_open(const wchar_t* interface_name); - -/// Writes data to the opened usb handle -int usb_write(usb_handle* handle, const void* data, int len); - -/// Reads data using the opened usb handle -int usb_read(usb_handle *handle, void* data, int len); - -/// Cleans up opened usb handle -void usb_cleanup_handle(usb_handle* handle); - -/// Cleans up (but don't close) opened usb handle -void usb_kick(usb_handle* handle); - -/// Closes opened usb handle -int usb_close(usb_handle* handle); - - -usb_handle* do_usb_open(const wchar_t* interface_name) { - // Allocate our handle - usb_handle* ret = (usb_handle*)malloc(sizeof(usb_handle)); - if (NULL == ret) - return NULL; - - // Create interface. - ret->adb_interface = AdbCreateInterfaceByName(interface_name); - - if (NULL == ret->adb_interface) { - free(ret); - errno = GetLastError(); - return NULL; - } - - // Open read pipe (endpoint) - ret->adb_read_pipe = - AdbOpenDefaultBulkReadEndpoint(ret->adb_interface, - AdbOpenAccessTypeReadWrite, - AdbOpenSharingModeReadWrite); - if (NULL != ret->adb_read_pipe) { - // Open write pipe (endpoint) - ret->adb_write_pipe = - AdbOpenDefaultBulkWriteEndpoint(ret->adb_interface, - AdbOpenAccessTypeReadWrite, - AdbOpenSharingModeReadWrite); - if (NULL != ret->adb_write_pipe) { - // Save interface name - unsigned long name_len = 0; - - // First get expected name length - AdbGetInterfaceName(ret->adb_interface, - NULL, - &name_len, - true); - if (0 != name_len) { - ret->interface_name = (char*)malloc(name_len); - - if (NULL != ret->interface_name) { - // Now save the name - if (AdbGetInterfaceName(ret->adb_interface, - ret->interface_name, - &name_len, - true)) { - // We're done at this point - return ret; - } - } else { - SetLastError(ERROR_OUTOFMEMORY); - } - } - } - } - - // Something went wrong. - errno = GetLastError(); - usb_cleanup_handle(ret); - free(ret); - SetLastError(errno); - - return NULL; -} - -int usb_write(usb_handle* handle, const void* data, int len) { - unsigned long time_out = 500 + len * 8; - unsigned long written = 0; - unsigned count = 0; - int ret; - - DBG("usb_write %d\n", len); - if (NULL != handle) { - // Perform write - while(len > 0) { - int xfer = (len > 4096) ? 4096 : len; - ret = AdbWriteEndpointSync(handle->adb_write_pipe, - (void*)data, - (unsigned long)xfer, - &written, - time_out); - errno = GetLastError(); - DBG("AdbWriteEndpointSync returned %d, errno: %d\n", ret, errno); - if (ret == 0) { - // assume ERROR_INVALID_HANDLE indicates we are disconnected - if (errno == ERROR_INVALID_HANDLE) - usb_kick(handle); - return -1; - } - - count += written; - len -= written; - data += written; - - if (len == 0) - return count; - } - } else { - DBG("usb_write NULL handle\n"); - SetLastError(ERROR_INVALID_HANDLE); - } - - DBG("usb_write failed: %d\n", errno); - - return -1; -} - -int usb_read(usb_handle *handle, void* data, int len) { - unsigned long time_out = 500 + len * 8; - unsigned long read = 0; - int ret; - - DBG("usb_read %d\n", len); - if (NULL != handle) { - while (1) { - int xfer = (len > 4096) ? 4096 : len; - - ret = AdbReadEndpointSync(handle->adb_read_pipe, - (void*)data, - (unsigned long)xfer, - &read, - time_out); - errno = GetLastError(); - DBG("usb_read got: %ld, expected: %d, errno: %d\n", read, xfer, errno); - if (ret) { - return read; - } else if (errno != ERROR_SEM_TIMEOUT) { - // assume ERROR_INVALID_HANDLE indicates we are disconnected - if (errno == ERROR_INVALID_HANDLE) - usb_kick(handle); - break; - } - // else we timed out - try again - } - } else { - DBG("usb_read NULL handle\n"); - SetLastError(ERROR_INVALID_HANDLE); - } - - DBG("usb_read failed: %d\n", errno); - - return -1; -} - -void usb_cleanup_handle(usb_handle* handle) { - if (NULL != handle) { - if (NULL != handle->interface_name) - free(handle->interface_name); - if (NULL != handle->adb_write_pipe) - AdbCloseHandle(handle->adb_write_pipe); - if (NULL != handle->adb_read_pipe) - AdbCloseHandle(handle->adb_read_pipe); - if (NULL != handle->adb_interface) - AdbCloseHandle(handle->adb_interface); - - handle->interface_name = NULL; - handle->adb_write_pipe = NULL; - handle->adb_read_pipe = NULL; - handle->adb_interface = NULL; - } -} - -void usb_kick(usb_handle* handle) { - if (NULL != handle) { - usb_cleanup_handle(handle); - } else { - SetLastError(ERROR_INVALID_HANDLE); - errno = ERROR_INVALID_HANDLE; - } -} - -int usb_close(usb_handle* handle) { - DBG("usb_close\n"); - - if (NULL != handle) { - // Cleanup handle - usb_cleanup_handle(handle); - free(handle); - } - - return 0; -} - -int recognized_device(usb_handle* handle, ifc_match_func callback) { - struct usb_ifc_info info; - USB_DEVICE_DESCRIPTOR device_desc; - USB_INTERFACE_DESCRIPTOR interf_desc; - - if (NULL == handle) - return 0; - - // Check vendor and product id first - if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, - &device_desc)) { - return 0; - } - - // Then check interface properties - if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, - &interf_desc)) { - return 0; - } - - // Must have two endpoints - if (2 != interf_desc.bNumEndpoints) { - return 0; - } - - info.dev_vendor = device_desc.idVendor; - info.dev_product = device_desc.idProduct; - info.dev_class = device_desc.bDeviceClass; - info.dev_subclass = device_desc.bDeviceSubClass; - info.dev_protocol = device_desc.bDeviceProtocol; - info.ifc_class = interf_desc.bInterfaceClass; - info.ifc_subclass = interf_desc.bInterfaceSubClass; - info.ifc_protocol = interf_desc.bInterfaceProtocol; - - // read serial number (if there is one) - unsigned long serial_number_len = sizeof(info.serial_number); - if (!AdbGetSerialNumber(handle->adb_interface, info.serial_number, - &serial_number_len, true)) { - info.serial_number[0] = 0; - } - - if (callback(&info) == 0) { - return 1; - } - - return 0; -} - -static usb_handle *find_usb_device(ifc_match_func callback) { - usb_handle* handle = NULL; - char entry_buffer[2048]; - char interf_name[2048]; - AdbInterfaceInfo* next_interface = (AdbInterfaceInfo*)(&entry_buffer[0]); - unsigned long entry_buffer_size = sizeof(entry_buffer); - char* copy_name; - - // Enumerate all present and active interfaces. - ADBAPIHANDLE enum_handle = - AdbEnumInterfaces(usb_class_id, true, true, true); - - if (NULL == enum_handle) - return NULL; - - while (AdbNextInterface(enum_handle, next_interface, &entry_buffer_size)) { - // TODO(vchtchetkine): FIXME - temp hack converting wchar_t into char. - // It would be better to change AdbNextInterface so it will return - // interface name as single char string. - const wchar_t* wchar_name = next_interface->device_name; - for(copy_name = interf_name; - L'\0' != *wchar_name; - wchar_name++, copy_name++) { - *copy_name = (char)(*wchar_name); - } - *copy_name = '\0'; - - handle = do_usb_open(next_interface->device_name); - if (NULL != handle) { - // Lets see if this interface (device) belongs to us - if (recognized_device(handle, callback)) { - // found it! - break; - } else { - usb_cleanup_handle(handle); - free(handle); - handle = NULL; - } - } - - entry_buffer_size = sizeof(entry_buffer); - } - - AdbCloseHandle(enum_handle); - return handle; -} - -usb_handle *usb_open(ifc_match_func callback) -{ - return find_usb_device(callback); -} - -// called from fastboot.c -void sleep(int seconds) -{ - Sleep(seconds * 1000); -} diff --git a/fastboot/usbtest.c b/fastboot/usbtest.c deleted file mode 100644 index e34d7e62..00000000 --- a/fastboot/usbtest.c +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> - -#include <sys/time.h> - -#include "usb.h" - -static unsigned arg_size = 4096; -static unsigned arg_count = 4096; - -long long NOW(void) -{ - struct timeval tv; - gettimeofday(&tv, 0); - - return (((long long) tv.tv_sec) * ((long long) 1000000)) + - (((long long) tv.tv_usec)); -} - -int printifc(usb_ifc_info *info) -{ - printf("dev: csp=%02x/%02x/%02x v=%04x p=%04x ", - info->dev_class, info->dev_subclass, info->dev_protocol, - info->dev_vendor, info->dev_product); - printf("ifc: csp=%02x/%02x/%02x%s%s\n", - info->ifc_class, info->ifc_subclass, info->ifc_protocol, - info->has_bulk_in ? " in" : "", - info->has_bulk_out ? " out" : ""); - return -1; -} - -int match_null(usb_ifc_info *info) -{ - if(info->dev_vendor != 0x18d1) return -1; - if(info->ifc_class != 0xff) return -1; - if(info->ifc_subclass != 0xfe) return -1; - if(info->ifc_protocol != 0x01) return -1; - return 0; -} - -int match_zero(usb_ifc_info *info) -{ - if(info->dev_vendor != 0x18d1) return -1; - if(info->ifc_class != 0xff) return -1; - if(info->ifc_subclass != 0xfe) return -1; - if(info->ifc_protocol != 0x02) return -1; - return 0; -} - -int match_loop(usb_ifc_info *info) -{ - if(info->dev_vendor != 0x18d1) return -1; - if(info->ifc_class != 0xff) return -1; - if(info->ifc_subclass != 0xfe) return -1; - if(info->ifc_protocol != 0x03) return -1; - return 0; -} - -int test_null(usb_handle *usb) -{ - int i; - unsigned char buf[4096]; - memset(buf, 0xee, 4096); - long long t0, t1; - - t0 = NOW(); - for(i = 0; i < arg_count; i++) { - if(usb_write(usb, buf, arg_size) != arg_size) { - fprintf(stderr,"write failed (%s)\n", strerror(errno)); - return -1; - } - } - t1 = NOW(); - fprintf(stderr,"%d bytes in %lld uS\n", arg_count * arg_size, (t1 - t0)); - return 0; -} - -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) { - fprintf(stderr,"read failed (%s)\n", strerror(errno)); - return -1; - } - } - t1 = NOW(); - fprintf(stderr,"%d bytes in %lld uS\n", arg_count * arg_size, (t1 - t0)); - return 0; -} - -struct -{ - const char *cmd; - ifc_match_func match; - int (*test)(usb_handle *usb); - const char *help; -} tests[] = { - { "list", printifc, 0, "list interfaces" }, - { "send", match_null, test_null, "send to null interface" }, - { "recv", match_zero, test_zero, "recv from zero interface" }, - { "loop", match_loop, 0, "exercise loopback interface" }, - {}, -}; - -int usage(void) -{ - int i; - - fprintf(stderr,"usage: usbtest <testname>\n\navailable tests:\n"); - for(i = 0; tests[i].cmd; i++) { - fprintf(stderr," %-8s %s\n", tests[i].cmd, tests[i].help); - } - return -1; -} - -int process_args(int argc, char **argv) -{ - while(argc-- > 0) { - char *arg = *argv++; - if(!strncmp(arg,"count=",6)) { - arg_count = atoi(arg + 6); - } else if(!strncmp(arg,"size=",5)) { - arg_size = atoi(arg + 5); - } else { - fprintf(stderr,"unknown argument: %s\n", arg); - return -1; - } - } - - if(arg_count == 0) { - fprintf(stderr,"count may not be zero\n"); - return -1; - } - - if(arg_size > 4096) { - fprintf(stderr,"size may not be greater than 4096\n"); - return -1; - } - - return 0; -} - -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)) - return -1; - } - - for(i = 0; tests[i].cmd; i++) { - if(!strcmp(argv[1], tests[i].cmd)) { - usb = usb_open(tests[i].match); - if(tests[i].test) { - if(usb == 0) { - fprintf(stderr,"usbtest: %s: could not find interface\n", - tests[i].cmd); - return -1; - } - if(tests[i].test(usb)) { - fprintf(stderr,"usbtest: %s: FAIL\n", tests[i].cmd); - return -1; - } else { - fprintf(stderr,"usbtest: %s: OKAY\n", tests[i].cmd); - } - } - return 0; - } - } - - return usage(); -} diff --git a/fastboot/util_linux.c b/fastboot/util_linux.c deleted file mode 100644 index 912e16f9..00000000 --- a/fastboot/util_linux.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> -#include <fcntl.h> -#include <unistd.h> -#include <limits.h> - -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); - - if(err <= 0) { - path[0] = 0; - } else { - path[err] = 0; - x = strrchr(path,'/'); - if(x) x[1] = 0; - } -} diff --git a/fastboot/util_osx.c b/fastboot/util_osx.c deleted file mode 100644 index 068241ce..00000000 --- a/fastboot/util_osx.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <utils/executablepath.h> -#import <Carbon/Carbon.h> -#include <unistd.h> - -void get_my_path(char s[PATH_MAX]) -{ - char *x; - ProcessSerialNumber psn; - GetCurrentProcess(&psn); - CFDictionaryRef dict; - dict = ProcessInformationCopyDictionary(&psn, 0xffffffff); - CFStringRef value = (CFStringRef)CFDictionaryGetValue(dict, - CFSTR("CFBundleExecutable")); - CFStringGetCString(value, s, PATH_MAX - 1, kCFStringEncodingUTF8); - x = strrchr(s, '/'); - if(x) x[1] = 0; -} - - diff --git a/fastboot/util_windows.c b/fastboot/util_windows.c deleted file mode 100644 index 37077a49..00000000 --- a/fastboot/util_windows.c +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * * Redistributions of source code must retain the above copyright - * 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 - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * 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 - * 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 <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> -#include <fcntl.h> -#include <unistd.h> -#include <limits.h> - -#include <windows.h> - -void get_my_path(char exe[PATH_MAX]) -{ - char* r; - - GetModuleFileName( NULL, exe, PATH_MAX-1 ); - exe[PATH_MAX-1] = 0; - r = strrchr( exe, '\\' ); - if (r) - *r = 0; -} - - -void *load_file(const char *fn, unsigned *_sz) -{ - HANDLE file; - char *data; - DWORD file_size; - - file = CreateFile( fn, - GENERIC_READ, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - 0, - NULL ); - - if (file == INVALID_HANDLE_VALUE) - return NULL; - - file_size = GetFileSize( file, NULL ); - data = NULL; - - if (file_size > 0) { - data = (char*) malloc( file_size ); - if (data == NULL) { - fprintf(stderr, "load_file: could not allocate %ld bytes\n", file_size ); - file_size = 0; - } else { - DWORD out_bytes; - - if ( !ReadFile( file, data, file_size, &out_bytes, NULL ) || - out_bytes != file_size ) - { - fprintf(stderr, "load_file: could not read %ld bytes from '%s'\n", file_size, fn); - free(data); - data = NULL; - file_size = 0; - } - } - } - CloseHandle( file ); - - *_sz = (unsigned) file_size; - return data; -} |
