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