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