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