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