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