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