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