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