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