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