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