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