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> |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 45 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 46 | #include <functional> |
Josh Gao | 9da9ac5 | 2016-01-19 14:50:18 -0800 | [diff] [blame] | 47 | #include <utility> |
| 48 | #include <vector> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 50 | #include <android-base/macros.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 51 | #include <android-base/parseint.h> |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 52 | #include <android-base/parsenetaddress.h> |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 53 | #include <android-base/stringprintf.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 54 | #include <android-base/strings.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 55 | #include <sparse/sparse.h> |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 56 | #include <ziparchive/zip_archive.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 58 | #include "bootimg_utils.h" |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 59 | #include "diagnose_usb.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | #include "fastboot.h" |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 61 | #include "fs.h" |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 62 | #include "tcp.h" |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 63 | #include "transport.h" |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 64 | #include "udp.h" |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 65 | #include "usb.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 67 | #ifndef O_BINARY |
| 68 | #define O_BINARY 0 |
| 69 | #endif |
| 70 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 71 | char cur_product[FB_RESPONSE_SZ + 1]; |
| 72 | |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 73 | static const char* serial = nullptr; |
| 74 | static const char* product = nullptr; |
| 75 | static const char* cmdline = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | static unsigned short vendor_id = 0; |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 77 | static int long_listing = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 78 | static int64_t sparse_limit = -1; |
| 79 | static int64_t target_sparse_limit = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 81 | static unsigned page_size = 2048; |
| 82 | static unsigned base_addr = 0x10000000; |
| 83 | static unsigned kernel_offset = 0x00008000; |
| 84 | static unsigned ramdisk_offset = 0x01000000; |
| 85 | static unsigned second_offset = 0x00f00000; |
| 86 | static unsigned tags_offset = 0x00000100; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 87 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 88 | enum fb_buffer_type { |
| 89 | FB_BUFFER, |
| 90 | FB_BUFFER_SPARSE, |
| 91 | }; |
| 92 | |
| 93 | struct fastboot_buffer { |
| 94 | enum fb_buffer_type type; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 95 | void* data; |
| 96 | int64_t sz; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static struct { |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 100 | char img_name[17]; |
| 101 | char sig_name[17]; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 102 | char part_name[9]; |
| 103 | bool is_optional; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 104 | bool is_secondary; |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 105 | } images[] = { |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 106 | {"boot.img", "boot.sig", "boot", false, false}, |
| 107 | {"boot_other.img", "boot.sig", "boot", true, true}, |
| 108 | {"recovery.img", "recovery.sig", "recovery", true, false}, |
| 109 | {"system.img", "system.sig", "system", false, false}, |
| 110 | {"system_other.img", "system.sig", "system", true, true}, |
| 111 | {"vendor.img", "vendor.sig", "vendor", true, false}, |
| 112 | {"vendor_other.img", "vendor.sig", "vendor", true, true}, |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 113 | }; |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 114 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 115 | static std::string find_item_given_name(const char* img_name, const char* product) { |
| 116 | if(product) { |
| 117 | std::string path = get_my_path(); |
| 118 | path.erase(path.find_last_of('/')); |
| 119 | return android::base::StringPrintf("%s/../../../target/product/%s/%s", |
| 120 | path.c_str(), product, img_name); |
| 121 | } |
| 122 | |
| 123 | char *dir = getenv("ANDROID_PRODUCT_OUT"); |
| 124 | if (dir == nullptr || dir[0] == '\0') { |
| 125 | die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); |
| 126 | } |
| 127 | |
| 128 | return android::base::StringPrintf("%s/%s", dir, img_name); |
| 129 | } |
| 130 | |
| 131 | std::string find_item(const char* item, const char* product) { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 132 | const char *fn; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 133 | |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 134 | if (!strcmp(item,"boot")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | fn = "boot.img"; |
| 136 | } else if(!strcmp(item,"recovery")) { |
| 137 | fn = "recovery.img"; |
| 138 | } else if(!strcmp(item,"system")) { |
| 139 | fn = "system.img"; |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 140 | } else if(!strcmp(item,"vendor")) { |
| 141 | fn = "vendor.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | } else if(!strcmp(item,"userdata")) { |
| 143 | fn = "userdata.img"; |
Jean-Baptiste Queru | d7608a4 | 2011-09-30 14:39:25 -0700 | [diff] [blame] | 144 | } else if(!strcmp(item,"cache")) { |
| 145 | fn = "cache.img"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | } else if(!strcmp(item,"info")) { |
| 147 | fn = "android-info.txt"; |
| 148 | } else { |
| 149 | fprintf(stderr,"unknown partition '%s'\n", item); |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 150 | return ""; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 151 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 154 | return find_item_given_name(fn, product); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 157 | static int64_t get_file_size(int fd) { |
| 158 | struct stat sb; |
| 159 | return fstat(fd, &sb) == -1 ? -1 : sb.st_size; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 162 | static void* load_fd(int fd, int64_t* sz) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 163 | int errno_tmp; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 164 | char* data = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 166 | *sz = get_file_size(fd); |
| 167 | if (*sz < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 168 | goto oops; |
| 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 | data = (char*) malloc(*sz); |
| 172 | if (data == nullptr) goto oops; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 174 | if(read(fd, data, *sz) != *sz) goto oops; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | close(fd); |
| 176 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | return data; |
| 178 | |
| 179 | oops: |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 180 | errno_tmp = errno; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | close(fd); |
| 182 | if(data != 0) free(data); |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 183 | errno = errno_tmp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | return 0; |
| 185 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 186 | |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 187 | static void* load_file(const std::string& path, int64_t* sz) { |
| 188 | int fd = open(path.c_str(), O_RDONLY | O_BINARY); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 189 | if (fd == -1) return nullptr; |
| 190 | return load_fd(fd, sz); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 191 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 193 | 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] | 194 | // Require a matching vendor id if the user specified one with -i. |
Elliott Hughes | b46964f | 2015-08-19 17:49:45 -0700 | [diff] [blame] | 195 | if (vendor_id != 0 && info->dev_vendor != vendor_id) { |
Elliott Hughes | e1746fd | 2015-08-10 15:30:54 -0700 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) { |
| 200 | return -1; |
| 201 | } |
| 202 | |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 203 | // require matching serial number or device path if requested |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | // at the command line with the -s option. |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 205 | if (local_serial && (strcmp(local_serial, info->serial_number) != 0 && |
| 206 | strcmp(local_serial, info->device_path) != 0)) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 210 | static int match_fastboot(usb_ifc_info* info) { |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 211 | return match_fastboot_with_serial(info, serial); |
| 212 | } |
| 213 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 214 | static int list_devices_callback(usb_ifc_info* info) { |
| 215 | if (match_fastboot_with_serial(info, nullptr) == 0) { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 216 | std::string serial = info->serial_number; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 217 | if (!info->writable) { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 218 | serial = UsbNoPermissionsShortHelpText(); |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 219 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | if (!serial[0]) { |
| 221 | serial = "????????????"; |
| 222 | } |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 223 | // output compatible with "adb devices" |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 224 | if (!long_listing) { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 225 | printf("%s\tfastboot", serial.c_str()); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 226 | } else { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 227 | printf("%-22s fastboot", serial.c_str()); |
| 228 | if (strlen(info->device_path) > 0) printf(" %s", info->device_path); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 229 | } |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 230 | putchar('\n'); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return -1; |
| 234 | } |
| 235 | |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 236 | // Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify |
| 237 | // a specific device, otherwise the first USB device found will be used. |
| 238 | // |
| 239 | // If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr. |
| 240 | // Otherwise it blocks until the target is available. |
| 241 | // |
| 242 | // The returned Transport is a singleton, so multiple calls to this function will return the same |
| 243 | // object, and the caller should not attempt to delete the returned Transport. |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 244 | static Transport* open_device() { |
| 245 | static Transport* transport = nullptr; |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 246 | bool announce = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 248 | if (transport != nullptr) { |
| 249 | return transport; |
| 250 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 251 | |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 252 | Socket::Protocol protocol = Socket::Protocol::kTcp; |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 253 | std::string host; |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 254 | int port = 0; |
| 255 | if (serial != nullptr) { |
| 256 | const char* net_address = nullptr; |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 257 | |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 258 | if (android::base::StartsWith(serial, "tcp:")) { |
| 259 | protocol = Socket::Protocol::kTcp; |
| 260 | port = tcp::kDefaultPort; |
| 261 | net_address = serial + strlen("tcp:"); |
| 262 | } else if (android::base::StartsWith(serial, "udp:")) { |
| 263 | protocol = Socket::Protocol::kUdp; |
| 264 | port = udp::kDefaultPort; |
| 265 | net_address = serial + strlen("udp:"); |
| 266 | } |
| 267 | |
| 268 | if (net_address != nullptr) { |
| 269 | std::string error; |
| 270 | if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) { |
| 271 | fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address, |
| 272 | error.c_str()); |
| 273 | return nullptr; |
| 274 | } |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | while (true) { |
| 279 | if (!host.empty()) { |
| 280 | std::string error; |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 281 | if (protocol == Socket::Protocol::kTcp) { |
| 282 | transport = tcp::Connect(host, port, &error).release(); |
| 283 | } else if (protocol == Socket::Protocol::kUdp) { |
| 284 | transport = udp::Connect(host, port, &error).release(); |
| 285 | } |
| 286 | |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 287 | if (transport == nullptr && announce) { |
| 288 | fprintf(stderr, "error: %s\n", error.c_str()); |
| 289 | } |
| 290 | } else { |
| 291 | transport = usb_open(match_fastboot); |
| 292 | } |
| 293 | |
| 294 | if (transport != nullptr) { |
| 295 | return transport; |
| 296 | } |
| 297 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 298 | if (announce) { |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 299 | announce = false; |
Elliott Hughes | b46964f | 2015-08-19 17:49:45 -0700 | [diff] [blame] | 300 | 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] | 301 | } |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 302 | usleep(1000); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 306 | static void list_devices() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | // We don't actually open a USB device here, |
| 308 | // just getting our callback called so we can |
| 309 | // list all the connected devices. |
| 310 | usb_open(list_devices_callback); |
| 311 | } |
| 312 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 313 | static void usage() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 314 | fprintf(stderr, |
| 315 | /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ |
| 316 | "usage: fastboot [ <option> ] <command>\n" |
| 317 | "\n" |
| 318 | "commands:\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 319 | " update <filename> Reflash device from update.zip.\n" |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 320 | " Sets the flashed slot as active.\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 321 | " flashall Flash boot, system, vendor, and --\n" |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 322 | " if found -- recovery. If the device\n" |
| 323 | " supports slots, the slot that has\n" |
| 324 | " been flashed to is set as active.\n" |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 325 | " Secondary images may be flashed to\n" |
| 326 | " an inactive slot.\n" |
| 327 | " flash-primary Same as flashall, but do not flash\n" |
| 328 | " secondary images.\n" |
| 329 | " flash-secondary Only flashes the secondary images.\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 330 | " flash <partition> [ <filename> ] Write a file to a flash partition.\n" |
| 331 | " flashing lock Locks the device. Prevents flashing.\n" |
| 332 | " flashing unlock Unlocks the device. Allows flashing\n" |
| 333 | " any partition except\n" |
| 334 | " bootloader-related partitions.\n" |
| 335 | " flashing lock_critical Prevents flashing bootloader-related\n" |
| 336 | " partitions.\n" |
| 337 | " flashing unlock_critical Enables flashing bootloader-related\n" |
| 338 | " partitions.\n" |
| 339 | " flashing get_unlock_ability Queries bootloader to see if the\n" |
| 340 | " device is unlocked.\n" |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 341 | " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n" |
Elliott Hughes | 45be42e | 2015-09-02 20:15:48 -0700 | [diff] [blame] | 342 | " unlock nonce.\n" |
| 343 | " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n" |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 344 | " flashing lock_bootloader Locks the bootloader to prevent\n" |
Elliott Hughes | 45be42e | 2015-09-02 20:15:48 -0700 | [diff] [blame] | 345 | " bootloader version rollback.\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 346 | " erase <partition> Erase a flash partition.\n" |
| 347 | " format[:[<fs type>][:[<size>]] <partition>\n" |
| 348 | " Format a flash partition. Can\n" |
| 349 | " override the fs type and/or size\n" |
| 350 | " the bootloader reports.\n" |
| 351 | " getvar <variable> Display a bootloader variable.\n" |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 352 | " set_active <slot> Sets the active slot. If slots are\n" |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 353 | " not supported, this does nothing.\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 354 | " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n" |
| 355 | " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n" |
| 356 | " Create bootimage and flash it.\n" |
| 357 | " devices [-l] List all connected devices [with\n" |
| 358 | " device paths].\n" |
| 359 | " continue Continue with autoboot.\n" |
| 360 | " reboot [bootloader] Reboot device [into bootloader].\n" |
| 361 | " reboot-bootloader Reboot device into bootloader.\n" |
| 362 | " help Show this help message.\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 363 | "\n" |
| 364 | "options:\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 365 | " -w Erase userdata and cache (and format\n" |
| 366 | " if supported by partition type).\n" |
| 367 | " -u Do not erase partition before\n" |
| 368 | " formatting.\n" |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 369 | " -s <specific device> Specify a device. For USB, provide either\n" |
| 370 | " a serial number or path to device port.\n" |
Alex Deymo | f62d0cd | 2016-03-24 19:51:05 -0700 | [diff] [blame] | 371 | " For ethernet, provide an address in the\n" |
| 372 | " form <protocol>:<hostname>[:port] where\n" |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 373 | " <protocol> is either tcp or udp.\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 374 | " -p <product> Specify product name.\n" |
| 375 | " -c <cmdline> Override kernel commandline.\n" |
| 376 | " -i <vendor id> Specify a custom USB vendor id.\n" |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 377 | " -b, --base <base_addr> Specify a custom kernel base\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 378 | " address (default: 0x10000000).\n" |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 379 | " --kernel-offset Specify a custom kernel offset.\n" |
| 380 | " (default: 0x00008000)\n" |
| 381 | " --ramdisk-offset Specify a custom ramdisk offset.\n" |
| 382 | " (default: 0x01000000)\n" |
| 383 | " --tags-offset Specify a custom tags offset.\n" |
| 384 | " (default: 0x00000100)\n" |
| 385 | " -n, --page-size <page size> Specify the nand page size\n" |
Elliott Hughes | 08df533 | 2015-06-10 11:58:14 -0700 | [diff] [blame] | 386 | " (default: 2048).\n" |
| 387 | " -S <size>[K|M|G] Automatically sparse files greater\n" |
| 388 | " than 'size'. 0 to disable.\n" |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 389 | " --slot <slot> Specify slot name to be used if the\n" |
| 390 | " device supports slots. All operations\n" |
| 391 | " on partitions that support slots will\n" |
| 392 | " be done on the slot specified.\n" |
| 393 | " 'all' can be given to refer to all slots.\n" |
| 394 | " 'other' can be given to refer to a\n" |
| 395 | " non-current slot. If this flag is not\n" |
| 396 | " used, slotted partitions will default\n" |
| 397 | " to the current active slot.\n" |
| 398 | " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n" |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 399 | " provided, this will default to the value\n" |
| 400 | " given by --slot. If slots are not\n" |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 401 | " supported, this sets the current slot\n" |
| 402 | " to be active. This will run after all\n" |
| 403 | " non-reboot commands.\n" |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 404 | " --unbuffered Do not buffer input or output.\n" |
| 405 | " --version Display version.\n" |
| 406 | " -h, --help show this message.\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 409 | static void* load_bootable_image(const char* kernel, const char* ramdisk, |
| 410 | const char* secondstage, int64_t* sz, |
| 411 | const char* cmdline) { |
| 412 | if (kernel == nullptr) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 413 | fprintf(stderr, "no image specified\n"); |
| 414 | return 0; |
| 415 | } |
| 416 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 417 | int64_t ksize; |
| 418 | void* kdata = load_file(kernel, &ksize); |
| 419 | if (kdata == nullptr) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 420 | 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] | 421 | return 0; |
| 422 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 423 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 424 | // Is this actually a boot image? |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 425 | if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 426 | if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 427 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 428 | if (ramdisk) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n"); |
| 430 | return 0; |
| 431 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 432 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 433 | *sz = ksize; |
| 434 | return kdata; |
| 435 | } |
| 436 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 437 | void* rdata = nullptr; |
| 438 | int64_t rsize = 0; |
| 439 | if (ramdisk) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 440 | rdata = load_file(ramdisk, &rsize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 441 | if (rdata == nullptr) { |
Matt Gumbel | 64ba258 | 2011-12-08 11:59:38 -0800 | [diff] [blame] | 442 | 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] | 443 | return 0; |
| 444 | } |
| 445 | } |
| 446 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 447 | void* sdata = nullptr; |
| 448 | int64_t ssize = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 449 | if (secondstage) { |
| 450 | sdata = load_file(secondstage, &ssize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 451 | if (sdata == nullptr) { |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 452 | fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno)); |
| 453 | return 0; |
| 454 | } |
| 455 | } |
| 456 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 457 | fprintf(stderr,"creating boot image...\n"); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 458 | int64_t bsize = 0; |
| 459 | void* bdata = mkbootimg(kdata, ksize, kernel_offset, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 460 | rdata, rsize, ramdisk_offset, |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 461 | sdata, ssize, second_offset, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 462 | page_size, base_addr, tags_offset, &bsize); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 463 | if (bdata == nullptr) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 464 | fprintf(stderr,"failed to create boot.img\n"); |
| 465 | return 0; |
| 466 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 467 | if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline); |
| 468 | fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | *sz = bsize; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 470 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 471 | return bdata; |
| 472 | } |
| 473 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 474 | 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] | 475 | { |
Yusuke Sato | 0744754 | 2015-06-25 14:39:19 -0700 | [diff] [blame] | 476 | ZipString zip_entry_name(entry_name); |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 477 | ZipEntry zip_entry; |
| 478 | if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 479 | fprintf(stderr, "archive does not contain '%s'\n", entry_name); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 480 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 483 | *sz = zip_entry.uncompressed_length; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 485 | uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length)); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 486 | if (data == nullptr) { |
| 487 | 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] | 488 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 491 | int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length); |
| 492 | if (error != 0) { |
| 493 | 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] | 494 | free(data); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 495 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 496 | } |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 497 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 498 | return data; |
| 499 | } |
| 500 | |
Elliott Hughes | a26fbee | 2015-06-03 15:22:11 -0700 | [diff] [blame] | 501 | #if defined(_WIN32) |
| 502 | |
| 503 | // TODO: move this to somewhere it can be shared. |
| 504 | |
| 505 | #include <windows.h> |
| 506 | |
| 507 | // Windows' tmpfile(3) requires administrator rights because |
| 508 | // it creates temporary files in the root directory. |
| 509 | static FILE* win32_tmpfile() { |
| 510 | char temp_path[PATH_MAX]; |
| 511 | DWORD nchars = GetTempPath(sizeof(temp_path), temp_path); |
| 512 | if (nchars == 0 || nchars >= sizeof(temp_path)) { |
| 513 | fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError()); |
| 514 | return nullptr; |
| 515 | } |
| 516 | |
| 517 | char filename[PATH_MAX]; |
| 518 | if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) { |
| 519 | fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError()); |
| 520 | return nullptr; |
| 521 | } |
| 522 | |
| 523 | return fopen(filename, "w+bTD"); |
| 524 | } |
| 525 | |
| 526 | #define tmpfile win32_tmpfile |
| 527 | |
| 528 | #endif |
| 529 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 530 | static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) { |
| 531 | FILE* fp = tmpfile(); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 532 | if (fp == nullptr) { |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 533 | fprintf(stderr, "failed to create temporary file for '%s': %s\n", |
| 534 | entry_name, strerror(errno)); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 535 | return -1; |
| 536 | } |
| 537 | |
Yusuke Sato | 0744754 | 2015-06-25 14:39:19 -0700 | [diff] [blame] | 538 | ZipString zip_entry_name(entry_name); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 539 | ZipEntry zip_entry; |
| 540 | if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { |
| 541 | fprintf(stderr, "archive does not contain '%s'\n", entry_name); |
Greg Kaiser | dc9b62b | 2016-08-11 11:34:16 -0700 | [diff] [blame] | 542 | fclose(fp); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 543 | return -1; |
| 544 | } |
| 545 | |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 546 | int fd = fileno(fp); |
| 547 | int error = ExtractEntryToFile(zip, &zip_entry, fd); |
| 548 | if (error != 0) { |
| 549 | fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error)); |
Greg Kaiser | dc9b62b | 2016-08-11 11:34:16 -0700 | [diff] [blame] | 550 | fclose(fp); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 551 | return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 554 | lseek(fd, 0, SEEK_SET); |
Greg Kaiser | dc9b62b | 2016-08-11 11:34:16 -0700 | [diff] [blame] | 555 | // TODO: We're leaking 'fp' here. |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 556 | return fd; |
| 557 | } |
| 558 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 | static char *strip(char *s) |
| 560 | { |
| 561 | int n; |
| 562 | while(*s && isspace(*s)) s++; |
| 563 | n = strlen(s); |
| 564 | while(n-- > 0) { |
| 565 | if(!isspace(s[n])) break; |
| 566 | s[n] = 0; |
| 567 | } |
| 568 | return s; |
| 569 | } |
| 570 | |
| 571 | #define MAX_OPTIONS 32 |
| 572 | static int setup_requirement_line(char *name) |
| 573 | { |
| 574 | char *val[MAX_OPTIONS]; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 575 | char *prod = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 576 | unsigned n, count; |
| 577 | char *x; |
| 578 | int invert = 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 579 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | if (!strncmp(name, "reject ", 7)) { |
| 581 | name += 7; |
| 582 | invert = 1; |
| 583 | } else if (!strncmp(name, "require ", 8)) { |
| 584 | name += 8; |
| 585 | invert = 0; |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 586 | } else if (!strncmp(name, "require-for-product:", 20)) { |
| 587 | // Get the product and point name past it |
| 588 | prod = name + 20; |
| 589 | name = strchr(name, ' '); |
| 590 | if (!name) return -1; |
| 591 | *name = 0; |
| 592 | name += 1; |
| 593 | invert = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | x = strchr(name, '='); |
| 597 | if (x == 0) return 0; |
| 598 | *x = 0; |
| 599 | val[0] = x + 1; |
| 600 | |
| 601 | for(count = 1; count < MAX_OPTIONS; count++) { |
| 602 | x = strchr(val[count - 1],'|'); |
| 603 | if (x == 0) break; |
| 604 | *x = 0; |
| 605 | val[count] = x + 1; |
| 606 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 607 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 608 | name = strip(name); |
| 609 | for(n = 0; n < count; n++) val[n] = strip(val[n]); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 610 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 611 | name = strip(name); |
| 612 | if (name == 0) return -1; |
| 613 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 614 | const char* var = name; |
| 615 | // Work around an unfortunate name mismatch. |
| 616 | if (!strcmp(name,"board")) var = "product"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 617 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 618 | 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] | 619 | if (out == 0) return -1; |
| 620 | |
| 621 | for(n = 0; n < count; n++) { |
| 622 | out[n] = strdup(strip(val[n])); |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 623 | if (out[n] == 0) { |
| 624 | for(size_t i = 0; i < n; ++i) { |
| 625 | free((char*) out[i]); |
| 626 | } |
| 627 | free(out); |
| 628 | return -1; |
| 629 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 632 | fb_queue_require(prod, var, invert, n, out); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 633 | return 0; |
| 634 | } |
| 635 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 636 | static void setup_requirements(char* data, int64_t sz) { |
| 637 | char* s = data; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 638 | while (sz-- > 0) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 639 | if (*s == '\n') { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 640 | *s++ = 0; |
| 641 | if (setup_requirement_line(data)) { |
| 642 | die("out of memory"); |
| 643 | } |
| 644 | data = s; |
| 645 | } else { |
| 646 | s++; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 651 | static void queue_info_dump() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 652 | fb_queue_notice("--------------------------------------------"); |
| 653 | fb_queue_display("version-bootloader", "Bootloader Version..."); |
| 654 | fb_queue_display("version-baseband", "Baseband Version....."); |
| 655 | fb_queue_display("serialno", "Serial Number........"); |
| 656 | fb_queue_notice("--------------------------------------------"); |
| 657 | } |
| 658 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 659 | static struct sparse_file **load_sparse_files(int fd, int max_size) |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 660 | { |
Mohamad Ayyash | 80cc1f6 | 2015-03-31 12:09:29 -0700 | [diff] [blame] | 661 | struct sparse_file* s = sparse_file_import_auto(fd, false, true); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 662 | if (!s) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 663 | die("cannot sparse read file\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 666 | int files = sparse_file_resparse(s, max_size, nullptr, 0); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 667 | if (files < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 668 | die("Failed to resparse\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 671 | 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] | 672 | if (!out_s) { |
| 673 | die("Failed to allocate sparse file array\n"); |
| 674 | } |
| 675 | |
| 676 | files = sparse_file_resparse(s, max_size, out_s, files); |
| 677 | if (files < 0) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 678 | die("Failed to resparse\n"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | return out_s; |
| 682 | } |
| 683 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 684 | static int64_t get_target_sparse_limit(Transport* transport) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 685 | std::string max_download_size; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 686 | if (!fb_getvar(transport, "max-download-size", &max_download_size) || |
| 687 | max_download_size.empty()) { |
Elliott Hughes | 3ab0586 | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 688 | fprintf(stderr, "target didn't report max-download-size\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 689 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Elliott Hughes | 77c0e66 | 2015-11-02 15:51:12 -0800 | [diff] [blame] | 692 | // Some bootloaders (angler, for example) send spurious whitespace too. |
| 693 | max_download_size = android::base::Trim(max_download_size); |
| 694 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 695 | uint64_t limit; |
| 696 | if (!android::base::ParseUint(max_download_size.c_str(), &limit)) { |
Elliott Hughes | 3ab0586 | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 697 | fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str()); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | if (limit > 0) { |
| 701 | fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit); |
| 702 | } |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 703 | return limit; |
| 704 | } |
| 705 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 706 | static int64_t get_sparse_limit(Transport* transport, int64_t size) { |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 707 | int64_t limit; |
| 708 | |
| 709 | if (sparse_limit == 0) { |
| 710 | return 0; |
| 711 | } else if (sparse_limit > 0) { |
| 712 | limit = sparse_limit; |
| 713 | } else { |
| 714 | if (target_sparse_limit == -1) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 715 | target_sparse_limit = get_target_sparse_limit(transport); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 716 | } |
| 717 | if (target_sparse_limit > 0) { |
| 718 | limit = target_sparse_limit; |
| 719 | } else { |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 720 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | if (size > limit) { |
| 725 | return limit; |
| 726 | } |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 731 | // Until we get lazy inode table init working in make_ext4fs, we need to |
| 732 | // erase partitions of type ext4 before flashing a filesystem so no stale |
| 733 | // inodes are left lying around. Otherwise, e2fsck gets very upset. |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 734 | static bool needs_erase(Transport* transport, const char* partition) { |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 735 | std::string partition_type; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 736 | if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) { |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 737 | return false; |
| 738 | } |
| 739 | return partition_type == "ext4"; |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 742 | static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 743 | int64_t sz = get_file_size(fd); |
| 744 | if (sz == -1) { |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 745 | return false; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 746 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 747 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 748 | lseek64(fd, 0, SEEK_SET); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 749 | int64_t limit = get_sparse_limit(transport, sz); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 750 | if (limit) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 751 | sparse_file** s = load_sparse_files(fd, limit); |
| 752 | if (s == nullptr) { |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 753 | return false; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 754 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 755 | buf->type = FB_BUFFER_SPARSE; |
| 756 | buf->data = s; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 757 | } else { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 758 | void* data = load_fd(fd, &sz); |
| 759 | if (data == nullptr) return -1; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 760 | buf->type = FB_BUFFER; |
Sasha Levitskiy | 782111b | 2014-05-05 19:43:15 -0700 | [diff] [blame] | 761 | buf->data = data; |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 762 | buf->sz = sz; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 763 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 764 | |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 765 | return true; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 768 | static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) { |
| 769 | int fd = open(fname, O_RDONLY | O_BINARY); |
| 770 | if (fd == -1) { |
| 771 | return false; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 772 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 773 | return load_buf_fd(transport, fd, buf); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | static void flash_buf(const char *pname, struct fastboot_buffer *buf) |
| 777 | { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 778 | sparse_file** s; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 779 | |
| 780 | switch (buf->type) { |
Josh Gao | 9da9ac5 | 2016-01-19 14:50:18 -0800 | [diff] [blame] | 781 | case FB_BUFFER_SPARSE: { |
| 782 | std::vector<std::pair<sparse_file*, int64_t>> sparse_files; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 783 | s = reinterpret_cast<sparse_file**>(buf->data); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 784 | while (*s) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 785 | int64_t sz = sparse_file_len(*s, true, false); |
Josh Gao | 9da9ac5 | 2016-01-19 14:50:18 -0800 | [diff] [blame] | 786 | sparse_files.emplace_back(*s, sz); |
| 787 | ++s; |
| 788 | } |
| 789 | |
| 790 | for (size_t i = 0; i < sparse_files.size(); ++i) { |
| 791 | const auto& pair = sparse_files[i]; |
| 792 | fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size()); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 793 | } |
| 794 | break; |
Josh Gao | 9da9ac5 | 2016-01-19 14:50:18 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 797 | case FB_BUFFER: |
| 798 | fb_queue_flash(pname, buf->data, buf->sz); |
| 799 | break; |
| 800 | default: |
| 801 | die("unknown buffer type: %d", buf->type); |
| 802 | } |
| 803 | } |
| 804 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 805 | static std::string get_current_slot(Transport* transport) |
| 806 | { |
| 807 | std::string current_slot; |
| 808 | if (fb_getvar(transport, "current-slot", ¤t_slot)) { |
| 809 | if (current_slot == "_a") return "a"; // Legacy support |
| 810 | if (current_slot == "_b") return "b"; // Legacy support |
| 811 | return current_slot; |
| 812 | } |
| 813 | return ""; |
| 814 | } |
| 815 | |
| 816 | // Legacy support |
| 817 | static std::vector<std::string> get_suffixes_obsolete(Transport* transport) { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 818 | std::vector<std::string> suffixes; |
| 819 | std::string suffix_list; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 820 | if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) { |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 821 | return suffixes; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 822 | } |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 823 | suffixes = android::base::Split(suffix_list, ","); |
| 824 | // Unfortunately some devices will return an error message in the |
| 825 | // guise of a valid value. If we only see only one suffix, it's probably |
| 826 | // not real. |
| 827 | if (suffixes.size() == 1) { |
| 828 | suffixes.clear(); |
| 829 | } |
| 830 | return suffixes; |
| 831 | } |
| 832 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 833 | // Legacy support |
| 834 | static bool supports_AB_obsolete(Transport* transport) { |
| 835 | return !get_suffixes_obsolete(transport).empty(); |
| 836 | } |
| 837 | |
| 838 | static int get_slot_count(Transport* transport) { |
| 839 | std::string var; |
| 840 | int count; |
| 841 | if (!fb_getvar(transport, "slot-count", &var)) { |
| 842 | if (supports_AB_obsolete(transport)) return 2; // Legacy support |
| 843 | } |
| 844 | if (!android::base::ParseInt(var.c_str(), &count)) return 0; |
| 845 | return count; |
| 846 | } |
| 847 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 848 | static bool supports_AB(Transport* transport) { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 849 | return get_slot_count(transport) >= 2; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 852 | // Given a current slot, this returns what the 'other' slot is. |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 853 | static std::string get_other_slot(const std::string& current_slot, int count) { |
| 854 | if (count == 0) return ""; |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 855 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 856 | char next = (current_slot[0] - 'a' + 1)%count + 'a'; |
| 857 | return std::string(1, next); |
| 858 | } |
| 859 | |
| 860 | static std::string get_other_slot(Transport* transport, const std::string& current_slot) { |
| 861 | return get_other_slot(current_slot, get_slot_count(transport)); |
| 862 | } |
| 863 | |
| 864 | static std::string get_other_slot(Transport* transport, int count) { |
| 865 | return get_other_slot(get_current_slot(transport), count); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 868 | static std::string get_other_slot(Transport* transport) { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 869 | return get_other_slot(get_current_slot(transport), get_slot_count(transport)); |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 872 | static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) { |
| 873 | std::string slot = slot_name; |
| 874 | if (slot == "_a") slot = "a"; // Legacy support |
| 875 | if (slot == "_b") slot = "b"; // Legacy support |
| 876 | if (slot == "all") { |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 877 | if (allow_all) { |
| 878 | return "all"; |
| 879 | } else { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 880 | int count = get_slot_count(transport); |
| 881 | if (count > 0) { |
| 882 | return "a"; |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 883 | } else { |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 884 | die("No known slots."); |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 885 | } |
| 886 | } |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 887 | } |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 888 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 889 | int count = get_slot_count(transport); |
| 890 | if (count == 0) die("Device does not support slots.\n"); |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 891 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 892 | if (slot == "other") { |
| 893 | std::string other = get_other_slot(transport, count); |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 894 | if (other == "") { |
| 895 | die("No known slots."); |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 896 | } |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 897 | return other; |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 900 | if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot; |
| 901 | |
| 902 | fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str()); |
| 903 | for (int i=0; i<count; i++) { |
| 904 | fprintf(stderr, "%c\n", (char)(i + 'a')); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 905 | } |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 906 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 907 | exit(1); |
| 908 | } |
| 909 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 910 | static std::string verify_slot(Transport* transport, const std::string& slot) { |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 911 | return verify_slot(transport, slot, true); |
| 912 | } |
| 913 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 914 | static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot, |
Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 915 | const std::function<void(const std::string&)>& func, bool force_slot) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 916 | std::string has_slot; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 917 | std::string current_slot; |
| 918 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 919 | if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 920 | /* If has-slot is not supported, the answer is no. */ |
| 921 | has_slot = "no"; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 922 | } |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 923 | if (has_slot == "yes") { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 924 | if (slot == "") { |
| 925 | current_slot = get_current_slot(transport); |
| 926 | if (current_slot == "") { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 927 | die("Failed to identify current slot.\n"); |
| 928 | } |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 929 | func(part + "_" + current_slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 930 | } else { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 931 | func(part + '_' + slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 932 | } |
| 933 | } else { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 934 | if (force_slot && slot != "") { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 935 | fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n", |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 936 | part.c_str(), slot.c_str()); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 937 | } |
| 938 | func(part); |
| 939 | } |
| 940 | } |
| 941 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 942 | /* This function will find the real partition name given a base name, and a slot. If slot is NULL or |
| 943 | * empty, it will use the current slot. If slot is "all", it will return a list of all possible |
| 944 | * partition names. If force_slot is true, it will fail if a slot is specified, and the given |
| 945 | * partition does not support slots. |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 946 | */ |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 947 | static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot, |
Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 948 | const std::function<void(const std::string&)>& func, bool force_slot) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 949 | std::string has_slot; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 950 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 951 | if (slot == "all") { |
| 952 | if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) { |
| 953 | die("Could not check if partition %s has slot.", part.c_str()); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 954 | } |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 955 | if (has_slot == "yes") { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 956 | for (int i=0; i < get_slot_count(transport); i++) { |
| 957 | do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 958 | } |
| 959 | } else { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 960 | do_for_partition(transport, part, "", func, force_slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 961 | } |
| 962 | } else { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 963 | do_for_partition(transport, part, slot, func, force_slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 967 | static void do_flash(Transport* transport, const char* pname, const char* fname) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 968 | struct fastboot_buffer buf; |
| 969 | |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 970 | if (!load_buf(transport, fname, &buf)) { |
| 971 | die("cannot load '%s': %s", fname, strerror(errno)); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 972 | } |
| 973 | flash_buf(pname, &buf); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 976 | static void do_update_signature(ZipArchiveHandle zip, char* fn) { |
| 977 | int64_t sz; |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 978 | void* data = unzip_file(zip, fn, &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 979 | if (data == nullptr) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 980 | fb_queue_download("signature", data, sz); |
| 981 | fb_queue_command("signature", "installing signature"); |
| 982 | } |
| 983 | |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 984 | // Sets slot_override as the active slot. If slot_override is blank, |
| 985 | // set current slot as active instead. This clears slot-unbootable. |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 986 | static void set_active(Transport* transport, const std::string& slot_override) { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 987 | std::string separator = ""; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 988 | if (!supports_AB(transport)) { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 989 | if (supports_AB_obsolete(transport)) { |
| 990 | separator = "_"; // Legacy support |
| 991 | } else { |
| 992 | return; |
| 993 | } |
| 994 | } |
| 995 | if (slot_override != "") { |
| 996 | fb_set_active((separator + slot_override).c_str()); |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 997 | } else { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 998 | std::string current_slot = get_current_slot(transport); |
| 999 | if (current_slot != "") { |
| 1000 | fb_set_active((separator + current_slot).c_str()); |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1005 | static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1006 | queue_info_dump(); |
| 1007 | |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 1008 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
| 1009 | |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 1010 | ZipArchiveHandle zip; |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 1011 | int error = OpenArchive(filename, &zip); |
| 1012 | if (error != 0) { |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 1013 | CloseArchive(zip); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 1014 | die("failed to open zip file '%s': %s", filename, ErrorCodeString(error)); |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 1015 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1016 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1017 | int64_t sz; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 1018 | void* data = unzip_file(zip, "android-info.txt", &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1019 | if (data == nullptr) { |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 1020 | CloseArchive(zip); |
Elliott Hughes | 7c6d884 | 2015-03-19 10:30:53 -0700 | [diff] [blame] | 1021 | 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] | 1022 | } |
| 1023 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 1024 | setup_requirements(reinterpret_cast<char*>(data), sz); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1025 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1026 | std::string secondary; |
| 1027 | bool update_secondary = slot_override != "all"; |
| 1028 | if (update_secondary) { |
| 1029 | if (slot_override != "") { |
| 1030 | secondary = get_other_slot(transport, slot_override); |
| 1031 | } else { |
| 1032 | secondary = get_other_slot(transport); |
| 1033 | } |
| 1034 | if (secondary == "") { |
| 1035 | if (supports_AB(transport)) { |
| 1036 | fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n"); |
| 1037 | } |
| 1038 | update_secondary = false; |
| 1039 | } |
| 1040 | } |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 1041 | for (size_t i = 0; i < arraysize(images); ++i) { |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1042 | const char* slot = slot_override.c_str(); |
| 1043 | if (images[i].is_secondary) { |
| 1044 | if (update_secondary) { |
| 1045 | slot = secondary.c_str(); |
| 1046 | } else { |
| 1047 | continue; |
| 1048 | } |
| 1049 | } |
| 1050 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 1051 | int fd = unzip_to_file(zip, images[i].img_name); |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 1052 | if (fd == -1) { |
| 1053 | if (images[i].is_optional) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1054 | continue; |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 1055 | } |
Narayan Kamath | f6e9ffb | 2015-05-11 16:59:46 +0100 | [diff] [blame] | 1056 | CloseArchive(zip); |
Elliott Hughes | acdbe92 | 2015-06-02 21:37:05 -0700 | [diff] [blame] | 1057 | exit(1); // unzip_to_file already explained why. |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1058 | } |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 1059 | fastboot_buffer buf; |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 1060 | if (!load_buf_fd(transport, fd, &buf)) { |
| 1061 | die("cannot load %s from flash: %s", images[i].img_name, strerror(errno)); |
| 1062 | } |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1063 | |
| 1064 | auto update = [&](const std::string &partition) { |
| 1065 | do_update_signature(zip, images[i].sig_name); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1066 | if (erase_first && needs_erase(transport, partition.c_str())) { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1067 | fb_queue_erase(partition.c_str()); |
| 1068 | } |
| 1069 | flash_buf(partition.c_str(), &buf); |
| 1070 | /* not closing the fd here since the sparse code keeps the fd around |
| 1071 | * but hasn't mmaped data yet. The tmpfile will get cleaned up when the |
| 1072 | * program exits. |
| 1073 | */ |
| 1074 | }; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1075 | do_for_partitions(transport, images[i].part_name, slot, update, false); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1076 | } |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 1077 | |
| 1078 | CloseArchive(zip); |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1079 | set_active(transport, slot_override); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1082 | static void do_send_signature(const std::string& fn) { |
| 1083 | std::size_t extension_loc = fn.find(".img"); |
| 1084 | if (extension_loc == std::string::npos) return; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1085 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1086 | std::string fs_sig = fn.substr(0, extension_loc) + ".sig"; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1087 | |
| 1088 | int64_t sz; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1089 | void* data = load_file(fs_sig.c_str(), &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1090 | if (data == nullptr) return; |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 1091 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1092 | fb_queue_download("signature", data, sz); |
| 1093 | fb_queue_command("signature", "installing signature"); |
| 1094 | } |
| 1095 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1096 | static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool flash_primary, bool flash_secondary) { |
| 1097 | std::string fname; |
| 1098 | if (flash_primary) { |
| 1099 | queue_info_dump(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1100 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1101 | fb_queue_query_save("product", cur_product, sizeof(cur_product)); |
Wink Saville | b98762f | 2011-04-04 17:54:59 -0700 | [diff] [blame] | 1102 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1103 | fname = find_item("info", product); |
| 1104 | if (fname.empty()) die("cannot find android-info.txt"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1105 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1106 | int64_t sz; |
| 1107 | void* data = load_file(fname.c_str(), &sz); |
| 1108 | 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] | 1109 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1110 | setup_requirements(reinterpret_cast<char*>(data), sz); |
| 1111 | } |
| 1112 | std::string secondary; |
| 1113 | if (flash_secondary) { |
| 1114 | if (slot_override != "") { |
| 1115 | secondary = get_other_slot(transport, slot_override); |
| 1116 | } else { |
| 1117 | secondary = get_other_slot(transport); |
| 1118 | } |
| 1119 | if (secondary == "") { |
| 1120 | if (supports_AB(transport)) { |
| 1121 | fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n"); |
| 1122 | } |
| 1123 | flash_secondary = false; |
| 1124 | } |
| 1125 | } |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 1126 | |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 1127 | for (size_t i = 0; i < arraysize(images); i++) { |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1128 | const char* slot = NULL; |
| 1129 | if (images[i].is_secondary) { |
| 1130 | if (flash_secondary) slot = secondary.c_str(); |
| 1131 | } else { |
| 1132 | if (flash_primary) slot = slot_override.c_str(); |
| 1133 | } |
| 1134 | if (!slot) continue; |
| 1135 | fname = find_item_given_name(images[i].img_name, product); |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 1136 | fastboot_buffer buf; |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 1137 | if (!load_buf(transport, fname.c_str(), &buf)) { |
| 1138 | if (images[i].is_optional) continue; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1139 | die("could not load '%s': %s\n", images[i].img_name, strerror(errno)); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1140 | } |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1141 | |
| 1142 | auto flashall = [&](const std::string &partition) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 1143 | do_send_signature(fname.c_str()); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1144 | if (erase_first && needs_erase(transport, partition.c_str())) { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1145 | fb_queue_erase(partition.c_str()); |
| 1146 | } |
| 1147 | flash_buf(partition.c_str(), &buf); |
| 1148 | }; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1149 | do_for_partitions(transport, images[i].part_name, slot, flashall, false); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1150 | } |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1151 | |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1152 | if (flash_primary) set_active(transport, slot_override); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | #define skip(n) do { argc -= (n); argv += (n); } while (0) |
JP Abgrall | 2d13d14 | 2011-03-01 23:35:07 -0800 | [diff] [blame] | 1156 | #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] | 1157 | |
Elliott Hughes | 45be42e | 2015-09-02 20:15:48 -0700 | [diff] [blame] | 1158 | static int do_bypass_unlock_command(int argc, char **argv) |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 1159 | { |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 1160 | if (argc <= 2) return 0; |
| 1161 | skip(2); |
| 1162 | |
| 1163 | /* |
| 1164 | * Process unlock_bootloader, we have to load the message file |
| 1165 | * and send that to the remote device. |
| 1166 | */ |
| 1167 | require(1); |
Elliott Hughes | 259ad4a | 2015-09-02 20:33:44 -0700 | [diff] [blame] | 1168 | |
| 1169 | int64_t sz; |
| 1170 | void* data = load_file(*argv, &sz); |
| 1171 | if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno)); |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 1172 | fb_queue_download("unlock_message", data, sz); |
| 1173 | fb_queue_command("flashing unlock_bootloader", "unlocking bootloader"); |
| 1174 | skip(1); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Elliott Hughes | 45be42e | 2015-09-02 20:15:48 -0700 | [diff] [blame] | 1178 | static int do_oem_command(int argc, char **argv) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1179 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1180 | char command[256]; |
| 1181 | if (argc <= 1) return 0; |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1182 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1183 | command[0] = 0; |
| 1184 | while(1) { |
| 1185 | strcat(command,*argv); |
| 1186 | skip(1); |
| 1187 | if(argc == 0) break; |
| 1188 | strcat(command," "); |
| 1189 | } |
| 1190 | |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1191 | fb_queue_command(command,""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1192 | return 0; |
| 1193 | } |
| 1194 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 1195 | static int64_t parse_num(const char *arg) |
| 1196 | { |
| 1197 | char *endptr; |
| 1198 | unsigned long long num; |
| 1199 | |
| 1200 | num = strtoull(arg, &endptr, 0); |
| 1201 | if (endptr == arg) { |
| 1202 | return -1; |
| 1203 | } |
| 1204 | |
| 1205 | if (*endptr == 'k' || *endptr == 'K') { |
| 1206 | if (num >= (-1ULL) / 1024) { |
| 1207 | return -1; |
| 1208 | } |
| 1209 | num *= 1024LL; |
| 1210 | endptr++; |
| 1211 | } else if (*endptr == 'm' || *endptr == 'M') { |
| 1212 | if (num >= (-1ULL) / (1024 * 1024)) { |
| 1213 | return -1; |
| 1214 | } |
| 1215 | num *= 1024LL * 1024LL; |
| 1216 | endptr++; |
| 1217 | } else if (*endptr == 'g' || *endptr == 'G') { |
| 1218 | if (num >= (-1ULL) / (1024 * 1024 * 1024)) { |
| 1219 | return -1; |
| 1220 | } |
| 1221 | num *= 1024LL * 1024LL * 1024LL; |
| 1222 | endptr++; |
| 1223 | } |
| 1224 | |
| 1225 | if (*endptr != '\0') { |
| 1226 | return -1; |
| 1227 | } |
| 1228 | |
| 1229 | if (num > INT64_MAX) { |
| 1230 | return -1; |
| 1231 | } |
| 1232 | |
| 1233 | return num; |
| 1234 | } |
| 1235 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1236 | static void fb_perform_format(Transport* transport, |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1237 | const char* partition, int skip_if_not_supported, |
| 1238 | const char* type_override, const char* size_override) { |
| 1239 | std::string partition_type, partition_size; |
| 1240 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1241 | struct fastboot_buffer buf; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1242 | const char* errMsg = nullptr; |
| 1243 | const struct fs_generator* gen = nullptr; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1244 | int fd; |
| 1245 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1246 | unsigned int limit = INT_MAX; |
| 1247 | if (target_sparse_limit > 0 && target_sparse_limit < limit) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1248 | limit = target_sparse_limit; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1249 | } |
| 1250 | if (sparse_limit > 0 && sparse_limit < limit) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1251 | limit = sparse_limit; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1252 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1253 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1254 | if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1255 | errMsg = "Can't determine partition type.\n"; |
| 1256 | goto failed; |
| 1257 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1258 | if (type_override) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1259 | if (partition_type != type_override) { |
| 1260 | fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n", |
| 1261 | partition, partition_type.c_str(), type_override); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1262 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1263 | partition_type = type_override; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1264 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1265 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1266 | if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1267 | errMsg = "Unable to get partition size\n"; |
| 1268 | goto failed; |
| 1269 | } |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1270 | if (size_override) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1271 | if (partition_size != size_override) { |
| 1272 | fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n", |
| 1273 | partition, partition_size.c_str(), size_override); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1274 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1275 | partition_size = size_override; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1276 | } |
Elliott Hughes | a2db261 | 2015-11-12 07:28:39 -0800 | [diff] [blame] | 1277 | // Some bootloaders (angler, for example), send spurious leading whitespace. |
| 1278 | partition_size = android::base::Trim(partition_size); |
| 1279 | // Some bootloaders (hammerhead, for example) use implicit hex. |
| 1280 | // This code used to use strtol with base 16. |
| 1281 | if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1282 | |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 1283 | gen = fs_get_generator(partition_type); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1284 | if (!gen) { |
| 1285 | if (skip_if_not_supported) { |
| 1286 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1287 | 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] | 1288 | return; |
| 1289 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1290 | fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n", |
| 1291 | partition_type.c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1292 | return; |
| 1293 | } |
| 1294 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1295 | int64_t size; |
| 1296 | if (!android::base::ParseInt(partition_size.c_str(), &size)) { |
| 1297 | fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str()); |
| 1298 | return; |
| 1299 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1300 | |
| 1301 | fd = fileno(tmpfile()); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1302 | if (fs_generator_generate(gen, fd, size)) { |
| 1303 | fprintf(stderr, "Cannot generate image: %s\n", strerror(errno)); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1304 | close(fd); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1305 | return; |
| 1306 | } |
| 1307 | |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 1308 | if (!load_buf_fd(transport, fd, &buf)) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1309 | fprintf(stderr, "Cannot read image: %s\n", strerror(errno)); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1310 | close(fd); |
| 1311 | return; |
| 1312 | } |
| 1313 | flash_buf(partition, &buf); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1314 | return; |
| 1315 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1316 | failed: |
| 1317 | if (skip_if_not_supported) { |
| 1318 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1319 | if (errMsg) fprintf(stderr, "%s", errMsg); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1320 | } |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 1321 | fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1324 | int main(int argc, char **argv) |
| 1325 | { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1326 | bool wants_wipe = false; |
| 1327 | bool wants_reboot = false; |
| 1328 | bool wants_reboot_bootloader = false; |
| 1329 | bool wants_set_active = false; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1330 | bool erase_first = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1331 | void *data; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1332 | int64_t sz; |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1333 | int longindex; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1334 | std::string slot_override; |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1335 | std::string next_active; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1336 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1337 | const struct option longopts[] = { |
| 1338 | {"base", required_argument, 0, 'b'}, |
| 1339 | {"kernel_offset", required_argument, 0, 'k'}, |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 1340 | {"kernel-offset", required_argument, 0, 'k'}, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1341 | {"page_size", required_argument, 0, 'n'}, |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 1342 | {"page-size", required_argument, 0, 'n'}, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1343 | {"ramdisk_offset", required_argument, 0, 'r'}, |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 1344 | {"ramdisk-offset", required_argument, 0, 'r'}, |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1345 | {"tags_offset", required_argument, 0, 't'}, |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 1346 | {"tags-offset", required_argument, 0, 't'}, |
Elliott Hughes | 379646b | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1347 | {"help", no_argument, 0, 'h'}, |
| 1348 | {"unbuffered", no_argument, 0, 0}, |
| 1349 | {"version", no_argument, 0, 0}, |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1350 | {"slot", required_argument, 0, 0}, |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1351 | {"set_active", optional_argument, 0, 'a'}, |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 1352 | {"set-active", optional_argument, 0, 'a'}, |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1353 | {0, 0, 0, 0} |
| 1354 | }; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1355 | |
| 1356 | serial = getenv("ANDROID_SERIAL"); |
| 1357 | |
| 1358 | while (1) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1359 | int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex); |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1360 | if (c < 0) { |
| 1361 | break; |
| 1362 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1363 | /* Alphabetical cases */ |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1364 | switch (c) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1365 | case 'a': |
| 1366 | wants_set_active = true; |
| 1367 | if (optarg) |
| 1368 | next_active = optarg; |
| 1369 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1370 | case 'b': |
| 1371 | base_addr = strtoul(optarg, 0, 16); |
| 1372 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1373 | case 'c': |
| 1374 | cmdline = optarg; |
| 1375 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1376 | case 'h': |
| 1377 | usage(); |
| 1378 | return 1; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1379 | case 'i': { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1380 | char *endptr = nullptr; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1381 | unsigned long val; |
| 1382 | |
| 1383 | val = strtoul(optarg, &endptr, 0); |
| 1384 | if (!endptr || *endptr != '\0' || (val & ~0xffff)) |
| 1385 | die("invalid vendor id '%s'", optarg); |
| 1386 | vendor_id = (unsigned short)val; |
| 1387 | break; |
| 1388 | } |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1389 | case 'k': |
| 1390 | kernel_offset = strtoul(optarg, 0, 16); |
| 1391 | break; |
| 1392 | case 'l': |
| 1393 | long_listing = 1; |
| 1394 | break; |
| 1395 | case 'n': |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1396 | page_size = (unsigned)strtoul(optarg, nullptr, 0); |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1397 | if (!page_size) die("invalid page size"); |
| 1398 | break; |
| 1399 | case 'p': |
| 1400 | product = optarg; |
| 1401 | break; |
| 1402 | case 'r': |
| 1403 | ramdisk_offset = strtoul(optarg, 0, 16); |
| 1404 | break; |
Mohamad Ayyash | 29fd7b1 | 2014-01-14 18:27:25 -0800 | [diff] [blame] | 1405 | case 't': |
| 1406 | tags_offset = strtoul(optarg, 0, 16); |
| 1407 | break; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1408 | case 's': |
| 1409 | serial = optarg; |
| 1410 | break; |
| 1411 | case 'S': |
| 1412 | sparse_limit = parse_num(optarg); |
| 1413 | if (sparse_limit < 0) { |
| 1414 | die("invalid sparse limit"); |
| 1415 | } |
| 1416 | break; |
| 1417 | case 'u': |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1418 | erase_first = false; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1419 | break; |
| 1420 | case 'w': |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1421 | wants_wipe = true; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1422 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1423 | case '?': |
| 1424 | return 1; |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1425 | case 0: |
| 1426 | if (strcmp("unbuffered", longopts[longindex].name) == 0) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1427 | setvbuf(stdout, nullptr, _IONBF, 0); |
| 1428 | setvbuf(stderr, nullptr, _IONBF, 0); |
Elliott Hughes | 379646b | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1429 | } else if (strcmp("version", longopts[longindex].name) == 0) { |
| 1430 | fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION); |
| 1431 | return 0; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1432 | } else if (strcmp("slot", longopts[longindex].name) == 0) { |
| 1433 | slot_override = std::string(optarg); |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1434 | } |
| 1435 | break; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1436 | default: |
| 1437 | abort(); |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | argc -= optind; |
| 1442 | argv += optind; |
| 1443 | |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1444 | if (argc == 0 && !wants_wipe && !wants_set_active) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1445 | usage(); |
Brian Carlstrom | eb31c0b | 2010-04-23 12:38:51 -0700 | [diff] [blame] | 1446 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1447 | } |
| 1448 | |
Colin Cross | 8fb6e06 | 2012-07-24 16:36:41 -0700 | [diff] [blame] | 1449 | if (argc > 0 && !strcmp(*argv, "devices")) { |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1450 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1451 | list_devices(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1452 | return 0; |
| 1453 | } |
| 1454 | |
Colin Cross | c7b75dc | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 1455 | if (argc > 0 && !strcmp(*argv, "help")) { |
| 1456 | usage(); |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1460 | Transport* transport = open_device(); |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 1461 | if (transport == nullptr) { |
| 1462 | return 1; |
| 1463 | } |
| 1464 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 1465 | if (!supports_AB(transport) && supports_AB_obsolete(transport)) { |
| 1466 | fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n"); |
| 1467 | } |
| 1468 | if (slot_override != "") slot_override = verify_slot(transport, slot_override); |
| 1469 | if (next_active != "") next_active = verify_slot(transport, next_active, false); |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1470 | |
| 1471 | if (wants_set_active) { |
| 1472 | if (next_active == "") { |
| 1473 | if (slot_override == "") { |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1474 | std::string current_slot; |
| 1475 | if (fb_getvar(transport, "current-slot", ¤t_slot)) { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 1476 | next_active = verify_slot(transport, current_slot, false); |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1477 | } else { |
| 1478 | wants_set_active = false; |
| 1479 | } |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1480 | } else { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 1481 | next_active = verify_slot(transport, slot_override, false); |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | } |
Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 1485 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1486 | while (argc > 0) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1487 | if (!strcmp(*argv, "getvar")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1488 | require(2); |
| 1489 | fb_queue_display(argv[1], argv[1]); |
| 1490 | skip(2); |
| 1491 | } else if(!strcmp(*argv, "erase")) { |
| 1492 | require(2); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1493 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1494 | auto erase = [&](const std::string &partition) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1495 | std::string partition_type; |
| 1496 | if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) && |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1497 | fs_get_generator(partition_type) != nullptr) { |
| 1498 | fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n", |
| 1499 | partition_type.c_str()); |
| 1500 | } |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1501 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1502 | fb_queue_erase(partition.c_str()); |
| 1503 | }; |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 1504 | do_for_partitions(transport, argv[1], slot_override, erase, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1505 | skip(2); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1506 | } else if(!strncmp(*argv, "format", strlen("format"))) { |
| 1507 | char *overrides; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1508 | char *type_override = nullptr; |
| 1509 | char *size_override = nullptr; |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1510 | require(2); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1511 | /* |
| 1512 | * Parsing for: "format[:[type][:[size]]]" |
| 1513 | * Some valid things: |
| 1514 | * - select ontly the size, and leave default fs type: |
| 1515 | * format::0x4000000 userdata |
| 1516 | * - default fs type and size: |
| 1517 | * format userdata |
| 1518 | * format:: userdata |
| 1519 | */ |
| 1520 | overrides = strchr(*argv, ':'); |
| 1521 | if (overrides) { |
| 1522 | overrides++; |
| 1523 | size_override = strchr(overrides, ':'); |
| 1524 | if (size_override) { |
| 1525 | size_override[0] = '\0'; |
| 1526 | size_override++; |
| 1527 | } |
| 1528 | type_override = overrides; |
| 1529 | } |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1530 | if (type_override && !type_override[0]) type_override = nullptr; |
| 1531 | if (size_override && !size_override[0]) size_override = nullptr; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1532 | |
| 1533 | auto format = [&](const std::string &partition) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1534 | if (erase_first && needs_erase(transport, partition.c_str())) { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1535 | fb_queue_erase(partition.c_str()); |
| 1536 | } |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1537 | fb_perform_format(transport, partition.c_str(), 0, type_override, size_override); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1538 | }; |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 1539 | do_for_partitions(transport, argv[1], slot_override, format, true); |
Anatol Pomazau | c8ba536 | 2011-12-15 17:50:18 -0800 | [diff] [blame] | 1540 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1541 | } else if(!strcmp(*argv, "signature")) { |
| 1542 | require(2); |
| 1543 | data = load_file(argv[1], &sz); |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1544 | 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] | 1545 | if (sz != 256) die("signature must be 256 bytes"); |
| 1546 | fb_queue_download("signature", data, sz); |
| 1547 | fb_queue_command("signature", "installing signature"); |
| 1548 | skip(2); |
| 1549 | } else if(!strcmp(*argv, "reboot")) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1550 | wants_reboot = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1551 | skip(1); |
Elliott Hughes | ca85df0 | 2015-02-25 10:02:00 -0800 | [diff] [blame] | 1552 | if (argc > 0) { |
| 1553 | if (!strcmp(*argv, "bootloader")) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1554 | wants_reboot = false; |
| 1555 | wants_reboot_bootloader = true; |
Elliott Hughes | ca85df0 | 2015-02-25 10:02:00 -0800 | [diff] [blame] | 1556 | skip(1); |
| 1557 | } |
| 1558 | } |
| 1559 | require(0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1560 | } else if(!strcmp(*argv, "reboot-bootloader")) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1561 | wants_reboot_bootloader = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1562 | skip(1); |
| 1563 | } else if (!strcmp(*argv, "continue")) { |
| 1564 | fb_queue_command("continue", "resuming boot"); |
| 1565 | skip(1); |
| 1566 | } else if(!strcmp(*argv, "boot")) { |
| 1567 | char *kname = 0; |
| 1568 | char *rname = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1569 | char *sname = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1570 | skip(1); |
| 1571 | if (argc > 0) { |
| 1572 | kname = argv[0]; |
| 1573 | skip(1); |
| 1574 | } |
| 1575 | if (argc > 0) { |
| 1576 | rname = argv[0]; |
| 1577 | skip(1); |
| 1578 | } |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1579 | if (argc > 0) { |
| 1580 | sname = argv[0]; |
| 1581 | skip(1); |
| 1582 | } |
| 1583 | data = load_bootable_image(kname, rname, sname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1584 | if (data == 0) return 1; |
| 1585 | fb_queue_download("boot.img", data, sz); |
| 1586 | fb_queue_command("boot", "booting"); |
| 1587 | } else if(!strcmp(*argv, "flash")) { |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 1588 | char* pname = argv[1]; |
| 1589 | std::string fname; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1590 | require(2); |
| 1591 | if (argc > 2) { |
| 1592 | fname = argv[2]; |
| 1593 | skip(3); |
| 1594 | } else { |
| 1595 | fname = find_item(pname, product); |
| 1596 | skip(2); |
| 1597 | } |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 1598 | if (fname.empty()) die("cannot determine image filename for '%s'", pname); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1599 | |
| 1600 | auto flash = [&](const std::string &partition) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1601 | if (erase_first && needs_erase(transport, partition.c_str())) { |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1602 | fb_queue_erase(partition.c_str()); |
| 1603 | } |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 1604 | do_flash(transport, partition.c_str(), fname.c_str()); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1605 | }; |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 1606 | do_for_partitions(transport, pname, slot_override, flash, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1607 | } else if(!strcmp(*argv, "flash:raw")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1608 | char *kname = argv[2]; |
| 1609 | char *rname = 0; |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1610 | char *sname = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1611 | require(3); |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1612 | skip(3); |
| 1613 | if (argc > 0) { |
| 1614 | rname = argv[0]; |
| 1615 | skip(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1616 | } |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 1617 | if (argc > 0) { |
| 1618 | sname = argv[0]; |
| 1619 | skip(1); |
| 1620 | } |
| 1621 | data = load_bootable_image(kname, rname, sname, &sz, cmdline); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1622 | if (data == 0) die("cannot load bootable image"); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1623 | auto flashraw = [&](const std::string &partition) { |
| 1624 | fb_queue_flash(partition.c_str(), data, sz); |
| 1625 | }; |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 1626 | do_for_partitions(transport, argv[1], slot_override, flashraw, true); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1627 | } else if(!strcmp(*argv, "flashall")) { |
| 1628 | skip(1); |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1629 | if (slot_override == "all") { |
| 1630 | fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed."); |
| 1631 | do_flashall(transport, slot_override, erase_first, true, false); |
| 1632 | } else { |
| 1633 | do_flashall(transport, slot_override, erase_first, true, true); |
| 1634 | } |
| 1635 | wants_reboot = true; |
| 1636 | } else if(!strcmp(*argv, "flash-primary")) { |
| 1637 | skip(1); |
| 1638 | do_flashall(transport, slot_override, erase_first, true, false); |
| 1639 | wants_reboot = true; |
| 1640 | } else if(!strcmp(*argv, "flash-secondary")) { |
| 1641 | skip(1); |
| 1642 | do_flashall(transport, slot_override, erase_first, false, true); |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1643 | wants_reboot = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1644 | } else if(!strcmp(*argv, "update")) { |
| 1645 | if (argc > 1) { |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1646 | do_update(transport, argv[1], slot_override, erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1647 | skip(2); |
| 1648 | } else { |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1649 | do_update(transport, "update.zip", slot_override, erase_first); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1650 | skip(1); |
| 1651 | } |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 1652 | wants_reboot = 1; |
| 1653 | } else if(!strcmp(*argv, "set_active")) { |
| 1654 | require(2); |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame^] | 1655 | std::string slot = verify_slot(transport, std::string(argv[1]), false); |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 1656 | fb_set_active(slot.c_str()); |
| 1657 | skip(2); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1658 | } else if(!strcmp(*argv, "oem")) { |
| 1659 | argc = do_oem_command(argc, argv); |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 1660 | } else if(!strcmp(*argv, "flashing")) { |
| 1661 | if (argc == 2 && (!strcmp(*(argv+1), "unlock") || |
| 1662 | !strcmp(*(argv+1), "lock") || |
| 1663 | !strcmp(*(argv+1), "unlock_critical") || |
| 1664 | !strcmp(*(argv+1), "lock_critical") || |
| 1665 | !strcmp(*(argv+1), "get_unlock_ability") || |
| 1666 | !strcmp(*(argv+1), "get_unlock_bootloader_nonce") || |
| 1667 | !strcmp(*(argv+1), "lock_bootloader"))) { |
| 1668 | argc = do_oem_command(argc, argv); |
| 1669 | } else |
| 1670 | if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) { |
| 1671 | argc = do_bypass_unlock_command(argc, argv); |
Badhri Jagan Sridharan | bf11095 | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 1672 | } else { |
| 1673 | usage(); |
| 1674 | return 1; |
| 1675 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1676 | } else { |
| 1677 | usage(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1678 | return 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | if (wants_wipe) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1683 | fprintf(stderr, "wiping userdata...\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1684 | fb_queue_erase("userdata"); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1685 | fb_perform_format(transport, "userdata", 1, nullptr, nullptr); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1686 | |
| 1687 | std::string cache_type; |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1688 | if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1689 | fprintf(stderr, "wiping cache...\n"); |
| 1690 | fb_queue_erase("cache"); |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1691 | fb_perform_format(transport, "cache", 1, nullptr, nullptr); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1692 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1693 | } |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1694 | if (wants_set_active) { |
| 1695 | fb_set_active(next_active.c_str()); |
| 1696 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1697 | if (wants_reboot) { |
| 1698 | fb_queue_reboot(); |
Mark Wachsler | ec25e7b | 2014-06-24 11:04:54 -0400 | [diff] [blame] | 1699 | fb_queue_wait_for_disconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1700 | } else if (wants_reboot_bootloader) { |
| 1701 | fb_queue_command("reboot-bootloader", "rebooting into bootloader"); |
Mark Wachsler | 157b001 | 2013-10-02 09:35:38 -0400 | [diff] [blame] | 1702 | fb_queue_wait_for_disconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1705 | return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1706 | } |