Skip to content

Commit 767506f

Browse files
ccfriesGerrit Code Review
authored andcommitted
Merge "Refactor libfastboot"
2 parents a570644 + db51120 commit 767506f

15 files changed

+905
-519
lines changed

fastboot/Android.bp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
cc_library_host_static {
2+
name: "libfastboot2",
3+
4+
//host_supported: true,
5+
6+
compile_multilib: "first",
7+
srcs: [
8+
"bootimg_utils.cpp",
9+
"fs.cpp",
10+
"socket.cpp",
11+
"tcp.cpp",
12+
"udp.cpp",
13+
"util.cpp",
14+
"fastboot_driver.cpp",
15+
],
16+
17+
static_libs: [
18+
"libziparchive",
19+
"libsparse",
20+
"libutils",
21+
"liblog",
22+
"libz",
23+
"libdiagnose_usb",
24+
"libbase",
25+
"libcutils",
26+
"libgtest",
27+
"libgtest_main",
28+
"libbase",
29+
"libadb_host"
30+
],
31+
32+
header_libs: [
33+
"bootimg_headers"
34+
],
35+
36+
export_header_lib_headers: [
37+
"bootimg_headers"
38+
],
39+
40+
41+
target: {
42+
linux: {
43+
srcs: ["usb_linux.cpp"],
44+
},
45+
},
46+
47+
cflags: [
48+
"-Wall",
49+
"-Wextra",
50+
"-Werror",
51+
"-Wunreachable-code",
52+
],
53+
54+
export_include_dirs: ["."],
55+
56+
}

fastboot/Android.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ LOCAL_SRC_FILES := \
5050
bootimg_utils.cpp \
5151
engine.cpp \
5252
fastboot.cpp \
53-
fs.cpp\
54-
protocol.cpp \
53+
fs.cpp \
5554
socket.cpp \
5655
tcp.cpp \
5756
udp.cpp \
5857
util.cpp \
58+
fastboot_driver.cpp \
5959

6060
LOCAL_SRC_FILES_darwin := usb_osx.cpp
6161
LOCAL_SRC_FILES_linux := usb_linux.cpp

fastboot/bootimg_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include "bootimg_utils.h"
3030

31-
#include "fastboot.h"
31+
#include "util.h"
3232

3333
#include <stdio.h>
3434
#include <stdlib.h>

fastboot/engine.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2626
* SUCH DAMAGE.
2727
*/
28-
29-
#include "fastboot.h"
28+
#include "engine.h"
3029

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

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

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

85-
char buf[FB_RESPONSE_SZ + 1];
86-
memset(buf, 0, sizeof(buf));
87-
if (fb_command_response(transport, cmd, buf)) {
88-
return false;
89-
}
90-
*value = buf;
91-
return true;
88+
const std::string fb_get_error() {
89+
return fb->Error();
90+
}
91+
92+
bool fb_getvar(const std::string& key, std::string* value) {
93+
return !fb->GetVar(key, value);
9294
}
9395

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

313-
int64_t fb_execute_queue(Transport* transport) {
315+
int64_t fb_execute_queue() {
314316
int64_t status = 0;
315317
for (auto& a : action_list) {
316318
a->start = now();
@@ -319,33 +321,34 @@ int64_t fb_execute_queue(Transport* transport) {
319321
verbose("\n");
320322
}
321323
if (a->op == OP_DOWNLOAD) {
322-
status = fb_download_data(transport, a->data, a->size);
324+
char* cbuf = static_cast<char*>(a->data);
325+
status = fb->Download(cbuf, a->size);
323326
status = a->func(*a, status, status ? fb_get_error().c_str() : "");
324327
if (status) break;
325328
} else if (a->op == OP_DOWNLOAD_FD) {
326-
status = fb_download_data_fd(transport, a->fd, a->size);
329+
status = fb->Download(a->fd, a->size);
327330
status = a->func(*a, status, status ? fb_get_error().c_str() : "");
328331
if (status) break;
329332
} else if (a->op == OP_COMMAND) {
330-
status = fb_command(transport, a->cmd);
333+
status = fb->RawCommand(a->cmd);
331334
status = a->func(*a, status, status ? fb_get_error().c_str() : "");
332335
if (status) break;
333336
} else if (a->op == OP_QUERY) {
334-
char resp[FB_RESPONSE_SZ + 1] = {};
335-
status = fb_command_response(transport, a->cmd, resp);
336-
status = a->func(*a, status, status ? fb_get_error().c_str() : resp);
337+
std::string resp;
338+
status = fb->RawCommand(a->cmd, &resp);
339+
status = a->func(*a, status, status ? fb_get_error().c_str() : resp.c_str());
337340
if (status) break;
338341
} else if (a->op == OP_NOTICE) {
339342
// We already showed the notice because it's in `Action::msg`.
340343
fprintf(stderr, "\n");
341344
} else if (a->op == OP_DOWNLOAD_SPARSE) {
342-
status = fb_download_data_sparse(transport, reinterpret_cast<sparse_file*>(a->data));
345+
status = fb->Download(reinterpret_cast<sparse_file*>(a->data));
343346
status = a->func(*a, status, status ? fb_get_error().c_str() : "");
344347
if (status) break;
345348
} else if (a->op == OP_WAIT_FOR_DISCONNECT) {
346-
transport->WaitForDisconnect();
349+
fb->WaitForDisconnect();
347350
} else if (a->op == OP_UPLOAD) {
348-
status = fb_upload_data(transport, reinterpret_cast<char*>(a->data));
351+
status = fb->Upload(reinterpret_cast<const char*>(a->data));
349352
status = a->func(*a, status, status ? fb_get_error().c_str() : "");
350353
} else {
351354
die("unknown action: %d", a->op);

fastboot/fastboot.h renamed to fastboot/engine.h

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,24 @@
3434
#include <string>
3535

3636
#include <bootimg.h>
37+
#include "fastboot_driver.h"
38+
#include "util.h"
3739

3840
#include "constants.h"
3941

4042
class Transport;
4143
struct sparse_file;
4244

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

47+
//#define FB_COMMAND_SZ (fastboot::FB_COMMAND_SZ)
48+
//#define FB_RESPONSE_SZ (fastboot::FB_RESPONSE_SZ)
49+
5250
/* engine.c - high level command queue engine */
53-
bool fb_getvar(Transport* transport, const std::string& key, std::string* value);
51+
52+
void fb_init(fastboot::FastBootDriver& fbi);
53+
54+
bool fb_getvar(const std::string& key, std::string* value);
5455
void fb_queue_flash(const std::string& partition, void* data, uint32_t sz);
5556
void fb_queue_flash_fd(const std::string& partition, int fd, uint32_t sz);
5657
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);
6869
void fb_queue_upload(const std::string& outfile);
6970
void fb_queue_notice(const std::string& notice);
7071
void fb_queue_wait_for_disconnect(void);
71-
int64_t fb_execute_queue(Transport* transport);
72+
int64_t fb_execute_queue();
7273
void fb_set_active(const std::string& slot);
7374

74-
/* util stuff */
75-
double now();
76-
char* xstrdup(const char*);
77-
void set_verbose();
78-
79-
// These printf-like functions are implemented in terms of vsnprintf, so they
80-
// use the same attribute for compile-time format string checking.
81-
void die(const char* fmt, ...) __attribute__((__noreturn__))
82-
__attribute__((__format__(__printf__, 1, 2)));
83-
void verbose(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
84-
8575
/* Current product */
8676
extern char cur_product[FB_RESPONSE_SZ + 1];
8777

88-
class FastBoot {
78+
class FastBootTool {
8979
public:
9080
int Main(int argc, char* argv[]);
9181

0 commit comments

Comments
 (0)