Merge "Move fastboot to C++."

This commit is contained in:
Elliott Hughes 2015-06-24 04:23:28 +00:00 committed by Gerrit Code Review
commit dc80d03eab
8 changed files with 27 additions and 38 deletions

View file

@ -21,7 +21,7 @@ include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../mkbootimg \ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../mkbootimg \
$(LOCAL_PATH)/../../extras/ext4_utils \ $(LOCAL_PATH)/../../extras/ext4_utils \
$(LOCAL_PATH)/../../extras/f2fs_utils $(LOCAL_PATH)/../../extras/f2fs_utils
LOCAL_SRC_FILES := protocol.c engine.c bootimg_utils.cpp fastboot.cpp util.c fs.c LOCAL_SRC_FILES := protocol.cpp engine.cpp bootimg_utils.cpp fastboot.cpp util.cpp fs.cpp
LOCAL_MODULE := fastboot LOCAL_MODULE := fastboot
LOCAL_MODULE_TAGS := debug LOCAL_MODULE_TAGS := debug
LOCAL_CONLYFLAGS += -std=gnu99 LOCAL_CONLYFLAGS += -std=gnu99
@ -30,7 +30,7 @@ LOCAL_CFLAGS += -Wall -Wextra -Werror -Wunreachable-code
LOCAL_CFLAGS += -DFASTBOOT_REVISION='"$(fastboot_version)"' LOCAL_CFLAGS += -DFASTBOOT_REVISION='"$(fastboot_version)"'
ifeq ($(HOST_OS),linux) ifeq ($(HOST_OS),linux)
LOCAL_SRC_FILES += usb_linux.c util_linux.c LOCAL_SRC_FILES += usb_linux.cpp util_linux.cpp
endif endif
ifeq ($(HOST_OS),darwin) ifeq ($(HOST_OS),darwin)
@ -97,10 +97,9 @@ endif
$(call dist-for-goals,dist_files sdk,$(my_dist_files)) $(call dist-for-goals,dist_files sdk,$(my_dist_files))
my_dist_files := my_dist_files :=
ifeq ($(HOST_OS),linux) ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_SRC_FILES := usbtest.c usb_linux.c util.c LOCAL_SRC_FILES := usbtest.cpp usb_linux.cpp util.cpp
LOCAL_MODULE := usbtest LOCAL_MODULE := usbtest
LOCAL_CFLAGS := -Werror LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_EXECUTABLE) include $(BUILD_HOST_EXECUTABLE)

View file

@ -45,10 +45,6 @@
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#ifndef __unused
#define __unused __attribute__((__unused__))
#endif
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define OP_DOWNLOAD 1 #define OP_DOWNLOAD 1
@ -73,7 +69,7 @@ struct Action
unsigned size; unsigned size;
const char *msg; const char *msg;
int (*func)(Action *a, int status, char *resp); int (*func)(Action* a, int status, const char* resp);
double start; double start;
}; };
@ -121,8 +117,7 @@ int fb_format_supported(usb_handle *usb, const char *partition, const char *type
return !!fs_get_generator(fs_type); return !!fs_get_generator(fs_type);
} }
static int cb_default(Action *a, int status, char *resp) static int cb_default(Action* a, int status, const char* resp) {
{
if (status) { if (status) {
fprintf(stderr,"FAILED (%s)\n", resp); fprintf(stderr,"FAILED (%s)\n", resp);
} else { } else {
@ -135,12 +130,11 @@ static int cb_default(Action *a, int status, char *resp)
static Action *queue_action(unsigned op, const char *fmt, ...) static Action *queue_action(unsigned op, const char *fmt, ...)
{ {
Action *a;
va_list ap; va_list ap;
size_t cmdsize; size_t cmdsize;
a = calloc(1, sizeof(Action)); Action* a = reinterpret_cast<Action*>(calloc(1, sizeof(Action)));
if (a == 0) die("out of memory"); if (a == nullptr) die("out of memory");
va_start(ap, fmt); va_start(ap, fmt);
cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap); cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
@ -198,8 +192,7 @@ void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
a->msg = mkmsg("writing '%s'", ptn); a->msg = mkmsg("writing '%s'", ptn);
} }
static int match(char *str, const char **value, unsigned count) static int match(const char* str, const char** value, unsigned count) {
{
unsigned n; unsigned n;
for (n = 0; n < count; n++) { for (n = 0; n < count; n++) {
@ -222,9 +215,9 @@ static int match(char *str, const char **value, unsigned count)
static int cb_check(Action *a, int status, char *resp, int invert) static int cb_check(Action* a, int status, const char* resp, int invert)
{ {
const char **value = a->data; const char** value = reinterpret_cast<const char**>(a->data);
unsigned count = a->size; unsigned count = a->size;
unsigned n; unsigned n;
int yes; int yes;
@ -265,18 +258,16 @@ static int cb_check(Action *a, int status, char *resp, int invert)
return -1; return -1;
} }
static int cb_require(Action *a, int status, char *resp) static int cb_require(Action*a, int status, const char* resp) {
{
return cb_check(a, status, resp, 0); return cb_check(a, status, resp, 0);
} }
static int cb_reject(Action *a, int status, char *resp) static int cb_reject(Action* a, int status, const char* resp) {
{
return cb_check(a, status, resp, 1); return cb_check(a, status, resp, 1);
} }
void fb_queue_require(const char *prod, const char *var, void fb_queue_require(const char *prod, const char *var,
int invert, unsigned nvalues, const char **value) int invert, unsigned nvalues, const char **value)
{ {
Action *a; Action *a;
a = queue_action(OP_QUERY, "getvar:%s", var); a = queue_action(OP_QUERY, "getvar:%s", var);
@ -285,11 +276,10 @@ void fb_queue_require(const char *prod, const char *var,
a->size = nvalues; a->size = nvalues;
a->msg = mkmsg("checking %s", var); a->msg = mkmsg("checking %s", var);
a->func = invert ? cb_reject : cb_require; a->func = invert ? cb_reject : cb_require;
if (a->data == 0) die("out of memory"); if (a->data == nullptr) die("out of memory");
} }
static int cb_display(Action *a, int status, char *resp) static int cb_display(Action* a, int status, const char* resp) {
{
if (status) { if (status) {
fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp); fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
return status; return status;
@ -303,17 +293,16 @@ void fb_queue_display(const char *var, const char *prettyname)
Action *a; Action *a;
a = queue_action(OP_QUERY, "getvar:%s", var); a = queue_action(OP_QUERY, "getvar:%s", var);
a->data = strdup(prettyname); a->data = strdup(prettyname);
if (a->data == 0) die("out of memory"); if (a->data == nullptr) die("out of memory");
a->func = cb_display; a->func = cb_display;
} }
static int cb_save(Action *a, int status, char *resp) static int cb_save(Action* a, int status, const char* resp) {
{
if (status) { if (status) {
fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp); fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
return status; return status;
} }
strncpy(a->data, resp, a->size); strncpy(reinterpret_cast<char*>(a->data), resp, a->size);
return 0; return 0;
} }
@ -326,8 +315,7 @@ void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
a->func = cb_save; a->func = cb_save;
} }
static int cb_do_nothing(Action *a __unused, int status __unused, char *resp __unused) static int cb_do_nothing(Action*, int , const char*) {
{
fprintf(stderr,"\n"); fprintf(stderr,"\n");
return 0; return 0;
} }
@ -398,7 +386,7 @@ int fb_execute_queue(usb_handle *usb)
} else if (a->op == OP_NOTICE) { } else if (a->op == OP_NOTICE) {
fprintf(stderr,"%s\n",(char*)a->data); fprintf(stderr,"%s\n",(char*)a->data);
} else if (a->op == OP_DOWNLOAD_SPARSE) { } else if (a->op == OP_DOWNLOAD_SPARSE) {
status = fb_download_data_sparse(usb, a->data); status = fb_download_data_sparse(usb, reinterpret_cast<sparse_file*>(a->data));
status = a->func(a, status, status ? fb_get_error() : ""); status = a->func(a, status, status ? fb_get_error() : "");
if (status) break; if (status) break;
} else if (a->op == OP_WAIT_FOR_DISCONNECT) { } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
@ -414,5 +402,5 @@ int fb_execute_queue(usb_handle *usb)
int fb_queue_is_empty(void) int fb_queue_is_empty(void)
{ {
return (action_list == NULL); return (action_list == nullptr);
} }

View file

@ -38,7 +38,7 @@ static int generate_f2fs_image(int fd, long long partSize)
static const struct fs_generator { static const struct fs_generator {
char *fs_type; //must match what fastboot reports for partition type const char* fs_type; //must match what fastboot reports for partition type
int (*generate)(int fd, long long partSize); //returns 0 or error value int (*generate)(int fd, long long partSize); //returns 0 or error value
} generators[] = { } generators[] = {

View file

@ -223,9 +223,9 @@ static int usb_buf_len;
static int fb_download_data_sparse_write(void *priv, const void *data, int len) static int fb_download_data_sparse_write(void *priv, const void *data, int len)
{ {
int r; int r;
usb_handle *usb = priv; usb_handle* usb = reinterpret_cast<usb_handle*>(priv);
int to_write; int to_write;
const char *ptr = data; const char* ptr = reinterpret_cast<const char*>(data);
if (usb_buf_len) { if (usb_buf_len) {
to_write = min(USB_BUF_SIZE - usb_buf_len, len); to_write = min(USB_BUF_SIZE - usb_buf_len, len);

View file

@ -340,7 +340,7 @@ static usb_handle *find_usb_device(const char *base, ifc_match_func callback)
if(filter_usb_device(de->d_name, desc, n, writable, callback, if(filter_usb_device(de->d_name, desc, n, writable, callback,
&in, &out, &ifc) == 0) { &in, &out, &ifc) == 0) {
usb = calloc(1, sizeof(usb_handle)); usb = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
strcpy(usb->fname, devname); strcpy(usb->fname, devname);
usb->ep_in = in; usb->ep_in = in;
usb->ep_out = out; usb->ep_out = out;

View file

@ -26,6 +26,8 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include "fastboot.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>