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