| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018 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 | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 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 | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 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 | */ | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 28 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 29 | #include "fastboot_driver.h" | 
|  | 30 |  | 
|  | 31 | #include <errno.h> | 
|  | 32 | #include <fcntl.h> | 
| Yifan Hong | bcd2770 | 2021-02-16 19:37:32 -0800 | [diff] [blame] | 33 | #include <inttypes.h> | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 34 | #include <stdio.h> | 
|  | 35 | #include <stdlib.h> | 
|  | 36 | #include <string.h> | 
|  | 37 | #include <algorithm> | 
|  | 38 | #include <chrono> | 
|  | 39 | #include <fstream> | 
|  | 40 | #include <memory> | 
|  | 41 | #include <regex> | 
|  | 42 | #include <vector> | 
|  | 43 |  | 
|  | 44 | #include <android-base/file.h> | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 45 | #include <android-base/mapped_file.h> | 
| Yifan Hong | bcd2770 | 2021-02-16 19:37:32 -0800 | [diff] [blame] | 46 | #include <android-base/parseint.h> | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> | 
|  | 48 | #include <android-base/strings.h> | 
|  | 49 | #include <android-base/unique_fd.h> | 
| Yifan Hong | 17d469b | 2021-02-18 15:15:46 -0800 | [diff] [blame] | 50 | #include <storage_literals/storage_literals.h> | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | #include "constants.h" | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 53 | #include "transport.h" | 
|  | 54 |  | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 55 | using android::base::StringPrintf; | 
| Yifan Hong | 17d469b | 2021-02-18 15:15:46 -0800 | [diff] [blame] | 56 | using namespace android::storage_literals; | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 57 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 58 | namespace fastboot { | 
|  | 59 |  | 
|  | 60 | /*************************** PUBLIC *******************************/ | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 61 | FastBootDriver::FastBootDriver(Transport* transport, DriverCallbacks driver_callbacks, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 62 | bool no_checks) | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 63 | : transport_(transport), | 
|  | 64 | prolog_(std::move(driver_callbacks.prolog)), | 
|  | 65 | epilog_(std::move(driver_callbacks.epilog)), | 
|  | 66 | info_(std::move(driver_callbacks.info)), | 
| Raphael Herouart | 99097cc | 2022-12-30 11:51:54 +0000 | [diff] [blame] | 67 | text_(std::move(driver_callbacks.text)), | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 68 | disable_checks_(no_checks) {} | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 69 |  | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 70 | FastBootDriver::~FastBootDriver() { | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 73 | RetCode FastBootDriver::Boot(std::string* response, std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 74 | return RawCommand(FB_CMD_BOOT, "Booting", response, info); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
|  | 77 | RetCode FastBootDriver::Continue(std::string* response, std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 78 | return RawCommand(FB_CMD_CONTINUE, "Resuming boot", response, info); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 81 | RetCode FastBootDriver::CreatePartition(const std::string& partition, const std::string& size) { | 
|  | 82 | return RawCommand(FB_CMD_CREATE_PARTITION ":" + partition + ":" + size, | 
|  | 83 | "Creating '" + partition + "'"); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 86 | RetCode FastBootDriver::DeletePartition(const std::string& partition) { | 
|  | 87 | return RawCommand(FB_CMD_DELETE_PARTITION ":" + partition, "Deleting '" + partition + "'"); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | RetCode FastBootDriver::Erase(const std::string& partition, std::string* response, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 91 | std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 92 | return RawCommand(FB_CMD_ERASE ":" + partition, "Erasing '" + partition + "'", response, info); | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | RetCode FastBootDriver::Flash(const std::string& partition, std::string* response, | 
|  | 96 | std::vector<std::string>* info) { | 
|  | 97 | return RawCommand(FB_CMD_FLASH ":" + partition, "Writing '" + partition + "'", response, info); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
|  | 100 | RetCode FastBootDriver::GetVar(const std::string& key, std::string* val, | 
|  | 101 | std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 102 | return RawCommand(FB_CMD_GETVAR ":" + key, val, info); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | RetCode FastBootDriver::GetVarAll(std::vector<std::string>* response) { | 
|  | 106 | std::string tmp; | 
|  | 107 | return GetVar("all", &tmp, response); | 
|  | 108 | } | 
|  | 109 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 110 | RetCode FastBootDriver::Reboot(std::string* response, std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 111 | return RawCommand(FB_CMD_REBOOT, "Rebooting", response, info); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 114 | RetCode FastBootDriver::RebootTo(std::string target, std::string* response, | 
|  | 115 | std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 116 | return RawCommand("reboot-" + target, "Rebooting into " + target, response, info); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | RetCode FastBootDriver::ResizePartition(const std::string& partition, const std::string& size) { | 
|  | 120 | return RawCommand(FB_CMD_RESIZE_PARTITION ":" + partition + ":" + size, | 
|  | 121 | "Resizing '" + partition + "'"); | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 124 | RetCode FastBootDriver::SetActive(const std::string& slot, std::string* response, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 125 | std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 126 | return RawCommand(FB_CMD_SET_ACTIVE ":" + slot, "Setting current slot to '" + slot + "'", | 
|  | 127 | response, info); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 130 | RetCode FastBootDriver::SnapshotUpdateCommand(const std::string& command, std::string* response, | 
|  | 131 | std::vector<std::string>* info) { | 
| David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 132 | prolog_(StringPrintf("Snapshot %s", command.c_str())); | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 133 | std::string raw = FB_CMD_SNAPSHOT_UPDATE ":" + command; | 
| David Anderson | 220ddb1 | 2019-10-31 18:02:41 -0700 | [diff] [blame] | 134 | auto result = RawCommand(raw, response, info); | 
|  | 135 | epilog_(result); | 
|  | 136 | return result; | 
| David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 137 | } | 
|  | 138 |  | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 139 | RetCode FastBootDriver::FlashPartition(const std::string& partition, | 
|  | 140 | const std::vector<char>& data) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 141 | RetCode ret; | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 142 | if ((ret = Download(partition, data))) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 143 | return ret; | 
|  | 144 | } | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 145 | return Flash(partition); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| Yifan Hong | 58532df | 2021-03-22 16:39:13 -0700 | [diff] [blame] | 148 | RetCode FastBootDriver::FlashPartition(const std::string& partition, android::base::borrowed_fd fd, | 
|  | 149 | uint32_t size) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 150 | RetCode ret; | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 151 | if ((ret = Download(partition, fd, size))) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 152 | return ret; | 
|  | 153 | } | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 154 | return Flash(partition); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 157 | RetCode FastBootDriver::FlashPartition(const std::string& partition, sparse_file* s, uint32_t size, | 
|  | 158 | size_t current, size_t total) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 159 | RetCode ret; | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 160 | if ((ret = Download(partition, s, size, current, total, false))) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 161 | return ret; | 
|  | 162 | } | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 163 | return Flash(partition); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 166 | RetCode FastBootDriver::Partitions(std::vector<std::tuple<std::string, uint64_t>>* partitions) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 167 | std::vector<std::string> all; | 
|  | 168 | RetCode ret; | 
|  | 169 | if ((ret = GetVarAll(&all))) { | 
|  | 170 | return ret; | 
|  | 171 | } | 
|  | 172 |  | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 173 | std::regex reg("partition-size[[:s:]]*:[[:s:]]*([[:w:]]+)[[:s:]]*:[[:s:]]*0x([[:xdigit:]]+)"); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 174 | std::smatch sm; | 
|  | 175 |  | 
|  | 176 | for (auto& s : all) { | 
|  | 177 | if (std::regex_match(s, sm, reg)) { | 
|  | 178 | std::string m1(sm[1]); | 
|  | 179 | std::string m2(sm[2]); | 
| David Anderson | 6c30f6e | 2018-09-04 15:57:39 -0700 | [diff] [blame] | 180 | uint64_t tmp = strtoll(m2.c_str(), 0, 16); | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 181 | partitions->push_back(std::make_tuple(m1, tmp)); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 182 | } | 
|  | 183 | } | 
|  | 184 | return SUCCESS; | 
|  | 185 | } | 
|  | 186 |  | 
| Yifan Hong | 58532df | 2021-03-22 16:39:13 -0700 | [diff] [blame] | 187 | RetCode FastBootDriver::Download(const std::string& name, android::base::borrowed_fd fd, | 
|  | 188 | size_t size, std::string* response, | 
|  | 189 | std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 190 | prolog_(StringPrintf("Sending '%s' (%zu KB)", name.c_str(), size / 1024)); | 
|  | 191 | auto result = Download(fd, size, response, info); | 
|  | 192 | epilog_(result); | 
|  | 193 | return result; | 
|  | 194 | } | 
|  | 195 |  | 
| Yifan Hong | 58532df | 2021-03-22 16:39:13 -0700 | [diff] [blame] | 196 | RetCode FastBootDriver::Download(android::base::borrowed_fd fd, size_t size, std::string* response, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 197 | std::vector<std::string>* info) { | 
|  | 198 | RetCode ret; | 
|  | 199 |  | 
|  | 200 | if ((size <= 0 || size > MAX_DOWNLOAD_SIZE) && !disable_checks_) { | 
|  | 201 | error_ = "File is too large to download"; | 
|  | 202 | return BAD_ARG; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | uint32_t u32size = static_cast<uint32_t>(size); | 
|  | 206 | if ((ret = DownloadCommand(u32size, response, info))) { | 
|  | 207 | return ret; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | // Write the buffer | 
|  | 211 | if ((ret = SendBuffer(fd, size))) { | 
|  | 212 | return ret; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | // Wait for response | 
|  | 216 | return HandleResponse(response, info); | 
|  | 217 | } | 
|  | 218 |  | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 219 | RetCode FastBootDriver::Download(const std::string& name, const std::vector<char>& buf, | 
|  | 220 | std::string* response, std::vector<std::string>* info) { | 
|  | 221 | prolog_(StringPrintf("Sending '%s' (%zu KB)", name.c_str(), buf.size() / 1024)); | 
|  | 222 | auto result = Download(buf, response, info); | 
|  | 223 | epilog_(result); | 
|  | 224 | return result; | 
|  | 225 | } | 
|  | 226 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 227 | RetCode FastBootDriver::Download(const std::vector<char>& buf, std::string* response, | 
|  | 228 | std::vector<std::string>* info) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 229 | RetCode ret; | 
|  | 230 | error_ = ""; | 
| Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 231 | if ((buf.size() == 0 || buf.size() > MAX_DOWNLOAD_SIZE) && !disable_checks_) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 232 | error_ = "Buffer is too large or 0 bytes"; | 
|  | 233 | return BAD_ARG; | 
|  | 234 | } | 
|  | 235 |  | 
| Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 236 | if ((ret = DownloadCommand(buf.size(), response, info))) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 237 | return ret; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | // Write the buffer | 
| Tom Cherry | dfd85df | 2018-09-20 14:45:05 -0700 | [diff] [blame] | 241 | if ((ret = SendBuffer(buf))) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 242 | return ret; | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | // Wait for response | 
|  | 246 | return HandleResponse(response, info); | 
|  | 247 | } | 
|  | 248 |  | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 249 | RetCode FastBootDriver::Download(const std::string& partition, struct sparse_file* s, uint32_t size, | 
|  | 250 | size_t current, size_t total, bool use_crc, std::string* response, | 
|  | 251 | std::vector<std::string>* info) { | 
|  | 252 | prolog_(StringPrintf("Sending sparse '%s' %zu/%zu (%u KB)", partition.c_str(), current, total, | 
|  | 253 | size / 1024)); | 
|  | 254 | auto result = Download(s, use_crc, response, info); | 
|  | 255 | epilog_(result); | 
|  | 256 | return result; | 
|  | 257 | } | 
|  | 258 |  | 
| Aaron Wisner | 9812b58 | 2018-08-22 11:01:04 -0500 | [diff] [blame] | 259 | RetCode FastBootDriver::Download(sparse_file* s, bool use_crc, std::string* response, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 260 | std::vector<std::string>* info) { | 
|  | 261 | error_ = ""; | 
| Aaron Wisner | 9812b58 | 2018-08-22 11:01:04 -0500 | [diff] [blame] | 262 | int64_t size = sparse_file_len(s, true, use_crc); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 263 | if (size <= 0 || size > MAX_DOWNLOAD_SIZE) { | 
|  | 264 | error_ = "Sparse file is too large or invalid"; | 
|  | 265 | return BAD_ARG; | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | RetCode ret; | 
|  | 269 | uint32_t u32size = static_cast<uint32_t>(size); | 
|  | 270 | if ((ret = DownloadCommand(u32size, response, info))) { | 
|  | 271 | return ret; | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | struct SparseCBPrivate { | 
|  | 275 | FastBootDriver* self; | 
|  | 276 | std::vector<char> tpbuf; | 
|  | 277 | } cb_priv; | 
|  | 278 | cb_priv.self = this; | 
|  | 279 |  | 
|  | 280 | auto cb = [](void* priv, const void* buf, size_t len) -> int { | 
|  | 281 | SparseCBPrivate* data = static_cast<SparseCBPrivate*>(priv); | 
|  | 282 | const char* cbuf = static_cast<const char*>(buf); | 
|  | 283 | return data->self->SparseWriteCallback(data->tpbuf, cbuf, len); | 
|  | 284 | }; | 
|  | 285 |  | 
| Aaron Wisner | 9812b58 | 2018-08-22 11:01:04 -0500 | [diff] [blame] | 286 | if (sparse_file_callback(s, true, use_crc, cb, &cb_priv) < 0) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 287 | error_ = "Error reading sparse file"; | 
|  | 288 | return IO_ERROR; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | // Now flush | 
|  | 292 | if (cb_priv.tpbuf.size() && (ret = SendBuffer(cb_priv.tpbuf))) { | 
|  | 293 | return ret; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | return HandleResponse(response, info); | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | RetCode FastBootDriver::Upload(const std::string& outfile, std::string* response, | 
|  | 300 | std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 301 | prolog_("Uploading '" + outfile + "'"); | 
|  | 302 | auto result = UploadInner(outfile, response, info); | 
|  | 303 | epilog_(result); | 
|  | 304 | return result; | 
|  | 305 | } | 
|  | 306 |  | 
| Yifan Hong | bbf374d | 2021-02-17 11:16:39 -0800 | [diff] [blame] | 307 | // This function executes cmd, then expect a "DATA" response with a number N, followed | 
|  | 308 | // by N bytes, and another response. | 
|  | 309 | // This is the common way for the device to send data to the driver used by upload and fetch. | 
|  | 310 | RetCode FastBootDriver::RunAndReadBuffer( | 
|  | 311 | const std::string& cmd, std::string* response, std::vector<std::string>* info, | 
|  | 312 | const std::function<RetCode(const char* data, uint64_t size)>& write_fn) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 313 | RetCode ret; | 
| qiwu chen | 325ba6f | 2019-08-29 14:50:51 +0800 | [diff] [blame] | 314 | int dsize = 0; | 
| Yifan Hong | bbf374d | 2021-02-17 11:16:39 -0800 | [diff] [blame] | 315 | if ((ret = RawCommand(cmd, response, info, &dsize))) { | 
|  | 316 | error_ = android::base::StringPrintf("%s request failed: %s", cmd.c_str(), error_.c_str()); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 317 | return ret; | 
|  | 318 | } | 
|  | 319 |  | 
| Yifan Hong | bbf374d | 2021-02-17 11:16:39 -0800 | [diff] [blame] | 320 | if (dsize <= 0) { | 
|  | 321 | error_ = android::base::StringPrintf("%s request failed, device reports %d bytes available", | 
|  | 322 | cmd.c_str(), dsize); | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 323 | return BAD_DEV_RESP; | 
|  | 324 | } | 
|  | 325 |  | 
| Yifan Hong | 17d469b | 2021-02-18 15:15:46 -0800 | [diff] [blame] | 326 | const uint64_t total_size = dsize; | 
|  | 327 | const uint64_t buf_size = std::min<uint64_t>(total_size, 1_MiB); | 
|  | 328 | std::vector<char> data(buf_size); | 
|  | 329 | uint64_t current_offset = 0; | 
|  | 330 | while (current_offset < total_size) { | 
|  | 331 | uint64_t remaining = total_size - current_offset; | 
|  | 332 | uint64_t chunk_size = std::min(buf_size, remaining); | 
|  | 333 | if ((ret = ReadBuffer(data.data(), chunk_size)) != SUCCESS) { | 
|  | 334 | return ret; | 
|  | 335 | } | 
|  | 336 | if ((ret = write_fn(data.data(), chunk_size)) != SUCCESS) { | 
|  | 337 | return ret; | 
|  | 338 | } | 
|  | 339 | current_offset += chunk_size; | 
| Yifan Hong | bbf374d | 2021-02-17 11:16:39 -0800 | [diff] [blame] | 340 | } | 
|  | 341 | return HandleResponse(response, info); | 
|  | 342 | } | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 343 |  | 
| Yifan Hong | bbf374d | 2021-02-17 11:16:39 -0800 | [diff] [blame] | 344 | RetCode FastBootDriver::UploadInner(const std::string& outfile, std::string* response, | 
|  | 345 | std::vector<std::string>* info) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 346 | std::ofstream ofs; | 
|  | 347 | ofs.open(outfile, std::ofstream::out | std::ofstream::binary); | 
|  | 348 | if (ofs.fail()) { | 
|  | 349 | error_ = android::base::StringPrintf("Failed to open '%s'", outfile.c_str()); | 
|  | 350 | return IO_ERROR; | 
|  | 351 | } | 
| Yifan Hong | bbf374d | 2021-02-17 11:16:39 -0800 | [diff] [blame] | 352 | auto write_fn = [&](const char* data, uint64_t size) { | 
|  | 353 | ofs.write(data, size); | 
|  | 354 | if (ofs.fail() || ofs.bad()) { | 
|  | 355 | error_ = android::base::StringPrintf("Writing to '%s' failed", outfile.c_str()); | 
|  | 356 | return IO_ERROR; | 
|  | 357 | } | 
|  | 358 | return SUCCESS; | 
|  | 359 | }; | 
|  | 360 | RetCode ret = RunAndReadBuffer(FB_CMD_UPLOAD, response, info, write_fn); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 361 | ofs.close(); | 
| Yifan Hong | bbf374d | 2021-02-17 11:16:39 -0800 | [diff] [blame] | 362 | return ret; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 363 | } | 
|  | 364 |  | 
| Yifan Hong | bcd2770 | 2021-02-16 19:37:32 -0800 | [diff] [blame] | 365 | RetCode FastBootDriver::FetchToFd(const std::string& partition, android::base::borrowed_fd fd, | 
|  | 366 | int64_t offset, int64_t size, std::string* response, | 
|  | 367 | std::vector<std::string>* info) { | 
|  | 368 | prolog_(android::base::StringPrintf("Fetching %s (offset=%" PRIx64 ", size=%" PRIx64 ")", | 
|  | 369 | partition.c_str(), offset, size)); | 
|  | 370 | std::string cmd = FB_CMD_FETCH ":" + partition; | 
|  | 371 | if (offset >= 0) { | 
|  | 372 | cmd += android::base::StringPrintf(":0x%08" PRIx64, offset); | 
|  | 373 | if (size >= 0) { | 
|  | 374 | cmd += android::base::StringPrintf(":0x%08" PRIx64, size); | 
|  | 375 | } | 
|  | 376 | } | 
|  | 377 | RetCode ret = RunAndReadBuffer(cmd, response, info, [&](const char* data, uint64_t size) { | 
|  | 378 | if (!android::base::WriteFully(fd, data, size)) { | 
|  | 379 | error_ = android::base::StringPrintf("Cannot write: %s", strerror(errno)); | 
|  | 380 | return IO_ERROR; | 
|  | 381 | } | 
|  | 382 | return SUCCESS; | 
|  | 383 | }); | 
|  | 384 | epilog_(ret); | 
|  | 385 | return ret; | 
|  | 386 | } | 
|  | 387 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 388 | // Helpers | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 389 | void FastBootDriver::SetInfoCallback(std::function<void(const std::string&)> info) { | 
|  | 390 | info_ = info; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 391 | } | 
|  | 392 |  | 
|  | 393 | const std::string FastBootDriver::RCString(RetCode rc) { | 
|  | 394 | switch (rc) { | 
|  | 395 | case SUCCESS: | 
|  | 396 | return std::string("Success"); | 
|  | 397 |  | 
|  | 398 | case BAD_ARG: | 
|  | 399 | return std::string("Invalid Argument"); | 
|  | 400 |  | 
|  | 401 | case IO_ERROR: | 
|  | 402 | return std::string("I/O Error"); | 
|  | 403 |  | 
|  | 404 | case BAD_DEV_RESP: | 
|  | 405 | return std::string("Invalid Device Response"); | 
|  | 406 |  | 
|  | 407 | case DEVICE_FAIL: | 
|  | 408 | return std::string("Device Error"); | 
|  | 409 |  | 
|  | 410 | case TIMEOUT: | 
|  | 411 | return std::string("Timeout"); | 
|  | 412 |  | 
|  | 413 | default: | 
|  | 414 | return std::string("Unknown Error"); | 
|  | 415 | } | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | std::string FastBootDriver::Error() { | 
|  | 419 | return error_; | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | RetCode FastBootDriver::WaitForDisconnect() { | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 423 | return transport_->WaitForDisconnect() ? IO_ERROR : SUCCESS; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 424 | } | 
|  | 425 |  | 
|  | 426 | /****************************** PROTECTED *************************************/ | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 427 | RetCode FastBootDriver::RawCommand(const std::string& cmd, const std::string& message, | 
|  | 428 | std::string* response, std::vector<std::string>* info, | 
|  | 429 | int* dsize) { | 
|  | 430 | prolog_(message); | 
|  | 431 | auto result = RawCommand(cmd, response, info, dsize); | 
|  | 432 | epilog_(result); | 
|  | 433 | return result; | 
|  | 434 | } | 
|  | 435 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 436 | RetCode FastBootDriver::RawCommand(const std::string& cmd, std::string* response, | 
|  | 437 | std::vector<std::string>* info, int* dsize) { | 
|  | 438 | error_ = "";  // Clear any pending error | 
|  | 439 | if (cmd.size() > FB_COMMAND_SZ && !disable_checks_) { | 
|  | 440 | error_ = "Command length to RawCommand() is too long"; | 
|  | 441 | return BAD_ARG; | 
|  | 442 | } | 
|  | 443 |  | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 444 | if (transport_->Write(cmd.c_str(), cmd.size()) != static_cast<int>(cmd.size())) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 445 | error_ = ErrnoStr("Write to device failed"); | 
|  | 446 | return IO_ERROR; | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | // Read the response | 
|  | 450 | return HandleResponse(response, info, dsize); | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | RetCode FastBootDriver::DownloadCommand(uint32_t size, std::string* response, | 
|  | 454 | std::vector<std::string>* info) { | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 455 | std::string cmd(android::base::StringPrintf("%s:%08" PRIx32, FB_CMD_DOWNLOAD, size)); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 456 | RetCode ret; | 
|  | 457 | if ((ret = RawCommand(cmd, response, info))) { | 
|  | 458 | return ret; | 
|  | 459 | } | 
|  | 460 | return SUCCESS; | 
|  | 461 | } | 
|  | 462 |  | 
|  | 463 | RetCode FastBootDriver::HandleResponse(std::string* response, std::vector<std::string>* info, | 
|  | 464 | int* dsize) { | 
|  | 465 | char status[FB_RESPONSE_SZ + 1]; | 
| Dima Zavin | a5b85a4 | 2019-02-28 14:50:25 -0800 | [diff] [blame] | 466 | auto start = std::chrono::steady_clock::now(); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 467 |  | 
|  | 468 | auto set_response = [response](std::string s) { | 
|  | 469 | if (response) *response = std::move(s); | 
|  | 470 | }; | 
|  | 471 | auto add_info = [info](std::string s) { | 
|  | 472 | if (info) info->push_back(std::move(s)); | 
|  | 473 | }; | 
|  | 474 |  | 
|  | 475 | // erase response | 
|  | 476 | set_response(""); | 
| Dima Zavin | a5b85a4 | 2019-02-28 14:50:25 -0800 | [diff] [blame] | 477 | while ((std::chrono::steady_clock::now() - start) < std::chrono::seconds(RESP_TIMEOUT)) { | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 478 | int r = transport_->Read(status, FB_RESPONSE_SZ); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 479 | if (r < 0) { | 
|  | 480 | error_ = ErrnoStr("Status read failed"); | 
|  | 481 | return IO_ERROR; | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 | status[r] = '\0';  // Need the null terminator | 
|  | 485 | std::string input(status); | 
|  | 486 | if (android::base::StartsWith(input, "INFO")) { | 
|  | 487 | std::string tmp = input.substr(strlen("INFO")); | 
| Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 488 | info_(tmp); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 489 | add_info(std::move(tmp)); | 
| Dima Zavin | 6d46a49 | 2019-02-23 21:14:38 -0800 | [diff] [blame] | 490 | // We may receive one or more INFO packets during long operations, | 
|  | 491 | // e.g. flash/erase if they are back by slow media like NAND/NOR | 
|  | 492 | // flash. In that case, reset the timer since it's not a real | 
|  | 493 | // timeout. | 
|  | 494 | start = std::chrono::steady_clock::now(); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 495 | } else if (android::base::StartsWith(input, "OKAY")) { | 
|  | 496 | set_response(input.substr(strlen("OKAY"))); | 
|  | 497 | return SUCCESS; | 
|  | 498 | } else if (android::base::StartsWith(input, "FAIL")) { | 
|  | 499 | error_ = android::base::StringPrintf("remote: '%s'", status + strlen("FAIL")); | 
|  | 500 | set_response(input.substr(strlen("FAIL"))); | 
|  | 501 | return DEVICE_FAIL; | 
| Raphael Herouart | 99097cc | 2022-12-30 11:51:54 +0000 | [diff] [blame] | 502 | } else if (android::base::StartsWith(input, "TEXT")) { | 
|  | 503 | text_(input.substr(strlen("TEXT"))); | 
|  | 504 | // Reset timeout as many more TEXT may come | 
|  | 505 | start = std::chrono::steady_clock::now(); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 506 | } else if (android::base::StartsWith(input, "DATA")) { | 
|  | 507 | std::string tmp = input.substr(strlen("DATA")); | 
|  | 508 | uint32_t num = strtol(tmp.c_str(), 0, 16); | 
|  | 509 | if (num > MAX_DOWNLOAD_SIZE) { | 
|  | 510 | error_ = android::base::StringPrintf("Data size too large (%d)", num); | 
|  | 511 | return BAD_DEV_RESP; | 
|  | 512 | } | 
|  | 513 | if (dsize) *dsize = num; | 
|  | 514 | set_response(std::move(tmp)); | 
|  | 515 | return SUCCESS; | 
|  | 516 | } else { | 
|  | 517 | error_ = android::base::StringPrintf("Device sent unknown status code: %s", status); | 
|  | 518 | return BAD_DEV_RESP; | 
|  | 519 | } | 
|  | 520 |  | 
|  | 521 | }  // End of while loop | 
|  | 522 |  | 
|  | 523 | return TIMEOUT; | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | std::string FastBootDriver::ErrnoStr(const std::string& msg) { | 
|  | 527 | return android::base::StringPrintf("%s (%s)", msg.c_str(), strerror(errno)); | 
|  | 528 | } | 
|  | 529 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 530 | /******************************* PRIVATE **************************************/ | 
| Yifan Hong | 58532df | 2021-03-22 16:39:13 -0700 | [diff] [blame] | 531 | RetCode FastBootDriver::SendBuffer(android::base::borrowed_fd fd, size_t size) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 532 | static constexpr uint32_t MAX_MAP_SIZE = 512 * 1024 * 1024; | 
|  | 533 | off64_t offset = 0; | 
|  | 534 | uint32_t remaining = size; | 
|  | 535 | RetCode ret; | 
|  | 536 |  | 
|  | 537 | while (remaining) { | 
|  | 538 | // Memory map the file | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 539 | size_t len = std::min(remaining, MAX_MAP_SIZE); | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 540 | auto mapping{android::base::MappedFile::FromFd(fd, offset, len, PROT_READ)}; | 
|  | 541 | if (!mapping) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 542 | error_ = "Creating filemap failed"; | 
|  | 543 | return IO_ERROR; | 
|  | 544 | } | 
|  | 545 |  | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 546 | if ((ret = SendBuffer(mapping->data(), mapping->size()))) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 547 | return ret; | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | remaining -= len; | 
|  | 551 | offset += len; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | return SUCCESS; | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 | RetCode FastBootDriver::SendBuffer(const std::vector<char>& buf) { | 
|  | 558 | // Write the buffer | 
|  | 559 | return SendBuffer(buf.data(), buf.size()); | 
|  | 560 | } | 
|  | 561 |  | 
|  | 562 | RetCode FastBootDriver::SendBuffer(const void* buf, size_t size) { | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 563 | // ioctl on 0-length buffer causes freezing | 
| David Anderson | 0c7bde8 | 2018-07-30 12:54:53 -0700 | [diff] [blame] | 564 | if (!size) { | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 565 | return BAD_ARG; | 
| David Anderson | 0c7bde8 | 2018-07-30 12:54:53 -0700 | [diff] [blame] | 566 | } | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 567 | // Write the buffer | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 568 | ssize_t tmp = transport_->Write(buf, size); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 569 |  | 
|  | 570 | if (tmp < 0) { | 
|  | 571 | error_ = ErrnoStr("Write to device failed in SendBuffer()"); | 
|  | 572 | return IO_ERROR; | 
|  | 573 | } else if (static_cast<size_t>(tmp) != size) { | 
|  | 574 | error_ = android::base::StringPrintf("Failed to write all %zu bytes", size); | 
|  | 575 |  | 
|  | 576 | return IO_ERROR; | 
|  | 577 | } | 
|  | 578 |  | 
|  | 579 | return SUCCESS; | 
|  | 580 | } | 
|  | 581 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 582 | RetCode FastBootDriver::ReadBuffer(void* buf, size_t size) { | 
|  | 583 | // Read the buffer | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 584 | ssize_t tmp = transport_->Read(buf, size); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 585 |  | 
|  | 586 | if (tmp < 0) { | 
|  | 587 | error_ = ErrnoStr("Read from device failed in ReadBuffer()"); | 
|  | 588 | return IO_ERROR; | 
|  | 589 | } else if (static_cast<size_t>(tmp) != size) { | 
|  | 590 | error_ = android::base::StringPrintf("Failed to read all %zu bytes", size); | 
|  | 591 | return IO_ERROR; | 
|  | 592 | } | 
|  | 593 |  | 
|  | 594 | return SUCCESS; | 
|  | 595 | } | 
|  | 596 |  | 
|  | 597 | int FastBootDriver::SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len) { | 
|  | 598 | size_t total = 0; | 
|  | 599 | size_t to_write = std::min(TRANSPORT_CHUNK_SIZE - tpbuf.size(), len); | 
|  | 600 |  | 
|  | 601 | // Handle the residual | 
|  | 602 | tpbuf.insert(tpbuf.end(), data, data + to_write); | 
|  | 603 | if (tpbuf.size() < TRANSPORT_CHUNK_SIZE) {  // Nothing enough to send rn | 
|  | 604 | return 0; | 
|  | 605 | } | 
|  | 606 |  | 
|  | 607 | if (SendBuffer(tpbuf)) { | 
|  | 608 | error_ = ErrnoStr("Send failed in SparseWriteCallback()"); | 
|  | 609 | return -1; | 
|  | 610 | } | 
|  | 611 | tpbuf.clear(); | 
|  | 612 | total += to_write; | 
|  | 613 |  | 
|  | 614 | // Now we need to send a multiple of chunk size | 
|  | 615 | size_t nchunks = (len - total) / TRANSPORT_CHUNK_SIZE; | 
|  | 616 | size_t nbytes = TRANSPORT_CHUNK_SIZE * nchunks; | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 617 | if (nbytes && SendBuffer(data + total, nbytes)) {  // Don't send a ZLP | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 618 | error_ = ErrnoStr("Send failed in SparseWriteCallback()"); | 
|  | 619 | return -1; | 
|  | 620 | } | 
|  | 621 | total += nbytes; | 
|  | 622 |  | 
|  | 623 | if (len - total > 0) {  // We have residual data to save for next time | 
|  | 624 | tpbuf.assign(data + total, data + len); | 
|  | 625 | } | 
|  | 626 |  | 
|  | 627 | return 0; | 
|  | 628 | } | 
|  | 629 |  | 
| David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 630 | Transport* FastBootDriver::set_transport(Transport* transport) { | 
|  | 631 | std::swap(transport_, transport); | 
|  | 632 | return transport; | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 633 | } | 
|  | 634 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 635 | }  // End namespace fastboot |