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