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