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