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