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