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