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