The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 12 | * the documentation and/or other materials provided with the |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 29 | #define _LARGEFILE64_SOURCE |
| 30 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <stdarg.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 34 | #include <stdbool.h> |
| 35 | #include <stdint.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <unistd.h> |
| 40 | #include <limits.h> |
| 41 | #include <ctype.h> |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 42 | #include <getopt.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
| 44 | #include <sys/time.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 45 | #include <sys/types.h> |
| 46 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #include <bootimg.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 48 | #include <sparse/sparse.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | #include <zipfile/zipfile.h> |
| 50 | |
| 51 | #include "fastboot.h" |
| 52 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 53 | #ifndef O_BINARY |
| 54 | #define O_BINARY 0 |
| 55 | #endif |
| 56 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 57 | char cur_product[FB_RESPONSE_SZ + 1]; |
| 58 | |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 59 | void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline); |
| 60 | |
| 61 | boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, |
| 62 | void *ramdisk, unsigned ramdisk_size, |
| 63 | void *second, unsigned second_size, |
| 64 | unsigned page_size, unsigned base, |
| 65 | unsigned *bootimg_size); |
| 66 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | static usb_handle *usb = 0; |
| 68 | static const char *serial = 0; |
| 69 | static const char *product = 0; |
| 70 | static const char *cmdline = 0; |
| 71 | static int wipe_data = 0; |
| 72 | static unsigned short vendor_id = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 73 | static int64_t sparse_limit = -1; |
| 74 | static int64_t target_sparse_limit = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 76 | static unsigned base_addr = 0x10000000; |
| 77 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | void die(const char *fmt, ...) |
| 79 | { |
| 80 | va_list ap; |
| 81 | va_start(ap, fmt); |
| 82 | fprintf(stderr,"error: "); |
| 83 | vfprintf(stderr, fmt, ap); |
| 84 | fprintf(stderr,"\n"); |
| 85 | va_end(ap); |
| 86 | exit(1); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 87 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | |
| 89 | void get_my_path(char *path); |
| 90 | |
| 91 | char *find_item(const char *item, const char *product) |
| 92 | { |
| 93 | char *dir; |
| 94 | char *fn; |
| 95 | char path[PATH_MAX + 128]; |
| 96 | |
| 97 | if(!strcmp(item,"boot")) { |
| 98 | fn = "boot.img"; |
| 99 | } else if(!strcmp(item,"recovery")) { |
| 100 | fn = "recovery.img"; |
| 101 | } else if(!strcmp(item,"system")) { |
| 102 | fn = "system.img"; |
| 103 | } else if(!strcmp(item,"userdata")) { |
| 104 | fn = "userdata.img"; |
Jean-Baptiste Queru | d7608a4 | 2011-09-30 14:39:25 -0700 | [diff] [blame] | 105 | } else if(!strcmp(item,"cache")) { |
| 106 | fn = "cache.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | } else if(!strcmp(item,"info")) { |
| 108 | fn = "android-info.txt"; |
| 109 | } else { |
| 110 | fprintf(stderr,"unknown partition '%s'\n", item); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | if(product) { |
| 115 | get_my_path(path); |
| 116 | sprintf(path + strlen(path), |
| 117 | "../../../target/product/%s/%s", product, fn); |
| 118 | return strdup(path); |
| 119 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 120 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | dir = getenv("ANDROID_PRODUCT_OUT"); |
| 122 | if((dir == 0) || (dir[0] == 0)) { |
| 123 | die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); |
| 124 | return 0; |
| 125 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 126 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 127 | sprintf(path, "%s/%s", dir, fn); |
| 128 | return strdup(path); |
| 129 | } |
| 130 | |
| 131 | #ifdef _WIN32 |
| 132 | void *load_file(const char *fn, unsigned *_sz); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 133 | int64_t file_size(const char *fn); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | #else |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 135 | #if defined(__APPLE__) && defined(__MACH__) |
| 136 | #define lseek64 lseek |
| 137 | #define off64_t off_t |
| 138 | #endif |
| 139 | |
| 140 | int64_t file_size(const char *fn) |
| 141 | { |
| 142 | off64_t off; |
| 143 | int fd; |
| 144 | |
| 145 | fd = open(fn, O_RDONLY); |
| 146 | if (fd < 0) return -1; |
| 147 | |
| 148 | off = lseek64(fd, 0, SEEK_END); |
| 149 | close(fd); |
| 150 | |
| 151 | return off; |
| 152 | } |
| 153 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | void *load_file(const char *fn, unsigned *_sz) |
| 155 | { |
| 156 | char *data; |
| 157 | int sz; |
| 158 | int fd; |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 159 | int errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | |
| 161 | data = 0; |
| 162 | fd = open(fn, O_RDONLY); |
| 163 | if(fd < 0) return 0; |
| 164 | |
| 165 | sz = lseek(fd, 0, SEEK_END); |
| 166 | if(sz < 0) goto oops; |
| 167 | |
| 168 | if(lseek(fd, 0, SEEK_SET) != 0) goto oops; |
| 169 | |
| 170 | data = (char*) malloc(sz); |
| 171 | if(data == 0) goto oops; |
| 172 | |
| 173 | if(read(fd, data, sz) != sz) goto oops; |
| 174 | close(fd); |
| 175 | |
| 176 | if(_sz) *_sz = sz; |
| 177 | return data; |
| 178 | |
| 179 | oops: |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 180 | errno_tmp = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | close(fd); |
| 182 | if(data != 0) free(data); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 183 | errno = errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | int match_fastboot(usb_ifc_info *info) |
| 189 | { |
| 190 | if(!(vendor_id && (info->dev_vendor == vendor_id)) && |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 191 | (info->dev_vendor != 0x18d1) && // Google |
Wu, Hao | f60e863 | 2012-01-17 12:04:11 -0800 | [diff] [blame] | 192 | (info->dev_vendor != 0x8087) && // Intel |
The Android Open Source Project | f614d64 | 2009-03-18 17:39:49 -0700 | [diff] [blame] | 193 | (info->dev_vendor != 0x0451) && |
Robert CH Chou | e25ff1c | 2009-09-21 09:51:35 +0800 | [diff] [blame] | 194 | (info->dev_vendor != 0x0502) && |
Dima Zavin | 509f739 | 2010-05-14 14:48:30 -0700 | [diff] [blame] | 195 | (info->dev_vendor != 0x0fce) && // Sony Ericsson |
| 196 | (info->dev_vendor != 0x05c6) && // Qualcomm |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 197 | (info->dev_vendor != 0x22b8) && // Motorola |
Erik Gilling | 37e9e90 | 2010-01-20 17:40:05 -0800 | [diff] [blame] | 198 | (info->dev_vendor != 0x0955) && // Nvidia |
Xavier Ducrohet | af82f21 | 2010-01-21 17:39:25 -0800 | [diff] [blame] | 199 | (info->dev_vendor != 0x413c) && // DELL |
Xavier Ducrohet | 746f324 | 2012-01-13 16:03:37 -0800 | [diff] [blame] | 200 | (info->dev_vendor != 0x2314) && // INQ Mobile |
Ramanan Rajeswaran | 73c019b | 2012-02-24 13:00:34 -0800 | [diff] [blame] | 201 | (info->dev_vendor != 0x0b05) && // Asus |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 202 | (info->dev_vendor != 0x0bb4)) // HTC |
| 203 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | if(info->ifc_class != 0xff) return -1; |
| 205 | if(info->ifc_subclass != 0x42) return -1; |
| 206 | if(info->ifc_protocol != 0x03) return -1; |
| 207 | // require matching serial number if a serial number is specified |
| 208 | // at the command line with the -s option. |
| 209 | if (serial && strcmp(serial, info->serial_number) != 0) return -1; |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int list_devices_callback(usb_ifc_info *info) |
| 214 | { |
| 215 | if (match_fastboot(info) == 0) { |
| 216 | char* serial = info->serial_number; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 217 | if (!info->writable) { |
| 218 | serial = "no permissions"; // like "adb devices" |
| 219 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | if (!serial[0]) { |
| 221 | serial = "????????????"; |
| 222 | } |
| 223 | // output compatible with "adb devices" |
| 224 | printf("%s\tfastboot\n", serial); |
| 225 | } |
| 226 | |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | usb_handle *open_device(void) |
| 231 | { |
| 232 | static usb_handle *usb = 0; |
| 233 | int announce = 1; |
| 234 | |
| 235 | if(usb) return usb; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 236 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | for(;;) { |
| 238 | usb = usb_open(match_fastboot); |
| 239 | if(usb) return usb; |
| 240 | if(announce) { |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 241 | announce = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 242 | fprintf(stderr,"< waiting for device >\n"); |
| 243 | } |
| 244 | sleep(1); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void list_devices(void) { |
| 249 | // We don't actually open a USB device here, |
| 250 | // just getting our callback called so we can |
| 251 | // list all the connected devices. |
| 252 | usb_open(list_devices_callback); |
| 253 | } |
| 254 | |
| 255 | void usage(void) |
| 256 | { |
| 257 | fprintf(stderr, |
| 258 | /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ |
| 259 | "usage: fastboot [ <option> ] <command>\n" |
| 260 | "\n" |
| 261 | "commands:\n" |
| 262 | " update <filename> reflash device from update.zip\n" |
| 263 | " flashall flash boot + recovery + system\n" |
| 264 | " flash <partition> [ <filename> ] write a file to a flash partition\n" |
| 265 | " erase <partition> erase a flash partition\n" |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 266 | " format <partition> format a flash partition \n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | " getvar <variable> display a bootloader variable\n" |
| 268 | " boot <kernel> [ <ramdisk> ] download and boot kernel\n" |
| 269 | " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n" |
| 270 | " devices list all connected devices\n" |
Bruce Beare | 24ce4bc | 2010-10-14 09:43:26 -0700 | [diff] [blame] | 271 | " continue continue with autoboot\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 272 | " reboot reboot device normally\n" |
| 273 | " reboot-bootloader reboot device into bootloader\n" |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 274 | " help show this help message\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | "\n" |
| 276 | "options:\n" |
| 277 | " -w erase userdata and cache\n" |
| 278 | " -s <serial number> specify device serial number\n" |
| 279 | " -p <product> specify product name\n" |
| 280 | " -c <cmdline> override kernel commandline\n" |
| 281 | " -i <vendor id> specify a custom USB vendor id\n" |
Dima Zavin | 95ec983 | 2009-04-30 15:03:05 -0700 | [diff] [blame] | 282 | " -b <base_addr> specify a custom kernel base address\n" |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 283 | " -n <page size> specify the nand page size. default: 2048\n" |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 284 | " -S <size>[K|M|G] automatically sparse files greater than\n" |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 285 | " size. 0 to disable\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 286 | ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 289 | void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 290 | unsigned *sz, const char *cmdline) |
| 291 | { |
| 292 | void *kdata = 0, *rdata = 0; |
| 293 | unsigned ksize = 0, rsize = 0; |
| 294 | void *bdata; |
| 295 | unsigned bsize; |
| 296 | |
| 297 | if(kernel == 0) { |
| 298 | fprintf(stderr, "no image specified\n"); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | kdata = load_file(kernel, &ksize); |
| 303 | if(kdata == 0) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 304 | fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 305 | return 0; |
| 306 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 307 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 308 | /* is this actually a boot image? */ |
| 309 | if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 310 | if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 311 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 312 | if(ramdisk) { |
| 313 | fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); |
| 314 | return 0; |
| 315 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 316 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 | *sz = ksize; |
| 318 | return kdata; |
| 319 | } |
| 320 | |
| 321 | if(ramdisk) { |
| 322 | rdata = load_file(ramdisk, &rsize); |
| 323 | if(rdata == 0) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 324 | fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | fprintf(stderr,"creating boot image...\n"); |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 330 | bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | if(bdata == 0) { |
| 332 | fprintf(stderr,"failed to create boot.img\n"); |
| 333 | return 0; |
| 334 | } |
| 335 | if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); |
| 336 | fprintf(stderr,"creating boot image - %d bytes\n", bsize); |
| 337 | *sz = bsize; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 338 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 339 | return bdata; |
| 340 | } |
| 341 | |
| 342 | void *unzip_file(zipfile_t zip, const char *name, unsigned *sz) |
| 343 | { |
| 344 | void *data; |
| 345 | zipentry_t entry; |
| 346 | unsigned datasz; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 347 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 | entry = lookup_zipentry(zip, name); |
| 349 | if (entry == NULL) { |
| 350 | fprintf(stderr, "archive does not contain '%s'\n", name); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | *sz = get_zipentry_size(entry); |
| 355 | |
| 356 | datasz = *sz * 1.001; |
| 357 | data = malloc(datasz); |
| 358 | |
| 359 | if(data == 0) { |
| 360 | fprintf(stderr, "failed to allocate %d bytes\n", *sz); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | if (decompress_zipentry(entry, data, datasz)) { |
| 365 | fprintf(stderr, "failed to unzip '%s' from archive\n", name); |
| 366 | free(data); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | return data; |
| 371 | } |
| 372 | |
| 373 | static char *strip(char *s) |
| 374 | { |
| 375 | int n; |
| 376 | while(*s && isspace(*s)) s++; |
| 377 | n = strlen(s); |
| 378 | while(n-- > 0) { |
| 379 | if(!isspace(s[n])) break; |
| 380 | s[n] = 0; |
| 381 | } |
| 382 | return s; |
| 383 | } |
| 384 | |
| 385 | #define MAX_OPTIONS 32 |
| 386 | static int setup_requirement_line(char *name) |
| 387 | { |
| 388 | char *val[MAX_OPTIONS]; |
| 389 | const char **out; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 390 | char *prod = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | unsigned n, count; |
| 392 | char *x; |
| 393 | int invert = 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 394 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 395 | if (!strncmp(name, "reject ", 7)) { |
| 396 | name += 7; |
| 397 | invert = 1; |
| 398 | } else if (!strncmp(name, "require ", 8)) { |
| 399 | name += 8; |
| 400 | invert = 0; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 401 | } else if (!strncmp(name, "require-for-product:", 20)) { |
| 402 | // Get the product and point name past it |
| 403 | prod = name + 20; |
| 404 | name = strchr(name, ' '); |
| 405 | if (!name) return -1; |
| 406 | *name = 0; |
| 407 | name += 1; |
| 408 | invert = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | x = strchr(name, '='); |
| 412 | if (x == 0) return 0; |
| 413 | *x = 0; |
| 414 | val[0] = x + 1; |
| 415 | |
| 416 | for(count = 1; count < MAX_OPTIONS; count++) { |
| 417 | x = strchr(val[count - 1],'|'); |
| 418 | if (x == 0) break; |
| 419 | *x = 0; |
| 420 | val[count] = x + 1; |
| 421 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 422 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | name = strip(name); |
| 424 | for(n = 0; n < count; n++) val[n] = strip(val[n]); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 425 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | name = strip(name); |
| 427 | if (name == 0) return -1; |
| 428 | |
| 429 | /* work around an unfortunate name mismatch */ |
| 430 | if (!strcmp(name,"board")) name = "product"; |
| 431 | |
| 432 | out = malloc(sizeof(char*) * count); |
| 433 | if (out == 0) return -1; |
| 434 | |
| 435 | for(n = 0; n < count; n++) { |
| 436 | out[n] = strdup(strip(val[n])); |
| 437 | if (out[n] == 0) return -1; |
| 438 | } |
| 439 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 440 | fb_queue_require(prod, name, invert, n, out); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static void setup_requirements(char *data, unsigned sz) |
| 445 | { |
| 446 | char *s; |
| 447 | |
| 448 | s = data; |
| 449 | while (sz-- > 0) { |
| 450 | if(*s == '\n') { |
| 451 | *s++ = 0; |
| 452 | if (setup_requirement_line(data)) { |
| 453 | die("out of memory"); |
| 454 | } |
| 455 | data = s; |
| 456 | } else { |
| 457 | s++; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void queue_info_dump(void) |
| 463 | { |
| 464 | fb_queue_notice("--------------------------------------------"); |
| 465 | fb_queue_display("version-bootloader", "Bootloader Version..."); |
| 466 | fb_queue_display("version-baseband", "Baseband Version....."); |
| 467 | fb_queue_display("serialno", "Serial Number........"); |
| 468 | fb_queue_notice("--------------------------------------------"); |
| 469 | } |
| 470 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 471 | |
| 472 | struct sparse_file **load_sparse_files(const char *fname, int max_size) |
| 473 | { |
| 474 | int fd; |
| 475 | struct sparse_file *s; |
| 476 | int files; |
| 477 | struct sparse_file **out_s; |
| 478 | |
| 479 | fd = open(fname, O_RDONLY | O_BINARY); |
| 480 | if (fd < 0) { |
| 481 | die("cannot open '%s'\n", fname); |
| 482 | } |
| 483 | |
| 484 | s = sparse_file_import_auto(fd, false); |
| 485 | if (!s) { |
| 486 | die("cannot sparse read file '%s'\n", fname); |
| 487 | } |
| 488 | |
| 489 | files = sparse_file_resparse(s, max_size, NULL, 0); |
| 490 | if (files < 0) { |
| 491 | die("Failed to resparse '%s'\n", fname); |
| 492 | } |
| 493 | |
| 494 | out_s = calloc(sizeof(struct sparse_file *), files + 1); |
| 495 | if (!out_s) { |
| 496 | die("Failed to allocate sparse file array\n"); |
| 497 | } |
| 498 | |
| 499 | files = sparse_file_resparse(s, max_size, out_s, files); |
| 500 | if (files < 0) { |
| 501 | die("Failed to resparse '%s'\n", fname); |
| 502 | } |
| 503 | |
| 504 | return out_s; |
| 505 | } |
| 506 | |
| 507 | static int64_t get_target_sparse_limit(struct usb_handle *usb) |
| 508 | { |
| 509 | int64_t limit = 0; |
| 510 | char response[FB_RESPONSE_SZ + 1]; |
| 511 | int status = fb_getvar(usb, response, "max-download-size"); |
| 512 | |
| 513 | if (!status) { |
| 514 | limit = strtoul(response, NULL, 0); |
| 515 | if (limit > 0) { |
| 516 | fprintf(stderr, "target reported max download size of %lld bytes\n", |
| 517 | limit); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | return limit; |
| 522 | } |
| 523 | |
| 524 | static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size) |
| 525 | { |
| 526 | int64_t limit; |
| 527 | |
| 528 | if (sparse_limit == 0) { |
| 529 | return 0; |
| 530 | } else if (sparse_limit > 0) { |
| 531 | limit = sparse_limit; |
| 532 | } else { |
| 533 | if (target_sparse_limit == -1) { |
| 534 | target_sparse_limit = get_target_sparse_limit(usb); |
| 535 | } |
| 536 | if (target_sparse_limit > 0) { |
| 537 | limit = target_sparse_limit; |
| 538 | } else { |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 539 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | if (size > limit) { |
| 544 | return limit; |
| 545 | } |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | void do_flash(usb_handle *usb, const char *pname, const char *fname) |
| 551 | { |
| 552 | int64_t sz64; |
| 553 | void *data; |
| 554 | int64_t limit; |
| 555 | |
| 556 | sz64 = file_size(fname); |
| 557 | limit = get_sparse_limit(usb, sz64); |
| 558 | if (limit) { |
| 559 | struct sparse_file **s = load_sparse_files(fname, limit); |
| 560 | if (s == NULL) { |
| 561 | die("cannot sparse load '%s'\n", fname); |
| 562 | } |
| 563 | while (*s) { |
| 564 | sz64 = sparse_file_len(*s, true, false); |
| 565 | fb_queue_flash_sparse(pname, *s++, sz64); |
| 566 | } |
| 567 | } else { |
| 568 | unsigned int sz; |
| 569 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 570 | if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno)); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 571 | fb_queue_flash(pname, data, sz); |
| 572 | } |
| 573 | } |
| 574 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 575 | void do_update_signature(zipfile_t zip, char *fn) |
| 576 | { |
| 577 | void *data; |
| 578 | unsigned sz; |
| 579 | data = unzip_file(zip, fn, &sz); |
| 580 | if (data == 0) return; |
| 581 | fb_queue_download("signature", data, sz); |
| 582 | fb_queue_command("signature", "installing signature"); |
| 583 | } |
| 584 | |
| 585 | void do_update(char *fn) |
| 586 | { |
| 587 | void *zdata; |
| 588 | unsigned zsize; |
| 589 | void *data; |
| 590 | unsigned sz; |
| 591 | zipfile_t zip; |
| 592 | |
| 593 | queue_info_dump(); |
| 594 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 595 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 596 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 597 | zdata = load_file(fn, &zsize); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 598 | if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 599 | |
| 600 | zip = init_zipfile(zdata, zsize); |
| 601 | if(zip == 0) die("failed to access zipdata in '%s'"); |
| 602 | |
| 603 | data = unzip_file(zip, "android-info.txt", &sz); |
| 604 | if (data == 0) { |
| 605 | char *tmp; |
| 606 | /* fallback for older zipfiles */ |
| 607 | data = unzip_file(zip, "android-product.txt", &sz); |
| 608 | if ((data == 0) || (sz < 1)) { |
| 609 | die("update package has no android-info.txt or android-product.txt"); |
| 610 | } |
| 611 | tmp = malloc(sz + 128); |
| 612 | if (tmp == 0) die("out of memory"); |
| 613 | sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data); |
| 614 | data = tmp; |
| 615 | sz = strlen(tmp); |
| 616 | } |
| 617 | |
| 618 | setup_requirements(data, sz); |
| 619 | |
| 620 | data = unzip_file(zip, "boot.img", &sz); |
| 621 | if (data == 0) die("update package missing boot.img"); |
| 622 | do_update_signature(zip, "boot.sig"); |
| 623 | fb_queue_flash("boot", data, sz); |
| 624 | |
| 625 | data = unzip_file(zip, "recovery.img", &sz); |
| 626 | if (data != 0) { |
| 627 | do_update_signature(zip, "recovery.sig"); |
| 628 | fb_queue_flash("recovery", data, sz); |
| 629 | } |
| 630 | |
| 631 | data = unzip_file(zip, "system.img", &sz); |
| 632 | if (data == 0) die("update package missing system.img"); |
| 633 | do_update_signature(zip, "system.sig"); |
| 634 | fb_queue_flash("system", data, sz); |
| 635 | } |
| 636 | |
| 637 | void do_send_signature(char *fn) |
| 638 | { |
| 639 | void *data; |
| 640 | unsigned sz; |
| 641 | char *xtn; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 642 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 643 | xtn = strrchr(fn, '.'); |
| 644 | if (!xtn) return; |
| 645 | if (strcmp(xtn, ".img")) return; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 646 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 647 | strcpy(xtn,".sig"); |
| 648 | data = load_file(fn, &sz); |
| 649 | strcpy(xtn,".img"); |
| 650 | if (data == 0) return; |
| 651 | fb_queue_download("signature", data, sz); |
| 652 | fb_queue_command("signature", "installing signature"); |
| 653 | } |
| 654 | |
| 655 | void do_flashall(void) |
| 656 | { |
| 657 | char *fname; |
| 658 | void *data; |
| 659 | unsigned sz; |
| 660 | |
| 661 | queue_info_dump(); |
| 662 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 663 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 664 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 665 | fname = find_item("info", product); |
| 666 | if (fname == 0) die("cannot find android-info.txt"); |
| 667 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 668 | if (data == 0) die("could not load android-info.txt: %s", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 669 | setup_requirements(data, sz); |
| 670 | |
| 671 | fname = find_item("boot", product); |
| 672 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 673 | if (data == 0) die("could not load boot.img: %s", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 674 | do_send_signature(fname); |
| 675 | fb_queue_flash("boot", data, sz); |
| 676 | |
| 677 | fname = find_item("recovery", product); |
| 678 | data = load_file(fname, &sz); |
| 679 | if (data != 0) { |
| 680 | do_send_signature(fname); |
| 681 | fb_queue_flash("recovery", data, sz); |
| 682 | } |
| 683 | |
| 684 | fname = find_item("system", product); |
| 685 | data = load_file(fname, &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 686 | if (data == 0) die("could not load system.img: %s", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 687 | do_send_signature(fname); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 688 | fb_queue_flash("system", data, sz); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | #define skip(n) do { argc -= (n); argv += (n); } while (0) |
JP Abgrall | 2d13d14 | 2011-03-01 23:35:07 -0800 | [diff] [blame] | 692 | #define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 693 | |
| 694 | int do_oem_command(int argc, char **argv) |
| 695 | { |
| 696 | int i; |
| 697 | char command[256]; |
| 698 | if (argc <= 1) return 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 699 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 700 | command[0] = 0; |
| 701 | while(1) { |
| 702 | strcat(command,*argv); |
| 703 | skip(1); |
| 704 | if(argc == 0) break; |
| 705 | strcat(command," "); |
| 706 | } |
| 707 | |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 708 | fb_queue_command(command,""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 712 | static int64_t parse_num(const char *arg) |
| 713 | { |
| 714 | char *endptr; |
| 715 | unsigned long long num; |
| 716 | |
| 717 | num = strtoull(arg, &endptr, 0); |
| 718 | if (endptr == arg) { |
| 719 | return -1; |
| 720 | } |
| 721 | |
| 722 | if (*endptr == 'k' || *endptr == 'K') { |
| 723 | if (num >= (-1ULL) / 1024) { |
| 724 | return -1; |
| 725 | } |
| 726 | num *= 1024LL; |
| 727 | endptr++; |
| 728 | } else if (*endptr == 'm' || *endptr == 'M') { |
| 729 | if (num >= (-1ULL) / (1024 * 1024)) { |
| 730 | return -1; |
| 731 | } |
| 732 | num *= 1024LL * 1024LL; |
| 733 | endptr++; |
| 734 | } else if (*endptr == 'g' || *endptr == 'G') { |
| 735 | if (num >= (-1ULL) / (1024 * 1024 * 1024)) { |
| 736 | return -1; |
| 737 | } |
| 738 | num *= 1024LL * 1024LL * 1024LL; |
| 739 | endptr++; |
| 740 | } |
| 741 | |
| 742 | if (*endptr != '\0') { |
| 743 | return -1; |
| 744 | } |
| 745 | |
| 746 | if (num > INT64_MAX) { |
| 747 | return -1; |
| 748 | } |
| 749 | |
| 750 | return num; |
| 751 | } |
| 752 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 753 | int main(int argc, char **argv) |
| 754 | { |
| 755 | int wants_wipe = 0; |
| 756 | int wants_reboot = 0; |
| 757 | int wants_reboot_bootloader = 0; |
| 758 | void *data; |
| 759 | unsigned sz; |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 760 | unsigned page_size = 2048; |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 761 | int status; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 762 | int c; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 763 | int r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 764 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 765 | const struct option longopts = { 0, 0, 0, 0 }; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 766 | |
| 767 | serial = getenv("ANDROID_SERIAL"); |
| 768 | |
| 769 | while (1) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 770 | c = getopt_long(argc, argv, "wb:n:s:S:p:c:i:m:h", &longopts, NULL); |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 771 | if (c < 0) { |
| 772 | break; |
| 773 | } |
| 774 | |
| 775 | switch (c) { |
| 776 | case 'w': |
| 777 | wants_wipe = 1; |
| 778 | break; |
| 779 | case 'b': |
| 780 | base_addr = strtoul(optarg, 0, 16); |
| 781 | break; |
| 782 | case 'n': |
| 783 | page_size = (unsigned)strtoul(optarg, NULL, 0); |
| 784 | if (!page_size) die("invalid page size"); |
| 785 | break; |
| 786 | case 's': |
| 787 | serial = optarg; |
| 788 | break; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 789 | case 'S': |
| 790 | sparse_limit = parse_num(optarg); |
| 791 | if (sparse_limit < 0) { |
| 792 | die("invalid sparse limit"); |
| 793 | } |
| 794 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 795 | case 'p': |
| 796 | product = optarg; |
| 797 | break; |
| 798 | case 'c': |
| 799 | cmdline = optarg; |
| 800 | break; |
| 801 | case 'i': { |
| 802 | char *endptr = NULL; |
| 803 | unsigned long val; |
| 804 | |
| 805 | val = strtoul(optarg, &endptr, 0); |
| 806 | if (!endptr || *endptr != '\0' || (val & ~0xffff)) |
| 807 | die("invalid vendor id '%s'", optarg); |
| 808 | vendor_id = (unsigned short)val; |
| 809 | break; |
| 810 | } |
| 811 | case 'h': |
| 812 | usage(); |
| 813 | return 1; |
| 814 | case '?': |
| 815 | return 1; |
| 816 | default: |
| 817 | abort(); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | argc -= optind; |
| 822 | argv += optind; |
| 823 | |
| 824 | if (argc == 0 && !wants_wipe) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 825 | usage(); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 826 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 827 | } |
| 828 | |
Colin Cross | 8fb6e06 | 2012-07-24 16:36:41 -0700 | [diff] [blame] | 829 | if (argc > 0 && !strcmp(*argv, "devices")) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 830 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 831 | list_devices(); |
| 832 | return 0; |
| 833 | } |
| 834 | |
Colin Cross | 80f9867 | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 835 | if (argc > 0 && !strcmp(*argv, "help")) { |
| 836 | usage(); |
| 837 | return 0; |
| 838 | } |
| 839 | |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 840 | usb = open_device(); |
Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 841 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 842 | while (argc > 0) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 843 | if(!strcmp(*argv, "getvar")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 844 | require(2); |
| 845 | fb_queue_display(argv[1], argv[1]); |
| 846 | skip(2); |
| 847 | } else if(!strcmp(*argv, "erase")) { |
| 848 | require(2); |
| 849 | fb_queue_erase(argv[1]); |
| 850 | skip(2); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 851 | } else if(!strcmp(*argv, "format")) { |
| 852 | require(2); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 853 | fb_queue_format(argv[1], 0); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 854 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 855 | } else if(!strcmp(*argv, "signature")) { |
| 856 | require(2); |
| 857 | data = load_file(argv[1], &sz); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 858 | if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 859 | if (sz != 256) die("signature must be 256 bytes"); |
| 860 | fb_queue_download("signature", data, sz); |
| 861 | fb_queue_command("signature", "installing signature"); |
| 862 | skip(2); |
| 863 | } else if(!strcmp(*argv, "reboot")) { |
| 864 | wants_reboot = 1; |
| 865 | skip(1); |
| 866 | } else if(!strcmp(*argv, "reboot-bootloader")) { |
| 867 | wants_reboot_bootloader = 1; |
| 868 | skip(1); |
| 869 | } else if (!strcmp(*argv, "continue")) { |
| 870 | fb_queue_command("continue", "resuming boot"); |
| 871 | skip(1); |
| 872 | } else if(!strcmp(*argv, "boot")) { |
| 873 | char *kname = 0; |
| 874 | char *rname = 0; |
| 875 | skip(1); |
| 876 | if (argc > 0) { |
| 877 | kname = argv[0]; |
| 878 | skip(1); |
| 879 | } |
| 880 | if (argc > 0) { |
| 881 | rname = argv[0]; |
| 882 | skip(1); |
| 883 | } |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 884 | data = load_bootable_image(page_size, kname, rname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 885 | if (data == 0) return 1; |
| 886 | fb_queue_download("boot.img", data, sz); |
| 887 | fb_queue_command("boot", "booting"); |
| 888 | } else if(!strcmp(*argv, "flash")) { |
| 889 | char *pname = argv[1]; |
| 890 | char *fname = 0; |
| 891 | require(2); |
| 892 | if (argc > 2) { |
| 893 | fname = argv[2]; |
| 894 | skip(3); |
| 895 | } else { |
| 896 | fname = find_item(pname, product); |
| 897 | skip(2); |
| 898 | } |
| 899 | if (fname == 0) die("cannot determine image filename for '%s'", pname); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 900 | do_flash(usb, pname, fname); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 901 | } else if(!strcmp(*argv, "flash:raw")) { |
| 902 | char *pname = argv[1]; |
| 903 | char *kname = argv[2]; |
| 904 | char *rname = 0; |
| 905 | require(3); |
| 906 | if(argc > 3) { |
| 907 | rname = argv[3]; |
| 908 | skip(4); |
| 909 | } else { |
| 910 | skip(3); |
| 911 | } |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 912 | data = load_bootable_image(page_size, kname, rname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 913 | if (data == 0) die("cannot load bootable image"); |
| 914 | fb_queue_flash(pname, data, sz); |
| 915 | } else if(!strcmp(*argv, "flashall")) { |
| 916 | skip(1); |
| 917 | do_flashall(); |
| 918 | wants_reboot = 1; |
| 919 | } else if(!strcmp(*argv, "update")) { |
| 920 | if (argc > 1) { |
| 921 | do_update(argv[1]); |
| 922 | skip(2); |
| 923 | } else { |
| 924 | do_update("update.zip"); |
| 925 | skip(1); |
| 926 | } |
| 927 | wants_reboot = 1; |
| 928 | } else if(!strcmp(*argv, "oem")) { |
| 929 | argc = do_oem_command(argc, argv); |
| 930 | } else { |
| 931 | usage(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 932 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
| 936 | if (wants_wipe) { |
| 937 | fb_queue_erase("userdata"); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 938 | fb_queue_format("userdata", 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 939 | fb_queue_erase("cache"); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 940 | fb_queue_format("cache", 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 941 | } |
| 942 | if (wants_reboot) { |
| 943 | fb_queue_reboot(); |
| 944 | } else if (wants_reboot_bootloader) { |
| 945 | fb_queue_command("reboot-bootloader", "rebooting into bootloader"); |
| 946 | } |
| 947 | |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 948 | status = fb_execute_queue(usb); |
| 949 | return (status) ? 1 : 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 950 | } |