| 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 | #pragma once | 
|  | 29 | #include <cstdlib> | 
|  | 30 | #include <deque> | 
|  | 31 | #include <limits> | 
|  | 32 | #include <string> | 
|  | 33 | #include <vector> | 
|  | 34 |  | 
|  | 35 | #include <android-base/logging.h> | 
|  | 36 | #include <android-base/stringprintf.h> | 
|  | 37 | #include <bootimg.h> | 
|  | 38 | #include <inttypes.h> | 
|  | 39 | #include <sparse/sparse.h> | 
|  | 40 | #include "transport.h" | 
|  | 41 |  | 
|  | 42 | class Transport; | 
|  | 43 |  | 
|  | 44 | namespace fastboot { | 
|  | 45 |  | 
|  | 46 | static constexpr int FB_COMMAND_SZ = 64; | 
|  | 47 | static constexpr int FB_RESPONSE_SZ = 64; | 
|  | 48 |  | 
|  | 49 | enum RetCode : int { | 
|  | 50 | SUCCESS = 0, | 
|  | 51 | BAD_ARG, | 
|  | 52 | IO_ERROR, | 
|  | 53 | BAD_DEV_RESP, | 
|  | 54 | DEVICE_FAIL, | 
|  | 55 | TIMEOUT, | 
|  | 56 | }; | 
|  | 57 |  | 
|  | 58 | class FastBootDriver { | 
| Aaron Wisner | 00737b3 | 2018-07-20 17:14:04 -0500 | [diff] [blame] | 59 | friend class FastBootTest; | 
|  | 60 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 61 | public: | 
| Aaron Wisner | 50acca7 | 2018-08-10 17:44:28 -0500 | [diff] [blame] | 62 | static constexpr int RESP_TIMEOUT = 30;  // 30 seconds | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 63 | static constexpr uint32_t MAX_DOWNLOAD_SIZE = std::numeric_limits<uint32_t>::max(); | 
|  | 64 | static constexpr size_t TRANSPORT_CHUNK_SIZE = 1024; | 
|  | 65 |  | 
|  | 66 | FastBootDriver(Transport* transport, | 
|  | 67 | std::function<void(std::string&)> info = [](std::string&) {}, | 
|  | 68 | bool no_checks = false); | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 69 | ~FastBootDriver(); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 70 |  | 
|  | 71 | RetCode Boot(std::string* response = nullptr, std::vector<std::string>* info = nullptr); | 
|  | 72 | RetCode Continue(std::string* response = nullptr, std::vector<std::string>* info = nullptr); | 
|  | 73 | RetCode Download(int fd, size_t size, std::string* response = nullptr, | 
|  | 74 | std::vector<std::string>* info = nullptr); | 
|  | 75 | RetCode Download(const std::vector<char>& buf, std::string* response = nullptr, | 
|  | 76 | std::vector<std::string>* info = nullptr); | 
|  | 77 | // This will be removed after fastboot is modified to use a vector | 
|  | 78 | RetCode Download(const char* buf, uint32_t size, std::string* response = nullptr, | 
|  | 79 | std::vector<std::string>* info = nullptr); | 
| Aaron Wisner | 9812b58 | 2018-08-22 11:01:04 -0500 | [diff] [blame] | 80 | RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 81 | std::vector<std::string>* info = nullptr); | 
|  | 82 | RetCode Erase(const std::string& part, std::string* response = nullptr, | 
|  | 83 | std::vector<std::string>* info = nullptr); | 
|  | 84 | RetCode Flash(const std::string& part, std::string* response = nullptr, | 
|  | 85 | std::vector<std::string>* info = nullptr); | 
|  | 86 | RetCode GetVar(const std::string& key, std::string* val, | 
|  | 87 | std::vector<std::string>* info = nullptr); | 
|  | 88 | RetCode GetVarAll(std::vector<std::string>* response); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 89 | RetCode Reboot(std::string* response = nullptr, std::vector<std::string>* info = nullptr); | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 90 | RetCode RebootTo(std::string target, std::string* response = nullptr, | 
|  | 91 | std::vector<std::string>* info = nullptr); | 
| Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 92 | RetCode SetActive(const std::string& slot, std::string* response = nullptr, | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 93 | std::vector<std::string>* info = nullptr); | 
|  | 94 | RetCode Upload(const std::string& outfile, std::string* response = nullptr, | 
|  | 95 | std::vector<std::string>* info = nullptr); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 96 |  | 
|  | 97 | /* HIGHER LEVEL COMMANDS -- Composed of the commands above */ | 
|  | 98 | RetCode FlashPartition(const std::string& part, const std::vector<char>& data); | 
|  | 99 | RetCode FlashPartition(const std::string& part, int fd, uint32_t sz); | 
|  | 100 | RetCode FlashPartition(const std::string& part, sparse_file* s); | 
|  | 101 |  | 
| David Anderson | 6c30f6e | 2018-09-04 15:57:39 -0700 | [diff] [blame] | 102 | RetCode Partitions(std::vector<std::tuple<std::string, uint64_t>>* parts); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 103 | RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet, | 
|  | 104 | bool invert = false); | 
|  | 105 |  | 
|  | 106 | /* HELPERS */ | 
|  | 107 | void SetInfoCallback(std::function<void(std::string&)> info); | 
| Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 108 | static const std::string RCString(RetCode rc); | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 109 | std::string Error(); | 
|  | 110 | RetCode WaitForDisconnect(); | 
|  | 111 |  | 
| David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 112 | // Note: set_transport will return the previous transport. | 
|  | 113 | Transport* set_transport(Transport* transport); | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 114 | Transport* transport() const { return transport_; } | 
|  | 115 |  | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 116 | // This is temporarily public for engine.cpp | 
|  | 117 | RetCode RawCommand(const std::string& cmd, std::string* response = nullptr, | 
|  | 118 | std::vector<std::string>* info = nullptr, int* dsize = nullptr); | 
|  | 119 |  | 
|  | 120 | protected: | 
|  | 121 | RetCode DownloadCommand(uint32_t size, std::string* response = nullptr, | 
|  | 122 | std::vector<std::string>* info = nullptr); | 
|  | 123 | RetCode HandleResponse(std::string* response = nullptr, | 
|  | 124 | std::vector<std::string>* info = nullptr, int* dsize = nullptr); | 
|  | 125 |  | 
|  | 126 | std::string ErrnoStr(const std::string& msg); | 
|  | 127 |  | 
|  | 128 | // More like a namespace... | 
|  | 129 | struct Commands { | 
|  | 130 | static const std::string BOOT; | 
|  | 131 | static const std::string CONTINUE; | 
|  | 132 | static const std::string DOWNLOAD; | 
|  | 133 | static const std::string ERASE; | 
|  | 134 | static const std::string FLASH; | 
|  | 135 | static const std::string GET_VAR; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 136 | static const std::string REBOOT; | 
|  | 137 | static const std::string SET_ACTIVE; | 
|  | 138 | static const std::string UPLOAD; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 139 | }; | 
|  | 140 |  | 
| David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 141 | Transport* transport_; | 
| Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 142 |  | 
|  | 143 | private: | 
|  | 144 | RetCode SendBuffer(int fd, size_t size); | 
|  | 145 | RetCode SendBuffer(const std::vector<char>& buf); | 
|  | 146 | RetCode SendBuffer(const void* buf, size_t size); | 
|  | 147 |  | 
|  | 148 | RetCode ReadBuffer(std::vector<char>& buf); | 
|  | 149 | RetCode ReadBuffer(void* buf, size_t size); | 
|  | 150 |  | 
|  | 151 | int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len); | 
|  | 152 |  | 
|  | 153 | std::string error_; | 
|  | 154 | std::function<void(std::string&)> info_cb_; | 
|  | 155 | bool disable_checks_; | 
|  | 156 | }; | 
|  | 157 |  | 
|  | 158 | }  // namespace fastboot |