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