From 7101b2e2854985727b7ef65e5b5057e0ecf2d034 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 3 Jun 2015 10:49:29 -0700 Subject: [PATCH 01/13] recovery: Switch to clang And a few trival fixes to suppress warnings. Change-Id: Id28e3581aaca4bda59826afa80c0c1cdfb0442fc (cherry picked from commit 80e46e08de5f65702fa7f7cd3ef83f905d919bbc) --- Android.mk | 6 ++++-- adb_install.cpp | 2 +- applypatch/Android.mk | 5 +++++ edify/Android.mk | 2 ++ minadbd/Android.mk | 1 + minui/Android.mk | 1 + minzip/Android.mk | 2 ++ minzip/Zip.c | 2 -- mtdutils/Android.mk | 2 ++ recovery.cpp | 21 +++++++++++++++------ tests/Android.mk | 1 + uncrypt/Android.mk | 2 ++ updater/Android.mk | 2 ++ 13 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Android.mk b/Android.mk index b31f7301..ac1ef83d 100644 --- a/Android.mk +++ b/Android.mk @@ -14,11 +14,10 @@ LOCAL_PATH := $(call my-dir) - include $(CLEAR_VARS) LOCAL_SRC_FILES := fuse_sideload.c - +LOCAL_CLANG := true LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE @@ -55,6 +54,7 @@ RECOVERY_API_VERSION := 3 RECOVERY_FSTAB_VERSION := 2 LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) LOCAL_CFLAGS += -Wno-unused-parameter +LOCAL_CLANG := true LOCAL_C_INCLUDES += \ system/vold \ @@ -99,6 +99,7 @@ include $(BUILD_EXECUTABLE) # All the APIs for testing include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_MODULE := libverifier LOCAL_MODULE_TAGS := tests LOCAL_SRC_FILES := \ @@ -106,6 +107,7 @@ LOCAL_SRC_FILES := \ include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_MODULE := verifier_test LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_MODULE_TAGS := tests diff --git a/adb_install.cpp b/adb_install.cpp index e3b94ea5..4cfcb2ab 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -91,7 +91,7 @@ apply_from_adb(RecoveryUI* ui_, bool* wipe_cache, const char* install_file) { // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the host // connects and starts serving a package. Poll for its // appearance. (Note that inotify doesn't work with FUSE.) - int result; + int result = INSTALL_ERROR; int status; bool waited = false; struct stat st; diff --git a/applypatch/Android.mk b/applypatch/Android.mk index 4984093d..4936db2a 100644 --- a/applypatch/Android.mk +++ b/applypatch/Android.mk @@ -13,8 +13,10 @@ # limitations under the License. LOCAL_PATH := $(call my-dir) + include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_SRC_FILES := applypatch.c bspatch.c freecache.c imgpatch.c utils.c LOCAL_MODULE := libapplypatch LOCAL_MODULE_TAGS := eng @@ -25,6 +27,7 @@ include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_SRC_FILES := main.c LOCAL_MODULE := applypatch LOCAL_C_INCLUDES += bootable/recovery @@ -35,6 +38,7 @@ include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_SRC_FILES := main.c LOCAL_MODULE := applypatch_static LOCAL_FORCE_STATIC_EXECUTABLE := true @@ -47,6 +51,7 @@ include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_SRC_FILES := imgdiff.c utils.c bsdiff.c LOCAL_MODULE := imgdiff LOCAL_FORCE_STATIC_EXECUTABLE := true diff --git a/edify/Android.mk b/edify/Android.mk index 03c04e43..c3664504 100644 --- a/edify/Android.mk +++ b/edify/Android.mk @@ -25,6 +25,7 @@ LOCAL_CFLAGS := $(edify_cflags) -g -O0 LOCAL_MODULE := edify LOCAL_YACCFLAGS := -v LOCAL_CFLAGS += -Wno-unused-parameter +LOCAL_CLANG := true include $(BUILD_HOST_EXECUTABLE) @@ -38,5 +39,6 @@ LOCAL_SRC_FILES := $(edify_src_files) LOCAL_CFLAGS := $(edify_cflags) LOCAL_CFLAGS += -Wno-unused-parameter LOCAL_MODULE := libedify +LOCAL_CLANG := true include $(BUILD_STATIC_LIBRARY) diff --git a/minadbd/Android.mk b/minadbd/Android.mk index a7a3e087..3db3b411 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -15,6 +15,7 @@ LOCAL_SRC_FILES := \ fuse_adb_provider.cpp \ services.cpp \ +LOCAL_CLANG := true LOCAL_MODULE := libminadbd LOCAL_CFLAGS := $(minadbd_cflags) LOCAL_CONLY_FLAGS := -Wimplicit-function-declaration diff --git a/minui/Android.mk b/minui/Android.mk index 97724fbf..3057f452 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -41,6 +41,7 @@ include $(BUILD_STATIC_LIBRARY) # Used by OEMs for factory test images. include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_MODULE := libminui LOCAL_WHOLE_STATIC_LIBRARIES += libminui LOCAL_SHARED_LIBRARIES := libpng diff --git a/minzip/Android.mk b/minzip/Android.mk index 045f3557..48d26bcb 100644 --- a/minzip/Android.mk +++ b/minzip/Android.mk @@ -16,6 +16,8 @@ LOCAL_STATIC_LIBRARIES := libselinux LOCAL_MODULE := libminzip +LOCAL_CLANG := true + LOCAL_CFLAGS += -Wall include $(BUILD_STATIC_LIBRARY) diff --git a/minzip/Zip.c b/minzip/Zip.c index 40712e03..a64c833f 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.c @@ -506,7 +506,6 @@ static bool processDeflatedEntry(const ZipArchive *pArchive, void *cookie) { long result = -1; - unsigned char readBuf[32 * 1024]; unsigned char procBuf[32 * 1024]; z_stream zstream; int zerr; @@ -603,7 +602,6 @@ bool mzProcessZipEntryContents(const ZipArchive *pArchive, void *cookie) { bool ret = false; - off_t oldOff; switch (pEntry->compression) { case STORED: diff --git a/mtdutils/Android.mk b/mtdutils/Android.mk index f04355b5..b7d35c27 100644 --- a/mtdutils/Android.mk +++ b/mtdutils/Android.mk @@ -6,10 +6,12 @@ LOCAL_SRC_FILES := \ mounts.c LOCAL_MODULE := libmtdutils +LOCAL_CLANG := true include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_SRC_FILES := flash_image.c LOCAL_MODULE := flash_image LOCAL_MODULE_TAGS := eng diff --git a/recovery.cpp b/recovery.cpp index b7a54589..a0c74524 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -326,14 +326,18 @@ static void rotate_logs(int max) { ensure_path_mounted(LAST_KMSG_FILE); for (int i = max-1; i >= 0; --i) { - std::string old_log = android::base::StringPrintf((i == 0) ? "%s" : "%s.%d", - LAST_LOG_FILE, i); + std::string old_log = android::base::StringPrintf("%s", LAST_LOG_FILE); + if (i > 0) { + old_log += "." + std::to_string(i); + } std::string new_log = android::base::StringPrintf("%s.%d", LAST_LOG_FILE, i+1); // Ignore errors if old_log doesn't exist. rename(old_log.c_str(), new_log.c_str()); - std::string old_kmsg = android::base::StringPrintf((i == 0) ? "%s" : "%s.%d", - LAST_KMSG_FILE, i); + std::string old_kmsg = android::base::StringPrintf("%s", LAST_KMSG_FILE); + if (i > 0) { + old_kmsg += "." + std::to_string(i); + } std::string new_kmsg = android::base::StringPrintf("%s.%d", LAST_KMSG_FILE, i+1); rename(old_kmsg.c_str(), new_kmsg.c_str()); } @@ -706,7 +710,10 @@ static void choose_recovery_file(Device* device) { // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x for (int i = 0; i < KEEP_LOG_COUNT; i++) { char* log_file; - if (asprintf(&log_file, (i == 0) ? "%s" : "%s.%d", LAST_LOG_FILE, i) == -1) { + int ret; + ret = (i == 0) ? asprintf(&log_file, "%s", LAST_LOG_FILE) : + asprintf(&log_file, "%s.%d", LAST_LOG_FILE, i); + if (ret == -1) { // memory allocation failure - return early. Should never happen. return; } @@ -717,7 +724,9 @@ static void choose_recovery_file(Device* device) { } char* kmsg_file; - if (asprintf(&kmsg_file, (i == 0) ? "%s" : "%s.%d", LAST_KMSG_FILE, i) == -1) { + ret = (i == 0) ? asprintf(&kmsg_file, "%s", LAST_KMSG_FILE) : + asprintf(&kmsg_file, "%s.%d", LAST_KMSG_FILE, i); + if (ret == -1) { // memory allocation failure - return early. Should never happen. return; } diff --git a/tests/Android.mk b/tests/Android.mk index 02a272a2..4ce00b45 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -17,6 +17,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) +LOCAL_CLANG := true LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk LOCAL_STATIC_LIBRARIES := libverifier LOCAL_SRC_FILES := asn1_decoder_test.cpp diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk index c7d4d374..e73c8f1b 100644 --- a/uncrypt/Android.mk +++ b/uncrypt/Android.mk @@ -16,6 +16,8 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) +LOCAL_CLANG := true + LOCAL_SRC_FILES := uncrypt.cpp LOCAL_MODULE := uncrypt diff --git a/updater/Android.mk b/updater/Android.mk index ff02a33b..a3df805e 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -17,6 +17,8 @@ include $(CLEAR_VARS) # needed only for OTA packages.) LOCAL_MODULE_TAGS := eng +LOCAL_CLANG := true + LOCAL_SRC_FILES := $(updater_src_files) ifeq ($(TARGET_USERIMAGES_USE_EXT4), true) From 56deefba73fb318ba0498da49adc64de960a6e29 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 19 May 2015 11:33:18 -0700 Subject: [PATCH 02/13] Stop using libstdc++. These are already getting libc++, so it isn't necessary. If any of the other static libraries (such as adb) use new or delete from libc++, there will be symbol collisions. Change-Id: I55e43ec60006d3c2403122fa1174bde06f18e09f (cherry picked from commit e49a9e527a51f43db792263bb60bfc91293848da) --- Android.mk | 2 -- applypatch/Android.mk | 4 ++-- updater/Android.mk | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Android.mk b/Android.mk index ac1ef83d..e43d55f6 100644 --- a/Android.mk +++ b/Android.mk @@ -77,7 +77,6 @@ LOCAL_STATIC_LIBRARIES := \ libcutils \ liblog \ libselinux \ - libstdc++ \ libm \ libc @@ -122,7 +121,6 @@ LOCAL_STATIC_LIBRARIES := \ libminui \ libminzip \ libcutils \ - libstdc++ \ libc include $(BUILD_EXECUTABLE) diff --git a/applypatch/Android.mk b/applypatch/Android.mk index 4936db2a..eb3e4580 100644 --- a/applypatch/Android.mk +++ b/applypatch/Android.mk @@ -32,7 +32,7 @@ LOCAL_SRC_FILES := main.c LOCAL_MODULE := applypatch LOCAL_C_INCLUDES += bootable/recovery LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz -LOCAL_SHARED_LIBRARIES += libz libcutils libstdc++ libc +LOCAL_SHARED_LIBRARIES += libz libcutils libc include $(BUILD_EXECUTABLE) @@ -45,7 +45,7 @@ LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_MODULE_TAGS := eng LOCAL_C_INCLUDES += bootable/recovery LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz -LOCAL_STATIC_LIBRARIES += libz libcutils libstdc++ libc +LOCAL_STATIC_LIBRARIES += libz libcutils libc include $(BUILD_EXECUTABLE) diff --git a/updater/Android.mk b/updater/Android.mk index a3df805e..a0ea06fa 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -34,7 +34,7 @@ endif LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UPDATER_LIBS) $(TARGET_RECOVERY_UPDATER_EXTRA_LIBS) LOCAL_STATIC_LIBRARIES += libapplypatch libedify libmtdutils libminzip libz LOCAL_STATIC_LIBRARIES += libmincrypt libbz -LOCAL_STATIC_LIBRARIES += libcutils liblog libstdc++ libc +LOCAL_STATIC_LIBRARIES += libcutils liblog libc LOCAL_STATIC_LIBRARIES += libselinux tune2fs_static_libraries := \ libext2_com_err \ From 806f72f9e6ec0d15b550b79b0baa92a93fc646e3 Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Tue, 12 May 2015 12:48:46 +0100 Subject: [PATCH 03/13] Add error and range checks to parse_range Only trusted input is passed to parse_range, but check for invalid input to catch possible problems in transfer lists. Bug: 21033983 Bug: 21034030 Bug: 21034172 Bug: 21034406 Change-Id: I1e266de3de15c99ee596ebdb034419fdfe7eba1f (cherry picked from commit f2bac04e1ba0a5b79f8adbc35b493923b776f8b2) --- updater/blockimg.c | 81 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index b006d10c..e0be0391 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -61,30 +61,91 @@ typedef struct { int pos[0]; } RangeSet; +#define RANGESET_MAX_POINTS \ + ((int)((INT_MAX / sizeof(int)) - sizeof(RangeSet))) + static RangeSet* parse_range(char* text) { char* save; - int num; - num = strtol(strtok_r(text, ",", &save), NULL, 0); + char* token; + int i, num; + long int val; + RangeSet* out = NULL; + size_t bufsize; - RangeSet* out = malloc(sizeof(RangeSet) + num * sizeof(int)); - if (out == NULL) { - fprintf(stderr, "failed to allocate range of %zu bytes\n", - sizeof(RangeSet) + num * sizeof(int)); - exit(1); + if (!text) { + goto err; } + + token = strtok_r(text, ",", &save); + + if (!token) { + goto err; + } + + val = strtol(token, NULL, 0); + + if (val < 2 || val > RANGESET_MAX_POINTS) { + goto err; + } else if (val % 2) { + goto err; // must be even + } + + num = (int) val; + bufsize = sizeof(RangeSet) + num * sizeof(int); + + out = malloc(bufsize); + + if (!out) { + fprintf(stderr, "failed to allocate range of %zu bytes\n", bufsize); + goto err; + } + out->count = num / 2; out->size = 0; - int i; + for (i = 0; i < num; ++i) { - out->pos[i] = strtol(strtok_r(NULL, ",", &save), NULL, 0); - if (i%2) { + token = strtok_r(NULL, ",", &save); + + if (!token) { + goto err; + } + + val = strtol(token, NULL, 0); + + if (val < 0 || val > 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; + } + out->size -= out->pos[i]; } } + if (out->size <= 0) { + goto err; + } + return out; + +err: + fprintf(stderr, "failed to parse range '%s'\n", text ? text : "NULL"); + exit(1); } static int range_overlaps(RangeSet* r1, RangeSet* r2) { From 818fa781d1dbe35c0c5bfff3ebff1b45a2a676f0 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 23 Jun 2015 23:23:33 -0700 Subject: [PATCH 04/13] DO NOT MERGE recovery: Switch applypatch/ and updater/ to cpp. Mostly trivial changes to make cpp compiler happy. Change-Id: I69bd1d96fcccf506007f6144faf37e11cfba1270 (cherry picked from commit ba9a42aa7e10686de186636fe9fecbf8c4cc7c19) --- applypatch/Android.mk | 8 +- applypatch/{applypatch.c => applypatch.cpp} | 155 ++++++++--------- applypatch/{bsdiff.c => bsdiff.cpp} | 48 +++--- applypatch/{bspatch.c => bspatch.cpp} | 5 +- applypatch/{freecache.c => freecache.cpp} | 28 +++- applypatch/{imgdiff.c => imgdiff.cpp} | 84 +++++----- applypatch/{imgpatch.c => imgpatch.cpp} | 4 +- applypatch/{main.c => main.cpp} | 31 ++-- applypatch/{utils.c => utils.cpp} | 6 +- minzip/Hash.h | 8 + roots.cpp | 2 - updater/Android.mk | 18 +- updater/{blockimg.c => blockimg.cpp} | 31 ++-- updater/{install.c => install.cpp} | 174 ++++++++++---------- updater/{updater.c => updater.cpp} | 2 +- 15 files changed, 304 insertions(+), 300 deletions(-) rename applypatch/{applypatch.c => applypatch.cpp} (89%) rename applypatch/{bsdiff.c => bsdiff.cpp} (87%) rename applypatch/{bspatch.c => bspatch.cpp} (98%) rename applypatch/{freecache.c => freecache.cpp} (80%) rename applypatch/{imgdiff.c => imgdiff.cpp} (92%) rename applypatch/{imgpatch.c => imgpatch.cpp} (97%) rename applypatch/{main.c => main.cpp} (87%) rename applypatch/{utils.c => utils.cpp} (91%) rename updater/{blockimg.c => blockimg.cpp} (98%) rename updater/{install.c => install.cpp} (92%) rename updater/{updater.c => updater.cpp} (98%) diff --git a/applypatch/Android.mk b/applypatch/Android.mk index eb3e4580..1f73fd89 100644 --- a/applypatch/Android.mk +++ b/applypatch/Android.mk @@ -17,7 +17,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_CLANG := true -LOCAL_SRC_FILES := applypatch.c bspatch.c freecache.c imgpatch.c utils.c +LOCAL_SRC_FILES := applypatch.cpp bspatch.cpp freecache.cpp imgpatch.cpp utils.cpp LOCAL_MODULE := libapplypatch LOCAL_MODULE_TAGS := eng LOCAL_C_INCLUDES += external/bzip2 external/zlib bootable/recovery @@ -28,7 +28,7 @@ include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_CLANG := true -LOCAL_SRC_FILES := main.c +LOCAL_SRC_FILES := main.cpp LOCAL_MODULE := applypatch LOCAL_C_INCLUDES += bootable/recovery LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz @@ -39,7 +39,7 @@ include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) LOCAL_CLANG := true -LOCAL_SRC_FILES := main.c +LOCAL_SRC_FILES := main.cpp LOCAL_MODULE := applypatch_static LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_MODULE_TAGS := eng @@ -52,7 +52,7 @@ include $(BUILD_EXECUTABLE) include $(CLEAR_VARS) LOCAL_CLANG := true -LOCAL_SRC_FILES := imgdiff.c utils.c bsdiff.c +LOCAL_SRC_FILES := imgdiff.cpp utils.cpp bsdiff.cpp LOCAL_MODULE := imgdiff LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_C_INCLUDES += external/zlib external/bzip2 diff --git a/applypatch/applypatch.c b/applypatch/applypatch.cpp similarity index 89% rename from applypatch/applypatch.c rename to applypatch/applypatch.cpp index 2358d429..96bd88e8 100644 --- a/applypatch/applypatch.c +++ b/applypatch/applypatch.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -22,9 +23,7 @@ #include #include #include -#include #include -#include #include "mincrypt/sha.h" #include "applypatch.h" @@ -65,7 +64,7 @@ int LoadFileContents(const char* filename, FileContents* file) { } file->size = file->st.st_size; - file->data = malloc(file->size); + file->data = reinterpret_cast(malloc(file->size)); FILE* f = fopen(filename, "rb"); if (f == NULL) { @@ -75,10 +74,9 @@ int LoadFileContents(const char* filename, FileContents* file) { return -1; } - ssize_t bytes_read = fread(file->data, 1, file->size, f); - if (bytes_read != file->size) { - printf("short read of \"%s\" (%ld bytes of %ld)\n", - filename, (long)bytes_read, (long)file->size); + size_t bytes_read = fread(file->data, 1, file->size, f); + if (bytes_read != static_cast(file->size)) { + printf("short read of \"%s\" (%zu bytes of %zd)\n", filename, bytes_read, file->size); free(file->data); file->data = NULL; return -1; @@ -93,8 +91,8 @@ static size_t* size_array; // comparison function for qsort()ing an int array of indexes into // size_array[]. static int compare_size_indices(const void* a, const void* b) { - int aa = *(int*)a; - int bb = *(int*)b; + const int aa = *reinterpret_cast(a); + const int bb = *reinterpret_cast(b); if (size_array[aa] < size_array[bb]) { return -1; } else if (size_array[aa] > size_array[bb]) { @@ -132,8 +130,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { } else if (strcmp(magic, "EMMC") == 0) { type = EMMC; } else { - printf("LoadPartitionContents called with bad filename (%s)\n", - filename); + printf("LoadPartitionContents called with bad filename (%s)\n", filename); return -1; } const char* partition = strtok(NULL, ":"); @@ -151,9 +148,9 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { } int pairs = (colons-1)/2; // # of (size,sha1) pairs in filename - int* index = malloc(pairs * sizeof(int)); - size_t* size = malloc(pairs * sizeof(size_t)); - char** sha1sum = malloc(pairs * sizeof(char*)); + int* index = reinterpret_cast(malloc(pairs * sizeof(int))); + size_t* size = reinterpret_cast(malloc(pairs * sizeof(size_t))); + char** sha1sum = reinterpret_cast(malloc(pairs * sizeof(char*))); for (i = 0; i < pairs; ++i) { const char* size_str = strtok(NULL, ":"); @@ -175,7 +172,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { FILE* dev = NULL; switch (type) { - case MTD: + case MTD: { if (!mtd_partitions_scanned) { mtd_scan_partitions(); mtd_partitions_scanned = 1; @@ -195,6 +192,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { return -1; } break; + } case EMMC: dev = fopen(partition, "rb"); @@ -210,7 +208,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { uint8_t parsed_sha[SHA_DIGEST_SIZE]; // allocate enough memory to hold the largest size. - file->data = malloc(size[index[pairs-1]]); + file->data = reinterpret_cast(malloc(size[index[pairs-1]])); char* p = (char*)file->data; file->size = 0; // # bytes read so far @@ -248,8 +246,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { const uint8_t* sha_so_far = SHA_final(&temp_ctx); if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) { - printf("failed to parse sha1 %s in %s\n", - sha1sum[index[i]], filename); + printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]], filename); free(file->data); file->data = NULL; return -1; @@ -280,15 +277,14 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { if (i == pairs) { // Ran off the end of the list of (size,sha1) pairs without // finding a match. - printf("contents of partition \"%s\" didn't match %s\n", - partition, filename); + printf("contents of partition \"%s\" didn't match %s\n", partition, filename); free(file->data); file->data = NULL; return -1; } const uint8_t* sha_final = SHA_final(&sha_ctx); - for (i = 0; i < SHA_DIGEST_SIZE; ++i) { + for (size_t i = 0; i < SHA_DIGEST_SIZE; ++i) { file->sha1[i] = sha_final[i]; } @@ -311,16 +307,14 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { int SaveFileContents(const char* filename, const FileContents* file) { int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR); if (fd < 0) { - printf("failed to open \"%s\" for write: %s\n", - filename, strerror(errno)); + printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno)); return -1; } ssize_t bytes_written = FileSink(file->data, file->size, &fd); if (bytes_written != file->size) { - printf("short write of \"%s\" (%ld bytes of %ld) (%s)\n", - filename, (long)bytes_written, (long)file->size, - strerror(errno)); + printf("short write of \"%s\" (%zd bytes of %zd) (%s)\n", + filename, bytes_written, file->size, strerror(errno)); close(fd); return -1; } @@ -348,8 +342,7 @@ int SaveFileContents(const char* filename, const FileContents* file) { // Write a memory buffer to 'target' partition, a string of the form // "MTD:[:...]" or "EMMC::". Return 0 on // success. -int WriteToPartition(unsigned char* data, size_t len, - const char* target) { +int WriteToPartition(unsigned char* data, size_t len, const char* target) { char* copy = strdup(target); const char* magic = strtok(copy, ":"); @@ -370,7 +363,7 @@ int WriteToPartition(unsigned char* data, size_t len, } switch (type) { - case MTD: + case MTD: { if (!mtd_partitions_scanned) { mtd_scan_partitions(); mtd_partitions_scanned = 1; @@ -378,22 +371,19 @@ int WriteToPartition(unsigned char* data, size_t len, const MtdPartition* mtd = mtd_find_partition_by_name(partition); if (mtd == NULL) { - printf("mtd partition \"%s\" not found for writing\n", - partition); + printf("mtd partition \"%s\" not found for writing\n", partition); return -1; } MtdWriteContext* ctx = mtd_write_partition(mtd); if (ctx == NULL) { - printf("failed to init mtd partition \"%s\" for writing\n", - partition); + printf("failed to init mtd partition \"%s\" for writing\n", partition); return -1; } - size_t written = mtd_write_data(ctx, (char*)data, len); + size_t written = mtd_write_data(ctx, reinterpret_cast(data), len); if (written != len) { - printf("only wrote %zu of %zu bytes to MTD %s\n", - written, len, partition); + printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition); mtd_write_close(ctx); return -1; } @@ -409,22 +399,20 @@ int WriteToPartition(unsigned char* data, size_t len, return -1; } break; + } - case EMMC: - { + case EMMC: { size_t start = 0; - int success = 0; + bool success = false; int fd = open(partition, O_RDWR | O_SYNC); if (fd < 0) { printf("failed to open %s: %s\n", partition, strerror(errno)); return -1; } - int attempt; - for (attempt = 0; attempt < 2; ++attempt) { + for (int attempt = 0; attempt < 2; ++attempt) { if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) { - printf("failed seek on %s: %s\n", - partition, strerror(errno)); + printf("failed seek on %s: %s\n", partition, strerror(errno)); return -1; } while (start < len) { @@ -439,23 +427,20 @@ int WriteToPartition(unsigned char* data, size_t len, start += written; } if (fsync(fd) != 0) { - printf("failed to sync to %s (%s)\n", - partition, strerror(errno)); + printf("failed to sync to %s (%s)\n", partition, strerror(errno)); return -1; } if (close(fd) != 0) { - printf("failed to close %s (%s)\n", - partition, strerror(errno)); + printf("failed to close %s (%s)\n", partition, strerror(errno)); return -1; } fd = open(partition, O_RDONLY); if (fd < 0) { - printf("failed to reopen %s for verify (%s)\n", - partition, strerror(errno)); + printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno)); return -1; } - // drop caches so our subsequent verification read + // Drop caches so our subsequent verification read // won't just be reading the cache. sync(); int dc = open("/proc/sys/vm/drop_caches", O_WRONLY); @@ -475,10 +460,11 @@ int WriteToPartition(unsigned char* data, size_t len, } unsigned char buffer[4096]; start = len; - size_t p; - for (p = 0; p < len; p += sizeof(buffer)) { + for (size_t p = 0; p < len; p += sizeof(buffer)) { size_t to_read = len - p; - if (to_read > sizeof(buffer)) to_read = sizeof(buffer); + if (to_read > sizeof(buffer)) { + to_read = sizeof(buffer); + } size_t so_far = 0; while (so_far < to_read) { @@ -489,14 +475,14 @@ int WriteToPartition(unsigned char* data, size_t len, partition, p, strerror(errno)); return -1; } - if ((size_t)read_count < to_read) { + if (static_cast(read_count) < to_read) { printf("short verify read %s at %zu: %zd %zu %s\n", partition, p, read_count, to_read, strerror(errno)); } so_far += read_count; } - if (memcmp(buffer, data+p, to_read)) { + if (memcmp(buffer, data+p, to_read) != 0) { printf("verification failed starting at %zu\n", p); start = p; break; @@ -534,10 +520,9 @@ int WriteToPartition(unsigned char* data, size_t len, // the form ":". Return 0 on success, -1 on any // error. int ParseSha1(const char* str, uint8_t* digest) { - int i; const char* ps = str; uint8_t* pd = digest; - for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) { + for (int i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) { int digit; if (*ps >= '0' && *ps <= '9') { digit = *ps - '0'; @@ -564,9 +549,8 @@ int ParseSha1(const char* str, uint8_t* digest) { // found. int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str, int num_patches) { - int i; uint8_t patch_sha1[SHA_DIGEST_SIZE]; - for (i = 0; i < num_patches; ++i) { + for (int i = 0; i < num_patches; ++i) { if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 && memcmp(patch_sha1, sha1, SHA_DIGEST_SIZE) == 0) { return i; @@ -578,8 +562,8 @@ int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str, // Returns 0 if the contents of the file (argv[2]) or the cached file // match any of the sha1's on the command line (argv[3:]). Returns // nonzero otherwise. -int applypatch_check(const char* filename, - int num_patches, char** const patch_sha1_str) { +int applypatch_check(const char* filename, int num_patches, + char** const patch_sha1_str) { FileContents file; file.data = NULL; @@ -624,13 +608,13 @@ int ShowLicenses() { } ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) { - int fd = *(int *)token; + int fd = *reinterpret_cast(token); ssize_t done = 0; ssize_t wrote; - while (done < (ssize_t) len) { + while (done < len) { wrote = TEMP_FAILURE_RETRY(write(fd, data+done, len-done)); if (wrote == -1) { - printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno)); + printf("error writing %zd bytes: %s\n", (len-done), strerror(errno)); return done; } done += wrote; @@ -645,7 +629,7 @@ typedef struct { } MemorySinkInfo; ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) { - MemorySinkInfo* msi = (MemorySinkInfo*)token; + MemorySinkInfo* msi = reinterpret_cast(token); if (msi->size - msi->pos < len) { return -1; } @@ -675,9 +659,8 @@ int CacheSizeCheck(size_t bytes) { } static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { - int i; const char* hex = "0123456789abcdef"; - for (i = 0; i < 4; ++i) { + for (size_t i = 0; i < 4; ++i) { putchar(hex[(sha1[i]>>4) & 0xf]); putchar(hex[sha1[i] & 0xf]); } @@ -719,8 +702,7 @@ int applypatch(const char* source_filename, Value* bonus_data) { printf("patch %s: ", source_filename); - if (target_filename[0] == '-' && - target_filename[1] == '\0') { + if (target_filename[0] == '-' && target_filename[1] == '\0') { target_filename = source_filename; } @@ -761,8 +743,7 @@ int applypatch(const char* source_filename, } if (source_file.data != NULL) { - int to_use = FindMatchingPatch(source_file.sha1, - patch_sha1_str, num_patches); + int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str, num_patches); if (to_use >= 0) { source_patch_value = patch_data[to_use]; } @@ -779,8 +760,7 @@ int applypatch(const char* source_filename, return 1; } - int to_use = FindMatchingPatch(copy_file.sha1, - patch_sha1_str, num_patches); + int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str, num_patches); if (to_use >= 0) { copy_patch_value = patch_data[to_use]; } @@ -865,8 +845,8 @@ static int GenerateTarget(FileContents* source_file, (free_space > (256 << 10)) && // 256k (two-block) minimum (free_space > (target_size * 3 / 2)); // 50% margin of error if (!enough_space) { - printf("target %ld bytes; free space %ld bytes; retry %d; enough %d\n", - (long)target_size, (long)free_space, retry, enough_space); + printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n", + target_size, free_space, retry, enough_space); } } @@ -884,8 +864,7 @@ static int GenerateTarget(FileContents* source_file, // It's impossible to free space on the target filesystem by // deleting the source if the source is a partition. If // we're ever in a state where we need to do this, fail. - printf("not enough free space for target but source " - "is partition\n"); + printf("not enough free space for target but source is partition\n"); return 1; } @@ -902,7 +881,7 @@ static int GenerateTarget(FileContents* source_file, unlink(source_filename); size_t free_space = FreeSpaceForFile(target_fs); - printf("(now %ld bytes free for target) ", (long)free_space); + printf("(now %zu bytes free for target) ", free_space); } } @@ -927,10 +906,9 @@ static int GenerateTarget(FileContents* source_file, if (strncmp(target_filename, "MTD:", 4) == 0 || strncmp(target_filename, "EMMC:", 5) == 0) { // We store the decoded output in memory. - msi.buffer = malloc(target_size); + msi.buffer = reinterpret_cast(malloc(target_size)); if (msi.buffer == NULL) { - printf("failed to alloc %ld bytes for output\n", - (long)target_size); + printf("failed to alloc %zu bytes for output\n", target_size); return 1; } msi.pos = 0; @@ -939,12 +917,11 @@ static int GenerateTarget(FileContents* source_file, token = &msi; } else { // We write the decoded output to ".patch". - outname = (char*)malloc(strlen(target_filename) + 10); + outname = reinterpret_cast(malloc(strlen(target_filename) + 10)); strcpy(outname, target_filename); strcat(outname, ".patch"); - output = open(outname, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, - S_IRUSR | S_IWUSR); + output = open(outname, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR); if (output < 0) { printf("failed to open output file %s: %s\n", outname, strerror(errno)); @@ -1025,23 +1002,23 @@ static int GenerateTarget(FileContents* source_file, printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno)); return 1; } - if (chown(outname, source_to_use->st.st_uid, - source_to_use->st.st_gid) != 0) { + if (chown(outname, source_to_use->st.st_uid, source_to_use->st.st_gid) != 0) { printf("chown of \"%s\" failed: %s\n", outname, strerror(errno)); return 1; } // Finally, rename the .patch file to replace the target file. if (rename(outname, target_filename) != 0) { - printf("rename of .patch to \"%s\" failed: %s\n", - target_filename, strerror(errno)); + printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno)); return 1; } } // If this run of applypatch created the copy, and we're here, we // can delete it. - if (made_copy) unlink(CACHE_TEMP_SOURCE); + if (made_copy) { + unlink(CACHE_TEMP_SOURCE); + } // Success! return 0; diff --git a/applypatch/bsdiff.c b/applypatch/bsdiff.cpp similarity index 87% rename from applypatch/bsdiff.c rename to applypatch/bsdiff.cpp index b6d342b7..55dbe5cf 100644 --- a/applypatch/bsdiff.c +++ b/applypatch/bsdiff.cpp @@ -156,24 +156,24 @@ static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize) for(i=0;iy) { *pos=I[st]; @@ -185,10 +185,10 @@ static off_t search(off_t *I,u_char *old,off_t oldsize, }; x=st+(en-st)/2; - if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) { - return search(I,old,oldsize,new,newsize,x,en,pos); + if(memcmp(old+I[x],newdata,MIN(oldsize-I[x],newsize))<0) { + return search(I,old,oldsize,newdata,newsize,x,en,pos); } else { - return search(I,old,oldsize,new,newsize,st,x,pos); + return search(I,old,oldsize,newdata,newsize,st,x,pos); }; } @@ -212,8 +212,8 @@ static void offtout(off_t x,u_char *buf) // This is main() from bsdiff.c, with the following changes: // -// - old, oldsize, new, newsize are arguments; we don't load this -// data from files. old and new are owned by the caller; we +// - old, oldsize, newdata, newsize are arguments; we don't load this +// data from files. old and newdata are owned by the caller; we // don't free them at the end. // // - the "I" block of memory is owned by the caller, who passes a @@ -221,7 +221,7 @@ static void offtout(off_t x,u_char *buf) // bsdiff() multiple times with the same 'old' data, we only do // the qsufsort() step the first time. // -int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* new, off_t newsize, +int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* newdata, off_t newsize, const char* patch_filename) { int fd; @@ -242,15 +242,15 @@ int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* new, off_t newsize, if (*IP == NULL) { off_t* V; - *IP = malloc((oldsize+1) * sizeof(off_t)); - V = malloc((oldsize+1) * sizeof(off_t)); + *IP = reinterpret_cast(malloc((oldsize+1) * sizeof(off_t))); + V = reinterpret_cast(malloc((oldsize+1) * sizeof(off_t))); qsufsort(*IP, V, old, oldsize); free(V); } I = *IP; - if(((db=malloc(newsize+1))==NULL) || - ((eb=malloc(newsize+1))==NULL)) err(1,NULL); + if(((db=reinterpret_cast(malloc(newsize+1)))==NULL) || + ((eb=reinterpret_cast(malloc(newsize+1)))==NULL)) err(1,NULL); dblen=0; eblen=0; @@ -284,26 +284,26 @@ int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* new, off_t newsize, oldscore=0; for(scsc=scan+=len;scanoldscore+8)) break; if((scan+lastoffsetSf*2-lenf) { Sf=s; lenf=i; }; }; @@ -312,7 +312,7 @@ int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* new, off_t newsize, if(scan=lastscan+i)&&(pos>=i);i++) { - if(old[pos-i]==new[scan-i]) s++; + if(old[pos-i]==newdata[scan-i]) s++; if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; }; }; @@ -321,9 +321,9 @@ int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* new, off_t newsize, overlap=(lastscan+lenf)-(scan-lenb); s=0;Ss=0;lens=0; for(i=0;iSs) { Ss=s; lens=i+1; }; }; @@ -333,9 +333,9 @@ int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* new, off_t newsize, }; for(i=0;i(malloc(*new_size)); if (*new_data == NULL) { - printf("failed to allocate %ld bytes of memory for output file\n", - (long)*new_size); + printf("failed to allocate %zd bytes of memory for output file\n", *new_size); return 1; } diff --git a/applypatch/freecache.c b/applypatch/freecache.cpp similarity index 80% rename from applypatch/freecache.c rename to applypatch/freecache.cpp index 9827fda0..2eb2f55e 100644 --- a/applypatch/freecache.c +++ b/applypatch/freecache.cpp @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2010 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. + */ + #include #include #include @@ -76,7 +92,7 @@ int FindExpendableFiles(char*** names, int* entries) { struct dirent* de; int size = 32; *entries = 0; - *names = malloc(size * sizeof(char*)); + *names = reinterpret_cast(malloc(size * sizeof(char*))); char path[FILENAME_MAX]; @@ -84,8 +100,7 @@ int FindExpendableFiles(char*** names, int* entries) { // directories. const char* dirs[2] = {"/cache", "/cache/recovery/otatest"}; - unsigned int i; - for (i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) { + for (size_t i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) { d = opendir(dirs[i]); if (d == NULL) { printf("error opening %s: %s\n", dirs[i], strerror(errno)); @@ -107,7 +122,7 @@ int FindExpendableFiles(char*** names, int* entries) { if (stat(path, &st) == 0 && S_ISREG(st.st_mode)) { if (*entries >= size) { size *= 2; - *names = realloc(*names, size * sizeof(char*)); + *names = reinterpret_cast(realloc(*names, size * sizeof(char*))); } (*names)[(*entries)++] = strdup(path); } @@ -127,8 +142,7 @@ int FindExpendableFiles(char*** names, int* entries) { int MakeFreeSpaceOnCache(size_t bytes_needed) { size_t free_now = FreeSpaceForFile("/cache"); - printf("%ld bytes free on /cache (%ld needed)\n", - (long)free_now, (long)bytes_needed); + printf("%zu bytes free on /cache (%zu needed)\n", free_now, bytes_needed); if (free_now >= bytes_needed) { return 0; @@ -158,7 +172,7 @@ int MakeFreeSpaceOnCache(size_t bytes_needed) { if (names[i]) { unlink(names[i]); free_now = FreeSpaceForFile("/cache"); - printf("deleted %s; now %ld bytes free\n", names[i], (long)free_now); + printf("deleted %s; now %zu bytes free\n", names[i], free_now); free(names[i]); } } diff --git a/applypatch/imgdiff.c b/applypatch/imgdiff.cpp similarity index 92% rename from applypatch/imgdiff.c rename to applypatch/imgdiff.cpp index 3bac8be9..4d83ffb2 100644 --- a/applypatch/imgdiff.c +++ b/applypatch/imgdiff.cpp @@ -122,6 +122,7 @@ */ #include +#include #include #include #include @@ -179,7 +180,7 @@ static int fileentry_compare(const void* a, const void* b) { } // from bsdiff.c -int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* new, off_t newsize, +int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* newdata, off_t newsize, const char* patch_filename); unsigned char* ReadZip(const char* filename, @@ -191,9 +192,10 @@ unsigned char* ReadZip(const char* filename, return NULL; } - unsigned char* img = malloc(st.st_size); + size_t sz = static_cast(st.st_size); + unsigned char* img = reinterpret_cast(malloc(sz)); FILE* f = fopen(filename, "rb"); - if (fread(img, 1, st.st_size, f) != st.st_size) { + if (fread(img, 1, sz, f) != sz) { printf("failed to read \"%s\" %s\n", filename, strerror(errno)); fclose(f); return NULL; @@ -218,7 +220,8 @@ unsigned char* ReadZip(const char* filename, int cdcount = Read2(img+i+8); int cdoffset = Read4(img+i+16); - ZipFileEntry* temp_entries = malloc(cdcount * sizeof(ZipFileEntry)); + ZipFileEntry* temp_entries = reinterpret_cast(malloc( + cdcount * sizeof(ZipFileEntry))); int entrycount = 0; unsigned char* cd = img+cdoffset; @@ -235,7 +238,7 @@ unsigned char* ReadZip(const char* filename, int mlen = Read2(cd+32); // file comment len int hoffset = Read4(cd+42); // local header offset - char* filename = malloc(nlen+1); + char* filename = reinterpret_cast(malloc(nlen+1)); memcpy(filename, cd+46, nlen); filename[nlen] = '\0'; @@ -284,7 +287,7 @@ unsigned char* ReadZip(const char* filename, #endif *num_chunks = 0; - *chunks = malloc((entrycount*2+2) * sizeof(ImageChunk)); + *chunks = reinterpret_cast(malloc((entrycount*2+2) * sizeof(ImageChunk))); ImageChunk* curr = *chunks; if (include_pseudo_chunk) { @@ -311,7 +314,7 @@ unsigned char* ReadZip(const char* filename, curr->I = NULL; curr->len = temp_entries[nextentry].uncomp_len; - curr->data = malloc(curr->len); + curr->data = reinterpret_cast(malloc(curr->len)); z_stream strm; strm.zalloc = Z_NULL; @@ -381,9 +384,10 @@ unsigned char* ReadImage(const char* filename, return NULL; } - unsigned char* img = malloc(st.st_size + 4); + size_t sz = static_cast(st.st_size); + unsigned char* img = reinterpret_cast(malloc(sz + 4)); FILE* f = fopen(filename, "rb"); - if (fread(img, 1, st.st_size, f) != st.st_size) { + if (fread(img, 1, sz, f) != sz) { printf("failed to read \"%s\" %s\n", filename, strerror(errno)); fclose(f); return NULL; @@ -393,17 +397,17 @@ unsigned char* ReadImage(const char* filename, // append 4 zero bytes to the data so we can always search for the // four-byte string 1f8b0800 starting at any point in the actual // file data, without special-casing the end of the data. - memset(img+st.st_size, 0, 4); + memset(img+sz, 0, 4); size_t pos = 0; *num_chunks = 0; *chunks = NULL; - while (pos < st.st_size) { + while (pos < sz) { unsigned char* p = img+pos; - if (st.st_size - pos >= 4 && + if (sz - pos >= 4 && p[0] == 0x1f && p[1] == 0x8b && p[2] == 0x08 && // deflate compression p[3] == 0x00) { // no header flags @@ -411,7 +415,8 @@ unsigned char* ReadImage(const char* filename, size_t chunk_offset = pos; *num_chunks += 3; - *chunks = realloc(*chunks, *num_chunks * sizeof(ImageChunk)); + *chunks = reinterpret_cast(realloc(*chunks, + *num_chunks * sizeof(ImageChunk))); ImageChunk* curr = *chunks + (*num_chunks-3); // create a normal chunk for the header. @@ -435,7 +440,7 @@ unsigned char* ReadImage(const char* filename, size_t allocated = 32768; curr->len = 0; - curr->data = malloc(allocated); + curr->data = reinterpret_cast(malloc(allocated)); curr->start = pos; curr->deflate_data = p; @@ -443,7 +448,7 @@ unsigned char* ReadImage(const char* filename, strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; - strm.avail_in = st.st_size - pos; + strm.avail_in = sz - pos; strm.next_in = p; // -15 means we are decoding a 'raw' deflate stream; zlib will @@ -465,11 +470,11 @@ unsigned char* ReadImage(const char* filename, curr->len = allocated - strm.avail_out; if (strm.avail_out == 0) { allocated *= 2; - curr->data = realloc(curr->data, allocated); + curr->data = reinterpret_cast(realloc(curr->data, allocated)); } } while (ret != Z_STREAM_END); - curr->deflate_len = st.st_size - strm.avail_in - pos; + curr->deflate_len = sz - strm.avail_in - pos; inflateEnd(&strm); pos += curr->deflate_len; p += curr->deflate_len; @@ -493,8 +498,8 @@ unsigned char* ReadImage(const char* filename, // the decompression. size_t footer_size = Read4(p-4); if (footer_size != curr[-2].len) { - printf("Error: footer size %d != decompressed size %d\n", - footer_size, curr[-2].len); + printf("Error: footer size %zu != decompressed size %zu\n", + footer_size, curr[-2].len); free(img); return NULL; } @@ -502,7 +507,7 @@ unsigned char* ReadImage(const char* filename, // Reallocate the list for every chunk; we expect the number of // chunks to be small (5 for typical boot and recovery images). ++*num_chunks; - *chunks = realloc(*chunks, *num_chunks * sizeof(ImageChunk)); + *chunks = reinterpret_cast(realloc(*chunks, *num_chunks * sizeof(ImageChunk))); ImageChunk* curr = *chunks + (*num_chunks-1); curr->start = pos; curr->I = NULL; @@ -512,7 +517,7 @@ unsigned char* ReadImage(const char* filename, curr->type = CHUNK_NORMAL; curr->data = p; - for (curr->len = 0; curr->len < (st.st_size - pos); ++curr->len) { + for (curr->len = 0; curr->len < (sz - pos); ++curr->len) { if (p[curr->len] == 0x1f && p[curr->len+1] == 0x8b && p[curr->len+2] == 0x08 && @@ -587,7 +592,7 @@ int ReconstructDeflateChunk(ImageChunk* chunk) { } size_t p = 0; - unsigned char* out = malloc(BUFFER_SIZE); + unsigned char* out = reinterpret_cast(malloc(BUFFER_SIZE)); // We only check two combinations of encoder parameters: level 6 // (the default) and level 9 (the maximum). @@ -638,9 +643,11 @@ unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) { return NULL; } - unsigned char* data = malloc(st.st_size); + size_t sz = static_cast(st.st_size); + // TODO: Memory leak on error return. + unsigned char* data = reinterpret_cast(malloc(sz)); - if (tgt->type == CHUNK_NORMAL && tgt->len <= st.st_size) { + if (tgt->type == CHUNK_NORMAL && tgt->len <= sz) { unlink(ptemp); tgt->type = CHUNK_RAW; @@ -648,14 +655,14 @@ unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) { return tgt->data; } - *size = st.st_size; + *size = sz; FILE* f = fopen(ptemp, "rb"); if (f == NULL) { printf("failed to open patch %s: %s\n", ptemp, strerror(errno)); return NULL; } - if (fread(data, 1, st.st_size, f) != st.st_size) { + if (fread(data, 1, sz, f) != sz) { printf("failed to read patch %s: %s\n", ptemp, strerror(errno)); return NULL; } @@ -781,9 +788,8 @@ ImageChunk* FindChunkByName(const char* name, } void DumpChunks(ImageChunk* chunks, int num_chunks) { - int i; - for (i = 0; i < num_chunks; ++i) { - printf("chunk %d: type %d start %d len %d\n", + for (int i = 0; i < num_chunks; ++i) { + printf("chunk %d: type %d start %zu len %zu\n", i, chunks[i].type, chunks[i].start, chunks[i].len); } } @@ -806,7 +812,7 @@ int main(int argc, char** argv) { return 1; } bonus_size = st.st_size; - bonus_data = malloc(bonus_size); + bonus_data = reinterpret_cast(malloc(bonus_size)); FILE* f = fopen(argv[2], "rb"); if (f == NULL) { printf("failed to open bonus file %s: %s\n", argv[2], strerror(errno)); @@ -953,8 +959,9 @@ int main(int argc, char** argv) { DumpChunks(src_chunks, num_src_chunks); printf("Construct patches for %d chunks...\n", num_tgt_chunks); - unsigned char** patch_data = malloc(num_tgt_chunks * sizeof(unsigned char*)); - size_t* patch_size = malloc(num_tgt_chunks * sizeof(size_t)); + unsigned char** patch_data = reinterpret_cast(malloc( + num_tgt_chunks * sizeof(unsigned char*))); + size_t* patch_size = reinterpret_cast(malloc(num_tgt_chunks * sizeof(size_t))); for (i = 0; i < num_tgt_chunks; ++i) { if (zip_mode) { ImageChunk* src; @@ -967,15 +974,16 @@ int main(int argc, char** argv) { } } else { if (i == 1 && bonus_data) { - printf(" using %d bytes of bonus data for chunk %d\n", bonus_size, i); - src_chunks[i].data = realloc(src_chunks[i].data, src_chunks[i].len + bonus_size); + printf(" using %zu bytes of bonus data for chunk %d\n", bonus_size, i); + src_chunks[i].data = reinterpret_cast(realloc(src_chunks[i].data, + src_chunks[i].len + bonus_size)); memcpy(src_chunks[i].data+src_chunks[i].len, bonus_data, bonus_size); src_chunks[i].len += bonus_size; } patch_data[i] = MakePatch(src_chunks+i, tgt_chunks+i, patch_size+i); } - printf("patch %3d is %d bytes (of %d)\n", + printf("patch %3d is %zu bytes (of %zu)\n", i, patch_size[i], tgt_chunks[i].source_len); } @@ -1012,7 +1020,7 @@ int main(int argc, char** argv) { switch (tgt_chunks[i].type) { case CHUNK_NORMAL: - printf("chunk %3d: normal (%10d, %10d) %10d\n", i, + printf("chunk %3d: normal (%10zu, %10zu) %10zu\n", i, tgt_chunks[i].start, tgt_chunks[i].len, patch_size[i]); Write8(tgt_chunks[i].source_start, f); Write8(tgt_chunks[i].source_len, f); @@ -1021,7 +1029,7 @@ int main(int argc, char** argv) { break; case CHUNK_DEFLATE: - printf("chunk %3d: deflate (%10d, %10d) %10d %s\n", i, + printf("chunk %3d: deflate (%10zu, %10zu) %10zu %s\n", i, tgt_chunks[i].start, tgt_chunks[i].deflate_len, patch_size[i], tgt_chunks[i].filename); Write8(tgt_chunks[i].source_start, f); @@ -1038,7 +1046,7 @@ int main(int argc, char** argv) { break; case CHUNK_RAW: - printf("chunk %3d: raw (%10d, %10d)\n", i, + printf("chunk %3d: raw (%10zu, %10zu)\n", i, tgt_chunks[i].start, tgt_chunks[i].len); Write4(patch_size[i], f); fwrite(patch_data[i], 1, patch_size[i], f); diff --git a/applypatch/imgpatch.c b/applypatch/imgpatch.cpp similarity index 97% rename from applypatch/imgpatch.c rename to applypatch/imgpatch.cpp index 09b0a739..26888f8e 100644 --- a/applypatch/imgpatch.c +++ b/applypatch/imgpatch.cpp @@ -132,7 +132,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size __unused, // must be appended from the bonus_data value. size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0; - unsigned char* expanded_source = malloc(expanded_len); + unsigned char* expanded_source = reinterpret_cast(malloc(expanded_len)); if (expanded_source == NULL) { printf("failed to allocate %zu bytes for expanded_source\n", expanded_len); @@ -196,7 +196,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size __unused, // ... unless the buffer is too small, in which case we'll // allocate a fresh one. free(temp_data); - temp_data = malloc(32768); + temp_data = reinterpret_cast(malloc(32768)); temp_size = 32768; } diff --git a/applypatch/main.c b/applypatch/main.cpp similarity index 87% rename from applypatch/main.c rename to applypatch/main.cpp index 8e9fe80e..63ff5c2c 100644 --- a/applypatch/main.c +++ b/applypatch/main.cpp @@ -23,14 +23,14 @@ #include "edify/expr.h" #include "mincrypt/sha.h" -int CheckMode(int argc, char** argv) { +static int CheckMode(int argc, char** argv) { if (argc < 3) { return 2; } return applypatch_check(argv[2], argc-3, argv+3); } -int SpaceMode(int argc, char** argv) { +static int SpaceMode(int argc, char** argv) { if (argc != 3) { return 2; } @@ -45,19 +45,18 @@ int SpaceMode(int argc, char** argv) { // Parse arguments (which should be of the form "" or // ":" into the new parallel arrays *sha1s and -// *patches (loading file contents into the patches). Returns 0 on +// *patches (loading file contents into the patches). Returns true on // success. -static int ParsePatchArgs(int argc, char** argv, +static bool ParsePatchArgs(int argc, char** argv, char*** sha1s, Value*** patches, int* num_patches) { *num_patches = argc; - *sha1s = malloc(*num_patches * sizeof(char*)); - *patches = malloc(*num_patches * sizeof(Value*)); + *sha1s = reinterpret_cast(malloc(*num_patches * sizeof(char*))); + *patches = reinterpret_cast(malloc(*num_patches * sizeof(Value*))); memset(*patches, 0, *num_patches * sizeof(Value*)); uint8_t digest[SHA_DIGEST_SIZE]; - int i; - for (i = 0; i < *num_patches; ++i) { + for (int i = 0; i < *num_patches; ++i) { char* colon = strchr(argv[i], ':'); if (colon != NULL) { *colon = '\0'; @@ -66,7 +65,7 @@ static int ParsePatchArgs(int argc, char** argv, if (ParseSha1(argv[i], digest) != 0) { printf("failed to parse sha1 \"%s\"\n", argv[i]); - return -1; + return false; } (*sha1s)[i] = argv[i]; @@ -77,17 +76,17 @@ static int ParsePatchArgs(int argc, char** argv, if (LoadFileContents(colon, &fc) != 0) { goto abort; } - (*patches)[i] = malloc(sizeof(Value)); + (*patches)[i] = reinterpret_cast(malloc(sizeof(Value))); (*patches)[i]->type = VAL_BLOB; (*patches)[i]->size = fc.size; - (*patches)[i]->data = (char*)fc.data; + (*patches)[i]->data = reinterpret_cast(fc.data); } } - return 0; + return true; abort: - for (i = 0; i < *num_patches; ++i) { + for (int i = 0; i < *num_patches; ++i) { Value* p = (*patches)[i]; if (p != NULL) { free(p->data); @@ -96,7 +95,7 @@ static int ParsePatchArgs(int argc, char** argv, } free(*sha1s); free(*patches); - return -1; + return false; } int PatchMode(int argc, char** argv) { @@ -107,7 +106,7 @@ int PatchMode(int argc, char** argv) { printf("failed to load bonus file %s\n", argv[2]); return 1; } - bonus = malloc(sizeof(Value)); + bonus = reinterpret_cast(malloc(sizeof(Value))); bonus->type = VAL_BLOB; bonus->size = fc.size; bonus->data = (char*)fc.data; @@ -129,7 +128,7 @@ int PatchMode(int argc, char** argv) { char** sha1s; Value** patches; int num_patches; - if (ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches) != 0) { + if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches)) { printf("failed to parse patch args\n"); return 1; } diff --git a/applypatch/utils.c b/applypatch/utils.cpp similarity index 91% rename from applypatch/utils.c rename to applypatch/utils.cpp index 41ff676d..4a80be75 100644 --- a/applypatch/utils.c +++ b/applypatch/utils.cpp @@ -39,13 +39,13 @@ void Write8(long long value, FILE* f) { } int Read2(void* pv) { - unsigned char* p = pv; + unsigned char* p = reinterpret_cast(pv); return (int)(((unsigned int)p[1] << 8) | (unsigned int)p[0]); } int Read4(void* pv) { - unsigned char* p = pv; + unsigned char* p = reinterpret_cast(pv); return (int)(((unsigned int)p[3] << 24) | ((unsigned int)p[2] << 16) | ((unsigned int)p[1] << 8) | @@ -53,7 +53,7 @@ int Read4(void* pv) { } long long Read8(void* pv) { - unsigned char* p = pv; + unsigned char* p = reinterpret_cast(pv); return (long long)(((unsigned long long)p[7] << 56) | ((unsigned long long)p[6] << 48) | ((unsigned long long)p[5] << 40) | diff --git a/minzip/Hash.h b/minzip/Hash.h index 8194537f..e83eac41 100644 --- a/minzip/Hash.h +++ b/minzip/Hash.h @@ -15,6 +15,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + /* compute the hash of an item with a specific type */ typedef unsigned int (*HashCompute)(const void* item); @@ -183,4 +187,8 @@ typedef unsigned int (*HashCalcFunc)(const void* item); void mzHashTableProbeCount(HashTable* pHashTable, HashCalcFunc calcFunc, HashCompareFunc cmpFunc); +#ifdef __cplusplus +} +#endif + #endif /*_MINZIP_HASH*/ diff --git a/roots.cpp b/roots.cpp index 2bd457ef..9288177e 100644 --- a/roots.cpp +++ b/roots.cpp @@ -30,10 +30,8 @@ #include "roots.h" #include "common.h" #include "make_ext4fs.h" -extern "C" { #include "wipe.h" #include "cryptfs.h" -} static struct fstab *fstab = NULL; diff --git a/updater/Android.mk b/updater/Android.mk index a0ea06fa..0d4179b2 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -1,11 +1,23 @@ # Copyright 2009 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. LOCAL_PATH := $(call my-dir) updater_src_files := \ - install.c \ - blockimg.c \ - updater.c + install.cpp \ + blockimg.cpp \ + updater.cpp # # Build a statically-linked binary to include in OTA packages diff --git a/updater/blockimg.c b/updater/blockimg.cpp similarity index 98% rename from updater/blockimg.c rename to updater/blockimg.cpp index e0be0391..310bbf94 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -45,10 +46,6 @@ // erase to mean fill the region with zeroes. #define DEBUG_ERASE 0 -#ifndef BLKDISCARD -#define BLKDISCARD _IO(0x12,119) -#endif - #define STASH_DIRECTORY_BASE "/cache/recovery" #define STASH_DIRECTORY_MODE 0700 #define STASH_FILE_MODE 0600 @@ -67,7 +64,7 @@ typedef struct { static RangeSet* parse_range(char* text) { char* save; char* token; - int i, num; + int num; long int val; RangeSet* out = NULL; size_t bufsize; @@ -93,7 +90,7 @@ static RangeSet* parse_range(char* text) { num = (int) val; bufsize = sizeof(RangeSet) + num * sizeof(int); - out = malloc(bufsize); + out = reinterpret_cast(malloc(bufsize)); if (!out) { fprintf(stderr, "failed to allocate range of %zu bytes\n", bufsize); @@ -103,7 +100,7 @@ static RangeSet* parse_range(char* text) { out->count = num / 2; out->size = 0; - for (i = 0; i < num; ++i) { + for (int i = 0; i < num; ++i) { token = strtok_r(NULL, ",", &save); if (!token) { @@ -478,7 +475,7 @@ static char* GetStashFileName(const char* base, const char* id, const char* post } len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1; - fn = malloc(len); + fn = reinterpret_cast(malloc(len)); if (fn == NULL) { fprintf(stderr, "failed to malloc %d bytes for fn\n", len); @@ -529,7 +526,7 @@ static void EnumerateStash(const char* dirname, StashCallback callback, void* da } len = strlen(dirname) + 1 + strlen(item->d_name) + 1; - fn = malloc(len); + fn = reinterpret_cast(malloc(len)); if (fn == NULL) { fprintf(stderr, "failed to malloc %d bytes for fn\n", len); @@ -649,7 +646,8 @@ static int LoadStash(const char* base, const char* id, int verify, int* blocks, fprintf(stderr, " loading %s\n", fn); if ((st.st_size % BLOCKSIZE) != 0) { - fprintf(stderr, "%s size %zd not multiple of block size %d", fn, st.st_size, BLOCKSIZE); + fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d", + fn, static_cast(st.st_size), BLOCKSIZE); goto lsout; } @@ -876,7 +874,6 @@ csout: 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 res = -1; int blocks = 0; if (!wordsave || !buffer || !buffer_alloc || !isunresumable) { @@ -972,7 +969,6 @@ static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks, char* word; char* colonsave; char* colon; - int id; int res; RangeSet* locs; size_t stashalloc = 0; @@ -1088,7 +1084,6 @@ static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* sr char* srchash = NULL; char* tgthash = NULL; int stash_exists = 0; - int overlap_blocks = 0; int rc = -1; uint8_t* tgtbuffer = NULL; @@ -1511,7 +1506,7 @@ static int PerformCommandErase(CommandParameters* params) { range = strtok_r(NULL, " ", ¶ms->cpos); if (range == NULL) { - fprintf(stderr, "missing target blocks for zero\n"); + fprintf(stderr, "missing target blocks for erase\n"); goto pceout; } @@ -1686,7 +1681,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, // 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 = malloc(transfer_list_value->size + 1); + transfer_list = reinterpret_cast(malloc(transfer_list_value->size + 1)); if (transfer_list == NULL) { fprintf(stderr, "failed to allocate %zd bytes for transfer list\n", @@ -1964,13 +1959,15 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { goto done; } - int fd = open(blockdev_filename->data, O_RDWR); + int fd; + fd = open(blockdev_filename->data, O_RDWR); if (fd < 0) { ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno)); goto done; } - RangeSet* rs = parse_range(ranges->data); + RangeSet* rs; + rs = parse_range(ranges->data); uint8_t buffer[BLOCKSIZE]; SHA_CTX ctx; diff --git a/updater/install.c b/updater/install.cpp similarity index 92% rename from updater/install.c rename to updater/install.cpp index 01a5dd24..a2bc4029 100644 --- a/updater/install.c +++ b/updater/install.cpp @@ -75,9 +75,9 @@ void uiPrintf(State* state, const char* format, ...) { // Take a sha-1 digest and return it as a newly-allocated hex string. char* PrintSha1(const uint8_t* digest) { - char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1); - int i; + char* buffer = reinterpret_cast(malloc(SHA_DIGEST_SIZE*2 + 1)); const char* alphabet = "0123456789abcdef"; + size_t i; for (i = 0; i < SHA_DIGEST_SIZE; ++i) { buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf]; buffer[i*2+1] = alphabet[digest[i] & 0xf]; @@ -133,18 +133,20 @@ Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { goto done; } - char *secontext = NULL; + { + char *secontext = NULL; - if (sehandle) { - selabel_lookup(sehandle, &secontext, mount_point, 0755); - setfscreatecon(secontext); - } + if (sehandle) { + selabel_lookup(sehandle, &secontext, mount_point, 0755); + setfscreatecon(secontext); + } - mkdir(mount_point, 0755); + mkdir(mount_point, 0755); - if (secontext) { - freecon(secontext); - setfscreatecon(NULL); + if (secontext) { + freecon(secontext); + setfscreatecon(NULL); + } } if (strcmp(partition_type, "MTD") == 0) { @@ -202,11 +204,13 @@ Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) { } scan_mounted_volumes(); - const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); - if (vol == NULL) { - result = strdup(""); - } else { - result = mount_point; + { + const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); + if (vol == NULL) { + result = strdup(""); + } else { + result = mount_point; + } } done: @@ -230,17 +234,19 @@ Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) { } scan_mounted_volumes(); - const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); - if (vol == NULL) { - uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point); - result = strdup(""); - } else { - int ret = unmount_mounted_volume(vol); - if (ret != 0) { - uiPrintf(state, "unmount of %s failed (%d): %s\n", - mount_point, ret, strerror(errno)); + { + const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); + if (vol == NULL) { + uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point); + result = strdup(""); + } else { + int ret = unmount_mounted_volume(vol); + if (ret != 0) { + uiPrintf(state, "unmount of %s failed (%d): %s\n", + mount_point, ret, strerror(errno)); + } + result = mount_point; } - result = mount_point; } done: @@ -413,9 +419,8 @@ done: } Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) { - char** paths = malloc(argc * sizeof(char*)); - int i; - for (i = 0; i < argc; ++i) { + char** paths = reinterpret_cast(malloc(argc * sizeof(char*))); + for (int i = 0; i < argc; ++i) { paths[i] = Evaluate(state, argv[i]); if (paths[i] == NULL) { int j; @@ -430,7 +435,7 @@ Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) { bool recursive = (strcmp(name, "delete_recursive") == 0); int success = 0; - for (i = 0; i < argc; ++i) { + for (int i = 0; i < argc; ++i) { if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0) ++success; free(paths[i]); @@ -517,8 +522,6 @@ Value* PackageExtractFileFn(const char* name, State* state, } bool success = false; - UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); - if (argc == 2) { // The two-argument version extracts to a file. @@ -534,14 +537,16 @@ Value* PackageExtractFileFn(const char* name, State* state, goto done2; } - FILE* f = fopen(dest_path, "wb"); - if (f == NULL) { - printf("%s: can't open %s for write: %s\n", - name, dest_path, strerror(errno)); - goto done2; + { + FILE* f = fopen(dest_path, "wb"); + if (f == NULL) { + printf("%s: can't open %s for write: %s\n", + name, dest_path, strerror(errno)); + goto done2; + } + success = mzExtractZipEntryToFile(za, entry, fileno(f)); + fclose(f); } - success = mzExtractZipEntryToFile(za, entry, fileno(f)); - fclose(f); done2: free(zip_path); @@ -552,7 +557,7 @@ Value* PackageExtractFileFn(const char* name, State* state, // as the result. char* zip_path; - Value* v = malloc(sizeof(Value)); + Value* v = reinterpret_cast(malloc(sizeof(Value))); v->type = VAL_BLOB; v->size = -1; v->data = NULL; @@ -567,7 +572,7 @@ Value* PackageExtractFileFn(const char* name, State* state, } v->size = mzGetZipEntryUncompLen(entry); - v->data = malloc(v->size); + v->data = reinterpret_cast(malloc(v->size)); if (v->data == NULL) { printf("%s: failed to allocate %ld bytes for %s\n", name, (long)v->size, zip_path); @@ -866,17 +871,14 @@ static int do_SetMetadataRecursive(const char* filename, const struct stat *stat } static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) { - int i; int bad = 0; - static int nwarnings = 0; struct stat sb; Value* result = NULL; bool recursive = (strcmp(name, "set_metadata_recursive") == 0); if ((argc % 2) != 1) { - return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", - name, argc); + return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc); } char** args = ReadVarArgs(state, argc, argv); @@ -887,20 +889,22 @@ static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv goto done; } - struct perm_parsed_args parsed = ParsePermArgs(state, argc, args); + { + struct perm_parsed_args parsed = ParsePermArgs(state, argc, args); - if (recursive) { - recursive_parsed_args = parsed; - recursive_state = state; - bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS); - memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args)); - recursive_state = NULL; - } else { - bad += ApplyParsedPerms(state, args[0], &sb, parsed); + if (recursive) { + recursive_parsed_args = parsed; + recursive_state = state; + bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS); + memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args)); + recursive_state = NULL; + } else { + bad += ApplyParsedPerms(state, args[0], &sb, parsed); + } } done: - for (i = 0; i < argc; ++i) { + for (int i = 0; i < argc; ++i) { free(args[i]); } free(args); @@ -920,8 +924,7 @@ Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { if (argc != 1) { return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); } - char* key; - key = Evaluate(state, argv[0]); + char* key = Evaluate(state, argv[0]); if (key == NULL) return NULL; char value[PROPERTY_VALUE_MAX]; @@ -948,29 +951,27 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { struct stat st; if (stat(filename, &st) < 0) { - ErrorAbort(state, "%s: failed to stat \"%s\": %s", - name, filename, strerror(errno)); + ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno)); goto done; } #define MAX_FILE_GETPROP_SIZE 65536 if (st.st_size > MAX_FILE_GETPROP_SIZE) { - ErrorAbort(state, "%s too large for %s (max %d)", - filename, name, MAX_FILE_GETPROP_SIZE); + ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE); goto done; } - buffer = malloc(st.st_size+1); + buffer = reinterpret_cast(malloc(st.st_size+1)); if (buffer == NULL) { ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1); goto done; } - FILE* f = fopen(filename, "rb"); + FILE* f; + f = fopen(filename, "rb"); if (f == NULL) { - ErrorAbort(state, "%s: failed to open %s: %s", - name, filename, strerror(errno)); + ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno)); goto done; } @@ -984,7 +985,8 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { fclose(f); - char* line = strtok(buffer, "\n"); + char* line; + line = strtok(buffer, "\n"); do { // skip whitespace at start of line while (*line && isspace(*line)) ++line; @@ -1028,15 +1030,6 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { return StringValue(result); } - -static bool write_raw_image_cb(const unsigned char* data, - int data_len, void* ctx) { - int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len); - if (r == data_len) return true; - printf("%s\n", strerror(errno)); - return false; -} - // write_raw_image(filename_or_blob, partition) Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { char* result = NULL; @@ -1063,14 +1056,16 @@ Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { } mtd_scan_partitions(); - const MtdPartition* mtd = mtd_find_partition_by_name(partition); + const MtdPartition* mtd; + mtd = mtd_find_partition_by_name(partition); if (mtd == NULL) { printf("%s: no mtd partition named \"%s\"\n", name, partition); result = strdup(""); goto done; } - MtdWriteContext* ctx = mtd_write_partition(mtd); + MtdWriteContext* ctx; + ctx = mtd_write_partition(mtd); if (ctx == NULL) { printf("%s: can't write mtd partition \"%s\"\n", name, partition); @@ -1085,14 +1080,13 @@ Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { char* filename = contents->data; FILE* f = fopen(filename, "rb"); if (f == NULL) { - printf("%s: can't open %s: %s\n", - name, filename, strerror(errno)); + printf("%s: can't open %s: %s\n", name, filename, strerror(errno)); result = strdup(""); goto done; } success = true; - char* buffer = malloc(BUFSIZ); + char* buffer = reinterpret_cast(malloc(BUFSIZ)); int read; while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) { int wrote = mtd_write_data(ctx, buffer, read); @@ -1139,8 +1133,7 @@ Value* ApplyPatchSpaceFn(const char* name, State* state, char* endptr; size_t bytes = strtol(bytes_str, &endptr, 10); if (bytes == 0 && endptr == bytes_str) { - ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", - name, bytes_str); + ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str); free(bytes_str); return NULL; } @@ -1200,7 +1193,7 @@ Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { return NULL; } - char** patch_sha_str = malloc(patchcount * sizeof(char*)); + char** patch_sha_str = reinterpret_cast(malloc(patchcount * sizeof(char*))); for (i = 0; i < patchcount; ++i) { patch_sha_str[i] = patches[i*2]->data; patches[i*2]->data = NULL; @@ -1259,7 +1252,7 @@ Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { for (i = 0; i < argc; ++i) { size += strlen(args[i]); } - char* buffer = malloc(size+1); + char* buffer = reinterpret_cast(malloc(size+1)); size = 0; for (i = 0; i < argc; ++i) { strcpy(buffer+size, args[i]); @@ -1289,7 +1282,7 @@ Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { return NULL; } - char** args2 = malloc(sizeof(char*) * (argc+1)); + char** args2 = reinterpret_cast(malloc(sizeof(char*) * (argc+1))); memcpy(args2, args, sizeof(char*) * argc); args2[argc] = NULL; @@ -1356,7 +1349,7 @@ Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { } int i; - uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE); + uint8_t* arg_digest = reinterpret_cast(malloc(SHA_DIGEST_SIZE)); for (i = 1; i < argc; ++i) { if (args[i]->type != VAL_STRING) { printf("%s(): arg %d is not a string; skipping", @@ -1392,7 +1385,7 @@ Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { char* filename; if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; - Value* v = malloc(sizeof(Value)); + Value* v = reinterpret_cast(malloc(sizeof(Value))); v->type = VAL_BLOB; FileContents fc; @@ -1550,15 +1543,14 @@ Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) { return ErrorAbort(state, "%s() could not read args", name); } - int i; - char** args2 = malloc(sizeof(char*) * (argc+1)); + char** args2 = reinterpret_cast(malloc(sizeof(char*) * (argc+1))); // Tune2fs expects the program name as its args[0] args2[0] = strdup(name); - for (i = 0; i < argc; ++i) { + for (int i = 0; i < argc; ++i) { args2[i + 1] = args[i]; } int result = tune2fs_main(argc + 1, args2); - for (i = 0; i < argc; ++i) { + for (int i = 0; i < argc; ++i) { free(args[i]); } free(args); diff --git a/updater/updater.c b/updater/updater.cpp similarity index 98% rename from updater/updater.c rename to updater/updater.cpp index 661f6958..0f22e6d0 100644 --- a/updater/updater.c +++ b/updater/updater.cpp @@ -89,7 +89,7 @@ int main(int argc, char** argv) { return 4; } - char* script = malloc(script_entry->uncompLen+1); + char* script = reinterpret_cast(malloc(script_entry->uncompLen+1)); if (!mzReadZipEntry(&za, script_entry, script, script_entry->uncompLen)) { printf("failed to read script from package\n"); return 5; From 27604fcbee0010b800bfc16b5bf7a48c365c2cf3 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 17 Jul 2015 11:47:44 -0700 Subject: [PATCH 05/13] applypatch: Refactor strtok(). We have android::base::Split() for the work. Change-Id: Ic529db42090f700e6455d465c8b84b7f52d34d63 (cherry picked from commit 0a47ce27de454e272a883a0c452fad627fd7f419) --- applypatch/Android.mk | 6 +- applypatch/applypatch.cpp | 136 +++++++++++++++----------------------- 2 files changed, 56 insertions(+), 86 deletions(-) diff --git a/applypatch/Android.mk b/applypatch/Android.mk index 1f73fd89..cc17a13a 100644 --- a/applypatch/Android.mk +++ b/applypatch/Android.mk @@ -21,7 +21,7 @@ LOCAL_SRC_FILES := applypatch.cpp bspatch.cpp freecache.cpp imgpatch.cpp utils.c LOCAL_MODULE := libapplypatch LOCAL_MODULE_TAGS := eng LOCAL_C_INCLUDES += external/bzip2 external/zlib bootable/recovery -LOCAL_STATIC_LIBRARIES += libmtdutils libmincrypt libbz libz +LOCAL_STATIC_LIBRARIES += libbase libmtdutils libmincrypt libbz libz include $(BUILD_STATIC_LIBRARY) @@ -31,7 +31,7 @@ LOCAL_CLANG := true LOCAL_SRC_FILES := main.cpp LOCAL_MODULE := applypatch LOCAL_C_INCLUDES += bootable/recovery -LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz +LOCAL_STATIC_LIBRARIES += libapplypatch libbase libmtdutils libmincrypt libbz LOCAL_SHARED_LIBRARIES += libz libcutils libc include $(BUILD_EXECUTABLE) @@ -44,7 +44,7 @@ LOCAL_MODULE := applypatch_static LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_MODULE_TAGS := eng LOCAL_C_INCLUDES += bootable/recovery -LOCAL_STATIC_LIBRARIES += libapplypatch libmtdutils libmincrypt libbz +LOCAL_STATIC_LIBRARIES += libapplypatch libbase libmtdutils libmincrypt libbz LOCAL_STATIC_LIBRARIES += libz libcutils libc include $(BUILD_EXECUTABLE) diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 96bd88e8..02686333 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -25,6 +25,8 @@ #include #include +#include + #include "mincrypt/sha.h" #include "applypatch.h" #include "mtdutils/mtdutils.h" @@ -42,7 +44,7 @@ static int GenerateTarget(FileContents* source_file, size_t target_size, const Value* bonus_data); -static int mtd_partitions_scanned = 0; +static bool mtd_partitions_scanned = false; // Read a file into memory; store the file contents and associated // metadata in *file. @@ -87,21 +89,6 @@ int LoadFileContents(const char* filename, FileContents* file) { return 0; } -static size_t* size_array; -// comparison function for qsort()ing an int array of indexes into -// size_array[]. -static int compare_size_indices(const void* a, const void* b) { - const int aa = *reinterpret_cast(a); - const int bb = *reinterpret_cast(b); - if (size_array[aa] < size_array[bb]) { - return -1; - } else if (size_array[aa] > size_array[bb]) { - return 1; - } else { - return 0; - } -} - // Load the contents of an MTD or EMMC partition into the provided // FileContents. filename should be a string of the form // "MTD::::::..." (or @@ -120,53 +107,45 @@ static int compare_size_indices(const void* a, const void* b) { enum PartitionType { MTD, EMMC }; static int LoadPartitionContents(const char* filename, FileContents* file) { - char* copy = strdup(filename); - const char* magic = strtok(copy, ":"); + std::string copy(filename); + std::vector pieces = android::base::Split(copy, ":"); + if (pieces.size() < 4 || pieces.size() % 2 != 0) { + printf("LoadPartitionContents called with bad filename (%s)\n", filename); + return -1; + } enum PartitionType type; - - if (strcmp(magic, "MTD") == 0) { + if (pieces[0] == "MTD") { type = MTD; - } else if (strcmp(magic, "EMMC") == 0) { + } else if (pieces[0] == "EMMC") { type = EMMC; } else { printf("LoadPartitionContents called with bad filename (%s)\n", filename); return -1; } - const char* partition = strtok(NULL, ":"); + const char* partition = pieces[1].c_str(); - int i; - int colons = 0; - for (i = 0; filename[i] != '\0'; ++i) { - if (filename[i] == ':') { - ++colons; - } - } - if (colons < 3 || colons%2 == 0) { - printf("LoadPartitionContents called with bad filename (%s)\n", - filename); - } + size_t pairs = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename + std::vector index(pairs); + std::vector size(pairs); + std::vector sha1sum(pairs); - int pairs = (colons-1)/2; // # of (size,sha1) pairs in filename - int* index = reinterpret_cast(malloc(pairs * sizeof(int))); - size_t* size = reinterpret_cast(malloc(pairs * sizeof(size_t))); - char** sha1sum = reinterpret_cast(malloc(pairs * sizeof(char*))); - - for (i = 0; i < pairs; ++i) { - const char* size_str = strtok(NULL, ":"); - size[i] = strtol(size_str, NULL, 10); + for (size_t i = 0; i < pairs; ++i) { + size[i] = strtol(pieces[i*2+2].c_str(), NULL, 10); if (size[i] == 0) { printf("LoadPartitionContents called with bad size (%s)\n", filename); return -1; } - sha1sum[i] = strtok(NULL, ":"); + sha1sum[i] = pieces[i*2+3].c_str(); index[i] = i; } - // sort the index[] array so it indexes the pairs in order of - // increasing size. - size_array = size; - qsort(index, pairs, sizeof(int), compare_size_indices); + // Sort the index[] array so it indexes the pairs in order of increasing size. + sort(index.begin(), index.end(), + [&](const size_t& i, const size_t& j) { + return (size[i] < size[j]); + } + ); MtdReadContext* ctx = NULL; FILE* dev = NULL; @@ -175,20 +154,18 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { case MTD: { if (!mtd_partitions_scanned) { mtd_scan_partitions(); - mtd_partitions_scanned = 1; + mtd_partitions_scanned = true; } const MtdPartition* mtd = mtd_find_partition_by_name(partition); if (mtd == NULL) { - printf("mtd partition \"%s\" not found (loading %s)\n", - partition, filename); + printf("mtd partition \"%s\" not found (loading %s)\n", partition, filename); return -1; } ctx = mtd_read_partition(mtd); if (ctx == NULL) { - printf("failed to initialize read of mtd partition \"%s\"\n", - partition); + printf("failed to initialize read of mtd partition \"%s\"\n", partition); return -1; } break; @@ -197,8 +174,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { case EMMC: dev = fopen(partition, "rb"); if (dev == NULL) { - printf("failed to open emmc partition \"%s\": %s\n", - partition, strerror(errno)); + printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno)); return -1; } } @@ -207,15 +183,15 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { SHA_init(&sha_ctx); uint8_t parsed_sha[SHA_DIGEST_SIZE]; - // allocate enough memory to hold the largest size. + // Allocate enough memory to hold the largest size. file->data = reinterpret_cast(malloc(size[index[pairs-1]])); char* p = (char*)file->data; file->size = 0; // # bytes read so far + bool found = false; - for (i = 0; i < pairs; ++i) { - // Read enough additional bytes to get us up to the next size - // (again, we're trying the possibilities in order of increasing - // size). + for (size_t i = 0; i < pairs; ++i) { + // Read enough additional bytes to get us up to the next size. (Again, + // we're trying the possibilities in order of increasing size). size_t next = size[index[i]] - file->size; size_t read = 0; if (next > 0) { @@ -245,8 +221,8 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX)); const uint8_t* sha_so_far = SHA_final(&temp_ctx); - if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) { - printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]], filename); + if (ParseSha1(sha1sum[index[i]].c_str(), parsed_sha) != 0) { + printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]].c_str(), filename); free(file->data); file->data = NULL; return -1; @@ -256,7 +232,8 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { // we have a match. stop reading the partition; we'll return // the data we've read so far. printf("partition read matched size %zu sha %s\n", - size[index[i]], sha1sum[index[i]]); + size[index[i]], sha1sum[index[i]].c_str()); + found = true; break; } @@ -274,9 +251,8 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { } - if (i == pairs) { - // Ran off the end of the list of (size,sha1) pairs without - // finding a match. + if (!found) { + // Ran off the end of the list of (size,sha1) pairs without finding a match. printf("contents of partition \"%s\" didn't match %s\n", partition, filename); free(file->data); file->data = NULL; @@ -293,11 +269,6 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { file->st.st_uid = 0; file->st.st_gid = 0; - free(copy); - free(index); - free(size); - free(sha1sum); - return 0; } @@ -340,33 +311,33 @@ int SaveFileContents(const char* filename, const FileContents* file) { } // Write a memory buffer to 'target' partition, a string of the form -// "MTD:[:...]" or "EMMC::". Return 0 on +// "MTD:[:...]" or "EMMC:". Return 0 on // success. int WriteToPartition(unsigned char* data, size_t len, const char* target) { - char* copy = strdup(target); - const char* magic = strtok(copy, ":"); + std::string copy(target); + std::vector pieces = android::base::Split(copy, ":"); + + if (pieces.size() != 2) { + printf("WriteToPartition called with bad target (%s)\n", target); + return -1; + } enum PartitionType type; - if (strcmp(magic, "MTD") == 0) { + if (pieces[0] == "MTD") { type = MTD; - } else if (strcmp(magic, "EMMC") == 0) { + } else if (pieces[0] == "EMMC") { type = EMMC; } else { printf("WriteToPartition called with bad target (%s)\n", target); return -1; } - const char* partition = strtok(NULL, ":"); - - if (partition == NULL) { - printf("bad partition target name \"%s\"\n", target); - return -1; - } + const char* partition = pieces[1].c_str(); switch (type) { case MTD: { if (!mtd_partitions_scanned) { mtd_scan_partitions(); - mtd_partitions_scanned = 1; + mtd_partitions_scanned = true; } const MtdPartition* mtd = mtd_find_partition_by_name(partition); @@ -410,7 +381,7 @@ int WriteToPartition(unsigned char* data, size_t len, const char* target) { return -1; } - for (int attempt = 0; attempt < 2; ++attempt) { + for (size_t attempt = 0; attempt < 2; ++attempt) { if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) { printf("failed seek on %s: %s\n", partition, strerror(errno)); return -1; @@ -510,7 +481,6 @@ int WriteToPartition(unsigned char* data, size_t len, const char* target) { } } - free(copy); return 0; } From 3b199267d6d4c279cbef3286a74e16bf92dd8d8a Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 17 Jul 2015 13:39:52 -0700 Subject: [PATCH 06/13] updater: libapplypatch needs libbase now. Change-Id: Ibe3173edd6274b61bd9ca5ec394d7f6b4a403639 (cherry picked from commit 1b1ea17d554d127a970afe1d6004dd4627cd596e) --- updater/Android.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updater/Android.mk b/updater/Android.mk index 0d4179b2..82fa7e26 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -44,7 +44,7 @@ LOCAL_STATIC_LIBRARIES += \ endif LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UPDATER_LIBS) $(TARGET_RECOVERY_UPDATER_EXTRA_LIBS) -LOCAL_STATIC_LIBRARIES += libapplypatch libedify libmtdutils libminzip libz +LOCAL_STATIC_LIBRARIES += libapplypatch libbase libedify libmtdutils libminzip libz LOCAL_STATIC_LIBRARIES += libmincrypt libbz LOCAL_STATIC_LIBRARIES += libcutils liblog libc LOCAL_STATIC_LIBRARIES += libselinux From f47259b18492c921445d9e437a9f9ed11c52fac6 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 17 Jul 2015 18:11:12 -0700 Subject: [PATCH 07/13] applypatch: Support flash mode. We may carry a full copy of recovery image in the /system, and use /system/bin/install-recovery.sh to install the recovery. This CL adds support to flash the recovery partition with the given image. Bug: 22641135 Change-Id: I345eaaee269f6443527f45a9be7e4ee47f6b2b39 (cherry picked from commit 68c5a6796737bb583a8bdfa4c9cd9c7f12ef4276) --- applypatch/applypatch.cpp | 84 +++++++++++++++++++++++++++++++++------ applypatch/applypatch.h | 2 + applypatch/main.cpp | 27 +++++++++++-- 3 files changed, 97 insertions(+), 16 deletions(-) diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 02686333..2446b2a6 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -43,6 +43,7 @@ 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; @@ -461,7 +462,7 @@ int WriteToPartition(unsigned char* data, size_t len, const char* target) { } if (start == len) { - printf("verification read succeeded (attempt %d)\n", attempt+1); + printf("verification read succeeded (attempt %zu)\n", attempt+1); success = true; break; } @@ -628,12 +629,14 @@ int CacheSizeCheck(size_t bytes) { } } -static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { +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) { - putchar(hex[(sha1[i]>>4) & 0xf]); - putchar(hex[sha1[i] & 0xf]); + 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 @@ -648,7 +651,7 @@ static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { // entries in , the corresponding patch from // (which must be a VAL_BLOB) is applied to produce a // new file (the type of patch is automatically detected from the -// blob daat). If that new file has sha1 hash , +// blob data). If that new file has sha1 hash , // moves it to replace , and exits successfully. // Note that if and are not the // same, is NOT deleted on success. @@ -659,7 +662,7 @@ static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) { // status. // // may refer to a partition to read the source data. -// See the comments for the LoadPartition Contents() function above +// See the comments for the LoadPartitionContents() function above // for the format of such a filename. int applypatch(const char* source_filename, @@ -694,9 +697,7 @@ int applypatch(const char* source_filename, if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { // The early-exit case: the patch was already applied, this file // has the desired hash, nothing for us to do. - printf("already "); - print_short_sha1(target_sha1); - putchar('\n'); + printf("already %s\n", short_sha1(target_sha1).c_str()); free(source_file.data); return 0; } @@ -753,6 +754,67 @@ int applypatch(const char* source_filename, return result; } +/* + * This function flashes a given image to the target partition. It verifies + * the target cheksum first, and will return if target has the desired hash. + * It checks the checksum of the given source image before flashing, and + * verifies the target partition afterwards. The function is idempotent. + * Returns zero on success. + */ +int applypatch_flash(const char* source_filename, const char* target_filename, + const char* target_sha1_str, size_t target_size) { + printf("flash %s: ", target_filename); + + uint8_t target_sha1[SHA_DIGEST_SIZE]; + if (ParseSha1(target_sha1_str, target_sha1) != 0) { + printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); + return 1; + } + + FileContents source_file; + source_file.data = NULL; + std::string target_str(target_filename); + + std::vector pieces = android::base::Split(target_str, ":"); + if (pieces.size() != 2 || (pieces[0] != "MTD" && pieces[0] != "EMMC")) { + printf("invalid target name \"%s\"", target_filename); + return 1; + } + + // Load the target into the source_file object to see if already applied. + pieces.push_back(std::to_string(target_size)); + pieces.push_back(target_sha1_str); + std::string fullname = android::base::Join(pieces, ':'); + if (LoadPartitionContents(fullname.c_str(), &source_file) == 0 && + memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + // The early-exit case: the image was already applied, this partition + // has the desired hash, nothing for us to do. + printf("already %s\n", short_sha1(target_sha1).c_str()); + free(source_file.data); + return 0; + } + + if (LoadFileContents(source_filename, &source_file) == 0) { + if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { + // The source doesn't have desired checksum. + printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename); + printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(), + short_sha1(source_file.sha1).c_str()); + free(source_file.data); + return 1; + } + } + + if (WriteToPartition(source_file.data, target_size, target_filename) != 0) { + printf("write of copied data to %s failed\n", target_filename); + free(source_file.data); + return 1; + } + + free(source_file.data); + return 0; +} + static int GenerateTarget(FileContents* source_file, const Value* source_patch_value, FileContents* copy_file, @@ -953,9 +1015,7 @@ static int GenerateTarget(FileContents* source_file, printf("patch did not produce expected sha1\n"); return 1; } else { - printf("now "); - print_short_sha1(target_sha1); - putchar('\n'); + printf("now %s\n", short_sha1(target_sha1).c_str()); } if (output < 0) { diff --git a/applypatch/applypatch.h b/applypatch/applypatch.h index edec8481..415bc1b3 100644 --- a/applypatch/applypatch.h +++ b/applypatch/applypatch.h @@ -48,6 +48,8 @@ size_t FreeSpaceForFile(const char* filename); int CacheSizeCheck(size_t bytes); int ParseSha1(const char* str, uint8_t* digest); +int applypatch_flash(const char* source_filename, const char* target_filename, + const char* target_sha1_str, size_t target_size); int applypatch(const char* source_filename, const char* target_filename, const char* target_sha1_str, diff --git a/applypatch/main.cpp b/applypatch/main.cpp index 63ff5c2c..966d8b91 100644 --- a/applypatch/main.cpp +++ b/applypatch/main.cpp @@ -47,8 +47,8 @@ static int SpaceMode(int argc, char** argv) { // ":" into the new parallel arrays *sha1s and // *patches (loading file contents into the patches). Returns true on // success. -static bool ParsePatchArgs(int argc, char** argv, - char*** sha1s, Value*** patches, int* num_patches) { +static bool ParsePatchArgs(int argc, char** argv, char*** sha1s, + Value*** patches, int* num_patches) { *num_patches = argc; *sha1s = reinterpret_cast(malloc(*num_patches * sizeof(char*))); *patches = reinterpret_cast(malloc(*num_patches * sizeof(Value*))); @@ -98,7 +98,12 @@ static bool ParsePatchArgs(int argc, char** argv, return false; } -int PatchMode(int argc, char** argv) { +static int FlashMode(const char* src_filename, const char* tgt_filename, + const char* tgt_sha1, size_t tgt_size) { + return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size); +} + +static int PatchMode(int argc, char** argv) { Value* bonus = NULL; if (argc >= 3 && strcmp(argv[1], "-b") == 0) { FileContents fc; @@ -114,7 +119,7 @@ int PatchMode(int argc, char** argv) { argv += 2; } - if (argc < 6) { + if (argc < 4) { return 2; } @@ -125,6 +130,16 @@ int PatchMode(int argc, char** argv) { return 1; } + // If no : is provided, it is in flash mode. + if (argc == 5) { + if (bonus != NULL) { + printf("bonus file not supported in flash mode\n"); + return 1; + } + return FlashMode(argv[1], argv[2], argv[3], target_size); + } + + char** sha1s; Value** patches; int num_patches; @@ -162,6 +177,10 @@ int PatchMode(int argc, char** argv) { // - if the sha1 hash of is , does nothing and exits // successfully. // +// - otherwise, if no : is provided, flashes with +// . must be a partition name, while must +// be a regular image file. will not be deleted on success. +// // - otherwise, if the sha1 hash of is , applies the // bsdiff to to produce a new file (the type of patch // is automatically detected from the file header). If that new From 32ac97675bade3681203c46d001f76b11a359fd5 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 24 Jul 2015 15:29:12 -0700 Subject: [PATCH 08/13] applypatch: Fix the checking in WriteToPartition(). WriteToPartition() should consider a target name as valid if it contains multiple colons. But only the first two fields will be used. Bug: 22725128 Change-Id: I9d0236eaf97df9db9704acf53690d0ef85188e45 (cherry picked from commit 1ce7a2a63db84527e6195a6b123b1617f87c0f38) --- applypatch/applypatch.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 2446b2a6..751d3e39 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -312,13 +312,14 @@ int SaveFileContents(const char* filename, const FileContents* file) { } // Write a memory buffer to 'target' partition, a string of the form -// "MTD:[:...]" or "EMMC:". Return 0 on -// success. +// "MTD:[:...]" or "EMMC:[:...]". The target name +// might contain multiple colons, but WriteToPartition() only uses the first +// two and ignores the rest. Return 0 on success. int WriteToPartition(unsigned char* data, size_t len, const char* target) { std::string copy(target); std::vector pieces = android::base::Split(copy, ":"); - if (pieces.size() != 2) { + if (pieces.size() < 2) { printf("WriteToPartition called with bad target (%s)\n", target); return -1; } From 8f90389966fabf532b24741d49245215279533e1 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 30 Jul 2015 14:43:27 -0700 Subject: [PATCH 09/13] recovery: Allow "Mount /system" for system_root_image. When system images contain the root directory, there is no entry of "/system" in the fstab. Change it to look for "/" instead if ro.build.system_root_image is true. We actually mount the partition to /system_root instead, and create a symlink to /system_root/system for /system. This allows "adb shell" to work properly. Bug: 22855115 Change-Id: I91864444950dc3229fda3cc133ddbadeb8817fb8 (cherry picked from commit abb8f7785ee24eac42f6d28dbfef37872a06c7e9) --- recovery.cpp | 19 +++++++++++++++++-- roots.cpp | 26 ++++++++++++++++++-------- roots.h | 13 ++++--------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index a0c74524..09b061d7 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -862,9 +862,24 @@ prompt_and_wait(Device* device, int status) { break; case Device::MOUNT_SYSTEM: - if (ensure_path_mounted("/system") != -1) { - ui->Print("Mounted /system.\n"); + char system_root_image[PROPERTY_VALUE_MAX]; + property_get("ro.build.system_root_image", system_root_image, ""); + + // For a system image built with the root directory (i.e. + // system_root_image == "true"), we mount it to /system_root, and symlink /system + // to /system_root/system to make adb shell work (the symlink is created through + // the build system). + // Bug: 22855115 + if (strcmp(system_root_image, "true") == 0) { + if (ensure_path_mounted_at("/", "/system_root") != -1) { + ui->Print("Mounted /system.\n"); + } + } else { + if (ensure_path_mounted("/system") != -1) { + ui->Print("Mounted /system.\n"); + } } + break; } } diff --git a/roots.cpp b/roots.cpp index 9288177e..12c6b5ee 100644 --- a/roots.cpp +++ b/roots.cpp @@ -70,7 +70,8 @@ Volume* volume_for_path(const char* path) { return fs_mgr_get_entry_for_mount_point(fstab, path); } -int ensure_path_mounted(const char* path) { +// Mount the volume specified by path at the given mount_point. +int ensure_path_mounted_at(const char* path, const char* mount_point) { Volume* v = volume_for_path(path); if (v == NULL) { LOGE("unknown volume for path [%s]\n", path); @@ -88,14 +89,18 @@ int ensure_path_mounted(const char* path) { return -1; } + if (!mount_point) { + mount_point = v->mount_point; + } + const MountedVolume* mv = - find_mounted_volume_by_mount_point(v->mount_point); + find_mounted_volume_by_mount_point(mount_point); if (mv) { // volume is already mounted return 0; } - mkdir(v->mount_point, 0755); // in case it doesn't already exist + mkdir(mount_point, 0755); // in case it doesn't already exist if (strcmp(v->fs_type, "yaffs2") == 0) { // mount an MTD partition as a YAFFS2 filesystem. @@ -104,25 +109,30 @@ int ensure_path_mounted(const char* path) { partition = mtd_find_partition_by_name(v->blk_device); if (partition == NULL) { LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", - v->blk_device, v->mount_point); + v->blk_device, mount_point); return -1; } - return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0); + return mtd_mount_partition(partition, mount_point, v->fs_type, 0); } else if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 || strcmp(v->fs_type, "vfat") == 0) { - result = mount(v->blk_device, v->mount_point, v->fs_type, + result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options); if (result == 0) return 0; - LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno)); + LOGE("failed to mount %s (%s)\n", mount_point, strerror(errno)); return -1; } - LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point); + LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, mount_point); return -1; } +int ensure_path_mounted(const char* path) { + // Mount at the default mount point. + return ensure_path_mounted_at(path, nullptr); +} + int ensure_path_unmounted(const char* path) { Volume* v = volume_for_path(path); if (v == NULL) { diff --git a/roots.h b/roots.h index 230d9ded..6e3b2435 100644 --- a/roots.h +++ b/roots.h @@ -19,10 +19,6 @@ #include "common.h" -#ifdef __cplusplus -extern "C" { -#endif - // Load and parse volume data from /etc/recovery.fstab. void load_volume_table(); @@ -33,7 +29,10 @@ Volume* volume_for_path(const char* path); // success (volume is mounted). int ensure_path_mounted(const char* path); -// Make sure that the volume 'path' is on is mounted. Returns 0 on +// Similar to ensure_path_mounted, but allows one to specify the mount_point. +int ensure_path_mounted_at(const char* path, const char* mount_point); + +// Make sure that the volume 'path' is on is unmounted. Returns 0 on // success (volume is unmounted); int ensure_path_unmounted(const char* path); @@ -46,8 +45,4 @@ int format_volume(const char* volume); // mounted (/tmp and /cache) are mounted. Returns 0 on success. int setup_install_mounts(); -#ifdef __cplusplus -} -#endif - #endif // RECOVERY_ROOTS_H_ From 846c094fee9e50ed2b2e63dee17f5bafb2b9d1ce Mon Sep 17 00:00:00 2001 From: David Zeuthen Date: Wed, 2 Sep 2015 15:49:58 -0400 Subject: [PATCH 10/13] Add slot_suffix field to struct bootloader_message. This is needed by fs_mgr for certain A/B implementations. Change-Id: I7bb404d61198eb7a962c2b693911f5156745daae --- bootloader.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bootloader.h b/bootloader.h index c2895dd9..fd003a07 100644 --- a/bootloader.h +++ b/bootloader.h @@ -43,6 +43,13 @@ extern "C" { * multiple times, so that the UI can reflect which invocation of the * package it is. If the value is of the format "#/#" (eg, "1/3"), * the UI will add a simple indicator of that status. + * + * The slot_suffix field is used for A/B implementations where the + * bootloader does not set the androidboot.ro.boot.slot_suffix kernel + * commandline parameter. This is used by fs_mgr to mount /system and + * other partitions with the slotselect flag set in fstab. A/B + * implementations are free to use all 32 bytes and may store private + * data past the first NUL-byte in this field. */ struct bootloader_message { char command[32]; @@ -55,7 +62,8 @@ struct bootloader_message { // stage string (for multistage packages) and possible future // expansion. char stage[32]; - char reserved[224]; + char slot_suffix[32]; + char reserved[192]; }; /* Read and write the bootloader command from the "misc" partition. From 1171d3a12b13ca3f1d4301985cf068076e55ae26 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Sat, 5 Dec 2015 21:21:27 -0800 Subject: [PATCH 11/13] Add update_verifier for A/B OTA update. update_verifier checks the integrity of the updated system and vendor partitions on the first boot post an A/B OTA update. It marks the current slot as having booted successfully if it passes the verification. This CL doesn't perform any actual verification work which will be addressed in follow-up CLs. Bug: 26039641 Change-Id: Ia5504ed25b799b48b5886c2fc68073a360127f42 --- Android.mk | 1 + update_verifier/Android.mk | 24 +++++++++ update_verifier/update_verifier.cpp | 84 +++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 update_verifier/Android.mk create mode 100644 update_verifier/update_verifier.cpp diff --git a/Android.mk b/Android.mk index e43d55f6..c2896f3f 100644 --- a/Android.mk +++ b/Android.mk @@ -134,4 +134,5 @@ include $(LOCAL_PATH)/minui/Android.mk \ $(LOCAL_PATH)/edify/Android.mk \ $(LOCAL_PATH)/uncrypt/Android.mk \ $(LOCAL_PATH)/updater/Android.mk \ + $(LOCAL_PATH)/update_verifier/Android.mk \ $(LOCAL_PATH)/applypatch/Android.mk diff --git a/update_verifier/Android.mk b/update_verifier/Android.mk new file mode 100644 index 00000000..0bb05477 --- /dev/null +++ b/update_verifier/Android.mk @@ -0,0 +1,24 @@ +# 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. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_CLANG := true +LOCAL_SRC_FILES := update_verifier.cpp +LOCAL_MODULE := update_verifier +LOCAL_SHARED_LIBRARIES := libcutils libhardware + +include $(BUILD_EXECUTABLE) diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp new file mode 100644 index 00000000..9ba792b8 --- /dev/null +++ b/update_verifier/update_verifier.cpp @@ -0,0 +1,84 @@ +/* + * 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. + */ + +/* + * This program verifies the integrity of the partitions after an A/B OTA + * update. It gets invoked by init, and will only perform the verification if + * it's the first boot post an A/B OTA update. + * + * It relies on dm-verity to capture any corruption on the partitions being + * verified. dm-verity must be in enforcing mode, so that it will reboot the + * device on dm-verity failures. When that happens, the bootloader should + * mark the slot as unbootable and stops trying. We should never see a device + * started in dm-verity logging mode but with isSlotBootable equals to 0. + * + * The current slot will be marked as having booted successfully if the + * verifier reaches the end after the verification. + * + * TODO: The actual verification part will be added later after we have the + * A/B OTA package format in place. + */ + +#include +#include + +#include + +#define LOG_TAG "update_verifier" +#define INFO(x...) KLOG_INFO(LOG_TAG, x) +#define ERROR(x...) KLOG_ERROR(LOG_TAG, x) + +int main(int argc, char** argv) { + klog_init(); + klog_set_level(6); + for (int i = 1; i < argc; i++) { + INFO("Started with arg %d: %s\n", i, argv[i]); + } + + const hw_module_t* hw_module; + if (hw_get_module("bootctrl", &hw_module) != 0) { + ERROR("Error getting bootctrl module.\n"); + return -1; + } + + boot_control_module_t* module = reinterpret_cast( + const_cast(hw_module)); + module->init(module); + + unsigned current_slot = module->getCurrentSlot(module); + int bootable = module->isSlotBootable(module, current_slot); + INFO("Booting slot %u: isSlotBootable=%d\n", current_slot, bootable); + + if (bootable == 0) { + // The current slot has not booted successfully. + + // TODO: Add the actual verification after we have the A/B OTA package + // format in place. + + // TODO: Assert the dm-verity mode. Bootloader should never boot a newly + // flashed slot (isSlotBootable == 0) with dm-verity logging mode. + + int ret = module->markBootSuccessful(module); + if (ret != 0) { + ERROR("Error marking booted successfully: %s\n", strerror(-ret)); + return -1; + } + INFO("Marked slot %u as booted successfully.\n", current_slot); + } + + INFO("Leaving update_verifier.\n"); + return 0; +} From 45eac58ef188679f6df2d80efc0391c6d7904cd8 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 7 Dec 2015 17:04:58 -0800 Subject: [PATCH 12/13] update_verifier: Log to logd instead of kernel log. logd already gets started before we call update_verifier. Bug: 26039641 Change-Id: If00669a77bf9a6e5534e33f4e50b42eabba2667a --- update_verifier/Android.mk | 2 +- update_verifier/update_verifier.cpp | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/update_verifier/Android.mk b/update_verifier/Android.mk index 0bb05477..7f28bced 100644 --- a/update_verifier/Android.mk +++ b/update_verifier/Android.mk @@ -19,6 +19,6 @@ include $(CLEAR_VARS) LOCAL_CLANG := true LOCAL_SRC_FILES := update_verifier.cpp LOCAL_MODULE := update_verifier -LOCAL_SHARED_LIBRARIES := libcutils libhardware +LOCAL_SHARED_LIBRARIES := libhardware liblog include $(BUILD_EXECUTABLE) diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp index 9ba792b8..5e888157 100644 --- a/update_verifier/update_verifier.cpp +++ b/update_verifier/update_verifier.cpp @@ -32,25 +32,21 @@ * A/B OTA package format in place. */ -#include #include #include #define LOG_TAG "update_verifier" -#define INFO(x...) KLOG_INFO(LOG_TAG, x) -#define ERROR(x...) KLOG_ERROR(LOG_TAG, x) +#include int main(int argc, char** argv) { - klog_init(); - klog_set_level(6); for (int i = 1; i < argc; i++) { - INFO("Started with arg %d: %s\n", i, argv[i]); + SLOGI("Started with arg %d: %s\n", i, argv[i]); } const hw_module_t* hw_module; if (hw_get_module("bootctrl", &hw_module) != 0) { - ERROR("Error getting bootctrl module.\n"); + SLOGE("Error getting bootctrl module.\n"); return -1; } @@ -60,7 +56,7 @@ int main(int argc, char** argv) { unsigned current_slot = module->getCurrentSlot(module); int bootable = module->isSlotBootable(module, current_slot); - INFO("Booting slot %u: isSlotBootable=%d\n", current_slot, bootable); + SLOGI("Booting slot %u: isSlotBootable=%d\n", current_slot, bootable); if (bootable == 0) { // The current slot has not booted successfully. @@ -73,12 +69,12 @@ int main(int argc, char** argv) { int ret = module->markBootSuccessful(module); if (ret != 0) { - ERROR("Error marking booted successfully: %s\n", strerror(-ret)); + SLOGE("Error marking booted successfully: %s\n", strerror(-ret)); return -1; } - INFO("Marked slot %u as booted successfully.\n", current_slot); + SLOGI("Marked slot %u as booted successfully.\n", current_slot); } - INFO("Leaving update_verifier.\n"); + SLOGI("Leaving update_verifier.\n"); return 0; } From a5d5082222b7420801cdb77f09305dd4c3afb4db Mon Sep 17 00:00:00 2001 From: Andriy Naborskyy Date: Fri, 8 Jan 2016 10:11:41 -0800 Subject: [PATCH 13/13] Revert "Byte swap to support BGRA in recovery mode" This reverts commit e5879c3639789d61803605c12371a4f291e0b3cc. The swap in page flip code is not needed any more. New changes take care of ABGR and BGRA formats swapping bytes in png and drawing routines See commit fd778e3e406a7e83536ea66776996f032f24af64 Bug: 26243152 Change-Id: I313ee41bee2c143b4e5412515285a65ac394ec77 --- minui/graphics_fbdev.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index 997e9cac..0788f755 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -176,18 +176,6 @@ static GRSurface* fbdev_init(minui_backend* backend) { static GRSurface* fbdev_flip(minui_backend* backend __unused) { if (double_buffered) { -#if defined(RECOVERY_BGRA) - // In case of BGRA, do some byte swapping - unsigned int idx; - unsigned char tmp; - unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data; - for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes); - idx += 4) { - tmp = ucfb_vaddr[idx]; - ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2]; - ucfb_vaddr[idx + 2] = tmp; - } -#endif // Change gr_draw to point to the buffer currently displayed, // then flip the driver so we're displaying the other buffer // instead.