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 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <string.h> |
| 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
| 36 | #include <limits.h> |
| 37 | #include <ctype.h> |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame^] | 38 | #include <getopt.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | |
| 40 | #include <sys/time.h> |
| 41 | #include <bootimg.h> |
| 42 | #include <zipfile/zipfile.h> |
| 43 | |
| 44 | #include "fastboot.h" |
| 45 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 46 | char cur_product[FB_RESPONSE_SZ + 1]; |
| 47 | |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 48 | void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline); |
| 49 | |
| 50 | boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, |
| 51 | void *ramdisk, unsigned ramdisk_size, |
| 52 | void *second, unsigned second_size, |
| 53 | unsigned page_size, unsigned base, |
| 54 | unsigned *bootimg_size); |
| 55 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | static usb_handle *usb = 0; |
| 57 | static const char *serial = 0; |
| 58 | static const char *product = 0; |
| 59 | static const char *cmdline = 0; |
| 60 | static int wipe_data = 0; |
| 61 | static unsigned short vendor_id = 0; |
| 62 | |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 63 | static unsigned base_addr = 0x10000000; |
| 64 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | void die(const char *fmt, ...) |
| 66 | { |
| 67 | va_list ap; |
| 68 | va_start(ap, fmt); |
| 69 | fprintf(stderr,"error: "); |
| 70 | vfprintf(stderr, fmt, ap); |
| 71 | fprintf(stderr,"\n"); |
| 72 | va_end(ap); |
| 73 | exit(1); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 74 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | |
| 76 | void get_my_path(char *path); |
| 77 | |
| 78 | char *find_item(const char *item, const char *product) |
| 79 | { |
| 80 | char *dir; |
| 81 | char *fn; |
| 82 | char path[PATH_MAX + 128]; |
| 83 | |
| 84 | if(!strcmp(item,"boot")) { |
| 85 | fn = "boot.img"; |
| 86 | } else if(!strcmp(item,"recovery")) { |
| 87 | fn = "recovery.img"; |
| 88 | } else if(!strcmp(item,"system")) { |
| 89 | fn = "system.img"; |
| 90 | } else if(!strcmp(item,"userdata")) { |
| 91 | fn = "userdata.img"; |
Jean-Baptiste Queru | d7608a4 | 2011-09-30 14:39:25 -0700 | [diff] [blame] | 92 | } else if(!strcmp(item,"cache")) { |
| 93 | fn = "cache.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | } else if(!strcmp(item,"info")) { |
| 95 | fn = "android-info.txt"; |
| 96 | } else { |
| 97 | fprintf(stderr,"unknown partition '%s'\n", item); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | if(product) { |
| 102 | get_my_path(path); |
| 103 | sprintf(path + strlen(path), |
| 104 | "../../../target/product/%s/%s", product, fn); |
| 105 | return strdup(path); |
| 106 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 107 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | dir = getenv("ANDROID_PRODUCT_OUT"); |
| 109 | if((dir == 0) || (dir[0] == 0)) { |
| 110 | die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); |
| 111 | return 0; |
| 112 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 113 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 | sprintf(path, "%s/%s", dir, fn); |
| 115 | return strdup(path); |
| 116 | } |
| 117 | |
| 118 | #ifdef _WIN32 |
| 119 | void *load_file(const char *fn, unsigned *_sz); |
| 120 | #else |
| 121 | void *load_file(const char *fn, unsigned *_sz) |
| 122 | { |
| 123 | char *data; |
| 124 | int sz; |
| 125 | int fd; |
| 126 | |
| 127 | data = 0; |
| 128 | fd = open(fn, O_RDONLY); |
| 129 | if(fd < 0) return 0; |
| 130 | |
| 131 | sz = lseek(fd, 0, SEEK_END); |
| 132 | if(sz < 0) goto oops; |
| 133 | |
| 134 | if(lseek(fd, 0, SEEK_SET) != 0) goto oops; |
| 135 | |
| 136 | data = (char*) malloc(sz); |
| 137 | if(data == 0) goto oops; |
| 138 | |
| 139 | if(read(fd, data, sz) != sz) goto oops; |
| 140 | close(fd); |
| 141 | |
| 142 | if(_sz) *_sz = sz; |
| 143 | return data; |
| 144 | |
| 145 | oops: |
| 146 | close(fd); |
| 147 | if(data != 0) free(data); |
| 148 | return 0; |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | int match_fastboot(usb_ifc_info *info) |
| 153 | { |
| 154 | if(!(vendor_id && (info->dev_vendor == vendor_id)) && |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 155 | (info->dev_vendor != 0x18d1) && // Google |
Wu, Hao | f60e863 | 2012-01-17 12:04:11 -0800 | [diff] [blame] | 156 | (info->dev_vendor != 0x8087) && // Intel |
The Android Open Source Project | f614d64 | 2009-03-18 17:39:49 -0700 | [diff] [blame] | 157 | (info->dev_vendor != 0x0451) && |
Robert CH Chou | e25ff1c | 2009-09-21 09:51:35 +0800 | [diff] [blame] | 158 | (info->dev_vendor != 0x0502) && |
Dima Zavin | 509f739 | 2010-05-14 14:48:30 -0700 | [diff] [blame] | 159 | (info->dev_vendor != 0x0fce) && // Sony Ericsson |
| 160 | (info->dev_vendor != 0x05c6) && // Qualcomm |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 161 | (info->dev_vendor != 0x22b8) && // Motorola |
Erik Gilling | 37e9e90 | 2010-01-20 17:40:05 -0800 | [diff] [blame] | 162 | (info->dev_vendor != 0x0955) && // Nvidia |
Xavier Ducrohet | af82f21 | 2010-01-21 17:39:25 -0800 | [diff] [blame] | 163 | (info->dev_vendor != 0x413c) && // DELL |
Xavier Ducrohet | 746f324 | 2012-01-13 16:03:37 -0800 | [diff] [blame] | 164 | (info->dev_vendor != 0x2314) && // INQ Mobile |
Ramanan Rajeswaran | 73c019b | 2012-02-24 13:00:34 -0800 | [diff] [blame] | 165 | (info->dev_vendor != 0x0b05) && // Asus |
Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 166 | (info->dev_vendor != 0x0bb4)) // HTC |
| 167 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 168 | if(info->ifc_class != 0xff) return -1; |
| 169 | if(info->ifc_subclass != 0x42) return -1; |
| 170 | if(info->ifc_protocol != 0x03) return -1; |
| 171 | // require matching serial number if a serial number is specified |
| 172 | // at the command line with the -s option. |
| 173 | if (serial && strcmp(serial, info->serial_number) != 0) return -1; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int list_devices_callback(usb_ifc_info *info) |
| 178 | { |
| 179 | if (match_fastboot(info) == 0) { |
| 180 | char* serial = info->serial_number; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 181 | if (!info->writable) { |
| 182 | serial = "no permissions"; // like "adb devices" |
| 183 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | if (!serial[0]) { |
| 185 | serial = "????????????"; |
| 186 | } |
| 187 | // output compatible with "adb devices" |
| 188 | printf("%s\tfastboot\n", serial); |
| 189 | } |
| 190 | |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | usb_handle *open_device(void) |
| 195 | { |
| 196 | static usb_handle *usb = 0; |
| 197 | int announce = 1; |
| 198 | |
| 199 | if(usb) return usb; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 200 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | for(;;) { |
| 202 | usb = usb_open(match_fastboot); |
| 203 | if(usb) return usb; |
| 204 | if(announce) { |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 205 | announce = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | fprintf(stderr,"< waiting for device >\n"); |
| 207 | } |
| 208 | sleep(1); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void list_devices(void) { |
| 213 | // We don't actually open a USB device here, |
| 214 | // just getting our callback called so we can |
| 215 | // list all the connected devices. |
| 216 | usb_open(list_devices_callback); |
| 217 | } |
| 218 | |
| 219 | void usage(void) |
| 220 | { |
| 221 | fprintf(stderr, |
| 222 | /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ |
| 223 | "usage: fastboot [ <option> ] <command>\n" |
| 224 | "\n" |
| 225 | "commands:\n" |
| 226 | " update <filename> reflash device from update.zip\n" |
| 227 | " flashall flash boot + recovery + system\n" |
| 228 | " flash <partition> [ <filename> ] write a file to a flash partition\n" |
| 229 | " erase <partition> erase a flash partition\n" |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 230 | " format <partition> format a flash partition \n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | " getvar <variable> display a bootloader variable\n" |
| 232 | " boot <kernel> [ <ramdisk> ] download and boot kernel\n" |
| 233 | " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n" |
| 234 | " devices list all connected devices\n" |
Bruce Beare | 24ce4bc | 2010-10-14 09:43:26 -0700 | [diff] [blame] | 235 | " continue continue with autoboot\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | " reboot reboot device normally\n" |
| 237 | " reboot-bootloader reboot device into bootloader\n" |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 238 | " help show this help message\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | "\n" |
| 240 | "options:\n" |
| 241 | " -w erase userdata and cache\n" |
| 242 | " -s <serial number> specify device serial number\n" |
| 243 | " -p <product> specify product name\n" |
| 244 | " -c <cmdline> override kernel commandline\n" |
| 245 | " -i <vendor id> specify a custom USB vendor id\n" |
Dima Zavin | 95ec983 | 2009-04-30 15:03:05 -0700 | [diff] [blame] | 246 | " -b <base_addr> specify a custom kernel base address\n" |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 247 | " -n <page size> specify the nand page size. default: 2048\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 248 | ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 251 | 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] | 252 | unsigned *sz, const char *cmdline) |
| 253 | { |
| 254 | void *kdata = 0, *rdata = 0; |
| 255 | unsigned ksize = 0, rsize = 0; |
| 256 | void *bdata; |
| 257 | unsigned bsize; |
| 258 | |
| 259 | if(kernel == 0) { |
| 260 | fprintf(stderr, "no image specified\n"); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | kdata = load_file(kernel, &ksize); |
| 265 | if(kdata == 0) { |
| 266 | fprintf(stderr, "cannot load '%s'\n", kernel); |
| 267 | return 0; |
| 268 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 269 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | /* is this actually a boot image? */ |
| 271 | if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
| 272 | if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 273 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 274 | if(ramdisk) { |
| 275 | fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); |
| 276 | return 0; |
| 277 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 278 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 279 | *sz = ksize; |
| 280 | return kdata; |
| 281 | } |
| 282 | |
| 283 | if(ramdisk) { |
| 284 | rdata = load_file(ramdisk, &rsize); |
| 285 | if(rdata == 0) { |
| 286 | fprintf(stderr,"cannot load '%s'\n", ramdisk); |
| 287 | return 0; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | fprintf(stderr,"creating boot image...\n"); |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 292 | 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] | 293 | if(bdata == 0) { |
| 294 | fprintf(stderr,"failed to create boot.img\n"); |
| 295 | return 0; |
| 296 | } |
| 297 | if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); |
| 298 | fprintf(stderr,"creating boot image - %d bytes\n", bsize); |
| 299 | *sz = bsize; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 300 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 301 | return bdata; |
| 302 | } |
| 303 | |
| 304 | void *unzip_file(zipfile_t zip, const char *name, unsigned *sz) |
| 305 | { |
| 306 | void *data; |
| 307 | zipentry_t entry; |
| 308 | unsigned datasz; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 309 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 310 | entry = lookup_zipentry(zip, name); |
| 311 | if (entry == NULL) { |
| 312 | fprintf(stderr, "archive does not contain '%s'\n", name); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | *sz = get_zipentry_size(entry); |
| 317 | |
| 318 | datasz = *sz * 1.001; |
| 319 | data = malloc(datasz); |
| 320 | |
| 321 | if(data == 0) { |
| 322 | fprintf(stderr, "failed to allocate %d bytes\n", *sz); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | if (decompress_zipentry(entry, data, datasz)) { |
| 327 | fprintf(stderr, "failed to unzip '%s' from archive\n", name); |
| 328 | free(data); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | return data; |
| 333 | } |
| 334 | |
| 335 | static char *strip(char *s) |
| 336 | { |
| 337 | int n; |
| 338 | while(*s && isspace(*s)) s++; |
| 339 | n = strlen(s); |
| 340 | while(n-- > 0) { |
| 341 | if(!isspace(s[n])) break; |
| 342 | s[n] = 0; |
| 343 | } |
| 344 | return s; |
| 345 | } |
| 346 | |
| 347 | #define MAX_OPTIONS 32 |
| 348 | static int setup_requirement_line(char *name) |
| 349 | { |
| 350 | char *val[MAX_OPTIONS]; |
| 351 | const char **out; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 352 | char *prod = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | unsigned n, count; |
| 354 | char *x; |
| 355 | int invert = 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 356 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | if (!strncmp(name, "reject ", 7)) { |
| 358 | name += 7; |
| 359 | invert = 1; |
| 360 | } else if (!strncmp(name, "require ", 8)) { |
| 361 | name += 8; |
| 362 | invert = 0; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 363 | } else if (!strncmp(name, "require-for-product:", 20)) { |
| 364 | // Get the product and point name past it |
| 365 | prod = name + 20; |
| 366 | name = strchr(name, ' '); |
| 367 | if (!name) return -1; |
| 368 | *name = 0; |
| 369 | name += 1; |
| 370 | invert = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | x = strchr(name, '='); |
| 374 | if (x == 0) return 0; |
| 375 | *x = 0; |
| 376 | val[0] = x + 1; |
| 377 | |
| 378 | for(count = 1; count < MAX_OPTIONS; count++) { |
| 379 | x = strchr(val[count - 1],'|'); |
| 380 | if (x == 0) break; |
| 381 | *x = 0; |
| 382 | val[count] = x + 1; |
| 383 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 384 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | name = strip(name); |
| 386 | for(n = 0; n < count; n++) val[n] = strip(val[n]); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 387 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 388 | name = strip(name); |
| 389 | if (name == 0) return -1; |
| 390 | |
| 391 | /* work around an unfortunate name mismatch */ |
| 392 | if (!strcmp(name,"board")) name = "product"; |
| 393 | |
| 394 | out = malloc(sizeof(char*) * count); |
| 395 | if (out == 0) return -1; |
| 396 | |
| 397 | for(n = 0; n < count; n++) { |
| 398 | out[n] = strdup(strip(val[n])); |
| 399 | if (out[n] == 0) return -1; |
| 400 | } |
| 401 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 402 | fb_queue_require(prod, name, invert, n, out); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static void setup_requirements(char *data, unsigned sz) |
| 407 | { |
| 408 | char *s; |
| 409 | |
| 410 | s = data; |
| 411 | while (sz-- > 0) { |
| 412 | if(*s == '\n') { |
| 413 | *s++ = 0; |
| 414 | if (setup_requirement_line(data)) { |
| 415 | die("out of memory"); |
| 416 | } |
| 417 | data = s; |
| 418 | } else { |
| 419 | s++; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void queue_info_dump(void) |
| 425 | { |
| 426 | fb_queue_notice("--------------------------------------------"); |
| 427 | fb_queue_display("version-bootloader", "Bootloader Version..."); |
| 428 | fb_queue_display("version-baseband", "Baseband Version....."); |
| 429 | fb_queue_display("serialno", "Serial Number........"); |
| 430 | fb_queue_notice("--------------------------------------------"); |
| 431 | } |
| 432 | |
| 433 | void do_update_signature(zipfile_t zip, char *fn) |
| 434 | { |
| 435 | void *data; |
| 436 | unsigned sz; |
| 437 | data = unzip_file(zip, fn, &sz); |
| 438 | if (data == 0) return; |
| 439 | fb_queue_download("signature", data, sz); |
| 440 | fb_queue_command("signature", "installing signature"); |
| 441 | } |
| 442 | |
| 443 | void do_update(char *fn) |
| 444 | { |
| 445 | void *zdata; |
| 446 | unsigned zsize; |
| 447 | void *data; |
| 448 | unsigned sz; |
| 449 | zipfile_t zip; |
| 450 | |
| 451 | queue_info_dump(); |
| 452 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 453 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 454 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | zdata = load_file(fn, &zsize); |
| 456 | if (zdata == 0) die("failed to load '%s'", fn); |
| 457 | |
| 458 | zip = init_zipfile(zdata, zsize); |
| 459 | if(zip == 0) die("failed to access zipdata in '%s'"); |
| 460 | |
| 461 | data = unzip_file(zip, "android-info.txt", &sz); |
| 462 | if (data == 0) { |
| 463 | char *tmp; |
| 464 | /* fallback for older zipfiles */ |
| 465 | data = unzip_file(zip, "android-product.txt", &sz); |
| 466 | if ((data == 0) || (sz < 1)) { |
| 467 | die("update package has no android-info.txt or android-product.txt"); |
| 468 | } |
| 469 | tmp = malloc(sz + 128); |
| 470 | if (tmp == 0) die("out of memory"); |
| 471 | sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data); |
| 472 | data = tmp; |
| 473 | sz = strlen(tmp); |
| 474 | } |
| 475 | |
| 476 | setup_requirements(data, sz); |
| 477 | |
| 478 | data = unzip_file(zip, "boot.img", &sz); |
| 479 | if (data == 0) die("update package missing boot.img"); |
| 480 | do_update_signature(zip, "boot.sig"); |
| 481 | fb_queue_flash("boot", data, sz); |
| 482 | |
| 483 | data = unzip_file(zip, "recovery.img", &sz); |
| 484 | if (data != 0) { |
| 485 | do_update_signature(zip, "recovery.sig"); |
| 486 | fb_queue_flash("recovery", data, sz); |
| 487 | } |
| 488 | |
| 489 | data = unzip_file(zip, "system.img", &sz); |
| 490 | if (data == 0) die("update package missing system.img"); |
| 491 | do_update_signature(zip, "system.sig"); |
| 492 | fb_queue_flash("system", data, sz); |
| 493 | } |
| 494 | |
| 495 | void do_send_signature(char *fn) |
| 496 | { |
| 497 | void *data; |
| 498 | unsigned sz; |
| 499 | char *xtn; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 500 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 501 | xtn = strrchr(fn, '.'); |
| 502 | if (!xtn) return; |
| 503 | if (strcmp(xtn, ".img")) return; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 504 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 505 | strcpy(xtn,".sig"); |
| 506 | data = load_file(fn, &sz); |
| 507 | strcpy(xtn,".img"); |
| 508 | if (data == 0) return; |
| 509 | fb_queue_download("signature", data, sz); |
| 510 | fb_queue_command("signature", "installing signature"); |
| 511 | } |
| 512 | |
| 513 | void do_flashall(void) |
| 514 | { |
| 515 | char *fname; |
| 516 | void *data; |
| 517 | unsigned sz; |
| 518 | |
| 519 | queue_info_dump(); |
| 520 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 521 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 522 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | fname = find_item("info", product); |
| 524 | if (fname == 0) die("cannot find android-info.txt"); |
| 525 | data = load_file(fname, &sz); |
| 526 | if (data == 0) die("could not load android-info.txt"); |
| 527 | setup_requirements(data, sz); |
| 528 | |
| 529 | fname = find_item("boot", product); |
| 530 | data = load_file(fname, &sz); |
| 531 | if (data == 0) die("could not load boot.img"); |
| 532 | do_send_signature(fname); |
| 533 | fb_queue_flash("boot", data, sz); |
| 534 | |
| 535 | fname = find_item("recovery", product); |
| 536 | data = load_file(fname, &sz); |
| 537 | if (data != 0) { |
| 538 | do_send_signature(fname); |
| 539 | fb_queue_flash("recovery", data, sz); |
| 540 | } |
| 541 | |
| 542 | fname = find_item("system", product); |
| 543 | data = load_file(fname, &sz); |
| 544 | if (data == 0) die("could not load system.img"); |
| 545 | do_send_signature(fname); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 546 | fb_queue_flash("system", data, sz); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | #define skip(n) do { argc -= (n); argv += (n); } while (0) |
JP Abgrall | 2d13d14 | 2011-03-01 23:35:07 -0800 | [diff] [blame] | 550 | #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] | 551 | |
| 552 | int do_oem_command(int argc, char **argv) |
| 553 | { |
| 554 | int i; |
| 555 | char command[256]; |
| 556 | if (argc <= 1) return 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 557 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 558 | command[0] = 0; |
| 559 | while(1) { |
| 560 | strcat(command,*argv); |
| 561 | skip(1); |
| 562 | if(argc == 0) break; |
| 563 | strcat(command," "); |
| 564 | } |
| 565 | |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 566 | fb_queue_command(command,""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | int main(int argc, char **argv) |
| 571 | { |
| 572 | int wants_wipe = 0; |
| 573 | int wants_reboot = 0; |
| 574 | int wants_reboot_bootloader = 0; |
| 575 | void *data; |
| 576 | unsigned sz; |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 577 | unsigned page_size = 2048; |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 578 | int status; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame^] | 579 | int c; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame^] | 581 | struct option longopts = { 0, 0, 0, 0 }; |
| 582 | |
| 583 | serial = getenv("ANDROID_SERIAL"); |
| 584 | |
| 585 | while (1) { |
| 586 | c = getopt_long(argc, argv, "wb:n:s:p:c:i:h", &longopts, NULL); |
| 587 | if (c < 0) { |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | switch (c) { |
| 592 | case 'w': |
| 593 | wants_wipe = 1; |
| 594 | break; |
| 595 | case 'b': |
| 596 | base_addr = strtoul(optarg, 0, 16); |
| 597 | break; |
| 598 | case 'n': |
| 599 | page_size = (unsigned)strtoul(optarg, NULL, 0); |
| 600 | if (!page_size) die("invalid page size"); |
| 601 | break; |
| 602 | case 's': |
| 603 | serial = optarg; |
| 604 | break; |
| 605 | case 'p': |
| 606 | product = optarg; |
| 607 | break; |
| 608 | case 'c': |
| 609 | cmdline = optarg; |
| 610 | break; |
| 611 | case 'i': { |
| 612 | char *endptr = NULL; |
| 613 | unsigned long val; |
| 614 | |
| 615 | val = strtoul(optarg, &endptr, 0); |
| 616 | if (!endptr || *endptr != '\0' || (val & ~0xffff)) |
| 617 | die("invalid vendor id '%s'", optarg); |
| 618 | vendor_id = (unsigned short)val; |
| 619 | break; |
| 620 | } |
| 621 | case 'h': |
| 622 | usage(); |
| 623 | return 1; |
| 624 | case '?': |
| 625 | return 1; |
| 626 | default: |
| 627 | abort(); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | argc -= optind; |
| 632 | argv += optind; |
| 633 | |
| 634 | if (argc == 0 && !wants_wipe) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 635 | usage(); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 636 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | if (!strcmp(*argv, "devices")) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame^] | 640 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 641 | list_devices(); |
| 642 | return 0; |
| 643 | } |
| 644 | |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame^] | 645 | usb = open_device(); |
Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 646 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 647 | while (argc > 0) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame^] | 648 | if(!strcmp(*argv, "getvar")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 649 | require(2); |
| 650 | fb_queue_display(argv[1], argv[1]); |
| 651 | skip(2); |
| 652 | } else if(!strcmp(*argv, "erase")) { |
| 653 | require(2); |
| 654 | fb_queue_erase(argv[1]); |
| 655 | skip(2); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 656 | } else if(!strcmp(*argv, "format")) { |
| 657 | require(2); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 658 | fb_queue_format(argv[1], 0); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 659 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 660 | } else if(!strcmp(*argv, "signature")) { |
| 661 | require(2); |
| 662 | data = load_file(argv[1], &sz); |
| 663 | if (data == 0) die("could not load '%s'", argv[1]); |
| 664 | if (sz != 256) die("signature must be 256 bytes"); |
| 665 | fb_queue_download("signature", data, sz); |
| 666 | fb_queue_command("signature", "installing signature"); |
| 667 | skip(2); |
| 668 | } else if(!strcmp(*argv, "reboot")) { |
| 669 | wants_reboot = 1; |
| 670 | skip(1); |
| 671 | } else if(!strcmp(*argv, "reboot-bootloader")) { |
| 672 | wants_reboot_bootloader = 1; |
| 673 | skip(1); |
| 674 | } else if (!strcmp(*argv, "continue")) { |
| 675 | fb_queue_command("continue", "resuming boot"); |
| 676 | skip(1); |
| 677 | } else if(!strcmp(*argv, "boot")) { |
| 678 | char *kname = 0; |
| 679 | char *rname = 0; |
| 680 | skip(1); |
| 681 | if (argc > 0) { |
| 682 | kname = argv[0]; |
| 683 | skip(1); |
| 684 | } |
| 685 | if (argc > 0) { |
| 686 | rname = argv[0]; |
| 687 | skip(1); |
| 688 | } |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 689 | 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] | 690 | if (data == 0) return 1; |
| 691 | fb_queue_download("boot.img", data, sz); |
| 692 | fb_queue_command("boot", "booting"); |
| 693 | } else if(!strcmp(*argv, "flash")) { |
| 694 | char *pname = argv[1]; |
| 695 | char *fname = 0; |
| 696 | require(2); |
| 697 | if (argc > 2) { |
| 698 | fname = argv[2]; |
| 699 | skip(3); |
| 700 | } else { |
| 701 | fname = find_item(pname, product); |
| 702 | skip(2); |
| 703 | } |
| 704 | if (fname == 0) die("cannot determine image filename for '%s'", pname); |
| 705 | data = load_file(fname, &sz); |
| 706 | if (data == 0) die("cannot load '%s'\n", fname); |
| 707 | fb_queue_flash(pname, data, sz); |
| 708 | } else if(!strcmp(*argv, "flash:raw")) { |
| 709 | char *pname = argv[1]; |
| 710 | char *kname = argv[2]; |
| 711 | char *rname = 0; |
| 712 | require(3); |
| 713 | if(argc > 3) { |
| 714 | rname = argv[3]; |
| 715 | skip(4); |
| 716 | } else { |
| 717 | skip(3); |
| 718 | } |
Dima Zavin | 931175a | 2010-02-12 20:26:33 -0800 | [diff] [blame] | 719 | 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] | 720 | if (data == 0) die("cannot load bootable image"); |
| 721 | fb_queue_flash(pname, data, sz); |
| 722 | } else if(!strcmp(*argv, "flashall")) { |
| 723 | skip(1); |
| 724 | do_flashall(); |
| 725 | wants_reboot = 1; |
| 726 | } else if(!strcmp(*argv, "update")) { |
| 727 | if (argc > 1) { |
| 728 | do_update(argv[1]); |
| 729 | skip(2); |
| 730 | } else { |
| 731 | do_update("update.zip"); |
| 732 | skip(1); |
| 733 | } |
| 734 | wants_reboot = 1; |
| 735 | } else if(!strcmp(*argv, "oem")) { |
| 736 | argc = do_oem_command(argc, argv); |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame^] | 737 | } else if (!strcmp(*argv, "help")) { |
| 738 | usage(); |
| 739 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 740 | } else { |
| 741 | usage(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 742 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | if (wants_wipe) { |
| 747 | fb_queue_erase("userdata"); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 748 | fb_queue_format("userdata", 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 749 | fb_queue_erase("cache"); |
JP Abgrall | 30ae580 | 2012-05-07 20:25:24 -0700 | [diff] [blame] | 750 | fb_queue_format("cache", 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 751 | } |
| 752 | if (wants_reboot) { |
| 753 | fb_queue_reboot(); |
| 754 | } else if (wants_reboot_bootloader) { |
| 755 | fb_queue_command("reboot-bootloader", "rebooting into bootloader"); |
| 756 | } |
| 757 | |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 758 | status = fb_execute_queue(usb); |
| 759 | return (status) ? 1 : 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 760 | } |