Android-cuttlefish cvd tool
fastboot_driver.h
Go to the documentation of this file.
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 <functional>
31#include <limits>
32#include <memory>
33#include <string>
34#include <vector>
35
36#include <android-base/endian.h>
39#include <bootimg.h>
40#include <sparse/sparse.h>
41
43#include "transport.h"
44
45class Transport;
46
47namespace fastboot {
48
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&) {};
53 std::function<void(const std::string&)> text = [](const std::string&) {};
54};
55
57 friend class FastBootTest;
58
59 public:
60 static constexpr int RESP_TIMEOUT = 30; // 30 seconds
61 static constexpr uint32_t MAX_DOWNLOAD_SIZE = std::numeric_limits<uint32_t>::max();
62 static constexpr size_t TRANSPORT_CHUNK_SIZE = 1024;
63
64 FastBootDriver(std::unique_ptr<Transport> transport, DriverCallbacks driver_callbacks = {},
65 bool no_checks = false);
67
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);
70 RetCode CreatePartition(const std::string& partition, const std::string& size);
71 RetCode DeletePartition(const std::string& partition) override;
72 RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size,
73 std::string* response = nullptr,
74 std::vector<std::string>* info = nullptr) override;
75 RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr,
76 std::vector<std::string>* info = nullptr);
77 RetCode Download(const std::string& name, const std::vector<char>& buf,
78 std::string* response = nullptr, std::vector<std::string>* info = nullptr);
79 RetCode Download(const std::vector<char>& buf, std::string* response = nullptr,
80 std::vector<std::string>* info = nullptr);
81 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);
84 RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr,
85 std::vector<std::string>* info = nullptr);
86 RetCode Erase(const std::string& partition, std::string* response = nullptr,
87 std::vector<std::string>* info = nullptr) override;
88 RetCode Flash(const std::string& partition, std::string* response = nullptr,
89 std::vector<std::string>* info = nullptr);
90 RetCode GetVar(const std::string& key, std::string* val,
91 std::vector<std::string>* info = nullptr) override;
92 RetCode GetVarAll(std::vector<std::string>* response);
93 RetCode Reboot(std::string* response = nullptr,
94 std::vector<std::string>* info = nullptr) override;
95 RetCode RebootTo(std::string target, std::string* response = nullptr,
96 std::vector<std::string>* info = nullptr) override;
97 RetCode ResizePartition(const std::string& partition, const std::string& size) override;
98 RetCode SetActive(const std::string& slot, std::string* response = nullptr,
99 std::vector<std::string>* info = nullptr);
100 RetCode Upload(const std::string& outfile, std::string* response = nullptr,
101 std::vector<std::string>* info = nullptr);
102 RetCode SnapshotUpdateCommand(const std::string& command, std::string* response = nullptr,
103 std::vector<std::string>* info = nullptr);
104 RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd,
105 int64_t offset = -1, int64_t size = -1, std::string* response = nullptr,
106 std::vector<std::string>* info = nullptr) override;
107
108 /* HIGHER LEVEL COMMANDS -- Composed of the commands above */
109 RetCode FlashPartition(const std::string& partition, const std::vector<char>& data);
110 RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd,
111 uint32_t sz) override;
112 RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz,
113 size_t current, size_t total);
114
115 RetCode Partitions(std::vector<std::tuple<std::string, uint64_t>>* partitions);
116 RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet,
117 bool invert = false);
118
119 /* HELPERS */
120 void SetInfoCallback(std::function<void(const std::string&)> info);
121 static const std::string RCString(RetCode rc);
122 std::string Error();
123 RetCode WaitForDisconnect() override;
124
125 void set_transport(std::unique_ptr<Transport> transport);
126
127 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
131 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
142 std::unique_ptr<Transport> transport_;
143
144 private:
146 RetCode SendBuffer(const std::vector<char>& buf);
147 RetCode SendBuffer(const void* buf, size_t size);
148
149 RetCode ReadBuffer(void* buf, size_t size);
150
151 RetCode UploadInner(const std::string& outfile, std::string* response = nullptr,
152 std::vector<std::string>* info = nullptr);
153 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);
156
157 int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len);
158
159 std::string error_;
160 std::function<void(const std::string&)> prolog_;
161 std::function<void(int)> epilog_;
162 std::function<void(const std::string&)> info_;
163 std::function<void(const std::string&)> text_;
165};
166
167} // namespace fastboot
Definition: transport.h:23
Definition: fastboot_driver.h:56
FastBootDriver(std::unique_ptr< Transport > transport, DriverCallbacks driver_callbacks={}, bool no_checks=false)
Definition: fastboot_driver.cpp:61
static constexpr uint32_t MAX_DOWNLOAD_SIZE
Definition: fastboot_driver.h:61
RetCode ReadBuffer(void *buf, size_t size)
Definition: fastboot_driver.cpp:583
void set_transport(std::unique_ptr< Transport > transport)
Definition: fastboot_driver.cpp:631
RetCode SetActive(const std::string &slot, std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver.cpp:125
friend class FastBootTest
Definition: fastboot_driver.h:57
std::string Error()
Definition: fastboot_driver.cpp:419
RetCode HandleResponse(std::string *response=nullptr, std::vector< std::string > *info=nullptr, int *dsize=nullptr)
Definition: fastboot_driver.cpp:464
RetCode ResizePartition(const std::string &partition, const std::string &size) override
Definition: fastboot_driver.cpp:120
std::function< void(int)> epilog_
Definition: fastboot_driver.h:161
RetCode GetVarAll(std::vector< std::string > *response)
Definition: fastboot_driver.cpp:106
RetCode Continue(std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver.cpp:78
std::function< void(const std::string &)> prolog_
Definition: fastboot_driver.h:160
bool disable_checks_
Definition: fastboot_driver.h:164
RetCode Require(const std::string &var, const std::vector< std::string > &allowed, bool *reqmet, bool invert=false)
int SparseWriteCallback(std::vector< char > &tpbuf, const char *data, size_t len)
Definition: fastboot_driver.cpp:598
RetCode Download(const std::string &name, android::base::borrowed_fd fd, size_t size, std::string *response=nullptr, std::vector< std::string > *info=nullptr) override
Definition: fastboot_driver.cpp:188
RetCode RunAndReadBuffer(const std::string &cmd, std::string *response, std::vector< std::string > *info, const std::function< RetCode(const char *, uint64_t)> &write_fn)
Definition: fastboot_driver.cpp:311
std::function< void(const std::string &)> text_
Definition: fastboot_driver.h:163
RetCode Boot(std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver.cpp:74
RetCode DeletePartition(const std::string &partition) override
Definition: fastboot_driver.cpp:87
static constexpr int RESP_TIMEOUT
Definition: fastboot_driver.h:60
RetCode FlashPartition(const std::string &partition, const std::vector< char > &data)
Definition: fastboot_driver.cpp:140
RetCode Reboot(std::string *response=nullptr, std::vector< std::string > *info=nullptr) override
Definition: fastboot_driver.cpp:111
std::function< void(const std::string &)> info_
Definition: fastboot_driver.h:162
void SetInfoCallback(std::function< void(const std::string &)> info)
Definition: fastboot_driver.cpp:390
static const std::string RCString(RetCode rc)
Definition: fastboot_driver.cpp:394
RetCode Upload(const std::string &outfile, std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver.cpp:300
RetCode CreatePartition(const std::string &partition, const std::string &size)
Definition: fastboot_driver.cpp:82
RetCode SnapshotUpdateCommand(const std::string &command, std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver.cpp:131
RetCode SendBuffer(android::base::borrowed_fd fd, size_t size)
Definition: fastboot_driver.cpp:532
~FastBootDriver()
Definition: fastboot_driver.cpp:71
RetCode Partitions(std::vector< std::tuple< std::string, uint64_t > > *partitions)
Definition: fastboot_driver.cpp:167
static constexpr size_t TRANSPORT_CHUNK_SIZE
Definition: fastboot_driver.h:62
RetCode DownloadCommand(uint32_t size, std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver.cpp:454
RetCode Erase(const std::string &partition, std::string *response=nullptr, std::vector< std::string > *info=nullptr) override
Definition: fastboot_driver.cpp:91
RetCode Flash(const std::string &partition, std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver.cpp:96
RetCode GetVar(const std::string &key, std::string *val, std::vector< std::string > *info=nullptr) override
Definition: fastboot_driver.cpp:101
RetCode WaitForDisconnect() override
Definition: fastboot_driver.cpp:423
RetCode RebootTo(std::string target, std::string *response=nullptr, std::vector< std::string > *info=nullptr) override
Definition: fastboot_driver.cpp:115
RetCode FetchToFd(const std::string &partition, android::base::borrowed_fd fd, int64_t offset=-1, int64_t size=-1, std::string *response=nullptr, std::vector< std::string > *info=nullptr) override
Definition: fastboot_driver.cpp:366
RetCode RawCommand(const std::string &cmd, const std::string &message, std::string *response=nullptr, std::vector< std::string > *info=nullptr, int *dsize=nullptr)
Definition: fastboot_driver.cpp:428
std::string ErrnoStr(const std::string &msg)
Definition: fastboot_driver.cpp:527
std::string error_
Definition: fastboot_driver.h:159
RetCode UploadInner(const std::string &outfile, std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver.cpp:345
std::unique_ptr< Transport > transport_
Definition: fastboot_driver.h:142
Definition: fastboot_driver_interface.h:35
struct fs_info info
Definition: ext4_utils.cpp:42
char data[Size]
Definition: incremental_server.cpp:1
uint32_t size
Definition: io.h:2
Definition: fastboot_driver.cpp:58
RetCode
Definition: fastboot_driver_interface.h:26
Definition: unique_fd.h:292
Definition: fastboot_driver.h:49
std::function< void(int)> epilog
Definition: fastboot_driver.h:51
std::function< void(const std::string &)> text
Definition: fastboot_driver.h:53
std::function< void(const std::string &)> prolog
Definition: fastboot_driver.h:50
std::function< void(const std::string &)> info
Definition: fastboot_driver.h:52