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