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