diff --git a/fastboot/bootimg_utils.cpp b/fastboot/bootimg_utils.cpp index d8905a617..c1028ef3c 100644 --- a/fastboot/bootimg_utils.cpp +++ b/fastboot/bootimg_utils.cpp @@ -32,32 +32,27 @@ #include #include -void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline) +void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline) { strcpy((char*) h->cmdline, cmdline); } -boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset, - void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset, - void *second, unsigned second_size, unsigned second_offset, - unsigned page_size, unsigned base, unsigned tags_offset, - unsigned *bootimg_size) +boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset, + void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset, + void* second, int64_t second_size, off_t second_offset, + size_t page_size, size_t base, off_t tags_offset, + int64_t* bootimg_size) { - unsigned kernel_actual; - unsigned ramdisk_actual; - unsigned second_actual; - unsigned page_mask; + size_t page_mask = page_size - 1; - page_mask = page_size - 1; - - kernel_actual = (kernel_size + page_mask) & (~page_mask); - ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask); - second_actual = (second_size + page_mask) & (~page_mask); + int64_t kernel_actual = (kernel_size + page_mask) & (~page_mask); + int64_t ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask); + int64_t second_actual = (second_size + page_mask) & (~page_mask); *bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual; boot_img_hdr* hdr = reinterpret_cast(calloc(*bootimg_size, 1)); - if (hdr == 0) { + if (hdr == nullptr) { return hdr; } @@ -74,12 +69,9 @@ boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offs hdr->page_size = page_size; + memcpy(hdr->magic + page_size, kernel, kernel_size); + memcpy(hdr->magic + page_size + kernel_actual, ramdisk, ramdisk_size); + memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, second, second_size); - memcpy(hdr->magic + page_size, - kernel, kernel_size); - memcpy(hdr->magic + page_size + kernel_actual, - ramdisk, ramdisk_size); - memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, - second, second_size); return hdr; } diff --git a/fastboot/bootimg_utils.h b/fastboot/bootimg_utils.h index b1a86cda7..fcc866297 100644 --- a/fastboot/bootimg_utils.h +++ b/fastboot/bootimg_utils.h @@ -30,20 +30,14 @@ #define _FASTBOOT_BOOTIMG_UTILS_H_ #include +#include +#include -#if defined(__cplusplus) -extern "C" { -#endif - -void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline); -boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset, - void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset, - void *second, unsigned second_size, unsigned second_offset, - unsigned page_size, unsigned base, unsigned tags_offset, - unsigned *bootimg_size); - -#if defined(__cplusplus) -} -#endif +void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline); +boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset, + void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset, + void* second, int64_t second_size, off_t second_offset, + size_t page_size, size_t base, off_t tags_offset, + int64_t* bootimg_size); #endif diff --git a/fastboot/engine.cpp b/fastboot/engine.cpp index 66b8140f3..bda6ad8e1 100644 --- a/fastboot/engine.cpp +++ b/fastboot/engine.cpp @@ -30,8 +30,6 @@ #include "fs.h" #include -#include -#include #include #include #include @@ -39,12 +37,6 @@ #include #include -#ifdef USE_MINGW -#include -#else -#include -#endif - #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) #define OP_DOWNLOAD 1 @@ -58,15 +50,17 @@ typedef struct Action Action; #define CMD_SIZE 64 -struct Action -{ +struct Action { unsigned op; - Action *next; + Action* next; char cmd[CMD_SIZE]; - const char *prod; - void *data; - unsigned size; + const char* prod; + void* data; + + // The protocol only supports 32-bit sizes, so you'll have to break + // anything larger into chunks. + uint32_t size; const char *msg; int (*func)(Action* a, int status, const char* resp); @@ -267,7 +261,7 @@ static int cb_reject(Action* a, int status, const char* resp) { } void fb_queue_require(const char *prod, const char *var, - int invert, unsigned nvalues, const char **value) + bool invert, size_t nvalues, const char **value) { Action *a; a = queue_action(OP_QUERY, "getvar:%s", var); diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index 4dbf32f4b..e2971f252 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include @@ -67,12 +66,12 @@ static int long_listing = 0; static int64_t sparse_limit = -1; static int64_t target_sparse_limit = -1; -unsigned page_size = 2048; -unsigned base_addr = 0x10000000; -unsigned kernel_offset = 0x00008000; -unsigned ramdisk_offset = 0x01000000; -unsigned second_offset = 0x00f00000; -unsigned tags_offset = 0x00000100; +static unsigned page_size = 2048; +static unsigned base_addr = 0x10000000; +static unsigned kernel_offset = 0x00008000; +static unsigned ramdisk_offset = 0x01000000; +static unsigned second_offset = 0x00f00000; +static unsigned tags_offset = 0x00000100; enum fb_buffer_type { FB_BUFFER, @@ -81,8 +80,8 @@ enum fb_buffer_type { struct fastboot_buffer { enum fb_buffer_type type; - void *data; - unsigned int sz; + void* data; + int64_t sz; }; static struct { @@ -97,8 +96,7 @@ static struct { {"vendor.img", "vendor.sig", "vendor", true}, }; -char *find_item(const char *item, const char *product) -{ +static char* find_item(const char* item, const char* product) { char *dir; const char *fn; char path[PATH_MAX + 128]; @@ -139,36 +137,26 @@ char *find_item(const char *item, const char *product) return strdup(path); } -static int64_t file_size(int fd) -{ - struct stat st; - int ret; - - ret = fstat(fd, &st); - - return ret ? -1 : st.st_size; +static int64_t get_file_size(int fd) { + struct stat sb; + return fstat(fd, &sb) == -1 ? -1 : sb.st_size; } -static void *load_fd(int fd, unsigned *_sz) -{ - char *data; - int sz; +static void* load_fd(int fd, int64_t* sz) { int errno_tmp; + char* data = nullptr; - data = 0; - - sz = file_size(fd); - if (sz < 0) { + *sz = get_file_size(fd); + if (*sz < 0) { goto oops; } - data = (char*) malloc(sz); - if(data == 0) goto oops; + data = (char*) malloc(*sz); + if (data == nullptr) goto oops; - if(read(fd, data, sz) != sz) goto oops; + if(read(fd, data, *sz) != *sz) goto oops; close(fd); - if(_sz) *_sz = sz; return data; oops: @@ -179,17 +167,13 @@ oops: return 0; } -static void *load_file(const char *fn, unsigned *_sz) -{ - int fd; - - fd = open(fn, O_RDONLY | O_BINARY); - if(fd < 0) return 0; - - return load_fd(fd, _sz); +static void* load_file(const char* fn, int64_t* sz) { + int fd = open(fn, O_RDONLY | O_BINARY); + if (fd == -1) return nullptr; + return load_fd(fd, sz); } -int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) { +static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) { // Require a matching vendor id if the user specified one with -i. if (vendor_id != 0 && info->dev_vendor != vendor_id) { return -1; @@ -206,14 +190,12 @@ int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) { return 0; } -int match_fastboot(usb_ifc_info *info) -{ +static int match_fastboot(usb_ifc_info* info) { return match_fastboot_with_serial(info, serial); } -int list_devices_callback(usb_ifc_info *info) -{ - if (match_fastboot_with_serial(info, NULL) == 0) { +static int list_devices_callback(usb_ifc_info* info) { + if (match_fastboot_with_serial(info, nullptr) == 0) { const char* serial = info->serial_number; if (!info->writable) { serial = "no permissions"; // like "adb devices" @@ -234,8 +216,7 @@ int list_devices_callback(usb_ifc_info *info) return -1; } -usb_handle *open_device(void) -{ +static usb_handle* open_device() { static usb_handle *usb = 0; int announce = 1; @@ -252,15 +233,14 @@ usb_handle *open_device(void) } } -void list_devices(void) { +static void list_devices() { // We don't actually open a USB device here, // just getting our callback called so we can // list all the connected devices. usb_open(list_devices_callback); } -void usage(void) -{ +static void usage() { fprintf(stderr, /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ "usage: fastboot [