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