fastboot.c revision bb0d0721e6bc5dcedc9bbbac0ea8fa4e57f487ad
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#define _LARGEFILE64_SOURCE
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <stdarg.h>
34#include <stdbool.h>
35#include <stdint.h>
36#include <string.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <limits.h>
41#include <ctype.h>
42#include <getopt.h>
43
44#include <sys/time.h>
45#include <sys/types.h>
46
47#include <bootimg.h>
48#include <sparse/sparse.h>
49#include <zipfile/zipfile.h>
50
51#include "fastboot.h"
52
53#ifndef O_BINARY
54#define O_BINARY 0
55#endif
56
57char cur_product[FB_RESPONSE_SZ + 1];
58
59void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
60
61boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
62                        void *ramdisk, unsigned ramdisk_size,
63                        void *second, unsigned second_size,
64                        unsigned page_size, unsigned base,
65                        unsigned *bootimg_size);
66
67static usb_handle *usb = 0;
68static const char *serial = 0;
69static const char *product = 0;
70static const char *cmdline = 0;
71static int wipe_data = 0;
72static unsigned short vendor_id = 0;
73static int long_listing = 0;
74static int64_t sparse_limit = -1;
75static int64_t target_sparse_limit = -1;
76
77static unsigned base_addr = 0x10000000;
78
79void die(const char *fmt, ...)
80{
81    va_list ap;
82    va_start(ap, fmt);
83    fprintf(stderr,"error: ");
84    vfprintf(stderr, fmt, ap);
85    fprintf(stderr,"\n");
86    va_end(ap);
87    exit(1);
88}
89
90void get_my_path(char *path);
91
92char *find_item(const char *item, const char *product)
93{
94    char *dir;
95    char *fn;
96    char path[PATH_MAX + 128];
97
98    if(!strcmp(item,"boot")) {
99        fn = "boot.img";
100    } else if(!strcmp(item,"recovery")) {
101        fn = "recovery.img";
102    } else if(!strcmp(item,"system")) {
103        fn = "system.img";
104    } else if(!strcmp(item,"userdata")) {
105        fn = "userdata.img";
106    } else if(!strcmp(item,"cache")) {
107        fn = "cache.img";
108    } else if(!strcmp(item,"info")) {
109        fn = "android-info.txt";
110    } else {
111        fprintf(stderr,"unknown partition '%s'\n", item);
112        return 0;
113    }
114
115    if(product) {
116        get_my_path(path);
117        sprintf(path + strlen(path),
118                "../../../target/product/%s/%s", product, fn);
119        return strdup(path);
120    }
121
122    dir = getenv("ANDROID_PRODUCT_OUT");
123    if((dir == 0) || (dir[0] == 0)) {
124        die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
125        return 0;
126    }
127
128    sprintf(path, "%s/%s", dir, fn);
129    return strdup(path);
130}
131
132#ifdef _WIN32
133void *load_file(const char *fn, unsigned *_sz);
134int64_t file_size(const char *fn);
135#else
136#if defined(__APPLE__) && defined(__MACH__)
137#define lseek64 lseek
138#define off64_t off_t
139#endif
140
141int64_t file_size(const char *fn)
142{
143    off64_t off;
144    int fd;
145
146    fd = open(fn, O_RDONLY);
147    if (fd < 0) return -1;
148
149    off = lseek64(fd, 0, SEEK_END);
150    close(fd);
151
152    return off;
153}
154
155void *load_file(const char *fn, unsigned *_sz)
156{
157    char *data;
158    int sz;
159    int fd;
160    int errno_tmp;
161
162    data = 0;
163    fd = open(fn, O_RDONLY);
164    if(fd < 0) return 0;
165
166    sz = lseek(fd, 0, SEEK_END);
167    if(sz < 0) goto oops;
168
169    if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
170
171    data = (char*) malloc(sz);
172    if(data == 0) goto oops;
173
174    if(read(fd, data, sz) != sz) goto oops;
175    close(fd);
176
177    if(_sz) *_sz = sz;
178    return data;
179
180oops:
181    errno_tmp = errno;
182    close(fd);
183    if(data != 0) free(data);
184    errno = errno_tmp;
185    return 0;
186}
187#endif
188
189int match_fastboot(usb_ifc_info *info)
190{
191    return match_fastboot_with_serial(info, serial);
192}
193
194int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
195{
196    if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
197       (info->dev_vendor != 0x18d1) &&  // Google
198       (info->dev_vendor != 0x8087) &&  // Intel
199       (info->dev_vendor != 0x0451) &&
200       (info->dev_vendor != 0x0502) &&
201       (info->dev_vendor != 0x0fce) &&  // Sony Ericsson
202       (info->dev_vendor != 0x05c6) &&  // Qualcomm
203       (info->dev_vendor != 0x22b8) &&  // Motorola
204       (info->dev_vendor != 0x0955) &&  // Nvidia
205       (info->dev_vendor != 0x413c) &&  // DELL
206       (info->dev_vendor != 0x2314) &&  // INQ Mobile
207       (info->dev_vendor != 0x0b05) &&  // Asus
208       (info->dev_vendor != 0x0bb4))    // HTC
209            return -1;
210    if(info->ifc_class != 0xff) return -1;
211    if(info->ifc_subclass != 0x42) return -1;
212    if(info->ifc_protocol != 0x03) return -1;
213    // require matching serial number or device path if requested
214    // at the command line with the -s option.
215    if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
216                   strcmp(local_serial, info->device_path) != 0)) return -1;
217    return 0;
218}
219
220int list_devices_callback(usb_ifc_info *info)
221{
222    if (match_fastboot_with_serial(info, NULL) == 0) {
223        char* serial = info->serial_number;
224        if (!info->writable) {
225            serial = "no permissions"; // like "adb devices"
226        }
227        if (!serial[0]) {
228            serial = "????????????";
229        }
230        // output compatible with "adb devices"
231        if (!long_listing) {
232            printf("%s\tfastboot\n", serial);
233        } else if (!info->device_path) {
234            printf("%-22s fastboot\n", serial);
235        } else {
236            printf("%-22s fastboot %s\n", serial, info->device_path);
237        }
238    }
239
240    return -1;
241}
242
243usb_handle *open_device(void)
244{
245    static usb_handle *usb = 0;
246    int announce = 1;
247
248    if(usb) return usb;
249
250    for(;;) {
251        usb = usb_open(match_fastboot);
252        if(usb) return usb;
253        if(announce) {
254            announce = 0;
255            fprintf(stderr,"< waiting for device >\n");
256        }
257        sleep(1);
258    }
259}
260
261void list_devices(void) {
262    // We don't actually open a USB device here,
263    // just getting our callback called so we can
264    // list all the connected devices.
265    usb_open(list_devices_callback);
266}
267
268void usage(void)
269{
270    fprintf(stderr,
271/*           1234567890123456789012345678901234567890123456789012345678901234567890123456 */
272            "usage: fastboot [ <option> ] <command>\n"
273            "\n"
274            "commands:\n"
275            "  update <filename>                        reflash device from update.zip\n"
276            "  flashall                                 flash boot + recovery + system\n"
277            "  flash <partition> [ <filename> ]         write a file to a flash partition\n"
278            "  erase <partition>                        erase a flash partition\n"
279            "  format <partition>                       format a flash partition \n"
280            "  getvar <variable>                        display a bootloader variable\n"
281            "  boot <kernel> [ <ramdisk> ]              download and boot kernel\n"
282            "  flash:raw boot <kernel> [ <ramdisk> ]    create bootimage and flash it\n"
283            "  devices                                  list all connected devices\n"
284            "  continue                                 continue with autoboot\n"
285            "  reboot                                   reboot device normally\n"
286            "  reboot-bootloader                        reboot device into bootloader\n"
287            "  help                                     show this help message\n"
288            "\n"
289            "options:\n"
290            "  -w                                       erase userdata and cache\n"
291            "  -s <specific device>                     specify device serial number\n"
292            "                                           or path to device port\n"
293            "  -l                                       with \"devices\", lists device paths\n"
294            "  -p <product>                             specify product name\n"
295            "  -c <cmdline>                             override kernel commandline\n"
296            "  -i <vendor id>                           specify a custom USB vendor id\n"
297            "  -b <base_addr>                           specify a custom kernel base address\n"
298            "  -n <page size>                           specify the nand page size. default: 2048\n"
299            "  -S <size>[K|M|G]                         automatically sparse files greater than\n"
300            "                                           size.  0 to disable\n"
301        );
302}
303
304void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
305                          unsigned *sz, const char *cmdline)
306{
307    void *kdata = 0, *rdata = 0;
308    unsigned ksize = 0, rsize = 0;
309    void *bdata;
310    unsigned bsize;
311
312    if(kernel == 0) {
313        fprintf(stderr, "no image specified\n");
314        return 0;
315    }
316
317    kdata = load_file(kernel, &ksize);
318    if(kdata == 0) {
319        fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
320        return 0;
321    }
322
323        /* is this actually a boot image? */
324    if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
325        if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
326
327        if(ramdisk) {
328            fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
329            return 0;
330        }
331
332        *sz = ksize;
333        return kdata;
334    }
335
336    if(ramdisk) {
337        rdata = load_file(ramdisk, &rsize);
338        if(rdata == 0) {
339            fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
340            return  0;
341        }
342    }
343
344    fprintf(stderr,"creating boot image...\n");
345    bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
346    if(bdata == 0) {
347        fprintf(stderr,"failed to create boot.img\n");
348        return 0;
349    }
350    if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
351    fprintf(stderr,"creating boot image - %d bytes\n", bsize);
352    *sz = bsize;
353
354    return bdata;
355}
356
357void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
358{
359    void *data;
360    zipentry_t entry;
361    unsigned datasz;
362
363    entry = lookup_zipentry(zip, name);
364    if (entry == NULL) {
365        fprintf(stderr, "archive does not contain '%s'\n", name);
366        return 0;
367    }
368
369    *sz = get_zipentry_size(entry);
370
371    datasz = *sz * 1.001;
372    data = malloc(datasz);
373
374    if(data == 0) {
375        fprintf(stderr, "failed to allocate %d bytes\n", *sz);
376        return 0;
377    }
378
379    if (decompress_zipentry(entry, data, datasz)) {
380        fprintf(stderr, "failed to unzip '%s' from archive\n", name);
381        free(data);
382        return 0;
383    }
384
385    return data;
386}
387
388static char *strip(char *s)
389{
390    int n;
391    while(*s && isspace(*s)) s++;
392    n = strlen(s);
393    while(n-- > 0) {
394        if(!isspace(s[n])) break;
395        s[n] = 0;
396    }
397    return s;
398}
399
400#define MAX_OPTIONS 32
401static int setup_requirement_line(char *name)
402{
403    char *val[MAX_OPTIONS];
404    const char **out;
405    char *prod = NULL;
406    unsigned n, count;
407    char *x;
408    int invert = 0;
409
410    if (!strncmp(name, "reject ", 7)) {
411        name += 7;
412        invert = 1;
413    } else if (!strncmp(name, "require ", 8)) {
414        name += 8;
415        invert = 0;
416    } else if (!strncmp(name, "require-for-product:", 20)) {
417        // Get the product and point name past it
418        prod = name + 20;
419        name = strchr(name, ' ');
420        if (!name) return -1;
421        *name = 0;
422        name += 1;
423        invert = 0;
424    }
425
426    x = strchr(name, '=');
427    if (x == 0) return 0;
428    *x = 0;
429    val[0] = x + 1;
430
431    for(count = 1; count < MAX_OPTIONS; count++) {
432        x = strchr(val[count - 1],'|');
433        if (x == 0) break;
434        *x = 0;
435        val[count] = x + 1;
436    }
437
438    name = strip(name);
439    for(n = 0; n < count; n++) val[n] = strip(val[n]);
440
441    name = strip(name);
442    if (name == 0) return -1;
443
444        /* work around an unfortunate name mismatch */
445    if (!strcmp(name,"board")) name = "product";
446
447    out = malloc(sizeof(char*) * count);
448    if (out == 0) return -1;
449
450    for(n = 0; n < count; n++) {
451        out[n] = strdup(strip(val[n]));
452        if (out[n] == 0) return -1;
453    }
454
455    fb_queue_require(prod, name, invert, n, out);
456    return 0;
457}
458
459static void setup_requirements(char *data, unsigned sz)
460{
461    char *s;
462
463    s = data;
464    while (sz-- > 0) {
465        if(*s == '\n') {
466            *s++ = 0;
467            if (setup_requirement_line(data)) {
468                die("out of memory");
469            }
470            data = s;
471        } else {
472            s++;
473        }
474    }
475}
476
477void queue_info_dump(void)
478{
479    fb_queue_notice("--------------------------------------------");
480    fb_queue_display("version-bootloader", "Bootloader Version...");
481    fb_queue_display("version-baseband",   "Baseband Version.....");
482    fb_queue_display("serialno",           "Serial Number........");
483    fb_queue_notice("--------------------------------------------");
484}
485
486
487struct sparse_file **load_sparse_files(const char *fname, int max_size)
488{
489    int fd;
490    struct sparse_file *s;
491    int files;
492    struct sparse_file **out_s;
493
494    fd = open(fname, O_RDONLY | O_BINARY);
495    if (fd < 0) {
496        die("cannot open '%s'\n", fname);
497    }
498
499    s = sparse_file_import_auto(fd, false);
500    if (!s) {
501        die("cannot sparse read file '%s'\n", fname);
502    }
503
504    files = sparse_file_resparse(s, max_size, NULL, 0);
505    if (files < 0) {
506        die("Failed to resparse '%s'\n", fname);
507    }
508
509    out_s = calloc(sizeof(struct sparse_file *), files + 1);
510    if (!out_s) {
511        die("Failed to allocate sparse file array\n");
512    }
513
514    files = sparse_file_resparse(s, max_size, out_s, files);
515    if (files < 0) {
516        die("Failed to resparse '%s'\n", fname);
517    }
518
519    return out_s;
520}
521
522static int64_t get_target_sparse_limit(struct usb_handle *usb)
523{
524    int64_t limit = 0;
525    char response[FB_RESPONSE_SZ + 1];
526    int status = fb_getvar(usb, response, "max-download-size");
527
528    if (!status) {
529        limit = strtoul(response, NULL, 0);
530        if (limit > 0) {
531            fprintf(stderr, "target reported max download size of %lld bytes\n",
532                    limit);
533        }
534    }
535
536    return limit;
537}
538
539static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
540{
541    int64_t limit;
542
543    if (sparse_limit == 0) {
544        return 0;
545    } else if (sparse_limit > 0) {
546        limit = sparse_limit;
547    } else {
548        if (target_sparse_limit == -1) {
549            target_sparse_limit = get_target_sparse_limit(usb);
550        }
551        if (target_sparse_limit > 0) {
552            limit = target_sparse_limit;
553        } else {
554            return 0;
555        }
556    }
557
558    if (size > limit) {
559        return limit;
560    }
561
562    return 0;
563}
564
565void do_flash(usb_handle *usb, const char *pname, const char *fname)
566{
567    int64_t sz64;
568    void *data;
569    int64_t limit;
570
571    sz64 = file_size(fname);
572    limit = get_sparse_limit(usb, sz64);
573    if (limit) {
574        struct sparse_file **s = load_sparse_files(fname, limit);
575        if (s == NULL) {
576            die("cannot sparse load '%s'\n", fname);
577        }
578        while (*s) {
579            sz64 = sparse_file_len(*s, true, false);
580            fb_queue_flash_sparse(pname, *s++, sz64);
581        }
582    } else {
583        unsigned int sz;
584        data = load_file(fname, &sz);
585        if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
586        fb_queue_flash(pname, data, sz);
587    }
588}
589
590void do_update_signature(zipfile_t zip, char *fn)
591{
592    void *data;
593    unsigned sz;
594    data = unzip_file(zip, fn, &sz);
595    if (data == 0) return;
596    fb_queue_download("signature", data, sz);
597    fb_queue_command("signature", "installing signature");
598}
599
600void do_update(char *fn)
601{
602    void *zdata;
603    unsigned zsize;
604    void *data;
605    unsigned sz;
606    zipfile_t zip;
607
608    queue_info_dump();
609
610    fb_queue_query_save("product", cur_product, sizeof(cur_product));
611
612    zdata = load_file(fn, &zsize);
613    if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
614
615    zip = init_zipfile(zdata, zsize);
616    if(zip == 0) die("failed to access zipdata in '%s'");
617
618    data = unzip_file(zip, "android-info.txt", &sz);
619    if (data == 0) {
620        char *tmp;
621            /* fallback for older zipfiles */
622        data = unzip_file(zip, "android-product.txt", &sz);
623        if ((data == 0) || (sz < 1)) {
624            die("update package has no android-info.txt or android-product.txt");
625        }
626        tmp = malloc(sz + 128);
627        if (tmp == 0) die("out of memory");
628        sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
629        data = tmp;
630        sz = strlen(tmp);
631    }
632
633    setup_requirements(data, sz);
634
635    data = unzip_file(zip, "boot.img", &sz);
636    if (data == 0) die("update package missing boot.img");
637    do_update_signature(zip, "boot.sig");
638    fb_queue_flash("boot", data, sz);
639
640    data = unzip_file(zip, "recovery.img", &sz);
641    if (data != 0) {
642        do_update_signature(zip, "recovery.sig");
643        fb_queue_flash("recovery", data, sz);
644    }
645
646    data = unzip_file(zip, "system.img", &sz);
647    if (data == 0) die("update package missing system.img");
648    do_update_signature(zip, "system.sig");
649    fb_queue_flash("system", data, sz);
650}
651
652void do_send_signature(char *fn)
653{
654    void *data;
655    unsigned sz;
656    char *xtn;
657
658    xtn = strrchr(fn, '.');
659    if (!xtn) return;
660    if (strcmp(xtn, ".img")) return;
661
662    strcpy(xtn,".sig");
663    data = load_file(fn, &sz);
664    strcpy(xtn,".img");
665    if (data == 0) return;
666    fb_queue_download("signature", data, sz);
667    fb_queue_command("signature", "installing signature");
668}
669
670void do_flashall(void)
671{
672    char *fname;
673    void *data;
674    unsigned sz;
675
676    queue_info_dump();
677
678    fb_queue_query_save("product", cur_product, sizeof(cur_product));
679
680    fname = find_item("info", product);
681    if (fname == 0) die("cannot find android-info.txt");
682    data = load_file(fname, &sz);
683    if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
684    setup_requirements(data, sz);
685
686    fname = find_item("boot", product);
687    data = load_file(fname, &sz);
688    if (data == 0) die("could not load boot.img: %s", strerror(errno));
689    do_send_signature(fname);
690    fb_queue_flash("boot", data, sz);
691
692    fname = find_item("recovery", product);
693    data = load_file(fname, &sz);
694    if (data != 0) {
695        do_send_signature(fname);
696        fb_queue_flash("recovery", data, sz);
697    }
698
699    fname = find_item("system", product);
700    data = load_file(fname, &sz);
701    if (data == 0) die("could not load system.img: %s", strerror(errno));
702    do_send_signature(fname);
703    fb_queue_flash("system", data, sz);
704}
705
706#define skip(n) do { argc -= (n); argv += (n); } while (0)
707#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
708
709int do_oem_command(int argc, char **argv)
710{
711    int i;
712    char command[256];
713    if (argc <= 1) return 0;
714
715    command[0] = 0;
716    while(1) {
717        strcat(command,*argv);
718        skip(1);
719        if(argc == 0) break;
720        strcat(command," ");
721    }
722
723    fb_queue_command(command,"");
724    return 0;
725}
726
727static int64_t parse_num(const char *arg)
728{
729    char *endptr;
730    unsigned long long num;
731
732    num = strtoull(arg, &endptr, 0);
733    if (endptr == arg) {
734        return -1;
735    }
736
737    if (*endptr == 'k' || *endptr == 'K') {
738        if (num >= (-1ULL) / 1024) {
739            return -1;
740        }
741        num *= 1024LL;
742        endptr++;
743    } else if (*endptr == 'm' || *endptr == 'M') {
744        if (num >= (-1ULL) / (1024 * 1024)) {
745            return -1;
746        }
747        num *= 1024LL * 1024LL;
748        endptr++;
749    } else if (*endptr == 'g' || *endptr == 'G') {
750        if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
751            return -1;
752        }
753        num *= 1024LL * 1024LL * 1024LL;
754        endptr++;
755    }
756
757    if (*endptr != '\0') {
758        return -1;
759    }
760
761    if (num > INT64_MAX) {
762        return -1;
763    }
764
765    return num;
766}
767
768int main(int argc, char **argv)
769{
770    int wants_wipe = 0;
771    int wants_reboot = 0;
772    int wants_reboot_bootloader = 0;
773    void *data;
774    unsigned sz;
775    unsigned page_size = 2048;
776    int status;
777    int c;
778    int r;
779
780    const struct option longopts = { 0, 0, 0, 0 };
781
782    serial = getenv("ANDROID_SERIAL");
783
784    while (1) {
785        c = getopt_long(argc, argv, "wb:n:s:S:lp:c:i:m:h", &longopts, NULL);
786        if (c < 0) {
787            break;
788        }
789
790        switch (c) {
791        case 'w':
792            wants_wipe = 1;
793            break;
794        case 'b':
795            base_addr = strtoul(optarg, 0, 16);
796            break;
797        case 'n':
798            page_size = (unsigned)strtoul(optarg, NULL, 0);
799            if (!page_size) die("invalid page size");
800            break;
801        case 's':
802            serial = optarg;
803            break;
804        case 'S':
805            sparse_limit = parse_num(optarg);
806            if (sparse_limit < 0) {
807                    die("invalid sparse limit");
808            }
809            break;
810        case 'l':
811            long_listing = 1;
812            break;
813        case 'p':
814            product = optarg;
815            break;
816        case 'c':
817            cmdline = optarg;
818            break;
819        case 'i': {
820                char *endptr = NULL;
821                unsigned long val;
822
823                val = strtoul(optarg, &endptr, 0);
824                if (!endptr || *endptr != '\0' || (val & ~0xffff))
825                    die("invalid vendor id '%s'", optarg);
826                vendor_id = (unsigned short)val;
827                break;
828            }
829        case 'h':
830            usage();
831            return 1;
832        case '?':
833            return 1;
834        default:
835            abort();
836        }
837    }
838
839    argc -= optind;
840    argv += optind;
841
842    if (argc == 0 && !wants_wipe) {
843        usage();
844        return 1;
845    }
846
847    if (argc > 0 && !strcmp(*argv, "devices")) {
848        skip(1);
849        list_devices();
850        return 0;
851    }
852
853    usb = open_device();
854
855    while (argc > 0) {
856        if(!strcmp(*argv, "getvar")) {
857            require(2);
858            fb_queue_display(argv[1], argv[1]);
859            skip(2);
860        } else if(!strcmp(*argv, "erase")) {
861            require(2);
862            fb_queue_erase(argv[1]);
863            skip(2);
864        } else if(!strcmp(*argv, "format")) {
865            require(2);
866            fb_queue_format(argv[1], 0);
867            skip(2);
868        } else if(!strcmp(*argv, "signature")) {
869            require(2);
870            data = load_file(argv[1], &sz);
871            if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
872            if (sz != 256) die("signature must be 256 bytes");
873            fb_queue_download("signature", data, sz);
874            fb_queue_command("signature", "installing signature");
875            skip(2);
876        } else if(!strcmp(*argv, "reboot")) {
877            wants_reboot = 1;
878            skip(1);
879        } else if(!strcmp(*argv, "reboot-bootloader")) {
880            wants_reboot_bootloader = 1;
881            skip(1);
882        } else if (!strcmp(*argv, "continue")) {
883            fb_queue_command("continue", "resuming boot");
884            skip(1);
885        } else if(!strcmp(*argv, "boot")) {
886            char *kname = 0;
887            char *rname = 0;
888            skip(1);
889            if (argc > 0) {
890                kname = argv[0];
891                skip(1);
892            }
893            if (argc > 0) {
894                rname = argv[0];
895                skip(1);
896            }
897            data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
898            if (data == 0) return 1;
899            fb_queue_download("boot.img", data, sz);
900            fb_queue_command("boot", "booting");
901        } else if(!strcmp(*argv, "flash")) {
902            char *pname = argv[1];
903            char *fname = 0;
904            require(2);
905            if (argc > 2) {
906                fname = argv[2];
907                skip(3);
908            } else {
909                fname = find_item(pname, product);
910                skip(2);
911            }
912            if (fname == 0) die("cannot determine image filename for '%s'", pname);
913            do_flash(usb, pname, fname);
914        } else if(!strcmp(*argv, "flash:raw")) {
915            char *pname = argv[1];
916            char *kname = argv[2];
917            char *rname = 0;
918            require(3);
919            if(argc > 3) {
920                rname = argv[3];
921                skip(4);
922            } else {
923                skip(3);
924            }
925            data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
926            if (data == 0) die("cannot load bootable image");
927            fb_queue_flash(pname, data, sz);
928        } else if(!strcmp(*argv, "flashall")) {
929            skip(1);
930            do_flashall();
931            wants_reboot = 1;
932        } else if(!strcmp(*argv, "update")) {
933            if (argc > 1) {
934                do_update(argv[1]);
935                skip(2);
936            } else {
937                do_update("update.zip");
938                skip(1);
939            }
940            wants_reboot = 1;
941        } else if(!strcmp(*argv, "oem")) {
942            argc = do_oem_command(argc, argv);
943        } else if (!strcmp(*argv, "help")) {
944            usage();
945            return 0;
946        } else {
947            usage();
948            return 1;
949        }
950    }
951
952    if (wants_wipe) {
953        fb_queue_erase("userdata");
954        fb_queue_format("userdata", 1);
955        fb_queue_erase("cache");
956        fb_queue_format("cache", 1);
957    }
958    if (wants_reboot) {
959        fb_queue_reboot();
960    } else if (wants_reboot_bootloader) {
961        fb_queue_command("reboot-bootloader", "rebooting into bootloader");
962    }
963
964    if (fb_queue_is_empty())
965        return 0;
966
967    status = fb_execute_queue(usb);
968    return (status) ? 1 : 0;
969}
970