resolved conflicts for merge of 8f9b41d9 to master

Change-Id: I9af02b9bba1176174540c849f905dece8ade54d8
diff --git a/atrace/Android.mk b/atrace/Android.mk
new file mode 100644
index 0000000..c1a0d87
--- /dev/null
+++ b/atrace/Android.mk
@@ -0,0 +1,12 @@
+# Copyright 2012 The Android Open Source Project
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= atrace.c
+
+LOCAL_MODULE:= atrace
+
+LOCAL_MODULE_TAGS:= debug
+
+include $(BUILD_EXECUTABLE)
diff --git a/atrace/MODULE_LICENSE_APACHE2 b/atrace/MODULE_LICENSE_APACHE2
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/atrace/MODULE_LICENSE_APACHE2
diff --git a/atrace/NOTICE b/atrace/NOTICE
new file mode 100644
index 0000000..c77f135
--- /dev/null
+++ b/atrace/NOTICE
@@ -0,0 +1,190 @@
+
+   Copyright (c) 2012, 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.
+
+   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.
+
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
diff --git a/atrace/atrace.c b/atrace/atrace.c
new file mode 100644
index 0000000..a617432
--- /dev/null
+++ b/atrace/atrace.c
@@ -0,0 +1,370 @@
+/*
+ * Copyright (C) 2012 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 <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/sendfile.h>
+#include <time.h>
+
+/* Command line options */
+static int g_traceDurationSeconds = 5;
+static bool g_traceSchedSwitch = false;
+static bool g_traceCpuFrequency = false;
+static bool g_traceGovernorLoad = false;
+static bool g_traceWorkqueue = false;
+static bool g_traceOverwrite = false;
+static int g_traceBufferSizeKB = 2048;
+
+/* Global state */
+static bool g_traceAborted = false;
+
+/* Sys file paths */
+static const char* k_traceClockPath =
+    "/sys/kernel/debug/tracing/trace_clock";
+
+static const char* k_traceBufferSizePath =
+    "/sys/kernel/debug/tracing/buffer_size_kb";
+
+static const char* k_tracingOverwriteEnablePath =
+    "/sys/kernel/debug/tracing/options/overwrite";
+
+static const char* k_schedSwitchEnablePath =
+    "/sys/kernel/debug/tracing/events/sched/sched_switch/enable";
+
+static const char* k_cpuFreqEnablePath =
+    "/sys/kernel/debug/tracing/events/power/cpu_frequency/enable";
+
+static const char* k_governorLoadEnablePath =
+    "/sys/kernel/debug/tracing/events/cpufreq_interactive/enable";
+
+
+static const char* k_workqueueEnablePath =
+    "/sys/kernel/debug/tracing/events/workqueue/enable";
+
+static const char* k_tracingOnPath =
+    "/sys/kernel/debug/tracing/tracing_on";
+
+static const char* k_tracePath =
+    "/sys/kernel/debug/tracing/trace";
+
+static const char* k_traceMarkerPath =
+    "/sys/kernel/debug/tracing/trace_marker";
+
+// Write a string to a file, returning true if the write was successful.
+bool writeStr(const char* filename, const char* str)
+{
+    int fd = open(filename, O_WRONLY);
+    if (fd == -1) {
+        fprintf(stderr, "error opening %s: %s (%d)\n", filename,
+                strerror(errno), errno);
+        return false;
+    }
+
+    bool ok = true;
+    ssize_t len = strlen(str);
+    if (write(fd, str, len) != len) {
+        fprintf(stderr, "error writing to %s: %s (%d)\n", filename,
+                strerror(errno), errno);
+        ok = false;
+    }
+
+    close(fd);
+
+    return ok;
+}
+
+// Enable or disable a kernel option by writing a "1" or a "0" into a /sys file.
+static bool setKernelOptionEnable(const char* filename, bool enable)
+{
+    return writeStr(filename, enable ? "1" : "0");
+}
+
+// Enable or disable overwriting of the kernel trace buffers.  Disabling this
+// will cause tracing to stop once the trace buffers have filled up.
+static bool setTraceOverwriteEnable(bool enable)
+{
+    return setKernelOptionEnable(k_tracingOverwriteEnablePath, enable);
+}
+
+// Enable or disable tracing of the kernel scheduler switching.
+static bool setSchedSwitchTracingEnable(bool enable)
+{
+    return setKernelOptionEnable(k_schedSwitchEnablePath, enable);
+}
+
+// Enable or disable tracing of the CPU clock frequency.
+static bool setCpuFrequencyTracingEnable(bool enable)
+{
+    return setKernelOptionEnable(k_cpuFreqEnablePath, enable);
+}
+
+// Enable or disable tracing of the interactive CPU frequency governor's idea of
+// the CPU load.
+static bool setGovernorLoadTracingEnable(bool enable)
+{
+    return setKernelOptionEnable(k_governorLoadEnablePath, enable);
+}
+
+// Enable or disable tracing of the kernel workqueues.
+static bool setWorkqueueTracingEnabled(bool enable)
+{
+    return setKernelOptionEnable(k_workqueueEnablePath, enable);
+}
+
+// Enable or disable kernel tracing.
+static bool setTracingEnabled(bool enable)
+{
+    return setKernelOptionEnable(k_tracingOnPath, enable);
+}
+
+// Clear the contents of the kernel trace.
+static bool clearTrace()
+{
+    int traceFD = creat(k_tracePath, 0);
+    if (traceFD == -1) {
+        fprintf(stderr, "error truncating %s: %s (%d)\n", k_tracePath,
+                strerror(errno), errno);
+        return false;
+    }
+
+    close(traceFD);
+
+    return true;
+}
+
+// Set the size of the kernel's trace buffer in kilobytes.
+static bool setTraceBufferSizeKB(int size)
+{
+    char str[32] = "1";
+    int len;
+    if (size < 1) {
+        size = 1;
+    }
+    snprintf(str, 32, "%d", size);
+    return writeStr(k_traceBufferSizePath, str);
+}
+
+// Enable or disable the kernel's use of the global clock.  Disabling the global
+// clock will result in the kernel using a per-CPU local clock.
+static bool setGlobalClockEnable(bool enable)
+{
+    return writeStr(k_traceClockPath, enable ? "global" : "local");
+}
+
+// Enable tracing in the kernel.
+static bool startTrace()
+{
+    bool ok = true;
+
+    // Set up the tracing options.
+    ok &= setTraceOverwriteEnable(g_traceOverwrite);
+    ok &= setSchedSwitchTracingEnable(g_traceSchedSwitch);
+    ok &= setCpuFrequencyTracingEnable(g_traceCpuFrequency);
+    ok &= setGovernorLoadTracingEnable(g_traceGovernorLoad);
+    ok &= setWorkqueueTracingEnabled(g_traceWorkqueue);
+    ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
+    ok &= setGlobalClockEnable(true);
+
+    // Enable tracing.
+    ok &= setTracingEnabled(true);
+
+    if (!ok) {
+        fprintf(stderr, "error: unable to start trace\n");
+    }
+
+    return ok;
+}
+
+// Disable tracing in the kernel.
+static void stopTrace()
+{
+    // Disable tracing.
+    setTracingEnabled(false);
+
+    // Set the options back to their defaults.
+    setTraceOverwriteEnable(true);
+    setSchedSwitchTracingEnable(false);
+    setCpuFrequencyTracingEnable(false);
+    setGovernorLoadTracingEnable(false);
+    setWorkqueueTracingEnabled(false);
+    setGlobalClockEnable(false);
+
+    // Note that we can't reset the trace buffer size here because that would
+    // clear the trace before we've read it.
+}
+
+// Read the current kernel trace and write it to stdout.
+static void dumpTrace()
+{
+    int traceFD = open(k_tracePath, O_RDWR);
+    if (traceFD == -1) {
+        fprintf(stderr, "error opening %s: %s (%d)\n", k_tracePath,
+                strerror(errno), errno);
+        return;
+    }
+
+    ssize_t sent = 0;
+    while ((sent = sendfile(STDOUT_FILENO, traceFD, NULL, 64*1024*1024)) > 0);
+    if (sent == -1) {
+        fprintf(stderr, "error dumping trace: %s (%d)\n", strerror(errno),
+                errno);
+    }
+
+    close(traceFD);
+}
+
+// Print the command usage help to stderr.
+static void showHelp(const char *cmd)
+{
+    fprintf(stderr, "usage: %s [options]\n", cmd);
+    fprintf(stderr, "options include:\n"
+                    "  -b N            use a trace buffer size of N KB\n"
+                    "  -c              trace into a circular buffer\n"
+                    "  -f              trace CPU frequency changes\n"
+                    "  -l              trace CPU frequency governor load\n"
+                    "  -s              trace the kernel scheduler switches\n"
+                    "  -t N            trace for N seconds [defualt 5]\n"
+                    "  -w              trace the kernel workqueue\n");
+}
+
+static void handleSignal(int signo) {
+    g_traceAborted = true;
+}
+
+static void registerSigHandler() {
+    struct sigaction sa;
+    sigemptyset(&sa.sa_mask);
+    sa.sa_flags = 0;
+    sa.sa_handler = handleSignal;
+    sigaction(SIGHUP, &sa, NULL);
+    sigaction(SIGINT, &sa, NULL);
+    sigaction(SIGQUIT, &sa, NULL);
+    sigaction(SIGTERM, &sa, NULL);
+}
+
+int main(int argc, char **argv)
+{
+    if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
+        showHelp(argv[0]);
+        exit(0);
+    }
+
+    if (getuid() != 0) {
+        fprintf(stderr, "error: %s must be run as root.", argv[0]);
+        exit(1);
+    }
+
+    for (;;) {
+        int ret;
+
+        ret = getopt(argc, argv, "b:cflst:w");
+
+        if (ret < 0) {
+            break;
+        }
+
+        switch(ret) {
+            case 'b':
+                g_traceBufferSizeKB = atoi(optarg);
+            break;
+
+            case 'c':
+                g_traceOverwrite = true;
+            break;
+
+            case 'l':
+                g_traceGovernorLoad = true;
+            break;
+
+            case 'f':
+                g_traceCpuFrequency = true;
+            break;
+
+            case 's':
+                g_traceSchedSwitch = true;
+            break;
+
+            case 't':
+                g_traceDurationSeconds = atoi(optarg);
+            break;
+
+            case 'w':
+                g_traceWorkqueue = true;
+            break;
+
+            default:
+                fprintf(stderr, "\n");
+                showHelp(argv[0]);
+                exit(-1);
+            break;
+        }
+    }
+
+    registerSigHandler();
+
+    bool ok = startTrace();
+
+    if (ok) {
+        printf("capturing trace...");
+        fflush(stdout);
+
+        // We clear the trace after starting it because tracing gets enabled for
+        // each CPU individually in the kernel. Having the beginning of the trace
+        // contain entries from only one CPU can cause "begin" entries without a
+        // matching "end" entry to show up if a task gets migrated from one CPU to
+        // another.
+        ok = clearTrace();
+
+        if (ok) {
+            // Sleep to allow the trace to be captured.
+            struct timespec timeLeft;
+            timeLeft.tv_sec = g_traceDurationSeconds;
+            timeLeft.tv_nsec = 0;
+            do {
+                if (g_traceAborted) {
+                    break;
+                }
+            } while (nanosleep(&timeLeft, &timeLeft) == -1 && errno == EINTR);
+        }
+    }
+
+    // Stop the trace and restore the default settings.
+    stopTrace();
+
+    if (ok) {
+        if (!g_traceAborted) {
+            printf(" done\nTRACE:\n");
+            fflush(stdout);
+            dumpTrace();
+        } else {
+            printf("\ntrace aborted.\n");
+            fflush(stdout);
+        }
+        clearTrace();
+    } else {
+        fprintf(stderr, "unable to start tracing\n");
+    }
+
+    // Reset the trace buffer size to 1.
+    setTraceBufferSizeKB(1);
+
+    return g_traceAborted ? 1 : 0;
+}
diff --git a/bugmailer/Android.mk b/bugmailer/Android.mk
index 9ae4fd8..1bb3099 100644
--- a/bugmailer/Android.mk
+++ b/bugmailer/Android.mk
@@ -1,9 +1,12 @@
 # Copyright 2011 The Android Open Source Project
 #
 LOCAL_PATH:= $(call my-dir)
+
+ifneq ($(TARGET_BUILD_PDK), true)
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 LOCAL_MODULE := send_bug
 LOCAL_MODULE_TAGS := optional
 include $(BUILD_JAVA_LIBRARY)
+endif
diff --git a/bugmailer/src/com/android/commands/sendbug/SendBug.java b/bugmailer/src/com/android/commands/sendbug/SendBug.java
index 41a550a..da8788e 100644
--- a/bugmailer/src/com/android/commands/sendbug/SendBug.java
+++ b/bugmailer/src/com/android/commands/sendbug/SendBug.java
@@ -71,8 +71,8 @@
             }
             final IActivityManager mAm = ActivityManagerNative.getDefault();
             try {
-                mAm.startActivity(null, intent, intent.getType(), null, 0, null, null, 0, false,
-                        false, null, null, false);
+                mAm.startActivity(null, intent, intent.getType(), null, null, 0, 0,
+                        null, null, null);
             } catch (RemoteException e) {
                 // ignore
             }
@@ -87,7 +87,7 @@
         if (mPm != null) {
             final List<ResolveInfo> results;
             try {
-                results = mPm.queryIntentActivities(intent, null, 0);
+                results = mPm.queryIntentActivities(intent, null, 0, 0);
             } catch (RemoteException e) {
                 return null;
             }
diff --git a/ext4_utils/Android.mk b/ext4_utils/Android.mk
index 10b6285..d12a6be 100644
--- a/ext4_utils/Android.mk
+++ b/ext4_utils/Android.mk
@@ -4,7 +4,7 @@
 include $(CLEAR_VARS)
 
 libext4_utils_src_files := \
-	make_ext4fs.c \
+        make_ext4fs.c \
         ext4fixup.c \
         ext4_utils.c \
         allocate.c \
@@ -15,8 +15,40 @@
         indirect.c \
         uuid.c \
         sha1.c \
-	sparse_crc32.c \
-	wipe.c
+        sparse_crc32.c \
+        wipe.c
+
+# -- All host/targets including windows
+
+LOCAL_SRC_FILES := $(libext4_utils_src_files)
+LOCAL_MODULE := libext4_utils
+LOCAL_MODULE_TAGS := optional
+LOCAL_C_INCLUDES += external/zlib
+
+ifeq ($(HAVE_SELINUX), true)
+LOCAL_C_INCLUDES += external/libselinux/include
+LOCAL_SHARED_LIBRARIES += libselinux
+LOCAL_CFLAGS += -DHAVE_SELINUX
+endif # HAVE_SELINUX
+
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := make_ext4fs_main.c
+LOCAL_MODULE := make_ext4fs
+LOCAL_STATIC_LIBRARIES += libext4_utils libz
+ifeq ($(HOST_OS),windows)
+LOCAL_LDLIBS += -lws2_32
+endif
+
+include $(BUILD_HOST_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+# -- All host/targets excluding windows
+
+ifneq ($(HOST_OS),windows)
 
 LOCAL_SRC_FILES := $(libext4_utils_src_files)
 LOCAL_MODULE := libext4_utils
@@ -38,7 +70,6 @@
 LOCAL_MODULE := libext4_utils
 LOCAL_MODULE_TAGS := optional
 LOCAL_C_INCLUDES += external/zlib
-LOCAL_STATIC_LIBRARIES := libz
 
 ifeq ($(HAVE_SELINUX), true)
 LOCAL_C_INCLUDES += external/libselinux/include
@@ -50,21 +81,6 @@
 
 include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES := $(libext4_utils_src_files)
-LOCAL_MODULE := libext4_utils
-LOCAL_MODULE_TAGS := optional
-LOCAL_SHARED_LIBRARIES := libz
-
-ifeq ($(HAVE_SELINUX), true)
-LOCAL_C_INCLUDES += external/libselinux/include
-LOCAL_SHARED_LIBRARIES += libselinux
-LOCAL_CFLAGS += -DHAVE_SELINUX
-endif # HAVE_SELINUX
-
-include $(BUILD_HOST_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-
 LOCAL_SRC_FILES := make_ext4fs_main.c
 LOCAL_MODULE := make_ext4fs
 LOCAL_MODULE_TAGS := optional
@@ -80,20 +96,6 @@
 
 include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES := make_ext4fs_main.c
-LOCAL_MODULE := make_ext4fs
-LOCAL_STATIC_LIBRARIES += libext4_utils libz
-
-ifeq ($(HAVE_SELINUX), true)
-LOCAL_C_INCLUDES += external/libselinux/include
-LOCAL_STATIC_LIBRARIES += libselinux
-LOCAL_CFLAGS += -DHAVE_SELINUX
-endif # HAVE_SELINUX
-
-include $(BUILD_HOST_EXECUTABLE)
-
-include $(CLEAR_VARS)
-
 LOCAL_SRC_FILES := ext2simg.c
 LOCAL_MODULE := ext2simg
 LOCAL_MODULE_TAGS := optional
@@ -127,6 +129,7 @@
 LOCAL_SRC_FILES := simg2img.c \
 	sparse_crc32.c
 LOCAL_MODULE := simg2img
+LOCAL_MODULE_TAGS := debug
 
 include $(BUILD_HOST_EXECUTABLE)
 
@@ -135,6 +138,23 @@
 LOCAL_SRC_FILES := simg2img.c \
 	sparse_crc32.c
 LOCAL_MODULE := simg2img
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := img2simg.c
+LOCAL_MODULE := img2simg
+LOCAL_MODULE_TAGS := debug
+
+include $(BUILD_HOST_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := img2simg.c
+LOCAL_MODULE := img2simg
+LOCAL_MODULE_TAGS := optional
 
 include $(BUILD_EXECUTABLE)
 
@@ -174,3 +194,15 @@
 LOCAL_IS_HOST_MODULE := true
 
 include $(BUILD_PREBUILT)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := simg_dump.py
+LOCAL_MODULE_TAGS := debug
+LOCAL_SRC_FILES := simg_dump.py
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_IS_HOST_MODULE := true
+
+include $(BUILD_PREBUILT)
+
+endif
diff --git a/ext4_utils/backed_block.c b/ext4_utils/backed_block.c
index f8399ef..c0070f1 100644
--- a/ext4_utils/backed_block.c
+++ b/ext4_utils/backed_block.c
@@ -150,7 +150,7 @@
 /* Frees the memory used by the linked list of data blocks */
 void free_data_blocks()
 {
-        if (!data_blocks) return;
+	if (!data_blocks) return;
 	struct data_block *db = data_blocks;
 	while (db) {
 		struct data_block *next = db->next;
@@ -167,6 +167,6 @@
 		free(db);
 		db = next;
 	}
-        data_blocks = NULL;
-        last_used = NULL;
+	data_blocks = NULL;
+	last_used = NULL;
 }
diff --git a/ext4_utils/contents.c b/ext4_utils/contents.c
index 71143bc..de38bb0 100644
--- a/ext4_utils/contents.c
+++ b/ext4_utils/contents.c
@@ -27,6 +27,10 @@
 #include "indirect.h"
 #include "xattr.h"
 
+#ifdef USE_MINGW
+#define S_IFLNK 0  /* used by make_link, not needed under mingw */
+#endif
+
 static u32 dentry_size(u32 entries, struct dentry *dentries)
 {
 	u32 len = 24;
diff --git a/ext4_utils/ext2simg.c b/ext4_utils/ext2simg.c
index 9332bad..1fe085a 100644
--- a/ext4_utils/ext2simg.c
+++ b/ext4_utils/ext2simg.c
@@ -28,6 +28,10 @@
 #include <libgen.h>
 #include <unistd.h>
 
+#ifndef USE_MINGW /* O_BINARY is windows-specific flag */
+#define O_BINARY 0
+#endif
+
 extern struct fs_info info;
 
 static int verbose = 0;
@@ -169,7 +173,7 @@
 	const char *out = NULL;
 	int gzip = 0;
 	int sparse = 1;
-	int fd;
+	int infd, outfd;
 	int crc = 0;
 
 	while ((opt = getopt(argc, argv, "cvzS")) != -1) {
@@ -211,18 +215,29 @@
 		exit(EXIT_FAILURE);
 	}
 
-	fd = open(in, O_RDONLY);
+	infd = open(in, O_RDONLY);
 
-	if (fd < 0)
+	if (infd < 0)
 		critical_error_errno("failed to open input image");
 
-	read_ext(fd);
+	read_ext(infd);
 
-	build_sparse_ext(fd, in);
+	build_sparse_ext(infd, in);
 
-	close(fd);
+	close(infd);
 
-	write_ext4_image(out, gzip, sparse, crc, 0);
+	if (strcmp(out, "-")) {
+		outfd = open(out, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+		if (outfd < 0) {
+			error_errno("open");
+			return EXIT_FAILURE;
+		}
+	} else {
+		outfd = STDOUT_FILENO;
+	}
+
+	write_ext4_image(outfd, gzip, sparse, crc, 0);
+	close(outfd);
 
 	return 0;
 }
diff --git a/ext4_utils/ext4_utils.c b/ext4_utils/ext4_utils.c
index 3d5895e..7f226c8 100644
--- a/ext4_utils/ext4_utils.c
+++ b/ext4_utils/ext4_utils.c
@@ -23,12 +23,17 @@
 #include "extent.h"
 
 #include <fcntl.h>
-#include <arpa/inet.h>
-#include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <string.h>
 
+#ifdef USE_MINGW
+#include <winsock2.h>
+#else
+#include <arpa/inet.h>
+#include <sys/ioctl.h>
+#endif
+
 #if defined(__linux__)
 #include <linux/fs.h>
 #elif defined(__APPLE__) && defined(__MACH__)
@@ -137,11 +142,11 @@
 }
 
 /* Write the filesystem image to a file */
-void write_ext4_image(const char *filename, int gz, int sparse, int crc,
-		int wipe)
+void write_ext4_image(int fd, int gz, int sparse, int crc, int wipe)
 {
 	int ret = 0;
-	struct output_file *out = open_output_file(filename, gz, sparse,
+
+	struct output_file *out = open_output_fd(fd, gz, sparse,
 	        count_sparse_chunks(), crc, wipe);
 
 	if (!out)
@@ -446,15 +451,11 @@
 	}
 }
 
-static u64 get_block_device_size(const char *filename)
+static u64 get_block_device_size(int fd)
 {
-	int fd = open(filename, O_RDONLY);
 	u64 size = 0;
 	int ret;
 
-	if (fd < 0)
-		return 0;
-
 #if defined(__linux__)
 	ret = ioctl(fd, BLKGETSIZE64, &size);
 #elif defined(__APPLE__) && defined(__MACH__)
@@ -464,22 +465,20 @@
 	return 0;
 #endif
 
-	close(fd);
-
 	if (ret)
 		return 0;
 
 	return size;
 }
 
-u64 get_file_size(const char *filename)
+u64 get_file_size(int fd)
 {
 	struct stat buf;
 	int ret;
 	u64 reserve_len = 0;
 	s64 computed_size;
 
-	ret = stat(filename, &buf);
+	ret = fstat(fd, &buf);
 	if (ret)
 		return 0;
 
@@ -489,7 +488,7 @@
 	if (S_ISREG(buf.st_mode))
 		computed_size = buf.st_size - reserve_len;
 	else if (S_ISBLK(buf.st_mode))
-		computed_size = get_block_device_size(filename) - reserve_len;
+		computed_size = get_block_device_size(fd) - reserve_len;
 	else
 		computed_size = 0;
 
@@ -514,4 +513,3 @@
 
 	return num;
 }
-
diff --git a/ext4_utils/ext4_utils.h b/ext4_utils/ext4_utils.h
index 94f4bc9..1d701d0 100644
--- a/ext4_utils/ext4_utils.h
+++ b/ext4_utils/ext4_utils.h
@@ -17,7 +17,13 @@
 #ifndef _EXT4_UTILS_H_
 #define _EXT4_UTILS_H_
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
 #define _FILE_OFFSET_BITS 64
 #define _LARGEFILE64_SOURCE
 #include <sys/types.h>
@@ -58,7 +64,9 @@
 #define EXT4_SUPER_MAGIC 0xEF53
 #define EXT4_JNL_BACKUP_BLOCKS 1
 
+#ifndef min /* already defined by windows.h */
 #define min(a, b) ((a) < (b) ? (a) : (b))
+#endif
 
 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
 #define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
@@ -145,7 +153,7 @@
 }
 
 int ext4_bg_has_super_block(int bg);
-void write_ext4_image(const char *filename, int gz, int sparse, int crc,
+void write_ext4_image(int fd, int gz, int sparse, int crc,
 		int wipe);
 void ext4_create_fs_aux_info(void);
 void ext4_free_fs_aux_info(void);
@@ -154,8 +162,12 @@
 void ext4_create_journal_inode(void);
 void ext4_update_free(void);
 void ext4_queue_sb(void);
-u64 get_file_size(const char *filename);
+u64 get_file_size(int fd);
 u64 parse_num(const char *arg);
 void ext4_parse_sb(struct ext4_super_block *sb);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
diff --git a/ext4_utils/ext4fixup.c b/ext4_utils/ext4fixup.c
index 5e04602..c664ac3 100644
--- a/ext4_utils/ext4fixup.c
+++ b/ext4_utils/ext4fixup.c
@@ -24,10 +24,13 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/mman.h>
 #include <fcntl.h>
 #include <unistd.h>
 
+#ifndef USE_MINGW
+#include <sys/mman.h>
+#endif
+
 #if defined(__APPLE__) && defined(__MACH__)
 #define lseek64 lseek
 #define off64_t off_t
diff --git a/ext4_utils/img2simg.c b/ext4_utils/img2simg.c
new file mode 100644
index 0000000..3b46613
--- /dev/null
+++ b/ext4_utils/img2simg.c
@@ -0,0 +1,328 @@
+/*
+ * Copyright (C) 2010-2012 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.
+ */
+
+#define DEFAULT_BLOCK_SIZE	"4K"
+#define DEFAULT_CHUNK_SIZE	"64M"
+#define DEFAULT_SUFFIX		"%03d"
+
+#include "ext4_utils.h"
+#include "sparse_format.h"
+#if 0 /* endian.h is not on all platforms */
+# include <endian.h>
+#else
+  /* For now, just assume we're going to run on little-endian. */
+# define my_htole32(h) (h)
+# define my_htole16(h) (h)
+#endif
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#define COPY_BUF_SIZE (1024*1024)
+static char *copy_buf;
+
+static const char *progname(const char *argv0)
+{
+    const char *prog_name;
+    if ((prog_name = strrchr(argv0, '/')))
+	return(prog_name + 1);	/* Advance beyond '/'. */
+    return(argv0);		/* No '/' in argv0, use it as is. */
+}
+
+static void error_exit(const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    fputc('\n', stderr);
+    va_end(ap);
+
+    exit(EXIT_FAILURE);
+}
+
+static void usage(const char *argv0, const char *error_fmt, ...)
+{
+    fprintf(stderr,
+	    "Usage: %s [OPTIONS] <raw_image_file>\n",
+	    progname(argv0));
+    fprintf(stderr, "The <raw_image_file> will be split into as many sparse\n");
+    fprintf(stderr, "files as needed.  Each sparse file will contain a single\n");
+    fprintf(stderr, "DONT CARE chunk to offset to the correct block and then\n");
+    fprintf(stderr, "a single RAW chunk containing a portion of the data from\n");
+    fprintf(stderr, "the raw image file.  The sparse files will be named by\n");
+    fprintf(stderr, "appending a number to the name of the raw image file.\n");
+    fprintf(stderr, "\n");
+    fprintf(stderr, "OPTIONS (Defaults are enclosed by square brackets):\n");
+    fprintf(stderr, "  -s SUFFIX      Format appended number with SUFFIX [%s]\n",
+	    DEFAULT_SUFFIX);
+    fprintf(stderr, "  -B SIZE        Use a block size of SIZE [%s]\n",
+	    DEFAULT_BLOCK_SIZE);
+    fprintf(stderr, "  -C SIZE        Use a chunk size of SIZE [%s]\n",
+	    DEFAULT_CHUNK_SIZE);
+    fprintf(stderr, "SIZE is a decimal integer that may optionally be\n");
+    fprintf(stderr, "followed by a suffix that specifies a multiplier for\n");
+    fprintf(stderr, "the integer:\n");
+    fprintf(stderr, "       c         1 byte (the default when omitted)\n");
+    fprintf(stderr, "       w         2 bytes\n");
+    fprintf(stderr, "       b         512 bytes\n");
+    fprintf(stderr, "       kB        1000 bytes\n");
+    fprintf(stderr, "       K         1024 bytes\n");
+    fprintf(stderr, "       MB        1000*1000 bytes\n");
+    fprintf(stderr, "       M         1024*1024 bytes\n");
+    fprintf(stderr, "       GB        1000*1000*1000 bytes\n");
+    fprintf(stderr, "       G         1024*1024*1024 bytes\n");
+
+    if (error_fmt && *error_fmt)
+    {
+	fprintf(stderr, "\n");
+	va_list ap;
+	va_start(ap, error_fmt);
+	vfprintf(stderr, error_fmt, ap);
+	va_end(ap);
+	fprintf(stderr, "\n");
+    }
+
+    exit(EXIT_FAILURE);
+}
+
+static void cpy_file(int out_fd, char *out_path, int in_fd, char *in_path,
+		     size_t len)
+{
+    ssize_t s, cpy_len = COPY_BUF_SIZE;
+
+    while (len) {
+	if (len < COPY_BUF_SIZE)
+	    cpy_len = len;
+
+	s = read(in_fd, copy_buf, cpy_len);
+	if (s < 0)
+	    error_exit("\"%s\": %s", in_path, strerror(errno));
+	if (!s)
+	    error_exit("\"%s\": Unexpected EOF", in_path);
+
+	cpy_len = s;
+
+	s = write(out_fd, copy_buf, cpy_len);
+	if (s < 0)
+	    error_exit("\"%s\": %s", out_path, strerror(errno));
+	if (s != cpy_len)
+	    error_exit("\"%s\": Short data write (%lu)", out_path,
+		       (unsigned long)s);
+
+	len -= cpy_len;
+    }
+}
+
+static int parse_size(const char *size_str, size_t *size)
+{
+    static const size_t MAX_SIZE_T = ~(size_t)0;
+    size_t mult;
+    unsigned long long int value;
+    const char *end;
+    errno = 0;
+    value = strtoull(size_str, (char **)&end, 10);
+    if (errno != 0 || end == size_str || value > MAX_SIZE_T)
+	return -1;
+    if (*end == '\0') {
+	*size = value;
+	return 0;
+    }
+    if (!strcmp(end, "c"))
+	mult = 1;
+    else if (!strcmp(end, "w"))
+	mult = 2;
+    else if (!strcmp(end, "b"))
+	mult = 512;
+    else if (!strcmp(end, "kB"))
+	mult = 1000;
+    else if (!strcmp(end, "K"))
+	mult = 1024;
+    else if (!strcmp(end, "MB"))
+	mult = (size_t)1000*1000;
+    else if (!strcmp(end, "M"))
+	mult = (size_t)1024*1024;
+    else if (!strcmp(end, "GB"))
+	mult = (size_t)1000*1000*1000;
+    else if (!strcmp(end, "G"))
+	mult = (size_t)1024*1024*1024;
+    else
+	return -1;
+
+    if (value > MAX_SIZE_T / mult)
+	return -1;
+    *size = value * mult;
+    return 0;
+}
+
+int main(int argc, char *argv[])
+{
+    char *suffix = DEFAULT_SUFFIX;
+    char *block_size_str = DEFAULT_BLOCK_SIZE;
+    char *chunk_size_str = DEFAULT_CHUNK_SIZE;
+    size_t block_size, chunk_size, blocks_per_chunk, to_write;
+    char *in_path, *out_path, *out_fmt;
+    int in_fd, out_fd;
+    struct stat in_st;
+    off_t left_to_write;
+    struct {
+	sparse_header_t sparse_hdr;
+	chunk_header_t dont_care_hdr;
+	chunk_header_t raw_hdr;
+    } file_hdr;
+    unsigned int file_count;
+    ssize_t s;
+    int i;
+
+    /* Parse the command line. */
+    while ((i = getopt(argc, argv, "s:B:C:")) != -1)
+    {
+	switch (i) {
+	case 's':
+	    suffix = optarg;
+	    break;
+	case 'B':
+	    block_size_str = optarg;
+	    break;
+	case 'C':
+	    chunk_size_str = optarg;
+	    break;
+	default:
+	    usage(argv[0], NULL);
+	    break;
+	}
+    }
+
+    if (parse_size(block_size_str, &block_size))
+	usage(argv[0], "Can not parse \"%s\" as a block size.",
+	      block_size_str);
+    if (block_size % 4096)
+	usage(argv[0], "Block size is not a multiple of 4096.");
+
+    if (parse_size(chunk_size_str, &chunk_size))
+	usage(argv[0], "Can not parse \"%s\" as a chunk size.",
+	      chunk_size_str);
+    if (chunk_size % block_size)
+	usage(argv[0], "Chunk size is not a multiple of the block size.");
+    blocks_per_chunk = chunk_size / block_size;
+
+    if ((argc - optind) != 1)
+	usage(argv[0], "Missing or extra arguments.");
+    in_path = argv[optind];
+
+    /* Open the input file and validate it. */
+    if ((in_fd = open(in_path, O_RDONLY)) < 0)
+	error_exit("open \"%s\": %s", in_path, strerror(errno));
+    if (fstat(in_fd, &in_st))
+	error_exit("fstat \"%s\": %s", in_path, strerror(errno));
+    left_to_write = in_st.st_size;
+    if (left_to_write % block_size)
+	error_exit(
+	    "\"%s\" size (%llu) is not a multiple of the block size (%llu).\n",
+	    in_path,
+	    (unsigned long long)left_to_write, (unsigned long long)block_size);
+
+    /* Get a buffer for copying the chunks. */
+    if ((copy_buf = malloc(COPY_BUF_SIZE)) == 0)
+	error_exit("malloc copy buffer: %s", strerror(errno));
+
+    /* Get a buffer for a sprintf format to form output paths. */
+    if ((out_fmt = malloc(sizeof("%s") + strlen(suffix))) == 0)
+	error_exit("malloc format buffer: %s", strerror(errno));
+    out_fmt[0] = '%';
+    out_fmt[1] = 's';
+    strcpy(out_fmt + 2, suffix);
+
+    /* Get a buffer for an output path. */
+    i = snprintf(copy_buf, COPY_BUF_SIZE, out_fmt, in_path, UINT_MAX);
+    if (i >= COPY_BUF_SIZE)
+	error_exit("Ridulously long suffix: %s", suffix);
+    if ((out_path = malloc(i + 1)) == 0)
+	error_exit("malloc output path buffer: %s", strerror(errno));
+
+    /*
+     * Each file gets a sparse_header, a Don't Care chunk to offset to
+     * where the data belongs and then a Raw chunk with the actual data.
+     */
+    memset((void *)&file_hdr.sparse_hdr, 0, sizeof(file_hdr.sparse_hdr));
+    file_hdr.sparse_hdr.magic = my_htole32(SPARSE_HEADER_MAGIC);
+    file_hdr.sparse_hdr.major_version = my_htole16(1);
+    file_hdr.sparse_hdr.minor_version = my_htole16(0);
+    file_hdr.sparse_hdr.file_hdr_sz = my_htole16(sizeof(sparse_header_t));
+    file_hdr.sparse_hdr.chunk_hdr_sz = my_htole16(sizeof(chunk_header_t));
+    file_hdr.sparse_hdr.blk_sz = my_htole32(block_size);
+    /* The total_blks will be set in the file loop below. */
+    file_hdr.sparse_hdr.total_chunks = my_htole32(2);
+    file_hdr.sparse_hdr.image_checksum = my_htole32(0); /* Typically unused. */
+
+    memset((void *)&file_hdr.dont_care_hdr, 0, sizeof(file_hdr.dont_care_hdr));
+    file_hdr.dont_care_hdr.chunk_type = my_htole16(CHUNK_TYPE_DONT_CARE);
+    /* The Don't Care's chunk_sz will be set in the file loop below. */
+    file_hdr.dont_care_hdr.total_sz = my_htole32(sizeof(chunk_header_t));
+
+    memset((void *)&file_hdr.raw_hdr, 0, sizeof(file_hdr.raw_hdr));
+    file_hdr.raw_hdr.chunk_type = my_htole16(CHUNK_TYPE_RAW);
+    file_hdr.raw_hdr.chunk_sz = my_htole32(blocks_per_chunk);
+    file_hdr.raw_hdr.total_sz = my_htole32(chunk_size + sizeof(chunk_header_t));
+
+    /* Loop through writing chunk_size to each of the output files. */
+    to_write = chunk_size;
+    for (file_count = 1; left_to_write ; file_count++) {
+	/* Fix up the headers on the last block. */
+	if (left_to_write < (off_t)chunk_size) {
+	    to_write = left_to_write;
+	    file_hdr.raw_hdr.chunk_sz = my_htole32(left_to_write / block_size);
+	    file_hdr.raw_hdr.total_sz = my_htole32(left_to_write
+						+ sizeof(chunk_header_t));
+	}
+
+	/* Form the pathname for this output file and open it. */
+	sprintf(out_path, out_fmt, in_path, file_count);
+	if ((out_fd = creat(out_path, 0666)) < 0)
+	    error_exit("\"%s\": %s", out_path, strerror(errno));
+
+	/* Update and write the headers to this output file. */
+	s = (file_count-1) * blocks_per_chunk;
+	file_hdr.dont_care_hdr.chunk_sz = my_htole32(s);
+	file_hdr.sparse_hdr.total_blks = my_htole32(s
+						+ (to_write / block_size));
+	s = write(out_fd, (void *)&file_hdr, sizeof(file_hdr));
+	if (s < 0)
+	    error_exit("\"%s\": %s", out_path, strerror(errno));
+	if (s != sizeof(file_hdr))
+	    error_exit("\"%s\": Short write (%lu)", out_path, (unsigned long)s);
+
+	/* Copy this chunk from the input file to the output file. */
+	cpy_file(out_fd, out_path, in_fd, in_path, to_write);
+
+	/* Close this output file and update the amount left to write. */
+	if (close(out_fd))
+	    error_exit("close \"%s\": %s", out_path, strerror(errno));
+	left_to_write -= to_write;
+    }
+
+    if (close(in_fd))
+	error_exit("close \"%s\": %s", in_path, strerror(errno));
+
+    exit(EXIT_SUCCESS);
+}
diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c
index af05eb3..6cd5ade 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -22,7 +22,9 @@
 #include "uuid.h"
 #include "backed_block.h"
 
+#include <assert.h>
 #include <dirent.h>
+#include <fcntl.h>
 #include <libgen.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,8 +33,33 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#ifdef ANDROID
-#include <private/android_filesystem_config.h>
+#ifdef USE_MINGW
+
+#include <winsock2.h>
+
+/* These match the Linux definitions of these flags.
+   L_xx is defined to avoid conflicting with the win32 versions.
+*/
+#define L_S_IRUSR 00400
+#define L_S_IWUSR 00200
+#define L_S_IXUSR 00100
+#define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
+#define S_IRGRP 00040
+#define S_IWGRP 00020
+#define S_IXGRP 00010
+#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
+#define S_IROTH 00004
+#define S_IWOTH 00002
+#define S_IXOTH 00001
+#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
+#define S_ISUID 0004000
+#define S_ISGID 0002000
+#define S_ISVTX 0001000
+
+#else
+
+#define O_BINARY 0
+
 #endif
 
 /* TODO: Not implemented:
@@ -67,10 +94,12 @@
 	return root_inode;
 }
 
+#ifndef USE_MINGW
 /* Read a local directory and create the same tree in the generated filesystem.
    Calls itself recursively with each directory in the given directory */
 static u32 build_directory_structure(const char *full_path, const char *dir_path,
-		     u32 dir_inode, int android, struct selabel_handle *sehnd)
+		u32 dir_inode, fs_config_func_t fs_config_func,
+		struct selabel_handle *sehnd)
 {
 	int entries = 0;
 	struct dentry *dentries;
@@ -113,13 +142,13 @@
 		dentries[i].size = stat.st_size;
 		dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
 		dentries[i].mtime = stat.st_mtime;
-		if (android) {
+		if (fs_config_func != NULL) {
 #ifdef ANDROID
 			unsigned int mode = 0;
 			unsigned int uid = 0;
 			unsigned int gid = 0;
 			int dir = S_ISDIR(stat.st_mode);
-			fs_config(dentries[i].path, dir, &uid, &gid, &mode);
+			fs_config_func(dentries[i].path, dir, &uid, &gid, &mode);
 			dentries[i].mode = mode;
 			dentries[i].uid = uid;
 			dentries[i].gid = gid;
@@ -172,7 +201,7 @@
 			entry_inode = make_file(dentries[i].full_path, dentries[i].size);
 		} else if (dentries[i].file_type == EXT4_FT_DIR) {
 			entry_inode = build_directory_structure(dentries[i].full_path,
-					dentries[i].path, inode, android, sehnd);
+					dentries[i].path, inode, fs_config_func, sehnd);
 		} else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
 			entry_inode = make_link(dentries[i].full_path, dentries[i].link);
 		} else {
@@ -200,6 +229,7 @@
 	free(dentries);
 	return inode;
 }
+#endif
 
 static u32 compute_block_size()
 {
@@ -269,29 +299,40 @@
 int make_ext4fs(const char *filename, s64 len,
                 const char *mountpoint, struct selabel_handle *sehnd)
 {
-    reset_ext4fs_info();
-    info.len = len;
-    return make_ext4fs_internal(filename, NULL, mountpoint, 0, 0, 0, 0, 1, 0, sehnd);
+	int fd;
+	int status;
+
+	reset_ext4fs_info();
+	info.len = len;
+
+	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+	if (fd < 0) {
+		error_errno("open");
+		return EXIT_FAILURE;
+	}
+
+	status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, 0, sehnd);
+	close(fd);
+
+	return status;
 }
 
-int make_ext4fs_internal(const char *filename, const char *directory,
-                         const char *mountpoint,
-                         int android, int gzip, int sparse,
-                         int crc, int wipe, int init_itabs,
-                         struct selabel_handle *sehnd)
+int make_ext4fs_internal(int fd, const char *directory,
+                         char *mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse,
+                         int crc, int wipe, int init_itabs, struct selabel_handle *sehnd)
 {
-        u32 root_inode_num;
-        u16 root_mode;
+	u32 root_inode_num;
+	u16 root_mode;
 
 	if (setjmp(setjmp_env))
 		return EXIT_FAILURE; /* Handle a call to longjmp() */
 
 	if (info.len <= 0)
-		info.len = get_file_size(filename);
+		info.len = get_file_size(fd);
 
 	if (info.len <= 0) {
 		fprintf(stderr, "Need size of filesystem\n");
-                return EXIT_FAILURE;
+		return EXIT_FAILURE;
 	}
 
 	if (info.block_size <= 0)
@@ -364,10 +405,17 @@
 	if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
 		ext4_create_resize_inode();
 
+#ifdef USE_MINGW
+	// Windows needs only 'create an empty fs image' functionality
+	assert(!directory);
+	root_inode_num = build_default_directory_structure();
+#else
 	if (directory)
-		root_inode_num = build_directory_structure(directory, mountpoint, 0, android, sehnd);
+		root_inode_num = build_directory_structure(directory, mountpoint, 0,
+                        fs_config_func, sehnd);
 	else
 		root_inode_num = build_default_directory_structure();
+#endif
 
 	root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
 	inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
@@ -408,7 +456,7 @@
 			aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
 			aux_info.sb->s_blocks_count_lo);
 
-	write_ext4_image(filename, gzip, sparse, crc, wipe);
+	write_ext4_image(fd, gzip, sparse, crc, wipe);
 
 	return 0;
 }
diff --git a/ext4_utils/make_ext4fs.h b/ext4_utils/make_ext4fs.h
index 53ff840..c217c3d 100644
--- a/ext4_utils/make_ext4fs.h
+++ b/ext4_utils/make_ext4fs.h
@@ -20,6 +20,10 @@
 #include "ext4_utils.h"
 #include "ext4.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #ifdef HAVE_SELINUX
 #include <selinux/selinux.h>
 #include <selinux/label.h>
@@ -27,13 +31,18 @@
 struct selabel_handle;
 #endif
 
+typedef void (*fs_config_func_t)(const char *path, int dir, unsigned *uid, unsigned *gid,
+        unsigned *mode);
+
 void reset_ext4fs_info();
 int make_ext4fs(const char *filename, s64 len,
                 const char *mountpoint, struct selabel_handle *sehnd);
-int make_ext4fs_internal(const char *filename, const char *directory,
-                         const char *mountpoint,
-                         int android, int gzip, int sparse,
-                         int crc, int wipe, int init_itabs,
-                         struct selabel_handle *sehnd);
+int make_ext4fs_internal(int fd, const char *directory,
+                         char *mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse,
+                         int crc, int wipe, int init_itabs, struct selabel_handle *sehnd);
+
+#ifdef __cplusplus
+}
+#endif
 
 #endif
diff --git a/ext4_utils/make_ext4fs_main.c b/ext4_utils/make_ext4fs_main.c
index 6a885d2..d545423 100644
--- a/ext4_utils/make_ext4fs_main.c
+++ b/ext4_utils/make_ext4fs_main.c
@@ -14,8 +14,9 @@
  * limitations under the License.
  */
 
-#include <unistd.h>
+#include <fcntl.h>
 #include <libgen.h>
+#include <unistd.h>
 
 #if defined(__linux__)
 #include <linux/fs.h>
@@ -23,137 +24,166 @@
 #include <sys/disk.h>
 #endif
 
+#ifdef ANDROID
+#include <private/android_filesystem_config.h>
+#endif
+
 #include "make_ext4fs.h"
 
+#ifndef USE_MINGW /* O_BINARY is windows-specific flag */
+#define O_BINARY 0
+#endif
+
 extern struct fs_info info;
 
 
 static void usage(char *path)
 {
-        fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
-        fprintf(stderr, "    [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
-        fprintf(stderr, "    [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n");
-        fprintf(stderr, "    [ -S file_contexts ]\n");
-        fprintf(stderr, "    [ -z | -s ] [ -t ] [ -w ] [ -c ] [ -J ]\n");
-        fprintf(stderr, "    <filename> [<directory>]\n");
+	fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
+	fprintf(stderr, "    [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
+	fprintf(stderr, "    [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n");
+	fprintf(stderr, "    [ -S file_contexts ]\n");
+	fprintf(stderr, "    [ -z | -s ] [ -t ] [ -w ] [ -c ] [ -J ]\n");
+	fprintf(stderr, "    <filename> [<directory>]\n");
 }
 
 int main(int argc, char **argv)
 {
-        int opt;
-        const char *filename = NULL;
-        const char *directory = NULL;
-        char *mountpoint = "";
-        int android = 0;
-        int gzip = 0;
-        int sparse = 0;
-        int crc = 0;
-        int wipe = 0;
-        int init_itabs = 0;
-        struct selabel_handle *sehnd = NULL;
+	int opt;
+	const char *filename = NULL;
+	const char *directory = NULL;
+	char *mountpoint = "";
+	fs_config_func_t fs_config_func = NULL;
+	int gzip = 0;
+	int sparse = 0;
+	int crc = 0;
+	int wipe = 0;
+	int init_itabs = 0;
+	int fd;
+	int exitcode;
+	struct selabel_handle *sehnd = NULL;
 #ifdef HAVE_SELINUX
-        struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } };
+	struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } };
 #endif
 
-        while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:fwzJsctS:")) != -1) {
-                switch (opt) {
-                case 'l':
-                        info.len = parse_num(optarg);
-                        break;
-                case 'j':
-                        info.journal_blocks = parse_num(optarg);
-                        break;
-                case 'b':
-                        info.block_size = parse_num(optarg);
-                        break;
-                case 'g':
-                        info.blocks_per_group = parse_num(optarg);
-                        break;
-                case 'i':
-                        info.inodes = parse_num(optarg);
-                        break;
-                case 'I':
-                        info.inode_size = parse_num(optarg);
-                        break;
-                case 'L':
-                        info.label = optarg;
-                        break;
-                case 'f':
-                        force = 1;
-                        break;
-                case 'a':
-                        android = 1;
-                        mountpoint = optarg;
-                        break;
-                case 'w':
-                        wipe = 1;
-                        break;
-                case 'z':
-                        gzip = 1;
-                        break;
+	while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:fwzJsctS:")) != -1) {
+		switch (opt) {
+		case 'l':
+			info.len = parse_num(optarg);
+			break;
+		case 'j':
+			info.journal_blocks = parse_num(optarg);
+			break;
+		case 'b':
+			info.block_size = parse_num(optarg);
+			break;
+		case 'g':
+			info.blocks_per_group = parse_num(optarg);
+			break;
+		case 'i':
+			info.inodes = parse_num(optarg);
+			break;
+		case 'I':
+			info.inode_size = parse_num(optarg);
+			break;
+		case 'L':
+			info.label = optarg;
+			break;
+		case 'f':
+			force = 1;
+			break;
+		case 'a':
+#ifdef ANDROID
+			fs_config_func = fs_config;
+			mountpoint = optarg;
+#else
+			fprintf(stderr, "can't set android permissions - built without android support\n");
+			usage(argv[0]);
+			exit(EXIT_FAILURE);
+#endif
+			break;
+		case 'w':
+			wipe = 1;
+			break;
+		case 'z':
+			gzip = 1;
+			break;
 		case 'J':
 			info.no_journal = 1;
 			break;
 		case 'c':
 			crc = 1;
 			break;
-                case 's':
-                        sparse = 1;
-                        break;
-                case 't':
-                        init_itabs = 1;
-                        break;
-                case 'S':
+		case 's':
+			sparse = 1;
+			break;
+		case 't':
+			init_itabs = 1;
+			break;
+		case 'S':
 #ifdef HAVE_SELINUX
-                        seopts[0].value = optarg;
-                        sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
-                        if (!sehnd) {
-                            perror(optarg);
-                            exit(EXIT_FAILURE);
-                        }
+			seopts[0].value = optarg;
+			sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
+			if (!sehnd) {
+				perror(optarg);
+				exit(EXIT_FAILURE);
+			}
 #endif
-                        break;
-                default: /* '?' */
-                        usage(argv[0]);
-                        exit(EXIT_FAILURE);
-                }
-        }
-
-	if (gzip && sparse) {
-                fprintf(stderr, "Cannot specify both gzip and sparse\n");
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
+			   break;
+		default: /* '?' */
+			usage(argv[0]);
+			exit(EXIT_FAILURE);
+		}
 	}
 
-        if (wipe && sparse) {
-                fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
-        }
+	if (gzip && sparse) {
+		fprintf(stderr, "Cannot specify both gzip and sparse\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
 
-        if (wipe && gzip) {
-                fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
-        }
+	if (wipe && sparse) {
+		fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
 
-        if (optind >= argc) {
-                fprintf(stderr, "Expected filename after options\n");
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
-        }
+	if (wipe && gzip) {
+		fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
 
-        filename = argv[optind++];
+	if (optind >= argc) {
+		fprintf(stderr, "Expected filename after options\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
 
-        if (optind < argc)
-                directory = argv[optind++];
+	filename = argv[optind++];
 
-        if (optind < argc) {
-                fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
-                usage(argv[0]);
-                exit(EXIT_FAILURE);
-        }
+	if (optind < argc)
+		directory = argv[optind++];
 
-        return make_ext4fs_internal(filename, directory, mountpoint, android, gzip,
-                       sparse, crc, wipe, init_itabs, sehnd);
+	if (optind < argc) {
+		fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
+
+	if (strcmp(filename, "-")) {
+		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+		if (fd < 0) {
+			error_errno("open");
+			return EXIT_FAILURE;
+		}
+	} else {
+		fd = STDOUT_FILENO;
+	}
+
+	exitcode = make_ext4fs_internal(fd, directory, mountpoint, fs_config_func, gzip,
+			sparse, crc, wipe, init_itabs, sehnd);
+	close(fd);
+
+	return exitcode;
 }
diff --git a/ext4_utils/output_file.c b/ext4_utils/output_file.c
index d7cf91b..40655a9 100644
--- a/ext4_utils/output_file.c
+++ b/ext4_utils/output_file.c
@@ -20,15 +20,20 @@
 #include "sparse_crc32.h"
 #include "wipe.h"
 
+#include <fcntl.h>
+#include <stdbool.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/mman.h>
 #include <unistd.h>
-#include <fcntl.h>
-
 #include <zlib.h>
 
+#ifndef USE_MINGW
+#include <sys/mman.h>
+#define O_BINARY 0
+#endif
+
+
 #if defined(__APPLE__) && defined(__MACH__)
 #define lseek64 lseek
 #define off64_t off_t
@@ -48,6 +53,7 @@
 struct output_file {
 	int fd;
 	gzFile gz_fd;
+	bool close_fd;
 	int sparse;
 	u64 cur_out_ptr;
 	u32 chunk_cnt;
@@ -85,7 +91,9 @@
 
 static void file_close(struct output_file *out)
 {
-	close(out->fd);
+	if (out->close_fd) {
+		close(out->fd);
+	}
 }
 
 
@@ -343,7 +351,7 @@
 	out->ops->close(out);
 }
 
-struct output_file *open_output_file(const char *filename, int gz, int sparse,
+struct output_file *open_output_fd(int fd, int gz, int sparse,
         int chunks, int crc, int wipe)
 {
 	int ret;
@@ -362,25 +370,17 @@
 
 	if (gz) {
 		out->ops = &gz_file_ops;
-		out->gz_fd = gzopen(filename, "wb9");
+		out->gz_fd = gzdopen(fd, "wb9");
 		if (!out->gz_fd) {
 			error_errno("gzopen");
 			free(out);
 			return NULL;
 		}
 	} else {
-		if (strcmp(filename, "-")) {
-			out->fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
-			if (out->fd < 0) {
-				error_errno("open");
-				free(out);
-				return NULL;
-			}
-		} else {
-			out->fd = STDOUT_FILENO;
-		}
+		out->fd = fd;
 		out->ops = &file_ops;
 	}
+	out->close_fd = false;
 	out->sparse = sparse;
 	out->cur_out_ptr = 0ll;
 	out->chunk_cnt = 0;
@@ -407,6 +407,33 @@
 	return out;
 }
 
+struct output_file *open_output_file(const char *filename, int gz, int sparse,
+        int chunks, int crc, int wipe) {
+
+	int fd;
+	struct output_file *file;
+
+	if (strcmp(filename, "-")) {
+		fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+		if (fd < 0) {
+			error_errno("open");
+			return NULL;
+		}
+	} else {
+		fd = STDOUT_FILENO;
+	}
+
+	file = open_output_fd(fd, gz, sparse, chunks, crc, wipe);
+	if (!file) {
+		close(fd);
+		return NULL;
+	}
+
+	file->close_fd = true; // we opened descriptor thus we responsible for closing it
+
+	return file;
+}
+
 void pad_output_file(struct output_file *out, u64 len)
 {
 	int ret;
@@ -447,7 +474,7 @@
 void write_data_block(struct output_file *out, u64 off, u8 *data, int len)
 {
 	int ret;
-	
+
 	if (off + len > (u64) info.len) {
 		error("attempted to write block %llu past end of filesystem",
 				off + len - info.len);
@@ -512,6 +539,7 @@
 	int ret;
 	off64_t aligned_offset;
 	int aligned_diff;
+	int buffer_size;
 
 	if (off + len >= (u64) info.len) {
 		error("attempted to write block %llu past end of filesystem",
@@ -519,7 +547,7 @@
 		return;
 	}
 
-	int file_fd = open(file, O_RDONLY);
+	int file_fd = open(file, O_RDONLY | O_BINARY);
 	if (file_fd < 0) {
 		error_errno("open");
 		return;
@@ -527,14 +555,25 @@
 
 	aligned_offset = offset & ~(4096 - 1);
 	aligned_diff = offset - aligned_offset;
+	buffer_size = len + aligned_diff;
 
-	u8 *data = mmap64(NULL, len + aligned_diff, PROT_READ, MAP_SHARED, file_fd,
+#ifndef USE_MINGW
+	u8 *data = mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, file_fd,
 			aligned_offset);
 	if (data == MAP_FAILED) {
 		error_errno("mmap64");
 		close(file_fd);
 		return;
 	}
+#else
+	u8 *data = malloc(buffer_size);
+	if (!data) {
+		error_errno("malloc");
+		close(file_fd);
+		return;
+	}
+	memset(data, 0, buffer_size);
+#endif
 
 	if (out->sparse) {
 		write_chunk_raw(out, off, data + aligned_diff, len);
@@ -549,6 +588,11 @@
 	}
 
 err:
-	munmap(data, len);
+#ifndef USE_MINGW
+	munmap(data, buffer_size);
+#else
+	write(file_fd, data, buffer_size);
+	free(data);
+#endif
 	close(file_fd);
 }
diff --git a/ext4_utils/output_file.h b/ext4_utils/output_file.h
index b448706..a578c80 100644
--- a/ext4_utils/output_file.h
+++ b/ext4_utils/output_file.h
@@ -21,6 +21,8 @@
 
 struct output_file *open_output_file(const char *filename, int gz, int sparse,
         int chunks, int crc, int wipe);
+struct output_file *open_output_fd(int fd, int gz, int sparse,
+        int chunks, int crc, int wipe);
 void write_data_block(struct output_file *out, u64 off, u8 *data, int len);
 void write_fill_block(struct output_file *out, u64 off, u32 fill_val, int len);
 void write_data_file(struct output_file *out, u64 off, const char *file,
diff --git a/ext4_utils/sha1.c b/ext4_utils/sha1.c
index 1db9134..463ec38 100644
--- a/ext4_utils/sha1.c
+++ b/ext4_utils/sha1.c
@@ -17,7 +17,9 @@
 
 #define SHA1HANDSOFF		/* Copies data before messing with it. */
 
+#ifndef USE_MINGW
 #include <sys/cdefs.h>
+#endif
 #include <sys/types.h>
 #include <assert.h>
 #include <string.h>
diff --git a/ext4_utils/sha1.h b/ext4_utils/sha1.h
index 71334c8..9a8f7e3 100644
--- a/ext4_utils/sha1.h
+++ b/ext4_utils/sha1.h
@@ -9,10 +9,21 @@
 #ifndef _SYS_SHA1_H_
 #define	_SYS_SHA1_H_
 
-#include <sys/cdefs.h>
 #include <sys/types.h>
 #include <stdint.h>
 
+#ifdef USE_MINGW
+typedef unsigned char u_char;
+typedef unsigned int uint32_t;
+typedef unsigned int u_int32_t;
+typedef unsigned int u_int;
+
+#define __BEGIN_DECLS
+#define __END_DECLS
+#else
+#include <sys/cdefs.h>
+#endif
+
 #define SHA1_DIGEST_LENGTH		20
 #define SHA1_DIGEST_STRING_LENGTH	41
 
diff --git a/ext4_utils/simg2img.c b/ext4_utils/simg2img.c
index 228d289..12b92eb 100644
--- a/ext4_utils/simg2img.c
+++ b/ext4_utils/simg2img.c
@@ -247,7 +247,7 @@
 			fprintf(stderr, "Error reading chunk header\n");
 			exit(-1);
 		}
- 
+
 		if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
 			/* Skip the remaining bytes in a header that is longer than
 			 * we expected.
diff --git a/ext4_utils/simg_dump.py b/ext4_utils/simg_dump.py
new file mode 100755
index 0000000..6ece31d
--- /dev/null
+++ b/ext4_utils/simg_dump.py
@@ -0,0 +1,169 @@
+#! /usr/bin/env python
+
+# Copyright (C) 2012 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.
+
+from __future__ import print_function
+import getopt, posixpath, signal, struct, sys
+
+def usage(argv0):
+  print("""
+Usage: %s [-v] sparse_image_file ...
+ -v             verbose output
+""" % ( argv0 ))
+  sys.exit(2)
+
+def main():
+
+  signal.signal(signal.SIGPIPE, signal.SIG_DFL)
+
+  me = posixpath.basename(sys.argv[0])
+
+  # Parse the command line
+  verbose = 0			# -v
+  try:
+    opts, args = getopt.getopt(sys.argv[1:],
+                               "v",
+                               ["verbose"])
+  except getopt.GetoptError, e:
+    print(e)
+    usage(me)
+  for o, a in opts:
+    if o in ("-v", "--verbose"):
+      verbose += 1
+    else:
+      print("Unrecognized option \"%s\"" % (o))
+      usage(me)
+
+  if len(args) == 0:
+    print("No sparse_image_file specified")
+    usage(me)
+
+  for path in args:
+    FH = open(path, 'rb')
+    header_bin = FH.read(28)
+    header = struct.unpack("<I4H4I", header_bin)
+
+    magic = header[0]
+    major_version = header[1]
+    minor_version = header[2]
+    file_hdr_sz = header[3]
+    chunk_hdr_sz = header[4]
+    blk_sz = header[5]
+    total_blks = header[6]
+    total_chunks = header[7]
+    image_checksum = header[8]
+
+    if magic != 0xED26FF3A:
+      print("%s: %s: Magic should be 0xED26FF3A but is 0x%08X"
+            % (me, path, magic))
+      continue
+    if major_version != 1 or minor_version != 0:
+      print("%s: %s: I only know about version 1.0, but this is version %u.%u"
+            % (me, path, major_version, minor_version))
+      continue
+    if file_hdr_sz != 28:
+      print("%s: %s: The file header size was expected to be 28, but is %u."
+            % (me, path, file_hdr_sz))
+      continue
+    if chunk_hdr_sz != 12:
+      print("%s: %s: The chunk header size was expected to be 12, but is %u."
+            % (me, path, chunk_hdr_sz))
+      continue
+
+    print("%s: Total of %u %u-byte output blocks in %u input chunks."
+          % (path, total_blks, blk_sz, total_chunks))
+
+    if image_checksum != 0:
+      print("checksum=0x%08X" % (image_checksum))
+
+    if not verbose:
+      continue
+    print("            input_bytes      output_blocks")
+    print("chunk    offset     number  offset  number")
+    offset = 0
+    for i in xrange(1,total_chunks+1):
+      header_bin = FH.read(12)
+      header = struct.unpack("<2H2I", header_bin)
+      chunk_type = header[0]
+      reserved1 = header[1]
+      chunk_sz = header[2]
+      total_sz = header[3]
+      data_sz = total_sz - 12
+
+      print("%4u %10u %10u %7u %7u" % (i, FH.tell(), data_sz, offset, chunk_sz),
+            end=" ")
+
+      if chunk_type == 0xCAC1:
+        if data_sz != (chunk_sz * blk_sz):
+          print("Raw chunk input size (%u) does not match output size (%u)"
+                % (data_sz, chunk_sz * blk_sz))
+          break;
+        else:
+          print("Raw data", end="")
+          FH.read(data_sz)
+      elif chunk_type == 0xCAC2:
+        if data_sz != 4:
+          print("Fill chunk should have 4 bytes of fill, but this has %u"
+                % (data_sz), end="")
+          break;
+        else:
+          fill_bin = FH.read(4)
+          fill = struct.unpack("<I", fill_bin)
+          print("Fill with 0x%08X" % (fill))
+      elif chunk_type == 0xCAC3:
+        if data_sz != 0:
+          print("Don't care chunk input size is non-zero (%u)" % (data_sz))
+          break;
+        else:
+          print("Don't care", end="")
+      elif chunk_type == 0xCAC4:
+        if data_sz != 4:
+          print("CRC32 chunk should have 4 bytes of CRC, but this has %u"
+                % (data_sz), end="")
+          break;
+        else:
+          crc_bin = FH.read(4)
+          crc = struct.unpack("<I", crc)
+          print("Unverified CRC32 0x%08X" % (crc))
+      else:
+          print("Unknown chunk type 0x%04X" % (chunk_type), end="")
+          break;
+
+      if verbose > 1:
+        header = struct.unpack("<12B", header_bin)
+        print(" (%02X%02X %02X%02X %02X%02X%02X%02X %02X%02X%02X%02X)"
+              % (header[0], header[1], header[2], header[3],
+                 header[4], header[5], header[6], header[7],
+                 header[8], header[9], header[10], header[11]))
+      else:
+        print()
+
+      offset += chunk_sz
+
+    print("     %10u            %7u         End" % (FH.tell(), offset))
+
+    if total_blks != offset:
+      print("The header said we should have %u output blocks, but we saw %u"
+            % (total_blks, offset))
+
+    junk_len = len(FH.read())
+    if junk_len:
+      print("There were %u bytes of extra data at the end of the file."
+            % (junk_len))
+
+  sys.exit(0)
+
+if __name__ == "__main__":
+  main()
diff --git a/ext4_utils/uuid.c b/ext4_utils/uuid.c
index d518476..33d2494 100644
--- a/ext4_utils/uuid.c
+++ b/ext4_utils/uuid.c
@@ -15,7 +15,12 @@
  */
 
 #include <string.h>
+
+#ifdef USE_MINGW
+#include <winsock2.h>
+#else
 #include <arpa/inet.h>
+#endif
 
 #include "ext4_utils.h"
 #include "sha1.h"
diff --git a/libpagemap/include/pagemap/pagemap.h b/libpagemap/include/pagemap/pagemap.h
index 09ff29d..25c6161 100644
--- a/libpagemap/include/pagemap/pagemap.h
+++ b/libpagemap/include/pagemap/pagemap.h
@@ -129,10 +129,10 @@
                              unsigned long low, unsigned long hi,
                              uint64_t **range_out, size_t *len);
 
-#define _BITS(x, offset, bits) (((x) >> offset) & ((1LL << ((bits) + 1)) - 1))
+#define _BITS(x, offset, bits) (((x) >> offset) & ((1LL << (bits)) - 1))
 
 #define PM_PAGEMAP_PRESENT(x)     (_BITS(x, 63, 1))
-#define PM_PAGEMAP_SWAPPED(x)     (!_BITS(x, 62, 1))
+#define PM_PAGEMAP_SWAPPED(x)     (_BITS(x, 62, 1))
 #define PM_PAGEMAP_SHIFT(x)       (_BITS(x, 55, 6))
 #define PM_PAGEMAP_PFN(x)         (_BITS(x, 0, 55))
 #define PM_PAGEMAP_SWAP_OFFSET(x) (_BITS(x, 5, 50))
diff --git a/libpagemap/pm_map.c b/libpagemap/pm_map.c
index f683ba6..2d5c2f9 100644
--- a/libpagemap/pm_map.c
+++ b/libpagemap/pm_map.c
@@ -83,10 +83,6 @@
     pm_memusage_zero(&ws);
     
     for (i = 0; i < len; i++) {
-        if (!PM_PAGEMAP_PRESENT(pagemap[i]) ||
-            PM_PAGEMAP_SWAPPED(pagemap[i]))
-            continue;
-
         error = pm_kernel_flags(map->proc->ker, PM_PAGEMAP_PFN(pagemap[i]),
                                 &flags);
         if (error) goto out;
@@ -99,6 +95,7 @@
         if (error) goto out;
 
         ws.vss += map->proc->ker->pagesize;
+        if( PM_PAGEMAP_SWAPPED(pagemap[i]) ) continue;
         ws.rss += (count >= 1) ? (map->proc->ker->pagesize) : (0);
         ws.pss += (count >= 1) ? (map->proc->ker->pagesize / count) : (0);
         ws.uss += (count == 1) ? (map->proc->ker->pagesize) : (0);
diff --git a/libpagemap/pm_process.c b/libpagemap/pm_process.c
index b3c077e..dddff01 100644
--- a/libpagemap/pm_process.c
+++ b/libpagemap/pm_process.c
@@ -122,7 +122,7 @@
         free(range);
         *range_out = NULL;
         return 0;
-    } else if (error < 0 || (error > 0 && error < numpages * sizeof(uint64_t))) {
+    } else if (error < 0 || (error > 0 && error < (int)(numpages * sizeof(uint64_t)))) {
         error = (error < 0) ? errno : -1;
         free(range);
         return error;
@@ -210,7 +210,7 @@
 }
 
 #define INITIAL_MAPS 10
-#define MAX_LINE 256
+#define MAX_LINE 1024
 #define MAX_PERMS 5
 
 /* 
diff --git a/showmap/showmap.c b/showmap/showmap.c
index 5c4a26f..bb359f6 100644
--- a/showmap/showmap.c
+++ b/showmap/showmap.c
@@ -56,9 +56,7 @@
     if (line[name_pos]) {
         strlcpy(name, line + name_pos, sizeof(name));
     } else {
-        if ((start >= 0x10000000) && (start < 0x40000000)) {
-            strlcpy(name, "[stack]", sizeof(name));
-        } else if (prev && start == prev->end && is_library(prev->name)) {
+        if (prev && start == prev->end && is_library(prev->name)) {
             // anonymous mappings immediately adjacent to shared libraries
             // usually correspond to the library BSS segment, so we use the
             // library's own name
diff --git a/tests/bionic/libc/Android.mk b/tests/bionic/libc/Android.mk
index 5dcf130..52ef01e 100644
--- a/tests/bionic/libc/Android.mk
+++ b/tests/bionic/libc/Android.mk
@@ -61,6 +61,7 @@
 # First, the tests in 'common'
 
 sources := \
+    common/bench_stdio.c \
     common/test_clock.c \
     common/test_cpu_set.c \
     common/test_drand48.c \
@@ -252,4 +253,31 @@
 EXTRA_CFLAGS := -mandroid
 #$(call device-test, $(sources))
 
+# NOTE: We build both a shared and static version of bench_pthread.
+# the shared version will use the target device's C library, while
+# the static one will use the current build product implementation.
+# This is ideal to quantify pthread optimizations.
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := common/bench_pthread.c
+LOCAL_MODULE := bench_pthread_shared
+LOCAL_MODULE_TAGS := tests
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := common/bench_pthread.c
+LOCAL_MODULE := bench_pthread_static
+LOCAL_MODULE_TAGS := tests
+LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_STATIC_LIBRARIES := libc
+include $(BUILD_EXECUTABLE)
+
+ifeq ($(HOST_OS),linux)
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := common/bench_pthread.c
+LOCAL_MODULE := bench_pthread
+LOCAL_LDLIBS += -lpthread -lrt
+LOCAL_MODULE_TAGS := tests
+include $(BUILD_HOST_EXECUTABLE)
+endif
+
 endif  # BIONIC_TESTS
diff --git a/tests/bionic/libc/common/bench_pthread.c b/tests/bionic/libc/common/bench_pthread.c
new file mode 100644
index 0000000..4e46f61
--- /dev/null
+++ b/tests/bionic/libc/common/bench_pthread.c
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* This program is used to benchmark various pthread operations
+ * Note that we want to be able to build it with GLibc, both on
+ *  a Linux host and an Android device. For example, on ARM, one
+ * can build it manually with:
+ *
+ *     arm-linux-none-gnueabi-gcc -static -o bench_pthread_gnueabi \
+ *           bench_pthread.c -O2 -lpthread -lrt
+ */
+#define _GNU_SOURCE 1
+#include <time.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <limits.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define S(x)  S_(x)
+#define S_(x) #x
+
+#define C(x,y)  C_(x,y)
+#define C_(x,y) x ## y
+
+#ifndef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER
+#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER  PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
+#endif
+
+#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER
+#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER  PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
+#endif
+
+static int64_t now_ns(void)
+{
+    struct timespec ts;
+    /* NOTE: get thread-specific CPU-time clock to ensure
+     *       we don't measure stuff like kernel thread preemptions
+     *       that might happen during the benchmark
+     */
+    clock_gettime(CLOCK_THREAD_CPUTIME_ID,&ts);
+    return ts.tv_sec*1000000000LL + ts.tv_nsec;
+}
+
+#define SUBCOUNT   10000
+#define MAX_STATS  1000000
+
+/* Maximum time we'll wait for a single bench run */
+#define MAX_WAIT_MS  1000
+
+static int64_t  stats[MAX_STATS];
+
+static int
+compare_stats(const void* a, const void* b)
+{
+    uint64_t sa = *(const uint64_t*)a;
+    uint64_t sb = *(const uint64_t*)b;
+    if (sa < sb)
+        return -1;
+    if (sa > sb)
+        return +1;
+    else
+        return 0;
+}
+
+static void
+filter_stats(int count, const char* statement)
+{
+    int64_t  min, max, avg, median;
+
+    /* sort the array in increasing order */
+    qsort(stats, count, sizeof(stats[0]), compare_stats);
+
+    /* trim 10% to remove outliers */
+    int min_index = count*0.05;
+    int max_index = count - min_index;
+    if (max_index >= count)
+        max_index = count-1;
+
+    count = (max_index - min_index)+1;
+
+    /* the median is the center item */
+    median = stats[(min_index+max_index)/2];
+
+    /* the minimum is the first, the max the last */
+    min = stats[min_index];
+    max = stats[max_index];
+
+    /* compute the average */
+    int nn;
+    int64_t  total = 0;
+    for (nn = min_index; nn <= max_index; nn++) {
+        total += stats[nn];
+    }
+
+    printf("BENCH: %5.1f %5.1f %5.1f, %s\n",
+           min*1./SUBCOUNT,
+           max*1./SUBCOUNT,
+           median*1./SUBCOUNT,
+           statement);
+    if (0) {
+        for (nn = min_index; nn <= max_index; nn++) {
+            printf(" %lld", (long long)stats[nn]);
+        }
+        printf("\n");
+    }
+}
+
+#define BENCH_COUNT(stmnt,total) do { \
+        int64_t  count = total; \
+        int      num_stats = 0; \
+        int64_t  bench_start = now_ns(); \
+        while (num_stats < MAX_STATS && count >= SUBCOUNT) { \
+            int      tries = SUBCOUNT; \
+            int64_t  sub_start = now_ns(); \
+            count -= tries; \
+            for ( ; tries > 0; tries-- ) {\
+                stmnt;\
+            }\
+            int64_t  sub_end = now_ns(); \
+            stats[num_stats++] = sub_end - sub_start; \
+            if (sub_end - bench_start >= MAX_WAIT_MS*1e6) \
+                break; \
+        } \
+        filter_stats(num_stats, #stmnt); \
+    } while (0)
+
+#define DEFAULT_COUNT 10000000
+
+#define BENCH(stmnt) BENCH_COUNT(stmnt,DEFAULT_COUNT)
+
+/* Will be called by pthread_once() for benchmarking */
+static void _dummy_init(void)
+{
+    /* nothing */
+}
+
+/* Used when creating the key */
+static void key_destroy(void* param)
+{
+    /* nothing */
+}
+
+int main(void)
+{
+    pthread_once_t  once = PTHREAD_ONCE_INIT;
+    pthread_once(&once, _dummy_init);
+
+    pthread_key_t   key;
+    pthread_key_create(&key, key_destroy);
+    pthread_setspecific(key, (void*)(int)100);
+
+    BENCH(getpid());
+    BENCH(pthread_self());
+    BENCH(pthread_getspecific(key));
+    BENCH(pthread_once(&once, _dummy_init));
+
+    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+    BENCH(pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex));
+
+    pthread_mutex_t errorcheck_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
+    BENCH(pthread_mutex_lock(&errorcheck_mutex); pthread_mutex_unlock(&errorcheck_mutex));
+
+    pthread_mutex_t recursive_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
+    BENCH(pthread_mutex_lock(&recursive_mutex); pthread_mutex_unlock(&recursive_mutex));
+
+	/* TODO: Benchmark pshared mutexes */
+
+    sem_t semaphore;
+    int dummy;
+    sem_init(&semaphore, 1, 1);
+    BENCH(sem_getvalue(&semaphore,&dummy));
+    BENCH(sem_wait(&semaphore); sem_post(&semaphore));
+    return 0;
+}
diff --git a/tests/bionic/libc/common/bench_stdio.c b/tests/bionic/libc/common/bench_stdio.c
new file mode 100644
index 0000000..4c19bf1
--- /dev/null
+++ b/tests/bionic/libc/common/bench_stdio.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* This program tries to benchmark stdio operations like fread() and
+ * fwrite() with various chunk sizes. We always read/write from /dev/zero
+ * to ensure that disk speed and caching don't change our results.
+ *
+ * We really do this to measure improvements in the low-level stdio
+ * features.
+ */
+ 
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+#include <errno.h>
+
+static char buffer[1024*1024];
+
+/* Return current time in milli-seconds, as a double */
+static double
+now_ms(void)
+{
+    struct timespec  ts;
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+    return ts.tv_sec*1000. + ts.tv_nsec*1e-6;
+}
+
+void read_file(FILE* fp, int chunkSize)
+{
+    int totalSize = sizeof(buffer);
+    for ( ; totalSize > 0; totalSize -= chunkSize) {
+        fread(buffer, 1, chunkSize, fp);
+    }
+}
+
+void write_file(FILE* fp, int chunkSize)
+{
+    int totalSize = sizeof(buffer);
+    for ( ; totalSize > 0; totalSize -= chunkSize) {
+        fwrite(buffer, 1, chunkSize, fp);
+    }
+}
+
+#define  BENCH(op,...) \
+    do { \
+	double  time_ms = now_ms(); \
+	op ; \
+	time_ms = now_ms() - time_ms; \
+	double  bandwidth = sizeof(buffer)*1000./1024./time_ms; \
+	printf("bench %-30s %8.2f ms  (%.1f KB/s) \n", #op, time_ms, bandwidth ); \
+    } while (0)
+
+int main(void)
+{
+    FILE* fp = fopen("/dev/zero", "rw");
+
+    if (fp == NULL) {
+	fprintf(stderr,"Could not open /dev/zero: %s\n", strerror(errno));
+	return 1;
+    }
+
+    BENCH(read_file(fp,1));
+    BENCH(read_file(fp,2));
+    BENCH(read_file(fp,3));
+    BENCH(read_file(fp,4));
+    BENCH(read_file(fp,8));
+    BENCH(read_file(fp,16));
+    BENCH(read_file(fp,32));
+    BENCH(read_file(fp,64));
+    BENCH(read_file(fp,256));
+    BENCH(read_file(fp,1024));
+    BENCH(read_file(fp,4096));
+    BENCH(read_file(fp,16384));
+    BENCH(read_file(fp,65536));
+
+    BENCH(write_file(fp,1));
+    BENCH(write_file(fp,2));
+    BENCH(write_file(fp,3));
+    BENCH(write_file(fp,4));
+    BENCH(write_file(fp,8));
+    BENCH(write_file(fp,16));
+    BENCH(write_file(fp,32));
+    BENCH(write_file(fp,64));
+    BENCH(write_file(fp,256));
+    BENCH(write_file(fp,1024));
+    BENCH(write_file(fp,4096));
+    BENCH(write_file(fp,16384));
+    BENCH(write_file(fp,65536));
+
+    fclose(fp);
+    return 0;
+}
diff --git a/tests/framebuffer/refresh.c b/tests/framebuffer/refresh.c
index 43dc7cf..38e7871 100644
--- a/tests/framebuffer/refresh.c
+++ b/tests/framebuffer/refresh.c
@@ -67,12 +67,10 @@
     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
         return -errno;    
     
-    int refreshRate = 1000000000000000LLU / 
-            (
-          (uint64_t)( info.upper_margin + info.lower_margin + info.yres ) 
-                  * ( info.left_margin  + info.right_margin + info.xres )
-                  * info.pixclock
-            );
+    uint64_t denominator = (uint64_t)( info.upper_margin + info.lower_margin + info.yres )
+                         * ( info.left_margin  + info.right_margin + info.xres )
+                         * info.pixclock;
+    int refreshRate = denominator ? (1000000000000000LLU / denominator) : 0;
     
     float xdpi = (info.xres * 25.4f) / info.width; 
     float ydpi = (info.yres * 25.4f) / info.height;
diff --git a/tests/fstest/perm_checker.conf b/tests/fstest/perm_checker.conf
index 25f1b5f..580eb85 100644
--- a/tests/fstest/perm_checker.conf
+++ b/tests/fstest/perm_checker.conf
@@ -16,8 +16,8 @@
 /data/dalvik-cache/... 400 744 root 19999 root 19999
 /data/data 701 771 system system system system
 /data/data/... 000 775 system 19999 system 19999
-/data/local/ 771 771 shell shell shell shell
-/data/local/tmp/ 771 1771 shell shell shell shell
+/data/local/ 751 751 root root root root
+/data/local/tmp/ 771 771 shell shell shell shell
 /data/lost+found/ 700 770 root root root root
 /data/misc/ 1711 1771 root system root misc
 /data/misc/akmd_set.txt 600 640 root compass compass compass
diff --git a/tests/iptables/qtaguid/Android.mk b/tests/iptables/qtaguid/Android.mk
index 5eba34f..cf0a2ba 100644
--- a/tests/iptables/qtaguid/Android.mk
+++ b/tests/iptables/qtaguid/Android.mk
@@ -19,7 +19,6 @@
 include $(CLEAR_VARS)
 LOCAL_MODULE_TAGS := tests
 LOCAL_MODULE := socketTag
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA)/nativestresstest
 LOCAL_SRC_FILES := socketTag.cpp
 LOCAL_SHARED_LIBRARIES += libcutils libutils
 LOCAL_STATIC_LIBRARIES += libtestUtil
diff --git a/tests/iptables/qtaguid/socketTag.cpp b/tests/iptables/qtaguid/socketTag.cpp
index 68d9257..fb7ecdc 100644
--- a/tests/iptables/qtaguid/socketTag.cpp
+++ b/tests/iptables/qtaguid/socketTag.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  *
  */
+
 /*
  * This socket tagging test is to ensure that the
  * netfilter/xt_qtaguid kernel module somewhat behaves as expected
@@ -24,6 +25,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <gtest/gtest.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <string>
@@ -32,15 +34,15 @@
 #include <utils/Log.h>
 #include <testUtil.h>
 
+namespace android {
 
 class SockInfo {
 public:
-        SockInfo()
-                : fd(-1), addr(NULL) {};
-        int setup(uint64_t tag);
-        bool checkTag(uint64_t tag, uid_t uid);
-        int fd;
-        void *addr;
+    SockInfo() : fd(-1), addr(NULL) {};
+    int setup(uint64_t tag);
+    bool checkTag(uint64_t tag, uid_t uid);
+    int fd;
+    void *addr;
 };
 
 
@@ -90,6 +92,7 @@
     close(param_fd);
     return res;
 }
+
 /*----------------------------------------------------------------*/
 int SockInfo::setup(uint64_t tag) {
     fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -180,528 +183,217 @@
     return pos != NULL;
 }
 
-/*----------------------------------------------------------------*/
-int testSocketTagging(void) {
-    SockInfo sock0;
-    SockInfo sock1;
-    int res;
-    int total_errors = 0;
-    int ctrl_fd = -1;
-    int dev_fd = -1;
-    const uint64_t invalid_tag1 = 0x0000000100000001llu;
-    uint64_t valid_tag1;
-    uint64_t valid_tag2;
-    uint64_t max_uint_tag = 0xffffffff00000000llu;
-    uid_t fake_uid;
-    uid_t fake_uid2;
-    const char *test_name;
-    uid_t my_uid = getuid();
-    pid_t my_pid = getpid();
 
-    srand48(my_pid * my_uid);
-    /* Adjust fake UIDs and tags so that multiple instances can run in parallel. */
-    fake_uid = testRand();
-    fake_uid2 = testRand();
-    valid_tag1 = ((uint64_t)my_pid << 48) | ((uint64_t)testRand() << 32);
-    valid_tag2 = ((uint64_t)my_pid << 48) | ((uint64_t)testRand() << 32);
-    max_uint_tag = 1llu << 63 | (((uint64_t)my_pid << 48) ^ max_uint_tag);
-    testSetLogCatTag(LOG_TAG);
+class SocketTaggingTest : public ::testing::Test {
+protected:
+    virtual void SetUp() {
+        ctrl_fd = -1;
+        dev_fd = -1;
+        my_uid = getuid();
+        my_pid = getpid();
+        srand48(my_pid * my_uid);
+        // Adjust fake UIDs and tags so that multiple instances can run in parallel.
+        fake_uid = testRand();
+        fake_uid2 = testRand();
+        valid_tag1 = ((uint64_t)my_pid << 48) | ((uint64_t)testRand() << 32);
+        valid_tag2 = ((uint64_t)my_pid << 48) | ((uint64_t)testRand() << 32);
+        valid_tag2 &= 0xffffff00ffffffffllu;  // Leave some room to make counts visible.
+        testPrintI("* start: pid=%lu uid=%lu uid1=0x%lx/%lu uid2=0x%lx/%lu"
+                   " tag1=0x%llx/%llu tag2=0x%llx/%llu",
+                   my_pid, my_uid, fake_uid, fake_uid, fake_uid2, fake_uid2,
+                   valid_tag1, valid_tag1, valid_tag2, valid_tag2);
+        max_uint_tag = 0xffffffff00000000llu;
+        max_uint_tag = 1llu << 63 | (((uint64_t)my_pid << 48) ^ max_uint_tag);
 
-    testPrintI("** %s ** ============================", __FUNCTION__);
-    testPrintI("* start: pid=%lu uid=%lu uid1=0x%lx/%lu uid2=0x%lx/%lu"
-               " tag1=0x%llx/%llu tag2=0x%llx/%llu",
-               my_pid, my_uid, fake_uid, fake_uid, fake_uid2, fake_uid2,
-               valid_tag1, valid_tag1, valid_tag2, valid_tag2);
-
-    // ---------------
-    test_name = "kernel has qtaguid";
-    testPrintI("* test: %s ", test_name);
-    ctrl_fd = openCtrl();
-    if (ctrl_fd < 0) {
-        testPrintE("qtaguid ctrl open failed: %s", strerror(errno));
-        total_errors++;
-        goto done;
-    }
-    close(ctrl_fd);
-    dev_fd = open("/dev/xt_qtaguid", O_RDONLY);
-    if (dev_fd < 0) {
-        testPrintE("qtaguid dev open failed: %s", strerror(errno));
-        total_errors++;
-    }
-    // ---------------
-    test_name = "delete command doesn't fail";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("d 0 %u", fake_uid);
-    if (res < 0) {
-            testPrintE("! %s: Unexpected results", test_name);
-            total_errors++;
-    }
-
-    res = doCtrlCommand("d 0 %u", fake_uid2);
-    if (res < 0) {
-            testPrintE("! %s: Unexpected results", test_name);
-            total_errors++;
-    }
-
-    res = doCtrlCommand("d 0 %u", my_uid);
-    if (res < 0) {
-            testPrintE("! %s: Unexpected results", test_name);
-            total_errors++;
-    }
-    // ---------------
-    test_name = "setup sock0 and addr via tag";
-    testPrintI("* test: %s", test_name);
-    if (sock0.setup(valid_tag1) < 0) {
-        testPrintE("socket setup failed: %s", strerror(errno));
-        total_errors++;
-        goto done;
-
-    }
-    // ---------------
-    test_name = "setup sock1 and addr via tag";
-    testPrintI("* test: %s", test_name);
-    if (sock1.setup(valid_tag1) < 0) {
-        testPrintE("socket setup failed: %s", strerror(errno));
-        total_errors++;
-        goto done;
-    }
-    // ---------------
-    test_name = "insufficient args. Expected failure";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t");
-    if (res > 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "bad command. Expected failure";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("?");
-    if (res > 0) {
-            testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "no tag, no uid";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d", sock0.fd);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (!sock0.checkTag(0, my_uid)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "invalid tag. Expected failure";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d %llu", sock0.fd, invalid_tag1);
-    if (res > 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (sock0.checkTag(invalid_tag1, my_uid)) {
-        testPrintE("! %s: Unexpected results: tag should not be there", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "valid tag with no uid";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d %llu", sock0.fd, valid_tag1);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (!sock0.checkTag(valid_tag1, my_uid)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-    // ---------------
-    test_name = "valid untag";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("u %d", sock0.fd);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (sock0.checkTag(valid_tag1, my_uid)) {
-        testPrintE("! %s: Unexpected results: tag not removed", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "valid 1st tag";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag2, fake_uid);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (!sock0.checkTag(valid_tag2, fake_uid)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "valid re-tag";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag2, fake_uid);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (!sock0.checkTag(valid_tag2, fake_uid)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "valid re-tag with acct_tag change";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag1, fake_uid);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (!sock0.checkTag(valid_tag1, fake_uid)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "re-tag with uid change";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag2, fake_uid2);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (!sock0.checkTag(valid_tag2, fake_uid2)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "valid 64bit acct tag";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d %llu", sock0.fd, max_uint_tag);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (!sock0.checkTag(max_uint_tag, my_uid)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "tag another socket";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("t %d %llu %u", sock1.fd, valid_tag1, fake_uid2);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (!sock1.checkTag(valid_tag1, fake_uid2)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "valid untag";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("u %d", sock0.fd);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (sock0.checkTag(max_uint_tag, fake_uid)) {
-        testPrintE("! %s: Unexpected results: tag should not be there", test_name);
-        total_errors++;
-    }
-    if (!sock1.checkTag(valid_tag1, fake_uid2)) {
-        testPrintE("! %s: Unexpected results: tag not found", test_name);
-        total_errors++;
-    }
-    res = doCtrlCommand("u %d", sock1.fd);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (sock1.checkTag(valid_tag1, fake_uid2)) {
-        testPrintE("! %s: Unexpected results: tag should not be there", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "invalid sock0.fd. Expected failure";
-    testPrintI("* test: %s", test_name);
-    close(sock0.fd);
-    res = doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag1, my_uid);
-    if (res > 0) {
-            testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (sock0.checkTag(valid_tag1, my_uid)) {
-        testPrintE("! %s: Unexpected results: tag should not be there", test_name);
-        total_errors++;
-    }
-
-    // ---------------
-    test_name = "invalid untag. Expected failure";
-    testPrintI("* test: %s", test_name);
-    close(sock1.fd);
-    res = doCtrlCommand("u %d", sock1.fd);
-    if (res > 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-
-    if (total_errors) {
-        testPrintE("! Errors found");
-    } else {
-        testPrintI("No Errors found");
-    }
-
-done:
-    if (dev_fd >= 0) {
-        close(dev_fd);
-    }
-    if (ctrl_fd >= 0) {
+        testPrintI("kernel has qtaguid");
+        ctrl_fd = openCtrl();
+        ASSERT_GE(ctrl_fd, 0) << "qtaguid ctrl open failed";
         close(ctrl_fd);
-    }
-    return total_errors;
-}
+        dev_fd = open("/dev/xt_qtaguid", O_RDONLY);
+        EXPECT_GE(dev_fd, 0) << "qtaguid dev open failed";
 
-int testTagData(void) {
-    SockInfo sock0;
-    SockInfo sock1;
-    int res;
-    int total_errors = 0;
-    int ctrl_fd = -1;
-    int dev_fd = -1;
-    const uint64_t invalid_tag1 = 0x0000000100000001llu;
-    uint64_t valid_tag1;
-    uint64_t valid_tag2;
-    uint64_t max_uint_tag = 0xffffffff00000000llu;
-    uid_t fake_uid;
-    uid_t fake_uid2;
-    const char *test_name;
-    uid_t my_uid = getuid();
-    pid_t my_pid = getpid();
-    const int max_tags = 5;
+        // We want to clean up any previous faulty test runs.
+        testPrintI("delete command does not fail");
+        EXPECT_GE(doCtrlCommand("d 0 %u", fake_uid), 0) << "Failed to delete fake_uid";
+        EXPECT_GE(doCtrlCommand("d 0 %u", fake_uid2), 0) << "Failed to delete fake_uid2";
+        EXPECT_GE(doCtrlCommand("d 0 %u", my_uid), 0) << "Failed to delete my_uid";
 
-    srand48(my_pid * my_uid);
-    /* Adjust fake UIDs and tags so that multiple instances can run in parallel. */
-    fake_uid = testRand();
-    fake_uid2 = testRand();
-    valid_tag1 = ((uint64_t)my_pid << 48) | ((uint64_t)testRand() << 32);
-    valid_tag2 = ((uint64_t)my_pid << 48) | ((uint64_t)testRand() << 32);
-    valid_tag2 &= 0xffffff00ffffffffllu;  // Leave some room to make counts visible.
-    testSetLogCatTag(LOG_TAG);
-
-    testPrintI("** %s ** ============================", __FUNCTION__);
-    testPrintI("* start: pid=%lu uid=%lu uid1=0x%lx/%lu uid2=0x%lx/%lu"
-               " tag1=0x%llx/%llu tag2=0x%llx/%llu",
-               my_pid, my_uid, fake_uid, fake_uid, fake_uid2, fake_uid2,
-               valid_tag1, valid_tag1, valid_tag2, valid_tag2);
-
-    // ---------------
-    test_name = "kernel has qtaguid";
-    testPrintI("* test: %s ", test_name);
-    ctrl_fd = openCtrl();
-    if (ctrl_fd < 0) {
-        testPrintE("qtaguid ctrl open failed: %s", strerror(errno));
-        return 1;
-    }
-    close(ctrl_fd);
-    dev_fd = open("/dev/xt_qtaguid", O_RDONLY);
-    if (dev_fd < 0) {
-        testPrintE("! %s: qtaguid dev open failed: %s", test_name, strerror(errno));
-        total_errors++;
-    }
-    // ---------------
-    test_name = "delete command doesn't fail";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("d 0 %u", fake_uid);
-    if (res < 0) {
-            testPrintE("! %s: Unexpected results", test_name);
-            total_errors++;
+        testPrintI("setup sock0 and addr via tag");
+        ASSERT_FALSE(sock0.setup(valid_tag1))  << "socket0 setup failed";
+        testPrintI("setup sock1 and addr via tag");
+        ASSERT_FALSE(sock1.setup(valid_tag1))  << "socket1 setup failed";
     }
 
-    res = doCtrlCommand("d 0 %u", fake_uid2);
-    if (res < 0) {
-            testPrintE("! %s: Unexpected results", test_name);
-            total_errors++;
-    }
+   virtual void TearDown() {
+       if (dev_fd >= 0) {
+           close(dev_fd);
+       }
+       if (ctrl_fd >= 0) {
+           close(ctrl_fd);
+       }
+   }
 
-    res = doCtrlCommand("d 0 %u", my_uid);
-    if (res < 0) {
-            testPrintE("! %s: Unexpected results", test_name);
-            total_errors++;
-    }
+   SockInfo sock0;
+   SockInfo sock1;
+   int ctrl_fd;
+   int dev_fd;
+   uid_t fake_uid;
+   uid_t fake_uid2;
+   uid_t my_uid;
+   pid_t my_pid;
+   uint64_t valid_tag1;
+   uint64_t valid_tag2;
+   uint64_t max_uint_tag;
+   static const uint64_t invalid_tag1 = 0x0000000100000001llu;
+   static const int max_tags = 5;
+};
 
-    // ---------------
-    test_name = "setup sock0 and addr via tag";
-    testPrintI("* test: %s", test_name);
-    if (sock0.setup(valid_tag1)) {
-        testPrintE("socket setup failed: %s", strerror(errno));
-        return 1;
-    }
-    // ---------------
-    test_name = "setup sock1 and addr via tag";
-    testPrintI("* test: %s", test_name);
-    if (sock1.setup(valid_tag1)) {
-        testPrintE("socket setup failed: %s", strerror(errno));
-        return 1;
-    }
-    // ---------------
-    test_name = "setup tag limit";
-    testPrintI("* test: %s ", test_name);
+TEST_F(SocketTaggingTest, TagData) {
+    max_uint_tag = 0xffffffff00000000llu;
     char *max_tags_str;
+
+    testPrintI("setup tag limit");
     asprintf(&max_tags_str, "%d", max_tags);
-    res = writeModuleParam("max_sock_tags", max_tags_str);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        free(max_tags_str);
-        return 1;
-    }
+    ASSERT_GE(writeModuleParam("max_sock_tags", max_tags_str), 0) << "Failed to setup tag limit";
 
-    // ---------------
-    test_name = "tag quota reach limit";
-    testPrintI("* test: %s", test_name);
+    testPrintI("tag quota reach limit");
     for (int cnt = 0; cnt < max_tags; cnt++ ) {
-        uint64_t new_tag = valid_tag2 + ((uint64_t)cnt << 32);
-        res = doCtrlCommand("t %d %llu %u", sock0.fd, new_tag , fake_uid2);
-        if (res < 0) {
-                testPrintE("! %s: Unexpected results", test_name);
-                total_errors++;
-        }
-        if (!sock0.checkTag(new_tag, fake_uid2)) {
-                testPrintE("! %s: Unexpected results: tag not found", test_name);
-                total_errors++;
-        }
-    }
-    test_name = "tag quota go over limit";
-    testPrintI("* test: %s", test_name);
-    {
-        uint64_t new_tag = valid_tag2 + ((uint64_t)max_tags << 32);
-        res = doCtrlCommand("t %d %llu %u", sock0.fd, new_tag , fake_uid2);
-        if (res > 0) {
-                testPrintE("! %s: Unexpected results", test_name);
-                total_errors++;
-        }
-        if (!sock0.checkTag(valid_tag2 + (((uint64_t)max_tags - 1) << 32), fake_uid2)) {
-                testPrintE("! %s: Unexpected results: tag not found", test_name);
-                total_errors++;
-        }
+        uint64_t tag = valid_tag2 + ((uint64_t)cnt << 32);
+        EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, tag , fake_uid2), 0)
+            << "Tagging within limit failed";
+        EXPECT_TRUE(sock0.checkTag(tag, fake_uid2))<<  "Unexpected results: tag not found";
     }
 
-    // ---------------
-    test_name = "valid untag";
-    testPrintI("* test: %s", test_name);
-    res = doCtrlCommand("u %d", sock0.fd);
-    if (res < 0) {
-        testPrintE("! %s: Unexpected results", test_name);
-        total_errors++;
-    }
-    if (sock0.checkTag(valid_tag2 + (((uint64_t)max_tags - 1) << 32), fake_uid2)) {
-        testPrintE("! %s: Unexpected results: tag should not be there", test_name);
-        total_errors++;
-    }
+    testPrintI("tag quota go over limit");
+    uint64_t new_tag = valid_tag2 + ((uint64_t)max_tags << 32);
+    EXPECT_LT(doCtrlCommand("t %d %llu %u", sock0.fd, new_tag , fake_uid2), 0);
+    EXPECT_TRUE(sock0.checkTag(valid_tag2 + (((uint64_t)max_tags - 1) << 32),
+                               fake_uid2)) << "Unexpected results: tag not found";
 
-    // ---------------
-    test_name = "tag after untag shouldn't free up max tags";
-    testPrintI("* test: %s", test_name);
-    {
-        uint64_t new_tag = valid_tag2 + ((uint64_t)max_tags << 32);
-        res = doCtrlCommand("t %d %llu %u", sock0.fd, new_tag , fake_uid2);
-        if (res > 0) {
-                testPrintE("! %s: Unexpected results", test_name);
-                total_errors++;
-        }
-        if (sock0.checkTag(valid_tag2 + ((uint64_t)max_tags << 32), fake_uid2)) {
-            testPrintE("! %s: Unexpected results: tag should not be there", test_name);
-            total_errors++;
-        }
-    }
+    testPrintI("valid untag");
+    EXPECT_GE(doCtrlCommand("u %d", sock0.fd), 0);
+    EXPECT_FALSE(sock0.checkTag(valid_tag2 + (((uint64_t)max_tags - 1) << 32), fake_uid2))
+        << "Untagged tag should not be there";
 
-    // ---------------
-    test_name = "delete one tag";
-    testPrintI("* test: %s", test_name);
-    {
-            uint64_t new_tag = valid_tag2 + (((uint64_t)max_tags / 2) << 32);
-            res = doCtrlCommand("d %llu %u", new_tag, fake_uid2);
-            if (res < 0) {
-                    testPrintE("! %s: Unexpected results", test_name);
-                    total_errors++;
-            }
-    }
+    testPrintI("tag after untag should not free up max tags");
+    uint64_t new_tag2 = valid_tag2 + ((uint64_t)max_tags << 32);
+    EXPECT_LT(doCtrlCommand("t %d %llu %u", sock0.fd, new_tag2 , fake_uid2), 0);
+    EXPECT_FALSE(sock0.checkTag(valid_tag2 + ((uint64_t)max_tags << 32), fake_uid2))
+        << "Tag should not be there";
 
-    // ---------------
-    test_name = "2 tags after 1 delete pass/fail";
-    testPrintI("* test: %s", test_name);
-    {
-        uint64_t new_tag;
-        new_tag = valid_tag2 + (((uint64_t)max_tags + 1 ) << 32);
-        res = doCtrlCommand("t %d %llu %u", sock0.fd, new_tag , fake_uid2);
-        if (res < 0) {
-                testPrintE("! %s: Unexpected results", test_name);
-                total_errors++;
-        }
-        if (!sock0.checkTag(valid_tag2 + (((uint64_t)max_tags + 1) << 32), fake_uid2)) {
-            testPrintE("! %s: Unexpected results: tag not found", test_name);
-            total_errors++;
-        }
+    testPrintI("delete one tag");
+    uint64_t new_tag3 = valid_tag2 + (((uint64_t)max_tags / 2) << 32);
+    EXPECT_GE(doCtrlCommand("d %llu %u", new_tag3, fake_uid2), 0);
 
-        new_tag = valid_tag2 + (((uint64_t)max_tags + 2 ) << 32);
-        res = doCtrlCommand("t %d %llu %u", sock0.fd, new_tag , fake_uid2);
-        if (res > 0) {
-                testPrintE("! %s: Unexpected results", test_name);
-                total_errors++;
-        }
-        if (sock0.checkTag(valid_tag2 + (((uint64_t)max_tags + 2) << 32), fake_uid2)) {
-            testPrintE("! %s: Unexpected results: tag should not be there", test_name);
-            total_errors++;
-        }
-    }
+    testPrintI("2 tags after 1 delete pass/fail");
+    uint64_t new_tag4;
+    new_tag4 = valid_tag2 + (((uint64_t)max_tags + 1 ) << 32);
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, new_tag4 , fake_uid2), 0);
+    EXPECT_TRUE(sock0.checkTag(valid_tag2 + (((uint64_t)max_tags + 1) << 32), fake_uid2))
+        << "Tag not found";
+    new_tag4 = valid_tag2 + (((uint64_t)max_tags + 2 ) << 32);
+    EXPECT_LT(doCtrlCommand("t %d %llu %u", sock0.fd, new_tag4 , fake_uid2), 0);
+    EXPECT_FALSE(sock0.checkTag(valid_tag2 + (((uint64_t)max_tags + 2) << 32), fake_uid2))
+        << "Tag should not be there";
 
     /* TODO(jpa): test tagging two different sockets with same tags and
      * check refcounts  the tag_node should be +2
      */
-
-    // ---------------
-    if (total_errors) {
-        testPrintE("! Errors found");
-    } else {
-        testPrintI("No Errors found");
-    }
-
-done:
-    if (dev_fd >= 0) {
-        close(dev_fd);
-    }
-    if (ctrl_fd >= 0) {
-        close(ctrl_fd);
-    }
-    return total_errors;
 }
 
-/*----------------------------------------------------------------*/
-
-int main(int argc, char *argv[]) {
-    int res = 0;
-    res += testTagData();
-    res += testSocketTagging();
-    if (res) {
-        testPrintE("!! %d Errors found", res);
-    } else {
-        testPrintE("No Errors found");
-    }
-    return res;
+TEST_F(SocketTaggingTest, InsufficientArgsFails) {
+    // Insufficient args. Expected failure
+    EXPECT_LE(doCtrlCommand("t"), 0) << "Insufficient args, should fail.";
 }
+
+TEST_F(SocketTaggingTest, BadCommandFails) {
+    // Bad command. Expected failure";
+    EXPECT_LE(doCtrlCommand("?"), 0) << "Bad command, should fail";
+}
+
+TEST_F(SocketTaggingTest, NoTagNoUid) {
+    // no tag, no uid
+    EXPECT_GE(doCtrlCommand("t %d", sock0.fd), 0);
+    ASSERT_TRUE(sock0.checkTag(0, my_uid))  << "Tag not found";
+}
+
+TEST_F(SocketTaggingTest, InvalidTagFail) {
+    // Invalid tag. Expected failure
+    EXPECT_LE(doCtrlCommand("t %d %llu", sock0.fd, invalid_tag1), 0);
+    ASSERT_FALSE(sock0.checkTag(invalid_tag1, my_uid)) << "Tag should not be there";
+}
+
+TEST_F(SocketTaggingTest, ValidTagWithNoUid) {
+    // Valid tag with no uid
+    EXPECT_GE(doCtrlCommand("t %d %llu", sock0.fd, valid_tag1), 0);
+    EXPECT_TRUE(sock0.checkTag(valid_tag1, my_uid)) << "Tag not found";
+}
+
+TEST_F(SocketTaggingTest, ValidUntag) {
+    // Valid untag
+    EXPECT_GE(doCtrlCommand("t %d %llu", sock0.fd, valid_tag1), 0);
+    EXPECT_TRUE(sock0.checkTag(valid_tag1, my_uid)) << "Tag not found";
+    EXPECT_GE(doCtrlCommand("u %d", sock0.fd), 0);
+    EXPECT_FALSE(sock0.checkTag(valid_tag1, my_uid)) << "Tag should be removed";
+}
+
+TEST_F(SocketTaggingTest, ValidFirsttag) {
+    // Valid 1st tag
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag2, fake_uid), 0);
+    EXPECT_TRUE(sock0.checkTag(valid_tag2, fake_uid)) << "Tag not found.";
+}
+
+TEST_F(SocketTaggingTest, ValidReTag) {
+    // Valid re-tag
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag2, fake_uid), 0);
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag2, fake_uid), 0);
+    EXPECT_TRUE(sock0.checkTag(valid_tag2, fake_uid)) << "Tag not found.";
+}
+
+TEST_F(SocketTaggingTest, ValidReTagWithAcctTagChange) {
+    // Valid re-tag with acct_tag change
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag2, fake_uid), 0);
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag1, fake_uid), 0);
+    EXPECT_TRUE(sock0.checkTag(valid_tag1, fake_uid)) << "Tag not found.";
+}
+
+TEST_F(SocketTaggingTest, ReTagWithUidChange) {
+    // Re-tag with uid change
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag1, fake_uid), 0);
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag2, fake_uid2), 0);
+}
+
+TEST_F(SocketTaggingTest, Valid64BitAcctTag) {
+    // Valid 64bit acct tag
+    EXPECT_GE(doCtrlCommand("t %d %llu", sock0.fd, max_uint_tag), 0);
+    EXPECT_TRUE(sock0.checkTag(max_uint_tag, my_uid)) << "Tag not found.";
+}
+
+TEST_F(SocketTaggingTest, TagAnotherSocket) {
+    testPrintI("Tag two sockets");
+    EXPECT_GE(doCtrlCommand("t %d %llu", sock0.fd, max_uint_tag), 0);
+    EXPECT_GE(doCtrlCommand("t %d %llu %u", sock1.fd, valid_tag1, fake_uid2), 0);
+    EXPECT_TRUE(sock1.checkTag(valid_tag1, fake_uid2)) << "Tag not found.";
+    testPrintI("Untag socket0 of them only.");
+    EXPECT_GE(doCtrlCommand("u %d", sock0.fd), 0);
+    EXPECT_FALSE(sock0.checkTag(max_uint_tag, fake_uid)) << "Tag should not be there";
+    EXPECT_TRUE(sock1.checkTag(valid_tag1, fake_uid2)) << "Tag not found";
+    testPrintI("Now untag socket1 as well.");
+    EXPECT_GE(doCtrlCommand("u %d", sock1.fd), 0);
+    EXPECT_FALSE(sock1.checkTag(valid_tag1, fake_uid2)) << "Tag should not be there";
+}
+
+TEST_F(SocketTaggingTest, TagInvalidSocketFail) {
+    // Invalid tag. Expected failure
+    close(sock0.fd);
+    EXPECT_LE(doCtrlCommand("t %d %llu %u", sock0.fd, valid_tag1, my_uid), 0);
+    EXPECT_FALSE(sock0.checkTag(valid_tag1, my_uid)) << "Tag should not be there";
+}
+
+TEST_F(SocketTaggingTest, UntagInvalidSocketFail) {
+    // Invalid untag. Expected failure";
+    close(sock1.fd);
+    EXPECT_LE(doCtrlCommand("u %d", sock1.fd), 0);
+}
+
+}  // namespace android
diff --git a/tests/lib/testUtil/testUtil.c b/tests/lib/testUtil/testUtil.c
index d63fbba..b4edbd6 100644
--- a/tests/lib/testUtil/testUtil.c
+++ b/tests/lib/testUtil/testUtil.c
@@ -150,9 +150,9 @@
     va_start(args, fmt);
     vsnprintf(line, sizeof(line), fmt, args);
     if (stream == stderr) {
-        LOG(LOG_ERROR, logCatTag, "%s", line);
+        ALOG(LOG_ERROR, logCatTag, "%s", line);
     } else {
-        LOG(LOG_INFO, logCatTag, "%s", line);
+        ALOG(LOG_INFO, logCatTag, "%s", line);
     }
     vfprintf(stream, fmt, args);
     fputc('\n', stream);
diff --git a/tests/wifi/stress/wifiLoadScanAssoc.c b/tests/wifi/stress/wifiLoadScanAssoc.c
index 62b179c..17536e4 100644
--- a/tests/wifi/stress/wifiLoadScanAssoc.c
+++ b/tests/wifi/stress/wifiLoadScanAssoc.c
@@ -285,7 +285,7 @@
 
         // Start Supplicant
         randBind(&availCPU, &cpu);
-        if ((rv = wifi_start_supplicant()) != 0) {
+        if ((rv = wifi_start_supplicant(false)) != 0) {
             testPrintE("CPU: %i wifi_start_supplicant() failed, rv: %i\n",
                 cpu, rv);
             exit(21);
@@ -341,7 +341,7 @@
         }
 
         // Start supplicant
-        if ((rv = wifi_start_supplicant()) != 0) {
+        if ((rv = wifi_start_supplicant(false)) != 0) {
             testPrintE("main start supplicant failed, rv: %i", rv);
             exit(26);
         }