| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 12 |  *    the documentation and/or other materials provided with the | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 22 |  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 |  | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 29 | #define _LARGEFILE64_SOURCE | 
 | 30 |  | 
| Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 31 | #include <ctype.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include <errno.h> | 
 | 33 | #include <fcntl.h> | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 34 | #include <getopt.h> | 
| Ying Wang | cf86e2f | 2014-05-15 20:06:40 -0700 | [diff] [blame] | 35 | #include <inttypes.h> | 
| Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 36 | #include <limits.h> | 
 | 37 | #include <stdbool.h> | 
 | 38 | #include <stdint.h> | 
 | 39 | #include <stdio.h> | 
 | 40 | #include <stdlib.h> | 
 | 41 | #include <string.h> | 
 | 42 | #include <sys/stat.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | #include <sys/time.h> | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 44 | #include <sys/types.h> | 
| Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 45 | #include <unistd.h> | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 46 |  | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 47 | #include <sparse/sparse.h> | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 48 | #include <ziparchive/zip_archive.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 50 | #include "bootimg_utils.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | #include "fastboot.h" | 
| Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 52 | #include "fs.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 |  | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 54 | #ifndef O_BINARY | 
 | 55 | #define O_BINARY 0 | 
 | 56 | #endif | 
 | 57 |  | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 58 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a))) | 
 | 59 |  | 
| Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 60 | char cur_product[FB_RESPONSE_SZ + 1]; | 
 | 61 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | static const char *serial = 0; | 
 | 63 | static const char *product = 0; | 
 | 64 | static const char *cmdline = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | static unsigned short vendor_id = 0; | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 66 | static int long_listing = 0; | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 67 | static int64_t sparse_limit = -1; | 
 | 68 | static int64_t target_sparse_limit = -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 |  | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 70 | unsigned page_size = 2048; | 
 | 71 | unsigned base_addr      = 0x10000000; | 
 | 72 | unsigned kernel_offset  = 0x00008000; | 
 | 73 | unsigned ramdisk_offset = 0x01000000; | 
 | 74 | unsigned second_offset  = 0x00f00000; | 
 | 75 | unsigned tags_offset    = 0x00000100; | 
 | 76 |  | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 77 | enum fb_buffer_type { | 
 | 78 |     FB_BUFFER, | 
 | 79 |     FB_BUFFER_SPARSE, | 
 | 80 | }; | 
 | 81 |  | 
 | 82 | struct fastboot_buffer { | 
 | 83 |     enum fb_buffer_type type; | 
 | 84 |     void *data; | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 85 |     unsigned int sz; | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 86 | }; | 
 | 87 |  | 
 | 88 | static struct { | 
 | 89 |     char img_name[13]; | 
 | 90 |     char sig_name[13]; | 
 | 91 |     char part_name[9]; | 
 | 92 |     bool is_optional; | 
| Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 93 | } images[] = { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 94 |     {"boot.img", "boot.sig", "boot", false}, | 
 | 95 |     {"recovery.img", "recovery.sig", "recovery", true}, | 
 | 96 |     {"system.img", "system.sig", "system", false}, | 
| Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 97 |     {"vendor.img", "vendor.sig", "vendor", true}, | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 98 | }; | 
| Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 99 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | char *find_item(const char *item, const char *product) | 
 | 101 | { | 
 | 102 |     char *dir; | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 103 |     const char *fn; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 |     char path[PATH_MAX + 128]; | 
 | 105 |  | 
 | 106 |     if(!strcmp(item,"boot")) { | 
 | 107 |         fn = "boot.img"; | 
 | 108 |     } else if(!strcmp(item,"recovery")) { | 
 | 109 |         fn = "recovery.img"; | 
 | 110 |     } else if(!strcmp(item,"system")) { | 
 | 111 |         fn = "system.img"; | 
| Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 112 |     } else if(!strcmp(item,"vendor")) { | 
 | 113 |         fn = "vendor.img"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 |     } else if(!strcmp(item,"userdata")) { | 
 | 115 |         fn = "userdata.img"; | 
| Jean-Baptiste Queru | d7608a4 | 2011-09-30 14:39:25 -0700 | [diff] [blame] | 116 |     } else if(!strcmp(item,"cache")) { | 
 | 117 |         fn = "cache.img"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 |     } else if(!strcmp(item,"info")) { | 
 | 119 |         fn = "android-info.txt"; | 
 | 120 |     } else { | 
 | 121 |         fprintf(stderr,"unknown partition '%s'\n", item); | 
 | 122 |         return 0; | 
 | 123 |     } | 
 | 124 |  | 
 | 125 |     if(product) { | 
 | 126 |         get_my_path(path); | 
 | 127 |         sprintf(path + strlen(path), | 
 | 128 |                 "../../../target/product/%s/%s", product, fn); | 
 | 129 |         return strdup(path); | 
 | 130 |     } | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 131 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 |     dir = getenv("ANDROID_PRODUCT_OUT"); | 
 | 133 |     if((dir == 0) || (dir[0] == 0)) { | 
 | 134 |         die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); | 
 | 135 |         return 0; | 
 | 136 |     } | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 137 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 |     sprintf(path, "%s/%s", dir, fn); | 
 | 139 |     return strdup(path); | 
 | 140 | } | 
 | 141 |  | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 142 | static int64_t file_size(int fd) | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 143 | { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 144 |     struct stat st; | 
 | 145 |     int ret; | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 146 |  | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 147 |     ret = fstat(fd, &st); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 148 |  | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 149 |     return ret ? -1 : st.st_size; | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 150 | } | 
 | 151 |  | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 152 | static void *load_fd(int fd, unsigned *_sz) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | { | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 154 |     char *data; | 
 | 155 |     int sz; | 
| Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 156 |     int errno_tmp; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 |  | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 158 |     data = 0; | 
 | 159 |  | 
 | 160 |     sz = file_size(fd); | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 161 |     if (sz < 0) { | 
 | 162 |         goto oops; | 
 | 163 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 |  | 
 | 165 |     data = (char*) malloc(sz); | 
 | 166 |     if(data == 0) goto oops; | 
 | 167 |  | 
 | 168 |     if(read(fd, data, sz) != sz) goto oops; | 
 | 169 |     close(fd); | 
 | 170 |  | 
 | 171 |     if(_sz) *_sz = sz; | 
 | 172 |     return data; | 
 | 173 |  | 
 | 174 | oops: | 
| Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 175 |     errno_tmp = errno; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 176 |     close(fd); | 
 | 177 |     if(data != 0) free(data); | 
| Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 178 |     errno = errno_tmp; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 |     return 0; | 
 | 180 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 |  | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 182 | static void *load_file(const char *fn, unsigned *_sz) | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 183 | { | 
 | 184 |     int fd; | 
 | 185 |  | 
 | 186 |     fd = open(fn, O_RDONLY | O_BINARY); | 
 | 187 |     if(fd < 0) return 0; | 
 | 188 |  | 
 | 189 |     return load_fd(fd, _sz); | 
 | 190 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 191 |  | 
| JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 192 | int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial) | 
 | 193 | { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 |     if(!(vendor_id && (info->dev_vendor == vendor_id)) && | 
| Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 195 |        (info->dev_vendor != 0x18d1) &&  // Google | 
| Wu, Hao | f60e863 | 2012-01-17 12:04:11 -0800 | [diff] [blame] | 196 |        (info->dev_vendor != 0x8087) &&  // Intel | 
| The Android Open Source Project | f614d64 | 2009-03-18 17:39:49 -0700 | [diff] [blame] | 197 |        (info->dev_vendor != 0x0451) && | 
| Robert CH Chou | e25ff1c | 2009-09-21 09:51:35 +0800 | [diff] [blame] | 198 |        (info->dev_vendor != 0x0502) && | 
| Dima Zavin | 509f739 | 2010-05-14 14:48:30 -0700 | [diff] [blame] | 199 |        (info->dev_vendor != 0x0fce) &&  // Sony Ericsson | 
 | 200 |        (info->dev_vendor != 0x05c6) &&  // Qualcomm | 
| Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 201 |        (info->dev_vendor != 0x22b8) &&  // Motorola | 
| Erik Gilling | 37e9e90 | 2010-01-20 17:40:05 -0800 | [diff] [blame] | 202 |        (info->dev_vendor != 0x0955) &&  // Nvidia | 
| Xavier Ducrohet | af82f21 | 2010-01-21 17:39:25 -0800 | [diff] [blame] | 203 |        (info->dev_vendor != 0x413c) &&  // DELL | 
| Xavier Ducrohet | 746f324 | 2012-01-13 16:03:37 -0800 | [diff] [blame] | 204 |        (info->dev_vendor != 0x2314) &&  // INQ Mobile | 
| Ramanan Rajeswaran | 73c019b | 2012-02-24 13:00:34 -0800 | [diff] [blame] | 205 |        (info->dev_vendor != 0x0b05) &&  // Asus | 
| Mohamad Ayyash | 6c48349 | 2016-02-16 20:12:34 -0800 | [diff] [blame] | 206 |        (info->dev_vendor != 0x0bb4) &&  // HTC | 
 | 207 |        (info->dev_vendor != 0x07cf))    // CASIO | 
| Mike Lockwood | 09070d9 | 2009-08-05 17:04:36 -0400 | [diff] [blame] | 208 |             return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 |     if(info->ifc_class != 0xff) return -1; | 
 | 210 |     if(info->ifc_subclass != 0x42) return -1; | 
 | 211 |     if(info->ifc_protocol != 0x03) return -1; | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 212 |     // require matching serial number or device path if requested | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 |     // at the command line with the -s option. | 
| JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 214 |     if (local_serial && (strcmp(local_serial, info->serial_number) != 0 && | 
 | 215 |                    strcmp(local_serial, info->device_path) != 0)) return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 |     return 0; | 
 | 217 | } | 
 | 218 |  | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 219 | int match_fastboot(usb_ifc_info *info) | 
 | 220 | { | 
 | 221 |     return match_fastboot_with_serial(info, serial); | 
 | 222 | } | 
 | 223 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | int list_devices_callback(usb_ifc_info *info) | 
 | 225 | { | 
| JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 226 |     if (match_fastboot_with_serial(info, NULL) == 0) { | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 227 |         const char* serial = info->serial_number; | 
| Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 228 |         if (!info->writable) { | 
 | 229 |             serial = "no permissions"; // like "adb devices" | 
 | 230 |         } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 |         if (!serial[0]) { | 
 | 232 |             serial = "????????????"; | 
 | 233 |         } | 
| Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 234 |         // output compatible with "adb devices" | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 235 |         if (!long_listing) { | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 236 |             printf("%s\tfastboot\n", serial); | 
| Stephen Hines | 04f8953 | 2014-11-26 14:54:43 -0800 | [diff] [blame] | 237 |         } else if (strcmp("", info->device_path) == 0) { | 
| Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 238 |             printf("%-22s fastboot\n", serial); | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 239 |         } else { | 
| Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 240 |             printf("%-22s fastboot %s\n", serial, info->device_path); | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 241 |         } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 242 |     } | 
 | 243 |  | 
 | 244 |     return -1; | 
 | 245 | } | 
 | 246 |  | 
 | 247 | usb_handle *open_device(void) | 
 | 248 | { | 
 | 249 |     static usb_handle *usb = 0; | 
 | 250 |     int announce = 1; | 
 | 251 |  | 
 | 252 |     if(usb) return usb; | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 253 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 254 |     for(;;) { | 
 | 255 |         usb = usb_open(match_fastboot); | 
 | 256 |         if(usb) return usb; | 
 | 257 |         if(announce) { | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 258 |             announce = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 |             fprintf(stderr,"< waiting for device >\n"); | 
 | 260 |         } | 
| Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 261 |         usleep(1000); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 262 |     } | 
 | 263 | } | 
 | 264 |  | 
 | 265 | void list_devices(void) { | 
 | 266 |     // We don't actually open a USB device here, | 
 | 267 |     // just getting our callback called so we can | 
 | 268 |     // list all the connected devices. | 
 | 269 |     usb_open(list_devices_callback); | 
 | 270 | } | 
 | 271 |  | 
 | 272 | void usage(void) | 
 | 273 | { | 
 | 274 |     fprintf(stderr, | 
 | 275 | /*           1234567890123456789012345678901234567890123456789012345678901234567890123456 */ | 
 | 276 |             "usage: fastboot [ <option> ] <command>\n" | 
 | 277 |             "\n" | 
 | 278 |             "commands:\n" | 
 | 279 |             "  update <filename>                        reflash device from update.zip\n" | 
| Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 280 |             "  flashall                                 flash boot, system, vendor and if found,\n" | 
| Daniel Rosenberg | 015d73f | 2014-09-15 13:44:07 -0700 | [diff] [blame] | 281 |             "                                           recovery\n" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 282 |             "  flash <partition> [ <filename> ]         write a file to a flash partition\n" | 
| Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 283 |             "  flashing lock                            locks the device. Prevents flashing\n" | 
| Badhri Jagan Sridharan | a873e1d | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 284 |             "                                           partitions\n" | 
| Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 285 |             "  flashing unlock                          unlocks the device. Allows user to\n" | 
 | 286 |             "                                           flash any partition except the ones\n" | 
| Badhri Jagan Sridharan | a873e1d | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 287 |             "                                           that are related to bootloader\n" | 
| Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 288 |             "  flashing lock_critical                   Prevents flashing bootloader related\n" | 
| Badhri Jagan Sridharan | a873e1d | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 289 |             "                                           partitions\n" | 
| Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 290 |             "  flashing unlock_critical                 Enables flashing bootloader related\n" | 
| Badhri Jagan Sridharan | a873e1d | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 291 |             "                                           partitions\n" | 
| Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 292 |             "  flashing get_unlock_ability              Queries bootloader to see if the\n" | 
| Badhri Jagan Sridharan | a873e1d | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 293 |             "                                           device is unlocked\n" | 
| Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 294 |             "  flashing get_unlock_bootloader_nonce     Queries the bootloader to get the\n" | 
 | 295 |             "                                           unlock nonce\n" | 
 | 296 |             "  flashing unlock_bootloader <request>     Issue unlock bootloader using request\n" | 
 | 297 |             "  flashing lock_bootloader                 Locks the bootloader to prevent\n" | 
 | 298 |             "                                           bootloader version rollback\n" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 299 |             "  erase <partition>                        erase a flash partition\n" | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 300 |             "  format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n" | 
 | 301 |             "                                           Can override the fs type and/or\n" | 
 | 302 |             "                                           size the bootloader reports.\n" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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" | 
| Bruce Beare | 24ce4bc | 2010-10-14 09:43:26 -0700 | [diff] [blame] | 307 |             "  continue                                 continue with autoboot\n" | 
| Elliott Hughes | ca85df0 | 2015-02-25 10:02:00 -0800 | [diff] [blame] | 308 |             "  reboot [bootloader]                      reboot device, optionally into bootloader\n" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 |             "  reboot-bootloader                        reboot device into bootloader\n" | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 310 |             "  help                                     show this help message\n" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 311 |             "\n" | 
 | 312 |             "options:\n" | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 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" | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 317 |             "  -s <specific device>                     specify device serial number\n" | 
 | 318 |             "                                           or path to device port\n" | 
 | 319 |             "  -l                                       with \"devices\", lists device paths\n" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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" | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 323 |             "  -b <base_addr>                           specify a custom kernel base address.\n" | 
 | 324 |             "                                           default: 0x10000000\n" | 
 | 325 |             "  -n <page size>                           specify the nand page size.\n" | 
 | 326 |             "                                           default: 2048\n" | 
 | 327 |             "  -S <size>[K|M|G]                         automatically sparse files greater\n" | 
 | 328 |             "                                           than size.  0 to disable\n" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 |         ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | } | 
 | 331 |  | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 332 | void *load_bootable_image(const char *kernel, const char *ramdisk, | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 |                           unsigned *sz, const char *cmdline) | 
 | 334 | { | 
 | 335 |     void *kdata = 0, *rdata = 0; | 
 | 336 |     unsigned ksize = 0, rsize = 0; | 
 | 337 |     void *bdata; | 
 | 338 |     unsigned bsize; | 
 | 339 |  | 
 | 340 |     if(kernel == 0) { | 
 | 341 |         fprintf(stderr, "no image specified\n"); | 
 | 342 |         return 0; | 
 | 343 |     } | 
 | 344 |  | 
 | 345 |     kdata = load_file(kernel, &ksize); | 
 | 346 |     if(kdata == 0) { | 
| Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 347 |         fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 |         return 0; | 
 | 349 |     } | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 350 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 351 |         /* is this actually a boot image? */ | 
 | 352 |     if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { | 
 | 353 |         if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 354 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 |         if(ramdisk) { | 
 | 356 |             fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); | 
 | 357 |             return 0; | 
 | 358 |         } | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 359 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 360 |         *sz = ksize; | 
 | 361 |         return kdata; | 
 | 362 |     } | 
 | 363 |  | 
 | 364 |     if(ramdisk) { | 
 | 365 |         rdata = load_file(ramdisk, &rsize); | 
 | 366 |         if(rdata == 0) { | 
| Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 367 |             fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 368 |             return  0; | 
 | 369 |         } | 
 | 370 |     } | 
 | 371 |  | 
 | 372 |     fprintf(stderr,"creating boot image...\n"); | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 373 |     bdata = mkbootimg(kdata, ksize, kernel_offset, | 
 | 374 |                       rdata, rsize, ramdisk_offset, | 
 | 375 |                       0, 0, second_offset, | 
 | 376 |                       page_size, base_addr, tags_offset, &bsize); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 377 |     if(bdata == 0) { | 
 | 378 |         fprintf(stderr,"failed to create boot.img\n"); | 
 | 379 |         return 0; | 
 | 380 |     } | 
 | 381 |     if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); | 
 | 382 |     fprintf(stderr,"creating boot image - %d bytes\n", bsize); | 
 | 383 |     *sz = bsize; | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 384 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 |     return bdata; | 
 | 386 | } | 
 | 387 |  | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 388 | static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 389 | { | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 390 |     ZipEntryName zip_entry_name(entry_name); | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 391 |     ZipEntry zip_entry; | 
 | 392 |     if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 393 |         fprintf(stderr, "archive does not contain '%s'\n", entry_name); | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 394 |         return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 395 |     } | 
 | 396 |  | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 397 |     *sz = zip_entry.uncompressed_length; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 |  | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 399 |     uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length)); | 
 | 400 |     if (data == NULL) { | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 401 |         fprintf(stderr, "failed to allocate %u bytes for '%s'\n", *sz, entry_name); | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 402 |         return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 403 |     } | 
 | 404 |  | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 405 |     int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length); | 
 | 406 |     if (error != 0) { | 
 | 407 |         fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 |         free(data); | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 409 |         return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 410 |     } | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 411 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 412 |     return data; | 
 | 413 | } | 
 | 414 |  | 
| Elliott Hughes | 8bdd4bb | 2015-06-03 15:22:11 -0700 | [diff] [blame] | 415 | #if defined(_WIN32) | 
 | 416 |  | 
 | 417 | // TODO: move this to somewhere it can be shared. | 
 | 418 |  | 
 | 419 | #include <windows.h> | 
 | 420 |  | 
 | 421 | // Windows' tmpfile(3) requires administrator rights because | 
 | 422 | // it creates temporary files in the root directory. | 
 | 423 | static FILE* win32_tmpfile() { | 
 | 424 |     char temp_path[PATH_MAX]; | 
 | 425 |     DWORD nchars = GetTempPath(sizeof(temp_path), temp_path); | 
 | 426 |     if (nchars == 0 || nchars >= sizeof(temp_path)) { | 
 | 427 |         fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError()); | 
 | 428 |         return nullptr; | 
 | 429 |     } | 
 | 430 |  | 
 | 431 |     char filename[PATH_MAX]; | 
 | 432 |     if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) { | 
 | 433 |         fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError()); | 
 | 434 |         return nullptr; | 
 | 435 |     } | 
 | 436 |  | 
 | 437 |     return fopen(filename, "w+bTD"); | 
 | 438 | } | 
 | 439 |  | 
 | 440 | #define tmpfile win32_tmpfile | 
 | 441 |  | 
 | 442 | #endif | 
 | 443 |  | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 444 | static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) { | 
 | 445 |     FILE* fp = tmpfile(); | 
 | 446 |     if (fp == NULL) { | 
 | 447 |         fprintf(stderr, "failed to create temporary file for '%s': %s\n", | 
 | 448 |                 entry_name, strerror(errno)); | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 449 |         return -1; | 
 | 450 |     } | 
 | 451 |  | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 452 |     ZipEntryName zip_entry_name(entry_name); | 
 | 453 |     ZipEntry zip_entry; | 
 | 454 |     if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { | 
 | 455 |         fprintf(stderr, "archive does not contain '%s'\n", entry_name); | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 456 |         return -1; | 
 | 457 |     } | 
 | 458 |  | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 459 |     int fd = fileno(fp); | 
 | 460 |     int error = ExtractEntryToFile(zip, &zip_entry, fd); | 
 | 461 |     if (error != 0) { | 
 | 462 |         fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error)); | 
 | 463 |         return -1; | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 464 |     } | 
 | 465 |  | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 466 |     lseek(fd, 0, SEEK_SET); | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 467 |     return fd; | 
 | 468 | } | 
 | 469 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 470 | static char *strip(char *s) | 
 | 471 | { | 
 | 472 |     int n; | 
 | 473 |     while(*s && isspace(*s)) s++; | 
 | 474 |     n = strlen(s); | 
 | 475 |     while(n-- > 0) { | 
 | 476 |         if(!isspace(s[n])) break; | 
 | 477 |         s[n] = 0; | 
 | 478 |     } | 
 | 479 |     return s; | 
 | 480 | } | 
 | 481 |  | 
 | 482 | #define MAX_OPTIONS 32 | 
 | 483 | static int setup_requirement_line(char *name) | 
 | 484 | { | 
 | 485 |     char *val[MAX_OPTIONS]; | 
| Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 486 |     char *prod = NULL; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 487 |     unsigned n, count; | 
 | 488 |     char *x; | 
 | 489 |     int invert = 0; | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 490 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 491 |     if (!strncmp(name, "reject ", 7)) { | 
 | 492 |         name += 7; | 
 | 493 |         invert = 1; | 
 | 494 |     } else if (!strncmp(name, "require ", 8)) { | 
 | 495 |         name += 8; | 
 | 496 |         invert = 0; | 
| Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 497 |     } else if (!strncmp(name, "require-for-product:", 20)) { | 
 | 498 |         // Get the product and point name past it | 
 | 499 |         prod = name + 20; | 
 | 500 |         name = strchr(name, ' '); | 
 | 501 |         if (!name) return -1; | 
 | 502 |         *name = 0; | 
 | 503 |         name += 1; | 
 | 504 |         invert = 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 505 |     } | 
 | 506 |  | 
 | 507 |     x = strchr(name, '='); | 
 | 508 |     if (x == 0) return 0; | 
 | 509 |     *x = 0; | 
 | 510 |     val[0] = x + 1; | 
 | 511 |  | 
 | 512 |     for(count = 1; count < MAX_OPTIONS; count++) { | 
 | 513 |         x = strchr(val[count - 1],'|'); | 
 | 514 |         if (x == 0) break; | 
 | 515 |         *x = 0; | 
 | 516 |         val[count] = x + 1; | 
 | 517 |     } | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 518 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 |     name = strip(name); | 
 | 520 |     for(n = 0; n < count; n++) val[n] = strip(val[n]); | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 521 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 522 |     name = strip(name); | 
 | 523 |     if (name == 0) return -1; | 
 | 524 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 525 |     const char* var = name; | 
 | 526 |     // Work around an unfortunate name mismatch. | 
 | 527 |     if (!strcmp(name,"board")) var = "product"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 528 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 529 |     const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 |     if (out == 0) return -1; | 
 | 531 |  | 
 | 532 |     for(n = 0; n < count; n++) { | 
 | 533 |         out[n] = strdup(strip(val[n])); | 
| Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 534 |         if (out[n] == 0) { | 
 | 535 |             for(size_t i = 0; i < n; ++i) { | 
 | 536 |                 free((char*) out[i]); | 
 | 537 |             } | 
 | 538 |             free(out); | 
 | 539 |             return -1; | 
 | 540 |         } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 541 |     } | 
 | 542 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 543 |     fb_queue_require(prod, var, invert, n, out); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 544 |     return 0; | 
 | 545 | } | 
 | 546 |  | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 547 | static void setup_requirements(char *data, unsigned sz) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | { | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 549 |     char *s; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 550 |  | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 551 |     s = data; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 552 |     while (sz-- > 0) { | 
 | 553 |         if(*s == '\n') { | 
 | 554 |             *s++ = 0; | 
 | 555 |             if (setup_requirement_line(data)) { | 
 | 556 |                 die("out of memory"); | 
 | 557 |             } | 
 | 558 |             data = s; | 
 | 559 |         } else { | 
 | 560 |             s++; | 
 | 561 |         } | 
 | 562 |     } | 
 | 563 | } | 
 | 564 |  | 
 | 565 | void queue_info_dump(void) | 
 | 566 | { | 
 | 567 |     fb_queue_notice("--------------------------------------------"); | 
 | 568 |     fb_queue_display("version-bootloader", "Bootloader Version..."); | 
 | 569 |     fb_queue_display("version-baseband",   "Baseband Version....."); | 
 | 570 |     fb_queue_display("serialno",           "Serial Number........"); | 
 | 571 |     fb_queue_notice("--------------------------------------------"); | 
 | 572 | } | 
 | 573 |  | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 574 | static struct sparse_file **load_sparse_files(int fd, int max_size) | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 575 | { | 
| Mohamad Ayyash | 80cc1f6 | 2015-03-31 12:09:29 -0700 | [diff] [blame] | 576 |     struct sparse_file* s = sparse_file_import_auto(fd, false, true); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 577 |     if (!s) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 578 |         die("cannot sparse read file\n"); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 579 |     } | 
 | 580 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 581 |     int files = sparse_file_resparse(s, max_size, NULL, 0); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 582 |     if (files < 0) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 583 |         die("Failed to resparse\n"); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 584 |     } | 
 | 585 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 586 |     sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1)); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 587 |     if (!out_s) { | 
 | 588 |         die("Failed to allocate sparse file array\n"); | 
 | 589 |     } | 
 | 590 |  | 
 | 591 |     files = sparse_file_resparse(s, max_size, out_s, files); | 
 | 592 |     if (files < 0) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 593 |         die("Failed to resparse\n"); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 594 |     } | 
 | 595 |  | 
 | 596 |     return out_s; | 
 | 597 | } | 
 | 598 |  | 
 | 599 | static int64_t get_target_sparse_limit(struct usb_handle *usb) | 
 | 600 | { | 
 | 601 |     int64_t limit = 0; | 
 | 602 |     char response[FB_RESPONSE_SZ + 1]; | 
 | 603 |     int status = fb_getvar(usb, response, "max-download-size"); | 
 | 604 |  | 
 | 605 |     if (!status) { | 
 | 606 |         limit = strtoul(response, NULL, 0); | 
 | 607 |         if (limit > 0) { | 
| Ying Wang | cf86e2f | 2014-05-15 20:06:40 -0700 | [diff] [blame] | 608 |             fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 609 |                     limit); | 
 | 610 |         } | 
 | 611 |     } | 
 | 612 |  | 
 | 613 |     return limit; | 
 | 614 | } | 
 | 615 |  | 
 | 616 | static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size) | 
 | 617 | { | 
 | 618 |     int64_t limit; | 
 | 619 |  | 
 | 620 |     if (sparse_limit == 0) { | 
 | 621 |         return 0; | 
 | 622 |     } else if (sparse_limit > 0) { | 
 | 623 |         limit = sparse_limit; | 
 | 624 |     } else { | 
 | 625 |         if (target_sparse_limit == -1) { | 
 | 626 |             target_sparse_limit = get_target_sparse_limit(usb); | 
 | 627 |         } | 
 | 628 |         if (target_sparse_limit > 0) { | 
 | 629 |             limit = target_sparse_limit; | 
 | 630 |         } else { | 
| Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 631 |             return 0; | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 632 |         } | 
 | 633 |     } | 
 | 634 |  | 
 | 635 |     if (size > limit) { | 
 | 636 |         return limit; | 
 | 637 |     } | 
 | 638 |  | 
 | 639 |     return 0; | 
 | 640 | } | 
 | 641 |  | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 642 | /* Until we get lazy inode table init working in make_ext4fs, we need to | 
 | 643 |  * erase partitions of type ext4 before flashing a filesystem so no stale | 
 | 644 |  * inodes are left lying around.  Otherwise, e2fsck gets very upset. | 
 | 645 |  */ | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 646 | static int needs_erase(usb_handle* usb, const char *part) | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 647 | { | 
 | 648 |     /* The function fb_format_supported() currently returns the value | 
 | 649 |      * we want, so just call it. | 
 | 650 |      */ | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 651 |      return fb_format_supported(usb, part, NULL); | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 652 | } | 
 | 653 |  | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 654 | static int load_buf_fd(usb_handle *usb, int fd, | 
 | 655 |         struct fastboot_buffer *buf) | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 656 | { | 
| Sasha Levitskiy | 782111b | 2014-05-05 19:43:15 -0700 | [diff] [blame] | 657 |     int64_t sz64; | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 658 |     void *data; | 
 | 659 |     int64_t limit; | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 660 |  | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 661 |  | 
 | 662 |     sz64 = file_size(fd); | 
 | 663 |     if (sz64 < 0) { | 
 | 664 |         return -1; | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 665 |     } | 
| Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 666 |  | 
 | 667 |     lseek(fd, 0, SEEK_SET); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 668 |     limit = get_sparse_limit(usb, sz64); | 
 | 669 |     if (limit) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 670 |         struct sparse_file **s = load_sparse_files(fd, limit); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 671 |         if (s == NULL) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 672 |             return -1; | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 673 |         } | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 674 |         buf->type = FB_BUFFER_SPARSE; | 
 | 675 |         buf->data = s; | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 676 |     } else { | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 677 |         unsigned int sz; | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 678 |         data = load_fd(fd, &sz); | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 679 |         if (data == 0) return -1; | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 680 |         buf->type = FB_BUFFER; | 
| Sasha Levitskiy | 782111b | 2014-05-05 19:43:15 -0700 | [diff] [blame] | 681 |         buf->data = data; | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 682 |         buf->sz = sz; | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 683 |     } | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 684 |  | 
 | 685 |     return 0; | 
 | 686 | } | 
 | 687 |  | 
 | 688 | static int load_buf(usb_handle *usb, const char *fname, | 
 | 689 |         struct fastboot_buffer *buf) | 
 | 690 | { | 
 | 691 |     int fd; | 
 | 692 |  | 
 | 693 |     fd = open(fname, O_RDONLY | O_BINARY); | 
 | 694 |     if (fd < 0) { | 
| Daniel Rosenberg | 8228059 | 2014-04-29 13:45:05 -0700 | [diff] [blame] | 695 |         return -1; | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 696 |     } | 
 | 697 |  | 
 | 698 |     return load_buf_fd(usb, fd, buf); | 
 | 699 | } | 
 | 700 |  | 
 | 701 | static void flash_buf(const char *pname, struct fastboot_buffer *buf) | 
 | 702 | { | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 703 |     sparse_file** s; | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 704 |  | 
 | 705 |     switch (buf->type) { | 
 | 706 |         case FB_BUFFER_SPARSE: | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 707 |             s = reinterpret_cast<sparse_file**>(buf->data); | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 708 |             while (*s) { | 
 | 709 |                 int64_t sz64 = sparse_file_len(*s, true, false); | 
 | 710 |                 fb_queue_flash_sparse(pname, *s++, sz64); | 
 | 711 |             } | 
 | 712 |             break; | 
 | 713 |         case FB_BUFFER: | 
 | 714 |             fb_queue_flash(pname, buf->data, buf->sz); | 
 | 715 |             break; | 
 | 716 |         default: | 
 | 717 |             die("unknown buffer type: %d", buf->type); | 
 | 718 |     } | 
 | 719 | } | 
 | 720 |  | 
 | 721 | void do_flash(usb_handle *usb, const char *pname, const char *fname) | 
 | 722 | { | 
 | 723 |     struct fastboot_buffer buf; | 
 | 724 |  | 
 | 725 |     if (load_buf(usb, fname, &buf)) { | 
 | 726 |         die("cannot load '%s'", fname); | 
 | 727 |     } | 
 | 728 |     flash_buf(pname, &buf); | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 729 | } | 
 | 730 |  | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 731 | void do_update_signature(ZipArchiveHandle zip, char *fn) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 732 | { | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 733 |     unsigned sz; | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 734 |     void* data = unzip_file(zip, fn, &sz); | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 735 |     if (data == 0) return; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 736 |     fb_queue_download("signature", data, sz); | 
 | 737 |     fb_queue_command("signature", "installing signature"); | 
 | 738 | } | 
 | 739 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 740 | void do_update(usb_handle *usb, const char *filename, int erase_first) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 741 | { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 742 |     queue_info_dump(); | 
 | 743 |  | 
| Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 744 |     fb_queue_query_save("product", cur_product, sizeof(cur_product)); | 
 | 745 |  | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 746 |     ZipArchiveHandle zip; | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 747 |     int error = OpenArchive(filename, &zip); | 
 | 748 |     if (error != 0) { | 
| Narayan Kamath | 241bcf0 | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 749 |         CloseArchive(zip); | 
| Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 750 |         die("failed to open zip file '%s': %s", filename, ErrorCodeString(error)); | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 751 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 752 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 753 |     unsigned sz; | 
 | 754 |     void* data = unzip_file(zip, "android-info.txt", &sz); | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 755 |     if (data == 0) { | 
| Narayan Kamath | 241bcf0 | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 756 |         CloseArchive(zip); | 
| Elliott Hughes | 7c6d884 | 2015-03-19 10:30:53 -0700 | [diff] [blame] | 757 |         die("update package '%s' has no android-info.txt", filename); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 758 |     } | 
 | 759 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 760 |     setup_requirements(reinterpret_cast<char*>(data), sz); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 761 |  | 
| Elliott Hughes | 966339b | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 762 |     for (size_t i = 0; i < ARRAY_SIZE(images); ++i) { | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 763 |         int fd = unzip_to_file(zip, images[i].img_name); | 
| Elliott Hughes | 966339b | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 764 |         if (fd == -1) { | 
 | 765 |             if (images[i].is_optional) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 766 |                 continue; | 
| Elliott Hughes | 966339b | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 767 |             } | 
| Narayan Kamath | 241bcf0 | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 768 |             CloseArchive(zip); | 
| Elliott Hughes | 966339b | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 769 |             exit(1); // unzip_to_file already explained why. | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 770 |         } | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 771 |         fastboot_buffer buf; | 
 | 772 |         int rc = load_buf_fd(usb, fd, &buf); | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 773 |         if (rc) die("cannot load %s from flash", images[i].img_name); | 
 | 774 |         do_update_signature(zip, images[i].sig_name); | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 775 |         if (erase_first && needs_erase(usb, images[i].part_name)) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 776 |             fb_queue_erase(images[i].part_name); | 
 | 777 |         } | 
 | 778 |         flash_buf(images[i].part_name, &buf); | 
 | 779 |         /* not closing the fd here since the sparse code keeps the fd around | 
 | 780 |          * but hasn't mmaped data yet. The tmpfile will get cleaned up when the | 
 | 781 |          * program exits. | 
 | 782 |          */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 783 |     } | 
| Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 784 |  | 
 | 785 |     CloseArchive(zip); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 786 | } | 
 | 787 |  | 
 | 788 | void do_send_signature(char *fn) | 
 | 789 | { | 
 | 790 |     void *data; | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 791 |     unsigned sz; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 792 |     char *xtn; | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 793 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 794 |     xtn = strrchr(fn, '.'); | 
 | 795 |     if (!xtn) return; | 
 | 796 |     if (strcmp(xtn, ".img")) return; | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 797 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 798 |     strcpy(xtn,".sig"); | 
 | 799 |     data = load_file(fn, &sz); | 
 | 800 |     strcpy(xtn,".img"); | 
 | 801 |     if (data == 0) return; | 
 | 802 |     fb_queue_download("signature", data, sz); | 
 | 803 |     fb_queue_command("signature", "installing signature"); | 
 | 804 | } | 
 | 805 |  | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 806 | void do_flashall(usb_handle *usb, int erase_first) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 807 | { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 808 |     queue_info_dump(); | 
 | 809 |  | 
| Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 810 |     fb_queue_query_save("product", cur_product, sizeof(cur_product)); | 
 | 811 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 812 |     char* fname = find_item("info", product); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 813 |     if (fname == 0) die("cannot find android-info.txt"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 814 |  | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 815 |     unsigned sz; | 
 | 816 |     void* data = load_file(fname, &sz); | 
 | 817 |     if (data == 0) die("could not load android-info.txt: %s", strerror(errno)); | 
 | 818 |  | 
 | 819 |     setup_requirements(reinterpret_cast<char*>(data), sz); | 
 | 820 |  | 
 | 821 |     for (size_t i = 0; i < ARRAY_SIZE(images); i++) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 822 |         fname = find_item(images[i].part_name, product); | 
| Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 823 |         fastboot_buffer buf; | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 824 |         if (load_buf(usb, fname, &buf)) { | 
 | 825 |             if (images[i].is_optional) | 
 | 826 |                 continue; | 
 | 827 |             die("could not load %s\n", images[i].img_name); | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 828 |         } | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 829 |         do_send_signature(fname); | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 830 |         if (erase_first && needs_erase(usb, images[i].part_name)) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 831 |             fb_queue_erase(images[i].part_name); | 
 | 832 |         } | 
 | 833 |         flash_buf(images[i].part_name, &buf); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 834 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 835 | } | 
 | 836 |  | 
 | 837 | #define skip(n) do { argc -= (n); argv += (n); } while (0) | 
| JP Abgrall | 2d13d14 | 2011-03-01 23:35:07 -0800 | [diff] [blame] | 838 | #define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 839 |  | 
| Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 840 | int do_bypass_unlock_command(int argc, char **argv) | 
 | 841 | { | 
 | 842 |     unsigned sz; | 
 | 843 |     void *data; | 
 | 844 |  | 
 | 845 |     if (argc <= 2) return 0; | 
 | 846 |     skip(2); | 
 | 847 |  | 
 | 848 |     /* | 
 | 849 |      * Process unlock_bootloader, we have to load the message file | 
 | 850 |      * and send that to the remote device. | 
 | 851 |      */ | 
 | 852 |     require(1); | 
 | 853 |     data = load_file(*argv, &sz); | 
 | 854 |     if (data == 0) die("could not load '%s': %s", *argv, strerror(errno)); | 
 | 855 |     fb_queue_download("unlock_message", data, sz); | 
 | 856 |     fb_queue_command("flashing unlock_bootloader", "unlocking bootloader"); | 
 | 857 |     skip(1); | 
 | 858 |     return 0; | 
 | 859 | } | 
 | 860 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 861 | int do_oem_command(int argc, char **argv) | 
 | 862 | { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 863 |     char command[256]; | 
 | 864 |     if (argc <= 1) return 0; | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 865 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 866 |     command[0] = 0; | 
 | 867 |     while(1) { | 
 | 868 |         strcat(command,*argv); | 
 | 869 |         skip(1); | 
 | 870 |         if(argc == 0) break; | 
 | 871 |         strcat(command," "); | 
 | 872 |     } | 
 | 873 |  | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 874 |     fb_queue_command(command,""); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 875 |     return 0; | 
 | 876 | } | 
 | 877 |  | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 878 | static int64_t parse_num(const char *arg) | 
 | 879 | { | 
 | 880 |     char *endptr; | 
 | 881 |     unsigned long long num; | 
 | 882 |  | 
 | 883 |     num = strtoull(arg, &endptr, 0); | 
 | 884 |     if (endptr == arg) { | 
 | 885 |         return -1; | 
 | 886 |     } | 
 | 887 |  | 
 | 888 |     if (*endptr == 'k' || *endptr == 'K') { | 
 | 889 |         if (num >= (-1ULL) / 1024) { | 
 | 890 |             return -1; | 
 | 891 |         } | 
 | 892 |         num *= 1024LL; | 
 | 893 |         endptr++; | 
 | 894 |     } else if (*endptr == 'm' || *endptr == 'M') { | 
 | 895 |         if (num >= (-1ULL) / (1024 * 1024)) { | 
 | 896 |             return -1; | 
 | 897 |         } | 
 | 898 |         num *= 1024LL * 1024LL; | 
 | 899 |         endptr++; | 
 | 900 |     } else if (*endptr == 'g' || *endptr == 'G') { | 
 | 901 |         if (num >= (-1ULL) / (1024 * 1024 * 1024)) { | 
 | 902 |             return -1; | 
 | 903 |         } | 
 | 904 |         num *= 1024LL * 1024LL * 1024LL; | 
 | 905 |         endptr++; | 
 | 906 |     } | 
 | 907 |  | 
 | 908 |     if (*endptr != '\0') { | 
 | 909 |         return -1; | 
 | 910 |     } | 
 | 911 |  | 
 | 912 |     if (num > INT64_MAX) { | 
 | 913 |         return -1; | 
 | 914 |     } | 
 | 915 |  | 
 | 916 |     return num; | 
 | 917 | } | 
 | 918 |  | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 919 | void fb_perform_format(usb_handle* usb, | 
 | 920 |                        const char *partition, int skip_if_not_supported, | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 921 |                        const char *type_override, const char *size_override) | 
| Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 922 | { | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 923 |     char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1]; | 
 | 924 |     char *pType = pTypeBuff; | 
 | 925 |     char *pSize = pSizeBuff; | 
| Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 926 |     unsigned int limit = INT_MAX; | 
 | 927 |     struct fastboot_buffer buf; | 
 | 928 |     const char *errMsg = NULL; | 
 | 929 |     const struct fs_generator *gen; | 
 | 930 |     uint64_t pSz; | 
 | 931 |     int status; | 
 | 932 |     int fd; | 
 | 933 |  | 
 | 934 |     if (target_sparse_limit > 0 && target_sparse_limit < limit) | 
 | 935 |         limit = target_sparse_limit; | 
 | 936 |     if (sparse_limit > 0 && sparse_limit < limit) | 
 | 937 |         limit = sparse_limit; | 
 | 938 |  | 
 | 939 |     status = fb_getvar(usb, pType, "partition-type:%s", partition); | 
 | 940 |     if (status) { | 
 | 941 |         errMsg = "Can't determine partition type.\n"; | 
 | 942 |         goto failed; | 
 | 943 |     } | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 944 |     if (type_override) { | 
 | 945 |         if (strcmp(type_override, pType)) { | 
 | 946 |             fprintf(stderr, | 
 | 947 |                     "Warning: %s type is %s, but %s was requested for formating.\n", | 
 | 948 |                     partition, pType, type_override); | 
 | 949 |         } | 
| Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 950 |         pType = (char *)type_override; | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 951 |     } | 
| Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 952 |  | 
 | 953 |     status = fb_getvar(usb, pSize, "partition-size:%s", partition); | 
 | 954 |     if (status) { | 
 | 955 |         errMsg = "Unable to get partition size\n"; | 
 | 956 |         goto failed; | 
 | 957 |     } | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 958 |     if (size_override) { | 
 | 959 |         if (strcmp(size_override, pSize)) { | 
 | 960 |             fprintf(stderr, | 
 | 961 |                     "Warning: %s size is %s, but %s was requested for formating.\n", | 
 | 962 |                     partition, pSize, size_override); | 
 | 963 |         } | 
| Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 964 |         pSize = (char *)size_override; | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 965 |     } | 
| Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 966 |  | 
 | 967 |     gen = fs_get_generator(pType); | 
 | 968 |     if (!gen) { | 
 | 969 |         if (skip_if_not_supported) { | 
 | 970 |             fprintf(stderr, "Erase successful, but not automatically formatting.\n"); | 
 | 971 |             fprintf(stderr, "File system type %s not supported.\n", pType); | 
 | 972 |             return; | 
 | 973 |         } | 
 | 974 |         fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType); | 
 | 975 |         return; | 
 | 976 |     } | 
 | 977 |  | 
 | 978 |     pSz = strtoll(pSize, (char **)NULL, 16); | 
 | 979 |  | 
 | 980 |     fd = fileno(tmpfile()); | 
 | 981 |     if (fs_generator_generate(gen, fd, pSz)) { | 
 | 982 |         close(fd); | 
 | 983 |         fprintf(stderr, "Cannot generate image.\n"); | 
 | 984 |         return; | 
 | 985 |     } | 
 | 986 |  | 
 | 987 |     if (load_buf_fd(usb, fd, &buf)) { | 
 | 988 |         fprintf(stderr, "Cannot read image.\n"); | 
 | 989 |         close(fd); | 
 | 990 |         return; | 
 | 991 |     } | 
 | 992 |     flash_buf(partition, &buf); | 
 | 993 |  | 
 | 994 |     return; | 
 | 995 |  | 
 | 996 |  | 
 | 997 | failed: | 
 | 998 |     if (skip_if_not_supported) { | 
 | 999 |         fprintf(stderr, "Erase successful, but not automatically formatting.\n"); | 
 | 1000 |         if (errMsg) | 
 | 1001 |             fprintf(stderr, "%s", errMsg); | 
 | 1002 |     } | 
 | 1003 |     fprintf(stderr,"FAILED (%s)\n", fb_get_error()); | 
 | 1004 | } | 
 | 1005 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1006 | int main(int argc, char **argv) | 
 | 1007 | { | 
 | 1008 |     int wants_wipe = 0; | 
 | 1009 |     int wants_reboot = 0; | 
 | 1010 |     int wants_reboot_bootloader = 0; | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1011 |     int erase_first = 1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1012 |     void *data; | 
| Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 1013 |     unsigned sz; | 
| Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1014 |     int status; | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1015 |     int c; | 
| Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1016 |     int longindex; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1017 |  | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1018 |     const struct option longopts[] = { | 
 | 1019 |         {"base", required_argument, 0, 'b'}, | 
 | 1020 |         {"kernel_offset", required_argument, 0, 'k'}, | 
 | 1021 |         {"page_size", required_argument, 0, 'n'}, | 
 | 1022 |         {"ramdisk_offset", required_argument, 0, 'r'}, | 
| Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1023 |         {"tags_offset", required_argument, 0, 't'}, | 
| Elliott Hughes | e283ca2 | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1024 |         {"help", no_argument, 0, 'h'}, | 
 | 1025 |         {"unbuffered", no_argument, 0, 0}, | 
 | 1026 |         {"version", no_argument, 0, 0}, | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1027 |         {0, 0, 0, 0} | 
 | 1028 |     }; | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1029 |  | 
 | 1030 |     serial = getenv("ANDROID_SERIAL"); | 
 | 1031 |  | 
 | 1032 |     while (1) { | 
| Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1033 |         c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex); | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1034 |         if (c < 0) { | 
 | 1035 |             break; | 
 | 1036 |         } | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1037 |         /* Alphabetical cases */ | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1038 |         switch (c) { | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1039 |         case 'b': | 
 | 1040 |             base_addr = strtoul(optarg, 0, 16); | 
 | 1041 |             break; | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1042 |         case 'c': | 
 | 1043 |             cmdline = optarg; | 
 | 1044 |             break; | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1045 |         case 'h': | 
 | 1046 |             usage(); | 
 | 1047 |             return 1; | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1048 |         case 'i': { | 
 | 1049 |                 char *endptr = NULL; | 
 | 1050 |                 unsigned long val; | 
 | 1051 |  | 
 | 1052 |                 val = strtoul(optarg, &endptr, 0); | 
 | 1053 |                 if (!endptr || *endptr != '\0' || (val & ~0xffff)) | 
 | 1054 |                     die("invalid vendor id '%s'", optarg); | 
 | 1055 |                 vendor_id = (unsigned short)val; | 
 | 1056 |                 break; | 
 | 1057 |             } | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1058 |         case 'k': | 
 | 1059 |             kernel_offset = strtoul(optarg, 0, 16); | 
 | 1060 |             break; | 
 | 1061 |         case 'l': | 
 | 1062 |             long_listing = 1; | 
 | 1063 |             break; | 
 | 1064 |         case 'n': | 
 | 1065 |             page_size = (unsigned)strtoul(optarg, NULL, 0); | 
 | 1066 |             if (!page_size) die("invalid page size"); | 
 | 1067 |             break; | 
 | 1068 |         case 'p': | 
 | 1069 |             product = optarg; | 
 | 1070 |             break; | 
 | 1071 |         case 'r': | 
 | 1072 |             ramdisk_offset = strtoul(optarg, 0, 16); | 
 | 1073 |             break; | 
| Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1074 |         case 't': | 
 | 1075 |             tags_offset = strtoul(optarg, 0, 16); | 
 | 1076 |             break; | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1077 |         case 's': | 
 | 1078 |             serial = optarg; | 
 | 1079 |             break; | 
 | 1080 |         case 'S': | 
 | 1081 |             sparse_limit = parse_num(optarg); | 
 | 1082 |             if (sparse_limit < 0) { | 
 | 1083 |                     die("invalid sparse limit"); | 
 | 1084 |             } | 
 | 1085 |             break; | 
 | 1086 |         case 'u': | 
 | 1087 |             erase_first = 0; | 
 | 1088 |             break; | 
 | 1089 |         case 'w': | 
 | 1090 |             wants_wipe = 1; | 
 | 1091 |             break; | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1092 |         case '?': | 
 | 1093 |             return 1; | 
| Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1094 |         case 0: | 
 | 1095 |             if (strcmp("unbuffered", longopts[longindex].name) == 0) { | 
 | 1096 |                 setvbuf(stdout, NULL, _IONBF, 0); | 
 | 1097 |                 setvbuf(stderr, NULL, _IONBF, 0); | 
| Elliott Hughes | e283ca2 | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1098 |             } else if (strcmp("version", longopts[longindex].name) == 0) { | 
 | 1099 |                 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION); | 
 | 1100 |                 return 0; | 
| Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1101 |             } | 
 | 1102 |             break; | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1103 |         default: | 
 | 1104 |             abort(); | 
 | 1105 |         } | 
 | 1106 |     } | 
 | 1107 |  | 
 | 1108 |     argc -= optind; | 
 | 1109 |     argv += optind; | 
 | 1110 |  | 
 | 1111 |     if (argc == 0 && !wants_wipe) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1112 |         usage(); | 
| Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1113 |         return 1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1114 |     } | 
 | 1115 |  | 
| Colin Cross | 8fb6e06 | 2012-07-24 16:36:41 -0700 | [diff] [blame] | 1116 |     if (argc > 0 && !strcmp(*argv, "devices")) { | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1117 |         skip(1); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1118 |         list_devices(); | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1119 |         return 0; | 
 | 1120 |     } | 
 | 1121 |  | 
| Colin Cross | c7b75dc | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 1122 |     if (argc > 0 && !strcmp(*argv, "help")) { | 
 | 1123 |         usage(); | 
 | 1124 |         return 0; | 
 | 1125 |     } | 
 | 1126 |  | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 1127 |     usb_handle* usb = open_device(); | 
| Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 1128 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1129 |     while (argc > 0) { | 
| Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1130 |         if(!strcmp(*argv, "getvar")) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1131 |             require(2); | 
 | 1132 |             fb_queue_display(argv[1], argv[1]); | 
 | 1133 |             skip(2); | 
 | 1134 |         } else if(!strcmp(*argv, "erase")) { | 
 | 1135 |             require(2); | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1136 |  | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1137 |             if (fb_format_supported(usb, argv[1], NULL)) { | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1138 |                 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n"); | 
 | 1139 |             } | 
 | 1140 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1141 |             fb_queue_erase(argv[1]); | 
 | 1142 |             skip(2); | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1143 |         } else if(!strncmp(*argv, "format", strlen("format"))) { | 
 | 1144 |             char *overrides; | 
 | 1145 |             char *type_override = NULL; | 
 | 1146 |             char *size_override = NULL; | 
| Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1147 |             require(2); | 
| JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1148 |             /* | 
 | 1149 |              * Parsing for: "format[:[type][:[size]]]" | 
 | 1150 |              * Some valid things: | 
 | 1151 |              *  - select ontly the size, and leave default fs type: | 
 | 1152 |              *    format::0x4000000 userdata | 
 | 1153 |              *  - default fs type and size: | 
 | 1154 |              *    format userdata | 
 | 1155 |              *    format:: userdata | 
 | 1156 |              */ | 
 | 1157 |             overrides = strchr(*argv, ':'); | 
 | 1158 |             if (overrides) { | 
 | 1159 |                 overrides++; | 
 | 1160 |                 size_override = strchr(overrides, ':'); | 
 | 1161 |                 if (size_override) { | 
 | 1162 |                     size_override[0] = '\0'; | 
 | 1163 |                     size_override++; | 
 | 1164 |                 } | 
 | 1165 |                 type_override = overrides; | 
 | 1166 |             } | 
 | 1167 |             if (type_override && !type_override[0]) type_override = NULL; | 
 | 1168 |             if (size_override && !size_override[0]) size_override = NULL; | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 1169 |             if (erase_first && needs_erase(usb, argv[1])) { | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1170 |                 fb_queue_erase(argv[1]); | 
 | 1171 |             } | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 1172 |             fb_perform_format(usb, argv[1], 0, type_override, size_override); | 
| Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1173 |             skip(2); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1174 |         } else if(!strcmp(*argv, "signature")) { | 
 | 1175 |             require(2); | 
 | 1176 |             data = load_file(argv[1], &sz); | 
| Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 1177 |             if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1178 |             if (sz != 256) die("signature must be 256 bytes"); | 
 | 1179 |             fb_queue_download("signature", data, sz); | 
 | 1180 |             fb_queue_command("signature", "installing signature"); | 
 | 1181 |             skip(2); | 
 | 1182 |         } else if(!strcmp(*argv, "reboot")) { | 
 | 1183 |             wants_reboot = 1; | 
 | 1184 |             skip(1); | 
| Elliott Hughes | ca85df0 | 2015-02-25 10:02:00 -0800 | [diff] [blame] | 1185 |             if (argc > 0) { | 
 | 1186 |                 if (!strcmp(*argv, "bootloader")) { | 
 | 1187 |                     wants_reboot = 0; | 
 | 1188 |                     wants_reboot_bootloader = 1; | 
 | 1189 |                     skip(1); | 
 | 1190 |                 } | 
 | 1191 |             } | 
 | 1192 |             require(0); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1193 |         } else if(!strcmp(*argv, "reboot-bootloader")) { | 
 | 1194 |             wants_reboot_bootloader = 1; | 
 | 1195 |             skip(1); | 
 | 1196 |         } else if (!strcmp(*argv, "continue")) { | 
 | 1197 |             fb_queue_command("continue", "resuming boot"); | 
 | 1198 |             skip(1); | 
 | 1199 |         } else if(!strcmp(*argv, "boot")) { | 
 | 1200 |             char *kname = 0; | 
 | 1201 |             char *rname = 0; | 
 | 1202 |             skip(1); | 
 | 1203 |             if (argc > 0) { | 
 | 1204 |                 kname = argv[0]; | 
 | 1205 |                 skip(1); | 
 | 1206 |             } | 
 | 1207 |             if (argc > 0) { | 
 | 1208 |                 rname = argv[0]; | 
 | 1209 |                 skip(1); | 
 | 1210 |             } | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1211 |             data = load_bootable_image(kname, rname, &sz, cmdline); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1212 |             if (data == 0) return 1; | 
 | 1213 |             fb_queue_download("boot.img", data, sz); | 
 | 1214 |             fb_queue_command("boot", "booting"); | 
 | 1215 |         } else if(!strcmp(*argv, "flash")) { | 
 | 1216 |             char *pname = argv[1]; | 
 | 1217 |             char *fname = 0; | 
 | 1218 |             require(2); | 
 | 1219 |             if (argc > 2) { | 
 | 1220 |                 fname = argv[2]; | 
 | 1221 |                 skip(3); | 
 | 1222 |             } else { | 
 | 1223 |                 fname = find_item(pname, product); | 
 | 1224 |                 skip(2); | 
 | 1225 |             } | 
 | 1226 |             if (fname == 0) die("cannot determine image filename for '%s'", pname); | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 1227 |             if (erase_first && needs_erase(usb, pname)) { | 
| Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1228 |                 fb_queue_erase(pname); | 
 | 1229 |             } | 
| Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 1230 |             do_flash(usb, pname, fname); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1231 |         } else if(!strcmp(*argv, "flash:raw")) { | 
 | 1232 |             char *pname = argv[1]; | 
 | 1233 |             char *kname = argv[2]; | 
 | 1234 |             char *rname = 0; | 
 | 1235 |             require(3); | 
 | 1236 |             if(argc > 3) { | 
 | 1237 |                 rname = argv[3]; | 
 | 1238 |                 skip(4); | 
 | 1239 |             } else { | 
 | 1240 |                 skip(3); | 
 | 1241 |             } | 
| JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1242 |             data = load_bootable_image(kname, rname, &sz, cmdline); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1243 |             if (data == 0) die("cannot load bootable image"); | 
 | 1244 |             fb_queue_flash(pname, data, sz); | 
 | 1245 |         } else if(!strcmp(*argv, "flashall")) { | 
 | 1246 |             skip(1); | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1247 |             do_flashall(usb, erase_first); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1248 |             wants_reboot = 1; | 
 | 1249 |         } else if(!strcmp(*argv, "update")) { | 
 | 1250 |             if (argc > 1) { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1251 |                 do_update(usb, argv[1], erase_first); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1252 |                 skip(2); | 
 | 1253 |             } else { | 
| Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1254 |                 do_update(usb, "update.zip", erase_first); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1255 |                 skip(1); | 
 | 1256 |             } | 
 | 1257 |             wants_reboot = 1; | 
 | 1258 |         } else if(!strcmp(*argv, "oem")) { | 
 | 1259 |             argc = do_oem_command(argc, argv); | 
| Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 1260 |         } else if(!strcmp(*argv, "flashing")) { | 
 | 1261 |             if (argc == 2 && (!strcmp(*(argv+1), "unlock") || | 
 | 1262 |                               !strcmp(*(argv+1), "lock") || | 
 | 1263 |                               !strcmp(*(argv+1), "unlock_critical") || | 
 | 1264 |                               !strcmp(*(argv+1), "lock_critical") || | 
 | 1265 |                               !strcmp(*(argv+1), "get_unlock_ability") || | 
 | 1266 |                               !strcmp(*(argv+1), "get_unlock_bootloader_nonce") || | 
 | 1267 |                               !strcmp(*(argv+1), "lock_bootloader"))) { | 
 | 1268 |                 argc = do_oem_command(argc, argv); | 
 | 1269 |             } else | 
 | 1270 |             if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) { | 
 | 1271 |                 argc = do_bypass_unlock_command(argc, argv); | 
| Badhri Jagan Sridharan | a873e1d | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 1272 |             } else { | 
 | 1273 |               usage(); | 
 | 1274 |               return 1; | 
 | 1275 |             } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1276 |         } else { | 
 | 1277 |             usage(); | 
| Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1278 |             return 1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1279 |         } | 
 | 1280 |     } | 
 | 1281 |  | 
 | 1282 |     if (wants_wipe) { | 
 | 1283 |         fb_queue_erase("userdata"); | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 1284 |         fb_perform_format(usb, "userdata", 1, NULL, NULL); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1285 |         fb_queue_erase("cache"); | 
| Elliott Hughes | c688c23 | 2015-06-02 13:34:07 -0700 | [diff] [blame] | 1286 |         fb_perform_format(usb, "cache", 1, NULL, NULL); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1287 |     } | 
 | 1288 |     if (wants_reboot) { | 
 | 1289 |         fb_queue_reboot(); | 
| Mark Wachsler | ec25e7b | 2014-06-24 11:04:54 -0400 | [diff] [blame] | 1290 |         fb_queue_wait_for_disconnect(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1291 |     } else if (wants_reboot_bootloader) { | 
 | 1292 |         fb_queue_command("reboot-bootloader", "rebooting into bootloader"); | 
| Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 1293 |         fb_queue_wait_for_disconnect(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1294 |     } | 
 | 1295 |  | 
| Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 1296 |     if (fb_queue_is_empty()) | 
 | 1297 |         return 0; | 
 | 1298 |  | 
| Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1299 |     status = fb_execute_queue(usb); | 
 | 1300 |     return (status) ? 1 : 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1301 | } |