am 596219d9: (-s ours) am b91e7005: am 85090d0c: Merge "recovery: Switch fuse_* to C++."

* commit '596219d9c2ea760a473c9f90bfa0317c326913af':
diff --git a/Android.mk b/Android.mk
index 74e7b1d..d96849f 100644
--- a/Android.mk
+++ b/Android.mk
@@ -40,6 +40,7 @@
     screen_ui.cpp \
     ui.cpp \
     verifier.cpp \
+    wear_ui.cpp \
 
 LOCAL_MODULE := recovery
 
diff --git a/applypatch/Android.mk b/applypatch/Android.mk
index 1f73fd8..cc17a13 100644
--- a/applypatch/Android.mk
+++ b/applypatch/Android.mk
@@ -21,7 +21,7 @@
 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_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_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 96bd88e..1767761 100644
--- a/applypatch/applypatch.cpp
+++ b/applypatch/applypatch.cpp
@@ -25,10 +25,13 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <base/strings.h>
+
 #include "mincrypt/sha.h"
 #include "applypatch.h"
 #include "mtdutils/mtdutils.h"
 #include "edify/expr.h"
+#include "print_sha1.h"
 
 static int LoadPartitionContents(const char* filename, FileContents* file);
 static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token);
@@ -42,7 +45,7 @@
                           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 +90,6 @@
     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<const int*>(a);
-    const int bb = *reinterpret_cast<const int*>(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:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..."  (or
@@ -120,53 +108,45 @@
 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<std::string> 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<size_t> index(pairs);
+    std::vector<size_t> size(pairs);
+    std::vector<std::string> sha1sum(pairs);
 
-    int pairs = (colons-1)/2;     // # of (size,sha1) pairs in filename
-    int* index = reinterpret_cast<int*>(malloc(pairs * sizeof(int)));
-    size_t* size = reinterpret_cast<size_t*>(malloc(pairs * sizeof(size_t)));
-    char** sha1sum = reinterpret_cast<char**>(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 +155,18 @@
         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 +175,7 @@
         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 +184,15 @@
     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<unsigned char*>(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 +222,8 @@
         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 +233,8 @@
             // 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 +252,8 @@
     }
 
 
-    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 +270,6 @@
     file->st.st_uid = 0;
     file->st.st_gid = 0;
 
-    free(copy);
-    free(index);
-    free(size);
-    free(sha1sum);
-
     return 0;
 }
 
@@ -340,33 +312,34 @@
 }
 
 // Write a memory buffer to 'target' partition, a string of the form
-// "MTD:<partition>[:...]" or "EMMC:<partition_device>:".  Return 0 on
-// success.
+// "MTD:<partition>[:...]" or "EMMC:<partition_device>[:...]". 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) {
-    char* copy = strdup(target);
-    const char* magic = strtok(copy, ":");
+    std::string copy(target);
+    std::vector<std::string> 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 +383,7 @@
                 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;
@@ -490,7 +463,7 @@
                 }
 
                 if (start == len) {
-                    printf("verification read succeeded (attempt %d)\n", attempt+1);
+                    printf("verification read succeeded (attempt %zu)\n", attempt+1);
                     success = true;
                     break;
                 }
@@ -510,7 +483,6 @@
         }
     }
 
-    free(copy);
     return 0;
 }
 
@@ -658,14 +630,6 @@
     }
 }
 
-static void print_short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) {
-    const char* hex = "0123456789abcdef";
-    for (size_t i = 0; i < 4; ++i) {
-        putchar(hex[(sha1[i]>>4) & 0xf]);
-        putchar(hex[sha1[i] & 0xf]);
-    }
-}
-
 // This function applies binary patches to files in a way that is safe
 // (the original file is not touched until we have the desired
 // replacement for it) and idempotent (it's okay to run this program
@@ -678,7 +642,7 @@
 //   entries in <patch_sha1_str>, the corresponding patch from
 //   <patch_data> (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 <target_sha1_str>,
+//   blob data).  If that new file has sha1 hash <target_sha1_str>,
 //   moves it to replace <target_filename>, and exits successfully.
 //   Note that if <source_filename> and <target_filename> are not the
 //   same, <source_filename> is NOT deleted on success.
@@ -689,7 +653,7 @@
 //   status.
 //
 // <source_filename> 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,
@@ -724,9 +688,7 @@
         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;
         }
@@ -783,6 +745,67 @@
     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<std::string> 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,
@@ -983,9 +1006,7 @@
         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 edec848..415bc1b 100644
--- a/applypatch/applypatch.h
+++ b/applypatch/applypatch.h
@@ -48,6 +48,8 @@
 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 63ff5c2..966d8b9 100644
--- a/applypatch/main.cpp
+++ b/applypatch/main.cpp
@@ -47,8 +47,8 @@
 // "<sha1>:<filename>" 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<char**>(malloc(*num_patches * sizeof(char*)));
     *patches = reinterpret_cast<Value**>(malloc(*num_patches * sizeof(Value*)));
@@ -98,7 +98,12 @@
     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 @@
         argv += 2;
     }
 
-    if (argc < 6) {
+    if (argc < 4) {
         return 2;
     }
 
@@ -125,6 +130,16 @@
         return 1;
     }
 
+    // If no <src-sha1>:<patch> 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 @@
 // - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits
 //   successfully.
 //
+// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with
+//   <src-file>. <tgt-file> must be a partition name, while <src-file> must
+//   be a regular image file. <src-file> will not be deleted on success.
+//
 // - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the
 //   bsdiff <patch> to <src-file> to produce a new file (the type of patch
 //   is automatically detected from the file header).  If that new
diff --git a/bootloader.h b/bootloader.h
index c2895dd..4e9fb0a 100644
--- a/bootloader.h
+++ b/bootloader.h
@@ -17,10 +17,6 @@
 #ifndef _RECOVERY_BOOTLOADER_H
 #define _RECOVERY_BOOTLOADER_H
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 /* Bootloader Message
  *
  * This structure describes the content of a block in flash
@@ -64,8 +60,4 @@
 int get_bootloader_message(struct bootloader_message *out);
 int set_bootloader_message(const struct bootloader_message *in);
 
-#ifdef __cplusplus
-}
-#endif
-
 #endif
diff --git a/common.h b/common.h
index b818ceb..de8b409 100644
--- a/common.h
+++ b/common.h
@@ -21,10 +21,6 @@
 #include <stdio.h>
 #include <stdarg.h>
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 #define LOGE(...) ui_print("E:" __VA_ARGS__)
 #define LOGW(...) fprintf(stdout, "W:" __VA_ARGS__)
 #define LOGI(...) fprintf(stdout, "I:" __VA_ARGS__)
@@ -50,8 +46,4 @@
 
 bool is_ro_debuggable();
 
-#ifdef __cplusplus
-}
-#endif
-
 #endif  // RECOVERY_COMMON_H
diff --git a/edify/Android.mk b/edify/Android.mk
index c366450..9b859d4 100644
--- a/edify/Android.mk
+++ b/edify/Android.mk
@@ -5,12 +5,7 @@
 edify_src_files := \
 	lexer.l \
 	parser.y \
-	expr.c
-
-# "-x c" forces the lex/yacc files to be compiled as c the build system
-# otherwise forces them to be c++. Need to also add an explicit -std because the
-# build system will soon default C++ to -std=c++11.
-edify_cflags := -x c -std=gnu89
+	expr.cpp
 
 #
 # Build the host-side command line tool
@@ -19,12 +14,13 @@
 
 LOCAL_SRC_FILES := \
 		$(edify_src_files) \
-		main.c
+		main.cpp
 
-LOCAL_CFLAGS := $(edify_cflags) -g -O0
+LOCAL_CPPFLAGS := -g -O0
 LOCAL_MODULE := edify
 LOCAL_YACCFLAGS := -v
-LOCAL_CFLAGS += -Wno-unused-parameter
+LOCAL_CPPFLAGS += -Wno-unused-parameter
+LOCAL_CPPFLAGS += -Wno-deprecated-register
 LOCAL_CLANG := true
 
 include $(BUILD_HOST_EXECUTABLE)
@@ -36,8 +32,8 @@
 
 LOCAL_SRC_FILES := $(edify_src_files)
 
-LOCAL_CFLAGS := $(edify_cflags)
-LOCAL_CFLAGS += -Wno-unused-parameter
+LOCAL_CPPFLAGS := -Wno-unused-parameter
+LOCAL_CPPFLAGS += -Wno-deprecated-register
 LOCAL_MODULE := libedify
 LOCAL_CLANG := true
 
diff --git a/edify/expr.c b/edify/expr.cpp
similarity index 92%
rename from edify/expr.c
rename to edify/expr.cpp
index 79f6282..cd1e087 100644
--- a/edify/expr.c
+++ b/edify/expr.cpp
@@ -51,7 +51,7 @@
 
 Value* StringValue(char* str) {
     if (str == NULL) return NULL;
-    Value* v = malloc(sizeof(Value));
+    Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
     v->type = VAL_STRING;
     v->size = strlen(str);
     v->data = str;
@@ -68,7 +68,7 @@
     if (argc == 0) {
         return StringValue(strdup(""));
     }
-    char** strings = malloc(argc * sizeof(char*));
+    char** strings = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
     int i;
     for (i = 0; i < argc; ++i) {
         strings[i] = NULL;
@@ -83,8 +83,9 @@
         length += strlen(strings[i]);
     }
 
-    result = malloc(length+1);
-    int p = 0;
+    result = reinterpret_cast<char*>(malloc(length+1));
+    int p;
+    p = 0;
     for (i = 0; i < argc; ++i) {
         strcpy(result+p, strings[i]);
         p += strlen(strings[i]);
@@ -149,7 +150,7 @@
         if (!b) {
             int prefix_len;
             int len = argv[i]->end - argv[i]->start;
-            char* err_src = malloc(len + 20);
+            char* err_src = reinterpret_cast<char*>(malloc(len + 20));
             strcpy(err_src, "assert failed: ");
             prefix_len = strlen(err_src);
             memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
@@ -290,7 +291,8 @@
         goto done;
     }
 
-    long r_int = strtol(right, &end, 10);
+    long r_int;
+    r_int = strtol(right, &end, 10);
     if (right[0] == '\0' || *end != '\0') {
         goto done;
     }
@@ -325,11 +327,11 @@
 Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
     va_list v;
     va_start(v, count);
-    Expr* e = malloc(sizeof(Expr));
+    Expr* e = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
     e->fn = fn;
     e->name = "(operator)";
     e->argc = count;
-    e->argv = malloc(count * sizeof(Expr*));
+    e->argv = reinterpret_cast<Expr**>(malloc(count * sizeof(Expr*)));
     int i;
     for (i = 0; i < count; ++i) {
         e->argv[i] = va_arg(v, Expr*);
@@ -351,7 +353,7 @@
 void RegisterFunction(const char* name, Function fn) {
     if (fn_entries >= fn_size) {
         fn_size = fn_size*2 + 1;
-        fn_table = realloc(fn_table, fn_size * sizeof(NamedFunction));
+        fn_table = reinterpret_cast<NamedFunction*>(realloc(fn_table, fn_size * sizeof(NamedFunction)));
     }
     fn_table[fn_entries].name = name;
     fn_table[fn_entries].fn = fn;
@@ -371,8 +373,8 @@
 Function FindFunction(const char* name) {
     NamedFunction key;
     key.name = name;
-    NamedFunction* nf = bsearch(&key, fn_table, fn_entries,
-                                sizeof(NamedFunction), fn_entry_compare);
+    NamedFunction* nf = reinterpret_cast<NamedFunction*>(bsearch(&key, fn_table, fn_entries,
+            sizeof(NamedFunction), fn_entry_compare));
     if (nf == NULL) {
         return NULL;
     }
@@ -401,7 +403,7 @@
 // zero or more char** to put them in).  If any expression evaluates
 // to NULL, free the rest and return -1.  Return 0 on success.
 int ReadArgs(State* state, Expr* argv[], int count, ...) {
-    char** args = malloc(count * sizeof(char*));
+    char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*)));
     va_list v;
     va_start(v, count);
     int i;
@@ -427,7 +429,7 @@
 // zero or more Value** to put them in).  If any expression evaluates
 // to NULL, free the rest and return -1.  Return 0 on success.
 int ReadValueArgs(State* state, Expr* argv[], int count, ...) {
-    Value** args = malloc(count * sizeof(Value*));
+    Value** args = reinterpret_cast<Value**>(malloc(count * sizeof(Value*)));
     va_list v;
     va_start(v, count);
     int i;
@@ -494,7 +496,7 @@
 // Use printf-style arguments to compose an error message to put into
 // *state.  Returns NULL.
 Value* ErrorAbort(State* state, const char* format, ...) {
-    char* buffer = malloc(4096);
+    char* buffer = reinterpret_cast<char*>(malloc(4096));
     va_list v;
     va_start(v, format);
     vsnprintf(buffer, 4096, format, v);
diff --git a/edify/expr.h b/edify/expr.h
index a9ed2f9..36f8e96 100644
--- a/edify/expr.h
+++ b/edify/expr.h
@@ -21,10 +21,6 @@
 
 #include "yydefs.h"
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 #define MAX_STRING_LEN 1024
 
 typedef struct Expr Expr;
@@ -59,7 +55,7 @@
 
 struct Expr {
     Function fn;
-    char* name;
+    const char* name;
     int argc;
     Expr** argv;
     int start, end;
@@ -166,8 +162,4 @@
 
 int parse_string(const char* str, Expr** root, int* error_count);
 
-#ifdef __cplusplus
-}  // extern "C"
-#endif
-
 #endif  // _EXPRESSION_H
diff --git a/edify/main.c b/edify/main.cpp
similarity index 100%
rename from edify/main.c
rename to edify/main.cpp
diff --git a/edify/parser.y b/edify/parser.y
index f8fb2d1..098a637 100644
--- a/edify/parser.y
+++ b/edify/parser.y
@@ -70,7 +70,7 @@
 ;
 
 expr:  STRING {
-    $$ = malloc(sizeof(Expr));
+    $$ = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
     $$->fn = Literal;
     $$->name = $1;
     $$->argc = 0;
@@ -91,7 +91,7 @@
 |  IF expr THEN expr ENDIF           { $$ = Build(IfElseFn, @$, 2, $2, $4); }
 |  IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); }
 | STRING '(' arglist ')' {
-    $$ = malloc(sizeof(Expr));
+    $$ = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
     $$->fn = FindFunction($1);
     if ($$->fn == NULL) {
         char buffer[256];
@@ -113,12 +113,12 @@
 }
 | expr {
     $$.argc = 1;
-    $$.argv = malloc(sizeof(Expr*));
+    $$.argv = reinterpret_cast<Expr**>(malloc(sizeof(Expr*)));
     $$.argv[0] = $1;
 }
 | arglist ',' expr {
     $$.argc = $1.argc + 1;
-    $$.argv = realloc($$.argv, $$.argc * sizeof(Expr*));
+    $$.argv = reinterpret_cast<Expr**>(realloc($$.argv, $$.argc * sizeof(Expr*)));
     $$.argv[$$.argc-1] = $3;
 }
 ;
diff --git a/etc/init.rc b/etc/init.rc
index 0a4c6e9..dc18659 100644
--- a/etc/init.rc
+++ b/etc/init.rc
@@ -46,8 +46,8 @@
     class_start default
 
 # Load properties from /system/ + /factory after fs mount.
-on load_all_props_action
-    load_all_props
+on load_system_props_action
+    load_system_props
 
 on firmware_mounts_complete
    rm /dev/.booting
@@ -62,7 +62,7 @@
     # Load properties from /system/ + /factory after fs mount. Place
     # this in another action so that the load will be scheduled after the prior
     # issued fs triggers have completed.
-    trigger load_all_props_action
+    trigger load_system_props_action
 
     # Remove a file to wake up anything waiting for firmware
     trigger firmware_mounts_complete
diff --git a/minadbd/adb_main.cpp b/minadbd/adb_main.cpp
index 724f39c..514f196 100644
--- a/minadbd/adb_main.cpp
+++ b/minadbd/adb_main.cpp
@@ -27,7 +27,7 @@
 #include "adb_auth.h"
 #include "transport.h"
 
-int adb_main(int is_daemon, int server_port) {
+int adb_main(int is_daemon, int server_port, int /* reply_fd */) {
     adb_device_banner = "sideload";
 
     signal(SIGPIPE, SIG_IGN);
diff --git a/print_sha1.h b/print_sha1.h
new file mode 100644
index 0000000..9e37c5f
--- /dev/null
+++ b/print_sha1.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RECOVERY_PRINT_SHA1_H
+#define RECOVERY_PRINT_SHA1_H
+
+#include <stdint.h>
+#include <string>
+
+#include "mincrypt/sha.h"
+
+static std::string print_sha1(const uint8_t sha1[SHA_DIGEST_SIZE], size_t len) {
+    const char* hex = "0123456789abcdef";
+    std::string result = "";
+    for (size_t i = 0; i < len; ++i) {
+        result.push_back(hex[(sha1[i]>>4) & 0xf]);
+        result.push_back(hex[sha1[i] & 0xf]);
+    }
+    return result;
+}
+
+static std::string print_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) {
+    return print_sha1(sha1, SHA_DIGEST_SIZE);
+}
+
+static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_SIZE]) {
+    return print_sha1(sha1, 4);
+}
+
+#endif  // RECOVERY_PRINT_SHA1_H
diff --git a/recovery.cpp b/recovery.cpp
index 83ca581..c683bae 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -863,9 +863,24 @@
                 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;
         }
     }
@@ -927,7 +942,7 @@
     // only way recovery should be run with this argument is when it
     // starts a copy of itself from the apply_from_adb() function.
     if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
-        adb_main(0, DEFAULT_ADB_PORT);
+        adb_main(0, DEFAULT_ADB_PORT, -1);
         return 0;
     }
 
@@ -1021,11 +1036,15 @@
         if (strncmp(update_package, "CACHE:", 6) == 0) {
             int len = strlen(update_package) + 10;
             char* modified_path = (char*)malloc(len);
-            strlcpy(modified_path, "/cache/", len);
-            strlcat(modified_path, update_package+6, len);
-            printf("(replacing path \"%s\" with \"%s\")\n",
-                   update_package, modified_path);
-            update_package = modified_path;
+            if (modified_path) {
+                strlcpy(modified_path, "/cache/", len);
+                strlcat(modified_path, update_package+6, len);
+                printf("(replacing path \"%s\" with \"%s\")\n",
+                       update_package, modified_path);
+                update_package = modified_path;
+            }
+            else
+                printf("modified_path allocation failed\n");
         }
     }
     printf("\n");
diff --git a/roots.cpp b/roots.cpp
index 9288177..12c6b5e 100644
--- a/roots.cpp
+++ b/roots.cpp
@@ -70,7 +70,8 @@
     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 @@
         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 @@
         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 230d9de..6e3b243 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 @@
 // 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 @@
 // mounted (/tmp and /cache) are mounted.  Returns 0 on success.
 int setup_install_mounts();
 
-#ifdef __cplusplus
-}
-#endif
-
 #endif  // RECOVERY_ROOTS_H_
diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk
index e73c8f1..f31db42 100644
--- a/uncrypt/Android.mk
+++ b/uncrypt/Android.mk
@@ -20,6 +20,8 @@
 
 LOCAL_SRC_FILES := uncrypt.cpp
 
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/..
+
 LOCAL_MODULE := uncrypt
 
 LOCAL_STATIC_LIBRARIES := libbase liblog libfs_mgr libcutils
diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp
index 20a2729..aef4800 100644
--- a/uncrypt/uncrypt.cpp
+++ b/uncrypt/uncrypt.cpp
@@ -51,6 +51,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <memory>
+
 #include <base/file.h>
 #include <base/strings.h>
 #include <cutils/android_reboot.h>
@@ -60,6 +62,8 @@
 #define LOG_TAG "uncrypt"
 #include <log/log.h>
 
+#include "unique_fd.h"
+
 #define WINDOW_SIZE 5
 
 static const std::string cache_block_map = "/cache/recovery/block.map";
@@ -146,7 +150,7 @@
             (path[len] == '/' || path[len] == 0)) {
             *encrypted = false;
             *encryptable = false;
-            if (fs_mgr_is_encryptable(v)) {
+            if (fs_mgr_is_encryptable(v) || fs_mgr_is_file_encrypted(v)) {
                 *encryptable = true;
                 char buffer[PROPERTY_VALUE_MAX+1];
                 if (property_get("ro.crypto.state", buffer, "") &&
@@ -183,6 +187,7 @@
         return -1;
     }
     FILE* mapf = fdopen(mapfd, "w");
+    unique_file mapf_holder(mapf);
 
     // Make sure we can write to the status_file.
     if (!android::base::WriteStringToFd("0\n", status_fd)) {
@@ -191,8 +196,7 @@
     }
 
     struct stat sb;
-    int ret = stat(path, &sb);
-    if (ret != 0) {
+    if (stat(path, &sb) != 0) {
         ALOGE("failed to stat %s\n", path);
         return -1;
     }
@@ -221,15 +225,18 @@
     size_t pos = 0;
 
     int fd = open(path, O_RDONLY);
-    if (fd < 0) {
+    unique_fd fd_holder(fd);
+    if (fd == -1) {
         ALOGE("failed to open fd for reading: %s\n", strerror(errno));
         return -1;
     }
 
     int wfd = -1;
+    unique_fd wfd_holder(wfd);
     if (encrypted) {
         wfd = open(blk_dev, O_WRONLY | O_SYNC);
-        if (wfd < 0) {
+        wfd_holder = unique_fd(wfd);
+        if (wfd == -1) {
             ALOGE("failed to open fd for writing: %s\n", strerror(errno));
             return -1;
         }
@@ -247,8 +254,7 @@
         if ((tail+1) % WINDOW_SIZE == head) {
             // write out head buffer
             int block = head_block;
-            ret = ioctl(fd, FIBMAP, &block);
-            if (ret != 0) {
+            if (ioctl(fd, FIBMAP, &block) != 0) {
                 ALOGE("failed to find block %d\n", head_block);
                 return -1;
             }
@@ -288,8 +294,7 @@
     while (head != tail) {
         // write out head buffer
         int block = head_block;
-        ret = ioctl(fd, FIBMAP, &block);
-        if (ret != 0) {
+        if (ioctl(fd, FIBMAP, &block) != 0) {
             ALOGE("failed to find block %d\n", head_block);
             return -1;
         }
@@ -313,14 +318,11 @@
         ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno));
         return -1;
     }
-    fclose(mapf);
-    close(fd);
     if (encrypted) {
         if (fsync(wfd) == -1) {
             ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno));
             return -1;
         }
-        close(wfd);
     }
 
     return 0;
@@ -333,6 +335,8 @@
         if (!v->mount_point) continue;
         if (strcmp(v->mount_point, "/misc") == 0) {
             int fd = open(v->blk_device, O_WRONLY | O_SYNC);
+            unique_fd fd_holder(fd);
+
             uint8_t zeroes[1088];   // sizeof(bootloader_message) from recovery
             memset(zeroes, 0, sizeof(zeroes));
 
@@ -349,10 +353,8 @@
             }
             if (fsync(fd) == -1) {
                 ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
-                close(fd);
                 return;
             }
-            close(fd);
         }
     }
 }
@@ -437,6 +439,7 @@
             ALOGE("failed to open pipe \"%s\": %s\n", status_file.c_str(), strerror(errno));
             return 1;
         }
+        unique_fd status_fd_holder(status_fd);
 
         if (argc == 3) {
             // when command-line args are given this binary is being used
@@ -447,7 +450,6 @@
             std::string package;
             if (!find_uncrypt_package(package)) {
                 android::base::WriteStringToFd("-1\n", status_fd);
-                close(status_fd);
                 return 1;
             }
             input_path = package.c_str();
@@ -457,12 +459,10 @@
         int status = uncrypt(input_path, map_file, status_fd);
         if (status != 0) {
             android::base::WriteStringToFd("-1\n", status_fd);
-            close(status_fd);
             return 1;
         }
 
         android::base::WriteStringToFd("100\n", status_fd);
-        close(status_fd);
     }
 
     return 0;
diff --git a/unique_fd.h b/unique_fd.h
new file mode 100644
index 0000000..98a7c7b
--- /dev/null
+++ b/unique_fd.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UNIQUE_FD_H
+#define UNIQUE_FD_H
+
+#include <stdio.h>
+
+#include <memory>
+
+class unique_fd {
+  public:
+    unique_fd(int fd) : fd_(fd) { }
+
+    unique_fd(unique_fd&& uf) {
+        fd_ = uf.fd_;
+        uf.fd_ = -1;
+    }
+
+    ~unique_fd() {
+        if (fd_ != -1) {
+            close(fd_);
+        }
+    }
+
+    int get() {
+        return fd_;
+    }
+
+    // Movable.
+    unique_fd& operator=(unique_fd&& uf) {
+        fd_ = uf.fd_;
+        uf.fd_ = -1;
+        return *this;
+    }
+
+    explicit operator bool() const {
+        return fd_ != -1;
+    }
+
+  private:
+    int fd_;
+
+    // Non-copyable.
+    unique_fd(const unique_fd&) = delete;
+    unique_fd& operator=(const unique_fd&) = delete;
+};
+
+// Custom deleter for unique_file to avoid fclose(NULL).
+struct safe_fclose {
+    void operator()(FILE *fp) const {
+        if (fp) {
+            fclose(fp);
+        };
+    }
+};
+
+using unique_file = std::unique_ptr<FILE, safe_fclose>;
+
+#endif  // UNIQUE_FD_H
diff --git a/updater/Android.mk b/updater/Android.mk
index 0d4179b..82fa7e2 100644
--- a/updater/Android.mk
+++ b/updater/Android.mk
@@ -44,7 +44,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 += libapplypatch libbase libedify libmtdutils libminzip libz
 LOCAL_STATIC_LIBRARIES += libmincrypt libbz
 LOCAL_STATIC_LIBRARIES += libcutils liblog libc
 LOCAL_STATIC_LIBRARIES += libselinux
diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp
old mode 100644
new mode 100755
index 258d975..7da9adf
--- a/updater/blockimg.cpp
+++ b/updater/blockimg.cpp
@@ -32,11 +32,17 @@
 #include <time.h>
 #include <unistd.h>
 
+#include <memory>
+#include <string>
+
+#include <base/strings.h>
+
 #include "applypatch/applypatch.h"
 #include "edify/expr.h"
 #include "mincrypt/sha.h"
 #include "minzip/Hash.h"
 #include "updater.h"
+#include "print_sha1.h"
 
 #define BLOCKSIZE 4096
 
@@ -49,12 +55,10 @@
 #define STASH_DIRECTORY_MODE 0700
 #define STASH_FILE_MODE 0600
 
-char* PrintSha1(const uint8_t* digest);
-
 typedef struct {
-    int count;
-    int size;
-    int pos[0];
+    size_t count;
+    size_t size;
+    size_t pos[0];  // Actual limit is INT_MAX.
 } RangeSet;
 
 #define RANGESET_MAX_POINTS \
@@ -78,16 +82,17 @@
         goto err;
     }
 
+    errno = 0;
     val = strtol(token, NULL, 0);
 
-    if (val < 2 || val > RANGESET_MAX_POINTS) {
+    if (errno != 0 || 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);
+    bufsize = sizeof(RangeSet) + num * sizeof(size_t);
 
     out = reinterpret_cast<RangeSet*>(malloc(bufsize));
 
@@ -99,41 +104,50 @@
     out->count = num / 2;
     out->size = 0;
 
-    for (int i = 0; i < num; ++i) {
+    for (int i = 0; i < num; i += 2) {
         token = strtok_r(NULL, ",", &save);
 
         if (!token) {
             goto err;
         }
 
+        errno = 0;
         val = strtol(token, NULL, 0);
 
-        if (val < 0 || val > INT_MAX) {
+        if (errno != 0 || val < 0 || val > INT_MAX) {
             goto err;
         }
 
-        out->pos[i] = (int) val;
+        out->pos[i] = static_cast<size_t>(val);
 
-        if (i % 2) {
-            if (out->pos[i - 1] >= out->pos[i]) {
-                goto err; // empty or negative range
-            }
+        token = strtok_r(NULL, ",", &save);
 
-            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 (!token) {
+            goto err;
         }
+
+        errno = 0;
+        val = strtol(token, NULL, 0);
+
+        if (errno != 0 || val < 0 || val > INT_MAX) {
+            goto err;
+        }
+
+        out->pos[i+1] = static_cast<size_t>(val);
+
+        if (out->pos[i] >= out->pos[i+1]) {
+            goto err; // empty or negative range
+        }
+
+        size_t rs = out->pos[i+1] - out->pos[i];
+        if (out->size > SIZE_MAX - rs) {
+            goto err; // overflow
+        }
+
+        out->size += rs;
     }
 
-    if (out->size <= 0) {
+    if (out->size == 0) {
         goto err;
     }
 
@@ -144,28 +158,22 @@
     exit(1);
 }
 
-static int range_overlaps(RangeSet* r1, RangeSet* r2) {
-    int i, j, r1_0, r1_1, r2_0, r2_1;
+static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) {
+    for (size_t i = 0; i < r1.count; ++i) {
+        size_t r1_0 = r1.pos[i * 2];
+        size_t r1_1 = r1.pos[i * 2 + 1];
 
-    if (!r1 || !r2) {
-        return 0;
-    }
-
-    for (i = 0; i < r1->count; ++i) {
-        r1_0 = r1->pos[i * 2];
-        r1_1 = r1->pos[i * 2 + 1];
-
-        for (j = 0; j < r2->count; ++j) {
-            r2_0 = r2->pos[j * 2];
-            r2_1 = r2->pos[j * 2 + 1];
+        for (size_t j = 0; j < r2.count; ++j) {
+            size_t r2_0 = r2.pos[j * 2];
+            size_t r2_1 = r2.pos[j * 2 + 1];
 
             if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
-                return 1;
+                return true;
             }
         }
     }
 
-    return 0;
+    return false;
 }
 
 static int read_all(int fd, uint8_t* data, size_t size) {
@@ -192,11 +200,6 @@
         written += w;
     }
 
-    if (fsync(fd) == -1) {
-        fprintf(stderr, "fsync failed: %s\n", strerror(errno));
-        return -1;
-    }
-
     return 0;
 }
 
@@ -226,7 +229,7 @@
 typedef struct {
     int fd;
     RangeSet* tgt;
-    int p_block;
+    size_t p_block;
     size_t p_remain;
 } RangeSinkState;
 
@@ -347,20 +350,18 @@
 }
 
 static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
-    int i;
     size_t p = 0;
-    size_t size;
 
     if (!src || !buffer) {
         return -1;
     }
 
-    for (i = 0; i < src->count; ++i) {
+    for (size_t i = 0; i < src->count; ++i) {
         if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
             return -1;
         }
 
-        size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
+        size_t size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
 
         if (read_all(fd, buffer + p, size) == -1) {
             return -1;
@@ -373,20 +374,18 @@
 }
 
 static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
-    int i;
     size_t p = 0;
-    size_t size;
 
     if (!tgt || !buffer) {
         return -1;
     }
 
-    for (i = 0; i < tgt->count; ++i) {
+    for (size_t i = 0; i < tgt->count; ++i) {
         if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
             return -1;
         }
 
-        size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
+        size_t size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
 
         if (write_all(fd, buffer + p, size) == -1) {
             return -1;
@@ -409,7 +408,7 @@
 // in *tgt, if tgt is non-NULL.
 
 static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks,
-                               uint8_t** buffer, size_t* buffer_alloc, int fd) {
+                              uint8_t** buffer, size_t* buffer_alloc, int fd) {
     char* word;
     int rc;
 
@@ -430,8 +429,7 @@
 }
 
 static int VerifyBlocks(const char *expected, const uint8_t *buffer,
-                        size_t blocks, int printerror) {
-    char* hexdigest = NULL;
+                        size_t blocks, bool printerror) {
     int rc = -1;
     uint8_t digest[SHA_DIGEST_SIZE];
 
@@ -440,128 +438,75 @@
     }
 
     SHA_hash(buffer, blocks * BLOCKSIZE, digest);
-    hexdigest = PrintSha1(digest);
 
-    if (hexdigest != NULL) {
-        rc = strcmp(expected, hexdigest);
+    std::string hexdigest = print_sha1(digest);
 
-        if (rc != 0 && printerror) {
-            fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
-                expected, hexdigest);
-        }
+    rc = hexdigest != std::string(expected);
 
-        free(hexdigest);
+    if (rc != 0 && printerror) {
+        fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
+            expected, hexdigest.c_str());
     }
 
     return rc;
 }
 
-static char* GetStashFileName(const char* base, const char* id, const char* postfix) {
-    char* fn;
-    int len;
-    int res;
-
-    if (base == NULL) {
-        return NULL;
+static std::string GetStashFileName(const std::string& base, const std::string id,
+        const std::string postfix) {
+    if (base.empty()) {
+        return "";
     }
 
-    if (id == NULL) {
-        id = "";
-    }
-
-    if (postfix == NULL) {
-        postfix = "";
-    }
-
-    len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1;
-    fn = reinterpret_cast<char*>(malloc(len));
-
-    if (fn == NULL) {
-        fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
-        return NULL;
-    }
-
-    res = snprintf(fn, len, STASH_DIRECTORY_BASE "/%s/%s%s", base, id, postfix);
-
-    if (res < 0 || res >= len) {
-        fprintf(stderr, "failed to format file name (return value %d)\n", res);
-        free(fn);
-        return NULL;
-    }
+    std::string fn(STASH_DIRECTORY_BASE);
+    fn += "/" + base + "/" + id + postfix;
 
     return fn;
 }
 
-typedef void (*StashCallback)(const char*, void*);
+typedef void (*StashCallback)(const std::string&, void*);
 
 // Does a best effort enumeration of stash files. Ignores possible non-file
 // items in the stash directory and continues despite of errors. Calls the
 // 'callback' function for each file and passes 'data' to the function as a
 // parameter.
 
-static void EnumerateStash(const char* dirname, StashCallback callback, void* data) {
-    char* fn;
-    DIR* directory;
-    int len;
-    int res;
-    struct dirent* item;
-
-    if (dirname == NULL || callback == NULL) {
+static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
+    if (dirname.empty() || callback == NULL) {
         return;
     }
 
-    directory = opendir(dirname);
+    std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
 
     if (directory == NULL) {
         if (errno != ENOENT) {
-            fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname, strerror(errno));
+            fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
         }
         return;
     }
 
-    while ((item = readdir(directory)) != NULL) {
+    struct dirent* item;
+    while ((item = readdir(directory.get())) != NULL) {
         if (item->d_type != DT_REG) {
             continue;
         }
 
-        len = strlen(dirname) + 1 + strlen(item->d_name) + 1;
-        fn = reinterpret_cast<char*>(malloc(len));
-
-        if (fn == NULL) {
-            fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
-            continue;
-        }
-
-        res = snprintf(fn, len, "%s/%s", dirname, item->d_name);
-
-        if (res < 0 || res >= len) {
-            fprintf(stderr, "failed to format file name (return value %d)\n", res);
-            free(fn);
-            continue;
-        }
-
+        std::string fn = dirname + "/" + std::string(item->d_name);
         callback(fn, data);
-        free(fn);
-    }
-
-    if (closedir(directory) == -1) {
-        fprintf(stderr, "closedir \"%s\" failed: %s\n", dirname, strerror(errno));
     }
 }
 
-static void UpdateFileSize(const char* fn, void* data) {
-    int* size = (int*) data;
+static void UpdateFileSize(const std::string& fn, void* data) {
+    if (fn.empty() || !data) {
+        return;
+    }
+
     struct stat st;
-
-    if (!fn || !data) {
+    if (stat(fn.c_str(), &st) == -1) {
+        fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
         return;
     }
 
-    if (stat(fn, &st) == -1) {
-        fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
-        return;
-    }
-
+    int* size = reinterpret_cast<int*>(data);
     *size += st.st_size;
 }
 
@@ -569,57 +514,49 @@
 // contains files. There is nothing we can do about unlikely, but possible
 // errors, so they are merely logged.
 
-static void DeleteFile(const char* fn, void* data) {
-    if (fn) {
-        fprintf(stderr, "deleting %s\n", fn);
+static void DeleteFile(const std::string& fn, void* data) {
+    if (!fn.empty()) {
+        fprintf(stderr, "deleting %s\n", fn.c_str());
 
-        if (unlink(fn) == -1 && errno != ENOENT) {
-            fprintf(stderr, "unlink \"%s\" failed: %s\n", fn, strerror(errno));
+        if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
+            fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
         }
     }
 }
 
-static void DeletePartial(const char* fn, void* data) {
-    if (fn && strstr(fn, ".partial") != NULL) {
+static void DeletePartial(const std::string& fn, void* data) {
+    if (android::base::EndsWith(fn, ".partial")) {
         DeleteFile(fn, data);
     }
 }
 
-static void DeleteStash(const char* base) {
-    char* dirname;
-
-    if (base == NULL) {
+static void DeleteStash(const std::string& base) {
+    if (base.empty()) {
         return;
     }
 
-    dirname = GetStashFileName(base, NULL, NULL);
+    fprintf(stderr, "deleting stash %s\n", base.c_str());
 
-    if (dirname == NULL) {
-        return;
-    }
-
-    fprintf(stderr, "deleting stash %s\n", base);
+    std::string dirname = GetStashFileName(base, "", "");
     EnumerateStash(dirname, DeleteFile, NULL);
 
-    if (rmdir(dirname) == -1) {
+    if (rmdir(dirname.c_str()) == -1) {
         if (errno != ENOENT && errno != ENOTDIR) {
-            fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname, strerror(errno));
+            fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
         }
     }
-
-    free(dirname);
 }
 
-static int LoadStash(const char* base, const char* id, int verify, int* blocks, uint8_t** buffer,
-        size_t* buffer_alloc, int printnoent) {
-    char *fn = NULL;
+static int LoadStash(const std::string& base, const char* id, int verify, int* blocks,
+        uint8_t** buffer, size_t* buffer_alloc, bool printnoent) {
+    std::string fn;
     int blockcount = 0;
     int fd = -1;
     int rc = -1;
     int res;
     struct stat st;
 
-    if (!base || !id || !buffer || !buffer_alloc) {
+    if (base.empty() || !id || !buffer || !buffer_alloc) {
         goto lsout;
     }
 
@@ -627,33 +564,29 @@
         blocks = &blockcount;
     }
 
-    fn = GetStashFileName(base, id, NULL);
+    fn = GetStashFileName(base, std::string(id), "");
 
-    if (fn == NULL) {
-        goto lsout;
-    }
-
-    res = stat(fn, &st);
+    res = stat(fn.c_str(), &st);
 
     if (res == -1) {
         if (errno != ENOENT || printnoent) {
-            fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
+            fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
         }
         goto lsout;
     }
 
-    fprintf(stderr, " loading %s\n", fn);
+    fprintf(stderr, " loading %s\n", fn.c_str());
 
     if ((st.st_size % BLOCKSIZE) != 0) {
         fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
-                fn, static_cast<int64_t>(st.st_size), BLOCKSIZE);
+                fn.c_str(), static_cast<int64_t>(st.st_size), BLOCKSIZE);
         goto lsout;
     }
 
-    fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY));
+    fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY));
 
     if (fd == -1) {
-        fprintf(stderr, "open \"%s\" failed: %s\n", fn, strerror(errno));
+        fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
         goto lsout;
     }
 
@@ -665,8 +598,8 @@
 
     *blocks = st.st_size / BLOCKSIZE;
 
-    if (verify && VerifyBlocks(id, *buffer, *blocks, 1) != 0) {
-        fprintf(stderr, "unexpected contents in %s\n", fn);
+    if (verify && VerifyBlocks(id, *buffer, *blocks, true) != 0) {
+        fprintf(stderr, "unexpected contents in %s\n", fn.c_str());
         DeleteFile(fn, NULL);
         goto lsout;
     }
@@ -678,23 +611,21 @@
         close(fd);
     }
 
-    if (fn) {
-        free(fn);
-    }
-
     return rc;
 }
 
-static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer,
-        int checkspace, int *exists) {
-    char *fn = NULL;
-    char *cn = NULL;
+static int WriteStash(const std::string& base, const char* id, int blocks,
+        uint8_t* buffer, bool checkspace, int *exists) {
+    std::string fn;
+    std::string cn;
+    std::string dname;
     int fd = -1;
     int rc = -1;
+    int dfd = -1;
     int res;
     struct stat st;
 
-    if (base == NULL || buffer == NULL) {
+    if (base.empty() || buffer == NULL) {
         goto wsout;
     }
 
@@ -703,21 +634,17 @@
         goto wsout;
     }
 
-    fn = GetStashFileName(base, id, ".partial");
-    cn = GetStashFileName(base, id, NULL);
-
-    if (fn == NULL || cn == NULL) {
-        goto wsout;
-    }
+    fn = GetStashFileName(base, std::string(id), ".partial");
+    cn = GetStashFileName(base, std::string(id), "");
 
     if (exists) {
-        res = stat(cn, &st);
+        res = stat(cn.c_str(), &st);
 
         if (res == 0) {
             // The file already exists and since the name is the hash of the contents,
             // it's safe to assume the contents are identical (accidental hash collisions
             // are unlikely)
-            fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn);
+            fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str());
             *exists = 1;
             rc = 0;
             goto wsout;
@@ -726,12 +653,12 @@
         *exists = 0;
     }
 
-    fprintf(stderr, " writing %d blocks to %s\n", blocks, cn);
+    fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str());
 
-    fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, STASH_FILE_MODE));
+    fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
 
     if (fd == -1) {
-        fprintf(stderr, "failed to create \"%s\": %s\n", fn, strerror(errno));
+        fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno));
         goto wsout;
     }
 
@@ -740,12 +667,26 @@
     }
 
     if (fsync(fd) == -1) {
-        fprintf(stderr, "fsync \"%s\" failed: %s\n", fn, strerror(errno));
+        fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
         goto wsout;
     }
 
-    if (rename(fn, cn) == -1) {
-        fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn, cn, strerror(errno));
+    if (rename(fn.c_str(), cn.c_str()) == -1) {
+        fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(),
+                strerror(errno));
+        goto wsout;
+    }
+
+    dname = GetStashFileName(base, "", "");
+    dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY));
+
+    if (dfd == -1) {
+        fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
+        goto wsout;
+    }
+
+    if (fsync(dfd) == -1) {
+        fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
         goto wsout;
     }
 
@@ -756,12 +697,8 @@
         close(fd);
     }
 
-    if (fn) {
-        free(fn);
-    }
-
-    if (cn) {
-        free(cn);
+    if (dfd != -1) {
+        close(dfd);
     }
 
     return rc;
@@ -771,103 +708,79 @@
 // hash enough space for the expected amount of blocks we need to store. Returns
 // >0 if we created the directory, zero if it existed already, and <0 of failure.
 
-static int CreateStash(State* state, int maxblocks, const char* blockdev, char** base) {
-    char* dirname = NULL;
-    const uint8_t* digest;
-    int rc = -1;
-    int res;
-    int size = 0;
-    SHA_CTX ctx;
-    struct stat st;
-
-    if (blockdev == NULL || base == NULL) {
-        goto csout;
+static int CreateStash(State* state, int maxblocks, const char* blockdev,
+        std::string& base) {
+    if (blockdev == NULL) {
+        return -1;
     }
 
     // Stash directory should be different for each partition to avoid conflicts
     // when updating multiple partitions at the same time, so we use the hash of
     // the block device name as the base directory
+    SHA_CTX ctx;
     SHA_init(&ctx);
     SHA_update(&ctx, blockdev, strlen(blockdev));
-    digest = SHA_final(&ctx);
-    *base = PrintSha1(digest);
+    const uint8_t* digest = SHA_final(&ctx);
+    base = print_sha1(digest);
 
-    if (*base == NULL) {
-        goto csout;
-    }
-
-    dirname = GetStashFileName(*base, NULL, NULL);
-
-    if (dirname == NULL) {
-        goto csout;
-    }
-
-    res = stat(dirname, &st);
+    std::string dirname = GetStashFileName(base, "", "");
+    struct stat st;
+    int res = stat(dirname.c_str(), &st);
 
     if (res == -1 && errno != ENOENT) {
-        ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname, strerror(errno));
-        goto csout;
+        ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
+        return -1;
     } else if (res != 0) {
-        fprintf(stderr, "creating stash %s\n", dirname);
-        res = mkdir(dirname, STASH_DIRECTORY_MODE);
+        fprintf(stderr, "creating stash %s\n", dirname.c_str());
+        res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
 
         if (res != 0) {
-            ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname, strerror(errno));
-            goto csout;
+            ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
+            return -1;
         }
 
         if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
             ErrorAbort(state, "not enough space for stash\n");
-            goto csout;
+            return -1;
         }
 
-        rc = 1; // Created directory
-        goto csout;
+        return 1;  // Created directory
     }
 
-    fprintf(stderr, "using existing stash %s\n", dirname);
+    fprintf(stderr, "using existing stash %s\n", dirname.c_str());
 
     // If the directory already exists, calculate the space already allocated to
     // stash files and check if there's enough for all required blocks. Delete any
     // partially completed stash files first.
 
     EnumerateStash(dirname, DeletePartial, NULL);
+    int size = 0;
     EnumerateStash(dirname, UpdateFileSize, &size);
 
     size = (maxblocks * BLOCKSIZE) - size;
 
     if (size > 0 && CacheSizeCheck(size) != 0) {
         ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
-        goto csout;
-    }
-
-    rc = 0; // Using existing directory
-
-csout:
-    if (dirname) {
-        free(dirname);
-    }
-
-    return rc;
-}
-
-static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t* buffer_alloc,
-                      int fd, int usehash, int* isunresumable) {
-    char *id = NULL;
-    int blocks = 0;
-
-    if (!wordsave || !buffer || !buffer_alloc || !isunresumable) {
         return -1;
     }
 
-    id = strtok_r(NULL, " ", wordsave);
+    return 0; // Using existing directory
+}
 
+static int SaveStash(const std::string& base, char** wordsave, uint8_t** buffer,
+                     size_t* buffer_alloc, int fd, bool usehash) {
+    if (!wordsave || !buffer || !buffer_alloc) {
+        return -1;
+    }
+
+    char *id = strtok_r(NULL, " ", wordsave);
     if (id == NULL) {
         fprintf(stderr, "missing id field in stash command\n");
         return -1;
     }
 
-    if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, 0) == 0) {
+    int blocks = 0;
+    if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, false) == 0) {
         // Stash file already exists and has expected contents. Do not
         // read from source again, as the source may have been already
         // overwritten during a previous attempt.
@@ -878,7 +791,7 @@
         return -1;
     }
 
-    if (usehash && VerifyBlocks(id, *buffer, blocks, 1) != 0) {
+    if (usehash && VerifyBlocks(id, *buffer, blocks, true) != 0) {
         // Source blocks have unexpected contents. If we actually need this
         // data later, this is an unrecoverable error. However, the command
         // that uses the data may have already completed previously, so the
@@ -888,24 +801,17 @@
     }
 
     fprintf(stderr, "stashing %d blocks to %s\n", blocks, id);
-    return WriteStash(base, id, blocks, *buffer, 0, NULL);
+    return WriteStash(base, id, blocks, *buffer, false, NULL);
 }
 
-static int FreeStash(const char* base, const char* id) {
-    char *fn = NULL;
-
-    if (base == NULL || id == NULL) {
+static int FreeStash(const std::string& base, const char* id) {
+    if (base.empty() || id == NULL) {
         return -1;
     }
 
-    fn = GetStashFileName(base, id, NULL);
-
-    if (fn == NULL) {
-        return -1;
-    }
+    std::string fn = GetStashFileName(base, std::string(id), "");
 
     DeleteFile(fn, NULL);
-    free(fn);
 
     return 0;
 }
@@ -944,8 +850,8 @@
 // target RangeSet.  Any stashes required are loaded using LoadStash.
 
 static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
-                               uint8_t** buffer, size_t* buffer_alloc, int fd,
-                               const char* stashbase, int* overlap) {
+                              uint8_t** buffer, size_t* buffer_alloc, int fd,
+                              const std::string& stashbase, bool* overlap) {
     char* word;
     char* colonsave;
     char* colon;
@@ -972,7 +878,7 @@
         res = ReadBlocks(src, *buffer, fd);
 
         if (overlap && tgt) {
-            *overlap = range_overlaps(src, *tgt);
+            *overlap = range_overlaps(*src, **tgt);
         }
 
         free(src);
@@ -999,7 +905,7 @@
         colonsave = NULL;
         colon = strtok_r(word, ":", &colonsave);
 
-        res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, 1);
+        res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, true);
 
         if (res == -1) {
             // These source blocks will fail verification if used later, but we
@@ -1027,12 +933,12 @@
     char* cmdname;
     char* cpos;
     char* freestash;
-    char* stashbase;
-    int canwrite;
+    std::string stashbase;
+    bool canwrite;
     int createdstash;
     int fd;
     int foundwrites;
-    int isunresumable;
+    bool isunresumable;
     int version;
     int written;
     NewThreadInfo nti;
@@ -1060,7 +966,7 @@
 // can be performed.
 
 static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks,
-                              int onehash, int* overlap) {
+                              int onehash, bool* overlap) {
     char* srchash = NULL;
     char* tgthash = NULL;
     int stash_exists = 0;
@@ -1105,20 +1011,20 @@
         goto v3out;
     }
 
-    if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, 0) == 0) {
+    if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, false) == 0) {
         // Target blocks already have expected content, command should be skipped
         rc = 1;
         goto v3out;
     }
 
-    if (VerifyBlocks(srchash, params->buffer, *src_blocks, 1) == 0) {
+    if (VerifyBlocks(srchash, params->buffer, *src_blocks, true) == 0) {
         // If source and target blocks overlap, stash the source blocks so we can
         // resume from possible write errors
         if (*overlap) {
             fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks,
                 srchash);
 
-            if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1,
+            if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, true,
                     &stash_exists) != 0) {
                 fprintf(stderr, "failed to stash overlapping source blocks\n");
                 goto v3out;
@@ -1136,7 +1042,7 @@
     }
 
     if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, &params->buffer,
-                        &params->bufsize, 1) == 0) {
+                        &params->bufsize, true) == 0) {
         // Overlapping source blocks were previously stashed, command can proceed.
         // We are recovering from an interrupted command, so we don't know if the
         // stash can safely be deleted after this command.
@@ -1146,7 +1052,7 @@
 
     // Valid source data not available, update cannot be resumed
     fprintf(stderr, "partition has unexpected contents\n");
-    params->isunresumable = 1;
+    params->isunresumable = true;
 
 v3out:
     if (tgtbuffer) {
@@ -1158,7 +1064,7 @@
 
 static int PerformCommandMove(CommandParameters* params) {
     int blocks = 0;
-    int overlap = 0;
+    bool overlap = false;
     int rc = -1;
     int status = 0;
     RangeSet* tgt = NULL;
@@ -1223,7 +1129,7 @@
     }
 
     return SaveStash(params->stashbase, &params->cpos, &params->buffer, &params->bufsize,
-                params->fd, (params->version >= 3), &params->isunresumable);
+                params->fd, (params->version >= 3));
 }
 
 static int PerformCommandFree(CommandParameters* params) {
@@ -1240,8 +1146,6 @@
 
 static int PerformCommandZero(CommandParameters* params) {
     char* range = NULL;
-    int i;
-    int j;
     int rc = -1;
     RangeSet* tgt = NULL;
 
@@ -1264,12 +1168,12 @@
     memset(params->buffer, 0, BLOCKSIZE);
 
     if (params->canwrite) {
-        for (i = 0; i < tgt->count; ++i) {
+        for (size_t i = 0; i < tgt->count; ++i) {
             if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
                 goto pczout;
             }
 
-            for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
+            for (size_t j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
                 if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) {
                     goto pczout;
                 }
@@ -1349,7 +1253,7 @@
     char* logparams = NULL;
     char* value = NULL;
     int blocks = 0;
-    int overlap = 0;
+    bool overlap = false;
     int rc = -1;
     int status = 0;
     RangeSet* tgt = NULL;
@@ -1459,7 +1363,6 @@
 
 static int PerformCommandErase(CommandParameters* params) {
     char* range = NULL;
-    int i;
     int rc = -1;
     RangeSet* tgt = NULL;
     struct stat st;
@@ -1495,7 +1398,7 @@
     if (params->canwrite) {
         fprintf(stderr, " erasing %d blocks\n", tgt->size);
 
-        for (i = 0; i < tgt->count; ++i) {
+        for (size_t i = 0; i < tgt->count; ++i) {
             // offset in bytes
             blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE;
             // length in bytes
@@ -1555,7 +1458,7 @@
 //    - patch stream (filename within package.zip, must be uncompressed)
 
 static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[],
-            const Command* commands, int cmdcount, int dryrun) {
+            const Command* commands, int cmdcount, bool dryrun) {
 
     char* line = NULL;
     char* linesave = NULL;
@@ -1710,8 +1613,7 @@
         }
 
         if (stash_max_blocks >= 0) {
-            res = CreateStash(state, stash_max_blocks, blockdev_filename->data,
-                    &params.stashbase);
+            res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase);
 
             if (res == -1) {
                 goto pbiudone;
@@ -1762,6 +1664,10 @@
         }
 
         if (params.canwrite) {
+            if (fsync(params.fd) == -1) {
+                fprintf(stderr, "fsync failed: %s\n", strerror(errno));
+                goto pbiudone;
+            }
             fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
             fflush(cmd_pipe);
         }
@@ -1828,10 +1734,6 @@
         DeleteStash(params.stashbase);
     }
 
-    if (params.stashbase) {
-        free(params.stashbase);
-    }
-
     return StringValue(rc == 0 ? strdup("t") : strdup(""));
 }
 
@@ -1903,7 +1805,7 @@
 
     // Perform a dry run without writing to test if an update can proceed
     return PerformBlockImageUpdate(name, state, argc, argv, commands,
-                sizeof(commands) / sizeof(commands[0]), 1);
+                sizeof(commands) / sizeof(commands[0]), true);
 }
 
 Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1919,7 +1821,7 @@
     };
 
     return PerformBlockImageUpdate(name, state, argc, argv, commands,
-                sizeof(commands) / sizeof(commands[0]), 0);
+                sizeof(commands) / sizeof(commands[0]), false);
 }
 
 Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1953,15 +1855,14 @@
     SHA_CTX ctx;
     SHA_init(&ctx);
 
-    int i, j;
-    for (i = 0; i < rs->count; ++i) {
+    for (size_t i = 0; i < rs->count; ++i) {
         if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) {
             ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
                 strerror(errno));
             goto done;
         }
 
-        for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
+        for (size_t j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
             if (read_all(fd, buffer, BLOCKSIZE) == -1) {
                 ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
                     strerror(errno));
@@ -1980,7 +1881,7 @@
     if (digest == NULL) {
         return StringValue(strdup(""));
     } else {
-        return StringValue(PrintSha1(digest));
+        return StringValue(strdup(print_sha1(digest).c_str()));
     }
 }
 
diff --git a/wear_ui.cpp b/wear_ui.cpp
new file mode 100644
index 0000000..4ae42c4
--- /dev/null
+++ b/wear_ui.cpp
@@ -0,0 +1,650 @@
+/*
+ * Copyright (C) 2014 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 <errno.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <vector>
+
+#include "common.h"
+#include "device.h"
+#include "minui/minui.h"
+#include "wear_ui.h"
+#include "ui.h"
+#include "cutils/properties.h"
+#include "base/strings.h"
+
+static int char_width;
+static int char_height;
+
+// There's only (at most) one of these objects, and global callbacks
+// (for pthread_create, and the input event system) need to find it,
+// so use a global variable.
+static WearRecoveryUI* self = NULL;
+
+// Return the current time as a double (including fractions of a second).
+static double now() {
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    return tv.tv_sec + tv.tv_usec / 1000000.0;
+}
+
+WearRecoveryUI::WearRecoveryUI() :
+    progress_bar_height(3),
+    progress_bar_width(200),
+    progress_bar_y(259),
+    outer_height(0),
+    outer_width(0),
+    menu_unusable_rows(0),
+    intro_frames(22),
+    loop_frames(60),
+    currentIcon(NONE),
+    intro_done(false),
+    current_frame(0),
+    animation_fps(30),
+    rtl_locale(false),
+    progressBarType(EMPTY),
+    progressScopeStart(0),
+    progressScopeSize(0),
+    progress(0),
+    text_cols(0),
+    text_rows(0),
+    text_col(0),
+    text_row(0),
+    text_top(0),
+    show_text(false),
+    show_text_ever(false),
+    show_menu(false),
+    menu_items(0),
+    menu_sel(0) {
+
+    for (size_t i = 0; i < 5; i++)
+        backgroundIcon[i] = NULL;
+
+    pthread_mutex_init(&updateMutex, NULL);
+    self = this;
+}
+
+// Draw background frame on the screen.  Does not flip pages.
+// Should only be called with updateMutex locked.
+void WearRecoveryUI::draw_background_locked(Icon icon)
+{
+    gr_color(0, 0, 0, 255);
+    gr_fill(0, 0, gr_fb_width(), gr_fb_height());
+
+    if (icon) {
+        GRSurface* surface;
+        if (icon == INSTALLING_UPDATE || icon == ERASING) {
+            if (!intro_done) {
+                surface = introFrames[current_frame];
+            } else {
+                surface = loopFrames[current_frame];
+            }
+        }
+        else {
+            surface = backgroundIcon[icon];
+        }
+
+        int width = gr_get_width(surface);
+        int height = gr_get_height(surface);
+
+        int x = (gr_fb_width() - width) / 2;
+        int y = (gr_fb_height() - height) / 2;
+
+        gr_blit(surface, 0, 0, width, height, x, y);
+    }
+}
+
+// Draw the progress bar (if any) on the screen.  Does not flip pages.
+// Should only be called with updateMutex locked.
+void WearRecoveryUI::draw_progress_locked()
+{
+    if (currentIcon == ERROR) return;
+    if (progressBarType != DETERMINATE) return;
+
+    int width = progress_bar_width;
+    int height = progress_bar_height;
+    int dx = (gr_fb_width() - width)/2;
+    int dy = progress_bar_y;
+
+    float p = progressScopeStart + progress * progressScopeSize;
+    int pos = (int) (p * width);
+
+    gr_color(0x43, 0x43, 0x43, 0xff);
+    gr_fill(dx, dy, dx + width, dy + height);
+
+    if (pos > 0) {
+        gr_color(0x02, 0xa8, 0xf3, 255);
+        if (rtl_locale) {
+            // Fill the progress bar from right to left.
+            gr_fill(dx + width - pos, dy, dx + width, dy + height);
+        } else {
+            // Fill the progress bar from left to right.
+            gr_fill(dx, dy, dx + pos, dy + height);
+        }
+    }
+}
+
+void WearRecoveryUI::SetColor(UIElement e) {
+    switch (e) {
+        case HEADER:
+            gr_color(247, 0, 6, 255);
+            break;
+        case MENU:
+        case MENU_SEL_BG:
+            gr_color(0, 106, 157, 255);
+            break;
+        case MENU_SEL_FG:
+            gr_color(255, 255, 255, 255);
+            break;
+        case LOG:
+            gr_color(249, 194, 0, 255);
+            break;
+        case TEXT_FILL:
+            gr_color(0, 0, 0, 160);
+            break;
+        default:
+            gr_color(255, 255, 255, 255);
+            break;
+    }
+}
+
+void WearRecoveryUI::DrawTextLine(int x, int* y, const char* line, bool bold) {
+    gr_text(x, *y, line, bold);
+    *y += char_height + 4;
+}
+
+void WearRecoveryUI::DrawTextLines(int x, int* y, const char* const* lines) {
+    for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) {
+        DrawTextLine(x, y, lines[i], false);
+    }
+}
+
+static const char* HEADERS[] = {
+    "Swipe up/down to move.",
+    "Swipe left/right to select.",
+    "",
+    NULL
+};
+
+void WearRecoveryUI::draw_screen_locked()
+{
+    draw_background_locked(currentIcon);
+    draw_progress_locked();
+    char cur_selection_str[50];
+
+    if (show_text) {
+        SetColor(TEXT_FILL);
+        gr_fill(0, 0, gr_fb_width(), gr_fb_height());
+
+        int y = outer_height;
+        int x = outer_width;
+        if (show_menu) {
+            char recovery_fingerprint[PROPERTY_VALUE_MAX];
+            property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, "");
+            SetColor(HEADER);
+            DrawTextLine(x + 4, &y, "Android Recovery", true);
+            for (auto& chunk: android::base::Split(recovery_fingerprint, ":")) {
+                DrawTextLine(x +4, &y, chunk.c_str(), false);
+            }
+
+            // This is actually the help strings.
+            DrawTextLines(x + 4, &y, HEADERS);
+            SetColor(HEADER);
+            DrawTextLines(x + 4, &y, menu_headers_);
+
+            // Show the current menu item number in relation to total number if
+            // items don't fit on the screen.
+            if (menu_items > menu_end - menu_start) {
+                sprintf(cur_selection_str, "Current item: %d/%d", menu_sel + 1, menu_items);
+                gr_text(x+4, y, cur_selection_str, 1);
+                y += char_height+4;
+            }
+
+            // Menu begins here
+            SetColor(MENU);
+
+            for (int i = menu_start; i < menu_end; ++i) {
+
+                if (i == menu_sel) {
+                    // draw the highlight bar
+                    SetColor(MENU_SEL_BG);
+                    gr_fill(x, y-2, gr_fb_width()-x, y+char_height+2);
+                    // white text of selected item
+                    SetColor(MENU_SEL_FG);
+                    if (menu[i][0]) gr_text(x+4, y, menu[i], 1);
+                    SetColor(MENU);
+                } else {
+                    if (menu[i][0]) gr_text(x+4, y, menu[i], 0);
+                }
+                y += char_height+4;
+            }
+            SetColor(MENU);
+            y += 4;
+            gr_fill(0, y, gr_fb_width(), y+2);
+            y += 4;
+        }
+
+        SetColor(LOG);
+
+        // display from the bottom up, until we hit the top of the
+        // screen, the bottom of the menu, or we've displayed the
+        // entire text buffer.
+        int ty;
+        int row = (text_top+text_rows-1) % text_rows;
+        size_t count = 0;
+        for (int ty = gr_fb_height() - char_height - outer_height;
+             ty > y+2 && count < text_rows;
+             ty -= char_height, ++count) {
+            gr_text(x+4, ty, text[row], 0);
+            --row;
+            if (row < 0) row = text_rows-1;
+        }
+    }
+}
+
+void WearRecoveryUI::update_screen_locked()
+{
+    draw_screen_locked();
+    gr_flip();
+}
+
+// Keeps the progress bar updated, even when the process is otherwise busy.
+void* WearRecoveryUI::progress_thread(void *cookie) {
+    self->progress_loop();
+    return NULL;
+}
+
+void WearRecoveryUI::progress_loop() {
+    double interval = 1.0 / animation_fps;
+    for (;;) {
+        double start = now();
+        pthread_mutex_lock(&updateMutex);
+        int redraw = 0;
+
+        if ((currentIcon == INSTALLING_UPDATE || currentIcon == ERASING)
+                                                            && !show_text) {
+            if (!intro_done) {
+                if (current_frame == intro_frames - 1) {
+                    intro_done = true;
+                    current_frame = 0;
+                } else {
+                    current_frame++;
+                }
+            } else {
+                current_frame = (current_frame + 1) % loop_frames;
+            }
+            redraw = 1;
+        }
+
+        // move the progress bar forward on timed intervals, if configured
+        int duration = progressScopeDuration;
+        if (progressBarType == DETERMINATE && duration > 0) {
+            double elapsed = now() - progressScopeTime;
+            float p = 1.0 * elapsed / duration;
+            if (p > 1.0) p = 1.0;
+            if (p > progress) {
+                progress = p;
+                redraw = 1;
+            }
+        }
+
+        if (redraw)
+            update_screen_locked();
+
+        pthread_mutex_unlock(&updateMutex);
+        double end = now();
+        // minimum of 20ms delay between frames
+        double delay = interval - (end-start);
+        if (delay < 0.02) delay = 0.02;
+        usleep((long)(delay * 1000000));
+    }
+}
+
+void WearRecoveryUI::LoadBitmap(const char* filename, GRSurface** surface) {
+    int result = res_create_display_surface(filename, surface);
+    if (result < 0) {
+        LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
+    }
+}
+
+void WearRecoveryUI::Init()
+{
+    gr_init();
+
+    gr_font_size(&char_width, &char_height);
+
+    text_col = text_row = 0;
+    text_rows = (gr_fb_height()) / char_height;
+    visible_text_rows = (gr_fb_height() - (outer_height * 2)) / char_height;
+    if (text_rows > kMaxRows) text_rows = kMaxRows;
+    text_top = 1;
+
+    text_cols = (gr_fb_width() - (outer_width * 2)) / char_width;
+    if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
+
+    LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]);
+    backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
+    LoadBitmap("icon_error", &backgroundIcon[ERROR]);
+    backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
+
+    introFrames = (GRSurface**)malloc(intro_frames * sizeof(GRSurface*));
+    for (int i = 0; i < intro_frames; ++i) {
+        char filename[40];
+        sprintf(filename, "intro%02d", i);
+        LoadBitmap(filename, introFrames + i);
+    }
+
+    loopFrames = (GRSurface**)malloc(loop_frames * sizeof(GRSurface*));
+    for (int i = 0; i < loop_frames; ++i) {
+        char filename[40];
+        sprintf(filename, "loop%02d", i);
+        LoadBitmap(filename, loopFrames + i);
+    }
+
+    pthread_create(&progress_t, NULL, progress_thread, NULL);
+    RecoveryUI::Init();
+}
+
+void WearRecoveryUI::SetLocale(const char* locale) {
+    if (locale) {
+        char* lang = strdup(locale);
+        for (char* p = lang; *p; ++p) {
+            if (*p == '_') {
+                *p = '\0';
+                break;
+            }
+        }
+
+        // A bit cheesy: keep an explicit list of supported languages
+        // that are RTL.
+        if (strcmp(lang, "ar") == 0 ||   // Arabic
+            strcmp(lang, "fa") == 0 ||   // Persian (Farsi)
+            strcmp(lang, "he") == 0 ||   // Hebrew (new language code)
+            strcmp(lang, "iw") == 0 ||   // Hebrew (old language code)
+            strcmp(lang, "ur") == 0) {   // Urdu
+            rtl_locale = true;
+        }
+        free(lang);
+    }
+}
+
+void WearRecoveryUI::SetBackground(Icon icon)
+{
+    pthread_mutex_lock(&updateMutex);
+    currentIcon = icon;
+    update_screen_locked();
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void WearRecoveryUI::SetProgressType(ProgressType type)
+{
+    pthread_mutex_lock(&updateMutex);
+    if (progressBarType != type) {
+        progressBarType = type;
+    }
+    progressScopeStart = 0;
+    progressScopeSize = 0;
+    progress = 0;
+    update_screen_locked();
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void WearRecoveryUI::ShowProgress(float portion, float seconds)
+{
+    pthread_mutex_lock(&updateMutex);
+    progressBarType = DETERMINATE;
+    progressScopeStart += progressScopeSize;
+    progressScopeSize = portion;
+    progressScopeTime = now();
+    progressScopeDuration = seconds;
+    progress = 0;
+    update_screen_locked();
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void WearRecoveryUI::SetProgress(float fraction)
+{
+    pthread_mutex_lock(&updateMutex);
+    if (fraction < 0.0) fraction = 0.0;
+    if (fraction > 1.0) fraction = 1.0;
+    if (progressBarType == DETERMINATE && fraction > progress) {
+        // Skip updates that aren't visibly different.
+        int width = progress_bar_width;
+        float scale = width * progressScopeSize;
+        if ((int) (progress * scale) != (int) (fraction * scale)) {
+            progress = fraction;
+            update_screen_locked();
+        }
+    }
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void WearRecoveryUI::SetStage(int current, int max)
+{
+}
+
+void WearRecoveryUI::Print(const char *fmt, ...)
+{
+    char buf[256];
+    va_list ap;
+    va_start(ap, fmt);
+    vsnprintf(buf, 256, fmt, ap);
+    va_end(ap);
+
+    fputs(buf, stdout);
+
+    // This can get called before ui_init(), so be careful.
+    pthread_mutex_lock(&updateMutex);
+    if (text_rows > 0 && text_cols > 0) {
+        char *ptr;
+        for (ptr = buf; *ptr != '\0'; ++ptr) {
+            if (*ptr == '\n' || text_col >= text_cols) {
+                text[text_row][text_col] = '\0';
+                text_col = 0;
+                text_row = (text_row + 1) % text_rows;
+                if (text_row == text_top) text_top = (text_top + 1) % text_rows;
+            }
+            if (*ptr != '\n') text[text_row][text_col++] = *ptr;
+        }
+        text[text_row][text_col] = '\0';
+        update_screen_locked();
+    }
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void WearRecoveryUI::StartMenu(const char* const * headers, const char* const * items,
+                                 int initial_selection) {
+    pthread_mutex_lock(&updateMutex);
+    if (text_rows > 0 && text_cols > 0) {
+        menu_headers_ = headers;
+        size_t i = 0;
+        for (; i < text_rows && items[i] != nullptr; i++) {
+            strncpy(menu[i], items[i], text_cols - 1);
+            menu[i][text_cols - 1] = '\0';
+        }
+        menu_items = i;
+        show_menu = 1;
+        menu_sel = initial_selection;
+        menu_start = 0;
+        menu_end = visible_text_rows - 1 - menu_unusable_rows;
+        if (menu_items <= menu_end)
+          menu_end = menu_items;
+        update_screen_locked();
+    }
+    pthread_mutex_unlock(&updateMutex);
+}
+
+int WearRecoveryUI::SelectMenu(int sel) {
+    int old_sel;
+    pthread_mutex_lock(&updateMutex);
+    if (show_menu > 0) {
+        old_sel = menu_sel;
+        menu_sel = sel;
+        if (menu_sel < 0) menu_sel = 0;
+        if (menu_sel >= menu_items) menu_sel = menu_items-1;
+        if (menu_sel < menu_start) {
+          menu_start--;
+          menu_end--;
+        } else if (menu_sel >= menu_end && menu_sel < menu_items) {
+          menu_end++;
+          menu_start++;
+        }
+        sel = menu_sel;
+        if (menu_sel != old_sel) update_screen_locked();
+    }
+    pthread_mutex_unlock(&updateMutex);
+    return sel;
+}
+
+void WearRecoveryUI::EndMenu() {
+    int i;
+    pthread_mutex_lock(&updateMutex);
+    if (show_menu > 0 && text_rows > 0 && text_cols > 0) {
+        show_menu = 0;
+        update_screen_locked();
+    }
+    pthread_mutex_unlock(&updateMutex);
+}
+
+bool WearRecoveryUI::IsTextVisible()
+{
+    pthread_mutex_lock(&updateMutex);
+    int visible = show_text;
+    pthread_mutex_unlock(&updateMutex);
+    return visible;
+}
+
+bool WearRecoveryUI::WasTextEverVisible()
+{
+    pthread_mutex_lock(&updateMutex);
+    int ever_visible = show_text_ever;
+    pthread_mutex_unlock(&updateMutex);
+    return ever_visible;
+}
+
+void WearRecoveryUI::ShowText(bool visible)
+{
+    pthread_mutex_lock(&updateMutex);
+    // Don't show text during ota install or factory reset
+    if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) {
+        pthread_mutex_unlock(&updateMutex);
+        return;
+    }
+    show_text = visible;
+    if (show_text) show_text_ever = 1;
+    update_screen_locked();
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void WearRecoveryUI::Redraw()
+{
+    pthread_mutex_lock(&updateMutex);
+    update_screen_locked();
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void WearRecoveryUI::ShowFile(FILE* fp) {
+    std::vector<long> offsets;
+    offsets.push_back(ftell(fp));
+    ClearText();
+
+    struct stat sb;
+    fstat(fileno(fp), &sb);
+
+    bool show_prompt = false;
+    while (true) {
+        if (show_prompt) {
+            Print("--(%d%% of %d bytes)--",
+                  static_cast<int>(100 * (double(ftell(fp)) / double(sb.st_size))),
+                  static_cast<int>(sb.st_size));
+            Redraw();
+            while (show_prompt) {
+                show_prompt = false;
+                int key = WaitKey();
+                if (key == KEY_POWER || key == KEY_ENTER) {
+                    return;
+                } else if (key == KEY_UP || key == KEY_VOLUMEUP) {
+                    if (offsets.size() <= 1) {
+                        show_prompt = true;
+                    } else {
+                        offsets.pop_back();
+                        fseek(fp, offsets.back(), SEEK_SET);
+                    }
+                } else {
+                    if (feof(fp)) {
+                        return;
+                    }
+                    offsets.push_back(ftell(fp));
+                }
+            }
+            ClearText();
+        }
+
+        int ch = getc(fp);
+        if (ch == EOF) {
+            text_row = text_top = text_rows - 2;
+            show_prompt = true;
+        } else {
+            PutChar(ch);
+            if (text_col == 0 && text_row >= text_rows - 2) {
+                text_top = text_row;
+                show_prompt = true;
+            }
+        }
+    }
+}
+
+void WearRecoveryUI::PutChar(char ch) {
+    pthread_mutex_lock(&updateMutex);
+    if (ch != '\n') text[text_row][text_col++] = ch;
+    if (ch == '\n' || text_col >= text_cols) {
+        text_col = 0;
+        ++text_row;
+    }
+    pthread_mutex_unlock(&updateMutex);
+}
+
+void WearRecoveryUI::ShowFile(const char* filename) {
+    FILE* fp = fopen_path(filename, "re");
+    if (fp == nullptr) {
+        Print("  Unable to open %s: %s\n", filename, strerror(errno));
+        return;
+    }
+    ShowFile(fp);
+    fclose(fp);
+}
+
+void WearRecoveryUI::ClearText() {
+    pthread_mutex_lock(&updateMutex);
+    text_col = 0;
+    text_row = 0;
+    text_top = 1;
+    for (size_t i = 0; i < text_rows; ++i) {
+        memset(text[i], 0, text_cols + 1);
+    }
+    pthread_mutex_unlock(&updateMutex);
+}
diff --git a/wear_ui.h b/wear_ui.h
new file mode 100644
index 0000000..839a264
--- /dev/null
+++ b/wear_ui.h
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef RECOVERY_WEAR_UI_H
+#define RECOVERY_WEAR_UI_H
+
+#include <pthread.h>
+#include <stdio.h>
+
+#include "ui.h"
+#include "minui/minui.h"
+
+class WearRecoveryUI : public RecoveryUI {
+  public:
+    WearRecoveryUI();
+
+    void Init();
+    void SetLocale(const char* locale);
+
+    // overall recovery state ("background image")
+    void SetBackground(Icon icon);
+
+    // progress indicator
+    void SetProgressType(ProgressType type);
+    void ShowProgress(float portion, float seconds);
+    void SetProgress(float fraction);
+
+    void SetStage(int current, int max);
+
+    // text log
+    void ShowText(bool visible);
+    bool IsTextVisible();
+    bool WasTextEverVisible();
+
+    // printing messages
+    void Print(const char* fmt, ...);
+    void ShowFile(const char* filename);
+    void ShowFile(FILE* fp);
+
+    // menu display
+    void StartMenu(const char* const * headers, const char* const * items,
+                           int initial_selection);
+    int SelectMenu(int sel);
+    void EndMenu();
+
+    void Redraw();
+
+    enum UIElement { HEADER, MENU, MENU_SEL_BG, MENU_SEL_FG, LOG, TEXT_FILL };
+    virtual void SetColor(UIElement e);
+
+  protected:
+    int progress_bar_height, progress_bar_width;
+
+    // progress bar vertical position, it's centered horizontally
+    int progress_bar_y;
+
+    // outer of window
+    int outer_height, outer_width;
+
+    // Unusable rows when displaying the recovery menu, including the lines
+    // for headers (Android Recovery, build id and etc) and the bottom lines
+    // that may otherwise go out of the screen.
+    int menu_unusable_rows;
+
+    // number of intro frames (default: 22) and loop frames (default: 60)
+    int intro_frames;
+    int loop_frames;
+
+  private:
+    Icon currentIcon;
+
+    bool intro_done;
+
+    int current_frame;
+
+    int animation_fps;
+
+    bool rtl_locale;
+
+    pthread_mutex_t updateMutex;
+    GRSurface* backgroundIcon[5];
+    GRSurface* *introFrames;
+    GRSurface* *loopFrames;
+
+    ProgressType progressBarType;
+
+    float progressScopeStart, progressScopeSize, progress;
+    double progressScopeTime, progressScopeDuration;
+
+    static const int kMaxCols = 96;
+    static const int kMaxRows = 96;
+
+    // Log text overlay, displayed when a magic key is pressed
+    char text[kMaxRows][kMaxCols];
+    size_t text_cols, text_rows;
+    // Number of text rows seen on screen
+    int visible_text_rows;
+    size_t text_col, text_row, text_top;
+    bool show_text;
+    bool show_text_ever;   // has show_text ever been true?
+
+    char menu[kMaxRows][kMaxCols];
+    bool show_menu;
+    const char* const* menu_headers_;
+    int menu_items, menu_sel;
+    int menu_start, menu_end;
+
+    pthread_t progress_t;
+
+  private:
+    void draw_background_locked(Icon icon);
+    void draw_progress_locked();
+    void draw_screen_locked();
+    void update_screen_locked();
+    static void* progress_thread(void* cookie);
+    void progress_loop();
+    void LoadBitmap(const char* filename, GRSurface** surface);
+    void PutChar(char);
+    void ClearText();
+    void DrawTextLine(int x, int* y, const char* line, bool bold);
+    void DrawTextLines(int x, int* y, const char* const* lines);
+};
+
+#endif  // RECOVERY_WEAR_UI_H