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