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/endian.h> 36 #include <android-base/stringprintf.h> 37 #include <android-base/unique_fd.h> 38 #include <bootimg.h> 39 #include <inttypes.h> 40 #include <sparse/sparse.h> 41 42 #include "constants.h" 43 #include "fastboot_driver_interface.h" 44 #include "transport.h" 45 46 class Transport; 47 48 namespace fastboot { 49 50 struct DriverCallbacks { 51 std::function<void(const std::string&)> prolog = [](const std::string&) {}; 52 std::function<void(int)> epilog = [](int) {}; 53 std::function<void(const std::string&)> info = [](const std::string&) {}; 54 std::function<void(const std::string&)> text = [](const std::string&) {}; 55 }; 56 57 class FastBootDriver : public IFastBootDriver { 58 friend class FastBootTest; 59 60 public: 61 static constexpr int RESP_TIMEOUT = 30; // 30 seconds 62 static constexpr uint32_t MAX_DOWNLOAD_SIZE = std::numeric_limits<uint32_t>::max(); 63 static constexpr size_t TRANSPORT_CHUNK_SIZE = 1024; 64 65 FastBootDriver(Transport* transport, DriverCallbacks driver_callbacks = {}, 66 bool no_checks = false); 67 ~FastBootDriver(); 68 69 RetCode Boot(std::string* response = nullptr, std::vector<std::string>* info = nullptr); 70 RetCode Continue(std::string* response = nullptr, std::vector<std::string>* info = nullptr); 71 RetCode CreatePartition(const std::string& partition, const std::string& size); 72 RetCode DeletePartition(const std::string& partition) override; 73 RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size, 74 std::string* response = nullptr, 75 std::vector<std::string>* info = nullptr) override; 76 RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr, 77 std::vector<std::string>* info = nullptr); 78 RetCode Download(const std::string& name, const std::vector<char>& buf, 79 std::string* response = nullptr, std::vector<std::string>* info = nullptr); 80 RetCode Download(const std::vector<char>& buf, std::string* response = nullptr, 81 std::vector<std::string>* info = nullptr); 82 RetCode Download(const std::string& partition, struct sparse_file* s, uint32_t sz, 83 size_t current, size_t total, bool use_crc, std::string* response = nullptr, 84 std::vector<std::string>* info = nullptr); 85 RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr, 86 std::vector<std::string>* info = nullptr); 87 RetCode Erase(const std::string& partition, std::string* response = nullptr, 88 std::vector<std::string>* info = nullptr) override; 89 RetCode Flash(const std::string& partition, std::string* response = nullptr, 90 std::vector<std::string>* info = nullptr); 91 RetCode GetVar(const std::string& key, std::string* val, 92 std::vector<std::string>* info = nullptr) override; 93 RetCode GetVarAll(std::vector<std::string>* response); 94 RetCode Reboot(std::string* response = nullptr, 95 std::vector<std::string>* info = nullptr) override; 96 RetCode RebootTo(std::string target, std::string* response = nullptr, 97 std::vector<std::string>* info = nullptr) override; 98 RetCode ResizePartition(const std::string& partition, const std::string& size) override; 99 RetCode SetActive(const std::string& slot, std::string* response = nullptr, 100 std::vector<std::string>* info = nullptr); 101 RetCode Upload(const std::string& outfile, std::string* response = nullptr, 102 std::vector<std::string>* info = nullptr); 103 RetCode SnapshotUpdateCommand(const std::string& command, std::string* response = nullptr, 104 std::vector<std::string>* info = nullptr); 105 RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd, 106 int64_t offset = -1, int64_t size = -1, std::string* response = nullptr, 107 std::vector<std::string>* info = nullptr); 108 109 /* HIGHER LEVEL COMMANDS -- Composed of the commands above */ 110 RetCode FlashPartition(const std::string& partition, const std::vector<char>& data); 111 RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd, 112 uint32_t sz) override; 113 RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz, 114 size_t current, size_t total); 115 116 RetCode Partitions(std::vector<std::tuple<std::string, uint64_t>>* partitions); 117 RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet, 118 bool invert = false); 119 120 /* HELPERS */ 121 void SetInfoCallback(std::function<void(const std::string&)> info); 122 static const std::string RCString(RetCode rc); 123 std::string Error(); 124 RetCode WaitForDisconnect() override; 125 126 // Note: set_transport will return the previous transport. 127 Transport* set_transport(Transport* transport); transport()128 Transport* transport() const { return transport_; } 129 130 RetCode RawCommand(const std::string& cmd, const std::string& message, 131 std::string* response = nullptr, std::vector<std::string>* info = nullptr, 132 int* dsize = nullptr); 133 134 RetCode RawCommand(const std::string& cmd, std::string* response = nullptr, 135 std::vector<std::string>* info = nullptr, int* dsize = nullptr); 136 137 protected: 138 RetCode DownloadCommand(uint32_t size, std::string* response = nullptr, 139 std::vector<std::string>* info = nullptr); 140 RetCode HandleResponse(std::string* response = nullptr, 141 std::vector<std::string>* info = nullptr, int* dsize = nullptr); 142 143 std::string ErrnoStr(const std::string& msg); 144 145 Transport* transport_; 146 147 private: 148 RetCode SendBuffer(android::base::borrowed_fd fd, size_t size); 149 RetCode SendBuffer(const std::vector<char>& buf); 150 RetCode SendBuffer(const void* buf, size_t size); 151 152 RetCode ReadBuffer(void* buf, size_t size); 153 154 RetCode UploadInner(const std::string& outfile, std::string* response = nullptr, 155 std::vector<std::string>* info = nullptr); 156 RetCode RunAndReadBuffer(const std::string& cmd, std::string* response, 157 std::vector<std::string>* info, 158 const std::function<RetCode(const char*, uint64_t)>& write_fn); 159 160 int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len); 161 162 std::string error_; 163 std::function<void(const std::string&)> prolog_; 164 std::function<void(int)> epilog_; 165 std::function<void(const std::string&)> info_; 166 std::function<void(const std::string&)> text_; 167 bool disable_checks_; 168 }; 169 170 } // namespace fastboot 171