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