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