blob: f1c094f2849253cd362ebc9dd43a10502b20b707 [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>
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>
Yifan Hongbcd27702021-02-16 19:37:32 -080037#include <android-base/unique_fd.h>
Aaron Wisnerdb511202018-06-26 15:38:35 -050038#include <bootimg.h>
39#include <inttypes.h>
40#include <sparse/sparse.h>
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -070041
42#include "constants.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050043#include "transport.h"
44
45class Transport;
46
47namespace fastboot {
48
Aaron Wisnerdb511202018-06-26 15:38:35 -050049enum RetCode : int {
50 SUCCESS = 0,
51 BAD_ARG,
52 IO_ERROR,
53 BAD_DEV_RESP,
54 DEVICE_FAIL,
55 TIMEOUT,
56};
57
Tom Cherry9027af02018-09-24 15:48:09 -070058struct DriverCallbacks {
59 std::function<void(const std::string&)> prolog = [](const std::string&) {};
60 std::function<void(int)> epilog = [](int) {};
61 std::function<void(const std::string&)> info = [](const std::string&) {};
62};
63
Aaron Wisnerdb511202018-06-26 15:38:35 -050064class FastBootDriver {
Aaron Wisner00737b32018-07-20 17:14:04 -050065 friend class FastBootTest;
66
Aaron Wisnerdb511202018-06-26 15:38:35 -050067 public:
Aaron Wisner50acca72018-08-10 17:44:28 -050068 static constexpr int RESP_TIMEOUT = 30; // 30 seconds
Aaron Wisnerdb511202018-06-26 15:38:35 -050069 static constexpr uint32_t MAX_DOWNLOAD_SIZE = std::numeric_limits<uint32_t>::max();
70 static constexpr size_t TRANSPORT_CHUNK_SIZE = 1024;
71
Tom Cherry9027af02018-09-24 15:48:09 -070072 FastBootDriver(Transport* transport, DriverCallbacks driver_callbacks = {},
Aaron Wisnerdb511202018-06-26 15:38:35 -050073 bool no_checks = false);
David Anderson1d887432018-08-27 16:47:32 -070074 ~FastBootDriver();
Aaron Wisnerdb511202018-06-26 15:38:35 -050075
76 RetCode Boot(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
77 RetCode Continue(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070078 RetCode CreatePartition(const std::string& partition, const std::string& size);
79 RetCode DeletePartition(const std::string& partition);
80 RetCode Download(const std::string& name, int fd, size_t size, std::string* response = nullptr,
81 std::vector<std::string>* info = nullptr);
Aaron Wisnerdb511202018-06-26 15:38:35 -050082 RetCode Download(int fd, size_t size, std::string* response = nullptr,
83 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070084 RetCode Download(const std::string& name, const std::vector<char>& buf,
85 std::string* response = nullptr, std::vector<std::string>* info = nullptr);
Aaron Wisnerdb511202018-06-26 15:38:35 -050086 RetCode Download(const std::vector<char>& buf, std::string* response = nullptr,
87 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070088 RetCode Download(const std::string& partition, struct sparse_file* s, uint32_t sz,
89 size_t current, size_t total, bool use_crc, std::string* response = nullptr,
90 std::vector<std::string>* info = nullptr);
Aaron Wisner9812b582018-08-22 11:01:04 -050091 RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050092 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070093 RetCode Erase(const std::string& partition, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050094 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070095 RetCode Flash(const std::string& partition, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050096 std::vector<std::string>* info = nullptr);
97 RetCode GetVar(const std::string& key, std::string* val,
98 std::vector<std::string>* info = nullptr);
99 RetCode GetVarAll(std::vector<std::string>* response);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500100 RetCode Reboot(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
David Anderson1d887432018-08-27 16:47:32 -0700101 RetCode RebootTo(std::string target, std::string* response = nullptr,
102 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -0700103 RetCode ResizePartition(const std::string& partition, const std::string& size);
Tom Cherry11f12092018-08-29 21:36:28 -0700104 RetCode SetActive(const std::string& slot, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -0500105 std::vector<std::string>* info = nullptr);
106 RetCode Upload(const std::string& outfile, std::string* response = nullptr,
107 std::vector<std::string>* info = nullptr);
David Andersonab8f4662019-10-21 16:45:59 -0700108 RetCode SnapshotUpdateCommand(const std::string& command, std::string* response = nullptr,
109 std::vector<std::string>* info = nullptr);
Yifan Hongbcd27702021-02-16 19:37:32 -0800110 RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd,
111 int64_t offset = -1, int64_t size = -1, std::string* response = nullptr,
112 std::vector<std::string>* info = nullptr);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500113
114 /* HIGHER LEVEL COMMANDS -- Composed of the commands above */
Tom Cherry9027af02018-09-24 15:48:09 -0700115 RetCode FlashPartition(const std::string& partition, const std::vector<char>& data);
116 RetCode FlashPartition(const std::string& partition, int fd, uint32_t sz);
117 RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz,
118 size_t current, size_t total);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500119
Tom Cherry9027af02018-09-24 15:48:09 -0700120 RetCode Partitions(std::vector<std::tuple<std::string, uint64_t>>* partitions);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500121 RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet,
122 bool invert = false);
123
124 /* HELPERS */
Tom Cherry9027af02018-09-24 15:48:09 -0700125 void SetInfoCallback(std::function<void(const std::string&)> info);
Aaron Wisnerc771ae02018-08-01 12:57:20 -0500126 static const std::string RCString(RetCode rc);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500127 std::string Error();
128 RetCode WaitForDisconnect();
129
David Anderson03de6452018-09-04 14:32:54 -0700130 // Note: set_transport will return the previous transport.
131 Transport* set_transport(Transport* transport);
David Anderson1d887432018-08-27 16:47:32 -0700132 Transport* transport() const { return transport_; }
133
Tom Cherry9027af02018-09-24 15:48:09 -0700134 RetCode RawCommand(const std::string& cmd, const std::string& message,
135 std::string* response = nullptr, std::vector<std::string>* info = nullptr,
136 int* dsize = nullptr);
137
Aaron Wisnerdb511202018-06-26 15:38:35 -0500138 RetCode RawCommand(const std::string& cmd, std::string* response = nullptr,
139 std::vector<std::string>* info = nullptr, int* dsize = nullptr);
140
141 protected:
142 RetCode DownloadCommand(uint32_t size, std::string* response = nullptr,
143 std::vector<std::string>* info = nullptr);
144 RetCode HandleResponse(std::string* response = nullptr,
145 std::vector<std::string>* info = nullptr, int* dsize = nullptr);
146
147 std::string ErrnoStr(const std::string& msg);
148
David Anderson1d887432018-08-27 16:47:32 -0700149 Transport* transport_;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500150
151 private:
152 RetCode SendBuffer(int fd, size_t size);
153 RetCode SendBuffer(const std::vector<char>& buf);
154 RetCode SendBuffer(const void* buf, size_t size);
155
Aaron Wisnerdb511202018-06-26 15:38:35 -0500156 RetCode ReadBuffer(void* buf, size_t size);
157
Tom Cherry9027af02018-09-24 15:48:09 -0700158 RetCode UploadInner(const std::string& outfile, std::string* response = nullptr,
159 std::vector<std::string>* info = nullptr);
Yifan Hongbbf374d2021-02-17 11:16:39 -0800160 RetCode RunAndReadBuffer(const std::string& cmd, std::string* response,
161 std::vector<std::string>* info,
162 const std::function<RetCode(const char*, uint64_t)>& write_fn);
Tom Cherry9027af02018-09-24 15:48:09 -0700163
Aaron Wisnerdb511202018-06-26 15:38:35 -0500164 int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len);
165
166 std::string error_;
Tom Cherry9027af02018-09-24 15:48:09 -0700167 std::function<void(const std::string&)> prolog_;
168 std::function<void(int)> epilog_;
169 std::function<void(const std::string&)> info_;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500170 bool disable_checks_;
171};
172
173} // namespace fastboot