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 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 29 | #include "fastboot.h" |
| 30 | |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 31 | #include <ctype.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 34 | #include <getopt.h> |
Ying Wang | cf86e2f | 2014-05-15 20:06:40 -0700 | [diff] [blame] | 35 | #include <inttypes.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 36 | #include <limits.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 37 | #include <stdint.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <sys/stat.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | #include <sys/time.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 43 | #include <sys/types.h> |
Mark Salyzyn | 5957c1f | 2014-04-30 14:05:28 -0700 | [diff] [blame] | 44 | #include <unistd.h> |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 45 | |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 46 | #include <chrono> |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 47 | #include <functional> |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 48 | #include <regex> |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 49 | #include <thread> |
Josh Gao | 9da9ac5 | 2016-01-19 14:50:18 -0800 | [diff] [blame] | 50 | #include <utility> |
| 51 | #include <vector> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 53 | #include <android-base/file.h> |
Elliott Hughes | 749ae2d | 2016-06-28 14:48:45 -0700 | [diff] [blame] | 54 | #include <android-base/macros.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 55 | #include <android-base/parseint.h> |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 56 | #include <android-base/parsenetaddress.h> |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 57 | #include <android-base/stringprintf.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 58 | #include <android-base/strings.h> |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 59 | #include <android-base/test_utils.h> |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 60 | #include <android-base/unique_fd.h> |
Dan Willemsen | ab971b5 | 2018-08-29 14:58:02 -0700 | [diff] [blame] | 61 | #include <build/version.h> |
| 62 | #include <platform_tools_version.h> |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 63 | #include <sparse/sparse.h> |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 64 | #include <ziparchive/zip_archive.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 66 | #include "bootimg_utils.h" |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 67 | #include "diagnose_usb.h" |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 68 | #include "fastboot_driver.h" |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 69 | #include "fs.h" |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 70 | #include "tcp.h" |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 71 | #include "transport.h" |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 72 | #include "udp.h" |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 73 | #include "usb.h" |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 74 | #include "util.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 76 | using android::base::ReadFully; |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 77 | using android::base::Split; |
| 78 | using android::base::Trim; |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 79 | using android::base::unique_fd; |
| 80 | |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 81 | #ifndef O_BINARY |
| 82 | #define O_BINARY 0 |
| 83 | #endif |
| 84 | |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 85 | static const char* serial = nullptr; |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 86 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 87 | static bool g_long_listing = false; |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 88 | // Don't resparse files in too-big chunks. |
| 89 | // libsparse will support INT_MAX, but this results in large allocations, so |
| 90 | // let's keep it at 1GB to avoid memory pressure on the host. |
| 91 | static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024; |
Elliott Hughes | 542370d | 2018-04-19 19:49:44 -0700 | [diff] [blame] | 92 | static uint64_t sparse_limit = 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 93 | static int64_t target_sparse_limit = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 95 | static unsigned g_base_addr = 0x10000000; |
| 96 | static boot_img_hdr_v1 g_boot_img_hdr = {}; |
| 97 | static std::string g_cmdline; |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 98 | |
David Zeuthen | b6ea435 | 2017-08-07 14:29:26 -0400 | [diff] [blame] | 99 | static bool g_disable_verity = false; |
| 100 | static bool g_disable_verification = false; |
| 101 | |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 102 | static const std::string convert_fbe_marker_filename("convert_fbe"); |
| 103 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 104 | fastboot::FastBootDriver* fb = nullptr; |
| 105 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 106 | enum fb_buffer_type { |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 107 | FB_BUFFER_FD, |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 108 | FB_BUFFER_SPARSE, |
| 109 | }; |
| 110 | |
| 111 | struct fastboot_buffer { |
| 112 | enum fb_buffer_type type; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 113 | void* data; |
| 114 | int64_t sz; |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 115 | int fd; |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 116 | int64_t image_size; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
David Anderson | 0debda0 | 2018-08-28 13:54:03 -0700 | [diff] [blame] | 119 | enum class ImageType { |
| 120 | // Must be flashed for device to boot into the kernel. |
| 121 | BootCritical, |
| 122 | // Normal partition to be flashed during "flashall". |
| 123 | Normal, |
| 124 | // Partition that is never flashed during "flashall". |
| 125 | Extra |
| 126 | }; |
| 127 | |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 128 | struct Image { |
Elliott Hughes | 45964a8 | 2017-05-03 22:43:23 -0700 | [diff] [blame] | 129 | const char* nickname; |
| 130 | const char* img_name; |
| 131 | const char* sig_name; |
| 132 | const char* part_name; |
Hridya Valsaraju | f1f0a9c | 2018-08-02 10:46:21 -0700 | [diff] [blame] | 133 | bool optional_if_no_image; |
David Anderson | 0debda0 | 2018-08-28 13:54:03 -0700 | [diff] [blame] | 134 | ImageType type; |
Hridya Valsaraju | f1f0a9c | 2018-08-02 10:46:21 -0700 | [diff] [blame] | 135 | bool IsSecondary() const { return nickname == nullptr; } |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static Image images[] = { |
Dario Freni | c7ea1af | 2018-05-25 16:07:19 +0100 | [diff] [blame] | 139 | // clang-format off |
David Anderson | 0debda0 | 2018-08-28 13:54:03 -0700 | [diff] [blame] | 140 | { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical }, |
| 141 | { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal }, |
| 142 | { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra }, |
| 143 | { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical }, |
| 144 | { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical }, |
| 145 | { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal }, |
| 146 | { "product", "product.img", "product.sig", "product", true, ImageType::Normal }, |
Dario Freni | ab5583b | 2018-08-17 01:01:25 +0100 | [diff] [blame] | 147 | { "product_services", |
| 148 | "product_services.img", |
| 149 | "product_services.sig", |
David Anderson | 8cdea7f | 2018-08-06 15:36:00 -0700 | [diff] [blame] | 150 | "product_services", |
David Anderson | 0debda0 | 2018-08-28 13:54:03 -0700 | [diff] [blame] | 151 | true, ImageType::Normal }, |
| 152 | { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical }, |
| 153 | { "super", "super.img", "super.sig", "super", true, ImageType::Extra }, |
| 154 | { "system", "system.img", "system.sig", "system", false, ImageType::Normal }, |
| 155 | { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal }, |
| 156 | { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra }, |
| 157 | { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical }, |
David Anderson | 1109c92 | 2018-09-17 17:32:54 -0700 | [diff] [blame] | 158 | { "vbmeta_mainline", |
| 159 | "vbmeta_mainline.img", |
| 160 | "vbmeta_mainline.sig", |
| 161 | "vbmeta_mainline", |
| 162 | true, ImageType::BootCritical }, |
David Anderson | 0debda0 | 2018-08-28 13:54:03 -0700 | [diff] [blame] | 163 | { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal }, |
| 164 | { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal }, |
Dario Freni | c7ea1af | 2018-05-25 16:07:19 +0100 | [diff] [blame] | 165 | // clang-format on |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 166 | }; |
Brian Swetland | 2a63bb7 | 2009-04-28 16:05:07 -0700 | [diff] [blame] | 167 | |
David Anderson | 0debda0 | 2018-08-28 13:54:03 -0700 | [diff] [blame] | 168 | static std::string find_item_given_name(const std::string& img_name) { |
Elliott Hughes | 45964a8 | 2017-05-03 22:43:23 -0700 | [diff] [blame] | 169 | char* dir = getenv("ANDROID_PRODUCT_OUT"); |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 170 | if (dir == nullptr || dir[0] == '\0') { |
Elliott Hughes | 45964a8 | 2017-05-03 22:43:23 -0700 | [diff] [blame] | 171 | die("ANDROID_PRODUCT_OUT not set"); |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 172 | } |
David Anderson | 0debda0 | 2018-08-28 13:54:03 -0700 | [diff] [blame] | 173 | return std::string(dir) + "/" + img_name; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Elliott Hughes | 29d5d7d | 2017-05-11 15:05:13 -0700 | [diff] [blame] | 176 | static std::string find_item(const std::string& item) { |
Elliott Hughes | 45964a8 | 2017-05-03 22:43:23 -0700 | [diff] [blame] | 177 | for (size_t i = 0; i < arraysize(images); ++i) { |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 178 | if (images[i].nickname && item == images[i].nickname) { |
Elliott Hughes | 45964a8 | 2017-05-03 22:43:23 -0700 | [diff] [blame] | 179 | return find_item_given_name(images[i].img_name); |
| 180 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 183 | fprintf(stderr, "unknown partition '%s'\n", item.c_str()); |
Elliott Hughes | 45964a8 | 2017-05-03 22:43:23 -0700 | [diff] [blame] | 184 | return ""; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 187 | double last_start_time; |
| 188 | |
| 189 | static void Status(const std::string& message) { |
| 190 | static constexpr char kStatusFormat[] = "%-50s "; |
| 191 | fprintf(stderr, kStatusFormat, message.c_str()); |
| 192 | last_start_time = now(); |
| 193 | } |
| 194 | |
| 195 | static void Epilog(int status) { |
| 196 | if (status) { |
| 197 | fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str()); |
| 198 | die("Command failed"); |
| 199 | } else { |
| 200 | double split = now(); |
| 201 | fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static void InfoMessage(const std::string& info) { |
| 206 | fprintf(stderr, "(bootloader) %s\n", info.c_str()); |
| 207 | } |
| 208 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 209 | static int64_t get_file_size(int fd) { |
| 210 | struct stat sb; |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 211 | if (fstat(fd, &sb) == -1) { |
| 212 | die("could not get file size"); |
| 213 | } |
| 214 | return sb.st_size; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 217 | bool ReadFileToVector(const std::string& file, std::vector<char>* out) { |
| 218 | out->clear(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 220 | unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY))); |
| 221 | if (fd == -1) { |
| 222 | return false; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 223 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 225 | out->resize(get_file_size(fd)); |
| 226 | return ReadFully(fd, out->data(), out->size()); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 227 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 229 | 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] | 230 | if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) { |
| 231 | return -1; |
| 232 | } |
| 233 | |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 234 | // require matching serial number or device path if requested |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | // at the command line with the -s option. |
JP Abgrall | a032ded | 2012-06-06 11:53:33 -0700 | [diff] [blame] | 236 | if (local_serial && (strcmp(local_serial, info->serial_number) != 0 && |
| 237 | strcmp(local_serial, info->device_path) != 0)) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 241 | static int match_fastboot(usb_ifc_info* info) { |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 242 | return match_fastboot_with_serial(info, serial); |
| 243 | } |
| 244 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 245 | static int list_devices_callback(usb_ifc_info* info) { |
| 246 | if (match_fastboot_with_serial(info, nullptr) == 0) { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 247 | std::string serial = info->serial_number; |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 248 | if (!info->writable) { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 249 | serial = UsbNoPermissionsShortHelpText(); |
Elliott Hughes | b4add9b | 2009-10-06 18:07:49 -0700 | [diff] [blame] | 250 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | if (!serial[0]) { |
| 252 | serial = "????????????"; |
| 253 | } |
Scott Anderson | 866b1bd | 2012-06-04 20:29:11 -0700 | [diff] [blame] | 254 | // output compatible with "adb devices" |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 255 | if (!g_long_listing) { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 256 | printf("%s\tfastboot", serial.c_str()); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 257 | } else { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 258 | printf("%-22s fastboot", serial.c_str()); |
| 259 | if (strlen(info->device_path) > 0) printf(" %s", info->device_path); |
Scott Anderson | 13081c6 | 2012-04-06 12:39:30 -0700 | [diff] [blame] | 260 | } |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 261 | putchar('\n'); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return -1; |
| 265 | } |
| 266 | |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 267 | // Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify |
| 268 | // a specific device, otherwise the first USB device found will be used. |
| 269 | // |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 270 | // If |serial| is non-null but invalid, this exits. |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 271 | // Otherwise it blocks until the target is available. |
| 272 | // |
| 273 | // The returned Transport is a singleton, so multiple calls to this function will return the same |
| 274 | // object, and the caller should not attempt to delete the returned Transport. |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 275 | static Transport* open_device() { |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 276 | bool announce = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 | |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 278 | Socket::Protocol protocol = Socket::Protocol::kTcp; |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 279 | std::string host; |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 280 | int port = 0; |
| 281 | if (serial != nullptr) { |
| 282 | const char* net_address = nullptr; |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 283 | |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 284 | if (android::base::StartsWith(serial, "tcp:")) { |
| 285 | protocol = Socket::Protocol::kTcp; |
| 286 | port = tcp::kDefaultPort; |
| 287 | net_address = serial + strlen("tcp:"); |
| 288 | } else if (android::base::StartsWith(serial, "udp:")) { |
| 289 | protocol = Socket::Protocol::kUdp; |
| 290 | port = udp::kDefaultPort; |
| 291 | net_address = serial + strlen("udp:"); |
| 292 | } |
| 293 | |
| 294 | if (net_address != nullptr) { |
| 295 | std::string error; |
| 296 | if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) { |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 297 | die("invalid network address '%s': %s\n", net_address, error.c_str()); |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 298 | } |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 302 | Transport* transport = nullptr; |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 303 | while (true) { |
| 304 | if (!host.empty()) { |
| 305 | std::string error; |
David Pursell | 4601c97 | 2016-02-05 15:35:09 -0800 | [diff] [blame] | 306 | if (protocol == Socket::Protocol::kTcp) { |
| 307 | transport = tcp::Connect(host, port, &error).release(); |
| 308 | } else if (protocol == Socket::Protocol::kUdp) { |
| 309 | transport = udp::Connect(host, port, &error).release(); |
| 310 | } |
| 311 | |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 312 | if (transport == nullptr && announce) { |
| 313 | fprintf(stderr, "error: %s\n", error.c_str()); |
| 314 | } |
| 315 | } else { |
| 316 | transport = usb_open(match_fastboot); |
| 317 | } |
| 318 | |
| 319 | if (transport != nullptr) { |
| 320 | return transport; |
| 321 | } |
| 322 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 323 | if (announce) { |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 324 | announce = false; |
Elliott Hughes | b46964f | 2015-08-19 17:49:45 -0700 | [diff] [blame] | 325 | 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] | 326 | } |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 327 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 331 | static void list_devices() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | // We don't actually open a USB device here, |
| 333 | // just getting our callback called so we can |
| 334 | // list all the connected devices. |
| 335 | usb_open(list_devices_callback); |
| 336 | } |
| 337 | |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 338 | static void syntax_error(const char* fmt, ...) { |
| 339 | fprintf(stderr, "fastboot: usage: "); |
| 340 | |
| 341 | va_list ap; |
| 342 | va_start(ap, fmt); |
| 343 | vfprintf(stderr, fmt, ap); |
| 344 | va_end(ap); |
| 345 | |
| 346 | fprintf(stderr, "\n"); |
| 347 | exit(1); |
| 348 | } |
| 349 | |
| 350 | static int show_help() { |
| 351 | // clang-format off |
| 352 | fprintf(stdout, |
Elliott Hughes | 07fc682 | 2018-05-24 16:36:05 -0700 | [diff] [blame] | 353 | // 1 2 3 4 5 6 7 8 |
| 354 | // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 355 | "usage: fastboot [OPTION...] COMMAND...\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | "\n" |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 357 | "flashing:\n" |
| 358 | " update ZIP Flash all partitions from an update.zip package.\n" |
| 359 | " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n" |
| 360 | " On A/B devices, flashed slot is set as active.\n" |
| 361 | " Secondary images may be flashed to inactive slot.\n" |
Elliott Hughes | 07fc682 | 2018-05-24 16:36:05 -0700 | [diff] [blame] | 362 | " flash PARTITION [FILENAME] Flash given partition, using the image from\n" |
| 363 | " $ANDROID_PRODUCT_OUT if no filename is given.\n" |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 364 | "\n" |
| 365 | "basics:\n" |
| 366 | " devices [-l] List devices in bootloader (-l: with device paths).\n" |
| 367 | " getvar NAME Display given bootloader variable.\n" |
| 368 | " reboot [bootloader] Reboot device.\n" |
| 369 | "\n" |
| 370 | "locking/unlocking:\n" |
| 371 | " flashing lock|unlock Lock/unlock partitions for flashing\n" |
| 372 | " flashing lock_critical|unlock_critical\n" |
| 373 | " Lock/unlock 'critical' bootloader partitions.\n" |
| 374 | " flashing get_unlock_ability\n" |
| 375 | " Check whether unlocking is allowed (1) or not(0).\n" |
| 376 | "\n" |
| 377 | "advanced:\n" |
| 378 | " erase PARTITION Erase a flash partition.\n" |
| 379 | " format[:FS_TYPE[:SIZE]] PARTITION\n" |
| 380 | " Format a flash partition.\n" |
| 381 | " set_active SLOT Set the active slot.\n" |
| 382 | " oem [COMMAND...] Execute OEM-specific command.\n" |
| 383 | "\n" |
| 384 | "boot image:\n" |
| 385 | " boot KERNEL [RAMDISK [SECOND]]\n" |
| 386 | " Download and boot kernel from RAM.\n" |
| 387 | " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n" |
| 388 | " Create boot image and flash it.\n" |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 389 | " --cmdline CMDLINE Override kernel command line.\n" |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 390 | " --base ADDRESS Set kernel base address (default: 0x10000000).\n" |
| 391 | " --kernel-offset Set kernel offset (default: 0x00008000).\n" |
| 392 | " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n" |
| 393 | " --tags-offset Set tags offset (default: 0x00000100).\n" |
| 394 | " --page-size BYTES Set flash page size (default: 2048).\n" |
| 395 | " --header-version VERSION Set boot image header version.\n" |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 396 | " --os-version MAJOR[.MINOR[.PATCH]]\n" |
| 397 | " Set boot image OS version (default: 0.0.0).\n" |
| 398 | " --os-patch-level YYYY-MM-DD\n" |
| 399 | " Set boot image OS security patch level.\n" |
| 400 | // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`. |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 401 | "\n" |
Elliott Hughes | 2e9b779 | 2018-04-04 13:36:44 -0700 | [diff] [blame] | 402 | // TODO: what device(s) used this? is there any documentation? |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 403 | //" continue Continue with autoboot.\n" |
| 404 | //"\n" |
| 405 | "Android Things:\n" |
| 406 | " stage IN_FILE Sends given file to stage for the next command.\n" |
| 407 | " 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] | 408 | "\n" |
| 409 | "options:\n" |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 410 | " -w Wipe userdata.\n" |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 411 | " -s SERIAL Specify a USB device.\n" |
| 412 | " -s tcp|udp:HOST[:PORT] Specify a network device.\n" |
Elliott Hughes | 542370d | 2018-04-19 19:49:44 -0700 | [diff] [blame] | 413 | " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n" |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 414 | " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n" |
| 415 | " non-current slot (default: current active slot).\n" |
| 416 | " --set-active[=SLOT] Sets the active slot before rebooting.\n" |
| 417 | " --skip-secondary Don't flash secondary slots in flashall/update.\n" |
| 418 | " --skip-reboot Don't reboot device after flashing.\n" |
| 419 | " --disable-verity Sets disable-verity when flashing vbmeta.\n" |
| 420 | " --disable-verification Sets disable-verification when flashing vbmeta.\n" |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 421 | #if !defined(_WIN32) |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 422 | " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n" |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 423 | #endif |
Elliott Hughes | b76188f | 2018-04-03 13:30:18 -0700 | [diff] [blame] | 424 | // TODO: remove --unbuffered? |
| 425 | " --unbuffered Don't buffer input or output.\n" |
| 426 | " --verbose, -v Verbose output.\n" |
| 427 | " --version Display version.\n" |
| 428 | " --help, -h Show this message.\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | ); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 430 | // clang-format off |
| 431 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 432 | } |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 433 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 434 | static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk, |
| 435 | const std::string& second_stage) { |
| 436 | std::vector<char> kernel_data; |
| 437 | if (!ReadFileToVector(kernel, &kernel_data)) { |
| 438 | die("cannot load '%s': %s", kernel.c_str(), strerror(errno)); |
| 439 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 440 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 441 | // Is this actually a boot image? |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 442 | if (kernel_data.size() < sizeof(boot_img_hdr_v1)) { |
Elliott Hughes | aaa3b6b | 2018-01-18 16:08:24 -0800 | [diff] [blame] | 443 | die("cannot load '%s': too short", kernel.c_str()); |
| 444 | } |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 445 | if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) { |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 446 | if (!g_cmdline.empty()) { |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 447 | bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kernel_data.data()), g_cmdline); |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 448 | } |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 449 | |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 450 | if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk"); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 451 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 452 | return kernel_data; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 455 | std::vector<char> ramdisk_data; |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 456 | if (!ramdisk.empty()) { |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 457 | if (!ReadFileToVector(ramdisk, &ramdisk_data)) { |
| 458 | die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno)); |
| 459 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 462 | std::vector<char> second_stage_data; |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 463 | if (!second_stage.empty()) { |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 464 | if (!ReadFileToVector(second_stage, &second_stage_data)) { |
| 465 | die("cannot load '%s': %s", second_stage.c_str(), strerror(errno)); |
| 466 | } |
Jeremy Compostella | a42adff | 2014-07-17 17:17:54 +0200 | [diff] [blame] | 467 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 468 | fprintf(stderr,"creating boot image...\n"); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 469 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 470 | std::vector<char> out; |
| 471 | boot_img_hdr_v1* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data, |
| 472 | g_base_addr, g_boot_img_hdr, &out); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 473 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 474 | if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline); |
| 475 | fprintf(stderr, "creating boot image - %zu bytes\n", out.size()); |
| 476 | |
| 477 | return out; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 480 | static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name, |
| 481 | std::vector<char>* out) { |
| 482 | ZipString zip_entry_name(entry_name.c_str()); |
Elliott Hughes | d30ad8a | 2015-03-18 23:12:44 -0700 | [diff] [blame] | 483 | ZipEntry zip_entry; |
| 484 | if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 485 | fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str()); |
| 486 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 489 | out->resize(zip_entry.uncompressed_length); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 490 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 491 | fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(), |
| 492 | out->size() / 1024 / 1024); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 493 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 494 | int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()), |
| 495 | out->size()); |
| 496 | if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error)); |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 497 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 498 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Elliott Hughes | a26fbee | 2015-06-03 15:22:11 -0700 | [diff] [blame] | 501 | #if defined(_WIN32) |
| 502 | |
| 503 | // TODO: move this to somewhere it can be shared. |
| 504 | |
| 505 | #include <windows.h> |
| 506 | |
| 507 | // Windows' tmpfile(3) requires administrator rights because |
| 508 | // it creates temporary files in the root directory. |
| 509 | static FILE* win32_tmpfile() { |
| 510 | char temp_path[PATH_MAX]; |
| 511 | DWORD nchars = GetTempPath(sizeof(temp_path), temp_path); |
| 512 | if (nchars == 0 || nchars >= sizeof(temp_path)) { |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 513 | die("GetTempPath failed, error %ld", GetLastError()); |
Elliott Hughes | a26fbee | 2015-06-03 15:22:11 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | char filename[PATH_MAX]; |
| 517 | if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) { |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 518 | die("GetTempFileName failed, error %ld", GetLastError()); |
Elliott Hughes | a26fbee | 2015-06-03 15:22:11 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | return fopen(filename, "w+bTD"); |
| 522 | } |
| 523 | |
| 524 | #define tmpfile win32_tmpfile |
| 525 | |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 526 | static std::string make_temporary_directory() { |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 527 | die("make_temporary_directory not supported under Windows, sorry!"); |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 530 | static int make_temporary_fd(const char* /*what*/) { |
Elliott Hughes | bbfc281 | 2017-04-25 18:33:09 -0700 | [diff] [blame] | 531 | // TODO: reimplement to avoid leaking a FILE*. |
| 532 | return fileno(tmpfile()); |
| 533 | } |
| 534 | |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 535 | #else |
| 536 | |
Elliott Hughes | bbfc281 | 2017-04-25 18:33:09 -0700 | [diff] [blame] | 537 | static std::string make_temporary_template() { |
| 538 | const char* tmpdir = getenv("TMPDIR"); |
| 539 | if (tmpdir == nullptr) tmpdir = P_tmpdir; |
| 540 | return std::string(tmpdir) + "/fastboot_userdata_XXXXXX"; |
| 541 | } |
| 542 | |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 543 | static std::string make_temporary_directory() { |
Elliott Hughes | bbfc281 | 2017-04-25 18:33:09 -0700 | [diff] [blame] | 544 | std::string result(make_temporary_template()); |
| 545 | if (mkdtemp(&result[0]) == nullptr) { |
Elliott Hughes | da1dbd6 | 2018-05-21 23:02:26 -0700 | [diff] [blame] | 546 | die("unable to create temporary directory with template %s: %s", |
| 547 | result.c_str(), strerror(errno)); |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 548 | } |
| 549 | return result; |
| 550 | } |
| 551 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 552 | static int make_temporary_fd(const char* what) { |
Elliott Hughes | bbfc281 | 2017-04-25 18:33:09 -0700 | [diff] [blame] | 553 | std::string path_template(make_temporary_template()); |
| 554 | int fd = mkstemp(&path_template[0]); |
| 555 | if (fd == -1) { |
Elliott Hughes | da1dbd6 | 2018-05-21 23:02:26 -0700 | [diff] [blame] | 556 | die("failed to create temporary file for %s with template %s: %s\n", |
| 557 | path_template.c_str(), what, strerror(errno)); |
Elliott Hughes | bbfc281 | 2017-04-25 18:33:09 -0700 | [diff] [blame] | 558 | } |
| 559 | unlink(path_template.c_str()); |
| 560 | return fd; |
| 561 | } |
| 562 | |
Elliott Hughes | a26fbee | 2015-06-03 15:22:11 -0700 | [diff] [blame] | 563 | #endif |
| 564 | |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 565 | static std::string create_fbemarker_tmpdir() { |
| 566 | std::string dir = make_temporary_directory(); |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 567 | std::string marker_file = dir + "/" + convert_fbe_marker_filename; |
| 568 | int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666); |
| 569 | if (fd == -1) { |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 570 | die("unable to create FBE marker file %s locally: %s", |
| 571 | marker_file.c_str(), strerror(errno)); |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 572 | } |
| 573 | close(fd); |
| 574 | return dir; |
| 575 | } |
| 576 | |
| 577 | static void delete_fbemarker_tmpdir(const std::string& dir) { |
| 578 | std::string marker_file = dir + "/" + convert_fbe_marker_filename; |
| 579 | if (unlink(marker_file.c_str()) == -1) { |
| 580 | fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n", |
| 581 | marker_file.c_str(), errno, strerror(errno)); |
| 582 | return; |
| 583 | } |
| 584 | if (rmdir(dir.c_str()) == -1) { |
| 585 | fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n", |
| 586 | dir.c_str(), errno, strerror(errno)); |
| 587 | return; |
| 588 | } |
| 589 | } |
| 590 | |
Elliott Hughes | 45964a8 | 2017-05-03 22:43:23 -0700 | [diff] [blame] | 591 | static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) { |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 592 | unique_fd fd(make_temporary_fd(entry_name)); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 593 | |
Yusuke Sato | 0744754 | 2015-06-25 14:39:19 -0700 | [diff] [blame] | 594 | ZipString zip_entry_name(entry_name); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 595 | ZipEntry zip_entry; |
| 596 | if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) { |
| 597 | fprintf(stderr, "archive does not contain '%s'\n", entry_name); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 598 | errno = ENOENT; |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 599 | return -1; |
| 600 | } |
| 601 | |
Elliott Hughes | 23af112 | 2017-11-10 08:42:17 -0800 | [diff] [blame] | 602 | fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name, |
Elliott Hughes | bbfc281 | 2017-04-25 18:33:09 -0700 | [diff] [blame] | 603 | zip_entry.uncompressed_length / 1024 / 1024); |
Elliott Hughes | 23af112 | 2017-11-10 08:42:17 -0800 | [diff] [blame] | 604 | double start = now(); |
Elliott Hughes | a82c89d | 2015-03-19 11:44:32 -0700 | [diff] [blame] | 605 | int error = ExtractEntryToFile(zip, &zip_entry, fd); |
| 606 | if (error != 0) { |
Elliott Hughes | 23af112 | 2017-11-10 08:42:17 -0800 | [diff] [blame] | 607 | die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error)); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 610 | if (lseek(fd, 0, SEEK_SET) != 0) { |
Elliott Hughes | 23af112 | 2017-11-10 08:42:17 -0800 | [diff] [blame] | 611 | die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno)); |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Elliott Hughes | 23af112 | 2017-11-10 08:42:17 -0800 | [diff] [blame] | 614 | fprintf(stderr, " took %.3fs\n", now() - start); |
| 615 | |
Elliott Hughes | bbfc281 | 2017-04-25 18:33:09 -0700 | [diff] [blame] | 616 | return fd.release(); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 619 | static void CheckRequirement(const std::string& cur_product, const std::string& var, |
| 620 | const std::string& product, bool invert, |
| 621 | const std::vector<std::string>& options) { |
| 622 | Status("Checking '" + var + "'"); |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 623 | |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 624 | double start = now(); |
| 625 | |
| 626 | if (!product.empty()) { |
| 627 | if (product != cur_product) { |
| 628 | double split = now(); |
| 629 | fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n", |
| 630 | cur_product.c_str(), product.c_str(), (split - start)); |
| 631 | return; |
| 632 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 633 | } |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 634 | |
| 635 | std::string var_value; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 636 | if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) { |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 637 | fprintf(stderr, "FAILED\n\n"); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 638 | fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(), |
| 639 | fb->Error().c_str()); |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 640 | die("requirements not met!"); |
| 641 | } |
| 642 | |
| 643 | bool match = false; |
| 644 | for (const auto& option : options) { |
| 645 | if (option == var_value || (option.back() == '*' && |
| 646 | !var_value.compare(0, option.length() - 1, option, 0, |
| 647 | option.length() - 1))) { |
| 648 | match = true; |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | if (invert) { |
| 654 | match = !match; |
| 655 | } |
| 656 | |
| 657 | if (match) { |
| 658 | double split = now(); |
| 659 | fprintf(stderr, "OKAY [%7.3fs]\n", (split - start)); |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | fprintf(stderr, "FAILED\n\n"); |
| 664 | fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str()); |
| 665 | fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str()); |
| 666 | for (auto it = std::next(options.begin()); it != options.end(); ++it) { |
| 667 | fprintf(stderr, " or '%s'", it->c_str()); |
| 668 | } |
| 669 | fprintf(stderr, ".\n\n"); |
| 670 | die("requirements not met!"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 673 | bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product, |
| 674 | bool* invert, std::vector<std::string>* options) { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 675 | // "require product=alpha|beta|gamma" |
| 676 | // "require version-bootloader=1234" |
| 677 | // "require-for-product:gamma version-bootloader=istanbul|constantinople" |
| 678 | // "require partition-exists=vendor" |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 679 | *product = ""; |
| 680 | *invert = false; |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 681 | |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 682 | auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"}; |
| 683 | auto require_product_regex = |
| 684 | std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"}; |
| 685 | std::smatch match_results; |
| 686 | |
| 687 | if (std::regex_match(line, match_results, require_reject_regex)) { |
| 688 | *invert = Trim(match_results[1]) == "reject"; |
| 689 | } else if (std::regex_match(line, match_results, require_product_regex)) { |
| 690 | *product = match_results[1]; |
| 691 | } else { |
| 692 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 693 | } |
| 694 | |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 695 | *name = match_results[2]; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 696 | // Work around an unfortunate name mismatch. |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 697 | if (*name == "board") { |
| 698 | *name = "product"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 701 | auto raw_options = Split(match_results[3], "|"); |
| 702 | for (const auto& option : raw_options) { |
| 703 | auto trimmed_option = Trim(option); |
| 704 | options->emplace_back(trimmed_option); |
| 705 | } |
| 706 | |
| 707 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 708 | } |
| 709 | |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 710 | // "require partition-exists=x" is a special case, added because of the trouble we had when |
| 711 | // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them, |
| 712 | // missing out new partitions. A device with new partitions can use "partition-exists" to |
| 713 | // override the fields `optional_if_no_image` in the `images` array. |
| 714 | static void HandlePartitionExists(const std::vector<std::string>& options) { |
| 715 | const std::string& partition_name = options[0]; |
| 716 | std::string has_slot; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 717 | if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS || |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 718 | (has_slot != "yes" && has_slot != "no")) { |
| 719 | die("device doesn't have required partition %s!", partition_name.c_str()); |
| 720 | } |
| 721 | bool known_partition = false; |
| 722 | for (size_t i = 0; i < arraysize(images); ++i) { |
| 723 | if (images[i].nickname && images[i].nickname == partition_name) { |
| 724 | images[i].optional_if_no_image = false; |
| 725 | known_partition = true; |
| 726 | } |
| 727 | } |
| 728 | if (!known_partition) { |
| 729 | die("device requires partition %s which is not known to this version of fastboot", |
| 730 | partition_name.c_str()); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | static void CheckRequirements(const std::string& data) { |
| 735 | std::string cur_product; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 736 | if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) { |
| 737 | fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str()); |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | auto lines = Split(data, "\n"); |
| 741 | for (const auto& line : lines) { |
| 742 | if (line.empty()) { |
| 743 | continue; |
| 744 | } |
| 745 | |
| 746 | std::string name; |
| 747 | std::string product; |
| 748 | bool invert; |
| 749 | std::vector<std::string> options; |
| 750 | |
| 751 | if (!ParseRequirementLine(line, &name, &product, &invert, &options)) { |
| 752 | fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str()); |
| 753 | continue; |
| 754 | } |
| 755 | if (name == "partition-exists") { |
| 756 | HandlePartitionExists(options); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 757 | } else { |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 758 | CheckRequirement(cur_product, name, product, invert, options); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 763 | static void DisplayVarOrError(const std::string& label, const std::string& var) { |
| 764 | std::string value; |
| 765 | |
| 766 | if (fb->GetVar(var, &value) != fastboot::SUCCESS) { |
| 767 | Status("getvar:" + var); |
| 768 | fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str()); |
| 769 | return; |
| 770 | } |
| 771 | fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str()); |
| 772 | } |
| 773 | |
| 774 | static void DumpInfo() { |
| 775 | fprintf(stderr, "--------------------------------------------\n"); |
| 776 | DisplayVarOrError("Bootloader Version...", "version-bootloader"); |
| 777 | DisplayVarOrError("Baseband Version.....", "version-baseband"); |
| 778 | DisplayVarOrError("Serial Number........", "serialno"); |
| 779 | fprintf(stderr, "--------------------------------------------\n"); |
| 780 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Tao Bao | 41cf35f | 2018-04-24 10:54:21 -0700 | [diff] [blame] | 783 | static struct sparse_file** load_sparse_files(int fd, int64_t max_size) { |
Mohamad Ayyash | 80cc1f6 | 2015-03-31 12:09:29 -0700 | [diff] [blame] | 784 | struct sparse_file* s = sparse_file_import_auto(fd, false, true); |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 785 | if (!s) die("cannot sparse read file"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 786 | |
Tao Bao | 41cf35f | 2018-04-24 10:54:21 -0700 | [diff] [blame] | 787 | if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) { |
| 788 | die("invalid max size %" PRId64, max_size); |
| 789 | } |
| 790 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 791 | int files = sparse_file_resparse(s, max_size, nullptr, 0); |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 792 | if (files < 0) die("Failed to resparse"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 793 | |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 794 | 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] | 795 | if (!out_s) die("Failed to allocate sparse file array"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 796 | |
| 797 | files = sparse_file_resparse(s, max_size, out_s, files); |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 798 | if (files < 0) die("Failed to resparse"); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 799 | |
| 800 | return out_s; |
| 801 | } |
| 802 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 803 | static int64_t get_target_sparse_limit() { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 804 | std::string max_download_size; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 805 | if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS || |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 806 | max_download_size.empty()) { |
| 807 | verbose("target didn't report max-download-size"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 808 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Elliott Hughes | 77c0e66 | 2015-11-02 15:51:12 -0800 | [diff] [blame] | 811 | // Some bootloaders (angler, for example) send spurious whitespace too. |
| 812 | max_download_size = android::base::Trim(max_download_size); |
| 813 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 814 | uint64_t limit; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 815 | if (!android::base::ParseUint(max_download_size, &limit)) { |
Elliott Hughes | 3ab0586 | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 816 | 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] | 817 | return 0; |
| 818 | } |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 819 | 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] | 820 | return limit; |
| 821 | } |
| 822 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 823 | static int64_t get_sparse_limit(int64_t size) { |
Elliott Hughes | 542370d | 2018-04-19 19:49:44 -0700 | [diff] [blame] | 824 | int64_t limit = sparse_limit; |
| 825 | if (limit == 0) { |
| 826 | // Unlimited, so see what the target device's limit is. |
| 827 | // TODO: shouldn't we apply this limit even if you've used -S? |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 828 | if (target_sparse_limit == -1) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 829 | target_sparse_limit = get_target_sparse_limit(); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 830 | } |
| 831 | if (target_sparse_limit > 0) { |
| 832 | limit = target_sparse_limit; |
| 833 | } else { |
Colin Cross | 0bbfb39 | 2012-07-24 18:05:21 -0700 | [diff] [blame] | 834 | return 0; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | if (size > limit) { |
Chris Fries | 6a99971 | 2017-04-04 09:52:47 -0500 | [diff] [blame] | 839 | return std::min(limit, RESPARSE_LIMIT); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 845 | static bool load_buf_fd(int fd, struct fastboot_buffer* buf) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 846 | int64_t sz = get_file_size(fd); |
| 847 | if (sz == -1) { |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 848 | return false; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 849 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 850 | |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 851 | if (sparse_file* s = sparse_file_import_auto(fd, false, false)) { |
| 852 | buf->image_size = sparse_file_len(s, false, false); |
| 853 | sparse_file_destroy(s); |
| 854 | } else { |
| 855 | buf->image_size = sz; |
| 856 | } |
| 857 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 858 | lseek64(fd, 0, SEEK_SET); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 859 | int64_t limit = get_sparse_limit(sz); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 860 | if (limit) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 861 | sparse_file** s = load_sparse_files(fd, limit); |
| 862 | if (s == nullptr) { |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 863 | return false; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 864 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 865 | buf->type = FB_BUFFER_SPARSE; |
| 866 | buf->data = s; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 867 | } else { |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 868 | buf->type = FB_BUFFER_FD; |
| 869 | buf->data = nullptr; |
| 870 | buf->fd = fd; |
Alexander Levitskiy | 8d7ddb3 | 2014-05-07 23:31:59 +0000 | [diff] [blame] | 871 | buf->sz = sz; |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 872 | } |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 873 | |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 874 | return true; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 877 | static bool load_buf(const char* fname, struct fastboot_buffer* buf) { |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 878 | unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY))); |
| 879 | |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 880 | if (fd == -1) { |
| 881 | return false; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 882 | } |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 883 | |
| 884 | struct stat s; |
| 885 | if (fstat(fd, &s)) { |
| 886 | return false; |
| 887 | } |
| 888 | if (!S_ISREG(s.st_mode)) { |
| 889 | errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL; |
| 890 | return false; |
| 891 | } |
| 892 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 893 | return load_buf_fd(fd.release(), buf); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 894 | } |
| 895 | |
David Zeuthen | b6ea435 | 2017-08-07 14:29:26 -0400 | [diff] [blame] | 896 | static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) { |
| 897 | // Buffer needs to be at least the size of the VBMeta struct which |
| 898 | // is 256 bytes. |
| 899 | if (buf->sz < 256) { |
| 900 | return; |
| 901 | } |
| 902 | |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 903 | int fd = make_temporary_fd("vbmeta rewriting"); |
David Zeuthen | b6ea435 | 2017-08-07 14:29:26 -0400 | [diff] [blame] | 904 | |
| 905 | std::string data; |
| 906 | if (!android::base::ReadFdToString(buf->fd, &data)) { |
| 907 | die("Failed reading from vbmeta"); |
| 908 | } |
| 909 | |
| 910 | // There's a 32-bit big endian |flags| field at offset 120 where |
| 911 | // bit 0 corresponds to disable-verity and bit 1 corresponds to |
| 912 | // disable-verification. |
| 913 | // |
| 914 | // See external/avb/libavb/avb_vbmeta_image.h for the layout of |
| 915 | // the VBMeta struct. |
| 916 | if (g_disable_verity) { |
| 917 | data[123] |= 0x01; |
| 918 | } |
| 919 | if (g_disable_verification) { |
| 920 | data[123] |= 0x02; |
| 921 | } |
| 922 | |
| 923 | if (!android::base::WriteStringToFd(data, fd)) { |
| 924 | die("Failed writing to modified vbmeta"); |
| 925 | } |
| 926 | close(buf->fd); |
| 927 | buf->fd = fd; |
| 928 | lseek(fd, 0, SEEK_SET); |
| 929 | } |
| 930 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 931 | static void flash_buf(const std::string& partition, struct fastboot_buffer *buf) |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 932 | { |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 933 | sparse_file** s; |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 934 | |
David Zeuthen | b6ea435 | 2017-08-07 14:29:26 -0400 | [diff] [blame] | 935 | // Rewrite vbmeta if that's what we're flashing and modification has been requested. |
| 936 | if ((g_disable_verity || g_disable_verification) && |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 937 | (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) { |
David Zeuthen | b6ea435 | 2017-08-07 14:29:26 -0400 | [diff] [blame] | 938 | rewrite_vbmeta_buffer(buf); |
| 939 | } |
| 940 | |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 941 | switch (buf->type) { |
Josh Gao | 9da9ac5 | 2016-01-19 14:50:18 -0800 | [diff] [blame] | 942 | case FB_BUFFER_SPARSE: { |
| 943 | std::vector<std::pair<sparse_file*, int64_t>> sparse_files; |
Elliott Hughes | 253c18d | 2015-03-18 22:47:09 -0700 | [diff] [blame] | 944 | s = reinterpret_cast<sparse_file**>(buf->data); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 945 | while (*s) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 946 | int64_t sz = sparse_file_len(*s, true, false); |
Josh Gao | 9da9ac5 | 2016-01-19 14:50:18 -0800 | [diff] [blame] | 947 | sparse_files.emplace_back(*s, sz); |
| 948 | ++s; |
| 949 | } |
| 950 | |
| 951 | for (size_t i = 0; i < sparse_files.size(); ++i) { |
| 952 | const auto& pair = sparse_files[i]; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 953 | fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size()); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 954 | } |
| 955 | break; |
Josh Gao | 9da9ac5 | 2016-01-19 14:50:18 -0800 | [diff] [blame] | 956 | } |
Chris Fries | 0ea946c | 2017-04-12 10:25:57 -0500 | [diff] [blame] | 957 | case FB_BUFFER_FD: |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 958 | fb->FlashPartition(partition, buf->fd, buf->sz); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 959 | break; |
| 960 | default: |
| 961 | die("unknown buffer type: %d", buf->type); |
| 962 | } |
| 963 | } |
| 964 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 965 | static std::string get_current_slot() { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 966 | std::string current_slot; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 967 | if (fb->GetVar("current-slot", ¤t_slot) != fastboot::SUCCESS) return ""; |
Elliott Hughes | 42b18a5 | 2018-04-10 15:32:21 -0700 | [diff] [blame] | 968 | return current_slot; |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 971 | static int get_slot_count() { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 972 | std::string var; |
Elliott Hughes | 42b18a5 | 2018-04-10 15:32:21 -0700 | [diff] [blame] | 973 | int count = 0; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 974 | if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS || |
| 975 | !android::base::ParseInt(var, &count)) { |
Elliott Hughes | 42b18a5 | 2018-04-10 15:32:21 -0700 | [diff] [blame] | 976 | return 0; |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 977 | } |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 978 | return count; |
| 979 | } |
| 980 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 981 | static bool supports_AB() { |
| 982 | return get_slot_count() >= 2; |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 985 | // Given a current slot, this returns what the 'other' slot is. |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 986 | static std::string get_other_slot(const std::string& current_slot, int count) { |
| 987 | if (count == 0) return ""; |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 988 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 989 | char next = (current_slot[0] - 'a' + 1)%count + 'a'; |
| 990 | return std::string(1, next); |
| 991 | } |
| 992 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 993 | static std::string get_other_slot(const std::string& current_slot) { |
| 994 | return get_other_slot(current_slot, get_slot_count()); |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 997 | static std::string get_other_slot(int count) { |
| 998 | return get_other_slot(get_current_slot(), count); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1001 | static std::string get_other_slot() { |
| 1002 | return get_other_slot(get_current_slot(), get_slot_count()); |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1005 | static std::string verify_slot(const std::string& slot_name, bool allow_all) { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1006 | std::string slot = slot_name; |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1007 | if (slot == "all") { |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 1008 | if (allow_all) { |
| 1009 | return "all"; |
| 1010 | } else { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1011 | int count = get_slot_count(); |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1012 | if (count > 0) { |
| 1013 | return "a"; |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 1014 | } else { |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 1015 | die("No known slots"); |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 1016 | } |
| 1017 | } |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1018 | } |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 1019 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1020 | int count = get_slot_count(); |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 1021 | if (count == 0) die("Device does not support slots"); |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 1022 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1023 | if (slot == "other") { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1024 | std::string other = get_other_slot( count); |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 1025 | if (other == "") { |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 1026 | die("No known slots"); |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 1027 | } |
Daniel Rosenberg | ad3d3c1 | 2016-06-27 23:03:48 -0700 | [diff] [blame] | 1028 | return other; |
Daniel Rosenberg | c1743ba | 2016-01-05 16:54:40 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1031 | if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot; |
| 1032 | |
| 1033 | fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str()); |
| 1034 | for (int i=0; i<count; i++) { |
| 1035 | fprintf(stderr, "%c\n", (char)(i + 'a')); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1036 | } |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1037 | |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1038 | exit(1); |
| 1039 | } |
| 1040 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1041 | static std::string verify_slot(const std::string& slot) { |
| 1042 | return verify_slot(slot, true); |
Daniel Rosenberg | 9b43205 | 2015-11-25 15:17:10 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1045 | static void do_for_partition(const std::string& part, const std::string& slot, |
Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 1046 | const std::function<void(const std::string&)>& func, bool force_slot) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 1047 | std::string has_slot; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1048 | std::string current_slot; |
| 1049 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1050 | if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 1051 | /* If has-slot is not supported, the answer is no. */ |
| 1052 | has_slot = "no"; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1053 | } |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 1054 | if (has_slot == "yes") { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1055 | if (slot == "") { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1056 | current_slot = get_current_slot(); |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1057 | if (current_slot == "") { |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 1058 | die("Failed to identify current slot"); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1059 | } |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1060 | func(part + "_" + current_slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1061 | } else { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1062 | func(part + '_' + slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1063 | } |
| 1064 | } else { |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1065 | if (force_slot && slot != "") { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1066 | 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] | 1067 | part.c_str(), slot.c_str()); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1068 | } |
| 1069 | func(part); |
| 1070 | } |
| 1071 | } |
| 1072 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1073 | /* This function will find the real partition name given a base name, and a slot. If slot is NULL or |
| 1074 | * empty, it will use the current slot. If slot is "all", it will return a list of all possible |
| 1075 | * partition names. If force_slot is true, it will fail if a slot is specified, and the given |
| 1076 | * partition does not support slots. |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1077 | */ |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1078 | static void do_for_partitions(const std::string& part, const std::string& slot, |
Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 1079 | const std::function<void(const std::string&)>& func, bool force_slot) { |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 1080 | std::string has_slot; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1081 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1082 | if (slot == "all") { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1083 | if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) { |
Elliott Hughes | 4089d34 | 2017-10-27 14:21:12 -0700 | [diff] [blame] | 1084 | 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] | 1085 | } |
Daniel Rosenberg | a797479 | 2015-11-11 16:13:13 -0800 | [diff] [blame] | 1086 | if (has_slot == "yes") { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1087 | for (int i=0; i < get_slot_count(); i++) { |
| 1088 | do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1089 | } |
| 1090 | } else { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1091 | do_for_partition(part, "", func, force_slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } else { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1094 | do_for_partition(part, slot, func, force_slot); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1098 | static void do_flash(const char* pname, const char* fname) { |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1099 | struct fastboot_buffer buf; |
| 1100 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1101 | if (!load_buf(fname, &buf)) { |
Elliott Hughes | 53ec495 | 2016-05-11 12:39:27 -0700 | [diff] [blame] | 1102 | die("cannot load '%s': %s", fname, strerror(errno)); |
Rom Lemarchand | 622810c | 2013-06-28 09:54:59 -0700 | [diff] [blame] | 1103 | } |
| 1104 | flash_buf(pname, &buf); |
Colin Cross | f838788 | 2012-05-24 17:18:41 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1107 | // Sets slot_override as the active slot. If slot_override is blank, |
| 1108 | // set current slot as active instead. This clears slot-unbootable. |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1109 | static void set_active(const std::string& slot_override) { |
| 1110 | if (!supports_AB()) return; |
Elliott Hughes | 42b18a5 | 2018-04-10 15:32:21 -0700 | [diff] [blame] | 1111 | |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1112 | if (slot_override != "") { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1113 | fb->SetActive(slot_override); |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1114 | } else { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1115 | std::string current_slot = get_current_slot(); |
Daniel Rosenberg | 8091947 | 2016-06-30 19:25:31 -0700 | [diff] [blame] | 1116 | if (current_slot != "") { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1117 | fb->SetActive(current_slot); |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1122 | static bool is_userspace_fastboot() { |
| 1123 | std::string value; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1124 | return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes"; |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Hridya Valsaraju | f1f0a9c | 2018-08-02 10:46:21 -0700 | [diff] [blame] | 1127 | static bool if_partition_exists(const std::string& partition, const std::string& slot) { |
| 1128 | std::string has_slot; |
| 1129 | std::string partition_name = partition; |
| 1130 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1131 | if (fb->GetVar("has-slot:" + partition, &has_slot) == fastboot::SUCCESS && has_slot == "yes") { |
Hridya Valsaraju | f1f0a9c | 2018-08-02 10:46:21 -0700 | [diff] [blame] | 1132 | if (slot == "") { |
| 1133 | std::string current_slot = get_current_slot(); |
| 1134 | if (current_slot == "") { |
| 1135 | die("Failed to identify current slot"); |
| 1136 | } |
| 1137 | partition_name += "_" + current_slot; |
| 1138 | } else { |
| 1139 | partition_name += "_" + slot; |
| 1140 | } |
| 1141 | } |
| 1142 | std::string partition_size; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1143 | return fb->GetVar("partition-size:" + partition_name, &partition_size) == fastboot::SUCCESS; |
Hridya Valsaraju | f1f0a9c | 2018-08-02 10:46:21 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1146 | static bool is_logical(const std::string& partition) { |
| 1147 | std::string value; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1148 | return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes"; |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 1151 | static void reboot_to_userspace_fastboot() { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1152 | fb->RebootTo("fastboot"); |
| 1153 | |
| 1154 | auto* old_transport = fb->set_transport(nullptr); |
| 1155 | delete old_transport; |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 1156 | |
| 1157 | // Give the current connection time to close. |
| 1158 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); |
| 1159 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1160 | fb->set_transport(open_device()); |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1163 | class ImageSource { |
| 1164 | public: |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1165 | virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0; |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1166 | virtual int OpenFile(const std::string& name) const = 0; |
| 1167 | }; |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1168 | |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1169 | class FlashAllTool { |
| 1170 | public: |
| 1171 | FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe); |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1172 | |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1173 | void Flash(); |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1174 | |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1175 | private: |
| 1176 | void CheckRequirements(); |
| 1177 | void DetermineSecondarySlot(); |
| 1178 | void CollectImages(); |
| 1179 | void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images); |
| 1180 | void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf); |
| 1181 | void UpdateSuperPartition(); |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1182 | |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1183 | const ImageSource& source_; |
| 1184 | std::string slot_override_; |
| 1185 | bool skip_secondary_; |
| 1186 | bool wipe_; |
| 1187 | std::string secondary_slot_; |
| 1188 | std::vector<std::pair<const Image*, std::string>> boot_images_; |
| 1189 | std::vector<std::pair<const Image*, std::string>> os_images_; |
| 1190 | }; |
| 1191 | |
| 1192 | FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe) |
| 1193 | : source_(source), |
| 1194 | slot_override_(slot_override), |
| 1195 | skip_secondary_(skip_secondary), |
| 1196 | wipe_(wipe) |
| 1197 | { |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1200 | void FlashAllTool::Flash() { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1201 | DumpInfo(); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1202 | CheckRequirements(); |
| 1203 | DetermineSecondarySlot(); |
| 1204 | CollectImages(); |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1205 | |
David Anderson | 95d4093 | 2018-08-28 12:14:33 -0700 | [diff] [blame] | 1206 | // First flash boot partitions. We allow this to happen either in userspace |
| 1207 | // or in bootloader fastboot. |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1208 | FlashImages(boot_images_); |
David Anderson | 95d4093 | 2018-08-28 12:14:33 -0700 | [diff] [blame] | 1209 | |
| 1210 | // Sync the super partition. This will reboot to userspace fastboot if needed. |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1211 | UpdateSuperPartition(); |
David Anderson | 95d4093 | 2018-08-28 12:14:33 -0700 | [diff] [blame] | 1212 | |
| 1213 | // Resize any logical partition to 0, so each partition is reset to 0 |
| 1214 | // extents, and will achieve more optimal allocation. |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1215 | for (const auto& [image, slot] : os_images_) { |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1216 | auto resize_partition = [](const std::string& partition) -> void { |
| 1217 | if (is_logical(partition)) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1218 | fb->ResizePartition(partition, "0"); |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1219 | } |
| 1220 | }; |
David Anderson | 95d4093 | 2018-08-28 12:14:33 -0700 | [diff] [blame] | 1221 | do_for_partitions(image->part_name, slot, resize_partition, false); |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
David Anderson | 95d4093 | 2018-08-28 12:14:33 -0700 | [diff] [blame] | 1224 | // Flash OS images, resizing logical partitions as needed. |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1225 | FlashImages(os_images_); |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1226 | |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1227 | if (slot_override_ == "all") { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1228 | set_active("a"); |
Daniel Rosenberg | 92b4476 | 2016-07-13 20:03:25 -0700 | [diff] [blame] | 1229 | } else { |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1230 | set_active(slot_override_); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1231 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1234 | void FlashAllTool::CheckRequirements() { |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1235 | std::vector<char> contents; |
| 1236 | if (!source_.ReadFile("android-info.txt", &contents)) { |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1237 | die("could not read android-info.txt"); |
| 1238 | } |
Tom Cherry | 4aa60b3 | 2018-09-05 15:11:44 -0700 | [diff] [blame] | 1239 | ::CheckRequirements({contents.data(), contents.size()}); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | void FlashAllTool::DetermineSecondarySlot() { |
| 1243 | if (skip_secondary_) { |
| 1244 | return; |
| 1245 | } |
| 1246 | if (slot_override_ != "") { |
| 1247 | secondary_slot_ = get_other_slot(slot_override_); |
| 1248 | } else { |
| 1249 | secondary_slot_ = get_other_slot(); |
| 1250 | } |
| 1251 | if (secondary_slot_ == "") { |
| 1252 | if (supports_AB()) { |
| 1253 | fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n"); |
| 1254 | } |
| 1255 | skip_secondary_ = true; |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | void FlashAllTool::CollectImages() { |
| 1260 | for (size_t i = 0; i < arraysize(images); ++i) { |
| 1261 | std::string slot = slot_override_; |
| 1262 | if (images[i].IsSecondary()) { |
| 1263 | if (skip_secondary_) { |
| 1264 | continue; |
| 1265 | } |
| 1266 | slot = secondary_slot_; |
| 1267 | } |
| 1268 | if (images[i].type == ImageType::BootCritical) { |
| 1269 | boot_images_.emplace_back(&images[i], slot); |
| 1270 | } else if (images[i].type == ImageType::Normal) { |
| 1271 | os_images_.emplace_back(&images[i], slot); |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) { |
| 1277 | for (const auto& [image, slot] : images) { |
| 1278 | fastboot_buffer buf; |
| 1279 | int fd = source_.OpenFile(image->img_name); |
| 1280 | if (fd < 0 || !load_buf_fd(fd, &buf)) { |
| 1281 | if (image->optional_if_no_image) { |
| 1282 | continue; |
| 1283 | } |
| 1284 | die("could not load '%s': %s", image->img_name, strerror(errno)); |
| 1285 | } |
| 1286 | FlashImage(*image, slot, &buf); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) { |
| 1291 | auto flash = [&, this](const std::string& partition_name) { |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1292 | std::vector<char> signature_data; |
| 1293 | if (source_.ReadFile(image.sig_name, &signature_data)) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1294 | fb->Download("signature", signature_data); |
| 1295 | fb->RawCommand("signature", "installing signature"); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | if (is_logical(partition_name)) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1299 | fb->ResizePartition(partition_name, std::to_string(buf->image_size)); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1300 | } |
| 1301 | flash_buf(partition_name.c_str(), buf); |
| 1302 | }; |
| 1303 | do_for_partitions(image.part_name, slot, flash, false); |
| 1304 | } |
| 1305 | |
| 1306 | void FlashAllTool::UpdateSuperPartition() { |
| 1307 | if (!if_partition_exists("super", "")) { |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | int fd = source_.OpenFile("super_empty.img"); |
| 1312 | if (fd < 0) { |
| 1313 | return; |
| 1314 | } |
| 1315 | if (!is_userspace_fastboot()) { |
| 1316 | reboot_to_userspace_fastboot(); |
| 1317 | } |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1318 | fb->Download("super", fd, get_file_size(fd)); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1319 | |
| 1320 | std::string command = "update-super:super"; |
| 1321 | if (wipe_) { |
| 1322 | command += ":wipe"; |
| 1323 | } |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1324 | fb->RawCommand(command, "Updating super partition"); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | class ZipImageSource final : public ImageSource { |
| 1328 | public: |
| 1329 | explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {} |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1330 | bool ReadFile(const std::string& name, std::vector<char>* out) const override; |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1331 | int OpenFile(const std::string& name) const override; |
| 1332 | |
| 1333 | private: |
| 1334 | ZipArchiveHandle zip_; |
| 1335 | }; |
| 1336 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1337 | bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const { |
| 1338 | return UnzipToMemory(zip_, name, out); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | int ZipImageSource::OpenFile(const std::string& name) const { |
| 1342 | return unzip_to_file(zip_, name.c_str()); |
| 1343 | } |
| 1344 | |
| 1345 | static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) { |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1346 | ZipArchiveHandle zip; |
| 1347 | int error = OpenArchive(filename, &zip); |
| 1348 | if (error != 0) { |
| 1349 | die("failed to open zip file '%s': %s", filename, ErrorCodeString(error)); |
| 1350 | } |
| 1351 | |
| 1352 | FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false); |
| 1353 | tool.Flash(); |
| 1354 | |
| 1355 | CloseArchive(zip); |
| 1356 | } |
| 1357 | |
| 1358 | class LocalImageSource final : public ImageSource { |
| 1359 | public: |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1360 | bool ReadFile(const std::string& name, std::vector<char>* out) const override; |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1361 | int OpenFile(const std::string& name) const override; |
| 1362 | }; |
| 1363 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1364 | bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const { |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1365 | auto path = find_item_given_name(name); |
| 1366 | if (path.empty()) { |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1367 | return false; |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1368 | } |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1369 | return ReadFileToVector(path, out); |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | int LocalImageSource::OpenFile(const std::string& name) const { |
| 1373 | auto path = find_item_given_name(name); |
| 1374 | return open(path.c_str(), O_RDONLY); |
| 1375 | } |
| 1376 | |
| 1377 | static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) { |
David Anderson | cf444f3 | 2018-08-29 14:15:49 -0700 | [diff] [blame] | 1378 | FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe); |
| 1379 | tool.Flash(); |
| 1380 | } |
| 1381 | |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1382 | static std::string next_arg(std::vector<std::string>* args) { |
| 1383 | if (args->empty()) syntax_error("expected argument"); |
| 1384 | std::string result = args->front(); |
| 1385 | args->erase(args->begin()); |
| 1386 | return result; |
Patrick Tjin | 51e8b03 | 2015-09-01 08:15:23 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Elliott Hughes | 1eec97a | 2017-05-15 16:53:53 -0700 | [diff] [blame] | 1389 | 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] | 1390 | if (args->empty()) syntax_error("empty oem command"); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1391 | |
Elliott Hughes | 1eec97a | 2017-05-15 16:53:53 -0700 | [diff] [blame] | 1392 | std::string command(cmd); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1393 | while (!args->empty()) { |
Elliott Hughes | 29d5d7d | 2017-05-11 15:05:13 -0700 | [diff] [blame] | 1394 | command += " " + next_arg(args); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1395 | } |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1396 | fb->RawCommand(command, ""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 1399 | static std::string fb_fix_numeric_var(std::string var) { |
| 1400 | // Some bootloaders (angler, for example), send spurious leading whitespace. |
| 1401 | var = android::base::Trim(var); |
| 1402 | // Some bootloaders (hammerhead, for example) use implicit hex. |
| 1403 | // This code used to use strtol with base 16. |
| 1404 | if (!android::base::StartsWith(var, "0x")) var = "0x" + var; |
| 1405 | return var; |
| 1406 | } |
| 1407 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1408 | static unsigned fb_get_flash_block_size(std::string name) { |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 1409 | std::string sizeString; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1410 | if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 1411 | // This device does not report flash block sizes, so return 0. |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 1412 | return 0; |
| 1413 | } |
| 1414 | sizeString = fb_fix_numeric_var(sizeString); |
| 1415 | |
| 1416 | unsigned size; |
| 1417 | if (!android::base::ParseUint(sizeString, &size)) { |
| 1418 | fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str()); |
| 1419 | return 0; |
| 1420 | } |
Connor O'Brien | 6ef5c24 | 2017-11-01 17:37:32 -0700 | [diff] [blame] | 1421 | if ((size & (size - 1)) != 0) { |
| 1422 | 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] | 1423 | return 0; |
| 1424 | } |
| 1425 | return size; |
| 1426 | } |
| 1427 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1428 | static void fb_perform_format( |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 1429 | const std::string& partition, int skip_if_not_supported, |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1430 | const std::string& type_override, const std::string& size_override, |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 1431 | const std::string& initial_dir) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1432 | std::string partition_type, partition_size; |
| 1433 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1434 | struct fastboot_buffer buf; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1435 | const char* errMsg = nullptr; |
| 1436 | const struct fs_generator* gen = nullptr; |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 1437 | TemporaryFile output; |
| 1438 | unique_fd fd; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1439 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1440 | unsigned int limit = INT_MAX; |
| 1441 | if (target_sparse_limit > 0 && target_sparse_limit < limit) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1442 | limit = target_sparse_limit; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1443 | } |
| 1444 | if (sparse_limit > 0 && sparse_limit < limit) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1445 | limit = sparse_limit; |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1446 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1447 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1448 | if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1449 | errMsg = "Can't determine partition type.\n"; |
| 1450 | goto failed; |
| 1451 | } |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1452 | if (!type_override.empty()) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1453 | if (partition_type != type_override) { |
| 1454 | 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] | 1455 | partition.c_str(), partition_type.c_str(), type_override.c_str()); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1456 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1457 | partition_type = type_override; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1458 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1459 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1460 | if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1461 | errMsg = "Unable to get partition size\n"; |
| 1462 | goto failed; |
| 1463 | } |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1464 | if (!size_override.empty()) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1465 | if (partition_size != size_override) { |
| 1466 | 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] | 1467 | partition.c_str(), partition_size.c_str(), size_override.c_str()); |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1468 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1469 | partition_size = size_override; |
JP Abgrall | 7e85974 | 2014-05-06 15:14:15 -0700 | [diff] [blame] | 1470 | } |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 1471 | partition_size = fb_fix_numeric_var(partition_size); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1472 | |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 1473 | gen = fs_get_generator(partition_type); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1474 | if (!gen) { |
| 1475 | if (skip_if_not_supported) { |
| 1476 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1477 | 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] | 1478 | return; |
| 1479 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1480 | fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n", |
| 1481 | partition_type.c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1482 | return; |
| 1483 | } |
| 1484 | |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1485 | int64_t size; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 1486 | if (!android::base::ParseInt(partition_size, &size)) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1487 | fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str()); |
| 1488 | return; |
| 1489 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1490 | |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 1491 | unsigned eraseBlkSize, logicalBlkSize; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1492 | eraseBlkSize = fb_get_flash_block_size("erase-block-size"); |
| 1493 | logicalBlkSize = fb_get_flash_block_size("logical-block-size"); |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 1494 | |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 1495 | if (fs_generator_generate(gen, output.path, size, initial_dir, |
| 1496 | eraseBlkSize, logicalBlkSize)) { |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 1497 | die("Cannot generate image for %s", partition.c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1498 | return; |
| 1499 | } |
| 1500 | |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 1501 | fd.reset(open(output.path, O_RDONLY)); |
| 1502 | if (fd == -1) { |
| 1503 | fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno)); |
| 1504 | return; |
| 1505 | } |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1506 | if (!load_buf_fd(fd.release(), &buf)) { |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1507 | fprintf(stderr, "Cannot read image: %s\n", strerror(errno)); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1508 | return; |
| 1509 | } |
| 1510 | flash_buf(partition, &buf); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1511 | return; |
| 1512 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1513 | failed: |
| 1514 | if (skip_if_not_supported) { |
| 1515 | fprintf(stderr, "Erase successful, but not automatically formatting.\n"); |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1516 | if (errMsg) fprintf(stderr, "%s", errMsg); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1517 | } |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1518 | fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str()); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1521 | int FastBootTool::Main(int argc, char* argv[]) { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1522 | bool wants_wipe = false; |
| 1523 | bool wants_reboot = false; |
| 1524 | bool wants_reboot_bootloader = false; |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 1525 | bool wants_reboot_recovery = false; |
| 1526 | bool wants_reboot_fastboot = false; |
Mitchell Wills | 31dce30 | 2016-09-26 10:26:21 -0700 | [diff] [blame] | 1527 | bool skip_reboot = false; |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1528 | bool wants_set_active = false; |
Daniel Rosenberg | 92b4476 | 2016-07-13 20:03:25 -0700 | [diff] [blame] | 1529 | bool skip_secondary = false; |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 1530 | bool set_fbe_marker = false; |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1531 | int longindex; |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1532 | std::string slot_override; |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1533 | std::string next_active; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1534 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1535 | g_boot_img_hdr.kernel_addr = 0x00008000; |
| 1536 | g_boot_img_hdr.ramdisk_addr = 0x01000000; |
| 1537 | g_boot_img_hdr.second_addr = 0x00f00000; |
| 1538 | g_boot_img_hdr.tags_addr = 0x00000100; |
| 1539 | g_boot_img_hdr.page_size = 2048; |
| 1540 | |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1541 | const struct option longopts[] = { |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1542 | {"base", required_argument, 0, 0}, |
| 1543 | {"cmdline", required_argument, 0, 0}, |
| 1544 | {"disable-verification", no_argument, 0, 0}, |
| 1545 | {"disable-verity", no_argument, 0, 0}, |
| 1546 | {"header-version", required_argument, 0, 0}, |
Elliott Hughes | 379646b | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1547 | {"help", no_argument, 0, 'h'}, |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1548 | {"kernel-offset", required_argument, 0, 0}, |
| 1549 | {"os-patch-level", required_argument, 0, 0}, |
| 1550 | {"os-version", required_argument, 0, 0}, |
| 1551 | {"page-size", required_argument, 0, 0}, |
| 1552 | {"ramdisk-offset", required_argument, 0, 0}, |
Daniel Rosenberg | 7aa38bc | 2015-11-11 17:29:37 -0800 | [diff] [blame] | 1553 | {"set-active", optional_argument, 0, 'a'}, |
Mitchell Wills | 31dce30 | 2016-09-26 10:26:21 -0700 | [diff] [blame] | 1554 | {"skip-reboot", no_argument, 0, 0}, |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1555 | {"skip-secondary", no_argument, 0, 0}, |
| 1556 | {"slot", required_argument, 0, 0}, |
| 1557 | {"tags-offset", required_argument, 0, 0}, |
| 1558 | {"unbuffered", no_argument, 0, 0}, |
Elliott Hughes | f238d87 | 2018-03-29 14:46:29 -0700 | [diff] [blame] | 1559 | {"verbose", no_argument, 0, 'v'}, |
| 1560 | {"version", no_argument, 0, 0}, |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 1561 | #if !defined(_WIN32) |
| 1562 | {"wipe-and-use-fbe", no_argument, 0, 0}, |
| 1563 | #endif |
JP Abgrall | 7b8970c | 2013-03-07 17:06:41 -0800 | [diff] [blame] | 1564 | {0, 0, 0, 0} |
| 1565 | }; |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1566 | |
| 1567 | serial = getenv("ANDROID_SERIAL"); |
| 1568 | |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1569 | int c; |
Elliott Hughes | f3192bd | 2018-04-10 15:38:08 -0700 | [diff] [blame] | 1570 | while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) { |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1571 | if (c == 0) { |
| 1572 | std::string name{longopts[longindex].name}; |
| 1573 | if (name == "base") { |
| 1574 | g_base_addr = strtoul(optarg, 0, 16); |
| 1575 | } else if (name == "cmdline") { |
| 1576 | g_cmdline = optarg; |
| 1577 | } else if (name == "disable-verification") { |
| 1578 | g_disable_verification = true; |
| 1579 | } else if (name == "disable-verity") { |
| 1580 | g_disable_verity = true; |
| 1581 | } else if (name == "header-version") { |
| 1582 | g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0); |
| 1583 | } else if (name == "kernel-offset") { |
| 1584 | g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16); |
| 1585 | } else if (name == "os-patch-level") { |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 1586 | ParseOsPatchLevel(&g_boot_img_hdr, optarg); |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1587 | } else if (name == "os-version") { |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 1588 | ParseOsVersion(&g_boot_img_hdr, optarg); |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1589 | } else if (name == "page-size") { |
| 1590 | g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0); |
| 1591 | if (g_boot_img_hdr.page_size == 0) die("invalid page size"); |
| 1592 | } else if (name == "ramdisk-offset") { |
| 1593 | g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16); |
| 1594 | } else if (name == "skip-reboot") { |
| 1595 | skip_reboot = true; |
| 1596 | } else if (name == "skip-secondary") { |
| 1597 | skip_secondary = true; |
| 1598 | } else if (name == "slot") { |
| 1599 | slot_override = optarg; |
| 1600 | } else if (name == "tags-offset") { |
| 1601 | g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16); |
| 1602 | } else if (name == "unbuffered") { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 1603 | setvbuf(stdout, nullptr, _IONBF, 0); |
| 1604 | setvbuf(stderr, nullptr, _IONBF, 0); |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1605 | } else if (name == "version") { |
Dan Willemsen | ab971b5 | 2018-08-29 14:58:02 -0700 | [diff] [blame] | 1606 | fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str()); |
Elliott Hughes | 1fd46df | 2017-03-30 15:08:28 -0700 | [diff] [blame] | 1607 | fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str()); |
Elliott Hughes | 379646b | 2015-06-02 13:50:00 -0700 | [diff] [blame] | 1608 | return 0; |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 1609 | #if !defined(_WIN32) |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1610 | } else if (name == "wipe-and-use-fbe") { |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 1611 | wants_wipe = true; |
| 1612 | set_fbe_marker = true; |
| 1613 | #endif |
| 1614 | } else { |
Elliott Hughes | 855cdf8 | 2018-04-02 14:24:03 -0700 | [diff] [blame] | 1615 | die("unknown option %s", longopts[longindex].name); |
Florian Bäuerle | 27ded48 | 2014-11-24 11:29:34 +0100 | [diff] [blame] | 1616 | } |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1617 | } else { |
| 1618 | switch (c) { |
| 1619 | case 'a': |
| 1620 | wants_set_active = true; |
| 1621 | if (optarg) next_active = optarg; |
| 1622 | break; |
| 1623 | case 'h': |
| 1624 | return show_help(); |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1625 | case 'l': |
| 1626 | g_long_listing = true; |
| 1627 | break; |
| 1628 | case 's': |
| 1629 | serial = optarg; |
| 1630 | break; |
| 1631 | case 'S': |
Elliott Hughes | 542370d | 2018-04-19 19:49:44 -0700 | [diff] [blame] | 1632 | if (!android::base::ParseByteCount(optarg, &sparse_limit)) { |
| 1633 | die("invalid sparse limit %s", optarg); |
| 1634 | } |
Elliott Hughes | 577e8b4 | 2018-04-05 16:12:47 -0700 | [diff] [blame] | 1635 | break; |
| 1636 | case 'v': |
| 1637 | set_verbose(); |
| 1638 | break; |
| 1639 | case 'w': |
| 1640 | wants_wipe = true; |
| 1641 | break; |
| 1642 | case '?': |
| 1643 | return 1; |
| 1644 | default: |
| 1645 | abort(); |
| 1646 | } |
Colin Cross | 8879f98 | 2012-05-22 17:53:34 -0700 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | argc -= optind; |
| 1651 | argv += optind; |
| 1652 | |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1653 | 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] | 1654 | |
Colin Cross | 8fb6e06 | 2012-07-24 16:36:41 -0700 | [diff] [blame] | 1655 | if (argc > 0 && !strcmp(*argv, "devices")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1656 | list_devices(); |
Tsu Chiang Chuang | ee52055 | 2011-02-25 18:38:53 -0800 | [diff] [blame] | 1657 | return 0; |
| 1658 | } |
| 1659 | |
Colin Cross | c7b75dc | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 1660 | if (argc > 0 && !strcmp(*argv, "help")) { |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1661 | return show_help(); |
Colin Cross | c7b75dc | 2012-08-29 18:17:06 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1664 | Transport* transport = open_device(); |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 1665 | if (transport == nullptr) { |
| 1666 | return 1; |
| 1667 | } |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1668 | fastboot::DriverCallbacks driver_callbacks = { |
| 1669 | .prolog = Status, |
| 1670 | .epilog = Epilog, |
| 1671 | .info = InfoMessage, |
| 1672 | }; |
| 1673 | fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false); |
| 1674 | fb = &fastboot_driver; |
David Pursell | 2ec418a | 2016-01-20 08:32:08 -0800 | [diff] [blame] | 1675 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 1676 | const double start = now(); |
| 1677 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1678 | if (slot_override != "") slot_override = verify_slot(slot_override); |
| 1679 | if (next_active != "") next_active = verify_slot(next_active, false); |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1680 | |
| 1681 | if (wants_set_active) { |
| 1682 | if (next_active == "") { |
| 1683 | if (slot_override == "") { |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1684 | std::string current_slot; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1685 | if (fb->GetVar("current-slot", ¤t_slot) == fastboot::SUCCESS) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1686 | next_active = verify_slot(current_slot, false); |
Daniel Rosenberg | 1345409 | 2016-06-27 19:43:11 -0700 | [diff] [blame] | 1687 | } else { |
| 1688 | wants_set_active = false; |
| 1689 | } |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1690 | } else { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1691 | next_active = verify_slot(slot_override, false); |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | } |
Elliott Hughes | 31dbed7 | 2009-10-07 15:38:53 -0700 | [diff] [blame] | 1695 | |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1696 | std::vector<std::string> args(argv, argv + argc); |
| 1697 | while (!args.empty()) { |
| 1698 | std::string command = next_arg(&args); |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1699 | |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1700 | if (command == "getvar") { |
| 1701 | std::string variable = next_arg(&args); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1702 | DisplayVarOrError(variable, variable); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1703 | } else if (command == "erase") { |
| 1704 | std::string partition = next_arg(&args); |
| 1705 | auto erase = [&](const std::string& partition) { |
David Pursell | 0b15663 | 2015-10-30 11:22:01 -0700 | [diff] [blame] | 1706 | std::string partition_type; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1707 | if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS && |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1708 | fs_get_generator(partition_type) != nullptr) { |
| 1709 | fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n", |
| 1710 | partition_type.c_str()); |
| 1711 | } |
Ken Sumrall | 5ee5d38 | 2012-09-29 14:46:25 -0700 | [diff] [blame] | 1712 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1713 | fb->Erase(partition); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1714 | }; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1715 | do_for_partitions(partition, slot_override, erase, true); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1716 | } else if (android::base::StartsWith(command, "format")) { |
| 1717 | // Parsing for: "format[:[type][:[size]]]" |
| 1718 | // Some valid things: |
| 1719 | // - select only the size, and leave default fs type: |
| 1720 | // format::0x4000000 userdata |
| 1721 | // - default fs type and size: |
| 1722 | // format userdata |
| 1723 | // format:: userdata |
| 1724 | std::vector<std::string> pieces = android::base::Split(command, ":"); |
| 1725 | std::string type_override; |
| 1726 | if (pieces.size() > 1) type_override = pieces[1].c_str(); |
| 1727 | std::string size_override; |
| 1728 | if (pieces.size() > 2) size_override = pieces[2].c_str(); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1729 | |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1730 | std::string partition = next_arg(&args); |
| 1731 | |
| 1732 | auto format = [&](const std::string& partition) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1733 | fb_perform_format(partition, 0, type_override, size_override, ""); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1734 | }; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1735 | do_for_partitions(partition.c_str(), slot_override, format, true); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1736 | } else if (command == "signature") { |
| 1737 | std::string filename = next_arg(&args); |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1738 | std::vector<char> data; |
| 1739 | if (!ReadFileToVector(filename, &data)) { |
| 1740 | die("could not load '%s': %s", filename.c_str(), strerror(errno)); |
| 1741 | } |
| 1742 | if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size()); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1743 | fb->Download("signature", data); |
| 1744 | fb->RawCommand("signature", "installing signature"); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1745 | } else if (command == "reboot") { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1746 | wants_reboot = true; |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1747 | |
| 1748 | if (args.size() == 1) { |
| 1749 | std::string what = next_arg(&args); |
| 1750 | if (what == "bootloader") { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1751 | wants_reboot = false; |
| 1752 | wants_reboot_bootloader = true; |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 1753 | } else if (what == "recovery") { |
| 1754 | wants_reboot = false; |
| 1755 | wants_reboot_recovery = true; |
| 1756 | } else if (what == "fastboot") { |
| 1757 | wants_reboot = false; |
| 1758 | wants_reboot_fastboot = true; |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1759 | } else { |
| 1760 | syntax_error("unknown reboot target %s", what.c_str()); |
Alexey Polyudov | e0bfb75 | 2017-01-03 19:39:13 -0800 | [diff] [blame] | 1761 | } |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1762 | |
Elliott Hughes | ca85df0 | 2015-02-25 10:02:00 -0800 | [diff] [blame] | 1763 | } |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1764 | if (!args.empty()) syntax_error("junk after reboot command"); |
| 1765 | } else if (command == "reboot-bootloader") { |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1766 | wants_reboot_bootloader = true; |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 1767 | } else if (command == "reboot-recovery") { |
| 1768 | wants_reboot_recovery = true; |
| 1769 | } else if (command == "reboot-fastboot") { |
| 1770 | wants_reboot_fastboot = true; |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1771 | } else if (command == "continue") { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1772 | fb->Continue(); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1773 | } else if (command == "boot") { |
| 1774 | std::string kernel = next_arg(&args); |
| 1775 | std::string ramdisk; |
| 1776 | if (!args.empty()) ramdisk = next_arg(&args); |
| 1777 | std::string second_stage; |
| 1778 | if (!args.empty()) second_stage = next_arg(&args); |
| 1779 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1780 | auto data = LoadBootableImage(kernel, ramdisk, second_stage); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1781 | fb->Download("boot.img", data); |
| 1782 | fb->Boot(); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1783 | } else if (command == "flash") { |
| 1784 | std::string pname = next_arg(&args); |
| 1785 | |
Elliott Hughes | 2810d00 | 2016-04-25 14:31:18 -0700 | [diff] [blame] | 1786 | std::string fname; |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1787 | if (!args.empty()) { |
| 1788 | fname = next_arg(&args); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1789 | } else { |
Elliott Hughes | 45964a8 | 2017-05-03 22:43:23 -0700 | [diff] [blame] | 1790 | fname = find_item(pname); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1791 | } |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1792 | 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] | 1793 | |
| 1794 | auto flash = [&](const std::string &partition) { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1795 | do_flash(partition.c_str(), fname.c_str()); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1796 | }; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1797 | do_for_partitions(pname.c_str(), slot_override, flash, true); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1798 | } else if (command == "flash:raw") { |
| 1799 | std::string partition = next_arg(&args); |
| 1800 | std::string kernel = next_arg(&args); |
| 1801 | std::string ramdisk; |
| 1802 | if (!args.empty()) ramdisk = next_arg(&args); |
| 1803 | std::string second_stage; |
| 1804 | if (!args.empty()) second_stage = next_arg(&args); |
| 1805 | |
Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 1806 | auto data = LoadBootableImage(kernel, ramdisk, second_stage); |
| 1807 | auto flashraw = [&data](const std::string& partition) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1808 | fb->FlashPartition(partition, data); |
Daniel Rosenberg | b7bd4ae | 2015-09-14 21:05:41 -0700 | [diff] [blame] | 1809 | }; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1810 | do_for_partitions(partition, slot_override, flashraw, true); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1811 | } else if (command == "flashall") { |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1812 | if (slot_override == "all") { |
Daniel Rosenberg | 92b4476 | 2016-07-13 20:03:25 -0700 | [diff] [blame] | 1813 | fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n"); |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1814 | do_flashall(slot_override, true, wants_wipe); |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1815 | } else { |
David Anderson | 32e376f | 2018-08-16 13:43:11 -0700 | [diff] [blame] | 1816 | do_flashall(slot_override, skip_secondary, wants_wipe); |
Alex Light | bb9b8a5 | 2016-06-29 09:26:44 -0700 | [diff] [blame] | 1817 | } |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1818 | wants_reboot = true; |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1819 | } else if (command == "update") { |
Daniel Rosenberg | 92b4476 | 2016-07-13 20:03:25 -0700 | [diff] [blame] | 1820 | bool slot_all = (slot_override == "all"); |
| 1821 | if (slot_all) { |
| 1822 | fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n"); |
| 1823 | } |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1824 | std::string filename = "update.zip"; |
| 1825 | if (!args.empty()) { |
| 1826 | filename = next_arg(&args); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1827 | } |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1828 | do_update(filename.c_str(), slot_override, skip_secondary || slot_all); |
Mitchell Wills | 31dce30 | 2016-09-26 10:26:21 -0700 | [diff] [blame] | 1829 | wants_reboot = true; |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1830 | } else if (command == "set_active") { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1831 | std::string slot = verify_slot(next_arg(&args), false); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1832 | fb->SetActive(slot); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1833 | } else if (command == "stage") { |
| 1834 | std::string filename = next_arg(&args); |
| 1835 | |
Jocelyn Bohr | 98cc283 | 2017-01-26 19:20:53 -0800 | [diff] [blame] | 1836 | struct fastboot_buffer buf; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1837 | if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) { |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1838 | die("cannot load '%s'", filename.c_str()); |
Jocelyn Bohr | 98cc283 | 2017-01-26 19:20:53 -0800 | [diff] [blame] | 1839 | } |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1840 | fb->Download(filename, buf.fd, buf.sz); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1841 | } else if (command == "get_staged") { |
| 1842 | std::string filename = next_arg(&args); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1843 | fb->Upload(filename); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1844 | } else if (command == "oem") { |
Elliott Hughes | 1eec97a | 2017-05-15 16:53:53 -0700 | [diff] [blame] | 1845 | do_oem_command("oem", &args); |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1846 | } else if (command == "flashing") { |
| 1847 | if (args.empty()) { |
| 1848 | syntax_error("missing 'flashing' command"); |
| 1849 | } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" || |
| 1850 | args[0] == "unlock_critical" || |
| 1851 | args[0] == "lock_critical" || |
Elliott Hughes | 2e9b779 | 2018-04-04 13:36:44 -0700 | [diff] [blame] | 1852 | args[0] == "get_unlock_ability")) { |
Elliott Hughes | 1eec97a | 2017-05-15 16:53:53 -0700 | [diff] [blame] | 1853 | do_oem_command("flashing", &args); |
Badhri Jagan Sridharan | bf11095 | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 1854 | } else { |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1855 | syntax_error("unknown 'flashing' command %s", args[0].c_str()); |
Badhri Jagan Sridharan | bf11095 | 2015-05-15 16:43:47 -0700 | [diff] [blame] | 1856 | } |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 1857 | } else if (command == "create-logical-partition") { |
| 1858 | std::string partition = next_arg(&args); |
| 1859 | std::string size = next_arg(&args); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1860 | fb->CreatePartition(partition, size); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 1861 | } else if (command == "delete-logical-partition") { |
| 1862 | std::string partition = next_arg(&args); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1863 | fb->DeletePartition(partition); |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 1864 | } else if (command == "resize-logical-partition") { |
| 1865 | std::string partition = next_arg(&args); |
| 1866 | std::string size = next_arg(&args); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1867 | fb->ResizePartition(partition, size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1868 | } else { |
Elliott Hughes | d6365a7 | 2017-05-08 18:04:49 -0700 | [diff] [blame] | 1869 | syntax_error("unknown command %s", command.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | if (wants_wipe) { |
Paul Crowley | 4d17006 | 2018-04-24 17:06:30 -0700 | [diff] [blame] | 1874 | std::vector<std::string> partitions = { "userdata", "cache", "metadata" }; |
| 1875 | for (const auto& partition : partitions) { |
| 1876 | std::string partition_type; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1877 | if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) { |
| 1878 | continue; |
| 1879 | } |
Paul Crowley | 4d17006 | 2018-04-24 17:06:30 -0700 | [diff] [blame] | 1880 | if (partition_type.empty()) continue; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1881 | fb->Erase(partition); |
Paul Crowley | 4d17006 | 2018-04-24 17:06:30 -0700 | [diff] [blame] | 1882 | if (partition == "userdata" && set_fbe_marker) { |
| 1883 | fprintf(stderr, "setting FBE marker on initial userdata...\n"); |
| 1884 | std::string initial_userdata_dir = create_fbemarker_tmpdir(); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1885 | fb_perform_format(partition, 1, "", "", initial_userdata_dir); |
Paul Crowley | 4d17006 | 2018-04-24 17:06:30 -0700 | [diff] [blame] | 1886 | delete_fbemarker_tmpdir(initial_userdata_dir); |
| 1887 | } else { |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1888 | fb_perform_format(partition, 1, "", "", ""); |
Paul Crowley | 4d17006 | 2018-04-24 17:06:30 -0700 | [diff] [blame] | 1889 | } |
Elliott Hughes | 2fd45a9 | 2015-10-30 11:49:47 -0700 | [diff] [blame] | 1890 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1891 | } |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1892 | if (wants_set_active) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1893 | fb->SetActive(next_active); |
Daniel Rosenberg | 0d08856 | 2015-11-11 16:15:30 -0800 | [diff] [blame] | 1894 | } |
Mitchell Wills | 31dce30 | 2016-09-26 10:26:21 -0700 | [diff] [blame] | 1895 | if (wants_reboot && !skip_reboot) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1896 | fb->Reboot(); |
| 1897 | fb->WaitForDisconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1898 | } else if (wants_reboot_bootloader) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1899 | fb->RebootTo("bootloader"); |
| 1900 | fb->WaitForDisconnect(); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 1901 | } else if (wants_reboot_recovery) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1902 | fb->RebootTo("recovery"); |
| 1903 | fb->WaitForDisconnect(); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 1904 | } else if (wants_reboot_fastboot) { |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1905 | fb->RebootTo("fastboot"); |
| 1906 | fb->WaitForDisconnect(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
Elliott Hughes | 5620d22 | 2018-03-28 08:20:00 -0700 | [diff] [blame] | 1909 | fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start)); |
David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 1910 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame^] | 1911 | auto* old_transport = fb->set_transport(nullptr); |
| 1912 | delete old_transport; |
| 1913 | |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 1914 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1915 | } |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 1916 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1917 | void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) { |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 1918 | unsigned year, month, day; |
| 1919 | if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) { |
| 1920 | syntax_error("OS patch level should be YYYY-MM-DD: %s", arg); |
| 1921 | } |
| 1922 | if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year); |
| 1923 | if (month < 1 || month > 12) syntax_error("month out of range: %d", month); |
| 1924 | hdr->SetOsPatchLevel(year, month); |
| 1925 | } |
| 1926 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1927 | void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) { |
Elliott Hughes | 6ebec93 | 2018-04-10 14:22:13 -0700 | [diff] [blame] | 1928 | unsigned major = 0, minor = 0, patch = 0; |
| 1929 | std::vector<std::string> versions = android::base::Split(arg, "."); |
| 1930 | if (versions.size() < 1 || versions.size() > 3 || |
| 1931 | (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) || |
| 1932 | (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) || |
| 1933 | (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) || |
| 1934 | (major > 0x7f || minor > 0x7f || patch > 0x7f)) { |
| 1935 | syntax_error("bad OS version: %s", arg); |
| 1936 | } |
| 1937 | hdr->SetOsVersion(major, minor, patch); |
| 1938 | } |