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