blob: 64a4045c57fce9107502ed7984ba9cabf348e04c [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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
45static usb_handle *usb = 0;
46static const char *serial = 0;
47static const char *product = 0;
48static const char *cmdline = 0;
49static int wipe_data = 0;
50
51void die(const char *fmt, ...)
52{
53 va_list ap;
54 va_start(ap, fmt);
55 fprintf(stderr,"error: ");
56 vfprintf(stderr, fmt, ap);
57 fprintf(stderr,"\n");
58 va_end(ap);
59 exit(1);
60}
61
62void get_my_path(char *path);
63
64char *find_item(const char *item, const char *product)
65{
66 char *dir;
67 char *fn;
68 char path[PATH_MAX + 128];
69
70 if(!strcmp(item,"boot")) {
71 fn = "boot.img";
72 } else if(!strcmp(item,"recovery")) {
73 fn = "recovery.img";
74 } else if(!strcmp(item,"system")) {
75 fn = "system.img";
76 } else if(!strcmp(item,"userdata")) {
77 fn = "userdata.img";
78 } else if(!strcmp(item,"info")) {
79 fn = "android-info.txt";
80 } else {
81 fprintf(stderr,"unknown partition '%s'\n", item);
82 return 0;
83 }
84
85 if(product) {
86 get_my_path(path);
87 sprintf(path + strlen(path),
88 "../../../target/product/%s/%s", product, fn);
89 return strdup(path);
90 }
91
92 dir = getenv("ANDROID_PRODUCT_OUT");
93 if((dir == 0) || (dir[0] == 0)) {
94 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
95 return 0;
96 }
97
98 sprintf(path, "%s/%s", dir, fn);
99 return strdup(path);
100}
101
102#ifdef _WIN32
103void *load_file(const char *fn, unsigned *_sz);
104#else
105void *load_file(const char *fn, unsigned *_sz)
106{
107 char *data;
108 int sz;
109 int fd;
110
111 data = 0;
112 fd = open(fn, O_RDONLY);
113 if(fd < 0) return 0;
114
115 sz = lseek(fd, 0, SEEK_END);
116 if(sz < 0) goto oops;
117
118 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
119
120 data = (char*) malloc(sz);
121 if(data == 0) goto oops;
122
123 if(read(fd, data, sz) != sz) goto oops;
124 close(fd);
125
126 if(_sz) *_sz = sz;
127 return data;
128
129oops:
130 close(fd);
131 if(data != 0) free(data);
132 return 0;
133}
134#endif
135
136int match_fastboot(usb_ifc_info *info)
137{
138 if((info->dev_vendor != 0x18d1) &&
139 (info->dev_vendor != 0x0bb4)) return -1;
140 if(info->ifc_class != 0xff) return -1;
141 if(info->ifc_subclass != 0x42) return -1;
142 if(info->ifc_protocol != 0x03) return -1;
143 // require matching serial number if a serial number is specified
144 // at the command line with the -s option.
145 if (serial && strcmp(serial, info->serial_number) != 0) return -1;
146 return 0;
147}
148
149int list_devices_callback(usb_ifc_info *info)
150{
151 if (match_fastboot(info) == 0) {
152 char* serial = info->serial_number;
153 if (!serial[0]) {
154 serial = "????????????";
155 }
156 // output compatible with "adb devices"
157 printf("%s\tfastboot\n", serial);
158 }
159
160 return -1;
161}
162
163usb_handle *open_device(void)
164{
165 static usb_handle *usb = 0;
166 int announce = 1;
167
168 if(usb) return usb;
169
170 for(;;) {
171 usb = usb_open(match_fastboot);
172 if(usb) return usb;
173 if(announce) {
174 announce = 0;
175 fprintf(stderr,"< waiting for device >\n");
176 }
177 sleep(1);
178 }
179}
180
181void list_devices(void) {
182 // We don't actually open a USB device here,
183 // just getting our callback called so we can
184 // list all the connected devices.
185 usb_open(list_devices_callback);
186}
187
188void usage(void)
189{
190 fprintf(stderr,
191/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
192 "usage: fastboot [ <option> ] <command>\n"
193 "\n"
194 "commands:\n"
195 " update <filename> reflash device from update.zip\n"
196 " flashall 'flash boot' + 'flash system'\n"
197 " flash <partition> [ <filename> ] write a file to a flash partition\n"
198 " erase <partition> erase a flash partition\n"
199 " getvar <variable> display a bootloader variable\n"
200 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
201 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
202 " devices list all connected devices\n"
203 " reboot reboot device normally\n"
204 " reboot-bootloader reboot device into bootloader\n"
205 "\n"
206 "options:\n"
207 " -w erase userdata and cache\n"
208 " -s <serial number> specify device serial number\n"
209 " -p <product> specify product name\n"
210 " -c <cmdline> override kernel commandline\n"
211 );
212 exit(1);
213}
214
215void *load_bootable_image(const char *kernel, const char *ramdisk,
216 unsigned *sz, const char *cmdline)
217{
218 void *kdata = 0, *rdata = 0;
219 unsigned ksize = 0, rsize = 0;
220 void *bdata;
221 unsigned bsize;
222
223 if(kernel == 0) {
224 fprintf(stderr, "no image specified\n");
225 return 0;
226 }
227
228 kdata = load_file(kernel, &ksize);
229 if(kdata == 0) {
230 fprintf(stderr, "cannot load '%s'\n", kernel);
231 return 0;
232 }
233
234 /* is this actually a boot image? */
235 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
236 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
237
238 if(ramdisk) {
239 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
240 return 0;
241 }
242
243 *sz = ksize;
244 return kdata;
245 }
246
247 if(ramdisk) {
248 rdata = load_file(ramdisk, &rsize);
249 if(rdata == 0) {
250 fprintf(stderr,"cannot load '%s'\n", ramdisk);
251 return 0;
252 }
253 }
254
255 fprintf(stderr,"creating boot image...\n");
256 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, 2048, &bsize);
257 if(bdata == 0) {
258 fprintf(stderr,"failed to create boot.img\n");
259 return 0;
260 }
261 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
262 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
263 *sz = bsize;
264
265 return bdata;
266}
267
268void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
269{
270 void *data;
271 zipentry_t entry;
272 unsigned datasz;
273
274 entry = lookup_zipentry(zip, name);
275 if (entry == NULL) {
276 fprintf(stderr, "archive does not contain '%s'\n", name);
277 return 0;
278 }
279
280 *sz = get_zipentry_size(entry);
281
282 datasz = *sz * 1.001;
283 data = malloc(datasz);
284
285 if(data == 0) {
286 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
287 return 0;
288 }
289
290 if (decompress_zipentry(entry, data, datasz)) {
291 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
292 free(data);
293 return 0;
294 }
295
296 return data;
297}
298
299static char *strip(char *s)
300{
301 int n;
302 while(*s && isspace(*s)) s++;
303 n = strlen(s);
304 while(n-- > 0) {
305 if(!isspace(s[n])) break;
306 s[n] = 0;
307 }
308 return s;
309}
310
311#define MAX_OPTIONS 32
312static int setup_requirement_line(char *name)
313{
314 char *val[MAX_OPTIONS];
315 const char **out;
316 unsigned n, count;
317 char *x;
318 int invert = 0;
319
320 if (!strncmp(name, "reject ", 7)) {
321 name += 7;
322 invert = 1;
323 } else if (!strncmp(name, "require ", 8)) {
324 name += 8;
325 invert = 0;
326 }
327
328 x = strchr(name, '=');
329 if (x == 0) return 0;
330 *x = 0;
331 val[0] = x + 1;
332
333 for(count = 1; count < MAX_OPTIONS; count++) {
334 x = strchr(val[count - 1],'|');
335 if (x == 0) break;
336 *x = 0;
337 val[count] = x + 1;
338 }
339
340 name = strip(name);
341 for(n = 0; n < count; n++) val[n] = strip(val[n]);
342
343 name = strip(name);
344 if (name == 0) return -1;
345
346 /* work around an unfortunate name mismatch */
347 if (!strcmp(name,"board")) name = "product";
348
349 out = malloc(sizeof(char*) * count);
350 if (out == 0) return -1;
351
352 for(n = 0; n < count; n++) {
353 out[n] = strdup(strip(val[n]));
354 if (out[n] == 0) return -1;
355 }
356
357 fb_queue_require(name, invert, n, out);
358 return 0;
359}
360
361static void setup_requirements(char *data, unsigned sz)
362{
363 char *s;
364
365 s = data;
366 while (sz-- > 0) {
367 if(*s == '\n') {
368 *s++ = 0;
369 if (setup_requirement_line(data)) {
370 die("out of memory");
371 }
372 data = s;
373 } else {
374 s++;
375 }
376 }
377}
378
379void queue_info_dump(void)
380{
381 fb_queue_notice("--------------------------------------------");
382 fb_queue_display("version-bootloader", "Bootloader Version...");
383 fb_queue_display("version-baseband", "Baseband Version.....");
384 fb_queue_display("serialno", "Serial Number........");
385 fb_queue_notice("--------------------------------------------");
386}
387
388void do_update_signature(zipfile_t zip, char *fn)
389{
390 void *data;
391 unsigned sz;
392 data = unzip_file(zip, fn, &sz);
393 if (data == 0) return;
394 fb_queue_download("signature", data, sz);
395 fb_queue_command("signature", "installing signature");
396}
397
398void do_update(char *fn)
399{
400 void *zdata;
401 unsigned zsize;
402 void *data;
403 unsigned sz;
404 zipfile_t zip;
405
406 queue_info_dump();
407
408 zdata = load_file(fn, &zsize);
409 if (zdata == 0) die("failed to load '%s'", fn);
410
411 zip = init_zipfile(zdata, zsize);
412 if(zip == 0) die("failed to access zipdata in '%s'");
413
414 data = unzip_file(zip, "android-info.txt", &sz);
415 if (data == 0) {
416 char *tmp;
417 /* fallback for older zipfiles */
418 data = unzip_file(zip, "android-product.txt", &sz);
419 if ((data == 0) || (sz < 1)) {
420 die("update package has no android-info.txt or android-product.txt");
421 }
422 tmp = malloc(sz + 128);
423 if (tmp == 0) die("out of memory");
424 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
425 data = tmp;
426 sz = strlen(tmp);
427 }
428
429 setup_requirements(data, sz);
430
431 data = unzip_file(zip, "boot.img", &sz);
432 if (data == 0) die("update package missing boot.img");
433 do_update_signature(zip, "boot.sig");
434 fb_queue_flash("boot", data, sz);
435
436 data = unzip_file(zip, "recovery.img", &sz);
437 if (data != 0) {
438 do_update_signature(zip, "recovery.sig");
439 fb_queue_flash("recovery", data, sz);
440 }
441
442 data = unzip_file(zip, "system.img", &sz);
443 if (data == 0) die("update package missing system.img");
444 do_update_signature(zip, "system.sig");
445 fb_queue_flash("system", data, sz);
446}
447
448void do_send_signature(char *fn)
449{
450 void *data;
451 unsigned sz;
452 char *xtn;
453
454 xtn = strrchr(fn, '.');
455 if (!xtn) return;
456 if (strcmp(xtn, ".img")) return;
457
458 strcpy(xtn,".sig");
459 data = load_file(fn, &sz);
460 strcpy(xtn,".img");
461 if (data == 0) return;
462 fb_queue_download("signature", data, sz);
463 fb_queue_command("signature", "installing signature");
464}
465
466void do_flashall(void)
467{
468 char *fname;
469 void *data;
470 unsigned sz;
471
472 queue_info_dump();
473
474 fname = find_item("info", product);
475 if (fname == 0) die("cannot find android-info.txt");
476 data = load_file(fname, &sz);
477 if (data == 0) die("could not load android-info.txt");
478 setup_requirements(data, sz);
479
480 fname = find_item("boot", product);
481 data = load_file(fname, &sz);
482 if (data == 0) die("could not load boot.img");
483 do_send_signature(fname);
484 fb_queue_flash("boot", data, sz);
485
486 fname = find_item("recovery", product);
487 data = load_file(fname, &sz);
488 if (data != 0) {
489 do_send_signature(fname);
490 fb_queue_flash("recovery", data, sz);
491 }
492
493 fname = find_item("system", product);
494 data = load_file(fname, &sz);
495 if (data == 0) die("could not load system.img");
496 do_send_signature(fname);
497 fb_queue_flash("system", data, sz);
498}
499
500#define skip(n) do { argc -= (n); argv += (n); } while (0)
501#define require(n) do { if (argc < (n)) usage(); } while (0)
502
503int do_oem_command(int argc, char **argv)
504{
505 int i;
506 char command[256];
507 if (argc <= 1) return 0;
508
509 command[0] = 0;
510 while(1) {
511 strcat(command,*argv);
512 skip(1);
513 if(argc == 0) break;
514 strcat(command," ");
515 }
516
517 fb_queue_command(command,"");
518 return 0;
519}
520
521int main(int argc, char **argv)
522{
523 int wants_wipe = 0;
524 int wants_reboot = 0;
525 int wants_reboot_bootloader = 0;
526 void *data;
527 unsigned sz;
528
529 skip(1);
530 if (argc == 0) {
531 usage();
532 return 0;
533 }
534
535 if (!strcmp(*argv, "devices")) {
536 list_devices();
537 return 0;
538 }
539
540 while (argc > 0) {
541 if(!strcmp(*argv, "-w")) {
542 wants_wipe = 1;
543 skip(1);
544 } else if(!strcmp(*argv, "-s")) {
545 require(2);
546 serial = argv[1];
547 skip(2);
548 } else if(!strcmp(*argv, "-p")) {
549 require(2);
550 product = argv[1];
551 skip(2);
552 } else if(!strcmp(*argv, "-c")) {
553 require(2);
554 cmdline = argv[1];
555 skip(2);
556 } else if(!strcmp(*argv, "getvar")) {
557 require(2);
558 fb_queue_display(argv[1], argv[1]);
559 skip(2);
560 } else if(!strcmp(*argv, "erase")) {
561 require(2);
562 fb_queue_erase(argv[1]);
563 skip(2);
564 } else if(!strcmp(*argv, "signature")) {
565 require(2);
566 data = load_file(argv[1], &sz);
567 if (data == 0) die("could not load '%s'", argv[1]);
568 if (sz != 256) die("signature must be 256 bytes");
569 fb_queue_download("signature", data, sz);
570 fb_queue_command("signature", "installing signature");
571 skip(2);
572 } else if(!strcmp(*argv, "reboot")) {
573 wants_reboot = 1;
574 skip(1);
575 } else if(!strcmp(*argv, "reboot-bootloader")) {
576 wants_reboot_bootloader = 1;
577 skip(1);
578 } else if(!strcmp(*argv, "boot")) {
579 char *kname = 0;
580 char *rname = 0;
581 skip(1);
582 if (argc > 0) {
583 kname = argv[0];
584 skip(1);
585 }
586 if (argc > 0) {
587 rname = argv[0];
588 skip(1);
589 }
590 data = load_bootable_image(kname, rname, &sz, cmdline);
591 if (data == 0) return 1;
592 fb_queue_download("boot.img", data, sz);
593 fb_queue_command("boot", "booting");
594 } else if(!strcmp(*argv, "flash")) {
595 char *pname = argv[1];
596 char *fname = 0;
597 require(2);
598 if (argc > 2) {
599 fname = argv[2];
600 skip(3);
601 } else {
602 fname = find_item(pname, product);
603 skip(2);
604 }
605 if (fname == 0) die("cannot determine image filename for '%s'", pname);
606 data = load_file(fname, &sz);
607 if (data == 0) die("cannot load '%s'\n", fname);
608 fb_queue_flash(pname, data, sz);
609 } else if(!strcmp(*argv, "flash:raw")) {
610 char *pname = argv[1];
611 char *kname = argv[2];
612 char *rname = 0;
613 require(3);
614 if(argc > 3) {
615 rname = argv[3];
616 skip(4);
617 } else {
618 skip(3);
619 }
620 data = load_bootable_image(kname, rname, &sz, cmdline);
621 if (data == 0) die("cannot load bootable image");
622 fb_queue_flash(pname, data, sz);
623 } else if(!strcmp(*argv, "flashall")) {
624 skip(1);
625 do_flashall();
626 wants_reboot = 1;
627 } else if(!strcmp(*argv, "update")) {
628 if (argc > 1) {
629 do_update(argv[1]);
630 skip(2);
631 } else {
632 do_update("update.zip");
633 skip(1);
634 }
635 wants_reboot = 1;
636 } else if(!strcmp(*argv, "oem")) {
637 argc = do_oem_command(argc, argv);
638 } else {
639 usage();
640 }
641 }
642
643 if (wants_wipe) {
644 fb_queue_erase("userdata");
645 fb_queue_erase("cache");
646 }
647 if (wants_reboot) {
648 fb_queue_reboot();
649 } else if (wants_reboot_bootloader) {
650 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
651 }
652
653 usb = open_device();
654
655 fb_execute_queue(usb);
656 return 0;
657}