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