| 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 | */ | 
|  | 28 | #include "fastboot_driver.h" | 
|  | 29 |  | 
|  | 30 | #include <errno.h> | 
|  | 31 | #include <fcntl.h> | 
|  | 32 | #include <stdio.h> | 
|  | 33 | #include <stdlib.h> | 
|  | 34 | #include <string.h> | 
|  | 35 | #include <algorithm> | 
|  | 36 | #include <chrono> | 
|  | 37 | #include <fstream> | 
|  | 38 | #include <memory> | 
|  | 39 | #include <regex> | 
|  | 40 | #include <vector> | 
|  | 41 |  | 
|  | 42 | #include <android-base/file.h> | 
|  | 43 | #include <android-base/stringprintf.h> | 
|  | 44 | #include <android-base/strings.h> | 
|  | 45 | #include <android-base/unique_fd.h> | 
|  | 46 | #include <utils/FileMap.h> | 
|  | 47 | #include "fastboot_driver.h" | 
|  | 48 | #include "transport.h" | 
|  | 49 |  | 
|  | 50 | namespace fastboot { | 
|  | 51 |  | 
|  | 52 | /*************************** PUBLIC *******************************/ | 
|  | 53 | FastBootDriver::FastBootDriver(Transport* transport, std::function<void(std::string&)> info, | 
|  | 54 | bool no_checks) | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 55 | : transport_(transport) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 56 | info_cb_ = info; | 
|  | 57 | disable_checks_ = no_checks; | 
|  | 58 | } | 
|  | 59 |  | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 60 | FastBootDriver::~FastBootDriver() { | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 63 | RetCode FastBootDriver::Boot(std::string* response, std::vector<std::string>* info) { | 
|  | 64 | return RawCommand(Commands::BOOT, response, info); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | RetCode FastBootDriver::Continue(std::string* response, std::vector<std::string>* info) { | 
|  | 68 | return RawCommand(Commands::CONTINUE, response, info); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | RetCode FastBootDriver::Erase(const std::string& part, std::string* response, | 
|  | 72 | std::vector<std::string>* info) { | 
|  | 73 | return RawCommand(Commands::ERASE + part, response, info); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | RetCode FastBootDriver::Flash(const std::string& part, std::string* response, | 
|  | 77 | std::vector<std::string>* info) { | 
|  | 78 | return RawCommand(Commands::FLASH + part, response, info); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | RetCode FastBootDriver::GetVar(const std::string& key, std::string* val, | 
|  | 82 | std::vector<std::string>* info) { | 
|  | 83 | return RawCommand(Commands::GET_VAR + key, val, info); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | RetCode FastBootDriver::GetVarAll(std::vector<std::string>* response) { | 
|  | 87 | std::string tmp; | 
|  | 88 | return GetVar("all", &tmp, response); | 
|  | 89 | } | 
|  | 90 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 91 | RetCode FastBootDriver::Reboot(std::string* response, std::vector<std::string>* info) { | 
|  | 92 | return RawCommand(Commands::REBOOT, response, info); | 
|  | 93 | } | 
|  | 94 |  | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 95 | RetCode FastBootDriver::RebootTo(std::string target, std::string* response, | 
|  | 96 | std::vector<std::string>* info) { | 
|  | 97 | return RawCommand("reboot-" + target, response, info); | 
|  | 98 | } | 
|  | 99 |  | 
| Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 100 | RetCode FastBootDriver::SetActive(const std::string& slot, std::string* response, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 101 | std::vector<std::string>* info) { | 
| Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 102 | return RawCommand(Commands::SET_ACTIVE + slot, response, info); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 105 | RetCode FastBootDriver::FlashPartition(const std::string& part, const std::vector<char>& data) { | 
|  | 106 | RetCode ret; | 
|  | 107 | if ((ret = Download(data))) { | 
|  | 108 | return ret; | 
|  | 109 | } | 
|  | 110 | return RawCommand(Commands::FLASH + part); | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | RetCode FastBootDriver::FlashPartition(const std::string& part, int fd, uint32_t sz) { | 
|  | 114 | RetCode ret; | 
|  | 115 | if ((ret = Download(fd, sz))) { | 
|  | 116 | return ret; | 
|  | 117 | } | 
|  | 118 | return RawCommand(Commands::FLASH + part); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | RetCode FastBootDriver::FlashPartition(const std::string& part, sparse_file* s) { | 
|  | 122 | RetCode ret; | 
|  | 123 | if ((ret = Download(s))) { | 
|  | 124 | return ret; | 
|  | 125 | } | 
|  | 126 | return RawCommand(Commands::FLASH + part); | 
|  | 127 | } | 
|  | 128 |  | 
| David Anderson | 6c30f6e | 2018-09-04 15:57:39 -0700 | [diff] [blame] | 129 | RetCode FastBootDriver::Partitions(std::vector<std::tuple<std::string, uint64_t>>* parts) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 130 | std::vector<std::string> all; | 
|  | 131 | RetCode ret; | 
|  | 132 | if ((ret = GetVarAll(&all))) { | 
|  | 133 | return ret; | 
|  | 134 | } | 
|  | 135 |  | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 136 | std::regex reg("partition-size[[:s:]]*:[[:s:]]*([[:w:]]+)[[:s:]]*:[[:s:]]*0x([[:xdigit:]]+)"); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 137 | std::smatch sm; | 
|  | 138 |  | 
|  | 139 | for (auto& s : all) { | 
|  | 140 | if (std::regex_match(s, sm, reg)) { | 
|  | 141 | std::string m1(sm[1]); | 
|  | 142 | std::string m2(sm[2]); | 
| David Anderson | 6c30f6e | 2018-09-04 15:57:39 -0700 | [diff] [blame] | 143 | uint64_t tmp = strtoll(m2.c_str(), 0, 16); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 144 | parts->push_back(std::make_tuple(m1, tmp)); | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 | return SUCCESS; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | RetCode FastBootDriver::Require(const std::string& var, const std::vector<std::string>& allowed, | 
|  | 151 | bool* reqmet, bool invert) { | 
|  | 152 | *reqmet = invert; | 
|  | 153 | RetCode ret; | 
|  | 154 | std::string response; | 
|  | 155 | if ((ret = GetVar(var, &response))) { | 
|  | 156 | return ret; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | // Now check if we have a match | 
|  | 160 | for (const auto s : allowed) { | 
|  | 161 | // If it ends in *, and starting substring match | 
|  | 162 | if (response == s || (s.length() && s.back() == '*' && | 
|  | 163 | !response.compare(0, s.length() - 1, s, 0, s.length() - 1))) { | 
|  | 164 | *reqmet = !invert; | 
|  | 165 | break; | 
|  | 166 | } | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | return SUCCESS; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | RetCode FastBootDriver::Download(int fd, size_t size, std::string* response, | 
|  | 173 | std::vector<std::string>* info) { | 
|  | 174 | RetCode ret; | 
|  | 175 |  | 
|  | 176 | if ((size <= 0 || size > MAX_DOWNLOAD_SIZE) && !disable_checks_) { | 
|  | 177 | error_ = "File is too large to download"; | 
|  | 178 | return BAD_ARG; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | uint32_t u32size = static_cast<uint32_t>(size); | 
|  | 182 | if ((ret = DownloadCommand(u32size, response, info))) { | 
|  | 183 | return ret; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | // Write the buffer | 
|  | 187 | if ((ret = SendBuffer(fd, size))) { | 
|  | 188 | return ret; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | // Wait for response | 
|  | 192 | return HandleResponse(response, info); | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | RetCode FastBootDriver::Download(const std::vector<char>& buf, std::string* response, | 
|  | 196 | std::vector<std::string>* info) { | 
|  | 197 | return Download(buf.data(), buf.size(), response, info); | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | RetCode FastBootDriver::Download(const char* buf, uint32_t size, std::string* response, | 
|  | 201 | std::vector<std::string>* info) { | 
|  | 202 | RetCode ret; | 
|  | 203 | error_ = ""; | 
|  | 204 | if ((size == 0 || size > MAX_DOWNLOAD_SIZE) && !disable_checks_) { | 
|  | 205 | error_ = "Buffer is too large or 0 bytes"; | 
|  | 206 | return BAD_ARG; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | if ((ret = DownloadCommand(size, response, info))) { | 
|  | 210 | return ret; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | // Write the buffer | 
|  | 214 | if ((ret = SendBuffer(buf, size))) { | 
|  | 215 | return ret; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | // Wait for response | 
|  | 219 | return HandleResponse(response, info); | 
|  | 220 | } | 
|  | 221 |  | 
| Aaron Wisner | 9812b58 | 2018-08-22 11:01:04 -0500 | [diff] [blame] | 222 | RetCode FastBootDriver::Download(sparse_file* s, bool use_crc, std::string* response, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 223 | std::vector<std::string>* info) { | 
|  | 224 | error_ = ""; | 
| Aaron Wisner | 9812b58 | 2018-08-22 11:01:04 -0500 | [diff] [blame] | 225 | int64_t size = sparse_file_len(s, true, use_crc); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 226 | if (size <= 0 || size > MAX_DOWNLOAD_SIZE) { | 
|  | 227 | error_ = "Sparse file is too large or invalid"; | 
|  | 228 | return BAD_ARG; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | RetCode ret; | 
|  | 232 | uint32_t u32size = static_cast<uint32_t>(size); | 
|  | 233 | if ((ret = DownloadCommand(u32size, response, info))) { | 
|  | 234 | return ret; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | struct SparseCBPrivate { | 
|  | 238 | FastBootDriver* self; | 
|  | 239 | std::vector<char> tpbuf; | 
|  | 240 | } cb_priv; | 
|  | 241 | cb_priv.self = this; | 
|  | 242 |  | 
|  | 243 | auto cb = [](void* priv, const void* buf, size_t len) -> int { | 
|  | 244 | SparseCBPrivate* data = static_cast<SparseCBPrivate*>(priv); | 
|  | 245 | const char* cbuf = static_cast<const char*>(buf); | 
|  | 246 | return data->self->SparseWriteCallback(data->tpbuf, cbuf, len); | 
|  | 247 | }; | 
|  | 248 |  | 
| Aaron Wisner | 9812b58 | 2018-08-22 11:01:04 -0500 | [diff] [blame] | 249 | if (sparse_file_callback(s, true, use_crc, cb, &cb_priv) < 0) { | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 250 | error_ = "Error reading sparse file"; | 
|  | 251 | return IO_ERROR; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | // Now flush | 
|  | 255 | if (cb_priv.tpbuf.size() && (ret = SendBuffer(cb_priv.tpbuf))) { | 
|  | 256 | return ret; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | return HandleResponse(response, info); | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | RetCode FastBootDriver::Upload(const std::string& outfile, std::string* response, | 
|  | 263 | std::vector<std::string>* info) { | 
|  | 264 | RetCode ret; | 
|  | 265 | int dsize; | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 266 | if ((ret = RawCommand(Commands::UPLOAD, response, info, &dsize))) { | 
|  | 267 | error_ = "Upload request failed: " + error_; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 268 | return ret; | 
|  | 269 | } | 
|  | 270 |  | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 271 | if (!dsize) { | 
|  | 272 | error_ = "Upload request failed, device reports 0 bytes available"; | 
|  | 273 | return BAD_DEV_RESP; | 
|  | 274 | } | 
|  | 275 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 276 | std::vector<char> data; | 
|  | 277 | data.resize(dsize); | 
|  | 278 |  | 
|  | 279 | if ((ret = ReadBuffer(data))) { | 
|  | 280 | return ret; | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | std::ofstream ofs; | 
|  | 284 | ofs.open(outfile, std::ofstream::out | std::ofstream::binary); | 
|  | 285 | if (ofs.fail()) { | 
|  | 286 | error_ = android::base::StringPrintf("Failed to open '%s'", outfile.c_str()); | 
|  | 287 | return IO_ERROR; | 
|  | 288 | } | 
|  | 289 | ofs.write(data.data(), data.size()); | 
|  | 290 | if (ofs.fail() || ofs.bad()) { | 
|  | 291 | error_ = android::base::StringPrintf("Writing to '%s' failed", outfile.c_str()); | 
|  | 292 | return IO_ERROR; | 
|  | 293 | } | 
|  | 294 | ofs.close(); | 
|  | 295 |  | 
|  | 296 | return HandleResponse(response, info); | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | // Helpers | 
|  | 300 | void FastBootDriver::SetInfoCallback(std::function<void(std::string&)> info) { | 
|  | 301 | info_cb_ = info; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | const std::string FastBootDriver::RCString(RetCode rc) { | 
|  | 305 | switch (rc) { | 
|  | 306 | case SUCCESS: | 
|  | 307 | return std::string("Success"); | 
|  | 308 |  | 
|  | 309 | case BAD_ARG: | 
|  | 310 | return std::string("Invalid Argument"); | 
|  | 311 |  | 
|  | 312 | case IO_ERROR: | 
|  | 313 | return std::string("I/O Error"); | 
|  | 314 |  | 
|  | 315 | case BAD_DEV_RESP: | 
|  | 316 | return std::string("Invalid Device Response"); | 
|  | 317 |  | 
|  | 318 | case DEVICE_FAIL: | 
|  | 319 | return std::string("Device Error"); | 
|  | 320 |  | 
|  | 321 | case TIMEOUT: | 
|  | 322 | return std::string("Timeout"); | 
|  | 323 |  | 
|  | 324 | default: | 
|  | 325 | return std::string("Unknown Error"); | 
|  | 326 | } | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | std::string FastBootDriver::Error() { | 
|  | 330 | return error_; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | RetCode FastBootDriver::WaitForDisconnect() { | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 334 | return transport_->WaitForDisconnect() ? IO_ERROR : SUCCESS; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
|  | 337 | /****************************** PROTECTED *************************************/ | 
|  | 338 | RetCode FastBootDriver::RawCommand(const std::string& cmd, std::string* response, | 
|  | 339 | std::vector<std::string>* info, int* dsize) { | 
|  | 340 | error_ = "";  // Clear any pending error | 
|  | 341 | if (cmd.size() > FB_COMMAND_SZ && !disable_checks_) { | 
|  | 342 | error_ = "Command length to RawCommand() is too long"; | 
|  | 343 | return BAD_ARG; | 
|  | 344 | } | 
|  | 345 |  | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 346 | 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] | 347 | error_ = ErrnoStr("Write to device failed"); | 
|  | 348 | return IO_ERROR; | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | // Read the response | 
|  | 352 | return HandleResponse(response, info, dsize); | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | RetCode FastBootDriver::DownloadCommand(uint32_t size, std::string* response, | 
|  | 356 | std::vector<std::string>* info) { | 
|  | 357 | std::string cmd(android::base::StringPrintf("%s%08" PRIx32, Commands::DOWNLOAD.c_str(), size)); | 
|  | 358 | RetCode ret; | 
|  | 359 | if ((ret = RawCommand(cmd, response, info))) { | 
|  | 360 | return ret; | 
|  | 361 | } | 
|  | 362 | return SUCCESS; | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | RetCode FastBootDriver::HandleResponse(std::string* response, std::vector<std::string>* info, | 
|  | 366 | int* dsize) { | 
|  | 367 | char status[FB_RESPONSE_SZ + 1]; | 
|  | 368 | auto start = std::chrono::system_clock::now(); | 
|  | 369 |  | 
|  | 370 | auto set_response = [response](std::string s) { | 
|  | 371 | if (response) *response = std::move(s); | 
|  | 372 | }; | 
|  | 373 | auto add_info = [info](std::string s) { | 
|  | 374 | if (info) info->push_back(std::move(s)); | 
|  | 375 | }; | 
|  | 376 |  | 
|  | 377 | // erase response | 
|  | 378 | set_response(""); | 
|  | 379 | while ((std::chrono::system_clock::now() - start) < std::chrono::seconds(RESP_TIMEOUT)) { | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 380 | int r = transport_->Read(status, FB_RESPONSE_SZ); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 381 | if (r < 0) { | 
|  | 382 | error_ = ErrnoStr("Status read failed"); | 
|  | 383 | return IO_ERROR; | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | status[r] = '\0';  // Need the null terminator | 
|  | 387 | std::string input(status); | 
|  | 388 | if (android::base::StartsWith(input, "INFO")) { | 
|  | 389 | std::string tmp = input.substr(strlen("INFO")); | 
|  | 390 | info_cb_(tmp); | 
|  | 391 | add_info(std::move(tmp)); | 
|  | 392 | } else if (android::base::StartsWith(input, "OKAY")) { | 
|  | 393 | set_response(input.substr(strlen("OKAY"))); | 
|  | 394 | return SUCCESS; | 
|  | 395 | } else if (android::base::StartsWith(input, "FAIL")) { | 
|  | 396 | error_ = android::base::StringPrintf("remote: '%s'", status + strlen("FAIL")); | 
|  | 397 | set_response(input.substr(strlen("FAIL"))); | 
|  | 398 | return DEVICE_FAIL; | 
|  | 399 | } else if (android::base::StartsWith(input, "DATA")) { | 
|  | 400 | std::string tmp = input.substr(strlen("DATA")); | 
|  | 401 | uint32_t num = strtol(tmp.c_str(), 0, 16); | 
|  | 402 | if (num > MAX_DOWNLOAD_SIZE) { | 
|  | 403 | error_ = android::base::StringPrintf("Data size too large (%d)", num); | 
|  | 404 | return BAD_DEV_RESP; | 
|  | 405 | } | 
|  | 406 | if (dsize) *dsize = num; | 
|  | 407 | set_response(std::move(tmp)); | 
|  | 408 | return SUCCESS; | 
|  | 409 | } else { | 
|  | 410 | error_ = android::base::StringPrintf("Device sent unknown status code: %s", status); | 
|  | 411 | return BAD_DEV_RESP; | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | }  // End of while loop | 
|  | 415 |  | 
|  | 416 | return TIMEOUT; | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | std::string FastBootDriver::ErrnoStr(const std::string& msg) { | 
|  | 420 | return android::base::StringPrintf("%s (%s)", msg.c_str(), strerror(errno)); | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | const std::string FastBootDriver::Commands::BOOT = "boot"; | 
|  | 424 | const std::string FastBootDriver::Commands::CONTINUE = "continue"; | 
|  | 425 | const std::string FastBootDriver::Commands::DOWNLOAD = "download:"; | 
|  | 426 | const std::string FastBootDriver::Commands::ERASE = "erase:"; | 
|  | 427 | const std::string FastBootDriver::Commands::FLASH = "flash:"; | 
|  | 428 | const std::string FastBootDriver::Commands::GET_VAR = "getvar:"; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 429 | const std::string FastBootDriver::Commands::REBOOT = "reboot"; | 
|  | 430 | const std::string FastBootDriver::Commands::SET_ACTIVE = "set_active:"; | 
|  | 431 | const std::string FastBootDriver::Commands::UPLOAD = "upload"; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 432 |  | 
|  | 433 | /******************************* PRIVATE **************************************/ | 
|  | 434 | RetCode FastBootDriver::SendBuffer(int fd, size_t size) { | 
|  | 435 | static constexpr uint32_t MAX_MAP_SIZE = 512 * 1024 * 1024; | 
|  | 436 | off64_t offset = 0; | 
|  | 437 | uint32_t remaining = size; | 
|  | 438 | RetCode ret; | 
|  | 439 |  | 
|  | 440 | while (remaining) { | 
|  | 441 | // Memory map the file | 
|  | 442 | android::FileMap filemap; | 
|  | 443 | size_t len = std::min(remaining, MAX_MAP_SIZE); | 
|  | 444 |  | 
|  | 445 | if (!filemap.create(NULL, fd, offset, len, true)) { | 
|  | 446 | error_ = "Creating filemap failed"; | 
|  | 447 | return IO_ERROR; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | if ((ret = SendBuffer(filemap.getDataPtr(), len))) { | 
|  | 451 | return ret; | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | remaining -= len; | 
|  | 455 | offset += len; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | return SUCCESS; | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | RetCode FastBootDriver::SendBuffer(const std::vector<char>& buf) { | 
|  | 462 | // Write the buffer | 
|  | 463 | return SendBuffer(buf.data(), buf.size()); | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | RetCode FastBootDriver::SendBuffer(const void* buf, size_t size) { | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 467 | // ioctl on 0-length buffer causes freezing | 
| David Anderson | 0c7bde8 | 2018-07-30 12:54:53 -0700 | [diff] [blame] | 468 | if (!size) { | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 469 | return BAD_ARG; | 
| David Anderson | 0c7bde8 | 2018-07-30 12:54:53 -0700 | [diff] [blame] | 470 | } | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 471 | // Write the buffer | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 472 | ssize_t tmp = transport_->Write(buf, size); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 473 |  | 
|  | 474 | if (tmp < 0) { | 
|  | 475 | error_ = ErrnoStr("Write to device failed in SendBuffer()"); | 
|  | 476 | return IO_ERROR; | 
|  | 477 | } else if (static_cast<size_t>(tmp) != size) { | 
|  | 478 | error_ = android::base::StringPrintf("Failed to write all %zu bytes", size); | 
|  | 479 |  | 
|  | 480 | return IO_ERROR; | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | return SUCCESS; | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | RetCode FastBootDriver::ReadBuffer(std::vector<char>& buf) { | 
|  | 487 | // Read the buffer | 
|  | 488 | return ReadBuffer(buf.data(), buf.size()); | 
|  | 489 | } | 
|  | 490 |  | 
|  | 491 | RetCode FastBootDriver::ReadBuffer(void* buf, size_t size) { | 
|  | 492 | // Read the buffer | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 493 | ssize_t tmp = transport_->Read(buf, size); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 494 |  | 
|  | 495 | if (tmp < 0) { | 
|  | 496 | error_ = ErrnoStr("Read from device failed in ReadBuffer()"); | 
|  | 497 | return IO_ERROR; | 
|  | 498 | } else if (static_cast<size_t>(tmp) != size) { | 
|  | 499 | error_ = android::base::StringPrintf("Failed to read all %zu bytes", size); | 
|  | 500 | return IO_ERROR; | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | return SUCCESS; | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | int FastBootDriver::SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len) { | 
|  | 507 | size_t total = 0; | 
|  | 508 | size_t to_write = std::min(TRANSPORT_CHUNK_SIZE - tpbuf.size(), len); | 
|  | 509 |  | 
|  | 510 | // Handle the residual | 
|  | 511 | tpbuf.insert(tpbuf.end(), data, data + to_write); | 
|  | 512 | if (tpbuf.size() < TRANSPORT_CHUNK_SIZE) {  // Nothing enough to send rn | 
|  | 513 | return 0; | 
|  | 514 | } | 
|  | 515 |  | 
|  | 516 | if (SendBuffer(tpbuf)) { | 
|  | 517 | error_ = ErrnoStr("Send failed in SparseWriteCallback()"); | 
|  | 518 | return -1; | 
|  | 519 | } | 
|  | 520 | tpbuf.clear(); | 
|  | 521 | total += to_write; | 
|  | 522 |  | 
|  | 523 | // Now we need to send a multiple of chunk size | 
|  | 524 | size_t nchunks = (len - total) / TRANSPORT_CHUNK_SIZE; | 
|  | 525 | size_t nbytes = TRANSPORT_CHUNK_SIZE * nchunks; | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 526 | if (nbytes && SendBuffer(data + total, nbytes)) {  // Don't send a ZLP | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 527 | error_ = ErrnoStr("Send failed in SparseWriteCallback()"); | 
|  | 528 | return -1; | 
|  | 529 | } | 
|  | 530 | total += nbytes; | 
|  | 531 |  | 
|  | 532 | if (len - total > 0) {  // We have residual data to save for next time | 
|  | 533 | tpbuf.assign(data + total, data + len); | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | return 0; | 
|  | 537 | } | 
|  | 538 |  | 
| David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 539 | Transport* FastBootDriver::set_transport(Transport* transport) { | 
|  | 540 | std::swap(transport_, transport); | 
|  | 541 | return transport; | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 542 | } | 
|  | 543 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 544 | }  // End namespace fastboot |