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> |
Biswapriyo Nath | 214f37c | 2023-06-10 20:03:31 +0530 | [diff] [blame] | 31 | #include <functional> |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 32 | #include <limits> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
John Stultz | 3f061f9 | 2023-02-04 04:06:06 +0000 | [diff] [blame] | 36 | #include <android-base/endian.h> |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Yifan Hong | bcd2770 | 2021-02-16 19:37:32 -0800 | [diff] [blame] | 38 | #include <android-base/unique_fd.h> |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 39 | #include <bootimg.h> |
| 40 | #include <inttypes.h> |
| 41 | #include <sparse/sparse.h> |
Mark Salyzyn | 8e7e9cb | 2018-08-29 10:44:33 -0700 | [diff] [blame] | 42 | |
| 43 | #include "constants.h" |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 44 | #include "fastboot_driver_interface.h" |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 45 | #include "transport.h" |
| 46 | |
| 47 | class Transport; |
| 48 | |
| 49 | namespace fastboot { |
| 50 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 51 | struct DriverCallbacks { |
| 52 | std::function<void(const std::string&)> prolog = [](const std::string&) {}; |
| 53 | std::function<void(int)> epilog = [](int) {}; |
| 54 | std::function<void(const std::string&)> info = [](const std::string&) {}; |
Raphael Herouart | 99097cc | 2022-12-30 11:51:54 +0000 | [diff] [blame] | 55 | std::function<void(const std::string&)> text = [](const std::string&) {}; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 58 | class FastBootDriver : public IFastBootDriver { |
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 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 66 | FastBootDriver(Transport* transport, DriverCallbacks driver_callbacks = {}, |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 67 | bool no_checks = false); |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 68 | ~FastBootDriver(); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 69 | |
| 70 | RetCode Boot(std::string* response = nullptr, std::vector<std::string>* info = nullptr); |
| 71 | RetCode Continue(std::string* response = nullptr, std::vector<std::string>* info = nullptr); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 72 | RetCode CreatePartition(const std::string& partition, const std::string& size); |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 73 | RetCode DeletePartition(const std::string& partition) override; |
Yifan Hong | 58532df | 2021-03-22 16:39:13 -0700 | [diff] [blame] | 74 | RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size, |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 75 | std::string* response = nullptr, |
| 76 | std::vector<std::string>* info = nullptr) override; |
Yifan Hong | 58532df | 2021-03-22 16:39:13 -0700 | [diff] [blame] | 77 | RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr, |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 78 | std::vector<std::string>* info = nullptr); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 79 | RetCode Download(const std::string& name, const std::vector<char>& buf, |
| 80 | std::string* response = nullptr, std::vector<std::string>* info = nullptr); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 81 | RetCode Download(const std::vector<char>& buf, std::string* response = nullptr, |
| 82 | std::vector<std::string>* info = nullptr); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 83 | RetCode Download(const std::string& partition, struct sparse_file* s, uint32_t sz, |
| 84 | size_t current, size_t total, bool use_crc, std::string* response = nullptr, |
| 85 | std::vector<std::string>* info = nullptr); |
Aaron Wisner | 9812b58 | 2018-08-22 11:01:04 -0500 | [diff] [blame] | 86 | 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] | 87 | std::vector<std::string>* info = nullptr); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 88 | RetCode Erase(const std::string& partition, std::string* response = nullptr, |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 89 | std::vector<std::string>* info = nullptr) override; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 90 | RetCode Flash(const std::string& partition, std::string* response = nullptr, |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 91 | std::vector<std::string>* info = nullptr); |
| 92 | RetCode GetVar(const std::string& key, std::string* val, |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 93 | std::vector<std::string>* info = nullptr) override; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 94 | RetCode GetVarAll(std::vector<std::string>* response); |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 95 | RetCode Reboot(std::string* response = nullptr, |
| 96 | std::vector<std::string>* info = nullptr) override; |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 97 | RetCode RebootTo(std::string target, std::string* response = nullptr, |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 98 | std::vector<std::string>* info = nullptr) override; |
| 99 | RetCode ResizePartition(const std::string& partition, const std::string& size) override; |
Tom Cherry | 11f1209 | 2018-08-29 21:36:28 -0700 | [diff] [blame] | 100 | RetCode SetActive(const std::string& slot, std::string* response = nullptr, |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 101 | std::vector<std::string>* info = nullptr); |
| 102 | RetCode Upload(const std::string& outfile, std::string* response = nullptr, |
| 103 | std::vector<std::string>* info = nullptr); |
David Anderson | ab8f466 | 2019-10-21 16:45:59 -0700 | [diff] [blame] | 104 | RetCode SnapshotUpdateCommand(const std::string& command, std::string* response = nullptr, |
| 105 | std::vector<std::string>* info = nullptr); |
Yifan Hong | bcd2770 | 2021-02-16 19:37:32 -0800 | [diff] [blame] | 106 | RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd, |
| 107 | int64_t offset = -1, int64_t size = -1, std::string* response = nullptr, |
Daniel Zheng | a0d2401 | 2023-07-10 16:45:54 -0700 | [diff] [blame^] | 108 | std::vector<std::string>* info = nullptr) override; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 109 | |
| 110 | /* HIGHER LEVEL COMMANDS -- Composed of the commands above */ |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 111 | RetCode FlashPartition(const std::string& partition, const std::vector<char>& data); |
Yifan Hong | 58532df | 2021-03-22 16:39:13 -0700 | [diff] [blame] | 112 | RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd, |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 113 | uint32_t sz) override; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 114 | RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz, |
| 115 | size_t current, size_t total); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 116 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 117 | RetCode Partitions(std::vector<std::tuple<std::string, uint64_t>>* partitions); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 118 | RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet, |
| 119 | bool invert = false); |
| 120 | |
| 121 | /* HELPERS */ |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 122 | void SetInfoCallback(std::function<void(const std::string&)> info); |
Aaron Wisner | c771ae0 | 2018-08-01 12:57:20 -0500 | [diff] [blame] | 123 | static const std::string RCString(RetCode rc); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 124 | std::string Error(); |
Daniel Zheng | c9e869b | 2023-03-30 14:59:48 -0700 | [diff] [blame] | 125 | RetCode WaitForDisconnect() override; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 126 | |
David Anderson | 03de645 | 2018-09-04 14:32:54 -0700 | [diff] [blame] | 127 | // Note: set_transport will return the previous transport. |
| 128 | Transport* set_transport(Transport* transport); |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 129 | Transport* transport() const { return transport_; } |
| 130 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 131 | RetCode RawCommand(const std::string& cmd, const std::string& message, |
| 132 | std::string* response = nullptr, std::vector<std::string>* info = nullptr, |
| 133 | int* dsize = nullptr); |
| 134 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 135 | RetCode RawCommand(const std::string& cmd, std::string* response = nullptr, |
| 136 | std::vector<std::string>* info = nullptr, int* dsize = nullptr); |
| 137 | |
| 138 | protected: |
| 139 | RetCode DownloadCommand(uint32_t size, std::string* response = nullptr, |
| 140 | std::vector<std::string>* info = nullptr); |
| 141 | RetCode HandleResponse(std::string* response = nullptr, |
| 142 | std::vector<std::string>* info = nullptr, int* dsize = nullptr); |
| 143 | |
| 144 | std::string ErrnoStr(const std::string& msg); |
| 145 | |
David Anderson | 1d88743 | 2018-08-27 16:47:32 -0700 | [diff] [blame] | 146 | Transport* transport_; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 147 | |
| 148 | private: |
Yifan Hong | 58532df | 2021-03-22 16:39:13 -0700 | [diff] [blame] | 149 | RetCode SendBuffer(android::base::borrowed_fd fd, size_t size); |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 150 | RetCode SendBuffer(const std::vector<char>& buf); |
| 151 | RetCode SendBuffer(const void* buf, size_t size); |
| 152 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 153 | RetCode ReadBuffer(void* buf, size_t size); |
| 154 | |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 155 | RetCode UploadInner(const std::string& outfile, std::string* response = nullptr, |
| 156 | std::vector<std::string>* info = nullptr); |
Yifan Hong | bbf374d | 2021-02-17 11:16:39 -0800 | [diff] [blame] | 157 | RetCode RunAndReadBuffer(const std::string& cmd, std::string* response, |
| 158 | std::vector<std::string>* info, |
| 159 | const std::function<RetCode(const char*, uint64_t)>& write_fn); |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 160 | |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 161 | int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len); |
| 162 | |
| 163 | std::string error_; |
Tom Cherry | 9027af0 | 2018-09-24 15:48:09 -0700 | [diff] [blame] | 164 | std::function<void(const std::string&)> prolog_; |
| 165 | std::function<void(int)> epilog_; |
| 166 | std::function<void(const std::string&)> info_; |
Raphael Herouart | 99097cc | 2022-12-30 11:51:54 +0000 | [diff] [blame] | 167 | std::function<void(const std::string&)> text_; |
Aaron Wisner | db51120 | 2018-06-26 15:38:35 -0500 | [diff] [blame] | 168 | bool disable_checks_; |
| 169 | }; |
| 170 | |
| 171 | } // namespace fastboot |