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