2018-06-26 22:38:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstdlib>
|
2023-06-10 16:33:31 +02:00
|
|
|
#include <functional>
|
2018-06-26 22:38:35 +02:00
|
|
|
#include <limits>
|
2023-09-03 18:30:46 +02:00
|
|
|
#include <memory>
|
2018-06-26 22:38:35 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-02-04 05:06:06 +01:00
|
|
|
#include <android-base/endian.h>
|
2018-06-26 22:38:35 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2021-02-17 04:37:32 +01:00
|
|
|
#include <android-base/unique_fd.h>
|
2018-06-26 22:38:35 +02:00
|
|
|
#include <bootimg.h>
|
|
|
|
#include <sparse/sparse.h>
|
2018-08-29 19:44:33 +02:00
|
|
|
|
2023-03-30 23:59:48 +02:00
|
|
|
#include "fastboot_driver_interface.h"
|
2018-06-26 22:38:35 +02:00
|
|
|
#include "transport.h"
|
|
|
|
|
|
|
|
class Transport;
|
|
|
|
|
|
|
|
namespace fastboot {
|
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
struct DriverCallbacks {
|
|
|
|
std::function<void(const std::string&)> prolog = [](const std::string&) {};
|
|
|
|
std::function<void(int)> epilog = [](int) {};
|
|
|
|
std::function<void(const std::string&)> info = [](const std::string&) {};
|
2022-12-30 12:51:54 +01:00
|
|
|
std::function<void(const std::string&)> text = [](const std::string&) {};
|
2018-09-25 00:48:09 +02:00
|
|
|
};
|
|
|
|
|
2023-03-30 23:59:48 +02:00
|
|
|
class FastBootDriver : public IFastBootDriver {
|
2018-07-21 00:14:04 +02:00
|
|
|
friend class FastBootTest;
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
public:
|
2018-08-11 00:44:28 +02:00
|
|
|
static constexpr int RESP_TIMEOUT = 30; // 30 seconds
|
2018-06-26 22:38:35 +02:00
|
|
|
static constexpr uint32_t MAX_DOWNLOAD_SIZE = std::numeric_limits<uint32_t>::max();
|
|
|
|
static constexpr size_t TRANSPORT_CHUNK_SIZE = 1024;
|
|
|
|
|
2023-09-03 18:30:46 +02:00
|
|
|
FastBootDriver(std::unique_ptr<Transport> transport, DriverCallbacks driver_callbacks = {},
|
2018-06-26 22:38:35 +02:00
|
|
|
bool no_checks = false);
|
2018-08-28 01:47:32 +02:00
|
|
|
~FastBootDriver();
|
2018-06-26 22:38:35 +02:00
|
|
|
|
|
|
|
RetCode Boot(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
|
|
|
|
RetCode Continue(std::string* response = nullptr, std::vector<std::string>* info = nullptr);
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode CreatePartition(const std::string& partition, const std::string& size);
|
2023-03-30 23:59:48 +02:00
|
|
|
RetCode DeletePartition(const std::string& partition) override;
|
2021-03-23 00:39:13 +01:00
|
|
|
RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size,
|
2023-03-30 23:59:48 +02:00
|
|
|
std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr) override;
|
2021-03-23 00:39:13 +01:00
|
|
|
RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr,
|
2018-06-26 22:38:35 +02:00
|
|
|
std::vector<std::string>* info = nullptr);
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode Download(const std::string& name, const std::vector<char>& buf,
|
|
|
|
std::string* response = nullptr, std::vector<std::string>* info = nullptr);
|
2018-06-26 22:38:35 +02:00
|
|
|
RetCode Download(const std::vector<char>& buf, std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr);
|
2018-09-25 00:48:09 +02:00
|
|
|
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);
|
2018-08-22 18:01:04 +02:00
|
|
|
RetCode Download(sparse_file* s, bool use_crc = false, std::string* response = nullptr,
|
2018-06-26 22:38:35 +02:00
|
|
|
std::vector<std::string>* info = nullptr);
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode Erase(const std::string& partition, std::string* response = nullptr,
|
2023-03-30 23:59:48 +02:00
|
|
|
std::vector<std::string>* info = nullptr) override;
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode Flash(const std::string& partition, std::string* response = nullptr,
|
2018-06-26 22:38:35 +02:00
|
|
|
std::vector<std::string>* info = nullptr);
|
|
|
|
RetCode GetVar(const std::string& key, std::string* val,
|
2023-03-30 23:59:48 +02:00
|
|
|
std::vector<std::string>* info = nullptr) override;
|
2018-06-26 22:38:35 +02:00
|
|
|
RetCode GetVarAll(std::vector<std::string>* response);
|
2023-03-30 23:59:48 +02:00
|
|
|
RetCode Reboot(std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr) override;
|
2018-08-28 01:47:32 +02:00
|
|
|
RetCode RebootTo(std::string target, std::string* response = nullptr,
|
2023-03-30 23:59:48 +02:00
|
|
|
std::vector<std::string>* info = nullptr) override;
|
|
|
|
RetCode ResizePartition(const std::string& partition, const std::string& size) override;
|
2018-08-30 06:36:28 +02:00
|
|
|
RetCode SetActive(const std::string& slot, std::string* response = nullptr,
|
2018-06-26 22:38:35 +02:00
|
|
|
std::vector<std::string>* info = nullptr);
|
|
|
|
RetCode Upload(const std::string& outfile, std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr);
|
2019-10-22 01:45:59 +02:00
|
|
|
RetCode SnapshotUpdateCommand(const std::string& command, std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr);
|
2021-02-17 04:37:32 +01:00
|
|
|
RetCode FetchToFd(const std::string& partition, android::base::borrowed_fd fd,
|
|
|
|
int64_t offset = -1, int64_t size = -1, std::string* response = nullptr,
|
2023-07-11 01:45:54 +02:00
|
|
|
std::vector<std::string>* info = nullptr) override;
|
2018-06-26 22:38:35 +02:00
|
|
|
|
|
|
|
/* HIGHER LEVEL COMMANDS -- Composed of the commands above */
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode FlashPartition(const std::string& partition, const std::vector<char>& data);
|
2021-03-23 00:39:13 +01:00
|
|
|
RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd,
|
2023-03-30 23:59:48 +02:00
|
|
|
uint32_t sz) override;
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz,
|
|
|
|
size_t current, size_t total);
|
2018-06-26 22:38:35 +02:00
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode Partitions(std::vector<std::tuple<std::string, uint64_t>>* partitions);
|
2018-06-26 22:38:35 +02:00
|
|
|
RetCode Require(const std::string& var, const std::vector<std::string>& allowed, bool* reqmet,
|
|
|
|
bool invert = false);
|
|
|
|
|
|
|
|
/* HELPERS */
|
2018-09-25 00:48:09 +02:00
|
|
|
void SetInfoCallback(std::function<void(const std::string&)> info);
|
2018-08-01 19:57:20 +02:00
|
|
|
static const std::string RCString(RetCode rc);
|
2018-06-26 22:38:35 +02:00
|
|
|
std::string Error();
|
2023-03-30 23:59:48 +02:00
|
|
|
RetCode WaitForDisconnect() override;
|
2018-06-26 22:38:35 +02:00
|
|
|
|
2023-09-03 18:30:46 +02:00
|
|
|
void set_transport(std::unique_ptr<Transport> transport);
|
2018-08-28 01:47:32 +02:00
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode RawCommand(const std::string& cmd, const std::string& message,
|
|
|
|
std::string* response = nullptr, std::vector<std::string>* info = nullptr,
|
|
|
|
int* dsize = nullptr);
|
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
RetCode RawCommand(const std::string& cmd, std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr, int* dsize = nullptr);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RetCode DownloadCommand(uint32_t size, std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr);
|
|
|
|
RetCode HandleResponse(std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr, int* dsize = nullptr);
|
|
|
|
|
|
|
|
std::string ErrnoStr(const std::string& msg);
|
|
|
|
|
2023-09-03 18:30:46 +02:00
|
|
|
std::unique_ptr<Transport> transport_;
|
2018-06-26 22:38:35 +02:00
|
|
|
|
|
|
|
private:
|
2021-03-23 00:39:13 +01:00
|
|
|
RetCode SendBuffer(android::base::borrowed_fd fd, size_t size);
|
2018-06-26 22:38:35 +02:00
|
|
|
RetCode SendBuffer(const std::vector<char>& buf);
|
|
|
|
RetCode SendBuffer(const void* buf, size_t size);
|
|
|
|
|
|
|
|
RetCode ReadBuffer(void* buf, size_t size);
|
|
|
|
|
2018-09-25 00:48:09 +02:00
|
|
|
RetCode UploadInner(const std::string& outfile, std::string* response = nullptr,
|
|
|
|
std::vector<std::string>* info = nullptr);
|
2021-02-17 20:16:39 +01:00
|
|
|
RetCode RunAndReadBuffer(const std::string& cmd, std::string* response,
|
|
|
|
std::vector<std::string>* info,
|
|
|
|
const std::function<RetCode(const char*, uint64_t)>& write_fn);
|
2018-09-25 00:48:09 +02:00
|
|
|
|
2018-06-26 22:38:35 +02:00
|
|
|
int SparseWriteCallback(std::vector<char>& tpbuf, const char* data, size_t len);
|
|
|
|
|
|
|
|
std::string error_;
|
2018-09-25 00:48:09 +02:00
|
|
|
std::function<void(const std::string&)> prolog_;
|
|
|
|
std::function<void(int)> epilog_;
|
|
|
|
std::function<void(const std::string&)> info_;
|
2022-12-30 12:51:54 +01:00
|
|
|
std::function<void(const std::string&)> text_;
|
2018-06-26 22:38:35 +02:00
|
|
|
bool disable_checks_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace fastboot
|