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