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

Commit db511207 authored by Aaron Wisner's avatar Aaron Wisner
Browse files

Refactor libfastboot

This change creates a nice and clean API for issuing
fastboot commands without using the fastboot tool itself.

Test: fastboot tool itself (now using libfastboot2)
on sailfish, walleye, and other devices.
Test: flash bootloader bootloader.img
Test: flash radio radio.img
Test: -w update img.zip
Test: Manually getvar and reboot commands.

Bug: 111126621
Change-Id: I0022536b204ce0c5ad8329367fd522fa3c57877d
parent 1fefb9f1
Loading
Loading
Loading
Loading

fastboot/Android.bp

0 → 100644
+56 −0
Original line number Original line 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 Original line Diff line number Diff line
@@ -51,11 +51,11 @@ LOCAL_SRC_FILES := \
    engine.cpp \
    engine.cpp \
    fastboot.cpp \
    fastboot.cpp \
    fs.cpp \
    fs.cpp \
    protocol.cpp \
    socket.cpp \
    socket.cpp \
    tcp.cpp \
    tcp.cpp \
    udp.cpp \
    udp.cpp \
    util.cpp \
    util.cpp \
    fastboot_driver.cpp \


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


#include "bootimg_utils.h"
#include "bootimg_utils.h"


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


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

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


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


static std::vector<std::unique_ptr<Action>> action_list;
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) {
void fb_init(fastboot::FastBootDriver& fbi) {
    std::string cmd = FB_CMD_GETVAR ":" + key;
    fb = &fbi;
    auto cb = [](std::string& info) { fprintf(stderr, "(bootloader) %s\n", info.c_str()); };
    fb->SetInfoCallback(cb);
}


    char buf[FB_RESPONSE_SZ + 1];
const std::string fb_get_error() {
    memset(buf, 0, sizeof(buf));
    return fb->Error();
    if (fb_command_response(transport, cmd, buf)) {
        return false;
}
}
    *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) {
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, "");
    queue_action(OP_WAIT_FOR_DISCONNECT, "");
}
}


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


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


#include "constants.h"
#include "constants.h"


class Transport;
class Transport;
struct sparse_file;
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();
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 */
/* 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(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_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,
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_upload(const std::string& outfile);
void fb_queue_notice(const std::string& notice);
void fb_queue_notice(const std::string& notice);
void fb_queue_wait_for_disconnect(void);
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);
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 */
/* Current product */
extern char cur_product[FB_RESPONSE_SZ + 1];
extern char cur_product[FB_RESPONSE_SZ + 1];


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


Loading