diff options
-rw-r--r-- | Android.mk | 6 | ||||
-rw-r--r-- | applypatch/Android.mk | 13 | ||||
-rw-r--r-- | applypatch/applypatch.cpp | 14 | ||||
-rw-r--r-- | applypatch/imgdiff.cpp | 10 | ||||
-rw-r--r-- | bootloader.h | 8 | ||||
-rw-r--r-- | common.h | 8 | ||||
-rw-r--r-- | edify/Android.mk | 18 | ||||
-rw-r--r-- | edify/expr.cpp (renamed from edify/expr.c) | 30 | ||||
-rw-r--r-- | edify/expr.h | 10 | ||||
-rw-r--r-- | edify/main.cpp (renamed from edify/main.c) | 0 | ||||
-rw-r--r-- | edify/parser.y | 8 | ||||
-rw-r--r-- | etc/init.rc | 1 | ||||
-rw-r--r-- | fuse_sdcard_provider.cpp (renamed from fuse_sdcard_provider.c) | 12 | ||||
-rw-r--r-- | fuse_sdcard_provider.h | 6 | ||||
-rw-r--r-- | fuse_sideload.cpp (renamed from fuse_sideload.c) | 15 | ||||
-rw-r--r-- | fuse_sideload.h | 6 | ||||
-rw-r--r-- | install.cpp | 4 | ||||
-rw-r--r-- | minadbd/adb_main.cpp | 10 | ||||
-rw-r--r-- | minadbd/services.cpp | 17 | ||||
-rw-r--r-- | minzip/Android.mk | 2 | ||||
-rw-r--r-- | minzip/Hash.c | 4 | ||||
-rw-r--r-- | minzip/SysUtil.c | 104 | ||||
-rw-r--r-- | minzip/Zip.c | 21 | ||||
-rw-r--r-- | mtdutils/mtdutils.c | 8 | ||||
-rw-r--r-- | print_sha1.h | 43 | ||||
-rw-r--r-- | recovery.cpp | 157 | ||||
l--------- | res-560dpi | 1 | ||||
-rw-r--r-- | screen_ui.cpp | 37 | ||||
-rw-r--r-- | screen_ui.h | 2 | ||||
-rw-r--r-- | ui.cpp | 4 | ||||
-rw-r--r-- | ui.h | 4 | ||||
-rw-r--r-- | uncrypt/Android.mk | 4 | ||||
-rw-r--r-- | uncrypt/uncrypt.cpp | 52 | ||||
-rw-r--r-- | uncrypt/uncrypt.rc | 9 | ||||
-rw-r--r-- | unique_fd.h | 62 | ||||
-rw-r--r-- | updater/Android.mk | 3 | ||||
-rw-r--r-- | updater/blockimg.cpp | 1638 | ||||
-rw-r--r-- | updater/install.cpp | 81 | ||||
-rw-r--r-- | verifier_test.cpp | 6 | ||||
-rw-r--r-- | wear_ui.cpp | 2 |
40 files changed, 1145 insertions, 1295 deletions
diff --git a/Android.mk b/Android.mk index c2896f3fc..602a85673 100644 --- a/Android.mk +++ b/Android.mk @@ -16,7 +16,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -LOCAL_SRC_FILES := fuse_sideload.c +LOCAL_SRC_FILES := fuse_sideload.cpp LOCAL_CLANG := true LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE @@ -33,7 +33,7 @@ LOCAL_SRC_FILES := \ asn1_decoder.cpp \ bootloader.cpp \ device.cpp \ - fuse_sdcard_provider.c \ + fuse_sdcard_provider.cpp \ install.cpp \ recovery.cpp \ roots.cpp \ @@ -46,9 +46,11 @@ LOCAL_MODULE := recovery LOCAL_FORCE_STATIC_EXECUTABLE := true +ifeq ($(TARGET_USERIMAGES_USE_F2FS),true) ifeq ($(HOST_OS),linux) LOCAL_REQUIRED_MODULES := mkfs.f2fs endif +endif RECOVERY_API_VERSION := 3 RECOVERY_FSTAB_VERSION := 2 diff --git a/applypatch/Android.mk b/applypatch/Android.mk index cc17a13ae..93a272997 100644 --- a/applypatch/Android.mk +++ b/applypatch/Android.mk @@ -39,19 +39,6 @@ include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) LOCAL_CLANG := true -LOCAL_SRC_FILES := main.cpp -LOCAL_MODULE := applypatch_static -LOCAL_FORCE_STATIC_EXECUTABLE := true -LOCAL_MODULE_TAGS := eng -LOCAL_C_INCLUDES += bootable/recovery -LOCAL_STATIC_LIBRARIES += libapplypatch libbase libmtdutils libmincrypt libbz -LOCAL_STATIC_LIBRARIES += libz libcutils libc - -include $(BUILD_EXECUTABLE) - -include $(CLEAR_VARS) - -LOCAL_CLANG := true LOCAL_SRC_FILES := imgdiff.cpp utils.cpp bsdiff.cpp LOCAL_MODULE := imgdiff LOCAL_FORCE_STATIC_EXECUTABLE := true diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 751d3e392..f9425af93 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -25,12 +25,13 @@ #include <sys/types.h> #include <unistd.h> -#include <base/strings.h> +#include <android-base/strings.h> #include "mincrypt/sha.h" #include "applypatch.h" #include "mtdutils/mtdutils.h" #include "edify/expr.h" +#include "print_sha1.h" static int LoadPartitionContents(const char* filename, FileContents* file); static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token); @@ -43,7 +44,6 @@ static int GenerateTarget(FileContents* source_file, const uint8_t target_sha1[SHA_DIGEST_SIZE], size_t target_size, const Value* bonus_data); -static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]); static bool mtd_partitions_scanned = false; @@ -630,16 +630,6 @@ int CacheSizeCheck(size_t bytes) { } } -static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { - const char* hex = "0123456789abcdef"; - std::string result = ""; - for (size_t i = 0; i < 4; ++i) { - result.push_back(hex[(sha1[i]>>4) & 0xf]); - result.push_back(hex[sha1[i] & 0xf]); - } - return result; -} - // This function applies binary patches to files in a way that is safe // (the original file is not touched until we have the desired // replacement for it) and idempotent (it's okay to run this program diff --git a/applypatch/imgdiff.cpp b/applypatch/imgdiff.cpp index 4d83ffb2e..50cabbe6b 100644 --- a/applypatch/imgdiff.cpp +++ b/applypatch/imgdiff.cpp @@ -628,7 +628,15 @@ unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) { } char ptemp[] = "/tmp/imgdiff-patch-XXXXXX"; - mkstemp(ptemp); + int fd = mkstemp(ptemp); + + if (fd == -1) { + printf("MakePatch failed to create a temporary file: %s\n", + strerror(errno)); + return NULL; + } + close(fd); // temporary file is created and we don't need its file + // descriptor int r = bsdiff(src->data, src->len, &(src->I), tgt->data, tgt->len, ptemp); if (r != 0) { diff --git a/bootloader.h b/bootloader.h index fd003a076..742a4abfb 100644 --- a/bootloader.h +++ b/bootloader.h @@ -17,10 +17,6 @@ #ifndef _RECOVERY_BOOTLOADER_H #define _RECOVERY_BOOTLOADER_H -#ifdef __cplusplus -extern "C" { -#endif - /* Bootloader Message * * This structure describes the content of a block in flash @@ -72,8 +68,4 @@ struct bootloader_message { int get_bootloader_message(struct bootloader_message *out); int set_bootloader_message(const struct bootloader_message *in); -#ifdef __cplusplus -} -#endif - #endif @@ -21,10 +21,6 @@ #include <stdio.h> #include <stdarg.h> -#ifdef __cplusplus -extern "C" { -#endif - #define LOGE(...) ui_print("E:" __VA_ARGS__) #define LOGW(...) fprintf(stdout, "W:" __VA_ARGS__) #define LOGI(...) fprintf(stdout, "I:" __VA_ARGS__) @@ -50,8 +46,4 @@ void ui_print(const char* format, ...); bool is_ro_debuggable(); -#ifdef __cplusplus -} -#endif - #endif // RECOVERY_COMMON_H diff --git a/edify/Android.mk b/edify/Android.mk index c36645045..9b859d42e 100644 --- a/edify/Android.mk +++ b/edify/Android.mk @@ -5,12 +5,7 @@ LOCAL_PATH := $(call my-dir) edify_src_files := \ lexer.l \ parser.y \ - expr.c - -# "-x c" forces the lex/yacc files to be compiled as c the build system -# otherwise forces them to be c++. Need to also add an explicit -std because the -# build system will soon default C++ to -std=c++11. -edify_cflags := -x c -std=gnu89 + expr.cpp # # Build the host-side command line tool @@ -19,12 +14,13 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ $(edify_src_files) \ - main.c + main.cpp -LOCAL_CFLAGS := $(edify_cflags) -g -O0 +LOCAL_CPPFLAGS := -g -O0 LOCAL_MODULE := edify LOCAL_YACCFLAGS := -v -LOCAL_CFLAGS += -Wno-unused-parameter +LOCAL_CPPFLAGS += -Wno-unused-parameter +LOCAL_CPPFLAGS += -Wno-deprecated-register LOCAL_CLANG := true include $(BUILD_HOST_EXECUTABLE) @@ -36,8 +32,8 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := $(edify_src_files) -LOCAL_CFLAGS := $(edify_cflags) -LOCAL_CFLAGS += -Wno-unused-parameter +LOCAL_CPPFLAGS := -Wno-unused-parameter +LOCAL_CPPFLAGS += -Wno-deprecated-register LOCAL_MODULE := libedify LOCAL_CLANG := true diff --git a/edify/expr.c b/edify/expr.cpp index 79f6282d8..cd1e08726 100644 --- a/edify/expr.c +++ b/edify/expr.cpp @@ -51,7 +51,7 @@ Value* EvaluateValue(State* state, Expr* expr) { Value* StringValue(char* str) { if (str == NULL) return NULL; - Value* v = malloc(sizeof(Value)); + Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value))); v->type = VAL_STRING; v->size = strlen(str); v->data = str; @@ -68,7 +68,7 @@ Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) { if (argc == 0) { return StringValue(strdup("")); } - char** strings = malloc(argc * sizeof(char*)); + char** strings = reinterpret_cast<char**>(malloc(argc * sizeof(char*))); int i; for (i = 0; i < argc; ++i) { strings[i] = NULL; @@ -83,8 +83,9 @@ Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) { length += strlen(strings[i]); } - result = malloc(length+1); - int p = 0; + result = reinterpret_cast<char*>(malloc(length+1)); + int p; + p = 0; for (i = 0; i < argc; ++i) { strcpy(result+p, strings[i]); p += strlen(strings[i]); @@ -149,7 +150,7 @@ Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) { if (!b) { int prefix_len; int len = argv[i]->end - argv[i]->start; - char* err_src = malloc(len + 20); + char* err_src = reinterpret_cast<char*>(malloc(len + 20)); strcpy(err_src, "assert failed: "); prefix_len = strlen(err_src); memcpy(err_src + prefix_len, state->script + argv[i]->start, len); @@ -290,7 +291,8 @@ Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) { goto done; } - long r_int = strtol(right, &end, 10); + long r_int; + r_int = strtol(right, &end, 10); if (right[0] == '\0' || *end != '\0') { goto done; } @@ -325,11 +327,11 @@ Value* Literal(const char* name, State* state, int argc, Expr* argv[]) { Expr* Build(Function fn, YYLTYPE loc, int count, ...) { va_list v; va_start(v, count); - Expr* e = malloc(sizeof(Expr)); + Expr* e = reinterpret_cast<Expr*>(malloc(sizeof(Expr))); e->fn = fn; e->name = "(operator)"; e->argc = count; - e->argv = malloc(count * sizeof(Expr*)); + e->argv = reinterpret_cast<Expr**>(malloc(count * sizeof(Expr*))); int i; for (i = 0; i < count; ++i) { e->argv[i] = va_arg(v, Expr*); @@ -351,7 +353,7 @@ NamedFunction* fn_table = NULL; void RegisterFunction(const char* name, Function fn) { if (fn_entries >= fn_size) { fn_size = fn_size*2 + 1; - fn_table = realloc(fn_table, fn_size * sizeof(NamedFunction)); + fn_table = reinterpret_cast<NamedFunction*>(realloc(fn_table, fn_size * sizeof(NamedFunction))); } fn_table[fn_entries].name = name; fn_table[fn_entries].fn = fn; @@ -371,8 +373,8 @@ void FinishRegistration() { Function FindFunction(const char* name) { NamedFunction key; key.name = name; - NamedFunction* nf = bsearch(&key, fn_table, fn_entries, - sizeof(NamedFunction), fn_entry_compare); + NamedFunction* nf = reinterpret_cast<NamedFunction*>(bsearch(&key, fn_table, fn_entries, + sizeof(NamedFunction), fn_entry_compare)); if (nf == NULL) { return NULL; } @@ -401,7 +403,7 @@ void RegisterBuiltins() { // zero or more char** to put them in). If any expression evaluates // to NULL, free the rest and return -1. Return 0 on success. int ReadArgs(State* state, Expr* argv[], int count, ...) { - char** args = malloc(count * sizeof(char*)); + char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*))); va_list v; va_start(v, count); int i; @@ -427,7 +429,7 @@ int ReadArgs(State* state, Expr* argv[], int count, ...) { // zero or more Value** to put them in). If any expression evaluates // to NULL, free the rest and return -1. Return 0 on success. int ReadValueArgs(State* state, Expr* argv[], int count, ...) { - Value** args = malloc(count * sizeof(Value*)); + Value** args = reinterpret_cast<Value**>(malloc(count * sizeof(Value*))); va_list v; va_start(v, count); int i; @@ -494,7 +496,7 @@ Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) { // Use printf-style arguments to compose an error message to put into // *state. Returns NULL. Value* ErrorAbort(State* state, const char* format, ...) { - char* buffer = malloc(4096); + char* buffer = reinterpret_cast<char*>(malloc(4096)); va_list v; va_start(v, format); vsnprintf(buffer, 4096, format, v); diff --git a/edify/expr.h b/edify/expr.h index a9ed2f9c5..36f8e9612 100644 --- a/edify/expr.h +++ b/edify/expr.h @@ -21,10 +21,6 @@ #include "yydefs.h" -#ifdef __cplusplus -extern "C" { -#endif - #define MAX_STRING_LEN 1024 typedef struct Expr Expr; @@ -59,7 +55,7 @@ typedef Value* (*Function)(const char* name, State* state, struct Expr { Function fn; - char* name; + const char* name; int argc; Expr** argv; int start, end; @@ -166,8 +162,4 @@ void FreeValue(Value* v); int parse_string(const char* str, Expr** root, int* error_count); -#ifdef __cplusplus -} // extern "C" -#endif - #endif // _EXPRESSION_H diff --git a/edify/main.c b/edify/main.cpp index b1baa0b13..b1baa0b13 100644 --- a/edify/main.c +++ b/edify/main.cpp diff --git a/edify/parser.y b/edify/parser.y index f8fb2d12f..098a6370a 100644 --- a/edify/parser.y +++ b/edify/parser.y @@ -70,7 +70,7 @@ input: expr { *root = $1; } ; expr: STRING { - $$ = malloc(sizeof(Expr)); + $$ = reinterpret_cast<Expr*>(malloc(sizeof(Expr))); $$->fn = Literal; $$->name = $1; $$->argc = 0; @@ -91,7 +91,7 @@ expr: STRING { | IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); } | IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); } | STRING '(' arglist ')' { - $$ = malloc(sizeof(Expr)); + $$ = reinterpret_cast<Expr*>(malloc(sizeof(Expr))); $$->fn = FindFunction($1); if ($$->fn == NULL) { char buffer[256]; @@ -113,12 +113,12 @@ arglist: /* empty */ { } | expr { $$.argc = 1; - $$.argv = malloc(sizeof(Expr*)); + $$.argv = reinterpret_cast<Expr**>(malloc(sizeof(Expr*))); $$.argv[0] = $1; } | arglist ',' expr { $$.argc = $1.argc + 1; - $$.argv = realloc($$.argv, $$.argc * sizeof(Expr*)); + $$.argv = reinterpret_cast<Expr**>(realloc($$.argv, $$.argc * sizeof(Expr*))); $$.argv[$$.argc-1] = $3; } ; diff --git a/etc/init.rc b/etc/init.rc index 427727768..dc1865986 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -5,7 +5,6 @@ on early-init start healthd on init - export PATH /sbin:/system/bin export ANDROID_ROOT /system export ANDROID_DATA /data export EXTERNAL_STORAGE /sdcard diff --git a/fuse_sdcard_provider.c b/fuse_sdcard_provider.cpp index 4565c7b5b..eb6454f1d 100644 --- a/fuse_sdcard_provider.c +++ b/fuse_sdcard_provider.cpp @@ -34,7 +34,7 @@ struct file_data { }; static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { - struct file_data* fd = (struct file_data*)cookie; + file_data* fd = reinterpret_cast<file_data*>(cookie); off64_t offset = ((off64_t) block) * fd->block_size; if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) { @@ -56,7 +56,7 @@ static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32 } static void close_file(void* cookie) { - struct file_data* fd = (struct file_data*)cookie; + file_data* fd = reinterpret_cast<file_data*>(cookie); close(fd->fd); } @@ -67,7 +67,7 @@ struct token { }; static void* run_sdcard_fuse(void* cookie) { - struct token* t = (struct token*)cookie; + token* t = reinterpret_cast<token*>(cookie); struct stat sb; if (stat(t->path, &sb) < 0) { @@ -100,7 +100,7 @@ static void* run_sdcard_fuse(void* cookie) { #define SDCARD_INSTALL_TIMEOUT 10 void* start_sdcard_fuse(const char* path) { - struct token* t = malloc(sizeof(struct token)); + token* t = new token; t->path = path; pthread_create(&(t->th), NULL, run_sdcard_fuse, t); @@ -128,7 +128,7 @@ void* start_sdcard_fuse(const char* path) { void finish_sdcard_fuse(void* cookie) { if (cookie == NULL) return; - struct token* t = (struct token*)cookie; + token* t = reinterpret_cast<token*>(cookie); // Calling stat() on this magic filename signals the fuse // filesystem to shut down. @@ -136,5 +136,5 @@ void finish_sdcard_fuse(void* cookie) { stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st); pthread_join(t->th, NULL); - free(t); + delete t; } diff --git a/fuse_sdcard_provider.h b/fuse_sdcard_provider.h index dbfbcd521..dc2982ca0 100644 --- a/fuse_sdcard_provider.h +++ b/fuse_sdcard_provider.h @@ -17,13 +17,7 @@ #ifndef __FUSE_SDCARD_PROVIDER_H #define __FUSE_SDCARD_PROVIDER_H -#include <sys/cdefs.h> - -__BEGIN_DECLS - void* start_sdcard_fuse(const char* path); void finish_sdcard_fuse(void* token); -__END_DECLS - #endif diff --git a/fuse_sideload.c b/fuse_sideload.cpp index 48e6cc53a..9c3e75f89 100644 --- a/fuse_sideload.c +++ b/fuse_sideload.cpp @@ -116,7 +116,7 @@ static void fuse_reply(struct fuse_data* fd, __u64 unique, const void *data, siz } static int handle_init(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { - const struct fuse_init_in* req = data; + const struct fuse_init_in* req = reinterpret_cast<const struct fuse_init_in*>(data); struct fuse_init_out out; size_t fuse_struct_size; @@ -170,8 +170,7 @@ static void fill_attr(struct fuse_attr* attr, struct fuse_data* fd, attr->mode = mode; } -static int handle_getattr(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { - const struct fuse_getattr_in* req = data; +static int handle_getattr(void* /* data */, struct fuse_data* fd, const struct fuse_in_header* hdr) { struct fuse_attr_out out; memset(&out, 0, sizeof(out)); out.attr_valid = 10; @@ -197,12 +196,12 @@ static int handle_lookup(void* data, struct fuse_data* fd, out.entry_valid = 10; out.attr_valid = 10; - if (strncmp(FUSE_SIDELOAD_HOST_FILENAME, data, + if (strncmp(FUSE_SIDELOAD_HOST_FILENAME, reinterpret_cast<const char*>(data), sizeof(FUSE_SIDELOAD_HOST_FILENAME)) == 0) { out.nodeid = PACKAGE_FILE_ID; out.generation = PACKAGE_FILE_ID; fill_attr(&(out.attr), fd, PACKAGE_FILE_ID, fd->file_size, S_IFREG | 0444); - } else if (strncmp(FUSE_SIDELOAD_HOST_EXIT_FLAG, data, + } else if (strncmp(FUSE_SIDELOAD_HOST_EXIT_FLAG, reinterpret_cast<const char*>(data), sizeof(FUSE_SIDELOAD_HOST_EXIT_FLAG)) == 0) { out.nodeid = EXIT_FLAG_ID; out.generation = EXIT_FLAG_ID; @@ -215,9 +214,7 @@ static int handle_lookup(void* data, struct fuse_data* fd, return (out.nodeid == EXIT_FLAG_ID) ? NO_STATUS_EXIT : NO_STATUS; } -static int handle_open(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { - const struct fuse_open_in* req = data; - +static int handle_open(void* /* data */, struct fuse_data* fd, const struct fuse_in_header* hdr) { if (hdr->nodeid == EXIT_FLAG_ID) return -EPERM; if (hdr->nodeid != PACKAGE_FILE_ID) return -ENOENT; @@ -292,7 +289,7 @@ static int fetch_block(struct fuse_data* fd, uint32_t block) { } static int handle_read(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { - const struct fuse_read_in* req = data; + const struct fuse_read_in* req = reinterpret_cast<const struct fuse_read_in*>(data); struct fuse_out_header outhdr; struct iovec vec[3]; int vec_used; diff --git a/fuse_sideload.h b/fuse_sideload.h index f9e3bf0d3..c0b16efbe 100644 --- a/fuse_sideload.h +++ b/fuse_sideload.h @@ -17,10 +17,6 @@ #ifndef __FUSE_SIDELOAD_H #define __FUSE_SIDELOAD_H -#include <sys/cdefs.h> - -__BEGIN_DECLS - // define the filenames created by the sideload FUSE filesystem #define FUSE_SIDELOAD_HOST_MOUNTPOINT "/sideload" #define FUSE_SIDELOAD_HOST_FILENAME "package.zip" @@ -39,6 +35,4 @@ struct provider_vtab { int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_size, uint32_t block_size); -__END_DECLS - #endif diff --git a/install.cpp b/install.cpp index c7d382f3e..7d88ed72a 100644 --- a/install.cpp +++ b/install.cpp @@ -164,9 +164,9 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { } else if (strcmp(command, "ui_print") == 0) { char* str = strtok(NULL, "\n"); if (str) { - ui->Print("%s", str); + ui->PrintOnScreenOnly("%s", str); } else { - ui->Print("\n"); + ui->PrintOnScreenOnly("\n"); } fflush(stdout); } else if (strcmp(command, "wipe_cache") == 0) { diff --git a/minadbd/adb_main.cpp b/minadbd/adb_main.cpp index 7fae99a9a..0694280cb 100644 --- a/minadbd/adb_main.cpp +++ b/minadbd/adb_main.cpp @@ -19,21 +19,15 @@ #include <stdio.h> #include <stdlib.h> -#define TRACE_TAG TRACE_ADB - #include "sysdeps.h" #include "adb.h" #include "adb_auth.h" #include "transport.h" -int adb_main(int is_daemon, int server_port) -{ - atexit(usb_cleanup); - +int adb_server_main(int is_daemon, int server_port, int /* reply_fd */) { adb_device_banner = "sideload"; - // No SIGCHLD. Let the service subproc handle its children. signal(SIGPIPE, SIG_IGN); // We can't require authentication for sideloading. http://b/22025550. @@ -42,7 +36,7 @@ int adb_main(int is_daemon, int server_port) init_transport_registration(); usb_init(); - D("Event loop starting\n"); + VLOG(ADB) << "Event loop starting"; fdevent_loop(); return 0; diff --git a/minadbd/services.cpp b/minadbd/services.cpp index dd1fd7c4b..d25648fb4 100644 --- a/minadbd/services.cpp +++ b/minadbd/services.cpp @@ -23,7 +23,6 @@ #include "sysdeps.h" -#define TRACE_TAG TRACE_SERVICES #include "adb.h" #include "fdevent.h" #include "fuse_adb_provider.h" @@ -44,13 +43,14 @@ void* service_bootstrap_func(void* x) { } static void sideload_host_service(int sfd, void* data) { - const char* args = reinterpret_cast<const char*>(data); + char* args = reinterpret_cast<char*>(data); int file_size; int block_size; if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) { printf("bad sideload-host arguments: %s\n", args); exit(1); } + free(args); printf("sideload-host file size %d block size %d\n", file_size, block_size); @@ -61,8 +61,7 @@ static void sideload_host_service(int sfd, void* data) { exit(result == 0 ? 0 : 1); } -static int create_service_thread(void (*func)(int, void *), void *cookie) -{ +static int create_service_thread(void (*func)(int, void *), void *cookie) { int s[2]; if(adb_socketpair(s)) { printf("cannot create service socket pair\n"); @@ -75,8 +74,7 @@ static int create_service_thread(void (*func)(int, void *), void *cookie) sti->cookie = cookie; sti->fd = s[1]; - adb_thread_t t; - if (adb_thread_create( &t, service_bootstrap_func, sti)){ + if (!adb_thread_create(service_bootstrap_func, sti)) { free(sti); adb_close(s[0]); adb_close(s[1]); @@ -84,11 +82,11 @@ static int create_service_thread(void (*func)(int, void *), void *cookie) return -1; } - D("service thread started, %d:%d\n",s[0], s[1]); + VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1]; return s[0]; } -int service_to_fd(const char* name) { +int service_to_fd(const char* name, const atransport* transport) { int ret = -1; if (!strncmp(name, "sideload:", 9)) { @@ -97,7 +95,8 @@ int service_to_fd(const char* name) { // sideload-host). exit(3); } else if (!strncmp(name, "sideload-host:", 14)) { - ret = create_service_thread(sideload_host_service, (void*)(name + 14)); + char* arg = strdup(name + 14); + ret = create_service_thread(sideload_host_service, arg); } if (ret >= 0) { close_on_exec(ret); diff --git a/minzip/Android.mk b/minzip/Android.mk index 48d26bcb7..22eabfbb1 100644 --- a/minzip/Android.mk +++ b/minzip/Android.mk @@ -18,6 +18,6 @@ LOCAL_MODULE := libminzip LOCAL_CLANG := true -LOCAL_CFLAGS += -Wall +LOCAL_CFLAGS += -Werror -Wall include $(BUILD_STATIC_LIBRARY) diff --git a/minzip/Hash.c b/minzip/Hash.c index 8f8ed68e5..49bcb3161 100644 --- a/minzip/Hash.c +++ b/minzip/Hash.c @@ -361,7 +361,7 @@ void mzHashTableProbeCount(HashTable* pHashTable, HashCalcFunc calcFunc, { const void* data = (const void*)mzHashIterData(&iter); int count; - + count = countProbes(pHashTable, (*calcFunc)(data), data, cmpFunc); numEntries++; @@ -373,7 +373,7 @@ void mzHashTableProbeCount(HashTable* pHashTable, HashCalcFunc calcFunc, totalProbe += count; } - LOGI("Probe: min=%d max=%d, total=%d in %d (%d), avg=%.3f\n", + LOGV("Probe: min=%d max=%d, total=%d in %d (%d), avg=%.3f\n", minProbe, maxProbe, totalProbe, numEntries, pHashTable->tableSize, (float) totalProbe / (float) numEntries); } diff --git a/minzip/SysUtil.c b/minzip/SysUtil.c index b160c9e3d..09ec8768f 100644 --- a/minzip/SysUtil.c +++ b/minzip/SysUtil.c @@ -3,86 +3,46 @@ * * System utilities. */ -#include <stdlib.h> +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdbool.h> #include <stdio.h> -#include <unistd.h> +#include <stdlib.h> #include <string.h> #include <sys/mman.h> -#include <sys/types.h> #include <sys/stat.h> -#include <fcntl.h> -#include <limits.h> -#include <errno.h> -#include <assert.h> +#include <sys/types.h> +#include <unistd.h> #define LOG_TAG "sysutil" #include "Log.h" #include "SysUtil.h" -static int getFileStartAndLength(int fd, off_t *start_, size_t *length_) -{ - off_t start, end; - size_t length; - - assert(start_ != NULL); - assert(length_ != NULL); - - // TODO: isn't start always 0 for the single call site? just use fstat instead? - - start = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_CUR)); - end = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_END)); - - if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1 || - start == (off_t) -1 || end == (off_t) -1) { - LOGE("could not determine length of file\n"); - return -1; - } - - length = end - start; - if (length == 0) { - LOGE("file is empty\n"); - return -1; - } - - *start_ = start; - *length_ = length; - - return 0; -} - -/* - * Map a file (from fd's current offset) into a private, read-only memory - * segment. The file offset must be a multiple of the page size. - * - * On success, returns 0 and fills out "pMap". On failure, returns a nonzero - * value and does not disturb "pMap". - */ -static int sysMapFD(int fd, MemMapping* pMap) -{ - off_t start; - size_t length; - void* memPtr; - +static bool sysMapFD(int fd, MemMapping* pMap) { assert(pMap != NULL); - if (getFileStartAndLength(fd, &start, &length) < 0) - return -1; + struct stat sb; + if (fstat(fd, &sb) == -1) { + LOGE("fstat(%d) failed: %s\n", fd, strerror(errno)); + return false; + } - memPtr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, start); + void* memPtr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (memPtr == MAP_FAILED) { - LOGW("mmap(%d, R, PRIVATE, %d, %d) failed: %s\n", (int) length, - fd, (int) start, strerror(errno)); - return -1; + LOGE("mmap(%d, R, PRIVATE, %d, 0) failed: %s\n", (int) sb.st_size, fd, strerror(errno)); + return false; } pMap->addr = memPtr; - pMap->length = length; + pMap->length = sb.st_size; pMap->range_count = 1; pMap->ranges = malloc(sizeof(MappedRange)); pMap->ranges[0].addr = memPtr; - pMap->ranges[0].length = length; + pMap->ranges[0].length = sb.st_size; - return 0; + return true; } static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) @@ -95,7 +55,7 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) unsigned int i; if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) { - LOGW("failed to read block device from header\n"); + LOGE("failed to read block device from header\n"); return -1; } for (i = 0; i < sizeof(block_dev); ++i) { @@ -106,7 +66,7 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) } if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) { - LOGW("failed to parse block map header\n"); + LOGE("failed to parse block map header\n"); return -1; } @@ -120,7 +80,7 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) unsigned char* reserve; reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); if (reserve == MAP_FAILED) { - LOGW("failed to reserve address space: %s\n", strerror(errno)); + LOGE("failed to reserve address space: %s\n", strerror(errno)); return -1; } @@ -129,7 +89,7 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) int fd = open(block_dev, O_RDONLY); if (fd < 0) { - LOGW("failed to open block device %s: %s\n", block_dev, strerror(errno)); + LOGE("failed to open block device %s: %s\n", block_dev, strerror(errno)); return -1; } @@ -137,13 +97,13 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) for (i = 0; i < range_count; ++i) { int start, end; if (fscanf(mapf, "%d %d\n", &start, &end) != 2) { - LOGW("failed to parse range %d in block map\n", i); + LOGE("failed to parse range %d in block map\n", i); return -1; } void* addr = mmap64(next, (end-start)*blksize, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize); if (addr == MAP_FAILED) { - LOGW("failed to map block %d: %s\n", i, strerror(errno)); + LOGE("failed to map block %d: %s\n", i, strerror(errno)); return -1; } pMap->ranges[i].addr = addr; @@ -168,25 +128,25 @@ int sysMapFile(const char* fn, MemMapping* pMap) // A map of blocks FILE* mapf = fopen(fn+1, "r"); if (mapf == NULL) { - LOGV("Unable to open '%s': %s\n", fn+1, strerror(errno)); + LOGE("Unable to open '%s': %s\n", fn+1, strerror(errno)); return -1; } if (sysMapBlockFile(mapf, pMap) != 0) { - LOGW("Map of '%s' failed\n", fn); + LOGE("Map of '%s' failed\n", fn); return -1; } fclose(mapf); } else { // This is a regular file. - int fd = open(fn, O_RDONLY, 0); - if (fd < 0) { + int fd = open(fn, O_RDONLY); + if (fd == -1) { LOGE("Unable to open '%s': %s\n", fn, strerror(errno)); return -1; } - if (sysMapFD(fd, pMap) != 0) { + if (!sysMapFD(fd, pMap)) { LOGE("Map of '%s' failed\n", fn); close(fd); return -1; @@ -205,7 +165,7 @@ void sysReleaseMap(MemMapping* pMap) int i; for (i = 0; i < pMap->range_count; ++i) { if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) { - LOGW("munmap(%p, %d) failed: %s\n", + LOGE("munmap(%p, %d) failed: %s\n", pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno)); } } diff --git a/minzip/Zip.c b/minzip/Zip.c index a64c833f9..bdb565c64 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.c @@ -198,10 +198,10 @@ static bool parseZipArchive(ZipArchive* pArchive) */ val = get4LE(pArchive->addr); if (val == ENDSIG) { - LOGI("Found Zip archive, but it looks empty\n"); + LOGW("Found Zip archive, but it looks empty\n"); goto bail; } else if (val != LOCSIG) { - LOGV("Not a Zip archive (found 0x%08x)\n", val); + LOGW("Not a Zip archive (found 0x%08x)\n", val); goto bail; } @@ -217,7 +217,7 @@ static bool parseZipArchive(ZipArchive* pArchive) ptr--; } if (ptr < (const unsigned char*) pArchive->addr) { - LOGI("Could not find end-of-central-directory in Zip\n"); + LOGW("Could not find end-of-central-directory in Zip\n"); goto bail; } @@ -429,7 +429,7 @@ int mzOpenZipArchive(unsigned char* addr, size_t length, ZipArchive* pArchive) if (length < ENDHDR) { err = -1; - LOGV("File '%s' too small to be zip (%zd)\n", fileName, map.length); + LOGW("Archive %p is too small to be zip (%zd)\n", pArchive, length); goto bail; } @@ -438,7 +438,7 @@ int mzOpenZipArchive(unsigned char* addr, size_t length, ZipArchive* pArchive) if (!parseZipArchive(pArchive)) { err = -1; - LOGV("Parsing '%s' failed\n", fileName); + LOGW("Parsing archive %p failed\n", pArchive); goto bail; } @@ -548,7 +548,7 @@ static bool processDeflatedEntry(const ZipArchive *pArchive, /* uncompress the data */ zerr = inflate(&zstream, Z_NO_FLUSH); if (zerr != Z_OK && zerr != Z_STREAM_END) { - LOGD("zlib inflate call failed (zerr=%d)\n", zerr); + LOGW("zlib inflate call failed (zerr=%d)\n", zerr); goto z_bail; } @@ -619,13 +619,6 @@ bool mzProcessZipEntryContents(const ZipArchive *pArchive, return ret; } -static bool crcProcessFunction(const unsigned char *data, int dataLen, - void *crc) -{ - *(unsigned long *)crc = crc32(*(unsigned long *)crc, data, dataLen); - return true; -} - typedef struct { char *buf; int bufLen; @@ -1014,7 +1007,7 @@ bool mzExtractRecursive(const ZipArchive *pArchive, if (callback != NULL) callback(targetFile, cookie); } - LOGD("Extracted %d file(s)\n", extractCount); + LOGV("Extracted %d file(s)\n", extractCount); free(helper.buf); free(zpath); diff --git a/mtdutils/mtdutils.c b/mtdutils/mtdutils.c index cc3033444..cd4f52cd5 100644 --- a/mtdutils/mtdutils.c +++ b/mtdutils/mtdutils.c @@ -300,20 +300,20 @@ static int read_block(const MtdPartition *partition, int fd, char *data) if (TEMP_FAILURE_RETRY(lseek64(fd, pos, SEEK_SET)) != pos || TEMP_FAILURE_RETRY(read(fd, data, size)) != size) { printf("mtd: read error at 0x%08llx (%s)\n", - pos, strerror(errno)); + (long long)pos, strerror(errno)); } else if (ioctl(fd, ECCGETSTATS, &after)) { printf("mtd: ECCGETSTATS error (%s)\n", strerror(errno)); return -1; } else if (after.failed != before.failed) { printf("mtd: ECC errors (%d soft, %d hard) at 0x%08llx\n", - after.corrected - before.corrected, - after.failed - before.failed, pos); + after.corrected - before.corrected, + after.failed - before.failed, (long long)pos); // copy the comparison baseline for the next read. memcpy(&before, &after, sizeof(struct mtd_ecc_stats)); } else if ((mgbb = ioctl(fd, MEMGETBADBLOCK, &pos))) { fprintf(stderr, "mtd: MEMGETBADBLOCK returned %d at 0x%08llx: %s\n", - mgbb, pos, strerror(errno)); + mgbb, (long long)pos, strerror(errno)); } else { return 0; // Success! } diff --git a/print_sha1.h b/print_sha1.h new file mode 100644 index 000000000..9e37c5fe3 --- /dev/null +++ b/print_sha1.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef RECOVERY_PRINT_SHA1_H +#define RECOVERY_PRINT_SHA1_H + +#include <stdint.h> +#include <string> + +#include "mincrypt/sha.h" + +static std::string print_sha1(const uint8_t sha1[SHA_DIGEST_SIZE], size_t len) { + const char* hex = "0123456789abcdef"; + std::string result = ""; + for (size_t i = 0; i < len; ++i) { + result.push_back(hex[(sha1[i]>>4) & 0xf]); + result.push_back(hex[sha1[i] & 0xf]); + } + return result; +} + +static std::string print_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { + return print_sha1(sha1, SHA_DIGEST_SIZE); +} + +static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { + return print_sha1(sha1, 4); +} + +#endif // RECOVERY_PRINT_SHA1_H diff --git a/recovery.cpp b/recovery.cpp index 09b061d7d..dace52f98 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -31,24 +31,26 @@ #include <time.h> #include <unistd.h> -#include <base/file.h> -#include <base/stringprintf.h> +#include <chrono> +#include <adb.h> +#include <android-base/file.h> +#include <android-base/stringprintf.h> +#include <cutils/android_reboot.h> +#include <cutils/properties.h> + +#include "adb_install.h" #include "bootloader.h" #include "common.h" -#include "cutils/properties.h" -#include "cutils/android_reboot.h" +#include "device.h" +#include "fuse_sdcard_provider.h" +#include "fuse_sideload.h" #include "install.h" #include "minui/minui.h" #include "minzip/DirUtil.h" #include "roots.h" #include "ui.h" #include "screen_ui.h" -#include "device.h" -#include "adb_install.h" -#include "adb.h" -#include "fuse_sideload.h" -#include "fuse_sdcard_provider.h" struct selabel_handle *sehandle; @@ -151,8 +153,7 @@ static const int MAX_ARG_LENGTH = 4096; static const int MAX_ARGS = 100; // open a given path, mounting partitions as necessary -FILE* -fopen_path(const char *path, const char *mode) { +FILE* fopen_path(const char *path, const char *mode) { if (ensure_path_mounted(path) != 0) { LOGE("Can't mount %s\n", path); return NULL; @@ -166,23 +167,102 @@ fopen_path(const char *path, const char *mode) { return fp; } +// close a file, log an error if the error indicator is set +static void check_and_fclose(FILE *fp, const char *name) { + fflush(fp); + if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); + fclose(fp); +} + bool is_ro_debuggable() { char value[PROPERTY_VALUE_MAX+1]; return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1'); } static void redirect_stdio(const char* filename) { - // If these fail, there's not really anywhere to complain... - freopen(filename, "a", stdout); setbuf(stdout, NULL); - freopen(filename, "a", stderr); setbuf(stderr, NULL); -} + int pipefd[2]; + if (pipe(pipefd) == -1) { + LOGE("pipe failed: %s\n", strerror(errno)); -// close a file, log an error if the error indicator is set -static void -check_and_fclose(FILE *fp, const char *name) { - fflush(fp); - if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno)); - fclose(fp); + // Fall back to traditional logging mode without timestamps. + // If these fail, there's not really anywhere to complain... + freopen(filename, "a", stdout); setbuf(stdout, NULL); + freopen(filename, "a", stderr); setbuf(stderr, NULL); + + return; + } + + pid_t pid = fork(); + if (pid == -1) { + LOGE("fork failed: %s\n", strerror(errno)); + + // Fall back to traditional logging mode without timestamps. + // If these fail, there's not really anywhere to complain... + freopen(filename, "a", stdout); setbuf(stdout, NULL); + freopen(filename, "a", stderr); setbuf(stderr, NULL); + + return; + } + + if (pid == 0) { + /// Close the unused write end. + close(pipefd[1]); + + auto start = std::chrono::steady_clock::now(); + + // Child logger to actually write to the log file. + FILE* log_fp = fopen(filename, "a"); + if (log_fp == nullptr) { + LOGE("fopen \"%s\" failed: %s\n", filename, strerror(errno)); + close(pipefd[0]); + _exit(1); + } + + FILE* pipe_fp = fdopen(pipefd[0], "r"); + if (pipe_fp == nullptr) { + LOGE("fdopen failed: %s\n", strerror(errno)); + check_and_fclose(log_fp, filename); + close(pipefd[0]); + _exit(1); + } + + char* line = nullptr; + size_t len = 0; + while (getline(&line, &len, pipe_fp) != -1) { + auto now = std::chrono::steady_clock::now(); + double duration = std::chrono::duration_cast<std::chrono::duration<double>>( + now - start).count(); + if (line[0] == '\n') { + fprintf(log_fp, "[%12.6lf]\n", duration); + } else { + fprintf(log_fp, "[%12.6lf] %s", duration, line); + } + fflush(log_fp); + } + + LOGE("getline failed: %s\n", strerror(errno)); + + free(line); + check_and_fclose(log_fp, filename); + close(pipefd[0]); + _exit(1); + } else { + // Redirect stdout/stderr to the logger process. + // Close the unused read end. + close(pipefd[0]); + + setbuf(stdout, nullptr); + setbuf(stderr, nullptr); + + if (dup2(pipefd[1], STDOUT_FILENO) == -1) { + LOGE("dup2 stdout failed: %s\n", strerror(errno)); + } + if (dup2(pipefd[1], STDERR_FILENO) == -1) { + LOGE("dup2 stderr failed: %s\n", strerror(errno)); + } + + close(pipefd[1]); + } } // command line args come from, in decreasing precedence: @@ -745,10 +825,7 @@ static void choose_recovery_file(Device* device) { int chosen_item = get_menu_selection(headers, entries, 1, 0, device); if (strcmp(entries[chosen_item], "Back") == 0) break; - // TODO: do we need to redirect? ShowFile could just avoid writing to stdio. - redirect_stdio("/dev/null"); ui->ShowFile(entries[chosen_item]); - redirect_stdio(TEMPORARY_LOG_FILE); } for (size_t i = 0; i < (sizeof(entries) / sizeof(*entries)); i++) { @@ -767,6 +844,7 @@ static int apply_from_sdcard(Device* device, bool* wipe_cache) { char* path = browse_directory(SDCARD_ROOT, device); if (path == NULL) { ui->Print("\n-- No package file selected.\n"); + ensure_path_unmounted(SDCARD_ROOT); return INSTALL_ERROR; } @@ -929,10 +1007,6 @@ ui_print(const char* format, ...) { int main(int argc, char **argv) { - time_t start = time(NULL); - - redirect_stdio(TEMPORARY_LOG_FILE); - // If this binary is started with the single argument "--adbd", // instead of being the normal recovery binary, it turns into kind // of a stripped-down version of adbd that only supports the @@ -941,10 +1015,16 @@ main(int argc, char **argv) { // only way recovery should be run with this argument is when it // starts a copy of itself from the apply_from_adb() function. if (argc == 2 && strcmp(argv[1], "--adbd") == 0) { - adb_main(0, DEFAULT_ADB_PORT); + adb_server_main(0, DEFAULT_ADB_PORT, -1); return 0; } + time_t start = time(NULL); + + // redirect_stdio should be called only in non-sideload mode. Otherwise + // we may have two logger instances with different timestamps. + redirect_stdio(TEMPORARY_LOG_FILE); + printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start)); load_volume_table(); @@ -1035,11 +1115,15 @@ main(int argc, char **argv) { if (strncmp(update_package, "CACHE:", 6) == 0) { int len = strlen(update_package) + 10; char* modified_path = (char*)malloc(len); - strlcpy(modified_path, "/cache/", len); - strlcat(modified_path, update_package+6, len); - printf("(replacing path \"%s\" with \"%s\")\n", - update_package, modified_path); - update_package = modified_path; + if (modified_path) { + strlcpy(modified_path, "/cache/", len); + strlcat(modified_path, update_package+6, len); + printf("(replacing path \"%s\" with \"%s\")\n", + update_package, modified_path); + update_package = modified_path; + } + else + printf("modified_path allocation failed\n"); } } printf("\n"); @@ -1138,6 +1222,9 @@ main(int argc, char **argv) { property_set(ANDROID_RB_PROPERTY, "reboot,"); break; } - sleep(5); // should reboot before this finishes + while (true) { + pause(); + } + // Should be unreachable. return EXIT_SUCCESS; } diff --git a/res-560dpi b/res-560dpi new file mode 120000 index 000000000..8576a9b95 --- /dev/null +++ b/res-560dpi @@ -0,0 +1 @@ +res-xxhdpi
\ No newline at end of file diff --git a/screen_ui.cpp b/screen_ui.cpp index ff9591514..23fc90154 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -30,8 +30,10 @@ #include <vector> -#include "base/strings.h" -#include "cutils/properties.h" +#include <android-base/strings.h> +#include <android-base/stringprintf.h> +#include <cutils/properties.h> + #include "common.h" #include "device.h" #include "minui/minui.h" @@ -506,18 +508,17 @@ void ScreenRecoveryUI::SetStage(int current, int max) { pthread_mutex_unlock(&updateMutex); } -void ScreenRecoveryUI::Print(const char *fmt, ...) { - char buf[256]; - va_list ap; - va_start(ap, fmt); - vsnprintf(buf, 256, fmt, ap); - va_end(ap); +void ScreenRecoveryUI::PrintV(const char* fmt, bool copy_to_stdout, va_list ap) { + std::string str; + android::base::StringAppendV(&str, fmt, ap); - fputs(buf, stdout); + if (copy_to_stdout) { + fputs(str.c_str(), stdout); + } pthread_mutex_lock(&updateMutex); if (text_rows_ > 0 && text_cols_ > 0) { - for (const char* ptr = buf; *ptr != '\0'; ++ptr) { + for (const char* ptr = str.c_str(); *ptr != '\0'; ++ptr) { if (*ptr == '\n' || text_col_ >= text_cols_) { text_[text_row_][text_col_] = '\0'; text_col_ = 0; @@ -532,6 +533,20 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) { pthread_mutex_unlock(&updateMutex); } +void ScreenRecoveryUI::Print(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + PrintV(fmt, true, ap); + va_end(ap); +} + +void ScreenRecoveryUI::PrintOnScreenOnly(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + PrintV(fmt, false, ap); + va_end(ap); +} + void ScreenRecoveryUI::PutChar(char ch) { pthread_mutex_lock(&updateMutex); if (ch != '\n') text_[text_row_][text_col_++] = ch; @@ -566,7 +581,7 @@ void ScreenRecoveryUI::ShowFile(FILE* fp) { bool show_prompt = false; while (true) { if (show_prompt) { - Print("--(%d%% of %d bytes)--", + PrintOnScreenOnly("--(%d%% of %d bytes)--", static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))), static_cast<int>(sb.st_size)); Redraw(); diff --git a/screen_ui.h b/screen_ui.h index ea05bf15f..8e18864d7 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -49,6 +49,7 @@ class ScreenRecoveryUI : public RecoveryUI { // printing messages void Print(const char* fmt, ...) __printflike(2, 3); + void PrintOnScreenOnly(const char* fmt, ...) __printflike(2, 3); void ShowFile(const char* filename); // menu display @@ -125,6 +126,7 @@ class ScreenRecoveryUI : public RecoveryUI { void ProgressThreadLoop(); void ShowFile(FILE*); + void PrintV(const char*, bool, va_list); void PutChar(char); void ClearText(); @@ -28,6 +28,7 @@ #include <time.h> #include <unistd.h> +#include <cutils/properties.h> #include <cutils/android_reboot.h> #include "common.h" @@ -174,7 +175,8 @@ void RecoveryUI::ProcessKey(int key_code, int updown) { case RecoveryUI::REBOOT: if (reboot_enabled) { - android_reboot(ANDROID_RB_RESTART, 0, 0); + property_set(ANDROID_RB_PROPERTY, "reboot,"); + while (true) { pause(); } } break; @@ -62,8 +62,10 @@ class RecoveryUI { virtual bool WasTextEverVisible() = 0; // Write a message to the on-screen log (shown if the user has - // toggled on the text display). + // toggled on the text display). Print() will also dump the message + // to stdout / log file, while PrintOnScreenOnly() not. virtual void Print(const char* fmt, ...) __printflike(2, 3) = 0; + virtual void PrintOnScreenOnly(const char* fmt, ...) __printflike(2, 3) = 0; virtual void ShowFile(const char* filename) = 0; diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk index e73c8f1b6..6422cb2f4 100644 --- a/uncrypt/Android.mk +++ b/uncrypt/Android.mk @@ -20,8 +20,12 @@ LOCAL_CLANG := true LOCAL_SRC_FILES := uncrypt.cpp +LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. + LOCAL_MODULE := uncrypt LOCAL_STATIC_LIBRARIES := libbase liblog libfs_mgr libcutils +LOCAL_INIT_RC := uncrypt.rc + include $(BUILD_EXECUTABLE) diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 46da86d61..482504192 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -51,13 +51,19 @@ #include <sys/types.h> #include <unistd.h> -#include <base/file.h> -#include <base/strings.h> +#include <memory> + +#include <android-base/file.h> +#include <android-base/strings.h> +#include <cutils/android_reboot.h> #include <cutils/properties.h> #include <fs_mgr.h> + #define LOG_TAG "uncrypt" #include <log/log.h> +#include "unique_fd.h" + #define WINDOW_SIZE 5 static const std::string cache_block_map = "/cache/recovery/block.map"; @@ -180,7 +186,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* ALOGE("failed to open %s\n", map_file); return -1; } - FILE* mapf = fdopen(mapfd, "w"); + std::unique_ptr<FILE, int(*)(FILE*)> mapf(fdopen(mapfd, "w"), fclose); // Make sure we can write to the status_file. if (!android::base::WriteStringToFd("0\n", status_fd)) { @@ -189,8 +195,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* } struct stat sb; - int ret = stat(path, &sb); - if (ret != 0) { + if (stat(path, &sb) != 0) { ALOGE("failed to stat %s\n", path); return -1; } @@ -206,7 +211,8 @@ static int produce_block_map(const char* path, const char* map_file, const char* ranges[0] = -1; ranges[1] = -1; - fprintf(mapf, "%s\n%lld %lu\n", blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize); + fprintf(mapf.get(), "%s\n%lld %lu\n", + blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize); unsigned char* buffers[WINDOW_SIZE]; if (encrypted) { @@ -219,15 +225,18 @@ static int produce_block_map(const char* path, const char* map_file, const char* size_t pos = 0; int fd = open(path, O_RDONLY); - if (fd < 0) { + unique_fd fd_holder(fd); + if (fd == -1) { ALOGE("failed to open fd for reading: %s\n", strerror(errno)); return -1; } int wfd = -1; + unique_fd wfd_holder(wfd); if (encrypted) { - wfd = open(blk_dev, O_WRONLY | O_SYNC); - if (wfd < 0) { + wfd = open(blk_dev, O_WRONLY); + wfd_holder = unique_fd(wfd); + if (wfd == -1) { ALOGE("failed to open fd for writing: %s\n", strerror(errno)); return -1; } @@ -245,8 +254,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* if ((tail+1) % WINDOW_SIZE == head) { // write out head buffer int block = head_block; - ret = ioctl(fd, FIBMAP, &block); - if (ret != 0) { + if (ioctl(fd, FIBMAP, &block) != 0) { ALOGE("failed to find block %d\n", head_block); return -1; } @@ -286,8 +294,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* while (head != tail) { // write out head buffer int block = head_block; - ret = ioctl(fd, FIBMAP, &block); - if (ret != 0) { + if (ioctl(fd, FIBMAP, &block) != 0) { ALOGE("failed to find block %d\n", head_block); return -1; } @@ -302,23 +309,20 @@ static int produce_block_map(const char* path, const char* map_file, const char* ++head_block; } - fprintf(mapf, "%d\n", range_used); + fprintf(mapf.get(), "%d\n", range_used); for (int i = 0; i < range_used; ++i) { - fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); + fprintf(mapf.get(), "%d %d\n", ranges[i*2], ranges[i*2+1]); } if (fsync(mapfd) == -1) { ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno)); return -1; } - fclose(mapf); - close(fd); if (encrypted) { if (fsync(wfd) == -1) { ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno)); return -1; } - close(wfd); } return 0; @@ -331,6 +335,8 @@ static void wipe_misc() { if (!v->mount_point) continue; if (strcmp(v->mount_point, "/misc") == 0) { int fd = open(v->blk_device, O_WRONLY | O_SYNC); + unique_fd fd_holder(fd); + uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery memset(zeroes, 0, sizeof(zeroes)); @@ -347,10 +353,8 @@ static void wipe_misc() { } if (fsync(fd) == -1) { ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); - close(fd); return; } - close(fd); } } } @@ -358,7 +362,9 @@ static void wipe_misc() { static void reboot_to_recovery() { ALOGI("rebooting to recovery"); property_set("sys.powerctl", "reboot,recovery"); - sleep(10); + while (true) { + pause(); + } ALOGE("reboot didn't succeed?"); } @@ -433,6 +439,7 @@ int main(int argc, char** argv) { ALOGE("failed to open pipe \"%s\": %s\n", status_file.c_str(), strerror(errno)); return 1; } + unique_fd status_fd_holder(status_fd); if (argc == 3) { // when command-line args are given this binary is being used @@ -443,7 +450,6 @@ int main(int argc, char** argv) { std::string package; if (!find_uncrypt_package(package)) { android::base::WriteStringToFd("-1\n", status_fd); - close(status_fd); return 1; } input_path = package.c_str(); @@ -453,12 +459,10 @@ int main(int argc, char** argv) { int status = uncrypt(input_path, map_file, status_fd); if (status != 0) { android::base::WriteStringToFd("-1\n", status_fd); - close(status_fd); return 1; } android::base::WriteStringToFd("100\n", status_fd); - close(status_fd); } return 0; diff --git a/uncrypt/uncrypt.rc b/uncrypt/uncrypt.rc new file mode 100644 index 000000000..5f4c47936 --- /dev/null +++ b/uncrypt/uncrypt.rc @@ -0,0 +1,9 @@ +service uncrypt /system/bin/uncrypt + class main + disabled + oneshot + +service pre-recovery /system/bin/uncrypt --reboot + class main + disabled + oneshot diff --git a/unique_fd.h b/unique_fd.h new file mode 100644 index 000000000..cc85383f8 --- /dev/null +++ b/unique_fd.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UNIQUE_FD_H +#define UNIQUE_FD_H + +#include <stdio.h> + +#include <memory> + +class unique_fd { + public: + unique_fd(int fd) : fd_(fd) { } + + unique_fd(unique_fd&& uf) { + fd_ = uf.fd_; + uf.fd_ = -1; + } + + ~unique_fd() { + if (fd_ != -1) { + close(fd_); + } + } + + int get() { + return fd_; + } + + // Movable. + unique_fd& operator=(unique_fd&& uf) { + fd_ = uf.fd_; + uf.fd_ = -1; + return *this; + } + + explicit operator bool() const { + return fd_ != -1; + } + + private: + int fd_; + + // Non-copyable. + unique_fd(const unique_fd&) = delete; + unique_fd& operator=(const unique_fd&) = delete; +}; + +#endif // UNIQUE_FD_H diff --git a/updater/Android.mk b/updater/Android.mk index 82fa7e265..dcf437474 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -33,12 +33,13 @@ LOCAL_CLANG := true LOCAL_SRC_FILES := $(updater_src_files) +LOCAL_STATIC_LIBRARIES += libfec libfec_rs libext4_utils_static libsquashfs_utils libcrypto_static + ifeq ($(TARGET_USERIMAGES_USE_EXT4), true) LOCAL_CFLAGS += -DUSE_EXT4 LOCAL_CFLAGS += -Wno-unused-parameter LOCAL_C_INCLUDES += system/extras/ext4_utils LOCAL_STATIC_LIBRARIES += \ - libext4_utils_static \ libsparse_static \ libz endif diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp index 310bbf94c..a9d8cc68c 100644 --- a/updater/blockimg.cpp +++ b/updater/blockimg.cpp @@ -19,7 +19,6 @@ #include <dirent.h> #include <fcntl.h> #include <inttypes.h> -#include <libgen.h> #include <linux/fs.h> #include <pthread.h> #include <stdarg.h> @@ -32,11 +31,21 @@ #include <sys/ioctl.h> #include <time.h> #include <unistd.h> +#include <fec/io.h> + +#include <memory> +#include <string> +#include <vector> + +#include <android-base/parseint.h> +#include <android-base/strings.h> #include "applypatch/applypatch.h" #include "edify/expr.h" #include "mincrypt/sha.h" #include "minzip/Hash.h" +#include "print_sha1.h" +#include "unique_fd.h" #include "updater.h" #define BLOCKSIZE 4096 @@ -50,123 +59,87 @@ #define STASH_DIRECTORY_MODE 0700 #define STASH_FILE_MODE 0600 -char* PrintSha1(const uint8_t* digest); +struct RangeSet { + size_t count; // Limit is INT_MAX. + size_t size; + std::vector<size_t> pos; // Actual limit is INT_MAX. +}; -typedef struct { - int count; - int size; - int pos[0]; -} RangeSet; +static void parse_range(const char* range_text, RangeSet& rs) { -#define RANGESET_MAX_POINTS \ - ((int)((INT_MAX / sizeof(int)) - sizeof(RangeSet))) + if (range_text == nullptr) { + fprintf(stderr, "failed to parse range: null range\n"); + exit(1); + } -static RangeSet* parse_range(char* text) { - char* save; - char* token; - int num; + std::vector<std::string> pieces = android::base::Split(std::string(range_text), ","); long int val; - RangeSet* out = NULL; - size_t bufsize; - if (!text) { + if (pieces.size() < 3) { goto err; } - token = strtok_r(text, ",", &save); - - if (!token) { + size_t num; + if (!android::base::ParseUint(pieces[0].c_str(), &num, static_cast<size_t>(INT_MAX))) { goto err; } - val = strtol(token, NULL, 0); - - if (val < 2 || val > RANGESET_MAX_POINTS) { - goto err; - } else if (val % 2) { + if (num == 0 || num % 2) { goto err; // must be even - } - - num = (int) val; - bufsize = sizeof(RangeSet) + num * sizeof(int); - - out = reinterpret_cast<RangeSet*>(malloc(bufsize)); - - if (!out) { - fprintf(stderr, "failed to allocate range of %zu bytes\n", bufsize); + } else if (num != pieces.size() - 1) { goto err; } - out->count = num / 2; - out->size = 0; + rs.pos.resize(num); + rs.count = num / 2; + rs.size = 0; - for (int i = 0; i < num; ++i) { - token = strtok_r(NULL, ",", &save); - - if (!token) { + for (size_t i = 0; i < num; i += 2) { + if (!android::base::ParseUint(pieces[i+1].c_str(), &rs.pos[i], + static_cast<size_t>(INT_MAX))) { goto err; } - val = strtol(token, NULL, 0); - - if (val < 0 || val > INT_MAX) { + if (!android::base::ParseUint(pieces[i+2].c_str(), &rs.pos[i+1], + static_cast<size_t>(INT_MAX))) { goto err; } - out->pos[i] = (int) val; - - if (i % 2) { - if (out->pos[i - 1] >= out->pos[i]) { - goto err; // empty or negative range - } - - if (out->size > INT_MAX - out->pos[i]) { - goto err; // overflow - } - - out->size += out->pos[i]; - } else { - if (out->size < 0) { - goto err; - } + if (rs.pos[i] >= rs.pos[i+1]) { + goto err; // empty or negative range + } - out->size -= out->pos[i]; + size_t sz = rs.pos[i+1] - rs.pos[i]; + if (rs.size > SIZE_MAX - sz) { + goto err; // overflow } - } - if (out->size <= 0) { - goto err; + rs.size += sz; } - return out; + return; err: - fprintf(stderr, "failed to parse range '%s'\n", text ? text : "NULL"); + fprintf(stderr, "failed to parse range '%s'\n", range_text); exit(1); } -static int range_overlaps(RangeSet* r1, RangeSet* r2) { - int i, j, r1_0, r1_1, r2_0, r2_1; - - if (!r1 || !r2) { - return 0; - } - - for (i = 0; i < r1->count; ++i) { - r1_0 = r1->pos[i * 2]; - r1_1 = r1->pos[i * 2 + 1]; +static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) { + for (size_t i = 0; i < r1.count; ++i) { + size_t r1_0 = r1.pos[i * 2]; + size_t r1_1 = r1.pos[i * 2 + 1]; - for (j = 0; j < r2->count; ++j) { - r2_0 = r2->pos[j * 2]; - r2_1 = r2->pos[j * 2 + 1]; + for (size_t j = 0; j < r2.count; ++j) { + size_t r2_0 = r2.pos[j * 2]; + size_t r2_1 = r2.pos[j * 2 + 1]; if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) { - return 1; + return true; } } } - return 0; + return false; } static int read_all(int fd, uint8_t* data, size_t size) { @@ -182,6 +155,10 @@ static int read_all(int fd, uint8_t* data, size_t size) { return 0; } +static int read_all(int fd, std::vector<uint8_t>& buffer, size_t size) { + return read_all(fd, buffer.data(), size); +} + static int write_all(int fd, const uint8_t* data, size_t size) { size_t written = 0; while (written < size) { @@ -193,14 +170,13 @@ static int write_all(int fd, const uint8_t* data, size_t size) { written += w; } - if (fsync(fd) == -1) { - fprintf(stderr, "fsync failed: %s\n", strerror(errno)); - return -1; - } - return 0; } +static int write_all(int fd, const std::vector<uint8_t>& buffer, size_t size) { + return write_all(fd, buffer.data(), size); +} + static bool check_lseek(int fd, off64_t offset, int whence) { off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)); if (rc == -1) { @@ -210,31 +186,26 @@ static bool check_lseek(int fd, off64_t offset, int whence) { return true; } -static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) { +static void allocate(size_t size, std::vector<uint8_t>& buffer) { // if the buffer's big enough, reuse it. - if (size <= *buffer_alloc) return; + if (size <= buffer.size()) return; - free(*buffer); - - *buffer = (uint8_t*) malloc(size); - if (*buffer == NULL) { - fprintf(stderr, "failed to allocate %zu bytes\n", size); - exit(1); - } - *buffer_alloc = size; + buffer.resize(size); } -typedef struct { +struct RangeSinkState { + RangeSinkState(RangeSet& rs) : tgt(rs) { }; + int fd; - RangeSet* tgt; - int p_block; + const RangeSet& tgt; + size_t p_block; size_t p_remain; -} RangeSinkState; +}; static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) { - RangeSinkState* rss = (RangeSinkState*) token; + RangeSinkState* rss = reinterpret_cast<RangeSinkState*>(token); - if (rss->p_remain <= 0) { + if (rss->p_remain == 0) { fprintf(stderr, "range sink write overrun"); return 0; } @@ -260,11 +231,11 @@ static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) { if (rss->p_remain == 0) { // move to the next block ++rss->p_block; - if (rss->p_block < rss->tgt->count) { - rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] - - rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE; + if (rss->p_block < rss->tgt.count) { + rss->p_remain = (rss->tgt.pos[rss->p_block * 2 + 1] - + rss->tgt.pos[rss->p_block * 2]) * BLOCKSIZE; - if (!check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE, + if (!check_lseek(rss->fd, (off64_t)rss->tgt.pos[rss->p_block*2] * BLOCKSIZE, SEEK_SET)) { break; } @@ -300,7 +271,7 @@ static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) { // condition. When the background thread is done writing, it clears // rss and signals the condition again. -typedef struct { +struct NewThreadInfo { ZipArchive* za; const ZipEntry* entry; @@ -308,16 +279,16 @@ typedef struct { pthread_mutex_t mu; pthread_cond_t cv; -} NewThreadInfo; +}; static bool receive_new_data(const unsigned char* data, int size, void* cookie) { - NewThreadInfo* nti = (NewThreadInfo*) cookie; + NewThreadInfo* nti = reinterpret_cast<NewThreadInfo*>(cookie); while (size > 0) { - // Wait for nti->rss to be non-NULL, indicating some of this + // Wait for nti->rss to be non-null, indicating some of this // data is wanted. pthread_mutex_lock(&nti->mu); - while (nti->rss == NULL) { + while (nti->rss == nullptr) { pthread_cond_wait(&nti->cv, &nti->mu); } pthread_mutex_unlock(&nti->mu); @@ -328,11 +299,11 @@ static bool receive_new_data(const unsigned char* data, int size, void* cookie) data += written; size -= written; - if (nti->rss->p_block == nti->rss->tgt->count) { + if (nti->rss->p_block == nti->rss->tgt.count) { // we have written all the bytes desired by this rss. pthread_mutex_lock(&nti->mu); - nti->rss = NULL; + nti->rss = nullptr; pthread_cond_broadcast(&nti->cv); pthread_mutex_unlock(&nti->mu); } @@ -344,26 +315,21 @@ static bool receive_new_data(const unsigned char* data, int size, void* cookie) static void* unzip_new_data(void* cookie) { NewThreadInfo* nti = (NewThreadInfo*) cookie; mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti); - return NULL; + return nullptr; } -static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) { - int i; +static int ReadBlocks(const RangeSet& src, std::vector<uint8_t>& buffer, int fd) { size_t p = 0; - size_t size; + uint8_t* data = buffer.data(); - if (!src || !buffer) { - return -1; - } - - for (i = 0; i < src->count; ++i) { - if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) { + for (size_t i = 0; i < src.count; ++i) { + if (!check_lseek(fd, (off64_t) src.pos[i * 2] * BLOCKSIZE, SEEK_SET)) { return -1; } - size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE; + size_t size = (src.pos[i * 2 + 1] - src.pos[i * 2]) * BLOCKSIZE; - if (read_all(fd, buffer + p, size) == -1) { + if (read_all(fd, data + p, size) == -1) { return -1; } @@ -373,23 +339,18 @@ static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) { return 0; } -static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) { - int i; - size_t p = 0; - size_t size; - - if (!tgt || !buffer) { - return -1; - } +static int WriteBlocks(const RangeSet& tgt, const std::vector<uint8_t>& buffer, int fd) { + const uint8_t* data = buffer.data(); - for (i = 0; i < tgt->count; ++i) { - if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) { + size_t p = 0; + for (size_t i = 0; i < tgt.count; ++i) { + if (!check_lseek(fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) { return -1; } - size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE; + size_t size = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * BLOCKSIZE; - if (write_all(fd, buffer + p, size) == -1) { + if (write_all(fd, data + p, size) == -1) { return -1; } @@ -406,541 +367,402 @@ static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) { // <src_range> <tgt_range> // // The source range is loaded into the provided buffer, reallocating -// it to make it larger if necessary. The target ranges are returned -// in *tgt, if tgt is non-NULL. - -static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks, - uint8_t** buffer, size_t* buffer_alloc, int fd) { - char* word; - int rc; +// it to make it larger if necessary. - word = strtok_r(NULL, " ", wordsave); - RangeSet* src = parse_range(word); +static int LoadSrcTgtVersion1(char** wordsave, RangeSet& tgt, size_t& src_blocks, + std::vector<uint8_t>& buffer, int fd) { + // <src_range> + char* word = strtok_r(nullptr, " ", wordsave); + RangeSet src; + parse_range(word, src); - if (tgt != NULL) { - word = strtok_r(NULL, " ", wordsave); - *tgt = parse_range(word); - } + // <tgt_range> + word = strtok_r(nullptr, " ", wordsave); + parse_range(word, tgt); - allocate(src->size * BLOCKSIZE, buffer, buffer_alloc); - rc = ReadBlocks(src, *buffer, fd); - *src_blocks = src->size; + allocate(src.size * BLOCKSIZE, buffer); + int rc = ReadBlocks(src, buffer, fd); + src_blocks = src.size; - free(src); return rc; } -static int VerifyBlocks(const char *expected, const uint8_t *buffer, - size_t blocks, int printerror) { - char* hexdigest = NULL; - int rc = -1; +static int VerifyBlocks(const std::string& expected, const std::vector<uint8_t>& buffer, + const size_t blocks, bool printerror) { uint8_t digest[SHA_DIGEST_SIZE]; + const uint8_t* data = buffer.data(); - if (!expected || !buffer) { - return rc; - } - - SHA_hash(buffer, blocks * BLOCKSIZE, digest); - hexdigest = PrintSha1(digest); + SHA_hash(data, blocks * BLOCKSIZE, digest); - if (hexdigest != NULL) { - rc = strcmp(expected, hexdigest); + std::string hexdigest = print_sha1(digest); - if (rc != 0 && printerror) { + if (hexdigest != expected) { + if (printerror) { fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n", - expected, hexdigest); + expected.c_str(), hexdigest.c_str()); } - - free(hexdigest); + return -1; } - return rc; + return 0; } -static char* GetStashFileName(const char* base, const char* id, const char* postfix) { - char* fn; - int len; - int res; - - if (base == NULL) { - return NULL; - } - - if (id == NULL) { - id = ""; - } - - if (postfix == NULL) { - postfix = ""; +static std::string GetStashFileName(const std::string& base, const std::string& id, + const std::string& postfix) { + if (base.empty()) { + return ""; } - len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1; - fn = reinterpret_cast<char*>(malloc(len)); - - if (fn == NULL) { - fprintf(stderr, "failed to malloc %d bytes for fn\n", len); - return NULL; - } - - res = snprintf(fn, len, STASH_DIRECTORY_BASE "/%s/%s%s", base, id, postfix); - - if (res < 0 || res >= len) { - fprintf(stderr, "failed to format file name (return value %d)\n", res); - free(fn); - return NULL; - } + std::string fn(STASH_DIRECTORY_BASE); + fn += "/" + base + "/" + id + postfix; return fn; } -typedef void (*StashCallback)(const char*, void*); +typedef void (*StashCallback)(const std::string&, void*); // Does a best effort enumeration of stash files. Ignores possible non-file // items in the stash directory and continues despite of errors. Calls the // 'callback' function for each file and passes 'data' to the function as a // parameter. -static void EnumerateStash(const char* dirname, StashCallback callback, void* data) { - char* fn; - DIR* directory; - int len; - int res; - struct dirent* item; - - if (dirname == NULL || callback == NULL) { +static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) { + if (dirname.empty() || callback == nullptr) { return; } - directory = opendir(dirname); + std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir); - if (directory == NULL) { + if (directory == nullptr) { if (errno != ENOENT) { - fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname, strerror(errno)); + fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno)); } return; } - while ((item = readdir(directory)) != NULL) { + struct dirent* item; + while ((item = readdir(directory.get())) != nullptr) { if (item->d_type != DT_REG) { continue; } - len = strlen(dirname) + 1 + strlen(item->d_name) + 1; - fn = reinterpret_cast<char*>(malloc(len)); - - if (fn == NULL) { - fprintf(stderr, "failed to malloc %d bytes for fn\n", len); - continue; - } - - res = snprintf(fn, len, "%s/%s", dirname, item->d_name); - - if (res < 0 || res >= len) { - fprintf(stderr, "failed to format file name (return value %d)\n", res); - free(fn); - continue; - } - + std::string fn = dirname + "/" + std::string(item->d_name); callback(fn, data); - free(fn); - } - - if (closedir(directory) == -1) { - fprintf(stderr, "closedir \"%s\" failed: %s\n", dirname, strerror(errno)); } } -static void UpdateFileSize(const char* fn, void* data) { - int* size = (int*) data; - struct stat st; - - if (!fn || !data) { +static void UpdateFileSize(const std::string& fn, void* data) { + if (fn.empty() || !data) { return; } - if (stat(fn, &st) == -1) { - fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno)); + struct stat sb; + if (stat(fn.c_str(), &sb) == -1) { + fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); return; } - *size += st.st_size; + int* size = reinterpret_cast<int*>(data); + *size += sb.st_size; } // Deletes the stash directory and all files in it. Assumes that it only // contains files. There is nothing we can do about unlikely, but possible // errors, so they are merely logged. -static void DeleteFile(const char* fn, void* data) { - if (fn) { - fprintf(stderr, "deleting %s\n", fn); +static void DeleteFile(const std::string& fn, void* /* data */) { + if (!fn.empty()) { + fprintf(stderr, "deleting %s\n", fn.c_str()); - if (unlink(fn) == -1 && errno != ENOENT) { - fprintf(stderr, "unlink \"%s\" failed: %s\n", fn, strerror(errno)); + if (unlink(fn.c_str()) == -1 && errno != ENOENT) { + fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); } } } -static void DeletePartial(const char* fn, void* data) { - if (fn && strstr(fn, ".partial") != NULL) { +static void DeletePartial(const std::string& fn, void* data) { + if (android::base::EndsWith(fn, ".partial")) { DeleteFile(fn, data); } } -static void DeleteStash(const char* base) { - char* dirname; - - if (base == NULL) { +static void DeleteStash(const std::string& base) { + if (base.empty()) { return; } - dirname = GetStashFileName(base, NULL, NULL); + fprintf(stderr, "deleting stash %s\n", base.c_str()); - if (dirname == NULL) { - return; - } - - fprintf(stderr, "deleting stash %s\n", base); - EnumerateStash(dirname, DeleteFile, NULL); + std::string dirname = GetStashFileName(base, "", ""); + EnumerateStash(dirname, DeleteFile, nullptr); - if (rmdir(dirname) == -1) { + if (rmdir(dirname.c_str()) == -1) { if (errno != ENOENT && errno != ENOTDIR) { - fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname, strerror(errno)); + fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno)); } } - - free(dirname); } -static int LoadStash(const char* base, const char* id, int verify, int* blocks, uint8_t** buffer, - size_t* buffer_alloc, int printnoent) { - char *fn = NULL; - int blockcount = 0; - int fd = -1; - int rc = -1; - int res; - struct stat st; - - if (!base || !id || !buffer || !buffer_alloc) { - goto lsout; +static int LoadStash(const std::string& base, const std::string& id, bool verify, size_t* blocks, + std::vector<uint8_t>& buffer, bool printnoent) { + if (base.empty()) { + return -1; } + size_t blockcount = 0; + if (!blocks) { blocks = &blockcount; } - fn = GetStashFileName(base, id, NULL); - - if (fn == NULL) { - goto lsout; - } + std::string fn = GetStashFileName(base, id, ""); - res = stat(fn, &st); + struct stat sb; + int res = stat(fn.c_str(), &sb); if (res == -1) { if (errno != ENOENT || printnoent) { - fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno)); + fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); } - goto lsout; + return -1; } - fprintf(stderr, " loading %s\n", fn); + fprintf(stderr, " loading %s\n", fn.c_str()); - if ((st.st_size % BLOCKSIZE) != 0) { + if ((sb.st_size % BLOCKSIZE) != 0) { fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d", - fn, static_cast<int64_t>(st.st_size), BLOCKSIZE); - goto lsout; + fn.c_str(), static_cast<int64_t>(sb.st_size), BLOCKSIZE); + return -1; } - fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY)); + int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)); + unique_fd fd_holder(fd); if (fd == -1) { - fprintf(stderr, "open \"%s\" failed: %s\n", fn, strerror(errno)); - goto lsout; - } - - allocate(st.st_size, buffer, buffer_alloc); - - if (read_all(fd, *buffer, st.st_size) == -1) { - goto lsout; + fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); + return -1; } - *blocks = st.st_size / BLOCKSIZE; + allocate(sb.st_size, buffer); - if (verify && VerifyBlocks(id, *buffer, *blocks, 1) != 0) { - fprintf(stderr, "unexpected contents in %s\n", fn); - DeleteFile(fn, NULL); - goto lsout; + if (read_all(fd, buffer, sb.st_size) == -1) { + return -1; } - rc = 0; + *blocks = sb.st_size / BLOCKSIZE; -lsout: - if (fd != -1) { - close(fd); - } - - if (fn) { - free(fn); + if (verify && VerifyBlocks(id, buffer, *blocks, true) != 0) { + fprintf(stderr, "unexpected contents in %s\n", fn.c_str()); + DeleteFile(fn, nullptr); + return -1; } - return rc; + return 0; } -static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer, - int checkspace, int *exists) { - char *fn = NULL; - char *cn = NULL; - int fd = -1; - int rc = -1; - int dfd = -1; - int res; - struct stat st; - - if (base == NULL || buffer == NULL) { - goto wsout; +static int WriteStash(const std::string& base, const std::string& id, int blocks, + std::vector<uint8_t>& buffer, bool checkspace, bool *exists) { + if (base.empty()) { + return -1; } if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) { fprintf(stderr, "not enough space to write stash\n"); - goto wsout; + return -1; } - fn = GetStashFileName(base, id, ".partial"); - cn = GetStashFileName(base, id, NULL); - - if (fn == NULL || cn == NULL) { - goto wsout; - } + std::string fn = GetStashFileName(base, id, ".partial"); + std::string cn = GetStashFileName(base, id, ""); if (exists) { - res = stat(cn, &st); + struct stat sb; + int res = stat(cn.c_str(), &sb); if (res == 0) { // The file already exists and since the name is the hash of the contents, // it's safe to assume the contents are identical (accidental hash collisions // are unlikely) - fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn); - *exists = 1; - rc = 0; - goto wsout; + fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str()); + *exists = true; + return 0; } - *exists = 0; + *exists = false; } - fprintf(stderr, " writing %d blocks to %s\n", blocks, cn); + fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str()); - fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, STASH_FILE_MODE)); + int fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE)); + unique_fd fd_holder(fd); if (fd == -1) { - fprintf(stderr, "failed to create \"%s\": %s\n", fn, strerror(errno)); - goto wsout; + fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno)); + return -1; } if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) { - goto wsout; + return -1; } if (fsync(fd) == -1) { - fprintf(stderr, "fsync \"%s\" failed: %s\n", fn, strerror(errno)); - goto wsout; + fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno)); + return -1; } - if (rename(fn, cn) == -1) { - fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn, cn, strerror(errno)); - goto wsout; + if (rename(fn.c_str(), cn.c_str()) == -1) { + fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(), + strerror(errno)); + return -1; } - const char* dname; - dname = dirname(cn); - dfd = TEMP_FAILURE_RETRY(open(dname, O_RDONLY | O_DIRECTORY)); + std::string dname = GetStashFileName(base, "", ""); + int dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY)); + unique_fd dfd_holder(dfd); if (dfd == -1) { - fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname, strerror(errno)); - goto wsout; + fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno)); + return -1; } if (fsync(dfd) == -1) { - fprintf(stderr, "fsync \"%s\" failed: %s\n", dname, strerror(errno)); - goto wsout; - } - - rc = 0; - -wsout: - if (fd != -1) { - close(fd); - } - - if (dfd != -1) { - close(dfd); - } - - if (fn) { - free(fn); - } - - if (cn) { - free(cn); + fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno)); + return -1; } - return rc; + return 0; } // Creates a directory for storing stash files and checks if the /cache partition // hash enough space for the expected amount of blocks we need to store. Returns // >0 if we created the directory, zero if it existed already, and <0 of failure. -static int CreateStash(State* state, int maxblocks, const char* blockdev, char** base) { - char* dirname = NULL; - const uint8_t* digest; - int rc = -1; - int res; - int size = 0; - SHA_CTX ctx; - struct stat st; - - if (blockdev == NULL || base == NULL) { - goto csout; +static int CreateStash(State* state, int maxblocks, const char* blockdev, std::string& base) { + if (blockdev == nullptr) { + return -1; } // Stash directory should be different for each partition to avoid conflicts // when updating multiple partitions at the same time, so we use the hash of // the block device name as the base directory + SHA_CTX ctx; SHA_init(&ctx); SHA_update(&ctx, blockdev, strlen(blockdev)); - digest = SHA_final(&ctx); - *base = PrintSha1(digest); - - if (*base == NULL) { - goto csout; - } - - dirname = GetStashFileName(*base, NULL, NULL); - - if (dirname == NULL) { - goto csout; - } + const uint8_t* digest = SHA_final(&ctx); + base = print_sha1(digest); - res = stat(dirname, &st); + std::string dirname = GetStashFileName(base, "", ""); + struct stat sb; + int res = stat(dirname.c_str(), &sb); if (res == -1 && errno != ENOENT) { - ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname, strerror(errno)); - goto csout; + ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname.c_str(), strerror(errno)); + return -1; } else if (res != 0) { - fprintf(stderr, "creating stash %s\n", dirname); - res = mkdir(dirname, STASH_DIRECTORY_MODE); + fprintf(stderr, "creating stash %s\n", dirname.c_str()); + res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE); if (res != 0) { - ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname, strerror(errno)); - goto csout; + ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno)); + return -1; } if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) { ErrorAbort(state, "not enough space for stash\n"); - goto csout; + return -1; } - rc = 1; // Created directory - goto csout; + return 1; // Created directory } - fprintf(stderr, "using existing stash %s\n", dirname); + fprintf(stderr, "using existing stash %s\n", dirname.c_str()); // If the directory already exists, calculate the space already allocated to // stash files and check if there's enough for all required blocks. Delete any // partially completed stash files first. - EnumerateStash(dirname, DeletePartial, NULL); + EnumerateStash(dirname, DeletePartial, nullptr); + int size = 0; EnumerateStash(dirname, UpdateFileSize, &size); - size = (maxblocks * BLOCKSIZE) - size; + size = maxblocks * BLOCKSIZE - size; if (size > 0 && CacheSizeCheck(size) != 0) { ErrorAbort(state, "not enough space for stash (%d more needed)\n", size); - goto csout; - } - - rc = 0; // Using existing directory - -csout: - if (dirname) { - free(dirname); + return -1; } - return rc; + return 0; // Using existing directory } -static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t* buffer_alloc, - int fd, int usehash, int* isunresumable) { - char *id = NULL; - int blocks = 0; - - if (!wordsave || !buffer || !buffer_alloc || !isunresumable) { +static int SaveStash(const std::string& base, char** wordsave, std::vector<uint8_t>& buffer, + int fd, bool usehash) { + if (!wordsave) { return -1; } - id = strtok_r(NULL, " ", wordsave); - - if (id == NULL) { + char *id_tok = strtok_r(nullptr, " ", wordsave); + if (id_tok == nullptr) { fprintf(stderr, "missing id field in stash command\n"); return -1; } + std::string id(id_tok); - if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, 0) == 0) { + size_t blocks = 0; + if (usehash && LoadStash(base, id, true, &blocks, buffer, false) == 0) { // Stash file already exists and has expected contents. Do not // read from source again, as the source may have been already // overwritten during a previous attempt. return 0; } - if (LoadSrcTgtVersion1(wordsave, NULL, &blocks, buffer, buffer_alloc, fd) == -1) { + char* word = strtok_r(nullptr, " ", wordsave); + RangeSet src; + parse_range(word, src); + + allocate(src.size * BLOCKSIZE, buffer); + if (ReadBlocks(src, buffer, fd) == -1) { return -1; } + blocks = src.size; - if (usehash && VerifyBlocks(id, *buffer, blocks, 1) != 0) { + if (usehash && VerifyBlocks(id, buffer, blocks, true) != 0) { // Source blocks have unexpected contents. If we actually need this // data later, this is an unrecoverable error. However, the command // that uses the data may have already completed previously, so the // possible failure will occur during source block verification. - fprintf(stderr, "failed to load source blocks for stash %s\n", id); + fprintf(stderr, "failed to load source blocks for stash %s\n", id.c_str()); return 0; } - fprintf(stderr, "stashing %d blocks to %s\n", blocks, id); - return WriteStash(base, id, blocks, *buffer, 0, NULL); + fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str()); + return WriteStash(base, id, blocks, buffer, false, nullptr); } -static int FreeStash(const char* base, const char* id) { - char *fn = NULL; - - if (base == NULL || id == NULL) { +static int FreeStash(const std::string& base, const char* id) { + if (base.empty() || id == nullptr) { return -1; } - fn = GetStashFileName(base, id, NULL); + std::string fn = GetStashFileName(base, std::string(id), ""); - if (fn == NULL) { - return -1; - } - - DeleteFile(fn, NULL); - free(fn); + DeleteFile(fn, nullptr); return 0; } -static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) { +static void MoveRange(std::vector<uint8_t>& dest, const RangeSet& locs, + const std::vector<uint8_t>& source) { // source contains packed data, which we want to move to the - // locations given in *locs in the dest buffer. source and dest + // locations given in locs in the dest buffer. source and dest // may be the same buffer. - int start = locs->size; - int i; - for (i = locs->count-1; i >= 0; --i) { - int blocks = locs->pos[i*2+1] - locs->pos[i*2]; + const uint8_t* from = source.data(); + uint8_t* to = dest.data(); + size_t start = locs.size; + for (int i = locs.count-1; i >= 0; --i) { + size_t blocks = locs.pos[i*2+1] - locs.pos[i*2]; start -= blocks; - memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE), + memmove(to + (locs.pos[i*2] * BLOCKSIZE), from + (start * BLOCKSIZE), blocks * BLOCKSIZE); } } @@ -963,63 +785,57 @@ static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) { // reallocated if needed to accommodate the source data. *tgt is the // target RangeSet. Any stashes required are loaded using LoadStash. -static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks, - uint8_t** buffer, size_t* buffer_alloc, int fd, - const char* stashbase, int* overlap) { - char* word; - char* colonsave; - char* colon; - int res; - RangeSet* locs; - size_t stashalloc = 0; - uint8_t* stash = NULL; - - if (tgt != NULL) { - word = strtok_r(NULL, " ", wordsave); - *tgt = parse_range(word); - } +static int LoadSrcTgtVersion2(char** wordsave, RangeSet& tgt, size_t& src_blocks, + std::vector<uint8_t>& buffer, int fd, const std::string& stashbase, bool* overlap) { + // <tgt_range> + char* word = strtok_r(nullptr, " ", wordsave); + parse_range(word, tgt); - word = strtok_r(NULL, " ", wordsave); - *src_blocks = strtol(word, NULL, 0); + // <src_block_count> + word = strtok_r(nullptr, " ", wordsave); + android::base::ParseUint(word, &src_blocks); - allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc); + allocate(src_blocks * BLOCKSIZE, buffer); - word = strtok_r(NULL, " ", wordsave); + // "-" or <src_range> [<src_loc>] + word = strtok_r(nullptr, " ", wordsave); if (word[0] == '-' && word[1] == '\0') { // no source ranges, only stashes } else { - RangeSet* src = parse_range(word); - res = ReadBlocks(src, *buffer, fd); + RangeSet src; + parse_range(word, src); + int res = ReadBlocks(src, buffer, fd); - if (overlap && tgt) { - *overlap = range_overlaps(src, *tgt); + if (overlap) { + *overlap = range_overlaps(src, tgt); } - free(src); - if (res == -1) { return -1; } - word = strtok_r(NULL, " ", wordsave); - if (word == NULL) { + word = strtok_r(nullptr, " ", wordsave); + if (word == nullptr) { // no stashes, only source range return 0; } - locs = parse_range(word); - MoveRange(*buffer, locs, *buffer); - free(locs); + RangeSet locs; + parse_range(word, locs); + MoveRange(buffer, locs, buffer); } - while ((word = strtok_r(NULL, " ", wordsave)) != NULL) { + // <[stash_id:stash-range]> + char* colonsave; + while ((word = strtok_r(nullptr, " ", wordsave)) != nullptr) { // Each word is a an index into the stash table, a colon, and // then a rangeset describing where in the source block that // stashed data should go. - colonsave = NULL; - colon = strtok_r(word, ":", &colonsave); + colonsave = nullptr; + char* colon = strtok_r(word, ":", &colonsave); - res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, 1); + std::vector<uint8_t> stash; + int res = LoadStash(stashbase, std::string(colon), false, nullptr, stash, true); if (res == -1) { // These source blocks will fail verification if used later, but we @@ -1028,39 +844,34 @@ static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks, continue; } - colon = strtok_r(NULL, ":", &colonsave); - locs = parse_range(colon); + colon = strtok_r(nullptr, ":", &colonsave); + RangeSet locs; + parse_range(colon, locs); - MoveRange(*buffer, locs, stash); - free(locs); - } - - if (stash) { - free(stash); + MoveRange(buffer, locs, stash); } return 0; } // Parameters for transfer list command functions -typedef struct { +struct CommandParameters { char* cmdname; char* cpos; char* freestash; - char* stashbase; - int canwrite; + std::string stashbase; + bool canwrite; int createdstash; int fd; - int foundwrites; - int isunresumable; + bool foundwrites; + bool isunresumable; int version; - int written; + size_t written; NewThreadInfo nti; pthread_t thread; - size_t bufsize; - uint8_t* buffer; + std::vector<uint8_t> buffer; uint8_t* patch_start; -} CommandParameters; +}; // Do a source/target load for move/bsdiff/imgdiff in version 3. // @@ -1079,472 +890,363 @@ typedef struct { // If the return value is 0, source blocks have expected content and the command // can be performed. -static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks, - int onehash, int* overlap) { - char* srchash = NULL; - char* tgthash = NULL; - int stash_exists = 0; - int rc = -1; - uint8_t* tgtbuffer = NULL; +static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t& src_blocks, + bool onehash, bool& overlap) { - if (!params|| !tgt || !src_blocks || !overlap) { - goto v3out; - } - - srchash = strtok_r(NULL, " ", ¶ms->cpos); - - if (srchash == NULL) { + char* srchash = strtok_r(nullptr, " ", ¶ms.cpos); + if (srchash == nullptr) { fprintf(stderr, "missing source hash\n"); - goto v3out; + return -1; } + char* tgthash = nullptr; if (onehash) { tgthash = srchash; } else { - tgthash = strtok_r(NULL, " ", ¶ms->cpos); + tgthash = strtok_r(nullptr, " ", ¶ms.cpos); - if (tgthash == NULL) { + if (tgthash == nullptr) { fprintf(stderr, "missing target hash\n"); - goto v3out; + return -1; } } - if (LoadSrcTgtVersion2(¶ms->cpos, tgt, src_blocks, ¶ms->buffer, ¶ms->bufsize, - params->fd, params->stashbase, overlap) == -1) { - goto v3out; + if (LoadSrcTgtVersion2(¶ms.cpos, tgt, src_blocks, params.buffer, params.fd, + params.stashbase, &overlap) == -1) { + return -1; } - tgtbuffer = (uint8_t*) malloc((*tgt)->size * BLOCKSIZE); - - if (tgtbuffer == NULL) { - fprintf(stderr, "failed to allocate %d bytes\n", (*tgt)->size * BLOCKSIZE); - goto v3out; - } + std::vector<uint8_t> tgtbuffer(tgt.size * BLOCKSIZE); - if (ReadBlocks(*tgt, tgtbuffer, params->fd) == -1) { - goto v3out; + if (ReadBlocks(tgt, tgtbuffer, params.fd) == -1) { + return -1; } - if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, 0) == 0) { + if (VerifyBlocks(tgthash, tgtbuffer, tgt.size, false) == 0) { // Target blocks already have expected content, command should be skipped - rc = 1; - goto v3out; + return 1; } - if (VerifyBlocks(srchash, params->buffer, *src_blocks, 1) == 0) { + if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) { // If source and target blocks overlap, stash the source blocks so we can // resume from possible write errors - if (*overlap) { - fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks, - srchash); + if (overlap) { + fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks, srchash); - if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1, - &stash_exists) != 0) { + bool stash_exists = false; + if (WriteStash(params.stashbase, srchash, src_blocks, params.buffer, true, + &stash_exists) != 0) { fprintf(stderr, "failed to stash overlapping source blocks\n"); - goto v3out; + return -1; } // Can be deleted when the write has completed if (!stash_exists) { - params->freestash = srchash; + params.freestash = srchash; } } // Source blocks have expected content, command can proceed - rc = 0; - goto v3out; + return 0; } - if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, ¶ms->buffer, - ¶ms->bufsize, 1) == 0) { + if (overlap && LoadStash(params.stashbase, srchash, true, nullptr, params.buffer, true) == 0) { // Overlapping source blocks were previously stashed, command can proceed. // We are recovering from an interrupted command, so we don't know if the // stash can safely be deleted after this command. - rc = 0; - goto v3out; + return 0; } // Valid source data not available, update cannot be resumed fprintf(stderr, "partition has unexpected contents\n"); - params->isunresumable = 1; + params.isunresumable = true; -v3out: - if (tgtbuffer) { - free(tgtbuffer); - } - - return rc; + return -1; } -static int PerformCommandMove(CommandParameters* params) { - int blocks = 0; - int overlap = 0; - int rc = -1; +static int PerformCommandMove(CommandParameters& params) { + size_t blocks = 0; + bool overlap = false; int status = 0; - RangeSet* tgt = NULL; - - if (!params) { - goto pcmout; - } + RangeSet tgt; - if (params->version == 1) { - status = LoadSrcTgtVersion1(¶ms->cpos, &tgt, &blocks, ¶ms->buffer, - ¶ms->bufsize, params->fd); - } else if (params->version == 2) { - status = LoadSrcTgtVersion2(¶ms->cpos, &tgt, &blocks, ¶ms->buffer, - ¶ms->bufsize, params->fd, params->stashbase, NULL); - } else if (params->version >= 3) { - status = LoadSrcTgtVersion3(params, &tgt, &blocks, 1, &overlap); + if (params.version == 1) { + status = LoadSrcTgtVersion1(¶ms.cpos, tgt, blocks, params.buffer, params.fd); + } else if (params.version == 2) { + status = LoadSrcTgtVersion2(¶ms.cpos, tgt, blocks, params.buffer, params.fd, + params.stashbase, nullptr); + } else if (params.version >= 3) { + status = LoadSrcTgtVersion3(params, tgt, blocks, true, overlap); } if (status == -1) { fprintf(stderr, "failed to read blocks for move\n"); - goto pcmout; + return -1; } if (status == 0) { - params->foundwrites = 1; - } else if (params->foundwrites) { - fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname); + params.foundwrites = true; + } else if (params.foundwrites) { + fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname); } - if (params->canwrite) { + if (params.canwrite) { if (status == 0) { - fprintf(stderr, " moving %d blocks\n", blocks); + fprintf(stderr, " moving %zu blocks\n", blocks); - if (WriteBlocks(tgt, params->buffer, params->fd) == -1) { - goto pcmout; + if (WriteBlocks(tgt, params.buffer, params.fd) == -1) { + return -1; } } else { - fprintf(stderr, "skipping %d already moved blocks\n", blocks); + fprintf(stderr, "skipping %zu already moved blocks\n", blocks); } } - if (params->freestash) { - FreeStash(params->stashbase, params->freestash); - params->freestash = NULL; + if (params.freestash) { + FreeStash(params.stashbase, params.freestash); + params.freestash = nullptr; } - params->written += tgt->size; - rc = 0; - -pcmout: - if (tgt) { - free(tgt); - } + params.written += tgt.size; - return rc; + return 0; } -static int PerformCommandStash(CommandParameters* params) { - if (!params) { - return -1; - } - - return SaveStash(params->stashbase, ¶ms->cpos, ¶ms->buffer, ¶ms->bufsize, - params->fd, (params->version >= 3), ¶ms->isunresumable); +static int PerformCommandStash(CommandParameters& params) { + return SaveStash(params.stashbase, ¶ms.cpos, params.buffer, params.fd, + (params.version >= 3)); } -static int PerformCommandFree(CommandParameters* params) { - if (!params) { - return -1; - } - - if (params->createdstash || params->canwrite) { - return FreeStash(params->stashbase, params->cpos); +static int PerformCommandFree(CommandParameters& params) { + if (params.createdstash || params.canwrite) { + return FreeStash(params.stashbase, params.cpos); } return 0; } -static int PerformCommandZero(CommandParameters* params) { - char* range = NULL; - int i; - int j; - int rc = -1; - RangeSet* tgt = NULL; - - if (!params) { - goto pczout; - } - - range = strtok_r(NULL, " ", ¶ms->cpos); +static int PerformCommandZero(CommandParameters& params) { + char* range = strtok_r(nullptr, " ", ¶ms.cpos); - if (range == NULL) { + if (range == nullptr) { fprintf(stderr, "missing target blocks for zero\n"); - goto pczout; + return -1; } - tgt = parse_range(range); + RangeSet tgt; + parse_range(range, tgt); - fprintf(stderr, " zeroing %d blocks\n", tgt->size); + fprintf(stderr, " zeroing %zu blocks\n", tgt.size); - allocate(BLOCKSIZE, ¶ms->buffer, ¶ms->bufsize); - memset(params->buffer, 0, BLOCKSIZE); + allocate(BLOCKSIZE, params.buffer); + memset(params.buffer.data(), 0, BLOCKSIZE); - if (params->canwrite) { - for (i = 0; i < tgt->count; ++i) { - if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) { - goto pczout; + if (params.canwrite) { + for (size_t i = 0; i < tgt.count; ++i) { + if (!check_lseek(params.fd, (off64_t) tgt.pos[i * 2] * BLOCKSIZE, SEEK_SET)) { + return -1; } - for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) { - if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) { - goto pczout; + for (size_t j = tgt.pos[i * 2]; j < tgt.pos[i * 2 + 1]; ++j) { + if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) { + return -1; } } } } - if (params->cmdname[0] == 'z') { + if (params.cmdname[0] == 'z') { // Update only for the zero command, as the erase command will call // this if DEBUG_ERASE is defined. - params->written += tgt->size; - } - - rc = 0; - -pczout: - if (tgt) { - free(tgt); + params.written += tgt.size; } - return rc; + return 0; } -static int PerformCommandNew(CommandParameters* params) { - char* range = NULL; - int rc = -1; - RangeSet* tgt = NULL; - RangeSinkState rss; - - if (!params) { - goto pcnout; - } +static int PerformCommandNew(CommandParameters& params) { + char* range = strtok_r(nullptr, " ", ¶ms.cpos); - range = strtok_r(NULL, " ", ¶ms->cpos); - - if (range == NULL) { - goto pcnout; + if (range == nullptr) { + return -1; } - tgt = parse_range(range); + RangeSet tgt; + parse_range(range, tgt); - if (params->canwrite) { - fprintf(stderr, " writing %d blocks of new data\n", tgt->size); + if (params.canwrite) { + fprintf(stderr, " writing %zu blocks of new data\n", tgt.size); - rss.fd = params->fd; - rss.tgt = tgt; + RangeSinkState rss(tgt); + rss.fd = params.fd; rss.p_block = 0; - rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; + rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE; - if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) { - goto pcnout; + if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) { + return -1; } - pthread_mutex_lock(¶ms->nti.mu); - params->nti.rss = &rss; - pthread_cond_broadcast(¶ms->nti.cv); + pthread_mutex_lock(¶ms.nti.mu); + params.nti.rss = &rss; + pthread_cond_broadcast(¶ms.nti.cv); - while (params->nti.rss) { - pthread_cond_wait(¶ms->nti.cv, ¶ms->nti.mu); + while (params.nti.rss) { + pthread_cond_wait(¶ms.nti.cv, ¶ms.nti.mu); } - pthread_mutex_unlock(¶ms->nti.mu); + pthread_mutex_unlock(¶ms.nti.mu); } - params->written += tgt->size; - rc = 0; + params.written += tgt.size; -pcnout: - if (tgt) { - free(tgt); - } - - return rc; + return 0; } -static int PerformCommandDiff(CommandParameters* params) { - char* logparams = NULL; - char* value = NULL; - int blocks = 0; - int overlap = 0; - int rc = -1; - int status = 0; - RangeSet* tgt = NULL; - RangeSinkState rss; - size_t len = 0; - size_t offset = 0; - Value patch_value; +static int PerformCommandDiff(CommandParameters& params) { - if (!params) { - goto pcdout; - } - - logparams = strdup(params->cpos); - value = strtok_r(NULL, " ", ¶ms->cpos); + const std::string logparams(params.cpos); + char* value = strtok_r(nullptr, " ", ¶ms.cpos); - if (value == NULL) { - fprintf(stderr, "missing patch offset for %s\n", params->cmdname); - goto pcdout; + if (value == nullptr) { + fprintf(stderr, "missing patch offset for %s\n", params.cmdname); + return -1; } - offset = strtoul(value, NULL, 0); + size_t offset = strtoul(value, nullptr, 0); - value = strtok_r(NULL, " ", ¶ms->cpos); + value = strtok_r(nullptr, " ", ¶ms.cpos); - if (value == NULL) { - fprintf(stderr, "missing patch length for %s\n", params->cmdname); - goto pcdout; + if (value == nullptr) { + fprintf(stderr, "missing patch length for %s\n", params.cmdname); + return -1; } - len = strtoul(value, NULL, 0); + size_t len = strtoul(value, nullptr, 0); - if (params->version == 1) { - status = LoadSrcTgtVersion1(¶ms->cpos, &tgt, &blocks, ¶ms->buffer, - ¶ms->bufsize, params->fd); - } else if (params->version == 2) { - status = LoadSrcTgtVersion2(¶ms->cpos, &tgt, &blocks, ¶ms->buffer, - ¶ms->bufsize, params->fd, params->stashbase, NULL); - } else if (params->version >= 3) { - status = LoadSrcTgtVersion3(params, &tgt, &blocks, 0, &overlap); + RangeSet tgt; + size_t blocks = 0; + bool overlap = false; + int status = 0; + if (params.version == 1) { + status = LoadSrcTgtVersion1(¶ms.cpos, tgt, blocks, params.buffer, params.fd); + } else if (params.version == 2) { + status = LoadSrcTgtVersion2(¶ms.cpos, tgt, blocks, params.buffer, params.fd, + params.stashbase, nullptr); + } else if (params.version >= 3) { + status = LoadSrcTgtVersion3(params, tgt, blocks, false, overlap); } if (status == -1) { fprintf(stderr, "failed to read blocks for diff\n"); - goto pcdout; + return -1; } if (status == 0) { - params->foundwrites = 1; - } else if (params->foundwrites) { - fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname); + params.foundwrites = true; + } else if (params.foundwrites) { + fprintf(stderr, "warning: commands executed out of order [%s]\n", params.cmdname); } - if (params->canwrite) { + if (params.canwrite) { if (status == 0) { - fprintf(stderr, "patching %d blocks to %d\n", blocks, tgt->size); + fprintf(stderr, "patching %zu blocks to %zu\n", blocks, tgt.size); + Value patch_value; patch_value.type = VAL_BLOB; patch_value.size = len; - patch_value.data = (char*) (params->patch_start + offset); + patch_value.data = (char*) (params.patch_start + offset); - rss.fd = params->fd; - rss.tgt = tgt; + RangeSinkState rss(tgt); + rss.fd = params.fd; rss.p_block = 0; - rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; + rss.p_remain = (tgt.pos[1] - tgt.pos[0]) * BLOCKSIZE; - if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) { - goto pcdout; + if (!check_lseek(params.fd, (off64_t) tgt.pos[0] * BLOCKSIZE, SEEK_SET)) { + return -1; } - if (params->cmdname[0] == 'i') { // imgdiff - ApplyImagePatch(params->buffer, blocks * BLOCKSIZE, &patch_value, - &RangeSinkWrite, &rss, NULL, NULL); + if (params.cmdname[0] == 'i') { // imgdiff + ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, + &RangeSinkWrite, &rss, nullptr, nullptr); } else { - ApplyBSDiffPatch(params->buffer, blocks * BLOCKSIZE, &patch_value, - 0, &RangeSinkWrite, &rss, NULL); + ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, &patch_value, 0, + &RangeSinkWrite, &rss, nullptr); } // We expect the output of the patcher to fill the tgt ranges exactly. - if (rss.p_block != tgt->count || rss.p_remain != 0) { + if (rss.p_block != tgt.count || rss.p_remain != 0) { fprintf(stderr, "range sink underrun?\n"); } } else { - fprintf(stderr, "skipping %d blocks already patched to %d [%s]\n", - blocks, tgt->size, logparams); + fprintf(stderr, "skipping %zu blocks already patched to %zu [%s]\n", + blocks, tgt.size, logparams.c_str()); } } - if (params->freestash) { - FreeStash(params->stashbase, params->freestash); - params->freestash = NULL; + if (params.freestash) { + FreeStash(params.stashbase, params.freestash); + params.freestash = nullptr; } - params->written += tgt->size; - rc = 0; - -pcdout: - if (logparams) { - free(logparams); - } + params.written += tgt.size; - if (tgt) { - free(tgt); - } - - return rc; + return 0; } -static int PerformCommandErase(CommandParameters* params) { - char* range = NULL; - int i; - int rc = -1; - RangeSet* tgt = NULL; - struct stat st; - uint64_t blocks[2]; - +static int PerformCommandErase(CommandParameters& params) { if (DEBUG_ERASE) { return PerformCommandZero(params); } - if (!params) { - goto pceout; - } - - if (fstat(params->fd, &st) == -1) { + struct stat sb; + if (fstat(params.fd, &sb) == -1) { fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno)); - goto pceout; + return -1; } - if (!S_ISBLK(st.st_mode)) { + if (!S_ISBLK(sb.st_mode)) { fprintf(stderr, "not a block device; skipping erase\n"); - goto pceout; + return -1; } - range = strtok_r(NULL, " ", ¶ms->cpos); + char* range = strtok_r(nullptr, " ", ¶ms.cpos); - if (range == NULL) { + if (range == nullptr) { fprintf(stderr, "missing target blocks for erase\n"); - goto pceout; + return -1; } - tgt = parse_range(range); + RangeSet tgt; + parse_range(range, tgt); - if (params->canwrite) { - fprintf(stderr, " erasing %d blocks\n", tgt->size); + if (params.canwrite) { + fprintf(stderr, " erasing %zu blocks\n", tgt.size); - for (i = 0; i < tgt->count; ++i) { + for (size_t i = 0; i < tgt.count; ++i) { + uint64_t blocks[2]; // offset in bytes - blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE; + blocks[0] = tgt.pos[i * 2] * (uint64_t) BLOCKSIZE; // length in bytes - blocks[1] = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * (uint64_t) BLOCKSIZE; + blocks[1] = (tgt.pos[i * 2 + 1] - tgt.pos[i * 2]) * (uint64_t) BLOCKSIZE; - if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) { + if (ioctl(params.fd, BLKDISCARD, &blocks) == -1) { fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno)); - goto pceout; + return -1; } } } - rc = 0; - -pceout: - if (tgt) { - free(tgt); - } - - return rc; + return 0; } // Definitions for transfer list command functions -typedef int (*CommandFunction)(CommandParameters*); +typedef int (*CommandFunction)(CommandParameters&); -typedef struct { +struct Command { const char* name; CommandFunction f; -} Command; +}; // CompareCommands and CompareCommandNames are for the hash table @@ -1574,224 +1276,200 @@ static unsigned int HashString(const char *s) { // - new data stream (filename within package.zip) // - patch stream (filename within package.zip, must be uncompressed) -static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[], - const Command* commands, int cmdcount, int dryrun) { +static Value* PerformBlockImageUpdate(const char* name, State* state, int /* argc */, Expr* argv[], + const Command* commands, size_t cmdcount, bool dryrun) { - char* line = NULL; - char* linesave = NULL; - char* logcmd = NULL; - char* transfer_list = NULL; CommandParameters params; - const Command* cmd = NULL; - const ZipEntry* new_entry = NULL; - const ZipEntry* patch_entry = NULL; - FILE* cmd_pipe = NULL; - HashTable* cmdht = NULL; - int i; - int res; - int rc = -1; - int stash_max_blocks = 0; - int total_blocks = 0; - pthread_attr_t attr; - unsigned int cmdhash; - UpdaterInfo* ui = NULL; - Value* blockdev_filename = NULL; - Value* new_data_fn = NULL; - Value* patch_data_fn = NULL; - Value* transfer_list_value = NULL; - ZipArchive* za = NULL; - memset(¶ms, 0, sizeof(params)); params.canwrite = !dryrun; fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update"); + Value* blockdev_filename = nullptr; + Value* transfer_list_value = nullptr; + Value* new_data_fn = nullptr; + Value* patch_data_fn = nullptr; if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value, &new_data_fn, &patch_data_fn) < 0) { - goto pbiudone; + return StringValue(strdup("")); } + std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename, + FreeValue); + std::unique_ptr<Value, decltype(&FreeValue)> transfer_list_value_holder(transfer_list_value, + FreeValue); + std::unique_ptr<Value, decltype(&FreeValue)> new_data_fn_holder(new_data_fn, FreeValue); + std::unique_ptr<Value, decltype(&FreeValue)> patch_data_fn_holder(patch_data_fn, FreeValue); if (blockdev_filename->type != VAL_STRING) { ErrorAbort(state, "blockdev_filename argument to %s must be string", name); - goto pbiudone; + return StringValue(strdup("")); } if (transfer_list_value->type != VAL_BLOB) { ErrorAbort(state, "transfer_list argument to %s must be blob", name); - goto pbiudone; + return StringValue(strdup("")); } if (new_data_fn->type != VAL_STRING) { ErrorAbort(state, "new_data_fn argument to %s must be string", name); - goto pbiudone; + return StringValue(strdup("")); } if (patch_data_fn->type != VAL_STRING) { ErrorAbort(state, "patch_data_fn argument to %s must be string", name); - goto pbiudone; + return StringValue(strdup("")); } - ui = (UpdaterInfo*) state->cookie; + UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie); - if (ui == NULL) { - goto pbiudone; + if (ui == nullptr) { + return StringValue(strdup("")); } - cmd_pipe = ui->cmd_pipe; - za = ui->package_zip; + FILE* cmd_pipe = ui->cmd_pipe; + ZipArchive* za = ui->package_zip; - if (cmd_pipe == NULL || za == NULL) { - goto pbiudone; + if (cmd_pipe == nullptr || za == nullptr) { + return StringValue(strdup("")); } - patch_entry = mzFindZipEntry(za, patch_data_fn->data); - - if (patch_entry == NULL) { + const ZipEntry* patch_entry = mzFindZipEntry(za, patch_data_fn->data); + if (patch_entry == nullptr) { fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data); - goto pbiudone; + return StringValue(strdup("")); } params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry); - new_entry = mzFindZipEntry(za, new_data_fn->data); - - if (new_entry == NULL) { + const ZipEntry* new_entry = mzFindZipEntry(za, new_data_fn->data); + if (new_entry == nullptr) { fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data); - goto pbiudone; + return StringValue(strdup("")); } params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR)); + unique_fd fd_holder(params.fd); if (params.fd == -1) { fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno)); - goto pbiudone; + return StringValue(strdup("")); } if (params.canwrite) { params.nti.za = za; params.nti.entry = new_entry; - pthread_mutex_init(¶ms.nti.mu, NULL); - pthread_cond_init(¶ms.nti.cv, NULL); + pthread_mutex_init(¶ms.nti.mu, nullptr); + pthread_cond_init(¶ms.nti.cv, nullptr); + pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); int error = pthread_create(¶ms.thread, &attr, unzip_new_data, ¶ms.nti); if (error != 0) { fprintf(stderr, "pthread_create failed: %s\n", strerror(error)); - goto pbiudone; + return StringValue(strdup("")); } } // The data in transfer_list_value is not necessarily null-terminated, so we need // to copy it to a new buffer and add the null that strtok_r will need. - transfer_list = reinterpret_cast<char*>(malloc(transfer_list_value->size + 1)); - - if (transfer_list == NULL) { - fprintf(stderr, "failed to allocate %zd bytes for transfer list\n", - transfer_list_value->size + 1); - goto pbiudone; - } - - memcpy(transfer_list, transfer_list_value->data, transfer_list_value->size); - transfer_list[transfer_list_value->size] = '\0'; + const std::string transfer_list(transfer_list_value->data, transfer_list_value->size); + std::vector<std::string> lines = android::base::Split(transfer_list, "\n"); // First line in transfer list is the version number - line = strtok_r(transfer_list, "\n", &linesave); - params.version = strtol(line, NULL, 0); - - if (params.version < 1 || params.version > 3) { - fprintf(stderr, "unexpected transfer list version [%s]\n", line); - goto pbiudone; + if (!android::base::ParseInt(lines[0].c_str(), ¶ms.version, 1, 4)) { + fprintf(stderr, "unexpected transfer list version [%s]\n", lines[0].c_str()); + return StringValue(strdup("")); } fprintf(stderr, "blockimg version is %d\n", params.version); // Second line in transfer list is the total number of blocks we expect to write - line = strtok_r(NULL, "\n", &linesave); - total_blocks = strtol(line, NULL, 0); + int total_blocks; + if (!android::base::ParseInt(lines[1].c_str(), &total_blocks, 0)) { + ErrorAbort(state, "unexpected block count [%s]\n", lines[1].c_str()); + return StringValue(strdup("")); + } - if (total_blocks < 0) { - ErrorAbort(state, "unexpected block count [%s]\n", line); - goto pbiudone; - } else if (total_blocks == 0) { - rc = 0; - goto pbiudone; + if (total_blocks == 0) { + return StringValue(strdup("t")); } + size_t start = 2; if (params.version >= 2) { // Third line is how many stash entries are needed simultaneously - line = strtok_r(NULL, "\n", &linesave); - fprintf(stderr, "maximum stash entries %s\n", line); + fprintf(stderr, "maximum stash entries %s\n", lines[2].c_str()); // Fourth line is the maximum number of blocks that will be stashed simultaneously - line = strtok_r(NULL, "\n", &linesave); - stash_max_blocks = strtol(line, NULL, 0); - - if (stash_max_blocks < 0) { - ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", line); - goto pbiudone; + int stash_max_blocks; + if (!android::base::ParseInt(lines[3].c_str(), &stash_max_blocks, 0)) { + ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", lines[3].c_str()); + return StringValue(strdup("")); } - if (stash_max_blocks >= 0) { - res = CreateStash(state, stash_max_blocks, blockdev_filename->data, - ¶ms.stashbase); - - if (res == -1) { - goto pbiudone; - } + int res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase); - params.createdstash = res; + if (res == -1) { + return StringValue(strdup("")); } + + params.createdstash = res; + + start += 2; } // Build a hash table of the available commands - cmdht = mzHashTableCreate(cmdcount, NULL); + HashTable* cmdht = mzHashTableCreate(cmdcount, nullptr); + std::unique_ptr<HashTable, decltype(&mzHashTableFree)> cmdht_holder(cmdht, mzHashTableFree); - for (i = 0; i < cmdcount; ++i) { - cmdhash = HashString(commands[i].name); + for (size_t i = 0; i < cmdcount; ++i) { + unsigned int cmdhash = HashString(commands[i].name); mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true); } + int rc = -1; + // Subsequent lines are all individual transfer commands - for (line = strtok_r(NULL, "\n", &linesave); line; - line = strtok_r(NULL, "\n", &linesave)) { + for (auto it = lines.cbegin() + start; it != lines.cend(); it++) { + const std::string& line_str(*it); + if (line_str.empty()) { + continue; + } - logcmd = strdup(line); + char* line = strdup(line_str.c_str()); params.cmdname = strtok_r(line, " ", ¶ms.cpos); - if (params.cmdname == NULL) { + if (params.cmdname == nullptr) { fprintf(stderr, "missing command [%s]\n", line); goto pbiudone; } - cmdhash = HashString(params.cmdname); - cmd = (const Command*) mzHashTableLookup(cmdht, cmdhash, params.cmdname, - CompareCommandNames, false); + unsigned int cmdhash = HashString(params.cmdname); + const Command* cmd = reinterpret_cast<const Command*>(mzHashTableLookup(cmdht, cmdhash, + params.cmdname, CompareCommandNames, false)); - if (cmd == NULL) { + if (cmd == nullptr) { fprintf(stderr, "unexpected command [%s]\n", params.cmdname); goto pbiudone; } - if (cmd->f != NULL && cmd->f(¶ms) == -1) { - fprintf(stderr, "failed to execute command [%s]\n", - logcmd ? logcmd : params.cmdname); + if (cmd->f != nullptr && cmd->f(params) == -1) { + fprintf(stderr, "failed to execute command [%s]\n", line_str.c_str()); goto pbiudone; } - if (logcmd) { - free(logcmd); - logcmd = NULL; - } - if (params.canwrite) { + if (fsync(params.fd) == -1) { + fprintf(stderr, "fsync failed: %s\n", strerror(errno)); + goto pbiudone; + } fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks); fflush(cmd_pipe); } } if (params.canwrite) { - pthread_join(params.thread, NULL); + pthread_join(params.thread, nullptr); - fprintf(stderr, "wrote %d blocks; expected %d\n", params.written, total_blocks); - fprintf(stderr, "max alloc needed was %zu\n", params.bufsize); + fprintf(stderr, "wrote %zu blocks; expected %d\n", params.written, total_blocks); + fprintf(stderr, "max alloc needed was %zu\n", params.buffer.size()); // Delete stash only after successfully completing the update, as it // may contain blocks needed to complete the update later. @@ -1810,48 +1488,12 @@ pbiudone: close(params.fd); } - if (logcmd) { - free(logcmd); - } - - if (cmdht) { - mzHashTableFree(cmdht); - } - - if (params.buffer) { - free(params.buffer); - } - - if (transfer_list) { - free(transfer_list); - } - - if (blockdev_filename) { - FreeValue(blockdev_filename); - } - - if (transfer_list_value) { - FreeValue(transfer_list_value); - } - - if (new_data_fn) { - FreeValue(new_data_fn); - } - - if (patch_data_fn) { - FreeValue(patch_data_fn); - } - // Only delete the stash if the update cannot be resumed, or it's // a verification run and we created the stash. if (params.isunresumable || (!params.canwrite && params.createdstash)) { DeleteStash(params.stashbase); } - if (params.stashbase) { - free(params.stashbase); - } - return StringValue(rc == 0 ? strdup("t") : strdup("")); } @@ -1909,21 +1551,21 @@ pbiudone: // the source data. Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) { - // Commands which are not tested are set to NULL to skip them completely + // Commands which are not tested are set to nullptr to skip them completely const Command commands[] = { { "bsdiff", PerformCommandDiff }, - { "erase", NULL }, + { "erase", nullptr }, { "free", PerformCommandFree }, { "imgdiff", PerformCommandDiff }, { "move", PerformCommandMove }, - { "new", NULL }, + { "new", nullptr }, { "stash", PerformCommandStash }, - { "zero", NULL } + { "zero", nullptr } }; // Perform a dry run without writing to test if an update can proceed return PerformBlockImageUpdate(name, state, argc, argv, commands, - sizeof(commands) / sizeof(commands[0]), 1); + sizeof(commands) / sizeof(commands[0]), true); } Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) { @@ -1939,73 +1581,141 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] }; return PerformBlockImageUpdate(name, state, argc, argv, commands, - sizeof(commands) / sizeof(commands[0]), 0); + sizeof(commands) / sizeof(commands[0]), false); } -Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { +Value* RangeSha1Fn(const char* name, State* state, int /* argc */, Expr* argv[]) { Value* blockdev_filename; Value* ranges; - const uint8_t* digest = NULL; + if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) { - return NULL; + return StringValue(strdup("")); } + std::unique_ptr<Value, decltype(&FreeValue)> ranges_holder(ranges, FreeValue); + std::unique_ptr<Value, decltype(&FreeValue)> blockdev_filename_holder(blockdev_filename, + FreeValue); if (blockdev_filename->type != VAL_STRING) { ErrorAbort(state, "blockdev_filename argument to %s must be string", name); - goto done; + return StringValue(strdup("")); } if (ranges->type != VAL_STRING) { ErrorAbort(state, "ranges argument to %s must be string", name); - goto done; + return StringValue(strdup("")); } - int fd; - fd = open(blockdev_filename->data, O_RDWR); + int fd = open(blockdev_filename->data, O_RDWR); + unique_fd fd_holder(fd); if (fd < 0) { ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno)); - goto done; + return StringValue(strdup("")); } - RangeSet* rs; - rs = parse_range(ranges->data); - uint8_t buffer[BLOCKSIZE]; + RangeSet rs; + parse_range(ranges->data, rs); SHA_CTX ctx; SHA_init(&ctx); - int i, j; - for (i = 0; i < rs->count; ++i) { - if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) { - ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data, - strerror(errno)); - goto done; + std::vector<uint8_t> buffer(BLOCKSIZE); + for (size_t i = 0; i < rs.count; ++i) { + if (!check_lseek(fd, (off64_t)rs.pos[i*2] * BLOCKSIZE, SEEK_SET)) { + ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data, strerror(errno)); + return StringValue(strdup("")); } - for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) { + for (size_t j = rs.pos[i*2]; j < rs.pos[i*2+1]; ++j) { if (read_all(fd, buffer, BLOCKSIZE) == -1) { ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data, - strerror(errno)); - goto done; + strerror(errno)); + return StringValue(strdup("")); } - SHA_update(&ctx, buffer, BLOCKSIZE); + SHA_update(&ctx, buffer.data(), BLOCKSIZE); } } - digest = SHA_final(&ctx); - close(fd); + const uint8_t* digest = SHA_final(&ctx); + + return StringValue(strdup(print_sha1(digest).c_str())); +} + +Value* BlockImageRecoverFn(const char* name, State* state, int argc, Expr* argv[]) { + Value* arg_filename; + Value* arg_ranges; + + if (ReadValueArgs(state, argv, 2, &arg_filename, &arg_ranges) < 0) { + return NULL; + } + + std::unique_ptr<Value, decltype(&FreeValue)> filename(arg_filename, FreeValue); + std::unique_ptr<Value, decltype(&FreeValue)> ranges(arg_ranges, FreeValue); - done: - FreeValue(blockdev_filename); - FreeValue(ranges); - if (digest == NULL) { + if (filename->type != VAL_STRING) { + ErrorAbort(state, "filename argument to %s must be string", name); + return StringValue(strdup("")); + } + if (ranges->type != VAL_STRING) { + ErrorAbort(state, "ranges argument to %s must be string", name); return StringValue(strdup("")); - } else { - return StringValue(PrintSha1(digest)); } + + // When opened with O_RDWR, libfec rewrites corrupted blocks when they are read + fec::io fh(filename->data, O_RDWR); + + if (!fh) { + ErrorAbort(state, "fec_open \"%s\" failed: %s", filename->data, strerror(errno)); + return StringValue(strdup("")); + } + + if (!fh.has_ecc() || !fh.has_verity()) { + ErrorAbort(state, "unable to use metadata to correct errors"); + return StringValue(strdup("")); + } + + fec_status status; + + if (!fh.get_status(status)) { + ErrorAbort(state, "failed to read FEC status"); + return StringValue(strdup("")); + } + + RangeSet rs; + parse_range(ranges->data, rs); + + uint8_t buffer[BLOCKSIZE]; + + for (size_t i = 0; i < rs.count; ++i) { + for (size_t j = rs.pos[i * 2]; j < rs.pos[i * 2 + 1]; ++j) { + // Stay within the data area, libfec validates and corrects metadata + if (status.data_size <= (uint64_t)j * BLOCKSIZE) { + continue; + } + + if (fh.pread(buffer, BLOCKSIZE, (off64_t)j * BLOCKSIZE) != BLOCKSIZE) { + ErrorAbort(state, "failed to recover %s (block %d): %s", filename->data, + j, strerror(errno)); + return StringValue(strdup("")); + } + + // If we want to be able to recover from a situation where rewriting a corrected + // block doesn't guarantee the same data will be returned when re-read later, we + // can save a copy of corrected blocks to /cache. Note: + // + // 1. Maximum space required from /cache is the same as the maximum number of + // corrupted blocks we can correct. For RS(255, 253) and a 2 GiB partition, + // this would be ~16 MiB, for example. + // + // 2. To find out if this block was corrupted, call fec_get_status after each + // read and check if the errors field value has increased. + } + } + + return StringValue(strdup("t")); } void RegisterBlockImageFunctions() { RegisterFunction("block_image_verify", BlockImageVerifyFn); RegisterFunction("block_image_update", BlockImageUpdateFn); + RegisterFunction("block_image_recover", BlockImageRecoverFn); RegisterFunction("range_sha1", RangeSha1Fn); } diff --git a/updater/install.cpp b/updater/install.cpp index a2bc4029f..e2b3db7ce 100644 --- a/updater/install.cpp +++ b/updater/install.cpp @@ -34,6 +34,10 @@ #include <linux/xattr.h> #include <inttypes.h> +#include <android-base/parseint.h> +#include <android-base/strings.h> +#include <android-base/stringprintf.h> + #include "bootloader.h" #include "applypatch/applypatch.h" #include "cutils/android_reboot.h" @@ -53,23 +57,35 @@ #include "wipe.h" #endif -void uiPrint(State* state, char* buffer) { - char* line = strtok(buffer, "\n"); - UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); - while (line) { - fprintf(ui->cmd_pipe, "ui_print %s\n", line); - line = strtok(NULL, "\n"); +// Send over the buffer to recovery though the command pipe. +static void uiPrint(State* state, const std::string& buffer) { + UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie); + + // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "". + // So skip sending empty strings to UI. + std::vector<std::string> lines = android::base::Split(buffer, "\n"); + for (auto& line: lines) { + if (!line.empty()) { + fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str()); + fprintf(ui->cmd_pipe, "ui_print\n"); + } } - fprintf(ui->cmd_pipe, "ui_print\n"); + + // On the updater side, we need to dump the contents to stderr (which has + // been redirected to the log file). Because the recovery will only print + // the contents to screen when processing pipe command ui_print. + fprintf(stderr, "%s", buffer.c_str()); } __attribute__((__format__(printf, 2, 3))) __nonnull((2)) void uiPrintf(State* state, const char* format, ...) { - char error_msg[1024]; + std::string error_msg; + va_list ap; va_start(ap, format); - vsnprintf(error_msg, sizeof(error_msg), format, ap); + android::base::StringAppendV(&error_msg, format, ap); va_end(ap); + uiPrint(state, error_msg); } @@ -154,7 +170,7 @@ Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { const MtdPartition* mtd; mtd = mtd_find_partition_by_name(location); if (mtd == NULL) { - uiPrintf(state, "%s: no mtd partition named \"%s\"", + uiPrintf(state, "%s: no mtd partition named \"%s\"\n", name, location); result = strdup(""); goto done; @@ -459,7 +475,8 @@ Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) { } double frac = strtod(frac_str, NULL); - int sec = strtol(sec_str, NULL, 10); + int sec; + android::base::ParseInt(sec_str, &sec); UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); @@ -975,7 +992,7 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { goto done; } - if (fread(buffer, 1, st.st_size, f) != st.st_size) { + if (fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) { ErrorAbort(state, "%s: failed to read %lld bytes from %s", name, (long long)st.st_size+1, filename); fclose(f); @@ -1130,12 +1147,11 @@ Value* ApplyPatchSpaceFn(const char* name, State* state, return NULL; } - char* endptr; - size_t bytes = strtol(bytes_str, &endptr, 10); - if (bytes == 0 && endptr == bytes_str) { + size_t bytes; + if (!android::base::ParseUint(bytes_str, &bytes)) { ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str); free(bytes_str); - return NULL; + return nullptr; } return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); @@ -1159,16 +1175,14 @@ Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { return NULL; } - char* endptr; - size_t target_size = strtol(target_size_str, &endptr, 10); - if (target_size == 0 && endptr == target_size_str) { - ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", - name, target_size_str); + size_t target_size; + if (!android::base::ParseUint(target_size_str, &target_size)) { + ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", name, target_size_str); free(source_filename); free(target_filename); free(target_sha1); free(target_size_str); - return NULL; + return nullptr; } int patchcount = (argc-4) / 2; @@ -1241,28 +1255,24 @@ Value* ApplyPatchCheckFn(const char* name, State* state, return StringValue(strdup(result == 0 ? "t" : "")); } +// This is the updater side handler for ui_print() in edify script. Contents +// will be sent over to the recovery side for on-screen display. Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { char** args = ReadVarArgs(state, argc, argv); if (args == NULL) { return NULL; } - int size = 0; - int i; - for (i = 0; i < argc; ++i) { - size += strlen(args[i]); - } - char* buffer = reinterpret_cast<char*>(malloc(size+1)); - size = 0; - for (i = 0; i < argc; ++i) { - strcpy(buffer+size, args[i]); - size += strlen(args[i]); + std::string buffer; + for (int i = 0; i < argc; ++i) { + buffer += args[i]; free(args[i]); } free(args); - buffer[size] = '\0'; + + buffer += "\n"; uiPrint(state, buffer); - return StringValue(buffer); + return StringValue(strdup(buffer.c_str())); } Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { @@ -1512,7 +1522,8 @@ Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) char* len_str; if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL; - size_t len = strtoull(len_str, NULL, 0); + size_t len; + android::base::ParseUint(len_str, &len); int fd = open(filename, O_WRONLY, 0644); int success = wipe_block_device(fd, len); diff --git a/verifier_test.cpp b/verifier_test.cpp index 82546edce..21633dc20 100644 --- a/verifier_test.cpp +++ b/verifier_test.cpp @@ -141,6 +141,12 @@ class FakeUI : public RecoveryUI { vfprintf(stderr, fmt, ap); va_end(ap); } + void PrintOnScreenOnly(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + } void ShowFile(const char*) { } void StartMenu(const char* const * headers, const char* const * items, diff --git a/wear_ui.cpp b/wear_ui.cpp index 55b7afc8f..3ee38e8a4 100644 --- a/wear_ui.cpp +++ b/wear_ui.cpp @@ -35,7 +35,7 @@ #include "wear_ui.h" #include "ui.h" #include "cutils/properties.h" -#include "base/strings.h" +#include "android-base/strings.h" static int char_width; static int char_height; |