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