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