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