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