blob: b422c9179c18755f1926045bdd9f32177840ff1d [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
John Stultz3f061f92023-02-04 04:06:06 +000035#include <android-base/endian.h>
Aaron Wisnerdb511202018-06-26 15:38:35 -050036#include <android-base/logging.h>
37#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>
40#include <inttypes.h>
41#include <sparse/sparse.h>
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -070042
43#include "constants.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050044#include "transport.h"
45
46class Transport;
47
48namespace fastboot {
49
Aaron Wisnerdb511202018-06-26 15:38:35 -050050enum RetCode : int {
51 SUCCESS = 0,
52 BAD_ARG,
53 IO_ERROR,
54 BAD_DEV_RESP,
55 DEVICE_FAIL,
56 TIMEOUT,
57};
58
Tom Cherry9027af02018-09-24 15:48:09 -070059struct DriverCallbacks {
60 std::function<void(const std::string&)> prolog = [](const std::string&) {};
61 std::function<void(int)> epilog = [](int) {};
62 std::function<void(const std::string&)> info = [](const std::string&) {};
63};
64
Aaron Wisnerdb511202018-06-26 15:38:35 -050065class FastBootDriver {
Aaron Wisner00737b32018-07-20 17:14:04 -050066 friend class FastBootTest;
67
Aaron Wisnerdb511202018-06-26 15:38:35 -050068 public:
Aaron Wisner50acca72018-08-10 17:44:28 -050069 static constexpr int RESP_TIMEOUT = 30; // 30 seconds
Aaron Wisnerdb511202018-06-26 15:38:35 -050070 static constexpr uint32_t MAX_DOWNLOAD_SIZE = std::numeric_limits<uint32_t>::max();
71 static constexpr size_t TRANSPORT_CHUNK_SIZE = 1024;
72
Tom Cherry9027af02018-09-24 15:48:09 -070073 FastBootDriver(Transport* transport, DriverCallbacks driver_callbacks = {},
Aaron Wisnerdb511202018-06-26 15:38:35 -050074 bool no_checks = false);
David Anderson1d887432018-08-27 16:47:32 -070075 ~FastBootDriver();
Aaron Wisnerdb511202018-06-26 15:38:35 -050076
77 RetCode Boot(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
78 RetCode Continue(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070079 RetCode CreatePartition(const std::string& partition, const std::string& size);
80 RetCode DeletePartition(const std::string& partition);
Yifan Hong58532df2021-03-22 16:39:13 -070081 RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size,
82 std::string* response = nullptr, std::vector<std::string>* info = nullptr);
83 RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050084 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070085 RetCode Download(const std::string& name, const std::vector<char>& buf,
86 std::string* response = nullptr, std::vector<std::string>* info = nullptr);
Aaron Wisnerdb511202018-06-26 15:38:35 -050087 RetCode Download(const std::vector<char>& buf, std::string* response = nullptr,
88 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070089 RetCode Download(const std::string& partition, struct sparse_file* s, uint32_t sz,
90 size_t current, size_t total, bool use_crc, std::string* response = nullptr,
91 std::vector<std::string>* info = nullptr);
Aaron Wisner9812b582018-08-22 11:01:04 -050092 RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050093 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070094 RetCode Erase(const std::string& partition, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050095 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -070096 RetCode Flash(const std::string& partition, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -050097 std::vector<std::string>* info = nullptr);
98 RetCode GetVar(const std::string& key, std::string* val,
99 std::vector<std::string>* info = nullptr);
100 RetCode GetVarAll(std::vector<std::string>* response);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500101 RetCode Reboot(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
David Anderson1d887432018-08-27 16:47:32 -0700102 RetCode RebootTo(std::string target, std::string* response = nullptr,
103 std::vector<std::string>* info = nullptr);
Tom Cherry9027af02018-09-24 15:48:09 -0700104 RetCode ResizePartition(const std::string& partition, const std::string& size);
Tom Cherry11f12092018-08-29 21:36:28 -0700105 RetCode SetActive(const std::string& slot, std::string* response = nullptr,
Aaron Wisnerdb511202018-06-26 15:38:35 -0500106 std::vector<std::string>* info = nullptr);
107 RetCode Upload(const std::string& outfile, std::string* response = nullptr,
108 std::vector<std::string>* info = nullptr);
David Andersonab8f4662019-10-21 16:45:59 -0700109 RetCode SnapshotUpdateCommand(const std::string& command, std::string* response = nullptr,
110 std::vector<std::string>* info = nullptr);
Yifan Hongbcd27702021-02-16 19:37:32 -0800111 RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd,
112 int64_t offset = -1, int64_t size = -1, std::string* response = nullptr,
113 std::vector<std::string>* info = nullptr);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500114
115 /* HIGHER LEVEL COMMANDS -- Composed of the commands above */
Tom Cherry9027af02018-09-24 15:48:09 -0700116 RetCode FlashPartition(const std::string& partition, const std::vector<char>& data);
Yifan Hong58532df2021-03-22 16:39:13 -0700117 RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd,
118 uint32_t sz);
Tom Cherry9027af02018-09-24 15:48:09 -0700119 RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz,
120 size_t current, size_t total);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500121
Tom Cherry9027af02018-09-24 15:48:09 -0700122 RetCode Partitions(std::vector<std::tuple<std::string, uint64_t>>* partitions);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500123 RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet,
124 bool invert = false);
125
126 /* HELPERS */
Tom Cherry9027af02018-09-24 15:48:09 -0700127 void SetInfoCallback(std::function<void(const std::string&)> info);
Aaron Wisnerc771ae02018-08-01 12:57:20 -0500128 static const std::string RCString(RetCode rc);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500129 std::string Error();
130 RetCode WaitForDisconnect();
131
David Anderson03de6452018-09-04 14:32:54 -0700132 // Note: set_transport will return the previous transport.
133 Transport* set_transport(Transport* transport);
David Anderson1d887432018-08-27 16:47:32 -0700134 Transport* transport() const { return transport_; }
135
Tom Cherry9027af02018-09-24 15:48:09 -0700136 RetCode RawCommand(const std::string& cmd, const std::string& message,
137 std::string* response = nullptr, std::vector<std::string>* info = nullptr,
138 int* dsize = nullptr);
139
Aaron Wisnerdb511202018-06-26 15:38:35 -0500140 RetCode RawCommand(const std::string& cmd, std::string* response = nullptr,
141 std::vector<std::string>* info = nullptr, int* dsize = nullptr);
142
143 protected:
144 RetCode DownloadCommand(uint32_t size, std::string* response = nullptr,
145 std::vector<std::string>* info = nullptr);
146 RetCode HandleResponse(std::string* response = nullptr,
147 std::vector<std::string>* info = nullptr, int* dsize = nullptr);
148
149 std::string ErrnoStr(const std::string& msg);
150
David Anderson1d887432018-08-27 16:47:32 -0700151 Transport* transport_;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500152
153 private:
Yifan Hong58532df2021-03-22 16:39:13 -0700154 RetCode SendBuffer(android::base::borrowed_fd fd, size_t size);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500155 RetCode SendBuffer(const std::vector<char>& buf);
156 RetCode SendBuffer(const void* buf, size_t size);
157
Aaron Wisnerdb511202018-06-26 15:38:35 -0500158 RetCode ReadBuffer(void* buf, size_t size);
159
Tom Cherry9027af02018-09-24 15:48:09 -0700160 RetCode UploadInner(const std::string& outfile, std::string* response = nullptr,
161 std::vector<std::string>* info = nullptr);
Yifan Hongbbf374d2021-02-17 11:16:39 -0800162 RetCode RunAndReadBuffer(const std::string& cmd, std::string* response,
163 std::vector<std::string>* info,
164 const std::function<RetCode(const char*, uint64_t)>& write_fn);
Tom Cherry9027af02018-09-24 15:48:09 -0700165
Aaron Wisnerdb511202018-06-26 15:38:35 -0500166 int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len);
167
168 std::string error_;
Tom Cherry9027af02018-09-24 15:48:09 -0700169 std::function<void(const std::string&)> prolog_;
170 std::function<void(int)> epilog_;
171 std::function<void(const std::string&)> info_;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500172 bool disable_checks_;
173};
174
175} // namespace fastboot