blob: 49cebd97a1c064594b33c3b90068ac1bcc3b3f5f [file] [log] [blame]
Aaron Wisnerdb511202018-06-26 15:38:35 -05001/*
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>
Biswapriyo Nath214f37c2023-06-10 20:03:31 +053030#include <functional>
Aaron Wisnerdb511202018-06-26 15:38:35 -050031#include <limits>
Dmitrii Merkurev0b627d92023-09-03 17:30:46 +010032#include <memory>
Aaron Wisnerdb511202018-06-26 15:38:35 -050033#include <string>
34#include <vector>
35
John Stultz3f061f92023-02-04 04:06:06 +000036#include <android-base/endian.h>
Aaron Wisnerdb511202018-06-26 15:38:35 -050037#include <android-base/stringprintf.h>
Yifan Hongbcd27702021-02-16 19:37:32 -080038#include <android-base/unique_fd.h>
Aaron Wisnerdb511202018-06-26 15:38:35 -050039#include <bootimg.h>
Aaron Wisnerdb511202018-06-26 15:38:35 -050040#include <sparse/sparse.h>
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -070041
Daniel Zhengc9e869b2023-03-30 14:59:48 -070042#include "fastboot_driver_interface.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050043#include "transport.h"
44
45class Transport;
46
47namespace fastboot {
48
Tom Cherry9027af02018-09-24 15:48:09 -070049struct DriverCallbacks {
50 std::function<void(const std::string&)> prolog = [](const std::string&) {};
51 std::function<void(int)> epilog = [](int) {};
52 std::function<void(const std::string&)> info = [](const std::string&) {};
Raphael Herouart99097cc2022-12-30 11:51:54 +000053 std::function<void(const std::string&)> text = [](const std::string&) {};
Tom Cherry9027af02018-09-24 15:48:09 -070054};
55
Daniel Zhengc9e869b2023-03-30 14:59:48 -070056class FastBootDriver : public IFastBootDriver {
Aaron Wisner00737b32018-07-20 17:14:04 -050057 friend class FastBootTest;
58
Aaron Wisnerdb511202018-06-26 15:38:35 -050059 public:
Aaron Wisner50acca72018-08-10 17:44:28 -050060 static constexpr int RESP_TIMEOUT = 30; // 30 seconds
Aaron Wisnerdb511202018-06-26 15:38:35 -050061 static constexpr uint32_t MAX_DOWNLOAD_SIZE = std::numeric_limits<uint32_t>::max();
62 static constexpr size_t TRANSPORT_CHUNK_SIZE = 1024;
63
Dmitrii Merkurev0b627d92023-09-03 17:30:46 +010064 FastBootDriver(std::unique_ptr<Transport> transport, DriverCallbacks driver_callbacks = {},
Aaron Wisnerdb511202018-06-26 15:38:35 -050065 bool no_checks = false);
David Anderson1d887432018-08-27 16:47:32 -070066 ~FastBootDriver();
Aaron Wisnerdb511202018-06-26 15:38:35 -050067
68 RetCode Boot(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
69 RetCode Continue(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070070 RetCode CreatePartition(const std::string& partition, const std::string& size);
Daniel Zhengc9e869b2023-03-30 14:59:48 -070071 RetCode DeletePartition(const std::string& partition) override;
Yifan Hong58532df2021-03-22 16:39:13 -070072 RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size,
Daniel Zhengc9e869b2023-03-30 14:59:48 -070073 std::string* response = nullptr,
74 std::vector<std::string>* info = nullptr) override;
Yifan Hong58532df2021-03-22 16:39:13 -070075 RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050076 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070077 RetCode Download(const std::string& name, const std::vector<char>& buf,
78 std::string* response = nullptr, std::vector<std::string>* info = nullptr);
Aaron Wisnerdb511202018-06-26 15:38:35 -050079 RetCode Download(const std::vector<char>& buf, std::string* response = nullptr,
80 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070081 RetCode Download(const std::string& partition, struct sparse_file* s, uint32_t sz,
82 size_t current, size_t total, bool use_crc, std::string* response = nullptr,
83 std::vector<std::string>* info = nullptr);
Aaron Wisner9812b582018-08-22 11:01:04 -050084 RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050085 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070086 RetCode Erase(const std::string& partition, std::string* response = nullptr,
Daniel Zhengc9e869b2023-03-30 14:59:48 -070087 std::vector<std::string>* info = nullptr) override;
Tom Cherry9027af02018-09-24 15:48:09 -070088 RetCode Flash(const std::string& partition, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050089 std::vector<std::string>* info = nullptr);
90 RetCode GetVar(const std::string& key, std::string* val,
Daniel Zhengc9e869b2023-03-30 14:59:48 -070091 std::vector<std::string>* info = nullptr) override;
Aaron Wisnerdb511202018-06-26 15:38:35 -050092 RetCode GetVarAll(std::vector<std::string>* response);
Daniel Zhengc9e869b2023-03-30 14:59:48 -070093 RetCode Reboot(std::string* response = nullptr,
94 std::vector<std::string>* info = nullptr) override;
David Anderson1d887432018-08-27 16:47:32 -070095 RetCode RebootTo(std::string target, std::string* response = nullptr,
Daniel Zhengc9e869b2023-03-30 14:59:48 -070096 std::vector<std::string>* info = nullptr) override;
97 RetCode ResizePartition(const std::string& partition, const std::string& size) override;
Tom Cherry11f12092018-08-29 21:36:28 -070098 RetCode SetActive(const std::string& slot, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050099 std::vector<std::string>* info = nullptr);
100 RetCode Upload(const std::string& outfile, std::string* response = nullptr,
101 std::vector<std::string>* info = nullptr);
David Andersonab8f4662019-10-21 16:45:59 -0700102 RetCode SnapshotUpdateCommand(const std::string& command, std::string* response = nullptr,
103 std::vector<std::string>* info = nullptr);
Yifan Hongbcd27702021-02-16 19:37:32 -0800104 RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd,
105 int64_t offset = -1, int64_t size = -1, std::string* response = nullptr,
Daniel Zhenga0d24012023-07-10 16:45:54 -0700106 std::vector<std::string>* info = nullptr) override;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500107
108 /* HIGHER LEVEL COMMANDS -- Composed of the commands above */
Tom Cherry9027af02018-09-24 15:48:09 -0700109 RetCode FlashPartition(const std::string& partition, const std::vector<char>& data);
Yifan Hong58532df2021-03-22 16:39:13 -0700110 RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd,
Daniel Zhengc9e869b2023-03-30 14:59:48 -0700111 uint32_t sz) override;
Tom Cherry9027af02018-09-24 15:48:09 -0700112 RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz,
113 size_t current, size_t total);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500114
Tom Cherry9027af02018-09-24 15:48:09 -0700115 RetCode Partitions(std::vector<std::tuple<std::string, uint64_t>>* partitions);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500116 RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet,
117 bool invert = false);
118
119 /* HELPERS */
Tom Cherry9027af02018-09-24 15:48:09 -0700120 void SetInfoCallback(std::function<void(const std::string&)> info);
Aaron Wisnerc771ae02018-08-01 12:57:20 -0500121 static const std::string RCString(RetCode rc);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500122 std::string Error();
Daniel Zhengc9e869b2023-03-30 14:59:48 -0700123 RetCode WaitForDisconnect() override;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500124
Dmitrii Merkurev0b627d92023-09-03 17:30:46 +0100125 void set_transport(std::unique_ptr<Transport> transport);
David Anderson1d887432018-08-27 16:47:32 -0700126
Tom Cherry9027af02018-09-24 15:48:09 -0700127 RetCode RawCommand(const std::string& cmd, const std::string& message,
128 std::string* response = nullptr, std::vector<std::string>* info = nullptr,
129 int* dsize = nullptr);
130
Aaron Wisnerdb511202018-06-26 15:38:35 -0500131 RetCode RawCommand(const std::string& cmd, std::string* response = nullptr,
132 std::vector<std::string>* info = nullptr, int* dsize = nullptr);
133
134 protected:
135 RetCode DownloadCommand(uint32_t size, std::string* response = nullptr,
136 std::vector<std::string>* info = nullptr);
137 RetCode HandleResponse(std::string* response = nullptr,
138 std::vector<std::string>* info = nullptr, int* dsize = nullptr);
139
140 std::string ErrnoStr(const std::string& msg);
141
Dmitrii Merkurev0b627d92023-09-03 17:30:46 +0100142 std::unique_ptr<Transport> transport_;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500143
144 private:
Yifan Hong58532df2021-03-22 16:39:13 -0700145 RetCode SendBuffer(android::base::borrowed_fd fd, size_t size);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500146 RetCode SendBuffer(const std::vector<char>& buf);
147 RetCode SendBuffer(const void* buf, size_t size);
148
Aaron Wisnerdb511202018-06-26 15:38:35 -0500149 RetCode ReadBuffer(void* buf, size_t size);
150
Tom Cherry9027af02018-09-24 15:48:09 -0700151 RetCode UploadInner(const std::string& outfile, std::string* response = nullptr,
152 std::vector<std::string>* info = nullptr);
Yifan Hongbbf374d2021-02-17 11:16:39 -0800153 RetCode RunAndReadBuffer(const std::string& cmd, std::string* response,
154 std::vector<std::string>* info,
155 const std::function<RetCode(const char*, uint64_t)>& write_fn);
Tom Cherry9027af02018-09-24 15:48:09 -0700156
Aaron Wisnerdb511202018-06-26 15:38:35 -0500157 int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len);
158
159 std::string error_;
Tom Cherry9027af02018-09-24 15:48:09 -0700160 std::function<void(const std::string&)> prolog_;
161 std::function<void(int)> epilog_;
162 std::function<void(const std::string&)> info_;
Raphael Herouart99097cc2022-12-30 11:51:54 +0000163 std::function<void(const std::string&)> text_;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500164 bool disable_checks_;
165};
166
167} // namespace fastboot