Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 40cd9e03 authored by Chris Fries's avatar Chris Fries Committed by android-build-merger
Browse files

Merge "Refactor libfastboot"

am: 767506fc

Change-Id: I1e20f30872969dd847fe67a23ebc5975dcb743e7
parents 6a928587 767506fc
Loading
Loading
Loading
Loading

fastboot/Android.bp

0 → 100644
+56 −0
Original line number Diff line number Diff line
cc_library_host_static {
    name: "libfastboot2",

    //host_supported: true,

    compile_multilib: "first",
    srcs: [
      "bootimg_utils.cpp",
      "fs.cpp",
      "socket.cpp",
      "tcp.cpp",
      "udp.cpp",
      "util.cpp",
      "fastboot_driver.cpp",
    ],

    static_libs: [
      "libziparchive",
      "libsparse",
      "libutils",
      "liblog",
      "libz",
      "libdiagnose_usb",
      "libbase",
      "libcutils",
      "libgtest",
      "libgtest_main",
      "libbase",
      "libadb_host"
    ],

    header_libs: [
      "bootimg_headers"
    ],

    export_header_lib_headers: [
      "bootimg_headers"
    ],


    target: {
      linux: {
        srcs: ["usb_linux.cpp"],
      },
    },

    cflags: [
      "-Wall",
      "-Wextra",
      "-Werror",
      "-Wunreachable-code",
    ],

    export_include_dirs: ["."],

}
+2 −2
Original line number Diff line number Diff line
@@ -51,11 +51,11 @@ LOCAL_SRC_FILES := \
    engine.cpp \
    fastboot.cpp \
    fs.cpp \
    protocol.cpp \
    socket.cpp \
    tcp.cpp \
    udp.cpp \
    util.cpp \
    fastboot_driver.cpp \

LOCAL_SRC_FILES_darwin := usb_osx.cpp
LOCAL_SRC_FILES_linux := usb_linux.cpp
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@

#include "bootimg_utils.h"

#include "fastboot.h"
#include "util.h"

#include <stdio.h>
#include <stdlib.h>
+24 −21
Original line number Diff line number Diff line
@@ -25,8 +25,7 @@
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "fastboot.h"
#include "engine.h"

#include <errno.h>
#include <stdarg.h>
@@ -78,17 +77,20 @@ struct Action {
};

static std::vector<std::unique_ptr<Action>> action_list;
static fastboot::FastBootDriver* fb = nullptr;

bool fb_getvar(Transport* transport, const std::string& key, std::string* value) {
    std::string cmd = FB_CMD_GETVAR ":" + key;
void fb_init(fastboot::FastBootDriver& fbi) {
    fb = &fbi;
    auto cb = [](std::string& info) { fprintf(stderr, "(bootloader) %s\n", info.c_str()); };
    fb->SetInfoCallback(cb);
}

    char buf[FB_RESPONSE_SZ + 1];
    memset(buf, 0, sizeof(buf));
    if (fb_command_response(transport, cmd, buf)) {
        return false;
const std::string fb_get_error() {
    return fb->Error();
}
    *value = buf;
    return true;

bool fb_getvar(const std::string& key, std::string* value) {
    return !fb->GetVar(key, value);
}

static int cb_default(Action& a, int status, const char* resp) {
@@ -310,7 +312,7 @@ void fb_queue_wait_for_disconnect() {
    queue_action(OP_WAIT_FOR_DISCONNECT, "");
}

int64_t fb_execute_queue(Transport* transport) {
int64_t fb_execute_queue() {
    int64_t status = 0;
    for (auto& a : action_list) {
        a->start = now();
@@ -319,33 +321,34 @@ int64_t fb_execute_queue(Transport* transport) {
            verbose("\n");
        }
        if (a->op == OP_DOWNLOAD) {
            status = fb_download_data(transport, a->data, a->size);
            char* cbuf = static_cast<char*>(a->data);
            status = fb->Download(cbuf, a->size);
            status = a->func(*a, status, status ? fb_get_error().c_str() : "");
            if (status) break;
        } else if (a->op == OP_DOWNLOAD_FD) {
            status = fb_download_data_fd(transport, a->fd, a->size);
            status = fb->Download(a->fd, a->size);
            status = a->func(*a, status, status ? fb_get_error().c_str() : "");
            if (status) break;
        } else if (a->op == OP_COMMAND) {
            status = fb_command(transport, a->cmd);
            status = fb->RawCommand(a->cmd);
            status = a->func(*a, status, status ? fb_get_error().c_str() : "");
            if (status) break;
        } else if (a->op == OP_QUERY) {
            char resp[FB_RESPONSE_SZ + 1] = {};
            status = fb_command_response(transport, a->cmd, resp);
            status = a->func(*a, status, status ? fb_get_error().c_str() : resp);
            std::string resp;
            status = fb->RawCommand(a->cmd, &resp);
            status = a->func(*a, status, status ? fb_get_error().c_str() : resp.c_str());
            if (status) break;
        } else if (a->op == OP_NOTICE) {
            // We already showed the notice because it's in `Action::msg`.
            fprintf(stderr, "\n");
        } else if (a->op == OP_DOWNLOAD_SPARSE) {
            status = fb_download_data_sparse(transport, reinterpret_cast<sparse_file*>(a->data));
            status = fb->Download(reinterpret_cast<sparse_file*>(a->data));
            status = a->func(*a, status, status ? fb_get_error().c_str() : "");
            if (status) break;
        } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
            transport->WaitForDisconnect();
            fb->WaitForDisconnect();
        } else if (a->op == OP_UPLOAD) {
            status = fb_upload_data(transport, reinterpret_cast<char*>(a->data));
            status = fb->Upload(reinterpret_cast<const char*>(a->data));
            status = a->func(*a, status, status ? fb_get_error().c_str() : "");
        } else {
            die("unknown action: %d", a->op);
+11 −21
Original line number Diff line number Diff line
@@ -34,23 +34,24 @@
#include <string>

#include <bootimg.h>
#include "fastboot_driver.h"
#include "util.h"

#include "constants.h"

class Transport;
struct sparse_file;

/* protocol.c - fastboot protocol */
int fb_command(Transport* transport, const std::string& cmd);
int fb_command_response(Transport* transport, const std::string& cmd, char* response);
int64_t fb_download_data(Transport* transport, const void* data, uint32_t size);
int64_t fb_download_data_fd(Transport* transport, int fd, uint32_t size);
int fb_download_data_sparse(Transport* transport, struct sparse_file* s);
int64_t fb_upload_data(Transport* transport, const char* outfile);
const std::string fb_get_error();

//#define FB_COMMAND_SZ (fastboot::FB_COMMAND_SZ)
//#define FB_RESPONSE_SZ (fastboot::FB_RESPONSE_SZ)

/* engine.c - high level command queue engine */
bool fb_getvar(Transport* transport, const std::string& key, std::string* value);

void fb_init(fastboot::FastBootDriver& fbi);

bool fb_getvar(const std::string& key, std::string* value);
void fb_queue_flash(const std::string& partition, void* data, uint32_t sz);
void fb_queue_flash_fd(const std::string& partition, int fd, uint32_t sz);
void fb_queue_flash_sparse(const std::string& partition, struct sparse_file* s, uint32_t sz,
@@ -68,24 +69,13 @@ void fb_queue_download_fd(const std::string& name, int fd, uint32_t sz);
void fb_queue_upload(const std::string& outfile);
void fb_queue_notice(const std::string& notice);
void fb_queue_wait_for_disconnect(void);
int64_t fb_execute_queue(Transport* transport);
int64_t fb_execute_queue();
void fb_set_active(const std::string& slot);

/* util stuff */
double now();
char* xstrdup(const char*);
void set_verbose();

// These printf-like functions are implemented in terms of vsnprintf, so they
// use the same attribute for compile-time format string checking.
void die(const char* fmt, ...) __attribute__((__noreturn__))
__attribute__((__format__(__printf__, 1, 2)));
void verbose(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2)));

/* Current product */
extern char cur_product[FB_RESPONSE_SZ + 1];

class FastBoot {
class FastBootTool {
  public:
    int Main(int argc, char* argv[]);

Loading