Android-cuttlefish cvd tool
fastboot_driver_interface.h
Go to the documentation of this file.
1//
2// Copyright (C) 2023 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16#pragma once
17
18#include <functional>
19#include <string>
20#include <vector>
21
22#include <sparse/sparse.h>
23#include "android-base/unique_fd.h"
24
25class Transport;
26
27namespace fastboot {
28
29enum RetCode : int {
36};
37
39 public:
40 template <typename F>
41 auto RunWithNoCrash(F&& functor) {
42 const auto old_setting = GetCrashOnError();
43 SetCrashOnError(false);
44 auto ret = functor();
45 SetCrashOnError(old_setting);
46 return ret;
47 }
48 RetCode virtual FlashPartition(const std::string& partition, const std::vector<char>& data) = 0;
49 RetCode virtual FlashPartition(const std::string& partition, android::base::borrowed_fd fd,
50 uint32_t sz) = 0;
51 RetCode virtual DeletePartition(const std::string& partition) = 0;
53 RetCode virtual Reboot(std::string* response = nullptr,
54 std::vector<std::string>* info = nullptr) = 0;
55
56 RetCode virtual RebootTo(std::string target, std::string* response = nullptr,
57 std::vector<std::string>* info = nullptr) = 0;
58 RetCode virtual GetVar(const std::string& key, std::string* val,
59 std::vector<std::string>* info = nullptr) = 0;
60 RetCode virtual FetchToFd(const std::string& partition, android::base::borrowed_fd fd,
61 int64_t offset = -1, int64_t size = -1,
62 std::string* response = nullptr,
63 std::vector<std::string>* info = nullptr) = 0;
64
65 RetCode virtual Fetch(const std::string& partition,
66 const std::function<RetCode(const char*, uint64_t)>& write_fn,
67 int64_t offset = -1, int64_t size = -1, std::string* response = nullptr,
68 std::vector<std::string>* info = nullptr) = 0;
69
70 // Fetch data to containers such as std::vector, container must support
71 // insertion at end of container.
72 template <typename T>
73 RetCode Fetch(const std::string& partition, T* container, int64_t offset = -1,
74 int64_t size = -1, std::string* response = nullptr,
75 std::vector<std::string>* info = nullptr) {
76 return Fetch(
77 partition,
78 [container](const char* data, uint64_t size) {
79 container->insert(container->end(), data, data + size);
80 return fastboot::SUCCESS;
81 },
82 offset, size, response, info);
83 }
84 RetCode FetchAtOffset(const std::string& partition, void* data, size_t size, int64_t offset) {
85 auto bytes = reinterpret_cast<char*>(data);
86 return Fetch(
87 partition,
88 [bytes](const char* chunk, uint64_t chunk_size) mutable {
89 memcpy(bytes, chunk, chunk_size);
90 bytes += chunk_size;
91 return SUCCESS;
92 },
93 offset, size);
94 }
95 RetCode virtual Download(const std::string& name, android::base::borrowed_fd fd, size_t size,
96 std::string* response = nullptr,
97 std::vector<std::string>* info = nullptr) = 0;
98 RetCode virtual Download(android::base::borrowed_fd fd, size_t size,
99 std::string* response = nullptr,
100 std::vector<std::string>* info = nullptr) = 0;
101 RetCode virtual Download(const std::string& name, const std::vector<char>& buf,
102 std::string* response = nullptr,
103 std::vector<std::string>* info = nullptr) = 0;
104 RetCode virtual Download(const std::vector<char>& buf, std::string* response = nullptr,
105 std::vector<std::string>* info = nullptr) = 0;
106 RetCode virtual Download(const std::string& partition, struct sparse_file* s, uint32_t sz,
107 size_t current, size_t total, bool use_crc,
108 std::string* response = nullptr,
109 std::vector<std::string>* info = nullptr) = 0;
110 RetCode virtual RawCommand(const std::string& cmd, const std::string& message,
111 std::string* response = nullptr,
112 std::vector<std::string>* info = nullptr, int* dsize = nullptr) = 0;
113 virtual RetCode RawCommand(const std::string& cmd, std::string* response = nullptr,
114 std::vector<std::string>* info = nullptr, int* dsize = nullptr) = 0;
115 RetCode virtual ResizePartition(const std::string& partition, const std::string& size) = 0;
116 RetCode virtual Erase(const std::string& partition, std::string* response = nullptr,
117 std::vector<std::string>* info = nullptr) = 0;
118 RetCode virtual Flash(const std::string& partition, std::string* response = nullptr,
119 std::vector<std::string>* info = nullptr) = 0;
120 virtual ~IFastBootDriver() = default;
121 virtual std::string Error() = 0;
122 virtual RetCode SetActive(const std::string& slot, std::string* response = nullptr,
123 std::vector<std::string>* info = nullptr) = 0;
124 virtual void set_transport(std::unique_ptr<Transport> transport) = 0;
125 virtual RetCode SnapshotUpdateCommand(const std::string& command,
126 std::string* response = nullptr,
127 std::vector<std::string>* info = nullptr) = 0;
128 virtual RetCode Boot(std::string* response = nullptr,
129 std::vector<std::string>* info = nullptr) = 0;
130 virtual RetCode Continue(std::string* response = nullptr,
131 std::vector<std::string>* info = nullptr) = 0;
132 virtual RetCode CreatePartition(const std::string& partition, const std::string& size) = 0;
133 virtual RetCode Upload(const std::string& outfile, std::string* response = nullptr,
134 std::vector<std::string>* info = nullptr) = 0;
135
136 protected:
137 friend struct NoCrashGuard;
138 virtual void SetCrashOnError(bool) = 0;
139 virtual bool GetCrashOnError() const = 0;
140};
141
142// By default, IFastBootDriver's various methods would crash if a device reports
143// failure status for a command. While |NoCrashGuard| is in scope, the specified
144// IFastBootDriver instance would no longer crash on error.
146 [[nodiscard("RAII guard should not be ignored")]] constexpr explicit NoCrashGuard(
147 IFastBootDriver* fb)
148 : driver(fb), old_setting(fb->GetCrashOnError()) {
149 fb->SetCrashOnError(false);
150 }
151 constexpr ~NoCrashGuard() {
152 if (driver) {
154 }
155 }
156
157 private:
159 const bool old_setting;
160};
161} // namespace fastboot
Definition: transport.h:23
Definition: fastboot_driver_interface.h:38
RetCode FetchAtOffset(const std::string &partition, void *data, size_t size, int64_t offset)
Definition: fastboot_driver_interface.h:84
virtual RetCode RawCommand(const std::string &cmd, std::string *response=nullptr, std::vector< std::string > *info=nullptr, int *dsize=nullptr)=0
virtual RetCode Erase(const std::string &partition, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode Fetch(const std::string &partition, const std::function< RetCode(const char *, uint64_t)> &write_fn, int64_t offset=-1, int64_t size=-1, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode FlashPartition(const std::string &partition, android::base::borrowed_fd fd, uint32_t sz)=0
virtual RetCode Download(const std::string &name, android::base::borrowed_fd fd, size_t size, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode Reboot(std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode SnapshotUpdateCommand(const std::string &command, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode Flash(const std::string &partition, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode Continue(std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual std::string Error()=0
RetCode Fetch(const std::string &partition, T *container, int64_t offset=-1, int64_t size=-1, std::string *response=nullptr, std::vector< std::string > *info=nullptr)
Definition: fastboot_driver_interface.h:73
virtual RetCode SetActive(const std::string &slot, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual void SetCrashOnError(bool)=0
virtual RetCode Download(const std::vector< char > &buf, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode Download(android::base::borrowed_fd fd, size_t size, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode RawCommand(const std::string &cmd, const std::string &message, std::string *response=nullptr, std::vector< std::string > *info=nullptr, int *dsize=nullptr)=0
virtual RetCode ResizePartition(const std::string &partition, const std::string &size)=0
virtual RetCode RebootTo(std::string target, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual bool GetCrashOnError() const =0
virtual RetCode DeletePartition(const std::string &partition)=0
virtual RetCode Download(const std::string &name, const std::vector< char > &buf, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode Download(const std::string &partition, struct sparse_file *s, uint32_t sz, size_t current, size_t total, bool use_crc, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode Upload(const std::string &outfile, std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
virtual RetCode Boot(std::string *response=nullptr, std::vector< std::string > *info=nullptr)=0
auto RunWithNoCrash(F &&functor)
Definition: fastboot_driver_interface.h:41
virtual RetCode FlashPartition(const std::string &partition, const std::vector< char > &data)=0
virtual RetCode CreatePartition(const std::string &partition, const std::string &size)=0
virtual ~IFastBootDriver()=default
virtual RetCode GetVar(const std::string &key, std::string *val, std::vector< std::string > *info=nullptr)=0
virtual void set_transport(std::unique_ptr< Transport > transport)=0
virtual 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)=0
virtual RetCode WaitForDisconnect()=0
struct fs_info info
Definition: ext4_utils.cpp:42
uint32_t size
Definition: io.h:2
char data[Size]
Definition: incremental_server.cpp:1
void * memcpy(void *dest, const void *src, size_t n)
Definition: mem_overrides.cpp:8
Definition: fastboot_driver.cpp:58
RetCode
Definition: fastboot_driver_interface.h:29
@ BAD_ARG
Definition: fastboot_driver_interface.h:31
@ IO_ERROR
Definition: fastboot_driver_interface.h:32
@ BAD_DEV_RESP
Definition: fastboot_driver_interface.h:33
@ SUCCESS
Definition: fastboot_driver_interface.h:30
@ TIMEOUT
Definition: fastboot_driver_interface.h:35
@ DEVICE_FAIL
Definition: fastboot_driver_interface.h:34
Definition: fastboot_driver_interface.h:145
constexpr ~NoCrashGuard()
Definition: fastboot_driver_interface.h:151
const bool old_setting
Definition: fastboot_driver_interface.h:159
IFastBootDriver * driver
Definition: fastboot_driver_interface.h:158
constexpr NoCrashGuard(IFastBootDriver *fb)
Definition: fastboot_driver_interface.h:146