am 317b4024: Revert "Make encryption configurable"

* commit '317b4024a2a46b8c57abfa08f2a649df13572bd3':
  Revert "Make encryption configurable"
diff --git a/CleanSpec.mk b/CleanSpec.mk
index e78fc88..7f9536e 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -54,3 +54,4 @@
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/root/default.prop)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/recovery/root/default.prop)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/EXECUTABLES/lmkd_intermediates/import_includes)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/SHARED_LIBRARIES/libsysutils_intermediates/import_includes)
diff --git a/README b/README
deleted file mode 100644
index 0083247..0000000
--- a/README
+++ /dev/null
@@ -1,20 +0,0 @@
-
-The system/ directory is intended for pieces of the world that are the
-core of the embedded linux platform at the heart of Android.  These
-essential bits are required for basic booting, operation, and debugging.
-
-They should not depend on libraries outside of system/... (some of them
-do currently -- they need to be updated or changed) and they should not
-be required for the simulator build.
-
-The license for all these pieces should be clean (Apache2, BSD, or MIT).
-
-Currently system/bluetooth/... and system/extra/... have some pieces
-with GPL/LGPL licensed code.
-
-Assorted Issues:
-
-- pppd depends on libutils for logging
-- pppd depends on libcrypt/libcrypto
-- init, linker, debuggerd, toolbox, usbd depend on libcutils
-- should probably rename bionic to libc
diff --git a/ThirdPartyProject.prop b/ThirdPartyProject.prop
deleted file mode 100644
index 18b0594..0000000
--- a/ThirdPartyProject.prop
+++ /dev/null
@@ -1,10 +0,0 @@
-# Copyright 2010 Google Inc. All Rights Reserved.
-#Fri Jul 16 10:03:09 PDT 2010
-currentVersion=2.6.32
-version=2.6.32
-isNative=true
-feedurl=http\://kernel.org/pub/linux/kernel/v2.6/
-name=linux
-keywords=linux
-onDevice=true
-homepage=http\://kernel.org
diff --git a/adb/Android.mk b/adb/Android.mk
index b70c153..415952d 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -5,6 +5,35 @@
 
 LOCAL_PATH:= $(call my-dir)
 
+# libadb
+# =========================================================
+
+# Much of adb is duplicated in bootable/recovery/minadb and fastboot. Changes
+# made to adb rarely get ported to the other two, so the trees have diverged a
+# bit. We'd like to stop this because it is a maintenance nightmare, but the
+# divergence makes this difficult to do all at once. For now, we will start
+# small by moving common files into a static library. Hopefully some day we can
+# get enough of adb in here that we no longer need minadb. https://b/17626262
+LIBADB_SRC_FILES :=
+LIBADB_C_FLAGS := -Wall -Werror -D_XOPEN_SOURCE -D_GNU_SOURCE
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libadb
+LOCAL_CFLAGS := $(LIBADB_CFLAGS) -DADB_HOST=0
+LOCAL_SRC_FILES := $(LIBADB_SRC_FILES) fdevent.cpp
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libadb
+LOCAL_CFLAGS := $(LIBADB_CFLAGS) -DADB_HOST=1
+LOCAL_SRC_FILES := $(LIBADB_SRC_FILES)
+ifeq ($(HOST_OS),windows)
+    LOCAL_SRC_FILES += sysdeps_win32.c
+else
+    LOCAL_SRC_FILES += fdevent.cpp
+endif
+include $(BUILD_HOST_STATIC_LIBRARY)
+
 # adb host tool
 # =========================================================
 include $(CLEAR_VARS)
@@ -24,22 +53,13 @@
   USB_SRCS := usb_osx.c
   EXTRA_SRCS := get_my_path_darwin.c
   LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
-endif
-
-ifeq ($(HOST_OS),freebsd)
-  USB_SRCS := usb_libusb.c
-  EXTRA_SRCS := get_my_path_freebsd.c
-  LOCAL_LDLIBS += -lpthread -lusb
+  LOCAL_CFLAGS += -Wno-sizeof-pointer-memaccess -Wno-unused-parameter
 endif
 
 ifeq ($(HOST_OS),windows)
   USB_SRCS := usb_windows.c
   EXTRA_SRCS := get_my_path_windows.c
   EXTRA_STATIC_LIBS := AdbWinApi
-  ifneq ($(strip $(USE_CYGWIN)),)
-    # Pure cygwin case
-    LOCAL_LDLIBS += -lpthread -lgdi32
-  endif
   ifneq ($(strip $(USE_MINGW)),)
     # MinGW under Linux case
     LOCAL_LDLIBS += -lws2_32 -lgdi32
@@ -62,14 +82,9 @@
 	file_sync_client.c \
 	$(EXTRA_SRCS) \
 	$(USB_SRCS) \
-	usb_vendors.c
-
-LOCAL_C_INCLUDES += external/openssl/include
 
 ifneq ($(USE_SYSDEPS_WIN32),)
   LOCAL_SRC_FILES += sysdeps_win32.c
-else
-  LOCAL_SRC_FILES += fdevent.c
 endif
 
 LOCAL_CFLAGS += -O2 -g -DADB_HOST=1 -Wall -Wno-unused-parameter -Werror
@@ -77,11 +92,17 @@
 LOCAL_MODULE := adb
 LOCAL_MODULE_TAGS := debug
 
-LOCAL_STATIC_LIBRARIES := libzipfile libunz libcrypto_static $(EXTRA_STATIC_LIBS)
+LOCAL_STATIC_LIBRARIES := \
+    libadb \
+    libzipfile \
+    libcrypto_static \
+    $(EXTRA_STATIC_LIBS) \
+
 ifeq ($(USE_SYSDEPS_WIN32),)
 	LOCAL_STATIC_LIBRARIES += libcutils
 endif
 
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 include $(BUILD_HOST_EXECUTABLE)
 
 $(call dist-for-goals,dist_files sdk,$(LOCAL_BUILT_MODULE))
@@ -100,7 +121,6 @@
 
 LOCAL_SRC_FILES := \
 	adb.c \
-	fdevent.c \
 	transport.c \
 	transport_local.c \
 	transport_usb.c \
@@ -111,11 +131,16 @@
 	jdwp_service.c \
 	framebuffer_service.c \
 	remount_service.c \
-	disable_verity_service.c \
+	set_verity_enable_state_service.c \
 	usb_linux_client.c
 
-LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter -Werror
-LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
+LOCAL_CFLAGS := \
+	-O2 \
+	-g \
+	-DADB_HOST=0 \
+	-D_XOPEN_SOURCE \
+	-D_GNU_SOURCE \
+	-Wall -Wno-unused-parameter -Werror -Wno-deprecated-declarations \
 
 ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
 LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
@@ -132,55 +157,16 @@
 LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
 LOCAL_C_INCLUDES += system/extras/ext4_utils system/core/fs_mgr/include
 
-LOCAL_STATIC_LIBRARIES := liblog \
-	libfs_mgr \
-	libcutils \
-	libc \
-	libmincrypt \
-	libselinux \
-	libext4_utils_static
+LOCAL_STATIC_LIBRARIES := \
+    libadb \
+    libfs_mgr \
+    liblog \
+    libcutils \
+    libc \
+    libmincrypt \
+    libselinux \
+    libext4_utils_static \
+
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 
 include $(BUILD_EXECUTABLE)
-
-
-# adb host tool for device-as-host
-# =========================================================
-ifneq ($(SDK_ONLY),true)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
-	adb.c \
-	console.c \
-	transport.c \
-	transport_local.c \
-	transport_usb.c \
-	commandline.c \
-	adb_client.c \
-	adb_auth_host.c \
-	sockets.c \
-	services.c \
-	file_sync_client.c \
-	get_my_path_linux.c \
-	usb_linux.c \
-	usb_vendors.c \
-	fdevent.c
-
-LOCAL_CFLAGS := \
-	-O2 \
-	-g \
-	-DADB_HOST=1 \
-	-DADB_HOST_ON_TARGET=1 \
-	-Wall -Wno-unused-parameter -Werror \
-	-D_XOPEN_SOURCE \
-	-D_GNU_SOURCE
-
-LOCAL_C_INCLUDES += external/openssl/include
-
-LOCAL_MODULE := adb
-
-LOCAL_STATIC_LIBRARIES := libzipfile libunz libcutils liblog
-
-LOCAL_SHARED_LIBRARIES := libcrypto
-
-include $(BUILD_EXECUTABLE)
-endif
diff --git a/adb/adb.c b/adb/adb.c
index 10a1e0d..4258a01 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -41,8 +41,6 @@
 #include <sys/prctl.h>
 #include <getopt.h>
 #include <selinux/selinux.h>
-#else
-#include "usb_vendors.h"
 #endif
 
 #if ADB_TRACE
@@ -329,6 +327,7 @@
 }
 #endif
 
+#if ADB_HOST
 static void send_msg_with_okay(int fd, const char* msg, size_t msglen) {
     char header[9];
     if (msglen > 0xffff)
@@ -337,6 +336,7 @@
     writex(fd, header, 8);
     writex(fd, msg, msglen);
 }
+#endif // ADB_HOST
 
 static void send_connect(atransport *t)
 {
@@ -414,6 +414,7 @@
     send_connect(t);
 }
 
+#if ADB_HOST
 static char *connection_state_name(atransport *t)
 {
     if (t == NULL) {
@@ -437,6 +438,7 @@
         return "unknown";
     }
 }
+#endif // ADB_HOST
 
 /* qual_overwrite is used to overwrite a qualifier string.  dst is a
  * pointer to a char pointer.  It is assumed that if *dst is non-NULL, it
@@ -943,7 +945,7 @@
     return INSTALL_STATUS_INTERNAL_ERROR;
 }
 
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
 static BOOL WINAPI ctrlc_handler(DWORD type)
 {
     exit(STATUS_CONTROL_C_EXIT);
@@ -958,7 +960,7 @@
 
 void start_logging(void)
 {
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
     char    temp[ MAX_PATH ];
     FILE*   fnul;
     FILE*   flog;
@@ -1066,7 +1068,7 @@
 
 int launch_server(int server_port)
 {
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
     /* we need to start the server in the background                    */
     /* we create a PIPE that will be used to wait for the server's "OK" */
     /* message since the pipe handles must be inheritable, we use a     */
@@ -1123,10 +1125,11 @@
 
     /* get path of current program */
     GetModuleFileName( NULL, program_path, sizeof(program_path) );
-
+    char args[64];
+    snprintf(args, sizeof(args), "adb -P %d fork-server server",  server_port);
     ret = CreateProcess(
             program_path,                              /* program path  */
-            "adb fork-server server",
+            args,
                                     /* the fork-server argument will set the
                                        debug = 2 in the child           */
             NULL,                   /* process handle is not inheritable */
@@ -1165,7 +1168,7 @@
             return -1;
         }
     }
-#elif defined(HAVE_FORKEXEC)
+#else /* !defined(_WIN32) */
     char    path[PATH_MAX];
     int     fd[2];
 
@@ -1216,12 +1219,10 @@
 
         setsid();
     }
-#else
-#error "cannot implement background server start on this platform"
-#endif
+#endif /* !defined(_WIN32) */
     return 0;
 }
-#endif
+#endif /* ADB_HOST */
 
 /* Constructs a local name of form tcp:port.
  * target_str points to the target string, it's content will be overwritten.
@@ -1303,9 +1304,9 @@
 #endif
 
     atexit(adb_cleanup);
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
     SetConsoleCtrlHandler( ctrlc_handler, TRUE );
-#elif defined(HAVE_FORKEXEC)
+#else
     // No SIGCHLD. Let the service subproc handle its children.
     signal(SIGPIPE, SIG_IGN);
 #endif
@@ -1318,7 +1319,6 @@
 #ifdef WORKAROUND_BUG6558362
     if(is_daemon) adb_set_affinity();
 #endif
-    usb_vendors_init();
     usb_init();
     local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
     adb_auth_init();
@@ -1348,15 +1348,14 @@
     ** AID_ADB to access the USB driver
     ** AID_LOG to read system logs (adb logcat)
     ** AID_INPUT to diagnose input issues (getevent)
-    ** AID_INET to diagnose network issues (netcfg, ping)
-    ** AID_GRAPHICS to access the frame buffer
+    ** AID_INET to diagnose network issues (ping)
     ** AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump)
     ** AID_SDCARD_R to allow reading from the SD card
     ** AID_SDCARD_RW to allow writing to the SD card
     ** AID_NET_BW_STATS to read out qtaguid statistics
     */
-    gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_GRAPHICS,
-                       AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
+    gid_t groups[] = { AID_ADB, AID_LOG, AID_INPUT, AID_INET, AID_NET_BT,
+                       AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW,
                        AID_NET_BW_STATS };
     if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
         exit(1);
@@ -1421,10 +1420,10 @@
     if (is_daemon)
     {
         // inform our parent that we are up and running.
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
         DWORD  count;
         WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK\n", 3, &count, NULL );
-#elif defined(HAVE_FORKEXEC)
+#else
         fprintf(stderr, "OK\n");
 #endif
         start_logging();
@@ -1554,8 +1553,6 @@
 
 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
 {
-    atransport *transport = NULL;
-
     if(!strcmp(service, "kill")) {
         fprintf(stderr,"adb server killed by remote request\n");
         fflush(stdout);
@@ -1565,6 +1562,7 @@
     }
 
 #if ADB_HOST
+    atransport *transport = NULL;
     // "transport:" is used for switching transport with a specified serial number
     // "transport-usb:" is used for switching transport to the only USB transport
     // "transport-local:" is used for switching transport to the only local transport
diff --git a/adb/adb.h b/adb/adb.h
index 44e5981..6ac4731 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -20,6 +20,7 @@
 #include <limits.h>
 
 #include "adb_trace.h"
+#include "fdevent.h"
 #include "transport.h"  /* readx(), writex() */
 
 #define MAX_PAYLOAD 4096
@@ -328,8 +329,10 @@
 
 #if !ADB_HOST
 void framebuffer_service(int fd, void *cookie);
+// Allow enable-verity to write to system and vendor block devices
+int make_block_device_writable(const char* dev);
 void remount_service(int fd, void *cookie);
-void disable_verity_service(int fd, void* cookie);
+void set_verity_enabled_state_service(int fd, void* cookie);
 #endif
 
 /* packet allocator */
@@ -339,6 +342,9 @@
 int check_header(apacket *p);
 int check_data(apacket *p);
 
+// Define it if you want to dump packets.
+#define DEBUG_PACKETS 0
+
 #if !DEBUG_PACKETS
 #define print_packet(tag,p) do {} while (0)
 #endif
@@ -376,7 +382,6 @@
 int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
 #endif
 
-unsigned host_to_le32(unsigned n);
 int adb_commandline(int argc, char **argv);
 
 int connection_state(atransport *t);
diff --git a/adb/adb_auth_host.c b/adb/adb_auth_host.c
index dd83900..1d48667 100644
--- a/adb/adb_auth_host.c
+++ b/adb/adb_auth_host.c
@@ -48,6 +48,10 @@
 #include <openssl/rsa.h>
 #include <openssl/sha.h>
 
+#if defined(OPENSSL_IS_BORINGSSL)
+#include <openssl/base64.h>
+#endif
+
 #define TRACE_TAG TRACE_AUTH
 
 #define ANDROID_PATH   ".android"
@@ -151,43 +155,67 @@
 static int write_public_keyfile(RSA *private_key, const char *private_key_path)
 {
     RSAPublicKey pkey;
-    BIO *bio, *b64, *bfile;
+    FILE *outfile = NULL;
     char path[PATH_MAX], info[MAX_PAYLOAD];
-    int ret;
+    uint8_t *encoded = NULL;
+    size_t encoded_length;
+    int ret = 0;
 
-    ret = snprintf(path, sizeof(path), "%s.pub", private_key_path);
-    if (ret >= (signed)sizeof(path))
+    if (snprintf(path, sizeof(path), "%s.pub", private_key_path) >=
+        (int)sizeof(path)) {
+        D("Path too long while writing public key\n");
         return 0;
+    }
 
-    ret = RSA_to_RSAPublicKey(private_key, &pkey);
-    if (!ret) {
+    if (!RSA_to_RSAPublicKey(private_key, &pkey)) {
         D("Failed to convert to publickey\n");
         return 0;
     }
 
-    bfile = BIO_new_file(path, "w");
-    if (!bfile) {
+    outfile = fopen(path, "w");
+    if (!outfile) {
         D("Failed to open '%s'\n", path);
         return 0;
     }
 
     D("Writing public key to '%s'\n", path);
 
-    b64 = BIO_new(BIO_f_base64());
-    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
+#if defined(OPENSSL_IS_BORINGSSL)
+    if (!EVP_EncodedLength(&encoded_length, sizeof(pkey))) {
+        D("Public key too large to base64 encode");
+        goto out;
+    }
+#else
+    /* While we switch from OpenSSL to BoringSSL we have to implement
+     * |EVP_EncodedLength| here. */
+    encoded_length = 1 + ((sizeof(pkey) + 2) / 3 * 4);
+#endif
 
-    bio = BIO_push(b64, bfile);
-    BIO_write(bio, &pkey, sizeof(pkey));
-    (void) BIO_flush(bio);
-    BIO_pop(b64);
-    BIO_free(b64);
+    encoded = malloc(encoded_length);
+    if (encoded == NULL) {
+        D("Allocation failure");
+        goto out;
+    }
 
+    encoded_length = EVP_EncodeBlock(encoded, (uint8_t*) &pkey, sizeof(pkey));
     get_user_info(info, sizeof(info));
-    BIO_write(bfile, info, strlen(info));
-    (void) BIO_flush(bfile);
-    BIO_free_all(bfile);
 
-    return 1;
+    if (fwrite(encoded, encoded_length, 1, outfile) != 1 ||
+        fwrite(info, strlen(info), 1, outfile) != 1) {
+        D("Write error while writing public key");
+        goto out;
+    }
+
+    ret = 1;
+
+ out:
+    if (outfile != NULL) {
+        fclose(outfile);
+    }
+    if (encoded != NULL) {
+        free(encoded);
+    }
+    return ret;
 }
 
 static int generate_key(const char *file)
@@ -367,6 +395,11 @@
     unsigned int len;
     struct adb_private_key *key = node_to_item(node, struct adb_private_key, node);
 
+    if (token_size != TOKEN_SIZE) {
+        D("Unexpected token size %zd\n", token_size);
+        return 0;
+    }
+
     if (!RSA_sign(NID_sha1, token, token_size, sig, &len, key->rsa)) {
         return 0;
     }
diff --git a/adb/adb_client.c b/adb/adb_client.c
index eb1720d..ac5e15a 100644
--- a/adb/adb_client.c
+++ b/adb/adb_client.c
@@ -279,7 +279,7 @@
 
     fd = _adb_connect(service);
     if(fd == -1) {
-        D("_adb_connect error: %s\n", __adb_error);
+        D("_adb_connect error: %s", __adb_error);
     } else if(fd == -2) {
         fprintf(stderr,"** daemon still not running\n");
     }
diff --git a/adb/adb_trace.h b/adb/adb_trace.h
index 8a5d9f8..b8a2f4c 100644
--- a/adb/adb_trace.h
+++ b/adb/adb_trace.h
@@ -73,8 +73,9 @@
             if (ADB_TRACING) {                         \
                 int save_errno = errno;                \
                 adb_mutex_lock(&D_lock);               \
-                fprintf(stderr, "%s::%s():",           \
-                        __FILE__, __FUNCTION__);       \
+                fprintf(stderr, "%16s: %5d:%5lu | ",   \
+                        __FUNCTION__,                  \
+                        getpid(), adb_thread_id());    \
                 errno = save_errno;                    \
                 fprintf(stderr, __VA_ARGS__ );         \
                 fflush(stderr);                        \
@@ -96,15 +97,16 @@
         } while (0)
 #  define  DD(...)                                     \
         do {                                           \
-          int save_errno = errno;                      \
-          adb_mutex_lock(&D_lock);                     \
-          fprintf(stderr, "%s::%s():",                 \
-                  __FILE__, __FUNCTION__);             \
-          errno = save_errno;                          \
-          fprintf(stderr, __VA_ARGS__ );               \
-          fflush(stderr);                              \
-          adb_mutex_unlock(&D_lock);                   \
-          errno = save_errno;                          \
+            int save_errno = errno;                    \
+            adb_mutex_lock(&D_lock);                   \
+            fprintf(stderr, "%16s: %5d:%5lu | ",       \
+                    __FUNCTION__,                      \
+                    getpid(), adb_thread_id());        \
+            errno = save_errno;                        \
+            fprintf(stderr, __VA_ARGS__ );             \
+            fflush(stderr);                            \
+            adb_mutex_unlock(&D_lock);                 \
+            errno = save_errno;                        \
         } while (0)
 #else
 #  define  D(...)                                      \
diff --git a/adb/commandline.c b/adb/commandline.c
index 7704878..a06885b 100644
--- a/adb/commandline.c
+++ b/adb/commandline.c
@@ -29,7 +29,7 @@
 
 #include "sysdeps.h"
 
-#ifdef HAVE_TERMIO_H
+#if !defined(_WIN32)
 #include <termios.h>
 #endif
 
@@ -191,6 +191,7 @@
         "  adb restore <file>           - restore device contents from the <file> backup archive\n"
         "\n"
         "  adb disable-verity           - disable dm-verity checking on USERDEBUG builds\n"
+        "  adb enable-verity            - re-enable dm-verity checking on USERDEBUG builds\n"
         "  adb keygen <file>            - generate adb public/private key. The private key is stored in <file>,\n"
         "                                 and the public key is stored in <file>.pub. Any existing files\n"
         "                                 are overwritten.\n"
@@ -239,7 +240,18 @@
     return 1;
 }
 
-#ifdef HAVE_TERMIO_H
+#if defined(_WIN32)
+
+// Windows does not have <termio.h>.
+static void stdin_raw_init(int fd) {
+
+}
+
+static void stdin_raw_restore(int fd) {
+
+}
+
+#else
 static struct termios tio_save;
 
 static void stdin_raw_init(int fd)
@@ -313,11 +325,11 @@
     long total = 0;
 
     D("copy_to_file(%d -> %d)\n", inFd, outFd);
-#ifdef HAVE_TERMIO_H
+
     if (inFd == STDIN_FILENO) {
         stdin_raw_init(STDIN_FILENO);
     }
-#endif
+
     for (;;) {
         if (inFd == STDIN_FILENO) {
             len = unix_read(inFd, buf, BUFSIZE);
@@ -344,11 +356,11 @@
         }
         total += len;
     }
-#ifdef HAVE_TERMIO_H
+
     if (inFd == STDIN_FILENO) {
         stdin_raw_restore(STDIN_FILENO);
     }
-#endif
+
     D("copy_to_file() finished after %lu bytes\n", total);
     free(buf);
 }
@@ -389,9 +401,7 @@
             case '.':
                 if(state == 2) {
                     fprintf(stderr,"\n* disconnect *\n");
-#ifdef HAVE_TERMIO_H
                     stdin_raw_restore(fdi);
-#endif
                     exit(0);
                 }
             default:
@@ -423,14 +433,10 @@
     fds[0] = fd;
     fds[1] = fdi;
 
-#ifdef HAVE_TERMIO_H
     stdin_raw_init(fdi);
-#endif
     adb_thread_create(&thr, stdin_read_thread, fds);
     read_and_dump(fd);
-#ifdef HAVE_TERMIO_H
     stdin_raw_restore(fdi);
-#endif
     return 0;
 }
 
@@ -466,7 +472,7 @@
     }
 
     int opt = CHUNK_SIZE;
-    opt = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
+    opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
 
     total = sz;
     ptr = data;
@@ -575,7 +581,7 @@
     }
 
     int opt = SIDELOAD_HOST_BLOCK_SIZE;
-    opt = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
+    opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
 
     int last_percent = -1;
     for (;;) {
@@ -733,7 +739,7 @@
  */
 int ppp(int argc, char **argv)
 {
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
     fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
     return -1;
 #else
@@ -796,7 +802,7 @@
         adb_close(fd);
         return 0;
     }
-#endif /* !HAVE_WIN32_PROC */
+#endif /* !defined(_WIN32) */
 }
 
 static int send_shellcommand(transport_type transport, char* serial, char* buf)
@@ -1162,17 +1168,17 @@
     }
 
     /* modifiers and flags */
-    while(argc > 0) {
-        if(!strcmp(argv[0],"server")) {
+    while (argc > 0) {
+        if (!strcmp(argv[0],"server")) {
             is_server = 1;
-        } else if(!strcmp(argv[0],"nodaemon")) {
+        } else if (!strcmp(argv[0],"nodaemon")) {
             no_daemon = 1;
         } else if (!strcmp(argv[0], "fork-server")) {
             /* this is a special flag used only when the ADB client launches the ADB Server */
             is_daemon = 1;
-        } else if(!strcmp(argv[0],"persist")) {
+        } else if (!strcmp(argv[0],"persist")) {
             persist = 1;
-        } else if(!strncmp(argv[0], "-p", 2)) {
+        } else if (!strncmp(argv[0], "-p", 2)) {
             const char *product = NULL;
             if (argv[0][2] == '\0') {
                 if (argc < 2) return usage();
@@ -1192,7 +1198,7 @@
             if (isdigit(argv[0][2])) {
                 serial = argv[0] + 2;
             } else {
-                if(argc < 2 || argv[0][2] != '\0') return usage();
+                if (argc < 2 || argv[0][2] != '\0') return usage();
                 serial = argv[1];
                 argc--;
                 argv++;
@@ -1203,7 +1209,7 @@
             ttype = kTransportLocal;
         } else if (!strcmp(argv[0],"-a")) {
             gListenAll = 1;
-        } else if(!strncmp(argv[0], "-H", 2)) {
+        } else if (!strncmp(argv[0], "-H", 2)) {
             const char *hostname = NULL;
             if (argv[0][2] == '\0') {
                 if (argc < 2) return usage();
@@ -1215,7 +1221,7 @@
             }
             adb_set_tcp_name(hostname);
 
-        } else if(!strncmp(argv[0], "-P", 2)) {
+        } else if (!strncmp(argv[0], "-P", 2)) {
             if (argv[0][2] == '\0') {
                 if (argc < 2) return usage();
                 server_port_str = argv[1];
@@ -1254,20 +1260,51 @@
         } else {
             r = launch_server(server_port);
         }
-        if(r) {
+        if (r) {
             fprintf(stderr,"* could not start server *\n");
         }
         return r;
     }
 
-top:
-    if(argc == 0) {
+    if (argc == 0) {
         return usage();
     }
 
-    /* adb_connect() commands */
+    /* handle wait-for-* prefix */
+    if (!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
+        char* service = argv[0];
+        if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) {
+            if (ttype == kTransportUsb) {
+                service = "wait-for-usb";
+            } else if (ttype == kTransportLocal) {
+                service = "wait-for-local";
+            } else {
+                service = "wait-for-any";
+            }
+        }
 
-    if(!strcmp(argv[0], "devices")) {
+        format_host_command(buf, sizeof buf, service, ttype, serial);
+
+        if (adb_command(buf)) {
+            D("failure: %s *\n",adb_error());
+            fprintf(stderr,"error: %s\n", adb_error());
+            return 1;
+        }
+
+        /* Allow a command to be run after wait-for-device,
+            * e.g. 'adb wait-for-device shell'.
+            */
+        if (argc == 1) {
+            return 0;
+        }
+
+        /* Fall through */
+        argc--;
+        argv++;
+    }
+
+    /* adb_connect() commands */
+    if (!strcmp(argv[0], "devices")) {
         char *tmp;
         char *listopt;
         if (argc < 2)
@@ -1280,7 +1317,7 @@
         }
         snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);
         tmp = adb_query(buf);
-        if(tmp) {
+        if (tmp) {
             printf("List of devices attached \n");
             printf("%s\n", tmp);
             return 0;
@@ -1288,8 +1325,7 @@
             return 1;
         }
     }
-
-    if(!strcmp(argv[0], "connect")) {
+    else if (!strcmp(argv[0], "connect")) {
         char *tmp;
         if (argc != 2) {
             fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
@@ -1297,15 +1333,14 @@
         }
         snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
         tmp = adb_query(buf);
-        if(tmp) {
+        if (tmp) {
             printf("%s\n", tmp);
             return 0;
         } else {
             return 1;
         }
     }
-
-    if(!strcmp(argv[0], "disconnect")) {
+    else if (!strcmp(argv[0], "disconnect")) {
         char *tmp;
         if (argc > 2) {
             fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
@@ -1317,19 +1352,17 @@
             snprintf(buf, sizeof buf, "host:disconnect:");
         }
         tmp = adb_query(buf);
-        if(tmp) {
+        if (tmp) {
             printf("%s\n", tmp);
             return 0;
         } else {
             return 1;
         }
     }
-
-    if (!strcmp(argv[0], "emu")) {
+    else if (!strcmp(argv[0], "emu")) {
         return adb_send_emulator_command(argc, argv);
     }
-
-    if(!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
+    else if (!strcmp(argv[0], "shell") || !strcmp(argv[0], "hell")) {
         int r;
         int fd;
 
@@ -1340,7 +1373,7 @@
             fflush(stdout);
         }
 
-        if(argc < 2) {
+        if (argc < 2) {
             D("starting interactive shell\n");
             r = interactive_shell();
             if (h) {
@@ -1363,7 +1396,7 @@
         for(;;) {
             D("interactive shell loop. buff=%s\n", buf);
             fd = adb_connect(buf);
-            if(fd >= 0) {
+            if (fd >= 0) {
                 D("about to read_and_dump(fd=%d)\n", fd);
                 read_and_dump(fd);
                 D("read_and_dump() done.\n");
@@ -1374,7 +1407,7 @@
                 r = -1;
             }
 
-            if(persist) {
+            if (persist) {
                 fprintf(stderr,"\n- waiting for device -\n");
                 adb_sleep_ms(1000);
                 do_cmd(ttype, serial, "wait-for-device", 0);
@@ -1388,8 +1421,7 @@
             }
         }
     }
-
-    if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) {
+    else if (!strcmp(argv[0], "exec-in") || !strcmp(argv[0], "exec-out")) {
         int exec_in = !strcmp(argv[0], "exec-in");
         int fd;
 
@@ -1418,30 +1450,31 @@
         adb_close(fd);
         return 0;
     }
-
-    if(!strcmp(argv[0], "kill-server")) {
+    else if (!strcmp(argv[0], "kill-server")) {
         int fd;
         fd = _adb_connect("host:kill");
-        if(fd == -1) {
+        if (fd == -1) {
             fprintf(stderr,"* server not running *\n");
             return 1;
         }
         return 0;
     }
-
-    if(!strcmp(argv[0], "sideload")) {
-        if(argc != 2) return usage();
+    else if (!strcmp(argv[0], "sideload")) {
+        if (argc != 2) return usage();
         if (adb_sideload_host(argv[1])) {
             return 1;
         } else {
             return 0;
         }
     }
-
-    if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot")
-            || !strcmp(argv[0], "reboot-bootloader")
-            || !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb")
-            || !strcmp(argv[0], "root") || !strcmp(argv[0], "disable-verity")) {
+    else if (!strcmp(argv[0], "remount") ||
+             !strcmp(argv[0], "reboot") ||
+             !strcmp(argv[0], "reboot-bootloader") ||
+             !strcmp(argv[0], "tcpip") ||
+             !strcmp(argv[0], "usb") ||
+             !strcmp(argv[0], "root") ||
+             !strcmp(argv[0], "disable-verity") ||
+             !strcmp(argv[0], "enable-verity")) {
         char command[100];
         if (!strcmp(argv[0], "reboot-bootloader"))
             snprintf(command, sizeof(command), "reboot:bootloader");
@@ -1450,7 +1483,7 @@
         else
             snprintf(command, sizeof(command), "%s:", argv[0]);
         int fd = adb_connect(command);
-        if(fd >= 0) {
+        if (fd >= 0) {
             read_and_dump(fd);
             adb_close(fd);
             return 0;
@@ -1458,49 +1491,13 @@
         fprintf(stderr,"error: %s\n", adb_error());
         return 1;
     }
-
-    if(!strcmp(argv[0], "bugreport")) {
+    else if (!strcmp(argv[0], "bugreport")) {
         if (argc != 1) return usage();
         do_cmd(ttype, serial, "shell", "bugreport", 0);
         return 0;
     }
-
     /* adb_command() wrapper commands */
-
-    if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
-        char* service = argv[0];
-        if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) {
-            if (ttype == kTransportUsb) {
-                service = "wait-for-usb";
-            } else if (ttype == kTransportLocal) {
-                service = "wait-for-local";
-            } else {
-                service = "wait-for-any";
-            }
-        }
-
-        format_host_command(buf, sizeof buf, service, ttype, serial);
-
-        if (adb_command(buf)) {
-            D("failure: %s *\n",adb_error());
-            fprintf(stderr,"error: %s\n", adb_error());
-            return 1;
-        }
-
-        /* Allow a command to be run after wait-for-device,
-            * e.g. 'adb wait-for-device shell'.
-            */
-        if(argc > 1) {
-            argc--;
-            argv++;
-            goto top;
-        }
-        return 0;
-    }
-
-    if(!strcmp(argv[0], "forward") ||
-       !strcmp(argv[0], "reverse"))
-    {
+    else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
         char host_prefix[64];
         char reverse = (char) !strcmp(argv[0], "reverse");
         char remove = 0;
@@ -1581,25 +1578,22 @@
         {
           if (argc != 3)
             return usage();
-          const char* command = no_rebind ? "forward:norebind:" : "forward";
+          const char* command = no_rebind ? "forward:norebind" : "forward";
           snprintf(buf, sizeof buf, "%s:%s:%s;%s", host_prefix, command, argv[1], argv[2]);
         }
 
-        if(adb_command(buf)) {
+        if (adb_command(buf)) {
             fprintf(stderr,"error: %s\n", adb_error());
             return 1;
         }
         return 0;
     }
-
     /* do_sync_*() commands */
-
-    if(!strcmp(argv[0], "ls")) {
-        if(argc != 2) return usage();
+    else if (!strcmp(argv[0], "ls")) {
+        if (argc != 2) return usage();
         return do_sync_ls(argv[1]);
     }
-
-    if(!strcmp(argv[0], "push")) {
+    else if (!strcmp(argv[0], "push")) {
         int show_progress = 0;
         int copy_attrs = 0; // unused
         const char* lpath = NULL, *rpath = NULL;
@@ -1612,8 +1606,7 @@
 
         return usage();
     }
-
-    if(!strcmp(argv[0], "pull")) {
+    else if (!strcmp(argv[0], "pull")) {
         int show_progress = 0;
         int copy_attrs = 0;
         const char* rpath = NULL, *lpath = ".";
@@ -1626,28 +1619,24 @@
 
         return usage();
     }
-
-    if (!strcmp(argv[0], "install")) {
+    else if (!strcmp(argv[0], "install")) {
         if (argc < 2) return usage();
         return install_app(ttype, serial, argc, argv);
     }
-
-    if (!strcmp(argv[0], "install-multiple")) {
+    else if (!strcmp(argv[0], "install-multiple")) {
         if (argc < 2) return usage();
         return install_multiple_app(ttype, serial, argc, argv);
     }
-
-    if (!strcmp(argv[0], "uninstall")) {
+    else if (!strcmp(argv[0], "uninstall")) {
         if (argc < 2) return usage();
         return uninstall_app(ttype, serial, argc, argv);
     }
-
-    if(!strcmp(argv[0], "sync")) {
+    else if (!strcmp(argv[0], "sync")) {
         char *srcarg, *android_srcpath, *data_srcpath, *vendor_srcpath;
         int listonly = 0;
 
         int ret;
-        if(argc < 2) {
+        if (argc < 2) {
             /* No local path was specified. */
             srcarg = NULL;
         } else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
@@ -1657,20 +1646,20 @@
             } else {
                 srcarg = NULL;
             }
-        } else if(argc == 2) {
+        } else if (argc == 2) {
             /* A local path or "android"/"data" arg was specified. */
             srcarg = argv[1];
         } else {
             return usage();
         }
         ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath, &vendor_srcpath);
-        if(ret != 0) return usage();
+        if (ret != 0) return usage();
 
-        if(android_srcpath != NULL)
+        if (android_srcpath != NULL)
             ret = do_sync_sync(android_srcpath, "/system", listonly);
-        if(ret == 0 && vendor_srcpath != NULL)
+        if (ret == 0 && vendor_srcpath != NULL)
             ret = do_sync_sync(vendor_srcpath, "/vendor", listonly);
-        if(ret == 0 && data_srcpath != NULL)
+        if (ret == 0 && data_srcpath != NULL)
             ret = do_sync_sync(data_srcpath, "/data", listonly);
 
         free(android_srcpath);
@@ -1678,10 +1667,8 @@
         free(data_srcpath);
         return ret;
     }
-
     /* passthrough commands */
-
-    if(!strcmp(argv[0],"get-state") ||
+    else if (!strcmp(argv[0],"get-state") ||
         !strcmp(argv[0],"get-serialno") ||
         !strcmp(argv[0],"get-devpath"))
     {
@@ -1689,47 +1676,38 @@
 
         format_host_command(buf, sizeof buf, argv[0], ttype, serial);
         tmp = adb_query(buf);
-        if(tmp) {
+        if (tmp) {
             printf("%s\n", tmp);
             return 0;
         } else {
             return 1;
         }
     }
-
     /* other commands */
-
-    if(!strcmp(argv[0],"status-window")) {
+    else if (!strcmp(argv[0],"status-window")) {
         status_window(ttype, serial);
         return 0;
     }
-
-    if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
+    else if (!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat") || !strcmp(argv[0],"longcat")) {
         return logcat(ttype, serial, argc, argv);
     }
-
-    if(!strcmp(argv[0],"ppp")) {
+    else if (!strcmp(argv[0],"ppp")) {
         return ppp(argc, argv);
     }
-
-    if (!strcmp(argv[0], "start-server")) {
+    else if (!strcmp(argv[0], "start-server")) {
         return adb_connect("host:start-server");
     }
-
-    if (!strcmp(argv[0], "backup")) {
+    else if (!strcmp(argv[0], "backup")) {
         return backup(argc, argv);
     }
-
-    if (!strcmp(argv[0], "restore")) {
+    else if (!strcmp(argv[0], "restore")) {
         return restore(argc, argv);
     }
-
-    if (!strcmp(argv[0], "keygen")) {
+    else if (!strcmp(argv[0], "keygen")) {
         if (argc < 2) return usage();
         return adb_auth_keygen(argv[1]);
     }
-
-    if (!strcmp(argv[0], "jdwp")) {
+    else if (!strcmp(argv[0], "jdwp")) {
         int  fd = adb_connect("jdwp");
         if (fd >= 0) {
             read_and_dump(fd);
@@ -1740,14 +1718,12 @@
             return -1;
         }
     }
-
     /* "adb /?" is a common idiom under Windows */
-    if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
+    else if (!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
         help();
         return 0;
     }
-
-    if(!strcmp(argv[0], "version")) {
+    else if (!strcmp(argv[0], "version")) {
         version(stdout);
         return 0;
     }
diff --git a/adb/disable_verity_service.c b/adb/disable_verity_service.c
deleted file mode 100644
index ed3da52..0000000
--- a/adb/disable_verity_service.c
+++ /dev/null
@@ -1,199 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "sysdeps.h"
-
-#define  TRACE_TAG  TRACE_ADB
-#include "adb.h"
-
-#include <stdio.h>
-#include <stdarg.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <inttypes.h>
-
-#include "cutils/properties.h"
-#include "ext4_sb.h"
-#include <fs_mgr.h>
-
-#define FSTAB_PREFIX "/fstab."
-struct fstab *fstab;
-
-__attribute__((__format__(printf, 2, 3))) __nonnull((2))
-static void write_console(int fd, const char* format, ...)
-{
-    char buffer[256];
-    va_list args;
-    va_start (args, format);
-    vsnprintf (buffer, sizeof(buffer), format, args);
-    va_end (args);
-
-    adb_write(fd, buffer, strnlen(buffer, sizeof(buffer)));
-}
-
-static int get_target_device_size(int fd, const char *blk_device,
-                                  uint64_t *device_size)
-{
-    int data_device;
-    struct ext4_super_block sb;
-    struct fs_info info;
-
-    info.len = 0;  /* Only len is set to 0 to ask the device for real size. */
-
-    data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
-    if (data_device < 0) {
-        write_console(fd, "Error opening block device (%s)\n", strerror(errno));
-        return -1;
-    }
-
-    if (lseek64(data_device, 1024, SEEK_SET) < 0) {
-        write_console(fd, "Error seeking to superblock\n");
-        adb_close(data_device);
-        return -1;
-    }
-
-    if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
-        write_console(fd, "Error reading superblock\n");
-        adb_close(data_device);
-        return -1;
-    }
-
-    ext4_parse_sb(&sb, &info);
-    *device_size = info.len;
-
-    adb_close(data_device);
-    return 0;
-}
-
-static int disable_verity(int fd, const char *block_device,
-                          const char* mount_point)
-{
-    uint32_t magic_number;
-    const uint32_t voff = VERITY_METADATA_MAGIC_DISABLE;
-    uint64_t device_length;
-    int device;
-    int retval = -1;
-
-    device = adb_open(block_device, O_RDWR | O_CLOEXEC);
-    if (device == -1) {
-        write_console(fd, "Could not open block device %s (%s).\n",
-                      block_device, strerror(errno));
-        write_console(fd, "Maybe run adb remount?\n");
-        goto errout;
-    }
-
-    // find the start of the verity metadata
-    if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
-        write_console(fd, "Could not get target device size.\n");
-        goto errout;
-    }
-
-    if (lseek64(device, device_length, SEEK_SET) < 0) {
-        write_console(fd,
-                      "Could not seek to start of verity metadata block.\n");
-        goto errout;
-    }
-
-    // check the magic number
-    if (adb_read(device, &magic_number, sizeof(magic_number))
-             != sizeof(magic_number)) {
-        write_console(fd, "Couldn't read magic number!\n");
-        goto errout;
-    }
-
-    if (magic_number == VERITY_METADATA_MAGIC_DISABLE) {
-        write_console(fd, "Verity already disabled on %s\n", mount_point);
-        goto errout;
-    }
-
-    if (magic_number != VERITY_METADATA_MAGIC_NUMBER) {
-        write_console(fd,
-                      "Couldn't find verity metadata at offset %"PRIu64"!\n",
-                      device_length);
-        goto errout;
-    }
-
-    if (lseek64(device, device_length, SEEK_SET) < 0) {
-        write_console(fd,
-                      "Could not seek to start of verity metadata block.\n");
-        goto errout;
-    }
-
-    if (adb_write(device, &voff, sizeof(voff)) != sizeof(voff)) {
-        write_console(fd, "Could not set verity disabled flag on device %s\n",
-                      block_device);
-        goto errout;
-    }
-
-    write_console(fd, "Verity disabled on %s\n", mount_point);
-    retval = 0;
-errout:
-    if (device != -1)
-        adb_close(device);
-    return retval;
-}
-
-void disable_verity_service(int fd, void* cookie)
-{
-#ifdef ALLOW_ADBD_DISABLE_VERITY
-    char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
-    char propbuf[PROPERTY_VALUE_MAX];
-    int i;
-    bool any_disabled = false;
-
-    property_get("ro.secure", propbuf, "0");
-    if (strcmp(propbuf, "1")) {
-        write_console(fd, "verity not enabled - ENG build\n");
-        goto errout;
-    }
-
-    property_get("ro.debuggable", propbuf, "0");
-    if (strcmp(propbuf, "1")) {
-        write_console(fd, "verity cannot be disabled - USER build\n");
-        goto errout;
-    }
-
-    property_get("ro.hardware", propbuf, "");
-    snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
-
-    fstab = fs_mgr_read_fstab(fstab_filename);
-    if (!fstab) {
-        write_console(fd, "Failed to open %s\nMaybe run adb root?\n",
-                      fstab_filename);
-        goto errout;
-    }
-
-    /* Loop through entries looking for ones that vold manages */
-    for (i = 0; i < fstab->num_entries; i++) {
-        if(fs_mgr_is_verified(&fstab->recs[i])) {
-            if (!disable_verity(fd, fstab->recs[i].blk_device,
-                                fstab->recs[i].mount_point)) {
-                any_disabled = true;
-            }
-       }
-    }
-
-    if (any_disabled) {
-        write_console(fd,
-                      "Now reboot your device for settings to take effect\n");
-    }
-#else
-    write_console(fd, "disable-verity only works for userdebug builds\n");
-#endif
-
-errout:
-    adb_close(fd);
-}
diff --git a/adb/fdevent.c b/adb/fdevent.cpp
similarity index 96%
rename from adb/fdevent.c
rename to adb/fdevent.cpp
index 43e600c..bd0f3b2 100644
--- a/adb/fdevent.c
+++ b/adb/fdevent.cpp
@@ -88,6 +88,12 @@
 static fdevent list_pending = {
     .next = &list_pending,
     .prev = &list_pending,
+    .fd = -1,
+    .force_eof = 0,
+    .state = 0,
+    .events = 0,
+    .func = nullptr,
+    .arg = nullptr,
 };
 
 static fdevent **fd_table = 0;
@@ -312,7 +318,7 @@
 }
 
 #if !DEBUG
-static inline void dump_all_fds(const char *extra_msg) {}
+static inline void dump_all_fds(const char* /* extra_msg */) {}
 #else
 static void dump_all_fds(const char *extra_msg)
 {
@@ -434,7 +440,8 @@
         while(fd_table_max <= fde->fd) {
             fd_table_max *= 2;
         }
-        fd_table = realloc(fd_table, sizeof(fdevent*) * fd_table_max);
+        fd_table = reinterpret_cast<fdevent**>(
+            realloc(fd_table, sizeof(fdevent*) * fd_table_max));
         if(fd_table == 0) {
             FATAL("could not expand fd_table to %d entries\n", fd_table_max);
         }
@@ -505,7 +512,8 @@
     fde->func(fde->fd, events, fde->arg);
 }
 
-static void fdevent_subproc_event_func(int fd, unsigned ev, void *userdata)
+static void fdevent_subproc_event_func(int fd, unsigned ev,
+                                       void* /* userdata */)
 {
 
     D("subproc handling on fd=%d ev=%04x\n", fd, ev);
@@ -661,6 +669,8 @@
     if(adb_socketpair(s)) {
         FATAL("cannot create shell-exit socket-pair\n");
     }
+    D("socketpair: (%d,%d)", s[0], s[1]);
+
     SHELL_EXIT_NOTIFY_FD = s[0];
     fdevent *fde;
     fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL);
diff --git a/adb/fdevent.h b/adb/fdevent.h
index a0ebe2a..a8102ca 100644
--- a/adb/fdevent.h
+++ b/adb/fdevent.h
@@ -19,6 +19,10 @@
 
 #include <stdint.h>  /* for int64_t */
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* events that may be observed */
 #define FDE_READ              0x0001
 #define FDE_WRITE             0x0002
@@ -64,20 +68,22 @@
 */
 void fdevent_loop();
 
-struct fdevent 
-{
+struct fdevent {
     fdevent *next;
     fdevent *prev;
 
     int fd;
     int force_eof;
 
-    unsigned short state;
-    unsigned short events;
+    uint16_t state;
+    uint16_t events;
 
     fd_func func;
     void *arg;
 };
 
+#ifdef __cplusplus
+}
+#endif
 
 #endif
diff --git a/adb/file_sync_client.c b/adb/file_sync_client.c
index ad59e81..ee09d5d 100644
--- a/adb/file_sync_client.c
+++ b/adb/file_sync_client.c
@@ -309,7 +309,9 @@
     return err;
 }
 
-#ifdef HAVE_SYMLINKS
+#if defined(_WIN32)
+extern int write_data_link(int fd, const char *path, syncsendbuf *sbuf) __attribute__((error("no symlinks on Windows")));
+#else
 static int write_data_link(int fd, const char *path, syncsendbuf *sbuf)
 {
     int len, ret;
@@ -364,10 +366,8 @@
         free(file_buffer);
     } else if (S_ISREG(mode))
         write_data_file(fd, lpath, sbuf, show_progress);
-#ifdef HAVE_SYMLINKS
     else if (S_ISLNK(mode))
         write_data_link(fd, lpath, sbuf);
-#endif
     else
         goto fail;
 
@@ -893,6 +893,21 @@
     return r1 ? : r2;
 }
 
+/* Return a copy of the path string with / appended if needed */
+static char *add_slash_to_path(const char *path)
+{
+    if (path[strlen(path) - 1] != '/') {
+        size_t len = strlen(path) + 2;
+        char *path_with_slash = malloc(len);
+        if (path_with_slash == NULL)
+            return NULL;
+        snprintf(path_with_slash, len, "%s/", path);
+        return path_with_slash;
+    } else {
+        return strdup(path);
+    }
+}
+
 static int copy_remote_dir_local(int fd, const char *rpath, const char *lpath,
                                  int copy_attrs)
 {
@@ -900,28 +915,32 @@
     copyinfo *ci, *next;
     int pulled = 0;
     int skipped = 0;
+    char *rpath_clean = NULL;
+    char *lpath_clean = NULL;
+    int ret = 0;
+
+    if (rpath[0] == '\0' || lpath[0] == '\0') {
+        ret = -1;
+        goto finish;
+    }
 
     /* Make sure that both directory paths end in a slash. */
-    if (rpath[0] == 0 || lpath[0] == 0) return -1;
-    if (rpath[strlen(rpath) - 1] != '/') {
-        int  tmplen = strlen(rpath) + 2;
-        char *tmp = malloc(tmplen);
-        if (tmp == 0) return -1;
-        snprintf(tmp, tmplen, "%s/", rpath);
-        rpath = tmp;
+    rpath_clean = add_slash_to_path(rpath);
+    if (!rpath_clean) {
+        ret = -1;
+        goto finish;
     }
-    if (lpath[strlen(lpath) - 1] != '/') {
-        int  tmplen = strlen(lpath) + 2;
-        char *tmp = malloc(tmplen);
-        if (tmp == 0) return -1;
-        snprintf(tmp, tmplen, "%s/", lpath);
-        lpath = tmp;
+    lpath_clean = add_slash_to_path(lpath);
+    if (!lpath_clean) {
+        ret = -1;
+        goto finish;
     }
 
-    fprintf(stderr, "pull: building file list...\n");
     /* Recursively build the list of files to copy. */
-    if (remote_build_list(fd, &filelist, rpath, lpath)) {
-        return -1;
+    fprintf(stderr, "pull: building file list...\n");
+    if (remote_build_list(fd, &filelist, rpath_clean, lpath_clean)) {
+        ret = -1;
+        goto finish;
     }
 
     for (ci = filelist; ci != 0; ci = next) {
@@ -929,11 +948,13 @@
         if (ci->flag == 0) {
             fprintf(stderr, "pull: %s -> %s\n", ci->src, ci->dst);
             if (sync_recv(fd, ci->src, ci->dst, 0 /* no show progress */)) {
-                return 1;
+                ret = -1;
+                goto finish;
             }
 
             if (copy_attrs && set_time_and_mode(ci->dst, ci->time, ci->mode)) {
-               return 1;
+                ret = -1;
+                goto finish;
             }
             pulled++;
         } else {
@@ -946,7 +967,10 @@
             pulled, (pulled == 1) ? "" : "s",
             skipped, (skipped == 1) ? "" : "s");
 
-    return 0;
+finish:
+    free(lpath_clean);
+    free(rpath_clean);
+    return ret;
 }
 
 int do_sync_pull(const char *rpath, const char *lpath, int show_progress, int copy_attrs)
diff --git a/adb/file_sync_service.c b/adb/file_sync_service.c
index 7933858..7de82b7 100644
--- a/adb/file_sync_service.c
+++ b/adb/file_sync_service.c
@@ -270,7 +270,9 @@
     return -1;
 }
 
-#ifdef HAVE_SYMLINKS
+#if defined(_WIN32)
+extern int handle_send_link(int s, char *path, char *buffer) __attribute__((error("no symlinks on Windows")));
+#else
 static int handle_send_link(int s, char *path, char *buffer)
 {
     syncmsg msg;
@@ -321,25 +323,20 @@
 
     return 0;
 }
-#endif /* HAVE_SYMLINKS */
+#endif
 
 static int do_send(int s, char *path, char *buffer)
 {
-    char *tmp;
     unsigned int mode;
-    int is_link, ret;
+    bool is_link = false;
     bool do_unlink;
 
-    tmp = strrchr(path,',');
+    char* tmp = strrchr(path,',');
     if(tmp) {
         *tmp = 0;
         errno = 0;
         mode = strtoul(tmp + 1, NULL, 0);
-#ifndef HAVE_SYMLINKS
-        is_link = 0;
-#else
         is_link = S_ISLNK((mode_t) mode);
-#endif
         mode &= 0777;
     }
     if(!tmp || errno) {
@@ -355,32 +352,26 @@
         }
     }
 
-#ifdef HAVE_SYMLINKS
-    if(is_link)
-        ret = handle_send_link(s, path, buffer);
-    else {
-#else
-    {
-#endif
-        uid_t uid = -1;
-        gid_t gid = -1;
-        uint64_t cap = 0;
-
-        /* copy user permission bits to "group" and "other" permissions */
-        mode |= ((mode >> 3) & 0070);
-        mode |= ((mode >> 3) & 0007);
-
-        tmp = path;
-        if(*tmp == '/') {
-            tmp++;
-        }
-        if (is_on_system(path) || is_on_vendor(path)) {
-            fs_config(tmp, 0, &uid, &gid, &mode, &cap);
-        }
-        ret = handle_send_file(s, path, uid, gid, mode, buffer, do_unlink);
+    if (is_link) {
+        return handle_send_link(s, path, buffer);
     }
 
-    return ret;
+    uid_t uid = -1;
+    gid_t gid = -1;
+    uint64_t cap = 0;
+
+    /* copy user permission bits to "group" and "other" permissions */
+    mode |= ((mode >> 3) & 0070);
+    mode |= ((mode >> 3) & 0007);
+
+    tmp = path;
+    if(*tmp == '/') {
+        tmp++;
+    }
+    if (is_on_system(path) || is_on_vendor(path)) {
+        fs_config(tmp, 0, &uid, &gid, &mode, &cap);
+    }
+    return handle_send_file(s, path, uid, gid, mode, buffer, do_unlink);
 }
 
 static int do_recv(int s, const char *path, char *buffer)
diff --git a/adb/file_sync_service.h b/adb/file_sync_service.h
index 5dd2e80..c3c8574 100644
--- a/adb/file_sync_service.h
+++ b/adb/file_sync_service.h
@@ -17,22 +17,10 @@
 #ifndef _FILE_SYNC_SERVICE_H_
 #define _FILE_SYNC_SERVICE_H_
 
-#ifdef HAVE_BIG_ENDIAN
-static inline unsigned __swap_uint32(unsigned x) 
-{
-    return (((x) & 0xFF000000) >> 24)
-        | (((x) & 0x00FF0000) >> 8)
-        | (((x) & 0x0000FF00) << 8)
-        | (((x) & 0x000000FF) << 24);
-}
-#define htoll(x) __swap_uint32(x)
-#define ltohl(x) __swap_uint32(x)
-#define MKID(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((a) << 24))
-#else
 #define htoll(x) (x)
 #define ltohl(x) (x)
+
 #define MKID(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
-#endif
 
 #define ID_STAT MKID('S','T','A','T')
 #define ID_LIST MKID('L','I','S','T')
diff --git a/adb/framebuffer_service.c b/adb/framebuffer_service.c
index 8cbe840..61578aa 100644
--- a/adb/framebuffer_service.c
+++ b/adb/framebuffer_service.c
@@ -76,6 +76,7 @@
         exit(1);
     }
 
+    close(fds[1]);
     fd_screencap = fds[0];
 
     /* read w, h & format */
@@ -173,10 +174,9 @@
     }
 
 done:
-    TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
-
     close(fds[0]);
-    close(fds[1]);
+
+    TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
 pipefail:
     close(fd);
 }
diff --git a/adb/get_my_path_freebsd.c b/adb/get_my_path_freebsd.c
deleted file mode 100644
index b06ec66..0000000
--- a/adb/get_my_path_freebsd.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (C) 2009 bsdroid project
- *               Alexey Tarasov <tarasov@dodologics.com>
- *
- * Copyright (C) 2007 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 <sys/types.h>
-#include <unistd.h>
-#include <limits.h>
-#include <stdio.h>
-
-void
-get_my_path(char *exe, size_t maxLen)
-{
-    char proc[64];
-
-    snprintf(proc, sizeof(proc), "/proc/%d/file", getpid());
-
-    int err = readlink(proc, exe, maxLen - 1);
-
-    exe[err > 0 ? err : 0] = '\0';
-}
-
diff --git a/adb/jdwp_service.c b/adb/jdwp_service.c
index cd62b55..3074e42 100644
--- a/adb/jdwp_service.c
+++ b/adb/jdwp_service.c
@@ -415,6 +415,7 @@
               __FUNCTION__, strerror(errno));
             return -1;
         }
+        D("socketpair: (%d,%d)", fds[0], fds[1]);
 
         proc->out_fds[ proc->out_count ] = fds[1];
         if (++proc->out_count == 1)
diff --git a/adb/remount_service.c b/adb/remount_service.c
index 36367a7..d7b0dd1 100644
--- a/adb/remount_service.c
+++ b/adb/remount_service.c
@@ -18,6 +18,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <mntent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -36,38 +37,21 @@
 /* Returns the device used to mount a directory in /proc/mounts */
 static char *find_mount(const char *dir)
 {
-    int fd;
-    int res;
-    char *token = NULL;
-    const char delims[] = "\n";
-    char buf[4096];
+    FILE* fp;
+    struct mntent* mentry;
+    char* device = NULL;
 
-    fd = unix_open("/proc/mounts", O_RDONLY | O_CLOEXEC);
-    if (fd < 0)
+    if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
         return NULL;
-
-    buf[sizeof(buf) - 1] = '\0';
-    adb_read(fd, buf, sizeof(buf) - 1);
-    adb_close(fd);
-
-    token = strtok(buf, delims);
-
-    while (token) {
-        char mount_dev[256];
-        char mount_dir[256];
-        int mount_freq;
-        int mount_passno;
-
-        res = sscanf(token, "%255s %255s %*s %*s %d %d\n",
-                     mount_dev, mount_dir, &mount_freq, &mount_passno);
-        mount_dev[255] = 0;
-        mount_dir[255] = 0;
-        if (res == 4 && (strcmp(dir, mount_dir) == 0))
-            return strdup(mount_dev);
-
-        token = strtok(NULL, delims);
     }
-    return NULL;
+    while ((mentry = getmntent(fp)) != NULL) {
+        if (strcmp(dir, mentry->mnt_dir) == 0) {
+            device = strdup(mentry->mnt_fsname);
+            break;
+        }
+    }
+    endmntent(fp);
+    return device;
 }
 
 static int hasVendorPartition()
@@ -79,34 +63,50 @@
     return false;
 }
 
-/* Init mounts /system as read only, remount to enable writes. */
-static int remount(const char* dir, int* dir_ro)
+int make_block_device_writable(const char* dev)
 {
-    char *dev;
-    int fd;
+    int fd = -1;
     int OFF = 0;
-
-    if (dir_ro == 0) {
-        return 0;
-    }
-
-    dev = find_mount(dir);
+    int rc = -1;
 
     if (!dev)
-        return -1;
+        goto errout;
 
     fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
     if (fd < 0)
-        return -1;
+        goto errout;
 
-    ioctl(fd, BLKROSET, &OFF);
-    adb_close(fd);
+    if (ioctl(fd, BLKROSET, &OFF)) {
+        goto errout;
+    }
 
-    *dir_ro = mount(dev, dir, "none", MS_REMOUNT, NULL);
+    rc = 0;
 
+errout:
+    if (fd >= 0) {
+        adb_close(fd);
+    }
+    return rc;
+}
+
+/* Init mounts /system as read only, remount to enable writes. */
+static int remount(const char* dir, int* dir_ro)
+{
+    char *dev = 0;
+    int rc = -1;
+
+    dev = find_mount(dir);
+
+    if (!dev || make_block_device_writable(dev)) {
+        goto errout;
+    }
+
+    rc = mount(dev, dir, "none", MS_REMOUNT, NULL);
+    *dir_ro = rc;
+
+errout:
     free(dev);
-
-    return *dir_ro;
+    return rc;
 }
 
 static void write_string(int fd, const char* str)
@@ -167,4 +167,3 @@
 
     adb_close(fd);
 }
-
diff --git a/adb/services.c b/adb/services.c
index 21b08dc..e4ce0bc 100644
--- a/adb/services.c
+++ b/adb/services.c
@@ -164,6 +164,7 @@
         printf("cannot create service socket pair\n");
         return -1;
     }
+    D("socketpair: (%d,%d)", s[0], s[1]);
 
     sti = malloc(sizeof(stinfo));
     if(sti == 0) fatal("cannot allocate stinfo");
@@ -202,10 +203,10 @@
 static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
 {
     D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
     fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
     return -1;
-#else /* !HAVE_WIN32_PROC */
+#else
     int ptm;
 
     ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
@@ -251,23 +252,24 @@
     } else {
         return ptm;
     }
-#endif /* !HAVE_WIN32_PROC */
+#endif /* !defined(_WIN32) */
 }
 
 static int create_subproc_raw(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
 {
     D("create_subproc_raw(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
-#ifdef HAVE_WIN32_PROC
+#if defined(_WIN32)
     fprintf(stderr, "error: create_subproc_raw not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
     return -1;
-#else /* !HAVE_WIN32_PROC */
+#else
 
     // 0 is parent socket, 1 is child socket
     int sv[2];
-    if (unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
+    if (adb_socketpair(sv) < 0) {
         printf("[ cannot create socket pair - %s ]\n", strerror(errno));
         return -1;
     }
+    D("socketpair: (%d,%d)", sv[0], sv[1]);
 
     *pid = fork();
     if (*pid < 0) {
@@ -295,7 +297,7 @@
         adb_close(sv[1]);
         return sv[0];
     }
-#endif /* !HAVE_WIN32_PROC */
+#endif /* !defined(_WIN32) */
 }
 #endif  /* !ABD_HOST */
 
@@ -453,7 +455,7 @@
         ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
     } else if(!strncmp(name, "tcpip:", 6)) {
         int port;
-        if (sscanf(name + 6, "%d", &port) == 0) {
+        if (sscanf(name + 6, "%d", &port) != 1) {
             port = 0;
         }
         ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
@@ -470,7 +472,9 @@
             }
         }
     } else if(!strncmp(name, "disable-verity:", 15)) {
-        ret = create_service_thread(disable_verity_service, NULL);
+        ret = create_service_thread(set_verity_enabled_state_service, (void*)0);
+    } else if(!strncmp(name, "enable-verity:", 15)) {
+        ret = create_service_thread(set_verity_enabled_state_service, (void*)1);
 #endif
     }
     if (ret >= 0) {
@@ -523,7 +527,7 @@
         }
         // zero terminate the host at the point we found the colon
         hostbuf[portstr - host] = 0;
-        if (sscanf(portstr + 1, "%d", &port) == 0) {
+        if (sscanf(portstr + 1, "%d", &port) != 1) {
             snprintf(buffer, buffer_size, "bad port number %s", portstr);
             return;
         }
diff --git a/adb/set_verity_enable_state_service.c b/adb/set_verity_enable_state_service.c
new file mode 100644
index 0000000..184674d
--- /dev/null
+++ b/adb/set_verity_enable_state_service.c
@@ -0,0 +1,227 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <sys/stat.h>
+
+#define  TRACE_TAG  TRACE_ADB
+#include "adb.h"
+#include "cutils/properties.h"
+#include "ext4_sb.h"
+#include "fs_mgr.h"
+#include "sysdeps.h"
+
+#define FSTAB_PREFIX "/fstab."
+struct fstab *fstab;
+
+#ifdef ALLOW_ADBD_DISABLE_VERITY
+static const bool kAllowDisableVerity = true;
+#else
+static const bool kAllowDisableVerity = false;
+#endif
+
+__attribute__((__format__(printf, 2, 3))) __nonnull((2))
+static void write_console(int fd, const char* format, ...)
+{
+    char buffer[256];
+    va_list args;
+    va_start (args, format);
+    vsnprintf (buffer, sizeof(buffer), format, args);
+    va_end (args);
+
+    adb_write(fd, buffer, strnlen(buffer, sizeof(buffer)));
+}
+
+static int get_target_device_size(int fd, const char *blk_device,
+                                  uint64_t *device_size)
+{
+    int data_device;
+    struct ext4_super_block sb;
+    struct fs_info info;
+
+    info.len = 0;  /* Only len is set to 0 to ask the device for real size. */
+
+    data_device = adb_open(blk_device, O_RDONLY | O_CLOEXEC);
+    if (data_device < 0) {
+        write_console(fd, "Error opening block device (%s)\n", strerror(errno));
+        return -1;
+    }
+
+    if (lseek64(data_device, 1024, SEEK_SET) < 0) {
+        write_console(fd, "Error seeking to superblock\n");
+        adb_close(data_device);
+        return -1;
+    }
+
+    if (adb_read(data_device, &sb, sizeof(sb)) != sizeof(sb)) {
+        write_console(fd, "Error reading superblock\n");
+        adb_close(data_device);
+        return -1;
+    }
+
+    ext4_parse_sb(&sb, &info);
+    *device_size = info.len;
+
+    adb_close(data_device);
+    return 0;
+}
+
+/* Turn verity on/off */
+static int set_verity_enabled_state(int fd, const char *block_device,
+                                    const char* mount_point, bool enable)
+{
+    uint32_t magic_number;
+    const uint32_t new_magic = enable ? VERITY_METADATA_MAGIC_NUMBER
+                                      : VERITY_METADATA_MAGIC_DISABLE;
+    uint64_t device_length;
+    int device = -1;
+    int retval = -1;
+
+    if (make_block_device_writable(block_device)) {
+        write_console(fd, "Could not make block device %s writable (%s).\n",
+                      block_device, strerror(errno));
+        goto errout;
+    }
+
+    device = adb_open(block_device, O_RDWR | O_CLOEXEC);
+    if (device == -1) {
+        write_console(fd, "Could not open block device %s (%s).\n",
+                      block_device, strerror(errno));
+        write_console(fd, "Maybe run adb remount?\n");
+        goto errout;
+    }
+
+    // find the start of the verity metadata
+    if (get_target_device_size(fd, (char*)block_device, &device_length) < 0) {
+        write_console(fd, "Could not get target device size.\n");
+        goto errout;
+    }
+
+    if (lseek64(device, device_length, SEEK_SET) < 0) {
+        write_console(fd,
+                      "Could not seek to start of verity metadata block.\n");
+        goto errout;
+    }
+
+    // check the magic number
+    if (adb_read(device, &magic_number, sizeof(magic_number))
+             != sizeof(magic_number)) {
+        write_console(fd, "Couldn't read magic number!\n");
+        goto errout;
+    }
+
+    if (!enable && magic_number == VERITY_METADATA_MAGIC_DISABLE) {
+        write_console(fd, "Verity already disabled on %s\n", mount_point);
+        goto errout;
+    }
+
+    if (enable && magic_number == VERITY_METADATA_MAGIC_NUMBER) {
+        write_console(fd, "Verity already enabled on %s\n", mount_point);
+        goto errout;
+    }
+
+    if (magic_number != VERITY_METADATA_MAGIC_NUMBER
+            && magic_number != VERITY_METADATA_MAGIC_DISABLE) {
+        write_console(fd,
+                      "Couldn't find verity metadata at offset %"PRIu64"!\n",
+                      device_length);
+        goto errout;
+    }
+
+    if (lseek64(device, device_length, SEEK_SET) < 0) {
+        write_console(fd,
+                      "Could not seek to start of verity metadata block.\n");
+        goto errout;
+    }
+
+    if (adb_write(device, &new_magic, sizeof(new_magic)) != sizeof(new_magic)) {
+        write_console(
+            fd, "Could not set verity %s flag on device %s with error %s\n",
+            enable ? "enabled" : "disabled",
+            block_device, strerror(errno));
+        goto errout;
+    }
+
+    write_console(fd, "Verity %s on %s\n",
+                  enable ? "enabled" : "disabled",
+                  mount_point);
+    retval = 0;
+errout:
+    if (device != -1)
+        adb_close(device);
+    return retval;
+}
+
+void set_verity_enabled_state_service(int fd, void* cookie)
+{
+    bool enable = (cookie != NULL);
+    if (kAllowDisableVerity) {
+        char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
+        char propbuf[PROPERTY_VALUE_MAX];
+        int i;
+        bool any_changed = false;
+
+        property_get("ro.secure", propbuf, "0");
+        if (strcmp(propbuf, "1")) {
+            write_console(fd, "verity not enabled - ENG build\n");
+            goto errout;
+        }
+
+        property_get("ro.debuggable", propbuf, "0");
+        if (strcmp(propbuf, "1")) {
+            write_console(
+                fd, "verity cannot be disabled/enabled - USER build\n");
+            goto errout;
+        }
+
+        property_get("ro.hardware", propbuf, "");
+        snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s",
+                 propbuf);
+
+        fstab = fs_mgr_read_fstab(fstab_filename);
+        if (!fstab) {
+            write_console(fd, "Failed to open %s\nMaybe run adb root?\n",
+                          fstab_filename);
+            goto errout;
+        }
+
+        /* Loop through entries looking for ones that vold manages */
+        for (i = 0; i < fstab->num_entries; i++) {
+            if(fs_mgr_is_verified(&fstab->recs[i])) {
+                if (!set_verity_enabled_state(fd, fstab->recs[i].blk_device,
+                                              fstab->recs[i].mount_point,
+                                              enable)) {
+                    any_changed = true;
+                }
+           }
+        }
+
+        if (any_changed) {
+            write_console(
+                fd, "Now reboot your device for settings to take effect\n");
+        }
+    } else {
+        write_console(fd, "%s-verity only works for userdebug builds\n",
+                      enable ? "enable" : "disable");
+    }
+
+errout:
+    adb_close(fd);
+}
diff --git a/adb/sysdeps.h b/adb/sysdeps.h
index cc1f839..086dd61 100644
--- a/adb/sysdeps.h
+++ b/adb/sysdeps.h
@@ -26,16 +26,18 @@
 
 #ifdef _WIN32
 
+#include <ctype.h>
+#include <direct.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <io.h>
+#include <process.h>
+#include <sys/stat.h>
 #include <winsock2.h>
 #include <windows.h>
 #include <ws2tcpip.h>
-#include <process.h>
-#include <fcntl.h>
-#include <io.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <ctype.h>
-#include <direct.h>
+
+#include "fdevent.h"
 
 #define OS_PATH_SEPARATOR '\\'
 #define OS_PATH_SEPARATOR_STR "\\"
@@ -77,13 +79,16 @@
     return 0;
 }
 
+static __inline__  unsigned long adb_thread_id()
+{
+    return GetCurrentThreadId();
+}
+
 static __inline__ void  close_on_exec(int  fd)
 {
     /* nothing really */
 }
 
-extern void  disable_tcp_nagle(int  fd);
-
 #define  lstat    stat   /* no symlinks on Win32 */
 
 #define  S_ISLNK(m)   0   /* no symlinks on Win32 */
@@ -182,8 +187,6 @@
 #define FDE_ERROR             0x0004
 #define FDE_DONT_CLOSE        0x0080
 
-typedef struct fdevent fdevent;
-
 typedef void (*fd_func)(int fd, unsigned events, void *userdata);
 
 fdevent *fdevent_create(int fd, fd_func func, void *arg);
@@ -195,20 +198,6 @@
 void     fdevent_del(fdevent *fde, unsigned events);
 void     fdevent_loop();
 
-struct fdevent {
-    fdevent *next;
-    fdevent *prev;
-
-    int fd;
-    int force_eof;
-
-    unsigned short state;
-    unsigned short events;
-
-    fd_func func;
-    void *arg;
-};
-
 static __inline__ void  adb_sleep_ms( int  mseconds )
 {
     Sleep( mseconds );
@@ -219,10 +208,21 @@
 #undef   accept
 #define  accept  ___xxx_accept
 
+extern int  adb_setsockopt(int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen);
+
+#undef   setsockopt
+#define  setsockopt  ___xxx_setsockopt
+
 static __inline__  int  adb_socket_setbufsize( int   fd, int  bufsize )
 {
     int opt = bufsize;
-    return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
+    return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
+}
+
+static __inline__ void  disable_tcp_nagle( int  fd )
+{
+    int  on = 1;
+    adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
 }
 
 extern int  adb_socketpair( int  sv[2] );
@@ -459,6 +459,13 @@
     setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
 }
 
+static __inline__ int  adb_setsockopt( int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen )
+{
+    return setsockopt( fd, level, optname, optval, optlen );
+}
+
+#undef   setsockopt
+#define  setsockopt  ___xxx_setsockopt
 
 static __inline__ int  unix_socketpair( int  d, int  type, int  protocol, int sv[2] )
 {
@@ -516,6 +523,12 @@
 {
     return strtok_r(str, delim, saveptr);
 }
+
+static __inline__ unsigned long adb_thread_id()
+{
+    return (unsigned long)pthread_self();
+}
+
 #undef   strtok_r
 #define  strtok_r  ___xxx_strtok_r
 
diff --git a/adb/sysdeps_win32.c b/adb/sysdeps_win32.c
index e69ec2b..f132b8c 100644
--- a/adb/sysdeps_win32.c
+++ b/adb/sysdeps_win32.c
@@ -440,7 +440,8 @@
 {
     FH   f = _fh_from_int(fd);
 
-    if (!f) {
+    if (!f || f->clazz != &_fh_socket_class) {
+        D("adb_shutdown: invalid fd %d\n", fd);
         return -1;
     }
 
@@ -471,6 +472,8 @@
 /**************************************************************************/
 /**************************************************************************/
 
+#undef setsockopt
+
 static void
 _socket_set_errno( void )
 {
@@ -786,15 +789,16 @@
 }
 
 
-void  disable_tcp_nagle(int fd)
+int  adb_setsockopt( int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen )
 {
     FH   fh = _fh_from_int(fd);
-    int  on = 1;
 
-    if ( !fh || fh->clazz != &_fh_socket_class )
-        return;
+    if ( !fh || fh->clazz != &_fh_socket_class ) {
+        D("adb_setsockopt: invalid fd %d\n", fd);
+        return -1;
+    }
 
-    setsockopt( fh->fh_socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&on, sizeof(on) );
+    return setsockopt( fh->fh_socket, level, optname, optval, optlen );
 }
 
 /**************************************************************************/
diff --git a/adb/tests/test_adb.py b/adb/tests/test_adb.py
new file mode 100644
index 0000000..b0ae07f
--- /dev/null
+++ b/adb/tests/test_adb.py
@@ -0,0 +1,390 @@
+#!/usr/bin/env python2
+"""Simple conformance test for adb.
+
+This script will use the available adb in path and run simple
+tests that attempt to touch all accessible attached devices.
+"""
+import hashlib
+import os
+import random
+import re
+import subprocess
+import tempfile
+import unittest
+import sys
+import shlex
+
+
+def trace(cmd):
+    """Print debug message if tracing enabled."""
+    if False:
+        print >> sys.stderr, cmd
+
+
+def call(cmd_str):
+    """Run process and return output tuple (stdout, stderr, ret code)."""
+    trace(cmd_str)
+    process = subprocess.Popen(shlex.split(cmd_str),
+                               stdout=subprocess.PIPE,
+                               stderr=subprocess.PIPE)
+    stdout, stderr = process.communicate()
+    return stdout, stderr, process.returncode
+
+
+def call_combined(cmd_str):
+    """Run process and return output tuple (stdout+stderr, ret code)."""
+    trace(cmd_str)
+    process = subprocess.Popen(shlex.split(cmd_str),
+                               stdout=subprocess.PIPE,
+                               stderr=subprocess.STDOUT)
+    stdout, _ = process.communicate()
+    return stdout, process.returncode
+
+
+def call_checked(cmd_str):
+    """Run process and get stdout+stderr, raise an exception on trouble."""
+    trace(cmd_str)
+    return subprocess.check_output(shlex.split(cmd_str),
+                                   stderr=subprocess.STDOUT)
+
+
+def call_checked_list(cmd_str):
+    return call_checked(cmd_str).split('\n')
+
+
+def call_checked_list_skip(cmd_str):
+    out_list = call_checked_list(cmd_str)
+
+    def is_init_line(line):
+        if (len(line) >= 3) and (line[0] == "*") and (line[-2] == "*"):
+            return True
+        else:
+            return False
+
+    return [line for line in out_list if not is_init_line(line)]
+
+
+def get_device_list():
+    output = call_checked_list_skip("adb devices")
+    dev_list = []
+    for line in output[1:]:
+        if line.strip() == "":
+            continue
+        device, _ = line.split()
+        dev_list.append(device)
+    return dev_list
+
+
+def get_attached_device_count():
+    return len(get_device_list())
+
+
+def compute_md5(string):
+    hsh = hashlib.md5()
+    hsh.update(string)
+    return hsh.hexdigest()
+
+
+class HostFile(object):
+    def __init__(self, handle, md5):
+        self.handle = handle
+        self.md5 = md5
+        self.full_path = handle.name
+        self.base_name = os.path.basename(self.full_path)
+
+
+class DeviceFile(object):
+    def __init__(self, md5, full_path):
+        self.md5 = md5
+        self.full_path = full_path
+        self.base_name = os.path.basename(self.full_path)
+
+
+def make_random_host_files(in_dir, num_files, rand_size=True):
+    files = {}
+    min_size = 1 * (1 << 10)
+    max_size = 16 * (1 << 10)
+    fixed_size = min_size
+
+    for _ in range(num_files):
+        file_handle = tempfile.NamedTemporaryFile(dir=in_dir)
+
+        if rand_size:
+            size = random.randrange(min_size, max_size, 1024)
+        else:
+            size = fixed_size
+        rand_str = os.urandom(size)
+        file_handle.write(rand_str)
+        file_handle.flush()
+
+        md5 = compute_md5(rand_str)
+        files[file_handle.name] = HostFile(file_handle, md5)
+    return files
+
+
+def make_random_device_files(adb, in_dir, num_files, rand_size=True):
+    files = {}
+    min_size = 1 * (1 << 10)
+    max_size = 16 * (1 << 10)
+    fixed_size = min_size
+
+    for i in range(num_files):
+        if rand_size:
+            size = random.randrange(min_size, max_size, 1024)
+        else:
+            size = fixed_size
+
+        base_name = "device_tmpfile" + str(i)
+        full_path = in_dir + "/" + base_name
+
+        adb.shell("dd if=/dev/urandom of={} bs={} count=1".format(full_path,
+                                                                  size))
+        dev_md5, _ = adb.shell("md5sum {}".format(full_path)).split()
+
+        files[full_path] = DeviceFile(dev_md5, full_path)
+    return files
+
+
+class AdbWrapper(object):
+    """Convenience wrapper object for the adb command."""
+    def __init__(self, device=None, out_dir=None):
+        self.device = device
+        self.out_dir = out_dir
+        self.adb_cmd = "adb "
+        if self.device:
+            self.adb_cmd += "-s {} ".format(device)
+        if self.out_dir:
+            self.adb_cmd += "-p {} ".format(out_dir)
+
+    def shell(self, cmd):
+        return call_checked(self.adb_cmd + "shell " + cmd)
+
+    def shell_nocheck(self, cmd):
+        return call_combined(self.adb_cmd + "shell " + cmd)
+
+    def push(self, local, remote):
+        return call_checked(self.adb_cmd + "push {} {}".format(local, remote))
+
+    def pull(self, remote, local):
+        return call_checked(self.adb_cmd + "pull {} {}".format(remote, local))
+
+    def sync(self, directory=""):
+        return call_checked(self.adb_cmd + "sync {}".format(directory))
+
+    def forward(self, local, remote):
+        return call_checked(self.adb_cmd + "forward {} {}".format(local,
+                                                                  remote))
+
+    def tcpip(self, port):
+        return call_checked(self.adb_cmd + "tcpip {}".format(port))
+
+    def usb(self):
+        return call_checked(self.adb_cmd + "usb")
+
+    def forward_remove(self, local):
+        return call_checked(self.adb_cmd + "forward --remove {}".format(local))
+
+    def forward_remove_all(self):
+        return call_checked(self.adb_cmd + "forward --remove-all")
+
+    def connect(self, host):
+        return call_checked(self.adb_cmd + "connect {}".format(host))
+
+    def disconnect(self, host):
+        return call_checked(self.adb_cmd + "disconnect {}".format(host))
+
+    def reverse(self, remote, local):
+        return call_checked(self.adb_cmd + "reverse {} {}".format(remote,
+                                                                  local))
+
+    def reverse_remove_all(self):
+        return call_checked(self.adb_cmd + "reverse --remove-all")
+
+    def reverse_remove(self, remote):
+        return call_checked(
+            self.adb_cmd + "reverse --remove {}".format(remote))
+
+    def wait(self):
+        return call_checked(self.adb_cmd + "wait-for-device")
+
+
+class AdbBasic(unittest.TestCase):
+    def test_devices(self):
+        """Get uptime for each device plugged in from /proc/uptime."""
+        dev_list = get_device_list()
+        for device in dev_list:
+            out = call_checked(
+                "adb -s {} shell cat /proc/uptime".format(device))
+            self.assertEqual(len(out.split()), 2)
+            self.assertGreater(float(out.split()[0]), 0.0)
+            self.assertGreater(float(out.split()[1]), 0.0)
+
+    def test_help(self):
+        """Make sure we get _something_ out of help."""
+        out = call_checked("adb help")
+        self.assertTrue(len(out) > 0)
+
+    def test_version(self):
+        """Get a version number out of the output of adb."""
+        out = call_checked("adb version").split()
+        version_num = False
+        for item in out:
+            if re.match(r"[\d+\.]*\d", item):
+                version_num = True
+        self.assertTrue(version_num)
+
+
+class AdbFile(unittest.TestCase):
+    SCRATCH_DIR = "/data/local/tmp"
+    DEVICE_TEMP_FILE = SCRATCH_DIR + "/adb_test_file"
+    DEVICE_TEMP_DIR = SCRATCH_DIR + "/adb_test_dir"
+
+    def test_push(self):
+        """Push a file to all attached devices."""
+        dev_list = get_device_list()
+        for device in dev_list:
+            self.push_with_device(device)
+
+    def push_with_device(self, device):
+        """Push a randomly generated file to specified device."""
+        kbytes = 512
+        adb = AdbWrapper(device)
+        with tempfile.NamedTemporaryFile(mode="w") as tmp:
+            rand_str = os.urandom(1024 * kbytes)
+            tmp.write(rand_str)
+            tmp.flush()
+
+            host_md5 = compute_md5(rand_str)
+            adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_FILE))
+            try:
+                adb.push(local=tmp.name, remote=AdbFile.DEVICE_TEMP_FILE)
+                dev_md5, _ = adb.shell(
+                    "md5sum {}".format(AdbFile.DEVICE_TEMP_FILE)).split()
+                self.assertEqual(host_md5, dev_md5)
+            finally:
+                adb.shell_nocheck("rm {}".format(AdbFile.DEVICE_TEMP_FILE))
+
+    # TODO: write push directory test.
+
+    def test_pull(self):
+        """Pull a file from all attached devices."""
+        dev_list = get_device_list()
+        for device in dev_list:
+            self.pull_with_device(device)
+
+    def pull_with_device(self, device):
+        """Pull a randomly generated file from specified device."""
+        kbytes = 512
+        adb = AdbWrapper(device)
+        adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_FILE))
+        try:
+            adb.shell("dd if=/dev/urandom of={} bs=1024 count={}".format(
+                AdbFile.DEVICE_TEMP_FILE, kbytes))
+            dev_md5, _ = adb.shell(
+                "md5sum {}".format(AdbFile.DEVICE_TEMP_FILE)).split()
+
+            with tempfile.NamedTemporaryFile(mode="w") as tmp_write:
+                adb.pull(remote=AdbFile.DEVICE_TEMP_FILE, local=tmp_write.name)
+                with open(tmp_write.name) as tmp_read:
+                    host_contents = tmp_read.read()
+                    host_md5 = compute_md5(host_contents)
+                self.assertEqual(dev_md5, host_md5)
+        finally:
+            adb.shell_nocheck("rm {}".format(AdbFile.DEVICE_TEMP_FILE))
+
+    def test_pull_dir(self):
+        """Pull a directory from all attached devices."""
+        dev_list = get_device_list()
+        for device in dev_list:
+            self.pull_dir_with_device(device)
+
+    def pull_dir_with_device(self, device):
+        """Pull a randomly generated directory of files from the device."""
+        adb = AdbWrapper(device)
+        temp_files = {}
+        host_dir = None
+        try:
+            # create temporary host directory
+            host_dir = tempfile.mkdtemp()
+
+            # create temporary dir on device
+            adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
+            adb.shell("mkdir -p {}".format(AdbFile.DEVICE_TEMP_DIR))
+
+            # populate device dir with random files
+            temp_files = make_random_device_files(
+                adb, in_dir=AdbFile.DEVICE_TEMP_DIR, num_files=32)
+
+            adb.pull(remote=AdbFile.DEVICE_TEMP_DIR, local=host_dir)
+
+            for device_full_path in temp_files:
+                host_path = os.path.join(
+                    host_dir, temp_files[device_full_path].base_name)
+                with open(host_path) as host_file:
+                    host_md5 = compute_md5(host_file.read())
+                    self.assertEqual(host_md5,
+                                     temp_files[device_full_path].md5)
+        finally:
+            for dev_file in temp_files.values():
+                host_path = os.path.join(host_dir, dev_file.base_name)
+                os.remove(host_path)
+            adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
+            if host_dir:
+                os.removedirs(host_dir)
+
+    def test_sync(self):
+        """Sync a directory with all attached devices."""
+        dev_list = get_device_list()
+        for device in dev_list:
+            self.sync_dir_with_device(device)
+
+    def sync_dir_with_device(self, device):
+        """Sync a randomly generated directory of files to specified device."""
+        try:
+            adb = AdbWrapper(device)
+            temp_files = {}
+
+            # create temporary host directory
+            base_dir = tempfile.mkdtemp()
+
+            # create mirror device directory hierarchy within base_dir
+            full_dir_path = base_dir + AdbFile.DEVICE_TEMP_DIR
+            os.makedirs(full_dir_path)
+
+            # create 32 random files within the host mirror
+            temp_files = make_random_host_files(in_dir=full_dir_path,
+                                                num_files=32)
+
+            # clean up any trash on the device
+            adb = AdbWrapper(device, out_dir=base_dir)
+            adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
+
+            # issue the sync
+            adb.sync("data")
+
+            # confirm that every file on the device mirrors that on the host
+            for host_full_path in temp_files.keys():
+                device_full_path = os.path.join(
+                    AdbFile.DEVICE_TEMP_DIR,
+                    temp_files[host_full_path].base_name)
+                dev_md5, _ = adb.shell(
+                    "md5sum {}".format(device_full_path)).split()
+                self.assertEqual(temp_files[host_full_path].md5, dev_md5)
+
+        finally:
+            adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
+            if temp_files:
+                for tf in temp_files.values():
+                    tf.handle.close()
+            if base_dir:
+                os.removedirs(base_dir + AdbFile.DEVICE_TEMP_DIR)
+
+
+if __name__ == '__main__':
+    random.seed(0)
+    dev_count = get_attached_device_count()
+    if dev_count:
+        suite = unittest.TestLoader().loadTestsFromName(__name__)
+        unittest.TextTestRunner(verbosity=3).run(suite)
+    else:
+        print "Test suite must be run with attached devices"
diff --git a/adb/transport.c b/adb/transport.c
index f35880c..ffe59da 100644
--- a/adb/transport.c
+++ b/adb/transport.c
@@ -629,7 +629,7 @@
             fatal_errno("cannot open transport socketpair");
         }
 
-        D("transport: %s (%d,%d) starting\n", t->serial, s[0], s[1]);
+        D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
 
         t->transport_socket = s[0];
         t->fd = s[1];
@@ -673,6 +673,7 @@
     if(adb_socketpair(s)){
         fatal_errno("cannot open transport registration socketpair");
     }
+    D("socketpair: (%d,%d)", s[0], s[1]);
 
     transport_registration_send = s[0];
     transport_registration_recv = s[1];
@@ -1164,7 +1165,9 @@
 
 #if ADB_TRACE
     D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len);
-    dump_hex( ptr, len0 );
+    if (ADB_TRACING) {
+        dump_hex( ptr, len0 );
+    }
 #endif
     return 0;
 }
@@ -1176,7 +1179,9 @@
 
 #if ADB_TRACE
     D("writex: fd=%d len=%d: ", fd, (int)len);
-    dump_hex( ptr, len );
+    if (ADB_TRACING) {
+        dump_hex( ptr, len );
+    }
 #endif
     while(len > 0) {
         r = adb_write(fd, p, len);
diff --git a/adb/transport.h b/adb/transport.h
index 992e052..c1b8ff3 100644
--- a/adb/transport.h
+++ b/adb/transport.h
@@ -17,10 +17,19 @@
 #ifndef __TRANSPORT_H
 #define __TRANSPORT_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* convenience wrappers around read/write that will retry on
 ** EINTR and/or short read/write.  Returns 0 on success, -1
 ** on error or EOF.
 */
 int readx(int fd, void *ptr, size_t len);
 int writex(int fd, const void *ptr, size_t len);
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif   /* __TRANSPORT_H */
diff --git a/adb/transport_local.c b/adb/transport_local.c
index 948cc15..6c4e220 100644
--- a/adb/transport_local.c
+++ b/adb/transport_local.c
@@ -28,21 +28,6 @@
 #define  TRACE_TAG  TRACE_TRANSPORT
 #include "adb.h"
 
-#ifdef HAVE_BIG_ENDIAN
-#define H4(x)	(((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
-static inline void fix_endians(apacket *p)
-{
-    p->msg.command     = H4(p->msg.command);
-    p->msg.arg0        = H4(p->msg.arg0);
-    p->msg.arg1        = H4(p->msg.arg1);
-    p->msg.data_length = H4(p->msg.data_length);
-    p->msg.data_check  = H4(p->msg.data_check);
-    p->msg.magic       = H4(p->msg.magic);
-}
-#else
-#define fix_endians(p) do {} while (0)
-#endif
-
 #if ADB_HOST
 /* we keep a list of opened transports. The atransport struct knows to which
  * local transport it is connected. The list is used to detect when we're
@@ -62,12 +47,6 @@
         return -1;
     }
 
-    fix_endians(p);
-
-#if 0 && defined HAVE_BIG_ENDIAN
-    D("read remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
-      p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
-#endif
     if(check_header(p)) {
         D("bad header: terminated (data)\n");
         return -1;
@@ -90,12 +69,6 @@
 {
     int   length = p->msg.data_length;
 
-    fix_endians(p);
-
-#if 0 && defined HAVE_BIG_ENDIAN
-    D("write remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
-      p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
-#endif
     if(writex(t->sfd, &p->msg, sizeof(amessage) + length)) {
         D("remote local: write terminated\n");
         return -1;
diff --git a/adb/transport_usb.c b/adb/transport_usb.c
index ee6b637..1138ddd 100644
--- a/adb/transport_usb.c
+++ b/adb/transport_usb.c
@@ -23,33 +23,6 @@
 #define  TRACE_TAG  TRACE_TRANSPORT
 #include "adb.h"
 
-#if ADB_HOST
-#include "usb_vendors.h"
-#endif
-
-#ifdef HAVE_BIG_ENDIAN
-#define H4(x)	(((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
-static inline void fix_endians(apacket *p)
-{
-    p->msg.command     = H4(p->msg.command);
-    p->msg.arg0        = H4(p->msg.arg0);
-    p->msg.arg1        = H4(p->msg.arg1);
-    p->msg.data_length = H4(p->msg.data_length);
-    p->msg.data_check  = H4(p->msg.data_check);
-    p->msg.magic       = H4(p->msg.magic);
-}
-unsigned host_to_le32(unsigned n)
-{
-    return H4(n);
-}
-#else
-#define fix_endians(p) do {} while (0)
-unsigned host_to_le32(unsigned n)
-{
-    return n;
-}
-#endif
-
 static int remote_read(apacket *p, atransport *t)
 {
     if(usb_read(t->usb, &p->msg, sizeof(amessage))){
@@ -57,8 +30,6 @@
         return -1;
     }
 
-    fix_endians(p);
-
     if(check_header(p)) {
         D("remote usb: check_header failed\n");
         return -1;
@@ -83,8 +54,6 @@
 {
     unsigned size = p->msg.data_length;
 
-    fix_endians(p);
-
     if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
         D("remote usb: 1 - write terminated\n");
         return -1;
@@ -131,18 +100,6 @@
 #if ADB_HOST
 int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol)
 {
-    unsigned i;
-    for (i = 0; i < vendorIdCount; i++) {
-        if (vid == vendorIds[i]) {
-            if (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS &&
-                    usb_protocol == ADB_PROTOCOL) {
-                return 1;
-            }
-
-            return 0;
-        }
-    }
-
-    return 0;
+    return (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS && usb_protocol == ADB_PROTOCOL);
 }
 #endif
diff --git a/adb/usb_libusb.c b/adb/usb_libusb.c
deleted file mode 100644
index 06ff5dc..0000000
--- a/adb/usb_libusb.c
+++ /dev/null
@@ -1,657 +0,0 @@
-/* 
- * Copyright (C) 2009 bsdroid project
- *               Alexey Tarasov <tarasov@dodologics.com>
- *   
- * Copyright (C) 2007 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 <sys/endian.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/uio.h>
-
-#include <err.h>
-#include <errno.h>
-#include <poll.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <strings.h>
-#include <string.h>
-#include <sysexits.h>
-#include <unistd.h>
-#include <libusb.h>
-#include "sysdeps.h"
-
-#define   TRACE_TAG  TRACE_USB
-#include "adb.h"
-
-static adb_mutex_t usb_lock = ADB_MUTEX_INITIALIZER;
-static libusb_context *ctx = NULL;
-
-struct usb_handle
-{
-    usb_handle            *prev;
-    usb_handle            *next;
-
-    libusb_device         *dev;
-    libusb_device_handle  *devh;
-    int                   interface;
-    uint8_t               dev_bus;
-    uint8_t               dev_addr;
-	
-    int                   zero_mask;
-    unsigned char         end_point_address[2];
-    char                  serial[128];
-    
-    adb_cond_t            notify;
-    adb_mutex_t           lock;
-};
-
-static struct usb_handle handle_list = {
-        .prev = &handle_list,
-        .next = &handle_list,
-};
-
-void
-usb_cleanup()
-{
-	libusb_exit(ctx);
-}
-
-void
-report_bulk_libusb_error(int r)
-{
-    switch (r) {
-    case LIBUSB_ERROR_TIMEOUT:
-        D("Transfer timeout\n");
-        break;
-
-    case LIBUSB_ERROR_PIPE:
-        D("Control request is not supported\n");
-        break;
-
-    case LIBUSB_ERROR_OVERFLOW:
-        D("Device offered more data\n");
-        break;
-
-    case LIBUSB_ERROR_NO_DEVICE :
-        D("Device was disconnected\n");
-        break;
-
-    default:
-        D("Error %d during transfer\n", r);
-        break;
-    };
-}
-
-static int
-usb_bulk_write(usb_handle *uh, const void *data, int len)
-{
-    int r = 0;
-    int transferred = 0;
-
-    r = libusb_bulk_transfer(uh->devh, uh->end_point_address[1], (void *)data, len,
-                             &transferred, 0);
-   
-    if (r != 0) {
-        D("usb_bulk_write(): ");
-        report_bulk_libusb_error(r);
-        return r;
-    }
-   
-    return (transferred);
-}
-
-static int
-usb_bulk_read(usb_handle *uh, void *data, int len)
-{
-    int r = 0;
-    int transferred = 0;
-
-    r = libusb_bulk_transfer(uh->devh, uh->end_point_address[0], data, len,
-                             &transferred, 0);
-
-    if (r != 0) {
-        D("usb_bulk_read(): ");
-        report_bulk_libusb_error(r);
-        return r;
-    }
-   
-    return (transferred);
-}
-
-int
-usb_write(struct usb_handle *uh, const void *_data, int len)
-{
-    unsigned char *data = (unsigned char*) _data;
-    int n;
-    int need_zero = 0;
-
-    if (uh->zero_mask == 1) {
-        if (!(len & uh->zero_mask)) {
-            need_zero = 1;
-        }
-    }
-
-    D("usb_write(): %p:%d -> transport %p\n", _data, len, uh);
-    
-    while (len > 0) {
-        int xfer = (len > 4096) ? 4096 : len;
-
-        n = usb_bulk_write(uh, data, xfer);
-        
-        if (n != xfer) {
-            D("usb_write(): failed for transport %p (%d bytes left)\n", uh, len);
-            return -1;
-        }
-
-        len -= xfer;
-        data += xfer;
-    }
-
-    if (need_zero){
-        n = usb_bulk_write(uh, _data, 0);
-        
-        if (n < 0) {
-            D("usb_write(): failed to finish operation for transport %p\n", uh);
-        }
-        return n;
-    }
-
-    return 0;
-}
-
-int
-usb_read(struct usb_handle *uh, void *_data, int len)
-{
-    unsigned char *data = (unsigned char*) _data;
-    int n;
-
-    D("usb_read(): %p:%d <- transport %p\n", _data, len, uh);
-    
-    while (len > 0) {
-        int xfer = (len > 4096) ? 4096 : len;
-
-        n = usb_bulk_read(uh, data, xfer);
-        
-        if (n != xfer) {
-            if (n > 0) {
-                data += n;
-                len -= n;
-                continue;
-            }
-            
-            D("usb_read(): failed for transport %p (%d bytes left)\n", uh, len);
-            return -1;
-        }
-
-        len -= xfer;
-        data += xfer;
-    }
-
-    return 0;
- }
-
-int
-usb_close(struct usb_handle *h)
-{
-    D("usb_close(): closing transport %p\n", h);
-    adb_mutex_lock(&usb_lock);
-    
-    h->next->prev = h->prev;
-    h->prev->next = h->next;
-    h->prev = NULL;
-    h->next = NULL;
-
-    libusb_release_interface(h->devh, h->interface);
-    libusb_close(h->devh);
-    libusb_unref_device(h->dev);
-    
-    adb_mutex_unlock(&usb_lock);
-
-    free(h);
-
-    return (0);
-}
-
-void usb_kick(struct usb_handle *h)
-{
-    D("usb_cick(): kicking transport %p\n", h);
-    
-    adb_mutex_lock(&h->lock);
-    unregister_usb_transport(h);
-    adb_mutex_unlock(&h->lock);
-    
-    h->next->prev = h->prev;
-    h->prev->next = h->next;
-    h->prev = NULL;
-    h->next = NULL;
-
-    libusb_release_interface(h->devh, h->interface);
-    libusb_close(h->devh);
-    libusb_unref_device(h->dev);
-    free(h);
-}
-
-int
-check_usb_interface(libusb_interface *interface,
-                    libusb_device_descriptor *desc,
-                    struct usb_handle *uh)
-{    
-    int e;
-    
-    if (interface->num_altsetting == 0) {
-        D("check_usb_interface(): No interface settings\n");
-        return -1;
-    }
-    
-    libusb_interface_descriptor *idesc = &interface->altsetting[0];
-    
-    if (idesc->bNumEndpoints != 2) {
-        D("check_usb_interface(): Interface have not 2 endpoints, ignoring\n");
-        return -1;
-    }
-
-    for (e = 0; e < idesc->bNumEndpoints; e++) {
-        libusb_endpoint_descriptor *edesc = &idesc->endpoint[e];
-        
-        if (edesc->bmAttributes != LIBUSB_TRANSFER_TYPE_BULK) {
-            D("check_usb_interface(): Endpoint (%u) is not bulk (%u), ignoring\n",
-                    edesc->bmAttributes, LIBUSB_TRANSFER_TYPE_BULK);
-            return -1;
-        }
-        
-        if (edesc->bEndpointAddress & LIBUSB_ENDPOINT_IN)
-            uh->end_point_address[0] = edesc->bEndpointAddress;
-        else
-            uh->end_point_address[1] = edesc->bEndpointAddress;
-        
-            /* aproto 01 needs 0 termination */
-        if (idesc->bInterfaceProtocol == 0x01) {
-            uh->zero_mask = edesc->wMaxPacketSize - 1;
-            D("check_usb_interface(): Forced Android interface protocol v.1\n");
-        }
-    }
-
-    D("check_usb_interface(): Device: %04x:%04x "
-      "iclass: %x, isclass: %x, iproto: %x ep: %x/%x-> ",
-        desc->idVendor, desc->idProduct, idesc->bInterfaceClass,
-	idesc->bInterfaceSubClass, idesc->bInterfaceProtocol,
-	uh->end_point_address[0], uh->end_point_address[1]);
-    
-    if (!is_adb_interface(desc->idVendor, desc->idProduct,
-            idesc->bInterfaceClass, idesc->bInterfaceSubClass,
-            idesc->bInterfaceProtocol))
-    {
-        D("not matches\n");
-        return -1;
-    }
-
-    D("matches\n");
-    return 1;
-}
-
-int
-check_usb_interfaces(libusb_config_descriptor *config,
-                     libusb_device_descriptor *desc, struct usb_handle *uh)
-{  
-    int i;
-    
-    for (i = 0; i < config->bNumInterfaces; ++i) {
-        if (check_usb_interface(&config->interface[i], desc, uh) != -1) {
-            /* found some interface and saved information about it */
-            D("check_usb_interfaces(): Interface %d of %04x:%04x "
-              "matches Android device\n", i, desc->idVendor,
-	      desc->idProduct);
-            
-            return  i;
-        }
-    }
-    
-    return -1;
-}
-
-int
-register_device(struct usb_handle *uh, const char *serial)
-{
-    D("register_device(): Registering %p [%s] as USB transport\n",
-       uh, serial);
-
-    struct usb_handle *usb= NULL;
-
-    usb = calloc(1, sizeof(struct usb_handle));
-    memcpy(usb, uh, sizeof(struct usb_handle));
-    strcpy(usb->serial, uh->serial);
-
-    adb_cond_init(&usb->notify, 0);
-    adb_mutex_init(&usb->lock, 0);
-
-    adb_mutex_lock(&usb_lock);
-    
-    usb->next = &handle_list;
-    usb->prev = handle_list.prev;
-    usb->prev->next = usb;
-    usb->next->prev = usb;
-
-    adb_mutex_unlock(&usb_lock);
-
-    register_usb_transport(usb, serial, NULL, 1); 
-
-    return (1);
-}
-
-int
-already_registered(usb_handle *uh)
-{
-    struct usb_handle *usb= NULL;
-    int exists = 0;
-    
-    adb_mutex_lock(&usb_lock);
-
-    for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
-        if ((usb->dev_bus == uh->dev_bus) &&
-            (usb->dev_addr == uh->dev_addr))
-        {
-            exists = 1;
-            break;
-        }
-    }
-
-    adb_mutex_unlock(&usb_lock);
-
-    return exists;
-}
-
-void
-check_device(libusb_device *dev) 
-{
-    struct usb_handle uh;
-    int i = 0;
-    int found = -1;
-    char serial[256] = {0};
-
-    libusb_device_descriptor desc;
-    libusb_config_descriptor *config = NULL;
-    
-    int r = libusb_get_device_descriptor(dev, &desc);
-
-    if (r != LIBUSB_SUCCESS) {
-        D("check_device(): Failed to get device descriptor\n");
-        return;
-    }
-    
-    if ((desc.idVendor == 0) && (desc.idProduct == 0))
-        return;
-    
-    D("check_device(): Probing usb device %04x:%04x\n",
-        desc.idVendor, desc.idProduct);
-    
-    if (!is_adb_interface (desc.idVendor, desc.idProduct,
-                           ADB_CLASS, ADB_SUBCLASS, ADB_PROTOCOL))
-    {
-        D("check_device(): Ignored due unknown vendor id\n");
-        return;
-    }
-    
-    uh.dev_bus = libusb_get_bus_number(dev);
-    uh.dev_addr = libusb_get_device_address(dev);
-    
-    if (already_registered(&uh)) {
-        D("check_device(): Device (bus: %d, address: %d) "
-          "is already registered\n", uh.dev_bus, uh.dev_addr);
-        return;
-    }
-    
-    D("check_device(): Device bus: %d, address: %d\n",
-        uh.dev_bus, uh.dev_addr);
-
-    r = libusb_get_active_config_descriptor(dev, &config);
-    
-    if (r != 0) {
-        if (r == LIBUSB_ERROR_NOT_FOUND) {
-            D("check_device(): Device %4x:%4x is unconfigured\n", 
-                desc.idVendor, desc.idProduct);
-            return;
-        }
-        
-        D("check_device(): Failed to get configuration for %4x:%4x\n",
-            desc.idVendor, desc.idProduct);
-        return;
-    }
-    
-    if (config == NULL) {
-        D("check_device(): Sanity check failed after "
-          "getting active config\n");
-        return;
-    }
-    
-    if (config->interface != NULL) {
-        found = check_usb_interfaces(config, &desc, &uh);
-    }
-    
-    /* not needed anymore */
-    libusb_free_config_descriptor(config);
-    
-    r = libusb_open(dev, &uh.devh);
-    uh.dev = dev;
-
-    if (r != 0) {
-        switch (r) {
-            case LIBUSB_ERROR_NO_MEM:
-                D("check_device(): Memory allocation problem\n");
-                break;
-                
-            case LIBUSB_ERROR_ACCESS:
-                D("check_device(): Permissions problem, "
-                  "current user priveleges are messed up?\n");
-                break;
-                
-            case LIBUSB_ERROR_NO_DEVICE:
-                D("check_device(): Device disconected, bad cable?\n");
-                break;
-            
-            default:
-                D("check_device(): libusb triggered error %d\n", r);
-        }
-        // skip rest
-        found = -1;
-    }
-    
-    if (found >= 0) {
-        D("check_device(): Device matches Android interface\n");
-        // read the device's serial number
-        memset(serial, 0, sizeof(serial));
-        uh.interface = found;
-        
-        r = libusb_claim_interface(uh.devh, uh.interface);
-        
-        if (r < 0) {
-            D("check_device(): Failed to claim interface %d\n",
-                uh.interface);
-
-            goto fail;
-        }
-
-        if (desc.iSerialNumber) {
-            // reading serial
-            uint16_t    buffer[128] = {0};
-            uint16_t    languages[128] = {0};
-            int languageCount = 0;
-
-            memset(languages, 0, sizeof(languages));
-            r = libusb_control_transfer(uh.devh, 
-                LIBUSB_ENDPOINT_IN |  LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE,
-                LIBUSB_REQUEST_GET_DESCRIPTOR, LIBUSB_DT_STRING << 8,
-		0, (uint8_t *)languages, sizeof(languages), 0);
-
-            if (r <= 0) {
-                D("check_device(): Failed to get languages count\n");
-                goto fail;
-            } 
-            
-            languageCount = (r - 2) / 2;
-            
-            for (i = 1; i <= languageCount; ++i) {
-                memset(buffer, 0, sizeof(buffer));
-
-                r = libusb_control_transfer(uh.devh, 
-                    LIBUSB_ENDPOINT_IN |  LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE,
-                    LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc.iSerialNumber,
-		    languages[i], (uint8_t *)buffer, sizeof(buffer), 0);
-            
-                if (r > 0) { /* converting serial */
-                    int j = 0;
-                    r /= 2;
-                
-                    for (j = 1; j < r; ++j)
-                        serial[j - 1] = buffer[j];
-                
-                    serial[j - 1] = '\0';
-                    break; /* languagesCount cycle */
-                }
-            }
-            
-            if (register_device(&uh, serial) == 0) {
-                D("check_device(): Failed to register device\n");
-                goto fail_interface;
-            }
-            
-            libusb_ref_device(dev);
-        }
-    }
-    
-    return;
-
-fail_interface:
-    libusb_release_interface(uh.devh, uh.interface);
-
-fail:
-    libusb_close(uh.devh);
-    uh.devh = NULL;
-}
-
-int
-check_device_connected(struct usb_handle *uh)
-{
-    int r = libusb_kernel_driver_active(uh->devh, uh->interface);
-    
-    if (r == LIBUSB_ERROR_NO_DEVICE)
-        return 0;
-    
-    if (r < 0)
-        return -1;
-    
-    return 1;
-}
-
-void
-kick_disconnected()
-{
-    struct usb_handle *usb= NULL;
-    
-    adb_mutex_lock(&usb_lock);
-
-    for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
-        
-        if (check_device_connected(usb) == 0) {
-            D("kick_disconnected(): Transport %p is not online anymore\n",
-                usb);
-
-            usb_kick(usb);
-        }
-    }
-    
-    adb_mutex_unlock(&usb_lock);
-}
-
-void
-scan_usb_devices()
-{
-    D("scan_usb_devices(): started\n");
-    
-    libusb_device **devs= NULL;
-    libusb_device *dev= NULL;
-    ssize_t cnt = libusb_get_device_list(ctx, &devs);
-
-    if (cnt < 0) {
-        D("scan_usb_devices(): Failed to get device list (error: %d)\n",
-            cnt);
-
-        return;
-    }
-    
-    int i = 0;
-
-    while ((dev = devs[i++]) != NULL) {
-        check_device(dev);
-    }
-
-    libusb_free_device_list(devs, 1);
-}
-
-void *
-device_poll_thread(void* unused)
-{
-    D("device_poll_thread(): Created USB scan thread\n");
-    
-    for (;;) {
-        sleep(5);
-        kick_disconnected();
-        scan_usb_devices();
-    }
-
-    /* never reaching this point */
-    return (NULL);
-}
-
-static void
-sigalrm_handler(int signo)
-{
-    /* nothing */
-}
-
-void
-usb_init()
-{
-    D("usb_init(): started\n");
-    adb_thread_t        tid;
-    struct sigaction actions;
-
-    int r = libusb_init(&ctx);
-
-    if (r != LIBUSB_SUCCESS) {
-        err(EX_IOERR, "Failed to init libusb\n");
-    }
-
-    memset(&actions, 0, sizeof(actions));
-    
-    sigemptyset(&actions.sa_mask);
-    
-    actions.sa_flags = 0;
-    actions.sa_handler = sigalrm_handler;
-    
-    sigaction(SIGALRM, &actions, NULL);
-
-	/* initial device scan */
-	scan_usb_devices();
-	
-	/* starting USB event polling thread */
-    if (adb_thread_create(&tid, device_poll_thread, NULL)) {
-            err(EX_IOERR, "cannot create USB scan thread\n");
-    }
-    
-    D("usb_init(): finished\n");
-}
-
diff --git a/adb/usb_linux.c b/adb/usb_linux.c
index f16bdd0..7d13a5d 100644
--- a/adb/usb_linux.c
+++ b/adb/usb_linux.c
@@ -237,8 +237,20 @@
                             // looks like ADB...
                         ep1 = (struct usb_endpoint_descriptor *)bufptr;
                         bufptr += USB_DT_ENDPOINT_SIZE;
+                            // For USB 3.0 SuperSpeed devices, skip potential
+                            // USB 3.0 SuperSpeed Endpoint Companion descriptor
+                        if (bufptr+2 <= devdesc + desclength &&
+                            bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
+                            bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
+                            bufptr += USB_DT_SS_EP_COMP_SIZE;
+                        }
                         ep2 = (struct usb_endpoint_descriptor *)bufptr;
                         bufptr += USB_DT_ENDPOINT_SIZE;
+                        if (bufptr+2 <= devdesc + desclength &&
+                            bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
+                            bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
+                            bufptr += USB_DT_SS_EP_COMP_SIZE;
+                        }
 
                         if (bufptr > devdesc + desclength ||
                             ep1->bLength != USB_DT_ENDPOINT_SIZE ||
diff --git a/adb/usb_linux_client.c b/adb/usb_linux_client.c
index 8426e0e..ee6b37c 100644
--- a/adb/usb_linux_client.c
+++ b/adb/usb_linux_client.c
@@ -33,6 +33,7 @@
 
 #define MAX_PACKET_SIZE_FS	64
 #define MAX_PACKET_SIZE_HS	512
+#define MAX_PACKET_SIZE_SS	1024
 
 #define cpu_to_le16(x)  htole16(x)
 #define cpu_to_le32(x)  htole32(x)
@@ -55,71 +56,81 @@
     int bulk_in;  /* "in" from the host's perspective => sink for adbd */
 };
 
-static const struct {
-    struct usb_functionfs_descs_head header;
-    struct {
-        struct usb_interface_descriptor intf;
-        struct usb_endpoint_descriptor_no_audio source;
-        struct usb_endpoint_descriptor_no_audio sink;
-    } __attribute__((packed)) fs_descs, hs_descs;
-} __attribute__((packed)) descriptors = {
-    .header = {
-        .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
-        .length = cpu_to_le32(sizeof(descriptors)),
-        .fs_count = 3,
-        .hs_count = 3,
+struct func_desc {
+    struct usb_interface_descriptor intf;
+    struct usb_endpoint_descriptor_no_audio source;
+    struct usb_endpoint_descriptor_no_audio sink;
+} __attribute__((packed));
+
+struct desc_v1 {
+    struct usb_functionfs_descs_head_v1 {
+        __le32 magic;
+        __le32 length;
+        __le32 fs_count;
+        __le32 hs_count;
+    } __attribute__((packed)) header;
+    struct func_desc fs_descs, hs_descs;
+} __attribute__((packed));
+
+struct desc_v2 {
+    struct usb_functionfs_descs_head_v2 header;
+    // The rest of the structure depends on the flags in the header.
+    __le32 fs_count;
+    __le32 hs_count;
+    struct func_desc fs_descs, hs_descs;
+} __attribute__((packed));
+
+struct func_desc fs_descriptors = {
+    .intf = {
+        .bLength = sizeof(fs_descriptors.intf),
+        .bDescriptorType = USB_DT_INTERFACE,
+        .bInterfaceNumber = 0,
+        .bNumEndpoints = 2,
+        .bInterfaceClass = ADB_CLASS,
+        .bInterfaceSubClass = ADB_SUBCLASS,
+        .bInterfaceProtocol = ADB_PROTOCOL,
+        .iInterface = 1, /* first string from the provided table */
     },
-    .fs_descs = {
-        .intf = {
-            .bLength = sizeof(descriptors.fs_descs.intf),
-            .bDescriptorType = USB_DT_INTERFACE,
-            .bInterfaceNumber = 0,
-            .bNumEndpoints = 2,
-            .bInterfaceClass = ADB_CLASS,
-            .bInterfaceSubClass = ADB_SUBCLASS,
-            .bInterfaceProtocol = ADB_PROTOCOL,
-            .iInterface = 1, /* first string from the provided table */
-        },
-        .source = {
-            .bLength = sizeof(descriptors.fs_descs.source),
-            .bDescriptorType = USB_DT_ENDPOINT,
-            .bEndpointAddress = 1 | USB_DIR_OUT,
-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
-            .wMaxPacketSize = MAX_PACKET_SIZE_FS,
-        },
-        .sink = {
-            .bLength = sizeof(descriptors.fs_descs.sink),
-            .bDescriptorType = USB_DT_ENDPOINT,
-            .bEndpointAddress = 2 | USB_DIR_IN,
-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
-            .wMaxPacketSize = MAX_PACKET_SIZE_FS,
-        },
+    .source = {
+        .bLength = sizeof(fs_descriptors.source),
+        .bDescriptorType = USB_DT_ENDPOINT,
+        .bEndpointAddress = 1 | USB_DIR_OUT,
+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
+        .wMaxPacketSize = MAX_PACKET_SIZE_FS,
     },
-    .hs_descs = {
-        .intf = {
-            .bLength = sizeof(descriptors.hs_descs.intf),
-            .bDescriptorType = USB_DT_INTERFACE,
-            .bInterfaceNumber = 0,
-            .bNumEndpoints = 2,
-            .bInterfaceClass = ADB_CLASS,
-            .bInterfaceSubClass = ADB_SUBCLASS,
-            .bInterfaceProtocol = ADB_PROTOCOL,
-            .iInterface = 1, /* first string from the provided table */
-        },
-        .source = {
-            .bLength = sizeof(descriptors.hs_descs.source),
-            .bDescriptorType = USB_DT_ENDPOINT,
-            .bEndpointAddress = 1 | USB_DIR_OUT,
-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
-            .wMaxPacketSize = MAX_PACKET_SIZE_HS,
-        },
-        .sink = {
-            .bLength = sizeof(descriptors.hs_descs.sink),
-            .bDescriptorType = USB_DT_ENDPOINT,
-            .bEndpointAddress = 2 | USB_DIR_IN,
-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
-            .wMaxPacketSize = MAX_PACKET_SIZE_HS,
-        },
+    .sink = {
+        .bLength = sizeof(fs_descriptors.sink),
+        .bDescriptorType = USB_DT_ENDPOINT,
+        .bEndpointAddress = 2 | USB_DIR_IN,
+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
+        .wMaxPacketSize = MAX_PACKET_SIZE_FS,
+    },
+};
+
+struct func_desc hs_descriptors = {
+    .intf = {
+        .bLength = sizeof(hs_descriptors.intf),
+        .bDescriptorType = USB_DT_INTERFACE,
+        .bInterfaceNumber = 0,
+        .bNumEndpoints = 2,
+        .bInterfaceClass = ADB_CLASS,
+        .bInterfaceSubClass = ADB_SUBCLASS,
+        .bInterfaceProtocol = ADB_PROTOCOL,
+        .iInterface = 1, /* first string from the provided table */
+    },
+    .source = {
+        .bLength = sizeof(hs_descriptors.source),
+        .bDescriptorType = USB_DT_ENDPOINT,
+        .bEndpointAddress = 1 | USB_DIR_OUT,
+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
+        .wMaxPacketSize = MAX_PACKET_SIZE_HS,
+    },
+    .sink = {
+        .bLength = sizeof(hs_descriptors.sink),
+        .bDescriptorType = USB_DT_ENDPOINT,
+        .bEndpointAddress = 2 | USB_DIR_IN,
+        .bmAttributes = USB_ENDPOINT_XFER_BULK,
+        .wMaxPacketSize = MAX_PACKET_SIZE_HS,
     },
 };
 
@@ -263,6 +274,16 @@
 static void init_functionfs(struct usb_handle *h)
 {
     ssize_t ret;
+    struct desc_v1 v1_descriptor;
+    struct desc_v2 v2_descriptor;
+
+    v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2);
+    v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor));
+    v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC;
+    v2_descriptor.fs_count = 3;
+    v2_descriptor.hs_count = 3;
+    v2_descriptor.fs_descs = fs_descriptors;
+    v2_descriptor.hs_descs = hs_descriptors;
 
     if (h->control < 0) { // might have already done this before
         D("OPENING %s\n", USB_FFS_ADB_EP0);
@@ -272,10 +293,20 @@
             goto err;
         }
 
-        ret = adb_write(h->control, &descriptors, sizeof(descriptors));
+        ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor));
         if (ret < 0) {
-            D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
-            goto err;
+            v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
+            v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor));
+            v1_descriptor.header.fs_count = 3;
+            v1_descriptor.header.hs_count = 3;
+            v1_descriptor.fs_descs = fs_descriptors;
+            v1_descriptor.hs_descs = hs_descriptors;
+            D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno);
+            ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor));
+            if (ret < 0) {
+                D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
+                goto err;
+            }
         }
 
         ret = adb_write(h->control, &strings, sizeof(strings));
diff --git a/adb/usb_osx.c b/adb/usb_osx.c
index ca4f2af..ba157f1 100644
--- a/adb/usb_osx.c
+++ b/adb/usb_osx.c
@@ -28,12 +28,11 @@
 
 #define TRACE_TAG   TRACE_USB
 #include "adb.h"
-#include "usb_vendors.h"
 
 #define  DBG   D
 
 static IONotificationPortRef    notificationPort = 0;
-static io_iterator_t*           notificationIterators;
+static io_iterator_t            notificationIterator;
 
 struct usb_handle
 {
@@ -61,8 +60,6 @@
 {
     CFMutableDictionaryRef  matchingDict;
     CFRunLoopSourceRef      runLoopSource;
-    SInt32                  vendor, if_subclass, if_protocol;
-    unsigned                i;
 
     //* To set up asynchronous notifications, create a notification port and
     //* add its run loop event source to the program's run loop
@@ -70,47 +67,33 @@
     runLoopSource = IONotificationPortGetRunLoopSource(notificationPort);
     CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
 
-    memset(notificationIterators, 0, sizeof(notificationIterators));
+    //* Create our matching dictionary to find the Android device's
+    //* adb interface
+    //* IOServiceAddMatchingNotification consumes the reference, so we do
+    //* not need to release this
+    matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
 
-    //* loop through all supported vendors
-    for (i = 0; i < vendorIdCount; i++) {
-        //* Create our matching dictionary to find the Android device's
-        //* adb interface
-        //* IOServiceAddMatchingNotification consumes the reference, so we do
-        //* not need to release this
-        matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
-
-        if (!matchingDict) {
-            DBG("ERR: Couldn't create USB matching dictionary.\n");
-            return -1;
-        }
-
-        //* Match based on vendor id, interface subclass and protocol
-        vendor = vendorIds[i];
-        if_subclass = ADB_SUBCLASS;
-        if_protocol = ADB_PROTOCOL;
-        CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID),
-                             CFNumberCreate(kCFAllocatorDefault,
-                                            kCFNumberSInt32Type, &vendor));
-        CFDictionarySetValue(matchingDict, CFSTR(kUSBInterfaceSubClass),
-                             CFNumberCreate(kCFAllocatorDefault,
-                                            kCFNumberSInt32Type, &if_subclass));
-        CFDictionarySetValue(matchingDict, CFSTR(kUSBInterfaceProtocol),
-                             CFNumberCreate(kCFAllocatorDefault,
-                                            kCFNumberSInt32Type, &if_protocol));
-        IOServiceAddMatchingNotification(
-                notificationPort,
-                kIOFirstMatchNotification,
-                matchingDict,
-                AndroidInterfaceAdded,
-                NULL,
-                &notificationIterators[i]);
-
-        //* Iterate over set of matching interfaces to access already-present
-        //* devices and to arm the notification
-        AndroidInterfaceAdded(NULL, notificationIterators[i]);
+    if (!matchingDict) {
+        DBG("ERR: Couldn't create USB matching dictionary.\n");
+        return -1;
     }
 
+    //* We have to get notifications for all potential candidates and test them
+    //* at connection time because the matching rules don't allow for a
+    //* USB interface class of 0xff for class+subclass+protocol matches
+    //* See https://developer.apple.com/library/mac/qa/qa1076/_index.html
+    IOServiceAddMatchingNotification(
+            notificationPort,
+            kIOFirstMatchNotification,
+            matchingDict,
+            AndroidInterfaceAdded,
+            NULL,
+            &notificationIterator);
+
+    //* Iterate over set of matching interfaces to access already-present
+    //* devices and to arm the notification
+    AndroidInterfaceAdded(NULL, notificationIterator);
+
     return 0;
 }
 
@@ -126,6 +109,7 @@
     HRESULT                  result;
     SInt32                   score;
     UInt32                   locationId;
+    UInt8                    class, subclass, protocol;
     UInt16                   vendor;
     UInt16                   product;
     UInt8                    serialIndex;
@@ -156,6 +140,16 @@
             continue;
         }
 
+        kr = (*iface)->GetInterfaceClass(iface, &class);
+        kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
+        kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
+        if(class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
+            // Ignore non-ADB devices.
+            DBG("Ignoring interface with incorrect class/subclass/protocol - %d, %d, %d\n", class, subclass, protocol);
+            (*iface)->Release(iface);
+            continue;
+        }
+
         //* this gets us an ioservice, with which we will find the actual
         //* device; after getting a plugin, and querying the interface, of
         //* course.
@@ -192,12 +186,12 @@
 
         //* Now after all that, we actually have a ref to the device and
         //* the interface that matched our criteria
-
         kr = (*dev)->GetDeviceVendor(dev, &vendor);
         kr = (*dev)->GetDeviceProduct(dev, &product);
         kr = (*dev)->GetLocationID(dev, &locationId);
         if (kr == 0) {
-            snprintf(devpathBuf, sizeof(devpathBuf), "usb:%lX", locationId);
+            snprintf(devpathBuf, sizeof(devpathBuf), "usb:%" PRIu32 "X",
+	             (unsigned int)locationId);
             devpath = devpathBuf;
         }
         kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
@@ -384,8 +378,6 @@
 
 void* RunLoopThread(void* unused)
 {
-    unsigned i;
-
     InitUSB();
 
     currentRunLoop = CFRunLoopGetCurrent();
@@ -398,9 +390,7 @@
     CFRunLoopRun();
     currentRunLoop = 0;
 
-    for (i = 0; i < vendorIdCount; i++) {
-        IOObjectRelease(notificationIterators[i]);
-    }
+    IOObjectRelease(notificationIterator);
     IONotificationPortDestroy(notificationPort);
 
     DBG("RunLoopThread done\n");
@@ -415,9 +405,6 @@
     {
         adb_thread_t    tid;
 
-        notificationIterators = (io_iterator_t*)malloc(
-            vendorIdCount * sizeof(io_iterator_t));
-
         adb_mutex_init(&start_lock, NULL);
         adb_cond_init(&start_cond, NULL);
 
@@ -442,11 +429,6 @@
     close_usb_devices();
     if (currentRunLoop)
         CFRunLoopStop(currentRunLoop);
-
-    if (notificationIterators != NULL) {
-        free(notificationIterators);
-        notificationIterators = NULL;
-    }
 }
 
 int usb_write(usb_handle *handle, const void *buf, int len)
diff --git a/adb/usb_vendors.c b/adb/usb_vendors.c
deleted file mode 100755
index 19bcae4..0000000
--- a/adb/usb_vendors.c
+++ /dev/null
@@ -1,370 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "usb_vendors.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#ifdef _WIN32
-#  ifndef WIN32_LEAN_AND_MEAN
-#    define WIN32_LEAN_AND_MEAN
-#  endif
-#  include "windows.h"
-#  include "shlobj.h"
-#else
-#  include <unistd.h>
-#  include <sys/stat.h>
-#endif
-
-#include "sysdeps.h"
-#include "adb.h"
-
-#define ANDROID_PATH            ".android"
-#define ANDROID_ADB_INI         "adb_usb.ini"
-
-#define TRACE_TAG               TRACE_USB
-
-/* Keep the list below sorted alphabetically by #define name */
-// Acer's USB Vendor ID
-#define VENDOR_ID_ACER          0x0502
-// Allwinner's USB Vendor ID
-#define VENDOR_ID_ALLWINNER     0x1F3A
-// Amlogic's USB Vendor ID
-#define VENDOR_ID_AMLOGIC       0x1b8e
-// AnyDATA's USB Vendor ID
-#define VENDOR_ID_ANYDATA       0x16D5
-// Archos's USB Vendor ID
-#define VENDOR_ID_ARCHOS        0x0E79
-// Asus's USB Vendor ID
-#define VENDOR_ID_ASUS          0x0b05
-// BYD's USB Vendor ID
-#define VENDOR_ID_BYD           0x1D91
-// Compal's USB Vendor ID
-#define VENDOR_ID_COMPAL        0x04B7
-// Compalcomm's USB Vendor ID
-#define VENDOR_ID_COMPALCOMM    0x1219
-// Dell's USB Vendor ID
-#define VENDOR_ID_DELL          0x413c
-// ECS's USB Vendor ID
-#define VENDOR_ID_ECS           0x03fc
-// EMERGING_TECH's USB Vendor ID
-#define VENDOR_ID_EMERGING_TECH 0x297F
-// Emerson's USB Vendor ID
-#define VENDOR_ID_EMERSON       0x2207
-// Foxconn's USB Vendor ID
-#define VENDOR_ID_FOXCONN       0x0489
-// Fujitsu's USB Vendor ID
-#define VENDOR_ID_FUJITSU       0x04C5
-// Funai's USB Vendor ID
-#define VENDOR_ID_FUNAI         0x0F1C
-// Garmin-Asus's USB Vendor ID
-#define VENDOR_ID_GARMIN_ASUS   0x091E
-// Gigabyte's USB Vendor ID
-#define VENDOR_ID_GIGABYTE      0x0414
-// Gigaset's USB Vendor ID
-#define VENDOR_ID_GIGASET       0x1E85
-// GIONEE's USB Vendor ID
-#define VENDOR_ID_GIONEE        0x271D
-// Google's USB Vendor ID
-#define VENDOR_ID_GOOGLE        0x18d1
-// Haier's USB Vendor ID
-#define VENDOR_ID_HAIER         0x201E
-// Harris's USB Vendor ID
-#define VENDOR_ID_HARRIS        0x19A5
-// Hisense's USB Vendor ID
-#define VENDOR_ID_HISENSE       0x109b
-// Honeywell's USB Vendor ID
-#define VENDOR_ID_HONEYWELL     0x0c2e
-// HP's USB Vendor ID
-#define VENDOR_ID_HP            0x03f0
-// HTC's USB Vendor ID
-#define VENDOR_ID_HTC           0x0bb4
-// Huawei's USB Vendor ID
-#define VENDOR_ID_HUAWEI        0x12D1
-// INQ Mobile's USB Vendor ID
-#define VENDOR_ID_INQ_MOBILE    0x2314
-// Intel's USB Vendor ID
-#define VENDOR_ID_INTEL         0x8087
-// Intermec's USB Vendor ID
-#define VENDOR_ID_INTERMEC      0x067e
-// IRiver's USB Vendor ID
-#define VENDOR_ID_IRIVER        0x2420
-// K-Touch's USB Vendor ID
-#define VENDOR_ID_K_TOUCH       0x24E3
-// KT Tech's USB Vendor ID
-#define VENDOR_ID_KT_TECH       0x2116
-// Kobo's USB Vendor ID
-#define VENDOR_ID_KOBO          0x2237
-// Kyocera's USB Vendor ID
-#define VENDOR_ID_KYOCERA       0x0482
-// Lab126's USB Vendor ID
-#define VENDOR_ID_LAB126        0x1949
-// Lenovo's USB Vendor ID
-#define VENDOR_ID_LENOVO        0x17EF
-// LenovoMobile's USB Vendor ID
-#define VENDOR_ID_LENOVOMOBILE  0x2006
-// LG's USB Vendor ID
-#define VENDOR_ID_LGE           0x1004
-// Lumigon's USB Vendor ID
-#define VENDOR_ID_LUMIGON       0x25E3
-// Motorola's USB Vendor ID
-#define VENDOR_ID_MOTOROLA      0x22b8
-// MSI's USB Vendor ID
-#define VENDOR_ID_MSI           0x0DB0
-// MTK's USB Vendor ID
-#define VENDOR_ID_MTK           0x0e8d
-// NEC's USB Vendor ID
-#define VENDOR_ID_NEC           0x0409
-// B&N Nook's USB Vendor ID
-#define VENDOR_ID_NOOK          0x2080
-// Nvidia's USB Vendor ID
-#define VENDOR_ID_NVIDIA        0x0955
-// OPPO's USB Vendor ID
-#define VENDOR_ID_OPPO          0x22D9
-// On-The-Go-Video's USB Vendor ID
-#define VENDOR_ID_OTGV          0x2257
-// OUYA's USB Vendor ID
-#define VENDOR_ID_OUYA          0x2836
-// Pantech's USB Vendor ID
-#define VENDOR_ID_PANTECH       0x10A9
-// Pegatron's USB Vendor ID
-#define VENDOR_ID_PEGATRON      0x1D4D
-// Philips's USB Vendor ID
-#define VENDOR_ID_PHILIPS       0x0471
-// Panasonic Mobile Communication's USB Vendor ID
-#define VENDOR_ID_PMC           0x04DA
-// Positivo's USB Vendor ID
-#define VENDOR_ID_POSITIVO      0x1662
-// Prestigio's USB Vendor ID
-#define VENDOR_ID_PRESTIGIO     0x29e4
-// Qisda's USB Vendor ID
-#define VENDOR_ID_QISDA         0x1D45
-// Qualcomm's USB Vendor ID
-#define VENDOR_ID_QUALCOMM      0x05c6
-// Quanta's USB Vendor ID
-#define VENDOR_ID_QUANTA        0x0408
-// Rockchip's USB Vendor ID
-#define VENDOR_ID_ROCKCHIP      0x2207
-// Samsung's USB Vendor ID
-#define VENDOR_ID_SAMSUNG       0x04e8
-// Sharp's USB Vendor ID
-#define VENDOR_ID_SHARP         0x04dd
-// SK Telesys's USB Vendor ID
-#define VENDOR_ID_SK_TELESYS    0x1F53
-// Smartisan's USB Vendor ID
-#define VENDOR_ID_SMARTISAN     0x29a9
-// Sony's USB Vendor ID
-#define VENDOR_ID_SONY          0x054C
-// Sony Ericsson's USB Vendor ID
-#define VENDOR_ID_SONY_ERICSSON 0x0FCE
-// T & A Mobile Phones' USB Vendor ID
-#define VENDOR_ID_T_AND_A       0x1BBB
-// TechFaith's USB Vendor ID
-#define VENDOR_ID_TECHFAITH     0x1d09
-// Teleepoch's USB Vendor ID
-#define VENDOR_ID_TELEEPOCH     0x2340
-// Texas Instruments's USB Vendor ID
-#define VENDOR_ID_TI            0x0451
-// Toshiba's USB Vendor ID
-#define VENDOR_ID_TOSHIBA       0x0930
-// Unowhy's USB Vendor ID
-#define VENDOR_ID_UNOWHY        0x2A49
-// Vizio's USB Vendor ID
-#define VENDOR_ID_VIZIO         0xE040
-// Wacom's USB Vendor ID
-#define VENDOR_ID_WACOM         0x0531
-// Xiaomi's USB Vendor ID
-#define VENDOR_ID_XIAOMI        0x2717
-// YotaDevices's USB Vendor ID
-#define VENDOR_ID_YOTADEVICES   0x2916
-// Yulong Coolpad's USB Vendor ID
-#define VENDOR_ID_YULONG_COOLPAD 0x1EBF
-// ZTE's USB Vendor ID
-#define VENDOR_ID_ZTE           0x19D2
-/* Keep the list above sorted alphabetically by #define name */
-
-/** built-in vendor list */
-/* Keep the list below sorted alphabetically */
-int builtInVendorIds[] = {
-    VENDOR_ID_ACER,
-    VENDOR_ID_ALLWINNER,
-    VENDOR_ID_AMLOGIC,
-    VENDOR_ID_ANYDATA,
-    VENDOR_ID_ARCHOS,
-    VENDOR_ID_ASUS,
-    VENDOR_ID_BYD,
-    VENDOR_ID_COMPAL,
-    VENDOR_ID_COMPALCOMM,
-    VENDOR_ID_DELL,
-    VENDOR_ID_ECS,
-    VENDOR_ID_EMERGING_TECH,
-    VENDOR_ID_EMERSON,
-    VENDOR_ID_FOXCONN,
-    VENDOR_ID_FUJITSU,
-    VENDOR_ID_FUNAI,
-    VENDOR_ID_GARMIN_ASUS,
-    VENDOR_ID_GIGABYTE,
-    VENDOR_ID_GIGASET,
-    VENDOR_ID_GIONEE,
-    VENDOR_ID_GOOGLE,
-    VENDOR_ID_HAIER,
-    VENDOR_ID_HARRIS,
-    VENDOR_ID_HISENSE,
-    VENDOR_ID_HONEYWELL,
-    VENDOR_ID_HP,
-    VENDOR_ID_HTC,
-    VENDOR_ID_HUAWEI,
-    VENDOR_ID_INQ_MOBILE,
-    VENDOR_ID_INTEL,
-    VENDOR_ID_INTERMEC,
-    VENDOR_ID_IRIVER,
-    VENDOR_ID_KOBO,
-    VENDOR_ID_K_TOUCH,
-    VENDOR_ID_KT_TECH,
-    VENDOR_ID_KYOCERA,
-    VENDOR_ID_LAB126,
-    VENDOR_ID_LENOVO,
-    VENDOR_ID_LENOVOMOBILE,
-    VENDOR_ID_LGE,
-    VENDOR_ID_LUMIGON,
-    VENDOR_ID_MOTOROLA,
-    VENDOR_ID_MSI,
-    VENDOR_ID_MTK,
-    VENDOR_ID_NEC,
-    VENDOR_ID_NOOK,
-    VENDOR_ID_NVIDIA,
-    VENDOR_ID_OPPO,
-    VENDOR_ID_OTGV,
-    VENDOR_ID_OUYA,
-    VENDOR_ID_PANTECH,
-    VENDOR_ID_PEGATRON,
-    VENDOR_ID_PHILIPS,
-    VENDOR_ID_PMC,
-    VENDOR_ID_POSITIVO,
-    VENDOR_ID_PRESTIGIO,
-    VENDOR_ID_QISDA,
-    VENDOR_ID_QUALCOMM,
-    VENDOR_ID_QUANTA,
-    VENDOR_ID_ROCKCHIP,
-    VENDOR_ID_SAMSUNG,
-    VENDOR_ID_SHARP,
-    VENDOR_ID_SK_TELESYS,
-    VENDOR_ID_SMARTISAN,
-    VENDOR_ID_SONY,
-    VENDOR_ID_SONY_ERICSSON,
-    VENDOR_ID_T_AND_A,
-    VENDOR_ID_TECHFAITH,
-    VENDOR_ID_TELEEPOCH,
-    VENDOR_ID_TI,
-    VENDOR_ID_TOSHIBA,
-    VENDOR_ID_UNOWHY,
-    VENDOR_ID_VIZIO,
-    VENDOR_ID_WACOM,
-    VENDOR_ID_XIAOMI,
-    VENDOR_ID_YOTADEVICES,
-    VENDOR_ID_YULONG_COOLPAD,
-    VENDOR_ID_ZTE,
-};
-/* Keep the list above sorted alphabetically */
-
-#define BUILT_IN_VENDOR_COUNT    (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
-
-/* max number of supported vendor ids (built-in + 3rd party). increase as needed */
-#define VENDOR_COUNT_MAX         128
-
-int vendorIds[VENDOR_COUNT_MAX];
-unsigned vendorIdCount = 0;
-
-int get_adb_usb_ini(char* buff, size_t len);
-
-void usb_vendors_init(void)
-{
-    if (VENDOR_COUNT_MAX < BUILT_IN_VENDOR_COUNT) {
-        fprintf(stderr, "VENDOR_COUNT_MAX not big enough for built-in vendor list.\n");
-        exit(2);
-    }
-
-    /* add the built-in vendors at the beginning of the array */
-    memcpy(vendorIds, builtInVendorIds, sizeof(builtInVendorIds));
-
-    /* default array size is the number of built-in vendors */
-    vendorIdCount = BUILT_IN_VENDOR_COUNT;
-
-    if (VENDOR_COUNT_MAX == BUILT_IN_VENDOR_COUNT)
-        return;
-
-    char temp[PATH_MAX];
-    if (get_adb_usb_ini(temp, sizeof(temp)) == 0) {
-        FILE * f = fopen(temp, "rt");
-
-        if (f != NULL) {
-            /* The vendor id file is pretty basic. 1 vendor id per line.
-               Lines starting with # are comments */
-            while (fgets(temp, sizeof(temp), f) != NULL) {
-                if (temp[0] == '#')
-                    continue;
-
-                long value = strtol(temp, NULL, 0);
-                if (errno == EINVAL || errno == ERANGE || value > INT_MAX || value < 0) {
-                    fprintf(stderr, "Invalid content in %s. Quitting.\n", ANDROID_ADB_INI);
-                    exit(2);
-                }
-
-                vendorIds[vendorIdCount++] = (int)value;
-
-                /* make sure we don't go beyond the array */
-                if (vendorIdCount == VENDOR_COUNT_MAX) {
-                    break;
-                }
-            }
-            fclose(f);
-        }
-    }
-}
-
-/* Utils methods */
-
-/* builds the path to the adb vendor id file. returns 0 if success */
-int build_path(char* buff, size_t len, const char* format, const char* home)
-{
-    if (snprintf(buff, len, format, home, ANDROID_PATH, ANDROID_ADB_INI) >= (signed)len) {
-        return 1;
-    }
-
-    return 0;
-}
-
-/* fills buff with the path to the adb vendor id file. returns 0 if success */
-int get_adb_usb_ini(char* buff, size_t len)
-{
-#ifdef _WIN32
-    const char* home = getenv("ANDROID_SDK_HOME");
-    if (home != NULL) {
-        return build_path(buff, len, "%s\\%s\\%s", home);
-    } else {
-        char path[MAX_PATH];
-        SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, path);
-        return build_path(buff, len, "%s\\%s\\%s", path);
-    }
-#else
-    const char* home = getenv("HOME");
-    if (home == NULL)
-        home = "/tmp";
-
-    return build_path(buff, len, "%s/%s/%s", home);
-#endif
-}
diff --git a/adb/usb_vendors.h b/adb/usb_vendors.h
deleted file mode 100644
index cee23a1..0000000
--- a/adb/usb_vendors.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __USB_VENDORS_H
-#define __USB_VENDORS_H
-
-extern int vendorIds[];
-extern unsigned  vendorIdCount;
-
-void usb_vendors_init(void);
-
-#endif
diff --git a/adf/libadf/adf.c b/adf/libadf/adf.c
index 1d19152..66c329c 100644
--- a/adf/libadf/adf.c
+++ b/adf/libadf/adf.c
@@ -17,9 +17,11 @@
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <malloc.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <linux/limits.h>
 
diff --git a/adf/libadf/tests/Android.mk b/adf/libadf/tests/Android.mk
new file mode 100644
index 0000000..93efafa
--- /dev/null
+++ b/adf/libadf/tests/Android.mk
@@ -0,0 +1,22 @@
+#
+# Copyright (C) 2013 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+LOCAL_PATH := $(my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := adf_test.cpp
+LOCAL_MODULE := adf-unit-tests
+LOCAL_STATIC_LIBRARIES := libadf
+include $(BUILD_NATIVE_TEST)
diff --git a/adf/libadf/tests/adf_test.cpp b/adf/libadf/tests/adf_test.cpp
new file mode 100644
index 0000000..d95330d
--- /dev/null
+++ b/adf/libadf/tests/adf_test.cpp
@@ -0,0 +1,342 @@
+/*
+ * Copyright (C) 2013 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 <adf/adf.h>
+#include <gtest/gtest.h>
+#include <sys/mman.h>
+
+class AdfTest : public testing::Test {
+public:
+    AdfTest() : intf_id(0), intf(-1), eng_id(0), eng(-1) { }
+
+    virtual void SetUp() {
+        int err = adf_device_open(dev_id, O_RDWR, &dev);
+        ASSERT_GE(err, 0) << "opening ADF device " << dev_id <<
+                " failed: " << strerror(-err);
+
+        err = adf_find_simple_post_configuration(&dev, fmt8888, n_fmt8888,
+                &intf_id, &eng_id);
+        ASSERT_GE(err, 0) << "finding ADF configuration failed: " <<
+                strerror(-err);
+
+        intf = adf_interface_open(&dev, intf_id, O_RDWR);
+        ASSERT_GE(intf, 0) << "opening ADF interface " << dev_id << "." <<
+                intf_id << " failed: " << strerror(-intf);
+
+        eng = adf_overlay_engine_open(&dev, eng_id, O_RDWR);
+        ASSERT_GE(eng, 0) << "opening ADF overlay engine " << dev_id << "." <<
+                eng_id << " failed: " << strerror(-eng);
+    }
+
+    virtual void TearDown() {
+        if (eng >= 0)
+            close(eng);
+        if (intf >= 0)
+            close(intf);
+        adf_device_close(&dev);
+    }
+
+    void get8888Format(uint32_t &fmt, char fmt_str[ADF_FORMAT_STR_SIZE]) {
+        adf_overlay_engine_data data;
+        int err = adf_get_overlay_engine_data(eng, &data);
+        ASSERT_GE(err, 0) << "getting ADF overlay engine data failed: " <<
+                strerror(-err);
+
+        for (size_t i = 0; i < data.n_supported_formats; i++) {
+            for (size_t j = 0; j < n_fmt8888; j++) {
+                if (data.supported_formats[i] == fmt8888[j]) {
+                    fmt = data.supported_formats[i];
+                    adf_format_str(fmt, fmt_str);
+                    adf_free_overlay_engine_data(&data);
+                    return;
+                }
+            }
+        }
+
+        adf_free_overlay_engine_data(&data);
+        FAIL(); /* this should never happen */
+    }
+
+    void drawCheckerboard(void *buf, uint32_t w, uint32_t h, uint32_t pitch) {
+        uint8_t *buf8 = reinterpret_cast<uint8_t *>(buf);
+        for (uint32_t y = 0; y < h / 2; y++) {
+            uint32_t *scanline = reinterpret_cast<uint32_t *>(buf8 + y * pitch);
+            for (uint32_t x = 0; x < w / 2; x++)
+                scanline[x] = 0xFF0000FF;
+            for (uint32_t x = w / 2; x < w; x++)
+                scanline[x] = 0xFF00FFFF;
+        }
+        for (uint32_t y = h / 2; y < h; y++) {
+            uint32_t *scanline = reinterpret_cast<uint32_t *>(buf8 + y * pitch);
+            for (uint32_t x = 0; x < w / 2; x++)
+                scanline[x] = 0xFFFF00FF;
+            for (uint32_t x = w / 2; x < w; x++)
+                scanline[x] = 0xFFFFFFFF;
+        }
+    }
+
+    /* various helpers to call ADF and die on failure */
+
+    void getInterfaceData(adf_interface_data &data) {
+         int err = adf_get_interface_data(intf, &data);
+         ASSERT_GE(err, 0) << "getting ADF interface data failed: " <<
+                 strerror(-err);
+    }
+
+    void getCurrentMode(uint32_t &w, uint32_t &h) {
+        adf_interface_data data;
+        ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
+        w = data.current_mode.hdisplay;
+        h = data.current_mode.vdisplay;
+        adf_free_interface_data(&data);
+    }
+
+    void blank(uint8_t mode) {
+        int err = adf_interface_blank(intf, mode);
+        ASSERT_FALSE(err < 0 && err != -EBUSY) <<
+                "unblanking interface failed: " << strerror(-err);
+    }
+
+    void attach() {
+        int err = adf_device_attach(&dev, eng_id, intf_id);
+        ASSERT_FALSE(err < 0 && err != -EALREADY) <<
+                "attaching overlay engine " << eng_id << " to interface " <<
+                intf_id << " failed: " << strerror(-err);
+    }
+
+    void detach() {
+        int err = adf_device_detach(&dev, eng_id, intf_id);
+        ASSERT_FALSE(err < 0 && err != -EINVAL) <<
+                "detaching overlay engine " << eng_id << " from interface " <<
+                intf_id << " failed: " << strerror(-err);
+    }
+
+    void readVsyncTimestamp(uint64_t &timestamp) {
+        adf_event *event;
+        int err = adf_read_event(intf, &event);
+        ASSERT_GE(err, 0) << "reading ADF event failed: " << strerror(-err);
+
+        ASSERT_EQ(ADF_EVENT_VSYNC, event->type);
+        ASSERT_EQ(sizeof(adf_vsync_event), event->length);
+
+        adf_vsync_event *vsync_event =
+                reinterpret_cast<adf_vsync_event *>(event);
+        timestamp = vsync_event->timestamp;
+        free(event);
+    }
+
+protected:
+    adf_device dev;
+    adf_id_t intf_id;
+    int intf;
+    adf_id_t eng_id;
+    int eng;
+
+private:
+    const static adf_id_t dev_id = 0;
+    const static __u32 fmt8888[];
+    const static size_t n_fmt8888;
+};
+
+const __u32 AdfTest::fmt8888[] = {
+   DRM_FORMAT_XRGB8888,
+   DRM_FORMAT_XBGR8888,
+   DRM_FORMAT_RGBX8888,
+   DRM_FORMAT_BGRX8888,
+   DRM_FORMAT_ARGB8888,
+   DRM_FORMAT_ABGR8888,
+   DRM_FORMAT_RGBA8888,
+   DRM_FORMAT_BGRA8888
+};
+const size_t AdfTest::n_fmt8888 = sizeof(fmt8888) / sizeof(fmt8888[0]);
+
+TEST(adf, devices) {
+    adf_id_t *devs;
+    ssize_t n_devs = adf_devices(&devs);
+    free(devs);
+
+    ASSERT_GE(n_devs, 0) << "enumerating ADF devices failed: " <<
+            strerror(-n_devs);
+    ASSERT_TRUE(devs != NULL);
+}
+
+TEST_F(AdfTest, device_data) {
+    adf_device_data data;
+    int err = adf_get_device_data(&dev, &data);
+    ASSERT_GE(err, 0) << "getting ADF device data failed: " << strerror(-err);
+
+    EXPECT_LT(data.n_attachments, ADF_MAX_ATTACHMENTS);
+    EXPECT_GT(data.n_allowed_attachments, 0);
+    EXPECT_LT(data.n_allowed_attachments, ADF_MAX_ATTACHMENTS);
+    EXPECT_LT(data.custom_data_size, ADF_MAX_CUSTOM_DATA_SIZE);
+    adf_free_device_data(&data);
+}
+
+TEST_F(AdfTest, interface_data) {
+    adf_interface_data data;
+    ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
+
+    EXPECT_LT(data.type, ADF_INTF_TYPE_MAX);
+    EXPECT_LE(data.dpms_state, DRM_MODE_DPMS_OFF);
+    EXPECT_EQ(1, data.hotplug_detect);
+    EXPECT_GT(data.n_available_modes, 0);
+    EXPECT_LT(data.custom_data_size, ADF_MAX_CUSTOM_DATA_SIZE);
+    adf_free_interface_data(&data);
+}
+
+TEST_F(AdfTest, overlay_engine_data) {
+    adf_overlay_engine_data data;
+    int err = adf_get_overlay_engine_data(eng, &data);
+    ASSERT_GE(err, 0) << "getting ADF overlay engine failed: " <<
+            strerror(-err);
+
+    EXPECT_GT(data.n_supported_formats, 0);
+    EXPECT_LT(data.n_supported_formats, ADF_MAX_SUPPORTED_FORMATS);
+    EXPECT_LT(data.custom_data_size, ADF_MAX_CUSTOM_DATA_SIZE);
+    adf_free_overlay_engine_data(&data);
+}
+
+TEST_F(AdfTest, blank) {
+    int err = adf_interface_blank(intf, (uint8_t)-1);
+    EXPECT_EQ(-EINVAL, err) << "setting bogus DPMS mode should have failed";
+
+    err = adf_interface_blank(eng, DRM_MODE_DPMS_OFF);
+    EXPECT_EQ(-EINVAL, err) << "blanking overlay engine should have failed";
+
+    ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_OFF));
+    err = adf_interface_blank(intf, DRM_MODE_DPMS_OFF);
+    EXPECT_EQ(-EBUSY, err) << "blanking interface twice should have failed";
+
+    ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
+    err = adf_interface_blank(intf, DRM_MODE_DPMS_ON);
+    EXPECT_EQ(-EBUSY, err) << "unblanking interface twice should have failed";
+
+    adf_interface_data data;
+    ASSERT_NO_FATAL_FAILURE(getInterfaceData(data));
+    EXPECT_EQ(DRM_MODE_DPMS_ON, data.dpms_state);
+    adf_free_interface_data(&data);
+}
+
+TEST_F(AdfTest, event) {
+    int err = adf_set_event(intf, ADF_EVENT_TYPE_MAX, true);
+    EXPECT_EQ(-EINVAL, err) << "enabling bogus ADF event should have failed";
+
+    err = adf_set_event(intf, ADF_EVENT_TYPE_MAX, false);
+    EXPECT_EQ(-EINVAL, err) << "disabling bogus ADF event should have failed";
+
+    err = adf_set_event(intf, ADF_EVENT_VSYNC, true);
+    ASSERT_GE(err, 0) << "enabling vsync event failed: " << strerror(-err);
+
+    err = adf_set_event(intf, ADF_EVENT_VSYNC, true);
+    EXPECT_EQ(-EALREADY, err) <<
+            "enabling vsync event twice should have failed";
+
+    ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
+
+    uint64_t timestamp1, timestamp2;
+    ASSERT_NO_FATAL_FAILURE(readVsyncTimestamp(timestamp1));
+    ASSERT_NO_FATAL_FAILURE(readVsyncTimestamp(timestamp2));
+    EXPECT_GT(timestamp2, timestamp1);
+
+    err = adf_set_event(intf, ADF_EVENT_VSYNC, false);
+    EXPECT_GE(err, 0) << "disabling vsync event failed: " << strerror(-err);
+
+    err = adf_set_event(intf, ADF_EVENT_VSYNC, false);
+    EXPECT_EQ(-EALREADY, err) <<
+            "disabling vsync event twice should have failed";
+}
+
+TEST_F(AdfTest, attach) {
+    ASSERT_NO_FATAL_FAILURE(attach());
+    int err = adf_device_attach(&dev, eng_id, intf_id);
+    EXPECT_EQ(-EALREADY, err) << "attaching overlay engine " << eng_id <<
+            " to interface " << intf_id << " twice should have failed";
+
+    ASSERT_NO_FATAL_FAILURE(detach());
+    err = adf_device_detach(&dev, eng_id, intf_id);
+    EXPECT_EQ(-EINVAL, err) << "detaching overlay engine " << eng_id <<
+            " from interface " << intf_id << " twice should have failed";
+
+    err = adf_device_attach(&dev, eng_id, ADF_MAX_INTERFACES);
+    EXPECT_EQ(-EINVAL, err) << "attaching overlay engine " << eng_id <<
+            " to bogus interface should have failed";
+
+    err = adf_device_detach(&dev, eng_id, ADF_MAX_INTERFACES);
+    EXPECT_EQ(-EINVAL, err) << "detaching overlay engine " << eng_id <<
+            " from bogus interface should have failed";
+}
+
+TEST_F(AdfTest, simple_buffer_alloc) {
+    uint32_t w = 0, h = 0;
+    ASSERT_NO_FATAL_FAILURE(getCurrentMode(w, h));
+
+    uint32_t format;
+    char format_str[ADF_FORMAT_STR_SIZE];
+    ASSERT_NO_FATAL_FAILURE(get8888Format(format, format_str));
+
+    uint32_t offset;
+    uint32_t pitch;
+    int buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, format, &offset,
+            &pitch);
+    EXPECT_GE(buf_fd, 0) << "allocating " << w << "x" << h << " " <<
+            format_str << " buffer failed: " << strerror(-buf_fd);
+    EXPECT_GE(pitch, w * 4);
+    close(buf_fd);
+
+    buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, 0xDEADBEEF, &offset,
+            &pitch);
+    /* n.b.: ADF only allows simple buffers with built-in RGB formats,
+       so this should fail even if a driver supports custom format 0xDEADBEEF */
+    EXPECT_EQ(-EINVAL, buf_fd) <<
+            "allocating buffer with bogus format should have failed";
+}
+
+TEST_F(AdfTest, simple_buffer) {
+    uint32_t w = 0, h = 0;
+    ASSERT_NO_FATAL_FAILURE(getCurrentMode(w, h));
+
+    uint32_t format = 0;
+    char format_str[ADF_FORMAT_STR_SIZE];
+    ASSERT_NO_FATAL_FAILURE(get8888Format(format, format_str));
+
+    uint32_t offset;
+    uint32_t pitch;
+    int buf_fd = adf_interface_simple_buffer_alloc(intf, w, h, format, &offset,
+            &pitch);
+    ASSERT_GE(buf_fd, 0) << "allocating " << w << "x" << h << " " <<
+            format_str << " buffer failed: " << strerror(-buf_fd);
+    EXPECT_GE(pitch, w * 4);
+
+    void *mapped = mmap(NULL, pitch * h, PROT_WRITE, MAP_SHARED, buf_fd,
+            offset);
+    ASSERT_NE(mapped, MAP_FAILED) << "mapping " << w << "x" << h << " " <<
+            format_str << " buffer failed: " << strerror(-errno);
+    drawCheckerboard(mapped, w, h, pitch);
+    munmap(mapped, pitch * h);
+
+    ASSERT_NO_FATAL_FAILURE(attach());
+    ASSERT_NO_FATAL_FAILURE(blank(DRM_MODE_DPMS_ON));
+
+    int release_fence = adf_interface_simple_post(intf, eng_id, w, h, format,
+            buf_fd, offset, pitch, -1);
+    close(buf_fd);
+    ASSERT_GE(release_fence, 0) << "posting " << w << "x" << h << " " <<
+            format_str << " buffer failed: " << strerror(-release_fence);
+    close(release_fence);
+}
diff --git a/adf/libadfhwc/adfhwc.cpp b/adf/libadfhwc/adfhwc.cpp
index 57e09eb..21f245e 100644
--- a/adf/libadfhwc/adfhwc.cpp
+++ b/adf/libadfhwc/adfhwc.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <fcntl.h>
+#include <malloc.h>
 #include <poll.h>
 #include <pthread.h>
 #include <sys/resource.h>
diff --git a/debuggerd/Android.mk b/debuggerd/Android.mk
index c33b263..fbaac39 100644
--- a/debuggerd/Android.mk
+++ b/debuggerd/Android.mk
@@ -22,13 +22,17 @@
     -Wunused \
     -Werror \
 
+ifeq ($(TARGET_IS_64_BIT),true)
+LOCAL_CPPFLAGS += -DTARGET_IS_64_BIT
+endif
+
 LOCAL_SHARED_LIBRARIES := \
     libbacktrace \
     libcutils \
     liblog \
     libselinux \
 
-include external/stlport/libstlport.mk
+LOCAL_CLANG := true
 
 LOCAL_MODULE := debuggerd
 LOCAL_MODULE_STEM_32 := debuggerd
diff --git a/debuggerd/crasher.c b/debuggerd/crasher.c
index d315ee5..af86fe9 100644
--- a/debuggerd/crasher.c
+++ b/debuggerd/crasher.c
@@ -32,16 +32,23 @@
     }
 }
 
-static int smash_stack(int i __unused) {
+static char* smash_stack_dummy_buf;
+__attribute__ ((noinline)) static void smash_stack_dummy_function(volatile int* plen) {
+  smash_stack_dummy_buf[*plen] = 0;
+}
+
+// This must be marked with "__attribute__ ((noinline))", to ensure the
+// compiler generates the proper stack guards around this function.
+// Assign local array address to global variable to force stack guards.
+// Use another noinline function to corrupt the stack.
+__attribute__ ((noinline)) static int smash_stack(volatile int* plen) {
     printf("crasher: deliberately corrupting stack...\n");
-    // Unless there's a "big enough" buffer on the stack, gcc
-    // doesn't bother inserting checks.
-    char buf[8];
-    // If we don't write something relatively unpredictable
-    // into the buffer and then do something with it, gcc
-    // optimizes everything away and just returns a constant.
-    *(int*)(&buf[7]) = (uintptr_t) &buf[0];
-    return *(int*)(&buf[0]);
+
+    char buf[128];
+    smash_stack_dummy_buf = buf;
+    // This should corrupt stack guards and make process abort.
+    smash_stack_dummy_function(plen);
+    return 0;
 }
 
 static void* global = 0; // So GCC doesn't optimize the tail recursion out of overflow_stack.
@@ -59,7 +66,7 @@
     for(;;) {
         usleep(250*1000);
         write(2, &c, 1);
-        if(c == 'C') *((unsigned*) 0) = 42;
+        if(c == 'C') *((volatile unsigned*) 0) = 42;
     }
     return NULL;
 }
@@ -125,7 +132,8 @@
     } else if (!strcmp(arg, "SIGSEGV-non-null")) {
         sigsegv_non_null();
     } else if (!strcmp(arg, "smash-stack")) {
-        return smash_stack(42);
+        volatile int len = 128;
+        return smash_stack(&len);
     } else if (!strcmp(arg, "stack-overflow")) {
         overflow_stack(NULL);
     } else if (!strcmp(arg, "nostack")) {
diff --git a/debuggerd/debuggerd.cpp b/debuggerd/debuggerd.cpp
index 06c16f8..039b8ec 100644
--- a/debuggerd/debuggerd.cpp
+++ b/debuggerd/debuggerd.cpp
@@ -30,6 +30,8 @@
 #include <sys/stat.h>
 #include <sys/poll.h>
 
+#include <selinux/android.h>
+
 #include <log/logger.h>
 
 #include <cutils/sockets.h>
@@ -45,6 +47,14 @@
 #include "tombstone.h"
 #include "utility.h"
 
+// If the 32 bit executable is compiled on a 64 bit system,
+// use the 32 bit socket name.
+#if defined(TARGET_IS_64_BIT) && !defined(__LP64__)
+#define SOCKET_NAME DEBUGGER32_SOCKET_NAME
+#else
+#define SOCKET_NAME DEBUGGER_SOCKET_NAME
+#endif
+
 struct debugger_request_t {
   debugger_action_t action;
   pid_t pid, tid;
@@ -124,6 +134,53 @@
   return fields == 7 ? 0 : -1;
 }
 
+static int selinux_enabled;
+
+/*
+ * Corresponds with debugger_action_t enum type in
+ * include/cutils/debugger.h.
+ */
+static const char *debuggerd_perms[] = {
+  NULL, /* crash is only used on self, no check applied */
+  "dump_tombstone",
+  "dump_backtrace"
+};
+
+static bool selinux_action_allowed(int s, pid_t tid, debugger_action_t action)
+{
+  char *scon = NULL, *tcon = NULL;
+  const char *tclass = "debuggerd";
+  const char *perm;
+  bool allowed = false;
+
+  if (selinux_enabled <= 0)
+    return true;
+
+  if (action <= 0 || action >= (sizeof(debuggerd_perms)/sizeof(debuggerd_perms[0]))) {
+    ALOGE("SELinux:  No permission defined for debugger action %d", action);
+    return false;
+  }
+
+  perm = debuggerd_perms[action];
+
+  if (getpeercon(s, &scon) < 0) {
+    ALOGE("Cannot get peer context from socket\n");
+    goto out;
+  }
+
+  if (getpidcon(tid, &tcon) < 0) {
+    ALOGE("Cannot get context for tid %d\n", tid);
+    goto out;
+  }
+
+  allowed = (selinux_check_access(scon, tcon, tclass, perm, NULL) == 0);
+
+out:
+   freecon(scon);
+   freecon(tcon);
+   return allowed;
+}
+
 static int read_request(int fd, debugger_request_t* out_request) {
   ucred cr;
   socklen_t len = sizeof(cr);
@@ -158,7 +215,7 @@
     return -1;
   }
 
-  out_request->action = msg.action;
+  out_request->action = static_cast<debugger_action_t>(msg.action);
   out_request->tid = msg.tid;
   out_request->pid = cr.pid;
   out_request->uid = cr.uid;
@@ -186,6 +243,9 @@
       ALOGE("tid %d does not exist. ignoring explicit dump request\n", out_request->tid);
       return -1;
     }
+
+    if (!selinux_action_allowed(fd, out_request->tid, out_request->action))
+      return -1;
   } else {
     // No one else is allowed to dump arbitrary processes.
     return -1;
@@ -203,6 +263,85 @@
   return false;
 }
 
+#if defined(__LP64__)
+static bool is32bit(pid_t tid) {
+  char* exeline;
+  if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
+    return false;
+  }
+  int fd = TEMP_FAILURE_RETRY(open(exeline, O_RDONLY | O_CLOEXEC));
+  int saved_errno = errno;
+  free(exeline);
+  if (fd == -1) {
+    ALOGW("Failed to open /proc/%d/exe %s", tid, strerror(saved_errno));
+    return false;
+  }
+
+  char ehdr[EI_NIDENT];
+  ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, &ehdr, sizeof(ehdr)));
+  TEMP_FAILURE_RETRY(close(fd));
+  if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
+    return false;
+  }
+  if (ehdr[EI_CLASS] == ELFCLASS32) {
+    return true;
+  }
+  return false;
+}
+
+static void redirect_to_32(int fd, debugger_request_t* request) {
+  debugger_msg_t msg;
+  memset(&msg, 0, sizeof(msg));
+  msg.tid = request->tid;
+  msg.action = request->action;
+
+  int sock_fd = socket_local_client(DEBUGGER32_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
+                                    SOCK_STREAM | SOCK_CLOEXEC);
+  if (sock_fd < 0) {
+    ALOGE("Failed to connect to debuggerd32: %s", strerror(errno));
+    return;
+  }
+
+  if (TEMP_FAILURE_RETRY(write(sock_fd, &msg, sizeof(msg))) != (ssize_t) sizeof(msg)) {
+    ALOGE("Failed to write request to debuggerd32 socket: %s", strerror(errno));
+    TEMP_FAILURE_RETRY(close(sock_fd));
+    return;
+  }
+
+  char ack;
+  if (TEMP_FAILURE_RETRY(read(sock_fd, &ack, 1)) == -1) {
+    ALOGE("Failed to read ack from debuggerd32 socket: %s", strerror(errno));
+    TEMP_FAILURE_RETRY(close(sock_fd));
+    return;
+  }
+
+  char buffer[1024];
+  ssize_t bytes_read;
+  while ((bytes_read = TEMP_FAILURE_RETRY(read(sock_fd, buffer, sizeof(buffer)))) > 0) {
+    ssize_t bytes_to_send = bytes_read;
+    ssize_t bytes_written;
+    do {
+      bytes_written = TEMP_FAILURE_RETRY(write(fd, buffer + bytes_read - bytes_to_send,
+                                               bytes_to_send));
+      if (bytes_written == -1) {
+        if (errno == EAGAIN) {
+          // Retry the write.
+          continue;
+        }
+        ALOGE("Error while writing data to fd: %s", strerror(errno));
+        break;
+      }
+      bytes_to_send -= bytes_written;
+    } while (bytes_written != 0 && bytes_to_send > 0);
+    if (bytes_to_send != 0) {
+        ALOGE("Failed to write all data to fd: read %zd, sent %zd", bytes_read, bytes_to_send);
+        break;
+    }
+  }
+  TEMP_FAILURE_RETRY(close(sock_fd));
+}
+#endif
+
 static void handle_request(int fd) {
   ALOGV("handle_request(%d)\n", fd);
 
@@ -213,6 +352,24 @@
     ALOGV("BOOM: pid=%d uid=%d gid=%d tid=%d\n",
          request.pid, request.uid, request.gid, request.tid);
 
+#if defined(__LP64__)
+    // On 64 bit systems, requests to dump 32 bit and 64 bit tids come
+    // to the 64 bit debuggerd. If the process is a 32 bit executable,
+    // redirect the request to the 32 bit debuggerd.
+    if (is32bit(request.tid)) {
+      // Only dump backtrace and dump tombstone requests can be redirected.
+      if (request.action == DEBUGGER_ACTION_DUMP_BACKTRACE
+          || request.action == DEBUGGER_ACTION_DUMP_TOMBSTONE) {
+        redirect_to_32(fd, &request);
+      } else {
+        ALOGE("debuggerd: Not allowed to redirect action %d to 32 bit debuggerd\n",
+              request.action);
+      }
+      TEMP_FAILURE_RETRY(close(fd));
+      return;
+    }
+#endif
+
     // At this point, the thread that made the request is blocked in
     // a read() call.  If the thread has crashed, then this gives us
     // time to PTRACE_ATTACH to it before it has a chance to really fault.
@@ -376,7 +533,7 @@
   act.sa_flags = SA_NOCLDWAIT;
   sigaction(SIGCHLD, &act, 0);
 
-  int s = socket_local_server(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
+  int s = socket_local_server(SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
   if (s < 0)
     return 1;
   fcntl(s, F_SETFD, FD_CLOEXEC);
@@ -430,7 +587,11 @@
 }
 
 int main(int argc, char** argv) {
+  union selinux_callback cb;
   if (argc == 1) {
+    selinux_enabled = is_selinux_enabled();
+    cb.func_log = selinux_log_callback;
+    selinux_set_callback(SELINUX_CB_LOG, cb);
     return do_server();
   }
 
diff --git a/debuggerd/mips/machine.cpp b/debuggerd/mips/machine.cpp
index 97834c7..1145963 100644
--- a/debuggerd/mips/machine.cpp
+++ b/debuggerd/mips/machine.cpp
@@ -29,22 +29,10 @@
 
 #define R(x) (static_cast<unsigned int>(x))
 
-// The MIPS uapi ptrace.h has the wrong definition for pt_regs. PTRACE_GETREGS
-// writes 64-bit quantities even though the public struct uses 32-bit ones.
-struct pt_regs_mips_t {
-  uint64_t regs[32];
-  uint64_t lo;
-  uint64_t hi;
-  uint64_t cp0_epc;
-  uint64_t cp0_badvaddr;
-  uint64_t cp0_status;
-  uint64_t cp0_cause;
-};
-
 // If configured to do so, dump memory around *all* registers
 // for the crashing thread.
 void dump_memory_and_code(log_t* log, pid_t tid) {
-  pt_regs_mips_t r;
+  pt_regs r;
   if (ptrace(PTRACE_GETREGS, tid, 0, &r)) {
     return;
   }
@@ -85,7 +73,7 @@
 }
 
 void dump_registers(log_t* log, pid_t tid) {
-  pt_regs_mips_t r;
+  pt_regs r;
   if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
     _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
     return;
diff --git a/debuggerd/tombstone.cpp b/debuggerd/tombstone.cpp
index 0c1b80f..11a15d6 100644
--- a/debuggerd/tombstone.cpp
+++ b/debuggerd/tombstone.cpp
@@ -16,6 +16,7 @@
 
 #define LOG_TAG "DEBUG"
 
+#include <arpa/inet.h>
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -239,12 +240,13 @@
       break;
     }
 
-    const backtrace_map_t* map = backtrace->FindMap(stack_content);
+    backtrace_map_t map;
+    backtrace->FillInMap(stack_content, &map);
     const char* map_name;
-    if (!map) {
+    if (BacktraceMap::IsValid(map)) {
       map_name = "";
     } else {
-      map_name = map->name.c_str();
+      map_name = map.name.c_str();
     }
     uintptr_t offset = 0;
     std::string func_name(backtrace->GetFunctionName(stack_content, &offset));
diff --git a/debuggerd/utility.cpp b/debuggerd/utility.cpp
index 2baf9de..e10feff 100644
--- a/debuggerd/utility.cpp
+++ b/debuggerd/utility.cpp
@@ -131,12 +131,6 @@
   return -1;
 }
 
-#if defined (__mips__)
-#define DUMP_MEMORY_AS_ASCII 1
-#else
-#define DUMP_MEMORY_AS_ASCII 0
-#endif
-
 void dump_memory(log_t* log, pid_t tid, uintptr_t addr) {
     char code_buffer[64];
     char ascii_buffer[32];
@@ -183,7 +177,6 @@
                                static_cast<uintptr_t>(data));
             }
 
-#if DUMP_MEMORY_AS_ASCII
             for (size_t j = 0; j < sizeof(long); j++) {
                 /*
                  * Our isprint() allows high-ASCII characters that display
@@ -197,7 +190,6 @@
                     *asc_out++ = '.';
                 }
             }
-#endif
             p += sizeof(long);
         }
         *asc_out = '\0';
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index e11691f..aa5b14a 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -32,6 +32,7 @@
   LOCAL_SRC_FILES += usb_osx.c util_osx.c
   LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit \
 	-framework Carbon
+  LOCAL_CFLAGS += -Wno-unused-parameter
 endif
 
 ifeq ($(HOST_OS),windows)
@@ -52,7 +53,6 @@
 LOCAL_STATIC_LIBRARIES := \
     $(EXTRA_STATIC_LIBS) \
     libzipfile \
-    libunz \
     libext4_utils_host \
     libsparse_host \
     libz
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index 43d05aa..959d3ad 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -244,7 +244,7 @@
         // output compatible with "adb devices"
         if (!long_listing) {
             printf("%s\tfastboot\n", serial);
-        } else if (!info->device_path) {
+        } else if (strcmp("", info->device_path) == 0) {
             printf("%-22s fastboot\n", serial);
         } else {
             printf("%-22s fastboot %s\n", serial, info->device_path);
@@ -988,6 +988,7 @@
     unsigned sz;
     int status;
     int c;
+    int longindex;
 
     const struct option longopts[] = {
         {"base", required_argument, 0, 'b'},
@@ -996,13 +997,14 @@
         {"ramdisk_offset", required_argument, 0, 'r'},
         {"tags_offset", required_argument, 0, 't'},
         {"help", 0, 0, 'h'},
+        {"unbuffered", 0, 0, 0},
         {0, 0, 0, 0}
     };
 
     serial = getenv("ANDROID_SERIAL");
 
     while (1) {
-        c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, NULL);
+        c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex);
         if (c < 0) {
             break;
         }
@@ -1063,6 +1065,12 @@
             break;
         case '?':
             return 1;
+        case 0:
+            if (strcmp("unbuffered", longopts[longindex].name) == 0) {
+                setvbuf(stdout, NULL, _IONBF, 0);
+                setvbuf(stderr, NULL, _IONBF, 0);
+            }
+            break;
         default:
             abort();
         }
diff --git a/fastboot/fastboot_protocol.txt b/fastboot/fastboot_protocol.txt
index 2248992..37b1959 100644
--- a/fastboot/fastboot_protocol.txt
+++ b/fastboot/fastboot_protocol.txt
@@ -12,8 +12,8 @@
 ------------------
 
 * Two bulk endpoints (in, out) are required
-* Max packet size must be 64 bytes for full-speed and 512 bytes for 
-  high-speed USB
+* Max packet size must be 64 bytes for full-speed, 512 bytes for
+  high-speed and 1024 bytes for Super Speed USB.
 * The protocol is entirely host-driven and synchronous (unlike the
   multi-channel, bi-directional, asynchronous ADB protocol)
 
diff --git a/fastboot/protocol.c b/fastboot/protocol.c
index 84e9837..10a84c1 100644
--- a/fastboot/protocol.c
+++ b/fastboot/protocol.c
@@ -216,7 +216,7 @@
     }
 }
 
-#define USB_BUF_SIZE 512
+#define USB_BUF_SIZE 1024
 static char usb_buf[USB_BUF_SIZE];
 static int usb_buf_len;
 
diff --git a/fastboot/usb_linux.c b/fastboot/usb_linux.c
index fabbd51..022f364 100644
--- a/fastboot/usb_linux.c
+++ b/fastboot/usb_linux.c
@@ -223,6 +223,13 @@
             } else {
                 out = ept->bEndpointAddress;
             }
+
+            // For USB 3.0 devices skip the SS Endpoint Companion descriptor
+            if (check((struct usb_descriptor_hdr *)ptr, len,
+                      USB_DT_SS_ENDPOINT_COMP, USB_DT_SS_EP_COMP_SIZE) == 0) {
+                len -= USB_DT_SS_EP_COMP_SIZE;
+                ptr += USB_DT_SS_EP_COMP_SIZE;
+            }
         }
 
         info.has_bulk_in = (in != -1);
diff --git a/fastboot/usb_osx.c b/fastboot/usb_osx.c
index 0f55e0d..0b6c515 100644
--- a/fastboot/usb_osx.c
+++ b/fastboot/usb_osx.c
@@ -328,7 +328,8 @@
         ERR("GetLocationId");
         goto error;
     }
-    snprintf(handle->info.device_path, sizeof(handle->info.device_path), "usb:%lX", locationId);
+    snprintf(handle->info.device_path, sizeof(handle->info.device_path),
+             "usb:%" PRIu32 "X", (unsigned int)locationId);
 
     kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
 
diff --git a/fastbootd/Android.mk b/fastbootd/Android.mk
deleted file mode 100644
index 6aa7400..0000000
--- a/fastbootd/Android.mk
+++ /dev/null
@@ -1,92 +0,0 @@
-# Copyright (C) 2013 Google Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_C_INCLUDES := \
-    external/openssl/include \
-    external/mdnsresponder/mDNSShared \
-    $(LOCAL_PATH)/include \
-    external/zlib/ \
-
-LOCAL_SRC_FILES := \
-    config.c \
-    commands.c \
-    commands/boot.c \
-    commands/flash.c \
-    commands/partitions.c \
-    commands/virtual_partitions.c \
-    fastbootd.c \
-    protocol.c \
-    network_discovery.c \
-    socket_client.c \
-    secure.c \
-    transport.c \
-    transport_socket.c \
-    trigger.c \
-    usb_linux_client.c \
-    utils.c \
-
-LOCAL_MODULE := fastbootd
-LOCAL_MODULE_TAGS := optional
-LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter -DFLASH_CERT
-LOCAL_LDFLAGS := -ldl
-
-LOCAL_STATIC_LIBRARIES := \
-    libc \
-    libcrypto_static \
-    libcutils \
-    libmdnssd \
-    libsparse_static \
-    libz
-
-LOCAL_HAL_STATIC_LIBRARIES := libvendortrigger
-
-LOCAL_FORCE_STATIC_EXECUTABLE := true
-
-include $(BUILD_EXECUTABLE)
-
-include $(CLEAR_VARS)
-LOCAL_C_INCLUDES := \
-    external/zlib/
-
-LOCAL_SRC_FILES := \
-    commands/partitions.c \
-    other/gptedit.c \
-    utils.c
-
-LOCAL_MODULE := gptedit
-LOCAL_MODULE_TAGS := optional
-LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter
-
-LOCAL_STATIC_LIBRARIES := \
-    libsparse_static \
-    libc \
-    libcutils \
-    libz
-
-LOCAL_FORCE_STATIC_EXECUTABLE := true
-
-include $(BUILD_EXECUTABLE)
-
-# vendor trigger HAL
-include $(CLEAR_VARS)
-LOCAL_CFLAGS := -Wall -Werror
-LOCAL_MODULE := libvendortrigger.default
-LOCAL_MODULE_TAGS := optional
-LOCAL_SRC_FILES := vendor_trigger_default.c
-LOCAL_STATIC_LIBRARIES := libcutils
-include $(BUILD_STATIC_LIBRARY)
diff --git a/fastbootd/bootimg.h b/fastbootd/bootimg.h
deleted file mode 100644
index 44fde92..0000000
--- a/fastbootd/bootimg.h
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (C) 2008 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.
- */
-
-#ifndef _BOOT_IMAGE_H_
-#define _BOOT_IMAGE_H_
-
-typedef struct boot_img_hdr boot_img_hdr;
-
-#define BOOT_MAGIC "ANDROID!"
-#define BOOT_MAGIC_SIZE 8
-#define BOOT_NAME_SIZE 16
-#define BOOT_ARGS_SIZE 512
-
-struct boot_img_hdr
-{
-    unsigned char magic[BOOT_MAGIC_SIZE];
-
-    unsigned kernel_size;  /* size in bytes */
-    unsigned kernel_addr;  /* physical load addr */
-
-    unsigned ramdisk_size; /* size in bytes */
-    unsigned ramdisk_addr; /* physical load addr */
-
-    unsigned second_size;  /* size in bytes */
-    unsigned second_addr;  /* physical load addr */
-
-    unsigned tags_addr;    /* physical addr for kernel tags */
-    unsigned page_size;    /* flash page size we assume */
-    unsigned unused[2];    /* future expansion: should be 0 */
-
-    unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
-    
-    unsigned char cmdline[BOOT_ARGS_SIZE];
-
-    unsigned id[8]; /* timestamp / checksum / sha1 / etc */
-};
-
-/*
-** +-----------------+ 
-** | boot header     | 1 page
-** +-----------------+
-** | kernel          | n pages  
-** +-----------------+
-** | ramdisk         | m pages  
-** +-----------------+
-** | second stage    | o pages
-** +-----------------+
-**
-** n = (kernel_size + page_size - 1) / page_size
-** m = (ramdisk_size + page_size - 1) / page_size
-** o = (second_size + page_size - 1) / page_size
-**
-** 0. all entities are page_size aligned in flash
-** 1. kernel and ramdisk are required (size != 0)
-** 2. second is optional (second_size == 0 -> no second)
-** 3. load each element (kernel, ramdisk, second) at
-**    the specified physical address (kernel_addr, etc)
-** 4. prepare tags at tag_addr.  kernel_args[] is
-**    appended to the kernel commandline in the tags.
-** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
-** 6. if second_size != 0: jump to second_addr
-**    else: jump to kernel_addr
-*/
-
-boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
-                        void *ramdisk, unsigned ramdisk_size,
-                        void *second, unsigned second_size,
-                        unsigned page_size,
-                        unsigned *bootimg_size);
-
-void bootimg_set_cmdline(boot_img_hdr *hdr, const char *cmdline);                
-#endif
diff --git a/fastbootd/commands.c b/fastbootd/commands.c
deleted file mode 100644
index 98b7866..0000000
--- a/fastbootd/commands.c
+++ /dev/null
@@ -1,409 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <inttypes.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <sys/reboot.h>
-#include <fcntl.h>
-
-#include "bootimg.h"
-#include "commands/boot.h"
-#include "commands/flash.h"
-#include "commands/partitions.h"
-#include "commands/virtual_partitions.h"
-#include "debug.h"
-#include "protocol.h"
-#include "trigger.h"
-#include "utils.h"
-
-#define ATAGS_LOCATION "/proc/atags"
-
-static void cmd_boot(struct protocol_handle *phandle, const char *arg)
-{
-    int sz, atags_sz, new_atags_sz;
-    int rv;
-    unsigned kernel_actual;
-    unsigned ramdisk_actual;
-    unsigned second_actual;
-    void *kernel_ptr;
-    void *ramdisk_ptr;
-    void *second_ptr;
-    struct boot_img_hdr *hdr;
-    char *ptr = NULL;
-    char *atags_ptr = NULL;
-    char *new_atags = NULL;
-    int data_fd = 0;
-
-    D(DEBUG, "cmd_boot %s\n", arg);
-
-    if (phandle->download_fd < 0) {
-        fastboot_fail(phandle, "no kernel file");
-        return;
-    }
-
-    atags_ptr = read_atags(ATAGS_LOCATION, &atags_sz);
-    if (atags_ptr == NULL) {
-        fastboot_fail(phandle, "atags read error");
-        goto error;
-    }
-
-    // TODO: With cms we can also verify partition name included as
-    // cms signed attribute
-    if (flash_validate_certificate(phandle->download_fd, &data_fd) != 1) {
-        fastboot_fail(phandle, "Access forbiden you need the certificate");
-        return;
-    }
-
-    sz = get_file_size(data_fd);
-
-    ptr = (char *) mmap(NULL, sz, PROT_READ,
-                        MAP_POPULATE | MAP_PRIVATE, data_fd, 0);
-
-    hdr = (struct boot_img_hdr *) ptr;
-
-    if (ptr == MAP_FAILED) {
-        fastboot_fail(phandle, "internal fastbootd error");
-        goto error;
-    }
-
-    if ((size_t) sz < sizeof(*hdr)) {
-        fastboot_fail(phandle, "invalid bootimage header");
-        goto error;
-    }
-
-    kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, hdr->page_size);
-    ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, hdr->page_size);
-    second_actual = ROUND_TO_PAGE(hdr->second_size, hdr->page_size);
-
-    new_atags = (char *) create_atags((unsigned *) atags_ptr, atags_sz, hdr, &new_atags_sz);
-
-    if (new_atags == NULL) {
-        fastboot_fail(phandle, "atags generate error");
-        goto error;
-    }
-    if (new_atags_sz > 0x4000) {
-        fastboot_fail(phandle, "atags file to large");
-        goto error;
-    }
-
-    if ((int) (hdr->page_size + kernel_actual + ramdisk_actual) < sz) {
-        fastboot_fail(phandle, "incomplete bootimage");
-        goto error;
-    }
-
-    kernel_ptr = (void *)((uintptr_t) ptr + hdr->page_size);
-    ramdisk_ptr = (void *)((uintptr_t) kernel_ptr + kernel_actual);
-    second_ptr = (void *)((uintptr_t) ramdisk_ptr + ramdisk_actual);
-
-    D(INFO, "preparing to boot");
-    // Prepares boot physical address. Addresses from header are ignored
-    rv = prepare_boot_linux(hdr->kernel_addr, kernel_ptr, kernel_actual,
-                            hdr->ramdisk_addr, ramdisk_ptr, ramdisk_actual,
-                            hdr->second_addr, second_ptr, second_actual,
-                            hdr->tags_addr, new_atags, ROUND_TO_PAGE(new_atags_sz, hdr->page_size));
-    if (rv < 0) {
-        fastboot_fail(phandle, "kexec prepare failed");
-        goto error;
-    }
-
-    fastboot_okay(phandle, "");
-
-    free(atags_ptr);
-    munmap(ptr, sz);
-    free(new_atags);
-    close(data_fd);
-
-    D(INFO, "Kexec going to reboot");
-    reboot(LINUX_REBOOT_CMD_KEXEC);
-
-    fastboot_fail(phandle, "reboot error");
-
-    return;
-
-error:
-
-    if (atags_ptr != NULL)
-        free(atags_ptr);
-    if (ptr != NULL)
-        munmap(ptr, sz);
-
-}
-
-static void cmd_erase(struct protocol_handle *phandle, const char *arg)
-{
-    int partition_fd;
-    char path[PATH_MAX];
-    D(DEBUG, "cmd_erase %s\n", arg);
-
-    if (flash_find_entry(arg, path, PATH_MAX)) {
-        fastboot_fail(phandle, "partition table doesn't exist");
-        return;
-    }
-
-    if (path == NULL) {
-        fastboot_fail(phandle, "Couldn't find partition");
-        return;
-    }
-
-    partition_fd = flash_get_partiton(path);
-    if (partition_fd < 0) {
-        fastboot_fail(phandle, "partiton file does not exists");
-    }
-
-    if (flash_erase(partition_fd)) {
-        fastboot_fail(phandle, "failed to erase partition");
-        flash_close(partition_fd);
-        return;
-    }
-
-    if (flash_close(partition_fd) < 0) {
-        D(ERR, "could not close device %s", strerror(errno));
-        fastboot_fail(phandle, "failed to erase partition");
-        return;
-    }
-    fastboot_okay(phandle, "");
-}
-
-static int GPT_header_location() {
-    const char *location_str = fastboot_getvar("gpt_sector");
-    char *str;
-    int location;
-
-    if (!strcmp("", location_str)) {
-        D(INFO, "GPT location not specified using second sector");
-        return 1;
-    }
-    else {
-        location = strtoul(location_str, &str, 10);
-        D(INFO, "GPT location specified as %d", location);
-
-        if (*str != '\0')
-            return -1;
-
-        return location - 1;
-    }
-}
-
-static void cmd_gpt_layout(struct protocol_handle *phandle, const char *arg) {
-    struct GPT_entry_table *oldtable;
-    int location;
-    struct GPT_content content;
-    const char *device;
-    device = fastboot_getvar("blockdev");
-
-    if (!strcmp(device, "")) {
-        fastboot_fail(phandle, "blockdev not defined in config file");
-        return;
-    }
-
-    //TODO: add same verification as in cmd_flash
-    if (phandle->download_fd < 0) {
-        fastboot_fail(phandle, "no layout file");
-        return;
-    }
-
-    location = GPT_header_location();
-    oldtable = GPT_get_device(device, location);
-
-    GPT_default_content(&content, oldtable);
-    if (oldtable == NULL)
-        D(WARN, "Could not get old gpt table");
-    else
-        GPT_release_device(oldtable);
-
-    if (!GPT_parse_file(phandle->download_fd, &content)) {
-        fastboot_fail(phandle, "Could not parse partition config file");
-        return;
-    }
-
-    if (trigger_gpt_layout(&content)) {
-        fastboot_fail(phandle, "Vendor forbids this opperation");
-        GPT_release_content(&content);
-        return;
-    }
-
-    if (!GPT_write_content(device, &content)) {
-        fastboot_fail(phandle, "Unable to write gpt file");
-        GPT_release_content(&content);
-        return;
-    }
-
-    GPT_release_content(&content);
-    fastboot_okay(phandle, "");
-}
-
-static void cmd_flash(struct protocol_handle *phandle, const char *arg)
-{
-    int partition;
-    uint64_t sz;
-    char data[BOOT_MAGIC_SIZE];
-    char path[PATH_MAX];
-    ssize_t header_sz = 0;
-    int data_fd = 0;
-
-    D(DEBUG, "cmd_flash %s\n", arg);
-
-    if (try_handle_virtual_partition(phandle, arg)) {
-        return;
-    }
-
-    if (phandle->download_fd < 0) {
-        fastboot_fail(phandle, "no kernel file");
-        return;
-    }
-
-    if (flash_find_entry(arg, path, PATH_MAX)) {
-        fastboot_fail(phandle, "partition table doesn't exist");
-        return;
-    }
-
-    if (flash_validate_certificate(phandle->download_fd, &data_fd) != 1) {
-        fastboot_fail(phandle, "Access forbiden you need certificate");
-        return;
-    }
-
-    // TODO: Maybe its goot idea to check whether the partition is bootable
-    if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
-        if (read_data_once(data_fd, data, BOOT_MAGIC_SIZE) < BOOT_MAGIC_SIZE) {
-            fastboot_fail(phandle, "incoming data read error, cannot read boot header");
-            return;
-        }
-        if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
-            fastboot_fail(phandle, "image is not a boot image");
-            return;
-        }
-    }
-
-    partition = flash_get_partiton(path);
-
-    sz = get_file_size64(data_fd);
-
-    sz -= header_sz;
-
-    if (sz > get_file_size64(partition)) {
-        flash_close(partition);
-        D(WARN, "size of file too large");
-        fastboot_fail(phandle, "size of file too large");
-        return;
-    }
-
-    D(INFO, "writing %"PRId64" bytes to '%s'\n", sz, arg);
-
-    if (flash_write(partition, phandle->download_fd, sz, header_sz)) {
-        fastboot_fail(phandle, "flash write failure");
-        return;
-    }
-    D(INFO, "partition '%s' updated\n", arg);
-
-    flash_close(partition);
-    close(data_fd);
-
-    fastboot_okay(phandle, "");
-}
-
-static void cmd_continue(struct protocol_handle *phandle, const char *arg)
-{
-    fastboot_okay(phandle, "");
-#if 0
-    udc_stop();
-
-    boot_linux_from_flash();
-#endif
-}
-
-static void cmd_getvar(struct protocol_handle *phandle, const char *arg)
-{
-    const char *value;
-    D(DEBUG, "cmd_getvar %s\n", arg);
-
-    value = fastboot_getvar(arg);
-
-    fastboot_okay(phandle, value);
-}
-
-static void cmd_download(struct protocol_handle *phandle, const char *arg)
-{
-    unsigned len = strtoul(arg, NULL, 16);
-    int old_fd;
-
-    if (len > 256 * 1024 * 1024) {
-        fastboot_fail(phandle, "data too large");
-        return;
-    }
-
-    fastboot_data(phandle, len);
-
-    old_fd = protocol_get_download(phandle);
-    if (old_fd >= 0) {
-        off_t len = lseek(old_fd, 0, SEEK_END);
-        D(INFO, "disposing of unused fd %d, size %ld", old_fd, len);
-        close(old_fd);
-    }
-
-    phandle->download_fd = protocol_handle_download(phandle, len);
-    if (phandle->download_fd < 0) {
-        fastboot_fail(phandle, "download failed");
-        return;
-    }
-
-    fastboot_okay(phandle, "");
-}
-
-static void cmd_oem(struct protocol_handle *phandle, const char *arg) {
-    const char *response = "";
-
-    //TODO: Maybe it should get download descriptor also
-    if (trigger_oem_cmd(arg, &response))
-        fastboot_fail(phandle, response);
-    else
-        fastboot_okay(phandle, response);
-}
-
-void commands_init()
-{
-    virtual_partition_register("partition-table", cmd_gpt_layout);
-
-    fastboot_register("boot", cmd_boot);
-    fastboot_register("erase:", cmd_erase);
-    fastboot_register("flash:", cmd_flash);
-    fastboot_register("continue", cmd_continue);
-    fastboot_register("getvar:", cmd_getvar);
-    fastboot_register("download:", cmd_download);
-    fastboot_register("oem", cmd_oem);
-    //fastboot_publish("version", "0.5");
-    //fastboot_publish("product", "swordfish");
-    //fastboot_publish("kernel", "lk");
-}
diff --git a/fastbootd/commands/boot.c b/fastbootd/commands/boot.c
deleted file mode 100644
index 922914b..0000000
--- a/fastbootd/commands/boot.c
+++ /dev/null
@@ -1,254 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <sys/syscall.h>
-#include <stdio.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "boot.h"
-#include "debug.h"
-#include "utils.h"
-#include "bootimg.h"
-
-
-#define KEXEC_ARM_ATAGS_OFFSET  0x1000
-#define KEXEC_ARM_ZIMAGE_OFFSET 0x8000
-
-#define MEMORY_SIZE 0x0800000
-#define START_ADDRESS 0x44000000
-#define KERNEL_START (START_ADDRESS + KEXEC_ARM_ZIMAGE_OFFSET)
-
-#define ATAG_NONE_TYPE      0x00000000
-#define ATAG_CORE_TYPE      0x54410001
-#define ATAG_RAMDISK_TYPE   0x54410004
-#define ATAG_INITRD2_TYPE   0x54420005
-#define ATAG_CMDLINE_TYPE   0x54410009
-
-#define MAX_ATAG_SIZE 0x4000
-
-struct atag_info {
-    unsigned size;
-    unsigned type;
-};
-
-struct atag_initrd2 {
-    unsigned start;
-    unsigned size;
-};
-
-struct atag_cmdline {
-    char cmdline[0];
-};
-
-struct atag {
-    struct atag_info info;
-    union {
-        struct atag_initrd2 initrd2;
-        struct atag_cmdline cmdline;
-    } data;
-};
-
-
-long kexec_load(unsigned int entry, unsigned long nr_segments,
-                struct kexec_segment *segment, unsigned long flags) {
-   return syscall(__NR_kexec_load, entry, nr_segments, segment, flags);
-}
-
-/*
- * Prepares arguments for kexec
- * Kernel address is not set into kernel_phys
- * Ramdisk is set to position relative to kernel
- */
-int prepare_boot_linux(uintptr_t kernel_phys, void *kernel_addr, int kernel_size,
-                       uintptr_t ramdisk_phys, void *ramdisk_addr, int ramdisk_size,
-                       uintptr_t second_phys, void *second_addr, int second_size,
-                       uintptr_t atags_phys, void *atags_addr, int atags_size) {
-    struct kexec_segment segment[4];
-    int segment_count = 2;
-    unsigned entry = START_ADDRESS + KEXEC_ARM_ZIMAGE_OFFSET;
-    int rv;
-    int page_size = getpagesize();
-
-    segment[0].buf = kernel_addr;
-    segment[0].bufsz = kernel_size;
-    segment[0].mem = (void *) KERNEL_START;
-    segment[0].memsz = ROUND_TO_PAGE(kernel_size, page_size);
-
-    if (kernel_size > MEMORY_SIZE - KEXEC_ARM_ZIMAGE_OFFSET) {
-        D(INFO, "Kernel image too big");
-        return -1;
-    }
-
-    segment[1].buf = atags_addr;
-    segment[1].bufsz = atags_size;
-    segment[1].mem = (void *) (START_ADDRESS + KEXEC_ARM_ATAGS_OFFSET);
-    segment[1].memsz = ROUND_TO_PAGE(atags_size, page_size);
-
-    D(INFO, "Ramdisk size is %d", ramdisk_size);
-
-    if (ramdisk_size != 0) {
-        segment[segment_count].buf = ramdisk_addr;
-        segment[segment_count].bufsz = ramdisk_size;
-        segment[segment_count].mem = (void *) (KERNEL_START + ramdisk_phys - kernel_phys);
-        segment[segment_count].memsz = ROUND_TO_PAGE(ramdisk_phys, page_size);
-        ++segment_count;
-    }
-
-    D(INFO, "Ramdisk size is %d", ramdisk_size);
-    if (second_size != 0) {
-        segment[segment_count].buf = second_addr;
-        segment[segment_count].bufsz = second_size;
-        segment[segment_count].mem = (void *) (KERNEL_START + second_phys - kernel_phys);
-        segment[segment_count].memsz = ROUND_TO_PAGE(second_size, page_size);
-        entry = second_phys;
-        ++segment_count;
-    }
-
-    rv = kexec_load(entry, segment_count, segment, KEXEC_ARCH_DEFAULT);
-
-    if (rv != 0) {
-        D(INFO, "Kexec_load returned non-zero exit code: %s\n", strerror(errno));
-        return -1;
-    }
-
-    return 1;
-
-}
-
-unsigned *create_atags(unsigned *atags_position, int atag_size, const struct boot_img_hdr *hdr, int *size) {
-    struct atag *current_tag = (struct atag *) atags_position;
-    unsigned *current_tag_raw = atags_position;
-    unsigned *new_atags = malloc(ROUND_TO_PAGE(atag_size + BOOT_ARGS_SIZE * sizeof(char),
-                                               hdr->page_size));
-    //This pointer will point into the beggining of buffer free space
-    unsigned *natags_raw_buff = new_atags;
-    int new_atags_size = 0;
-    int current_size;
-    int cmdl_length;
-
-    // copy tags from current atag file
-    while (current_tag->info.type != ATAG_NONE_TYPE) {
-        switch (current_tag->info.type) {
-            case ATAG_CMDLINE_TYPE:
-            case ATAG_RAMDISK_TYPE:
-            case ATAG_INITRD2_TYPE: break;
-            default:
-                memcpy((void *)natags_raw_buff, (void *)current_tag_raw, current_tag->info.size * sizeof(unsigned));
-                natags_raw_buff += current_tag->info.size;
-                new_atags_size += current_tag->info.size;
-        }
-
-        current_tag_raw += current_tag->info.size;
-        current_tag = (struct atag *) current_tag_raw;
-
-        if (current_tag_raw >= atags_position + atag_size) {
-            D(ERR, "Critical error in atags");
-            return NULL;
-        }
-    }
-
-    // set INITRD2 tag
-    if (hdr->ramdisk_size > 0) {
-        current_size = (sizeof(struct atag_info) + sizeof(struct atag_initrd2)) / sizeof(unsigned);
-        *((struct atag *) natags_raw_buff) = (struct atag) {
-            .info = {
-                .size = current_size,
-                .type = ATAG_INITRD2_TYPE
-            },
-            .data = {
-                .initrd2 = (struct atag_initrd2) {
-                    .start = hdr->ramdisk_addr,
-                    .size = hdr->ramdisk_size
-                }
-            }
-        };
-
-        new_atags_size += current_size;
-        natags_raw_buff += current_size;
-    }
-
-    // set ATAG_CMDLINE
-    cmdl_length = strnlen((char *) hdr->cmdline, BOOT_ARGS_SIZE - 1);
-    current_size = sizeof(struct atag_info) + (1 + cmdl_length);
-    current_size = (current_size + sizeof(unsigned) - 1) / sizeof(unsigned);
-    *((struct atag *) natags_raw_buff) = (struct atag) {
-        .info = {
-            .size = current_size,
-            .type = ATAG_CMDLINE_TYPE
-        },
-    };
-
-    //copy cmdline and ensure that there is null character
-    memcpy(((struct atag *) natags_raw_buff)->data.cmdline.cmdline,
-           (char *) hdr->cmdline, cmdl_length);
-    ((struct atag *) natags_raw_buff)->data.cmdline.cmdline[cmdl_length] = '\0';
-
-    new_atags_size += current_size;
-    natags_raw_buff += current_size;
-
-    // set ATAG_NONE
-    *((struct atag *) natags_raw_buff) = (struct atag) {
-        .info = {
-            .size = 0,
-            .type = ATAG_NONE_TYPE
-        },
-    };
-    new_atags_size += sizeof(struct atag_info) / sizeof(unsigned);
-    natags_raw_buff += sizeof(struct atag_info) / sizeof(unsigned);
-
-    *size = new_atags_size * sizeof(unsigned);
-    return new_atags;
-}
-
-char *read_atags(const char * path, int *atags_sz) {
-    int afd = -1;
-    char *atags_ptr = NULL;
-
-    afd = open(path, O_RDONLY);
-    if (afd < 0) {
-        D(ERR, "wrong atags file");
-        return 0;
-    }
-
-    atags_ptr = (char *) malloc(MAX_ATAG_SIZE);
-    if (atags_ptr == NULL) {
-        D(ERR, "insufficient memory");
-        return 0;
-    }
-
-    *atags_sz = read(afd, atags_ptr, MAX_ATAG_SIZE);
-
-    close(afd);
-    return atags_ptr;
-}
-
diff --git a/fastbootd/commands/boot.h b/fastbootd/commands/boot.h
deleted file mode 100644
index a5efd01..0000000
--- a/fastbootd/commands/boot.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef __FASTBOOT_BOOT_H
-#define __FASTBOOT_BOOT_H
-
-#include <sys/cdefs.h>
-#include <linux/kexec.h>
-
-#include "bootimg.h"
-
-#define KEXEC_TYPE_DEFAULT 0
-#define KEXEC_TYPE_CRASH   1
-
-int prepare_boot_linux(uintptr_t, void *, int, uintptr_t, void *, int,
-                       uintptr_t, void *, int, uintptr_t, void *, int);
-unsigned *create_atags(unsigned *, int, const struct boot_img_hdr *, int *);
-long kexec_load(unsigned int, unsigned long, struct kexec_segment *, unsigned long);
-char *read_atags(const char *, int *);
-
-#endif /* _SYS_KEXEC_H */
-
diff --git a/fastbootd/commands/flash.c b/fastbootd/commands/flash.c
deleted file mode 100644
index 1eb4d1b..0000000
--- a/fastbootd/commands/flash.c
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <inttypes.h>
-#include <sys/mman.h>
-
-#include "flash.h"
-#include "protocol.h"
-#include "debug.h"
-#include "utils.h"
-#include "commands/partitions.h"
-
-#ifdef FLASH_CERT
-#include "secure.h"
-#endif
-
-#define ALLOWED_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-."
-#define BUFFER_SIZE 1024 * 1024
-#define MIN(a, b) (a > b ? b : a)
-
-
-int flash_find_entry(const char *name, char *out, size_t outlen)
-{
-//TODO: Assumption: All the partitions has they unique name
-
-    const char *path = fastboot_getvar("device-directory");
-    size_t length;
-    if (strcmp(path, "") == 0) {
-        D(ERR, "device-directory: not defined in config file");
-        return -1;
-    }
-
-    length = strspn(name, ALLOWED_CHARS);
-    if (length != strlen(name)) {
-        D(ERR, "Not allowed char in name: %c", name[length]);
-        return -1;
-    }
-
-    if (snprintf(out, outlen, "%s%s", path, name) >= (int) outlen) {
-        D(ERR, "Too long path to partition file");
-        return -1;
-    }
-
-    if (access(out, F_OK ) == -1) {
-        D(ERR, "could not find partition file %s", name);
-        return -1;
-    }
-
-    return 0;
-}
-
-int flash_erase(int fd)
-{
-    int64_t size;
-    size = get_block_device_size(fd);
-    D(DEBUG, "erase %"PRId64" data from %d\n", size, fd);
-
-    return wipe_block_device(fd, size);
-}
-
-int flash_write(int partition_fd, int data_fd, ssize_t size, ssize_t skip)
-{
-    ssize_t written = 0;
-    struct GPT_mapping input;
-    struct GPT_mapping output;
-
-    while (written < size) {
-        int current_size = MIN(size - written, BUFFER_SIZE);
-
-        if (gpt_mmap(&input, written + skip, current_size, data_fd)) {
-            D(ERR, "Error in writing data, unable to map data file %zd at %zd size %d", size, skip, current_size);
-            return -1;
-        }
-        if (gpt_mmap(&output, written, current_size, partition_fd)) {
-            D(ERR, "Error in writing data, unable to map output partition");
-            return -1;
-        }
-
-        memcpy(output.ptr, input.ptr, current_size);
-
-        gpt_unmap(&input);
-        gpt_unmap(&output);
-
-        written += current_size;
-    }
-
-    return 0;
-}
-
-#ifdef FLASH_CERT
-
-int flash_validate_certificate(int signed_fd, int *data_fd) {
-    int ret = 0;
-    const char *cert_path;
-    X509_STORE *store = NULL;
-    CMS_ContentInfo *content_info;
-    BIO *content;
-
-    cert_path = fastboot_getvar("certificate-path");
-    if (!strcmp(cert_path, "")) {
-        D(ERR, "could not find cert-key value in config file");
-        goto finish;
-    }
-
-    store = cert_store_from_path(cert_path);
-    if (store == NULL) {
-        D(ERR, "unable to create certification store");
-        goto finish;
-    }
-
-    if (cert_read(signed_fd, &content_info, &content)) {
-        D(ERR, "reading data failed");
-        goto finish;
-    }
-
-    ret = cert_verify(content, content_info, store, data_fd);
-    cert_release(content, content_info);
-
-    return ret;
-
-finish:
-    if (store != NULL)
-        cert_release_store(store);
-
-    return ret;
-}
-
-#else
-int flash_validate_certificate(int signed_fd, int *data_fd) {
-    return 1;
-}
-#endif
diff --git a/fastbootd/commands/flash.h b/fastbootd/commands/flash.h
deleted file mode 100644
index 5a64cab..0000000
--- a/fastbootd/commands/flash.h
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef _FASTBOOTD_ERASE_H
-#define _FASTBOOTD_ERASE_H
-
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include "debug.h"
-
-int flash_find_entry(const char *, char *, size_t);
-int flash_erase(int fd);
-
-static inline int flash_get_partiton(const char *path) {
-    return open(path, O_RDWR);
-}
-
-static inline int flash_close(int fd) {
-    return close(fd);
-}
-
-int flash_write(int partition, int data, ssize_t size, ssize_t skip);
-
-static inline ssize_t read_data_once(int fd, char *buffer, ssize_t size) {
-    ssize_t readcount = 0;
-    ssize_t len;
-
-    while ((len = TEMP_FAILURE_RETRY(read(fd, (void *) &buffer[readcount], size - readcount))) > 0) {
-        readcount += len;
-    }
-    if (len < 0) {
-        D(ERR, "Read error:%s", strerror(errno));
-        return len;
-    }
-
-    return readcount;
-}
-
-int flash_validate_certificate(int signed_fd, int *data_fd);
-
-#endif
-
diff --git a/fastbootd/commands/partitions.c b/fastbootd/commands/partitions.c
deleted file mode 100644
index 74232e6..0000000
--- a/fastbootd/commands/partitions.c
+++ /dev/null
@@ -1,772 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <endian.h>
-#include <zlib.h>
-#include <linux/hdreg.h>
-#include <sys/ioctl.h>
-#include <stdlib.h>
-#include <cutils/config_utils.h>
-#include <inttypes.h>
-
-#include "partitions.h"
-#include "debug.h"
-#include "utils.h"
-#include "protocol.h"
-
-#define BLKRRPART  _IO(0x12,95) /* re-read partition table */
-#define BLKSSZGET  _IO(0x12,104)
-
-#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
-#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
-#define ALIGN_DOWN(x, y) ((y) * ((x) / (y)))
-
-
-const uint8_t partition_type_uuid[16] = {
-    0xa2, 0xa0, 0xd0, 0xeb, 0xe5, 0xb9, 0x33, 0x44,
-    0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7,
-};
-
-//TODO: There is assumption that we are using little endian
-
-static void GPT_entry_clear(struct GPT_entry_raw *entry)
-{
-    memset(entry, 0, sizeof(*entry));
-}
-
-/*
- * returns mapped location to choosen area
- * mapped_ptr is pointer to whole area mapped (it can be bigger then requested)
- */
-int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd)
-{
-    unsigned int location_diff = location & ~PAGE_MASK;
-
-    mapping->size = ALIGN(size + location_diff, PAGE_SIZE);
-
-    uint64_t sz = get_file_size64(fd);
-    if (sz < size + location) {
-        D(ERR, "the location of mapping area is outside of the device size %" PRId64, sz);
-        return 1;
-    }
-    location = ALIGN_DOWN(location, PAGE_SIZE);
-
-    mapping->map_ptr = mmap64(NULL, mapping->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, location);
-
-    if (mapping->map_ptr == MAP_FAILED) {
-        mapping->ptr = MAP_FAILED;
-        D(ERR, "map failed: %s", strerror(errno));
-        return 1;
-    }
-
-    mapping->ptr = (void *)((char *) mapping->map_ptr + location_diff);
-    return 0;
-}
-
-void gpt_unmap(struct GPT_mapping *mapping) {
-    munmap(mapping->map_ptr, mapping->size);
-}
-
-
-#define LBA_ADDR(table, value)   ((uint64_t) (table)->sector_size * (value))
-
-int GPT_map_from_content(struct GPT_entry_table *table, const struct GPT_content *content)
-{
-
-    // Mapping header
-    if (gpt_mmap(&table->header_map, LBA_ADDR(table, content->header.current_lba),
-                 table->sector_size, table->fd)) {
-        D(ERR, "unable to map header:%s\n", strerror(errno));
-        goto error_header;
-    }
-
-    table->header = (struct GPT_header *) table->header_map.ptr;
-
-    table->partition_table_size = ROUND_UP(content->header.entries_count * sizeof(*table->entries),
-                                           table->sector_size);
-
-    // Mapping entry table
-    if (gpt_mmap(&table->entries_map, LBA_ADDR(table, content->header.entries_lba),
-                 table->partition_table_size, table->fd)) {
-        D(ERR, "unable to map entries");
-        goto error_signature;
-    }
-
-    table->entries = (struct GPT_entry_raw *) table->entries_map.ptr;
-
-    // Mapping secondary header
-    if (gpt_mmap(&table->sec_header_map, LBA_ADDR(table, content->header.backup_lba),
-                 table->sector_size, table->fd)) {
-        D(ERR, "unable to map backup gpt header");
-        goto error_sec_header;
-    }
-
-    // Mapping secondary entries table
-    if (gpt_mmap(&table->sec_entries_map,
-                 LBA_ADDR(table, content->header.backup_lba) - table->partition_table_size,
-                 table->partition_table_size, table->fd)) {
-        D(ERR, "unable to map secondary gpt table");
-        goto error_sec_entries;
-    }
-
-    table->second_header = (struct GPT_header *) table->sec_header_map.ptr;
-    table->second_entries = (struct GPT_entry_raw *) table->sec_entries_map.ptr;
-    table->second_valid = strcmp("EFI PART", (char *) table->second_header->signature) == 0;
-
-    return 0;
-
-error_sec_entries:
-    gpt_unmap(&table->sec_header_map);
-error_sec_header:
-    gpt_unmap(&table->entries_map);
-error_signature:
-    gpt_unmap(&table->header_map);
-error_header:
-    return 1;
-}
-
-int GPT_map(struct GPT_entry_table *table, unsigned header_lba)
-{
-    struct GPT_content content;
-    struct GPT_mapping mapping;
-    struct GPT_header *header;
-
-    if (gpt_mmap(&mapping, LBA_ADDR(table, header_lba), table->sector_size, table->fd)) {
-        D(ERR, "unable to map header: %s", strerror(errno));
-        goto error_header;
-    }
-
-    header = (struct GPT_header *) mapping.ptr;
-
-    if (strcmp("EFI PART", (char *) header->signature)) {
-        D(ERR, "GPT entry not valid");
-        goto error_signature;
-    }
-
-    content.header = *header;
-
-    gpt_unmap(&mapping);
-
-    return GPT_map_from_content(table, &content);
-
-error_signature:
-    gpt_unmap(&table->header_map);
-error_header:
-    return 1;
-}
-
-struct GPT_entry_table* GPT_get_device(const char *path, unsigned header_lba)
-{
-    struct GPT_entry_table *table;
-    size_t sector_bytes;
-
-    table = (struct GPT_entry_table *) malloc(sizeof(*table));
-    table->fd = open(path, O_RDWR);
-
-    if (table->fd < 0) {
-        D(ERR, "unable to open file %s:%s\n", path, strerror(errno));
-        return NULL;
-    }
-
-    if (!ioctl(table->fd, BLKSSZGET, &sector_bytes)) {
-        table->sector_size = (unsigned) sector_bytes;
-        D(INFO, "Got sector size %d", table->sector_size);
-    } else {
-        D(WARN, "unable to get sector size, assuming 512");
-        table->sector_size = 512;
-    }
-
-    if (GPT_map(table, header_lba)) {
-        D(ERR, "Could not map gpt");
-        return NULL;
-    }
-
-    return table;
-}
-
-static struct GPT_entry_table* GPT_get_from_content(const char *path, const struct GPT_content *content)
-{
-    struct GPT_entry_table *table;
-    size_t sector_bytes;
-
-    table = (struct GPT_entry_table *) malloc(sizeof(*table));
-    table->fd = open(path, O_RDWR);
-
-    if (table->fd < 0) {
-        D(ERR, "unable to open file %s:%s\n", path, strerror(errno));
-        return NULL;
-    }
-
-    if (!ioctl(table->fd, BLKSSZGET, &sector_bytes)) {
-        table->sector_size = (unsigned) sector_bytes;
-        D(INFO, "Got sector size %d", table->sector_size);
-    } else {
-        D(WARN, "unable to get sector size %s, assuming 512", strerror(errno));
-        table->sector_size = 512;
-    }
-
-    if (GPT_map_from_content(table, content)) {
-        D(ERR, "Could not map gpt");
-        return NULL;
-    }
-
-    return table;
-}
-
-
-void GPT_release_device(struct GPT_entry_table *table)
-{
-    gpt_unmap(&table->header_map);
-    gpt_unmap(&table->entries_map);
-    gpt_unmap(&table->sec_header_map);
-    gpt_unmap(&table->sec_entries_map);
-    close(table->fd);
-    free(table);
-}
-
-static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
-static int GPT_check_overlap_except(struct GPT_entry_table *table,
-                                    struct GPT_entry_raw *entry,
-                                    struct GPT_entry_raw *exclude);
-
-void GPT_edit_entry(struct GPT_entry_table *table,
-                    struct GPT_entry_raw *old_entry,
-                    struct GPT_entry_raw *new_entry)
-{
-    struct GPT_entry_raw *current_entry = GPT_get_pointer(table, old_entry);
-
-    if (GPT_check_overlap_except(table, new_entry, current_entry)) {
-        D(ERR, "Couldn't add overlaping partition");
-        return;
-    }
-
-    if (current_entry == NULL) {
-        D(ERR, "Couldn't find entry");
-        return;
-    }
-
-    *current_entry = *new_entry;
-}
-
-int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
-{
-    struct GPT_entry_raw *raw = GPT_get_pointer(table, entry);
-
-    if (raw == NULL) {
-        D(ERR, "could not find entry");
-        return 1;
-    }
-    D(DEBUG, "Deleting gpt entry '%s'\n", raw->partition_guid);
-
-    // Entry in the middle of table may become empty
-    GPT_entry_clear(raw);
-
-    return 0;
-}
-
-void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
-{
-    unsigned i;
-    int inserted = 0;
-    if (GPT_check_overlap(table, entry)) {
-        D(ERR, "Couldn't add overlaping partition");
-        return;
-    }
-
-    if (GPT_get_pointer(table, entry) != NULL) {
-        D(WARN, "Add entry fault, this entry already exists");
-        return;
-    }
-
-    struct GPT_entry_raw *entries = table->entries;
-
-    for (i = 0; i < table->header->entries_count; ++i) {
-        if (!entries[i].type_guid[0]) {
-            inserted = 1;
-            D(DEBUG, "inserting");
-            memcpy(&entries[i], entry, sizeof(entries[i]));
-            break;
-        }
-    }
-
-    if (!inserted) {
-        D(ERR, "Unable to find empty partion entry");
-    }
-}
-
-struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name);
-
-struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
-{
-    if (entry->partition_guid[0] != 0)
-        return GPT_get_pointer_by_guid(table, (const char *) entry->partition_guid);
-    else if (entry->name[0] != 0)
-        return GPT_get_pointer_by_UTFname(table, entry->name);
-
-    D(WARN, "Name or guid needed to find entry");
-    return NULL;
-}
-
-struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *table, const char *name)
-{
-    int current = (int) table->header->entries_count;
-
-    for (current = current - 1; current >= 0; --current) {
-        if (strncmp((char *) name,
-                    (char *) table->entries[current].partition_guid, 16) == 0) {
-                return &table->entries[current];
-        }
-    }
-
-    return NULL;
-}
-
-int strncmp_UTF16_char(const uint16_t *s1, const char *s2, size_t n)
-{
-    if (n == 0)
-        return (0);
-    do {
-        if (((*s1) & 127) != *s2++)
-            return (((unsigned char) ((*s1) & 127)) - *(unsigned char *)--s2);
-        if (*s1++ == 0)
-            break;
-    } while (--n != 0);
-    return (0);
-}
-
-int strncmp_UTF16(const uint16_t *s1, const uint16_t *s2, size_t n)
-{
-    if (n == 0)
-        return (0);
-    do {
-        if ((*s1) != *s2++)
-            return (*s1 - *--s2);
-        if (*s1++ == 0)
-            break;
-    } while (--n != 0);
-    return (0);
-}
-
-struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *table, const char *name)
-{
-    int count = (int) table->header->entries_count;
-    int current;
-
-    for (current = 0; current < count; ++current) {
-        if (strncmp_UTF16_char(table->entries[current].name,
-                         (char *) name, 16) == 0) {
-                    return &table->entries[current];
-        }
-    }
-
-    return NULL;
-}
-
-struct GPT_entry_raw *GPT_get_pointer_by_UTFname(struct GPT_entry_table *table, const uint16_t *name)
-{
-    int count = (int) table->header->entries_count;
-    int current;
-
-    for (current = 0; current < count; ++current) {
-        if (strncmp_UTF16(table->entries[current].name,
-                          name, GPT_NAMELEN) == 0) {
-                return &table->entries[current];
-        }
-    }
-
-    return NULL;
-}
-
-void GPT_sync(struct GPT_entry_table *table)
-{
-    uint32_t crc;
-
-    //calculate crc32
-    crc = crc32(0, Z_NULL, 0);
-    crc = crc32(crc, (void*) table->entries, table->header->entries_count * sizeof(*table->entries));
-    table->header->partition_array_checksum = crc;
-
-    table->header->header_checksum = 0;
-    crc = crc32(0, Z_NULL, 0);
-    crc = crc32(crc, (void*) table->header, table->header->header_size);
-    table->header->header_checksum = crc;
-
-    //sync secondary partion
-    if (table->second_valid) {
-        memcpy((void *)table->second_entries, (void *) table->entries, table->partition_table_size);
-        memcpy((void *)table->second_header, (void *)table->header, sizeof(*table->header));
-    }
-
-    if(!ioctl(table->fd, BLKRRPART, NULL)) {
-        D(WARN, "Unable to force kernel to refresh partition table");
-    }
-}
-
-void GPT_to_UTF16(uint16_t *to, const char *from, int n)
-{
-    int i;
-    for (i = 0; i < (n - 1) && (to[i] = from[i]) != '\0'; ++i);
-    to[i] = '\0';
-}
-
-void GPT_from_UTF16(char *to, const uint16_t *from, int n)
-{
-    int i;
-    for (i = 0; i < (n - 1) && (to[i] = from[i] & 127) != '\0'; ++i);
-    to[i] = '\0';
-}
-
-static int GPT_check_overlap_except(struct GPT_entry_table *table,
-                                    struct GPT_entry_raw *entry,
-                                    struct GPT_entry_raw *exclude) {
-    int current = (int) table->header->entries_count;
-    int dontcheck;
-    struct GPT_entry_raw *current_entry;
-    if (entry->last_lba < entry->first_lba) {
-        D(WARN, "Start address have to be less than end address");
-        return 1;
-    }
-
-    for (current = current - 1; current >= 0; --current) {
-        current_entry = &table->entries[current];
-        dontcheck = strncmp((char *) entry->partition_guid,
-                           (char *) current_entry->partition_guid , 16) == 0;
-        dontcheck |= current_entry->type_guid[0] == 0;
-        dontcheck |= current_entry == exclude;
-
-        if (!dontcheck && ((entry->last_lba >= current_entry->first_lba &&
-                            entry->first_lba < current_entry->last_lba ))) {
-            return 1;
-        }
-    }
-
-    return 0;
-}
-
-static int GPT_check_overlap(struct GPT_entry_table *table, struct GPT_entry_raw *entry)
-{
-    return GPT_check_overlap_except(table, entry, NULL);
-}
-
-static char *get_key_value(char *ptr, char **key, char **value)
-{
-    *key = ptr;
-    ptr = strchr(ptr, '=');
-
-    if (ptr == NULL)
-        return NULL;
-
-    *ptr++ = '\0';
-    *value = ptr;
-    ptr = strchr(ptr, ';');
-
-    if (ptr == NULL)
-        ptr = *value + strlen(*value);
-    else
-        *ptr = '\0';
-
-    *key = strip(*key);
-    *value = strip(*value);
-
-    return ptr;
-}
-
-//TODO: little endian?
-static int add_key_value(const char *key, const char *value, struct GPT_entry_raw *entry)
-{
-    char *endptr;
-    if (!strcmp(key, "type")) {
-        strncpy((char *) entry->type_guid, value, 16);
-        entry->type_guid[15] = 0;
-    }
-    else if (!strcmp(key, "guid")) {
-        strncpy((char *) entry->partition_guid, value, 16);
-        entry->type_guid[15] = 0;
-    }
-    else if (!strcmp(key, "firstlba")) {
-        entry->first_lba = strtoul(value, &endptr, 10);
-        if (*endptr != '\0') goto error;
-    }
-    else if (!strcmp(key, "lastlba")) {
-        entry->last_lba = strtoul(value, &endptr, 10);
-        if (*endptr != '\0') goto error;
-    }
-    else if (!strcmp(key, "flags")) {
-        entry->flags = strtoul(value, &endptr, 16);
-        if (*endptr != '\0') goto error;
-    }
-    else if (!strcmp(key, "name")) {
-        GPT_to_UTF16(entry->name, value, GPT_NAMELEN);
-    }
-    else {
-        goto error;
-    }
-
-    return 0;
-
-error:
-    D(ERR, "Could not find key or parse value: %s,%s", key, value);
-    return 1;
-}
-
-int GPT_parse_entry(char *string, struct GPT_entry_raw *entry)
-{
-    char *ptr = string;
-    char *key, *value;
-
-    while ((ptr = get_key_value(ptr, &key, &value)) != NULL) {
-        if (add_key_value(key, value, entry)) {
-            D(WARN, "key or value not valid: %s %s", key, value);
-            return 1;
-        }
-    }
-
-    return 0;
-}
-
-void entry_set_guid(int n, uint8_t *guid)
-{
-    int fd;
-    fd = open("/dev/urandom", O_RDONLY);
-    read(fd, guid, 16);
-    close(fd);
-
-    //rfc4122
-    guid[8] = (guid[8] & 0x3F) | 0x80;
-    guid[7] = (guid[7] & 0x0F) | 0x40;
-}
-
-void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table)
-{
-    if (table != NULL) {
-        memcpy(&content->header, table->header, sizeof(content->header));
-        content->header.header_size = sizeof(content->header);
-        content->header.entry_size = sizeof(struct GPT_entry_raw);
-    }
-    else {
-        D(WARN, "Could not locate old gpt table, using default values");
-        memset(&content->header, 0, sizeof(content->header) / sizeof(int));
-        content->header = (struct GPT_header) {
-            .revision = 0x10000,
-            .header_size = sizeof(content->header),
-            .header_checksum = 0,
-            .reserved_zeros = 0,
-            .current_lba = 1,
-            .backup_lba = 1,
-            .entry_size = sizeof(struct GPT_entry_raw),
-            .partition_array_checksum = 0
-        };
-        strncpy((char *)content->header.signature, "EFI PART", 8);
-        strncpy((char *)content->header.disk_guid, "ANDROID MMC DISK", 16);
-    }
-}
-
-static int get_config_uint64(cnode *node, uint64_t *ptr, const char *name)
-{
-    const char *tmp;
-    uint64_t val;
-    char *endptr;
-    if ((tmp = config_str(node, name, NULL))) {
-        val = strtoull(tmp, &endptr, 10);
-        if (*endptr != '\0') {
-            D(WARN, "Value for %s is not a number: %s", name, tmp);
-            return 1;
-        }
-        *ptr = val;
-        return 0;
-    }
-    return 1;
-}
-
-static int get_config_string(cnode *node, char *ptr, int max_len, const char *name)
-{
-    size_t begin, end;
-    const char *value = config_str(node, name, NULL);
-    if (!value)
-        return -1;
-
-    begin = strcspn(value, "\"") + 1;
-    end = strcspn(&value[begin], "\"");
-
-    if ((int) end > max_len) {
-        D(WARN, "Identifier \"%s\" too long", value);
-        return -1;
-    }
-
-    strncpy(ptr, &value[begin], end);
-    if((int) end < max_len)
-        ptr[end] = 0;
-    return 0;
-}
-
-static void GPT_parse_header(cnode *node, struct GPT_content *content)
-{
-    get_config_uint64(node, &content->header.current_lba, "header_lba");
-    get_config_uint64(node, &content->header.backup_lba, "backup_lba");
-    get_config_uint64(node, &content->header.first_usable_lba, "first_lba");
-    get_config_uint64(node, &content->header.last_usable_lba, "last_lba");
-    get_config_uint64(node, &content->header.entries_lba, "entries_lba");
-    get_config_string(node, (char *) content->header.disk_guid, 16, "guid");
-}
-
-static int GPT_parse_partitions(cnode *node, struct GPT_content *content)
-{
-    cnode *current;
-    int i;
-    uint64_t partition_size;
-    struct GPT_entry_raw *entry;
-    for (i = 0, current = node->first_child; current; current = current->next, ++i) {
-        entry = &content->entries[i];
-        entry_set_guid(i, content->entries[i].partition_guid);
-        memcpy(&content->entries[i].type_guid, partition_type_uuid, 16);
-        if (get_config_uint64(current, &entry->first_lba, "first_lba")) {
-            D(ERR, "first_lba not specified");
-            return 1;
-        }
-        if (get_config_uint64(current, &partition_size, "partition_size")) {
-            D(ERR, "partition_size not specified");
-            return 1;
-        }
-        if (config_str(current, "system", NULL)) {
-            entry->flags |= GPT_FLAG_SYSTEM;
-        }
-        if (config_str(current, "bootable", NULL)) {
-            entry->flags |= GPT_FLAG_BOOTABLE;
-        }
-        if (config_str(current, "readonly", NULL)) {
-            entry->flags |= GPT_FLAG_READONLY;
-        }
-        if (config_str(current, "automount", NULL)) {
-            entry->flags |= GPT_FLAG_DOAUTOMOUNT;
-        }
-
-        get_config_uint64(current, &content->entries[i].flags, "flags");
-        content->entries[i].last_lba = content->entries[i].first_lba + partition_size - 1;
-        GPT_to_UTF16(content->entries[i].name, current->name, 16);
-    }
-    return 0;
-}
-
-static inline int cnode_count(cnode *node)
-{
-    int i;
-    cnode *current;
-    for (i = 0, current = node->first_child; current; current = current->next, ++i)
-        ;
-    return i;
-}
-
-
-static int GPT_parse_cnode(cnode *root, struct GPT_content *content)
-{
-    cnode *partnode;
-
-    if (!(partnode = config_find(root, "partitions"))) {
-        D(ERR, "Could not find partition table");
-        return 0;
-    }
-
-    GPT_parse_header(root, content);
-
-    content->header.entries_count = cnode_count(partnode);
-    content->entries = malloc(content->header.entries_count * sizeof(struct GPT_entry_raw));
-
-    if (GPT_parse_partitions(partnode, content)) {
-        D(ERR, "Could not parse partitions");
-        return 0;
-    }
-
-    return 1;
-}
-
-int GPT_parse_file(int fd, struct GPT_content *content)
-{
-    char *data;
-    int size;
-    int ret;
-    cnode *root = config_node("", "");
-
-    size = get_file_size(fd);
-    data = (char *) mmap(NULL, size + 1, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-
-    if (data == NULL) {
-        if (size == 0)
-            D(ERR, "config file empty");
-        else
-            D(ERR, "Out of memory");
-        return 0;
-    }
-
-    data[size - 1] = 0;
-    config_load(root, data);
-
-    if (root->first_child == NULL) {
-        D(ERR, "Could not read config file");
-        return 0;
-    }
-
-    ret = GPT_parse_cnode(root, content);
-    munmap(data, size);
-    return ret;
-}
-
-void GPT_release_content(struct GPT_content *content)
-{
-    free(content->entries);
-}
-
-int GPT_write_content(const char *device, struct GPT_content *content)
-{
-    struct GPT_entry_table *maptable;
-
-    maptable = GPT_get_from_content(device, content);
-    if (maptable == NULL) {
-        D(ERR, "could not map device");
-        return 0;
-    }
-
-    memcpy(maptable->header, &content->header, sizeof(*maptable->header));
-    memcpy(maptable->entries, content->entries,
-           content->header.entries_count * sizeof(*maptable->entries));
-
-    GPT_sync(maptable);
-    GPT_release_device(maptable);
-
-    return 1;
-}
-
diff --git a/fastbootd/commands/partitions.h b/fastbootd/commands/partitions.h
deleted file mode 100644
index 9a6a88d..0000000
--- a/fastbootd/commands/partitions.h
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-
-#ifndef __FASTBOOTD_PATITIONS_
-#define __FASTBOOTD_PATITIONS_
-
-#include <stdint.h>
-
-#define GPT_ENTRIES 128
-#define GPT_NAMELEN 36
-
-#define GPT_FLAG_SYSTEM (1ULL << 0)
-#define GPT_FLAG_BOOTABLE (1ULL << 2)
-#define GPT_FLAG_READONLY (1ULL << 60)
-#define GPT_FLAG_DOAUTOMOUNT (1ULL << 63)
-
-// it should be passed in little endian order
-struct GPT_entry_raw {
-    uint8_t type_guid[16];
-    uint8_t partition_guid[16];
-    uint64_t first_lba; // little endian
-    uint64_t last_lba;
-    uint64_t flags;
-    uint16_t name[GPT_NAMELEN]; // UTF-16 LE
-};
-
-struct GPT_mapping {
-    void *map_ptr;
-    void *ptr;
-    unsigned size;
-};
-
-struct GPT_entry_table {
-    int fd;
-
-    struct GPT_mapping header_map;
-    struct GPT_mapping entries_map;
-    struct GPT_mapping sec_header_map;
-    struct GPT_mapping sec_entries_map;
-
-    struct GPT_header *header;
-    struct GPT_entry_raw *entries;
-    struct GPT_header *second_header;
-    struct GPT_entry_raw *second_entries;
-
-    unsigned sector_size;
-    unsigned partition_table_size;
-    int second_valid;
-};
-
-struct GPT_header {
-    uint8_t signature[8];
-    uint32_t revision;
-    uint32_t header_size;
-    uint32_t header_checksum;
-    uint32_t reserved_zeros;
-    uint64_t current_lba;
-    uint64_t backup_lba;
-    uint64_t first_usable_lba;
-    uint64_t last_usable_lba;
-    uint8_t disk_guid[16];
-    uint64_t entries_lba;
-    uint32_t entries_count;
-    uint32_t entry_size;
-    uint32_t partition_array_checksum;
-    // the rest should be filled with zeros
-} __attribute__((packed));
-
-struct GPT_content {
-    struct GPT_header header;
-    struct GPT_entry_raw *entries;
-};
-
-
-struct GPT_entry_table* GPT_get_device(const char *, unsigned lba);
-
-void GPT_release_device(struct GPT_entry_table *);
-
-void GPT_edit_entry(struct GPT_entry_table *table,
-                    struct GPT_entry_raw *old_entry,
-                    struct GPT_entry_raw *new_entry);
-
-int GPT_delete_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
-
-void GPT_add_entry(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
-
-struct GPT_entry_raw *GPT_get_pointer(struct GPT_entry_table *table, struct GPT_entry_raw *entry);
-struct GPT_entry_raw *GPT_get_pointer_by_guid(struct GPT_entry_table *, const char *);
-struct GPT_entry_raw *GPT_get_pointer_by_name(struct GPT_entry_table *, const char *);
-
-//Use after every edit operation
-void GPT_sync();
-
-void GPT_to_UTF16(uint16_t *, const char *, int );
-void GPT_from_UTF16(char *, const uint16_t *, int);
-
-int GPT_parse_entry(char *string, struct GPT_entry_raw *entry);
-
-void GPT_default_content(struct GPT_content *content, struct GPT_entry_table *table);
-
-void GPT_release_content(struct GPT_content *content);
-
-int GPT_parse_file(int fd, struct GPT_content *content);
-
-int GPT_write_content(const char *device, struct GPT_content *content);
-
-int gpt_mmap(struct GPT_mapping *mapping, uint64_t location, int size, int fd);
-
-void gpt_unmap(struct GPT_mapping *mapping);
-
-#endif
diff --git a/fastbootd/commands/virtual_partitions.c b/fastbootd/commands/virtual_partitions.c
deleted file mode 100644
index 813f485..0000000
--- a/fastbootd/commands/virtual_partitions.c
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include "commands/virtual_partitions.h"
-#include "debug.h"
-
-static struct virtual_partition *partitions = NULL;
-
-int try_handle_virtual_partition(struct protocol_handle *handle, const char *arg)
-{
-    struct virtual_partition *current;
-
-    for (current = partitions; current != NULL; current = current->next) {
-        if (!strcmp(current->name, arg)) {
-            current->handler(handle, arg);
-        }
-    }
-
-    return 0;
-}
-
-void virtual_partition_register(
-        const char * name,
-        void (*handler)(struct protocol_handle *phandle, const char *arg))
-{
-    struct virtual_partition *new;
-    new = malloc(sizeof(*new));
-    if (new) {
-        new->name = name;
-        new->handler = handler;
-        new->next = partitions;
-        partitions = new;
-    }
-    else {
-        D(ERR, "Out of memory");
-    }
-}
diff --git a/fastbootd/commands/virtual_partitions.h b/fastbootd/commands/virtual_partitions.h
deleted file mode 100644
index 88df71e..0000000
--- a/fastbootd/commands/virtual_partitions.h
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef FASTBOOTD_VIRTUAL_PARTITIONS_H
-#define FASTBOOTD_VIRTUAL_PARTITIONS_H
-
-#include "protocol.h"
-
-struct virtual_partition {
-    struct virtual_partition *next;
-    const char *name;
-    void (*handler)(struct protocol_handle *phandle, const char *arg);
-};
-
-int try_handle_virtual_partition(struct protocol_handle *handle, const char *arg);
-
-void virtual_partition_register(
-        const char * name,
-        void (*handler)(struct protocol_handle *phandle, const char *arg));
-
-#endif
diff --git a/fastbootd/config.c b/fastbootd/config.c
deleted file mode 100644
index fe6da69..0000000
--- a/fastbootd/config.c
+++ /dev/null
@@ -1,145 +0,0 @@
-/*
- * Copyright (c) 2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <fcntl.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-
-#include "protocol.h"
-#include "utils.h"
-
-#include "debug.h"
-
-// TODO: change config path
-#define CONFIG_PATH "/data/fastboot.cfg"
-
-static int config_parse_line(char *line)
-{
-    char *c;
-    char *key;
-    char *value;
-
-    c = strchr(line, '#');
-    if (c)
-        *c = '\0';
-
-    if (strspn(line, " \t") == strlen(line))
-        return 0;
-
-    c = strchr(line, '=');
-    if (c == NULL)
-        return -1;
-
-    key = line;
-    *c = '\0';
-    value = c + 1;
-
-    key = strip(key);
-    value = strip(value);
-
-    key = strdup(key);
-    value = strdup(value);
-
-    fastboot_publish(key, value);
-
-    return 0;
-}
-
-static void config_parse(char *buffer)
-{
-    char *saveptr;
-    char *str = buffer;
-    char *line = buffer;
-    int c;
-    int ret;
-
-    for (c = 1; line != NULL; c++) {
-        line = strtok_r(str, "\r\n", &saveptr);
-        if (line != NULL) {
-            D(VERBOSE, "'%s'", line);
-            ret = config_parse_line(line);
-            if (ret < 0) {
-                D(WARN, "error parsing " CONFIG_PATH " line %d", c);
-            }
-        }
-        str = NULL;
-    }
-}
-
-void config_init()
-{
-    int fd;
-    off_t len;
-    ssize_t ret;
-    size_t count = 0;
-    char *buffer;
-
-    fd = open(CONFIG_PATH, O_RDONLY);
-    if (fd < 0) {
-        D(ERR, "failed to open " CONFIG_PATH);
-        return;
-    }
-
-    len = lseek(fd, 0, SEEK_END);
-    if (len < 0) {
-        D(ERR, "failed to seek to end of " CONFIG_PATH);
-        return;
-    }
-
-    lseek(fd, 0, SEEK_SET);
-
-    buffer = malloc(len + 1);
-    if (buffer == NULL) {
-        D(ERR, "failed to allocate %ld bytes", len);
-        return;
-    }
-
-    while (count < (size_t)len) {
-        ret = read(fd, buffer + count, len - count);
-        if (ret < 0 && errno != EINTR) {
-            D(ERR, "failed to read " CONFIG_PATH ": %d %s", errno, strerror(errno));
-            return;
-        }
-        if (ret == 0) {
-            D(ERR, "early EOF reading " CONFIG_PATH);
-            return;
-        }
-
-        count += ret;
-    }
-
-    buffer[len] = '\0';
-
-    config_parse(buffer);
-
-    free(buffer);
-}
diff --git a/fastbootd/debug.h b/fastbootd/debug.h
deleted file mode 100644
index 74620b8..0000000
--- a/fastbootd/debug.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _FASTBOOTD_DEBUG_H_
-#define _FASTBOOTD_DEBUG_H_
-
-#include <stdio.h>
-
-#include <cutils/klog.h>
-
-#define ERR 0
-#define WARN 1
-#define INFO 2
-#define VERBOSE 3
-#define DEBUG 4
-
-extern unsigned int debug_level;
-
-//#define DLOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
-#define DLOG(fmt, ...) KLOG_INFO("fastbootd", fmt, ##__VA_ARGS__)
-
-#define D(level, fmt, ...) \
-    do { \
-        if (debug_level == level || debug_level > level) { \
-            DLOG("%s:%d " fmt "\n", __BASE_FILE__, __LINE__, ##__VA_ARGS__); \
-        } \
-    } while (0)
-
-#endif
diff --git a/fastbootd/fastbootd.c b/fastbootd/fastbootd.c
deleted file mode 100644
index 2b51b33..0000000
--- a/fastbootd/fastbootd.c
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Copyright (C) 2013 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 <stdio.h>
-#include <unistd.h>
-#include <cutils/klog.h>
-#include <getopt.h>
-#include <stdlib.h>
-
-#include "debug.h"
-#include "trigger.h"
-#include "socket_client.h"
-#include "secure.h"
-
-unsigned int debug_level = DEBUG;
-
-void commands_init();
-void usb_init();
-void config_init();
-int transport_socket_init();
-int network_discovery_init();
-void ssh_server_start();
-
-int main(int argc, char **argv)
-{
-    int socket_client = 0;
-    int c;
-    int network = 1;
-
-    klog_init();
-    klog_set_level(6);
-
-    const struct option longopts[] = {
-        {"socket", no_argument, 0, 'S'},
-        {"nonetwork", no_argument, 0, 'n'},
-        {0, 0, 0, 0}
-    };
-
-    while (1) {
-        c = getopt_long(argc, argv, "Sn", longopts, NULL);
-        /* Alphabetical cases */
-        if (c < 0)
-            break;
-        switch (c) {
-        case 'S':
-            socket_client = 1;
-            break;
-        case 'n':
-            network = 0;
-            break;
-        case '?':
-            return 1;
-        default:
-            return 0;
-        }
-    }
-
-    (void)argc;
-    (void)argv;
-
-    klog_init();
-    klog_set_level(6);
-
-    if (socket_client) {
-        //TODO: Shouldn't we change current tty into raw mode?
-        run_socket_client();
-    }
-    else {
-        cert_init_crypto();
-        config_init();
-        load_trigger();
-        commands_init();
-        usb_init();
-
-        if (network) {
-            if (!transport_socket_init())
-                exit(1);
-            ssh_server_start();
-            network_discovery_init();
-        }
-
-        while (1) {
-            sleep(1);
-        }
-    }
-    return 0;
-}
diff --git a/fastbootd/network_discovery.c b/fastbootd/network_discovery.c
deleted file mode 100644
index 1cd3e48..0000000
--- a/fastbootd/network_discovery.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <dns_sd.h>
-#include <cutils/properties.h>
-#include <unistd.h>
-
-#include "debug.h"
-#include "network_discovery.h"
-#include "utils.h"
-
-#define MDNS_SERVICE_NAME "mdnsd"
-#define MDNS_SERVICE_STATUS "init.svc.mdnsd"
-#define FASTBOOTD_TYPE "_fastbootd._tcp"
-#define FASTBOOTD_DOMAIN "local."
-#define FASTBOOTD_NAME "fastbootd"
-
-
-static void reg_reply(DNSServiceRef sdref, const DNSServiceFlags flags, DNSServiceErrorType errorCode,
-    const char *name, const char *regtype, const char *domain, void *context)
-{
-    (void)sdref;    // Unused
-    (void)flags;    // Unused
-    (void)context;  // Unused
-    if (errorCode == kDNSServiceErr_ServiceNotRunning) {
-        fprintf(stderr, "Error code %d\n", errorCode);
-    }
-
-
-    printf("Got a reply for service %s.%s%s: ", name, regtype, domain);
-
-    if (errorCode == kDNSServiceErr_NoError)
-    {
-        if (flags & kDNSServiceFlagsAdd)
-            printf("Name now registered and active\n");
-        else
-            printf("Name registration removed\n");
-        if (errorCode == kDNSServiceErr_NameConflict)
-            printf("Name in use, please choose another\n");
-        else
-            printf("Error %d\n", errorCode);
-
-        if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
-    }
-}
-
-static int register_service() {
-    DNSServiceRef sdref = NULL;
-    const char *domain = FASTBOOTD_DOMAIN;
-    const char *type = FASTBOOTD_TYPE;
-    const char *host = NULL;
-    char name[PROP_VALUE_MAX];
-    uint16_t port = 22;
-    int flags = 0;
-    DNSServiceErrorType result;
-    property_get("ro.serialno", name, "");
-    if (!strcmp(name, "")) {
-        D(ERR, "No property serialno");
-        return -1;
-    }
-
-    result = DNSServiceRegister(&sdref, flags, kDNSServiceInterfaceIndexAny,
-                       name, type, domain, host, port,
-                       0, NULL, reg_reply, NULL);
-    if (result != kDNSServiceErr_NoError) {
-        D(ERR, "Unable to register service");
-        return -1;
-    }
-    return 0;
-}
-
-
-int network_discovery_init()
-{
-    D(INFO, "Starting discovery");
-    if (service_start(MDNS_SERVICE_NAME)) {
-        D(ERR, "Unable to start discovery");
-        return -1;
-    }
-
-    if (register_service()) {
-        D(ERR, "Unable to register service");
-        return -1;
-    }
-
-    return 0;
-}
-
diff --git a/fastbootd/network_discovery.h b/fastbootd/network_discovery.h
deleted file mode 100644
index 75fda63..0000000
--- a/fastbootd/network_discovery.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef _FASTBOOTD_NETWORK_DISCOVERY_H
-#define _FASTBOOTD_NETWORK_DISCOVERY_H
-
-int network_discovery_init();
-
-#endif
diff --git a/fastbootd/other/gptedit.c b/fastbootd/other/gptedit.c
deleted file mode 100644
index d423529..0000000
--- a/fastbootd/other/gptedit.c
+++ /dev/null
@@ -1,303 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <getopt.h>
-#include <inttypes.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include <cutils/klog.h>
-
-#include "commands/partitions.h"
-#include "debug.h"
-
-unsigned int debug_level = DEBUG;
-//TODO: add tool to generate config file
-
-void usage() {
-    fprintf(stderr,
-            "usage: test_gpt [ <option> ] <file>\n"
-            "\n"
-            "options:\n"
-            "  -p                                       print partitions\n"
-            "  -c                                       print config file\n"
-            "  -a                                       adds new partition\n"
-            "  -d                                       deletes partition (-o needed)\n"
-            "\n"
-            "  -n name@startlba,endlba                  new partition detail\n"
-            "  -o                                       old partition name\n"
-            "  -t                                       type guid\n"
-            "  -g                                       partition guid\n"
-            "  -l gpt_location                          specyfies gpt secto\n"
-    );
-
-}
-
-void printGPT(struct GPT_entry_table *table);
-void addGPT(struct GPT_entry_table *table, const char *arg, const char *guid, const char *tguid);
-void deleteGPT(struct GPT_entry_table *table, const char *name);
-void configPrintGPT(struct GPT_entry_table *table);
-
-int main(int argc, char *argv[]) {
-    int print_cmd = 0;
-    int config_cmd = 0;
-    int add_cmd = 0;
-    int del_cmd = 0;
-    int sync_cmd = 0;
-    int c;
-    const char *new_partition = NULL;
-    const char *old_partition = NULL;
-    const char *type_guid = NULL;
-    const char *partition_guid = NULL;
-    unsigned gpt_location = 1;
-
-    klog_init();
-    klog_set_level(6);
-
-    const struct option longopts[] = {
-        {"print", no_argument, 0, 'p'},
-        {"config-print", no_argument, 0, 'c'},
-        {"add", no_argument, 0, 'a'},
-        {"del", no_argument, 0, 'd'},
-        {"new", required_argument, 0, 'n'},
-        {"old", required_argument, 0, 'o'},
-        {"type", required_argument, 0, 't'},
-        {"sync", required_argument, 0, 's'},
-        {"guid", required_argument, 0, 'g'},
-        {"location", required_argument, 0, 'l'},
-        {0, 0, 0, 0}
-    };
-
-    while (1) {
-        c = getopt_long(argc, argv, "pcadt:g:n:o:sl:", longopts, NULL);
-        /* Alphabetical cases */
-        if (c < 0)
-            break;
-        switch (c) {
-        case 'p':
-            print_cmd = 1;
-            break;
-        case 'c':
-            config_cmd = 1;
-            break;
-        case 'a':
-            add_cmd = 1;
-            break;
-        case 'd':
-            del_cmd = 1;
-            break;
-        case 'n':
-            new_partition = optarg;
-            break;
-        case 'o':
-            old_partition = optarg;
-            break;
-        case 't':
-            type_guid = optarg;
-        case 'g':
-            partition_guid = optarg;
-            break;
-        case 's':
-            sync_cmd = 1;
-            break;
-        case 'l':
-            gpt_location = strtoul(optarg, NULL, 10);
-            fprintf(stderr, "Got offset as %d", gpt_location);
-            break;
-        case '?':
-            return 1;
-        default:
-            abort();
-        }
-    }
-
-    argc -= optind;
-    argv += optind;
-
-    if (argc < 1) {
-        usage();
-        return 1;
-    }
-
-    const char *path = argv[0];
-    struct GPT_entry_table *table = GPT_get_device(path, gpt_location);
-    if (table == NULL) {
-        fprintf(stderr, "unable to get GPT table from %s\n", path);
-        return 1;
-    }
-
-    if (add_cmd)
-        addGPT(table, new_partition, partition_guid, type_guid);
-    if (del_cmd)
-        deleteGPT(table, old_partition);
-    if (print_cmd)
-        printGPT(table);
-    if (config_cmd)
-        configPrintGPT(table);
-    if (sync_cmd)
-        GPT_sync(table);
-
-    GPT_release_device(table);
-
-    return 0;
-}
-
-void printGPT(struct GPT_entry_table *table) {
-    struct GPT_entry_raw *entry = table->entries;
-    unsigned n, m;
-    char name[GPT_NAMELEN + 1];
-
-    printf("ptn  start block   end block     name\n");
-    printf("---- ------------- -------------\n");
-
-    for (n = 0; n < table->header->entries_count; n++, entry++) {
-        if (entry->type_guid[0] == 0)
-            continue;
-        for (m = 0; m < GPT_NAMELEN; m++) {
-            name[m] = entry->name[m] & 127;
-        }
-        name[m] = 0;
-        printf("#%03d %13"PRId64" %13"PRId64" %s\n",
-            n + 1, entry->first_lba, entry->last_lba, name);
-    }
-}
-
-void configPrintGPT(struct GPT_entry_table *table) {
-    struct GPT_entry_raw *entry = table->entries;
-    unsigned n, m;
-    char name[GPT_NAMELEN + 1];
-    char temp_guid[17];
-    temp_guid[16] = 0;
-
-    printf("header_lba %"PRId64"\n", table->header->current_lba);
-    printf("backup_lba %"PRId64"\n", table->header->backup_lba);
-    printf("first_lba %"PRId64"\n", table->header->first_usable_lba);
-    printf("last_lba %"PRId64"\n", table->header->last_usable_lba);
-    printf("entries_lba %"PRId64"\n", table->header->entries_lba);
-    snprintf(temp_guid, 17, "%s", table->header->disk_guid);
-    printf("guid \"%s\"", temp_guid);
-
-    printf("\npartitions {\n");
-
-    for (n = 0; n < table->header->entries_count; n++, entry++) {
-        uint64_t size = entry->last_lba - entry->first_lba + 1;
-
-        if (entry->type_guid[0] == 0)
-            continue;
-        for (m = 0; m < GPT_NAMELEN; m++) {
-            name[m] = entry->name[m] & 127;
-        }
-        name[m] = 0;
-
-        printf("    %s {\n", name);
-        snprintf(temp_guid, 17, "%s", entry->partition_guid);
-        printf("        guid \"%s\"\n", temp_guid);
-        printf("        first_lba %"PRId64"\n", entry->first_lba);
-        printf("        partition_size %"PRId64"\n", size);
-        if (entry->flags & GPT_FLAG_SYSTEM)
-            printf("        system\n");
-        if (entry->flags & GPT_FLAG_BOOTABLE)
-            printf("        bootable\n");
-        if (entry->flags & GPT_FLAG_READONLY)
-            printf("        readonly\n");
-        if (entry->flags & GPT_FLAG_DOAUTOMOUNT)
-            printf("        automount\n");
-        printf("    }\n\n");
-    }
-    printf("}\n");
-}
-
-void addGPT(struct GPT_entry_table *table, const char *str  , const char *guid, const char *tguid) {
-    char *c, *c2;
-    char *arg = malloc(strlen(str));
-    char *name = arg;
-    unsigned start, end;
-    strcpy(arg, str);
-    if (guid == NULL || tguid == NULL) {
-        fprintf(stderr, "Type guid and partion guid needed");
-        free(arg);
-        return;
-    }
-
-    c = strchr(arg, '@');
-
-    if (c == NULL) {
-        fprintf(stderr, "Wrong entry format");
-        free(arg);
-        return;
-    }
-
-    *c++ = '\0';
-
-    c2 = strchr(c, ',');
-
-    if (c2 == NULL) {
-        fprintf(stderr, "Wrong entry format");
-        free(arg);
-        return;
-    }
-
-    start = strtoul(c, NULL, 10);
-    *c2++ = '\0';
-    end = strtoul(c2, NULL, 10);
-
-    struct GPT_entry_raw data;
-    strncpy((char *)data.partition_guid, guid, 15);
-    data.partition_guid[15] = '\0';
-    strncpy((char *)data.type_guid, tguid, 15);
-    data.type_guid[15] = '\0';
-    GPT_to_UTF16(data.name, name, GPT_NAMELEN);
-    data.first_lba = start;
-    data.last_lba = end;
-
-    fprintf(stderr, "Adding (%d,%d) %s as, [%s, %s]", start, end, name, (char *) data.type_guid, (char *) data.partition_guid);
-    GPT_add_entry(table, &data);
-    free(arg);
-}
-
-void deleteGPT(struct GPT_entry_table *table, const char *name) {
-    struct GPT_entry_raw *entry;
-
-    if (name == NULL) {
-        fprintf(stderr, "Need partition name");
-        return;
-    }
-
-    entry = GPT_get_pointer_by_name(table, name);
-
-    if (!entry) {
-        fprintf(stderr, "Unable to find partition: %s", name);
-        return;
-    }
-    GPT_delete_entry(table, entry);
-}
-
diff --git a/fastbootd/other/partitions.sample.cfg b/fastbootd/other/partitions.sample.cfg
deleted file mode 100644
index 49562cf..0000000
--- a/fastbootd/other/partitions.sample.cfg
+++ /dev/null
@@ -1,60 +0,0 @@
-
-header_lba 1
-backup_lba 101
-first_lba 43
-last_lba 100
-entries_lba 2
-
-partitions {
-    #You can generate this as output from gptedit -c
-    SOS {
-        first_lba 28672
-        partition_size 16384
-    }
-
-    DTB {
-        first_lba 45056
-        partition_size 8192
-    }
-
-    LNX {
-        first_lba 53248
-        partition_size 16384
-    }
-
-    APP {
-        first_lba 69632
-        partition_size 1048576
-    }
-
-    CAC {
-        first_lba 1118208
-        partition_size 1572864
-    }
-
-    MSC {
-        first_lba 2691072
-        partition_size 4096
-    }
-
-    USP {
-        first_lba 2695168
-        partition_size 65536
-    }
-
-    MDA {
-        first_lba 2760704
-        partition_size 4096
-    }
-
-    FCT {
-        first_lba 2764800
-        partition_size 32768
-    }
-
-    UDA {
-        first_lba 2797568
-        partition_size 27975680
-    }
-}
-
diff --git a/fastbootd/other/sign/src/SignImg.java b/fastbootd/other/sign/src/SignImg.java
deleted file mode 100644
index 338d427..0000000
--- a/fastbootd/other/sign/src/SignImg.java
+++ /dev/null
@@ -1,181 +0,0 @@
-package signtool;
-
-import java.io.*;
-import java.util.Properties;
-import java.util.ArrayList;
-
-import javax.mail.internet.*;
-import javax.mail.MessagingException;
-import javax.mail.Session;
-import javax.activation.MailcapCommandMap;
-import javax.activation.CommandMap;
-
-import java.security.PrivateKey;
-import java.security.Security;
-import java.security.KeyFactory;
-import java.security.KeyStore;
-import java.security.NoSuchAlgorithmException;
-import java.security.spec.PKCS8EncodedKeySpec;
-import java.security.spec.InvalidKeySpecException;
-import java.security.cert.X509Certificate;
-import java.security.cert.CertificateFactory;
-import java.security.cert.Certificate;
-import java.security.cert.CertificateException;
-import java.security.cert.CertificateEncodingException;
-
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
-import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
-import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
-import org.bouncycastle.operator.ContentSigner;
-import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
-import org.bouncycastle.cms.CMSProcessableByteArray;
-import org.bouncycastle.cms.CMSSignedGenerator;
-import org.bouncycastle.cms.CMSSignedDataGenerator;
-import org.bouncycastle.cms.CMSSignedGenerator;
-import org.bouncycastle.cms.CMSProcessable;
-import org.bouncycastle.cms.CMSSignedData;
-import org.bouncycastle.cms.CMSTypedData;
-import org.bouncycastle.cert.jcajce.JcaCertStore;
-import org.bouncycastle.util.Store;
-import org.bouncycastle.asn1.ASN1InputStream;    
-import org.bouncycastle.asn1.DEROutputStream;
-import org.bouncycastle.asn1.ASN1Object;
-
-
-public class SignImg {
-
-    /* It reads private key in pkcs#8 formate
-     * Conversion:
-     * openssl pkcs8 -topk8 -nocrypt -outform DER < inkey.pem > outkey.pk8
-     */
-    private static PrivateKey getPrivateKey(String path) throws IOException, FileNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException {
-        File file = new File(path);
-        FileInputStream fis = new FileInputStream(file);
-        byte[] data = new byte[(int)file.length()];
-        fis.read(data);
-        fis.close();
-
-        PKCS8EncodedKeySpec kspec = new PKCS8EncodedKeySpec(data);
-        KeyFactory kf = KeyFactory.getInstance("RSA");
-        PrivateKey privateKey = kf.generatePrivate(kspec);
-
-        return privateKey;
-    }
-
-    private static MimeBodyPart getContent(String path) throws IOException, FileNotFoundException, MessagingException {
-        MimeBodyPart body = new MimeBodyPart();
-
-        File file = new File(path);
-        FileInputStream fis = new FileInputStream(file);
-        byte[] data = new byte[(int)file.length()];
-        fis.read(data);
-        fis.close();
-
-        body.setContent(data, "application/octet-stream");
-
-        return body;
-    }
-
-    private static CMSProcessableByteArray getCMSContent(String path) throws IOException, FileNotFoundException, MessagingException {
-        File file = new File(path);
-        FileInputStream fis = new FileInputStream(file);
-        byte[] data = new byte[(int)file.length()];
-        fis.read(data);
-        fis.close();
-        CMSProcessableByteArray cms = new CMSProcessableByteArray(data);
-
-        return cms;
-    }
-
-    private static X509Certificate readCert(String path) throws IOException, FileNotFoundException, CertificateException {
-        File file = new File(path);
-        FileInputStream is = new FileInputStream(file);
-
-        CertificateFactory cf = CertificateFactory.getInstance("X.509");
-        Certificate cert = cf.generateCertificate(is);
-        is.close();
-
-        return (X509Certificate) cert;
-    }
-
-    private static void save(MimeBodyPart content, String path) throws IOException, FileNotFoundException, MessagingException {
-        File file = new File(path);
-        FileOutputStream os = new FileOutputStream(file);
-
-        content.writeTo(os);
-
-        os.close();
-    }
-
-    private static Store certToStore(X509Certificate certificate) throws CertificateEncodingException {
-        ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
-        certList.add(certificate);
-        return new JcaCertStore(certList);
-    }
-
-    public static void setDefaultMailcap()
-    {
-        MailcapCommandMap _mailcap =
-            (MailcapCommandMap)CommandMap.getDefaultCommandMap();
-
-        _mailcap.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
-        _mailcap.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
-        _mailcap.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
-        _mailcap.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
-        _mailcap.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
-
-        CommandMap.setDefaultCommandMap(_mailcap);
-    } 
-
-    public static void main(String[] args) {
-        try {
-            if (args.length < 4) {
-                System.out.println("Usage: signimg data private_key certificate output");
-                return;
-            }
-            System.out.println("Signing the image");
-            setDefaultMailcap();
-
-            Security.addProvider(new BouncyCastleProvider());
-
-            PrivateKey key = getPrivateKey(args[1]);
-            System.out.println("File read sucessfully");
-
-            CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
-
-            CMSTypedData body = getCMSContent(args[0]);
-            System.out.println("Content read sucessfully");
-
-            X509Certificate cert = (X509Certificate) readCert(args[2]);
-            System.out.println("Certificate read sucessfully");
-
-            ContentSigner sha256Signer = new JcaContentSignerBuilder("SHA256withRSA").setProvider("BC").build(key);
-
-            Store certs = certToStore(cert);
-
-            generator.addCertificates(certs);
-            generator.addSignerInfoGenerator(
-                          new JcaSignerInfoGeneratorBuilder(
-                                new JcaDigestCalculatorProviderBuilder().setProvider("BC").build())
-                          .build(sha256Signer, cert));
-
-            CMSSignedData signed = generator.generate(body, true);
-            System.out.println("Signed");
-
-            Properties props = System.getProperties();
-            Session session = Session.getDefaultInstance(props, null);
-            
-            File file = new File(args[3]);
-            FileOutputStream os = new FileOutputStream(file);
-
-            ASN1InputStream asn1 = new ASN1InputStream(signed.getEncoded());
-            ByteArrayOutputStream out = new ByteArrayOutputStream(); 
-            DEROutputStream dOut = new DEROutputStream(os); 
-            dOut.writeObject(ASN1Object.fromByteArray(signed.getEncoded()));
-
-        }
-        catch (Exception ex) {
-            System.out.println("Exception during programm execution: " + ex.getMessage());
-        }
-    }
-}
diff --git a/fastbootd/protocol.c b/fastbootd/protocol.c
deleted file mode 100644
index 3908020..0000000
--- a/fastbootd/protocol.c
+++ /dev/null
@@ -1,195 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "debug.h"
-#include "protocol.h"
-#include "transport.h"
-
-#define STATE_OFFLINE   0
-#define STATE_COMMAND   1
-#define STATE_COMPLETE  2
-#define STATE_ERROR     3
-
-struct fastboot_cmd {
-    struct fastboot_cmd *next;
-    const char *prefix;
-    unsigned prefix_len;
-    void (*execute)(struct protocol_handle *phandle, const char *arg);
-};
-
-struct fastboot_var {
-    struct fastboot_var *next;
-    const char *name;
-    const char *value;
-};
-
-static struct fastboot_cmd *cmdlist;
-
-void fastboot_register(const char *prefix,
-        void (*phandle)(struct protocol_handle *phandle, const char *arg))
-{
-    struct fastboot_cmd *cmd;
-    cmd = malloc(sizeof(*cmd));
-    if (cmd) {
-        cmd->prefix = prefix;
-        cmd->prefix_len = strlen(prefix);
-        cmd->execute = phandle;
-        cmd->next = cmdlist;
-        cmdlist = cmd;
-    }
-}
-
-static struct fastboot_var *varlist;
-
-void fastboot_publish(const char *name, const char *value)
-{
-    struct fastboot_var *var;
-    var = malloc(sizeof(*var));
-    if (var) {
-        var->name = name;
-        var->value = value;
-        var->next = varlist;
-        varlist = var;
-    }
-}
-
-const char *fastboot_getvar(const char *name)
-{
-    struct fastboot_var *var;
-
-    for (var = varlist; var; var = var->next) {
-        if (!strcmp(var->name, name)) {
-            return var->value;
-        }
-    }
-
-    return "";
-}
-
-int protocol_handle_download(struct protocol_handle *phandle, size_t len)
-{
-    return transport_handle_download(phandle->transport_handle, len);
-}
-
-static ssize_t protocol_handle_write(struct protocol_handle *phandle,
-        char *buffer, size_t len)
-{
-    return transport_handle_write(phandle->transport_handle, buffer, len);
-}
-
-static void fastboot_ack(struct protocol_handle *phandle, const char *code,
-        const char *reason)
-{
-    char response[64];
-
-    if (phandle->state != STATE_COMMAND)
-        return;
-
-    if (reason == 0)
-        reason = "";
-
-    snprintf(response, 64, "%s%s", code, reason);
-    phandle->state = STATE_COMPLETE;
-
-    protocol_handle_write(phandle, response, strlen(response));
-}
-
-void fastboot_fail(struct protocol_handle *phandle, const char *reason)
-{
-    fastboot_ack(phandle, "FAIL", reason);
-}
-
-void fastboot_okay(struct protocol_handle *phandle, const char *info)
-{
-    fastboot_ack(phandle, "OKAY", info);
-}
-
-void fastboot_data(struct protocol_handle *phandle, size_t len)
-{
-    char response[64];
-    ssize_t ret;
-
-    snprintf(response, 64, "DATA%08zx", len);
-    ret = protocol_handle_write(phandle, response, strlen(response));
-    if (ret < 0)
-        return;
-}
-
-void protocol_handle_command(struct protocol_handle *phandle, char *buffer)
-{
-    D(INFO,"fastboot: %s\n", buffer);
-
-    struct fastboot_cmd *cmd;
-
-    for (cmd = cmdlist; cmd; cmd = cmd->next) {
-        if (memcmp(buffer, cmd->prefix, cmd->prefix_len))
-            continue;
-        phandle->state = STATE_COMMAND;
-        cmd->execute(phandle, buffer + cmd->prefix_len);
-        if (phandle->state == STATE_COMMAND)
-            fastboot_fail(phandle, "unknown reason");
-        return;
-    }
-
-    fastboot_fail(phandle, "unknown command");
-}
-
-struct protocol_handle *create_protocol_handle(struct transport_handle *thandle)
-{
-    struct protocol_handle *phandle;
-
-    phandle = calloc(sizeof(struct protocol_handle), 1);
-
-    phandle->transport_handle = thandle;
-    phandle->state = STATE_OFFLINE;
-    phandle->download_fd = -1;
-
-    pthread_mutex_init(&phandle->lock, NULL);
-
-    return phandle;
-}
-
-int protocol_get_download(struct protocol_handle *phandle)
-{
-    int fd;
-
-    pthread_mutex_lock(&phandle->lock);
-    fd = phandle->download_fd;
-    phandle->download_fd = -1;
-    pthread_mutex_unlock(&phandle->lock);
-
-    return fd;
-}
diff --git a/fastbootd/protocol.h b/fastbootd/protocol.h
deleted file mode 100644
index ea2a8df..0000000
--- a/fastbootd/protocol.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef _FASTBOOTD_PROTOCOL_H_
-#define _FASTBOOTD_PROTOCOL_H_
-
-#include <pthread.h>
-#include <stddef.h>
-
-struct protocol_handle {
-    struct transport_handle *transport_handle;
-    unsigned int state;
-    int download_fd;
-
-    pthread_mutex_t lock;
-};
-
-void fastboot_register(const char *prefix,
-               void (*handle)(struct protocol_handle *handle, const char *arg));
-
-void fastboot_publish(const char *name, const char *value);
-const char *fastboot_getvar(const char *name);
-
-struct protocol_handle *create_protocol_handle(struct transport_handle *t);
-void protocol_handle_command(struct protocol_handle *handle, char *buffer);
-int protocol_handle_download(struct protocol_handle *phandle, size_t len);
-int protocol_get_download(struct protocol_handle *phandle);
-
-void fastboot_fail(struct protocol_handle *handle, const char *reason);
-void fastboot_okay(struct protocol_handle *handle, const char *reason);
-void fastboot_data(struct protocol_handle *handle, size_t len);
-
-#endif
diff --git a/fastbootd/secure.c b/fastbootd/secure.c
deleted file mode 100644
index 186e026..0000000
--- a/fastbootd/secure.c
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-
-#include <openssl/pem.h>
-#include <openssl/engine.h>
-#include <openssl/x509.h>
-#include <openssl/x509v3.h>
-#include <openssl/pem.h>
-#include <openssl/cms.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include "secure.h"
-#include "debug.h"
-#include "utils.h"
-
-
-void cert_init_crypto() {
-    CRYPTO_malloc_init();
-    ERR_load_crypto_strings();
-    OpenSSL_add_all_algorithms();
-    ENGINE_load_builtin_engines();
-}
-
-X509_STORE *cert_store_from_path(const char *path) {
-
-    X509_STORE *store;
-    struct stat st;
-    X509_LOOKUP *lookup;
-
-    if (stat(path, &st)) {
-        D(ERR, "Unable to stat cert path");
-        goto error;
-    }
-
-    if (!(store = X509_STORE_new())) {
-        goto error;
-    }
-
-    if (S_ISDIR(st.st_mode)) {
-        lookup = X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
-        if (lookup == NULL)
-            goto error;
-        if (!X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM)) {
-            D(ERR, "Error loading cert directory %s", path);
-            goto error;
-        }
-    }
-    else if(S_ISREG(st.st_mode)) {
-        lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file());
-        if (lookup == NULL)
-            goto error;
-        if (!X509_LOOKUP_load_file(lookup, path, X509_FILETYPE_PEM)) {
-            D(ERR, "Error loading cert directory %s", path);
-            goto error;
-        }
-    }
-    else {
-        D(ERR, "cert path is not directory or regular file");
-        goto error;
-    }
-
-    return store;
-
-error:
-    return NULL;
-}
-
-
-int cert_read(int fd, CMS_ContentInfo **content, BIO **output) {
-    BIO *input;
-    *output = NULL;
-
-
-    input = BIO_new_fd(fd, BIO_NOCLOSE);
-    if (input == NULL) {
-        D(ERR, "Unable to open input");
-        goto error;
-    }
-
-    //TODO:
-    // read with d2i_CMS_bio to support DER
-    // with java or just encode data with base64
-    *content = SMIME_read_CMS(input, output);
-    if (*content == NULL) {
-        unsigned long err = ERR_peek_last_error();
-        D(ERR, "Unable to parse input file: %s", ERR_lib_error_string(err));
-        goto error_read;
-    }
-
-    BIO_free(input);
-
-    return 0;
-
-error_read:
-    BIO_free(input);
-error:
-    return 1;
-}
-
-int cert_verify(BIO *content, CMS_ContentInfo *content_info, X509_STORE *store, int *out_fd) {
-    BIO *output_temp;
-    int ret;
-
-    *out_fd = create_temp_file();
-    if (*out_fd < 0) {
-        D(ERR, "unable to create temporary file");
-        return -1;
-    }
-
-    output_temp = BIO_new_fd(*out_fd, BIO_NOCLOSE);
-    if (output_temp == NULL) {
-        D(ERR, "unable to create temporary bio");
-        close(*out_fd);
-        return -1;
-    }
-
-    ret = CMS_verify(content_info, NULL ,store, content, output_temp, 0);
-
-    if (ret == 0) {
-        char buf[256];
-        unsigned long err = ERR_peek_last_error();
-        D(ERR, "Verification failed with reason: %s, %s", ERR_lib_error_string(err),  ERR_error_string(err, buf));
-        D(ERR, "Data used: content %p", content);
-    }
-
-    ERR_clear_error();
-    ERR_remove_state(0);
-
-    BIO_free(output_temp);
-
-    return ret;
-}
-
-void cert_release(BIO *content, CMS_ContentInfo *info) {
-    BIO_free(content);
-    CMS_ContentInfo_free(info);
-}
-
diff --git a/fastbootd/secure.h b/fastbootd/secure.h
deleted file mode 100644
index 878a643..0000000
--- a/fastbootd/secure.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef _FASTBOOTD_SECURE_H
-#define _FASTBOOTD_SECURE_H
-
-#include <openssl/ssl.h>
-#include <openssl/x509.h>
-#include <openssl/x509v3.h>
-#include <openssl/pem.h>
-#include <openssl/cms.h>
-
-void cert_init_crypto();
-
-X509_STORE *cert_store_from_path(const char*stream);
-
-static inline void cert_release_store(X509_STORE *store) {
-    X509_STORE_free(store);
-}
-
-int cert_read(int fd, CMS_ContentInfo **content, BIO **output);
-int cert_verify(BIO *content, CMS_ContentInfo *content_info, X509_STORE *store, int *out_fd);
-void cert_release(BIO *content, CMS_ContentInfo *info);
-
-#endif
diff --git a/fastbootd/socket_client.c b/fastbootd/socket_client.c
deleted file mode 100644
index da636db..0000000
--- a/fastbootd/socket_client.c
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <stdio.h>
-#include <cutils/sockets.h>
-#include <poll.h>
-#include <unistd.h>
-
-#include "utils.h"
-
-#define BUFFER_SIZE 256
-
-#define STDIN_FD 0
-#define STDOUT_FD 1
-#define STDERR_FD 2
-
-void run_socket_client() {
-    int fd;
-    char buffer[BUFFER_SIZE];
-    int n;
-    struct pollfd fds[2];
-
-    fd = socket_local_client("fastbootd",
-                         ANDROID_SOCKET_NAMESPACE_RESERVED,
-                         SOCK_STREAM);
-
-    if (fd < 0) {
-        fprintf(stderr, "ERROR: Unable to open fastbootd socket\n");
-        return;
-    }
-
-    fds[0].fd = STDIN_FD;
-    fds[0].events = POLLIN;
-    fds[1].fd = fd;
-    fds[1].events = POLLIN;
-
-    while(true) {
-        if (poll(fds, 2, -1) <= 0) {
-            fprintf(stderr, "ERROR: socket error");
-            return;
-        }
-
-        if (fds[0].revents & POLLIN) {
-            if ((n = read(STDIN_FD, buffer, BUFFER_SIZE)) < 0) {
-                goto error;
-            }
-
-            if (bulk_write(fd, buffer, n) < 0) {
-                goto error;
-            }
-        }
-
-        if (fds[1].revents & POLLIN) {
-            if ((n = read(fd, buffer, BUFFER_SIZE)) < 0) {
-                goto error;
-            }
-
-            if (bulk_write(STDOUT_FD, buffer, n) < 0) {
-                goto error;
-            }
-        }
-    }
-
-error:
-    fprintf(stderr, "Transport error\n");
-}
diff --git a/fastbootd/socket_client.h b/fastbootd/socket_client.h
deleted file mode 100644
index 4481ff2..0000000
--- a/fastbootd/socket_client.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef _FASTBOOTD_SOCKET_CLIENT_H
-#define _FASTBOOTD_SOCKET_CLIENT_H
-
-void run_socket_client();
-
-#endif
diff --git a/fastbootd/transport.c b/fastbootd/transport.c
deleted file mode 100644
index 9a16fd7..0000000
--- a/fastbootd/transport.c
+++ /dev/null
@@ -1,150 +0,0 @@
-/*
- * Copyright (C) 2013 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 <pthread.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/mman.h>
-
-#include "debug.h"
-#include "protocol.h"
-#include "transport.h"
-
-#define COMMAND_BUF_SIZE 64
-
-ssize_t transport_handle_write(struct transport_handle *thandle, char *buffer, size_t len)
-{
-    return thandle->transport->write(thandle, buffer, len);
-}
-
-void transport_handle_close(struct transport_handle *thandle)
-{
-    thandle->transport->close(thandle);
-}
-
-int transport_handle_download(struct transport_handle *thandle, size_t len)
-{
-    ssize_t ret;
-    size_t n = 0;
-    int fd;
-    // TODO: move out of /dev
-    char tempname[] = "/dev/fastboot_download_XXXXXX";
-    char *buffer;
-
-    fd = mkstemp(tempname);
-    if (fd < 0)
-        return -1;
-
-    unlink(tempname);
-
-    ftruncate(fd, len);
-
-    buffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-    if (buffer == MAP_FAILED) {
-        D(ERR, "mmap(%zu) failed: %d %s", len, errno, strerror(errno));
-        goto err;
-    }
-
-    while (n < len) {
-        ret = thandle->transport->read(thandle, buffer + n, len - n);
-        if (ret <= 0) {
-            D(WARN, "transport read failed, ret=%zd %s", ret, strerror(-ret));
-            break;
-        }
-        n += ret;
-    }
-
-    munmap(buffer, len);
-
-    if (n != len)
-        goto err;
-
-    return fd;
-
-err:
-    close(fd);
-    transport_handle_close(thandle);
-    return -1;
-}
-
-static void *transport_data_thread(void *arg)
-{
-    struct transport_handle *thandle = arg;
-    struct protocol_handle *phandle = create_protocol_handle(thandle);
-
-    while (!thandle->stopped) {
-        int ret;
-        char buffer[COMMAND_BUF_SIZE + 1];
-        D(VERBOSE, "transport_data_thread\n");
-
-        ret = thandle->transport->read(thandle, buffer, COMMAND_BUF_SIZE);
-        if (ret <= 0) {
-            D(DEBUG, "ret = %d\n", ret);
-            break;
-        }
-        if (ret > 0) {
-            buffer[ret] = 0;
-            //TODO: multiple threads
-            protocol_handle_command(phandle, buffer);
-        }
-    }
-
-    transport_handle_close(thandle);
-    free(thandle);
-
-    return NULL;
-}
-
-static void *transport_connect_thread(void *arg)
-{
-    struct transport *transport = arg;
-    while (!transport->stopped) {
-        struct transport_handle *thandle;
-        pthread_t thread;
-        pthread_attr_t attr;
-
-        D(VERBOSE, "transport_connect_thread\n");
-        thandle = transport->connect(transport);
-        if (thandle == NULL) {
-            D(ERR, "transport connect failed\n");
-            sleep(1);
-            continue;
-        }
-        thandle->transport = transport;
-
-        pthread_attr_init(&attr);
-        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
-        pthread_create(&thread, &attr, transport_data_thread, thandle);
-
-        sleep(1);
-    }
-
-    return NULL;
-}
-
-void transport_register(struct transport *transport)
-{
-    pthread_t thread;
-    pthread_attr_t attr;
-
-    pthread_attr_init(&attr);
-    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
-    pthread_create(&thread, &attr, transport_connect_thread, transport);
-}
diff --git a/fastbootd/transport.h b/fastbootd/transport.h
deleted file mode 100644
index 209340d..0000000
--- a/fastbootd/transport.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _FASTBOOTD_TRANSPORT_H_
-#define _FASTBOOTD_TRANSPORT_H_
-
-#include <stdbool.h>
-
-struct transport_handle {
-    struct transport *transport;
-
-    bool stopped;
-};
-
-struct transport {
-    void (*init)();
-    void (*close)(struct transport_handle *thandle);
-    ssize_t (*read)(struct transport_handle *thandle, void *data, size_t len);
-    ssize_t (*write)(struct transport_handle *thandle, const void *data, size_t len);
-    struct transport_handle *(*connect)(struct transport *transport);
-    bool stopped;
-};
-
-void transport_register(struct transport *transport);
-ssize_t transport_handle_write(struct transport_handle *handle, char *buffer, size_t len);
-int transport_handle_download(struct transport_handle *handle, size_t len);
-
-#endif
diff --git a/fastbootd/transport_socket.c b/fastbootd/transport_socket.c
deleted file mode 100644
index 664d473..0000000
--- a/fastbootd/transport_socket.c
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <unistd.h>
-#include <cutils/sockets.h>
-
-#include "debug.h"
-#include "transport.h"
-#include "utils.h"
-
-
-#define container_of(ptr, type, member) \
-    ((type*)((char*)(ptr) - offsetof(type, member)))
-
-#define SOCKET_WORKING 0
-#define SOCKET_STOPPED -1
-
-
-struct socket_transport {
-    struct transport transport;
-
-    int fd;
-};
-
-struct socket_handle {
-    struct transport_handle handle;
-
-    int fd;
-};
-
-void socket_close(struct transport_handle *thandle)
-{
-    struct socket_handle * handle = container_of(thandle, struct socket_handle, handle);
-    close(handle->fd);
-}
-
-struct transport_handle *socket_connect(struct transport *transport)
-{
-    struct socket_handle *handle = calloc(sizeof(struct socket_handle), 1);
-    struct socket_transport *socket_transport = container_of(transport, struct socket_transport, transport);
-    struct sockaddr addr;
-    socklen_t alen = sizeof(addr);
-
-    handle->fd = accept(socket_transport->fd, &addr, &alen);
-
-    if (handle->fd < 0) {
-        D(WARN, "socket connect error");
-        return NULL;
-    }
-
-    D(DEBUG, "[ socket_thread - registering device ]");
-    return &handle->handle;
-}
-
-ssize_t socket_write(struct transport_handle *thandle, const void *data, size_t len)
-{
-    ssize_t ret;
-    struct socket_handle *handle = container_of(thandle, struct socket_handle, handle);
-
-    D(DEBUG, "about to write (fd=%d, len=%zu)", handle->fd, len);
-    ret = bulk_write(handle->fd, data, len);
-    if (ret < 0) {
-        D(ERR, "ERROR: fd = %d, ret = %zd", handle->fd, ret);
-        return -1;
-    }
-    D(DEBUG, "[ socket_write done fd=%d ]", handle->fd);
-    return ret;
-}
-
-ssize_t socket_read(struct transport_handle *thandle, void *data, size_t len)
-{
-    ssize_t ret;
-    struct socket_handle *handle = container_of(thandle, struct socket_handle, handle);
-
-    D(DEBUG, "about to read (fd=%d, len=%zu)", handle->fd, len);
-    ret = bulk_read(handle->fd, data, len);
-    if (ret < 0) {
-        D(ERR, "ERROR: fd = %d, ret = %zd", handle->fd, ret);
-        return -1;
-    }
-    D(DEBUG, "[ socket_read done fd=%d ret=%zd]", handle->fd, ret);
-    return ret;
-}
-
-static int listen_socket_init(struct socket_transport *socket_transport)
-{
-    int s = android_get_control_socket("fastbootd");
-
-    if (s < 0) {
-        D(WARN, "android_get_control_socket(fastbootd): %s\n", strerror(errno));
-        return 0;
-    }
-
-    if (listen(s, 4) < 0) {
-        D(WARN, "listen(control socket): %s\n", strerror(errno));
-        return 0;
-    }
-
-    socket_transport->fd = s;
-
-    return 1;
-}
-
-
-int transport_socket_init()
-{
-    struct socket_transport *socket_transport = malloc(sizeof(struct socket_transport));
-
-    socket_transport->transport.connect = socket_connect;
-    socket_transport->transport.close = socket_close;
-    socket_transport->transport.read = socket_read;
-    socket_transport->transport.write = socket_write;
-
-    if (!listen_socket_init(socket_transport)) {
-        D(ERR, "socket transport init failed");
-        free(socket_transport);
-        return 0;
-    }
-
-    transport_register(&socket_transport->transport);
-    return 1;
-}
-
diff --git a/fastbootd/trigger.c b/fastbootd/trigger.c
deleted file mode 100644
index df0f895..0000000
--- a/fastbootd/trigger.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <dlfcn.h>
-
-#include <hardware/hardware.h>
-#include "debug.h"
-#include "trigger.h"
-#include "protocol.h"
-#include "vendor_trigger.h"
-
-static const int version = 1;
-
-int load_trigger() {
-    int libversion;
-
-    if (trigger_init() != 0) {
-        D(ERR, "libvendortrigger failed to initialize");
-        return 1;
-    }
-
-    if (trigger_check_version(version, &libversion)) {
-        D(ERR, "Library report incompability");
-        return 1;
-    }
-
-    D(INFO, "libvendortrigger loaded");
-    return 0;
-}
diff --git a/fastbootd/trigger.h b/fastbootd/trigger.h
deleted file mode 100644
index d2d9573..0000000
--- a/fastbootd/trigger.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef __FASTBOOTD_TRIGGER_H_
-#define __FASTBOOTD_TRIGGER_H_
-
-#include "commands/partitions.h"
-#include "vendor_trigger.h"
-
-int load_trigger();
-
-#endif
diff --git a/fastbootd/usb_linux_client.c b/fastbootd/usb_linux_client.c
deleted file mode 100644
index 64420e9..0000000
--- a/fastbootd/usb_linux_client.c
+++ /dev/null
@@ -1,308 +0,0 @@
-/*
- * Copyright (C) 2007 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 <endian.h>
-#include <fcntl.h>
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <sys/ioctl.h>
-#include <sys/types.h>
-
-#include <linux/usb/ch9.h>
-#include <linux/usb/functionfs.h>
-
-#include "debug.h"
-#include "transport.h"
-#include "utils.h"
-
-#define   TRACE_TAG  TRACE_USB
-
-#define MAX_PACKET_SIZE_FS     64
-#define MAX_PACKET_SIZE_HS     512
-
-#define cpu_to_le16(x)  htole16(x)
-#define cpu_to_le32(x)  htole32(x)
-
-#define FASTBOOT_CLASS         0xff
-#define FASTBOOT_SUBCLASS      0x42
-#define FASTBOOT_PROTOCOL      0x3
-
-#define USB_FFS_FASTBOOT_PATH  "/dev/usb-ffs/adb/"
-#define USB_FFS_FASTBOOT_EP(x) USB_FFS_FASTBOOT_PATH#x
-
-#define USB_FFS_FASTBOOT_EP0   USB_FFS_FASTBOOT_EP(ep0)
-#define USB_FFS_FASTBOOT_OUT   USB_FFS_FASTBOOT_EP(ep1)
-#define USB_FFS_FASTBOOT_IN    USB_FFS_FASTBOOT_EP(ep2)
-
-#define container_of(ptr, type, member) \
-    ((type*)((char*)(ptr) - offsetof(type, member)))
-
-struct usb_transport {
-    struct transport transport;
-
-    pthread_cond_t notify;
-    pthread_mutex_t lock;
-
-    int control;
-    int bulk_out; /* "out" from the host's perspective => source for fastbootd */
-    int bulk_in;  /* "in" from the host's perspective => sink for fastbootd */
-};
-
-struct usb_handle {
-    struct transport_handle handle;
-};
-
-static const struct {
-    struct usb_functionfs_descs_head header;
-    struct {
-        struct usb_interface_descriptor intf;
-        struct usb_endpoint_descriptor_no_audio source;
-        struct usb_endpoint_descriptor_no_audio sink;
-    } __attribute__((packed)) fs_descs, hs_descs;
-} __attribute__((packed)) descriptors = {
-    .header = {
-        .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
-        .length = cpu_to_le32(sizeof(descriptors)),
-        .fs_count = 3,
-        .hs_count = 3,
-    },
-    .fs_descs = {
-        .intf = {
-            .bLength = sizeof(descriptors.fs_descs.intf),
-            .bDescriptorType = USB_DT_INTERFACE,
-            .bInterfaceNumber = 0,
-            .bNumEndpoints = 2,
-            .bInterfaceClass = FASTBOOT_CLASS,
-            .bInterfaceSubClass = FASTBOOT_SUBCLASS,
-            .bInterfaceProtocol = FASTBOOT_PROTOCOL,
-            .iInterface = 1, /* first string from the provided table */
-        },
-        .source = {
-            .bLength = sizeof(descriptors.fs_descs.source),
-            .bDescriptorType = USB_DT_ENDPOINT,
-            .bEndpointAddress = 1 | USB_DIR_OUT,
-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
-            .wMaxPacketSize = MAX_PACKET_SIZE_FS,
-        },
-        .sink = {
-            .bLength = sizeof(descriptors.fs_descs.sink),
-            .bDescriptorType = USB_DT_ENDPOINT,
-            .bEndpointAddress = 2 | USB_DIR_IN,
-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
-            .wMaxPacketSize = MAX_PACKET_SIZE_FS,
-        },
-    },
-    .hs_descs = {
-        .intf = {
-            .bLength = sizeof(descriptors.hs_descs.intf),
-            .bDescriptorType = USB_DT_INTERFACE,
-            .bInterfaceNumber = 0,
-            .bNumEndpoints = 2,
-            .bInterfaceClass = FASTBOOT_CLASS,
-            .bInterfaceSubClass = FASTBOOT_SUBCLASS,
-            .bInterfaceProtocol = FASTBOOT_PROTOCOL,
-            .iInterface = 1, /* first string from the provided table */
-        },
-        .source = {
-            .bLength = sizeof(descriptors.hs_descs.source),
-            .bDescriptorType = USB_DT_ENDPOINT,
-            .bEndpointAddress = 1 | USB_DIR_OUT,
-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
-            .wMaxPacketSize = MAX_PACKET_SIZE_HS,
-        },
-        .sink = {
-            .bLength = sizeof(descriptors.hs_descs.sink),
-            .bDescriptorType = USB_DT_ENDPOINT,
-            .bEndpointAddress = 2 | USB_DIR_IN,
-            .bmAttributes = USB_ENDPOINT_XFER_BULK,
-            .wMaxPacketSize = MAX_PACKET_SIZE_HS,
-        },
-    },
-};
-
-#define STR_INTERFACE_ "Fastboot Interface"
-
-static const struct {
-    struct usb_functionfs_strings_head header;
-    struct {
-        __le16 code;
-        const char str1[sizeof(STR_INTERFACE_)];
-    } __attribute__((packed)) lang0;
-} __attribute__((packed)) strings = {
-    .header = {
-        .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
-        .length = cpu_to_le32(sizeof(strings)),
-        .str_count = cpu_to_le32(1),
-        .lang_count = cpu_to_le32(1),
-    },
-    .lang0 = {
-        cpu_to_le16(0x0409), /* en-us */
-        STR_INTERFACE_,
-    },
-};
-
-static int init_functionfs(struct usb_transport *usb_transport)
-{
-    ssize_t ret;
-
-    D(VERBOSE, "OPENING %s", USB_FFS_FASTBOOT_EP0);
-    usb_transport->control = open(USB_FFS_FASTBOOT_EP0, O_RDWR);
-    if (usb_transport->control < 0) {
-        D(ERR, "[ %s: cannot open control endpoint: errno=%d]", USB_FFS_FASTBOOT_EP0, errno);
-        goto err;
-    }
-
-    ret = write(usb_transport->control, &descriptors, sizeof(descriptors));
-    if (ret < 0) {
-        D(ERR, "[ %s: write descriptors failed: errno=%d ]", USB_FFS_FASTBOOT_EP0, errno);
-        goto err;
-    }
-
-    ret = write(usb_transport->control, &strings, sizeof(strings));
-    if (ret < 0) {
-        D(ERR, "[ %s: writing strings failed: errno=%d]", USB_FFS_FASTBOOT_EP0, errno);
-        goto err;
-    }
-
-    usb_transport->bulk_out = open(USB_FFS_FASTBOOT_OUT, O_RDWR);
-    if (usb_transport->bulk_out < 0) {
-        D(ERR, "[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_FASTBOOT_OUT, errno);
-        goto err;
-    }
-
-    usb_transport->bulk_in = open(USB_FFS_FASTBOOT_IN, O_RDWR);
-    if (usb_transport->bulk_in < 0) {
-        D(ERR, "[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_FASTBOOT_IN, errno);
-        goto err;
-    }
-
-    return 0;
-
-err:
-    if (usb_transport->bulk_in > 0) {
-        close(usb_transport->bulk_in);
-        usb_transport->bulk_in = -1;
-    }
-    if (usb_transport->bulk_out > 0) {
-        close(usb_transport->bulk_out);
-        usb_transport->bulk_out = -1;
-    }
-    if (usb_transport->control > 0) {
-        close(usb_transport->control);
-        usb_transport->control = -1;
-    }
-    return -1;
-}
-
-static ssize_t usb_write(struct transport_handle *thandle, const void *data, size_t len)
-{
-    ssize_t ret;
-    struct transport *t = thandle->transport;
-    struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
-
-    D(DEBUG, "about to write (fd=%d, len=%zu)", usb_transport->bulk_in, len);
-    ret = bulk_write(usb_transport->bulk_in, data, len);
-    if (ret < 0) {
-        D(ERR, "ERROR: fd = %d, ret = %zd", usb_transport->bulk_in, ret);
-        return -1;
-    }
-    D(DEBUG, "[ usb_write done fd=%d ]", usb_transport->bulk_in);
-    return ret;
-}
-
-ssize_t usb_read(struct transport_handle *thandle, void *data, size_t len)
-{
-    ssize_t ret;
-    struct transport *t = thandle->transport;
-    struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
-
-    D(DEBUG, "about to read (fd=%d, len=%zu)", usb_transport->bulk_out, len);
-    ret = bulk_read(usb_transport->bulk_out, data, len);
-    if (ret < 0) {
-        D(ERR, "ERROR: fd = %d, ret = %zd", usb_transport->bulk_out, ret);
-        return -1;
-    }
-    D(DEBUG, "[ usb_read done fd=%d ret=%zd]", usb_transport->bulk_out, ret);
-    return ret;
-}
-
-void usb_close(struct transport_handle *thandle)
-{
-    int err;
-    struct transport *t = thandle->transport;
-    struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
-
-    err = ioctl(usb_transport->bulk_in, FUNCTIONFS_CLEAR_HALT);
-    if (err < 0)
-        D(WARN, "[ kick: source (fd=%d) clear halt failed (%d) ]", usb_transport->bulk_in, errno);
-
-    err = ioctl(usb_transport->bulk_out, FUNCTIONFS_CLEAR_HALT);
-    if (err < 0)
-        D(WARN, "[ kick: sink (fd=%d) clear halt failed (%d) ]", usb_transport->bulk_out, errno);
-
-    pthread_mutex_lock(&usb_transport->lock);
-    close(usb_transport->control);
-    close(usb_transport->bulk_out);
-    close(usb_transport->bulk_in);
-    usb_transport->control = usb_transport->bulk_out = usb_transport->bulk_in = -1;
-
-    pthread_cond_signal(&usb_transport->notify);
-    pthread_mutex_unlock(&usb_transport->lock);
-}
-
-struct transport_handle *usb_connect(struct transport *transport)
-{
-    int ret;
-    struct usb_handle *usb_handle = calloc(sizeof(struct usb_handle), 1);
-    struct usb_transport *usb_transport = container_of(transport, struct usb_transport, transport);
-
-    pthread_mutex_lock(&usb_transport->lock);
-    while (usb_transport->control != -1)
-        pthread_cond_wait(&usb_transport->notify, &usb_transport->lock);
-    pthread_mutex_unlock(&usb_transport->lock);
-
-    ret = init_functionfs(usb_transport);
-    if (ret < 0) {
-        D(ERR, "usb connect: failed to initialize usb transport");
-        return NULL;
-    }
-
-    D(DEBUG, "[ usb_thread - registering device ]");
-    return &usb_handle->handle;
-}
-
-void usb_init()
-{
-    struct usb_transport *usb_transport = calloc(1, sizeof(struct usb_transport));
-
-    usb_transport->transport.connect = usb_connect;
-    usb_transport->transport.close = usb_close;
-    usb_transport->transport.read = usb_read;
-    usb_transport->transport.write = usb_write;
-    usb_transport->control  = -1;
-    usb_transport->bulk_out = -1;
-    usb_transport->bulk_out = -1;
-
-    pthread_cond_init(&usb_transport->notify, NULL);
-    pthread_mutex_init(&usb_transport->lock, NULL);
-
-    transport_register(&usb_transport->transport);
-}
-
diff --git a/fastbootd/utils.c b/fastbootd/utils.c
deleted file mode 100644
index bef2463..0000000
--- a/fastbootd/utils.c
+++ /dev/null
@@ -1,260 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <sys/ioctl.h>
-#include <linux/fs.h>
-#include <stdlib.h>
-#include <cutils/properties.h>
-
-#include "utils.h"
-#include "debug.h"
-
-#ifndef BLKDISCARD
-#define BLKDISCARD _IO(0x12,119)
-#endif
-
-#ifndef BLKSECDISCARD
-#define BLKSECDISCARD _IO(0x12,125)
-#endif
-
-#define READ_BUF_SIZE (16*1024)
-
-int get_stream_size(FILE *stream) {
-    int size;
-    fseek(stream, 0, SEEK_END);
-    size = ftell(stream);
-    fseek(stream, 0, SEEK_SET);
-    return size;
-}
-
-uint64_t get_block_device_size(int fd)
-{
-    uint64_t size = 0;
-    int ret;
-
-    ret = ioctl(fd, BLKGETSIZE64, &size);
-
-    if (ret)
-        return 0;
-
-    return size;
-}
-
-uint64_t get_file_size(int fd)
-{
-    struct stat buf;
-    int ret;
-    int64_t computed_size;
-
-    ret = fstat(fd, &buf);
-    if (ret)
-        return 0;
-
-    if (S_ISREG(buf.st_mode))
-        computed_size = buf.st_size;
-    else if (S_ISBLK(buf.st_mode))
-        computed_size = get_block_device_size(fd);
-    else
-        computed_size = 0;
-
-    return computed_size;
-}
-
-uint64_t get_file_size64(int fd)
-{
-    struct stat64 buf;
-    int ret;
-    uint64_t computed_size;
-
-    ret = fstat64(fd, &buf);
-    if (ret)
-        return 0;
-
-    if (S_ISREG(buf.st_mode))
-        computed_size = buf.st_size;
-    else if (S_ISBLK(buf.st_mode))
-        computed_size = get_block_device_size(fd);
-    else
-        computed_size = 0;
-
-    return computed_size;
-}
-
-
-char *strip(char *str)
-{
-    int n;
-
-    n = strspn(str, " \t");
-    str += n;
-    n = strcspn(str, " \t");
-    str[n] = '\0';
-
-    return str;
-}
-
-int wipe_block_device(int fd, int64_t len)
-{
-    uint64_t range[2];
-    int ret;
-
-    range[0] = 0;
-    range[1] = len;
-    ret = ioctl(fd, BLKSECDISCARD, &range);
-    if (ret < 0) {
-        range[0] = 0;
-        range[1] = len;
-        ret = ioctl(fd, BLKDISCARD, &range);
-        if (ret < 0) {
-            D(WARN, "Discard failed\n");
-            return 1;
-        } else {
-            D(WARN, "Wipe via secure discard failed, used discard instead\n");
-            return 0;
-        }
-    }
-
-    return 0;
-}
-
-int create_temp_file() {
-    char tempname[] = "/dev/fastboot_data_XXXXXX";
-    int fd;
-
-    fd = mkstemp(tempname);
-    if (fd < 0)
-        return -1;
-
-    unlink(tempname);
-
-    return fd;
-}
-
-ssize_t bulk_write(int bulk_in, const char *buf, size_t length)
-{
-    size_t count = 0;
-    ssize_t ret;
-
-    do {
-        ret = TEMP_FAILURE_RETRY(write(bulk_in, buf + count, length - count));
-        if (ret < 0) {
-            D(WARN, "[ bulk_write failed fd=%d length=%zu errno=%d %s ]",
-                    bulk_in, length, errno, strerror(errno));
-            return -1;
-        } else {
-            count += ret;
-        }
-    } while (count < length);
-
-    D(VERBOSE, "[ bulk_write done fd=%d ]", bulk_in);
-    return count;
-}
-
-ssize_t bulk_read(int bulk_out, char *buf, size_t length)
-{
-    ssize_t ret;
-    size_t n = 0;
-
-    while (n < length) {
-        size_t to_read = (length - n > READ_BUF_SIZE) ? READ_BUF_SIZE : length - n;
-        ret = TEMP_FAILURE_RETRY(read(bulk_out, buf + n, to_read));
-        if (ret < 0) {
-            D(WARN, "[ bulk_read failed fd=%d length=%zu errno=%d %s ]",
-                    bulk_out, length, errno, strerror(errno));
-            return ret;
-        }
-        n += ret;
-        if (ret < (ssize_t)to_read) {
-            D(VERBOSE, "bulk_read short read, ret=%zd to_read=%zu n=%zu length=%zu",
-                    ret, to_read, n, length);
-            break;
-        }
-    }
-
-    return n;
-}
-
-#define NAP_TIME 200  // 200 ms between polls
-static int wait_for_property(const char *name, const char *desired_value, int maxwait)
-{
-    char value[PROPERTY_VALUE_MAX] = {'\0'};
-    int maxnaps = (maxwait * 1000) / NAP_TIME;
-
-    if (maxnaps < 1) {
-        maxnaps = 1;
-    }
-
-    while (maxnaps-- > 0) {
-        usleep(NAP_TIME * 1000);
-        if (property_get(name, value, NULL)) {
-            if (desired_value == NULL || strcmp(value, desired_value) == 0) {
-                return 0;
-            }
-        }
-    }
-    return -1; /* failure */
-}
-
-int service_start(const char *service_name)
-{
-    int result = 0;
-    char property_value[PROPERTY_VALUE_MAX];
-
-    property_get(service_name, property_value, "");
-    if (strcmp("running", property_value) != 0) {
-        D(INFO, "Starting %s", service_name);
-        property_set("ctl.start", service_name);
-        if (wait_for_property(service_name, "running", 5))
-            result = -1;
-    }
-
-    return result;
-}
-
-int service_stop(const char *service_name)
-{
-    int result = 0;
-
-    D(INFO, "Stopping MDNSD");
-    property_set("ctl.stop", service_name);
-    if (wait_for_property(service_name, "stopped", 5))
-        result = -1;
-
-    return result;
-}
-
-int ssh_server_start()
-{
-    return service_start("sshd");
-}
diff --git a/fastbootd/utils.h b/fastbootd/utils.h
deleted file mode 100644
index 3d98699..0000000
--- a/fastbootd/utils.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef _FASTBOOT_UTLIS_H
-#define _FASTBOOT_UTILS_H
-
-#include <stdio.h>
-
-int get_stream_size(FILE *);
-
-char *strip(char *str);
-
-uint64_t get_file_size64(int fd);
-uint64_t get_file_size(int fd);
-uint64_t get_block_device_size(int fd);
-int wipe_block_device(int fd, int64_t len);
-int create_temp_file();
-ssize_t bulk_read(int bulk_out, char *buf, size_t length);
-ssize_t bulk_write(int bulk_in, const char *buf, size_t length);
-int service_start(const char *service_name);
-int service_stop(const char *service_name);
-int ssh_server_start();
-
-#define ROUND_TO_PAGE(address,pagesize) ((address + pagesize - 1) & (~(pagesize - 1)))
-
-#define ROUND_UP(number,size) (((number + size - 1) / size) * size)
-
-#endif
diff --git a/fastbootd/vendor_trigger.h b/fastbootd/vendor_trigger.h
deleted file mode 100644
index 0c83be6..0000000
--- a/fastbootd/vendor_trigger.h
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#ifndef __VENDOR_TRIGGER_H_
-#define __VENDOR_TRIGGER_H_
-
-__BEGIN_DECLS
-
-struct GPT_entry_raw;
-struct GPT_content;
-
-/*
- * Implemented in libvendortrigger to handle platform-specific behavior.
- */
-
-/*
- * trigger_init() is called once at startup time before calling any other method
- *
- * returns 0 on success and nonzero on error
- */
-int trigger_init(void);
-
-/*
- * This function runs once after trigger_init completes.
- *
- * version is number parameter indicating version on the fastbootd side
- * libversion is version indicateing version of the library version
- *
- * returns 0 if it can cooperate with the current version and 1 in opposite
- */
-int trigger_check_version(const int version, int *libversion);
-
-/*
- * Return value -1 forbid the action from the vendor site and sets errno
- */
-int trigger_gpt_layout(struct GPT_content *);
-int trigger_oem_cmd(const char *arg, const char **response);
-
-__END_DECLS
-
-#endif
diff --git a/fastbootd/vendor_trigger_default.c b/fastbootd/vendor_trigger_default.c
deleted file mode 100644
index 3627024..0000000
--- a/fastbootd/vendor_trigger_default.c
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (c) 2009-2013, Google Inc.
- * 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <stdlib.h>
-#include <cutils/klog.h>
-#include <vendor_trigger.h>
-
-static const int version = 1;
-
-int trigger_init(void) {
-    klog_init();
-    klog_set_level(7);
-    return 0;
-}
-
-int trigger_check_version(const int fastboot_version, int *libversion) {
-    KLOG_DEBUG("fastbootd", "%s: %d (%d)", __func__, fastboot_version, version);
-    *libversion = version;
-    return !(fastboot_version == version);
-}
-
-int trigger_gpt_layout(struct GPT_content *table) {
-    KLOG_DEBUG("fastbootd", "%s: %p", __func__, table);
-    return 0;
-}
-
-int trigger_oem_cmd(const char *arg, const char **response) {
-    KLOG_DEBUG("fastbootd", "%s: %s", __func__, arg);
-    return 0;
-}
diff --git a/fs_mgr/fs_mgr.c b/fs_mgr/fs_mgr.c
index ad02922..8b2d439 100644
--- a/fs_mgr/fs_mgr.c
+++ b/fs_mgr/fs_mgr.c
@@ -28,6 +28,9 @@
 #include <libgen.h>
 #include <time.h>
 #include <sys/swap.h>
+#include <dirent.h>
+#include <ext4.h>
+#include <ext4_sb.h>
 
 #include <linux/loop.h>
 #include <private/android_filesystem_config.h>
@@ -116,7 +119,17 @@
         ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
         INFO("%s(): mount(%s,%s,%s)=%d\n", __func__, blk_device, target, fs_type, ret);
         if (!ret) {
-            umount(target);
+            int i;
+            for (i = 0; i < 5; i++) {
+                // Try to umount 5 times before continuing on.
+                // Should we try rebooting if all attempts fail?
+                int result = umount(target);
+                if (result == 0) {
+                    break;
+                }
+                ERROR("%s(): umount(%s)=%d: %s\n", __func__, target, result, strerror(errno));
+                sleep(1);
+            }
         }
 
         /*
@@ -335,6 +348,83 @@
     return 0;
 }
 
+static int translate_ext_labels(struct fstab_rec *rec)
+{
+    DIR *blockdir = NULL;
+    struct dirent *ent;
+    char *label;
+    size_t label_len;
+    int ret = -1;
+
+    if (strncmp(rec->blk_device, "LABEL=", 6))
+        return 0;
+
+    label = rec->blk_device + 6;
+    label_len = strlen(label);
+
+    if (label_len > 16) {
+        ERROR("FS label is longer than allowed by filesystem\n");
+        goto out;
+    }
+
+
+    blockdir = opendir("/dev/block");
+    if (!blockdir) {
+        ERROR("couldn't open /dev/block\n");
+        goto out;
+    }
+
+    while ((ent = readdir(blockdir))) {
+        int fd;
+        char super_buf[1024];
+        struct ext4_super_block *sb;
+
+        if (ent->d_type != DT_BLK)
+            continue;
+
+        fd = openat(dirfd(blockdir), ent->d_name, O_RDONLY);
+        if (fd < 0) {
+            ERROR("Cannot open block device /dev/block/%s\n", ent->d_name);
+            goto out;
+        }
+
+        if (TEMP_FAILURE_RETRY(lseek(fd, 1024, SEEK_SET)) < 0 ||
+            TEMP_FAILURE_RETRY(read(fd, super_buf, 1024)) != 1024) {
+            /* Probably a loopback device or something else without a readable
+             * superblock.
+             */
+            close(fd);
+            continue;
+        }
+
+        sb = (struct ext4_super_block *)super_buf;
+        if (sb->s_magic != EXT4_SUPER_MAGIC) {
+            INFO("/dev/block/%s not ext{234}\n", ent->d_name);
+            continue;
+        }
+
+        if (!strncmp(label, sb->s_volume_name, label_len)) {
+            char *new_blk_device;
+
+            if (asprintf(&new_blk_device, "/dev/block/%s", ent->d_name) < 0) {
+                ERROR("Could not allocate block device string\n");
+                goto out;
+            }
+
+            INFO("resolved label %s to %s\n", rec->blk_device, new_blk_device);
+
+            free(rec->blk_device);
+            rec->blk_device = new_blk_device;
+            ret = 0;
+            break;
+        }
+    }
+
+out:
+    closedir(blockdir);
+    return ret;
+}
+
 /* When multiple fstab records share the same mount_point, it will
  * try to mount each one in turn, and ignore any duplicates after a
  * first successful mount.
@@ -366,6 +456,17 @@
             continue;
         }
 
+        /* Translate LABEL= file system labels into block devices */
+        if (!strcmp(fstab->recs[i].fs_type, "ext2") ||
+            !strcmp(fstab->recs[i].fs_type, "ext3") ||
+            !strcmp(fstab->recs[i].fs_type, "ext4")) {
+            int tret = translate_ext_labels(&fstab->recs[i]);
+            if (tret < 0) {
+                ERROR("Could not translate label to block device\n");
+                continue;
+            }
+        }
+
         if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
             wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
         }
@@ -400,8 +501,8 @@
                         encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
                     }
                 } else {
-                    INFO("Could not umount %s - allow continue unencrypted\n",
-                         fstab->recs[attempted_idx].mount_point);
+                    WARNING("Could not umount %s (%s) - allow continue unencrypted\n",
+                            fstab->recs[attempted_idx].mount_point, strerror(errno));
                     continue;
                 }
             }
diff --git a/fs_mgr/fs_mgr_priv.h b/fs_mgr/fs_mgr_priv.h
index 34938fa..4ba6f92 100644
--- a/fs_mgr/fs_mgr_priv.h
+++ b/fs_mgr/fs_mgr_priv.h
@@ -21,6 +21,7 @@
 #include <fs_mgr.h>
 
 #define INFO(x...)    KLOG_INFO("fs_mgr", x)
+#define WARNING(x...) KLOG_WARNING("fs_mgr", x)
 #define ERROR(x...)   KLOG_ERROR("fs_mgr", x)
 
 #define CRYPTO_TMPFS_OPTIONS "size=256m,mode=0771,uid=1000,gid=1000"
diff --git a/fs_mgr/fs_mgr_verity.c b/fs_mgr/fs_mgr_verity.c
index a82db4e..f02781c 100644
--- a/fs_mgr/fs_mgr_verity.c
+++ b/fs_mgr/fs_mgr_verity.c
@@ -120,7 +120,9 @@
 {
     int data_device;
     struct ext4_super_block sb;
-    struct fs_info info = {0};
+    struct fs_info info;
+
+    info.len = 0;  /* Only len is set to 0 to ask the device for real size. */
 
     data_device = TEMP_FAILURE_RETRY(open(blk_device, O_RDONLY | O_CLOEXEC));
     if (data_device == -1) {
@@ -376,7 +378,7 @@
 
 int fs_mgr_setup_verity(struct fstab_rec *fstab) {
 
-    int retval = FS_MGR_SETUP_VERITY_FAIL;
+    int retval = -1;
     int fd = -1;
 
     char *verity_blk_name = 0;
@@ -407,8 +409,6 @@
         goto out;
     }
 
-    retval = FS_MGR_SETUP_VERITY_FAIL;
-
     // get the device mapper fd
     if ((fd = open("/dev/device-mapper", O_RDWR)) < 0) {
         ERROR("Error opening device mapper (%s)", strerror(errno));
diff --git a/healthd/Android.mk b/healthd/Android.mk
index 1d238b1..07e1d73 100644
--- a/healthd/Android.mk
+++ b/healthd/Android.mk
@@ -1,7 +1,5 @@
 # Copyright 2013 The Android Open Source Project
 
-ifneq ($(BUILD_TINY_ANDROID),true)
-
 LOCAL_PATH := $(call my-dir)
 
 include $(CLEAR_VARS)
@@ -77,5 +75,3 @@
 
 _add-charger-image :=
 _img_modules :=
-
-endif
diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp
index 9388ed0..7ea8250 100644
--- a/healthd/BatteryMonitor.cpp
+++ b/healthd/BatteryMonitor.cpp
@@ -389,7 +389,6 @@
             if (!strcmp(name, ".") || !strcmp(name, ".."))
                 continue;
 
-            char buf[20];
             // Look for "type" file in each subdirectory
             path.clear();
             path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
diff --git a/healthd/BatteryPropertiesRegistrar.cpp b/healthd/BatteryPropertiesRegistrar.cpp
index 74bcbfd..09667a1 100644
--- a/healthd/BatteryPropertiesRegistrar.cpp
+++ b/healthd/BatteryPropertiesRegistrar.cpp
@@ -48,13 +48,13 @@
         Mutex::Autolock _l(mRegistrationLock);
         // check whether this is a duplicate
         for (size_t i = 0; i < mListeners.size(); i++) {
-            if (mListeners[i]->asBinder() == listener->asBinder()) {
+            if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
                 return;
             }
         }
 
         mListeners.add(listener);
-        listener->asBinder()->linkToDeath(this);
+        IInterface::asBinder(listener)->linkToDeath(this);
     }
     healthd_battery_update();
 }
@@ -64,8 +64,8 @@
         return;
     Mutex::Autolock _l(mRegistrationLock);
     for (size_t i = 0; i < mListeners.size(); i++) {
-        if (mListeners[i]->asBinder() == listener->asBinder()) {
-            mListeners[i]->asBinder()->unlinkToDeath(this);
+        if (IInterface::asBinder(mListeners[i]) == IInterface::asBinder(listener)) {
+            IInterface::asBinder(mListeners[i])->unlinkToDeath(this);
             mListeners.removeAt(i);
             break;
         }
@@ -93,7 +93,7 @@
     Mutex::Autolock _l(mRegistrationLock);
 
     for (size_t i = 0; i < mListeners.size(); i++) {
-        if (mListeners[i]->asBinder() == who) {
+        if (IInterface::asBinder(mListeners[i]) == who) {
             mListeners.removeAt(i);
             break;
         }
diff --git a/healthd/healthd.cpp b/healthd/healthd.cpp
index f4171bd..1fee855 100644
--- a/healthd/healthd.cpp
+++ b/healthd/healthd.cpp
@@ -65,7 +65,6 @@
 #define MAX_EPOLL_EVENTS 40
 static int uevent_fd;
 static int wakealarm_fd;
-static int binder_fd;
 
 // -1 for no epoll timeout
 static int awake_poll_interval = -1;
diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp
index 5039649..9ed5944 100644
--- a/healthd/healthd_mode_charger.cpp
+++ b/healthd/healthd_mode_charger.cpp
@@ -344,7 +344,6 @@
 static void update_screen_state(struct charger *charger, int64_t now)
 {
     struct animation *batt_anim = charger->batt_anim;
-    int cur_frame;
     int disp_time;
 
     if (!batt_anim->run || now < charger->next_screen_transition)
@@ -387,7 +386,6 @@
 
     /* animation starting, set up the animation */
     if (batt_anim->cur_frame == 0) {
-        int ret;
 
         LOGV("[%" PRId64 "] animation starting\n", now);
         if (batt_prop && batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
@@ -510,7 +508,6 @@
 static void process_key(struct charger *charger, int code, int64_t now)
 {
     struct key_state *key = &charger->keys[code];
-    int64_t next_key_check;
 
     if (code == KEY_POWER) {
         if (key->down) {
@@ -583,7 +580,6 @@
 {
     struct charger *charger = &charger_state;
     int64_t now = curr_time_ms();
-    int ret;
 
     handle_input_state(charger, now);
     handle_power_supply_state(charger, now);
@@ -618,8 +614,6 @@
     int64_t now = curr_time_ms();
     int64_t next_event = INT64_MAX;
     int64_t timeout;
-    struct input_event ev;
-    int ret;
 
     LOGV("[%" PRId64 "] next screen: %" PRId64 " next key: %" PRId64 " next pwr: %" PRId64 "\n", now,
          charger->next_screen_transition, charger->next_key_check,
diff --git a/include/backtrace/Backtrace.h b/include/backtrace/Backtrace.h
index e07d322..e2d718b 100644
--- a/include/backtrace/Backtrace.h
+++ b/include/backtrace/Backtrace.h
@@ -39,7 +39,7 @@
   uintptr_t pc;           // The absolute pc.
   uintptr_t sp;           // The top of the stack.
   size_t stack_size;      // The size of the stack, zero indicate an unknown stack size.
-  const backtrace_map_t* map;   // The map associated with the given pc.
+  backtrace_map_t map;    // The map associated with the given pc.
   std::string func_name;  // The function name associated with this pc, NULL if not found.
   uintptr_t func_offset;  // pc relative to the start of the function, only valid if func_name is not NULL.
 };
@@ -78,8 +78,8 @@
   // If the string is empty, then no valid function name was found.
   virtual std::string GetFunctionName(uintptr_t pc, uintptr_t* offset);
 
-  // Find the map associated with the given pc.
-  virtual const backtrace_map_t* FindMap(uintptr_t pc);
+  // Fill in the map data associated with the given pc.
+  virtual void FillInMap(uintptr_t pc, backtrace_map_t* map);
 
   // Read the data at a specific address.
   virtual bool ReadWord(uintptr_t ptr, word_t* out_value) = 0;
diff --git a/include/backtrace/BacktraceMap.h b/include/backtrace/BacktraceMap.h
index 4ed23a8..da96307 100644
--- a/include/backtrace/BacktraceMap.h
+++ b/include/backtrace/BacktraceMap.h
@@ -33,6 +33,8 @@
 #include <string>
 
 struct backtrace_map_t {
+  backtrace_map_t(): start(0), end(0), flags(0) {}
+
   uintptr_t start;
   uintptr_t end;
   int flags;
@@ -48,15 +50,16 @@
 
   virtual ~BacktraceMap();
 
-  // Get the map data structure for the given address.
-  virtual const backtrace_map_t* Find(uintptr_t addr);
+  // Fill in the map data structure for the given address.
+  virtual void FillIn(uintptr_t addr, backtrace_map_t* map);
 
   // The flags returned are the same flags as used by the mmap call.
   // The values are PROT_*.
   int GetFlags(uintptr_t pc) {
-    const backtrace_map_t* map = Find(pc);
-    if (map) {
-      return map->flags;
+    backtrace_map_t map;
+    FillIn(pc, &map);
+    if (IsValid(map)) {
+      return map.flags;
     }
     return PROT_NONE;
   }
@@ -75,6 +78,10 @@
 
   virtual bool Build();
 
+  static inline bool IsValid(const backtrace_map_t& map) {
+    return map.end > 0;
+  }
+
 protected:
   BacktraceMap(pid_t pid);
 
diff --git a/include/cutils/android_reboot.h b/include/cutils/android_reboot.h
index 8c30e8e..85e1b7e 100644
--- a/include/cutils/android_reboot.h
+++ b/include/cutils/android_reboot.h
@@ -27,7 +27,7 @@
 /* Properties */
 #define ANDROID_RB_PROPERTY "sys.powerctl"
 
-int android_reboot(int cmd, int flags, char *arg);
+int android_reboot(int cmd, int flags, const char *arg);
 
 __END_DECLS
 
diff --git a/include/cutils/aref.h b/include/cutils/aref.h
index 460ac02..3bd36ea 100644
--- a/include/cutils/aref.h
+++ b/include/cutils/aref.h
@@ -20,11 +20,7 @@
 #include <stddef.h>
 #include <sys/cdefs.h>
 
-#ifdef ANDROID_SMP
-#include <cutils/atomic-inline.h>
-#else
 #include <cutils/atomic.h>
-#endif
 
 __BEGIN_DECLS
 
diff --git a/include/cutils/atomic-arm.h b/include/cutils/atomic-arm.h
deleted file mode 100644
index 6b031b6..0000000
--- a/include/cutils/atomic-arm.h
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_ARM_H
-#define ANDROID_CUTILS_ATOMIC_ARM_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE void android_compiler_barrier()
-{
-    __asm__ __volatile__ ("" : : : "memory");
-}
-
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier()
-{
-#if ANDROID_SMP == 0
-    android_compiler_barrier();
-#else
-    __asm__ __volatile__ ("dmb" : : : "memory");
-#endif
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
-{
-    int32_t value = *ptr;
-    android_memory_barrier();
-    return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_release_load(volatile const int32_t *ptr)
-{
-    android_memory_barrier();
-    return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
-    *ptr = value;
-    android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
-    android_memory_barrier();
-    *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_cas(int32_t old_value, int32_t new_value,
-                       volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    do {
-        __asm__ __volatile__ ("ldrex %0, [%3]\n"
-                              "mov %1, #0\n"
-                              "teq %0, %4\n"
-#ifdef __thumb2__
-                              "it eq\n"
-#endif
-                              "strexeq %1, %5, [%3]"
-                              : "=&r" (prev), "=&r" (status), "+m"(*ptr)
-                              : "r" (ptr), "Ir" (old_value), "r" (new_value)
-                              : "cc");
-    } while (__builtin_expect(status != 0, 0));
-    return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_acquire_cas(int32_t old_value, int32_t new_value,
-                               volatile int32_t *ptr)
-{
-    int status = android_atomic_cas(old_value, new_value, ptr);
-    android_memory_barrier();
-    return status;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_release_cas(int32_t old_value, int32_t new_value,
-                               volatile int32_t *ptr)
-{
-    android_memory_barrier();
-    return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
-    int32_t prev, tmp, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ ("ldrex %0, [%4]\n"
-                              "add %1, %0, %5\n"
-                              "strex %2, %1, [%4]"
-                              : "=&r" (prev), "=&r" (tmp),
-                                "=&r" (status), "+m" (*ptr)
-                              : "r" (ptr), "Ir" (increment)
-                              : "cc");
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t android_atomic_inc(volatile int32_t *addr)
-{
-    return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t android_atomic_dec(volatile int32_t *addr)
-{
-    return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, tmp, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ ("ldrex %0, [%4]\n"
-                              "and %1, %0, %5\n"
-                              "strex %2, %1, [%4]"
-                              : "=&r" (prev), "=&r" (tmp),
-                                "=&r" (status), "+m" (*ptr)
-                              : "r" (ptr), "Ir" (value)
-                              : "cc");
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, tmp, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ ("ldrex %0, [%4]\n"
-                              "orr %1, %0, %5\n"
-                              "strex %2, %1, [%4]"
-                              : "=&r" (prev), "=&r" (tmp),
-                                "=&r" (status), "+m" (*ptr)
-                              : "r" (ptr), "Ir" (value)
-                              : "cc");
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_ARM_H */
diff --git a/include/cutils/atomic-arm64.h b/include/cutils/atomic-arm64.h
deleted file mode 100644
index 7ae47d7..0000000
--- a/include/cutils/atomic-arm64.h
+++ /dev/null
@@ -1,156 +0,0 @@
-/*
- * Copyright (C) 2014 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.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_AARCH64_H
-#define ANDROID_CUTILS_ATOMIC_AARCH64_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-/*
-   TODOAArch64: Revisit the below functions and check for potential
-   optimizations using assembly code or otherwise.
-*/
-
-extern ANDROID_ATOMIC_INLINE
-void android_compiler_barrier(void)
-{
-    __asm__ __volatile__ ("" : : : "memory");
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_memory_barrier(void)
-{
-    __asm__ __volatile__ ("dmb ish" : : : "memory");
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
-{
-    int32_t value = *ptr;
-    android_memory_barrier();
-    return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_release_load(volatile const int32_t *ptr)
-{
-    android_memory_barrier();
-    return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
-    *ptr = value;
-    android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
-    android_memory_barrier();
-    *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_cas(int32_t old_value, int32_t new_value,
-                       volatile int32_t *ptr)
-{
-    return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_acquire_cas(int32_t old_value, int32_t new_value,
-                               volatile int32_t *ptr)
-{
-    int status = android_atomic_cas(old_value, new_value, ptr);
-    android_memory_barrier();
-    return status;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_release_cas(int32_t old_value, int32_t new_value,
-                               volatile int32_t *ptr)
-{
-    android_memory_barrier();
-    return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        prev = *ptr;
-        status = android_atomic_cas(prev, prev + increment, ptr);
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_inc(volatile int32_t *addr)
-{
-    return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_dec(volatile int32_t *addr)
-{
-    return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        prev = *ptr;
-        status = android_atomic_cas(prev, prev & value, ptr);
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        prev = *ptr;
-        status = android_atomic_cas(prev, prev | value, ptr);
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_AARCH64_H */
diff --git a/include/cutils/atomic-inline.h b/include/cutils/atomic-inline.h
deleted file mode 100644
index a31e913..0000000
--- a/include/cutils/atomic-inline.h
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_INLINE_H
-#define ANDROID_CUTILS_ATOMIC_INLINE_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * Inline declarations and macros for some special-purpose atomic
- * operations.  These are intended for rare circumstances where a
- * memory barrier needs to be issued inline rather than as a function
- * call.
- *
- * Most code should not use these.
- *
- * Anything that does include this file must set ANDROID_SMP to either
- * 0 or 1, indicating compilation for UP or SMP, respectively.
- *
- * Macros defined in this header:
- *
- * void ANDROID_MEMBAR_FULL(void)
- *   Full memory barrier.  Provides a compiler reordering barrier, and
- *   on SMP systems emits an appropriate instruction.
- */
-
-#if !defined(ANDROID_SMP)
-# error "Must define ANDROID_SMP before including atomic-inline.h"
-#endif
-
-#if defined(__aarch64__)
-#include <cutils/atomic-arm64.h>
-#elif defined(__arm__)
-#include <cutils/atomic-arm.h>
-#elif defined(__i386__)
-#include <cutils/atomic-x86.h>
-#elif defined(__x86_64__)
-#include <cutils/atomic-x86_64.h>
-#elif defined(__mips64)
-#include <cutils/atomic-mips64.h>
-#elif defined(__mips__)
-#include <cutils/atomic-mips.h>
-#else
-#error atomic operations are unsupported
-#endif
-
-#if ANDROID_SMP == 0
-#define ANDROID_MEMBAR_FULL android_compiler_barrier
-#else
-#define ANDROID_MEMBAR_FULL android_memory_barrier
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* ANDROID_CUTILS_ATOMIC_INLINE_H */
diff --git a/include/cutils/atomic-mips.h b/include/cutils/atomic-mips.h
deleted file mode 100644
index 5d4f097..0000000
--- a/include/cutils/atomic-mips.h
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_MIPS_H
-#define ANDROID_CUTILS_ATOMIC_MIPS_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void)
-{
-    __asm__ __volatile__ ("" : : : "memory");
-}
-
-#if ANDROID_SMP == 0
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
-    android_compiler_barrier();
-}
-#else
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
-    __asm__ __volatile__ ("sync" : : : "memory");
-}
-#endif
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_acquire_load(volatile const int32_t *ptr)
-{
-    int32_t value = *ptr;
-    android_memory_barrier();
-    return value;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_release_load(volatile const int32_t *ptr)
-{
-    android_memory_barrier();
-    return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE void
-android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
-    *ptr = value;
-    android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE void
-android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
-    android_memory_barrier();
-    *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    do {
-        __asm__ __volatile__ (
-            "    ll     %[prev], (%[ptr])\n"
-            "    li     %[status], 1\n"
-            "    bne    %[prev], %[old], 9f\n"
-            "    move   %[status], %[new_value]\n"
-            "    sc     %[status], (%[ptr])\n"
-            "9:\n"
-            : [prev] "=&r" (prev), [status] "=&r" (status)
-            : [ptr] "r" (ptr), [old] "r" (old_value), [new_value] "r" (new_value)
-            );
-    } while (__builtin_expect(status == 0, 0));
-    return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_acquire_cas(int32_t old_value,
-                           int32_t new_value,
-                           volatile int32_t *ptr)
-{
-    int status = android_atomic_cas(old_value, new_value, ptr);
-    android_memory_barrier();
-    return status;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_release_cas(int32_t old_value,
-                           int32_t new_value,
-                           volatile int32_t *ptr)
-{
-    android_memory_barrier();
-    return android_atomic_cas(old_value, new_value, ptr);
-}
-
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ (
-        "    ll    %[prev], (%[ptr])\n"
-        "    addu  %[status], %[prev], %[inc]\n"
-        "    sc    %[status], (%[ptr])\n"
-        :  [status] "=&r" (status), [prev] "=&r" (prev)
-        :  [ptr] "r" (ptr), [inc] "Ir" (increment)
-        );
-    } while (__builtin_expect(status == 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_inc(volatile int32_t *addr)
-{
-    return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_dec(volatile int32_t *addr)
-{
-    return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ (
-        "    ll    %[prev], (%[ptr])\n"
-        "    and   %[status], %[prev], %[value]\n"
-        "    sc    %[status], (%[ptr])\n"
-        : [prev] "=&r" (prev), [status] "=&r" (status)
-        : [ptr] "r" (ptr), [value] "Ir" (value)
-            );
-    } while (__builtin_expect(status == 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ (
-        "    ll    %[prev], (%[ptr])\n"
-        "    or    %[status], %[prev], %[value]\n"
-        "    sc    %[status], (%[ptr])\n"
-        : [prev] "=&r" (prev), [status] "=&r" (status)
-        : [ptr] "r" (ptr), [value] "Ir" (value)
-            );
-    } while (__builtin_expect(status == 0, 0));
-    return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_MIPS_H */
diff --git a/include/cutils/atomic-mips64.h b/include/cutils/atomic-mips64.h
deleted file mode 100644
index 9d8f65e..0000000
--- a/include/cutils/atomic-mips64.h
+++ /dev/null
@@ -1,166 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_MIPS64_H
-#define ANDROID_CUTILS_ATOMIC_MIPS64_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void)
-{
-    __asm__ __volatile__ ("" : : : "memory");
-}
-
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
-    __asm__ __volatile__ ("sync" : : : "memory");
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
-{
-    int32_t value = *ptr;
-    android_memory_barrier();
-    return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_release_load(volatile const int32_t *ptr)
-{
-    android_memory_barrier();
-    return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
-    *ptr = value;
-    android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
-    android_memory_barrier();
-    *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    do {
-        __asm__ __volatile__ (
-            "    ll     %[prev], (%[ptr])\n"
-            "    li     %[status], 1\n"
-            "    bne    %[prev], %[old], 9f\n"
-            "    move   %[status], %[new_value]\n"
-            "    sc     %[status], (%[ptr])\n"
-            "9:\n"
-            : [prev] "=&r" (prev), [status] "=&r" (status)
-            : [ptr] "r" (ptr), [old] "r" (old_value), [new_value] "r" (new_value)
-            );
-    } while (__builtin_expect(status == 0, 0));
-    return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_acquire_cas(int32_t old_value,
-                           int32_t new_value,
-                           volatile int32_t *ptr)
-{
-    int status = android_atomic_cas(old_value, new_value, ptr);
-    android_memory_barrier();
-    return status;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_release_cas(int32_t old_value,
-                           int32_t new_value,
-                           volatile int32_t *ptr)
-{
-    android_memory_barrier();
-    return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ (
-        "    ll    %[prev], (%[ptr])\n"
-        "    addu  %[status], %[prev], %[inc]\n"
-        "    sc    %[status], (%[ptr])\n"
-        :  [status] "=&r" (status), [prev] "=&r" (prev)
-        :  [ptr] "r" (ptr), [inc] "Ir" (increment)
-        );
-    } while (__builtin_expect(status == 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_inc(volatile int32_t *addr)
-{
-    return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_dec(volatile int32_t *addr)
-{
-    return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ (
-        "    ll    %[prev], (%[ptr])\n"
-        "    and   %[status], %[prev], %[value]\n"
-        "    sc    %[status], (%[ptr])\n"
-        : [prev] "=&r" (prev), [status] "=&r" (status)
-        : [ptr] "r" (ptr), [value] "Ir" (value)
-            );
-    } while (__builtin_expect(status == 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    android_memory_barrier();
-    do {
-        __asm__ __volatile__ (
-        "    ll    %[prev], (%[ptr])\n"
-        "    or    %[status], %[prev], %[value]\n"
-        "    sc    %[status], (%[ptr])\n"
-        : [prev] "=&r" (prev), [status] "=&r" (status)
-        : [ptr] "r" (ptr), [value] "Ir" (value)
-            );
-    } while (__builtin_expect(status == 0, 0));
-    return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_MIPS_H */
diff --git a/include/cutils/atomic-x86.h b/include/cutils/atomic-x86.h
deleted file mode 100644
index 06bf1a3..0000000
--- a/include/cutils/atomic-x86.h
+++ /dev/null
@@ -1,145 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_X86_H
-#define ANDROID_CUTILS_ATOMIC_X86_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE void android_compiler_barrier(void)
-{
-    __asm__ __volatile__ ("" : : : "memory");
-}
-
-#if ANDROID_SMP == 0
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
-    android_compiler_barrier();
-}
-#else
-extern ANDROID_ATOMIC_INLINE void android_memory_barrier(void)
-{
-    __asm__ __volatile__ ("mfence" : : : "memory");
-}
-#endif
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_acquire_load(volatile const int32_t *ptr)
-{
-    int32_t value = *ptr;
-    android_compiler_barrier();
-    return value;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_release_load(volatile const int32_t *ptr)
-{
-    android_memory_barrier();
-    return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE void
-android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
-    *ptr = value;
-    android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE void
-android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
-    android_compiler_barrier();
-    *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_cas(int32_t old_value, int32_t new_value, volatile int32_t *ptr)
-{
-    int32_t prev;
-    __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
-                          : "=a" (prev)
-                          : "q" (new_value), "m" (*ptr), "0" (old_value)
-                          : "memory");
-    return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_acquire_cas(int32_t old_value,
-                           int32_t new_value,
-                           volatile int32_t *ptr)
-{
-    /* Loads are not reordered with other loads. */
-    return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE int
-android_atomic_release_cas(int32_t old_value,
-                           int32_t new_value,
-                           volatile int32_t *ptr)
-{
-    /* Stores are not reordered with other stores. */
-    return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
-    __asm__ __volatile__ ("lock; xaddl %0, %1"
-                          : "+r" (increment), "+m" (*ptr)
-                          : : "memory");
-    /* increment now holds the old value of *ptr */
-    return increment;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_inc(volatile int32_t *addr)
-{
-    return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_dec(volatile int32_t *addr)
-{
-    return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    do {
-        prev = *ptr;
-        status = android_atomic_cas(prev, prev & value, ptr);
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE int32_t
-android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    do {
-        prev = *ptr;
-        status = android_atomic_cas(prev, prev | value, ptr);
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_X86_H */
diff --git a/include/cutils/atomic-x86_64.h b/include/cutils/atomic-x86_64.h
deleted file mode 100644
index 99cb070..0000000
--- a/include/cutils/atomic-x86_64.h
+++ /dev/null
@@ -1,151 +0,0 @@
-/*
- * Copyright (C) 2014 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.
- */
-
-#ifndef ANDROID_CUTILS_ATOMIC_X86_64_H
-#define ANDROID_CUTILS_ATOMIC_X86_64_H
-
-#include <stdint.h>
-
-#ifndef ANDROID_ATOMIC_INLINE
-#define ANDROID_ATOMIC_INLINE inline __attribute__((always_inline))
-#endif
-
-extern ANDROID_ATOMIC_INLINE
-void android_compiler_barrier(void)
-{
-    __asm__ __volatile__ ("" : : : "memory");
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_memory_barrier(void)
-{
-    __asm__ __volatile__ ("mfence" : : : "memory");
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_acquire_load(volatile const int32_t *ptr)
-{
-    int32_t value = *ptr;
-    android_compiler_barrier();
-    return value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_release_load(volatile const int32_t *ptr)
-{
-    android_memory_barrier();
-    return *ptr;
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_acquire_store(int32_t value, volatile int32_t *ptr)
-{
-    *ptr = value;
-    android_memory_barrier();
-}
-
-extern ANDROID_ATOMIC_INLINE
-void android_atomic_release_store(int32_t value, volatile int32_t *ptr)
-{
-    android_compiler_barrier();
-    *ptr = value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_cas(int32_t old_value, int32_t new_value,
-                       volatile int32_t *ptr)
-{
-    int32_t prev;
-    __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
-                          : "=a" (prev)
-                          : "q" (new_value), "m" (*ptr), "0" (old_value)
-                          : "memory");
-    return prev != old_value;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_acquire_cas(int32_t old_value, int32_t new_value,
-                               volatile int32_t *ptr)
-{
-    /* Loads are not reordered with other loads. */
-    return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int android_atomic_release_cas(int32_t old_value, int32_t new_value,
-                               volatile int32_t *ptr)
-{
-    /* Stores are not reordered with other stores. */
-    return android_atomic_cas(old_value, new_value, ptr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_add(int32_t increment, volatile int32_t *ptr)
-{
-    __asm__ __volatile__ ("lock; xaddl %0, %1"
-                          : "+r" (increment), "+m" (*ptr)
-                          : : "memory");
-    /* increment now holds the old value of *ptr */
-    return increment;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_inc(volatile int32_t *addr)
-{
-    return android_atomic_add(1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_dec(volatile int32_t *addr)
-{
-    return android_atomic_add(-1, addr);
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_and(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    do {
-        prev = *ptr;
-        status = android_atomic_cas(prev, prev & value, ptr);
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-extern ANDROID_ATOMIC_INLINE
-int32_t android_atomic_or(int32_t value, volatile int32_t *ptr)
-{
-    int32_t prev, status;
-    do {
-        prev = *ptr;
-        status = android_atomic_cas(prev, prev | value, ptr);
-    } while (__builtin_expect(status != 0, 0));
-    return prev;
-}
-
-#endif /* ANDROID_CUTILS_ATOMIC_X86_64_H */
diff --git a/include/cutils/atomic.h b/include/cutils/atomic.h
index 79409a7..ded972a 100644
--- a/include/cutils/atomic.h
+++ b/include/cutils/atomic.h
@@ -19,9 +19,10 @@
 
 #include <stdint.h>
 #include <sys/types.h>
+#include <stdatomic.h>
 
-#ifdef __cplusplus
-extern "C" {
+#ifndef ANDROID_ATOMIC_INLINE
+#define ANDROID_ATOMIC_INLINE static inline
 #endif
 
 /*
@@ -77,11 +78,41 @@
  * These have the same characteristics (e.g. what happens on overflow)
  * as the equivalent non-atomic C operations.
  */
-int32_t android_atomic_inc(volatile int32_t* addr);
-int32_t android_atomic_dec(volatile int32_t* addr);
-int32_t android_atomic_add(int32_t value, volatile int32_t* addr);
-int32_t android_atomic_and(int32_t value, volatile int32_t* addr);
-int32_t android_atomic_or(int32_t value, volatile int32_t* addr);
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_inc(volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+        /* Int32_t, if it exists, is the same as int_least32_t. */
+    return atomic_fetch_add_explicit(a, 1, memory_order_release);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_dec(volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    return atomic_fetch_sub_explicit(a, 1, memory_order_release);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_add(int32_t value, volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    return atomic_fetch_add_explicit(a, value, memory_order_release);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_and(int32_t value, volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    return atomic_fetch_and_explicit(a, value, memory_order_release);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_or(int32_t value, volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    return atomic_fetch_or_explicit(a, value, memory_order_release);
+}
 
 /*
  * Perform an atomic load with "acquire" or "release" ordering.
@@ -96,29 +127,53 @@
  * this comment, you are in the vast majority, and should not be
  * using release loads or replacing them with anything other than
  * locks or default sequentially consistent atomics.
- *
- * This is only necessary if you need the memory barrier.  A 32-bit read
- * from a 32-bit aligned address is atomic on all supported platforms.
  */
-int32_t android_atomic_acquire_load(volatile const int32_t* addr);
-int32_t android_atomic_release_load(volatile const int32_t* addr);
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_acquire_load(volatile const int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    return atomic_load_explicit(a, memory_order_acquire);
+}
+
+ANDROID_ATOMIC_INLINE
+int32_t android_atomic_release_load(volatile const int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    atomic_thread_fence(memory_order_seq_cst);
+    /* Any reasonable clients of this interface would probably prefer   */
+    /* something weaker.  But some remaining clients seem to be         */
+    /* abusing this API in strange ways, e.g. by using it as a fence.   */
+    /* Thus we are conservative until we can get rid of remaining       */
+    /* clients (and this function).                                     */
+    return atomic_load_explicit(a, memory_order_relaxed);
+}
 
 /*
  * Perform an atomic store with "acquire" or "release" ordering.
  *
- * Note that the notion of a "acquire" ordering for a store does not
+ * Note that the notion of an "acquire" ordering for a store does not
  * really fit into the C11 or C++11 memory model.  The extra ordering
  * is normally observable only by code using memory_order_relaxed
  * atomics, or data races.  In the rare cases in which such ordering
  * is called for, use memory_order_relaxed atomics and a trailing
  * atomic_thread_fence (typically with memory_order_release,
  * not memory_order_acquire!) instead.
- *
- * This is only necessary if you need the memory barrier.  A 32-bit write
- * to a 32-bit aligned address is atomic on all supported platforms.
  */
-void android_atomic_acquire_store(int32_t value, volatile int32_t* addr);
-void android_atomic_release_store(int32_t value, volatile int32_t* addr);
+ANDROID_ATOMIC_INLINE
+void android_atomic_acquire_store(int32_t value, volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    atomic_store_explicit(a, value, memory_order_relaxed);
+    atomic_thread_fence(memory_order_seq_cst);
+    /* Again overly conservative to accomodate weird clients.   */
+}
+
+ANDROID_ATOMIC_INLINE
+void android_atomic_release_store(int32_t value, volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    atomic_store_explicit(a, value, memory_order_release);
+}
 
 /*
  * Compare-and-set operation with "acquire" or "release" ordering.
@@ -132,10 +187,44 @@
  * Implementations that use the release CAS in a loop may be less efficient
  * than possible, because we re-issue the memory barrier on each iteration.
  */
+ANDROID_ATOMIC_INLINE
 int android_atomic_acquire_cas(int32_t oldvalue, int32_t newvalue,
-        volatile int32_t* addr);
+                           volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    return (int)(!atomic_compare_exchange_strong_explicit(
+                                          a, &oldvalue, newvalue,
+                                          memory_order_acquire,
+                                          memory_order_acquire));
+}
+
+ANDROID_ATOMIC_INLINE
 int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue,
-        volatile int32_t* addr);
+                               volatile int32_t* addr)
+{
+    volatile atomic_int_least32_t* a = (volatile atomic_int_least32_t*)addr;
+    return (int)(!atomic_compare_exchange_strong_explicit(
+                                          a, &oldvalue, newvalue,
+                                          memory_order_release,
+                                          memory_order_relaxed));
+}
+
+/*
+ * Fence primitives.
+ */
+ANDROID_ATOMIC_INLINE
+void android_compiler_barrier(void)
+{
+    __asm__ __volatile__ ("" : : : "memory");
+    /* Could probably also be:                          */
+    /* atomic_signal_fence(memory_order_seq_cst);       */
+}
+
+ANDROID_ATOMIC_INLINE
+void android_memory_barrier(void)
+{
+    atomic_thread_fence(memory_order_seq_cst);
+}
 
 /*
  * Aliases for code using an older version of this header.  These are now
@@ -145,8 +234,4 @@
 #define android_atomic_write android_atomic_release_store
 #define android_atomic_cmpxchg android_atomic_release_cas
 
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
 #endif // ANDROID_CUTILS_ATOMIC_H
diff --git a/include/cutils/cpu_info.h b/include/cutils/cpu_info.h
deleted file mode 100644
index 78c1884..0000000
--- a/include/cutils/cpu_info.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __CUTILS_CPU_INFO_H
-#define __CUTILS_CPU_INFO_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* returns a string contiaining an ASCII representation of the CPU serial number, 
-** or NULL if cpu info not available.
-** The string is a static variable, so don't call free() on it.
-*/
-extern const char* get_cpu_serial_number(void);
-    
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __CUTILS_CPU_INFO_H */ 
diff --git a/include/cutils/debugger.h b/include/cutils/debugger.h
index bae687d..285e1af 100644
--- a/include/cutils/debugger.h
+++ b/include/cutils/debugger.h
@@ -17,20 +17,14 @@
 #ifndef __CUTILS_DEBUGGER_H
 #define __CUTILS_DEBUGGER_H
 
+#include <sys/cdefs.h>
 #include <sys/types.h>
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+__BEGIN_DECLS
 
-#define DEBUGGER32_SOCKET_NAME "android:debuggerd"
-#define DEBUGGER64_SOCKET_NAME "android:debuggerd64"
-
-#if defined(__LP64__)
-#define DEBUGGER_SOCKET_NAME DEBUGGER64_SOCKET_NAME
-#else
-#define DEBUGGER_SOCKET_NAME DEBUGGER32_SOCKET_NAME
-#endif
+#define DEBUGGER_SOCKET_NAME "android:debuggerd"
+#define DEBUGGER32_SOCKET_NAME "android:debuggerd32"
+#define DEBUGGER64_SOCKET_NAME DEBUGGER_SOCKET_NAME
 
 typedef enum {
     // dump a crash
@@ -41,23 +35,17 @@
     DEBUGGER_ACTION_DUMP_BACKTRACE,
 } debugger_action_t;
 
-typedef struct {
-    debugger_action_t action;
+// Make sure that all values have a fixed size so that this structure
+// is the same for 32 bit and 64 bit processes.
+// NOTE: Any changes to this structure must also be reflected in
+//       bionic/linker/debugger.cpp.
+typedef struct __attribute__((packed)) {
+    int32_t action;
     pid_t tid;
-    uintptr_t abort_msg_address;
+    uint64_t abort_msg_address;
     int32_t original_si_code;
 } debugger_msg_t;
 
-#if defined(__LP64__)
-// For a 64 bit process to contact the 32 bit debuggerd.
-typedef struct {
-    debugger_action_t action;
-    pid_t tid;
-    uint32_t abort_msg_address;
-    int32_t original_si_code;
-} debugger32_msg_t;
-#endif
-
 /* Dumps a process backtrace, registers, and stack to a tombstone file (requires root).
  * Stores the tombstone path in the provided buffer.
  * Returns 0 on success, -1 on error.
@@ -84,8 +72,6 @@
  */
 int dump_backtrace_to_file_timeout(pid_t tid, int fd, int timeout_secs);
 
-#ifdef __cplusplus
-}
-#endif
+__END_DECLS
 
 #endif /* __CUTILS_DEBUGGER_H */
diff --git a/include/cutils/open_memstream.h b/include/cutils/open_memstream.h
index b7998be..c1a81eb 100644
--- a/include/cutils/open_memstream.h
+++ b/include/cutils/open_memstream.h
@@ -19,7 +19,7 @@
 
 #include <stdio.h>
 
-#ifndef HAVE_OPEN_MEMSTREAM
+#if defined(__APPLE__)
 
 #ifdef __cplusplus
 extern "C" {
@@ -31,6 +31,6 @@
 }
 #endif
 
-#endif /*!HAVE_OPEN_MEMSTREAM*/
+#endif /* __APPLE__ */
 
 #endif /*__CUTILS_OPEN_MEMSTREAM_H__*/
diff --git a/include/cutils/properties.h b/include/cutils/properties.h
index 798db8b..24aa224 100644
--- a/include/cutils/properties.h
+++ b/include/cutils/properties.h
@@ -126,22 +126,6 @@
 
 #endif
 
-#ifdef HAVE_SYSTEM_PROPERTY_SERVER
-/*
- * We have an external property server instead of built-in libc support.
- * Used by the simulator.
- */
-#define SYSTEM_PROPERTY_PIPE_NAME       "/tmp/android-sysprop"
-
-enum {
-    kSystemPropertyUnknown = 0,
-    kSystemPropertyGet,
-    kSystemPropertySet,
-    kSystemPropertyList
-};
-#endif /*HAVE_SYSTEM_PROPERTY_SERVER*/
-
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/cutils/sockets.h b/include/cutils/sockets.h
index daf43ec..c47588c 100644
--- a/include/cutils/sockets.h
+++ b/include/cutils/sockets.h
@@ -25,7 +25,7 @@
 #ifdef HAVE_WINSOCK
 #include <winsock2.h>
 typedef int  socklen_t;
-#elif HAVE_SYS_SOCKET_H
+#else
 #include <sys/socket.h>
 #endif
 
diff --git a/include/cutils/threads.h b/include/cutils/threads.h
index acf8f48..ade9a0c 100644
--- a/include/cutils/threads.h
+++ b/include/cutils/threads.h
@@ -29,7 +29,7 @@
 /***********************************************************************/
 /***********************************************************************/
 
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
 
 #include  <pthread.h>
 
@@ -42,7 +42,7 @@
 
 #define  THREAD_STORE_INITIALIZER  { PTHREAD_MUTEX_INITIALIZER, 0, 0 }
 
-#elif defined HAVE_WIN32_THREADS
+#else // !defined(_WIN32)
 
 #include <windows.h>
 
@@ -56,15 +56,13 @@
 
 #define  THREAD_STORE_INITIALIZER  { 0, 0, 0, {0, 0, 0, 0, 0, 0} }
 
-#else
-#  error  "no thread_store_t implementation for your platform !!"
-#endif
+#endif // !defined(_WIN32)
 
 typedef void  (*thread_store_destruct_t)(void*  value);
 
 extern void*  thread_store_get(thread_store_t*  store);
 
-extern void   thread_store_set(thread_store_t*          store, 
+extern void   thread_store_set(thread_store_t*          store,
                                void*                    value,
                                thread_store_destruct_t  destroy);
 
@@ -76,7 +74,7 @@
 /***********************************************************************/
 /***********************************************************************/
 
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
 
 typedef pthread_mutex_t   mutex_t;
 
@@ -98,10 +96,10 @@
 {
     pthread_mutex_destroy(lock);
 }
-#endif
 
-#ifdef HAVE_WIN32_THREADS
-typedef struct { 
+#else // !defined(_WIN32)
+
+typedef struct {
     int                init;
     CRITICAL_SECTION   lock[1];
 } mutex_t;
@@ -134,10 +132,10 @@
 {
     if (lock->init) {
         lock->init = 0;
-        DeleteCriticalSection(lock->lock); 
+        DeleteCriticalSection(lock->lock);
     }
 }
-#endif
+#endif // !defined(_WIN32)
 
 #ifdef __cplusplus
 }
diff --git a/include/cutils/trace.h b/include/cutils/trace.h
index fd24561..59ff6c1 100644
--- a/include/cutils/trace.h
+++ b/include/cutils/trace.h
@@ -25,12 +25,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <cutils/compiler.h>
-#ifdef ANDROID_SMP
-#include <cutils/atomic-inline.h>
-#else
 #include <cutils/atomic.h>
-#endif
+#include <cutils/compiler.h>
 
 __BEGIN_DECLS
 
@@ -86,13 +82,6 @@
 
 #ifdef HAVE_ANDROID_OS
 /**
- * Maximum size of a message that can be logged to the trace buffer.
- * Note this message includes a tag, the pid, and the string given as the name.
- * Names should be kept short to get the most use of the trace buffer.
- */
-#define ATRACE_MESSAGE_LENGTH 1024
-
-/**
  * Opens the trace file for writing and reads the property for initial tags.
  * The atrace.tags.enableflags property sets the tags to trace.
  * This function should not be explicitly called, the first call to any normal
@@ -184,11 +173,8 @@
 static inline void atrace_begin(uint64_t tag, const char* name)
 {
     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
-        char buf[ATRACE_MESSAGE_LENGTH];
-        size_t len;
-
-        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
-        write(atrace_marker_fd, buf, len);
+        void atrace_begin_body(const char*);
+        atrace_begin_body(name);
     }
 }
 
@@ -218,12 +204,8 @@
         int32_t cookie)
 {
     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
-        char buf[ATRACE_MESSAGE_LENGTH];
-        size_t len;
-
-        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%" PRId32,
-                getpid(), name, cookie);
-        write(atrace_marker_fd, buf, len);
+        void atrace_async_begin_body(const char*, int32_t);
+        atrace_async_begin_body(name, cookie);
     }
 }
 
@@ -232,20 +214,14 @@
  * This should have a corresponding ATRACE_ASYNC_BEGIN.
  */
 #define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
-static inline void atrace_async_end(uint64_t tag, const char* name,
-        int32_t cookie)
+static inline void atrace_async_end(uint64_t tag, const char* name, int32_t cookie)
 {
     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
-        char buf[ATRACE_MESSAGE_LENGTH];
-        size_t len;
-
-        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%" PRId32,
-                getpid(), name, cookie);
-        write(atrace_marker_fd, buf, len);
+        void atrace_async_end_body(const char*, int32_t);
+        atrace_async_end_body(name, cookie);
     }
 }
 
-
 /**
  * Traces an integer counter value.  name is used to identify the counter.
  * This can be used to track how a value changes over time.
@@ -254,12 +230,8 @@
 static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
 {
     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
-        char buf[ATRACE_MESSAGE_LENGTH];
-        size_t len;
-
-        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId32,
-                getpid(), name, value);
-        write(atrace_marker_fd, buf, len);
+        void atrace_int_body(const char*, int32_t);
+        atrace_int_body(name, value);
     }
 }
 
@@ -271,12 +243,8 @@
 static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)
 {
     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
-        char buf[ATRACE_MESSAGE_LENGTH];
-        size_t len;
-
-        len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId64,
-                getpid(), name, value);
-        write(atrace_marker_fd, buf, len);
+        void atrace_int64_body(const char*, int64_t);
+        atrace_int64_body(name, value);
     }
 }
 
diff --git a/include/cutils/uevent.h b/include/cutils/uevent.h
index 4cca7e5..da1c2aa 100644
--- a/include/cutils/uevent.h
+++ b/include/cutils/uevent.h
@@ -27,6 +27,7 @@
 int uevent_open_socket(int buf_sz, bool passcred);
 ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length);
 ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer, size_t length, uid_t *uid);
+ssize_t uevent_kernel_recv(int socket, void *buffer, size_t length, bool require_group, uid_t *uid);
 
 #ifdef __cplusplus
 }
diff --git a/include/log/log.h b/include/log/log.h
index ace12d6..3d86533 100644
--- a/include/log/log.h
+++ b/include/log/log.h
@@ -28,14 +28,12 @@
 #ifndef _LIBS_LOG_LOG_H
 #define _LIBS_LOG_LOG_H
 
-#include <sys/types.h>
-#ifdef HAVE_PTHREADS
-#include <pthread.h>
-#endif
 #include <stdarg.h>
 #include <stdio.h>
+#include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+
 #include <log/logd.h>
 #include <log/uio.h>
 
diff --git a/include/log/log_read.h b/include/log/log_read.h
index 946711a..1b70aff 100644
--- a/include/log/log_read.h
+++ b/include/log/log_read.h
@@ -100,6 +100,12 @@
         log_time local(*this);
         return local -= T;
     }
+    log_time operator+= (const timespec &T);
+    log_time operator+ (const timespec &T) const
+    {
+        log_time local(*this);
+        return local += T;
+    }
 
     // log_time
     bool operator== (const log_time &T) const
@@ -134,6 +140,12 @@
         log_time local(*this);
         return local -= T;
     }
+    log_time operator+= (const log_time &T);
+    log_time operator+ (const log_time &T) const
+    {
+        log_time local(*this);
+        return local += T;
+    }
 
     uint64_t nsec() const
     {
diff --git a/include/log/logd.h b/include/log/logd.h
index 2e6f220..0fe515f 100644
--- a/include/log/logd.h
+++ b/include/log/logd.h
@@ -23,16 +23,17 @@
 #include <android/log.h>
 
 /* the rest is only used internally by the system */
-#include <time.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <stdint.h>
-#include <sys/types.h>
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
 #include <pthread.h>
 #endif
-#include <log/uio.h>
 #include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <log/uio.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/include/log/logprint.h b/include/log/logprint.h
index 481c96e..1e42b47 100644
--- a/include/log/logprint.h
+++ b/include/log/logprint.h
@@ -36,6 +36,7 @@
     FORMAT_TIME,
     FORMAT_THREADTIME,
     FORMAT_LONG,
+    FORMAT_COLOR,
 } AndroidLogPrintFormat;
 
 typedef struct AndroidLogFormat_t AndroidLogFormat;
diff --git a/include/log/uio.h b/include/log/uio.h
index a71f515..7059da5 100644
--- a/include/log/uio.h
+++ b/include/log/uio.h
@@ -14,20 +14,23 @@
  * limitations under the License.
  */
 
-//
-// implementation of sys/uio.h for platforms that don't have it (Win32)
-//
 #ifndef _LIBS_CUTILS_UIO_H
 #define _LIBS_CUTILS_UIO_H
 
-#ifdef HAVE_SYS_UIO_H
+#if !defined(_WIN32)
+
 #include <sys/uio.h>
+
 #else
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+//
+// Implementation of sys/uio.h for Win32.
+//
+
 #include <stddef.h>
 
 struct iovec {
@@ -42,7 +45,7 @@
 }
 #endif
 
-#endif /* !HAVE_SYS_UIO_H */
+#endif
 
 #endif /* _LIBS_UTILS_UIO_H */
 
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 2f528b9..101cacd 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -83,6 +83,11 @@
 #define AID_CACHE         2001  /* cache access */
 #define AID_DIAG          2002  /* access to diagnostic resources */
 
+/* The range 2900-2999 is reserved for OEM, and must never be
+ * used here */
+#define AID_OEM_RESERVED_START 2900
+#define AID_OEM_RESERVED_END   2999
+
 /* The 3000 series are intended for use as supplemental group id's only.
  * They indicate special Android capabilities that the kernel is aware of. */
 #define AID_NET_BT_ADMIN  3001  /* bluetooth: create any socket */
@@ -227,7 +232,6 @@
     { 00550, AID_ROOT,      AID_SHELL,     0, "system/etc/init.goldfish.sh" },
     { 00440, AID_ROOT,      AID_SHELL,     0, "system/etc/init.trout.rc" },
     { 00550, AID_ROOT,      AID_SHELL,     0, "system/etc/init.ril" },
-    { 00550, AID_ROOT,      AID_SHELL,     0, "system/etc/init.testmenu" },
     { 00550, AID_DHCP,      AID_SHELL,     0, "system/etc/dhcpcd/dhcpcd-run-hooks" },
     { 00444, AID_RADIO,     AID_AUDIO,     0, "system/etc/AudioPara4.csv" },
     { 00555, AID_ROOT,      AID_ROOT,      0, "system/etc/ppp/*" },
diff --git a/include/private/android_logger.h b/include/private/android_logger.h
new file mode 100644
index 0000000..cc7ba30
--- /dev/null
+++ b/include/private/android_logger.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This file is used to define the internal protocol for the Android Logger */
+
+#ifndef _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+#define _SYSTEM_CORE_INCLUDE_PRIVATE_ANDROID_LOGGER_H_
+
+#include <stdint.h>
+
+#include <log/log.h>
+#include <log/log_read.h>
+
+#define LOGGER_MAGIC 'l'
+
+/* Header Structure to pstore */
+typedef struct __attribute__((__packed__)) {
+    uint8_t magic;
+    uint16_t len;
+    uint16_t uid;
+    uint16_t pid;
+} android_pmsg_log_header_t;
+
+/* Header Structure to logd, and second header for pstore */
+typedef struct __attribute__((__packed__)) {
+    typeof_log_id_t id;
+    uint16_t tid;
+    log_time realtime;
+} android_log_header_t;
+
+#endif
diff --git a/include/private/pixelflinger/ggl_fixed.h b/include/private/pixelflinger/ggl_fixed.h
index d0493f3..787f620 100644
--- a/include/private/pixelflinger/ggl_fixed.h
+++ b/include/private/pixelflinger/ggl_fixed.h
@@ -190,7 +190,7 @@
         );
     return res;
 }
-#elif defined(__mips__)
+#elif defined(__mips__) && __mips_isa_rev < 6
 
 /*inline MIPS implementations*/
 inline GGLfixed gglMulx(GGLfixed a, GGLfixed b, int shift) CONST;
diff --git a/include/sysutils/NetlinkEvent.h b/include/sysutils/NetlinkEvent.h
index c345cdb..4fa49c5 100644
--- a/include/sysutils/NetlinkEvent.h
+++ b/include/sysutils/NetlinkEvent.h
@@ -57,6 +57,7 @@
     bool parseIfInfoMessage(const struct nlmsghdr *nh);
     bool parseIfAddrMessage(const struct nlmsghdr *nh);
     bool parseUlogPacketMessage(const struct nlmsghdr *nh);
+    bool parseNfPacketMessage(struct nlmsghdr *nh);
     bool parseRtMessage(const struct nlmsghdr *nh);
     bool parseNdUserOptMessage(const struct nlmsghdr *nh);
 };
diff --git a/include/sysutils/NetlinkListener.h b/include/sysutils/NetlinkListener.h
index 6e52c3b..82465d6 100644
--- a/include/sysutils/NetlinkListener.h
+++ b/include/sysutils/NetlinkListener.h
@@ -27,6 +27,7 @@
 public:
     static const int NETLINK_FORMAT_ASCII = 0;
     static const int NETLINK_FORMAT_BINARY = 1;
+    static const int NETLINK_FORMAT_BINARY_UNICAST = 2;
 
 #if 1
     /* temporary version until we can get Motorola to update their
diff --git a/include/utils/AndroidThreads.h b/include/utils/AndroidThreads.h
index 4eee14d..aad1e82 100644
--- a/include/utils/AndroidThreads.h
+++ b/include/utils/AndroidThreads.h
@@ -20,7 +20,7 @@
 #include <stdint.h>
 #include <sys/types.h>
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 # include <pthread.h>
 #endif
 
@@ -73,9 +73,6 @@
 // ------------------------------------------------------------------
 // Extra functions working with raw pids.
 
-// Get pid for the current thread.
-extern pid_t androidGetTid();
-
 #ifdef HAVE_ANDROID_OS
 // Change the priority AND scheduling group of a particular thread.  The priority
 // should be one of the ANDROID_PRIORITY constants.  Returns INVALID_OPERATION
diff --git a/include/utils/Compat.h b/include/utils/Compat.h
index fb7748e..a238afe 100644
--- a/include/utils/Compat.h
+++ b/include/utils/Compat.h
@@ -19,11 +19,9 @@
 
 #include <unistd.h>
 
-/* Compatibility definitions for non-Linux (i.e., BSD-based) hosts. */
-#ifndef HAVE_OFF64_T
-#if _FILE_OFFSET_BITS < 64
-#error "_FILE_OFFSET_BITS < 64; large files are not supported on this platform"
-#endif /* _FILE_OFFSET_BITS < 64 */
+#if defined(__APPLE__)
+
+/* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
 
 typedef off_t off64_t;
 
@@ -31,13 +29,17 @@
     return lseek(fd, offset, whence);
 }
 
-#ifdef HAVE_PREAD
 static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) {
     return pread(fd, buf, nbytes, offset);
 }
-#endif
 
-#endif /* !HAVE_OFF64_T */
+#endif /* __APPLE__ */
+
+#if defined(_WIN32)
+#define O_CLOEXEC 0
+#define O_NOFOLLOW 0
+#define DEFFILEMODE 0666
+#endif /* _WIN32 */
 
 #if HAVE_PRINTF_ZD
 #  define ZD "%zd"
@@ -48,6 +50,17 @@
 #endif
 
 /*
+ * Needed for cases where something should be constexpr if possible, but not
+ * being constexpr is fine if in pre-C++11 code (such as a const static float
+ * member variable).
+ */
+#if __cplusplus >= 201103L
+#define CONSTEXPR constexpr
+#else
+#define CONSTEXPR
+#endif
+
+/*
  * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
  * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
  * not already defined, then define it here.
diff --git a/include/utils/Condition.h b/include/utils/Condition.h
index 1c99d1a..5a72519 100644
--- a/include/utils/Condition.h
+++ b/include/utils/Condition.h
@@ -21,7 +21,7 @@
 #include <sys/types.h>
 #include <time.h>
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 # include <pthread.h>
 #endif
 
@@ -74,7 +74,7 @@
     void broadcast();
 
 private:
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
     pthread_cond_t mCond;
 #else
     void*   mState;
@@ -83,7 +83,7 @@
 
 // ---------------------------------------------------------------------------
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 
 inline Condition::Condition() {
     pthread_cond_init(&mCond, NULL);
@@ -113,15 +113,15 @@
     return -pthread_cond_timedwait_relative_np(&mCond, &mutex.mMutex, &ts);
 #else // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
     struct timespec ts;
-#if defined(HAVE_POSIX_CLOCKS)
+#if defined(__linux__)
     clock_gettime(CLOCK_REALTIME, &ts);
-#else // HAVE_POSIX_CLOCKS
+#else // __APPLE__
     // we don't support the clocks here.
     struct timeval t;
     gettimeofday(&t, NULL);
     ts.tv_sec = t.tv_sec;
     ts.tv_nsec= t.tv_usec*1000;
-#endif // HAVE_POSIX_CLOCKS
+#endif
     ts.tv_sec += reltime/1000000000;
     ts.tv_nsec+= reltime%1000000000;
     if (ts.tv_nsec >= 1000000000) {
@@ -149,7 +149,7 @@
     pthread_cond_broadcast(&mCond);
 }
 
-#endif // HAVE_PTHREADS
+#endif // !defined(_WIN32)
 
 // ---------------------------------------------------------------------------
 }; // namespace android
diff --git a/include/utils/Endian.h b/include/utils/Endian.h
index 19f2504..591cae0 100644
--- a/include/utils/Endian.h
+++ b/include/utils/Endian.h
@@ -20,21 +20,16 @@
 #ifndef _LIBS_UTILS_ENDIAN_H
 #define _LIBS_UTILS_ENDIAN_H
 
-#if defined(HAVE_ENDIAN_H)
-
-#include <endian.h>
-
-#else /*not HAVE_ENDIAN_H*/
+#if defined(__APPLE__) || defined(_WIN32)
 
 #define __BIG_ENDIAN 0x1000
 #define __LITTLE_ENDIAN 0x0001
+#define __BYTE_ORDER __LITTLE_ENDIAN
 
-#if defined(HAVE_LITTLE_ENDIAN)
-# define __BYTE_ORDER __LITTLE_ENDIAN
 #else
-# define __BYTE_ORDER __BIG_ENDIAN
-#endif
 
-#endif /*not HAVE_ENDIAN_H*/
+#include <endian.h>
+
+#endif
 
 #endif /*_LIBS_UTILS_ENDIAN_H*/
diff --git a/include/utils/FileMap.h b/include/utils/FileMap.h
index dfe6d51..6c0aa52 100644
--- a/include/utils/FileMap.h
+++ b/include/utils/FileMap.h
@@ -24,7 +24,11 @@
 
 #include <utils/Compat.h>
 
-#ifdef HAVE_WIN32_FILEMAP
+#if defined(__MINGW32__)
+// Ensure that we always pull in winsock2.h before windows.h
+#ifdef HAVE_WINSOCK
+#include <winsock2.h>
+#endif
 #include <windows.h>
 #endif
 
@@ -123,7 +127,7 @@
     off64_t     mDataOffset;    // offset used when map was created
     void*       mDataPtr;       // start of requested data, offset from base
     size_t      mDataLength;    // length, measured from "mDataPtr"
-#ifdef HAVE_WIN32_FILEMAP
+#if defined(__MINGW32__)
     HANDLE      mFileHandle;    // Win32 file handle
     HANDLE      mFileMapping;   // Win32 file mapping handle
 #endif
diff --git a/include/utils/Mutex.h b/include/utils/Mutex.h
index dd201c8..757519b 100644
--- a/include/utils/Mutex.h
+++ b/include/utils/Mutex.h
@@ -21,11 +21,12 @@
 #include <sys/types.h>
 #include <time.h>
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 # include <pthread.h>
 #endif
 
 #include <utils/Errors.h>
+#include <utils/Timers.h>
 
 // ---------------------------------------------------------------------------
 namespace android {
@@ -45,7 +46,7 @@
         PRIVATE = 0,
         SHARED = 1
     };
-    
+
                 Mutex();
                 Mutex(const char* name);
                 Mutex(int type, const char* name = NULL);
@@ -58,6 +59,16 @@
     // lock if possible; returns 0 on success, error otherwise
     status_t    tryLock();
 
+#if HAVE_ANDROID_OS
+    // lock the mutex, but don't wait longer than timeoutMilliseconds.
+    // Returns 0 on success, TIMED_OUT for failure due to timeout expiration.
+    //
+    // OSX doesn't have pthread_mutex_timedlock() or equivalent. To keep
+    // capabilities consistent across host OSes, this method is only available
+    // when building Android binaries.
+    status_t    timedLock(nsecs_t timeoutMilliseconds);
+#endif
+
     // Manages the mutex automatically. It'll be locked when Autolock is
     // constructed and released when Autolock goes out of scope.
     class Autolock {
@@ -71,12 +82,12 @@
 
 private:
     friend class Condition;
-    
+
     // A mutex cannot be copied
                 Mutex(const Mutex&);
     Mutex&      operator = (const Mutex&);
-    
-#if defined(HAVE_PTHREADS)
+
+#if !defined(_WIN32)
     pthread_mutex_t mMutex;
 #else
     void    _init();
@@ -86,7 +97,7 @@
 
 // ---------------------------------------------------------------------------
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 
 inline Mutex::Mutex() {
     pthread_mutex_init(&mMutex, NULL);
@@ -117,8 +128,17 @@
 inline status_t Mutex::tryLock() {
     return -pthread_mutex_trylock(&mMutex);
 }
+#if HAVE_ANDROID_OS
+inline status_t Mutex::timedLock(nsecs_t timeoutNs) {
+    const struct timespec ts = {
+        /* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000),
+        /* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000),
+    };
+    return -pthread_mutex_timedlock(&mMutex, &ts);
+}
+#endif
 
-#endif // HAVE_PTHREADS
+#endif // !defined(_WIN32)
 
 // ---------------------------------------------------------------------------
 
@@ -127,7 +147,7 @@
  * When the function returns, it will go out of scope, and release the
  * mutex.
  */
- 
+
 typedef Mutex::Autolock AutoMutex;
 
 // ---------------------------------------------------------------------------
diff --git a/include/utils/RWLock.h b/include/utils/RWLock.h
index 90beb5f..e743b1c 100644
--- a/include/utils/RWLock.h
+++ b/include/utils/RWLock.h
@@ -20,7 +20,7 @@
 #include <stdint.h>
 #include <sys/types.h>
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 # include <pthread.h>
 #endif
 
@@ -31,7 +31,7 @@
 namespace android {
 // ---------------------------------------------------------------------------
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 
 /*
  * Simple mutex class.  The implementation is system-dependent.
@@ -117,7 +117,7 @@
     pthread_rwlock_unlock(&mRWLock);
 }
 
-#endif // HAVE_PTHREADS
+#endif // !defined(_WIN32)
 
 // ---------------------------------------------------------------------------
 }; // namespace android
diff --git a/include/utils/RefBase.h b/include/utils/RefBase.h
index 8e15c19..eac6a78 100644
--- a/include/utils/RefBase.h
+++ b/include/utils/RefBase.h
@@ -491,7 +491,8 @@
                 TYPE::renameRefId(d[i].get(), &s[i], &d[i]);
             }
         public:
-            Renamer(sp<TYPE>* d, sp<TYPE> const* s) : s(s), d(d) { }
+            Renamer(sp<TYPE>* d, sp<TYPE> const* s) : d(d), s(s) { }
+            virtual ~Renamer() { }
         };
 
         memmove(d, s, n*sizeof(sp<TYPE>));
@@ -510,7 +511,8 @@
                 TYPE::renameRefId(d[i].get_refs(), &s[i], &d[i]);
             }
         public:
-            Renamer(wp<TYPE>* d, wp<TYPE> const* s) : s(s), d(d) { }
+            Renamer(wp<TYPE>* d, wp<TYPE> const* s) : d(d), s(s) { }
+            virtual ~Renamer() { }
         };
 
         memmove(d, s, n*sizeof(wp<TYPE>));
diff --git a/include/utils/Singleton.h b/include/utils/Singleton.h
index c60680e..ffc03cb 100644
--- a/include/utils/Singleton.h
+++ b/include/utils/Singleton.h
@@ -65,9 +65,10 @@
  */
 
 #define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE)                 \
-    template<> Mutex Singleton< TYPE >::sLock(Mutex::PRIVATE);  \
-    template<> TYPE* Singleton< TYPE >::sInstance(0);           \
-    template class Singleton< TYPE >;
+    template<> ::android::Mutex  \
+        (::android::Singleton< TYPE >::sLock)(::android::Mutex::PRIVATE);  \
+    template<> TYPE* ::android::Singleton< TYPE >::sInstance(0);  \
+    template class ::android::Singleton< TYPE >;
 
 
 // ---------------------------------------------------------------------------
diff --git a/include/utils/Thread.h b/include/utils/Thread.h
index df30611..28839fd 100644
--- a/include/utils/Thread.h
+++ b/include/utils/Thread.h
@@ -21,7 +21,7 @@
 #include <sys/types.h>
 #include <time.h>
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 # include <pthread.h>
 #endif
 
@@ -71,8 +71,8 @@
             bool        isRunning() const;
 
 #ifdef HAVE_ANDROID_OS
-    // Return the thread's kernel ID, same as the thread itself calling gettid() or
-    // androidGetTid(), or -1 if the thread is not running.
+    // Return the thread's kernel ID, same as the thread itself calling gettid(),
+    // or -1 if the thread is not running.
             pid_t       getTid() const;
 #endif
 
diff --git a/include/utils/Unicode.h b/include/utils/Unicode.h
index 5b98de2..b76a5e2 100644
--- a/include/utils/Unicode.h
+++ b/include/utils/Unicode.h
@@ -22,12 +22,6 @@
 
 extern "C" {
 
-// Definitions exist in C++11
-#if defined __cplusplus && __cplusplus < 201103L
-typedef uint32_t char32_t;
-typedef uint16_t char16_t;
-#endif
-
 // Standard string functions on char16_t strings.
 int strcmp16(const char16_t *, const char16_t *);
 int strncmp16(const char16_t *s1, const char16_t *s2, size_t n);
diff --git a/include/utils/file.h b/include/utils/file.h
new file mode 100644
index 0000000..a80afb1
--- /dev/null
+++ b/include/utils/file.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UTILS_FILE_H
+#define UTILS_FILE_H
+
+#include <string>
+#include <sys/stat.h>
+
+namespace android {
+
+bool ReadFdToString(int fd, std::string* content);
+bool ReadFileToString(const std::string& path, std::string* content);
+
+bool WriteStringToFile(const std::string& content, const std::string& path);
+bool WriteStringToFd(const std::string& content, int fd);
+
+#if !defined(_WIN32)
+bool WriteStringToFile(const std::string& content, const std::string& path,
+                       mode_t mode, uid_t owner, gid_t group);
+#endif
+
+} // namespace android
+
+#endif
diff --git a/include/utils/stringprintf.h b/include/utils/stringprintf.h
new file mode 100644
index 0000000..e7dbac7
--- /dev/null
+++ b/include/utils/stringprintf.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef UTILS_STRINGPRINTF_H_
+#define UTILS_STRINGPRINTF_H_
+
+#include <stdarg.h>
+#include <string>
+
+namespace android {
+
+// Returns a string corresponding to printf-like formatting of the arguments.
+std::string StringPrintf(const char* fmt, ...)
+        __attribute__((__format__(__printf__, 1, 2)));
+
+// Appends a printf-like formatting of the arguments to 'dst'.
+void StringAppendF(std::string* dst, const char* fmt, ...)
+        __attribute__((__format__(__printf__, 2, 3)));
+
+// Appends a printf-like formatting of the arguments to 'dst'.
+void StringAppendV(std::string* dst, const char* format, va_list ap);
+
+}  // namespace android
+
+#endif
diff --git a/include/ziparchive/zip_archive.h b/include/ziparchive/zip_archive.h
index 1877494..386a390 100644
--- a/include/ziparchive/zip_archive.h
+++ b/include/ziparchive/zip_archive.h
@@ -21,6 +21,7 @@
 #define LIBZIPARCHIVE_ZIPARCHIVE_H_
 
 #include <stdint.h>
+#include <string.h>
 #include <sys/types.h>
 #include <utils/Compat.h>
 
@@ -33,8 +34,16 @@
 };
 
 struct ZipEntryName {
-  const char* name;
+  const uint8_t* name;
   uint16_t name_length;
+
+  ZipEntryName() {}
+
+  /*
+   * entry_name has to be an c-style string with only ASCII characters.
+   */
+  explicit ZipEntryName(const char* entry_name)
+      : name(reinterpret_cast<const uint8_t*>(entry_name)), name_length(strlen(entry_name)) {}
 };
 
 /*
@@ -92,6 +101,9 @@
  * Sets handle to the value of the opaque handle for this file descriptor.
  * This handle must be released by calling CloseArchive with this handle.
  *
+ * If assume_ownership parameter is 'true' calling CloseArchive will close
+ * the file.
+ *
  * This function maps and scans the central directory and builds a table
  * of entries for future lookups.
  *
@@ -100,7 +112,7 @@
  * Returns 0 on success, and negative values on failure.
  */
 int32_t OpenArchiveFd(const int fd, const char* debugFileName,
-                      ZipArchiveHandle *handle);
+                      ZipArchiveHandle *handle, bool assume_ownership = true);
 
 /*
  * Close archive, releasing resources associated with it. This will
@@ -124,24 +136,24 @@
  * and length, a call to VerifyCrcAndLengths must be made after entry data
  * has been processed.
  */
-int32_t FindEntry(const ZipArchiveHandle handle, const char* entryName,
+int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
                   ZipEntry* data);
 
 /*
  * Start iterating over all entries of a zip file. The order of iteration
  * is not guaranteed to be the same as the order of elements
- * in the central directory but is stable for a given zip file. |cookie|
- * must point to a writeable memory location, and will be set to the value
- * of an opaque cookie which can be used to make one or more calls to
- * Next.
+ * in the central directory but is stable for a given zip file. |cookie| will
+ * contain the value of an opaque cookie which can be used to make one or more
+ * calls to Next. All calls to StartIteration must be matched by a call to
+ * EndIteration to free any allocated memory.
  *
  * This method also accepts an optional prefix to restrict iteration to
- * entry names that start with |prefix|.
+ * entry names that start with |optional_prefix|.
  *
  * Returns 0 on success and negative values on failure.
  */
 int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
-                       const char* prefix);
+                       const ZipEntryName* optional_prefix);
 
 /*
  * Advance to the next element in the zipfile in iteration order.
@@ -152,6 +164,12 @@
 int32_t Next(void* cookie, ZipEntry* data, ZipEntryName *name);
 
 /*
+ * End iteration over all entries of a zip file and frees the memory allocated
+ * in StartIteration.
+ */
+void EndIteration(void* cookie);
+
+/*
  * Uncompress and write an entry to an open file identified by |fd|.
  * |entry->uncompressed_length| bytes will be written to the file at
  * its current offset, and the file will be truncated at the end of
diff --git a/init/Android.mk b/init/Android.mk
index 489dc93..7f3788a 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -1,35 +1,54 @@
 # Copyright 2005 The Android Open Source Project
 
 LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES:= \
-	builtins.c \
-	init.c \
-	devices.c \
-	property_service.c \
-	util.c \
-	parser.c \
-	keychords.c \
-	signal_handler.c \
-	init_parser.c \
-	ueventd.c \
-	ueventd_parser.c \
-	watchdogd.c
-
-LOCAL_CFLAGS    += -Wno-unused-parameter
+# --
 
 ifeq ($(strip $(INIT_BOOTCHART)),true)
-LOCAL_SRC_FILES += bootchart.c
-LOCAL_CFLAGS    += -DBOOTCHART=1
+init_options += -DBOOTCHART=1
+else
+init_options  += -DBOOTCHART=0
 endif
 
 ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
-LOCAL_CFLAGS += -DALLOW_LOCAL_PROP_OVERRIDE=1 -DALLOW_DISABLE_SELINUX=1
+init_options += -DALLOW_LOCAL_PROP_OVERRIDE=1 -DALLOW_DISABLE_SELINUX=1
+else
+init_options += -DALLOW_LOCAL_PROP_OVERRIDE=0 -DALLOW_DISABLE_SELINUX=0
 endif
 
-# Enable ueventd logging
-#LOCAL_CFLAGS += -DLOG_UEVENTS=1
+init_options += -DLOG_UEVENTS=0
+
+init_cflags += \
+    $(init_options) \
+    -Wall -Wextra \
+    -Wno-unused-parameter \
+    -Werror \
+
+# --
+
+include $(CLEAR_VARS)
+LOCAL_CPPFLAGS := $(init_cflags)
+LOCAL_SRC_FILES:= \
+    init_parser.cpp \
+    parser.cpp \
+    util.cpp \
+
+LOCAL_MODULE := libinit
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_CPPFLAGS := $(init_cflags)
+LOCAL_SRC_FILES:= \
+    bootchart.cpp \
+    builtins.cpp \
+    devices.cpp \
+    init.cpp \
+    keychords.cpp \
+    property_service.cpp \
+    signal_handler.cpp \
+    ueventd.cpp \
+    ueventd_parser.cpp \
+    watchdogd.cpp \
 
 LOCAL_MODULE:= init
 
@@ -38,34 +57,35 @@
 LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
 
 LOCAL_STATIC_LIBRARIES := \
-	libfs_mgr \
-	liblogwrap \
-	libcutils \
-	liblog \
-	libc \
-	libselinux \
-	libmincrypt \
-	libext4_utils_static
+    libinit \
+    libfs_mgr \
+    liblogwrap \
+    libcutils \
+    libutils \
+    liblog \
+    libc \
+    libselinux \
+    libmincrypt \
+    libext4_utils_static
 
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
+# Create symlinks
+LOCAL_POST_INSTALL_CMD := $(hide) mkdir -p $(TARGET_ROOT_OUT)/sbin; \
+    ln -sf ../init $(TARGET_ROOT_OUT)/sbin/ueventd; \
+    ln -sf ../init $(TARGET_ROOT_OUT)/sbin/watchdogd
 
 include $(BUILD_EXECUTABLE)
 
-# Make a symlink from /sbin/ueventd and /sbin/watchdogd to /init
-SYMLINKS := \
-	$(TARGET_ROOT_OUT)/sbin/ueventd \
-	$(TARGET_ROOT_OUT)/sbin/watchdogd
 
-$(SYMLINKS): INIT_BINARY := $(LOCAL_MODULE)
-$(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk
-	@echo "Symlink: $@ -> ../$(INIT_BINARY)"
-	@mkdir -p $(dir $@)
-	@rm -rf $@
-	$(hide) ln -sf ../$(INIT_BINARY) $@
 
-ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)
 
-# We need this so that the installed files could be picked up based on the
-# local module name
-ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \
-    $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS)
+include $(CLEAR_VARS)
+LOCAL_MODULE := init_tests
+LOCAL_SRC_FILES := \
+    util_test.cpp \
+
+LOCAL_SHARED_LIBRARIES += \
+    libcutils \
+    libutils \
+
+LOCAL_STATIC_LIBRARIES := libinit
+include $(BUILD_NATIVE_TEST)
diff --git a/init/bootchart.c b/init/bootchart.cpp
similarity index 76%
rename from init/bootchart.c
rename to init/bootchart.cpp
index f72fcaa..d275096 100644
--- a/init/bootchart.c
+++ b/init/bootchart.cpp
@@ -20,20 +20,18 @@
  * some C code that is run right from the init script.
  */
 
-#include <stdio.h>
-#include <time.h>
-#include <dirent.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <sys/stat.h>
 #include "bootchart.h"
 
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
 #define VERSION         "0.8"
 #define SAMPLE_PERIOD   0.2
 #define LOG_ROOT        "/data/bootchart"
@@ -47,28 +45,12 @@
 #define LOG_STOPFILE    "/data/bootchart-stop"
 
 static int
-unix_read(int  fd, void*  buff, int  len)
-{
-    int  ret;
-    do { ret = read(fd, buff, len); } while (ret < 0 && errno == EINTR);
-    return ret;
-}
-
-static int
-unix_write(int  fd, const void*  buff, int  len)
-{
-    int  ret;
-    do { ret = write(fd, buff, len); } while (ret < 0 && errno == EINTR);
-    return ret;
-}
-
-static int
 proc_read(const char*  filename, char* buff, size_t  buffsize)
 {
     int  len = 0;
-    int  fd  = open(filename, O_RDONLY);
+    int  fd  = open(filename, O_RDONLY | O_CLOEXEC);
     if (fd >= 0) {
-        len = unix_read(fd, buff, buffsize-1);
+        len = TEMP_FAILURE_RETRY(read(fd, buff, buffsize-1));
         close(fd);
     }
     buff[len > 0 ? len : 0] = 0;
@@ -77,21 +59,21 @@
 
 #define FILE_BUFF_SIZE    65536
 
-typedef struct {
+struct FileBuff {
     int   count;
     int   fd;
     char  data[FILE_BUFF_SIZE];
-} FileBuffRec, *FileBuff;
+};
 
 static void
-file_buff_open( FileBuff  buff, const char*  path )
+file_buff_open( FileBuff*  buff, const char*  path )
 {
     buff->count = 0;
     buff->fd    = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0755);
 }
 
 static void
-file_buff_write( FileBuff  buff, const void*  src, int  len )
+file_buff_write( FileBuff*  buff, const void*  src, int  len )
 {
     while (len > 0) {
         int  avail = sizeof(buff->data) - buff->count;
@@ -104,21 +86,33 @@
 
         buff->count += avail;
         if (buff->count == FILE_BUFF_SIZE) {
-            unix_write( buff->fd, buff->data, buff->count );
+            TEMP_FAILURE_RETRY(write(buff->fd, buff->data, buff->count));
             buff->count = 0;
         }
     }
 }
 
 static void
-file_buff_done( FileBuff  buff )
+file_buff_done( FileBuff*  buff )
 {
     if (buff->count > 0) {
-        unix_write( buff->fd, buff->data, buff->count );
+        TEMP_FAILURE_RETRY(write(buff->fd, buff->data, buff->count));
         buff->count = 0;
     }
 }
 
+static long long
+get_uptime_jiffies()
+{
+    char       buff[64];
+    long long  jiffies = 0;
+
+    if (proc_read("/proc/uptime", buff, sizeof(buff)) > 0)
+        jiffies = 100LL*strtod(buff,NULL);
+
+    return jiffies;
+}
+
 static void
 log_header(void)
 {
@@ -132,7 +126,7 @@
     struct tm  now = *localtime(&now_t);
     strftime(date, sizeof(date), "%x %X", &now);
 
-    out = fopen( LOG_HEADER, "w" );
+    out = fopen( LOG_HEADER, "we" );
     if (out == NULL)
         return;
 
@@ -158,60 +152,25 @@
 }
 
 static void
-close_on_exec(int  fd)
-{
-    fcntl(fd, F_SETFD, FD_CLOEXEC);
-}
-
-static void
-open_log_file(int*  plogfd, const char*  logfile)
-{
-    int    logfd = *plogfd;
-
-    /* create log file if needed */
-    if (logfd < 0) 
-    {
-        logfd = open(logfile,O_WRONLY|O_CREAT|O_TRUNC,0755);
-        if (logfd < 0) {
-            *plogfd = -2;
-            return;
-        }
-        close_on_exec(logfd);
-        *plogfd = logfd;
-    }
-}
-
-static void
-do_log_uptime(FileBuff  log)
+do_log_uptime(FileBuff*  log)
 {
     char  buff[65];
-    int   fd, ret, len;
+    int   len;
 
-    fd = open("/proc/uptime",O_RDONLY);
-    if (fd >= 0) {
-        int  ret;
-        ret = unix_read(fd, buff, 64);
-        close(fd);
-        buff[64] = 0;
-        if (ret >= 0) {
-            long long  jiffies = 100LL*strtod(buff,NULL);
-            int        len;
-            snprintf(buff,sizeof(buff),"%lld\n",jiffies);
-            len = strlen(buff);
-            file_buff_write(log, buff, len);
-        }
-    }
+    snprintf(buff,sizeof(buff),"%lld\n",get_uptime_jiffies());
+    len = strlen(buff);
+    file_buff_write(log, buff, len);
 }
 
 static void
-do_log_ln(FileBuff  log)
+do_log_ln(FileBuff*  log)
 {
     file_buff_write(log, "\n", 1);
 }
 
 
 static void
-do_log_file(FileBuff  log, const char*  procfile)
+do_log_file(FileBuff*  log, const char*  procfile)
 {
     char   buff[1024];
     int    fd;
@@ -219,12 +178,10 @@
     do_log_uptime(log);
 
     /* append file content */
-    fd = open(procfile,O_RDONLY);
+    fd = open(procfile,O_RDONLY|O_CLOEXEC);
     if (fd >= 0) {
-        close_on_exec(fd);
         for (;;) {
-            int  ret;
-            ret = unix_read(fd, buff, sizeof(buff));
+            int ret = TEMP_FAILURE_RETRY(read(fd, buff, sizeof(buff)));
             if (ret <= 0)
                 break;
 
@@ -239,7 +196,7 @@
 }
 
 static void
-do_log_procs(FileBuff  log)
+do_log_procs(FileBuff*  log)
 {
     DIR*  dir = opendir("/proc");
     struct dirent*  entry;
@@ -263,9 +220,9 @@
 
             /* read process stat line */
             snprintf(filename,sizeof(filename),"/proc/%d/stat",pid);
-            fd = open(filename,O_RDONLY);
+            fd = open(filename,O_RDONLY|O_CLOEXEC);
             if (fd >= 0) {
-               len = unix_read(fd, buff, sizeof(buff)-1);
+               len = TEMP_FAILURE_RETRY(read(fd, buff, sizeof(buff)-1));
                close(fd);
                if (len > 0) {
                     int  len2 = strlen(cmdline);
@@ -291,9 +248,9 @@
     do_log_ln(log);
 }
 
-static FileBuffRec  log_stat[1];
-static FileBuffRec  log_procs[1];
-static FileBuffRec  log_disks[1];
+static FileBuff  log_stat[1];
+static FileBuff  log_procs[1];
+static FileBuff  log_disks[1];
 
 /* called to setup bootcharting */
 int   bootchart_init( void )
@@ -331,7 +288,7 @@
 
     count = (timeout*1000 + BOOTCHART_POLLING_MS-1)/BOOTCHART_POLLING_MS;
 
-    do {ret=mkdir(LOG_ROOT,0755);}while (ret < 0 && errno == EINTR);
+    ret = TEMP_FAILURE_RETRY(mkdir(LOG_ROOT,0755));
 
     file_buff_open(log_stat,  LOG_STAT);
     file_buff_open(log_procs, LOG_PROCS);
@@ -339,7 +296,7 @@
 
     /* create kernel process accounting file */
     {
-        int  fd = open( LOG_ACCT, O_WRONLY|O_CREAT|O_TRUNC,0644);
+        int  fd = open( LOG_ACCT, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC,0644);
         if (fd >= 0) {
             close(fd);
             acct( LOG_ACCT );
@@ -376,3 +333,9 @@
     file_buff_done(log_procs);
     acct(NULL);
 }
+
+/* called to get time (in ms) used by bootchart */
+long long  bootchart_gettime( void )
+{
+    return 10LL*get_uptime_jiffies();
+}
diff --git a/init/bootchart.h b/init/bootchart.h
index 39d2d4f..9ba3c40 100644
--- a/init/bootchart.h
+++ b/init/bootchart.h
@@ -17,20 +17,13 @@
 #ifndef _BOOTCHART_H
 #define _BOOTCHART_H
 
-#ifndef BOOTCHART
-# define  BOOTCHART  0
-#endif
-
-#if BOOTCHART
-
 extern int   bootchart_init(void);
 extern int   bootchart_step(void);
 extern void  bootchart_finish(void);
+extern long long  bootchart_gettime(void);
 
-# define BOOTCHART_POLLING_MS   200   /* polling period in ms */
-# define BOOTCHART_DEFAULT_TIME_SEC    (2*60)  /* default polling time in seconds */
-# define BOOTCHART_MAX_TIME_SEC        (10*60) /* max polling time in seconds */
-
-#endif /* BOOTCHART */
+#define BOOTCHART_POLLING_MS   200   /* polling period in ms */
+#define BOOTCHART_DEFAULT_TIME_SEC    (2*60)  /* default polling time in seconds */
+#define BOOTCHART_MAX_TIME_SEC        (10*60) /* max polling time in seconds */
 
 #endif /* _BOOTCHART_H */
diff --git a/init/builtins.c b/init/builtins.cpp
similarity index 86%
rename from init/builtins.c
rename to init/builtins.cpp
index c192551..31c6a99 100644
--- a/init/builtins.c
+++ b/init/builtins.cpp
@@ -29,6 +29,7 @@
 #include <stdlib.h>
 #include <sys/mount.h>
 #include <sys/resource.h>
+#include <sys/time.h>
 #include <sys/wait.h>
 #include <linux/loop.h>
 #include <cutils/partition_utils.h>
@@ -48,112 +49,29 @@
 
 #include <private/android_filesystem_config.h>
 
+#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
+
 int add_environment(const char *name, const char *value);
 
-extern int init_module(void *, unsigned long, const char *);
-
-static int write_file(const char *path, const char *value)
-{
-    int fd, ret, len;
-
-    fd = open(path, O_WRONLY|O_CREAT|O_NOFOLLOW, 0600);
-
-    if (fd < 0)
-        return -errno;
-
-    len = strlen(value);
-
-    do {
-        ret = write(fd, value, len);
-    } while (ret < 0 && errno == EINTR);
-
-    close(fd);
-    if (ret < 0) {
-        return -errno;
-    } else {
-        return 0;
-    }
-}
-
-static int _open(const char *path)
-{
-    int fd;
-
-    fd = open(path, O_RDONLY | O_NOFOLLOW);
-    if (fd < 0)
-        fd = open(path, O_WRONLY | O_NOFOLLOW);
-
-    return fd;
-}
-
-static int _chown(const char *path, unsigned int uid, unsigned int gid)
-{
-    int fd;
-    int ret;
-
-    fd = _open(path);
-    if (fd < 0) {
-        return -1;
-    }
-
-    ret = fchown(fd, uid, gid);
-    if (ret < 0) {
-        int errno_copy = errno;
-        close(fd);
-        errno = errno_copy;
-        return -1;
-    }
-
-    close(fd);
-
-    return 0;
-}
-
-static int _chmod(const char *path, mode_t mode)
-{
-    int fd;
-    int ret;
-
-    fd = _open(path);
-    if (fd < 0) {
-        return -1;
-    }
-
-    ret = fchmod(fd, mode);
-    if (ret < 0) {
-        int errno_copy = errno;
-        close(fd);
-        errno = errno_copy;
-        return -1;
-    }
-
-    close(fd);
-
-    return 0;
-}
+// System call provided by bionic but not in any header file.
+extern "C" int init_module(void *, unsigned long, const char *);
 
 static int insmod(const char *filename, char *options)
 {
-    void *module;
-    unsigned size;
-    int ret;
-
-    module = read_file(filename, &size);
-    if (!module)
+    std::string module;
+    if (!read_file(filename, &module)) {
         return -1;
+    }
 
-    ret = init_module(module, size, options);
-
-    free(module);
-
-    return ret;
+    // TODO: use finit_module for >= 3.8 kernels.
+    return init_module(&module[0], module.size(), options);
 }
 
 static int setkey(struct kbentry *kbe)
 {
     int fd, ret;
 
-    fd = open("/dev/tty0", O_RDWR | O_SYNC);
+    fd = open("/dev/tty0", O_RDWR | O_SYNC | O_CLOEXEC);
     if (fd < 0)
         return -1;
 
@@ -185,7 +103,7 @@
         ifr.ifr_flags &= ~IFF_UP;
 
     ret = ioctl(s, SIOCSIFFLAGS, &ifr);
-    
+
 done:
     close(s);
     return ret;
@@ -259,6 +177,67 @@
     return -1;
 }
 
+int do_execonce(int nargs, char **args)
+{
+    pid_t child;
+    int child_status = 0;
+    static int already_done;
+
+    if (already_done) {
+      return -1;
+    }
+    already_done = 1;
+    if (!(child = fork())) {
+        /*
+         * Child process.
+         */
+        zap_stdio();
+        char *exec_args[100];
+        size_t num_process_args = nargs;
+
+        memset(exec_args, 0, sizeof(exec_args));
+        if (num_process_args > ARRAY_SIZE(exec_args) - 1) {
+            ERROR("exec called with %zu args, limit is %zu", num_process_args,
+                  ARRAY_SIZE(exec_args) - 1);
+            _exit(1);
+        }
+        for (size_t i = 1; i < num_process_args; i++)
+            exec_args[i - 1] = args[i];
+
+        if (execv(exec_args[0], exec_args) == -1) {
+            ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno));
+            _exit(1);
+        }
+        ERROR("Returned from execv()!");
+        _exit(1);
+    }
+
+    /*
+     * Parent process.
+     */
+    if (child == -1) {
+        ERROR("Fork failed\n");
+        return -1;
+    }
+
+    if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) {
+        ERROR("waitpid(): failed (%s)\n", strerror(errno));
+        return -1;
+    }
+
+    if (WIFSIGNALED(child_status)) {
+        INFO("Child exited due to signal %d\n", WTERMSIG(child_status));
+        return -1;
+    } else if (WIFEXITED(child_status)) {
+        INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status));
+        return WEXITSTATUS(child_status);
+    }
+
+    ERROR("Abnormal child process exit\n");
+
+    return -1;
+}
+
 int do_export(int nargs, char **args)
 {
     return add_environment(args[1], args[2]);
@@ -319,7 +298,7 @@
     ret = make_dir(args[1], mode);
     /* chmod in case the directory already exists */
     if (ret == -1 && errno == EEXIST) {
-        ret = _chmod(args[1], mode);
+        ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
     }
     if (ret == -1) {
         return -errno;
@@ -333,13 +312,13 @@
             gid = decode_uid(args[4]);
         }
 
-        if (_chown(args[1], uid, gid) < 0) {
+        if (lchown(args[1], uid, gid) == -1) {
             return -errno;
         }
 
         /* chown may have cleared S_ISUID and S_ISGID, chmod again */
         if (mode & (S_ISUID | S_ISGID)) {
-            ret = _chmod(args[1], mode);
+            ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW);
             if (ret == -1) {
                 return -errno;
             }
@@ -424,15 +403,16 @@
         struct loop_info info;
 
         mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
-        fd = open(source + 5, mode);
+        fd = open(source + 5, mode | O_CLOEXEC);
         if (fd < 0) {
             return -1;
         }
 
         for (n = 0; ; n++) {
             sprintf(tmp, "/dev/block/loop%d", n);
-            loop = open(tmp, mode);
+            loop = open(tmp, mode | O_CLOEXEC);
             if (loop < 0) {
+                close(fd);
                 return -1;
             }
 
@@ -476,7 +456,7 @@
 static int wipe_data_via_recovery()
 {
     mkdir("/cache/recovery", 0700);
-    int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600);
+    int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
     if (fd >= 0) {
         write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
         write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
@@ -500,7 +480,6 @@
     int ret = -1;
     int child_ret = -1;
     int status;
-    const char *prop;
     struct fstab *fstab;
 
     if (nargs != 2) {
@@ -667,7 +646,7 @@
     int res;
     int len = 0;
     int cmd = 0;
-    char *reboot_target;
+    const char *reboot_target;
 
     res = expand_props(command, args[1], sizeof(command));
     if (res) {
@@ -727,7 +706,7 @@
         return -1;
 
     memset(&tz, 0, sizeof(tz));
-    tz.tz_minuteswest = atoi(args[1]);   
+    tz.tz_minuteswest = atoi(args[1]);
     if (settimeofday(NULL, &tz))
         return -1;
     return 0;
@@ -737,15 +716,13 @@
 {
     const char *path = args[1];
     const char *value = args[2];
-    char prop_val[PROP_VALUE_MAX];
-    int ret;
 
-    ret = expand_props(prop_val, value, sizeof(prop_val));
-    if (ret) {
+    char expanded_value[PROP_VALUE_MAX];
+    if (expand_props(expanded_value, value, sizeof(expanded_value))) {
         ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
         return -EINVAL;
     }
-    return write_file(path, prop_val);
+    return write_file(path, expanded_value);
 }
 
 int do_copy(int nargs, char **args)
@@ -760,16 +737,16 @@
     if (nargs != 3)
         return -1;
 
-    if (stat(args[1], &info) < 0) 
+    if (stat(args[1], &info) < 0)
         return -1;
 
-    if ((fd1 = open(args[1], O_RDONLY)) < 0) 
+    if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0)
         goto out_err;
 
-    if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
+    if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
         goto out_err;
 
-    if (!(buffer = malloc(info.st_size)))
+    if (!(buffer = (char*) malloc(info.st_size)))
         goto out_err;
 
     p = buffer;
@@ -813,10 +790,10 @@
 int do_chown(int nargs, char **args) {
     /* GID is optional. */
     if (nargs == 3) {
-        if (_chown(args[2], decode_uid(args[1]), -1) < 0)
+        if (lchown(args[2], decode_uid(args[1]), -1) == -1)
             return -errno;
     } else if (nargs == 4) {
-        if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0)
+        if (lchown(args[3], decode_uid(args[1]), decode_uid(args[2])) == -1)
             return -errno;
     } else {
         return -1;
@@ -839,7 +816,7 @@
 
 int do_chmod(int nargs, char **args) {
     mode_t mode = get_mode(args[1]);
-    if (_chmod(args[2], mode) < 0) {
+    if (fchmodat(AT_FDCWD, args[2], mode, AT_SYMLINK_NOFOLLOW) < 0) {
         return -errno;
     }
     return 0;
diff --git a/init/devices.c b/init/devices.cpp
similarity index 94%
rename from init/devices.c
rename to init/devices.cpp
index 1012fee..9275439 100644
--- a/init/devices.c
+++ b/init/devices.cpp
@@ -48,8 +48,6 @@
 #include "util.h"
 #include "log.h"
 
-#define UNUSED __attribute__((__unused__))
-
 #define SYSFS_PREFIX    "/sys"
 #define FIRMWARE_DIR1   "/etc/firmware"
 #define FIRMWARE_DIR2   "/vendor/firmware"
@@ -101,7 +99,7 @@
                   mode_t perm, unsigned int uid, unsigned int gid,
                   unsigned short prefix,
                   unsigned short wildcard) {
-    struct perm_node *node = calloc(1, sizeof(*node));
+    struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
     if (!node)
         return -ENOMEM;
 
@@ -191,7 +189,6 @@
 static mode_t get_device_perm(const char *path, const char **links,
                 unsigned *uid, unsigned *gid)
 {
-    mode_t perm;
     struct listnode *node;
     struct perm_node *perm_node;
     struct perms_ *dp;
@@ -232,7 +229,7 @@
 }
 
 static void make_device(const char *path,
-                        const char *upath UNUSED,
+                        const char */*upath*/,
                         int block, int major, int minor,
                         const char **links)
 {
@@ -290,7 +287,7 @@
 
     INFO("adding platform device %s (%s)\n", name, path);
 
-    bus = calloc(1, sizeof(struct platform_node));
+    bus = (platform_node*) calloc(1, sizeof(struct platform_node));
     bus->path = strdup(path);
     bus->path_len = path_len;
     bus->name = bus->path + (name - path);
@@ -367,8 +364,6 @@
     return 0;
 }
 
-#if LOG_UEVENTS
-
 static inline suseconds_t get_usecs(void)
 {
     struct timeval tv;
@@ -376,15 +371,6 @@
     return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
 }
 
-#define log_event_print(x...) INFO(x)
-
-#else
-
-#define log_event_print(fmt, args...)   do { } while (0)
-#define get_usecs()                     0
-
-#endif
-
 static void parse_event(const char *msg, struct uevent *uevent)
 {
     uevent->action = "";
@@ -433,9 +419,11 @@
             ;
     }
 
-    log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
-                    uevent->action, uevent->path, uevent->subsystem,
-                    uevent->firmware, uevent->major, uevent->minor);
+    if (LOG_UEVENTS) {
+        INFO("event { '%s', '%s', '%s', '%s', %d, %d }\n",
+             uevent->action, uevent->path, uevent->subsystem,
+             uevent->firmware, uevent->major, uevent->minor);
+    }
 }
 
 static char **get_character_device_symlinks(struct uevent *uevent)
@@ -451,14 +439,14 @@
     if (!pdev)
         return NULL;
 
-    links = malloc(sizeof(char *) * 2);
+    links = (char**) malloc(sizeof(char *) * 2);
     if (!links)
         return NULL;
     memset(links, 0, sizeof(char *) * 2);
 
     /* skip "/devices/platform/<driver>" */
     parent = strchr(uevent->path + pdev->path_len, '/');
-    if (!*parent)
+    if (!parent)
         goto err;
 
     if (!strncmp(parent, "/usb", 4)) {
@@ -497,15 +485,10 @@
     struct platform_node *pdev;
     char *slash;
     const char *type;
-    int width;
     char buf[256];
     char link_path[256];
-    int fd;
     int link_num = 0;
-    int ret;
     char *p;
-    unsigned int size;
-    struct stat info;
 
     pdev = find_platform_device(uevent->path);
     if (pdev) {
@@ -518,7 +501,7 @@
         return NULL;
     }
 
-    char **links = malloc(sizeof(char *) * 4);
+    char **links = (char**) malloc(sizeof(char *) * 4);
     if (!links)
         return NULL;
     memset(links, 0, sizeof(char *) * 4);
@@ -667,14 +650,9 @@
     mkdir_recursive(dir, 0755);
 }
 
-static inline void __attribute__((__deprecated__)) kernel_logger()
-{
-    INFO("kernel logger is deprecated\n");
-}
-
 static void handle_generic_device_event(struct uevent *uevent)
 {
-    char *base;
+    const char *base;
     const char *name;
     char devpath[DEVPATH_LEN] = {0};
     char **links = NULL;
@@ -758,7 +736,7 @@
          make_dir(base, 0755);
      } else if(!strncmp(uevent->subsystem, "misc", 4) &&
                  !strncmp(name, "log_", 4)) {
-         kernel_logger();
+         INFO("kernel logger is deprecated\n");
          base = "/dev/log/";
          make_dir(base, 0755);
          name += 4;
@@ -871,20 +849,20 @@
     if (l == -1)
         goto data_free_out;
 
-    loading_fd = open(loading, O_WRONLY);
+    loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
     if(loading_fd < 0)
         goto file_free_out;
 
-    data_fd = open(data, O_WRONLY);
+    data_fd = open(data, O_WRONLY|O_CLOEXEC);
     if(data_fd < 0)
         goto loading_close_out;
 
 try_loading_again:
-    fw_fd = open(file1, O_RDONLY);
+    fw_fd = open(file1, O_RDONLY|O_CLOEXEC);
     if(fw_fd < 0) {
-        fw_fd = open(file2, O_RDONLY);
+        fw_fd = open(file2, O_RDONLY|O_CLOEXEC);
         if (fw_fd < 0) {
-            fw_fd = open(file3, O_RDONLY);
+            fw_fd = open(file3, O_RDONLY|O_CLOEXEC);
             if (fw_fd < 0) {
                 if (booting) {
                         /* If we're not fully booted, we may be missing
@@ -926,7 +904,6 @@
 static void handle_firmware_event(struct uevent *uevent)
 {
     pid_t pid;
-    int ret;
 
     if(strcmp(uevent->subsystem, "firmware"))
         return;
@@ -938,7 +915,9 @@
     pid = fork();
     if (!pid) {
         process_firmware_event(uevent);
-        exit(EXIT_SUCCESS);
+        _exit(EXIT_SUCCESS);
+    } else if (pid < 0) {
+        ERROR("could not fork to process firmware event: %s\n", strerror(errno));
     }
 }
 
@@ -977,7 +956,7 @@
 **
 ** We drain any pending events from the netlink socket every time
 ** we poke another uevent file to make sure we don't overrun the
-** socket's buffer.  
+** socket's buffer.
 */
 
 static void do_coldboot(DIR *d)
@@ -1043,17 +1022,19 @@
     fcntl(device_fd, F_SETFD, FD_CLOEXEC);
     fcntl(device_fd, F_SETFL, O_NONBLOCK);
 
-    if (stat(coldboot_done, &info) < 0) {
+    if (stat(COLDBOOT_DONE, &info) < 0) {
         t0 = get_usecs();
         coldboot("/sys/class");
         coldboot("/sys/block");
         coldboot("/sys/devices");
         t1 = get_usecs();
-        fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
+        fd = open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000);
         close(fd);
-        log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
-    } else {
-        log_event_print("skipping coldboot, already done\n");
+        if (LOG_UEVENTS) {
+            INFO("coldboot %ld uS\n", ((long) (t1 - t0)));
+        }
+    } else if (LOG_UEVENTS) {
+        INFO("skipping coldboot, already done\n");
     }
 }
 
diff --git a/init/devices.h b/init/devices.h
index 5d0fe88..6cb0a77 100644
--- a/init/devices.h
+++ b/init/devices.h
@@ -26,4 +26,5 @@
                          unsigned int gid, unsigned short prefix,
                          unsigned short wildcard);
 int get_device_fd();
+
 #endif	/* _INIT_DEVICES_H */
diff --git a/init/init.c b/init/init.cpp
similarity index 87%
rename from init/init.c
rename to init/init.cpp
index bd1db7a..3e3be2e 100644
--- a/init/init.c
+++ b/init/init.cpp
@@ -63,9 +63,8 @@
 
 static int property_triggers_enabled = 0;
 
-#if BOOTCHART
 static int   bootchart_count;
-#endif
+static long long bootchart_time = 0;
 
 static char console[32];
 static char bootmode[32];
@@ -75,7 +74,6 @@
 
 static struct action *cur_action = NULL;
 static struct command *cur_command = NULL;
-static struct listnode *command_queue = NULL;
 
 void notify_service_state(const char *name, const char *state)
 {
@@ -113,9 +111,8 @@
 
         /* Add entry if a free slot is available */
         if (ENV[n] == NULL) {
-            size_t len = key_len + strlen(val) + 2;
-            char *entry = malloc(len);
-            snprintf(entry, len, "%s=%s", key, val);
+            char* entry;
+            asprintf(&entry, "%s=%s", key, val);
             ENV[n] = entry;
             return 0;
         }
@@ -126,7 +123,7 @@
     return -1;
 }
 
-static void zap_stdio(void)
+void zap_stdio(void)
 {
     int fd;
     fd = open("/dev/null", O_RDWR);
@@ -169,7 +166,6 @@
     struct stat s;
     pid_t pid;
     int needs_console;
-    int n;
     char *scon = NULL;
     int rc;
 
@@ -294,14 +290,14 @@
             zap_stdio();
         }
 
-#if 0
-        for (n = 0; svc->args[n]; n++) {
-            INFO("args[%d] = '%s'\n", n, svc->args[n]);
+        if (false) {
+            for (size_t n = 0; svc->args[n]; n++) {
+                INFO("args[%zu] = '%s'\n", n, svc->args[n]);
+            }
+            for (size_t n = 0; ENV[n]; n++) {
+                INFO("env[%zu] = '%s'\n", n, ENV[n]);
+            }
         }
-        for (n = 0; ENV[n]; n++) {
-            INFO("env[%d] = '%s'\n", n, ENV[n]);
-        }
-#endif
 
         setpgid(0, getpid());
 
@@ -350,7 +346,7 @@
                 if (arg_idx == INIT_PARSER_MAXARGS)
                     break;
             }
-            arg_ptrs[arg_idx] = '\0';
+            arg_ptrs[arg_idx] = NULL;
             execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
         }
         _exit(127);
@@ -541,17 +537,35 @@
     return (list_tail(&act->commands) == &cmd->clist);
 }
 
+
+void build_triggers_string(char *name_str, int length, struct action *cur_action) {
+    struct listnode *node;
+    struct trigger *cur_trigger;
+
+    list_for_each(node, &cur_action->triggers) {
+        cur_trigger = node_to_item(node, struct trigger, nlist);
+        if (node != cur_action->triggers.next) {
+            strlcat(name_str, " " , length);
+        }
+        strlcat(name_str, cur_trigger->name , length);
+    }
+}
+
 void execute_one_command(void)
 {
     int ret, i;
     char cmd_str[256] = "";
+    char name_str[256] = "";
 
     if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
         cur_action = action_remove_queue_head();
         cur_command = NULL;
         if (!cur_action)
             return;
-        INFO("processing action %p (%s)\n", cur_action, cur_action->name);
+
+        build_triggers_string(name_str, sizeof(name_str), cur_action);
+
+        INFO("processing action %p (%s)\n", cur_action, name_str);
         cur_command = get_first_command(cur_action);
     } else {
         cur_command = get_next_command(cur_action, cur_command);
@@ -569,7 +583,7 @@
             }
         }
         INFO("command '%s' action=%s status=%d (%s:%d)\n",
-             cmd_str, cur_action ? cur_action->name : "", ret, cur_command->filename,
+             cmd_str, cur_action ? name_str : "", ret, cur_command->filename,
              cur_command->line);
     }
 }
@@ -577,10 +591,10 @@
 static int wait_for_coldboot_done_action(int nargs, char **args)
 {
     int ret;
-    INFO("wait for %s\n", coldboot_done);
-    ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
+    INFO("wait for %s\n", COLDBOOT_DONE);
+    ret = wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT);
     if (ret)
-        ERROR("Timed out waiting for %s\n", coldboot_done);
+        ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
     return ret;
 }
 
@@ -609,7 +623,7 @@
     size_t total_bytes_written = 0;
 
     hwrandom_fd = TEMP_FAILURE_RETRY(
-            open("/dev/hw_random", O_RDONLY | O_NOFOLLOW));
+            open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
     if (hwrandom_fd == -1) {
         if (errno == ENOENT) {
           ERROR("/dev/hw_random not found\n");
@@ -622,7 +636,7 @@
     }
 
     urandom_fd = TEMP_FAILURE_RETRY(
-            open("/dev/urandom", O_WRONLY | O_NOFOLLOW));
+            open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
     if (urandom_fd == -1) {
         ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
         goto ret;
@@ -676,12 +690,12 @@
         snprintf(console_name, sizeof(console_name), "/dev/%s", console);
     }
 
-    fd = open(console_name, O_RDWR);
+    fd = open(console_name, O_RDWR | O_CLOEXEC);
     if (fd >= 0)
         have_console = 1;
     close(fd);
 
-    fd = open("/dev/tty0", O_WRONLY);
+    fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
     if (fd >= 0) {
         const char *msg;
             msg = "\n"
@@ -843,7 +857,6 @@
     return 0;
 }
 
-#if BOOTCHART
 static int bootchart_init_action(int nargs, char **args)
 {
     bootchart_count = bootchart_init();
@@ -857,27 +870,6 @@
 
     return 0;
 }
-#endif
-
-static const struct selinux_opt seopts_prop[] = {
-        { SELABEL_OPT_PATH, "/property_contexts" },
-        { SELABEL_OPT_PATH, "/data/security/current/property_contexts" },
-        { 0, NULL }
-};
-
-struct selabel_handle* selinux_android_prop_context_handle(void)
-{
-    int policy_index = selinux_android_use_data_policy() ? 1 : 0;
-    struct selabel_handle* sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP,
-                                                   &seopts_prop[policy_index], 1);
-    if (!sehandle) {
-        ERROR("SELinux:  Could not load property_contexts:  %s\n",
-              strerror(errno));
-        return NULL;
-    }
-    INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[policy_index].value);
-    return sehandle;
-}
 
 void selinux_init_all_handles(void)
 {
@@ -888,45 +880,41 @@
 
 static bool selinux_is_disabled(void)
 {
-#ifdef ALLOW_DISABLE_SELINUX
-    char tmp[PROP_VALUE_MAX];
+    if (ALLOW_DISABLE_SELINUX) {
+        if (access("/sys/fs/selinux", F_OK) != 0) {
+            // SELinux is not compiled into the kernel, or has been disabled
+            // via the kernel command line "selinux=0".
+            return true;
+        }
 
-    if (access("/sys/fs/selinux", F_OK) != 0) {
-        /* SELinux is not compiled into the kernel, or has been disabled
-         * via the kernel command line "selinux=0".
-         */
-        return true;
+        char tmp[PROP_VALUE_MAX];
+        if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
+            // SELinux is compiled into the kernel, but we've been told to disable it.
+            return true;
+        }
     }
 
-    if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
-        /* SELinux is compiled into the kernel, but we've been told to disable it. */
-        return true;
-    }
-#endif
-
     return false;
 }
 
 static bool selinux_is_enforcing(void)
 {
-#ifdef ALLOW_DISABLE_SELINUX
-    char tmp[PROP_VALUE_MAX];
+    if (ALLOW_DISABLE_SELINUX) {
+        char tmp[PROP_VALUE_MAX];
+        if (property_get("ro.boot.selinux", tmp) == 0) {
+            // Property is not set.  Assume enforcing.
+            return true;
+        }
 
-    if (property_get("ro.boot.selinux", tmp) == 0) {
-        /* Property is not set.  Assume enforcing */
-        return true;
+        if (strcmp(tmp, "permissive") == 0) {
+            // SELinux is in the kernel, but we've been told to go into permissive mode.
+            return false;
+        }
+
+        if (strcmp(tmp, "enforcing") != 0) {
+            ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
+        }
     }
-
-    if (strcmp(tmp, "permissive") == 0) {
-        /* SELinux is in the kernel, but we've been told to go into permissive mode */
-        return false;
-    }
-
-    if (strcmp(tmp, "enforcing") != 0) {
-        ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
-    }
-
-#endif
     return true;
 }
 
@@ -952,7 +940,7 @@
     return 0;
 }
 
-static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len)
+static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len)
 {
     snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
     return 0;
@@ -1002,9 +990,6 @@
 {
     int fd_count = 0;
     struct pollfd ufds[4];
-    char *tmpdev;
-    char* debuggable;
-    char tmp[32];
     int property_set_fd_init = 0;
     int signal_fd_init = 0;
     int keychord_fd_init = 0;
@@ -1035,7 +1020,7 @@
     mount("sysfs", "/sys", "sysfs", 0, NULL);
 
         /* indicate that booting is in progress to background fw loaders, etc */
-    close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
+    close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
 
         /* We must have some place other than / to create the
          * device nodes for kmsg and null, otherwise we won't
@@ -1073,7 +1058,6 @@
     INFO("property init\n");
     property_load_boot_defaults();
 
-    INFO("reading config file\n");
     init_parse_config_file("/init.rc");
 
     action_for_each_trigger("early-init", action_add_queue_tail);
@@ -1103,10 +1087,9 @@
     /* run all property triggers based on current state of the properties */
     queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
 
-
-#if BOOTCHART
-    queue_builtin_action(bootchart_init_action, "bootchart_init");
-#endif
+    if (BOOTCHART) {
+        queue_builtin_action(bootchart_init_action, "bootchart_init");
+    }
 
     for(;;) {
         int nr, i, timeout = -1;
@@ -1142,19 +1125,39 @@
                 timeout = 0;
         }
 
-        if (!action_queue_empty() || cur_action)
+        if (!action_queue_empty() || cur_action) {
             timeout = 0;
+        }
 
-#if BOOTCHART
-        if (bootchart_count > 0) {
-            if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
-                timeout = BOOTCHART_POLLING_MS;
-            if (bootchart_step() < 0 || --bootchart_count == 0) {
-                bootchart_finish();
-                bootchart_count = 0;
+        if (BOOTCHART) {
+            if (bootchart_count > 0) {
+                long long current_time;
+                int elapsed_time, remaining_time;
+
+                current_time = bootchart_gettime();
+                elapsed_time = current_time - bootchart_time;
+
+                if (elapsed_time >= BOOTCHART_POLLING_MS) {
+                    /* count missed samples */
+                    while (elapsed_time >= BOOTCHART_POLLING_MS) {
+                        elapsed_time -= BOOTCHART_POLLING_MS;
+                        bootchart_count--;
+                    }
+                    /* count may be negative, take a sample anyway */
+                    bootchart_time = current_time;
+                    if (bootchart_step() < 0 || bootchart_count <= 0) {
+                        bootchart_finish();
+                        bootchart_count = 0;
+                    }
+                }
+                if (bootchart_count > 0) {
+                    remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
+                    if (timeout < 0 || timeout > remaining_time) {
+                        timeout = remaining_time;
+                    }
+                }
             }
         }
-#endif
 
         nr = poll(ufds, fd_count, timeout);
         if (nr <= 0)
diff --git a/init/init.h b/init/init.h
index a7615a3..eedec27 100644
--- a/init/init.h
+++ b/init/init.h
@@ -18,6 +18,7 @@
 #define _INIT_INIT_H
 
 #include <cutils/list.h>
+#include <cutils/iosched_policy.h>
 
 #include <sys/stat.h>
 
@@ -37,6 +38,11 @@
     char *args[1];
 };
 
+struct trigger {
+    struct listnode nlist;
+    const char *name;
+};
+
 struct action {
         /* node in list of all actions */
     struct listnode alist;
@@ -46,12 +52,15 @@
     struct listnode tlist;
 
     unsigned hash;
-    const char *name;
 
+        /* list of actions which triggers the commands*/
+    struct listnode triggers;
     struct listnode commands;
     struct command *current;
 };
 
+void build_triggers_string(char *name_str, int length, struct action *cur_action);
+
 struct socketinfo {
     struct socketinfo *next;
     const char *name;
@@ -114,7 +123,7 @@
     int nkeycodes;
     int keychord_id;
 
-    int ioprio_class;
+    IoSchedClass ioprio_class;
     int ioprio_pri;
 
     int nargs;
@@ -141,5 +150,6 @@
 extern struct selabel_handle *sehandle;
 extern struct selabel_handle *sehandle_prop;
 extern int selinux_reload_policy(void);
+void zap_stdio(void);
 
 #endif	/* _INIT_INIT_H */
diff --git a/init/init_parser.c b/init/init_parser.cpp
similarity index 78%
rename from init/init_parser.c
rename to init/init_parser.cpp
index 6466db2..65fc1a6 100644
--- a/init/init_parser.c
+++ b/init/init_parser.cpp
@@ -73,11 +73,50 @@
 #define kw_func(kw) (keyword_info[kw].func)
 #define kw_nargs(kw) (keyword_info[kw].nargs)
 
+void dump_parser_state() {
+    if (false) {
+        struct listnode* node;
+        list_for_each(node, &service_list) {
+            service* svc = node_to_item(node, struct service, slist);
+            INFO("service %s\n", svc->name);
+            INFO("  class '%s'\n", svc->classname);
+            INFO("  exec");
+            for (int n = 0; n < svc->nargs; n++) {
+                INFO(" '%s'", svc->args[n]);
+            }
+            INFO("\n");
+            for (socketinfo* si = svc->sockets; si; si = si->next) {
+                INFO("  socket %s %s 0%o\n", si->name, si->type, si->perm);
+            }
+        }
+
+        list_for_each(node, &action_list) {
+            action* act = node_to_item(node, struct action, alist);
+            INFO("on ");
+            char name_str[256] = "";
+            build_triggers_string(name_str, sizeof(name_str), act);
+            INFO("%s", name_str);
+            INFO("\n");
+
+            struct listnode* node2;
+            list_for_each(node2, &act->commands) {
+                command* cmd = node_to_item(node2, struct command, clist);
+                INFO("  %p", cmd->func);
+                for (int n = 0; n < cmd->nargs; n++) {
+                    INFO(" %s", cmd->args[n]);
+                }
+                INFO("\n");
+            }
+            INFO("\n");
+        }
+    }
+}
+
 static int lookup_keyword(const char *s)
 {
     switch (*s++) {
     case 'c':
-    if (!strcmp(s, "opy")) return K_copy;
+        if (!strcmp(s, "opy")) return K_copy;
         if (!strcmp(s, "apability")) return K_capability;
         if (!strcmp(s, "hdir")) return K_chdir;
         if (!strcmp(s, "hroot")) return K_chroot;
@@ -97,6 +136,7 @@
     case 'e':
         if (!strcmp(s, "nable")) return K_enable;
         if (!strcmp(s, "xec")) return K_exec;
+        if (!strcmp(s, "xeconce")) return K_execonce;
         if (!strcmp(s, "xport")) return K_export;
         break;
     case 'g':
@@ -131,6 +171,7 @@
         break;
     case 'p':
         if (!strcmp(s, "owerctl")) return K_powerctl;
+        break;
     case 'r':
         if (!strcmp(s, "estart")) return K_restart;
         if (!strcmp(s, "estorecon")) return K_restorecon;
@@ -169,8 +210,7 @@
     return K_UNKNOWN;
 }
 
-static void parse_line_no_op(struct parse_state *state, int nargs, char **args)
-{
+static void parse_line_no_op(struct parse_state*, int, char**) {
 }
 
 static int push_chars(char **dst, int *len, const char *chars, int cnt)
@@ -187,19 +227,14 @@
 
 int expand_props(char *dst, const char *src, int dst_size)
 {
-    int cnt = 0;
     char *dst_ptr = dst;
     const char *src_ptr = src;
-    int src_len;
-    int idx = 0;
     int ret = 0;
     int left = dst_size - 1;
 
     if (!src || !dst || dst_size == 0)
         return -1;
 
-    src_len = strlen(src);
-
     /* - variables can either be $x.y or ${x.y}, in case they are only part
      *   of the string.
      * - will accept $$ as a literal $.
@@ -294,8 +329,7 @@
 
 static void parse_import(struct parse_state *state, int nargs, char **args)
 {
-    struct listnode *import_list = state->priv;
-    struct import *import;
+    struct listnode *import_list = (listnode*) state->priv;
     char conf_file[PATH_MAX];
     int ret;
 
@@ -311,7 +345,7 @@
         return;
     }
 
-    import = calloc(1, sizeof(struct import));
+    struct import* import = (struct import*) calloc(1, sizeof(struct import));
     import->filename = strdup(conf_file);
     list_add_tail(import_list, &import->list);
     INFO("found import '%s', adding to import list", import->filename);
@@ -344,7 +378,7 @@
     state->parse_line = parse_line_no_op;
 }
 
-static void parse_config(const char *fn, char *s)
+static void parse_config(const char *fn, const std::string& data)
 {
     struct parse_state state;
     struct listnode import_list;
@@ -355,7 +389,7 @@
     nargs = 0;
     state.filename = fn;
     state.line = 0;
-    state.ptr = s;
+    state.ptr = strdup(data.c_str());  // TODO: fix this code!
     state.nexttoken = 0;
     state.parse_line = parse_line_no_op;
 
@@ -393,7 +427,6 @@
          struct import *import = node_to_item(node, struct import, list);
          int ret;
 
-         INFO("importing '%s'", import->filename);
          ret = init_parse_config_file(import->filename);
          if (ret)
              ERROR("could not import file '%s' from '%s'\n",
@@ -401,14 +434,15 @@
     }
 }
 
-int init_parse_config_file(const char *fn)
-{
-    char *data;
-    data = read_file(fn, 0);
-    if (!data) return -1;
+int init_parse_config_file(const char* path) {
+    INFO("Parsing %s...", path);
+    std::string data;
+    if (!read_file(path, &data)) {
+        return -1;
+    }
 
-    parse_config(fn, data);
-    DUMP();
+    parse_config(path, data);
+    dump_parser_state();
     return 0;
 }
 
@@ -504,83 +538,94 @@
 void action_for_each_trigger(const char *trigger,
                              void (*func)(struct action *act))
 {
-    struct listnode *node;
+    struct listnode *node, *node2;
     struct action *act;
+    struct trigger *cur_trigger;
+
     list_for_each(node, &action_list) {
         act = node_to_item(node, struct action, alist);
-        if (!strcmp(act->name, trigger)) {
-            func(act);
+        list_for_each(node2, &act->triggers) {
+            cur_trigger = node_to_item(node2, struct trigger, nlist);
+            if (!strcmp(cur_trigger->name, trigger)) {
+                func(act);
+            }
         }
     }
 }
 
+
 void queue_property_triggers(const char *name, const char *value)
 {
-    struct listnode *node;
+    struct listnode *node, *node2;
     struct action *act;
+    struct trigger *cur_trigger;
+    bool match;
+    int name_length;
+
     list_for_each(node, &action_list) {
         act = node_to_item(node, struct action, alist);
-        if (!strncmp(act->name, "property:", strlen("property:"))) {
-            const char *test = act->name + strlen("property:");
-            int name_length = strlen(name);
+            match = !name;
+        list_for_each(node2, &act->triggers) {
+            cur_trigger = node_to_item(node2, struct trigger, nlist);
+            if (!strncmp(cur_trigger->name, "property:", strlen("property:"))) {
+                const char *test = cur_trigger->name + strlen("property:");
+                if (!match) {
+                    name_length = strlen(name);
+                    if (!strncmp(name, test, name_length) &&
+                        test[name_length] == '=' &&
+                        (!strcmp(test + name_length + 1, value) ||
+                        !strcmp(test + name_length + 1, "*"))) {
+                        match = true;
+                        continue;
+                    }
+                } else {
+                     const char* equals = strchr(test, '=');
+                     if (equals) {
+                         char prop_name[PROP_NAME_MAX + 1];
+                         char value[PROP_VALUE_MAX];
+                         int length = equals - test;
+                         if (length <= PROP_NAME_MAX) {
+                             int ret;
+                             memcpy(prop_name, test, length);
+                             prop_name[length] = 0;
 
-            if (!strncmp(name, test, name_length) &&
-                    test[name_length] == '=' &&
-                    (!strcmp(test + name_length + 1, value) ||
-                     !strcmp(test + name_length + 1, "*"))) {
-                action_add_queue_tail(act);
-            }
+                             /* does the property exist, and match the trigger value? */
+                             ret = property_get(prop_name, value);
+                             if (ret > 0 && (!strcmp(equals + 1, value) ||
+                                !strcmp(equals + 1, "*"))) {
+                                 continue;
+                             }
+                         }
+                     }
+                 }
+             }
+             match = false;
+             break;
+        }
+        if (match) {
+            action_add_queue_tail(act);
         }
     }
 }
 
 void queue_all_property_triggers()
 {
-    struct listnode *node;
-    struct action *act;
-    list_for_each(node, &action_list) {
-        act = node_to_item(node, struct action, alist);
-        if (!strncmp(act->name, "property:", strlen("property:"))) {
-            /* parse property name and value
-               syntax is property:<name>=<value> */
-            const char* name = act->name + strlen("property:");
-            const char* equals = strchr(name, '=');
-            if (equals) {
-                char prop_name[PROP_NAME_MAX + 1];
-                char value[PROP_VALUE_MAX];
-                int length = equals - name;
-                if (length > PROP_NAME_MAX) {
-                    ERROR("property name too long in trigger %s", act->name);
-                } else {
-                    int ret;
-                    memcpy(prop_name, name, length);
-                    prop_name[length] = 0;
-
-                    /* does the property exist, and match the trigger value? */
-                    ret = property_get(prop_name, value);
-                    if (ret > 0 && (!strcmp(equals + 1, value) ||
-                                    !strcmp(equals + 1, "*"))) {
-                        action_add_queue_tail(act);
-                    }
-                }
-            }
-        }
-    }
+    queue_property_triggers(NULL, NULL);
 }
 
-void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
+void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
 {
-    struct action *act;
-    struct command *cmd;
-
-    act = calloc(1, sizeof(*act));
-    act->name = name;
+    action* act = (action*) calloc(1, sizeof(*act));
+    trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
+    cur_trigger->name = name;
+    list_init(&act->triggers);
+    list_add_tail(&act->triggers, &cur_trigger->nlist);
     list_init(&act->commands);
     list_init(&act->qlist);
 
-    cmd = calloc(1, sizeof(*cmd));
+    command* cmd = (command*) calloc(1, sizeof(*cmd));
     cmd->func = func;
-    cmd->args[0] = name;
+    cmd->args[0] = const_cast<char*>(name);
     cmd->nargs = 1;
     list_add_tail(&act->commands, &cmd->clist);
 
@@ -615,7 +660,6 @@
 
 static void *parse_service(struct parse_state *state, int nargs, char **args)
 {
-    struct service *svc;
     if (nargs < 3) {
         parse_error(state, "services must have a name and a program\n");
         return 0;
@@ -625,14 +669,14 @@
         return 0;
     }
 
-    svc = service_find_by_name(args[1]);
+    service* svc = (service*) service_find_by_name(args[1]);
     if (svc) {
         parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
         return 0;
     }
 
     nargs -= 2;
-    svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
+    svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
     if (!svc) {
         parse_error(state, "out of memory\n");
         return 0;
@@ -640,9 +684,12 @@
     svc->name = args[1];
     svc->classname = "default";
     memcpy(svc->args, args + 2, sizeof(char*) * nargs);
+    trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
     svc->args[nargs] = 0;
     svc->nargs = nargs;
-    svc->onrestart.name = "onrestart";
+    list_init(&svc->onrestart.triggers);
+    cur_trigger->name = "onrestart";
+    list_add_tail(&svc->onrestart.triggers, &cur_trigger->nlist);
     list_init(&svc->onrestart.commands);
     list_add_tail(&service_list, &svc->slist);
     return svc;
@@ -650,7 +697,7 @@
 
 static void parse_line_service(struct parse_state *state, int nargs, char **args)
 {
-    struct service *svc = state->context;
+    struct service *svc = (service*) state->context;
     struct command *cmd;
     int i, kw, kw_nargs;
 
@@ -719,7 +766,7 @@
         if (nargs < 2) {
             parse_error(state, "keycodes option requires atleast one keycode\n");
         } else {
-            svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
+            svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
             if (!svc->keycodes) {
                 parse_error(state, "could not allocate keycodes\n");
             } else {
@@ -748,7 +795,7 @@
             break;
         }
 
-        cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
+        cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
         cmd->func = kw_func(kw);
         cmd->nargs = nargs;
         memcpy(cmd->args, args, sizeof(char*) * nargs);
@@ -758,12 +805,11 @@
         svc->flags |= SVC_CRITICAL;
         break;
     case K_setenv: { /* name value */
-        struct svcenvinfo *ei;
         if (nargs < 3) {
             parse_error(state, "setenv option requires name and value arguments\n");
             break;
         }
-        ei = calloc(1, sizeof(*ei));
+        svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
         if (!ei) {
             parse_error(state, "out of memory\n");
             break;
@@ -775,7 +821,6 @@
         break;
     }
     case K_socket: {/* name type perm [ uid gid context ] */
-        struct socketinfo *si;
         if (nargs < 4) {
             parse_error(state, "socket option requires name, type, perm arguments\n");
             break;
@@ -785,7 +830,7 @@
             parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
             break;
         }
-        si = calloc(1, sizeof(*si));
+        socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
         if (!si) {
             parse_error(state, "out of memory\n");
             break;
@@ -825,17 +870,29 @@
 
 static void *parse_action(struct parse_state *state, int nargs, char **args)
 {
-    struct action *act;
+    struct trigger *cur_trigger;
+    int i;
     if (nargs < 2) {
         parse_error(state, "actions must have a trigger\n");
         return 0;
     }
-    if (nargs > 2) {
-        parse_error(state, "actions may not have extra parameters\n");
-        return 0;
+
+    action* act = (action*) calloc(1, sizeof(*act));
+    list_init(&act->triggers);
+
+    for (i = 1; i < nargs; i++) {
+        if (!(i % 2)) {
+            if (strcmp(args[i], "&&")) {
+                parse_error(state, "& is the only symbol allowed to concatenate actions\n");
+                return 0;
+            } else
+                continue;
+        }
+        cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
+        cur_trigger->name = args[i];
+        list_add_tail(&act->triggers, &cur_trigger->nlist);
     }
-    act = calloc(1, sizeof(*act));
-    act->name = args[1];
+
     list_init(&act->commands);
     list_init(&act->qlist);
     list_add_tail(&action_list, &act->alist);
@@ -845,9 +902,7 @@
 
 static void parse_line_action(struct parse_state* state, int nargs, char **args)
 {
-    struct command *cmd;
-    struct action *act = state->context;
-    int (*func)(int nargs, char **args);
+    struct action *act = (action*) state->context;
     int kw, n;
 
     if (nargs == 0) {
@@ -866,7 +921,7 @@
             n > 2 ? "arguments" : "argument");
         return;
     }
-    cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
+    command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
     cmd->func = kw_func(kw);
     cmd->line = state->line;
     cmd->filename = state->filename;
diff --git a/init/init_parser.h b/init/init_parser.h
index b078cad..0047da7 100644
--- a/init/init_parser.h
+++ b/init/init_parser.h
@@ -28,7 +28,7 @@
 int action_queue_empty(void);
 void queue_property_triggers(const char *name, const char *value);
 void queue_all_property_triggers();
-void queue_builtin_action(int (*func)(int nargs, char **args), char *name);
+void queue_builtin_action(int (*func)(int nargs, char **args), const char *name);
 
 int init_parse_config_file(const char *fn);
 int expand_props(char *dst, const char *src, int len);
diff --git a/init/keychords.c b/init/keychords.cpp
similarity index 95%
rename from init/keychords.c
rename to init/keychords.cpp
index 4a64042..d6464bd 100644
--- a/init/keychords.c
+++ b/init/keychords.cpp
@@ -40,7 +40,7 @@
     if (svc->keycodes) {
         /* add a new keychord to the list */
         size = sizeof(*keychord) + svc->nkeycodes * sizeof(keychord->keycodes[0]);
-        keychords = realloc(keychords, keychords_length + size);
+        keychords = (input_keychord*) realloc(keychords, keychords_length + size);
         if (!keychords) {
             ERROR("could not allocate keychords\n");
             keychords_length = 0;
@@ -72,12 +72,11 @@
     if (!keychords)
         return;
 
-    fd = open("/dev/keychord", O_RDWR);
+    fd = open("/dev/keychord", O_RDWR | O_CLOEXEC);
     if (fd < 0) {
         ERROR("could not open /dev/keychord\n");
         return;
     }
-    fcntl(fd, F_SETFD, FD_CLOEXEC);
 
     ret = write(fd, keychords, keychords_length);
     if (ret != keychords_length) {
diff --git a/init/keywords.h b/init/keywords.h
index 2d97e5b..0805cdd 100644
--- a/init/keywords.h
+++ b/init/keywords.h
@@ -1,4 +1,3 @@
-
 #ifndef KEYWORD
 int do_chroot(int nargs, char **args);
 int do_chdir(int nargs, char **args);
@@ -8,6 +7,7 @@
 int do_domainname(int nargs, char **args);
 int do_enable(int nargs, char **args);
 int do_exec(int nargs, char **args);
+int do_execonce(int nargs, char **args);
 int do_export(int nargs, char **args);
 int do_hostname(int nargs, char **args);
 int do_ifup(int nargs, char **args);
@@ -59,6 +59,7 @@
     KEYWORD(domainname,  COMMAND, 1, do_domainname)
     KEYWORD(enable,      COMMAND, 1, do_enable)
     KEYWORD(exec,        COMMAND, 1, do_exec)
+    KEYWORD(execonce,    COMMAND, 1, do_execonce)
     KEYWORD(export,      COMMAND, 2, do_export)
     KEYWORD(group,       OPTION,  0, 0)
     KEYWORD(hostname,    COMMAND, 1, do_hostname)
@@ -110,4 +111,3 @@
 #undef __MAKE_KEYWORD_ENUM__
 #undef KEYWORD
 #endif
-
diff --git a/init/parser.c b/init/parser.cpp
similarity index 73%
rename from init/parser.c
rename to init/parser.cpp
index 48e7aec..8193729 100644
--- a/init/parser.c
+++ b/init/parser.cpp
@@ -1,59 +1,17 @@
-#include <stdio.h>
+#include "parser.h"
+
 #include <stdarg.h>
+#include <stdio.h>
 #include <string.h>
 
-#include "parser.h"
 #include "log.h"
 
-#define RAW(x...) log_write(6, x)
-
-void DUMP(void)
-{
-#if 0
-    struct service *svc;
-    struct action *act;
-    struct command *cmd;
-    struct listnode *node;
-    struct listnode *node2;
-    struct socketinfo *si;
-    int n;
-    
-    list_for_each(node, &service_list) {
-        svc = node_to_item(node, struct service, slist);
-        RAW("service %s\n", svc->name);
-        RAW("  class '%s'\n", svc->classname);
-        RAW("  exec");
-        for (n = 0; n < svc->nargs; n++) {
-            RAW(" '%s'", svc->args[n]);
-        }
-        RAW("\n");
-        for (si = svc->sockets; si; si = si->next) {
-            RAW("  socket %s %s 0%o\n", si->name, si->type, si->perm);
-        }
-    }
-
-    list_for_each(node, &action_list) {
-        act = node_to_item(node, struct action, alist);
-        RAW("on %s\n", act->name);
-        list_for_each(node2, &act->commands) {
-            cmd = node_to_item(node2, struct command, clist);
-            RAW("  %p", cmd->func);
-            for (n = 0; n < cmd->nargs; n++) {
-                RAW(" %s", cmd->args[n]);
-            }
-            RAW("\n");
-        }
-        RAW("\n");
-    }
-#endif       
-}
-
 void parse_error(struct parse_state *state, const char *fmt, ...)
 {
     va_list ap;
     char buf[128];
     int off;
-    
+
     snprintf(buf, 128, "%s: %d: ", state->filename, state->line);
     buf[127] = 0;
     off = strlen(buf);
diff --git a/init/parser.h b/init/parser.h
index a58272a..95e1164 100644
--- a/init/parser.h
+++ b/init/parser.h
@@ -33,7 +33,7 @@
     void *priv;
 };
 
-void DUMP(void);
+void dump_parser_state(void);
 int next_token(struct parse_state *state);
 void parse_error(struct parse_state *state, const char *fmt, ...);
 
diff --git a/init/property_service.c b/init/property_service.cpp
similarity index 94%
rename from init/property_service.c
rename to init/property_service.cpp
index 91ef251..05c03d6 100644
--- a/init/property_service.c
+++ b/init/property_service.cpp
@@ -30,6 +30,8 @@
 #include <cutils/sockets.h>
 #include <cutils/multiuser.h>
 
+#include <utils/file.h>
+
 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
 #include <sys/_system_properties.h>
 
@@ -56,14 +58,13 @@
 
 static int property_set_fd = -1;
 
-typedef struct {
+struct workspace {
     size_t size;
     int fd;
-} workspace;
+};
 
 static int init_workspace(workspace *w, size_t size)
 {
-    void *data;
     int fd = open(PROP_FILENAME, O_RDONLY | O_NOFOLLOW);
     if (fd < 0)
         return -1;
@@ -98,8 +99,6 @@
         return 1;
 
     char *tctx = NULL;
-    const char *class = "property_service";
-    const char *perm = "set";
     int result = 0;
 
     if (!sctx)
@@ -111,7 +110,7 @@
     if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
         goto err;
 
-    if (selinux_check_access(sctx, tctx, class, perm, (void*) name) == 0)
+    if (selinux_check_access(sctx, tctx, "property_service", "set", (void*) name) == 0)
         result = 1;
 
     freecon(tctx);
@@ -142,9 +141,6 @@
  */
 static int check_perms(const char *name, char *sctx)
 {
-    int i;
-    unsigned int app_id;
-
     if(!strncmp(name, "ro.", 3))
         name +=3;
 
@@ -261,7 +257,6 @@
     prop_msg msg;
     int s;
     int r;
-    int res;
     struct ucred cr;
     struct sockaddr_un addr;
     socklen_t addr_size = sizeof(addr);
@@ -423,14 +418,9 @@
  */
 static void load_properties_from_file(const char *fn, const char *filter)
 {
-    char *data;
-    unsigned sz;
-
-    data = read_file(fn, &sz);
-
-    if(data != 0) {
-        load_properties(data, filter);
-        free(data);
+    std::string data;
+    if (read_file(fn, &data)) {
+        load_properties(&data[0], filter);
     }
 }
 
@@ -448,10 +438,8 @@
         while ((entry = readdir(dir)) != NULL) {
             if (strncmp("persist.", entry->d_name, strlen("persist.")))
                 continue;
-#if HAVE_DIRENT_D_TYPE
             if (entry->d_type != DT_REG)
                 continue;
-#endif
             /* open the file and read the property value */
             fd = openat(dir_fd, entry->d_name, O_RDONLY | O_NOFOLLOW);
             if (fd < 0) {
@@ -471,9 +459,9 @@
                     || (sb.st_uid != 0)
                     || (sb.st_gid != 0)
                     || (sb.st_nlink != 1)) {
-                ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%d mode=%o)\n",
+                ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%u mode=%o)\n",
                       entry->d_name, (unsigned int)sb.st_uid, (unsigned int)sb.st_gid,
-                      sb.st_nlink, sb.st_mode);
+                      (unsigned int)sb.st_nlink, sb.st_mode);
                 close(fd);
                 continue;
             }
@@ -512,15 +500,13 @@
 }
 
 static void load_override_properties() {
-#ifdef ALLOW_LOCAL_PROP_OVERRIDE
-    char debuggable[PROP_VALUE_MAX];
-    int ret;
-
-    ret = property_get("ro.debuggable", debuggable);
-    if (ret && (strcmp(debuggable, "1") == 0)) {
-        load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
+    if (ALLOW_LOCAL_PROP_OVERRIDE) {
+        char debuggable[PROP_VALUE_MAX];
+        int ret = property_get("ro.debuggable", debuggable);
+        if (ret && (strcmp(debuggable, "1") == 0)) {
+            load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE, NULL);
+        }
     }
-#endif /* ALLOW_LOCAL_PROP_OVERRIDE */
 }
 
 
@@ -541,6 +527,7 @@
     load_properties_from_file(PROP_PATH_SYSTEM_BUILD, NULL);
     load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT, NULL);
     load_properties_from_file(PROP_PATH_VENDOR_BUILD, NULL);
+    load_properties_from_file(PROP_PATH_BOOTIMAGE_BUILD, NULL);
     load_properties_from_file(PROP_PATH_FACTORY, "ro.*");
 
     load_override_properties();
diff --git a/init/property_service.h b/init/property_service.h
index 730495e..ff8b063 100644
--- a/init/property_service.h
+++ b/init/property_service.h
@@ -32,13 +32,19 @@
 extern int properties_inited();
 int get_property_set_fd(void);
 
+#ifndef __clang__
 extern void __property_get_size_error()
     __attribute__((__error__("property_get called with too small buffer")));
+#else
+extern void __property_get_size_error();
+#endif
 
 static inline
 __attribute__ ((always_inline))
 __attribute__ ((gnu_inline))
+#ifndef __clang__
 __attribute__ ((artificial))
+#endif
 int property_get(const char *name, char *value)
 {
     size_t value_len = __builtin_object_size(value, 0);
diff --git a/init/readme.txt b/init/readme.txt
index 26be536..16a9186 100644
--- a/init/readme.txt
+++ b/init/readme.txt
@@ -123,23 +123,27 @@
    Triggers of this form occur when the property <name> is set
    to the specific value <value>.
 
-device-added-<path>
-device-removed-<path>
-   Triggers of these forms occur when a device node is added
-   or removed.
+   One can also test multiple properties to execute a group
+   of commands. For example:
 
-service-exited-<name>
-   Triggers of this form occur when the specified service exits.
+   on property:test.a=1 && property:test.b=1
+       setprop test.c 1
 
+   The above stub sets test.c to 1 only when
+   both test.a=1 and test.b=1
 
 Commands
 --------
 
 exec <path> [ <argument> ]*
+   This command is not implemented.
+
+execonce <path> [ <argument> ]*
    Fork and execute a program (<path>).  This will block until
-   the program completes execution.  It is best to avoid exec
-   as unlike the builtin commands, it runs the risk of getting
-   init "stuck". (??? maybe there should be a timeout?)
+   the program completes execution.  This command can be run at most
+   once during init's lifetime. Subsequent invocations are ignored.
+   It is best to avoid exec as unlike the builtin commands, it runs
+   the risk of getting init "stuck".
 
 export <name> <value>
    Set the environment variable <name> equal to <value> in the
@@ -187,10 +191,12 @@
      on property:ro.boot.myfancyhardware=1
         enable my_fancy_service_for_my_fancy_hardware
 
-
 insmod <path>
    Install the module at <path>
 
+loglevel <level>
+   Sets the kernel log level to level. Properties are expanded within <level>.
+
 mkdir <path> [mode] [owner] [group]
    Create a directory at <path>, optionally with the given mode, owner, and
    group. If not provided, the directory is created with permissions 755 and
@@ -211,8 +217,6 @@
 restorecon_recursive <path> [ <path> ]*
    Recursively restore the directory tree named by <path> to the
    security contexts specified in the file_contexts configuration.
-   Do NOT use this with paths leading to shell-writable or app-writable
-   directories, e.g. /data/local/tmp, /data/data or any prefix thereof.
 
 setcon <securitycontext>
    Set the current process security context to the specified string.
@@ -227,7 +231,8 @@
    TBD
 
 setprop <name> <value>
-   Set system property <name> to <value>.
+   Set system property <name> to <value>. Properties are expanded
+   within <value>.
 
 setrlimit <resource> <cur> <max>
    Set the rlimit for a resource.
@@ -257,9 +262,10 @@
   or the timeout has been reached. If timeout is not specified it
   currently defaults to five seconds.
 
-write <path> <string>
-   Open the file at <path> and write a string to it with write(2)
-   without appending.
+write <path> <content>
+   Open the file at <path> and write a string to it with write(2).
+   If the file does not exist, it will be created. If it does exist,
+   it will be truncated. Properties are expanded within <content>.
 
 
 Properties
@@ -327,12 +333,6 @@
    user system
    group system
 
-on device-added-/dev/compass
-   start akmd
-
-on device-removed-/dev/compass
-   stop akmd
-
 service akmd /sbin/akmd
    disabled
    user akmd
@@ -342,8 +342,23 @@
 ---------------
 By default, programs executed by init will drop stdout and stderr into
 /dev/null. To help with debugging, you can execute your program via the
-Andoird program logwrapper. This will redirect stdout/stderr into the
+Android program logwrapper. This will redirect stdout/stderr into the
 Android logging system (accessed via logcat).
 
 For example
 service akmd /system/bin/logwrapper /sbin/akmd
+
+For quicker turnaround when working on init itself, use:
+
+  mm
+  m ramdisk-nodeps
+  m bootimage-nodeps
+  adb reboot bootloader
+  fastboot boot $ANDROID_PRODUCT_OUT/boot.img
+
+Alternatively, use the emulator:
+
+  emulator -partition-size 1024 -verbose -show-kernel -no-window
+
+You might want to call klog_set_level(6) after the klog_init() call
+so you see the kernel logging in dmesg (or the emulator output).
diff --git a/init/signal_handler.c b/init/signal_handler.cpp
similarity index 92%
rename from init/signal_handler.c
rename to init/signal_handler.cpp
index 7e8e1a7..c0898fb 100644
--- a/init/signal_handler.c
+++ b/init/signal_handler.cpp
@@ -43,7 +43,6 @@
 
 static int wait_for_one_process(int block)
 {
-    pid_t pid;
     int status;
     struct service *svc;
     struct socketinfo *si;
@@ -51,7 +50,7 @@
     struct listnode *node;
     struct command *cmd;
 
-    while ( (pid = waitpid(-1, &status, block ? 0 : WNOHANG)) == -1 && errno == EINTR );
+    pid_t pid = TEMP_FAILURE_RETRY(waitpid(-1, &status, block ? 0 : WNOHANG));
     if (pid <= 0) return -1;
     INFO("waitpid returned pid %d, status = %08x\n", pid, status);
 
@@ -147,13 +146,9 @@
     sigaction(SIGCHLD, &act, 0);
 
     /* create a signalling mechanism for the sigchld handler */
-    if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0) {
+    if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, s) == 0) {
         signal_fd = s[0];
         signal_recv_fd = s[1];
-        fcntl(s[0], F_SETFD, FD_CLOEXEC);
-        fcntl(s[0], F_SETFL, O_NONBLOCK);
-        fcntl(s[1], F_SETFD, FD_CLOEXEC);
-        fcntl(s[1], F_SETFL, O_NONBLOCK);
     }
 
     handle_signal();
diff --git a/init/ueventd.c b/init/ueventd.cpp
similarity index 95%
rename from init/ueventd.c
rename to init/ueventd.cpp
index 833e4fd..d56b91a 100644
--- a/init/ueventd.c
+++ b/init/ueventd.cpp
@@ -70,12 +70,12 @@
 
     open_devnull_stdio();
     klog_init();
-#if LOG_UEVENTS
-    /* Ensure we're at a logging level that will show the events */
-    if (klog_get_level() < KLOG_INFO_LEVEL) {
-        klog_set_level(KLOG_INFO_LEVEL);
+    if (LOG_UEVENTS) {
+        /* Ensure we're at a logging level that will show the events */
+        if (klog_get_level() < KLOG_INFO_LEVEL) {
+            klog_set_level(KLOG_INFO_LEVEL);
+        }
     }
-#endif
 
     union selinux_callback cb;
     cb.func_log = log_callback;
@@ -108,6 +108,8 @@
         if (ufd.revents & POLLIN)
                handle_device_fd();
     }
+
+    return 0;
 }
 
 static int get_android_id(const char *id)
diff --git a/init/ueventd.h b/init/ueventd.h
index 0a454c5..d12d7fe 100644
--- a/init/ueventd.h
+++ b/init/ueventd.h
@@ -20,16 +20,18 @@
 #include <cutils/list.h>
 #include <sys/types.h>
 
+enum devname_src_t {
+    DEVNAME_UNKNOWN = 0,
+    DEVNAME_UEVENT_DEVNAME,
+    DEVNAME_UEVENT_DEVPATH,
+};
+
 struct ueventd_subsystem {
     struct listnode slist;
 
     const char *name;
-    enum {
-        DEVNAME_UNKNOWN = 0,
-        DEVNAME_UEVENT_DEVNAME,
-        DEVNAME_UEVENT_DEVPATH,
-    } devname_src;
     const char *dirname;
+    devname_src_t devname_src;
 };
 
 int ueventd_main(int argc, char **argv);
diff --git a/init/ueventd_parser.c b/init/ueventd_parser.cpp
similarity index 88%
rename from init/ueventd_parser.c
rename to init/ueventd_parser.cpp
index e447006..7a4841f 100644
--- a/init/ueventd_parser.c
+++ b/init/ueventd_parser.cpp
@@ -67,9 +67,7 @@
     return K_UNKNOWN;
 }
 
-static void parse_line_no_op(struct parse_state *state __attribute__((unused)),
-        int nargs __attribute__((unused)), char **args  __attribute__((unused)))
-{
+static void parse_line_no_op(struct parse_state*, int, char**) {
 }
 
 static int valid_name(const char *name)
@@ -97,24 +95,20 @@
     return 0;
 }
 
-static void *parse_subsystem(struct parse_state *state,
-        int nargs __attribute__((unused)), char **args)
-{
-    struct ueventd_subsystem *s;
-
+static void *parse_subsystem(parse_state* state, int /*nargs*/, char** args) {
     if (!valid_name(args[1])) {
         parse_error(state, "invalid subsystem name '%s'\n", args[1]);
         return 0;
     }
 
-    s = ueventd_subsystem_find_by_name(args[1]);
+    ueventd_subsystem* s = ueventd_subsystem_find_by_name(args[1]);
     if (s) {
         parse_error(state, "ignored duplicate definition of subsystem '%s'\n",
                 args[1]);
         return 0;
     }
 
-    s = calloc(1, sizeof(*s));
+    s = (ueventd_subsystem*) calloc(1, sizeof(*s));
     if (!s) {
         parse_error(state, "out of memory\n");
         return 0;
@@ -128,7 +122,7 @@
 static void parse_line_subsystem(struct parse_state *state, int nargs,
         char **args)
 {
-    struct ueventd_subsystem *s = state->context;
+    struct ueventd_subsystem *s = (ueventd_subsystem*) state->context;
     int kw;
 
     if (nargs == 0) {
@@ -197,7 +191,7 @@
     }
 }
 
-static void parse_config(const char *fn, char *s)
+static void parse_config(const char *fn, const std::string& data)
 {
     struct parse_state state;
     char *args[UEVENTD_PARSER_MAXARGS];
@@ -205,7 +199,7 @@
     nargs = 0;
     state.filename = fn;
     state.line = 1;
-    state.ptr = s;
+    state.ptr = strdup(data.c_str());  // TODO: fix this code!
     state.nexttoken = 0;
     state.parse_line = parse_line_no_op;
     for (;;) {
@@ -232,17 +226,16 @@
 
 int ueventd_parse_config_file(const char *fn)
 {
-    char *data;
-    data = read_file(fn, 0);
-    if (!data) return -1;
+    std::string data;
+    if (!read_file(fn, &data)) {
+        return -1;
+    }
 
     parse_config(fn, data);
-    DUMP();
+    dump_parser_state();
     return 0;
 }
 
-static void parse_line_device(struct parse_state *state __attribute__((unused)),
-        int nargs, char **args)
-{
+static void parse_line_device(parse_state*, int nargs, char** args) {
     set_device_permission(nargs, args);
 }
diff --git a/init/util.c b/init/util.cpp
similarity index 80%
rename from init/util.c
rename to init/util.cpp
index e1a3ee3..3dddb15 100644
--- a/init/util.c
+++ b/init/util.cpp
@@ -35,6 +35,8 @@
 /* for ANDROID_SOCKET_* */
 #include <cutils/sockets.h>
 
+#include <utils/file.h>
+
 #include <private/android_filesystem_config.h>
 
 #include "init.h"
@@ -146,48 +148,42 @@
     return -1;
 }
 
-/* reads a file, making sure it is terminated with \n \0 */
-void *read_file(const char *fn, unsigned *_sz)
-{
-    char *data;
-    int sz;
-    int fd;
+bool read_file(const char* path, std::string* content) {
+    content->clear();
+
+    int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC));
+    if (fd == -1) {
+        return false;
+    }
+
+    // For security reasons, disallow world-writable
+    // or group-writable files.
     struct stat sb;
-
-    data = 0;
-    fd = open(fn, O_RDONLY);
-    if(fd < 0) return 0;
-
-    // for security reasons, disallow world-writable
-    // or group-writable files
-    if (fstat(fd, &sb) < 0) {
-        ERROR("fstat failed for '%s'\n", fn);
-        goto oops;
+    if (fstat(fd, &sb) == -1) {
+        ERROR("fstat failed for '%s': %s\n", path, strerror(errno));
+        return false;
     }
     if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
-        ERROR("skipping insecure file '%s'\n", fn);
-        goto oops;
+        ERROR("skipping insecure file '%s'\n", path);
+        return false;
     }
 
-    sz = lseek(fd, 0, SEEK_END);
-    if(sz < 0) goto oops;
+    bool okay = android::ReadFdToString(fd, content);
+    TEMP_FAILURE_RETRY(close(fd));
+    if (okay) {
+        content->append("\n", 1);
+    }
+    return okay;
+}
 
-    if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
-
-    data = (char*) malloc(sz + 2);
-    if(data == 0) goto oops;
-
-    if(read(fd, data, sz) != sz) goto oops;
-    close(fd);
-    data[sz] = '\n';
-    data[sz+1] = 0;
-    if(_sz) *_sz = sz;
-    return data;
-
-oops:
-    close(fd);
-    if(data != 0) free(data);
-    return 0;
+int write_file(const char* path, const char* content) {
+    int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600));
+    if (fd == -1) {
+        return -errno;
+    }
+    int result = android::WriteStringToFd(content, fd) ? 0 : -errno;
+    TEMP_FAILURE_RETRY(close(fd));
+    return result;
 }
 
 #define MAX_MTD_PARTITIONS 16
@@ -207,7 +203,7 @@
     ssize_t pmtdsize;
     int r;
 
-    fd = open("/proc/mtd", O_RDONLY);
+    fd = open("/proc/mtd", O_RDONLY|O_CLOEXEC);
     if (fd < 0)
         return;
 
@@ -404,72 +400,37 @@
     exit(1);
 }
 
-void get_hardware_name(char *hardware, unsigned int *revision)
-{
-    const char *cpuinfo = "/proc/cpuinfo";
-    char *data = NULL;
-    size_t len = 0, limit = 1024;
-    int fd, n;
-    char *x, *hw, *rev;
+void get_hardware_name(char *hardware, unsigned int *revision) {
+  // Hardware string was provided on kernel command line.
+  if (hardware[0]) {
+    return;
+  }
 
-    /* Hardware string was provided on kernel command line */
-    if (hardware[0])
-        return;
-
-    fd = open(cpuinfo, O_RDONLY);
-    if (fd < 0) return;
-
-    for (;;) {
-        x = realloc(data, limit);
-        if (!x) {
-            ERROR("Failed to allocate memory to read %s\n", cpuinfo);
-            goto done;
+  FILE* fp = fopen("/proc/cpuinfo", "re");
+  if (fp == NULL) {
+    return;
+  }
+  char buf[1024];
+  while (fgets(buf, sizeof(buf), fp) != NULL) {
+    if (strncmp(buf, "Hardware", 8) == 0) {
+      const char* hw = strstr(buf, ": ");
+      if (hw) {
+        hw += 2;
+        size_t n = 0;
+        while (*hw) {
+          if (!isspace(*hw)) {
+            hardware[n++] = tolower(*hw);
+          }
+          hw++;
+          if (n == 31) break;
         }
-        data = x;
-
-        n = read(fd, data + len, limit - len);
-        if (n < 0) {
-            ERROR("Failed reading %s: %s (%d)\n", cpuinfo, strerror(errno), errno);
-            goto done;
-        }
-        len += n;
-
-        if (len < limit)
-            break;
-
-        /* We filled the buffer, so increase size and loop to read more */
-        limit *= 2;
+        hardware[n] = 0;
+      }
+    } else if (strncmp(buf, "Revision", 8) == 0) {
+      sscanf(buf, "Revision : %ux", revision);
     }
-
-    data[len] = 0;
-    hw = strstr(data, "\nHardware");
-    rev = strstr(data, "\nRevision");
-
-    if (hw) {
-        x = strstr(hw, ": ");
-        if (x) {
-            x += 2;
-            n = 0;
-            while (*x && *x != '\n') {
-                if (!isspace(*x))
-                    hardware[n++] = tolower(*x);
-                x++;
-                if (n == 31) break;
-            }
-            hardware[n] = 0;
-        }
-    }
-
-    if (rev) {
-        x = strstr(rev, ": ");
-        if (x) {
-            *revision = strtoul(x + 2, 0, 16);
-        }
-    }
-
-done:
-    close(fd);
-    free(data);
+  }
+  fclose(fp);
 }
 
 void import_kernel_cmdline(int in_qemu,
@@ -479,7 +440,7 @@
     char *ptr;
     int fd;
 
-    fd = open("/proc/cmdline", O_RDONLY);
+    fd = open("/proc/cmdline", O_RDONLY | O_CLOEXEC);
     if (fd >= 0) {
         int n = read(fd, cmdline, sizeof(cmdline) - 1);
         if (n < 0) n = 0;
diff --git a/init/util.h b/init/util.h
index 04b8129..77da3ac 100644
--- a/init/util.h
+++ b/init/util.h
@@ -20,14 +20,19 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
+#include <string>
+
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
 
-static const char *coldboot_done = "/dev/.coldboot_done";
+#define COLDBOOT_DONE "/dev/.coldboot_done"
 
 int mtd_name_to_number(const char *name);
 int create_socket(const char *name, int type, mode_t perm,
                   uid_t uid, gid_t gid, const char *socketcon);
-void *read_file(const char *fn, unsigned *_sz);
+
+bool read_file(const char* path, std::string* content);
+int write_file(const char* path, const char* content);
+
 time_t gettime(void);
 unsigned int decode_uid(const char *s);
 
diff --git a/init/util_test.cpp b/init/util_test.cpp
new file mode 100644
index 0000000..e9a1581
--- /dev/null
+++ b/init/util_test.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "util.h"
+
+#include <errno.h>
+#include <gtest/gtest.h>
+
+TEST(util, read_file_ENOENT) {
+  std::string s("hello");
+  errno = 0;
+  EXPECT_FALSE(read_file("/proc/does-not-exist", &s));
+  EXPECT_EQ(ENOENT, errno);
+  EXPECT_EQ("", s); // s was cleared.
+}
+
+TEST(util, read_file_success) {
+  std::string s("hello");
+  EXPECT_TRUE(read_file("/proc/version", &s));
+  EXPECT_GT(s.length(), 6U);
+  EXPECT_EQ('\n', s[s.length() - 1]);
+  s[5] = 0;
+  EXPECT_STREQ("Linux", s.c_str());
+}
diff --git a/init/watchdogd.c b/init/watchdogd.cpp
similarity index 96%
rename from init/watchdogd.c
rename to init/watchdogd.cpp
index fb53836..0790811 100644
--- a/init/watchdogd.c
+++ b/init/watchdogd.cpp
@@ -18,6 +18,7 @@
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <linux/watchdog.h>
 
@@ -47,7 +48,7 @@
 
     timeout = interval + margin;
 
-    fd = open(DEV_NAME, O_RDWR);
+    fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
     if (fd < 0) {
         ERROR("watchdogd: Failed to open %s: %s\n", DEV_NAME, strerror(errno));
         return 1;
diff --git a/libbacktrace/Android.build.mk b/libbacktrace/Android.build.mk
index 2f55645..7e1cd53 100644
--- a/libbacktrace/Android.build.mk
+++ b/libbacktrace/Android.build.mk
@@ -61,18 +61,13 @@
     $($(module)_ldlibs_$(build_type)) \
 
 ifeq ($(build_type),target)
-  ifneq ($($(module)_libc++),)
-    include external/libcxx/libcxx.mk
-  else
-    include external/stlport/libstlport.mk
-  endif
-
   include $(BUILD_$(build_target))
 endif
 
 ifeq ($(build_type),host)
   # Only build if host builds are supported.
   ifeq ($(build_host),true)
+    LOCAL_CFLAGS += -Wno-extern-c-compat
     ifneq ($($(module)_libc++),)
       include external/libcxx/libcxx.mk
     endif
diff --git a/libbacktrace/Android.mk b/libbacktrace/Android.mk
index 9588dd6..f3b28dd 100755
--- a/libbacktrace/Android.mk
+++ b/libbacktrace/Android.mk
@@ -44,16 +44,12 @@
 
 libbacktrace_shared_libraries_target := \
 	libcutils \
-	libgccdemangle \
 
 libbacktrace_src_files += \
 	UnwindCurrent.cpp \
 	UnwindMap.cpp \
 	UnwindPtrace.cpp \
 
-libbacktrace_c_includes := \
-	external/libunwind/include \
-
 libbacktrace_shared_libraries := \
 	libunwind \
 	libunwind-ptrace \
@@ -74,58 +70,9 @@
 build_target := SHARED_LIBRARY
 include $(LOCAL_PATH)/Android.build.mk
 build_type := host
+libbacktrace_multilib := both
 include $(LOCAL_PATH)/Android.build.mk
 
-# Don't build for unbundled branches
-ifeq (,$(TARGET_BUILD_APPS))
-#-------------------------------------------------------------------------
-# The libbacktrace library (libc++)
-#-------------------------------------------------------------------------
-libbacktrace_libc++_src_files := \
-	BacktraceImpl.cpp \
-	BacktraceMap.cpp \
-	BacktraceThread.cpp \
-	thread_utils.c \
-
-libbacktrace_libc++_shared_libraries_target := \
-	libcutils \
-	libgccdemangle \
-
-libbacktrace_libc++_src_files += \
-	UnwindCurrent.cpp \
-	UnwindMap.cpp \
-	UnwindPtrace.cpp \
-
-libbacktrace_libc++_c_includes := \
-	external/libunwind/include \
-
-libbacktrace_libc++_shared_libraries := \
-	libunwind \
-	libunwind-ptrace \
-
-libbacktrace_libc++_shared_libraries_host := \
-	liblog \
-
-libbacktrace_libc++_static_libraries_host := \
-	libcutils \
-
-libbacktrace_libc++_ldlibs_host := \
-	-lpthread \
-	-lrt \
-
-libbacktrace_libc++_libc++ := true
-
-module := libbacktrace_libc++
-module_tag := optional
-build_type := target
-build_target := SHARED_LIBRARY
-include $(LOCAL_PATH)/Android.build.mk
-build_type := host
-libbacktrace_libc++_multilib := both
-include $(LOCAL_PATH)/Android.build.mk
-libbacktrace_libc++_multilib :=
-endif
-
 #-------------------------------------------------------------------------
 # The libbacktrace_test library needed by backtrace_test.
 #-------------------------------------------------------------------------
@@ -194,25 +141,8 @@
 LOCAL_SRC_FILES := \
 	BacktraceMap.cpp \
 
-include $(BUILD_HOST_SHARED_LIBRARY)
-
-# Don't build for unbundled branches
-ifeq (,$(TARGET_BUILD_APPS))
-#-------------------------------------------------------------------------
-# The libbacktrace library (libc++)
-#-------------------------------------------------------------------------
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libbacktrace_libc++
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_SRC_FILES := \
-	BacktraceMap.cpp \
-
 LOCAL_MULTILIB := both
 
 include $(BUILD_HOST_SHARED_LIBRARY)
 
-endif # TARGET_BUILD_APPS
-
 endif # HOST_OS-darwin
diff --git a/libbacktrace/BacktraceImpl.cpp b/libbacktrace/BacktraceImpl.cpp
index 405b042..fb8a725 100644
--- a/libbacktrace/BacktraceImpl.cpp
+++ b/libbacktrace/BacktraceImpl.cpp
@@ -99,15 +99,15 @@
 
 std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
   const char* map_name;
-  if (frame->map && !frame->map->name.empty()) {
-    map_name = frame->map->name.c_str();
+  if (BacktraceMap::IsValid(frame->map) && !frame->map.name.empty()) {
+    map_name = frame->map.name.c_str();
   } else {
     map_name = "<unknown>";
   }
 
   uintptr_t relative_pc;
-  if (frame->map) {
-    relative_pc = frame->pc - frame->map->start;
+  if (BacktraceMap::IsValid(frame->map)) {
+    relative_pc = frame->pc - frame->map.start;
   } else {
     relative_pc = frame->pc;
   }
@@ -128,8 +128,8 @@
   return buf;
 }
 
-const backtrace_map_t* Backtrace::FindMap(uintptr_t pc) {
-  return map_->Find(pc);
+void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
+  map_->FillIn(pc, map);
 }
 
 //-------------------------------------------------------------------------
@@ -147,8 +147,9 @@
     return false;
   }
 
-  const backtrace_map_t* map = FindMap(ptr);
-  if (map && map->flags & PROT_READ) {
+  backtrace_map_t map;
+  FillInMap(ptr, &map);
+  if (BacktraceMap::IsValid(map) && map.flags & PROT_READ) {
     *out_value = *reinterpret_cast<word_t*>(ptr);
     return true;
   } else {
diff --git a/libbacktrace/BacktraceImpl.h b/libbacktrace/BacktraceImpl.h
index d34ad05..cd61bdf 100755
--- a/libbacktrace/BacktraceImpl.h
+++ b/libbacktrace/BacktraceImpl.h
@@ -37,8 +37,8 @@
   inline pid_t Pid() { return backtrace_obj_->Pid(); }
   inline pid_t Tid() { return backtrace_obj_->Tid(); }
 
-  inline const backtrace_map_t* FindMap(uintptr_t addr) {
-    return backtrace_obj_->FindMap(addr);
+  inline void FillInMap(uintptr_t addr, backtrace_map_t* map) {
+    backtrace_obj_->FillInMap(addr, map);
   }
   inline std::string GetFunctionName(uintptr_t pc, uintptr_t* offset) {
     return backtrace_obj_->GetFunctionName(pc, offset);
diff --git a/libbacktrace/BacktraceMap.cpp b/libbacktrace/BacktraceMap.cpp
index f38e484..82a4085 100644
--- a/libbacktrace/BacktraceMap.cpp
+++ b/libbacktrace/BacktraceMap.cpp
@@ -37,14 +37,15 @@
 BacktraceMap::~BacktraceMap() {
 }
 
-const backtrace_map_t* BacktraceMap::Find(uintptr_t addr) {
+void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
   for (BacktraceMap::const_iterator it = begin();
        it != end(); ++it) {
     if (addr >= it->start && addr < it->end) {
-      return &*it;
+      *map = *it;
+      return;
     }
   }
-  return NULL;
+  *map = {};
 }
 
 bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
diff --git a/libbacktrace/UnwindCurrent.cpp b/libbacktrace/UnwindCurrent.cpp
index b176aaf..372555b 100755
--- a/libbacktrace/UnwindCurrent.cpp
+++ b/libbacktrace/UnwindCurrent.cpp
@@ -137,9 +137,8 @@
 
       if (!within_handler) {
         frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
-        frame->map = FindMap(frame->pc);
+        FillInMap(frame->pc, &frame->map);
       } else {
-        frame->map = NULL;
         frame->func_offset = 0;
       }
       num_frames++;
diff --git a/libbacktrace/UnwindMap.cpp b/libbacktrace/UnwindMap.cpp
index 387d768..284a561 100644
--- a/libbacktrace/UnwindMap.cpp
+++ b/libbacktrace/UnwindMap.cpp
@@ -113,18 +113,17 @@
   return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();;
 }
 
-const backtrace_map_t* UnwindMapLocal::Find(uintptr_t addr) {
-  const backtrace_map_t* map = BacktraceMap::Find(addr);
-  if (!map) {
+void UnwindMapLocal::FillIn(uintptr_t addr, backtrace_map_t* map) {
+  BacktraceMap::FillIn(addr, map);
+  if (!IsValid(*map)) {
     // Check to see if the underlying map changed and regenerate the map
     // if it did.
     if (unw_map_local_cursor_valid(&map_cursor_) < 0) {
       if (GenerateMap()) {
-        map = BacktraceMap::Find(addr);
+        BacktraceMap::FillIn(addr, map);
       }
     }
   }
-  return map;
 }
 
 //-------------------------------------------------------------------------
diff --git a/libbacktrace/UnwindMap.h b/libbacktrace/UnwindMap.h
index 2fdb29f..be8855e 100644
--- a/libbacktrace/UnwindMap.h
+++ b/libbacktrace/UnwindMap.h
@@ -45,7 +45,7 @@
 
   virtual bool Build();
 
-  virtual const backtrace_map_t* Find(uintptr_t addr);
+  virtual void FillIn(uintptr_t addr, backtrace_map_t* map);
 
 protected:
   virtual bool GenerateMap();
diff --git a/libbacktrace/UnwindPtrace.cpp b/libbacktrace/UnwindPtrace.cpp
index 7ba8775..efe758b 100644
--- a/libbacktrace/UnwindPtrace.cpp
+++ b/libbacktrace/UnwindPtrace.cpp
@@ -106,7 +106,7 @@
 
       frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
 
-      frame->map = FindMap(frame->pc);
+      FillInMap(frame->pc, &frame->map);
 
       num_frames++;
     } else {
diff --git a/libbacktrace/backtrace_test.cpp b/libbacktrace/backtrace_test.cpp
index 8002ed6..76aabd1 100644
--- a/libbacktrace/backtrace_test.cpp
+++ b/libbacktrace/backtrace_test.cpp
@@ -688,6 +688,25 @@
   delete map3;
 }
 
+TEST(libbacktrace, fillin_erases) {
+  BacktraceMap* back_map = BacktraceMap::Create(getpid());
+
+  backtrace_map_t map;
+
+  map.start = 1;
+  map.end = 3;
+  map.flags = 1;
+  map.name = "Initialized";
+  back_map->FillIn(0, &map);
+  delete back_map;
+
+  ASSERT_FALSE(BacktraceMap::IsValid(map));
+  ASSERT_EQ(static_cast<uintptr_t>(0), map.start);
+  ASSERT_EQ(static_cast<uintptr_t>(0), map.end);
+  ASSERT_EQ(0, map.flags);
+  ASSERT_EQ("", map.name);
+}
+
 TEST(libbacktrace, format_test) {
   UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), BACKTRACE_CURRENT_THREAD));
   ASSERT_TRUE(backtrace.get() != NULL);
@@ -697,13 +716,8 @@
   frame.pc = 2;
   frame.sp = 0;
   frame.stack_size = 0;
-  frame.map = NULL;
   frame.func_offset = 0;
 
-  backtrace_map_t map;
-  map.start = 0;
-  map.end = 0;
-
   // Check no map set.
   frame.num = 1;
 #if defined(__LP64__)
@@ -714,8 +728,8 @@
             backtrace->FormatFrameData(&frame));
 
   // Check map name empty, but exists.
-  frame.map = &map;
-  map.start = 1;
+  frame.map.start = 1;
+  frame.map.end = 1;
 #if defined(__LP64__)
   EXPECT_EQ("#01 pc 0000000000000001  <unknown>",
 #else
@@ -726,9 +740,9 @@
 
   // Check relative pc is set and map name is set.
   frame.pc = 0x12345679;
-  frame.map = &map;
-  map.name = "MapFake";
-  map.start =  1;
+  frame.map.name = "MapFake";
+  frame.map.start =  1;
+  frame.map.end =  1;
 #if defined(__LP64__)
   EXPECT_EQ("#01 pc 0000000012345678  MapFake",
 #else
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index b016a42..2c5e351 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -16,19 +16,11 @@
 LOCAL_PATH := $(my-dir)
 include $(CLEAR_VARS)
 
-ifeq ($(TARGET_CPU_SMP),true)
-    targetSmpFlag := -DANDROID_SMP=1
-else
-    targetSmpFlag := -DANDROID_SMP=0
-endif
-hostSmpFlag := -DANDROID_SMP=0
-
 commonSources := \
 	hashmap.c \
 	atomic.c.arm \
 	native_handle.c \
 	config_utils.c \
-	cpu_info.c \
 	load_file.c \
 	open_memstream.c \
 	strdup16to8.c \
@@ -77,7 +69,6 @@
 LOCAL_MODULE := libcutils
 LOCAL_SRC_FILES := $(commonSources) $(commonHostSources) dlmalloc_stubs.c
 LOCAL_STATIC_LIBRARIES := liblog
-LOCAL_CFLAGS += $(hostSmpFlag)
 ifneq ($(HOST_OS),windows)
 LOCAL_CFLAGS += -Werror
 endif
@@ -121,29 +112,40 @@
 LOCAL_SRC_FILES_arm += \
         arch-arm/memset32.S \
 
+# arch-arm/memset32.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
+
 LOCAL_SRC_FILES_arm64 += \
         arch-arm64/android_memset.S \
 
+ifndef ARCH_MIPS_REV6
 LOCAL_SRC_FILES_mips += \
         arch-mips/android_memset.c \
 
+LOCAL_CFLAGS_mips += -DHAVE_MEMSET16 -DHAVE_MEMSET32
+endif
+
+# TODO: switch mips64 back to using arch-mips/android_memset.c
+LOCAL_SRC_FILES_mips64 += \
+#       arch-mips/android_memset.c \
+
 LOCAL_SRC_FILES_x86 += \
         arch-x86/android_memset16.S \
         arch-x86/android_memset32.S \
 
 LOCAL_SRC_FILES_x86_64 += \
-        arch-x86_64/android_memset16_SSE2-atom.S \
-        arch-x86_64/android_memset32_SSE2-atom.S \
+        arch-x86_64/android_memset16.S \
+        arch-x86_64/android_memset32.S \
 
 LOCAL_CFLAGS_arm += -DHAVE_MEMSET16 -DHAVE_MEMSET32
 LOCAL_CFLAGS_arm64 += -DHAVE_MEMSET16 -DHAVE_MEMSET32
-LOCAL_CFLAGS_mips += -DHAVE_MEMSET16 -DHAVE_MEMSET32
+#LOCAL_CFLAGS_mips64 += -DHAVE_MEMSET16 -DHAVE_MEMSET32
 LOCAL_CFLAGS_x86 += -DHAVE_MEMSET16 -DHAVE_MEMSET32
 LOCAL_CFLAGS_x86_64 += -DHAVE_MEMSET16 -DHAVE_MEMSET32
 
 LOCAL_C_INCLUDES := $(libcutils_c_includes)
 LOCAL_STATIC_LIBRARIES := liblog
-LOCAL_CFLAGS += $(targetSmpFlag) -Werror
+LOCAL_CFLAGS += -Werror -std=gnu90
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 include $(BUILD_STATIC_LIBRARY)
 
@@ -153,7 +155,7 @@
 # liblog symbols present in libcutils.
 LOCAL_WHOLE_STATIC_LIBRARIES := libcutils liblog
 LOCAL_SHARED_LIBRARIES := liblog
-LOCAL_CFLAGS += $(targetSmpFlag) -Werror
+LOCAL_CFLAGS += -Werror
 LOCAL_C_INCLUDES := $(libcutils_c_includes)
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 include $(BUILD_SHARED_LIBRARY)
diff --git a/libcutils/android_reboot.c b/libcutils/android_reboot.c
index 5d98295..6ae23c1 100644
--- a/libcutils/android_reboot.c
+++ b/libcutils/android_reboot.c
@@ -20,6 +20,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <mntent.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -33,37 +34,21 @@
  */
 static int remount_ro_done(void)
 {
-    FILE *f;
-    char mount_dev[256];
-    char mount_dir[256];
-    char mount_type[256];
-    char mount_opts[256];
-    int mount_freq;
-    int mount_passno;
-    int match;
+    FILE* fp;
+    struct mntent* mentry;
     int found_rw_fs = 0;
 
-    f = fopen("/proc/mounts", "r");
-    if (! f) {
-        /* If we can't read /proc/mounts, just give up */
+    if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
+        /* If we can't read /proc/mounts, just give up. */
         return 1;
     }
-
-    do {
-        match = fscanf(f, "%255s %255s %255s %255s %d %d\n",
-                       mount_dev, mount_dir, mount_type,
-                       mount_opts, &mount_freq, &mount_passno);
-        mount_dev[255] = 0;
-        mount_dir[255] = 0;
-        mount_type[255] = 0;
-        mount_opts[255] = 0;
-        if ((match == 6) && !strncmp(mount_dev, "/dev/block", 10) && strstr(mount_opts, "rw,")) {
+    while ((mentry = getmntent(fp)) != NULL) {
+        if (!strncmp(mentry->mnt_fsname, "/dev/block", 10) && strstr(mentry->mnt_opts, "rw,")) {
             found_rw_fs = 1;
             break;
         }
-    } while (match != EOF);
-
-    fclose(f);
+    }
+    endmntent(fp);
 
     return !found_rw_fs;
 }
@@ -104,7 +89,7 @@
 }
 
 
-int android_reboot(int cmd, int flags UNUSED, char *arg)
+int android_reboot(int cmd, int flags UNUSED, const char *arg)
 {
     int ret;
 
diff --git a/libcutils/arch-x86/android_memset16.S b/libcutils/arch-x86/android_memset16.S
old mode 100644
new mode 100755
index f8b79bd..cb2ff14
--- a/libcutils/arch-x86/android_memset16.S
+++ b/libcutils/arch-x86/android_memset16.S
@@ -13,13 +13,707 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * Contributed by: Intel Corporation
- */
 
-# include "cache_wrapper.S"
-# undef __i686
-# define USE_AS_ANDROID
-# define sse2_memset16_atom android_memset16
-# include "sse2-memset16-atom.S"
+#include "cache.h"
 
+#ifndef MEMSET
+# define MEMSET		android_memset16
+#endif
+
+#ifndef L
+# define L(label)	.L##label
+#endif
+
+#ifndef ALIGN
+# define ALIGN(n)	.p2align n
+#endif
+
+#ifndef cfi_startproc
+# define cfi_startproc			.cfi_startproc
+#endif
+
+#ifndef cfi_endproc
+# define cfi_endproc			.cfi_endproc
+#endif
+
+#ifndef cfi_rel_offset
+# define cfi_rel_offset(reg, off)	.cfi_rel_offset reg, off
+#endif
+
+#ifndef cfi_restore
+# define cfi_restore(reg)		.cfi_restore reg
+#endif
+
+#ifndef cfi_adjust_cfa_offset
+# define cfi_adjust_cfa_offset(off)	.cfi_adjust_cfa_offset off
+#endif
+
+#ifndef ENTRY
+# define ENTRY(name)			\
+	.type name,  @function; 	\
+	.globl name;			\
+	.p2align 4;			\
+name:					\
+	cfi_startproc
+#endif
+
+#ifndef END
+# define END(name)			\
+	cfi_endproc;			\
+	.size name, .-name
+#endif
+
+#define CFI_PUSH(REG)						\
+  cfi_adjust_cfa_offset (4);					\
+  cfi_rel_offset (REG, 0)
+
+#define CFI_POP(REG)						\
+  cfi_adjust_cfa_offset (-4);					\
+  cfi_restore (REG)
+
+#define PUSH(REG)	pushl REG; CFI_PUSH (REG)
+#define POP(REG)	popl REG; CFI_POP (REG)
+
+#ifdef USE_AS_BZERO16
+# define DEST		PARMS
+# define LEN		DEST+4
+# define SETRTNVAL
+#else
+# define DEST		PARMS
+# define CHR		DEST+4
+# define LEN		CHR+4
+# define SETRTNVAL	movl DEST(%esp), %eax
+#endif
+
+#if (defined SHARED || defined __PIC__)
+# define ENTRANCE	PUSH (%ebx);
+# define RETURN_END	POP (%ebx); ret
+# define RETURN		RETURN_END; CFI_PUSH (%ebx)
+# define PARMS		8		/* Preserve EBX.  */
+# define JMPTBL(I, B)	I - B
+
+/* Load an entry in a jump table into EBX and branch to it.  TABLE is a
+   jump table with relative offsets.   */
+# define BRANCH_TO_JMPTBL_ENTRY(TABLE)				\
+    /* We first load PC into EBX.  */				\
+    call	__x86.get_pc_thunk.bx;				\
+    /* Get the address of the jump table.  */			\
+    add		$(TABLE - .), %ebx;				\
+    /* Get the entry and convert the relative offset to the	\
+       absolute address.  */					\
+    add		(%ebx,%ecx,4), %ebx;				\
+    /* We loaded the jump table and adjuested EDX. Go.  */	\
+    jmp		*%ebx
+
+	.section	.gnu.linkonce.t.__x86.get_pc_thunk.bx,"ax",@progbits
+	.globl	__x86.get_pc_thunk.bx
+	.hidden	__x86.get_pc_thunk.bx
+	ALIGN (4)
+	.type	__x86.get_pc_thunk.bx,@function
+__x86.get_pc_thunk.bx:
+	movl	(%esp), %ebx
+	ret
+#else
+# define ENTRANCE
+# define RETURN_END	ret
+# define RETURN		RETURN_END
+# define PARMS		4
+# define JMPTBL(I, B)	I
+
+/* Branch to an entry in a jump table.  TABLE is a jump table with
+   absolute offsets.  */
+# define BRANCH_TO_JMPTBL_ENTRY(TABLE)				\
+    jmp		*TABLE(,%ecx,4)
+#endif
+
+	.section .text.sse2,"ax",@progbits
+	ALIGN (4)
+ENTRY (MEMSET)
+	ENTRANCE
+
+	movl	LEN(%esp), %ecx
+	shr	$1, %ecx
+#ifdef USE_AS_BZERO16
+	xor	%eax, %eax
+#else
+	movzwl	CHR(%esp), %eax
+	mov	%eax, %edx
+	shl	$16, %eax
+	or	%edx, %eax
+#endif
+	movl	DEST(%esp), %edx
+	cmp	$32, %ecx
+	jae	L(32wordsormore)
+
+L(write_less32words):
+	lea	(%edx, %ecx, 2), %edx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_less32words))
+
+
+	.pushsection .rodata.sse2,"a",@progbits
+	ALIGN (2)
+L(table_less32words):
+	.int	JMPTBL (L(write_0words), L(table_less32words))
+	.int	JMPTBL (L(write_1words), L(table_less32words))
+	.int	JMPTBL (L(write_2words), L(table_less32words))
+	.int	JMPTBL (L(write_3words), L(table_less32words))
+	.int	JMPTBL (L(write_4words), L(table_less32words))
+	.int	JMPTBL (L(write_5words), L(table_less32words))
+	.int	JMPTBL (L(write_6words), L(table_less32words))
+	.int	JMPTBL (L(write_7words), L(table_less32words))
+	.int	JMPTBL (L(write_8words), L(table_less32words))
+	.int	JMPTBL (L(write_9words), L(table_less32words))
+	.int	JMPTBL (L(write_10words), L(table_less32words))
+	.int	JMPTBL (L(write_11words), L(table_less32words))
+	.int	JMPTBL (L(write_12words), L(table_less32words))
+	.int	JMPTBL (L(write_13words), L(table_less32words))
+	.int	JMPTBL (L(write_14words), L(table_less32words))
+	.int	JMPTBL (L(write_15words), L(table_less32words))
+	.int	JMPTBL (L(write_16words), L(table_less32words))
+	.int	JMPTBL (L(write_17words), L(table_less32words))
+	.int	JMPTBL (L(write_18words), L(table_less32words))
+	.int	JMPTBL (L(write_19words), L(table_less32words))
+	.int	JMPTBL (L(write_20words), L(table_less32words))
+	.int	JMPTBL (L(write_21words), L(table_less32words))
+	.int	JMPTBL (L(write_22words), L(table_less32words))
+	.int	JMPTBL (L(write_23words), L(table_less32words))
+	.int	JMPTBL (L(write_24words), L(table_less32words))
+	.int	JMPTBL (L(write_25words), L(table_less32words))
+	.int	JMPTBL (L(write_26words), L(table_less32words))
+	.int	JMPTBL (L(write_27words), L(table_less32words))
+	.int	JMPTBL (L(write_28words), L(table_less32words))
+	.int	JMPTBL (L(write_29words), L(table_less32words))
+	.int	JMPTBL (L(write_30words), L(table_less32words))
+	.int	JMPTBL (L(write_31words), L(table_less32words))
+	.popsection
+
+	ALIGN (4)
+L(write_28words):
+	movl	%eax, -56(%edx)
+	movl	%eax, -52(%edx)
+L(write_24words):
+	movl	%eax, -48(%edx)
+	movl	%eax, -44(%edx)
+L(write_20words):
+	movl	%eax, -40(%edx)
+	movl	%eax, -36(%edx)
+L(write_16words):
+	movl	%eax, -32(%edx)
+	movl	%eax, -28(%edx)
+L(write_12words):
+	movl	%eax, -24(%edx)
+	movl	%eax, -20(%edx)
+L(write_8words):
+	movl	%eax, -16(%edx)
+	movl	%eax, -12(%edx)
+L(write_4words):
+	movl	%eax, -8(%edx)
+	movl	%eax, -4(%edx)
+L(write_0words):
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+L(write_29words):
+	movl	%eax, -58(%edx)
+	movl	%eax, -54(%edx)
+L(write_25words):
+	movl	%eax, -50(%edx)
+	movl	%eax, -46(%edx)
+L(write_21words):
+	movl	%eax, -42(%edx)
+	movl	%eax, -38(%edx)
+L(write_17words):
+	movl	%eax, -34(%edx)
+	movl	%eax, -30(%edx)
+L(write_13words):
+	movl	%eax, -26(%edx)
+	movl	%eax, -22(%edx)
+L(write_9words):
+	movl	%eax, -18(%edx)
+	movl	%eax, -14(%edx)
+L(write_5words):
+	movl	%eax, -10(%edx)
+	movl	%eax, -6(%edx)
+L(write_1words):
+	mov	%ax, -2(%edx)
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+L(write_30words):
+	movl	%eax, -60(%edx)
+	movl	%eax, -56(%edx)
+L(write_26words):
+	movl	%eax, -52(%edx)
+	movl	%eax, -48(%edx)
+L(write_22words):
+	movl	%eax, -44(%edx)
+	movl	%eax, -40(%edx)
+L(write_18words):
+	movl	%eax, -36(%edx)
+	movl	%eax, -32(%edx)
+L(write_14words):
+	movl	%eax, -28(%edx)
+	movl	%eax, -24(%edx)
+L(write_10words):
+	movl	%eax, -20(%edx)
+	movl	%eax, -16(%edx)
+L(write_6words):
+	movl	%eax, -12(%edx)
+	movl	%eax, -8(%edx)
+L(write_2words):
+	movl	%eax, -4(%edx)
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+L(write_31words):
+	movl	%eax, -62(%edx)
+	movl	%eax, -58(%edx)
+L(write_27words):
+	movl	%eax, -54(%edx)
+	movl	%eax, -50(%edx)
+L(write_23words):
+	movl	%eax, -46(%edx)
+	movl	%eax, -42(%edx)
+L(write_19words):
+	movl	%eax, -38(%edx)
+	movl	%eax, -34(%edx)
+L(write_15words):
+	movl	%eax, -30(%edx)
+	movl	%eax, -26(%edx)
+L(write_11words):
+	movl	%eax, -22(%edx)
+	movl	%eax, -18(%edx)
+L(write_7words):
+	movl	%eax, -14(%edx)
+	movl	%eax, -10(%edx)
+L(write_3words):
+	movl	%eax, -6(%edx)
+	movw	%ax, -2(%edx)
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+
+L(32wordsormore):
+	shl	$1, %ecx
+	test	$0x01, %edx
+	jz	L(aligned2bytes)
+	mov	%eax, (%edx)
+	mov	%eax, -4(%edx, %ecx)
+	sub	$2, %ecx
+	add	$1, %edx
+	rol	$8, %eax
+L(aligned2bytes):
+#ifdef USE_AS_BZERO16
+	pxor	%xmm0, %xmm0
+#else
+	movd	%eax, %xmm0
+	pshufd	$0, %xmm0, %xmm0
+#endif
+	testl	$0xf, %edx
+	jz	L(aligned_16)
+/* ECX > 32 and EDX is not 16 byte aligned.  */
+L(not_aligned_16):
+	movdqu	%xmm0, (%edx)
+	movl	%edx, %eax
+	and	$-16, %edx
+	add	$16, %edx
+	sub	%edx, %eax
+	add	%eax, %ecx
+	movd	%xmm0, %eax
+
+	ALIGN (4)
+L(aligned_16):
+	cmp	$128, %ecx
+	jae	L(128bytesormore)
+
+L(aligned_16_less128bytes):
+	add	%ecx, %edx
+	shr	$1, %ecx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+	ALIGN (4)
+L(128bytesormore):
+#ifdef SHARED_CACHE_SIZE
+	PUSH (%ebx)
+	mov	$SHARED_CACHE_SIZE, %ebx
+#else
+# if (defined SHARED || defined __PIC__)
+	call	__x86.get_pc_thunk.bx
+	add	$_GLOBAL_OFFSET_TABLE_, %ebx
+	mov	__x86_shared_cache_size@GOTOFF(%ebx), %ebx
+# else
+	PUSH (%ebx)
+	mov	__x86_shared_cache_size, %ebx
+# endif
+#endif
+	cmp	%ebx, %ecx
+	jae	L(128bytesormore_nt_start)
+
+
+#ifdef DATA_CACHE_SIZE
+	POP (%ebx)
+# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
+	cmp	$DATA_CACHE_SIZE, %ecx
+#else
+# if (defined SHARED || defined __PIC__)
+#  define RESTORE_EBX_STATE
+	call	__x86.get_pc_thunk.bx
+	add	$_GLOBAL_OFFSET_TABLE_, %ebx
+	cmp	__x86_data_cache_size@GOTOFF(%ebx), %ecx
+# else
+	POP (%ebx)
+#  define RESTORE_EBX_STATE CFI_PUSH (%ebx)
+	cmp	__x86_data_cache_size, %ecx
+# endif
+#endif
+
+	jae	L(128bytes_L2_normal)
+	subl	$128, %ecx
+L(128bytesormore_normal):
+	sub	$128, %ecx
+	movdqa	%xmm0, (%edx)
+	movdqa	%xmm0, 0x10(%edx)
+	movdqa	%xmm0, 0x20(%edx)
+	movdqa	%xmm0, 0x30(%edx)
+	movdqa	%xmm0, 0x40(%edx)
+	movdqa	%xmm0, 0x50(%edx)
+	movdqa	%xmm0, 0x60(%edx)
+	movdqa	%xmm0, 0x70(%edx)
+	lea	128(%edx), %edx
+	jb	L(128bytesless_normal)
+
+
+	sub	$128, %ecx
+	movdqa	%xmm0, (%edx)
+	movdqa	%xmm0, 0x10(%edx)
+	movdqa	%xmm0, 0x20(%edx)
+	movdqa	%xmm0, 0x30(%edx)
+	movdqa	%xmm0, 0x40(%edx)
+	movdqa	%xmm0, 0x50(%edx)
+	movdqa	%xmm0, 0x60(%edx)
+	movdqa	%xmm0, 0x70(%edx)
+	lea	128(%edx), %edx
+	jae	L(128bytesormore_normal)
+
+L(128bytesless_normal):
+	lea	128(%ecx), %ecx
+	add	%ecx, %edx
+	shr	$1, %ecx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+	ALIGN (4)
+L(128bytes_L2_normal):
+	prefetcht0	0x380(%edx)
+	prefetcht0	0x3c0(%edx)
+	sub	$128, %ecx
+	movdqa	%xmm0, (%edx)
+	movaps	%xmm0, 0x10(%edx)
+	movaps	%xmm0, 0x20(%edx)
+	movaps	%xmm0, 0x30(%edx)
+	movaps	%xmm0, 0x40(%edx)
+	movaps	%xmm0, 0x50(%edx)
+	movaps	%xmm0, 0x60(%edx)
+	movaps	%xmm0, 0x70(%edx)
+	add	$128, %edx
+	cmp	$128, %ecx
+	jae	L(128bytes_L2_normal)
+
+L(128bytesless_L2_normal):
+	add	%ecx, %edx
+	shr	$1, %ecx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+	RESTORE_EBX_STATE
+L(128bytesormore_nt_start):
+	sub	%ebx, %ecx
+	mov	%ebx, %eax
+	and	$0x7f, %eax
+	add	%eax, %ecx
+	movd	%xmm0, %eax
+	ALIGN (4)
+L(128bytesormore_shared_cache_loop):
+	prefetcht0	0x3c0(%edx)
+	prefetcht0	0x380(%edx)
+	sub	$0x80, %ebx
+	movdqa	%xmm0, (%edx)
+	movdqa	%xmm0, 0x10(%edx)
+	movdqa	%xmm0, 0x20(%edx)
+	movdqa	%xmm0, 0x30(%edx)
+	movdqa	%xmm0, 0x40(%edx)
+	movdqa	%xmm0, 0x50(%edx)
+	movdqa	%xmm0, 0x60(%edx)
+	movdqa	%xmm0, 0x70(%edx)
+	add	$0x80, %edx
+	cmp	$0x80, %ebx
+	jae	L(128bytesormore_shared_cache_loop)
+	cmp	$0x80, %ecx
+	jb	L(shared_cache_loop_end)
+	ALIGN (4)
+L(128bytesormore_nt):
+	sub	$0x80, %ecx
+	movntdq	%xmm0, (%edx)
+	movntdq	%xmm0, 0x10(%edx)
+	movntdq	%xmm0, 0x20(%edx)
+	movntdq	%xmm0, 0x30(%edx)
+	movntdq	%xmm0, 0x40(%edx)
+	movntdq	%xmm0, 0x50(%edx)
+	movntdq	%xmm0, 0x60(%edx)
+	movntdq	%xmm0, 0x70(%edx)
+	add	$0x80, %edx
+	cmp	$0x80, %ecx
+	jae	L(128bytesormore_nt)
+	sfence
+L(shared_cache_loop_end):
+#if defined DATA_CACHE_SIZE || !(defined SHARED || defined __PIC__)
+	POP (%ebx)
+#endif
+	add	%ecx, %edx
+	shr	$1, %ecx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+
+	.pushsection .rodata.sse2,"a",@progbits
+	ALIGN (2)
+L(table_16_128bytes):
+	.int	JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_2bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_6bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_10bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_14bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_18bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_22bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_26bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_30bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_34bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_38bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_42bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_46bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_50bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_54bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_58bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_62bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_66bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_70bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_74bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_78bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_82bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_86bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_90bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_94bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_98bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_102bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_106bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_110bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_114bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_118bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_122bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_126bytes), L(table_16_128bytes))
+	.popsection
+
+
+	ALIGN (4)
+L(aligned_16_112bytes):
+	movdqa	%xmm0, -112(%edx)
+L(aligned_16_96bytes):
+	movdqa	%xmm0, -96(%edx)
+L(aligned_16_80bytes):
+	movdqa	%xmm0, -80(%edx)
+L(aligned_16_64bytes):
+	movdqa	%xmm0, -64(%edx)
+L(aligned_16_48bytes):
+	movdqa	%xmm0, -48(%edx)
+L(aligned_16_32bytes):
+	movdqa	%xmm0, -32(%edx)
+L(aligned_16_16bytes):
+	movdqa	%xmm0, -16(%edx)
+L(aligned_16_0bytes):
+	SETRTNVAL
+	RETURN
+
+
+	ALIGN (4)
+L(aligned_16_114bytes):
+	movdqa	%xmm0, -114(%edx)
+L(aligned_16_98bytes):
+	movdqa	%xmm0, -98(%edx)
+L(aligned_16_82bytes):
+	movdqa	%xmm0, -82(%edx)
+L(aligned_16_66bytes):
+	movdqa	%xmm0, -66(%edx)
+L(aligned_16_50bytes):
+	movdqa	%xmm0, -50(%edx)
+L(aligned_16_34bytes):
+	movdqa	%xmm0, -34(%edx)
+L(aligned_16_18bytes):
+	movdqa	%xmm0, -18(%edx)
+L(aligned_16_2bytes):
+	movw	%ax, -2(%edx)
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+L(aligned_16_116bytes):
+	movdqa	%xmm0, -116(%edx)
+L(aligned_16_100bytes):
+	movdqa	%xmm0, -100(%edx)
+L(aligned_16_84bytes):
+	movdqa	%xmm0, -84(%edx)
+L(aligned_16_68bytes):
+	movdqa	%xmm0, -68(%edx)
+L(aligned_16_52bytes):
+	movdqa	%xmm0, -52(%edx)
+L(aligned_16_36bytes):
+	movdqa	%xmm0, -36(%edx)
+L(aligned_16_20bytes):
+	movdqa	%xmm0, -20(%edx)
+L(aligned_16_4bytes):
+	movl	%eax, -4(%edx)
+	SETRTNVAL
+	RETURN
+
+
+	ALIGN (4)
+L(aligned_16_118bytes):
+	movdqa	%xmm0, -118(%edx)
+L(aligned_16_102bytes):
+	movdqa	%xmm0, -102(%edx)
+L(aligned_16_86bytes):
+	movdqa	%xmm0, -86(%edx)
+L(aligned_16_70bytes):
+	movdqa	%xmm0, -70(%edx)
+L(aligned_16_54bytes):
+	movdqa	%xmm0, -54(%edx)
+L(aligned_16_38bytes):
+	movdqa	%xmm0, -38(%edx)
+L(aligned_16_22bytes):
+	movdqa	%xmm0, -22(%edx)
+L(aligned_16_6bytes):
+	movl	%eax, -6(%edx)
+	movw	%ax, -2(%edx)
+	SETRTNVAL
+	RETURN
+
+
+	ALIGN (4)
+L(aligned_16_120bytes):
+	movdqa	%xmm0, -120(%edx)
+L(aligned_16_104bytes):
+	movdqa	%xmm0, -104(%edx)
+L(aligned_16_88bytes):
+	movdqa	%xmm0, -88(%edx)
+L(aligned_16_72bytes):
+	movdqa	%xmm0, -72(%edx)
+L(aligned_16_56bytes):
+	movdqa	%xmm0, -56(%edx)
+L(aligned_16_40bytes):
+	movdqa	%xmm0, -40(%edx)
+L(aligned_16_24bytes):
+	movdqa	%xmm0, -24(%edx)
+L(aligned_16_8bytes):
+	movq	%xmm0, -8(%edx)
+	SETRTNVAL
+	RETURN
+
+
+	ALIGN (4)
+L(aligned_16_122bytes):
+	movdqa	%xmm0, -122(%edx)
+L(aligned_16_106bytes):
+	movdqa	%xmm0, -106(%edx)
+L(aligned_16_90bytes):
+	movdqa	%xmm0, -90(%edx)
+L(aligned_16_74bytes):
+	movdqa	%xmm0, -74(%edx)
+L(aligned_16_58bytes):
+	movdqa	%xmm0, -58(%edx)
+L(aligned_16_42bytes):
+	movdqa	%xmm0, -42(%edx)
+L(aligned_16_26bytes):
+	movdqa	%xmm0, -26(%edx)
+L(aligned_16_10bytes):
+	movq	%xmm0, -10(%edx)
+	movw	%ax, -2(%edx)
+	SETRTNVAL
+	RETURN
+
+
+	ALIGN (4)
+L(aligned_16_124bytes):
+	movdqa	%xmm0, -124(%edx)
+L(aligned_16_108bytes):
+	movdqa	%xmm0, -108(%edx)
+L(aligned_16_92bytes):
+	movdqa	%xmm0, -92(%edx)
+L(aligned_16_76bytes):
+	movdqa	%xmm0, -76(%edx)
+L(aligned_16_60bytes):
+	movdqa	%xmm0, -60(%edx)
+L(aligned_16_44bytes):
+	movdqa	%xmm0, -44(%edx)
+L(aligned_16_28bytes):
+	movdqa	%xmm0, -28(%edx)
+L(aligned_16_12bytes):
+	movq	%xmm0, -12(%edx)
+	movl	%eax, -4(%edx)
+	SETRTNVAL
+	RETURN
+
+
+	ALIGN (4)
+L(aligned_16_126bytes):
+	movdqa	%xmm0, -126(%edx)
+L(aligned_16_110bytes):
+	movdqa	%xmm0, -110(%edx)
+L(aligned_16_94bytes):
+	movdqa	%xmm0, -94(%edx)
+L(aligned_16_78bytes):
+	movdqa	%xmm0, -78(%edx)
+L(aligned_16_62bytes):
+	movdqa	%xmm0, -62(%edx)
+L(aligned_16_46bytes):
+	movdqa	%xmm0, -46(%edx)
+L(aligned_16_30bytes):
+	movdqa	%xmm0, -30(%edx)
+L(aligned_16_14bytes):
+	movq	%xmm0, -14(%edx)
+	movl	%eax, -6(%edx)
+	movw	%ax, -2(%edx)
+	SETRTNVAL
+	RETURN
+
+END (MEMSET)
diff --git a/libcutils/arch-x86/android_memset32.S b/libcutils/arch-x86/android_memset32.S
old mode 100644
new mode 100755
index 6249fce..f4326dc
--- a/libcutils/arch-x86/android_memset32.S
+++ b/libcutils/arch-x86/android_memset32.S
@@ -13,13 +13,498 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * Contributed by: Intel Corporation
- */
 
-# include "cache_wrapper.S"
-# undef __i686
-# define USE_AS_ANDROID
-# define sse2_memset32_atom android_memset32
-# include "sse2-memset32-atom.S"
+#include "cache.h"
 
+#ifndef MEMSET
+# define MEMSET 	android_memset32
+#endif
+
+#ifndef L
+# define L(label)	.L##label
+#endif
+
+#ifndef ALIGN
+# define ALIGN(n)	.p2align n
+#endif
+
+#ifndef cfi_startproc
+# define cfi_startproc			.cfi_startproc
+#endif
+
+#ifndef cfi_endproc
+# define cfi_endproc			.cfi_endproc
+#endif
+
+#ifndef cfi_rel_offset
+# define cfi_rel_offset(reg, off)	.cfi_rel_offset reg, off
+#endif
+
+#ifndef cfi_restore
+# define cfi_restore(reg)		.cfi_restore reg
+#endif
+
+#ifndef cfi_adjust_cfa_offset
+# define cfi_adjust_cfa_offset(off)	.cfi_adjust_cfa_offset off
+#endif
+
+#ifndef ENTRY
+# define ENTRY(name)			\
+	.type name,  @function; 	\
+	.globl name;			\
+	.p2align 4;			\
+name:					\
+	cfi_startproc
+#endif
+
+#ifndef END
+# define END(name)			\
+	cfi_endproc;			\
+	.size name, .-name
+#endif
+
+#define CFI_PUSH(REG)						\
+  cfi_adjust_cfa_offset (4);					\
+  cfi_rel_offset (REG, 0)
+
+#define CFI_POP(REG)						\
+  cfi_adjust_cfa_offset (-4);					\
+  cfi_restore (REG)
+
+#define PUSH(REG)	pushl REG; CFI_PUSH (REG)
+#define POP(REG)	popl REG; CFI_POP (REG)
+
+#ifdef USE_AS_BZERO32
+# define DEST		PARMS
+# define LEN		DEST+4
+# define SETRTNVAL
+#else
+# define DEST		PARMS
+# define DWDS		DEST+4
+# define LEN		DWDS+4
+# define SETRTNVAL	movl DEST(%esp), %eax
+#endif
+
+#if (defined SHARED || defined __PIC__)
+# define ENTRANCE	PUSH (%ebx);
+# define RETURN_END	POP (%ebx); ret
+# define RETURN		RETURN_END; CFI_PUSH (%ebx)
+# define PARMS		8		/* Preserve EBX.  */
+# define JMPTBL(I, B)	I - B
+
+/* Load an entry in a jump table into EBX and branch to it.  TABLE is a
+   jump table with relative offsets.   */
+# define BRANCH_TO_JMPTBL_ENTRY(TABLE)				\
+    /* We first load PC into EBX.  */				\
+    call	__x86.get_pc_thunk.bx;				\
+    /* Get the address of the jump table.  */			\
+    add		$(TABLE - .), %ebx;				\
+    /* Get the entry and convert the relative offset to the	\
+       absolute address.  */					\
+    add		(%ebx,%ecx,4), %ebx;				\
+    /* We loaded the jump table and adjuested EDX. Go.  */	\
+    jmp		*%ebx
+
+	.section	.gnu.linkonce.t.__x86.get_pc_thunk.bx,"ax",@progbits
+	.globl	__x86.get_pc_thunk.bx
+	.hidden	__x86.get_pc_thunk.bx
+	ALIGN (4)
+	.type	__x86.get_pc_thunk.bx,@function
+__x86.get_pc_thunk.bx:
+	movl	(%esp), %ebx
+	ret
+#else
+# define ENTRANCE
+# define RETURN_END	ret
+# define RETURN		RETURN_END
+# define PARMS		4
+# define JMPTBL(I, B)	I
+
+/* Branch to an entry in a jump table.  TABLE is a jump table with
+   absolute offsets.  */
+# define BRANCH_TO_JMPTBL_ENTRY(TABLE)				\
+    jmp		*TABLE(,%ecx,4)
+#endif
+
+	.section .text.sse2,"ax",@progbits
+	ALIGN (4)
+ENTRY (MEMSET)
+	ENTRANCE
+
+	movl	LEN(%esp), %ecx
+	shr     $2, %ecx
+#ifdef USE_AS_BZERO32
+	xor	%eax, %eax
+#else
+	mov	DWDS(%esp), %eax
+	mov	%eax, %edx
+#endif
+	movl	DEST(%esp), %edx
+	cmp	$16, %ecx
+	jae	L(16dbwordsormore)
+
+L(write_less16dbwords):
+	lea	(%edx, %ecx, 4), %edx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_less16dbwords))
+
+	.pushsection .rodata.sse2,"a",@progbits
+	ALIGN (2)
+L(table_less16dbwords):
+	.int	JMPTBL (L(write_0dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_1dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_2dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_3dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_4dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_5dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_6dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_7dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_8dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_9dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_10dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_11dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_12dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_13dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_14dbwords), L(table_less16dbwords))
+	.int	JMPTBL (L(write_15dbwords), L(table_less16dbwords))
+	.popsection
+
+	ALIGN (4)
+L(write_15dbwords):
+	movl	%eax, -60(%edx)
+L(write_14dbwords):
+	movl	%eax, -56(%edx)
+L(write_13dbwords):
+	movl	%eax, -52(%edx)
+L(write_12dbwords):
+	movl	%eax, -48(%edx)
+L(write_11dbwords):
+	movl	%eax, -44(%edx)
+L(write_10dbwords):
+	movl	%eax, -40(%edx)
+L(write_9dbwords):
+	movl	%eax, -36(%edx)
+L(write_8dbwords):
+	movl	%eax, -32(%edx)
+L(write_7dbwords):
+	movl	%eax, -28(%edx)
+L(write_6dbwords):
+	movl	%eax, -24(%edx)
+L(write_5dbwords):
+	movl	%eax, -20(%edx)
+L(write_4dbwords):
+	movl	%eax, -16(%edx)
+L(write_3dbwords):
+	movl	%eax, -12(%edx)
+L(write_2dbwords):
+	movl	%eax, -8(%edx)
+L(write_1dbwords):
+	movl	%eax, -4(%edx)
+L(write_0dbwords):
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+L(16dbwordsormore):
+	test	$3, %edx
+	jz	L(aligned4bytes)
+	mov	%eax, (%edx)
+	mov	%eax, -4(%edx, %ecx, 4)
+	sub	$1, %ecx
+	rol	$24, %eax
+	add	$1, %edx
+	test	$3, %edx
+	jz	L(aligned4bytes)
+	ror	$8, %eax
+	add	$1, %edx
+	test	$3, %edx
+	jz	L(aligned4bytes)
+	ror	$8, %eax
+	add	$1, %edx
+L(aligned4bytes):
+	shl	$2, %ecx
+
+#ifdef USE_AS_BZERO32
+	pxor	%xmm0, %xmm0
+#else
+	movd	%eax, %xmm0
+	pshufd	$0, %xmm0, %xmm0
+#endif
+	testl	$0xf, %edx
+	jz	L(aligned_16)
+/* ECX > 32 and EDX is not 16 byte aligned.  */
+L(not_aligned_16):
+	movdqu	%xmm0, (%edx)
+	movl	%edx, %eax
+	and	$-16, %edx
+	add	$16, %edx
+	sub	%edx, %eax
+	add	%eax, %ecx
+	movd	%xmm0, %eax
+	ALIGN (4)
+L(aligned_16):
+	cmp	$128, %ecx
+	jae	L(128bytesormore)
+
+L(aligned_16_less128bytes):
+	add	%ecx, %edx
+	shr	$2, %ecx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+	ALIGN (4)
+L(128bytesormore):
+#ifdef SHARED_CACHE_SIZE
+	PUSH (%ebx)
+	mov	$SHARED_CACHE_SIZE, %ebx
+#else
+# if (defined SHARED || defined __PIC__)
+	call	__x86.get_pc_thunk.bx
+	add	$_GLOBAL_OFFSET_TABLE_, %ebx
+	mov	__x86_shared_cache_size@GOTOFF(%ebx), %ebx
+# else
+	PUSH (%ebx)
+	mov	__x86_shared_cache_size, %ebx
+# endif
+#endif
+	cmp	%ebx, %ecx
+	jae	L(128bytesormore_nt_start)
+
+#ifdef DATA_CACHE_SIZE
+	POP (%ebx)
+# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
+	cmp	$DATA_CACHE_SIZE, %ecx
+#else
+# if (defined SHARED || defined __PIC__)
+#  define RESTORE_EBX_STATE
+	call	__x86.get_pc_thunk.bx
+	add	$_GLOBAL_OFFSET_TABLE_, %ebx
+	cmp	__x86_data_cache_size@GOTOFF(%ebx), %ecx
+# else
+	POP (%ebx)
+#  define RESTORE_EBX_STATE CFI_PUSH (%ebx)
+	cmp	__x86_data_cache_size, %ecx
+# endif
+#endif
+
+	jae	L(128bytes_L2_normal)
+	subl	$128, %ecx
+L(128bytesormore_normal):
+	sub	$128, %ecx
+	movdqa	%xmm0, (%edx)
+	movdqa	%xmm0, 0x10(%edx)
+	movdqa	%xmm0, 0x20(%edx)
+	movdqa	%xmm0, 0x30(%edx)
+	movdqa	%xmm0, 0x40(%edx)
+	movdqa	%xmm0, 0x50(%edx)
+	movdqa	%xmm0, 0x60(%edx)
+	movdqa	%xmm0, 0x70(%edx)
+	lea	128(%edx), %edx
+	jb	L(128bytesless_normal)
+
+
+	sub	$128, %ecx
+	movdqa	%xmm0, (%edx)
+	movdqa	%xmm0, 0x10(%edx)
+	movdqa	%xmm0, 0x20(%edx)
+	movdqa	%xmm0, 0x30(%edx)
+	movdqa	%xmm0, 0x40(%edx)
+	movdqa	%xmm0, 0x50(%edx)
+	movdqa	%xmm0, 0x60(%edx)
+	movdqa	%xmm0, 0x70(%edx)
+	lea	128(%edx), %edx
+	jae	L(128bytesormore_normal)
+
+L(128bytesless_normal):
+	lea	128(%ecx), %ecx
+	add	%ecx, %edx
+	shr	$2, %ecx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+	ALIGN (4)
+L(128bytes_L2_normal):
+	prefetcht0	0x380(%edx)
+	prefetcht0	0x3c0(%edx)
+	sub	$128, %ecx
+	movdqa	%xmm0, (%edx)
+	movaps	%xmm0, 0x10(%edx)
+	movaps	%xmm0, 0x20(%edx)
+	movaps	%xmm0, 0x30(%edx)
+	movaps	%xmm0, 0x40(%edx)
+	movaps	%xmm0, 0x50(%edx)
+	movaps	%xmm0, 0x60(%edx)
+	movaps	%xmm0, 0x70(%edx)
+	add	$128, %edx
+	cmp	$128, %ecx
+	jae	L(128bytes_L2_normal)
+
+L(128bytesless_L2_normal):
+	add	%ecx, %edx
+	shr	$2, %ecx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+	RESTORE_EBX_STATE
+L(128bytesormore_nt_start):
+	sub	%ebx, %ecx
+	mov	%ebx, %eax
+	and	$0x7f, %eax
+	add	%eax, %ecx
+	movd	%xmm0, %eax
+	ALIGN (4)
+L(128bytesormore_shared_cache_loop):
+	prefetcht0	0x3c0(%edx)
+	prefetcht0	0x380(%edx)
+	sub	$0x80, %ebx
+	movdqa	%xmm0, (%edx)
+	movdqa	%xmm0, 0x10(%edx)
+	movdqa	%xmm0, 0x20(%edx)
+	movdqa	%xmm0, 0x30(%edx)
+	movdqa	%xmm0, 0x40(%edx)
+	movdqa	%xmm0, 0x50(%edx)
+	movdqa	%xmm0, 0x60(%edx)
+	movdqa	%xmm0, 0x70(%edx)
+	add	$0x80, %edx
+	cmp	$0x80, %ebx
+	jae	L(128bytesormore_shared_cache_loop)
+	cmp	$0x80, %ecx
+	jb	L(shared_cache_loop_end)
+
+	ALIGN (4)
+L(128bytesormore_nt):
+	sub	$0x80, %ecx
+	movntdq	%xmm0, (%edx)
+	movntdq	%xmm0, 0x10(%edx)
+	movntdq	%xmm0, 0x20(%edx)
+	movntdq	%xmm0, 0x30(%edx)
+	movntdq	%xmm0, 0x40(%edx)
+	movntdq	%xmm0, 0x50(%edx)
+	movntdq	%xmm0, 0x60(%edx)
+	movntdq	%xmm0, 0x70(%edx)
+	add	$0x80, %edx
+	cmp	$0x80, %ecx
+	jae	L(128bytesormore_nt)
+	sfence
+L(shared_cache_loop_end):
+#if defined DATA_CACHE_SIZE || !(defined SHARED || defined __PIC__)
+	POP (%ebx)
+#endif
+	add	%ecx, %edx
+	shr	$2, %ecx
+	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
+
+	.pushsection .rodata.sse2,"a",@progbits
+	ALIGN (2)
+L(table_16_128bytes):
+	.int	JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
+	.int	JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
+	.popsection
+
+	ALIGN (4)
+L(aligned_16_112bytes):
+	movdqa	%xmm0, -112(%edx)
+L(aligned_16_96bytes):
+	movdqa	%xmm0, -96(%edx)
+L(aligned_16_80bytes):
+	movdqa	%xmm0, -80(%edx)
+L(aligned_16_64bytes):
+	movdqa	%xmm0, -64(%edx)
+L(aligned_16_48bytes):
+	movdqa	%xmm0, -48(%edx)
+L(aligned_16_32bytes):
+	movdqa	%xmm0, -32(%edx)
+L(aligned_16_16bytes):
+	movdqa	%xmm0, -16(%edx)
+L(aligned_16_0bytes):
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+L(aligned_16_116bytes):
+	movdqa	%xmm0, -116(%edx)
+L(aligned_16_100bytes):
+	movdqa	%xmm0, -100(%edx)
+L(aligned_16_84bytes):
+	movdqa	%xmm0, -84(%edx)
+L(aligned_16_68bytes):
+	movdqa	%xmm0, -68(%edx)
+L(aligned_16_52bytes):
+	movdqa	%xmm0, -52(%edx)
+L(aligned_16_36bytes):
+	movdqa	%xmm0, -36(%edx)
+L(aligned_16_20bytes):
+	movdqa	%xmm0, -20(%edx)
+L(aligned_16_4bytes):
+	movl	%eax, -4(%edx)
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+L(aligned_16_120bytes):
+	movdqa	%xmm0, -120(%edx)
+L(aligned_16_104bytes):
+	movdqa	%xmm0, -104(%edx)
+L(aligned_16_88bytes):
+	movdqa	%xmm0, -88(%edx)
+L(aligned_16_72bytes):
+	movdqa	%xmm0, -72(%edx)
+L(aligned_16_56bytes):
+	movdqa	%xmm0, -56(%edx)
+L(aligned_16_40bytes):
+	movdqa	%xmm0, -40(%edx)
+L(aligned_16_24bytes):
+	movdqa	%xmm0, -24(%edx)
+L(aligned_16_8bytes):
+	movq	%xmm0, -8(%edx)
+	SETRTNVAL
+	RETURN
+
+	ALIGN (4)
+L(aligned_16_124bytes):
+	movdqa	%xmm0, -124(%edx)
+L(aligned_16_108bytes):
+	movdqa	%xmm0, -108(%edx)
+L(aligned_16_92bytes):
+	movdqa	%xmm0, -92(%edx)
+L(aligned_16_76bytes):
+	movdqa	%xmm0, -76(%edx)
+L(aligned_16_60bytes):
+	movdqa	%xmm0, -60(%edx)
+L(aligned_16_44bytes):
+	movdqa	%xmm0, -44(%edx)
+L(aligned_16_28bytes):
+	movdqa	%xmm0, -28(%edx)
+L(aligned_16_12bytes):
+	movq	%xmm0, -12(%edx)
+	movl	%eax, -4(%edx)
+	SETRTNVAL
+	RETURN
+
+END (MEMSET)
diff --git a/libcutils/arch-x86/cache_wrapper.S b/libcutils/arch-x86/cache.h
similarity index 95%
rename from libcutils/arch-x86/cache_wrapper.S
rename to libcutils/arch-x86/cache.h
index 9eee25c..1c22fea 100644
--- a/libcutils/arch-x86/cache_wrapper.S
+++ b/libcutils/arch-x86/cache.h
@@ -13,9 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * Contributed by: Intel Corporation
- */
 
 #if defined(__slm__)
 /* Values are optimized for Silvermont */
diff --git a/libcutils/arch-x86/sse2-memset16-atom.S b/libcutils/arch-x86/sse2-memset16-atom.S
deleted file mode 100755
index c2a762b..0000000
--- a/libcutils/arch-x86/sse2-memset16-atom.S
+++ /dev/null
@@ -1,722 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * Contributed by: Intel Corporation
- */
-
-#ifndef L
-# define L(label)	.L##label
-#endif
-
-#ifndef ALIGN
-# define ALIGN(n)	.p2align n
-#endif
-
-#ifndef cfi_startproc
-# define cfi_startproc			.cfi_startproc
-#endif
-
-#ifndef cfi_endproc
-# define cfi_endproc			.cfi_endproc
-#endif
-
-#ifndef cfi_rel_offset
-# define cfi_rel_offset(reg, off)	.cfi_rel_offset reg, off
-#endif
-
-#ifndef cfi_restore
-# define cfi_restore(reg)		.cfi_restore reg
-#endif
-
-#ifndef cfi_adjust_cfa_offset
-# define cfi_adjust_cfa_offset(off)	.cfi_adjust_cfa_offset off
-#endif
-
-#ifndef ENTRY
-# define ENTRY(name)			\
-	.type name,  @function; 	\
-	.globl name;			\
-	.p2align 4;			\
-name:					\
-	cfi_startproc
-#endif
-
-#ifndef END
-# define END(name)			\
-	cfi_endproc;			\
-	.size name, .-name
-#endif
-
-#define CFI_PUSH(REG)						\
-  cfi_adjust_cfa_offset (4);					\
-  cfi_rel_offset (REG, 0)
-
-#define CFI_POP(REG)						\
-  cfi_adjust_cfa_offset (-4);					\
-  cfi_restore (REG)
-
-#define PUSH(REG)	pushl REG; CFI_PUSH (REG)
-#define POP(REG)	popl REG; CFI_POP (REG)
-
-#ifdef USE_AS_BZERO16
-# define DEST		PARMS
-# define LEN		DEST+4
-#else
-# define DEST		PARMS
-# define CHR		DEST+4
-# define LEN		CHR+4
-#endif
-
-#if 1
-# define SETRTNVAL
-#else
-# define SETRTNVAL	movl DEST(%esp), %eax
-#endif
-
-#if (defined SHARED || defined __PIC__)
-# define ENTRANCE	PUSH (%ebx);
-# define RETURN_END	POP (%ebx); ret
-# define RETURN		RETURN_END; CFI_PUSH (%ebx)
-# define PARMS		8		/* Preserve EBX.  */
-# define JMPTBL(I, B)	I - B
-
-/* Load an entry in a jump table into EBX and branch to it.  TABLE is a
-   jump table with relative offsets.   */
-# define BRANCH_TO_JMPTBL_ENTRY(TABLE)				\
-    /* We first load PC into EBX.  */				\
-    call	__i686.get_pc_thunk.bx;				\
-    /* Get the address of the jump table.  */			\
-    add		$(TABLE - .), %ebx;				\
-    /* Get the entry and convert the relative offset to the	\
-       absolute address.  */					\
-    add		(%ebx,%ecx,4), %ebx;				\
-    /* We loaded the jump table and adjuested EDX. Go.  */	\
-    jmp		*%ebx
-
-	.section	.gnu.linkonce.t.__i686.get_pc_thunk.bx,"ax",@progbits
-	.globl	__i686.get_pc_thunk.bx
-	.hidden	__i686.get_pc_thunk.bx
-	ALIGN (4)
-	.type	__i686.get_pc_thunk.bx,@function
-__i686.get_pc_thunk.bx:
-	movl	(%esp), %ebx
-	ret
-#else
-# define ENTRANCE
-# define RETURN_END	ret
-# define RETURN		RETURN_END
-# define PARMS		4
-# define JMPTBL(I, B)	I
-
-/* Branch to an entry in a jump table.  TABLE is a jump table with
-   absolute offsets.  */
-# define BRANCH_TO_JMPTBL_ENTRY(TABLE)				\
-    jmp		*TABLE(,%ecx,4)
-#endif
-
-	.section .text.sse2,"ax",@progbits
-	ALIGN (4)
-ENTRY (sse2_memset16_atom)
-	ENTRANCE
-
-	movl	LEN(%esp), %ecx
-#ifdef USE_AS_ANDROID
-	shr	$1, %ecx
-#endif
-#ifdef USE_AS_BZERO16
-	xor	%eax, %eax
-#else
-	movzwl	CHR(%esp), %eax
-	mov	%eax, %edx
-	shl	$16, %eax
-	or	%edx, %eax
-#endif
-	movl	DEST(%esp), %edx
-	cmp	$32, %ecx
-	jae	L(32wordsormore)
-
-L(write_less32words):
-	lea	(%edx, %ecx, 2), %edx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_less32words))
-
-
-	.pushsection .rodata.sse2,"a",@progbits
-	ALIGN (2)
-L(table_less32words):
-	.int	JMPTBL (L(write_0words), L(table_less32words))
-	.int	JMPTBL (L(write_1words), L(table_less32words))
-	.int	JMPTBL (L(write_2words), L(table_less32words))
-	.int	JMPTBL (L(write_3words), L(table_less32words))
-	.int	JMPTBL (L(write_4words), L(table_less32words))
-	.int	JMPTBL (L(write_5words), L(table_less32words))
-	.int	JMPTBL (L(write_6words), L(table_less32words))
-	.int	JMPTBL (L(write_7words), L(table_less32words))
-	.int	JMPTBL (L(write_8words), L(table_less32words))
-	.int	JMPTBL (L(write_9words), L(table_less32words))
-	.int	JMPTBL (L(write_10words), L(table_less32words))
-	.int	JMPTBL (L(write_11words), L(table_less32words))
-	.int	JMPTBL (L(write_12words), L(table_less32words))
-	.int	JMPTBL (L(write_13words), L(table_less32words))
-	.int	JMPTBL (L(write_14words), L(table_less32words))
-	.int	JMPTBL (L(write_15words), L(table_less32words))
-	.int	JMPTBL (L(write_16words), L(table_less32words))
-	.int	JMPTBL (L(write_17words), L(table_less32words))
-	.int	JMPTBL (L(write_18words), L(table_less32words))
-	.int	JMPTBL (L(write_19words), L(table_less32words))
-	.int	JMPTBL (L(write_20words), L(table_less32words))
-	.int	JMPTBL (L(write_21words), L(table_less32words))
-	.int	JMPTBL (L(write_22words), L(table_less32words))
-	.int	JMPTBL (L(write_23words), L(table_less32words))
-	.int	JMPTBL (L(write_24words), L(table_less32words))
-	.int	JMPTBL (L(write_25words), L(table_less32words))
-	.int	JMPTBL (L(write_26words), L(table_less32words))
-	.int	JMPTBL (L(write_27words), L(table_less32words))
-	.int	JMPTBL (L(write_28words), L(table_less32words))
-	.int	JMPTBL (L(write_29words), L(table_less32words))
-	.int	JMPTBL (L(write_30words), L(table_less32words))
-	.int	JMPTBL (L(write_31words), L(table_less32words))
-	.popsection
-
-	ALIGN (4)
-L(write_28words):
-	movl	%eax, -56(%edx)
-	movl	%eax, -52(%edx)
-L(write_24words):
-	movl	%eax, -48(%edx)
-	movl	%eax, -44(%edx)
-L(write_20words):
-	movl	%eax, -40(%edx)
-	movl	%eax, -36(%edx)
-L(write_16words):
-	movl	%eax, -32(%edx)
-	movl	%eax, -28(%edx)
-L(write_12words):
-	movl	%eax, -24(%edx)
-	movl	%eax, -20(%edx)
-L(write_8words):
-	movl	%eax, -16(%edx)
-	movl	%eax, -12(%edx)
-L(write_4words):
-	movl	%eax, -8(%edx)
-	movl	%eax, -4(%edx)
-L(write_0words):
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-L(write_29words):
-	movl	%eax, -58(%edx)
-	movl	%eax, -54(%edx)
-L(write_25words):
-	movl	%eax, -50(%edx)
-	movl	%eax, -46(%edx)
-L(write_21words):
-	movl	%eax, -42(%edx)
-	movl	%eax, -38(%edx)
-L(write_17words):
-	movl	%eax, -34(%edx)
-	movl	%eax, -30(%edx)
-L(write_13words):
-	movl	%eax, -26(%edx)
-	movl	%eax, -22(%edx)
-L(write_9words):
-	movl	%eax, -18(%edx)
-	movl	%eax, -14(%edx)
-L(write_5words):
-	movl	%eax, -10(%edx)
-	movl	%eax, -6(%edx)
-L(write_1words):
-	mov	%ax, -2(%edx)
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-L(write_30words):
-	movl	%eax, -60(%edx)
-	movl	%eax, -56(%edx)
-L(write_26words):
-	movl	%eax, -52(%edx)
-	movl	%eax, -48(%edx)
-L(write_22words):
-	movl	%eax, -44(%edx)
-	movl	%eax, -40(%edx)
-L(write_18words):
-	movl	%eax, -36(%edx)
-	movl	%eax, -32(%edx)
-L(write_14words):
-	movl	%eax, -28(%edx)
-	movl	%eax, -24(%edx)
-L(write_10words):
-	movl	%eax, -20(%edx)
-	movl	%eax, -16(%edx)
-L(write_6words):
-	movl	%eax, -12(%edx)
-	movl	%eax, -8(%edx)
-L(write_2words):
-	movl	%eax, -4(%edx)
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-L(write_31words):
-	movl	%eax, -62(%edx)
-	movl	%eax, -58(%edx)
-L(write_27words):
-	movl	%eax, -54(%edx)
-	movl	%eax, -50(%edx)
-L(write_23words):
-	movl	%eax, -46(%edx)
-	movl	%eax, -42(%edx)
-L(write_19words):
-	movl	%eax, -38(%edx)
-	movl	%eax, -34(%edx)
-L(write_15words):
-	movl	%eax, -30(%edx)
-	movl	%eax, -26(%edx)
-L(write_11words):
-	movl	%eax, -22(%edx)
-	movl	%eax, -18(%edx)
-L(write_7words):
-	movl	%eax, -14(%edx)
-	movl	%eax, -10(%edx)
-L(write_3words):
-	movl	%eax, -6(%edx)
-	movw	%ax, -2(%edx)
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-
-L(32wordsormore):
-	shl	$1, %ecx
-	test	$0x01, %edx
-	jz	L(aligned2bytes)
-	mov	%eax, (%edx)
-	mov	%eax, -4(%edx, %ecx)
-	sub	$2, %ecx
-	add	$1, %edx
-	rol	$8, %eax
-L(aligned2bytes):
-#ifdef USE_AS_BZERO16
-	pxor	%xmm0, %xmm0
-#else
-	movd	%eax, %xmm0
-	pshufd	$0, %xmm0, %xmm0
-#endif
-	testl	$0xf, %edx
-	jz	L(aligned_16)
-/* ECX > 32 and EDX is not 16 byte aligned.  */
-L(not_aligned_16):
-	movdqu	%xmm0, (%edx)
-	movl	%edx, %eax
-	and	$-16, %edx
-	add	$16, %edx
-	sub	%edx, %eax
-	add	%eax, %ecx
-	movd	%xmm0, %eax
-
-	ALIGN (4)
-L(aligned_16):
-	cmp	$128, %ecx
-	jae	L(128bytesormore)
-
-L(aligned_16_less128bytes):
-	add	%ecx, %edx
-	shr	$1, %ecx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-	ALIGN (4)
-L(128bytesormore):
-#ifdef SHARED_CACHE_SIZE
-	PUSH (%ebx)
-	mov	$SHARED_CACHE_SIZE, %ebx
-#else
-# if (defined SHARED || defined __PIC__)
-	call	__i686.get_pc_thunk.bx
-	add	$_GLOBAL_OFFSET_TABLE_, %ebx
-	mov	__x86_shared_cache_size@GOTOFF(%ebx), %ebx
-# else
-	PUSH (%ebx)
-	mov	__x86_shared_cache_size, %ebx
-# endif
-#endif
-	cmp	%ebx, %ecx
-	jae	L(128bytesormore_nt_start)
-
-	
-#ifdef DATA_CACHE_SIZE
-	POP (%ebx)
-# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
-	cmp	$DATA_CACHE_SIZE, %ecx
-#else
-# if (defined SHARED || defined __PIC__)
-#  define RESTORE_EBX_STATE
-	call	__i686.get_pc_thunk.bx
-	add	$_GLOBAL_OFFSET_TABLE_, %ebx
-	cmp	__x86_data_cache_size@GOTOFF(%ebx), %ecx
-# else
-	POP (%ebx)
-#  define RESTORE_EBX_STATE CFI_PUSH (%ebx)
-	cmp	__x86_data_cache_size, %ecx
-# endif
-#endif
-
-	jae	L(128bytes_L2_normal)
-	subl	$128, %ecx
-L(128bytesormore_normal):
-	sub	$128, %ecx
-	movdqa	%xmm0, (%edx)
-	movdqa	%xmm0, 0x10(%edx)
-	movdqa	%xmm0, 0x20(%edx)
-	movdqa	%xmm0, 0x30(%edx)
-	movdqa	%xmm0, 0x40(%edx)
-	movdqa	%xmm0, 0x50(%edx)
-	movdqa	%xmm0, 0x60(%edx)
-	movdqa	%xmm0, 0x70(%edx)
-	lea	128(%edx), %edx
-	jb	L(128bytesless_normal)
-
-
-	sub	$128, %ecx
-	movdqa	%xmm0, (%edx)
-	movdqa	%xmm0, 0x10(%edx)
-	movdqa	%xmm0, 0x20(%edx)
-	movdqa	%xmm0, 0x30(%edx)
-	movdqa	%xmm0, 0x40(%edx)
-	movdqa	%xmm0, 0x50(%edx)
-	movdqa	%xmm0, 0x60(%edx)
-	movdqa	%xmm0, 0x70(%edx)
-	lea	128(%edx), %edx
-	jae	L(128bytesormore_normal)
-
-L(128bytesless_normal):
-	lea	128(%ecx), %ecx
-	add	%ecx, %edx
-	shr	$1, %ecx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-	ALIGN (4)
-L(128bytes_L2_normal):
-	prefetcht0	0x380(%edx)
-	prefetcht0	0x3c0(%edx)
-	sub	$128, %ecx
-	movdqa	%xmm0, (%edx)
-	movaps	%xmm0, 0x10(%edx)
-	movaps	%xmm0, 0x20(%edx)
-	movaps	%xmm0, 0x30(%edx)
-	movaps	%xmm0, 0x40(%edx)
-	movaps	%xmm0, 0x50(%edx)
-	movaps	%xmm0, 0x60(%edx)
-	movaps	%xmm0, 0x70(%edx)
-	add	$128, %edx
-	cmp	$128, %ecx 	
-	jae	L(128bytes_L2_normal)
-
-L(128bytesless_L2_normal):
-	add	%ecx, %edx
-	shr	$1, %ecx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-	RESTORE_EBX_STATE
-L(128bytesormore_nt_start):
-	sub	%ebx, %ecx
-	mov	%ebx, %eax
-	and	$0x7f, %eax
-	add	%eax, %ecx
-	movd	%xmm0, %eax
-	ALIGN (4)
-L(128bytesormore_shared_cache_loop):
-	prefetcht0	0x3c0(%edx)
-	prefetcht0	0x380(%edx)
-	sub	$0x80, %ebx
-	movdqa	%xmm0, (%edx)
-	movdqa	%xmm0, 0x10(%edx)
-	movdqa	%xmm0, 0x20(%edx)
-	movdqa	%xmm0, 0x30(%edx)
-	movdqa	%xmm0, 0x40(%edx)
-	movdqa	%xmm0, 0x50(%edx)
-	movdqa	%xmm0, 0x60(%edx)
-	movdqa	%xmm0, 0x70(%edx)
-	add	$0x80, %edx
-	cmp	$0x80, %ebx
-	jae	L(128bytesormore_shared_cache_loop)
-	cmp	$0x80, %ecx
-	jb	L(shared_cache_loop_end)
-	ALIGN (4)
-L(128bytesormore_nt):
-	sub	$0x80, %ecx
-	movntdq	%xmm0, (%edx)
-	movntdq	%xmm0, 0x10(%edx)
-	movntdq	%xmm0, 0x20(%edx)
-	movntdq	%xmm0, 0x30(%edx)
-	movntdq	%xmm0, 0x40(%edx)
-	movntdq	%xmm0, 0x50(%edx)
-	movntdq	%xmm0, 0x60(%edx)
-	movntdq	%xmm0, 0x70(%edx)
-	add	$0x80, %edx
-	cmp	$0x80, %ecx
-	jae	L(128bytesormore_nt)
-	sfence
-L(shared_cache_loop_end):
-#if defined DATA_CACHE_SIZE || !(defined SHARED || defined __PIC__)
-	POP (%ebx)
-#endif
-	add	%ecx, %edx
-	shr	$1, %ecx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-
-	.pushsection .rodata.sse2,"a",@progbits
-	ALIGN (2)
-L(table_16_128bytes):
-	.int	JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_2bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_6bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_10bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_14bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_18bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_22bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_26bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_30bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_34bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_38bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_42bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_46bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_50bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_54bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_58bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_62bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_66bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_70bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_74bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_78bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_82bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_86bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_90bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_94bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_98bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_102bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_106bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_110bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_114bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_118bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_122bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_126bytes), L(table_16_128bytes))
-	.popsection
-
-
-	ALIGN (4)
-L(aligned_16_112bytes):
-	movdqa	%xmm0, -112(%edx)
-L(aligned_16_96bytes):
-	movdqa	%xmm0, -96(%edx)
-L(aligned_16_80bytes):
-	movdqa	%xmm0, -80(%edx)
-L(aligned_16_64bytes):
-	movdqa	%xmm0, -64(%edx)
-L(aligned_16_48bytes):
-	movdqa	%xmm0, -48(%edx)
-L(aligned_16_32bytes):
-	movdqa	%xmm0, -32(%edx)
-L(aligned_16_16bytes):
-	movdqa	%xmm0, -16(%edx)
-L(aligned_16_0bytes):
-	SETRTNVAL
-	RETURN
-
-
-	ALIGN (4)
-L(aligned_16_114bytes):
-	movdqa	%xmm0, -114(%edx)
-L(aligned_16_98bytes):
-	movdqa	%xmm0, -98(%edx)
-L(aligned_16_82bytes):
-	movdqa	%xmm0, -82(%edx)
-L(aligned_16_66bytes):
-	movdqa	%xmm0, -66(%edx)
-L(aligned_16_50bytes):
-	movdqa	%xmm0, -50(%edx)
-L(aligned_16_34bytes):
-	movdqa	%xmm0, -34(%edx)
-L(aligned_16_18bytes):
-	movdqa	%xmm0, -18(%edx)
-L(aligned_16_2bytes):
-	movw	%ax, -2(%edx)
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-L(aligned_16_116bytes):
-	movdqa	%xmm0, -116(%edx)
-L(aligned_16_100bytes):
-	movdqa	%xmm0, -100(%edx)
-L(aligned_16_84bytes):
-	movdqa	%xmm0, -84(%edx)
-L(aligned_16_68bytes):
-	movdqa	%xmm0, -68(%edx)
-L(aligned_16_52bytes):
-	movdqa	%xmm0, -52(%edx)
-L(aligned_16_36bytes):
-	movdqa	%xmm0, -36(%edx)
-L(aligned_16_20bytes):
-	movdqa	%xmm0, -20(%edx)
-L(aligned_16_4bytes):
-	movl	%eax, -4(%edx)
-	SETRTNVAL
-	RETURN
-
-
-	ALIGN (4)
-L(aligned_16_118bytes):
-	movdqa	%xmm0, -118(%edx)
-L(aligned_16_102bytes):
-	movdqa	%xmm0, -102(%edx)
-L(aligned_16_86bytes):
-	movdqa	%xmm0, -86(%edx)
-L(aligned_16_70bytes):
-	movdqa	%xmm0, -70(%edx)
-L(aligned_16_54bytes):
-	movdqa	%xmm0, -54(%edx)
-L(aligned_16_38bytes):
-	movdqa	%xmm0, -38(%edx)
-L(aligned_16_22bytes):
-	movdqa	%xmm0, -22(%edx)
-L(aligned_16_6bytes):
-	movl	%eax, -6(%edx)
-	movw	%ax, -2(%edx)
-	SETRTNVAL
-	RETURN
-
-
-	ALIGN (4)
-L(aligned_16_120bytes):
-	movdqa	%xmm0, -120(%edx)
-L(aligned_16_104bytes):
-	movdqa	%xmm0, -104(%edx)
-L(aligned_16_88bytes):
-	movdqa	%xmm0, -88(%edx)
-L(aligned_16_72bytes):
-	movdqa	%xmm0, -72(%edx)
-L(aligned_16_56bytes):
-	movdqa	%xmm0, -56(%edx)
-L(aligned_16_40bytes):
-	movdqa	%xmm0, -40(%edx)
-L(aligned_16_24bytes):
-	movdqa	%xmm0, -24(%edx)
-L(aligned_16_8bytes):
-	movq	%xmm0, -8(%edx)
-	SETRTNVAL
-	RETURN
-
-
-	ALIGN (4)
-L(aligned_16_122bytes):
-	movdqa	%xmm0, -122(%edx)
-L(aligned_16_106bytes):
-	movdqa	%xmm0, -106(%edx)
-L(aligned_16_90bytes):
-	movdqa	%xmm0, -90(%edx)
-L(aligned_16_74bytes):
-	movdqa	%xmm0, -74(%edx)
-L(aligned_16_58bytes):
-	movdqa	%xmm0, -58(%edx)
-L(aligned_16_42bytes):
-	movdqa	%xmm0, -42(%edx)
-L(aligned_16_26bytes):
-	movdqa	%xmm0, -26(%edx)
-L(aligned_16_10bytes):
-	movq	%xmm0, -10(%edx)
-	movw	%ax, -2(%edx)
-	SETRTNVAL
-	RETURN
-
-
-	ALIGN (4)
-L(aligned_16_124bytes):
-	movdqa	%xmm0, -124(%edx)
-L(aligned_16_108bytes):
-	movdqa	%xmm0, -108(%edx)
-L(aligned_16_92bytes):
-	movdqa	%xmm0, -92(%edx)
-L(aligned_16_76bytes):
-	movdqa	%xmm0, -76(%edx)
-L(aligned_16_60bytes):
-	movdqa	%xmm0, -60(%edx)
-L(aligned_16_44bytes):
-	movdqa	%xmm0, -44(%edx)
-L(aligned_16_28bytes):
-	movdqa	%xmm0, -28(%edx)
-L(aligned_16_12bytes):
-	movq	%xmm0, -12(%edx)
-	movl	%eax, -4(%edx)
-	SETRTNVAL
-	RETURN
-
-
-	ALIGN (4)
-L(aligned_16_126bytes):
-	movdqa	%xmm0, -126(%edx)
-L(aligned_16_110bytes):
-	movdqa	%xmm0, -110(%edx)
-L(aligned_16_94bytes):
-	movdqa	%xmm0, -94(%edx)
-L(aligned_16_78bytes):
-	movdqa	%xmm0, -78(%edx)
-L(aligned_16_62bytes):
-	movdqa	%xmm0, -62(%edx)
-L(aligned_16_46bytes):
-	movdqa	%xmm0, -46(%edx)
-L(aligned_16_30bytes):
-	movdqa	%xmm0, -30(%edx)
-L(aligned_16_14bytes):
-	movq	%xmm0, -14(%edx)
-	movl	%eax, -6(%edx)
-	movw	%ax, -2(%edx)
-	SETRTNVAL
-	RETURN
-
-END (sse2_memset16_atom)
diff --git a/libcutils/arch-x86/sse2-memset32-atom.S b/libcutils/arch-x86/sse2-memset32-atom.S
deleted file mode 100755
index 05eb64f..0000000
--- a/libcutils/arch-x86/sse2-memset32-atom.S
+++ /dev/null
@@ -1,513 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * Contributed by: Intel Corporation
- */
-
-#ifndef L
-# define L(label)	.L##label
-#endif
-
-#ifndef ALIGN
-# define ALIGN(n)	.p2align n
-#endif
-
-#ifndef cfi_startproc
-# define cfi_startproc			.cfi_startproc
-#endif
-
-#ifndef cfi_endproc
-# define cfi_endproc			.cfi_endproc
-#endif
-
-#ifndef cfi_rel_offset
-# define cfi_rel_offset(reg, off)	.cfi_rel_offset reg, off
-#endif
-
-#ifndef cfi_restore
-# define cfi_restore(reg)		.cfi_restore reg
-#endif
-
-#ifndef cfi_adjust_cfa_offset
-# define cfi_adjust_cfa_offset(off)	.cfi_adjust_cfa_offset off
-#endif
-
-#ifndef ENTRY
-# define ENTRY(name)			\
-	.type name,  @function; 	\
-	.globl name;			\
-	.p2align 4;			\
-name:					\
-	cfi_startproc
-#endif
-
-#ifndef END
-# define END(name)			\
-	cfi_endproc;			\
-	.size name, .-name
-#endif
-
-#define CFI_PUSH(REG)						\
-  cfi_adjust_cfa_offset (4);					\
-  cfi_rel_offset (REG, 0)
-
-#define CFI_POP(REG)						\
-  cfi_adjust_cfa_offset (-4);					\
-  cfi_restore (REG)
-
-#define PUSH(REG)	pushl REG; CFI_PUSH (REG)
-#define POP(REG)	popl REG; CFI_POP (REG)
-
-#ifdef USE_AS_BZERO32
-# define DEST		PARMS
-# define LEN		DEST+4
-#else
-# define DEST		PARMS
-# define DWDS		DEST+4
-# define LEN		DWDS+4
-#endif
-
-#ifdef USE_AS_WMEMSET32
-# define SETRTNVAL	movl DEST(%esp), %eax
-#else
-# define SETRTNVAL
-#endif
-
-#if (defined SHARED || defined __PIC__)
-# define ENTRANCE	PUSH (%ebx);
-# define RETURN_END	POP (%ebx); ret
-# define RETURN		RETURN_END; CFI_PUSH (%ebx)
-# define PARMS		8		/* Preserve EBX.  */
-# define JMPTBL(I, B)	I - B
-
-/* Load an entry in a jump table into EBX and branch to it.  TABLE is a
-   jump table with relative offsets.   */
-# define BRANCH_TO_JMPTBL_ENTRY(TABLE)				\
-    /* We first load PC into EBX.  */				\
-    call	__i686.get_pc_thunk.bx;				\
-    /* Get the address of the jump table.  */			\
-    add		$(TABLE - .), %ebx;				\
-    /* Get the entry and convert the relative offset to the	\
-       absolute address.  */					\
-    add		(%ebx,%ecx,4), %ebx;				\
-    /* We loaded the jump table and adjuested EDX. Go.  */	\
-    jmp		*%ebx
-
-	.section	.gnu.linkonce.t.__i686.get_pc_thunk.bx,"ax",@progbits
-	.globl	__i686.get_pc_thunk.bx
-	.hidden	__i686.get_pc_thunk.bx
-	ALIGN (4)
-	.type	__i686.get_pc_thunk.bx,@function
-__i686.get_pc_thunk.bx:
-	movl	(%esp), %ebx
-	ret
-#else
-# define ENTRANCE
-# define RETURN_END	ret
-# define RETURN		RETURN_END
-# define PARMS		4
-# define JMPTBL(I, B)	I
-
-/* Branch to an entry in a jump table.  TABLE is a jump table with
-   absolute offsets.  */
-# define BRANCH_TO_JMPTBL_ENTRY(TABLE)				\
-    jmp		*TABLE(,%ecx,4)
-#endif
-
-	.section .text.sse2,"ax",@progbits
-	ALIGN (4)
-ENTRY (sse2_memset32_atom)
-	ENTRANCE
-
-	movl	LEN(%esp), %ecx
-#ifdef USE_AS_ANDROID
-	shr     $2, %ecx
-#endif
-#ifdef USE_AS_BZERO32
-	xor	%eax, %eax
-#else
-	mov	DWDS(%esp), %eax
-	mov	%eax, %edx
-#endif
-	movl	DEST(%esp), %edx
-	cmp	$16, %ecx
-	jae	L(16dbwordsormore)
-
-L(write_less16dbwords):
-	lea	(%edx, %ecx, 4), %edx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_less16dbwords))
-
-	.pushsection .rodata.sse2,"a",@progbits
-	ALIGN (2)
-L(table_less16dbwords):
-	.int	JMPTBL (L(write_0dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_1dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_2dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_3dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_4dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_5dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_6dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_7dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_8dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_9dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_10dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_11dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_12dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_13dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_14dbwords), L(table_less16dbwords))
-	.int	JMPTBL (L(write_15dbwords), L(table_less16dbwords))
-	.popsection
-
-	ALIGN (4)
-L(write_15dbwords):
-	movl	%eax, -60(%edx)
-L(write_14dbwords):
-	movl	%eax, -56(%edx)
-L(write_13dbwords):
-	movl	%eax, -52(%edx)
-L(write_12dbwords):
-	movl	%eax, -48(%edx)
-L(write_11dbwords):
-	movl	%eax, -44(%edx)
-L(write_10dbwords):
-	movl	%eax, -40(%edx)
-L(write_9dbwords):
-	movl	%eax, -36(%edx)
-L(write_8dbwords):
-	movl	%eax, -32(%edx)
-L(write_7dbwords):
-	movl	%eax, -28(%edx)
-L(write_6dbwords):
-	movl	%eax, -24(%edx)
-L(write_5dbwords):
-	movl	%eax, -20(%edx)
-L(write_4dbwords):
-	movl	%eax, -16(%edx)
-L(write_3dbwords):
-	movl	%eax, -12(%edx)
-L(write_2dbwords):
-	movl	%eax, -8(%edx)
-L(write_1dbwords):
-	movl	%eax, -4(%edx)
-L(write_0dbwords):
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-L(16dbwordsormore):
-	test	$3, %edx
-	jz	L(aligned4bytes)
-	mov	%eax, (%edx)
-	mov	%eax, -4(%edx, %ecx, 4)
-	sub	$1, %ecx
-	rol	$24, %eax
-	add	$1, %edx
-	test	$3, %edx
-	jz	L(aligned4bytes)
-	ror	$8, %eax
-	add	$1, %edx
-	test	$3, %edx
-	jz	L(aligned4bytes)
-	ror	$8, %eax
-	add	$1, %edx
-L(aligned4bytes):
-	shl	$2, %ecx
-
-#ifdef USE_AS_BZERO32
-	pxor	%xmm0, %xmm0
-#else
-	movd	%eax, %xmm0
-	pshufd	$0, %xmm0, %xmm0
-#endif
-	testl	$0xf, %edx
-	jz	L(aligned_16)
-/* ECX > 32 and EDX is not 16 byte aligned.  */
-L(not_aligned_16):
-	movdqu	%xmm0, (%edx)
-	movl	%edx, %eax
-	and	$-16, %edx
-	add	$16, %edx
-	sub	%edx, %eax
-	add	%eax, %ecx
-	movd	%xmm0, %eax
-	ALIGN (4)
-L(aligned_16):
-	cmp	$128, %ecx
-	jae	L(128bytesormore)
-
-L(aligned_16_less128bytes):
-	add	%ecx, %edx
-	shr	$2, %ecx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-	ALIGN (4)
-L(128bytesormore):
-#ifdef SHARED_CACHE_SIZE
-	PUSH (%ebx)
-	mov	$SHARED_CACHE_SIZE, %ebx
-#else
-# if (defined SHARED || defined __PIC__)
-	call	__i686.get_pc_thunk.bx
-	add	$_GLOBAL_OFFSET_TABLE_, %ebx
-	mov	__x86_shared_cache_size@GOTOFF(%ebx), %ebx
-# else
-	PUSH (%ebx)
-	mov	__x86_shared_cache_size, %ebx
-# endif
-#endif
-	cmp	%ebx, %ecx
-	jae	L(128bytesormore_nt_start)
-	
-#ifdef DATA_CACHE_SIZE
-	POP (%ebx)
-# define RESTORE_EBX_STATE CFI_PUSH (%ebx)
-	cmp	$DATA_CACHE_SIZE, %ecx
-#else
-# if (defined SHARED || defined __PIC__)
-#  define RESTORE_EBX_STATE
-	call	__i686.get_pc_thunk.bx
-	add	$_GLOBAL_OFFSET_TABLE_, %ebx
-	cmp	__x86_data_cache_size@GOTOFF(%ebx), %ecx
-# else
-	POP (%ebx)
-#  define RESTORE_EBX_STATE CFI_PUSH (%ebx)
-	cmp	__x86_data_cache_size, %ecx
-# endif
-#endif
-
-	jae	L(128bytes_L2_normal)
-	subl	$128, %ecx
-L(128bytesormore_normal):
-	sub	$128, %ecx
-	movdqa	%xmm0, (%edx)
-	movdqa	%xmm0, 0x10(%edx)
-	movdqa	%xmm0, 0x20(%edx)
-	movdqa	%xmm0, 0x30(%edx)
-	movdqa	%xmm0, 0x40(%edx)
-	movdqa	%xmm0, 0x50(%edx)
-	movdqa	%xmm0, 0x60(%edx)
-	movdqa	%xmm0, 0x70(%edx)
-	lea	128(%edx), %edx
-	jb	L(128bytesless_normal)
-
-
-	sub	$128, %ecx
-	movdqa	%xmm0, (%edx)
-	movdqa	%xmm0, 0x10(%edx)
-	movdqa	%xmm0, 0x20(%edx)
-	movdqa	%xmm0, 0x30(%edx)
-	movdqa	%xmm0, 0x40(%edx)
-	movdqa	%xmm0, 0x50(%edx)
-	movdqa	%xmm0, 0x60(%edx)
-	movdqa	%xmm0, 0x70(%edx)
-	lea	128(%edx), %edx
-	jae	L(128bytesormore_normal)
-
-L(128bytesless_normal):
-	lea	128(%ecx), %ecx
-	add	%ecx, %edx
-	shr	$2, %ecx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-	ALIGN (4)
-L(128bytes_L2_normal):
-	prefetcht0	0x380(%edx)
-	prefetcht0	0x3c0(%edx)
-	sub	$128, %ecx
-	movdqa	%xmm0, (%edx)
-	movaps	%xmm0, 0x10(%edx)
-	movaps	%xmm0, 0x20(%edx)
-	movaps	%xmm0, 0x30(%edx)
-	movaps	%xmm0, 0x40(%edx)
-	movaps	%xmm0, 0x50(%edx)
-	movaps	%xmm0, 0x60(%edx)
-	movaps	%xmm0, 0x70(%edx)
-	add	$128, %edx
-	cmp	$128, %ecx 	
-	jae	L(128bytes_L2_normal)
-
-L(128bytesless_L2_normal):
-	add	%ecx, %edx
-	shr	$2, %ecx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-	RESTORE_EBX_STATE
-L(128bytesormore_nt_start):
-	sub	%ebx, %ecx
-	mov	%ebx, %eax
-	and	$0x7f, %eax
-	add	%eax, %ecx
-	movd	%xmm0, %eax
-	ALIGN (4)
-L(128bytesormore_shared_cache_loop):
-	prefetcht0	0x3c0(%edx)
-	prefetcht0	0x380(%edx)
-	sub	$0x80, %ebx
-	movdqa	%xmm0, (%edx)
-	movdqa	%xmm0, 0x10(%edx)
-	movdqa	%xmm0, 0x20(%edx)
-	movdqa	%xmm0, 0x30(%edx)
-	movdqa	%xmm0, 0x40(%edx)
-	movdqa	%xmm0, 0x50(%edx)
-	movdqa	%xmm0, 0x60(%edx)
-	movdqa	%xmm0, 0x70(%edx)
-	add	$0x80, %edx
-	cmp	$0x80, %ebx
-	jae	L(128bytesormore_shared_cache_loop)
-	cmp	$0x80, %ecx
-	jb	L(shared_cache_loop_end)
-
-	ALIGN (4)
-L(128bytesormore_nt):
-	sub	$0x80, %ecx
-	movntdq	%xmm0, (%edx)
-	movntdq	%xmm0, 0x10(%edx)
-	movntdq	%xmm0, 0x20(%edx)
-	movntdq	%xmm0, 0x30(%edx)
-	movntdq	%xmm0, 0x40(%edx)
-	movntdq	%xmm0, 0x50(%edx)
-	movntdq	%xmm0, 0x60(%edx)
-	movntdq	%xmm0, 0x70(%edx)
-	add	$0x80, %edx
-	cmp	$0x80, %ecx
-	jae	L(128bytesormore_nt)
-	sfence
-L(shared_cache_loop_end):
-#if defined DATA_CACHE_SIZE || !(defined SHARED || defined __PIC__)
-	POP (%ebx)
-#endif
-	add	%ecx, %edx
-	shr	$2, %ecx
-	BRANCH_TO_JMPTBL_ENTRY (L(table_16_128bytes))
-
-	.pushsection .rodata.sse2,"a",@progbits
-	ALIGN (2)
-L(table_16_128bytes):
-	.int	JMPTBL (L(aligned_16_0bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_4bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_8bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_12bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_16bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_20bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_24bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_28bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_32bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_36bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_40bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_44bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_48bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_52bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_56bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_60bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_64bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_68bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_72bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_76bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_80bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_84bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_88bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_92bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_96bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_100bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_104bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_108bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_112bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_116bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_120bytes), L(table_16_128bytes))
-	.int	JMPTBL (L(aligned_16_124bytes), L(table_16_128bytes))
-	.popsection
-
-	ALIGN (4)
-L(aligned_16_112bytes):
-	movdqa	%xmm0, -112(%edx)
-L(aligned_16_96bytes):
-	movdqa	%xmm0, -96(%edx)
-L(aligned_16_80bytes):
-	movdqa	%xmm0, -80(%edx)
-L(aligned_16_64bytes):
-	movdqa	%xmm0, -64(%edx)
-L(aligned_16_48bytes):
-	movdqa	%xmm0, -48(%edx)
-L(aligned_16_32bytes):
-	movdqa	%xmm0, -32(%edx)
-L(aligned_16_16bytes):
-	movdqa	%xmm0, -16(%edx)
-L(aligned_16_0bytes):
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-L(aligned_16_116bytes):
-	movdqa	%xmm0, -116(%edx)
-L(aligned_16_100bytes):
-	movdqa	%xmm0, -100(%edx)
-L(aligned_16_84bytes):
-	movdqa	%xmm0, -84(%edx)
-L(aligned_16_68bytes):
-	movdqa	%xmm0, -68(%edx)
-L(aligned_16_52bytes):
-	movdqa	%xmm0, -52(%edx)
-L(aligned_16_36bytes):
-	movdqa	%xmm0, -36(%edx)
-L(aligned_16_20bytes):
-	movdqa	%xmm0, -20(%edx)
-L(aligned_16_4bytes):
-	movl	%eax, -4(%edx)
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-L(aligned_16_120bytes):
-	movdqa	%xmm0, -120(%edx)
-L(aligned_16_104bytes):
-	movdqa	%xmm0, -104(%edx)
-L(aligned_16_88bytes):
-	movdqa	%xmm0, -88(%edx)
-L(aligned_16_72bytes):
-	movdqa	%xmm0, -72(%edx)
-L(aligned_16_56bytes):
-	movdqa	%xmm0, -56(%edx)
-L(aligned_16_40bytes):
-	movdqa	%xmm0, -40(%edx)
-L(aligned_16_24bytes):
-	movdqa	%xmm0, -24(%edx)
-L(aligned_16_8bytes):
-	movq	%xmm0, -8(%edx)
-	SETRTNVAL
-	RETURN
-
-	ALIGN (4)
-L(aligned_16_124bytes):
-	movdqa	%xmm0, -124(%edx)
-L(aligned_16_108bytes):
-	movdqa	%xmm0, -108(%edx)
-L(aligned_16_92bytes):
-	movdqa	%xmm0, -92(%edx)
-L(aligned_16_76bytes):
-	movdqa	%xmm0, -76(%edx)
-L(aligned_16_60bytes):
-	movdqa	%xmm0, -60(%edx)
-L(aligned_16_44bytes):
-	movdqa	%xmm0, -44(%edx)
-L(aligned_16_28bytes):
-	movdqa	%xmm0, -28(%edx)
-L(aligned_16_12bytes):
-	movq	%xmm0, -12(%edx)
-	movl	%eax, -4(%edx)
-	SETRTNVAL
-	RETURN
-
-END (sse2_memset32_atom)
diff --git a/libcutils/arch-x86_64/android_memset16_SSE2-atom.S b/libcutils/arch-x86_64/android_memset16.S
similarity index 98%
rename from libcutils/arch-x86_64/android_memset16_SSE2-atom.S
rename to libcutils/arch-x86_64/android_memset16.S
index 48a10ed..cb6d4a3 100644
--- a/libcutils/arch-x86_64/android_memset16_SSE2-atom.S
+++ b/libcutils/arch-x86_64/android_memset16.S
@@ -13,12 +13,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * Contributed by: Intel Corporation
- */
 
 #include "cache.h"
 
+#ifndef MEMSET
+# define MEMSET		android_memset16
+#endif
+
 #ifndef L
 # define L(label)	.L##label
 #endif
@@ -63,7 +64,7 @@
 
 	.section .text.sse2,"ax",@progbits
 	ALIGN (4)
-ENTRY (android_memset16)	// Address in rdi
+ENTRY (MEMSET)	// Address in rdi
 	shr    $1, %rdx			// Count in rdx
 	movzwl %si, %ecx
 	/* Fill the whole ECX with pattern.  */
@@ -561,4 +562,4 @@
 	movw   %cx, -2(%rdi)
 	ret
 
-END (android_memset16)
+END (MEMSET)
diff --git a/libcutils/arch-x86_64/android_memset32_SSE2-atom.S b/libcutils/arch-x86_64/android_memset32.S
similarity index 98%
rename from libcutils/arch-x86_64/android_memset32_SSE2-atom.S
rename to libcutils/arch-x86_64/android_memset32.S
index 4bdea8e..1514aa2 100644
--- a/libcutils/arch-x86_64/android_memset32_SSE2-atom.S
+++ b/libcutils/arch-x86_64/android_memset32.S
@@ -13,12 +13,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * Contributed by: Intel Corporation
- */
 
 #include "cache.h"
 
+#ifndef MEMSET
+# define MEMSET		android_memset32
+#endif
+
 #ifndef L
 # define L(label)	.L##label
 #endif
@@ -63,7 +64,7 @@
 
 	.section .text.sse2,"ax",@progbits
 	ALIGN (4)
-ENTRY (android_memset32)	// Address in rdi
+ENTRY (MEMSET)	// Address in rdi
 	shr    $2, %rdx			// Count in rdx
 	movl   %esi, %ecx		// Pattern in ecx
 
@@ -369,4 +370,4 @@
 	movl	%ecx, -4(%rdi)
 	ret
 
-END (android_memset32)
+END (MEMSET)
diff --git a/libcutils/arch-x86_64/cache.h b/libcutils/arch-x86_64/cache.h
index ab5dd2f..f144309 100644
--- a/libcutils/arch-x86_64/cache.h
+++ b/libcutils/arch-x86_64/cache.h
@@ -13,19 +13,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * Contributed by: Intel Corporation
- */
 
-#if defined(__slm__)
 /* Values are optimized for Silvermont */
 #define SHARED_CACHE_SIZE	(1024*1024)			/* Silvermont L2 Cache */
 #define DATA_CACHE_SIZE		(24*1024)			/* Silvermont L1 Data Cache */
-#else
-/* Values are optimized for Atom */
-#define SHARED_CACHE_SIZE	(512*1024)			/* Atom L2 Cache */
-#define DATA_CACHE_SIZE		(24*1024)			/* Atom L1 Data Cache */
-#endif
 
 #define SHARED_CACHE_SIZE_HALF	(SHARED_CACHE_SIZE / 2)
 #define DATA_CACHE_SIZE_HALF	(DATA_CACHE_SIZE / 2)
diff --git a/libcutils/atomic.c b/libcutils/atomic.c
index 1484ef8..d34aa00 100644
--- a/libcutils/atomic.c
+++ b/libcutils/atomic.c
@@ -14,6 +14,13 @@
  * limitations under the License.
  */
 
+/*
+ * Generate non-inlined versions of android_atomic functions.
+ * Nobody should be using these, but some binary blobs currently (late 2014)
+ * are.
+ * If you read this in 2015 or later, please try to delete this file.
+ */
+
 #define ANDROID_ATOMIC_INLINE
 
-#include <cutils/atomic-inline.h>
+#include <cutils/atomic.h>
diff --git a/libcutils/cpu_info.c b/libcutils/cpu_info.c
deleted file mode 100644
index 21fa1dc..0000000
--- a/libcutils/cpu_info.c
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
-** Copyright 2007, 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 <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <cutils/cpu_info.h>
-
-// we cache the serial number here.
-// this is also used as a fgets() line buffer when we are reading /proc/cpuinfo
-static char serial_number[100] = { 0 };
-
-extern const char* get_cpu_serial_number(void)
-{
-    if (serial_number[0] == 0)
-    {
-        FILE* file;
-        char* chp, *end;
-        char* whitespace;
-        
-        // read serial number from /proc/cpuinfo
-        file = fopen("proc/cpuinfo", "r");
-        if (! file)
-            return NULL;
-        
-        while ((chp = fgets(serial_number, sizeof(serial_number), file)) != NULL)
-        {
-            // look for something like "Serial          : 999206122a03591c"
-
-            if (strncmp(chp, "Serial", 6) != 0)
-                continue;
-            
-            chp = strchr(chp, ':');
-            if (!chp)
-                continue;
-                
-             // skip colon and whitespace
-            while ( *(++chp) == ' ') {}
-            
-            // truncate trailing whitespace
-            end = chp;
-            while (*end && *end != ' ' && *end != '\t' && *end != '\n' && *end != '\r')
-                ++end;
-            *end = 0; 
-            
-            whitespace = strchr(chp, ' ');
-            if (whitespace)
-                *whitespace = 0;
-            whitespace = strchr(chp, '\t');
-            if (whitespace)
-                *whitespace = 0;
-            whitespace = strchr(chp, '\r');
-            if (whitespace)
-                *whitespace = 0;
-            whitespace = strchr(chp, '\n');
-            if (whitespace)
-                *whitespace = 0;
-
-            // shift serial number to beginning of the buffer
-            memmove(serial_number, chp, strlen(chp) + 1);
-            break;
-        }
-        
-        fclose(file);
-    }
-
-    return (serial_number[0] ? serial_number : NULL);
-}
diff --git a/libcutils/debugger.c b/libcutils/debugger.c
index b8a2efc..2cd8ec3 100644
--- a/libcutils/debugger.c
+++ b/libcutils/debugger.c
@@ -29,33 +29,6 @@
 #define LOG_TAG "DEBUG"
 #include <log/log.h>
 
-#if defined(__LP64__)
-#include <elf.h>
-
-static bool is32bit(pid_t tid) {
-  char* exeline;
-  if (asprintf(&exeline, "/proc/%d/exe", tid) == -1) {
-    return false;
-  }
-  int fd = open(exeline, O_RDONLY | O_CLOEXEC);
-  free(exeline);
-  if (fd == -1) {
-    return false;
-  }
-
-  char ehdr[EI_NIDENT];
-  ssize_t bytes = read(fd, &ehdr, sizeof(ehdr));
-  close(fd);
-  if (bytes != (ssize_t) sizeof(ehdr) || memcmp(ELFMAG, ehdr, SELFMAG) != 0) {
-    return false;
-  }
-  if (ehdr[EI_CLASS] == ELFCLASS32) {
-    return true;
-  }
-  return false;
-}
-#endif
-
 static int send_request(int sock_fd, void* msg_ptr, size_t msg_len) {
   int result = 0;
   if (TEMP_FAILURE_RETRY(write(sock_fd, msg_ptr, msg_len)) != (ssize_t) msg_len) {
@@ -72,32 +45,11 @@
 static int make_dump_request(debugger_action_t action, pid_t tid, int timeout_secs) {
   const char* socket_name;
   debugger_msg_t msg;
-  size_t msg_len;
-  void* msg_ptr;
+  memset(&msg, 0, sizeof(msg));
+  msg.tid = tid;
+  msg.action = action;
 
-#if defined(__LP64__)
-  debugger32_msg_t msg32;
-  if (is32bit(tid)) {
-    msg_len = sizeof(debugger32_msg_t);
-    memset(&msg32, 0, msg_len);
-    msg32.tid = tid;
-    msg32.action = action;
-    msg_ptr = &msg32;
-
-    socket_name = DEBUGGER32_SOCKET_NAME;
-  } else
-#endif
-  {
-    msg_len = sizeof(debugger_msg_t);
-    memset(&msg, 0, msg_len);
-    msg.tid = tid;
-    msg.action = action;
-    msg_ptr = &msg;
-
-    socket_name = DEBUGGER_SOCKET_NAME;
-  }
-
-  int sock_fd = socket_local_client(socket_name, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
+  int sock_fd = socket_local_client(DEBUGGER_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT,
       SOCK_STREAM | SOCK_CLOEXEC);
   if (sock_fd < 0) {
     return -1;
@@ -116,7 +68,7 @@
     }
   }
 
-  if (send_request(sock_fd, msg_ptr, msg_len) < 0) {
+  if (send_request(sock_fd, &msg, sizeof(msg)) < 0) {
     TEMP_FAILURE_RETRY(close(sock_fd));
     return -1;
   }
diff --git a/libcutils/open_memstream.c b/libcutils/open_memstream.c
index 5b4388a..9183266 100644
--- a/libcutils/open_memstream.c
+++ b/libcutils/open_memstream.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef HAVE_OPEN_MEMSTREAM
+#if defined(__APPLE__)
 
 /*
  * Implementation of the POSIX open_memstream() function, which Linux has
@@ -59,8 +59,6 @@
 # define DBUG(x) ((void)0)
 #endif
 
-#ifdef HAVE_FUNOPEN
-
 /*
  * Definition of a seekable, write-only memory stream.
  */
@@ -251,12 +249,6 @@
     return fp;
 }
 
-#else /*not HAVE_FUNOPEN*/
-FILE* open_memstream(char** bufp, size_t* sizep)
-{
-    abort();
-}
-#endif /*HAVE_FUNOPEN*/
 
 
 
@@ -378,4 +370,4 @@
 
 #endif
 
-#endif /*!HAVE_OPEN_MEMSTREAM*/
+#endif /* __APPLE__ */
diff --git a/libcutils/process_name.c b/libcutils/process_name.c
index 9c3dfb8..cc931eb 100644
--- a/libcutils/process_name.c
+++ b/libcutils/process_name.c
@@ -17,7 +17,7 @@
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
-#if defined(HAVE_PRCTL)
+#if defined(__linux__)
 #include <sys/prctl.h>
 #endif
 #include <sys/stat.h>
@@ -51,7 +51,7 @@
     strcpy(copy, new_name);
     process_name = (const char*) copy;
 
-#if defined(HAVE_PRCTL)
+#if defined(__linux__)
     if (len < 16) {
         prctl(PR_SET_NAME, (unsigned long) new_name, 0, 0, 0);
     } else {
diff --git a/libcutils/properties.c b/libcutils/properties.c
index b283658..1190ab7 100644
--- a/libcutils/properties.c
+++ b/libcutils/properties.c
@@ -104,7 +104,7 @@
     return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value);
 }
 
-#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
+#ifdef __BIONIC__
 
 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
 #include <sys/_system_properties.h>
@@ -157,191 +157,9 @@
     return __system_property_foreach(property_list_callback, &data);
 }
 
-#elif defined(HAVE_SYSTEM_PROPERTY_SERVER)
-
-/*
- * The Linux simulator provides a "system property server" that uses IPC
- * to set/get/list properties.  The file descriptor is shared by all
- * threads in the process, so we use a mutex to ensure that requests
- * from multiple threads don't get interleaved.
- */
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <pthread.h>
-
-static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT;
-static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER;
-static int gPropFd = -1;
-
-/*
- * Connect to the properties server.
- *
- * Returns the socket descriptor on success.
- */
-static int connectToServer(const char* fileName)
-{
-    int sock = -1;
-    int cc;
-
-    struct sockaddr_un addr;
-    
-    sock = socket(AF_UNIX, SOCK_STREAM, 0);
-    if (sock < 0) {
-        ALOGW("UNIX domain socket create failed (errno=%d)\n", errno);
-        return -1;
-    }
-
-    /* connect to socket; fails if file doesn't exist */
-    strcpy(addr.sun_path, fileName);    // max 108 bytes
-    addr.sun_family = AF_UNIX;
-    cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
-    if (cc < 0) {
-        // ENOENT means socket file doesn't exist
-        // ECONNREFUSED means socket exists but nobody is listening
-        //ALOGW("AF_UNIX connect failed for '%s': %s\n",
-        //    fileName, strerror(errno));
-        close(sock);
-        return -1;
-    }
-
-    return sock;
-}
-
-/*
- * Perform one-time initialization.
- */
-static void init(void)
-{
-    assert(gPropFd == -1);
-
-    gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME);
-    if (gPropFd < 0) {
-        //ALOGW("not connected to system property server\n");
-    } else {
-        //ALOGV("Connected to system property server\n");
-    }
-}
-
-int property_get(const char *key, char *value, const char *default_value)
-{
-    char sendBuf[1+PROPERTY_KEY_MAX];
-    char recvBuf[1+PROPERTY_VALUE_MAX];
-    int len = -1;
-
-    //ALOGV("PROPERTY GET [%s]\n", key);
-
-    pthread_once(&gInitOnce, init);
-    if (gPropFd < 0) {
-        /* this mimics the behavior of the device implementation */
-        if (default_value != NULL) {
-            strcpy(value, default_value);
-            len = strlen(value);
-        }
-        return len;
-    }
-
-    if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
-
-    memset(sendBuf, 0xdd, sizeof(sendBuf));    // placate valgrind
-
-    sendBuf[0] = (char) kSystemPropertyGet;
-    strcpy(sendBuf+1, key);
-
-    pthread_mutex_lock(&gPropertyFdLock);
-    if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
-        pthread_mutex_unlock(&gPropertyFdLock);
-        return -1;
-    }
-    if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
-        pthread_mutex_unlock(&gPropertyFdLock);
-        return -1;
-    }
-    pthread_mutex_unlock(&gPropertyFdLock);
-
-    /* first byte is 0 if value not defined, 1 if found */
-    if (recvBuf[0] == 0) {
-        if (default_value != NULL) {
-            strcpy(value, default_value);
-            len = strlen(value);
-        } else {
-            /*
-             * If the value isn't defined, hand back an empty string and
-             * a zero length, rather than a failure.  This seems wrong,
-             * since you can't tell the difference between "undefined" and
-             * "defined but empty", but it's what the device does.
-             */
-            value[0] = '\0';
-            len = 0;
-        }
-    } else if (recvBuf[0] == 1) {
-        strcpy(value, recvBuf+1);
-        len = strlen(value);
-    } else {
-        ALOGE("Got strange response to property_get request (%d)\n",
-            recvBuf[0]);
-        assert(0);
-        return -1;
-    }
-    //ALOGV("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n",
-    //    recvBuf[0], default_value, len, key, value);
-
-    return len;
-}
-
-
-int property_set(const char *key, const char *value)
-{
-    char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX];
-    char recvBuf[1];
-    int result = -1;
-
-    //ALOGV("PROPERTY SET [%s]: [%s]\n", key, value);
-
-    pthread_once(&gInitOnce, init);
-    if (gPropFd < 0)
-        return -1;
-
-    if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
-    if (strlen(value) >= PROPERTY_VALUE_MAX) return -1;
-
-    memset(sendBuf, 0xdd, sizeof(sendBuf));    // placate valgrind
-
-    sendBuf[0] = (char) kSystemPropertySet;
-    strcpy(sendBuf+1, key);
-    strcpy(sendBuf+1+PROPERTY_KEY_MAX, value);
-
-    pthread_mutex_lock(&gPropertyFdLock);
-    if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
-        pthread_mutex_unlock(&gPropertyFdLock);
-        return -1;
-    }
-    if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
-        pthread_mutex_unlock(&gPropertyFdLock);
-        return -1;
-    }
-    pthread_mutex_unlock(&gPropertyFdLock);
-
-    if (recvBuf[0] != 1)
-        return -1;
-    return 0;
-}
-
-int property_list(void (*propfn)(const char *key, const char *value, void *cookie), 
-                  void *cookie)
-{
-    //ALOGV("PROPERTY LIST\n");
-    pthread_once(&gInitOnce, init);
-    if (gPropFd < 0)
-        return -1;
-
-    return 0;
-}
-
 #else
 
-/* SUPER-cheesy place-holder implementation for Win32 */
+/* SUPER-cheesy place-holder implementation for glibc/Mac OS/Windows. */
 
 #include <cutils/threads.h>
 
diff --git a/libcutils/sched_policy.c b/libcutils/sched_policy.c
index 493511e..dfc8777 100644
--- a/libcutils/sched_policy.c
+++ b/libcutils/sched_policy.c
@@ -37,7 +37,7 @@
    return p == SP_DEFAULT ? SP_SYSTEM_DEFAULT : p;
 }
 
-#if defined(HAVE_ANDROID_OS) && defined(HAVE_SCHED_H) && defined(HAVE_PTHREADS)
+#if defined(HAVE_ANDROID_OS)
 
 #include <pthread.h>
 #include <sched.h>
@@ -203,11 +203,9 @@
 
 int get_sched_policy(int tid, SchedPolicy *policy)
 {
-#ifdef HAVE_GETTID
     if (tid == 0) {
         tid = gettid();
     }
-#endif
     pthread_once(&the_once, __initialize);
 
     if (__sys_supports_schedgroups) {
@@ -240,11 +238,9 @@
 
 int set_sched_policy(int tid, SchedPolicy policy)
 {
-#ifdef HAVE_GETTID
     if (tid == 0) {
         tid = gettid();
     }
-#endif
     policy = _policy(policy);
     pthread_once(&the_once, __initialize);
 
diff --git a/libcutils/socket_local_client.c b/libcutils/socket_local_client.c
index ddcc2da..7b42daa 100644
--- a/libcutils/socket_local_client.c
+++ b/libcutils/socket_local_client.c
@@ -52,7 +52,7 @@
 
     switch (namespaceId) {
         case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
-#ifdef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
+#if defined(__linux__)
             namelen  = strlen(name);
 
             // Test with length +1 for the *initial* '\0'.
@@ -67,7 +67,7 @@
             
             p_addr->sun_path[0] = 0;
             memcpy(p_addr->sun_path + 1, name, namelen);
-#else /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
+#else
             /* this OS doesn't have the Linux abstract namespace */
 
             namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
@@ -79,7 +79,7 @@
 
             strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
             strcat(p_addr->sun_path, name);
-#endif /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
+#endif
         break;
 
         case ANDROID_SOCKET_NAMESPACE_RESERVED:
diff --git a/libcutils/socket_local_server.c b/libcutils/socket_local_server.c
index 7628fe4..60eb86b 100644
--- a/libcutils/socket_local_server.c
+++ b/libcutils/socket_local_server.c
@@ -66,7 +66,7 @@
     }
 
     /* basically: if this is a filesystem path, unlink first */
-#ifndef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
+#if !defined(__linux__)
     if (1) {
 #else
     if (namespaceId == ANDROID_SOCKET_NAMESPACE_RESERVED
diff --git a/libcutils/socket_network_client.c b/libcutils/socket_network_client.c
index 4826033..e0031ba 100644
--- a/libcutils/socket_network_client.c
+++ b/libcutils/socket_network_client.c
@@ -45,7 +45,6 @@
 {
     struct hostent *hp;
     struct sockaddr_in addr;
-    socklen_t alen;
     int s;
     int flags = 0, error = 0, ret = 0;
     fd_set rset, wset;
diff --git a/libcutils/tests/Android.mk b/libcutils/tests/Android.mk
index 8e65310..5a54698 100644
--- a/libcutils/tests/Android.mk
+++ b/libcutils/tests/Android.mk
@@ -19,6 +19,7 @@
     PropertiesTest.cpp \
 
 include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_MODULE := libcutils_test
 LOCAL_SRC_FILES := $(test_src_files)
 LOCAL_SHARED_LIBRARIES := \
@@ -31,18 +32,21 @@
 LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
 include $(BUILD_NATIVE_TEST)
 
-include $(CLEAR_VARS)
-LOCAL_MODULE := libcutils_test_static
-LOCAL_FORCE_STATIC_EXECUTABLE := true
-LOCAL_SRC_FILES := $(test_src_files)
-LOCAL_STATIC_LIBRARIES := \
-    libc \
-    libcutils \
-    liblog \
-    libstlport_static \
-    libutils \
+# The static libcutils tests cannot be built when using libc++ because there are
+# multiple symbol definition errors between libc++ and libgcc. b/18389856
+#include $(CLEAR_VARS)
+#LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+#LOCAL_MODULE := libcutils_test_static
+#LOCAL_FORCE_STATIC_EXECUTABLE := true
+#LOCAL_SRC_FILES := $(test_src_files)
+#LOCAL_STATIC_LIBRARIES := \
+#    libc \
+#    libcutils \
+#    liblog \
+#    libutils \
 
-LOCAL_MULTILIB := both
-LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
-LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
-include $(BUILD_NATIVE_TEST)
+#LOCAL_CXX_STL := stlport_static
+#LOCAL_MULTILIB := both
+#LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
+#LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
+#include $(BUILD_NATIVE_TEST)
diff --git a/libcutils/threads.c b/libcutils/threads.c
index bf182f0..ca600b3 100644
--- a/libcutils/threads.c
+++ b/libcutils/threads.c
@@ -1,22 +1,22 @@
 /*
 ** Copyright (C) 2007, 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 
+** 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 
+**     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 
+** 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 <cutils/threads.h>
 
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
 void*  thread_store_get( thread_store_t*  store )
 {
     if (!store->has_tls)
@@ -24,8 +24,8 @@
 
     return pthread_getspecific( store->tls );
 }
-    
-extern void   thread_store_set( thread_store_t*          store, 
+
+extern void   thread_store_set( thread_store_t*          store,
                                 void*                    value,
                                 thread_store_destruct_t  destroy)
 {
@@ -42,14 +42,12 @@
     pthread_setspecific( store->tls, value );
 }
 
-#endif
-
-#ifdef HAVE_WIN32_THREADS
+#else /* !defined(_WIN32) */
 void*  thread_store_get( thread_store_t*  store )
 {
     if (!store->has_tls)
         return NULL;
-    
+
     return (void*) TlsGetValue( store->tls );
 }
 
@@ -65,7 +63,7 @@
     } else while (store->lock_init != -2) {
         Sleep(10); /* 10ms */
     }
-        
+
     EnterCriticalSection( &store->lock );
     if (!store->has_tls) {
         store->tls = TlsAlloc();
@@ -76,7 +74,7 @@
         store->has_tls = 1;
     }
     LeaveCriticalSection( &store->lock );
-    
+
     TlsSetValue( store->tls, value );
 }
-#endif
+#endif /* !defined(_WIN32) */
diff --git a/libcutils/trace.c b/libcutils/trace.c
index f57aac2..4396625 100644
--- a/libcutils/trace.c
+++ b/libcutils/trace.c
@@ -30,6 +30,13 @@
 #define LOG_TAG "cutils-trace"
 #include <log/log.h>
 
+/**
+ * Maximum size of a message that can be logged to the trace buffer.
+ * Note this message includes a tag, the pid, and the string given as the name.
+ * Names should be kept short to get the most use of the trace buffer.
+ */
+#define ATRACE_MESSAGE_LENGTH 1024
+
 volatile int32_t        atrace_is_ready      = 0;
 int                     atrace_marker_fd     = -1;
 uint64_t                atrace_enabled_tags  = ATRACE_TAG_NOT_READY;
@@ -183,3 +190,53 @@
 {
     pthread_once(&atrace_once_control, atrace_init_once);
 }
+
+void atrace_begin_body(const char* name)
+{
+    char buf[ATRACE_MESSAGE_LENGTH];
+    size_t len;
+
+    len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
+    write(atrace_marker_fd, buf, len);
+}
+
+
+void atrace_async_begin_body(const char* name, int32_t cookie)
+{
+    char buf[ATRACE_MESSAGE_LENGTH];
+    size_t len;
+
+    len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%" PRId32,
+            getpid(), name, cookie);
+    write(atrace_marker_fd, buf, len);
+}
+
+void atrace_async_end_body(const char* name, int32_t cookie)
+{
+    char buf[ATRACE_MESSAGE_LENGTH];
+    size_t len;
+
+    len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%" PRId32,
+            getpid(), name, cookie);
+    write(atrace_marker_fd, buf, len);
+}
+
+void atrace_int_body(const char* name, int32_t value)
+{
+    char buf[ATRACE_MESSAGE_LENGTH];
+    size_t len;
+
+    len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId32,
+            getpid(), name, value);
+    write(atrace_marker_fd, buf, len);
+}
+
+void atrace_int64_body(const char* name, int64_t value)
+{
+    char buf[ATRACE_MESSAGE_LENGTH];
+    size_t len;
+
+    len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%" PRId64,
+            getpid(), name, value);
+    write(atrace_marker_fd, buf, len);
+}
diff --git a/libcutils/uevent.c b/libcutils/uevent.c
index 97a81e3..827170a 100644
--- a/libcutils/uevent.c
+++ b/libcutils/uevent.c
@@ -31,12 +31,12 @@
  */
 ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length)
 {
-    uid_t user = -1;
-    return uevent_kernel_multicast_uid_recv(socket, buffer, length, &user);
+    uid_t uid = -1;
+    return uevent_kernel_multicast_uid_recv(socket, buffer, length, &uid);
 }
 
 /**
- * Like the above, but passes a uid_t in by reference. In the event that this
+ * Like the above, but passes a uid_t in by pointer. In the event that this
  * fails due to a bad uid check, the uid_t will be set to the uid of the
  * socket's peer.
  *
@@ -44,8 +44,12 @@
  * returns -1, sets errno to EIO, and sets "user" to the UID associated with the
  * message. If the peer UID cannot be determined, "user" is set to -1."
  */
-ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer,
-                                         size_t length, uid_t *user)
+ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer, size_t length, uid_t *uid)
+{
+    return uevent_kernel_recv(socket, buffer, length, true, uid);
+}
+
+ssize_t uevent_kernel_recv(int socket, void *buffer, size_t length, bool require_group, uid_t *uid)
 {
     struct iovec iov = { buffer, length };
     struct sockaddr_nl addr;
@@ -60,7 +64,7 @@
         0,
     };
 
-    *user = -1;
+    *uid = -1;
     ssize_t n = recvmsg(socket, &hdr, 0);
     if (n <= 0) {
         return n;
@@ -73,14 +77,18 @@
     }
 
     struct ucred *cred = (struct ucred *)CMSG_DATA(cmsg);
-    *user = cred->uid;
+    *uid = cred->uid;
     if (cred->uid != 0) {
         /* ignoring netlink message from non-root user */
         goto out;
     }
 
-    if (addr.nl_groups == 0 || addr.nl_pid != 0) {
-        /* ignoring non-kernel or unicast netlink message */
+    if (addr.nl_pid != 0) {
+        /* ignore non-kernel */
+        goto out;
+    }
+    if (require_group && addr.nl_groups == 0) {
+        /* ignore unicast messages when requested */
         goto out;
     }
 
diff --git a/libion/ion.c b/libion/ion.c
index 80bdc2a..4908932 100644
--- a/libion/ion.c
+++ b/libion/ion.c
@@ -23,6 +23,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
+#include <string.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/types.h>
@@ -117,7 +118,6 @@
 
 int ion_share(int fd, ion_user_handle_t handle, int *share_fd)
 {
-    int map_fd;
     int ret;
     struct ion_fd_data data = {
         .handle = handle,
diff --git a/libion/ion_test.c b/libion/ion_test.c
index 8872282..b7d5583 100644
--- a/libion/ion_test.c
+++ b/libion/ion_test.c
@@ -164,8 +164,9 @@
         printf("master->master? [%10s]\n", ptr);
         if (recvmsg(sd[0], &msg, 0) < 0)
             perror("master recv 1");
+        close(fd);
+        _exit(0);
     } else {
-        struct msghdr msg;
         struct cmsghdr *cmsg;
         char* ptr;
         int fd, recv_fd;
@@ -205,6 +206,7 @@
         strcpy(ptr, "child");
         printf("child sending msg 2\n");
         sendmsg(sd[1], &child_msg, 0);
+        close(fd);
     }
 }
 
diff --git a/libion/tests/Android.mk b/libion/tests/Android.mk
index 8dc7f9d..abf527a 100644
--- a/libion/tests/Android.mk
+++ b/libion/tests/Android.mk
@@ -21,7 +21,6 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_CFLAGS += -g -Wall -Werror -std=gnu++11 -Wno-missing-field-initializers
 LOCAL_SHARED_LIBRARIES += libion
-LOCAL_STATIC_LIBRARIES += libgtest_main
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/../kernel-headers
 LOCAL_SRC_FILES := \
 	ion_test_fixture.cpp \
diff --git a/liblog/fake_log_device.c b/liblog/fake_log_device.c
index 136792d..cf3dc50 100644
--- a/liblog/fake_log_device.c
+++ b/liblog/fake_log_device.c
@@ -29,7 +29,7 @@
 
 #include <log/logd.h>
 
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
 #include <pthread.h>
 #endif
 
@@ -88,7 +88,7 @@
 } LogState;
 
 
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
 /*
  * Locking.  Since we're emulating a device, we need to be prepared
  * to have multiple callers at the same time.  This lock is used
@@ -106,10 +106,10 @@
 {
     pthread_mutex_unlock(&fakeLogDeviceLock);
 }
-#else   // !HAVE_PTHREADS
+#else   // !defined(_WIN32)
 #define lock() ((void)0)
 #define unlock() ((void)0)
-#endif  // !HAVE_PTHREADS
+#endif  // !defined(_WIN32)
 
 
 /*
@@ -320,9 +320,9 @@
     return priorityStrings[idx];
 }
 
-#ifndef HAVE_WRITEV
+#if defined(_WIN32)
 /*
- * Some platforms like WIN32 do not have writev().
+ * WIN32 does not have writev().
  * Make up something to replace it.
  */
 static ssize_t fake_writev(int fd, const struct iovec *iov, int iovcnt) {
@@ -352,7 +352,7 @@
 static void showLog(LogState *state,
         int logPrio, const char* tag, const char* msg)
 {
-#if defined(HAVE_LOCALTIME_R)
+#if !defined(_WIN32)
     struct tm tmBuf;
 #endif
     struct tm* ptm;
@@ -377,7 +377,7 @@
      * in the time stamp.  Don't use forward slashes, parenthesis,
      * brackets, asterisks, or other special chars here.
      */
-#if defined(HAVE_LOCALTIME_R)
+#if !defined(_WIN32)
     ptm = localtime_r(&when, &tmBuf);
 #else
     ptm = localtime(&when);
diff --git a/liblog/log_read.c b/liblog/log_read.c
index ca5a1a7..2f21a5d 100644
--- a/liblog/log_read.c
+++ b/liblog/log_read.c
@@ -72,7 +72,7 @@
 
     switch (namespaceId) {
     case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
-#ifdef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
+#if defined(__linux__)
         namelen  = strlen(name);
 
         /* Test with length +1 for the *initial* '\0'. */
@@ -87,7 +87,7 @@
 
         p_addr->sun_path[0] = 0;
         memcpy(p_addr->sun_path + 1, name, namelen);
-#else /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
+#else
         /* this OS doesn't have the Linux abstract namespace */
 
         namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
@@ -99,7 +99,7 @@
 
         strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
         strcat(p_addr->sun_path, name);
-#endif /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/
+#endif
         break;
 
     case ANDROID_SOCKET_NAMESPACE_RESERVED:
diff --git a/liblog/log_time.cpp b/liblog/log_time.cpp
index 755c2d9..50742df 100644
--- a/liblog/log_time.cpp
+++ b/liblog/log_time.cpp
@@ -39,7 +39,7 @@
 #endif
 
     struct tm *ptm;
-#if (defined(HAVE_LOCALTIME_R))
+#if !defined(_WIN32)
     struct tm tmBuf;
     ptm = localtime_r(&now, &tmBuf);
 #else
@@ -78,7 +78,7 @@
                 ++ret;
             }
             now = tv_sec;
-#if (defined(HAVE_LOCALTIME_R))
+#if !defined(_WIN32)
             ptm = localtime_r(&now, &tmBuf);
 #else
             ptm = localtime(&now);
@@ -150,6 +150,17 @@
     return *this;
 }
 
+log_time log_time::operator+= (const timespec &T) {
+    this->tv_nsec += (unsigned long int)T.tv_nsec;
+    if (this->tv_nsec >= NS_PER_SEC) {
+        this->tv_nsec -= NS_PER_SEC;
+        ++this->tv_sec;
+    }
+    this->tv_sec += T.tv_sec;
+
+    return *this;
+}
+
 log_time log_time::operator-= (const log_time &T) {
     // No concept of negative time, clamp to EPOCH
     if (*this <= T) {
@@ -166,3 +177,14 @@
 
     return *this;
 }
+
+log_time log_time::operator+= (const log_time &T) {
+    this->tv_nsec += T.tv_nsec;
+    if (this->tv_nsec >= NS_PER_SEC) {
+        this->tv_nsec -= NS_PER_SEC;
+        ++this->tv_sec;
+    }
+    this->tv_sec += T.tv_sec;
+
+    return *this;
+}
diff --git a/liblog/logd_write.c b/liblog/logd_write.c
index b2668ce..aaec619 100644
--- a/liblog/logd_write.c
+++ b/liblog/logd_write.c
@@ -15,7 +15,7 @@
  */
 #include <errno.h>
 #include <fcntl.h>
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
 #include <pthread.h>
 #endif
 #include <stdarg.h>
@@ -39,6 +39,7 @@
 #include <log/logger.h>
 #include <log/log_read.h>
 #include <private/android_filesystem_config.h>
+#include <private/android_logger.h>
 
 #define LOG_BUF_SIZE 1024
 
@@ -49,7 +50,7 @@
 
 static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
 static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
 #endif
 
@@ -61,6 +62,7 @@
 static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
 #else
 static int logd_fd = -1;
+static int pstore_fd = -1;
 #endif
 
 /*
@@ -109,6 +111,12 @@
         logd_fd = -1;
         close(i);
     }
+    if (pstore_fd >= 0) {
+        i = pstore_fd;
+        pstore_fd = -1;
+        close(i);
+    }
+    pstore_fd = open("/dev/pmsg0", O_WRONLY);
 
     i = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
     if (i < 0) {
@@ -155,37 +163,27 @@
         }
     } while (ret == -EINTR);
 #else
-    static const unsigned header_length = 3;
+    static const unsigned header_length = 2;
     struct iovec newVec[nr + header_length];
-    typeof_log_id_t log_id_buf;
-    uint16_t tid;
+    android_log_header_t header;
+    android_pmsg_log_header_t pmsg_header;
     struct timespec ts;
-    log_time realtime_ts;
     size_t i, payload_size;
     static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
+    static pid_t last_pid = (pid_t) -1;
 
     if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
         last_uid = getuid();
     }
-    if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
-        /*
-         * ignore log messages we send to ourself (logd).
-         * Such log messages are often generated by libraries we depend on
-         * which use standard Android logging.
-         */
-        return 0;
+    if (last_pid == (pid_t) -1) {
+        last_pid = getpid();
     }
-
-    if (logd_fd < 0) {
-        return -EBADF;
-    }
-
     /*
      *  struct {
-     *      // what we provide
-     *      typeof_log_id_t  log_id;
-     *      u16              tid;
-     *      log_time         realtime;
+     *      // whate we provire to pstore
+     *      android_pmsg_log_header_t pmsg_header;
+     *      // what we provide to socket
+     *      android_log_header_t header;
      *      // caller provides
      *      union {
      *          struct {
@@ -201,18 +199,21 @@
      */
 
     clock_gettime(CLOCK_REALTIME, &ts);
-    realtime_ts.tv_sec = ts.tv_sec;
-    realtime_ts.tv_nsec = ts.tv_nsec;
 
-    log_id_buf = log_id;
-    tid = gettid();
+    pmsg_header.magic = LOGGER_MAGIC;
+    pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
+    pmsg_header.uid = last_uid;
+    pmsg_header.pid = last_pid;
 
-    newVec[0].iov_base   = (unsigned char *) &log_id_buf;
-    newVec[0].iov_len    = sizeof_log_id_t;
-    newVec[1].iov_base   = (unsigned char *) &tid;
-    newVec[1].iov_len    = sizeof(tid);
-    newVec[2].iov_base   = (unsigned char *) &realtime_ts;
-    newVec[2].iov_len    = sizeof(log_time);
+    header.id = log_id;
+    header.tid = gettid();
+    header.realtime.tv_sec = ts.tv_sec;
+    header.realtime.tv_nsec = ts.tv_nsec;
+
+    newVec[0].iov_base   = (unsigned char *) &pmsg_header;
+    newVec[0].iov_len    = sizeof(pmsg_header);
+    newVec[1].iov_base   = (unsigned char *) &header;
+    newVec[1].iov_len    = sizeof(header);
 
     for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
         newVec[i].iov_base = vec[i - header_length].iov_base;
@@ -223,25 +224,46 @@
             if (newVec[i].iov_len) {
                 ++i;
             }
+            payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
             break;
         }
     }
+    pmsg_header.len += payload_size;
+
+    if (pstore_fd >= 0) {
+        TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
+    }
+
+    if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
+        /*
+         * ignore log messages we send to ourself (logd).
+         * Such log messages are often generated by libraries we depend on
+         * which use standard Android logging.
+         */
+        return 0;
+    }
+
+    if (logd_fd < 0) {
+        return -EBADF;
+    }
 
     /*
      * The write below could be lost, but will never block.
      *
+     * To logd, we drop the pmsg_header
+     *
      * ENOTCONN occurs if logd dies.
      * EAGAIN occurs if logd is overloaded.
      */
-    ret = writev(logd_fd, newVec, i);
+    ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
     if (ret < 0) {
         ret = -errno;
         if (ret == -ENOTCONN) {
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
             pthread_mutex_lock(&log_init_lock);
 #endif
             ret = __write_to_log_initialize();
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
             pthread_mutex_unlock(&log_init_lock);
 #endif
 
@@ -249,15 +271,15 @@
                 return ret;
             }
 
-            ret = writev(logd_fd, newVec, nr + header_length);
+            ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
             if (ret < 0) {
                 ret = -errno;
             }
         }
     }
 
-    if (ret > (ssize_t)(sizeof_log_id_t + sizeof(tid) + sizeof(log_time))) {
-        ret -= sizeof_log_id_t + sizeof(tid) + sizeof(log_time);
+    if (ret > (ssize_t)sizeof(header)) {
+        ret -= sizeof(header);
     }
 #endif
 
@@ -284,7 +306,7 @@
 
 static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
 {
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
     pthread_mutex_lock(&log_init_lock);
 #endif
 
@@ -293,7 +315,7 @@
 
         ret = __write_to_log_initialize();
         if (ret < 0) {
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
             pthread_mutex_unlock(&log_init_lock);
 #endif
             return ret;
@@ -302,7 +324,7 @@
         write_to_log = __write_to_log_kernel;
     }
 
-#ifdef HAVE_PTHREADS
+#if !defined(_WIN32)
     pthread_mutex_unlock(&log_init_lock);
 #endif
 
diff --git a/liblog/logd_write_kern.c b/liblog/logd_write_kern.c
index ae621cb..2ca3c94 100644
--- a/liblog/logd_write_kern.c
+++ b/liblog/logd_write_kern.c
@@ -16,9 +16,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
-#ifdef HAVE_PTHREADS
 #include <pthread.h>
-#endif
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -28,9 +26,7 @@
 #include <time.h>
 #include <unistd.h>
 
-#ifdef __BIONIC__
 #include <android/set_abort_message.h>
-#endif
 
 #include <log/log.h>
 #include <log/logd.h>
@@ -43,23 +39,14 @@
 
 #define LOG_BUF_SIZE 1024
 
-#if FAKE_LOG_DEVICE
-/* This will be defined when building for the host. */
-#include "fake_log_device.h"
-#define log_open(pathname, flags) fakeLogOpen(pathname, flags)
-#define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count)
-#define log_close(filedes) fakeLogClose(filedes)
-#else
 #define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
 #define log_writev(filedes, vector, count) writev(filedes, vector, count)
 #define log_close(filedes) close(filedes)
-#endif
 
 static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
 static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
-#ifdef HAVE_PTHREADS
+
 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
-#endif
 
 #ifndef __unused
 #define __unused  __attribute__((__unused__))
@@ -119,9 +106,7 @@
 
 static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
 {
-#ifdef HAVE_PTHREADS
     pthread_mutex_lock(&log_init_lock);
-#endif
 
     if (write_to_log == __write_to_log_init) {
         log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
@@ -147,9 +132,7 @@
         }
     }
 
-#ifdef HAVE_PTHREADS
     pthread_mutex_unlock(&log_init_lock);
-#endif
 
     return write_to_log(log_id, vec, nr);
 }
@@ -179,11 +162,9 @@
             tag = tmp_tag;
     }
 
-#if __BIONIC__
     if (prio == ANDROID_LOG_FATAL) {
         android_set_abort_message(msg);
     }
-#endif
 
     vec[0].iov_base   = (unsigned char *) &prio;
     vec[0].iov_len    = 1;
diff --git a/liblog/logprint.c b/liblog/logprint.c
index 08e830a..7ba4c8e 100644
--- a/liblog/logprint.c
+++ b/liblog/logprint.c
@@ -21,10 +21,12 @@
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/param.h>
 
 #include <log/logd.h>
 #include <log/logprint.h>
@@ -39,8 +41,23 @@
     android_LogPriority global_pri;
     FilterInfo *filters;
     AndroidLogPrintFormat format;
+    bool colored_output;
 };
 
+/*
+ *  gnome-terminal color tags
+ *    See http://misc.flogisoft.com/bash/tip_colors_and_formatting
+ *    for ideas on how to set the forground color of the text for xterm.
+ *    The color manipulation character stream is defined as:
+ *      ESC [ 3 8 ; 5 ; <color#> m
+ */
+#define ANDROID_COLOR_BLUE     75
+#define ANDROID_COLOR_DEFAULT 231
+#define ANDROID_COLOR_GREEN    40
+#define ANDROID_COLOR_ORANGE  166
+#define ANDROID_COLOR_RED     196
+#define ANDROID_COLOR_YELLOW  226
+
 static FilterInfo * filterinfo_new(const char * tag, android_LogPriority pri)
 {
     FilterInfo *p_ret;
@@ -110,6 +127,23 @@
     }
 }
 
+static int colorFromPri (android_LogPriority pri)
+{
+    switch (pri) {
+        case ANDROID_LOG_VERBOSE:       return ANDROID_COLOR_DEFAULT;
+        case ANDROID_LOG_DEBUG:         return ANDROID_COLOR_BLUE;
+        case ANDROID_LOG_INFO:          return ANDROID_COLOR_GREEN;
+        case ANDROID_LOG_WARN:          return ANDROID_COLOR_ORANGE;
+        case ANDROID_LOG_ERROR:         return ANDROID_COLOR_RED;
+        case ANDROID_LOG_FATAL:         return ANDROID_COLOR_RED;
+        case ANDROID_LOG_SILENT:        return ANDROID_COLOR_DEFAULT;
+
+        case ANDROID_LOG_DEFAULT:
+        case ANDROID_LOG_UNKNOWN:
+        default:                        return ANDROID_COLOR_DEFAULT;
+    }
+}
+
 static android_LogPriority filterPriForTag(
         AndroidLogFormat *p_format, const char *tag)
 {
@@ -149,6 +183,7 @@
 
     p_ret->global_pri = ANDROID_LOG_VERBOSE;
     p_ret->format = FORMAT_BRIEF;
+    p_ret->colored_output = false;
 
     return p_ret;
 }
@@ -174,7 +209,10 @@
 void android_log_setPrintFormat(AndroidLogFormat *p_format,
         AndroidLogPrintFormat format)
 {
-    p_format->format=format;
+    if (format == FORMAT_COLOR)
+        p_format->colored_output = true;
+    else
+        p_format->format = format;
 }
 
 /**
@@ -192,6 +230,7 @@
     else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME;
     else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME;
     else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG;
+    else if (strcmp(formatString, "color") == 0) format = FORMAT_COLOR;
     else format = FORMAT_OFF;
 
     return format;
@@ -305,15 +344,6 @@
     return -1;
 }
 
-static inline char * strip_end(char *str)
-{
-    char *end = str + strlen(str) - 1;
-
-    while (end >= str && isspace(*end))
-        *end-- = '\0';
-    return str;
-}
-
 /**
  * Splits a wire-format buffer into an AndroidLogEntry
  * entry allocated by caller. Pointers will point directly into buf
@@ -687,7 +717,7 @@
     const AndroidLogEntry *entry,
     size_t *p_outLength)
 {
-#if defined(HAVE_LOCALTIME_R)
+#if !defined(_WIN32)
     struct tm tmBuf;
 #endif
     struct tm* ptm;
@@ -698,6 +728,8 @@
     char * ret = NULL;
 
     priChar = filterPriToChar(entry->priority);
+    size_t prefixLen = 0, suffixLen = 0;
+    size_t len;
 
     /*
      * Get the current date/time in pretty form
@@ -708,7 +740,7 @@
      * in the time stamp.  Don't use forward slashes, parenthesis,
      * brackets, asterisks, or other special chars here.
      */
-#if defined(HAVE_LOCALTIME_R)
+#if !defined(_WIN32)
     ptm = localtime_r(&(entry->tv_sec), &tmBuf);
 #else
     ptm = localtime(&(entry->tv_sec));
@@ -719,73 +751,80 @@
     /*
      * Construct a buffer containing the log header and log message.
      */
-    size_t prefixLen, suffixLen;
+    if (p_format->colored_output) {
+        prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
+                             colorFromPri(entry->priority));
+        prefixLen = MIN(prefixLen, sizeof(prefixBuf));
+        suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m");
+        suffixLen = MIN(suffixLen, sizeof(suffixBuf));
+    }
 
     switch (p_format->format) {
         case FORMAT_TAG:
-            prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+            len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
                 "%c/%-8s: ", priChar, entry->tag);
-            strcpy(suffixBuf, "\n"); suffixLen = 1;
+            strcpy(suffixBuf + suffixLen, "\n");
+            ++suffixLen;
             break;
         case FORMAT_PROCESS:
-            prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
-                "%c(%5d) ", priChar, entry->pid);
-            suffixLen = snprintf(suffixBuf, sizeof(suffixBuf),
+            len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen,
                 "  (%s)\n", entry->tag);
+            suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
+            len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
+                "%c(%5d) ", priChar, entry->pid);
             break;
         case FORMAT_THREAD:
-            prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+            len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
                 "%c(%5d:%5d) ", priChar, entry->pid, entry->tid);
-            strcpy(suffixBuf, "\n");
-            suffixLen = 1;
+            strcpy(suffixBuf + suffixLen, "\n");
+            ++suffixLen;
             break;
         case FORMAT_RAW:
-            prefixBuf[0] = 0;
-            prefixLen = 0;
-            strcpy(suffixBuf, "\n");
-            suffixLen = 1;
+            prefixBuf[prefixLen] = 0;
+            len = 0;
+            strcpy(suffixBuf + suffixLen, "\n");
+            ++suffixLen;
             break;
         case FORMAT_TIME:
-            prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+            len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
                 "%s.%03ld %c/%-8s(%5d): ", timeBuf, entry->tv_nsec / 1000000,
                 priChar, entry->tag, entry->pid);
-            strcpy(suffixBuf, "\n");
-            suffixLen = 1;
+            strcpy(suffixBuf + suffixLen, "\n");
+            ++suffixLen;
             break;
         case FORMAT_THREADTIME:
-            prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+            len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
                 "%s.%03ld %5d %5d %c %-8s: ", timeBuf, entry->tv_nsec / 1000000,
                 entry->pid, entry->tid, priChar, entry->tag);
-            strcpy(suffixBuf, "\n");
-            suffixLen = 1;
+            strcpy(suffixBuf + suffixLen, "\n");
+            ++suffixLen;
             break;
         case FORMAT_LONG:
-            prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+            len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
                 "[ %s.%03ld %5d:%5d %c/%-8s ]\n",
                 timeBuf, entry->tv_nsec / 1000000, entry->pid,
                 entry->tid, priChar, entry->tag);
-            strcpy(suffixBuf, "\n\n");
-            suffixLen = 2;
+            strcpy(suffixBuf + suffixLen, "\n\n");
+            suffixLen += 2;
             prefixSuffixIsHeaderFooter = 1;
             break;
         case FORMAT_BRIEF:
         default:
-            prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),
+            len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
                 "%c/%-8s(%5d): ", priChar, entry->tag, entry->pid);
-            strcpy(suffixBuf, "\n");
-            suffixLen = 1;
+            strcpy(suffixBuf + suffixLen, "\n");
+            ++suffixLen;
             break;
     }
+
     /* snprintf has a weird return value.   It returns what would have been
      * written given a large enough buffer.  In the case that the prefix is
      * longer then our buffer(128), it messes up the calculations below
      * possibly causing heap corruption.  To avoid this we double check and
      * set the length at the maximum (size minus null byte)
      */
-    if(prefixLen >= sizeof(prefixBuf))
-        prefixLen = sizeof(prefixBuf) - 1;
-    if(suffixLen >= sizeof(suffixBuf))
-        suffixLen = sizeof(suffixBuf) - 1;
+    prefixLen += MIN(len, sizeof(prefixBuf) - prefixLen);
+    suffixLen = MIN(suffixLen, sizeof(suffixBuf));
 
     /* the following code is tragically unreadable */
 
diff --git a/liblog/tests/Android.mk b/liblog/tests/Android.mk
index cd1bf33..172b186 100644
--- a/liblog/tests/Android.mk
+++ b/liblog/tests/Android.mk
@@ -43,10 +43,6 @@
 LOCAL_CFLAGS += $(benchmark_c_flags)
 LOCAL_SHARED_LIBRARIES += liblog libm
 LOCAL_SRC_FILES := $(benchmark_src_files)
-ifndef LOCAL_SDK_VERSION
-LOCAL_C_INCLUDES += bionic bionic/libstdc++/include external/stlport/stlport
-LOCAL_SHARED_LIBRARIES += libstlport
-endif
 LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_NATIVE_TESTS)/$(LOCAL_MODULE)
 include $(BUILD_EXECUTABLE)
 
@@ -71,7 +67,7 @@
 test_src_files += \
     libc_test.cpp
 
-ifndef ($(TARGET_USES_LOGD),false)
+ifneq ($(TARGET_USES_LOGD),false)
 test_c_flags += -DTARGET_USES_LOGD
 endif
 
diff --git a/liblog/tests/benchmark_main.cpp b/liblog/tests/benchmark_main.cpp
index 090394c..e5ef970 100644
--- a/liblog/tests/benchmark_main.cpp
+++ b/liblog/tests/benchmark_main.cpp
@@ -17,6 +17,7 @@
 #include <benchmark.h>
 
 #include <inttypes.h>
+#include <math.h>
 #include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/liblog/uio.c b/liblog/uio.c
index 24a6507..f77cc49 100644
--- a/liblog/uio.c
+++ b/liblog/uio.c
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#ifndef HAVE_SYS_UIO_H
+#if defined(_WIN32)
 
 #include <log/uio.h>
 #include <unistd.h>
@@ -73,4 +73,4 @@
     return total;
 }
 
-#endif /* !HAVE_SYS_UIO_H */
+#endif
diff --git a/libmemtrack/memtrack.c b/libmemtrack/memtrack.c
index 9a656df..21d9ebd 100644
--- a/libmemtrack/memtrack.c
+++ b/libmemtrack/memtrack.c
@@ -20,6 +20,10 @@
 
 #include <log/log.h>
 
+#include <errno.h>
+#include <malloc.h>
+#include <string.h>
+
 #include <hardware/memtrack.h>
 
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
diff --git a/libmincrypt/Android.mk b/libmincrypt/Android.mk
index 7906986..503bcb4 100644
--- a/libmincrypt/Android.mk
+++ b/libmincrypt/Android.mk
@@ -6,6 +6,8 @@
 LOCAL_MODULE := libmincrypt
 LOCAL_SRC_FILES := dsa_sig.c p256.c p256_ec.c p256_ecdsa.c rsa.c sha.c sha256.c
 LOCAL_CFLAGS := -Wall -Werror
+# Clang's slp-vectorize phase has segmentation fault when compiling p256_ec.c.
+LOCAL_CLANG_CFLAGS += -fno-slp-vectorize
 include $(BUILD_STATIC_LIBRARY)
 
 include $(CLEAR_VARS)
diff --git a/libnativebridge/tests/CompleteFlow_test.cpp b/libnativebridge/tests/CompleteFlow_test.cpp
index cf06d2c..b033792 100644
--- a/libnativebridge/tests/CompleteFlow_test.cpp
+++ b/libnativebridge/tests/CompleteFlow_test.cpp
@@ -36,6 +36,7 @@
 
     // Unload
     UnloadNativeBridge();
+
     ASSERT_FALSE(NativeBridgeAvailable());
     ASSERT_FALSE(NativeBridgeError());
 
diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c
index e1df874..0f7c384 100644
--- a/libnetutils/dhcp_utils.c
+++ b/libnetutils/dhcp_utils.c
@@ -166,14 +166,6 @@
     return 0;
 }
 
-static const char *ipaddr_to_string(in_addr_t addr)
-{
-    struct in_addr in_addr;
-
-    in_addr.s_addr = addr;
-    return inet_ntoa(in_addr);
-}
-
 /*
  * Start the dhcp client daemon, and wait for it to finish
  * configuring the interface.
@@ -242,7 +234,6 @@
         return -1;
     }
     if (strcmp(prop_value, "ok") == 0) {
-        char dns_prop_name[PROPERTY_KEY_MAX];
         if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns,
                 server, lease, vendorInfo, domain, mtu) == -1) {
             return -1;
diff --git a/libnetutils/dhcpclient.c b/libnetutils/dhcpclient.c
index b58120e..a05b7cb 100644
--- a/libnetutils/dhcpclient.c
+++ b/libnetutils/dhcpclient.c
@@ -150,7 +150,7 @@
 
 void dump_dhcp_info(dhcp_info *info)
 {
-    char addr[20], gway[20], mask[20];
+    char addr[20], gway[20];
     ALOGD("--- dhcp %s (%d) ---",
             dhcp_type_to_name(info->type), info->type);
     strcpy(addr, ipaddr(info->ipaddr));
@@ -353,28 +353,28 @@
 static int is_valid_reply(dhcp_msg *msg, dhcp_msg *reply, int sz)
 {
     if (sz < DHCP_MSG_FIXED_SIZE) {
-        if (verbose) ALOGD("netcfg: Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE);
+        if (verbose) ALOGD("Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE);
         return 0;
     }
     if (reply->op != OP_BOOTREPLY) {
-        if (verbose) ALOGD("netcfg: Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY);
+        if (verbose) ALOGD("Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY);
         return 0;
     }
     if (reply->xid != msg->xid) {
-        if (verbose) ALOGD("netcfg: Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid),
-                          ntohl(msg->xid));
+        if (verbose) ALOGD("Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid),
+                           ntohl(msg->xid));
         return 0;
     }
     if (reply->htype != msg->htype) {
-        if (verbose) ALOGD("netcfg: Wrong Htype %d != %d\n", reply->htype, msg->htype);
+        if (verbose) ALOGD("Wrong Htype %d != %d\n", reply->htype, msg->htype);
         return 0;
     }
     if (reply->hlen != msg->hlen) {
-        if (verbose) ALOGD("netcfg: Wrong Hlen %d != %d\n", reply->hlen, msg->hlen);
+        if (verbose) ALOGD("Wrong Hlen %d != %d\n", reply->hlen, msg->hlen);
         return 0;
     }
     if (memcmp(msg->chaddr, reply->chaddr, msg->hlen)) {
-        if (verbose) ALOGD("netcfg: Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr));
+        if (verbose) ALOGD("Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr));
         return 0;
     }
     return 1;
diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c
index 913f51e..bfe7121 100644
--- a/libnetutils/ifc_utils.c
+++ b/libnetutils/ifc_utils.c
@@ -421,7 +421,6 @@
 
 int ifc_set_hwaddr(const char *name, const void *ptr)
 {
-    int r;
     struct ifreq ifr;
     ifc_init_ifr(name, &ifr);
 
diff --git a/libnetutils/packet.c b/libnetutils/packet.c
index 3cdefb0..cd26d05 100644
--- a/libnetutils/packet.c
+++ b/libnetutils/packet.c
@@ -15,6 +15,7 @@
  */
 
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <sys/uio.h>
 #include <sys/socket.h>
@@ -41,7 +42,7 @@
 
 int open_raw_socket(const char *ifname __attribute__((unused)), uint8_t *hwaddr, int if_index)
 {
-    int s, flag;
+    int s;
     struct sockaddr_ll bindaddr;
 
     if((s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
diff --git a/libpixelflinger/Android.mk b/libpixelflinger/Android.mk
index 484cf50..acd18b0 100644
--- a/libpixelflinger/Android.mk
+++ b/libpixelflinger/Android.mk
@@ -36,6 +36,7 @@
 
 ifeq ($(ARCH_ARM_HAVE_NEON),true)
 PIXELFLINGER_SRC_FILES_arm += col32cb16blend_neon.S
+PIXELFLINGER_CFLAGS_arm += -D__ARM_HAVE_NEON
 endif
 
 PIXELFLINGER_SRC_FILES_arm64 := \
@@ -44,11 +45,13 @@
 	arch-arm64/col32cb16blend.S \
 	arch-arm64/t32cb16blend.S \
 
+ifndef ARCH_MIPS_REV6
 PIXELFLINGER_SRC_FILES_mips := \
 	codeflinger/MIPSAssembler.cpp \
 	codeflinger/mips_disassem.c \
 	arch-mips/t32cb16blend.S \
 
+endif
 #
 # Shared library
 #
@@ -61,12 +64,14 @@
 LOCAL_CFLAGS := $(PIXELFLINGER_CFLAGS)
 LOCAL_SHARED_LIBRARIES := libcutils liblog
 
-ifneq ($(BUILD_TINY_ANDROID),true)
 # Really this should go away entirely or at least not depend on
 # libhardware, but this at least gets us built.
 LOCAL_SHARED_LIBRARIES += libhardware_legacy
 LOCAL_CFLAGS += -DWITH_LIB_HARDWARE
-endif
+# t32cb16blend.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
+# arch-arm64/col32cb16blend.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
 include $(BUILD_SHARED_LIBRARY)
 
 #
@@ -80,6 +85,10 @@
 LOCAL_SRC_FILES_arm64 := $(PIXELFLINGER_SRC_FILES_arm64)
 LOCAL_SRC_FILES_mips := $(PIXELFLINGER_SRC_FILES_mips)
 LOCAL_CFLAGS := $(PIXELFLINGER_CFLAGS)
+# t32cb16blend.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
+# arch-arm64/col32cb16blend.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
 include $(BUILD_STATIC_LIBRARY)
 
 
diff --git a/libpixelflinger/codeflinger/CodeCache.cpp b/libpixelflinger/codeflinger/CodeCache.cpp
index cfd2b37..d770302 100644
--- a/libpixelflinger/codeflinger/CodeCache.cpp
+++ b/libpixelflinger/codeflinger/CodeCache.cpp
@@ -201,8 +201,8 @@
         mCacheInUse += assemblySize;
         mWhen++;
         // synchronize caches...
-        void* base = assembly->base();
-        void* curr = (uint8_t*)base + assembly->size();
+        char* base = reinterpret_cast<char*>(assembly->base());
+        char* curr = reinterpret_cast<char*>(base + assembly->size());
         __builtin___clear_cache(base, curr);
     }
 
diff --git a/libpixelflinger/codeflinger/load_store.cpp b/libpixelflinger/codeflinger/load_store.cpp
index 0a46eaa..e5a1ae0 100644
--- a/libpixelflinger/codeflinger/load_store.cpp
+++ b/libpixelflinger/codeflinger/load_store.cpp
@@ -20,10 +20,6 @@
 #include <cutils/log.h>
 #include "GGLAssembler.h"
 
-#ifdef __ARM_ARCH__
-#include <machine/cpu-features.h>
-#endif
-
 namespace android {
 
 // ----------------------------------------------------------------------------
@@ -117,20 +113,6 @@
 #endif
     assert(h);
     
-#if __ARM_ARCH__ >= 7
-    const int mask = (1<<maskLen)-1;
-    if ((h == bits) && !l && (s != d.reg)) {
-        MOV(AL, 0, d.reg, s);                   // component = packed;
-    } else if ((h == bits) && l) {
-        MOV(AL, 0, d.reg, reg_imm(s, LSR, l));  // component = packed >> l;
-    } else if (!l && isValidImmediate(mask)) {
-        AND(AL, 0, d.reg, s, imm(mask));        // component = packed & mask;
-    } else if (!l && isValidImmediate(~mask)) {
-        BIC(AL, 0, d.reg, s, imm(~mask));       // component = packed & mask;
-    } else {
-        UBFX(AL, d.reg, s, l, maskLen);         // component = (packed & mask) >> l;
-    }
-#else
     if (h != bits) {
         const int mask = ((1<<maskLen)-1) << l;
         if (isValidImmediate(mask)) {
@@ -153,7 +135,6 @@
     if (s != d.reg) {
         MOV(AL, 0, d.reg, s);
     }
-#endif
 
     d.s = maskLen;
 }
diff --git a/libpixelflinger/codeflinger/texturing.cpp b/libpixelflinger/codeflinger/texturing.cpp
index 81950bf..29a3742 100644
--- a/libpixelflinger/codeflinger/texturing.cpp
+++ b/libpixelflinger/codeflinger/texturing.cpp
@@ -25,10 +25,6 @@
 
 #include "GGLAssembler.h"
 
-#ifdef __ARM_ARCH__
-#include <machine/cpu-features.h>
-#endif
-
 namespace android {
 
 // ---------------------------------------------------------------------------
@@ -888,106 +884,6 @@
     load(txPtr, texel, 0);
 }
 
-#if __ARM_ARCH__ >= 6
-// ARMv6 version, using UXTB16, and scheduled for Cortex-A8 pipeline
-void GGLAssembler::filter32(
-        const fragment_parts_t& parts,
-        pixel_t& texel, const texture_unit_t& tmu,
-        int U, int V, pointer_t& txPtr,
-        int FRAC_BITS)
-{
-    const int adjust = FRAC_BITS*2 - 8;
-    const int round  = 0;
-    const int prescale = 16 - adjust;
-
-    Scratch scratches(registerFile());
-    
-    int pixel= scratches.obtain();
-    int dh   = scratches.obtain();
-    int u    = scratches.obtain();
-    int k    = scratches.obtain();
-
-    int temp = scratches.obtain();
-    int dl   = scratches.obtain();
-
-    int offsetrt = scratches.obtain();
-    int offsetlb = scratches.obtain();
-
-    int pixellb = offsetlb;
-
-    // RB -> U * V
-    CONTEXT_LOAD(offsetrt, generated_vars.rt);
-    CONTEXT_LOAD(offsetlb, generated_vars.lb);
-    if(!round) {
-        MOV(AL, 0, U, reg_imm(U, LSL, prescale));
-    }
-    ADD(AL, 0, u, offsetrt, offsetlb);
-
-    LDR(AL, pixel, txPtr.reg, reg_scale_pre(u));
-    if (round) {
-        SMULBB(AL, u, U, V);
-        RSB(AL, 0, U, U, imm(1<<FRAC_BITS));
-    } else {
-        SMULWB(AL, u, U, V);
-        RSB(AL, 0, U, U, imm(1<<(FRAC_BITS+prescale)));
-    }
-    UXTB16(AL, temp, pixel, 0);
-    if (round) {
-        ADD(AL, 0, u, u, imm(1<<(adjust-1)));
-        MOV(AL, 0, u, reg_imm(u, LSR, adjust));
-    }
-    LDR(AL, pixellb, txPtr.reg, reg_scale_pre(offsetlb));
-    MUL(AL, 0, dh, temp, u);
-    UXTB16(AL, temp, pixel, 8);
-    MUL(AL, 0, dl, temp, u);
-    RSB(AL, 0, k, u, imm(0x100));
-
-    // LB -> (1-U) * V
-    if (round) {
-        SMULBB(AL, u, U, V);
-    } else {
-        SMULWB(AL, u, U, V);
-    }
-    UXTB16(AL, temp, pixellb, 0);
-    if (round) {
-        ADD(AL, 0, u, u, imm(1<<(adjust-1)));
-        MOV(AL, 0, u, reg_imm(u, LSR, adjust));
-    }
-    MLA(AL, 0, dh, temp, u, dh);    
-    UXTB16(AL, temp, pixellb, 8);
-    MLA(AL, 0, dl, temp, u, dl);
-    SUB(AL, 0, k, k, u);
-
-    // LT -> (1-U)*(1-V)
-    RSB(AL, 0, V, V, imm(1<<FRAC_BITS));
-    LDR(AL, pixel, txPtr.reg);
-    if (round) {
-        SMULBB(AL, u, U, V);
-    } else {
-        SMULWB(AL, u, U, V);
-    }
-    UXTB16(AL, temp, pixel, 0);
-    if (round) {
-        ADD(AL, 0, u, u, imm(1<<(adjust-1)));
-        MOV(AL, 0, u, reg_imm(u, LSR, adjust));
-    }
-    MLA(AL, 0, dh, temp, u, dh);    
-    UXTB16(AL, temp, pixel, 8);
-    MLA(AL, 0, dl, temp, u, dl);
-
-    // RT -> U*(1-V)            
-    LDR(AL, pixel, txPtr.reg, reg_scale_pre(offsetrt));
-    SUB(AL, 0, u, k, u);
-    UXTB16(AL, temp, pixel, 0);
-    MLA(AL, 0, dh, temp, u, dh);    
-    UXTB16(AL, temp, pixel, 8);
-    MLA(AL, 0, dl, temp, u, dl);
-
-    UXTB16(AL, dh, dh, 8);
-    UXTB16(AL, dl, dl, 8);
-    ORR(AL, 0, texel.reg, dh, reg_imm(dl, LSL, 8));
-}
-#else
 void GGLAssembler::filter32(
         const fragment_parts_t& /*parts*/,
         pixel_t& texel, const texture_unit_t& /*tmu*/,
@@ -1075,7 +971,6 @@
     AND(AL, 0, dl, dl, reg_imm(mask, LSL, 8));
     ORR(AL, 0, texel.reg, dh, dl);
 }
-#endif
 
 void GGLAssembler::build_texture_environment(
         component_t& fragment,
diff --git a/libpixelflinger/pixelflinger.cpp b/libpixelflinger/pixelflinger.cpp
index ea5bc8e..fd449b2 100644
--- a/libpixelflinger/pixelflinger.cpp
+++ b/libpixelflinger/pixelflinger.cpp
@@ -727,18 +727,10 @@
 
 int64_t ggl_system_time()
 {
-#if defined(HAVE_POSIX_CLOCKS)
     struct timespec t;
     t.tv_sec = t.tv_nsec = 0;
     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t);
     return int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
-#else
-    // we don't support the clocks here.
-    struct timeval t;
-    t.tv_sec = t.tv_usec = 0;
-    gettimeofday(&t, NULL);
-    return int64_t(t.tv_sec)*1000000000LL + int64_t(t.tv_usec)*1000LL;
-#endif
 }
 
 // ----------------------------------------------------------------------------
diff --git a/libpixelflinger/scanline.cpp b/libpixelflinger/scanline.cpp
index 26b9a3e..3d14531 100644
--- a/libpixelflinger/scanline.cpp
+++ b/libpixelflinger/scanline.cpp
@@ -39,7 +39,7 @@
 #include "codeflinger/ARMAssembler.h"
 #elif defined(__aarch64__)
 #include "codeflinger/Arm64Assembler.h"
-#elif defined(__mips__) && !defined(__LP64__)
+#elif defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
 #include "codeflinger/MIPSAssembler.h"
 #endif
 //#include "codeflinger/ARMAssemblerOptimizer.h"
@@ -59,7 +59,7 @@
 #   define ANDROID_CODEGEN      ANDROID_CODEGEN_GENERATED
 #endif
 
-#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__)) || defined(__aarch64__)
+#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6) || defined(__aarch64__)
 #   define ANDROID_ARM_CODEGEN  1
 #else
 #   define ANDROID_ARM_CODEGEN  0
@@ -73,7 +73,7 @@
  */
 #define DEBUG_NEEDS  0
 
-#if defined( __mips__) && !defined(__LP64__)
+#if defined( __mips__) && !defined(__LP64__) && __mips_isa_rev < 6
 #define ASSEMBLY_SCRATCH_SIZE   4096
 #elif defined(__aarch64__)
 #define ASSEMBLY_SCRATCH_SIZE   8192
@@ -134,7 +134,7 @@
 #elif defined(__aarch64__)
 extern "C" void scanline_t32cb16blend_arm64(uint16_t*, uint32_t*, size_t);
 extern "C" void scanline_col32cb16blend_arm64(uint16_t *dst, uint32_t col, size_t ct);
-#elif defined(__mips__)  && !defined(__LP64__)
+#elif defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
 extern "C" void scanline_t32cb16blend_mips(uint16_t*, uint32_t*, size_t);
 #endif
 
@@ -286,7 +286,7 @@
 
 #if ANDROID_ARM_CODEGEN
 
-#if defined(__mips__)
+#if defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
 static CodeCache gCodeCache(32 * 1024);
 #elif defined(__aarch64__)
 static CodeCache gCodeCache(48 * 1024);
@@ -2175,7 +2175,7 @@
 
 void scanline_t32cb16blend(context_t* c)
 {
-#if ((ANDROID_CODEGEN >= ANDROID_CODEGEN_ASM) && (defined(__arm__) || (defined(__mips__) && !defined(__LP64__)) || defined(__aarch64__)))
+#if ((ANDROID_CODEGEN >= ANDROID_CODEGEN_ASM) && (defined(__arm__) || (defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6) || defined(__aarch64__)))
     int32_t x = c->iterators.xl;
     size_t ct = c->iterators.xr - x;
     int32_t y = c->iterators.y;
diff --git a/libpixelflinger/tests/arch-arm64/assembler/Android.mk b/libpixelflinger/tests/arch-arm64/assembler/Android.mk
index eca36ef..961f323 100644
--- a/libpixelflinger/tests/arch-arm64/assembler/Android.mk
+++ b/libpixelflinger/tests/arch-arm64/assembler/Android.mk
@@ -5,6 +5,9 @@
     arm64_assembler_test.cpp\
     asm_test_jacket.S
 
+# asm_test_jacket.S does not compile with Clang.
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
+
 LOCAL_SHARED_LIBRARIES := \
     libcutils \
     libpixelflinger
diff --git a/libpixelflinger/tests/arch-arm64/assembler/arm64_assembler_test.cpp b/libpixelflinger/tests/arch-arm64/assembler/arm64_assembler_test.cpp
index 456be58..5f58797 100644
--- a/libpixelflinger/tests/arch-arm64/assembler/arm64_assembler_test.cpp
+++ b/libpixelflinger/tests/arch-arm64/assembler/arm64_assembler_test.cpp
@@ -409,7 +409,7 @@
 {
     const long base = long(instrMem);
     const long curr = base + long(instrMemSize);
-    __builtin___clear_cache((void*)base, (void*)curr);
+    __builtin___clear_cache((char*)base, (char*)curr);
 }
 void dataOpTest(dataOpTest_t test, ARMAssemblerInterface *a64asm, uint32_t Rd = 0,
                 uint32_t Rn = 1, uint32_t Rm = 2, uint32_t Rs = 3)
@@ -493,16 +493,17 @@
         if(i == Rd) continue;
         if(regs[i] != savedRegs[i])
         {
-            printf("Test %x failed Reg(%d) tampered Expected(0x%"PRIx64"),"
-                   "Actual(0x%"PRIx64") t\n", test.id, i, savedRegs[i], regs[i]);
+            printf("Test %x failed Reg(%d) tampered Expected(0x%" PRIx64 "),"
+                   "Actual(0x%" PRIx64 ") t\n", test.id, i, savedRegs[i],
+                   regs[i]);
             return;
         }
     }
 
     if(test.checkRd == 1 && (uint64_t)regs[Rd] != test.postRdValue)
     {
-        printf("Test %x failed, Expected(%"PRIx64"), Actual(%"PRIx64")\n",
-                test.id, test.postRdValue, regs[Rd]);
+        printf("Test %x failed, Expected(%" PRIx64 "), Actual(%" PRIx64 ")\n",
+               test.id, test.postRdValue, regs[Rd]);
     }
     else if(test.checkFlag == 1 && flags[test.postFlag] == 0)
     {
@@ -610,7 +611,7 @@
         if(regs[i] != savedRegs[i])
         {
             printf("Test %x failed Reg(%d) tampered"
-                   " Expected(0x%"PRIx64"), Actual(0x%"PRIx64") t\n",
+                   " Expected(0x%" PRIx64 "), Actual(0x%" PRIx64 ") t\n",
                    test.id, i, savedRegs[i], regs[i]);
             return;
         }
@@ -619,13 +620,13 @@
     if((uint64_t)regs[Rd] != test.postRdValue)
     {
         printf("Test %x failed, "
-               "Expected in Rd(0x%"PRIx64"), Actual(0x%"PRIx64")\n",
+               "Expected in Rd(0x%" PRIx64 "), Actual(0x%" PRIx64 ")\n",
                test.id, test.postRdValue, regs[Rd]);
     }
     else if((uint64_t)regs[Rn] != (uint64_t)(&dataMem[test.postRnValue]))
     {
         printf("Test %x failed, "
-               "Expected in Rn(0x%"PRIx64"), Actual(0x%"PRIx64")\n",
+               "Expected in Rn(0x%" PRIx64 "), Actual(0x%" PRIx64 ")\n",
                test.id, test.postRnValue, regs[Rn] - (uint64_t)dataMem);
     }
     else if(test.checkMem == true)
@@ -638,7 +639,7 @@
         if(value != test.postMemValue)
         {
             printf("Test %x failed, "
-                   "Expected in Mem(0x%"PRIx64"), Actual(0x%"PRIx64")\n",
+                   "Expected in Mem(0x%" PRIx64 "), Actual(0x%" PRIx64 ")\n",
                    test.id, test.postMemValue, value);
         }
         else
@@ -697,8 +698,8 @@
                 if(regs[j] != j)
                 {
                     printf("LDM/STM Test %x failed "
-                           "Reg%d expected(0x%x) Actual(0x%"PRIx64") \n",
-                            patterns[i],j,j,regs[j]);
+                           "Reg%d expected(0x%x) Actual(0x%" PRIx64 ") \n",
+                           patterns[i], j, j, regs[j]);
                     break;
                 }
             }
diff --git a/libpixelflinger/tests/arch-arm64/col32cb16blend/Android.mk b/libpixelflinger/tests/arch-arm64/col32cb16blend/Android.mk
index 3368eb0..5d69203 100644
--- a/libpixelflinger/tests/arch-arm64/col32cb16blend/Android.mk
+++ b/libpixelflinger/tests/arch-arm64/col32cb16blend/Android.mk
@@ -5,6 +5,8 @@
     col32cb16blend_test.c \
     ../../../arch-arm64/col32cb16blend.S
 
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
+
 LOCAL_SHARED_LIBRARIES :=
 
 LOCAL_C_INCLUDES :=
diff --git a/libpixelflinger/tests/arch-arm64/t32cb16blend/Android.mk b/libpixelflinger/tests/arch-arm64/t32cb16blend/Android.mk
index 8e5ec5e..2c1379b 100644
--- a/libpixelflinger/tests/arch-arm64/t32cb16blend/Android.mk
+++ b/libpixelflinger/tests/arch-arm64/t32cb16blend/Android.mk
@@ -5,6 +5,8 @@
     t32cb16blend_test.c \
     ../../../arch-arm64/t32cb16blend.S
 
+LOCAL_CLANG_ASFLAGS_arm64 += -no-integrated-as
+
 LOCAL_SHARED_LIBRARIES :=
 
 LOCAL_C_INCLUDES :=
diff --git a/libpixelflinger/tests/codegen/codegen.cpp b/libpixelflinger/tests/codegen/codegen.cpp
index 46c1ccc..148b6f4 100644
--- a/libpixelflinger/tests/codegen/codegen.cpp
+++ b/libpixelflinger/tests/codegen/codegen.cpp
@@ -9,16 +9,18 @@
 #include "codeflinger/CodeCache.h"
 #include "codeflinger/GGLAssembler.h"
 #include "codeflinger/ARMAssembler.h"
+#if defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
 #include "codeflinger/MIPSAssembler.h"
+#endif
 #include "codeflinger/Arm64Assembler.h"
 
-#if defined(__arm__) || defined(__mips__) || defined(__aarch64__)
+#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6) || defined(__aarch64__)
 #   define ANDROID_ARM_CODEGEN  1
 #else
 #   define ANDROID_ARM_CODEGEN  0
 #endif
 
-#if defined (__mips__)
+#if defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
 #define ASSEMBLY_SCRATCH_SIZE   4096
 #elif defined(__aarch64__)
 #define ASSEMBLY_SCRATCH_SIZE   8192
@@ -52,7 +54,7 @@
     GGLAssembler assembler( new ARMAssembler(a) );
 #endif
 
-#if defined(__mips__) && !defined(__LP64__)
+#if defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6
     GGLAssembler assembler( new ArmToMipsAssembler(a) );
 #endif
 
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
index 49f5903..a80965f 100644
--- a/libprocessgroup/processgroup.cpp
+++ b/libprocessgroup/processgroup.cpp
@@ -19,7 +19,9 @@
 
 #include <assert.h>
 #include <dirent.h>
+#include <errno.h>
 #include <fcntl.h>
+#include <inttypes.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -100,7 +102,7 @@
 
     ctx->buf_len += ret;
     ctx->buf[ctx->buf_len] = 0;
-    SLOGV("Read %d to buffer: %s", ret, ctx->buf);
+    SLOGV("Read %zd to buffer: %s", ret, ctx->buf);
 
     assert(ctx->buf_len <= sizeof(ctx->buf));
 
@@ -251,7 +253,7 @@
 {
     int processes;
     int sleep_us = 100;
-    long startTime = android::uptimeMillis();
+    int64_t startTime = android::uptimeMillis();
 
     while ((processes = killProcessGroupOnce(uid, initialPid, signal)) > 0) {
         SLOGV("killed %d processes for processgroup %d\n", processes, initialPid);
@@ -265,7 +267,7 @@
         }
     }
 
-    SLOGV("Killed process group uid %d pid %d in %ldms, %d procs remain", uid, initialPid,
+    SLOGV("Killed process group uid %d pid %d in %" PRId64 "ms, %d procs remain", uid, initialPid,
             android::uptimeMillis()-startTime, processes);
 
     if (processes == 0) {
@@ -279,12 +281,12 @@
 {
     int ret;
 
-    ret = mkdir(path, 0750);
+    ret = mkdir(path, mode);
     if (ret < 0 && errno != EEXIST) {
         return -errno;
     }
 
-    ret = chown(path, AID_SYSTEM, AID_SYSTEM);
+    ret = chown(path, uid, gid);
     if (ret < 0) {
         ret = -errno;
         rmdir(path);
diff --git a/libsparse/Android.mk b/libsparse/Android.mk
index 0abe33d..925b98b 100644
--- a/libsparse/Android.mk
+++ b/libsparse/Android.mk
@@ -16,7 +16,7 @@
 LOCAL_SRC_FILES := $(libsparse_src_files)
 LOCAL_MODULE := libsparse_host
 LOCAL_STATIC_LIBRARIES := libz
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
 LOCAL_CFLAGS := -Werror
 include $(BUILD_HOST_STATIC_LIBRARY)
 
@@ -25,7 +25,7 @@
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 LOCAL_SRC_FILES := $(libsparse_src_files)
 LOCAL_MODULE := libsparse
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
 LOCAL_SHARED_LIBRARIES := \
     libz
 LOCAL_CFLAGS := -Werror
@@ -36,7 +36,7 @@
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 LOCAL_SRC_FILES := $(libsparse_src_files)
 LOCAL_MODULE := libsparse_static
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/include external/zlib
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
 LOCAL_STATIC_LIBRARIES := libz
 LOCAL_CFLAGS := -Werror
 include $(BUILD_STATIC_LIBRARY)
diff --git a/libsuspend/autosuspend_autosleep.c b/libsuspend/autosuspend_autosleep.c
index 5451615..0d31e74 100644
--- a/libsuspend/autosuspend_autosleep.c
+++ b/libsuspend/autosuspend_autosleep.c
@@ -84,7 +84,6 @@
 
 struct autosuspend_ops *autosuspend_autosleep_init(void)
 {
-    int ret;
     char buf[80];
 
     autosleep_fd = open(SYS_POWER_AUTOSLEEP, O_WRONLY);
diff --git a/libsync/sync.c b/libsync/sync.c
index 4892866..d73bb11 100644
--- a/libsync/sync.c
+++ b/libsync/sync.c
@@ -17,6 +17,7 @@
  */
 
 #include <fcntl.h>
+#include <malloc.h>
 #include <stdint.h>
 #include <string.h>
 
diff --git a/libsysutils/Android.mk b/libsysutils/Android.mk
index 246f954..7bf53e3 100644
--- a/libsysutils/Android.mk
+++ b/libsysutils/Android.mk
@@ -1,5 +1,3 @@
-ifneq ($(BUILD_TINY_ANDROID),true)
-
 LOCAL_PATH:= $(call my-dir)
 
 include $(CLEAR_VARS)
@@ -16,12 +14,12 @@
 
 LOCAL_MODULE:= libsysutils
 
-LOCAL_C_INCLUDES :=
-
 LOCAL_CFLAGS := -Werror
 
-LOCAL_SHARED_LIBRARIES := libcutils liblog
+LOCAL_SHARED_LIBRARIES := \
+        libcutils \
+        liblog \
+        libnl
 
 include $(BUILD_SHARED_LIBRARY)
 
-endif
diff --git a/libsysutils/src/NetlinkEvent.cpp b/libsysutils/src/NetlinkEvent.cpp
index 9d596ef..909df86 100644
--- a/libsysutils/src/NetlinkEvent.cpp
+++ b/libsysutils/src/NetlinkEvent.cpp
@@ -32,13 +32,21 @@
 #include <linux/if_addr.h>
 #include <linux/if_link.h>
 #include <linux/netfilter/nfnetlink.h>
+#include <linux/netfilter/nfnetlink_log.h>
 #include <linux/netfilter_ipv4/ipt_ULOG.h>
+
 /* From kernel's net/netfilter/xt_quota2.c */
-const int QLOG_NL_EVENT  = 112;
+const int LOCAL_QLOG_NL_EVENT = 112;
+const int LOCAL_NFLOG_PACKET = NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET;
 
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
 
+#include <netlink/attr.h>
+#include <netlink/genl/genl.h>
+#include <netlink/handlers.h>
+#include <netlink/msg.h>
+
 const int NetlinkEvent::NlActionUnknown = 0;
 const int NetlinkEvent::NlActionAdd = 1;
 const int NetlinkEvent::NlActionRemove = 2;
@@ -95,7 +103,8 @@
         NL_EVENT_RTM_NAME(RTM_NEWROUTE);
         NL_EVENT_RTM_NAME(RTM_DELROUTE);
         NL_EVENT_RTM_NAME(RTM_NEWNDUSEROPT);
-        NL_EVENT_RTM_NAME(QLOG_NL_EVENT);
+        NL_EVENT_RTM_NAME(LOCAL_QLOG_NL_EVENT);
+        NL_EVENT_RTM_NAME(LOCAL_NFLOG_PACKET);
         default:
             return NULL;
     }
@@ -272,6 +281,41 @@
 }
 
 /*
+ * Parse a LOCAL_NFLOG_PACKET message.
+ */
+bool NetlinkEvent::parseNfPacketMessage(struct nlmsghdr *nh) {
+    int uid = -1;
+    int len = 0;
+    char* raw = NULL;
+
+    struct nlattr *uid_attr = nlmsg_find_attr(nh, sizeof(struct genlmsghdr), NFULA_UID);
+    if (uid_attr) {
+        uid = ntohl(nla_get_u32(uid_attr));
+    }
+
+    struct nlattr *payload = nlmsg_find_attr(nh, sizeof(struct genlmsghdr), NFULA_PAYLOAD);
+    if (payload) {
+        /* First 256 bytes is plenty */
+        len = nla_len(payload);
+        if (len > 256) len = 256;
+        raw = (char*) nla_data(payload);
+    }
+
+    char* hex = (char*) calloc(1, 5 + (len * 2));
+    strcpy(hex, "HEX=");
+    for (int i = 0; i < len; i++) {
+        hex[4 + (i * 2)] = "0123456789abcdef"[(raw[i] >> 4) & 0xf];
+        hex[5 + (i * 2)] = "0123456789abcdef"[raw[i] & 0xf];
+    }
+
+    asprintf(&mParams[0], "UID=%d", uid);
+    mParams[1] = hex;
+    mSubsystem = strdup("strict");
+    mAction = NlActionChange;
+    return true;
+}
+
+/*
  * Parse a RTM_NEWROUTE or RTM_DELROUTE message.
  */
 bool NetlinkEvent::parseRtMessage(const struct nlmsghdr *nh) {
@@ -478,7 +522,7 @@
  * TODO: consider only ever looking at the first message.
  */
 bool NetlinkEvent::parseBinaryNetlinkMessage(char *buffer, int size) {
-    const struct nlmsghdr *nh;
+    struct nlmsghdr *nh;
 
     for (nh = (struct nlmsghdr *) buffer;
          NLMSG_OK(nh, (unsigned) size) && (nh->nlmsg_type != NLMSG_DONE);
@@ -493,7 +537,7 @@
             if (parseIfInfoMessage(nh))
                 return true;
 
-        } else if (nh->nlmsg_type == QLOG_NL_EVENT) {
+        } else if (nh->nlmsg_type == LOCAL_QLOG_NL_EVENT) {
             if (parseUlogPacketMessage(nh))
                 return true;
 
@@ -511,6 +555,10 @@
             if (parseNdUserOptMessage(nh))
                 return true;
 
+        } else if (nh->nlmsg_type == LOCAL_NFLOG_PACKET) {
+            if (parseNfPacketMessage(nh))
+                return true;
+
         }
     }
 
@@ -588,7 +636,8 @@
 }
 
 bool NetlinkEvent::decode(char *buffer, int size, int format) {
-    if (format == NetlinkListener::NETLINK_FORMAT_BINARY) {
+    if (format == NetlinkListener::NETLINK_FORMAT_BINARY
+            || format == NetlinkListener::NETLINK_FORMAT_BINARY_UNICAST) {
         return parseBinaryNetlinkMessage(buffer, size);
     } else {
         return parseAsciiNetlinkMessage(buffer, size);
diff --git a/libsysutils/src/NetlinkListener.cpp b/libsysutils/src/NetlinkListener.cpp
index 81c5cc2..637aa1e 100644
--- a/libsysutils/src/NetlinkListener.cpp
+++ b/libsysutils/src/NetlinkListener.cpp
@@ -47,8 +47,13 @@
     ssize_t count;
     uid_t uid = -1;
 
-    count = TEMP_FAILURE_RETRY(uevent_kernel_multicast_uid_recv(
-                                       socket, mBuffer, sizeof(mBuffer), &uid));
+    bool require_group = true;
+    if (mFormat == NETLINK_FORMAT_BINARY_UNICAST) {
+        require_group = false;
+    }
+
+    count = TEMP_FAILURE_RETRY(uevent_kernel_recv(socket,
+            mBuffer, sizeof(mBuffer), require_group, &uid));
     if (count < 0) {
         if (uid > 0)
             LOG_EVENT_INT(65537, uid);
diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp
index d3ce8f5..bb9b6a1 100644
--- a/libsysutils/src/SocketClient.cpp
+++ b/libsysutils/src/SocketClient.cpp
@@ -1,5 +1,6 @@
 #include <alloca.h>
 #include <errno.h>
+#include <malloc.h>
 #include <pthread.h>
 #include <signal.h>
 #include <string.h>
@@ -220,7 +221,9 @@
 
     sigaction(SIGPIPE, &old_action, &new_action);
 
-    errno = e;
+    if (e != 0) {
+        errno = e;
+    }
     return ret;
 }
 
diff --git a/libutils/Android.mk b/libutils/Android.mk
index b1dc1f8..7bff14e 100644
--- a/libutils/Android.mk
+++ b/libutils/Android.mk
@@ -14,9 +14,6 @@
 
 LOCAL_PATH:= $(call my-dir)
 
-# libutils is a little unique: It's built twice, once for the host
-# and once for the device.
-
 commonSources:= \
 	BasicHashtable.cpp \
 	BlobCache.cpp \
@@ -42,7 +39,9 @@
 	Tokenizer.cpp \
 	Unicode.cpp \
 	VectorImpl.cpp \
-	misc.cpp
+	file.cpp \
+	misc.cpp \
+	stringprintf.cpp \
 
 host_commonCflags := -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -Werror
 
@@ -53,13 +52,6 @@
 endif
 endif
 
-host_commonLdlibs :=
-
-ifeq ($(TARGET_OS),linux)
-host_commonLdlibs += -lrt -ldl
-endif
-
-
 # For the host
 # =====================================================
 include $(CLEAR_VARS)
@@ -67,6 +59,9 @@
 ifeq ($(HOST_OS), linux)
 LOCAL_SRC_FILES += Looper.cpp
 endif
+ifeq ($(HOST_OS),darwin)
+LOCAL_CFLAGS += -Wno-unused-parameter
+endif
 LOCAL_MODULE:= libutils
 LOCAL_STATIC_LIBRARIES := liblog
 LOCAL_CFLAGS += $(host_commonCflags)
@@ -90,10 +85,6 @@
 endif
 LOCAL_CFLAGS += -Werror
 
-LOCAL_C_INCLUDES += \
-		bionic/libc \
-		external/zlib
-
 LOCAL_STATIC_LIBRARIES := \
 	libcutils
 
@@ -102,8 +93,6 @@
         liblog \
         libdl
 
-include external/stlport/libstlport.mk
-
 LOCAL_MODULE:= libutils
 include $(BUILD_STATIC_LIBRARY)
 
@@ -119,15 +108,8 @@
         liblog
 LOCAL_CFLAGS := -Werror
 
-include external/stlport/libstlport.mk
-
 include $(BUILD_SHARED_LIBRARY)
 
-# Include subdirectory makefiles
-# ============================================================
 
-# If we're building with ONE_SHOT_MAKEFILE (mm, mmm), then what the framework
-# team really wants is to build the stuff defined by this makefile.
-ifeq (,$(ONE_SHOT_MAKEFILE))
+# Build the tests in the tests/ subdirectory.
 include $(call first-makefiles-under,$(LOCAL_PATH))
-endif
diff --git a/libutils/FileMap.cpp b/libutils/FileMap.cpp
index f8d6bda..f49b4f9 100644
--- a/libutils/FileMap.cpp
+++ b/libutils/FileMap.cpp
@@ -23,7 +23,7 @@
 #include <utils/FileMap.h>
 #include <utils/Log.h>
 
-#if defined(HAVE_WIN32_FILEMAP) && !defined(__USE_MINGW_ANSI_STDIO)
+#if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
 # define PRId32 "I32d"
 # define PRIx32 "I32x"
 # define PRId64 "I64d"
@@ -33,7 +33,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#ifdef HAVE_POSIX_FILEMAP
+#if !defined(__MINGW32__)
 #include <sys/mman.h>
 #endif
 
@@ -64,12 +64,7 @@
     if (mFileName != NULL) {
         free(mFileName);
     }
-#ifdef HAVE_POSIX_FILEMAP
-    if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
-        ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
-    }
-#endif
-#ifdef HAVE_WIN32_FILEMAP
+#if defined(__MINGW32__)
     if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
         ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr,
               GetLastError() );
@@ -77,6 +72,10 @@
     if (mFileMapping != INVALID_HANDLE_VALUE) {
         CloseHandle(mFileMapping);
     }
+#else
+    if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
+        ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
+    }
 #endif
 }
 
@@ -90,7 +89,7 @@
 bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
         bool readOnly)
 {
-#ifdef HAVE_WIN32_FILEMAP
+#if defined(__MINGW32__)
     int     adjust;
     off64_t adjOffset;
     size_t  adjLength;
@@ -128,8 +127,7 @@
         mFileMapping = INVALID_HANDLE_VALUE;
         return false;
     }
-#endif
-#ifdef HAVE_POSIX_FILEMAP
+#else // !defined(__MINGW32__)
     int     prot, flags, adjust;
     off64_t adjOffset;
     size_t  adjLength;
@@ -179,7 +177,7 @@
         return false;
     }
     mBasePtr = ptr;
-#endif // HAVE_POSIX_FILEMAP
+#endif // !defined(__MINGW32__)
 
     mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
     mBaseLength = adjLength;
@@ -196,9 +194,9 @@
 }
 
 // Provide guidance to the system.
+#if !defined(_WIN32)
 int FileMap::advise(MapAdvice advice)
 {
-#if HAVE_MADVISE
     int cc, sysAdvice;
 
     switch (advice) {
@@ -216,7 +214,11 @@
     if (cc != 0)
         ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
     return cc;
-#else
-    return -1;
-#endif // HAVE_MADVISE
 }
+
+#else
+int FileMap::advise(MapAdvice /* advice */)
+{
+    return -1;
+}
+#endif
diff --git a/libutils/String8.cpp b/libutils/String8.cpp
index 9092cbc..3323b82 100644
--- a/libutils/String8.cpp
+++ b/libutils/String8.cpp
@@ -424,7 +424,7 @@
             next = len;
         }
 
-        memcpy(buf + tail, buf + index + skip, next - index - skip);
+        memmove(buf + tail, buf + index + skip, next - index - skip);
         tail += next - index - skip;
         index = next;
     }
diff --git a/libutils/SystemClock.cpp b/libutils/SystemClock.cpp
index dbad581..ac3dd98 100644
--- a/libutils/SystemClock.cpp
+++ b/libutils/SystemClock.cpp
@@ -68,7 +68,7 @@
  */
 #define DEBUG_TIMESTAMP         0
 
-#if DEBUG_TIMESTAMP && defined(ARCH_ARM)
+#if DEBUG_TIMESTAMP && defined(__arm__)
 static inline void checkTimeStamps(int64_t timestamp,
                                    int64_t volatile *prevTimestampPtr,
                                    int volatile *prevMethodPtr,
diff --git a/libutils/Threads.cpp b/libutils/Threads.cpp
index b09d510..1e014c6 100644
--- a/libutils/Threads.cpp
+++ b/libutils/Threads.cpp
@@ -24,21 +24,18 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 # include <pthread.h>
 # include <sched.h>
 # include <sys/resource.h>
-#ifdef HAVE_ANDROID_OS
-# include <private/bionic_pthread.h>
-#endif
-#elif defined(HAVE_WIN32_THREADS)
+#else
 # include <windows.h>
 # include <stdint.h>
 # include <process.h>
 # define HAVE_CREATETHREAD  // Cygwin, vs. HAVE__BEGINTHREADEX for MinGW
 #endif
 
-#if defined(HAVE_PRCTL)
+#if defined(__linux__)
 #include <sys/prctl.h>
 #endif
 
@@ -62,7 +59,7 @@
 using namespace android;
 
 // ----------------------------------------------------------------------------
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 // ----------------------------------------------------------------------------
 
 /*
@@ -93,7 +90,7 @@
         } else {
             set_sched_policy(0, SP_FOREGROUND);
         }
-        
+
         if (name) {
             androidSetThreadName(name);
             free(name);
@@ -103,7 +100,7 @@
 };
 
 void androidSetThreadName(const char* name) {
-#if defined(HAVE_PRCTL)
+#if defined(__linux__)
     // Mac OS doesn't have this, and we build libutil for the host too
     int hasAt = 0;
     int hasDot = 0;
@@ -130,7 +127,7 @@
                                size_t threadStackSize,
                                android_thread_id_t *threadId)
 {
-    pthread_attr_t attr; 
+    pthread_attr_t attr;
     pthread_attr_init(&attr);
     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 
@@ -149,14 +146,14 @@
         t->entryFunction = entryFunction;
         t->userData = userData;
         entryFunction = (android_thread_func_t)&thread_data_t::trampoline;
-        userData = t;            
+        userData = t;
     }
 #endif
 
     if (threadStackSize) {
         pthread_attr_setstacksize(&attr, threadStackSize);
     }
-    
+
     errno = 0;
     pthread_t thread;
     int result = pthread_create(&thread, &attr,
@@ -191,7 +188,7 @@
 }
 
 // ----------------------------------------------------------------------------
-#elif defined(HAVE_WIN32_THREADS)
+#else // !defined(_WIN32)
 // ----------------------------------------------------------------------------
 
 /*
@@ -271,9 +268,7 @@
 }
 
 // ----------------------------------------------------------------------------
-#else
-#error "Threads not supported"
-#endif
+#endif // !defined(_WIN32)
 
 // ----------------------------------------------------------------------------
 
@@ -306,21 +301,12 @@
     gCreateThreadFn = func;
 }
 
-pid_t androidGetTid()
-{
-#ifdef HAVE_GETTID
-    return gettid();
-#else
-    return getpid();
-#endif
-}
-
 #ifdef HAVE_ANDROID_OS
 int androidSetThreadPriority(pid_t tid, int pri)
 {
     int rc = 0;
-    
-#if defined(HAVE_PTHREADS)
+
+#if !defined(_WIN32)
     int lasterr = 0;
 
     if (pri >= ANDROID_PRIORITY_BACKGROUND) {
@@ -339,12 +325,12 @@
         errno = lasterr;
     }
 #endif
-    
+
     return rc;
 }
 
 int androidGetThreadPriority(pid_t tid) {
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
     return getpriority(PRIO_PROCESS, tid);
 #else
     return ANDROID_PRIORITY_NORMAL;
@@ -361,9 +347,9 @@
  * ===========================================================================
  */
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 // implemented as inlines in threads.h
-#elif defined(HAVE_WIN32_THREADS)
+#else
 
 Mutex::Mutex()
 {
@@ -425,9 +411,7 @@
     return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1;
 }
 
-#else
-#error "Somebody forgot to implement threads for this platform."
-#endif
+#endif // !defined(_WIN32)
 
 
 /*
@@ -436,9 +420,9 @@
  * ===========================================================================
  */
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 // implemented as inlines in threads.h
-#elif defined(HAVE_WIN32_THREADS)
+#else
 
 /*
  * Windows doesn't have a condition variable solution.  It's possible
@@ -486,7 +470,7 @@
         //printf("+++ wait: incr waitersCount to %d (tid=%ld)\n",
         //    condState->waitersCount, getThreadId());
         LeaveCriticalSection(&condState->waitersCountLock);
-    
+
         DWORD timeout = INFINITE;
         if (abstime) {
             nsecs_t reltime = *abstime - systemTime();
@@ -494,27 +478,27 @@
                 reltime = 0;
             timeout = reltime/1000000;
         }
-        
+
         // Atomically release the external mutex and wait on the semaphore.
         DWORD res =
             SignalObjectAndWait(hMutex, condState->sema, timeout, FALSE);
-    
+
         //printf("+++ wait: awake (tid=%ld)\n", getThreadId());
-    
+
         // Reacquire lock to avoid race conditions.
         EnterCriticalSection(&condState->waitersCountLock);
-    
+
         // No longer waiting.
         condState->waitersCount--;
-    
+
         // Check to see if we're the last waiter after a broadcast.
         bool lastWaiter = (condState->wasBroadcast && condState->waitersCount == 0);
-    
+
         //printf("+++ wait: lastWaiter=%d (wasBc=%d wc=%d)\n",
         //    lastWaiter, condState->wasBroadcast, condState->waitersCount);
-    
+
         LeaveCriticalSection(&condState->waitersCountLock);
-    
+
         // If we're the last waiter thread during this particular broadcast
         // then signal broadcast() that we're all awake.  It'll drop the
         // internal mutex.
@@ -530,11 +514,11 @@
             // Grab the internal mutex.
             WaitForSingleObject(condState->internalMutex, INFINITE);
         }
-    
+
         // Release the internal and grab the external.
         ReleaseMutex(condState->internalMutex);
         WaitForSingleObject(hMutex, INFINITE);
-    
+
         return res == WAIT_OBJECT_0 ? NO_ERROR : -1;
     }
 } WinCondition;
@@ -577,7 +561,7 @@
 {
     WinCondition* condState = (WinCondition*) mState;
     HANDLE hMutex = (HANDLE) mutex.mState;
-    
+
     return ((WinCondition*)mState)->wait(condState, hMutex, NULL);
 }
 
@@ -659,9 +643,7 @@
     ReleaseMutex(condState->internalMutex);
 }
 
-#else
-#error "condition variables not supported on this platform"
-#endif
+#endif // !defined(_WIN32)
 
 // ----------------------------------------------------------------------------
 
@@ -704,7 +686,7 @@
     mStatus = NO_ERROR;
     mExitPending = false;
     mThread = thread_id_t(-1);
-    
+
     // hold a strong reference on ourself
     mHoldSelf = this;
 
@@ -718,7 +700,7 @@
         res = androidCreateRawThreadEtc(_threadLoop,
                 this, name, priority, stack, &mThread);
     }
-    
+
     if (res == false) {
         mStatus = UNKNOWN_ERROR;   // something happened!
         mRunning = false;
@@ -727,7 +709,7 @@
 
         return UNKNOWN_ERROR;
     }
-    
+
     // Do not refer to mStatus here: The thread is already running (may, in fact
     // already have exited with a valid mStatus result). The NO_ERROR indication
     // here merely indicates successfully starting the thread and does not
@@ -791,14 +773,14 @@
             break;
         }
         }
-        
+
         // Release our strong reference, to let a chance to the thread
         // to die a peaceful death.
         strong.clear();
         // And immediately, re-acquire a strong reference for the next loop
         strong = weak.promote();
     } while(strong != 0);
-    
+
     return 0;
 }
 
@@ -819,7 +801,7 @@
 
         return WOULD_BLOCK;
     }
-    
+
     mExitPending = true;
 
     while (mRunning == true) {
@@ -864,7 +846,7 @@
     pid_t tid;
     if (mRunning) {
         pthread_t pthread = android_thread_id_t_to_pthread(mThread);
-        tid = __pthread_gettid(pthread);
+        tid = pthread_gettid_np(pthread);
     } else {
         ALOGW("Thread (this=%p): getTid() is undefined before run()", this);
         tid = -1;
diff --git a/libutils/Timers.cpp b/libutils/Timers.cpp
index 4687d4d..fb70e15 100644
--- a/libutils/Timers.cpp
+++ b/libutils/Timers.cpp
@@ -18,19 +18,10 @@
 // Timer functions.
 //
 #include <utils/Timers.h>
-#include <utils/Log.h>
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
+#include <limits.h>
 #include <sys/time.h>
 #include <time.h>
-#include <errno.h>
-#include <limits.h>
-
-#ifdef HAVE_WIN32_THREADS
-#include <windows.h>
-#endif
 
 #if defined(HAVE_ANDROID_OS)
 nsecs_t systemTime(int clock)
diff --git a/libutils/Unicode.cpp b/libutils/Unicode.cpp
index 378d2a7..fb876c9 100644
--- a/libutils/Unicode.cpp
+++ b/libutils/Unicode.cpp
@@ -24,17 +24,10 @@
 # undef  nhtos
 # undef  htons
 
-# ifdef HAVE_LITTLE_ENDIAN
-#  define ntohl(x)    ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
-#  define htonl(x)    ntohl(x)
-#  define ntohs(x)    ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
-#  define htons(x)    ntohs(x)
-# else
-#  define ntohl(x)    (x)
-#  define htonl(x)    (x)
-#  define ntohs(x)    (x)
-#  define htons(x)    (x)
-# endif
+# define ntohl(x)    ( ((x) << 24) | (((x) >> 24) & 255) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) )
+# define htonl(x)    ntohl(x)
+# define ntohs(x)    ( (((x) << 8) & 0xff00) | (((x) >> 8) & 255) )
+# define htons(x)    ntohs(x)
 #else
 # include <netinet/in.h>
 #endif
@@ -47,8 +40,9 @@
 // Surrogates aren't valid for UTF-32 characters, so define some
 // constants that will let us screen them out.
 static const char32_t kUnicodeSurrogateHighStart  = 0x0000D800;
-static const char32_t kUnicodeSurrogateHighEnd    = 0x0000DBFF;
-static const char32_t kUnicodeSurrogateLowStart   = 0x0000DC00;
+// Unused, here for completeness:
+// static const char32_t kUnicodeSurrogateHighEnd = 0x0000DBFF;
+// static const char32_t kUnicodeSurrogateLowStart = 0x0000DC00;
 static const char32_t kUnicodeSurrogateLowEnd     = 0x0000DFFF;
 static const char32_t kUnicodeSurrogateStart      = kUnicodeSurrogateHighStart;
 static const char32_t kUnicodeSurrogateEnd        = kUnicodeSurrogateLowEnd;
diff --git a/libutils/file.cpp b/libutils/file.cpp
new file mode 100644
index 0000000..577df78
--- /dev/null
+++ b/libutils/file.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/file.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <utils/Compat.h> // For TEMP_FAILURE_RETRY on Darwin.
+
+bool android::ReadFdToString(int fd, std::string* content) {
+  content->clear();
+
+  char buf[BUFSIZ];
+  ssize_t n;
+  while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
+    content->append(buf, n);
+  }
+  return (n == 0) ? true : false;
+}
+
+bool android::ReadFileToString(const std::string& path, std::string* content) {
+  content->clear();
+
+  int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
+  if (fd == -1) {
+    return false;
+  }
+  bool result = ReadFdToString(fd, content);
+  TEMP_FAILURE_RETRY(close(fd));
+  return result;
+}
+
+bool android::WriteStringToFd(const std::string& content, int fd) {
+  const char* p = content.data();
+  size_t left = content.size();
+  while (left > 0) {
+    ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
+    if (n == -1) {
+      return false;
+    }
+    p += n;
+    left -= n;
+  }
+  return true;
+}
+
+static bool CleanUpAfterFailedWrite(const std::string& path) {
+  // Something went wrong. Let's not leave a corrupt file lying around.
+  int saved_errno = errno;
+  unlink(path.c_str());
+  errno = saved_errno;
+  return false;
+}
+
+#if !defined(_WIN32)
+bool android::WriteStringToFile(const std::string& content, const std::string& path,
+                                mode_t mode, uid_t owner, gid_t group) {
+  int fd = TEMP_FAILURE_RETRY(open(path.c_str(),
+                                   O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
+                                   mode));
+  if (fd == -1) {
+    return false;
+  }
+
+  // We do an explicit fchmod here because we assume that the caller really meant what they
+  // said and doesn't want the umask-influenced mode.
+  bool result = (fchmod(fd, mode) != -1 && fchown(fd, owner, group) == -1 && WriteStringToFd(content, fd));
+  TEMP_FAILURE_RETRY(close(fd));
+  return result || CleanUpAfterFailedWrite(path);
+}
+#endif
+
+bool android::WriteStringToFile(const std::string& content, const std::string& path) {
+  int fd = TEMP_FAILURE_RETRY(open(path.c_str(),
+                                   O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
+                                   DEFFILEMODE));
+  if (fd == -1) {
+    return false;
+  }
+
+  bool result = WriteStringToFd(content, fd);
+  TEMP_FAILURE_RETRY(close(fd));
+  return result || CleanUpAfterFailedWrite(path);
+}
diff --git a/libutils/misc.cpp b/libutils/misc.cpp
index 58eb499..ed1ba23 100644
--- a/libutils/misc.cpp
+++ b/libutils/misc.cpp
@@ -27,7 +27,7 @@
 #include <errno.h>
 #include <stdio.h>
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 # include <pthread.h>
 #endif
 
@@ -42,13 +42,13 @@
     int priority;
 };
 
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
 static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
 static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
 #endif
 
 void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
     pthread_mutex_lock(&gSyspropMutex);
     if (gSyspropList == NULL) {
         gSyspropList = new Vector<sysprop_change_callback_info>();
@@ -72,7 +72,7 @@
 }
 
 void report_sysprop_change() {
-#if defined(HAVE_PTHREADS)
+#if !defined(_WIN32)
     pthread_mutex_lock(&gSyspropMutex);
     Vector<sysprop_change_callback_info> listeners;
     if (gSyspropList != NULL) {
diff --git a/libutils/stringprintf.cpp b/libutils/stringprintf.cpp
new file mode 100644
index 0000000..5eaa293
--- /dev/null
+++ b/libutils/stringprintf.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2011 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 <utils/stringprintf.h>
+
+#include <stdio.h>
+
+void android::StringAppendV(std::string* dst, const char* format, va_list ap) {
+  // First try with a small fixed size buffer
+  char space[1024];
+
+  // It's possible for methods that use a va_list to invalidate
+  // the data in it upon use.  The fix is to make a copy
+  // of the structure before using it and use that copy instead.
+  va_list backup_ap;
+  va_copy(backup_ap, ap);
+  int result = vsnprintf(space, sizeof(space), format, backup_ap);
+  va_end(backup_ap);
+
+  if (result < static_cast<int>(sizeof(space))) {
+    if (result >= 0) {
+      // Normal case -- everything fit.
+      dst->append(space, result);
+      return;
+    }
+
+    if (result < 0) {
+      // Just an error.
+      return;
+    }
+  }
+
+  // Increase the buffer size to the size requested by vsnprintf,
+  // plus one for the closing \0.
+  int length = result+1;
+  char* buf = new char[length];
+
+  // Restore the va_list before we use it again
+  va_copy(backup_ap, ap);
+  result = vsnprintf(buf, length, format, backup_ap);
+  va_end(backup_ap);
+
+  if (result >= 0 && result < length) {
+    // It fit
+    dst->append(buf, result);
+  }
+  delete[] buf;
+}
+
+std::string android::StringPrintf(const char* fmt, ...) {
+  va_list ap;
+  va_start(ap, fmt);
+  std::string result;
+  StringAppendV(&result, fmt, ap);
+  va_end(ap);
+  return result;
+}
+
+void android::StringAppendF(std::string* dst, const char* format, ...) {
+  va_list ap;
+  va_start(ap, format);
+  StringAppendV(dst, format, ap);
+  va_end(ap);
+}
diff --git a/libutils/tests/Android.mk b/libutils/tests/Android.mk
index caedaff..ce288ca 100644
--- a/libutils/tests/Android.mk
+++ b/libutils/tests/Android.mk
@@ -1,34 +1,43 @@
-# Build the unit tests.
-LOCAL_PATH := $(call my-dir)
-include $(CLEAR_VARS)
+#
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
 
 # Build the unit tests.
-test_src_files := \
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+
+LOCAL_MODULE := libutils_tests
+
+LOCAL_SRC_FILES := \
     BasicHashtable_test.cpp \
     BlobCache_test.cpp \
     BitSet_test.cpp \
+    file_test.cpp \
     Looper_test.cpp \
     LruCache_test.cpp \
     String8_test.cpp \
+    stringprintf_test.cpp \
     Unicode_test.cpp \
-    Vector_test.cpp
+    Vector_test.cpp \
 
-shared_libraries := \
+LOCAL_SHARED_LIBRARIES := \
     libz \
     liblog \
     libcutils \
     libutils \
-    libstlport
 
-static_libraries := \
-    libgtest \
-    libgtest_main
-
-$(foreach file,$(test_src_files), \
-    $(eval include $(CLEAR_VARS)) \
-    $(eval LOCAL_SHARED_LIBRARIES := $(shared_libraries)) \
-    $(eval LOCAL_STATIC_LIBRARIES := $(static_libraries)) \
-    $(eval LOCAL_SRC_FILES := $(file)) \
-    $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \
-    $(eval include $(BUILD_NATIVE_TEST)) \
-)
+include $(BUILD_NATIVE_TEST)
diff --git a/libutils/tests/BasicHashtable_test.cpp b/libutils/tests/BasicHashtable_test.cpp
index a61b1e1..4b3a717 100644
--- a/libutils/tests/BasicHashtable_test.cpp
+++ b/libutils/tests/BasicHashtable_test.cpp
@@ -21,12 +21,12 @@
 #include <gtest/gtest.h>
 #include <unistd.h>
 
-namespace android {
+namespace {
 
 typedef int SimpleKey;
 typedef int SimpleValue;
-typedef key_value_pair_t<SimpleKey, SimpleValue> SimpleEntry;
-typedef BasicHashtable<SimpleKey, SimpleEntry> SimpleHashtable;
+typedef android::key_value_pair_t<SimpleKey, SimpleValue> SimpleEntry;
+typedef android::BasicHashtable<SimpleKey, SimpleEntry> SimpleHashtable;
 
 struct ComplexKey {
     int k;
@@ -56,10 +56,6 @@
 
 ssize_t ComplexKey::instanceCount = 0;
 
-template<> inline hash_t hash_type(const ComplexKey& value) {
-    return hash_type(value.k);
-}
-
 struct ComplexValue {
     int v;
 
@@ -80,9 +76,18 @@
 
 ssize_t ComplexValue::instanceCount = 0;
 
+} // namespace
+
+
+namespace android {
+
 typedef key_value_pair_t<ComplexKey, ComplexValue> ComplexEntry;
 typedef BasicHashtable<ComplexKey, ComplexEntry> ComplexHashtable;
 
+template<> inline hash_t hash_type(const ComplexKey& value) {
+    return hash_type(value.k);
+}
+
 class BasicHashtableTest : public testing::Test {
 protected:
     virtual void SetUp() {
diff --git a/libutils/tests/LruCache_test.cpp b/libutils/tests/LruCache_test.cpp
index bcbea32..6534211 100644
--- a/libutils/tests/LruCache_test.cpp
+++ b/libutils/tests/LruCache_test.cpp
@@ -20,7 +20,7 @@
 #include <cutils/log.h>
 #include <gtest/gtest.h>
 
-namespace android {
+namespace {
 
 typedef int SimpleKey;
 typedef const char* StringValue;
@@ -53,10 +53,6 @@
 
 ssize_t ComplexKey::instanceCount = 0;
 
-template<> inline hash_t hash_type(const ComplexKey& value) {
-    return hash_type(value.k);
-}
-
 struct ComplexValue {
     int v;
 
@@ -77,8 +73,17 @@
 
 ssize_t ComplexValue::instanceCount = 0;
 
+} // namespace
+
+
+namespace android {
+
 typedef LruCache<ComplexKey, ComplexValue> ComplexCache;
 
+template<> inline android::hash_t hash_type(const ComplexKey& value) {
+    return hash_type(value.k);
+}
+
 class EntryRemovedCallback : public OnEntryRemoved<SimpleKey, StringValue> {
 public:
     EntryRemovedCallback() : callbackCount(0), lastKey(-1), lastValue(NULL) { }
diff --git a/libutils/tests/file_test.cpp b/libutils/tests/file_test.cpp
new file mode 100644
index 0000000..3703a49
--- /dev/null
+++ b/libutils/tests/file_test.cpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2013 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 "utils/file.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <gtest/gtest.h>
+
+class TemporaryFile {
+ public:
+  TemporaryFile() {
+    init("/data/local/tmp");
+    if (fd == -1) {
+      init("/tmp");
+    }
+  }
+
+  ~TemporaryFile() {
+    close(fd);
+    unlink(filename);
+  }
+
+  int fd;
+  char filename[1024];
+
+ private:
+  void init(const char* tmp_dir) {
+    snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
+    fd = mkstemp(filename);
+  }
+};
+
+TEST(file, ReadFileToString_ENOENT) {
+  std::string s("hello");
+  errno = 0;
+  ASSERT_FALSE(android::ReadFileToString("/proc/does-not-exist", &s));
+  EXPECT_EQ(ENOENT, errno);
+  EXPECT_EQ("", s); // s was cleared.
+}
+
+TEST(file, ReadFileToString_success) {
+  std::string s("hello");
+  ASSERT_TRUE(android::ReadFileToString("/proc/version", &s)) << errno;
+  EXPECT_GT(s.length(), 6U);
+  EXPECT_EQ('\n', s[s.length() - 1]);
+  s[5] = 0;
+  EXPECT_STREQ("Linux", s.c_str());
+}
+
+TEST(file, WriteStringToFile) {
+  TemporaryFile tf;
+  ASSERT_TRUE(tf.fd != -1);
+  ASSERT_TRUE(android::WriteStringToFile("abc", tf.filename)) << errno;
+  std::string s;
+  ASSERT_TRUE(android::ReadFileToString(tf.filename, &s)) << errno;
+  EXPECT_EQ("abc", s);
+}
+
+TEST(file, WriteStringToFd) {
+  TemporaryFile tf;
+  ASSERT_TRUE(tf.fd != -1);
+  ASSERT_TRUE(android::WriteStringToFd("abc", tf.fd));
+
+  ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)) << errno;
+
+  std::string s;
+  ASSERT_TRUE(android::ReadFdToString(tf.fd, &s)) << errno;
+  EXPECT_EQ("abc", s);
+}
diff --git a/libutils/tests/stringprintf_test.cpp b/libutils/tests/stringprintf_test.cpp
new file mode 100644
index 0000000..f995452
--- /dev/null
+++ b/libutils/tests/stringprintf_test.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2011 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 <utils/stringprintf.h>
+
+#include <gtest/gtest.h>
+
+TEST(StringPrintfTest, HexSizeT) {
+  size_t size = 0x00107e59;
+  EXPECT_EQ("00107e59", android::StringPrintf("%08zx", size));
+  EXPECT_EQ("0x00107e59", android::StringPrintf("0x%08zx", size));
+}
+
+TEST(StringPrintfTest, StringAppendF) {
+  std::string s("a");
+  android::StringAppendF(&s, "b");
+  EXPECT_EQ("ab", s);
+}
+
+TEST(StringPrintfTest, Errno) {
+  errno = 123;
+  android::StringPrintf("hello %s", "world");
+  EXPECT_EQ(123, errno);
+}
+
+void TestN(size_t n) {
+  char* buf = new char[n + 1];
+  memset(buf, 'x', n);
+  buf[n] = '\0';
+  std::string s(android::StringPrintf("%s", buf));
+  EXPECT_EQ(buf, s);
+  delete[] buf;
+}
+
+TEST(StringPrintfTest, At1023) {
+  TestN(1023);
+}
+
+TEST(StringPrintfTest, At1024) {
+  TestN(1024);
+}
+
+TEST(StringPrintfTest, At1025) {
+  TestN(1025);
+}
diff --git a/libziparchive/Android.mk b/libziparchive/Android.mk
index d23a94f..ba7b74d 100644
--- a/libziparchive/Android.mk
+++ b/libziparchive/Android.mk
@@ -14,31 +14,23 @@
 # limitations under the License.
 
 LOCAL_PATH := $(call my-dir)
+
+source_files := zip_archive.cc
+
 include $(CLEAR_VARS)
-
-source_files := \
-	zip_archive.h \
-	zip_archive.cc
-
-includes := external/zlib
-
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_CPP_EXTENSION := .cc
 LOCAL_SRC_FILES := ${source_files}
-
 LOCAL_STATIC_LIBRARIES := libz
 LOCAL_SHARED_LIBRARIES := libutils
 LOCAL_MODULE:= libziparchive
-
-LOCAL_C_INCLUDES += ${includes}
 LOCAL_CFLAGS := -Werror
 include $(BUILD_STATIC_LIBRARY)
 
 include $(CLEAR_VARS)
-LOCAL_MODULE := libziparchive
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_CPP_EXTENSION := .cc
 LOCAL_SRC_FILES := ${source_files}
-LOCAL_C_INCLUDES += ${includes}
-
 LOCAL_STATIC_LIBRARIES := libz libutils
 LOCAL_MODULE:= libziparchive-host
 LOCAL_CFLAGS := -Werror
@@ -49,29 +41,37 @@
 include $(BUILD_HOST_STATIC_LIBRARY)
 
 include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_SRC_FILES := ${source_files}
+LOCAL_STATIC_LIBRARIES := libz libutils
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_MODULE:= libziparchive-host
+LOCAL_CFLAGS := -Werror
+LOCAL_MULTILIB := both
+include $(BUILD_HOST_SHARED_LIBRARY)
+
+# Tests.
+include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_MODULE := ziparchive-tests
 LOCAL_CPP_EXTENSION := .cc
-LOCAL_CFLAGS += \
-    -DGTEST_OS_LINUX_ANDROID \
-    -DGTEST_HAS_STD_STRING \
-    -Werror
-LOCAL_SRC_FILES := zip_archive_test.cc
+LOCAL_CFLAGS := -Werror
+LOCAL_SRC_FILES := zip_archive_test.cc entry_name_utils_test.cc
 LOCAL_SHARED_LIBRARIES := liblog
-LOCAL_STATIC_LIBRARIES := libziparchive libz libgtest libgtest_main libutils
+LOCAL_STATIC_LIBRARIES := libziparchive libz libutils
 include $(BUILD_NATIVE_TEST)
 
 include $(CLEAR_VARS)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_MODULE := ziparchive-tests-host
 LOCAL_CPP_EXTENSION := .cc
 LOCAL_CFLAGS += \
-    -DGTEST_OS_LINUX \
-    -DGTEST_HAS_STD_STRING \
-    -Werror
-LOCAL_SRC_FILES := zip_archive_test.cc
-LOCAL_STATIC_LIBRARIES := libziparchive-host \
-	libz \
-	libgtest_host \
-	libgtest_main_host \
-	liblog \
-	libutils
+    -Werror \
+    -Wno-unnamed-type-template-args
+LOCAL_SRC_FILES := zip_archive_test.cc entry_name_utils_test.cc
+LOCAL_SHARED_LIBRARIES := libziparchive-host liblog
+LOCAL_STATIC_LIBRARIES := \
+    libz \
+    libutils
 include $(BUILD_HOST_NATIVE_TEST)
diff --git a/libziparchive/entry_name_utils-inl.h b/libziparchive/entry_name_utils-inl.h
new file mode 100644
index 0000000..ddbc286
--- /dev/null
+++ b/libziparchive/entry_name_utils-inl.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_
+#define LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+// Check if |length| bytes at |entry_name| constitute a valid entry name.
+// Entry names must be valid UTF-8 and must not contain '0'.
+inline bool IsValidEntryName(const uint8_t* entry_name, const size_t length) {
+  for (size_t i = 0; i < length; ++i) {
+    const uint8_t byte = entry_name[i];
+    if (byte == 0) {
+      return false;
+    } else if ((byte & 0x80) == 0) {
+      // Single byte sequence.
+      continue;
+    } else if ((byte & 0xc0) == 0x80 || (byte & 0xfe) == 0xfe) {
+      // Invalid sequence.
+      return false;
+    } else {
+      // 2-5 byte sequences.
+      for (uint8_t first = byte << 1; first & 0x80; first <<= 1) {
+        ++i;
+
+        // Missing continuation byte..
+        if (i == length) {
+          return false;
+        }
+
+        // Invalid continuation byte.
+        const uint8_t continuation_byte = entry_name[i];
+        if ((continuation_byte & 0xc0) != 0x80) {
+          return false;
+        }
+      }
+    }
+  }
+
+  return true;
+}
+
+
+#endif  // LIBZIPARCHIVE_ENTRY_NAME_UTILS_INL_H_
diff --git a/libziparchive/entry_name_utils_test.cc b/libziparchive/entry_name_utils_test.cc
new file mode 100644
index 0000000..20715bb
--- /dev/null
+++ b/libziparchive/entry_name_utils_test.cc
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "entry_name_utils-inl.h"
+
+#include <gtest/gtest.h>
+
+TEST(entry_name_utils, NullChars) {
+  // 'A', 'R', '\0', 'S', 'E'
+  const uint8_t zeroes[] = { 0x41, 0x52, 0x00, 0x53, 0x45 };
+  ASSERT_FALSE(IsValidEntryName(zeroes, sizeof(zeroes)));
+
+  const uint8_t zeroes_continuation_chars[] = { 0xc2, 0xa1, 0xc2, 0x00 };
+  ASSERT_FALSE(IsValidEntryName(zeroes_continuation_chars,
+                                sizeof(zeroes_continuation_chars)));
+}
+
+TEST(entry_name_utils, InvalidSequence) {
+  // 0xfe is an invalid start byte
+  const uint8_t invalid[] = { 0x41, 0xfe };
+  ASSERT_FALSE(IsValidEntryName(invalid, sizeof(invalid)));
+
+  // 0x91 is an invalid start byte (it's a valid continuation byte).
+  const uint8_t invalid2[] = { 0x41, 0x91 };
+  ASSERT_FALSE(IsValidEntryName(invalid2, sizeof(invalid2)));
+}
+
+TEST(entry_name_utils, TruncatedContinuation) {
+  // Malayalam script with truncated bytes. There should be 2 bytes
+  // after 0xe0
+  const uint8_t truncated[] = { 0xe0, 0xb4, 0x85, 0xe0, 0xb4 };
+  ASSERT_FALSE(IsValidEntryName(truncated, sizeof(truncated)));
+
+  // 0xc2 is the start of a 2 byte sequence that we've subsequently
+  // dropped.
+  const uint8_t truncated2[] = { 0xc2, 0xc2, 0xa1 };
+  ASSERT_FALSE(IsValidEntryName(truncated2, sizeof(truncated2)));
+}
+
+TEST(entry_name_utils, BadContinuation) {
+  // 0x41 is an invalid continuation char, since it's MSBs
+  // aren't "10..." (are 01).
+  const uint8_t bad[] = { 0xc2, 0xa1, 0xc2, 0x41 };
+  ASSERT_FALSE(IsValidEntryName(bad, sizeof(bad)));
+
+  // 0x41 is an invalid continuation char, since it's MSBs
+  // aren't "10..." (are 11).
+  const uint8_t bad2[] = { 0xc2, 0xa1, 0xc2, 0xfe };
+  ASSERT_FALSE(IsValidEntryName(bad2, sizeof(bad2)));
+}
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index 87dac0e..afc122d 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -33,8 +33,10 @@
 
 #include <JNIHelp.h>  // TEMP_FAILURE_RETRY may or may not be in unistd
 
+#include "entry_name_utils-inl.h"
 #include "ziparchive/zip_archive.h"
 
+
 // This is for windows. If we don't open a file in binary mode, weird
 // things will happen.
 #ifndef O_BINARY
@@ -193,7 +195,6 @@
 #undef DISALLOW_IMPLICIT_CONSTRUCTORS
 
 static const uint32_t kGPBDDFlagMask = 0x0008;         // mask value that signifies that the entry has a DD
-static const uint32_t kMaxErrorLen = 1024;
 
 // The maximum size of a central directory or a file
 // comment in bytes.
@@ -288,6 +289,7 @@
 struct ZipArchive {
   /* open Zip archive */
   const int fd;
+  const bool close_file;
 
   /* mapped central directory area */
   off64_t directory_offset;
@@ -305,8 +307,9 @@
   uint32_t hash_table_size;
   ZipEntryName* hash_table;
 
-  ZipArchive(const int fd) :
+  ZipArchive(const int fd, bool assume_ownership) :
       fd(fd),
+      close_file(assume_ownership),
       directory_offset(0),
       directory_map(NULL),
       num_entries(0),
@@ -314,7 +317,7 @@
       hash_table(NULL) {}
 
   ~ZipArchive() {
-    if (fd >= 0) {
+    if (close_file && fd >= 0) {
       close(fd);
     }
 
@@ -385,8 +388,10 @@
   return val;
 }
 
-static uint32_t ComputeHash(const char* str, uint16_t len) {
+static uint32_t ComputeHash(const ZipEntryName& name) {
   uint32_t hash = 0;
+  uint16_t len = name.name_length;
+  const uint8_t* str = name.name;
 
   while (len--) {
     hash = hash * 31 + *str++;
@@ -401,21 +406,21 @@
  */
 static int64_t EntryToIndex(const ZipEntryName* hash_table,
                             const uint32_t hash_table_size,
-                            const char* name, uint16_t length) {
-  const uint32_t hash = ComputeHash(name, length);
+                            const ZipEntryName& name) {
+  const uint32_t hash = ComputeHash(name);
 
   // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
   uint32_t ent = hash & (hash_table_size - 1);
   while (hash_table[ent].name != NULL) {
-    if (hash_table[ent].name_length == length &&
-        memcmp(hash_table[ent].name, name, length) == 0) {
+    if (hash_table[ent].name_length == name.name_length &&
+        memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
       return ent;
     }
 
     ent = (ent + 1) & (hash_table_size - 1);
   }
 
-  ALOGV("Zip: Unable to find entry %.*s", length, name);
+  ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
   return kEntryNotFound;
 }
 
@@ -423,8 +428,8 @@
  * Add a new entry to the hash table.
  */
 static int32_t AddToHash(ZipEntryName *hash_table, const uint64_t hash_table_size,
-                         const char* name, uint16_t length) {
-  const uint64_t hash = ComputeHash(name, length);
+                         const ZipEntryName& name) {
+  const uint64_t hash = ComputeHash(name);
   uint32_t ent = hash & (hash_table_size - 1);
 
   /*
@@ -432,17 +437,17 @@
    * Further, we guarantee that the hashtable size is not 0.
    */
   while (hash_table[ent].name != NULL) {
-    if (hash_table[ent].name_length == length &&
-        memcmp(hash_table[ent].name, name, length) == 0) {
+    if (hash_table[ent].name_length == name.name_length &&
+        memcmp(hash_table[ent].name, name.name, name.name_length) == 0) {
       // We've found a duplicate entry. We don't accept it
-      ALOGW("Zip: Found duplicate entry %.*s", length, name);
+      ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
       return kDuplicateEntry;
     }
     ent = (ent + 1) & (hash_table_size - 1);
   }
 
-  hash_table[ent].name = name;
-  hash_table[ent].name_length = length;
+  hash_table[ent].name = name.name;
+  hash_table[ent].name_length = name.name_length;
   return 0;
 }
 
@@ -638,17 +643,19 @@
     const uint16_t file_name_length = cdr->file_name_length;
     const uint16_t extra_length = cdr->extra_field_length;
     const uint16_t comment_length = cdr->comment_length;
-    const char* file_name = reinterpret_cast<const char*>(ptr + sizeof(CentralDirectoryRecord));
+    const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
 
-    /* check that file name doesn't contain \0 character */
-    if (memchr(file_name, 0, file_name_length) != NULL) {
-      ALOGW("Zip: entry name can't contain \\0 character");
+    /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
+    if (!IsValidEntryName(file_name, file_name_length)) {
       goto bail;
     }
 
     /* add the CDE filename to the hash table */
+    ZipEntryName entry_name;
+    entry_name.name = file_name;
+    entry_name.name_length = file_name_length;
     const int add_result = AddToHash(archive->hash_table,
-        archive->hash_table_size, file_name, file_name_length);
+        archive->hash_table_size, entry_name);
     if (add_result) {
       ALOGW("Zip: Error adding entry to hash table %d", add_result);
       result = add_result;
@@ -685,21 +692,22 @@
 }
 
 int32_t OpenArchiveFd(int fd, const char* debug_file_name,
-                      ZipArchiveHandle* handle) {
-  ZipArchive* archive = new ZipArchive(fd);
+                      ZipArchiveHandle* handle, bool assume_ownership) {
+  ZipArchive* archive = new ZipArchive(fd, assume_ownership);
   *handle = archive;
   return OpenArchiveInternal(archive, debug_file_name);
 }
 
 int32_t OpenArchive(const char* fileName, ZipArchiveHandle* handle) {
   const int fd = open(fileName, O_RDONLY | O_BINARY, 0);
-  ZipArchive* archive = new ZipArchive(fd);
+  ZipArchive* archive = new ZipArchive(fd, true);
   *handle = archive;
 
   if (fd < 0) {
     ALOGW("Unable to open '%s': %s", fileName, strerror(errno));
     return kIoError;
   }
+
   return OpenArchiveInternal(archive, fileName);
 }
 
@@ -739,7 +747,7 @@
 // as a side effect of this call.
 static inline ssize_t ReadAtOffset(int fd, uint8_t* buf, size_t len,
                                    off64_t off) {
-#ifdef HAVE_PREAD
+#if !defined(_WIN32)
   return TEMP_FAILURE_RETRY(pread64(fd, buf, len, off));
 #else
   // The only supported platform that doesn't support pread at the moment
@@ -751,18 +759,17 @@
   }
 
   return TEMP_FAILURE_RETRY(read(fd, buf, len));
-#endif  // HAVE_PREAD
+#endif
 }
 
 static int32_t FindEntry(const ZipArchive* archive, const int ent,
                          ZipEntry* data) {
   const uint16_t nameLen = archive->hash_table[ent].name_length;
-  const char* name = archive->hash_table[ent].name;
 
   // Recover the start of the central directory entry from the filename
   // pointer.  The filename is the first entry past the fixed-size data,
   // so we can just subtract back from that.
-  const uint8_t* ptr = reinterpret_cast<const uint8_t*>(name);
+  const uint8_t* ptr = archive->hash_table[ent].name;
   ptr -= sizeof(CentralDirectoryRecord);
 
   // This is the base of our mmapped region, we have to sanity check that
@@ -853,7 +860,7 @@
       return kIoError;
     }
 
-    if (memcmp(name, name_buf, nameLen)) {
+    if (memcmp(archive->hash_table[ent].name, name_buf, nameLen)) {
       free(name_buf);
       return kInconsistentInformation;
     }
@@ -890,12 +897,28 @@
 
 struct IterationHandle {
   uint32_t position;
-  const char* prefix;
+  // We're not using vector here because this code is used in the Windows SDK
+  // where the STL is not available.
+  const uint8_t* prefix;
   uint16_t prefix_len;
   ZipArchive* archive;
+
+  IterationHandle() : prefix(NULL), prefix_len(0) {}
+
+  IterationHandle(const ZipEntryName& prefix_name)
+      : prefix_len(prefix_name.name_length) {
+    uint8_t* prefix_copy = new uint8_t[prefix_len];
+    memcpy(prefix_copy, prefix_name.name, prefix_len);
+    prefix = prefix_copy;
+  }
+
+  ~IterationHandle() {
+    delete[] prefix;
+  }
 };
 
-int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr, const char* prefix) {
+int32_t StartIteration(ZipArchiveHandle handle, void** cookie_ptr,
+                       const ZipEntryName* optional_prefix) {
   ZipArchive* archive = (ZipArchive *) handle;
 
   if (archive == NULL || archive->hash_table == NULL) {
@@ -903,32 +926,32 @@
     return kInvalidHandle;
   }
 
-  IterationHandle* cookie = (IterationHandle*) malloc(sizeof(IterationHandle));
+  IterationHandle* cookie =
+      optional_prefix != NULL ? new IterationHandle(*optional_prefix) : new IterationHandle();
   cookie->position = 0;
-  cookie->prefix = prefix;
   cookie->archive = archive;
-  if (prefix != NULL) {
-    cookie->prefix_len = strlen(prefix);
-  }
 
   *cookie_ptr = cookie ;
   return 0;
 }
 
-int32_t FindEntry(const ZipArchiveHandle handle, const char* entryName,
+void EndIteration(void* cookie) {
+  delete reinterpret_cast<IterationHandle*>(cookie);
+}
+
+int32_t FindEntry(const ZipArchiveHandle handle, const ZipEntryName& entryName,
                   ZipEntry* data) {
   const ZipArchive* archive = (ZipArchive*) handle;
-  const int nameLen = strlen(entryName);
-  if (nameLen == 0 || nameLen > 65535) {
-    ALOGW("Zip: Invalid filename %s", entryName);
+  if (entryName.name_length == 0) {
+    ALOGW("Zip: Invalid filename %.*s", entryName.name_length, entryName.name);
     return kInvalidEntryName;
   }
 
   const int64_t ent = EntryToIndex(archive->hash_table,
-    archive->hash_table_size, entryName, nameLen);
+    archive->hash_table_size, entryName);
 
   if (ent < 0) {
-    ALOGV("Zip: Could not find entry %.*s", nameLen, entryName);
+    ALOGV("Zip: Could not find entry %.*s", entryName.name_length, entryName.name);
     return ent;
   }
 
@@ -953,7 +976,7 @@
 
   for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
     if (hash_table[i].name != NULL &&
-        (handle->prefix == NULL ||
+        (handle->prefix_len == 0 ||
          (memcmp(handle->prefix, hash_table[i].name, handle->prefix_len) == 0))) {
       handle->position = (i + 1);
       const int error = FindEntry(archive, i, data);
diff --git a/libziparchive/zip_archive_test.cc b/libziparchive/zip_archive_test.cc
index 813a87f..c8dafa9 100644
--- a/libziparchive/zip_archive_test.cc
+++ b/libziparchive/zip_archive_test.cc
@@ -17,6 +17,7 @@
 #include "ziparchive/zip_archive.h"
 
 #include <errno.h>
+#include <fcntl.h>
 #include <getopt.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -40,6 +41,27 @@
   '\n'
 };
 
+static const uint16_t kATxtNameLength = 5;
+static const uint16_t kBTxtNameLength = 5;
+static const uint16_t kNonexistentTxtNameLength = 15;
+static const uint16_t kEmptyTxtNameLength = 9;
+
+static const uint8_t kATxtName[kATxtNameLength] = {
+  'a', '.', 't', 'x', 't'
+};
+
+static const uint8_t kBTxtName[kBTxtNameLength] = {
+  'b', '.', 't', 'x', 't'
+};
+
+static const uint8_t kNonexistentTxtName[kNonexistentTxtNameLength] = {
+  'n', 'o', 'n', 'e', 'x', 'i', 's', 't', 'e', 'n', 't', '.', 't', 'x' ,'t'
+};
+
+static const uint8_t kEmptyTxtName[kEmptyTxtNameLength] = {
+  'e', 'm', 'p', 't', 'y', '.', 't', 'x', 't'
+};
+
 static int32_t OpenArchiveWrapper(const std::string& name,
                                   ZipArchiveHandle* handle) {
   const std::string abs_path = test_data_dir + "/" + name;
@@ -67,6 +89,26 @@
   ASSERT_EQ(-1, GetFileDescriptor(handle));
 }
 
+TEST(ziparchive, OpenAssumeFdOwnership) {
+  int fd = open((test_data_dir + "/" + kValidZip).c_str(), O_RDONLY);
+  ASSERT_NE(-1, fd);
+  ZipArchiveHandle handle;
+  ASSERT_EQ(0, OpenArchiveFd(fd, "OpenWithAssumeFdOwnership", &handle));
+  CloseArchive(handle);
+  ASSERT_EQ(-1, lseek(fd, 0, SEEK_SET));
+  ASSERT_EQ(EBADF, errno);
+}
+
+TEST(ziparchive, OpenDoNotAssumeFdOwnership) {
+  int fd = open((test_data_dir + "/" + kValidZip).c_str(), O_RDONLY);
+  ASSERT_NE(-1, fd);
+  ZipArchiveHandle handle;
+  ASSERT_EQ(0, OpenArchiveFd(fd, "OpenWithAssumeFdOwnership", &handle, false));
+  CloseArchive(handle);
+  ASSERT_EQ(0, lseek(fd, 0, SEEK_SET));
+  close(fd);
+}
+
 TEST(ziparchive, Iteration) {
   ZipArchiveHandle handle;
   ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
@@ -108,7 +150,10 @@
   ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
 
   ZipEntry data;
-  ASSERT_EQ(0, FindEntry(handle, "a.txt", &data));
+  ZipEntryName name;
+  name.name = kATxtName;
+  name.name_length = kATxtNameLength;
+  ASSERT_EQ(0, FindEntry(handle, name, &data));
 
   // Known facts about a.txt, from zipinfo -v.
   ASSERT_EQ(63, data.offset);
@@ -118,7 +163,10 @@
   ASSERT_EQ(0x950821c5, data.crc32);
 
   // An entry that doesn't exist. Should be a negative return code.
-  ASSERT_LT(FindEntry(handle, "nonexistent.txt", &data), 0);
+  ZipEntryName absent_name;
+  absent_name.name = kNonexistentTxtName;
+  absent_name.name_length = kNonexistentTxtNameLength;
+  ASSERT_LT(FindEntry(handle, absent_name, &data), 0);
 
   CloseArchive(handle);
 }
@@ -129,7 +177,10 @@
 
   // An entry that's deflated.
   ZipEntry data;
-  ASSERT_EQ(0, FindEntry(handle, "a.txt", &data));
+  ZipEntryName a_name;
+  a_name.name = kATxtName;
+  a_name.name_length = kATxtNameLength;
+  ASSERT_EQ(0, FindEntry(handle, a_name, &data));
   const uint32_t a_size = data.uncompressed_length;
   ASSERT_EQ(a_size, sizeof(kATxtContents));
   uint8_t* buffer = new uint8_t[a_size];
@@ -138,7 +189,10 @@
   delete[] buffer;
 
   // An entry that's stored.
-  ASSERT_EQ(0, FindEntry(handle, "b.txt", &data));
+  ZipEntryName b_name;
+  b_name.name = kBTxtName;
+  b_name.name_length = kBTxtNameLength;
+  ASSERT_EQ(0, FindEntry(handle, b_name, &data));
   const uint32_t b_size = data.uncompressed_length;
   ASSERT_EQ(b_size, sizeof(kBTxtContents));
   buffer = new uint8_t[b_size];
@@ -184,7 +238,10 @@
   ASSERT_EQ(0, OpenArchiveFd(fd, "EmptyEntriesTest", &handle));
 
   ZipEntry entry;
-  ASSERT_EQ(0, FindEntry(handle, "empty.txt", &entry));
+  ZipEntryName empty_name;
+  empty_name.name = kEmptyTxtName;
+  empty_name.name_length = kEmptyTxtNameLength;
+  ASSERT_EQ(0, FindEntry(handle, empty_name, &entry));
   ASSERT_EQ(static_cast<uint32_t>(0), entry.uncompressed_length);
   uint8_t buffer[1];
   ASSERT_EQ(0, ExtractToMemory(handle, &entry, buffer, 1));
@@ -231,7 +288,10 @@
   ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
 
   ZipEntry entry;
-  ASSERT_EQ(0, FindEntry(handle, "a.txt", &entry));
+  ZipEntryName name;
+  name.name = kATxtName;
+  name.name_length = kATxtNameLength;
+  ASSERT_EQ(0, FindEntry(handle, name, &entry));
   ASSERT_EQ(0, ExtractEntryToFile(handle, &entry, fd));
 
 
diff --git a/libzipfile/Android.mk b/libzipfile/Android.mk
index 12a2229..f054e15 100644
--- a/libzipfile/Android.mk
+++ b/libzipfile/Android.mk
@@ -7,13 +7,10 @@
 	centraldir.c \
 	zipfile.c
 
-LOCAL_STATIC_LIBRARIES := \
-	libunz
+LOCAL_STATIC_LIBRARIES := libz
 
 LOCAL_MODULE:= libzipfile
 
-LOCAL_C_INCLUDES += external/zlib
-
 LOCAL_CFLAGS := -Werror
 
 LOCAL_MULTILIB := both
@@ -27,13 +24,10 @@
 	centraldir.c \
 	zipfile.c
 
-LOCAL_STATIC_LIBRARIES := \
-	libunz
+LOCAL_STATIC_LIBRARIES := libz
 
 LOCAL_MODULE:= libzipfile
 
-LOCAL_C_INCLUDES += external/zlib
-
 LOCAL_CFLAGS := -Werror
 
 include $(BUILD_STATIC_LIBRARY)
@@ -45,12 +39,10 @@
 LOCAL_SRC_FILES:= \
 	test_zipfile.c
 
-LOCAL_STATIC_LIBRARIES := libzipfile libunz
+LOCAL_STATIC_LIBRARIES := libzipfile libz
 
 LOCAL_MODULE := test_zipfile
 
-LOCAL_C_INCLUDES += external/zlib
-
 LOCAL_CFLAGS := -Werror
 
 include $(BUILD_HOST_EXECUTABLE)
diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c
index a534a24..7bbc811 100644
--- a/lmkd/lmkd.c
+++ b/lmkd/lmkd.c
@@ -607,7 +607,6 @@
 static int find_and_kill_process(int other_free, int other_file, bool first)
 {
     int i;
-    int r;
     int min_score_adj = OOM_ADJUST_MAX + 1;
     int minfree = 0;
     int killed_size = 0;
@@ -643,7 +642,6 @@
 }
 
 static void mp_event(uint32_t events __unused) {
-    int i;
     int ret;
     unsigned long long evcount;
     struct sysmeminfo mi;
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp
index 858e56c..79f2ebd 100644
--- a/logcat/logcat.cpp
+++ b/logcat/logcat.cpp
@@ -4,6 +4,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -80,15 +81,20 @@
 
     close(g_outFD);
 
+    // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
+    // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
+    int maxRotationCountDigits =
+            (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
+
     for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
         char *file0, *file1;
 
-        asprintf(&file1, "%s.%d", g_outputFileName, i);
+        asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
 
         if (i - 1 == 0) {
             asprintf(&file0, "%s", g_outputFileName);
         } else {
-            asprintf(&file0, "%s.%d", g_outputFileName, i - 1);
+            asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
         }
 
         err = rename (file0, file1);
@@ -219,8 +225,8 @@
                     "  -f <filename>   Log to file. Default to stdout\n"
                     "  -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f\n"
                     "  -n <count>      Sets max number of rotated logs to <count>, default 4\n"
-                    "  -v <format>     Sets the log print format, where <format> is one of:\n\n"
-                    "                  brief process tag thread raw time threadtime long\n\n"
+                    "  -v <format>     Sets the log print format, where <format> is:\n\n"
+                    "                  brief color long process raw tag thread threadtime time\n\n"
                     "  -c              clear (flush) the entire log and exit\n"
                     "  -d              dump the log and then exit (don't block)\n"
                     "  -t <count>      print only the most recent <count> lines (implies -d)\n"
@@ -259,7 +265,7 @@
                    "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
                    "If no filterspec is found, filter defaults to '*:I'\n"
                    "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
-                   "or defaults to \"brief\"\n\n");
+                   "or defaults to \"threadtime\"\n\n");
 
 
 
@@ -449,36 +455,31 @@
                         delete dev;
                     }
 
-                    dev = devices = new log_device_t("main", false, 'm');
-                    android::g_devCount = 1;
-                    if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
-                        dev->next = new log_device_t("system", false, 's');
-                        if (dev->next) {
-                            dev = dev->next;
-                            android::g_devCount++;
+                    devices = dev = NULL;
+                    android::g_devCount = 0;
+                    needBinary = false;
+                    for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
+                        const char *name = android_log_id_to_name((log_id_t)i);
+                        log_id_t log_id = android_name_to_log_id(name);
+
+                        if (log_id != (log_id_t)i) {
+                            continue;
                         }
-                    }
-                    if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
-                        dev->next = new log_device_t("radio", false, 'r');
-                        if (dev->next) {
-                            dev = dev->next;
-                            android::g_devCount++;
+
+                        bool binary = strcmp(name, "events") == 0;
+                        log_device_t* d = new log_device_t(name, binary, *name);
+
+                        if (dev) {
+                            dev->next = d;
+                            dev = d;
+                        } else {
+                            devices = dev = d;
                         }
-                    }
-                    if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
-                        dev->next = new log_device_t("events", true, 'e');
-                        if (dev->next) {
-                            dev = dev->next;
-                            android::g_devCount++;
+                        android::g_devCount++;
+                        if (binary) {
                             needBinary = true;
                         }
                     }
-                    if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
-                        dev->next = new log_device_t("crash", false, 'c');
-                        if (dev->next) {
-                            android::g_devCount++;
-                        }
-                    }
                     break;
                 }
 
@@ -543,7 +544,9 @@
                     exit(-1);
                 }
 
-                hasSetLogFormat = 1;
+                if (strcmp("color", optarg)) { // exception for modifiers
+                    hasSetLogFormat = 1;
+                }
             break;
 
             case 'Q':
@@ -653,11 +656,12 @@
 
         if (logFormat != NULL) {
             err = setLogFormat(logFormat);
-
             if (err < 0) {
                 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
                                     logFormat);
             }
+        } else {
+            setLogFormat("threadtime");
         }
     }
 
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp
index 85756d5..b358485 100644
--- a/logcat/tests/logcat_test.cpp
+++ b/logcat/tests/logcat_test.cpp
@@ -243,7 +243,7 @@
 
     FILE *fp;
     ASSERT_TRUE(NULL != (fp = popen(
-      "logcat -b events -t 100 2>/dev/null",
+      "logcat -v brief -b events -t 100 2>/dev/null",
       "r")));
 
     char buffer[5120];
@@ -275,7 +275,7 @@
 
     // NB: crash log only available in user space
     ASSERT_TRUE(NULL != (fp = popen(
-      "logcat -b radio -b events -b system -b main -g 2>/dev/null",
+      "logcat -v brief -b radio -b events -b system -b main -g 2>/dev/null",
       "r")));
 
     char buffer[5120];
@@ -364,7 +364,7 @@
 
     ASSERT_TRUE(NULL != (fp = popen(
       "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&"
-      " logcat -b events 2>&1",
+      " logcat -v brief -b events 2>&1",
       "r")));
 
     char buffer[5120];
@@ -433,7 +433,7 @@
 
     ASSERT_TRUE(NULL != (fp = popen(
       "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&"
-      " logcat -b events -T 5 2>&1",
+      " logcat -v brief -b events -T 5 2>&1",
       "r")));
 
     char buffer[5120];
@@ -503,10 +503,14 @@
             int count = 0;
 
             while (fgets(buffer, sizeof(buffer), fp)) {
-                static const char match[] = "4 log.txt";
+                static const char match_1[] = "4 log.txt";
+                static const char match_2[] = "8 log.txt";
+                static const char match_3[] = "16 log.txt";
                 static const char total[] = "total ";
 
-                if (!strncmp(buffer, match, sizeof(match) - 1)) {
+                if (!strncmp(buffer, match_1, sizeof(match_1) - 1)
+                 || !strncmp(buffer, match_2, sizeof(match_2) - 1)
+                 || !strncmp(buffer, match_3, sizeof(match_3) - 1)) {
                     ++count;
                 } else if (strncmp(buffer, total, sizeof(total) - 1)) {
                     fprintf(stderr, "WARNING: Parse error: %s", buffer);
@@ -520,6 +524,59 @@
     EXPECT_FALSE(system(command));
 }
 
+TEST(logcat, logrotate_suffix) {
+    static const char tmp_out_dir_form[] = "/data/local/tmp/logcat.logrotate.XXXXXX";
+    char tmp_out_dir[sizeof(tmp_out_dir_form)];
+    ASSERT_TRUE(NULL != mkdtemp(strcpy(tmp_out_dir, tmp_out_dir_form)));
+
+    static const char logcat_cmd[] = "logcat -b radio -b events -b system -b main"
+                                     " -d -f %s/log.txt -n 10 -r 1";
+    char command[sizeof(tmp_out_dir) + sizeof(logcat_cmd)];
+    sprintf(command, logcat_cmd, tmp_out_dir);
+
+    int ret;
+    EXPECT_FALSE((ret = system(command)));
+    if (!ret) {
+        sprintf(command, "ls %s 2>/dev/null", tmp_out_dir);
+
+        FILE *fp;
+        EXPECT_TRUE(NULL != (fp = popen(command, "r")));
+        char buffer[5120];
+        int log_file_count = 0;
+
+        while (fgets(buffer, sizeof(buffer), fp)) {
+            static const char rotated_log_filename_prefix[] = "log.txt.";
+            static const size_t rotated_log_filename_prefix_len =
+                strlen(rotated_log_filename_prefix);
+            static const char log_filename[] = "log.txt";
+
+            if (!strncmp(buffer, rotated_log_filename_prefix, rotated_log_filename_prefix_len)) {
+              // Rotated file should have form log.txt.##
+              char* rotated_log_filename_suffix = buffer + rotated_log_filename_prefix_len;
+              char* endptr;
+              const long int suffix_value = strtol(rotated_log_filename_suffix, &endptr, 10);
+              EXPECT_EQ(rotated_log_filename_suffix + 2, endptr);
+              EXPECT_LE(suffix_value, 10);
+              EXPECT_GT(suffix_value, 0);
+              ++log_file_count;
+              continue;
+            }
+
+            if (!strncmp(buffer, log_filename, strlen(log_filename))) {
+              ++log_file_count;
+              continue;
+            }
+
+            fprintf(stderr, "ERROR: Unexpected file: %s", buffer);
+            ADD_FAILURE();
+        }
+        pclose(fp);
+        EXPECT_EQ(11, log_file_count);
+    }
+    sprintf(command, "rm -rf %s", tmp_out_dir);
+    EXPECT_FALSE(system(command));
+}
+
 static void caught_blocking_clear(int /*signum*/)
 {
     unsigned long long v = 0xDEADBEEFA55C0000ULL;
@@ -542,7 +599,7 @@
     ASSERT_TRUE(NULL != (fp = popen(
       "( trap exit HUP QUIT INT PIPE KILL ; sleep 6; echo DONE )&"
       " logcat -b events -c 2>&1 ;"
-      " logcat -b events 2>&1",
+      " logcat -v brief -b events 2>&1",
       "r")));
 
     char buffer[5120];
diff --git a/logd/LogAudit.cpp b/logd/LogAudit.cpp
index ee2f32d..c7c0249 100644
--- a/logd/LogAudit.cpp
+++ b/logd/LogAudit.cpp
@@ -19,7 +19,6 @@
 #include <limits.h>
 #include <stdarg.h>
 #include <stdlib.h>
-#include <sys/klog.h>
 #include <sys/prctl.h>
 #include <sys/uio.h>
 #include <syslog.h>
@@ -33,21 +32,23 @@
     '0' + (LOG_AUTH | (PRI)) % 10, \
     '>'
 
-LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmsg)
+LogAudit::LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg)
         : SocketListener(getLogSocket(), false)
         , logbuf(buf)
         , reader(reader)
-        , fdDmesg(-1) {
+        , fdDmesg(fdDmesg)
+        , initialized(false) {
     static const char auditd_message[] = { KMSG_PRIORITY(LOG_INFO),
         'l', 'o', 'g', 'd', '.', 'a', 'u', 'd', 'i', 't', 'd', ':',
         ' ', 's', 't', 'a', 'r', 't', '\n' };
-    write(fdDmsg, auditd_message, sizeof(auditd_message));
-    logDmesg();
-    fdDmesg = fdDmsg;
+    write(fdDmesg, auditd_message, sizeof(auditd_message));
 }
 
 bool LogAudit::onDataAvailable(SocketClient *cli) {
-    prctl(PR_SET_NAME, "logd.auditd");
+    if (!initialized) {
+        prctl(PR_SET_NAME, "logd.auditd");
+        initialized = true;
+    }
 
     struct audit_message rep;
 
@@ -60,7 +61,8 @@
         return false;
     }
 
-    logPrint("type=%d %.*s", rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
+    logPrint("type=%d %.*s",
+        rep.nlh.nlmsg_type, rep.nlh.nlmsg_len, rep.data);
 
     return true;
 }
@@ -87,7 +89,7 @@
     }
 
     bool info = strstr(str, " permissive=1") || strstr(str, " policy loaded ");
-    if (fdDmesg >= 0) {
+    if ((fdDmesg >= 0) && initialized) {
         struct iovec iov[3];
         static const char log_info[] = { KMSG_PRIORITY(LOG_INFO) };
         static const char log_warning[] = { KMSG_PRIORITY(LOG_WARNING) };
@@ -213,34 +215,23 @@
     return rc;
 }
 
-void LogAudit::logDmesg() {
-    int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
-    if (len <= 0) {
-        return;
+int LogAudit::log(char *buf) {
+    char *audit = strstr(buf, " audit(");
+    if (!audit) {
+        return 0;
     }
 
-    len++;
-    char buf[len];
+    *audit = '\0';
 
-    int rc = klogctl(KLOG_READ_ALL, buf, len);
-
-    buf[len - 1] = '\0';
-
-    for(char *tok = buf; (rc >= 0) && ((tok = strtok(tok, "\r\n"))); tok = NULL) {
-        char *audit = strstr(tok, " audit(");
-        if (!audit) {
-            continue;
-        }
-
-        *audit++ = '\0';
-
-        char *type = strstr(tok, "type=");
-        if (type) {
-            rc = logPrint("%s %s", type, audit);
-        } else {
-            rc = logPrint("%s", audit);
-        }
+    int rc;
+    char *type = strstr(buf, "type=");
+    if (type) {
+        rc = logPrint("%s %s", type, audit + 1);
+    } else {
+        rc = logPrint("%s", audit + 1);
     }
+    *audit = ' ';
+    return rc;
 }
 
 int LogAudit::getLogSocket() {
diff --git a/logd/LogAudit.h b/logd/LogAudit.h
index 111030a..f977be9 100644
--- a/logd/LogAudit.h
+++ b/logd/LogAudit.h
@@ -24,16 +24,17 @@
     LogBuffer *logbuf;
     LogReader *reader;
     int fdDmesg;
+    bool initialized;
 
 public:
     LogAudit(LogBuffer *buf, LogReader *reader, int fdDmesg);
+    int log(char *buf);
 
 protected:
     virtual bool onDataAvailable(SocketClient *cli);
 
 private:
     static int getLogSocket();
-    void logDmesg();
     int logPrint(const char *fmt, ...)
         __attribute__ ((__format__ (__printf__, 2, 3)));
 };
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index 8c1c344..3d0b38f 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -147,7 +147,8 @@
     //  NB: if end is region locked, place element at end of list
     LogBufferElementCollection::iterator it = mLogElements.end();
     LogBufferElementCollection::iterator last = it;
-    while (--it != mLogElements.begin()) {
+    while (last != mLogElements.begin()) {
+        --it;
         if ((*it)->getRealTime() <= realtime) {
             // halves the peak performance, use with caution
             if (dgramQlenStatistics) {
@@ -241,7 +242,7 @@
     LastLogTimes::iterator t = mTimes.begin();
     while(t != mTimes.end()) {
         LogTimeEntry *entry = (*t);
-        if (entry->owned_Locked()
+        if (entry->owned_Locked() && entry->isWatching(id)
                 && (!oldest || (oldest->mStart > entry->mStart))) {
             oldest = entry;
         }
@@ -353,7 +354,7 @@
                         // kick a misbehaving log reader client off the island
                         oldest->release_Locked();
                     } else {
-                        oldest->triggerSkip_Locked(pruneRows);
+                        oldest->triggerSkip_Locked(id, pruneRows);
                     }
                 }
                 break;
@@ -384,7 +385,7 @@
                         // kick a misbehaving log reader client off the island
                         oldest->release_Locked();
                     } else {
-                        oldest->triggerSkip_Locked(pruneRows);
+                        oldest->triggerSkip_Locked(id, pruneRows);
                     }
                     break;
                 }
@@ -444,7 +445,24 @@
     uid_t uid = reader->getUid();
 
     pthread_mutex_lock(&mLogElementsLock);
-    for (it = mLogElements.begin(); it != mLogElements.end(); ++it) {
+
+    if (start == LogTimeEntry::EPOCH) {
+        // client wants to start from the beginning
+        it = mLogElements.begin();
+    } else {
+        // Client wants to start from some specified time. Chances are
+        // we are better off starting from the end of the time sorted list.
+        for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) {
+            --it;
+            LogBufferElement *element = *it;
+            if (element->getMonotonicTime() <= start) {
+                it++;
+                break;
+            }
+        }
+    }
+
+    for (; it != mLogElements.end(); ++it) {
         LogBufferElement *element = *it;
 
         if (!privileged && (element->getUid() != uid)) {
diff --git a/logd/LogListener.cpp b/logd/LogListener.cpp
index 8186cea..fc9e30f 100644
--- a/logd/LogListener.cpp
+++ b/logd/LogListener.cpp
@@ -23,6 +23,7 @@
 
 #include <cutils/sockets.h>
 #include <log/logger.h>
+#include <private/android_logger.h>
 
 #include "LogListener.h"
 
@@ -54,7 +55,7 @@
     int socket = cli->getSocket();
 
     ssize_t n = recvmsg(socket, &hdr, 0);
-    if (n <= (ssize_t)(sizeof_log_id_t + sizeof(uint16_t) + sizeof(log_time))) {
+    if (n <= (ssize_t)(sizeof(android_log_header_t))) {
         return false;
     }
 
@@ -81,28 +82,19 @@
         return false;
     }
 
-    // First log element is always log_id.
-    log_id_t log_id = (log_id_t) *((typeof_log_id_t *) buffer);
-    if (log_id < 0 || log_id >= LOG_ID_MAX) {
+    android_log_header_t *header = reinterpret_cast<android_log_header_t *>(buffer);
+    if (/* header->id < LOG_ID_MIN || */ header->id >= LOG_ID_MAX) {
         return false;
     }
-    char *msg = ((char *)buffer) + sizeof_log_id_t;
-    n -= sizeof_log_id_t;
 
-    // second element is the thread id of the caller
-    pid_t tid = (pid_t) *((uint16_t *) msg);
-    msg += sizeof(uint16_t);
-    n -= sizeof(uint16_t);
-
-    // third element is the realtime at point of caller
-    log_time realtime(msg);
-    msg += sizeof(log_time);
-    n -= sizeof(log_time);
+    char *msg = ((char *)buffer) + sizeof(android_log_header_t);
+    n -= sizeof(android_log_header_t);
 
     // NB: hdr.msg_flags & MSG_TRUNC is not tested, silently passing a
     // truncated message to the logs.
 
-    logbuf->log(log_id, realtime, cred->uid, cred->pid, tid, msg,
+    logbuf->log((log_id_t)header->id, header->realtime,
+        cred->uid, cred->pid, header->tid, msg,
         ((size_t) n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
     reader->notifyNewLog();
 
diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp
index 2a45590..53036e6 100644
--- a/logd/LogStatistics.cpp
+++ b/logd/LogStatistics.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <fcntl.h>
+#include <malloc.h>
 #include <stdarg.h>
 #include <time.h>
 
@@ -690,8 +691,11 @@
             size_t sizesTotal = p->sizesTotal();
 
             android::String8 sz("");
-            sz.appendFormat((sizes != sizesTotal) ? "%zu/%zu" : "%zu",
-                            sizes, sizesTotal);
+            if (sizes == sizesTotal) {
+                sz.appendFormat("%zu", sizes);
+            } else {
+                sz.appendFormat("%zu/%zu", sizes, sizesTotal);
+            }
 
             android::String8 pd("");
             pd.appendFormat("%u%c", pid, p->pidGone() ? '?' : ' ');
@@ -802,12 +806,15 @@
             PidStatistics *pp = *pt;
             pid_t p = pp->getPid();
 
-            intermediate = string.format(oneline
-                                             ? ((p == PidStatistics::gone)
-                                                 ? "%d/?"
-                                                 : "%d/%d%c")
-                                             : "%d",
-                                         u, p, pp->pidGone() ? '?' : '\0');
+            if (!oneline) {
+                intermediate = string.format("%d", u);
+            } else if (p == PidStatistics::gone) {
+                intermediate = string.format("%d/?", u);
+            } else if (pp->pidGone()) {
+                intermediate = string.format("%d/%d?", u, p);
+            } else {
+                intermediate = string.format("%d/%d", u, p);
+            }
             string.appendFormat(first ? "\n%-12s" : "%-12s",
                                 intermediate.string());
             intermediate.clear();
diff --git a/logd/LogTimes.cpp b/logd/LogTimes.cpp
index ea4e8c8..5f9db8d 100644
--- a/logd/LogTimes.cpp
+++ b/logd/LogTimes.cpp
@@ -36,7 +36,6 @@
         , mReader(reader)
         , mLogMask(logMask)
         , mPid(pid)
-        , skipAhead(0)
         , mCount(0)
         , mTail(tail)
         , mIndex(0)
@@ -46,6 +45,7 @@
         , mEnd(CLOCK_MONOTONIC)
 {
         pthread_cond_init(&threadTriggeredCondition, NULL);
+        cleanSkip_Locked();
 }
 
 void LogTimeEntry::startReader_Locked(void) {
@@ -148,6 +148,8 @@
             break;
         }
 
+        me->cleanSkip_Locked();
+
         pthread_cond_wait(&me->threadTriggeredCondition, &timesLock);
     }
 
@@ -169,7 +171,7 @@
     }
 
     if ((!me->mPid || (me->mPid == element->getPid()))
-            && (me->mLogMask & (1 << element->getLogId()))) {
+            && (me->isWatching(element->getLogId()))) {
         ++me->mCount;
     }
 
@@ -184,19 +186,19 @@
 
     LogTimeEntry::lock();
 
-    if (me->skipAhead) {
-        me->skipAhead--;
+    me->mStart = element->getMonotonicTime();
+
+    if (me->skipAhead[element->getLogId()]) {
+        me->skipAhead[element->getLogId()]--;
         goto skip;
     }
 
-    me->mStart = element->getMonotonicTime();
-
     // Truncate to close race between first and second pass
     if (me->mNonBlock && me->mTail && (me->mIndex >= me->mCount)) {
         goto skip;
     }
 
-    if ((me->mLogMask & (1 << element->getLogId())) == 0) {
+    if (!me->isWatching(element->getLogId())) {
         goto skip;
     }
 
@@ -223,7 +225,7 @@
     }
 
 ok:
-    if (!me->skipAhead) {
+    if (!me->skipAhead[element->getLogId()]) {
         LogTimeEntry::unlock();
         return true;
     }
@@ -233,3 +235,9 @@
     LogTimeEntry::unlock();
     return false;
 }
+
+void LogTimeEntry::cleanSkip_Locked(void) {
+    for (log_id_t i = LOG_ID_MIN; i < LOG_ID_MAX; i = (log_id_t) (i + 1)) {
+        skipAhead[i] = 0;
+    }
+}
diff --git a/logd/LogTimes.h b/logd/LogTimes.h
index 0bfa7a2..81aedfb 100644
--- a/logd/LogTimes.h
+++ b/logd/LogTimes.h
@@ -22,6 +22,7 @@
 #include <sys/types.h>
 #include <sysutils/SocketClient.h>
 #include <utils/List.h>
+#include <log/log.h>
 
 class LogReader;
 
@@ -38,7 +39,7 @@
     static void threadStop(void *me);
     const unsigned int mLogMask;
     const pid_t mPid;
-    unsigned int skipAhead;
+    unsigned int skipAhead[LOG_ID_MAX];
     unsigned long mCount;
     unsigned long mTail;
     unsigned long mIndex;
@@ -67,7 +68,8 @@
         pthread_cond_signal(&threadTriggeredCondition);
     }
 
-    void triggerSkip_Locked(unsigned int skip) { skipAhead = skip; }
+    void triggerSkip_Locked(log_id_t id, unsigned int skip) { skipAhead[id] = skip; }
+    void cleanSkip_Locked(void);
 
     // Called after LogTimeEntry removed from list, lock implicitly held
     void release_Locked(void) {
@@ -99,7 +101,7 @@
         // No one else is holding a reference to this
         delete this;
     }
-
+    bool isWatching(log_id_t id) { return (mLogMask & (1<<id)) != 0; }
     // flushTo filter callbacks
     static bool FilterFirstPass(const LogBufferElement *element, void *me);
     static bool FilterSecondPass(const LogBufferElement *element, void *me);
diff --git a/logd/LogWhiteBlackList.cpp b/logd/LogWhiteBlackList.cpp
index e87b604..6910854 100644
--- a/logd/LogWhiteBlackList.cpp
+++ b/logd/LogWhiteBlackList.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <ctype.h>
+#include <malloc.h>
 
 #include <utils/String8.h>
 
@@ -39,10 +40,15 @@
 
 void Prune::format(char **strp) {
     if (mUid != uid_all) {
-        asprintf(strp, (mPid != pid_all) ? "%u/%u" : "%u", mUid, mPid);
-    } else {
-        // NB: mPid == pid_all can not happen if mUid == uid_all
-        asprintf(strp, (mPid != pid_all) ? "/%u" : "/", mPid);
+        if (mPid != pid_all) {
+            asprintf(strp, "%u/%u", mUid, mPid);
+        } else {
+            asprintf(strp, "%u", mUid);
+        }
+    } else if (mPid != pid_all) {
+        asprintf(strp, "/%u", mPid);
+    } else { // NB: mPid == pid_all can not happen if mUid == uid_all
+        asprintf(strp, "/");
     }
 }
 
diff --git a/logd/main.cpp b/logd/main.cpp
index 54da7e3..7a1ae54 100644
--- a/logd/main.cpp
+++ b/logd/main.cpp
@@ -22,12 +22,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/capability.h>
+#include <sys/klog.h>
 #include <sys/prctl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 #include <cutils/properties.h>
+#include <cutils/sched_policy.h>
 
 #include "private/android_filesystem_config.h"
 #include "CommandListener.h"
@@ -68,6 +70,10 @@
     struct sched_param param;
     memset(&param, 0, sizeof(param));
 
+    if (set_sched_policy(0, SP_BACKGROUND) < 0) {
+        return -1;
+    }
+
     if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
         return -1;
     }
@@ -195,6 +201,23 @@
     if (auditd) {
         // failure is an option ... messages are in dmesg (required by standard)
         LogAudit *al = new LogAudit(logBuf, reader, fdDmesg);
+
+        int len = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
+        if (len > 0) {
+            len++;
+            char buf[len];
+
+            int rc = klogctl(KLOG_READ_ALL, buf, len);
+
+            buf[len - 1] = '\0';
+
+            for(char *ptr, *tok = buf;
+                    (rc >= 0) && ((tok = strtok_r(tok, "\r\n", &ptr)));
+                    tok = NULL) {
+                rc = al->log(tok);
+            }
+        }
+
         if (al->startListener()) {
             delete al;
             close(fdDmesg);
diff --git a/logd/tests/logd_test.cpp b/logd/tests/logd_test.cpp
index 4bea4be..96877a9 100644
--- a/logd/tests/logd_test.cpp
+++ b/logd/tests/logd_test.cpp
@@ -417,7 +417,11 @@
         if (((p - cp) > 3) && !*p && ((unsigned int)(p - cp) < len)) {
             fprintf(stderr, "\"");
             while (*cp) {
-                fprintf(stderr, (*cp != '\n') ? "%c" : "\\n", *cp);
+                if (*cp != '\n') {
+                    fprintf(stderr, "%c", *cp);
+                } else {
+                    fprintf(stderr, "\\n");
+                }
                 ++cp;
                 --len;
             }
diff --git a/netcfg/Android.mk b/netcfg/Android.mk
index fc01a54..4796c11 100644
--- a/netcfg/Android.mk
+++ b/netcfg/Android.mk
@@ -1,17 +1,8 @@
-ifneq ($(BUILD_TINY_ANDROID),true)
 LOCAL_PATH:= $(call my-dir)
 
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES:= netcfg.c
 LOCAL_MODULE:= netcfg
-
-#LOCAL_FORCE_STATIC_EXECUTABLE := true
-#LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
-#LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
-#LOCAL_STATIC_LIBRARIES := libcutils libc
-
-LOCAL_SHARED_LIBRARIES := libc libnetutils
+LOCAL_SHARED_LIBRARIES := libnetutils
 LOCAL_CFLAGS := -Werror
-
 include $(BUILD_EXECUTABLE)
-endif
diff --git a/netcfg/netcfg.c b/netcfg/netcfg.c
index 4e83ba4..eec1b2f 100644
--- a/netcfg/netcfg.c
+++ b/netcfg/netcfg.c
@@ -22,14 +22,9 @@
 #include <netutils/ifc.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
-void die(const char *reason)
-{
-    perror(reason);
-    exit(1);
-}
-
-const char *ipaddr(in_addr_t addr)
+static const char *ipaddr(in_addr_t addr)
 {
     struct in_addr in_addr;
 
@@ -37,13 +32,13 @@
     return inet_ntoa(in_addr);
 }
 
-void usage(void)
+static void usage(void)
 {
-    fprintf(stderr,"usage: netcfg [<interface> {dhcp|up|down}]\n");
+    fprintf(stderr,"usage: netcfg [<interface> dhcp]\n");
     exit(1);
 }
 
-int dump_interface(const char *name)
+static int dump_interface(const char *name)
 {
     unsigned addr, flags;
     unsigned char hwbuf[ETH_ALEN];
@@ -68,7 +63,7 @@
     return 0;
 }
 
-int dump_interfaces(void)
+static int dump_interfaces(void)
 {
     DIR *d;
     struct dirent *de;
@@ -84,56 +79,11 @@
     return 0;
 }
 
-int set_hwaddr(const char *name, const char *asc) {
-    struct ether_addr *addr = ether_aton(asc);
-    if (!addr) {
-        printf("Failed to parse '%s'\n", asc);
-        return -1;
-    }
-    return ifc_set_hwaddr(name, addr->ether_addr_octet);
-}
-
-struct 
-{
-    const char *name;
-    int nargs;
-    void *func;
-} CMDS[] = {
-    { "dhcp",   1, do_dhcp },
-    { "up",     1, ifc_up },
-    { "down",   1, ifc_down },
-    { "deldefault", 1, ifc_remove_default_route },
-    { "hwaddr", 2, set_hwaddr },
-    { 0, 0, 0 },
-};
-
-static int call_func(void *_func, unsigned nargs, char **args)
-{
-    switch(nargs){
-    case 1: {
-        int (*func)(char *a0) = _func;
-        return func(args[0]);
-    }
-    case 2: {
-        int (*func)(char *a0, char *a1) = _func;
-        return func(args[0], args[1]);
-    }
-    case 3: {
-        int (*func)(char *a0, char *a1, char *a2) = _func;
-        return func(args[0], args[1], args[2]);
-    }
-    default:
-        return -1;
-    }
-}
-
 int main(int argc, char **argv)
 {
-    char *iname;
-    int n;
-    
     if(ifc_init()) {
-        die("Cannot perform requested operation");
+        perror("Cannot perform requested operation");
+        exit(1);
     }
 
     if(argc == 1) {
@@ -142,41 +92,23 @@
         return result;
     }
 
-    if(argc < 3) usage();
+    if(argc != 3) usage();
 
-    iname = argv[1];
+    char* iname = argv[1];
+    char* action = argv[2];
     if(strlen(iname) > 16) usage();
 
-    argc -= 2;
-    argv += 2;
-    while(argc > 0) {
-        for(n = 0; CMDS[n].name; n++){
-            if(!strcmp(argv[0], CMDS[n].name)) {
-                char *cmdname = argv[0];
-                int nargs = CMDS[n].nargs;
-                
-                argv[0] = iname;
-                if(argc < nargs) {
-                    fprintf(stderr, "not enough arguments for '%s'\n", cmdname);
-                    ifc_close();
-                    exit(1);
-                }
-                if(call_func(CMDS[n].func, nargs, argv)) {
-                    fprintf(stderr, "action '%s' failed (%s)\n", cmdname, strerror(errno));
-                    ifc_close();
-                    exit(1);
-                }
-                argc -= nargs;
-                argv += nargs;
-                goto done;
-            }
+    if (!strcmp(action, "dhcp")) {
+        if (do_dhcp(iname)) {
+            fprintf(stderr, "dhcp failed: %s\n", strerror(errno));
+            ifc_close();
+            exit(1);
         }
-        fprintf(stderr,"no such action '%s'\n", argv[0]);
+    } else {
+        fprintf(stderr,"no such action '%s'\n", action);
         usage();
-    done:
-        ;
     }
-    ifc_close();
 
+    ifc_close();
     return 0;
 }
diff --git a/rootdir/etc/hosts b/rootdir/etc/hosts
index 99848f6..649151c 100644
--- a/rootdir/etc/hosts
+++ b/rootdir/etc/hosts
@@ -1 +1,2 @@
-127.0.0.1		    localhost
+127.0.0.1       localhost
+::1             ip6-localhost
diff --git a/rootdir/etc/init.testmenu b/rootdir/etc/init.testmenu
deleted file mode 100755
index 7ae16d5..0000000
--- a/rootdir/etc/init.testmenu
+++ /dev/null
@@ -1,322 +0,0 @@
-#!/system/bin/sh
-
-atdev=/dev/omap_csmi_tty0
-pppdev=/dev/omap_csmi_tty1
-
-n1=`cat /data/phoneentry1 2>/dev/null`
-n2=`cat /data/phoneentry2 2>/dev/null`
-n3=`cat /data/phoneentry3 2>/dev/null`
-n1=${n1:-"*#06#"}
-n2=${n2:-"*#06#"}
-n3=${n3:-"*#06#"}
-phoneoutputpid=
-eventoutputpid=
-notifypid=
-notifytoggle=false
-pppdpid=
-powerdidletime=120
-
-# map phone specific keys
-setkey -k 0xe4 -v 0x23 # map #
-setkey -k 0xe3 -v 0x2a # map *
-setkey -k 231 -v 513 # map send to newline
-#setkey -k 0x67 -v 0x20b # map up to scroll back
-#setkey -k 0x6c -v 0x20a # map down to scroll forward
-setkey -k 0x73 -v 0x20b # map volume up to scroll back
-setkey -k 0x72 -v 0x20a # map volume down to scroll forward
-setkey -k 0x60 -v 0x211 # map PoC to next console
-
-# tuttle keys
-setkey -k 0x38 -v 0x703 # map leftalt to alt
-setkey -k 0x9b -v 0x703 # map mail to alt
-setkey -t 8 -k 0x9b -v 0x703 # map alt-mail to alt
-setkey -t 8 -k 0x10 -v 0x21 # map alt-q to !
-setkey -t 8 -k 0x11 -v 0x31 # map alt-w to 1
-setkey -t 8 -k 0x12 -v 0x32 # map alt-e to 2
-setkey -t 8 -k 0x13 -v 0x33 # map alt-r to 3
-setkey -t 8 -k 0x14 -v 0x2b # map alt-t to +
-setkey -t 8 -k 0x15 -v 0x28 # map alt-y to (
-setkey -t 8 -k 0x16 -v 0x29 # map alt-u to )
-setkey -t 8 -k 0x17 -v 0x2d # map alt-i to -
-setkey -t 8 -k 0x18 -v 0x5f # map alt-o to _
-setkey -t 8 -k 0x19 -v 0x22 # map alt-p to "
-setkey -t 8 -k 0x1e -v 0x23 # map alt-a to #
-setkey -t 8 -k 0x1f -v 0x34 # map alt-s to 4
-setkey -t 8 -k 0x20 -v 0x35 # map alt-d to 5
-setkey -t 8 -k 0x21 -v 0x36 # map alt-f to 6
-setkey -t 8 -k 0x22 -v 0x2f # map alt-g to /
-setkey -t 8 -k 0x23 -v 0x3f # map alt-h to ?
-setkey -t 8 -k 0x24 -v 0xa3 # map alt-j to pound
-setkey -t 8 -k 0x25 -v 0x24 # map alt-k to $
-setkey -t 8 -k 0x2c -v 0x2a # map alt-z to *
-setkey -t 8 -k 0x2d -v 0x37 # map alt-x to 7
-setkey -t 8 -k 0x2e -v 0x38 # map alt-c to 8
-setkey -t 8 -k 0x2f -v 0x39 # map alt-v to 9
-setkey -t 8 -k 0x30 -v 0x7c # map alt-b to |
-setkey -t 8 -k 0x31 -v 0x40 # map alt-n to @
-setkey -t 8 -k 0x32 -v 0x3d # map alt-m to =
-setkey -t 8 -k 0x33 -v 0x3b # map alt-, to ;
-setkey -t 8 -k 0x34 -v 0x3a # map alt-. to :
-setkey -t 8 -k 0x0f -v 0x30 # map alt-tab to 0
-setkey -t 8 -k 0x67 -v 0x20b # map alt-up to scroll back
-setkey -t 8 -k 0x6c -v 0x20a # map alt-down to scroll forward
-
-while true
-do
-	echo
-	echo "------------------------------"
-	echo " 1: init commands"
-	echo " 2: call commands"
-	echo " 3: misc phone"
-	echo " 4: phone debug output"
-	echo " 5: test data connection"
-	echo " 6: start runtime"
-	echo " 7: start runtime w/output"
-	echo " 8: stop runtime"
-	echo " 9: misc"
-	echo -n ": "
-	while true
-	do
-		c=`readtty -t 50 -f -a 1234567890#`
-		case "$c" in
-			"" ) ;;
-			* ) break;
-		esac
-	done
-	echo Got key -$c-
-	case $c in
-		"1" )
-			while true; do
-				echo
-				echo "------------------------------"
-				echo " 1: Print phone output"
-				echo " 2: ATQ0V1E1+CMEE=2;+CREG=0"
-				echo " 3: AT+CFUN=1"
-				echo " 4: AT+COPS=0"
-				echo " 5: AT+CREG?"
-				echo " 6: Stop phone output"
-				echo " 0: back"
-				echo -n ": "
-				c=`readtty -f -a 1234560#`
-				echo Got key -$c-
-				case "$c" in
-					"1" ) kill $phoneoutputpid; cat $atdev & phoneoutputpid=$! ;;
-					"2" ) echo -e "ATQ0V1E1+CMEE=2;+CREG=0\r" >$atdev;;
-					"3" ) echo -e "AT+CFUN=1\r" >$atdev;;
-					"4" ) echo -e "AT+COPS=0\r" >$atdev;;
-					"5" ) echo -e "AT+CREG?\r" >$atdev;;
-					"6" ) kill $phoneoutputpid; phoneoutputpid= ;;
-					"0" ) break;;
-				esac
-			done
-		;;
-		"2" )
-			while true; do
-				echo
-				echo "------------------------------"
-				echo " 1: Dial: ATD $n1;"
-				echo " 2: Dial: ATD $n2;"
-				echo " 3: Dial: ATD $n3;"
-				echo " 4: Set number for 1"
-				echo " 5: Set number for 2"
-				echo " 6: Set number for 3"
-				echo " 7: Dial: ATD ...;"
-				echo " 8: Hang up: ATH"
-				echo " 9: Answer: ATA"
-				echo " 0: back"
-				echo -n ": "
-				c=`readtty -f -a 1234567890#`
-				echo Got key -$c-
-				case "$c" in
-					"1" ) echo "Dialing $n1"; echo -e "ATD $n1;\r" >$atdev;;
-					"2" ) echo "Dialing $n2"; echo -e "ATD $n2;\r" >$atdev;;
-					"3" ) echo "Dialing $n3"; echo -e "ATD $n3;\r" >$atdev;;
-					"4" ) echo -n "Number: "; read n1; echo $n1 >/data/phoneentry1;;
-					"5" ) echo -n "Number: "; read n2; echo $n2 >/data/phoneentry2;;
-					"6" ) echo -n "Number: "; read n3; echo $n3 >/data/phoneentry3;;
-					"7" ) echo -n "Number: "; read n; echo "Dialing $n"; echo -e "ATD $n;\r" >$atdev;;
-					"8" ) echo -e "ATH\r" >$atdev;;
-					"9" ) echo -e "ATA\r" >$atdev;;
-					"0" ) break;;
-				esac
-			done
-		;;
-		"3" )
-			while true; do
-				echo
-				echo "------------------------------"
-				echo " 1: Save FFS data"
-				echo " 2: Load user FFS data"
-				echo " 3: Load system FFS data"
-				echo " 4: Reset FFS data"
-				echo " 5: Set uplink gain"
-				echo " 6: Set echo"
-				echo " 7: cat /dev/omap_csmi_battery_t"
-				echo " 8: cat /dev/omap_csmi_htc"
-				echo " 0: back"
-				echo -n ": "
-				c=`readtty -f -a 123456780#`
-				echo Got key -$c-
-				case "$c" in
-					"1" ) cat /dev/omap_csmi_ffs >/data/ffsdata;;
-					"2" ) cat /data/ffsdata >/dev/omap_csmi_ffs;;
-					"3" ) cat /system/ffsdata >/dev/omap_csmi_ffs;;
-					"4" ) echo - >/dev/omap_csmi_ffs;;
-					"5" )
-						echo -n "Gain: "; read g;
-						echo gu$g >/tmp/gain;
-						cat /tmp/gain 2>/dev/null >/dev/omap_csmi_audio_tes
-					;;
-					"6" )
-						echo -n "Echo param (hex): "; read e;
-						echo "e0x$e" >/tmp/echo;
-						cat /tmp/echo 2>/dev/null >/dev/omap_csmi_audio_tes
-					;;
-					"7" ) cat /dev/omap_csmi_battery_t;;
-					"8" ) cat /dev/omap_csmi_htc;;
-					"0" ) break;;
-				esac
-			done
-		;;
-		"4" )
-			while true; do
-				echo
-				echo "------------------------------"
-				echo " 1: Toggle debug I/O"
-				echo " 2: Toggle debug Flow"
-				echo " 3: Toggle debug Interrupt"
-				echo " 4: Toggle debug Info"
-				echo " 5: Toggle GSM run state"
-				echo " 6: Clear GSM data area"
-				echo " 0: back"
-				echo -n ": "
-				c=`readtty -f -a 1234560#`
-				echo Got key -$c-
-				case "$c" in
-					"1" ) echo -n "i" >/sys/devices/system/omap_csmi/debug;;
-					"2" ) echo -n "f" >/sys/devices/system/omap_csmi/debug;;
-					"3" ) echo -n "I" >/sys/devices/system/omap_csmi/debug;;
-					"4" ) echo -n "F" >/sys/devices/system/omap_csmi/debug;;
-					"5" ) echo -n "s" >/sys/devices/system/omap_csmi/debug;;
-					"6" ) echo -n "c" >/sys/devices/system/omap_csmi/debug;;
-					"0" ) break;;
-				esac
-			done
-		;;
-		"5" )
-			while true; do
-				echo
-				echo "------------------------------"
-				echo " 1: Start pppd - userspace"
-				echo " 2: Start pppd - kernel"
-				echo " 3: Start pppd - kernel <at1"
-				echo " 4: Configure ppp data to at2"
-				echo " 5: Test with HTTP GET"
-				echo " 6: Kill pppd"
-				echo " 0: back"
-				echo -n ": "
-				c=`readtty -f -a 1234560#`
-				echo Got key -$c-
-				case "$c" in
-					"1" ) kill $pppdpid; pppd notty < $pppdev > $pppdev & pppdpid=$!;;
-					"2" ) kill $pppdpid; pppd nodetach $pppdev & pppdpid=$!;;
-					"3" ) kill &pppdpid; pppd nodetach $pppdev connect "sh -c \"chat -v -f /etc/ppp/connect-data <$atdev >$atdev\"" & pppdpid=$!;;
-					"4" ) echo -e 'AT%DATA=2,"UART",1,,"SER","UART",0\r' >$atdev;;
-					"5" ) test-data-connection;;
-					"6" ) kill $pppdpid; pppdpid=;;
-					"0" ) break;;
-				esac
-			done
-		;;
-		"6" )
-			echo
-			echo ------------------------
-			echo Starting android runtime
-			echo ------------------------
-			start
-		;;
-		"7" )
-			echo
-			echo ------------------------
-			echo Starting android runtime
-			echo ------------------------
-			if exists /data/singleproc
-			then
-				single_process="-s"
-			else
-				single_process=""
-			fi
-			start runtime $single_process
-		;;
-		"8" )
-			stop
-		;;
-		"9" )
-			while true; do
-				echo
-				echo "------------------------------"
-				echo " 1: Print events"
-				echo " 2: Stop event output"
-				if $notifytoggle
-				then
-					echo " 3: stop notify"
-				else
-					echo " 3: notify /sys/android_power"
-				fi
-				echo " 4: start powerd"
-				echo " 5: start powerd verbose"
-				echo " 6: stop powerd"
-				echo " 7: set powerd idletime ($powerdidletime)"
-				echo " 8: start multitap shell"
-				if exists /data/singleproc
-				then
-					echo " 9: enable multiprocess"
-				else
-					echo " 9: disable multiprocess"
-				fi
-				echo " c: start shell"
-				echo " 0: back"
-				echo -n ": "
-				c=`readtty -f -a 1234567890c#`
-				echo Got key -$c-
-				case "$c" in
-					"1" ) kill $eventoutputpid; getevent & eventoutputpid=$! ;;
-					"2" ) kill $eventoutputpid; eventoutputpid= ;;
-					"3" )
-						if $notifytoggle
-						then
-							kill $notifypid
-							notifypid=
-							notifytoggle=false
-						else
-							kill $notifypid
-							notify -m 0x00000002 -c 0 -p -v 0 -w 30 /sys/android_power &
-							notifypid=$!
-							notifytoggle=true
-						fi
-					;;
-					"4" ) start powerd -i $powerdidletime ;;
-					"5" ) start powerd -i $powerdidletime -v ;;
-					"6" ) stop powerd ;;
-					"7" ) echo -n "Idle time (seconds): "; read powerdidletime ;;
-					"8" )
-						readtty -f -p -t 10 -e "[ ~" | sh -i
-					;;
-					"9" )
-						if exists /data/singleproc
-						then
-							echo "Enabling multiprocess environment."
-							rm /data/singleproc
-						else
-							echo "Disabling multiprocess environment."
-							echo >/data/singleproc "true"
-						fi
-					;;
-					"c" ) sh -i <>/dev/tty0 1>&0 2>&1 ;;
-					"0" ) break;;
-				esac
-			done
-		;;
-	esac
-done
-
diff --git a/rootdir/init.rc b/rootdir/init.rc
index cbcb842..26cc121 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -150,6 +150,11 @@
     mount pstore pstore /sys/fs/pstore
     chown system log /sys/fs/pstore/console-ramoops
     chmod 0440 /sys/fs/pstore/console-ramoops
+    chown system log /sys/fs/pstore/pmsg-ramoops-0
+    chmod 0440 /sys/fs/pstore/pmsg-ramoops-0
+
+    # enable armv8_deprecated instruction hooks
+    write /proc/sys/abi/swp 1
 
 # Healthd can trigger a full boot from charger mode by signaling this
 # property when the power button is held.
@@ -281,6 +286,7 @@
     mkdir /data/app-lib 0771 system system
     mkdir /data/app 0771 system system
     mkdir /data/property 0700 root root
+    mkdir /data/tombstones 0771 system system
 
     # create dalvik-cache, so as to enforce our permissions
     mkdir /data/dalvik-cache 0771 root root
diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index 474f630..9cf9ed9 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -16,6 +16,7 @@
 # Anyone can read the logs, but if they're not in the "logs"
 # group, then they'll only see log entries for their UID.
 /dev/log/*                0666   root       log
+/dev/pmsg0                0222   root       log
 
 # the msm hw3d client device node is world writable/readable.
 /dev/msm_hw3dc            0666   root       root
diff --git a/sdcard/sdcard.c b/sdcard/sdcard.c
index 9cfb040..4d50bf0 100644
--- a/sdcard/sdcard.c
+++ b/sdcard/sdcard.c
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <sys/inotify.h>
 #include <sys/mount.h>
+#include <sys/param.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
@@ -414,12 +415,12 @@
     attr->ino = node->ino;
     attr->size = s->st_size;
     attr->blocks = s->st_blocks;
-    attr->atime = s->st_atime;
-    attr->mtime = s->st_mtime;
-    attr->ctime = s->st_ctime;
-    attr->atimensec = s->st_atime_nsec;
-    attr->mtimensec = s->st_mtime_nsec;
-    attr->ctimensec = s->st_ctime_nsec;
+    attr->atime = s->st_atim.tv_sec;
+    attr->mtime = s->st_mtim.tv_sec;
+    attr->ctime = s->st_ctim.tv_sec;
+    attr->atimensec = s->st_atim.tv_nsec;
+    attr->mtimensec = s->st_mtim.tv_nsec;
+    attr->ctimensec = s->st_ctim.tv_nsec;
     attr->mode = s->st_mode;
     attr->nlink = s->st_nlink;
 
@@ -936,7 +937,9 @@
     if (!node) {
         return -ENOENT;
     }
-    if (!check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
+
+    if (!(req->valid & FATTR_FH) &&
+            !check_caller_access_to_node(fuse, hdr, node, W_OK, has_rw)) {
         return -EACCES;
     }
 
@@ -1301,6 +1304,7 @@
         return -errno;
     }
     out.size = res;
+    out.padding = 0;
     fuse_reply(fuse, hdr->unique, &out, sizeof(out));
     return NO_STATUS;
 }
@@ -1460,17 +1464,42 @@
         const struct fuse_in_header* hdr, const struct fuse_init_in* req)
 {
     struct fuse_init_out out;
+    size_t fuse_struct_size;
 
     TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
             handler->token, req->major, req->minor, req->max_readahead, req->flags);
+
+    /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
+     * defined (fuse version 7.6). The structure is the same from 7.6 through
+     * 7.22. Beginning with 7.23, the structure increased in size and added
+     * new parameters.
+     */
+    if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) {
+        ERROR("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6",
+              req->major, req->minor, FUSE_KERNEL_VERSION);
+        return -1;
+    }
+
+    out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION);
+    fuse_struct_size = sizeof(out);
+#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
+    /* FUSE_KERNEL_VERSION >= 23. */
+
+    /* If the kernel only works on minor revs older than or equal to 22,
+     * then use the older structure size since this code only uses the 7.22
+     * version of the structure. */
+    if (req->minor <= 22) {
+        fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
+    }
+#endif
+
     out.major = FUSE_KERNEL_VERSION;
-    out.minor = FUSE_KERNEL_MINOR_VERSION;
     out.max_readahead = req->max_readahead;
     out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
     out.max_background = 32;
     out.congestion_threshold = 32;
     out.max_write = MAX_WRITE;
-    fuse_reply(fuse, hdr->unique, &out, sizeof(out));
+    fuse_reply(fuse, hdr->unique, &out, fuse_struct_size);
     return NO_STATUS;
 }
 
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 84714cf..205fadf 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -9,36 +9,18 @@
 
 
 include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/bin/cat/cat.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=cat_main
-LOCAL_MODULE := libtoolbox_cat
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/sbin/chown/chown.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=chown_main
-LOCAL_MODULE := libtoolbox_chown
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := \
-    upstream-netbsd/bin/cp/cp.c \
-    upstream-netbsd/bin/cp/utils.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=cp_main
-LOCAL_MODULE := libtoolbox_cp
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
 LOCAL_SRC_FILES := \
     upstream-netbsd/bin/dd/args.c \
     upstream-netbsd/bin/dd/conv.c \
     upstream-netbsd/bin/dd/dd.c \
     upstream-netbsd/bin/dd/dd_hostops.c \
     upstream-netbsd/bin/dd/misc.c \
-    upstream-netbsd/bin/dd/position.c
+    upstream-netbsd/bin/dd/position.c \
+    upstream-netbsd/lib/libc/gen/getbsize.c \
+    upstream-netbsd/lib/libc/gen/humanize_number.c \
+    upstream-netbsd/lib/libc/stdlib/strsuftoll.c \
+    upstream-netbsd/lib/libc/string/swab.c \
+    upstream-netbsd/lib/libutil/raise_default_signal.c
 LOCAL_CFLAGS += $(common_cflags) -Dmain=dd_main -DNO_CONV
 LOCAL_MODULE := libtoolbox_dd
 LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
@@ -51,189 +33,69 @@
 LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
 include $(BUILD_STATIC_LIBRARY)
 
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := \
-    upstream-netbsd/usr.bin/grep/fastgrep.c \
-    upstream-netbsd/usr.bin/grep/file.c \
-    upstream-netbsd/usr.bin/grep/grep.c \
-    upstream-netbsd/usr.bin/grep/queue.c \
-    upstream-netbsd/usr.bin/grep/util.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=grep_main
-LOCAL_MODULE := libtoolbox_grep
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/bin/kill/kill.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=kill_main
-LOCAL_MODULE := libtoolbox_kill
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/bin/ln/ln.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=ln_main
-LOCAL_MODULE := libtoolbox_ln
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/bin/mv/mv.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=mv_main -D__SVR4
-LOCAL_MODULE := libtoolbox_mv
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/usr.bin/printenv/printenv.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=printenv_main
-LOCAL_MODULE := libtoolbox_printenv
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/bin/rm/rm.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=rm_main
-LOCAL_MODULE := libtoolbox_rm
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/bin/rmdir/rmdir.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=rmdir_main
-LOCAL_MODULE := libtoolbox_rmdir
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/bin/sleep/sleep.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=sleep_main
-LOCAL_MODULE := libtoolbox_sleep
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := upstream-netbsd/bin/sync/sync.c
-LOCAL_CFLAGS += $(common_cflags) -Dmain=sync_main
-LOCAL_MODULE := libtoolbox_sync
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
-include $(BUILD_STATIC_LIBRARY)
-
 
 include $(CLEAR_VARS)
 
 BSD_TOOLS := \
-    cat \
-    chown \
-    cp \
     dd \
     du \
-    grep \
-    kill \
-    ln \
-    mv \
-    printenv \
-    rm \
-    rmdir \
-    sleep \
-    sync \
 
 OUR_TOOLS := \
-    chcon \
-    chmod \
-    clear \
-    cmp \
-    date \
     df \
-    dmesg \
-    getenforce \
     getevent \
     getprop \
     getsebool \
-    hd \
-    id \
-    ifconfig \
     iftop \
-    insmod \
     ioctl \
     ionice \
     load_policy \
     log \
     ls \
-    lsmod \
     lsof \
-    md5 \
-    mkdir \
-    mknod \
-    mkswap \
     mount \
     nandread \
-    netstat \
     newfs_msdos \
-    nohup \
-    notify \
     ps \
-    readlink \
+    prlimit \
     renice \
     restorecon \
-    prlimit \
-    rmmod \
     route \
     runcon \
     schedtop \
     sendevent \
-    setenforce \
     setprop \
     setsebool \
     smd \
     start \
     stop \
-    swapoff \
-    swapon \
     top \
     touch \
     umount \
     uptime \
-    vmstat \
     watchprops \
     wipe \
 
-ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
-OUR_TOOLS += r
-endif
-
 ALL_TOOLS = $(BSD_TOOLS) $(OUR_TOOLS)
 
 LOCAL_SRC_FILES := \
-    upstream-netbsd/lib/libc/gen/getbsize.c \
-    upstream-netbsd/lib/libc/gen/humanize_number.c \
-    upstream-netbsd/lib/libc/stdlib/strsuftoll.c \
-    upstream-netbsd/lib/libc/string/swab.c \
-    upstream-netbsd/lib/libutil/raise_default_signal.c \
     dynarray.c \
-    pwcache.c \
-    $(patsubst %,%.c,$(OUR_TOOLS)) \
     toolbox.c \
+    $(patsubst %,%.c,$(OUR_TOOLS)) \
 
 LOCAL_CFLAGS += $(common_cflags)
 
-LOCAL_C_INCLUDES += external/openssl/include
-
 LOCAL_SHARED_LIBRARIES := \
-    libcrypto \
     libcutils \
     libselinux \
 
-# libusbhost is only used by lsusb, and that isn't usually included in toolbox.
-# The linker strips out all the unused library code in the normal case.
-LOCAL_STATIC_LIBRARIES := \
-    libusbhost \
-
 LOCAL_WHOLE_STATIC_LIBRARIES := $(patsubst %,libtoolbox_%,$(BSD_TOOLS))
 
 LOCAL_MODULE := toolbox
 LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
 
+# Install the symlinks.
+LOCAL_POST_INSTALL_CMD := $(hide) $(foreach t,$(ALL_TOOLS),ln -sf toolbox $(TARGET_OUT)/bin/$(t);)
+
 # Including this will define $(intermediates).
 #
 include $(BUILD_EXECUTABLE)
@@ -247,19 +109,26 @@
 $(TOOLS_H):
 	$(transform-generated-source)
 
-# Make #!/system/bin/toolbox launchers for each tool.
-#
-SYMLINKS := $(addprefix $(TARGET_OUT)/bin/,$(ALL_TOOLS))
-$(SYMLINKS): TOOLBOX_BINARY := $(LOCAL_MODULE)
-$(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk
-	@echo "Symlink: $@ -> $(TOOLBOX_BINARY)"
-	@mkdir -p $(dir $@)
-	@rm -rf $@
-	$(hide) ln -sf $(TOOLBOX_BINARY) $@
 
-ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)
+# We only want 'r' on userdebug and eng builds.
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := r.c
+LOCAL_CFLAGS += $(common_cflags)
+LOCAL_MODULE := r
+LOCAL_MODULE_TAGS := debug
+LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk
+include $(BUILD_EXECUTABLE)
 
-# We need this so that the installed files could be picked up based on the
-# local module name
-ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \
-    $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS)
+
+# We build BSD grep separately, so it can provide egrep and fgrep too.
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+    upstream-netbsd/usr.bin/grep/fastgrep.c \
+    upstream-netbsd/usr.bin/grep/file.c \
+    upstream-netbsd/usr.bin/grep/grep.c \
+    upstream-netbsd/usr.bin/grep/queue.c \
+    upstream-netbsd/usr.bin/grep/util.c
+LOCAL_CFLAGS += $(common_cflags)
+LOCAL_MODULE := grep
+LOCAL_POST_INSTALL_CMD := $(hide) $(foreach t,egrep fgrep,ln -sf grep $(TARGET_OUT)/bin/$(t);)
+include $(BUILD_EXECUTABLE)
diff --git a/toolbox/alarm.c b/toolbox/alarm.c
deleted file mode 100644
index 9bd58aa..0000000
--- a/toolbox/alarm.c
+++ /dev/null
@@ -1,190 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <string.h>
-#include <errno.h>
-#include <time.h>
-#include <asm/ioctl.h>
-//#include <linux/rtc.h>
-#include <linux/android_alarm.h>
-
-int alarm_main(int argc, char *argv[])
-{
-	int c;
-    int res;
-	struct tm tm;
-	time_t t;
-	struct timespec ts;
-//	struct rtc_time rtc_time;
-	char strbuf[26];
-	int afd;
-	int nfd;
-//	struct timeval timeout = { 0, 0 };
-    int wait = 0;
-	fd_set rfds;
-	const char wake_lock_id[] = "alarm_test"; 
-	int waitalarmmask = 0;
-
-    int useutc = 0;
-	android_alarm_type_t alarmtype_low = ANDROID_ALARM_RTC_WAKEUP;
-	android_alarm_type_t alarmtype_high = ANDROID_ALARM_RTC_WAKEUP;
-	android_alarm_type_t alarmtype = 0;
-
-    do {
-        //c = getopt(argc, argv, "uw:");
-        c = getopt(argc, argv, "uwat:");
-        if (c == EOF)
-            break;
-        switch (c) {
-        case 'u':
-            useutc = 1;
-            break;
-		case 't':
-			alarmtype_low = alarmtype_high = strtol(optarg, NULL, 0);
-			break;
-		case 'a':
-			alarmtype_low = ANDROID_ALARM_RTC_WAKEUP;
-			alarmtype_high = ANDROID_ALARM_TYPE_COUNT - 1;
-			break;
-        case 'w':
-            //timeout.tv_sec = strtol(optarg, NULL, 0);
-            wait = 1;
-            break;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            exit(1);
-        }
-    } while (1);
-    if(optind + 2 < argc) {
-        fprintf(stderr,"%s [-uwa] [-t type] [seconds]\n", argv[0]);
-        return 1;
-    }
-
-    afd = open("/dev/alarm", O_RDWR);
-    if(afd < 0) {
-        fprintf(stderr, "Unable to open rtc: %s\n", strerror(errno));
-        return 1;
-    }
-
-    if(optind == argc) {
-		for(alarmtype = alarmtype_low; alarmtype <= alarmtype_high; alarmtype++) {
-			waitalarmmask |= 1U << alarmtype;
-		}
-#if 0
-        res = ioctl(fd, RTC_ALM_READ, &tm);
-        if(res < 0) {
-            fprintf(stderr, "Unable to read alarm: %s\n", strerror(errno));
-			return 1;
-        }
-#endif
-#if 0
-		t = timegm(&tm);
-        if(useutc)
-            gmtime_r(&t, &tm);
-        else
-            localtime_r(&t, &tm);
-#endif
-#if 0
-        asctime_r(&tm, strbuf);
-        printf("%s", strbuf);
-#endif
-    }
-    else if(optind + 1 == argc) {
-#if 0
-        res = ioctl(fd, RTC_RD_TIME, &tm);
-        if(res < 0) {
-            fprintf(stderr, "Unable to set alarm: %s\n", strerror(errno));
-			return 1;
-        }
-        asctime_r(&tm, strbuf);
-        printf("Now: %s", strbuf);
-        time(&tv.tv_sec);
-#endif
-#if 0
-		time(&ts.tv_sec);
-		ts.tv_nsec = 0;
-		
-        //strptime(argv[optind], NULL, &tm);
-        //tv.tv_sec = mktime(&tm);
-        //tv.tv_usec = 0;
-#endif
-		for(alarmtype = alarmtype_low; alarmtype <= alarmtype_high; alarmtype++) {
-			waitalarmmask |= 1U << alarmtype;
-		    res = ioctl(afd, ANDROID_ALARM_GET_TIME(alarmtype), &ts);
-		    if(res < 0) {
-		        fprintf(stderr, "Unable to get current time: %s\n", strerror(errno));
-				return 1;
-		    }
-		    ts.tv_sec += strtol(argv[optind], NULL, 0);
-		    //strtotimeval(argv[optind], &tv);
-			gmtime_r(&ts.tv_sec, &tm);
-		    printf("time %s -> %ld.%09ld\n", argv[optind], ts.tv_sec, ts.tv_nsec);
-		    asctime_r(&tm, strbuf);
-		    printf("Requested %s", strbuf);
-			
-		    res = ioctl(afd, ANDROID_ALARM_SET(alarmtype), &ts);
-		    if(res < 0) {
-		        fprintf(stderr, "Unable to set alarm: %s\n", strerror(errno));
-				return 1;
-		    }
-		}
-#if 0
-        res = ioctl(fd, RTC_ALM_SET, &tm);
-        if(res < 0) {
-            fprintf(stderr, "Unable to set alarm: %s\n", strerror(errno));
-			return 1;
-        }
-        res = ioctl(fd, RTC_AIE_ON);
-        if(res < 0) {
-            fprintf(stderr, "Unable to enable alarm: %s\n", strerror(errno));
-			return 1;
-        }
-#endif
-    }
-    else {
-        fprintf(stderr,"%s [-u] [date]\n", argv[0]);
-        return 1;
-    }
-
-	if(wait) {
-		while(waitalarmmask) {
-			printf("wait for alarm %x\n", waitalarmmask);
-			res = ioctl(afd, ANDROID_ALARM_WAIT);
-			if(res < 0) {
-				fprintf(stderr, "alarm wait failed\n");
-			}
-			printf("got alarm %x\n", res);
-			waitalarmmask &= ~res;
-			nfd = open("/sys/android_power/acquire_full_wake_lock", O_RDWR);
-			write(nfd, wake_lock_id, sizeof(wake_lock_id) - 1);
-			close(nfd);
-			//sleep(5);
-			nfd = open("/sys/android_power/release_wake_lock", O_RDWR);
-			write(nfd, wake_lock_id, sizeof(wake_lock_id) - 1);
-			close(nfd);
-		}
-		printf("done\n");
-	}
-#if 0	
-	FD_ZERO(&rfds);
-	FD_SET(fd, &rfds);
-	res = select(fd + 1, &rfds, NULL, NULL, &timeout);
-    if(res < 0) {
-        fprintf(stderr, "select failed: %s\n", strerror(errno));
-		return 1;
-    }
-	if(res > 0) {
-		int event;
-		read(fd, &event, sizeof(event));
-		fprintf(stderr, "got %x\n", event);
-	}
-	else {
-		fprintf(stderr, "timeout waiting for alarm\n");
-	}
-#endif
-
-    close(afd);
-
-    return 0;
-}
diff --git a/toolbox/bsd-compatibility.h b/toolbox/bsd-compatibility.h
index 9c6c34a..434d370 100644
--- a/toolbox/bsd-compatibility.h
+++ b/toolbox/bsd-compatibility.h
@@ -50,16 +50,8 @@
 
 #define S_ISWHT(x) false
 
-// TODO: should this be in bionic? (glibc does this, even though it's not quite right.)
-#define O_RSYNC O_SYNC
-
 __BEGIN_DECLS
 
-/* From NetBSD <grp.h> and <pwd.h>. */
-char* group_from_gid(gid_t gid, int noname);
-int uid_from_user(const char* name, uid_t* uid);
-char* user_from_uid(uid_t uid, int noname);
-
 /* From NetBSD <stdlib.h>. */
 #define HN_DECIMAL              0x01
 #define HN_NOSPACE              0x02
diff --git a/toolbox/chcon.c b/toolbox/chcon.c
deleted file mode 100644
index d594b9b..0000000
--- a/toolbox/chcon.c
+++ /dev/null
@@ -1,25 +0,0 @@
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-
-int chcon_main(int argc, char **argv)
-{
-    int rc, i;
-
-    if (argc < 3) {
-        fprintf(stderr, "usage:  %s context path...\n", argv[0]);
-        exit(1);
-    }
-
-    for (i = 2; i < argc; i++) {
-        rc = setfilecon(argv[i], argv[1]);
-        if (rc < 0) {
-            fprintf(stderr, "%s:  Could not label %s with %s:  %s\n",
-                    argv[0], argv[i], argv[1], strerror(errno));
-            exit(2);
-        }
-    }
-    exit(0);
-}
diff --git a/toolbox/chmod.c b/toolbox/chmod.c
deleted file mode 100644
index 2a524e9..0000000
--- a/toolbox/chmod.c
+++ /dev/null
@@ -1,101 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <dirent.h>
-#include <errno.h>
-#include <sys/limits.h>
-#include <sys/stat.h>
-
-#include <unistd.h>
-#include <time.h>
-
-void recurse_chmod(char* path, int mode)
-{
-    struct dirent *dp;
-    DIR *dir = opendir(path);
-    if (dir == NULL) {
-        // not a directory, carry on
-        return;
-    }
-    char *subpath = malloc(sizeof(char)*PATH_MAX);
-    int pathlen = strlen(path);
-
-    while ((dp = readdir(dir)) != NULL) {
-        if (strcmp(dp->d_name, ".") == 0 ||
-            strcmp(dp->d_name, "..") == 0) continue;
-
-        if (strlen(dp->d_name) + pathlen + 2/*NUL and slash*/ > PATH_MAX) {
-            fprintf(stderr, "Invalid path specified: too long\n");
-            exit(1);
-        }
-
-        strcpy(subpath, path);
-        strcat(subpath, "/");
-        strcat(subpath, dp->d_name);
-
-        if (chmod(subpath, mode) < 0) {
-            fprintf(stderr, "Unable to chmod %s: %s\n", subpath, strerror(errno));
-            exit(1);
-        }
-
-        recurse_chmod(subpath, mode);
-    }
-    free(subpath);
-    closedir(dir);
-}
-
-static int usage()
-{
-    fprintf(stderr, "Usage: chmod [OPTION] <MODE> <FILE>\n");
-    fprintf(stderr, "  -R, --recursive         change files and directories recursively\n");
-    fprintf(stderr, "  --help                  display this help and exit\n");
-
-    return 10;
-}
-
-int chmod_main(int argc, char **argv)
-{
-    int i;
-
-    if (argc < 3 || strcmp(argv[1], "--help") == 0) {
-        return usage();
-    }
-
-    int recursive = (strcmp(argv[1], "-R") == 0 ||
-                     strcmp(argv[1], "--recursive") == 0) ? 1 : 0;
-
-    if (recursive && argc < 4) {
-        return usage();
-    }
-
-    if (recursive) {
-        argc--;
-        argv++;
-    }
-
-    int mode = 0;
-    const char* s = argv[1];
-    while (*s) {
-        if (*s >= '0' && *s <= '7') {
-            mode = (mode<<3) | (*s-'0');
-        }
-        else {
-            fprintf(stderr, "Bad mode\n");
-            return 10;
-        }
-        s++;
-    }
-
-    for (i = 2; i < argc; i++) {
-        if (chmod(argv[i], mode) < 0) {
-            fprintf(stderr, "Unable to chmod %s: %s\n", argv[i], strerror(errno));
-            return 10;
-        }
-        if (recursive) {
-            recurse_chmod(argv[i], mode);
-        }
-    }
-    return 0;
-}
-
diff --git a/toolbox/clear.c b/toolbox/clear.c
deleted file mode 100644
index df46ad2..0000000
--- a/toolbox/clear.c
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2012, 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <stdio.h>
-
-int clear_main(int argc, char **argv) {
-    /* This prints the clear screen and move cursor to top-left corner control
-     * characters for VT100 terminals. This means it will not work on
-     * non-VT100 compliant terminals, namely Windows' cmd.exe, but should
-     * work on anything unix-y. */
-    fputs("\x1b[2J\x1b[H", stdout);
-    return 0;
-}
diff --git a/toolbox/cmp.c b/toolbox/cmp.c
deleted file mode 100644
index 80635ad..0000000
--- a/toolbox/cmp.c
+++ /dev/null
@@ -1,91 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <errno.h>
-
-int cmp_main(int argc, char *argv[])
-{
-    int c;
-    int fd1, fd2;
-	char buf1[4096], buf2[4096];
-    int res, res1, res2;
-	int rv = 0;
-	int i;
-	int filepos = 0;
-
-	int show_byte = 0;
-	int show_all = 0;
-	int limit = 0;
-
-    do {
-        c = getopt(argc, argv, "bln:");
-        if (c == EOF)
-            break;
-        switch (c) {
-        case 'b':
-            show_byte = 1;
-            break;
-        case 'l':
-            show_all = 1;
-            break;
-        case 'n':
-            limit = atoi(optarg);
-            break;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            exit(1);
-        }
-    } while (1);
-
-    if (optind + 2 != argc) {
-        fprintf(stderr, "Usage: %s [-b] [-l] [-n count] file1 file2\n", argv[0]);
-        exit(1);
-    }
-
-    fd1 = open(argv[optind], O_RDONLY);
-    if(fd1 < 0) {
-        fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
-        return 1;
-    }
-
-    fd2 = open(argv[optind+1], O_RDONLY);
-    if(fd2 < 0) {
-        fprintf(stderr, "could not open %s, %s\n", argv[optind+1], strerror(errno));
-        return 1;
-    }
-    
-    while(1) {
-        res1 = read(fd1, &buf1, sizeof(buf1));
-        res2 = read(fd2, &buf2, sizeof(buf2));
-		res = res1 < res2 ? res1 : res2;
-		if(res1 == 0 && res2 == 0) {
-			return rv;
-		}
-		for(i = 0; i < res; i++) {
-			if(buf1[i] != buf2[i]) {
-				printf("%s %s differ byte %d", argv[optind], argv[optind+1], filepos + i);
-				if(show_byte)
-					printf(" 0x%02x 0x%02x", buf1[i], buf2[i]);
-				printf("\n");
-				if(!show_all)
-					return 1;
-				rv = 1;
-			}
-			if(limit) {
-				limit--;
-				if(limit == 0)
-					return rv;
-			}
-		}
-		if(res1 != res2 || res < 0) {
-			printf("%s on %s\n", res < 0 ? "Read error" : "EOF", res1 < res2 ? argv[optind] : argv[optind+1]);
-			return 1;
-		}
-		filepos += res;
-    }
-}
diff --git a/toolbox/date.c b/toolbox/date.c
deleted file mode 100644
index 70ce1d5..0000000
--- a/toolbox/date.c
+++ /dev/null
@@ -1,227 +0,0 @@
-#include <ctype.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <inttypes.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <unistd.h>
-
-#include <linux/android_alarm.h>
-#include <linux/rtc.h>
-#include <sys/ioctl.h>
-
-static int settime_alarm(struct timespec *ts) {
-    int fd, ret;
-
-    fd = open("/dev/alarm", O_RDWR);
-    if (fd < 0)
-        return fd;
-
-    ret = ioctl(fd, ANDROID_ALARM_SET_RTC, ts);
-    close(fd);
-    return ret;
-}
-
-static int settime_alarm_tm(struct tm *tm) {
-    time_t t;
-    struct timespec ts;
-
-    t = mktime(tm);
-    ts.tv_sec = t;
-    ts.tv_nsec = 0;
-    return settime_alarm(&ts);
-}
-
-static int settime_alarm_timeval(struct timeval *tv) {
-    struct timespec ts;
-
-    ts.tv_sec = tv->tv_sec;
-    ts.tv_nsec = tv->tv_usec * 1000;
-    return settime_alarm(&ts);
-}
-
-static int settime_rtc_tm(struct tm *tm) {
-    int fd, ret;
-    struct timeval tv;
-    struct rtc_time rtc;
-
-    fd = open("/dev/rtc0", O_RDWR);
-    if (fd < 0)
-        return fd;
-
-    tv.tv_sec = mktime(tm);
-    tv.tv_usec = 0;
-
-    ret = settimeofday(&tv, NULL);
-    if (ret < 0)
-        goto done;
-
-    memset(&rtc, 0, sizeof(rtc));
-    rtc.tm_sec = tm->tm_sec;
-    rtc.tm_min = tm->tm_min;
-    rtc.tm_hour = tm->tm_hour;
-    rtc.tm_mday = tm->tm_mday;
-    rtc.tm_mon = tm->tm_mon;
-    rtc.tm_year = tm->tm_year;
-    rtc.tm_wday = tm->tm_wday;
-    rtc.tm_yday = tm->tm_yday;
-    rtc.tm_isdst = tm->tm_isdst;
-
-    ret = ioctl(fd, RTC_SET_TIME, rtc);
-done:
-    close(fd);
-    return ret;
-}
-
-static int settime_rtc_timeval(struct timeval *tv) {
-    struct tm tm, *err;
-    time_t t = tv->tv_sec;
-
-    err = gmtime_r(&t, &tm);
-    if (!err)
-        return -1;
-
-    return settime_rtc_tm(&tm);
-}
-
-static void settime(char *s) {
-    struct tm tm;
-    int day = atoi(s);
-    int hour;
-
-    while (*s && *s != '.')
-        s++;
-
-    if (*s)
-        s++;
-
-    hour = atoi(s);
-
-    tm.tm_year = day / 10000 - 1900;
-    tm.tm_mon = (day % 10000) / 100 - 1;
-    tm.tm_mday = (day % 100);
-    tm.tm_hour = hour / 10000;
-    tm.tm_min = (hour % 10000) / 100;
-    tm.tm_sec = (hour % 100);
-    tm.tm_isdst = -1;
-
-    if (settime_alarm_tm(&tm) < 0)
-        settime_rtc_tm(&tm);
-}
-
-static char *parse_time(const char *str, struct timeval *ts) {
-  char *s;
-  long fs = 0; /* fractional seconds */
-
-  ts->tv_sec = strtoumax(str, &s, 10);
-
-  if (*s == '.') {
-    s++;
-    int count = 0;
-
-    /* read up to 6 digits (microseconds) */
-    while (*s && isdigit(*s)) {
-      if (++count < 7) {
-        fs = fs*10 + (*s - '0');
-      }
-      s++;
-    }
-
-    for (; count < 6; count++) {
-      fs *= 10;
-    }
-  }
-
-  ts->tv_usec = fs;
-  return s;
-}
-
-int date_main(int argc, char *argv[])
-{
-    int c;
-    int res;
-    struct tm tm;
-    time_t t;
-    struct timeval tv;
-    char strbuf[260];
-
-    int useutc = 0;
-
-    tzset();
-
-    do {
-        c = getopt(argc, argv, "us:");
-        if (c == EOF)
-            break;
-        switch (c) {
-        case 'u':
-            useutc = 1;
-            break;
-        case 's':
-            settime(optarg);
-            break;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            exit(1);
-        }
-    } while (1);
-    if(optind + 2 < argc) {
-        fprintf(stderr,"%s [-u] [date]\n", argv[0]);
-        return 1;
-    }
-
-    int hasfmt = argc == optind + 1 && argv[optind][0] == '+';
-    if(optind == argc || hasfmt) {
-        time(&t);
-        if (useutc) {
-            gmtime_r(&t, &tm);
-            strftime(strbuf, sizeof(strbuf),
-                     (hasfmt ? argv[optind] + 1 : "%a %b %e %H:%M:%S GMT %Y"),
-                     &tm);
-        } else {
-            localtime_r(&t, &tm);
-            strftime(strbuf, sizeof(strbuf),
-                     (hasfmt ? argv[optind] + 1 : "%a %b %e %H:%M:%S %Z %Y"),
-                     &tm);
-        }
-        printf("%s\n", strbuf);
-    }
-    else if(optind + 1 == argc) {
-#if 0
-        struct tm *tmptr;
-        tmptr = getdate(argv[optind]);
-        if(tmptr == NULL) {
-            fprintf(stderr,"getdate_r failed\n");
-            return 1;
-        }
-        tm = *tmptr;
-#if 0
-        if(getdate_r(argv[optind], &tm) < 0) {
-            fprintf(stderr,"getdate_r failed %s\n", strerror(errno));
-            return 1;
-        }
-#endif
-#endif
-        //strptime(argv[optind], NULL, &tm);
-        //tv.tv_sec = mktime(&tm);
-        //tv.tv_usec = 0;
-        parse_time(argv[optind], &tv);
-        printf("time %s -> %lu.%lu\n", argv[optind], tv.tv_sec, tv.tv_usec);
-        res = settime_alarm_timeval(&tv);
-        if (res < 0)
-            res = settime_rtc_timeval(&tv);
-        if(res < 0) {
-            fprintf(stderr,"settimeofday failed %s\n", strerror(errno));
-            return 1;
-        }
-    }
-    else {
-        fprintf(stderr,"%s [-s 20070325.123456] [-u] [date]\n", argv[0]);
-        return 1;
-    }
-
-    return 0;
-}
diff --git a/toolbox/dmesg.c b/toolbox/dmesg.c
deleted file mode 100644
index 9c73b00..0000000
--- a/toolbox/dmesg.c
+++ /dev/null
@@ -1,58 +0,0 @@
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <errno.h>
-#include <sys/klog.h>
-#include <string.h>
-
-#define FALLBACK_KLOG_BUF_SHIFT	17	/* CONFIG_LOG_BUF_SHIFT from our kernel */
-#define FALLBACK_KLOG_BUF_LEN	(1 << FALLBACK_KLOG_BUF_SHIFT)
-
-int dmesg_main(int argc, char **argv)
-{
-    char *buffer;
-    char *p;
-    ssize_t ret;
-    int n, op, klog_buf_len;
-
-    klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
-
-    if (klog_buf_len <= 0) {
-        klog_buf_len = FALLBACK_KLOG_BUF_LEN;
-    }
-
-    buffer = (char *)malloc(klog_buf_len + 1);
-
-    if (!buffer) {
-        perror("malloc");
-        return EXIT_FAILURE;
-    }
-
-    p = buffer;
-
-    if((argc == 2) && (!strcmp(argv[1],"-c"))) {
-        op = KLOG_READ_CLEAR;
-    } else {
-        op = KLOG_READ_ALL;
-    }
-
-    n = klogctl(op, buffer, klog_buf_len);
-    if (n < 0) {
-        perror("klogctl");
-        return EXIT_FAILURE;
-    }
-    buffer[n] = '\0';
-
-    while((ret = write(STDOUT_FILENO, p, n))) {
-        if (ret == -1) {
-	    if (errno == EINTR)
-                continue;
-	    perror("write");
-	    return EXIT_FAILURE;
-	}
-	p += ret;
-	n -= ret;
-    }
-
-    return 0;
-}
diff --git a/toolbox/dynarray.c b/toolbox/dynarray.c
index e9b7b03..47594e0 100644
--- a/toolbox/dynarray.c
+++ b/toolbox/dynarray.c
@@ -1,6 +1,7 @@
 #include "dynarray.h"
-#include <stdlib.h>
 #include <limits.h>
+#include <stdlib.h>
+#include <string.h>
 
 void
 dynarray_init( dynarray_t *a )
diff --git a/toolbox/exists.c b/toolbox/exists.c
deleted file mode 100644
index e348668..0000000
--- a/toolbox/exists.c
+++ /dev/null
@@ -1,16 +0,0 @@
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-int exists_main(int argc, char *argv[])
-{
-    struct stat s;
-
-    if(argc < 2) return 1;
-
-    if(stat(argv[1], &s)) {
-        return 1;
-    } else {
-        return 0;
-    }
-}
diff --git a/toolbox/getenforce.c b/toolbox/getenforce.c
deleted file mode 100644
index 9e7589a..0000000
--- a/toolbox/getenforce.c
+++ /dev/null
@@ -1,30 +0,0 @@
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-
-int getenforce_main(int argc, char **argv)
-{
-    int rc;
-
-    rc = is_selinux_enabled();
-    if (rc <= 0) {
-        printf("Disabled\n");
-        return 0;
-    }
-
-    rc = security_getenforce();
-    if (rc < 0) {
-        fprintf(stderr, "Could not get enforcing status:  %s\n",
-                strerror(errno));
-        return 2;
-    }
-
-    if (rc)
-        printf("Enforcing\n");
-    else
-        printf("Permissive\n");
-
-    return 0;
-}
diff --git a/toolbox/getevent.c b/toolbox/getevent.c
index da83ec3..c58eb5d 100644
--- a/toolbox/getevent.c
+++ b/toolbox/getevent.c
@@ -10,6 +10,7 @@
 #include <sys/poll.h>
 #include <linux/input.h>
 #include <errno.h>
+#include <unistd.h>
 
 #include "getevent.h"
 
diff --git a/toolbox/hd.c b/toolbox/hd.c
deleted file mode 100644
index 7c9998e..0000000
--- a/toolbox/hd.c
+++ /dev/null
@@ -1,97 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <errno.h>
-
-int hd_main(int argc, char *argv[])
-{
-    int c;
-    int fd;
-	unsigned char buf[4096];
-    int res;
-	int read_len;
-	int i;
-	int filepos = 0;
-	int sum;
-	int lsum;
-
-	int base = -1;
-	int count = 0;
-	int repeat = 0;
-
-    do {
-        c = getopt(argc, argv, "b:c:r:");
-        if (c == EOF)
-            break;
-        switch (c) {
-        case 'b':
-            base = strtol(optarg, NULL, 0);
-            break;
-        case 'c':
-            count = strtol(optarg, NULL, 0);
-            break;
-		case 'r':
-			repeat = strtol(optarg, NULL, 0);
-			break;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            exit(1);
-        }
-    } while (1);
-
-    if (optind + 1 != argc) {
-        fprintf(stderr, "Usage: %s [-b base] [-c count] [-r delay] file\n", argv[0]);
-        exit(1);
-    }
-
-    fd = open(argv[optind], O_RDONLY);
-    if(fd < 0) {
-        fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
-        return 1;
-    }
-
-	do {
-		if(base >= 0) {
-			lseek(fd, base, SEEK_SET);
-			filepos = base;
-		}
-		sum = 0;
-		lsum = 0;
-	    while(1) {
-			read_len = sizeof(buf);
-			if(count > 0 && base + count - filepos < read_len)
-				read_len = base + count - filepos;
-	        res = read(fd, &buf, read_len);
-			if(res == 0)
-				break;
-			for(i = 0; i < res; i++) {
-				if((i & 15) == 0) {
-					printf("%08x: ", filepos + i);
-				}
-				lsum += buf[i];
-				sum += buf[i];
-				printf("%02x ", buf[i]);
-				if(((i & 15) == 15) || (i == res - 1)) {
-					printf("s %x\n", lsum);
-					lsum = 0;
-				}
-			}
-			if(res < 0) {
-				printf("Read error on %s, offset %d len %d, %s\n", argv[optind], filepos, read_len, strerror(errno));
-				return 1;
-			}
-			filepos += res;
-			if(filepos == base + count)
-				break;
-	    }
-		printf("sum %x\n", sum);
-		if(repeat)
-			sleep(repeat);
-	} while(repeat);
-	return 0;
-}
diff --git a/toolbox/id.c b/toolbox/id.c
deleted file mode 100644
index 8ec79c1..0000000
--- a/toolbox/id.c
+++ /dev/null
@@ -1,57 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <pwd.h>
-#include <grp.h>
-#include <selinux/selinux.h>
-
-static void print_uid(uid_t uid)
-{
-    struct passwd *pw = getpwuid(uid);
-
-    if (pw) {
-        printf("%d(%s)", uid, pw->pw_name);
-    } else {
-        printf("%d",uid);
-    }
-}
-
-static void print_gid(gid_t gid)
-{
-    struct group *gr = getgrgid(gid);
-    if (gr) {
-        printf("%d(%s)", gid, gr->gr_name);
-    } else {
-        printf("%d",gid);
-    }
-}
-
-int id_main(int argc, char **argv)
-{
-    gid_t list[64];
-    int n, max;
-    char *secctx;
-
-    max = getgroups(64, list);
-    if (max < 0) max = 0;
-
-    printf("uid=");
-    print_uid(getuid());
-    printf(" gid=");
-    print_gid(getgid());
-    if (max) {
-        printf(" groups=");
-        print_gid(list[0]);
-        for(n = 1; n < max; n++) {
-            printf(",");
-            print_gid(list[n]);
-        }
-    }
-    if (getcon(&secctx) == 0) {
-        printf(" context=%s", secctx);
-        free(secctx);
-    }
-    printf("\n");
-    return 0;
-}
diff --git a/toolbox/ifconfig.c b/toolbox/ifconfig.c
deleted file mode 100644
index b953176..0000000
--- a/toolbox/ifconfig.c
+++ /dev/null
@@ -1,157 +0,0 @@
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include <errno.h>
-#include <string.h>
-#include <ctype.h>
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <linux/if.h>
-#include <linux/sockios.h>
-#include <arpa/inet.h>
-
-static void die(const char *s)
-{
-    fprintf(stderr,"error: %s (%s)\n", s, strerror(errno));
-    exit(-1);
-}
-
-static void setflags(int s, struct ifreq *ifr, int set, int clr)
-{
-    if(ioctl(s, SIOCGIFFLAGS, ifr) < 0) die("SIOCGIFFLAGS");
-    ifr->ifr_flags = (ifr->ifr_flags & (~clr)) | set;
-    if(ioctl(s, SIOCSIFFLAGS, ifr) < 0) die("SIOCSIFFLAGS");
-}
-
-static inline void init_sockaddr_in(struct sockaddr_in *sin, const char *addr)
-{
-    sin->sin_family = AF_INET;
-    sin->sin_port = 0;
-    sin->sin_addr.s_addr = inet_addr(addr);
-}
-
-static void setmtu(int s, struct ifreq *ifr, const char *mtu)
-{
-    int m = atoi(mtu);
-    ifr->ifr_mtu = m;
-    if(ioctl(s, SIOCSIFMTU, ifr) < 0) die("SIOCSIFMTU");
-}
-static void setdstaddr(int s, struct ifreq *ifr, const char *addr)
-{
-    init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_dstaddr, addr);
-    if(ioctl(s, SIOCSIFDSTADDR, ifr) < 0) die("SIOCSIFDSTADDR");
-}
-
-static void setnetmask(int s, struct ifreq *ifr, const char *addr)
-{
-    init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_netmask, addr);
-    if(ioctl(s, SIOCSIFNETMASK, ifr) < 0) die("SIOCSIFNETMASK");
-}
-
-static void setaddr(int s, struct ifreq *ifr, const char *addr)
-{
-    init_sockaddr_in((struct sockaddr_in *) &ifr->ifr_addr, addr);
-    if(ioctl(s, SIOCSIFADDR, ifr) < 0) die("SIOCSIFADDR");
-}
-
-int ifconfig_main(int argc, char *argv[])
-{
-    struct ifreq ifr;
-    int s;
-    unsigned int flags;
-    char astring[20];
-    char mstring[20];
-    char *updown, *brdcst, *loopbk, *ppp, *running, *multi;
-
-    argc--;
-    argv++;
-
-    if(argc == 0) return 0;
-
-    memset(&ifr, 0, sizeof(struct ifreq));
-    strncpy(ifr.ifr_name, argv[0], IFNAMSIZ);
-    ifr.ifr_name[IFNAMSIZ-1] = 0;
-    argc--, argv++;
-
-    if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-        die("cannot open control socket\n");
-    }
-
-    if (argc == 0) {
-        if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
-            perror(ifr.ifr_name);
-            return -1;
-        } else
-            strlcpy(astring,
-                   inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr),
-                   sizeof(astring));
-
-        if (ioctl(s, SIOCGIFNETMASK, &ifr) < 0) {
-            perror(ifr.ifr_name);
-            return -1;
-        } else
-            strlcpy(mstring,
-                   inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr),
-                   sizeof(mstring));
-
-        if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
-            perror(ifr.ifr_name);
-            return -1;
-        } else
-            flags = ifr.ifr_flags;
-
-        printf("%s: ip %s mask %s flags [", ifr.ifr_name,
-               astring,
-               mstring
-               );
-
-        updown =  (flags & IFF_UP)           ? "up" : "down";
-        brdcst =  (flags & IFF_BROADCAST)    ? " broadcast" : "";
-        loopbk =  (flags & IFF_LOOPBACK)     ? " loopback" : "";
-        ppp =     (flags & IFF_POINTOPOINT)  ? " point-to-point" : "";
-        running = (flags & IFF_RUNNING)      ? " running" : "";
-        multi =   (flags & IFF_MULTICAST)    ? " multicast" : "";
-        printf("%s%s%s%s%s%s]\n", updown, brdcst, loopbk, ppp, running, multi);
-        return 0;
-    }
-    
-    while(argc > 0) {
-        if (!strcmp(argv[0], "up")) {
-            setflags(s, &ifr, IFF_UP, 0);
-        } else if (!strcmp(argv[0], "mtu")) {
-            argc--, argv++;
-            if (!argc) {
-                errno = EINVAL;
-                die("expecting a value for parameter \"mtu\"");
-            }
-            setmtu(s, &ifr, argv[0]);
-        } else if (!strcmp(argv[0], "-pointopoint")) {
-            setflags(s, &ifr, IFF_POINTOPOINT, 1);
-        } else if (!strcmp(argv[0], "pointopoint")) {
-            argc--, argv++;
-            if (!argc) { 
-                errno = EINVAL;
-                die("expecting an IP address for parameter \"pointtopoint\"");
-            }
-            setdstaddr(s, &ifr, argv[0]);
-            setflags(s, &ifr, IFF_POINTOPOINT, 0);
-        } else if (!strcmp(argv[0], "down")) {
-            setflags(s, &ifr, 0, IFF_UP);
-        } else if (!strcmp(argv[0], "netmask")) {
-            argc--, argv++;
-            if (!argc) { 
-                errno = EINVAL;
-                die("expecting an IP address for parameter \"netmask\"");
-            }
-            setnetmask(s, &ifr, argv[0]);
-        } else if (isdigit(argv[0][0])) {
-            setaddr(s, &ifr, argv[0]);
-            setflags(s, &ifr, IFF_UP, 0);
-        }
-        argc--, argv++;
-    }
-    return 0;
-}
diff --git a/toolbox/insmod.c b/toolbox/insmod.c
deleted file mode 100644
index d252433..0000000
--- a/toolbox/insmod.c
+++ /dev/null
@@ -1,97 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <malloc.h>
-#include <errno.h>
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-extern int init_module(void *, unsigned long, const char *);
-
-static void *read_file(const char *filename, ssize_t *_size)
-{
-	int ret, fd;
-	struct stat sb;
-	ssize_t size;
-	void *buffer = NULL;
-
-	/* open the file */
-	fd = open(filename, O_RDONLY);
-	if (fd < 0)
-		return NULL;
-
-	/* find out how big it is */
-	if (fstat(fd, &sb) < 0)
-		goto bail;
-	size = sb.st_size;
-
-	/* allocate memory for it to be read into */
-	buffer = malloc(size);
-	if (!buffer)
-		goto bail;
-
-	/* slurp it into our buffer */
-	ret = read(fd, buffer, size);
-	if (ret != size)
-		goto bail;
-
-	/* let the caller know how big it is */
-	*_size = size;
-
-bail:
-	close(fd);
-	return buffer;
-}
-
-int insmod_main(int argc, char **argv)
-{
-	void *file;
-	ssize_t size = 0;
-	char opts[1024];
-	int ret;
-
-	/* make sure we've got an argument */
-	if (argc < 2) {
-		fprintf(stderr, "usage: insmod <module.o>\n");
-		return -1;
-	}
-
-	/* read the file into memory */
-	file = read_file(argv[1], &size);
-	if (!file) {
-		fprintf(stderr, "insmod: can't open '%s'\n", argv[1]);
-		return -1;
-	}
-
-	opts[0] = '\0';
-	if (argc > 2) {
-		int i, len;
-		char *end = opts + sizeof(opts) - 1;
-		char *ptr = opts;
-
-		for (i = 2; (i < argc) && (ptr < end); i++) {
-			len = MIN(strlen(argv[i]), (size_t)(end - ptr));
-			memcpy(ptr, argv[i], len);
-			ptr += len;
-			*ptr++ = ' ';
-		}
-		*(ptr - 1) = '\0';
-	}
-
-	/* pass it to the kernel */
-	ret = init_module(file, size, opts);
-	if (ret != 0) {
-		fprintf(stderr,
-                "insmod: init_module '%s' failed (%s)\n",
-                argv[1], strerror(errno));
-	}
-
-	/* free the file buffer */
-	free(file);
-
-	return ret;
-}
-
diff --git a/toolbox/ioctl.c b/toolbox/ioctl.c
index fd90812..d1cc14a 100644
--- a/toolbox/ioctl.c
+++ b/toolbox/ioctl.c
@@ -9,6 +9,7 @@
 #include <errno.h>
 #include <pthread.h>
 #include <sys/ioctl.h>
+#include <unistd.h>
 
 int ioctl_main(int argc, char *argv[])
 {
@@ -45,7 +46,7 @@
             break;
         case 'h':
             fprintf(stderr, "%s [-l <length>] [-a <argsize>] [-rdh] <device> <ioctlnr>\n"
-                    "  -l <lenght>   Length of io buffer\n"
+                    "  -l <length>   Length of io buffer\n"
                     "  -a <argsize>  Size of each argument (1-8)\n"
                     "  -r            Open device in read only mode\n"
                     "  -d            Direct argument (no iobuffer)\n"
diff --git a/toolbox/ionice.c b/toolbox/ionice.c
index 4a182f2..7abc261 100644
--- a/toolbox/ionice.c
+++ b/toolbox/ionice.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <errno.h>
 
diff --git a/toolbox/lsmod.c b/toolbox/lsmod.c
deleted file mode 100644
index 8b55ee6..0000000
--- a/toolbox/lsmod.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <stdio.h>
-
-extern int cat_main(int argc, char **argv);
-
-int lsmod_main(int argc, char **argv)
-{
-	char *cat_argv[] = { "cat", "/proc/modules", NULL };
-	return cat_main(2, cat_argv);
-}
-
diff --git a/toolbox/lsof.c b/toolbox/lsof.c
index bee981d..982f5aa 100644
--- a/toolbox/lsof.c
+++ b/toolbox/lsof.c
@@ -35,6 +35,7 @@
 #include <libgen.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 #include <pwd.h>
diff --git a/toolbox/lsusb.c b/toolbox/lsusb.c
deleted file mode 100644
index 236e74b..0000000
--- a/toolbox/lsusb.c
+++ /dev/null
@@ -1,227 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <endian.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-
-#include <usbhost/usbhost.h>
-
-static int verbose = 0;
-static char str_buff[4096];
-
-static const char *get_str(struct usb_device *dev, int id)
-{
-    char *str = usb_device_get_string(dev, id);
-
-    if (id && str) {
-        strlcpy(str_buff, str, sizeof(str_buff));
-        free(str);
-    } else {
-        snprintf(str_buff, sizeof(str_buff), "%02x", id);
-    }
-
-    return str_buff;
-}
-
-
-static void lsusb_parse_device_descriptor(struct usb_device *dev,
-                                          struct usb_device_descriptor *desc)
-{
-    printf("  Device Descriptor\n");
-    printf("\tbcdUSB: %04x\n", letoh16(desc->bcdUSB));
-    printf("\tbDeviceClass: %02x\n", desc->bDeviceClass);
-    printf("\tbDeviceSubClass: %02x\n", desc->bDeviceSubClass);
-    printf("\tbDeviceProtocol: %02x\n", desc->bDeviceProtocol);
-    printf("\tbMaxPacketSize0: %02x\n", desc->bMaxPacketSize0);
-    printf("\tidVendor: %04x\n", letoh16(desc->idVendor));
-    printf("\tidProduct: %04x\n", letoh16(desc->idProduct));
-    printf("\tbcdDevice: %04x\n", letoh16(desc->bcdDevice));
-    printf("\tiManufacturer: %s\n", get_str(dev, desc->iManufacturer));
-    printf("\tiProduct: %s\n", get_str(dev, desc->iProduct));
-    printf("\tiSerialNumber: %s\n", get_str(dev,desc->iSerialNumber));
-    printf("\tbNumConfiguration: %02x\n", desc->bNumConfigurations);
-    printf("\n");
-}
-
-static void lsusb_parse_config_descriptor(struct usb_device *dev,
-                                          struct usb_config_descriptor *desc)
-{
-    printf("  Config Descriptor\n");
-    printf("\twTotalLength: %04x\n", letoh16(desc->wTotalLength));
-    printf("\tbNumInterfaces: %02x\n", desc->bNumInterfaces);
-    printf("\tbConfigurationValue: %02x\n", desc->bConfigurationValue);
-    printf("\tiConfiguration: %s\n", get_str(dev, desc->iConfiguration));
-    printf("\tbmAttributes: %02x\n", desc->bmAttributes);
-    printf("\tbMaxPower: %d mA\n", desc->bMaxPower * 2);
-    printf("\n");
-}
-
-static void lsusb_parse_interface_descriptor(struct usb_device *dev,
-                                             struct usb_interface_descriptor *desc)
-{
-    printf("  Interface Descriptor\n");
-    printf("\tbInterfaceNumber: %02x\n", desc->bInterfaceNumber);
-    printf("\tbAlternateSetting: %02x\n", desc->bAlternateSetting);
-    printf("\tbNumEndpoints: %02x\n", desc->bNumEndpoints);
-    printf("\tbInterfaceClass: %02x\n", desc->bInterfaceClass);
-    printf("\tbInterfaceSubClass: %02x\n", desc->bInterfaceSubClass);
-    printf("\tbInterfaceProtocol: %02x\n", desc->bInterfaceProtocol);
-    printf("\tiInterface: %s\n", get_str(dev, desc->iInterface));
-    printf("\n");
-}
-
-static void lsusb_parse_endpoint_descriptor(struct usb_device *dev,
-                                            struct usb_endpoint_descriptor *desc)
-{
-    printf("  Endpoint Descriptor\n");
-    printf("\tbEndpointAddress: %02x\n", desc->bEndpointAddress);
-    printf("\tbmAttributes: %02x\n", desc->bmAttributes);
-    printf("\twMaxPacketSize: %02x\n", letoh16(desc->wMaxPacketSize));
-    printf("\tbInterval: %02x\n", desc->bInterval);
-    printf("\tbRefresh: %02x\n", desc->bRefresh);
-    printf("\tbSynchAddress: %02x\n", desc->bSynchAddress);
-    printf("\n");
-}
-
-static void lsusb_dump_descriptor(struct usb_device *dev,
-                                  struct usb_descriptor_header *desc)
-{
-    int i;
-    printf("  Descriptor type %02x\n", desc->bDescriptorType);
-
-    for (i = 0; i < desc->bLength; i++ ) {
-        if ((i % 16) == 0)
-            printf("\t%02x:", i);
-        printf(" %02x", ((uint8_t *)desc)[i]);
-        if ((i % 16) == 15)
-            printf("\n");
-    }
-
-    if ((i % 16) != 0)
-        printf("\n");
-    printf("\n");
-}
-
-static void lsusb_parse_descriptor(struct usb_device *dev,
-                                   struct usb_descriptor_header *desc)
-{
-    switch (desc->bDescriptorType) {
-    case USB_DT_DEVICE:
-        lsusb_parse_device_descriptor(dev, (struct usb_device_descriptor *) desc);
-        break;
-
-    case USB_DT_CONFIG:
-        lsusb_parse_config_descriptor(dev, (struct usb_config_descriptor *) desc);
-        break;
-
-    case USB_DT_INTERFACE:
-        lsusb_parse_interface_descriptor(dev, (struct usb_interface_descriptor *) desc);
-        break;
-
-    case USB_DT_ENDPOINT:
-        lsusb_parse_endpoint_descriptor(dev, (struct usb_endpoint_descriptor *) desc);
-        break;
-
-    default:
-        lsusb_dump_descriptor(dev, desc);
-
-        break;
-    }
-}
-
-static int lsusb_device_added(const char *dev_name, void *client_data)
-{
-    struct usb_device *dev = usb_device_open(dev_name);
-
-    if (!dev) {
-        fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno));
-        return 0;
-    }
-
-    if (verbose) {
-        struct usb_descriptor_iter iter;
-        struct usb_descriptor_header *desc;
-
-        printf("%s:\n", dev_name);
-
-        usb_descriptor_iter_init(dev, &iter);
-
-        while ((desc = usb_descriptor_iter_next(&iter)) != NULL)
-            lsusb_parse_descriptor(dev, desc);
-
-    } else {
-        uint16_t vid, pid;
-        char *mfg_name, *product_name, *serial;
-
-        vid = usb_device_get_vendor_id(dev);
-        pid = usb_device_get_product_id(dev);
-        mfg_name = usb_device_get_manufacturer_name(dev);
-        product_name = usb_device_get_product_name(dev);
-        serial = usb_device_get_serial(dev);
-
-        printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid,
-               mfg_name, product_name, serial);
-
-        free(mfg_name);
-        free(product_name);
-        free(serial);
-    }
-
-    usb_device_close(dev);
-
-    return 0;
-}
-
-static int lsusb_device_removed(const char *dev_name, void *client_data)
-{
-    return 0;
-}
-
-
-static int lsusb_discovery_done(void *client_data)
-{
-    return 1;
-}
-
-
-
-int lsusb_main(int argc, char **argv)
-{
-    struct usb_host_context *ctx;
-
-    if (argc == 2 && !strcmp(argv[1], "-v"))
-        verbose = 1;
-
-    ctx = usb_host_init();
-    if (!ctx) {
-        perror("usb_host_init:");
-        return 1;
-    }
-
-    usb_host_run(ctx,
-                 lsusb_device_added,
-                 lsusb_device_removed,
-                 lsusb_discovery_done,
-                 NULL);
-
-    usb_host_cleanup(ctx);
-
-    return 0;
-}
-
diff --git a/toolbox/md5.c b/toolbox/md5.c
deleted file mode 100644
index 5de4d9e..0000000
--- a/toolbox/md5.c
+++ /dev/null
@@ -1,71 +0,0 @@
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <openssl/md5.h>
-
-static int usage()
-{
-    fprintf(stderr,"md5 file ...\n");
-    return -1;
-}
-
-static int do_md5(const char *path)
-{
-    unsigned int i;
-    int fd;
-    MD5_CTX md5_ctx;
-    unsigned char md5[MD5_DIGEST_LENGTH];
-
-    fd = open(path, O_RDONLY);
-    if (fd < 0) {
-        fprintf(stderr,"could not open %s, %s\n", path, strerror(errno));
-        return -1;
-    }
-
-    MD5_Init(&md5_ctx);
-
-    while (1) {
-        char buf[4096];
-        ssize_t rlen;
-        rlen = read(fd, buf, sizeof(buf));
-        if (rlen == 0)
-            break;
-        else if (rlen < 0) {
-            (void)close(fd);
-            fprintf(stderr,"could not read %s, %s\n", path, strerror(errno));
-            return -1;
-        }
-        MD5_Update(&md5_ctx, buf, rlen);
-    }
-    if (close(fd)) {
-        fprintf(stderr,"could not close %s, %s\n", path, strerror(errno));
-        return -1;
-    }
-
-    MD5_Final(md5, &md5_ctx);
-
-    for (i = 0; i < (int)sizeof(md5); i++)
-        printf("%02x", md5[i]);
-    printf("  %s\n", path);
-
-    return 0;
-}
-
-int md5_main(int argc, char *argv[])
-{
-    int i, ret = 0;
-
-    if (argc < 2)
-        return usage();
-
-    /* loop over the file args */
-    for (i = 1; i < argc; i++) {
-        if (do_md5(argv[i]))
-            ret = 1;
-    }
-
-    return ret;
-}
diff --git a/toolbox/mkdir.c b/toolbox/mkdir.c
deleted file mode 100644
index 398d350..0000000
--- a/toolbox/mkdir.c
+++ /dev/null
@@ -1,77 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/limits.h>
-#include <sys/stat.h>
-
-static int usage()
-{
-    fprintf(stderr,"mkdir [OPTION] <target>\n");
-    fprintf(stderr,"    --help           display usage and exit\n");
-    fprintf(stderr,"    -p, --parents    create parent directories as needed\n");
-    return -1;
-}
-
-int mkdir_main(int argc, char *argv[])
-{
-    int ret;
-    if(argc < 2 || strcmp(argv[1], "--help") == 0) {
-        return usage();
-    }
-
-    int recursive = (strcmp(argv[1], "-p") == 0 ||
-                     strcmp(argv[1], "--parents") == 0) ? 1 : 0;
-
-    if(recursive && argc < 3) {
-        // -p specified without a path
-        return usage();
-    }
-
-    if(recursive) {
-        argc--;
-        argv++;
-    }
-
-    char currpath[PATH_MAX], *pathpiece;
-    struct stat st;
-
-    while(argc > 1) {
-        argc--;
-        argv++;
-        if(recursive) {
-            // reset path
-            strcpy(currpath, "");
-            // create the pieces of the path along the way
-            pathpiece = strtok(argv[0], "/");
-            if(argv[0][0] == '/') {
-                // prepend / if needed
-                strcat(currpath, "/");
-            }
-            while(pathpiece != NULL) {
-                if(strlen(currpath) + strlen(pathpiece) + 2/*NUL and slash*/ > PATH_MAX) {
-                    fprintf(stderr, "Invalid path specified: too long\n");
-                    return 1;
-                }
-                strcat(currpath, pathpiece);
-                strcat(currpath, "/");
-                if(stat(currpath, &st) != 0) {
-                    ret = mkdir(currpath, 0777);
-                    if(ret < 0) {
-                        fprintf(stderr, "mkdir failed for %s, %s\n", currpath, strerror(errno));
-                        return ret;
-                    }
-                }
-                pathpiece = strtok(NULL, "/");
-            }
-        } else {
-            ret = mkdir(argv[0], 0777);
-            if(ret < 0) {
-                fprintf(stderr, "mkdir failed for %s, %s\n", argv[0], strerror(errno));
-                return ret;
-            }
-        }
-    }
-    
-    return 0;
-}
diff --git a/toolbox/mknod.c b/toolbox/mknod.c
deleted file mode 100644
index 0fedece..0000000
--- a/toolbox/mknod.c
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Copyright (c) 2014, 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/stat.h>
-
-static int print_usage() {
-    fprintf(stderr, "mknod <path> [b|c|u|p] <major> <minor>\n");
-    return EXIT_FAILURE;
-}
-
-int mknod_main(int argc, char **argv) {
-    char *path = NULL;
-    int major = 0;
-    int minor = 0;
-    int args = 0;
-    mode_t mode = 0660;
-
-    /* Check correct argument count is 3 or 5 */
-    if (argc != 3 && argc != 5) {
-        fprintf(stderr, "Incorrect argument count\n");
-        return print_usage();
-    }
-
-    path = argv[1];
-
-    const char node_type = *argv[2];
-    switch (node_type) {
-    case 'b':
-        mode |= S_IFBLK;
-        args = 5;
-        break;
-    case 'c':
-    case 'u':
-        mode |= S_IFCHR;
-        args = 5;
-        break;
-    case 'p':
-        mode |= S_IFIFO;
-        args = 3;
-        break;
-    default:
-        fprintf(stderr, "Invalid node type '%c'\n", node_type);
-        return print_usage();
-    }
-
-    if (argc != args) {
-        if (args == 5) {
-            fprintf(stderr, "Node type '%c' requires <major> and <minor>\n", node_type);
-        } else {
-            fprintf(stderr, "Node type '%c' does not require <major> and <minor>\n", node_type);
-        }
-        return print_usage();
-    }
-
-    if (args == 5) {
-        major = atoi(argv[3]);
-        minor = atoi(argv[4]);
-    }
-
-    if (mknod(path, mode, makedev(major, minor))) {
-        perror("Unable to create node");
-        return EXIT_FAILURE;
-    }
-    return 0;
-}
diff --git a/toolbox/mkswap.c b/toolbox/mkswap.c
deleted file mode 100644
index 0904152..0000000
--- a/toolbox/mkswap.c
+++ /dev/null
@@ -1,93 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/swap.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-/* XXX This needs to be obtained from kernel headers. See b/9336527 */
-struct linux_swap_header {
-    char            bootbits[1024]; /* Space for disklabel etc. */
-    uint32_t        version;
-    uint32_t        last_page;
-    uint32_t        nr_badpages;
-    unsigned char   sws_uuid[16];
-    unsigned char   sws_volume[16];
-    uint32_t        padding[117];
-    uint32_t        badpages[1];
-};
-
-#define MAGIC_SWAP_HEADER     "SWAPSPACE2"
-#define MAGIC_SWAP_HEADER_LEN 10
-#define MIN_PAGES             10
-
-int mkswap_main(int argc, char **argv)
-{
-    int err = 0;
-    int fd;
-    ssize_t len;
-    off_t swap_size;
-    int pagesize;
-    struct linux_swap_header sw_hdr;
-
-    if (argc != 2) {
-        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
-        return -EINVAL;
-    }
-
-    fd = open(argv[1], O_WRONLY);
-    if (fd < 0) {
-        err = errno;
-        fprintf(stderr, "Cannot open %s\n", argv[1]);
-        return err;
-    }
-
-    pagesize = getpagesize();
-    /* Determine the length of the swap file */
-    swap_size = lseek(fd, 0, SEEK_END);
-    if (swap_size < MIN_PAGES * pagesize) {
-        fprintf(stderr, "Swap file needs to be at least %dkB\n",
-            (MIN_PAGES * pagesize) >> 10);
-        err = -ENOSPC;
-        goto err;
-    }
-    if (lseek(fd, 0, SEEK_SET)) {
-        err = errno;
-        fprintf(stderr, "Can't seek to the beginning of the file\n");
-        goto err;
-    }
-
-    memset(&sw_hdr, 0, sizeof(sw_hdr));
-    sw_hdr.version = 1;
-    sw_hdr.last_page = (swap_size / pagesize) - 1;
-
-    len = write(fd, &sw_hdr, sizeof(sw_hdr));
-    if (len != sizeof(sw_hdr)) {
-        err = errno;
-        fprintf(stderr, "Failed to write swap header into %s\n", argv[1]);
-        goto err;
-    }
-
-    /* Write the magic header */
-    if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) {
-        err = errno;
-        fprintf(stderr, "Failed to seek into %s\n", argv[1]);
-        goto err;
-    }
-
-    len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN);
-    if (len != MAGIC_SWAP_HEADER_LEN) {
-        err = errno;
-        fprintf(stderr, "Failed to write magic swap header into %s\n", argv[1]);
-        goto err;
-    }
-
-    if (fsync(fd) < 0) {
-        err = errno;
-        fprintf(stderr, "Failed to sync %s\n", argv[1]);
-        goto err;
-    }
-err:
-    close(fd);
-    return err;
-}
diff --git a/toolbox/netstat.c b/toolbox/netstat.c
deleted file mode 100644
index 05dc640..0000000
--- a/toolbox/netstat.c
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * Copyright (c) 2008, 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <arpa/inet.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-
-typedef union iaddr iaddr;
-typedef union iaddr6 iaddr6;
-
-union iaddr {
-    unsigned u;
-    unsigned char b[4];
-};
-
-union iaddr6 {
-    struct {
-        unsigned a;
-        unsigned b;
-        unsigned c;
-        unsigned d;
-    } u;
-    unsigned char b[16];
-};
-
-static const char *state2str(unsigned state)
-{
-    switch(state){
-    case 0x1: return "ESTABLISHED";
-    case 0x2: return "SYN_SENT";
-    case 0x3: return "SYN_RECV";
-    case 0x4: return "FIN_WAIT1";
-    case 0x5: return "FIN_WAIT2";
-    case 0x6: return "TIME_WAIT";
-    case 0x7: return "CLOSE";
-    case 0x8: return "CLOSE_WAIT";
-    case 0x9: return "LAST_ACK";
-    case 0xA: return "LISTEN";
-    case 0xB: return "CLOSING";
-    default: return "UNKNOWN";
-    }
-}
-
-/* addr + : + port + \0 */
-#define ADDR_LEN INET6_ADDRSTRLEN + 1 + 5 + 1
-
-static void addr2str(int af, const void *addr, unsigned port, char *buf)
-{
-    if (inet_ntop(af, addr, buf, ADDR_LEN) == NULL) {
-        *buf = '\0';
-        return;
-    }
-    size_t len = strlen(buf);
-    if (port) {
-        snprintf(buf+len, ADDR_LEN-len, ":%d", port);
-    } else {
-        strncat(buf+len, ":*", ADDR_LEN-len-1);
-    }
-}
-
-static void ipv4(const char *filename, const char *label) {
-    FILE *fp = fopen(filename, "r");
-    if (fp == NULL) {
-        return;
-    }
-    char buf[BUFSIZ];
-    fgets(buf, BUFSIZ, fp);
-    while (fgets(buf, BUFSIZ, fp)){
-        char lip[ADDR_LEN];
-        char rip[ADDR_LEN];
-        iaddr laddr, raddr;
-        unsigned lport, rport, state, txq, rxq, num;
-        int n = sscanf(buf, " %d: %x:%x %x:%x %x %x:%x",
-                       &num, &laddr.u, &lport, &raddr.u, &rport,
-                       &state, &txq, &rxq);
-        if (n == 8) {
-            addr2str(AF_INET, &laddr, lport, lip);
-            addr2str(AF_INET, &raddr, rport, rip);
-
-            printf("%4s  %6d %6d %-22s %-22s %s\n",
-                   label, rxq, txq, lip, rip,
-                   state2str(state));
-        }
-    }
-    fclose(fp);
-}
-
-static void ipv6(const char *filename, const char *label) {
-    FILE *fp = fopen(filename, "r");
-    if (fp == NULL) {
-        return;
-    }
-    char buf[BUFSIZ];
-    fgets(buf, BUFSIZ, fp);
-    while (fgets(buf, BUFSIZ, fp)){
-        char lip[ADDR_LEN];
-        char rip[ADDR_LEN];
-        iaddr6 laddr6, raddr6;
-        unsigned lport, rport, state, txq, rxq, num;
-        int n = sscanf(buf, " %d: %8x%8x%8x%8x:%x %8x%8x%8x%8x:%x %x %x:%x",
-                       &num, &laddr6.u.a, &laddr6.u.b, &laddr6.u.c, &laddr6.u.d, &lport,
-                       &raddr6.u.a, &raddr6.u.b, &raddr6.u.c, &raddr6.u.d, &rport,
-                       &state, &txq, &rxq);
-        if (n == 14) {
-            addr2str(AF_INET6, &laddr6, lport, lip);
-            addr2str(AF_INET6, &raddr6, rport, rip);
-
-            printf("%4s  %6d %6d %-22s %-22s %s\n",
-                   label, rxq, txq, lip, rip,
-                   state2str(state));
-        }
-    }
-    fclose(fp);
-}
-
-int netstat_main(int argc, char *argv[])
-{
-    printf("Proto Recv-Q Send-Q Local Address          Foreign Address        State\n");
-    ipv4("/proc/net/tcp",  "tcp");
-    ipv4("/proc/net/udp",  "udp");
-    ipv6("/proc/net/tcp6", "tcp6");
-    ipv6("/proc/net/udp6", "udp6");
-    return 0;
-}
diff --git a/toolbox/nohup.c b/toolbox/nohup.c
deleted file mode 100644
index 363999d..0000000
--- a/toolbox/nohup.c
+++ /dev/null
@@ -1,26 +0,0 @@
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-int nohup_main(int argc, char *argv[])
-{
-    if (argc < 2) {
-        fprintf(stderr, "Usage: %s [-n] program args...\n", argv[0]);
-        return EXIT_FAILURE;
-    }
-    signal(SIGHUP, SIG_IGN);
-    argv++;
-    if (strcmp(argv[0], "-n") == 0) {
-        argv++;
-        signal(SIGINT, SIG_IGN);
-        signal(SIGSTOP, SIG_IGN);
-        signal(SIGTTIN, SIG_IGN);
-        signal(SIGTTOU, SIG_IGN);
-        signal(SIGQUIT, SIG_IGN);
-        signal(SIGTERM, SIG_IGN);
-    }
-    execvp(argv[0], argv);
-    perror(argv[0]);
-    return EXIT_FAILURE;
-}
diff --git a/toolbox/notify.c b/toolbox/notify.c
deleted file mode 100644
index c983ed5..0000000
--- a/toolbox/notify.c
+++ /dev/null
@@ -1,145 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdint.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <sys/inotify.h>
-#include <errno.h>
-
-int notify_main(int argc, char *argv[])
-{
-    int c;
-    int nfd, ffd;
-    int res;
-	char event_buf[512];
-    struct inotify_event *event;
-	int event_mask = IN_ALL_EVENTS;
-    int event_count = 1;
-	int print_files = 0;
-	int verbose = 2;
-	int width = 80;
-	char **file_names;
-	int file_count;
-	int id_offset = 0;
-	int i;
-	char *buf;
-
-    do {
-        c = getopt(argc, argv, "m:c:pv:w:");
-        if (c == EOF)
-            break;
-        switch (c) {
-        case 'm':
-            event_mask = strtol(optarg, NULL, 0);
-            break;
-        case 'c':
-            event_count = atoi(optarg);
-            break;
-		case 'p':
-			print_files = 1;
-			break;
-        case 'v':
-            verbose = atoi(optarg);
-            break;
-        case 'w':
-            width = atoi(optarg);
-            break;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            exit(1);
-        }
-    } while (1);
-
-    if (argc <= optind) {
-        fprintf(stderr, "Usage: %s [-m eventmask] [-c count] [-p] [-v verbosity] path [path ...]\n", argv[0]);
-		return 1;
-    }
-
-    nfd = inotify_init();
-    if(nfd < 0) {
-        fprintf(stderr, "inotify_init failed, %s\n", strerror(errno));
-        return 1;
-    }
-	file_names = argv + optind;
-	file_count = argc - optind;
-	for(i = 0; i < file_count; i++) {
-		res = inotify_add_watch(nfd, file_names[i], event_mask);
-		if(res < 0) {
-	        fprintf(stderr, "inotify_add_watch failed for %s, %s\n", file_names[i], strerror(errno));
-			return 1;
-		}
-		if(i == 0)
-			id_offset = -res;
-		if(res + id_offset != i) {
-			fprintf(stderr, "%s got unexpected id %d instead of %d\n", file_names[i], res, i);
-			return 1;
-		}
-	}
-
-	buf = malloc(width + 2);
-    
-    while(1) {
-		int event_pos = 0;
-        res = read(nfd, event_buf, sizeof(event_buf));
-        if(res < (int)sizeof(*event)) {
-			if(errno == EINTR)
-				continue;
-            fprintf(stderr, "could not get event, %s\n", strerror(errno));
-            return 1;
-        }
-		//printf("got %d bytes of event information\n", res);
-		while(res >= (int)sizeof(*event)) {
-			int event_size;
-			event = (struct inotify_event *)(event_buf + event_pos);
-			if(verbose >= 2)
-		        printf("%s: %08x %08x \"%s\"\n", file_names[event->wd + id_offset], event->mask, event->cookie, event->len ? event->name : "");
-			else if(verbose >= 2)
-		        printf("%s: %08x \"%s\"\n", file_names[event->wd + id_offset], event->mask, event->len ? event->name : "");
-			else if(verbose >= 1)
-		        printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
-			if(print_files && (event->mask & IN_MODIFY)) {
-				char filename[512];
-				ssize_t read_len;
-				char *display_name;
-				int buflen;
-				strcpy(filename, file_names[event->wd + id_offset]);
-				if(event->len) {
-					strcat(filename, "/");
-					strcat(filename, event->name);
-				}
-				ffd = open(filename, O_RDONLY);
-				display_name = (verbose >= 2 || event->len == 0) ? filename : event->name;
-				buflen = width - strlen(display_name);
-				read_len = read(ffd, buf, buflen);
-				if(read_len > 0) {
-					if(read_len < buflen && buf[read_len-1] != '\n') {
-						buf[read_len] = '\n';
-						read_len++;
-					}
-					if(read_len == buflen) {
-						buf[--read_len] = '\0';
-						buf[--read_len] = '\n';
-						buf[--read_len] = '.';
-						buf[--read_len] = '.';
-						buf[--read_len] = '.';
-					}
-					else {
-						buf[read_len] = '\0';
-					}
-					printf("%s: %s", display_name, buf);
-				}
-				close(ffd);
-			}
-	        if(event_count && --event_count == 0)
-	            return 0;
-			event_size = sizeof(*event) + event->len;
-			res -= event_size;
-			event_pos += event_size;
-		}
-    }
-
-    return 0;
-}
diff --git a/toolbox/ps.c b/toolbox/ps.c
index 5458f6b..d0a8db3 100644
--- a/toolbox/ps.c
+++ b/toolbox/ps.c
@@ -1,15 +1,13 @@
+#include <ctype.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <pwd.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <ctype.h>
-#include <fcntl.h>
-
 #include <string.h>
-
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <dirent.h>
-
-#include <pwd.h>
+#include <unistd.h>
 
 #include <cutils/sched_policy.h>
 
@@ -32,6 +30,7 @@
 #define SHOW_ABI 64
 
 static int display_flags = 0;
+static int ppid_filter = 0;
 
 static void print_exe_abi(int pid);
 
@@ -147,7 +146,11 @@
         strcpy(user,pw->pw_name);
     }
 
-    if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) {
+    if(ppid_filter != 0 && ppid != ppid_filter) {
+        return 0;
+    }
+
+    if(!namefilter || !strncmp(cmdline[0] ? cmdline : name, namefilter, strlen(namefilter))) {
         if (display_flags & SHOW_MACLABEL) {
             fd = open(macline, O_RDONLY);
             strcpy(macline, "-");
@@ -268,6 +271,10 @@
             display_flags |= SHOW_CPU;
         } else if(!strcmp(argv[1],"--abi")) {
             display_flags |= SHOW_ABI;
+        } else if(!strcmp(argv[1],"--ppid")) {
+            ppid_filter = atoi(argv[2]);
+            argc--;
+            argv++;
         } else if(isdigit(argv[1][0])){
             pidfilter = atoi(argv[1]);
         } else {
diff --git a/toolbox/pwcache.c b/toolbox/pwcache.c
deleted file mode 100644
index 9d81981..0000000
--- a/toolbox/pwcache.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (C) 2014, 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.
- */
-
-#include <grp.h>
-#include <pwd.h>
-#include <stdio.h>
-#include <sys/types.h>
-
-int uid_from_user(const char* name, uid_t* uid) {
-  struct passwd* pw = getpwnam(name);
-  if (pw == NULL) {
-    return -1;
-  }
-  *uid = pw->pw_uid;
-  return 0;
-}
-
-char* group_from_gid(gid_t gid, int noname) {
-  struct group* g = getgrgid(gid);
-  if (g == NULL) {
-    static char buf[32];
-    snprintf(buf, sizeof(buf), "%lu", (long) gid);
-    return noname ? NULL : buf;
-  }
-  return g->gr_name;
-}
-
-char* user_from_uid(uid_t uid, int noname) {
-  struct passwd* pw = getpwuid(uid);
-  if (pw == NULL) {
-    static char buf[32];
-    snprintf(buf, sizeof(buf), "%lu", (long) uid);
-    return noname ? NULL : buf;
-  }
-  return pw->pw_name;
-}
diff --git a/toolbox/r.c b/toolbox/r.c
index 3b80db7..b96cdb2 100644
--- a/toolbox/r.c
+++ b/toolbox/r.c
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <unistd.h>
 
 #if __LP64__
 #define strtoptr strtoull
@@ -18,7 +19,7 @@
     return -1;
 }
 
-int r_main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
     if(argc < 2) return usage();
 
diff --git a/toolbox/readlink.c b/toolbox/readlink.c
deleted file mode 100644
index d114e20..0000000
--- a/toolbox/readlink.c
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Copyright (c) 2013, 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-static int skip_newline, quiet_errors, canonicalize;
-
-static void usage(char* name) {
-    fprintf(stderr, "Usage: %s [OPTION]... FILE\n", name);
-}
-
-int readlink_main(int argc, char* argv[]) {
-    int c;
-    while ((c = getopt(argc, argv, "nfqs")) != -1) {
-        switch (c) {
-        case 'n':
-            skip_newline = 1;
-            break;
-        case 'f':
-            canonicalize = 1;
-            break;
-        case 'q':
-        case 's':
-            quiet_errors = 1;
-            break;
-        case '?':
-        default:
-            usage(argv[0]);
-            return EXIT_FAILURE;
-        }
-    }
-    int index = optind;
-    if (argc - index != 1) {
-        usage(argv[0]);
-        return EXIT_FAILURE;
-    }
-
-    char name[PATH_MAX+1];
-    if (canonicalize) {
-        if(!realpath(argv[optind], name)) {
-            if (!quiet_errors) {
-                perror("readlink");
-            }
-            return EXIT_FAILURE;
-        }
-    } else {
-        ssize_t len = readlink(argv[1], name, PATH_MAX);
-
-        if (len < 0) {
-            if (!quiet_errors) {
-                perror("readlink");
-            }
-            return EXIT_FAILURE;
-        }
-        name[len] = '\0';
-    }
-
-    fputs(name, stdout);
-    if (!skip_newline) {
-        fputs("\n", stdout);
-    }
-
-    return EXIT_SUCCESS;
-}
diff --git a/toolbox/readtty.c b/toolbox/readtty.c
deleted file mode 100644
index 2b27548..0000000
--- a/toolbox/readtty.c
+++ /dev/null
@@ -1,183 +0,0 @@
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-
-struct {
-    char key;
-    char *chars;
-} map[] = {
-    { '1', "_ -1?!,.:;\"'<=>()_" },
-    { '2', "Cabc2ABC" },
-    { '3', "Fdef3DEF" },
-    { '4', "Ighi4GHI" },
-    { '5', "Ljkl5JKL" },
-    { '6', "Omno6MNO" },
-    { '7', "Spqrs7PQRS" },
-    { '8', "Vtuv8TUV" },
-    { '9', "Zwxyz9WXYZ" },
-    { '0', "*+&0@/#*" },
-};
-
-char next_char(char key, char current)
-{
-    int i;
-    char *next;
-    for(i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
-        if(key == map[i].key) {
-            next = strchr(map[i].chars, current);
-            if(next && next[1])
-                return next[1];
-            return map[i].chars[1];
-        }
-    }
-    return key;
-}
-
-char prev_char(char key, char current)
-{
-    int i;
-    char *next;
-    for(i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
-        if(key == map[i].key) {
-            next = strchr(map[i].chars+1, current);
-            if(next && next[-1])
-                return next[-1];
-            return map[i].chars[1];
-        }
-    }
-    return key;
-}
-
-int readtty_main(int argc, char *argv[])
-{
-    int c;
-    //int flags;
-    char buf[1];
-    int res;
-    struct termios ttyarg;
-    struct termios savedttyarg;
-    int nonblock = 0;
-    int timeout = 0;
-    int flush = 0;
-    int phone = 0;
-    char *accept = NULL;
-    char *rejectstring = NULL;
-    char last_char_in = 0;
-    char current_char = 0;
-    char *exit_string = NULL;
-    int exit_match = 0;
-
-    do {
-        c = getopt(argc, argv, "nt:fa:r:pe:");
-        if (c == EOF)
-            break;
-        switch (c) {
-        case 't':
-            timeout = atoi(optarg);
-            break;
-        case 'n':
-            nonblock = 1;
-            break;
-        case 'f':
-            flush = 1;
-            break;
-        case 'a':
-            accept = optarg;
-            break;
-        case 'r':
-            rejectstring = optarg;
-            break;
-        case 'p':
-            phone = 1;
-            break;
-        case 'e':
-            exit_string = optarg;
-            break;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            exit(1);
-        }
-    } while (1);
-
-    if(flush)
-        tcflush(STDIN_FILENO, TCIFLUSH);
-    ioctl(STDIN_FILENO, TCGETS , &savedttyarg) ;       /* set changed tty arguments */
-    ttyarg = savedttyarg;
-    ttyarg.c_cc[VMIN] = (timeout > 0 || nonblock) ? 0 : 1;                /* minimum of 0 chars */
-    ttyarg.c_cc[VTIME] = timeout;              /* wait max 15/10 sec */
-    ttyarg.c_iflag = BRKINT | ICRNL; 
-    ttyarg.c_lflag &= ~(ECHO | ICANON);
-    ioctl(STDIN_FILENO, TCSETS , &ttyarg);
-
-    while (1) {
-        res = read(STDIN_FILENO, buf, 1);
-        if(res <= 0) {
-            if(phone) {
-                if(current_char) {
-                    write(STDERR_FILENO, &current_char, 1);
-                    write(STDOUT_FILENO, &current_char, 1);
-                    if(exit_string && current_char == exit_string[exit_match]) {
-                        exit_match++;
-                        if(exit_string[exit_match] == '\0')
-                            break;
-                    }
-                    else
-                        exit_match = 0;
-                    current_char = 0;
-                }
-                continue;
-            }
-            break;
-        }
-        if(accept && strchr(accept, buf[0]) == NULL) {
-            if(rejectstring) {
-                write(STDOUT_FILENO, rejectstring, strlen(rejectstring));
-                break;
-            }
-            if(flush)
-                tcflush(STDIN_FILENO, TCIFLUSH);
-            continue;
-        }
-        if(phone) {
-            //if(!isprint(buf[0])) {
-            //  fprintf(stderr, "got unprintable character 0x%x\n", buf[0]);
-            //}
-            if(buf[0] == '\0') {
-                if(current_char) {
-                    current_char = prev_char(last_char_in, current_char);
-                    write(STDERR_FILENO, &current_char, 1);
-                    write(STDERR_FILENO, "\b", 1);
-                }
-                continue;
-            }
-            if(current_char && buf[0] != last_char_in) {
-                write(STDERR_FILENO, &current_char, 1);
-                write(STDOUT_FILENO, &current_char, 1);
-                if(exit_string && current_char == exit_string[exit_match]) {
-                    exit_match++;
-                    if(exit_string[exit_match] == '\0')
-                        break;
-                }
-                else
-                    exit_match = 0;
-                current_char = 0;
-            }
-            last_char_in = buf[0];
-            current_char = next_char(last_char_in, current_char);
-            write(STDERR_FILENO, &current_char, 1);
-            write(STDERR_FILENO, "\b", 1);
-            continue;
-        }
-        write(STDOUT_FILENO, buf, 1);
-        break;
-    }
-    ioctl(STDIN_FILENO, TCSETS , &savedttyarg) ;       /* set changed tty arguments */
-
-    return 0;
-}
diff --git a/toolbox/renice.c b/toolbox/renice.c
index 9dfeb51..99a06f4 100644
--- a/toolbox/renice.c
+++ b/toolbox/renice.c
@@ -32,6 +32,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sched.h>
diff --git a/toolbox/restorecon.c b/toolbox/restorecon.c
index 3568625..cb5799e 100644
--- a/toolbox/restorecon.c
+++ b/toolbox/restorecon.c
@@ -1,6 +1,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 #include <selinux/android.h>
diff --git a/toolbox/rmmod.c b/toolbox/rmmod.c
deleted file mode 100644
index c7e0d6a..0000000
--- a/toolbox/rmmod.c
+++ /dev/null
@@ -1,53 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <malloc.h>
-#include <errno.h>
-#include <asm/unistd.h>
-
-extern int delete_module(const char *, unsigned int);
-
-int rmmod_main(int argc, char **argv)
-{
-	int ret, i;
-	char *modname, *dot;
-
-	/* make sure we've got an argument */
-	if (argc < 2) {
-		fprintf(stderr, "usage: rmmod <module>\n");
-		return -1;
-	}
-
-	/* if given /foo/bar/blah.ko, make a weak attempt
-	 * to convert to "blah", just for convenience
-	 */
-	modname = strrchr(argv[1], '/');
-	if (!modname)
-		modname = argv[1];
-	else modname++;
-
-	dot = strchr(argv[1], '.');
-	if (dot)
-		*dot = '\0';
-
-	/* Replace "-" with "_". This would keep rmmod
-	 * compatible with module-init-tools version of
-	 * rmmod
-	 */
-	for (i = 0; modname[i] != '\0'; i++) {
-		if (modname[i] == '-')
-			modname[i] = '_';
-	}
-
-	/* pass it to the kernel */
-	ret = delete_module(modname, O_NONBLOCK | O_EXCL);
-	if (ret != 0) {
-		fprintf(stderr, "rmmod: delete_module '%s' failed (errno %d)\n",
-						modname, errno);
-		return -1;
-	}
-
-	return 0;
-}
-
diff --git a/toolbox/rotatefb.c b/toolbox/rotatefb.c
deleted file mode 100644
index 2ff4127..0000000
--- a/toolbox/rotatefb.c
+++ /dev/null
@@ -1,71 +0,0 @@
-#include <ctype.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-#include <errno.h>
-#include <linux/fb.h>
-
-
-int rotatefb_main(int argc, char *argv[])
-{
-    int c;
-    char *fbdev = "/dev/graphics/fb0";
-    int rotation = 0;
-    int fd;
-    int res;
-    struct fb_var_screeninfo fbinfo;
-
-    do {
-        c = getopt(argc, argv, "d:");
-        if (c == EOF)
-            break;
-        switch (c) {
-        case 'd':
-            fbdev = optarg;
-            break;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            exit(1);
-        }
-    } while (1);
-
-    if(optind + 1 != argc) {
-        fprintf(stderr, "%s: specify rotation\n", argv[0]);
-        exit(1);
-    }
-    rotation = atoi(argv[optind]);
-
-    fd = open(fbdev, O_RDWR);
-    if(fd < 0) {
-        fprintf(stderr, "cannot open %s\n", fbdev);
-        return 1;
-    }
-
-    res = ioctl(fd, FBIOGET_VSCREENINFO, &fbinfo);
-    if(res < 0) {
-        fprintf(stderr, "failed to get fbinfo: %s\n", strerror(errno));
-        return 1;
-    }
-    if((fbinfo.rotate ^ rotation) & 1) {
-        unsigned int xres = fbinfo.yres;
-        fbinfo.yres = fbinfo.xres;
-        fbinfo.xres = xres;
-        fbinfo.xres_virtual = fbinfo.xres;
-        fbinfo.yres_virtual = fbinfo.yres * 2;
-        if(fbinfo.yoffset == xres)
-            fbinfo.yoffset = fbinfo.yres;
-    }
-    fbinfo.rotate = rotation; 
-    res = ioctl(fd, FBIOPUT_VSCREENINFO, &fbinfo);
-    if(res < 0) {
-        fprintf(stderr, "failed to set fbinfo: %s\n", strerror(errno));
-        return 1;
-    }
-
-    return 0;
-}
diff --git a/toolbox/sendevent.c b/toolbox/sendevent.c
index 9b813f6..4d0ca17 100644
--- a/toolbox/sendevent.c
+++ b/toolbox/sendevent.c
@@ -1,49 +1,12 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/input.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdint.h>
-#include <fcntl.h>
 #include <sys/ioctl.h>
-//#include <linux/input.h> // this does not compile
-#include <errno.h>
-
-
-// from <linux/input.h>
-
-struct input_event {
-	struct timeval time;
-	__u16 type;
-	__u16 code;
-	__s32 value;
-};
-
-#define EVIOCGVERSION		_IOR('E', 0x01, int)			/* get driver version */
-#define EVIOCGID		_IOR('E', 0x02, struct input_id)	/* get device ID */
-#define EVIOCGKEYCODE		_IOR('E', 0x04, int[2])			/* get keycode */
-#define EVIOCSKEYCODE		_IOW('E', 0x04, int[2])			/* set keycode */
-
-#define EVIOCGNAME(len)		_IOC(_IOC_READ, 'E', 0x06, len)		/* get device name */
-#define EVIOCGPHYS(len)		_IOC(_IOC_READ, 'E', 0x07, len)		/* get physical location */
-#define EVIOCGUNIQ(len)		_IOC(_IOC_READ, 'E', 0x08, len)		/* get unique identifier */
-
-#define EVIOCGKEY(len)		_IOC(_IOC_READ, 'E', 0x18, len)		/* get global keystate */
-#define EVIOCGLED(len)		_IOC(_IOC_READ, 'E', 0x19, len)		/* get all LEDs */
-#define EVIOCGSND(len)		_IOC(_IOC_READ, 'E', 0x1a, len)		/* get all sounds status */
-#define EVIOCGSW(len)		_IOC(_IOC_READ, 'E', 0x1b, len)		/* get all switch states */
-
-#define EVIOCGBIT(ev,len)	_IOC(_IOC_READ, 'E', 0x20 + ev, len)	/* get event bits */
-#define EVIOCGABS(abs)		_IOR('E', 0x40 + abs, struct input_absinfo)		/* get abs value/limits */
-#define EVIOCSABS(abs)		_IOW('E', 0xc0 + abs, struct input_absinfo)		/* set abs value/limits */
-
-#define EVIOCSFF		_IOC(_IOC_WRITE, 'E', 0x80, sizeof(struct ff_effect))	/* send a force effect to a force feedback device */
-#define EVIOCRMFF		_IOW('E', 0x81, int)			/* Erase a force effect */
-#define EVIOCGEFFECTS		_IOR('E', 0x84, int)			/* Report number of effects playable at the same time */
-
-#define EVIOCGRAB		_IOW('E', 0x90, int)			/* Grab/Release device */
-
-// end <linux/input.h>
-
-
+#include <unistd.h>
 
 int sendevent_main(int argc, char *argv[])
 {
diff --git a/toolbox/setenforce.c b/toolbox/setenforce.c
deleted file mode 100644
index 444073d..0000000
--- a/toolbox/setenforce.c
+++ /dev/null
@@ -1,44 +0,0 @@
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <string.h>
-#include <strings.h>
-#include <errno.h>
-#include <selinux/selinux.h>
-
-static void usage(const char *progname)
-{
-    fprintf(stderr, "usage:  %s [ Enforcing | Permissive | 1 | 0 ]\n",
-            progname);
-    exit(1);
-}
-
-int setenforce_main(int argc, char **argv)
-{
-    int rc = 0;
-    if (argc != 2) {
-        usage(argv[0]);
-    }
-
-    if (is_selinux_enabled() <= 0) {
-        fprintf(stderr, "%s: SELinux is disabled\n", argv[0]);
-        return 1;
-    }
-    if (strlen(argv[1]) == 1 && (argv[1][0] == '0' || argv[1][0] == '1')) {
-        rc = security_setenforce(atoi(argv[1]));
-    } else {
-        if (strcasecmp(argv[1], "enforcing") == 0) {
-            rc = security_setenforce(1);
-        } else if (strcasecmp(argv[1], "permissive") == 0) {
-            rc = security_setenforce(0);
-        } else
-            usage(argv[0]);
-    }
-    if (rc < 0) {
-        fprintf(stderr, "%s:  Could not set enforcing status:  %s\n",
-                argv[0], strerror(errno));
-        return 2;
-    }
-    return 0;
-}
diff --git a/toolbox/setkey.c b/toolbox/setkey.c
deleted file mode 100644
index 1ff2774..0000000
--- a/toolbox/setkey.c
+++ /dev/null
@@ -1,89 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <string.h>
-#include <linux/kd.h>
-#include <linux/vt.h>
-#include <errno.h>
-
-static void setkey_usage(char *argv[])
-{
-    fprintf(stderr, "%s [-t <table>] [-k <index>] [-v value] [-r] [-h]\n"
-            "  -t <table> Select table\n"
-            "  -k <index> Select key\n"
-            "  -v <value> Set entry\n"
-            "  -r         Read current entry\n"
-            "  -h         Print help\n", argv[0]);
-}
-
-#define TTYDEV	"/dev/tty0"
-
-int setkey_main(int argc, char *argv[])
-{
-    int fd;
-    struct kbentry kbe;
-    int did_something = 0;
-
-    kbe.kb_table = 0;
-    kbe.kb_index = -1;
-    kbe.kb_value = 0;
-
-    fd = open(TTYDEV, O_RDWR | O_SYNC);
-    if (fd < 0) {
-        fprintf(stderr, "open %s: %s\n", TTYDEV, strerror(errno));
-        return 1;
-    }
-
-    do {
-        int c, ret;
-
-        c = getopt(argc, argv, "t:k:v:hr");
-        if (c == EOF)
-            break;
-
-        switch (c) {
-        case 't':
-            kbe.kb_table = strtol(optarg, NULL, 0);
-            break;
-        case 'k':
-            kbe.kb_index = strtol(optarg, NULL, 0);
-            break;
-        case 'v':
-            kbe.kb_value = strtol(optarg, NULL, 0);
-            ret = ioctl(fd, KDSKBENT, &kbe);
-            if (ret < 0) {
-                fprintf(stderr, "KDSKBENT %d %d %d failed: %s\n",
-                        kbe.kb_table, kbe.kb_index, kbe.kb_value,
-                        strerror(errno));
-                return 1;
-            }
-            did_something = 1;
-            break;
-        case 'r':
-            ret = ioctl(fd, KDGKBENT, &kbe);
-            if (ret < 0) {
-                fprintf(stderr, "KDGKBENT %d %d  failed: %s\n",
-                        kbe.kb_table, kbe.kb_index, strerror(errno));
-                return 1;
-            }
-            printf("0x%x 0x%x 0x%x\n",
-                   kbe.kb_table, kbe.kb_index, kbe.kb_value);
-            did_something = 1;
-            break;
-        case 'h':
-            setkey_usage(argv);
-            return 1;
-        case '?':
-            fprintf(stderr, "%s: invalid option -%c\n",
-                argv[0], optopt);
-            return 1;
-        }
-    } while (1);
-
-    if(optind != argc || !did_something) {
-        setkey_usage(argv);
-        return 1;
-    }
-
-    return 0;
-}
diff --git a/toolbox/smd.c b/toolbox/smd.c
index 91e495c..343dea7 100644
--- a/toolbox/smd.c
+++ b/toolbox/smd.c
@@ -3,6 +3,7 @@
 #include <string.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <unistd.h>
 
 int smd_main(int argc, char **argv)
 {
diff --git a/toolbox/swapoff.c b/toolbox/swapoff.c
deleted file mode 100644
index d8f6a00..0000000
--- a/toolbox/swapoff.c
+++ /dev/null
@@ -1,20 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/swap.h>
-
-int swapoff_main(int argc, char **argv)
-{
-    int err = 0;
-
-    if (argc != 2) {
-        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
-        return -EINVAL;
-    }
-
-    err = swapoff(argv[1]);
-    if (err) {
-        fprintf(stderr, "swapoff failed for %s\n", argv[1]);
-    }
-
-    return err;
-}
diff --git a/toolbox/swapon.c b/toolbox/swapon.c
deleted file mode 100644
index 150701a..0000000
--- a/toolbox/swapon.c
+++ /dev/null
@@ -1,66 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <getopt.h>
-#include <sys/swap.h>
-
-static void usage(char *name)
-{
-    fprintf(stderr, "Usage: %s [-p prio] <filename>\n"
-        "        prio must be between 0 and %d\n", name, SWAP_FLAG_PRIO_MASK);
-}
-
-static int parse_prio(char *prio_str)
-{
-    unsigned long p = strtoul(prio_str, NULL, 10);
-
-    return (p > SWAP_FLAG_PRIO_MASK)? -1 : (int)p;
-}
-
-int swapon_main(int argc, char **argv)
-{
-    int err = 0;
-    int flags = 0;
-    int prio;
-
-    opterr = 0;
-    do {
-        int c = getopt(argc, argv, "hp:");
-        if (c == -1)
-            break;
-
-        switch (c) {
-            case 'p':
-                if (optarg != NULL)
-                    prio = parse_prio(optarg);
-                else
-                    prio = -1;
-
-                if (prio < 0) {
-                    usage(argv[0]);
-                    return -EINVAL;
-                }
-                flags |= SWAP_FLAG_PREFER;
-                flags |= (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK;
-                break;
-            case 'h':
-                usage(argv[0]);
-                return 0;
-            case '?':
-                fprintf(stderr, "unknown option: %c\n", optopt);
-                return -EINVAL;
-        }
-    } while (1);
-
-    if (optind != argc - 1) {
-        usage(argv[0]);
-        return -EINVAL;
-    }
-
-    err = swapon(argv[argc - 1], flags);
-    if (err) {
-        fprintf(stderr, "swapon failed for %s\n", argv[argc - 1]);
-    }
-
-    return err;
-}
diff --git a/toolbox/syren.c b/toolbox/syren.c
deleted file mode 100644
index 47c2460..0000000
--- a/toolbox/syren.c
+++ /dev/null
@@ -1,158 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <malloc.h>
-
-/* ioctl crap */
-#define SYREN_RD		101
-#define SYREN_WR		102
-#define SYREN_OLD_RD	108
-#define SYREN_OLD_WR	109
-
-struct syren_io_args {
-	unsigned long	page;
-	unsigned long	addr;
-	unsigned long	value;
-};
-
-typedef struct {
-	u_char			page;
-	u_char			addr;
-	const char		*name;
-} syren_reg;
-
-static syren_reg registers[] = {
-	{ 0, 0x04, "TOGBR1" },
-	{ 0, 0x05, "TOGBR2" },
-	{ 0, 0x06, "VBDCTRL" },
-	{ 1, 0x07, "VBUCTRL" },
-	{ 1, 0x08, "VBCTRL" },
-	{ 1, 0x09, "PWDNRG" },
-	{ 1, 0x0a, "VBPOP" },
-	{ 1, 0x0b, "VBCTRL2" },
-	{ 1, 0x0f, "VAUDCTRL" },
-	{ 1, 0x10, "VAUSCTRL" },
-	{ 1, 0x11, "VAUOCTRL" },
-	{ 1, 0x12, "VAUDPLL" },
-	{ 1, 0x17, "VRPCSIMR" },
-	{ 0, 0, 0 }
-};
-
-static syren_reg *find_reg(const char *name)
-{
-	int i;
-
-	for (i = 0; registers[i].name != 0; i++) {
-		if (!strcasecmp(registers[i].name, name))
-			return &registers[i];
-	}
-
-	return NULL;
-}
-
-static int usage(void)
-{
-	fprintf(stderr, "usage: syren [r/w] [REGNAME | page:addr] (value)\n");
-	return 1;
-}
-
-int
-syren_main(int argc, char **argv)
-{
-	int cmd = -1;
-	syren_reg *r;
-	struct syren_io_args sio;
-	char name[32];
-	int fd;
-
-	if (argc < 3) {
-		return usage();
-	}
-
-	switch(argv[1][0]) {
-	case 'r':
-		cmd = SYREN_RD;
-		break;
-	case 'w':
-		cmd = SYREN_WR;
-		break;
-	case 'R':
-		cmd = SYREN_OLD_RD;
-		break;
-	case 'W':
-		cmd = SYREN_OLD_WR;
-		break;
-	default:
-		return usage();
-	}
-
-	if (cmd == SYREN_WR || cmd == SYREN_OLD_WR) {
-		if (argc < 4)
-			return usage();
-		sio.value = strtoul(argv[3], 0, 0);
-	}
-
-	fd = open("/dev/eac", O_RDONLY);
-	if (fd < 0) {
-		fprintf(stderr, "can't open /dev/eac\n");
-		return 1;
-	}
-
-	if (strcasecmp(argv[2], "all") == 0) {
-		int i;
-		if (cmd != SYREN_RD && cmd != SYREN_OLD_RD) {
-			fprintf(stderr, "can only read all registers\n");
-			return 1;
-		}
-
-		for (i = 0; registers[i].name; i++) {
-			sio.page = registers[i].page;
-			sio.addr = registers[i].addr;
-			if (ioctl(fd, cmd, &sio) < 0) {
-				fprintf(stderr, "%s: error\n", registers[i].name);
-			} else {
-				fprintf(stderr, "%s: %04x\n", registers[i].name, sio.value);
-			}
-		}
-
-		close(fd);
-		return 0;
-	}
-
-	r = find_reg(argv[2]);
-	if (r == NULL) {
-		if(strlen(argv[2]) >= sizeof(name)){
-			fprintf(stderr, "REGNAME too long\n");
-			return 0;
-		}
-		strlcpy(name, argv[2], sizeof(name));
-		char *addr_str = strchr(argv[2], ':');
-		if (addr_str == NULL)
-			return usage();
-		*addr_str++ = 0;
-		sio.page = strtoul(argv[2], 0, 0);
-		sio.addr = strtoul(addr_str, 0, 0);
-	} else {
-		strlcpy(name, r->name, sizeof(name));
-		sio.page = r->page;
-		sio.addr = r->addr;
-	}
-
-	if (ioctl(fd, cmd, &sio) < 0) {
-		fprintf(stderr, "ioctl(%d) failed\n", cmd);
-		return 1;
-	}
-
-	if (cmd == SYREN_RD || cmd == SYREN_OLD_RD) {
-		printf("%s: %04x\n", name, sio.value);
-	} else {
-		printf("wrote %04x to %s\n", sio.value, name);
-	}
-
-	close(fd);
-
-	return 0;
-}
-
diff --git a/toolbox/touch.c b/toolbox/touch.c
index 52ddf2a..980f0d3 100644
--- a/toolbox/touch.c
+++ b/toolbox/touch.c
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <fcntl.h>
 #include <time.h>
+#include <unistd.h>
 
 static void usage(void)
 {
diff --git a/toolbox/upstream-netbsd/bin/cat/cat.c b/toolbox/upstream-netbsd/bin/cat/cat.c
deleted file mode 100644
index cca8cf5..0000000
--- a/toolbox/upstream-netbsd/bin/cat/cat.c
+++ /dev/null
@@ -1,329 +0,0 @@
-/* $NetBSD: cat.c,v 1.54 2013/12/08 08:32:13 spz Exp $	*/
-
-/*
- * Copyright (c) 1989, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Kevin Fall.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#if HAVE_NBTOOL_CONFIG_H
-#include "nbtool_config.h"
-#endif
-
-#include <sys/cdefs.h>
-#if !defined(lint)
-__COPYRIGHT(
-"@(#) Copyright (c) 1989, 1993\
- The Regents of the University of California.  All rights reserved.");
-#if 0
-static char sccsid[] = "@(#)cat.c	8.2 (Berkeley) 4/27/95";
-#else
-__RCSID("$NetBSD: cat.c,v 1.54 2013/12/08 08:32:13 spz Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <locale.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-static int bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;
-static size_t bsize;
-static int rval;
-static const char *filename;
-
-void cook_args(char *argv[]);
-void cook_buf(FILE *);
-void raw_args(char *argv[]);
-void raw_cat(int);
-
-int
-main(int argc, char *argv[])
-{
-	int ch;
-	struct flock stdout_lock;
-
-	setprogname(argv[0]);
-	(void)setlocale(LC_ALL, "");
-
-	while ((ch = getopt(argc, argv, "B:beflnstuv")) != -1)
-		switch (ch) {
-		case 'B':
-			bsize = (size_t)strtol(optarg, NULL, 0);
-			break;
-		case 'b':
-			bflag = nflag = 1;	/* -b implies -n */
-			break;
-		case 'e':
-			eflag = vflag = 1;	/* -e implies -v */
-			break;
-		case 'f':
-			fflag = 1;
-			break;
-		case 'l':
-			lflag = 1;
-			break;
-		case 'n':
-			nflag = 1;
-			break;
-		case 's':
-			sflag = 1;
-			break;
-		case 't':
-			tflag = vflag = 1;	/* -t implies -v */
-			break;
-		case 'u':
-			setbuf(stdout, NULL);
-			break;
-		case 'v':
-			vflag = 1;
-			break;
-		default:
-		case '?':
-			(void)fprintf(stderr,
-			    "Usage: %s [-beflnstuv] [-B bsize] [-] "
-			    "[file ...]\n", getprogname());
-			return EXIT_FAILURE;
-		}
-	argv += optind;
-
-	if (lflag) {
-		stdout_lock.l_len = 0;
-		stdout_lock.l_start = 0;
-		stdout_lock.l_type = F_WRLCK;
-		stdout_lock.l_whence = SEEK_SET;
-		if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
-			err(EXIT_FAILURE, "stdout");
-	}
-
-	if (bflag || eflag || nflag || sflag || tflag || vflag)
-		cook_args(argv);
-	else
-		raw_args(argv);
-	if (fclose(stdout))
-		err(EXIT_FAILURE, "stdout");
-	return rval;
-}
-
-void
-cook_args(char **argv)
-{
-	FILE *fp;
-
-	fp = stdin;
-	filename = "stdin";
-	do {
-		if (*argv) {
-			if (!strcmp(*argv, "-"))
-				fp = stdin;
-			else if ((fp = fopen(*argv,
-			    fflag ? "rf" : "r")) == NULL) {
-				warn("%s", *argv);
-				rval = EXIT_FAILURE;
-				++argv;
-				continue;
-			}
-			filename = *argv++;
-		}
-		cook_buf(fp);
-		if (fp != stdin)
-			(void)fclose(fp);
-		else
-			clearerr(fp);
-	} while (*argv);
-}
-
-void
-cook_buf(FILE *fp)
-{
-	int ch, gobble, line, prev;
-
-	line = gobble = 0;
-	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
-		if (prev == '\n') {
-			if (ch == '\n') {
-				if (sflag) {
-					if (!gobble && nflag && !bflag)
-						(void)fprintf(stdout,
-							"%6d\t\n", ++line);
-					else if (!gobble && putchar(ch) == EOF)
-						break;
-					gobble = 1;
-					continue;
-				}
-				if (nflag) {
-					if (!bflag) {
-						(void)fprintf(stdout,
-						    "%6d\t", ++line);
-						if (ferror(stdout))
-							break;
-					} else if (eflag) {
-						(void)fprintf(stdout,
-						    "%6s\t", "");
-						if (ferror(stdout))
-							break;
-					}
-				}
-			} else if (nflag) {
-				(void)fprintf(stdout, "%6d\t", ++line);
-				if (ferror(stdout))
-					break;
-			}
-		}
-		gobble = 0;
-		if (ch == '\n') {
-			if (eflag)
-				if (putchar('$') == EOF)
-					break;
-		} else if (ch == '\t') {
-			if (tflag) {
-				if (putchar('^') == EOF || putchar('I') == EOF)
-					break;
-				continue;
-			}
-		} else if (vflag) {
-			if (!isascii(ch)) {
-				if (putchar('M') == EOF || putchar('-') == EOF)
-					break;
-				ch = toascii(ch);
-			}
-			if (iscntrl(ch)) {
-				if (putchar('^') == EOF ||
-				    putchar(ch == '\177' ? '?' :
-				    ch | 0100) == EOF)
-					break;
-				continue;
-			}
-		}
-		if (putchar(ch) == EOF)
-			break;
-	}
-	if (ferror(fp)) {
-		warn("%s", filename);
-		rval = EXIT_FAILURE;
-		clearerr(fp);
-	}
-	if (ferror(stdout))
-		err(EXIT_FAILURE, "stdout");
-}
-
-void
-raw_args(char **argv)
-{
-	int fd;
-
-	fd = fileno(stdin);
-	filename = "stdin";
-	do {
-		if (*argv) {
-			if (!strcmp(*argv, "-")) {
-				fd = fileno(stdin);
-				if (fd < 0)
-					goto skip;
-			} else if (fflag) {
-				struct stat st;
-				fd = open(*argv, O_RDONLY|O_NONBLOCK, 0);
-				if (fd < 0)
-					goto skip;
-
-				if (fstat(fd, &st) == -1) {
-					close(fd);
-					goto skip;
-				}
-				if (!S_ISREG(st.st_mode)) {
-					close(fd);
-					warnx("%s: not a regular file", *argv);
-					goto skipnomsg;
-				}
-			}
-			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
-skip:
-				warn("%s", *argv);
-skipnomsg:
-				rval = EXIT_FAILURE;
-				++argv;
-				continue;
-			}
-			filename = *argv++;
-		} else if (fd < 0) {
-			err(EXIT_FAILURE, "stdin");
-		}
-		raw_cat(fd);
-		if (fd != fileno(stdin))
-			(void)close(fd);
-	} while (*argv);
-}
-
-void
-raw_cat(int rfd)
-{
-	static char *buf;
-	static char fb_buf[BUFSIZ];
-
-	ssize_t nr, nw, off;
-	int wfd;
-
-	wfd = fileno(stdout);
-	if (wfd < 0)
-		err(EXIT_FAILURE, "stdout");
-	if (buf == NULL) {
-		struct stat sbuf;
-
-		if (bsize == 0) {
-			if (fstat(wfd, &sbuf) == 0 && sbuf.st_blksize > 0 &&
-			    (size_t)sbuf.st_blksize > sizeof(fb_buf))
-				bsize = sbuf.st_blksize;
-		}
-		if (bsize > sizeof(fb_buf)) {
-			buf = malloc(bsize);
-			if (buf == NULL)
-				warnx("malloc, using %zu buffer", bsize);
-		}
-		if (buf == NULL) {
-			bsize = sizeof(fb_buf);
-			buf = fb_buf;
-		}
-	}
-	while ((nr = read(rfd, buf, bsize)) > 0)
-		for (off = 0; nr; nr -= nw, off += nw)
-			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
-				err(EXIT_FAILURE, "stdout");
-	if (nr < 0) {
-		warn("%s", filename);
-		rval = EXIT_FAILURE;
-	}
-}
diff --git a/toolbox/upstream-netbsd/bin/cp/cp.c b/toolbox/upstream-netbsd/bin/cp/cp.c
deleted file mode 100644
index 4bbe1b7..0000000
--- a/toolbox/upstream-netbsd/bin/cp/cp.c
+++ /dev/null
@@ -1,548 +0,0 @@
-/* $NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $ */
-
-/*
- * Copyright (c) 1988, 1993, 1994
- *	The Regents of the University of California.  All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * David Hitz of Auspex Systems Inc.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT(
-"@(#) Copyright (c) 1988, 1993, 1994\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)cp.c	8.5 (Berkeley) 4/29/95";
-#else
-__RCSID("$NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $");
-#endif
-#endif /* not lint */
-
-/*
- * Cp copies source files to target files.
- * 
- * The global PATH_T structure "to" always contains the path to the
- * current target file.  Since fts(3) does not change directories,
- * this path can be either absolute or dot-relative.
- * 
- * The basic algorithm is to initialize "to" and use fts(3) to traverse
- * the file hierarchy rooted in the argument list.  A trivial case is the
- * case of 'cp file1 file2'.  The more interesting case is the case of
- * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the
- * path (relative to the root of the traversal) is appended to dir (stored
- * in "to") to form the final target path.
- */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-
-#include <assert.h>
-#include <err.h>
-#include <errno.h>
-#include <fts.h>
-#include <locale.h>
-#include <signal.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "extern.h"
-
-#define	STRIP_TRAILING_SLASH(p) {					\
-        while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/')	\
-                *--(p).p_end = '\0';					\
-}
-
-static char empty[] = "";
-PATH_T to = { .p_end = to.p_path, .target_end = empty  };
-
-uid_t myuid;
-int Hflag, Lflag, Rflag, Pflag, fflag, iflag, lflag, pflag, rflag, vflag, Nflag;
-mode_t myumask;
-sig_atomic_t pinfo;
-
-enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
-
-static int copy(char *[], enum op, int);
-
-static void
-progress(int sig __unused)
-{
-
-	pinfo++;
-}
-
-int
-main(int argc, char *argv[])
-{
-	struct stat to_stat, tmp_stat;
-	enum op type;
-	int ch, fts_options, r, have_trailing_slash;
-	char *target, **src;
-
-	setprogname(argv[0]);
-	(void)setlocale(LC_ALL, "");
-
-	Hflag = Lflag = Pflag = Rflag = 0;
-	while ((ch = getopt(argc, argv, "HLNPRfailprv")) != -1) 
-		switch (ch) {
-		case 'H':
-			Hflag = 1;
-			Lflag = Pflag = 0;
-			break;
-		case 'L':
-			Lflag = 1;
-			Hflag = Pflag = 0;
-			break;
-		case 'N':
-			Nflag = 1;
-			break;
-		case 'P':
-			Pflag = 1;
-			Hflag = Lflag = 0;
-			break;
-		case 'R':
-			Rflag = 1;
-			break;
-		case 'a':
-			Pflag = 1;
-			pflag = 1;
-			Rflag = 1;
-			Hflag = Lflag = 0;
-			break;
-		case 'f':
-			fflag = 1;
-			iflag = 0;
-			break;
-		case 'i':
-			iflag = isatty(fileno(stdin));
-			fflag = 0;
-			break;
-		case 'l':
-			lflag = 1;
-			break;
-		case 'p':
-			pflag = 1;
-			break;
-		case 'r':
-			rflag = 1;
-			break;
-		case 'v':
-			vflag = 1;
-			break;
-		case '?':
-		default:
-			usage();
-			/* NOTREACHED */
-		}
-	argc -= optind;
-	argv += optind;
-
-	if (argc < 2)
-		usage();
-
-	fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
-	if (rflag) {
-		if (Rflag) {
-			errx(EXIT_FAILURE,
-		    "the -R and -r options may not be specified together.");
-			/* NOTREACHED */
-		}
-		if (Hflag || Lflag || Pflag) {
-			errx(EXIT_FAILURE,
-	"the -H, -L, and -P options may not be specified with the -r option.");
-			/* NOTREACHED */
-		}
-		fts_options &= ~FTS_PHYSICAL;
-		fts_options |= FTS_LOGICAL;
-	}
-
-	if (Rflag) {
-		if (Hflag)
-			fts_options |= FTS_COMFOLLOW;
-		if (Lflag) {
-			fts_options &= ~FTS_PHYSICAL;
-			fts_options |= FTS_LOGICAL;
-		}
-	} else if (!Pflag) {
-		fts_options &= ~FTS_PHYSICAL;
-		fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
-	}
-
-	myuid = getuid();
-
-	/* Copy the umask for explicit mode setting. */
-	myumask = umask(0);
-	(void)umask(myumask);
-
-	/* Save the target base in "to". */
-	target = argv[--argc];
-	if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
-		errx(EXIT_FAILURE, "%s: name too long", target);
-	to.p_end = to.p_path + strlen(to.p_path);
-	have_trailing_slash = (to.p_end[-1] == '/');
-	if (have_trailing_slash)
-		STRIP_TRAILING_SLASH(to);
-	to.target_end = to.p_end;
-
-	/* Set end of argument list for fts(3). */
-	argv[argc] = NULL;     
-	
-	(void)signal(SIGINFO, progress);
-	
-	/*
-	 * Cp has two distinct cases:
-	 *
-	 * cp [-R] source target
-	 * cp [-R] source1 ... sourceN directory
-	 *
-	 * In both cases, source can be either a file or a directory.
-	 *
-	 * In (1), the target becomes a copy of the source. That is, if the
-	 * source is a file, the target will be a file, and likewise for
-	 * directories.
-	 *
-	 * In (2), the real target is not directory, but "directory/source".
-	 */
-	if (Pflag)
-		r = lstat(to.p_path, &to_stat);
-	else
-		r = stat(to.p_path, &to_stat);
-	if (r == -1 && errno != ENOENT) {
-		err(EXIT_FAILURE, "%s", to.p_path);
-		/* NOTREACHED */
-	}
-	if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
-		/*
-		 * Case (1).  Target is not a directory.
-		 */ 
-		if (argc > 1)
-			usage();
-		/*
-		 * Need to detect the case:
-		 *	cp -R dir foo
-		 * Where dir is a directory and foo does not exist, where
-		 * we want pathname concatenations turned on but not for
-		 * the initial mkdir().
-		 */
-		if (r == -1) {
-			if (rflag || (Rflag && (Lflag || Hflag)))
-				r = stat(*argv, &tmp_stat);
-			else
-				r = lstat(*argv, &tmp_stat);
-			if (r == -1) {
-				err(EXIT_FAILURE, "%s", *argv);
-				/* NOTREACHED */
-			}
-			
-			if (S_ISDIR(tmp_stat.st_mode) && (Rflag || rflag))
-				type = DIR_TO_DNE;
-			else
-				type = FILE_TO_FILE;
-		} else
-			type = FILE_TO_FILE;
-
-		if (have_trailing_slash && type == FILE_TO_FILE) {
-			if (r == -1)
-				errx(1, "directory %s does not exist",
-				     to.p_path);
-			else
-				errx(1, "%s is not a directory", to.p_path);
-		}
-	} else {
-		/*
-		 * Case (2).  Target is a directory.
-		 */
-		type = FILE_TO_DIR;
-	}
-
-	/*
-	 * make "cp -rp src/ dst" behave like "cp -rp src dst" not
-	 * like "cp -rp src/. dst"
-	 */
-	for (src = argv; *src; src++) {
-		size_t len = strlen(*src);
-		while (len-- > 1 && (*src)[len] == '/')
-			(*src)[len] = '\0';
-	}
-
-	exit(copy(argv, type, fts_options));
-	/* NOTREACHED */
-}
-
-static int dnestack[MAXPATHLEN]; /* unlikely we'll have more nested dirs */
-static ssize_t dnesp;
-static void
-pushdne(int dne)
-{
-
-	dnestack[dnesp++] = dne;
-	assert(dnesp < MAXPATHLEN);
-}
-
-static int
-popdne(void)
-{
-	int rv;
-
-	rv = dnestack[--dnesp];
-	assert(dnesp >= 0);
-	return rv;
-}
-
-static int
-copy(char *argv[], enum op type, int fts_options)
-{
-	struct stat to_stat;
-	FTS *ftsp;
-	FTSENT *curr;
-	int base, dne, sval;
-	int this_failed, any_failed;
-	size_t nlen;
-	char *p, *target_mid;
-
-	base = 0;	/* XXX gcc -Wuninitialized (see comment below) */
-
-	if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
-		err(EXIT_FAILURE, "%s", argv[0]);
-		/* NOTREACHED */
-	for (any_failed = 0; (curr = fts_read(ftsp)) != NULL;) {
-		this_failed = 0;
-		switch (curr->fts_info) {
-		case FTS_NS:
-		case FTS_DNR:
-		case FTS_ERR:
-			warnx("%s: %s", curr->fts_path,
-					strerror(curr->fts_errno));
-			this_failed = any_failed = 1;
-			continue;
-		case FTS_DC:			/* Warn, continue. */
-			warnx("%s: directory causes a cycle", curr->fts_path);
-			this_failed = any_failed = 1;
-			continue;
-		}
-
-		/*
-		 * If we are in case (2) or (3) above, we need to append the 
-                 * source name to the target name.  
-                 */
-		if (type != FILE_TO_FILE) {
-			if ((curr->fts_namelen +
-			    to.target_end - to.p_path + 1) > MAXPATHLEN) {
-				warnx("%s/%s: name too long (not copied)",
-						to.p_path, curr->fts_name);
-				this_failed = any_failed = 1;
-				continue;
-			}
-
-			/*
-			 * Need to remember the roots of traversals to create
-			 * correct pathnames.  If there's a directory being
-			 * copied to a non-existent directory, e.g.
-			 *	cp -R a/dir noexist
-			 * the resulting path name should be noexist/foo, not
-			 * noexist/dir/foo (where foo is a file in dir), which
-			 * is the case where the target exists.
-			 *
-			 * Also, check for "..".  This is for correct path
-			 * concatentation for paths ending in "..", e.g.
-			 *	cp -R .. /tmp
-			 * Paths ending in ".." are changed to ".".  This is
-			 * tricky, but seems the easiest way to fix the problem.
-			 *
-			 * XXX
-			 * Since the first level MUST be FTS_ROOTLEVEL, base
-			 * is always initialized.
-			 */
-			if (curr->fts_level == FTS_ROOTLEVEL) {
-				if (type != DIR_TO_DNE) {
-					p = strrchr(curr->fts_path, '/');
-					base = (p == NULL) ? 0 : 
-					    (int)(p - curr->fts_path + 1);
-
-					if (!strcmp(&curr->fts_path[base], 
-					    ".."))
-						base += 1;
-				} else
-					base = curr->fts_pathlen;
-			}
-
-			p = &curr->fts_path[base];
-			nlen = curr->fts_pathlen - base;
-			target_mid = to.target_end;
-			if (*p != '/' && target_mid[-1] != '/')
-				*target_mid++ = '/';
-			*target_mid = 0;
-
-			if (target_mid - to.p_path + nlen >= PATH_MAX) {
-				warnx("%s%s: name too long (not copied)",
-				    to.p_path, p);
-				this_failed = any_failed = 1;
-				continue;
-			}
-			(void)strncat(target_mid, p, nlen);
-			to.p_end = target_mid + nlen;
-			*to.p_end = 0;
-			STRIP_TRAILING_SLASH(to);
-		}
-
-		sval = Pflag ? lstat(to.p_path, &to_stat) : stat(to.p_path, &to_stat);
-		/* Not an error but need to remember it happened */
-		if (sval == -1)
-			dne = 1;
-		else {
-			if (to_stat.st_dev == curr->fts_statp->st_dev &&
-			    to_stat.st_ino == curr->fts_statp->st_ino) {
-				warnx("%s and %s are identical (not copied).",
-				    to.p_path, curr->fts_path);
-				this_failed = any_failed = 1;
-				if (S_ISDIR(curr->fts_statp->st_mode))
-					(void)fts_set(ftsp, curr, FTS_SKIP);
-				continue;
-			}
-			if (!S_ISDIR(curr->fts_statp->st_mode) &&
-			    S_ISDIR(to_stat.st_mode)) {
-		warnx("cannot overwrite directory %s with non-directory %s",
-				    to.p_path, curr->fts_path);
-				this_failed = any_failed = 1;
-				continue;
-			}
-			dne = 0;
-		}
-
-		switch (curr->fts_statp->st_mode & S_IFMT) {
-		case S_IFLNK:
-			/* Catch special case of a non dangling symlink */
-			if((fts_options & FTS_LOGICAL) ||
-			   ((fts_options & FTS_COMFOLLOW) && curr->fts_level == 0)) {
-				if (copy_file(curr, dne))
-					this_failed = any_failed = 1;
-			} else {	
-				if (copy_link(curr, !dne))
-					this_failed = any_failed = 1;
-			}
-			break;
-		case S_IFDIR:
-			if (!Rflag && !rflag) {
-				if (curr->fts_info == FTS_D)
-					warnx("%s is a directory (not copied).",
-					    curr->fts_path);
-				(void)fts_set(ftsp, curr, FTS_SKIP);
-				this_failed = any_failed = 1;
-				break;
-			}
-
-                        /*
-                         * Directories get noticed twice:
-                         *  In the first pass, create it if needed.
-                         *  In the second pass, after the children have been copied, set the permissions.
-                         */
-			if (curr->fts_info == FTS_D) /* First pass */
-			{
-				/*
-				 * If the directory doesn't exist, create the new
-				 * one with the from file mode plus owner RWX bits,
-				 * modified by the umask.  Trade-off between being
-				 * able to write the directory (if from directory is
-				 * 555) and not causing a permissions race.  If the
-				 * umask blocks owner writes, we fail..
-				 */
-				pushdne(dne);
-				if (dne) {
-					if (mkdir(to.p_path, 
-					    curr->fts_statp->st_mode | S_IRWXU) < 0)
-						err(EXIT_FAILURE, "%s",
-						    to.p_path);
-						/* NOTREACHED */
-				} else if (!S_ISDIR(to_stat.st_mode)) {
-					errno = ENOTDIR;
-					err(EXIT_FAILURE, "%s",
-						to.p_path);
-					/* NOTREACHED */
-				}
-			}
-			else if (curr->fts_info == FTS_DP) /* Second pass */
-			{
-	                        /*
-				 * If not -p and directory didn't exist, set it to be
-				 * the same as the from directory, umodified by the 
-                        	 * umask; arguably wrong, but it's been that way 
-                        	 * forever.
-				 */
-				if (pflag && setfile(curr->fts_statp, 0))
-					this_failed = any_failed = 1;
-				else if ((dne = popdne()))
-					(void)chmod(to.p_path, 
-					    curr->fts_statp->st_mode);
-			}
-			else
-			{
-				warnx("directory %s encountered when not expected.",
-				    curr->fts_path);
-				this_failed = any_failed = 1;
-				break;
-			}
-
-			break;
-		case S_IFBLK:
-		case S_IFCHR:
-			if (Rflag) {
-				if (copy_special(curr->fts_statp, !dne))
-					this_failed = any_failed = 1;
-			} else
-				if (copy_file(curr, dne))
-					this_failed = any_failed = 1;
-			break;
-		case S_IFIFO:
-			if (Rflag) {
-				if (copy_fifo(curr->fts_statp, !dne))
-					this_failed = any_failed = 1;
-			} else 
-				if (copy_file(curr, dne))
-					this_failed = any_failed = 1;
-			break;
-		default:
-			if (copy_file(curr, dne))
-				this_failed = any_failed = 1;
-			break;
-		}
-		if (vflag && !this_failed)
-			(void)printf("%s -> %s\n", curr->fts_path, to.p_path);
-	}
-	if (errno) {
-		err(EXIT_FAILURE, "fts_read");
-		/* NOTREACHED */
-	}
-	(void)fts_close(ftsp);
-	return (any_failed);
-}
diff --git a/toolbox/upstream-netbsd/bin/cp/extern.h b/toolbox/upstream-netbsd/bin/cp/extern.h
deleted file mode 100644
index e393844..0000000
--- a/toolbox/upstream-netbsd/bin/cp/extern.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/* $NetBSD: extern.h,v 1.17 2012/01/04 15:58:37 christos Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- *
- *	@(#)extern.h	8.2 (Berkeley) 4/1/94
- */
-
-#ifndef _EXTERN_H_
-#define _EXTERN_H_
-
-typedef struct {
-	char *p_end;			/* pointer to NULL at end of path */
-	char *target_end;		/* pointer to end of target base */
-	char p_path[MAXPATHLEN + 1];	/* pointer to the start of a path */
-} PATH_T;
-
-extern PATH_T to;
-extern uid_t myuid;
-extern int Rflag, rflag, Hflag, Lflag, Pflag, fflag, iflag, lflag, pflag, Nflag;
-extern mode_t myumask;
-extern sig_atomic_t pinfo;
-
-#include <sys/cdefs.h>
-
-__BEGIN_DECLS
-int	copy_fifo(struct stat *, int);
-int	copy_file(FTSENT *, int);
-int	copy_link(FTSENT *, int);
-int	copy_special(struct stat *, int);
-int	set_utimes(const char *, struct stat *);
-int	setfile(struct stat *, int);
-void	usage(void) __attribute__((__noreturn__));
-__END_DECLS
-
-#endif /* !_EXTERN_H_ */
diff --git a/toolbox/upstream-netbsd/bin/cp/utils.c b/toolbox/upstream-netbsd/bin/cp/utils.c
deleted file mode 100644
index d8f900a..0000000
--- a/toolbox/upstream-netbsd/bin/cp/utils.c
+++ /dev/null
@@ -1,444 +0,0 @@
-/* $NetBSD: utils.c,v 1.42 2013/12/11 06:00:11 dholland Exp $ */
-
-/*-
- * Copyright (c) 1991, 1993, 1994
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
-#else
-__RCSID("$NetBSD: utils.c,v 1.42 2013/12/11 06:00:11 dholland Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/mman.h>
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/extattr.h>
-
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <fts.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "extern.h"
-
-#define	MMAP_MAX_SIZE	(8 * 1048576)
-#define	MMAP_MAX_WRITE	(64 * 1024)
-
-int
-set_utimes(const char *file, struct stat *fs)
-{
-    static struct timeval tv[2];
-
-#ifdef __ANDROID__
-    tv[0].tv_sec = fs->st_atime;
-    tv[0].tv_usec = 0;
-    tv[1].tv_sec = fs->st_mtime;
-    tv[1].tv_usec = 0;
-
-    if (utimes(file, tv)) {
-        warn("utimes: %s", file);
-        return 1;
-    }
-#else
-    TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
-    TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
-
-    if (lutimes(file, tv)) {
-	warn("lutimes: %s", file);
-	return (1);
-    }
-#endif
-    return (0);
-}
-
-struct finfo {
-	const char *from;
-	const char *to;
-	size_t size;
-};
-
-static void
-progress(const struct finfo *fi, size_t written)
-{
-	int pcent = (int)((100.0 * written) / fi->size);
-
-	pinfo = 0;
-	(void)fprintf(stderr, "%s => %s %zu/%zu bytes %d%% written\n",
-	    fi->from, fi->to, written, fi->size, pcent);
-}
-
-int
-copy_file(FTSENT *entp, int dne)
-{
-	static char buf[MAXBSIZE];
-	struct stat to_stat, *fs;
-	int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount;
-	char *p;
-	size_t ptotal = 0;
-	
-	if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
-		warn("%s", entp->fts_path);
-		return (1);
-	}
-
-	to_fd = -1;
-	fs = entp->fts_statp;
-	tolnk = ((Rflag && !(Lflag || Hflag)) || Pflag);
-
-	/*
-	 * If the file exists and we're interactive, verify with the user.
-	 * If the file DNE, set the mode to be the from file, minus setuid
-	 * bits, modified by the umask; arguably wrong, but it makes copying
-	 * executables work right and it's been that way forever.  (The
-	 * other choice is 666 or'ed with the execute bits on the from file
-	 * modified by the umask.)
-	 */
-	if (!dne) {
-		struct stat sb;
-		int sval;
-
-		if (iflag) {
-			(void)fprintf(stderr, "overwrite %s? ", to.p_path);
-			checkch = ch = getchar();
-			while (ch != '\n' && ch != EOF)
-				ch = getchar();
-			if (checkch != 'y' && checkch != 'Y') {
-				(void)close(from_fd);
-				return (0);
-			}
-		}
-
-		sval = tolnk ?
-			lstat(to.p_path, &sb) : stat(to.p_path, &sb);
-		if (sval == -1) {
-			warn("stat: %s", to.p_path);
-			(void)close(from_fd);
-			return (1);
-		}
-
-		if (!(tolnk && S_ISLNK(sb.st_mode)))
-			to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
-	} else
-		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
-		    fs->st_mode & ~(S_ISUID | S_ISGID));
-
-	if (to_fd == -1 && (fflag || tolnk)) {
-		/*
-		 * attempt to remove existing destination file name and
-		 * create a new file
-		 */
-		(void)unlink(to.p_path);
-		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
-			     fs->st_mode & ~(S_ISUID | S_ISGID));
-	}
-
-	if (to_fd == -1) {
-		warn("%s", to.p_path);
-		(void)close(from_fd);
-		return (1);
-	}
-
-	rval = 0;
-
-	/* if hard linking then simply close the open fds, link and return */
-	if (lflag) {
-		(void)close(from_fd);
-		(void)close(to_fd);
-		(void)unlink(to.p_path);
-		if (link(entp->fts_path, to.p_path)) {
-			warn("%s", to.p_path);
-			return (1);
-		}
-		return (0);
-	}
-
-	/*
-	 * There's no reason to do anything other than close the file
-	 * now if it's empty, so let's not bother.
-	 */
-#ifndef __ANDROID__ // Files in /proc report length 0. mmap will fail but we'll fall back to read.
-	if (fs->st_size > 0) {
-#endif
-		struct finfo fi;
-
-		fi.from = entp->fts_path;
-		fi.to = to.p_path;
-		fi.size = (size_t)fs->st_size;
-
-		/*
-		 * Mmap and write if less than 8M (the limit is so
-		 * we don't totally trash memory on big files).
-		 * This is really a minor hack, but it wins some CPU back.
-		 */
-		bool use_read;
-
-		use_read = true;
-		if (fs->st_size <= MMAP_MAX_SIZE) {
-			size_t fsize = (size_t)fs->st_size;
-			p = mmap(NULL, fsize, PROT_READ, MAP_FILE|MAP_SHARED,
-			    from_fd, (off_t)0);
-			if (p != MAP_FAILED) {
-				size_t remainder;
-
-				use_read = false;
-
-				(void) madvise(p, (size_t)fs->st_size,
-				     MADV_SEQUENTIAL);
-
-				/*
-				 * Write out the data in small chunks to
-				 * avoid locking the output file for a
-				 * long time if the reading the data from
-				 * the source is slow.
-				 */
-				remainder = fsize;
-				do {
-					ssize_t chunk;
-
-					chunk = (remainder > MMAP_MAX_WRITE) ?
-					    MMAP_MAX_WRITE : remainder;
-					if (write(to_fd, &p[fsize - remainder],
-					    chunk) != chunk) {
-						warn("%s", to.p_path);
-						rval = 1;
-						break;
-					}
-					remainder -= chunk;
-					ptotal += chunk;
-					if (pinfo)
-						progress(&fi, ptotal);
-				} while (remainder > 0);
-
-				if (munmap(p, fsize) < 0) {
-					warn("%s", entp->fts_path);
-					rval = 1;
-				}
-			}
-		}
-
-		if (use_read) {
-			while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
-				wcount = write(to_fd, buf, (size_t)rcount);
-				if (rcount != wcount || wcount == -1) {
-					warn("%s", to.p_path);
-					rval = 1;
-					break;
-				}
-				ptotal += wcount;
-				if (pinfo)
-					progress(&fi, ptotal);
-			}
-			if (rcount < 0) {
-				warn("%s", entp->fts_path);
-				rval = 1;
-			}
-		}
-#ifndef __ANDROID__
-	}
-#endif
-
-#ifndef __ANDROID__
-	if (pflag && (fcpxattr(from_fd, to_fd) != 0))
-		warn("%s: error copying extended attributes", to.p_path);
-#endif
-
-	(void)close(from_fd);
-
-	if (rval == 1) {
-		(void)close(to_fd);
-		return (1);
-	}
-
-	if (pflag && setfile(fs, to_fd))
-		rval = 1;
-	/*
-	 * If the source was setuid or setgid, lose the bits unless the
-	 * copy is owned by the same user and group.
-	 */
-#define	RETAINBITS \
-	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
-	if (!pflag && dne
-	    && fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
-		if (fstat(to_fd, &to_stat)) {
-			warn("%s", to.p_path);
-			rval = 1;
-		} else if (fs->st_gid == to_stat.st_gid &&
-		    fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
-			warn("%s", to.p_path);
-			rval = 1;
-		}
-	}
-	if (close(to_fd)) {
-		warn("%s", to.p_path);
-		rval = 1;
-	}
-	/* set the mod/access times now after close of the fd */
-	if (pflag && set_utimes(to.p_path, fs)) { 
-	    rval = 1;
-	}
-	return (rval);
-}
-
-int
-copy_link(FTSENT *p, int exists)
-{
-	int len;
-	char target[MAXPATHLEN];
-
-	if ((len = readlink(p->fts_path, target, sizeof(target)-1)) == -1) {
-		warn("readlink: %s", p->fts_path);
-		return (1);
-	}
-	target[len] = '\0';
-	if (exists && unlink(to.p_path)) {
-		warn("unlink: %s", to.p_path);
-		return (1);
-	}
-	if (symlink(target, to.p_path)) {
-		warn("symlink: %s", target);
-		return (1);
-	}
-	return (pflag ? setfile(p->fts_statp, 0) : 0);
-}
-
-int
-copy_fifo(struct stat *from_stat, int exists)
-{
-	if (exists && unlink(to.p_path)) {
-		warn("unlink: %s", to.p_path);
-		return (1);
-	}
-	if (mkfifo(to.p_path, from_stat->st_mode)) {
-		warn("mkfifo: %s", to.p_path);
-		return (1);
-	}
-	return (pflag ? setfile(from_stat, 0) : 0);
-}
-
-int
-copy_special(struct stat *from_stat, int exists)
-{
-	if (exists && unlink(to.p_path)) {
-		warn("unlink: %s", to.p_path);
-		return (1);
-	}
-	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
-		warn("mknod: %s", to.p_path);
-		return (1);
-	}
-	return (pflag ? setfile(from_stat, 0) : 0);
-}
-
-
-/*
- * Function: setfile
- *
- * Purpose:
- *   Set the owner/group/permissions for the "to" file to the information
- *   in the stat structure.  If fd is zero, also call set_utimes() to set
- *   the mod/access times.  If fd is non-zero, the caller must do a utimes
- *   itself after close(fd).
- */
-int
-setfile(struct stat *fs, int fd)
-{
-	int rval, islink;
-
-	rval = 0;
-	islink = S_ISLNK(fs->st_mode);
-	fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
-
-	/*
-	 * Changing the ownership probably won't succeed, unless we're root
-	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
-	 * the mode; current BSD behavior is to remove all setuid bits on
-	 * chown.  If chown fails, lose setuid/setgid bits.
-	 */
-	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
-	    lchown(to.p_path, fs->st_uid, fs->st_gid)) {
-		if (errno != EPERM) {
-			warn("chown: %s", to.p_path);
-			rval = 1;
-		}
-		fs->st_mode &= ~(S_ISUID | S_ISGID);
-	}
-#ifdef __ANDROID__
-	if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
-#else
-	if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
-#endif
-		warn("chmod: %s", to.p_path);
-		rval = 1;
-	}
-
-#ifndef __ANDROID__
-	if (!islink && !Nflag) {
-		unsigned long fflags = fs->st_flags;
-		/*
-		 * XXX
-		 * NFS doesn't support chflags; ignore errors unless
-		 * there's reason to believe we're losing bits.
-		 * (Note, this still won't be right if the server
-		 * supports flags and we were trying to *remove* flags
-		 * on a file that we copied, i.e., that we didn't create.)
-		 */
-		errno = 0;
-		if ((fd ? fchflags(fd, fflags) :
-		    chflags(to.p_path, fflags)) == -1)
-			if (errno != EOPNOTSUPP || fs->st_flags != 0) {
-				warn("chflags: %s", to.p_path);
-				rval = 1;
-			}
-	}
-#endif
-	/* if fd is non-zero, caller must call set_utimes() after close() */
-	if (fd == 0 && set_utimes(to.p_path, fs))
-	    rval = 1;
-	return (rval);
-}
-
-void
-usage(void)
-{
-	(void)fprintf(stderr,
-	    "usage: %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src target\n"
-	    "       %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src1 ... srcN directory\n",
-	    getprogname(), getprogname());
-	exit(1);
-	/* NOTREACHED */
-}
diff --git a/toolbox/upstream-netbsd/bin/kill/kill.c b/toolbox/upstream-netbsd/bin/kill/kill.c
deleted file mode 100644
index 0592577..0000000
--- a/toolbox/upstream-netbsd/bin/kill/kill.c
+++ /dev/null
@@ -1,253 +0,0 @@
-/* $NetBSD: kill.c,v 1.27 2011/08/29 14:51:18 joerg Exp $ */
-
-/*
- * Copyright (c) 1988, 1993, 1994
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#if !defined(lint) && !defined(SHELL)
-__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)kill.c	8.4 (Berkeley) 4/28/95";
-#else
-__RCSID("$NetBSD: kill.c,v 1.27 2011/08/29 14:51:18 joerg Exp $");
-#endif
-#endif /* not lint */
-
-#include <ctype.h>
-#include <err.h>
-#include <errno.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <limits.h>
-#include <inttypes.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-#include <locale.h>
-#include <sys/ioctl.h>
-
-#ifdef SHELL            /* sh (aka ash) builtin */
-int killcmd(int, char *argv[]);
-#define main killcmd
-#include "../../bin/sh/bltin/bltin.h"
-#endif /* SHELL */ 
-
-__dead static void nosig(char *);
-static void printsignals(FILE *);
-static int signame_to_signum(char *);
-__dead static void usage(void);
-
-int
-main(int argc, char *argv[])
-{
-	int errors;
-	intmax_t numsig, pid;
-	char *ep;
-
-	setprogname(argv[0]);
-	setlocale(LC_ALL, "");
-	if (argc < 2)
-		usage();
-
-	numsig = SIGTERM;
-
-	argc--, argv++;
-	if (strcmp(*argv, "-l") == 0) {
-		argc--, argv++;
-		if (argc > 1)
-			usage();
-		if (argc == 1) {
-			if (isdigit((unsigned char)**argv) == 0)
-				usage();
-			numsig = strtoimax(*argv, &ep, 10);
-			/* check for correctly parsed number */
-			if (*ep != '\0' || numsig == INTMAX_MIN || numsig == INTMAX_MAX) {
-				errx(EXIT_FAILURE, "illegal signal number: %s",
-						*argv);
-				/* NOTREACHED */
-			}
-			if (numsig >= 128)
-				numsig -= 128;
-			/* and whether it fits into signals range */
-			if (numsig <= 0 || numsig >= NSIG)
-				nosig(*argv);
-			printf("%s\n", sys_signame[(int) numsig]);
-			exit(0);
-		}
-		printsignals(stdout);
-		exit(0);
-	}
-
-	if (!strcmp(*argv, "-s")) {
-		argc--, argv++;
-		if (argc < 1) {
-			warnx("option requires an argument -- s");
-			usage();
-		}
-		if (strcmp(*argv, "0")) {
-			if ((numsig = signame_to_signum(*argv)) < 0)
-				nosig(*argv);
-		} else
-			numsig = 0;
-		argc--, argv++;
-	} else if (**argv == '-') {
-		char *sn = *argv + 1;
-		if (isalpha((unsigned char)*sn)) {
-			if ((numsig = signame_to_signum(sn)) < 0)
-				nosig(sn);
-		} else if (isdigit((unsigned char)*sn)) {
-			numsig = strtoimax(sn, &ep, 10);
-			/* check for correctly parsed number */
-			if (*ep || numsig == INTMAX_MIN || numsig == INTMAX_MAX ) {
-				errx(EXIT_FAILURE, "illegal signal number: %s",
-						sn);
-				/* NOTREACHED */
-			}
-			/* and whether it fits into signals range */
-			if (numsig < 0 || numsig >= NSIG)
-				nosig(sn);
-		} else
-			nosig(sn);
-		argc--, argv++;
-	}
-
-	if (argc == 0)
-		usage();
-
-	for (errors = 0; argc; argc--, argv++) {
-#ifdef SHELL
-		extern int getjobpgrp(const char *);
-		if (*argv[0] == '%') {
-			pid = getjobpgrp(*argv);
-			if (pid == 0) {
-				warnx("illegal job id: %s", *argv);
-				errors = 1;
-				continue;
-			}
-		} else 
-#endif
-		{
-			pid = strtoimax(*argv, &ep, 10);
-			/* make sure the pid is a number and fits into pid_t */
-			if (!**argv || *ep || pid == INTMAX_MIN ||
-				pid == INTMAX_MAX || pid != (pid_t) pid) {
-
-				warnx("illegal process id: %s", *argv);
-				errors = 1;
-				continue;
-			}
-		}
-		if (kill((pid_t) pid, (int) numsig) == -1) {
-			warn("%s", *argv);
-			errors = 1;
-		}
-#ifdef SHELL
-		/* Wakeup the process if it was suspended, so it can
-		   exit without an explicit 'fg'. */
-		if (numsig == SIGTERM || numsig == SIGHUP)
-			kill((pid_t) pid, SIGCONT);
-#endif
-	}
-
-	exit(errors);
-	/* NOTREACHED */
-}
-
-static int
-signame_to_signum(char *sig)
-{
-	int n;
-
-	if (strncasecmp(sig, "sig", 3) == 0)
-		sig += 3;
-	for (n = 1; n < NSIG; n++) {
-		if (!strcasecmp(sys_signame[n], sig))
-			return (n);
-	}
-	return (-1);
-}
-
-static void
-nosig(char *name)
-{
-
-	warnx("unknown signal %s; valid signals:", name);
-	printsignals(stderr);
-	exit(1);
-	/* NOTREACHED */
-}
-
-static void
-printsignals(FILE *fp)
-{
-	int sig;
-	int len, nl;
-	const char *name;
-	int termwidth = 80;
-
-	if (isatty(fileno(fp))) {
-		struct winsize win;
-		if (ioctl(fileno(fp), TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
-			termwidth = win.ws_col;
-	}
-
-	for (len = 0, sig = 1; sig < NSIG; sig++) {
-		name = sys_signame[sig];
-		nl = 1 + strlen(name);
-
-		if (len + nl >= termwidth) {
-			fprintf(fp, "\n");
-			len = 0;
-		} else
-			if (len != 0)
-				fprintf(fp, " ");
-		len += nl;
-		fprintf(fp, "%s", name);
-	}
-	if (len != 0)
-		fprintf(fp, "\n");
-}
-
-static void
-usage(void)
-{
-
-	fprintf(stderr, "usage: %s [-s signal_name] pid ...\n"
-	    "       %s -l [exit_status]\n"
-	    "       %s -signal_name pid ...\n"
-	    "       %s -signal_number pid ...\n",
-	    getprogname(), getprogname(), getprogname(), getprogname());
-	exit(1);
-	/* NOTREACHED */
-}
diff --git a/toolbox/upstream-netbsd/bin/ln/ln.c b/toolbox/upstream-netbsd/bin/ln/ln.c
deleted file mode 100644
index 9127477..0000000
--- a/toolbox/upstream-netbsd/bin/ln/ln.c
+++ /dev/null
@@ -1,230 +0,0 @@
-/* $NetBSD: ln.c,v 1.35 2011/08/29 14:38:30 joerg Exp $ */
-
-/*
- * Copyright (c) 1987, 1993, 1994
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)ln.c	8.2 (Berkeley) 3/31/94";
-#else
-__RCSID("$NetBSD: ln.c,v 1.35 2011/08/29 14:38:30 joerg Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-
-#include <err.h>
-#include <errno.h>
-#include <locale.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-static int	fflag;				/* Unlink existing files. */
-static int	hflag;				/* Check new name for symlink first. */
-static int	iflag;				/* Interactive mode. */
-static int	sflag;				/* Symbolic, not hard, link. */
-static int	vflag;                          /* Verbose output */
-
-					/* System link call. */
-static int (*linkf)(const char *, const char *);
-static char   linkch;
-
-static int	linkit(const char *, const char *, int);
-__dead static void	usage(void);
-
-int
-main(int argc, char *argv[])
-{
-	struct stat sb;
-	int ch, exitval;
-	char *sourcedir;
-
-	setprogname(argv[0]);
-	(void)setlocale(LC_ALL, "");
-
-	while ((ch = getopt(argc, argv, "fhinsv")) != -1)
-		switch (ch) {
-		case 'f':
-			fflag = 1;
-			iflag = 0;
-			break;
-		case 'h':
-		case 'n':
-			hflag = 1;
-			break;
-		case 'i':
-			iflag = 1;
-			fflag = 0;
-			break;
-		case 's':
-			sflag = 1;
-			break;
-		case 'v':               
-			vflag = 1;
-			break;
-		case '?':
-		default:
-			usage();
-			/* NOTREACHED */
-		}
-
-	argv += optind;
-	argc -= optind;
-
-	if (sflag) {
-		linkf  = symlink;
-		linkch = '-';
-	} else {
-		linkf  = link;
-		linkch = '=';
-	}
-
-	switch(argc) {
-	case 0:
-		usage();
-		/* NOTREACHED */
-	case 1:				/* ln target */
-		exit(linkit(argv[0], ".", 1));
-		/* NOTREACHED */
-	case 2:				/* ln target source */
-		exit(linkit(argv[0], argv[1], 0));
-		/* NOTREACHED */
-	}
-
-					/* ln target1 target2 directory */
-	sourcedir = argv[argc - 1];
-	if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
-		/* we were asked not to follow symlinks, but found one at
-		   the target--simulate "not a directory" error */
-		errno = ENOTDIR;
-		err(EXIT_FAILURE, "%s", sourcedir);
-		/* NOTREACHED */
-	}
-	if (stat(sourcedir, &sb)) {
-		err(EXIT_FAILURE, "%s", sourcedir);
-		/* NOTREACHED */
-	}
-	if (!S_ISDIR(sb.st_mode)) {
-		usage();
-		/* NOTREACHED */
-	}
-	for (exitval = 0; *argv != sourcedir; ++argv)
-		exitval |= linkit(*argv, sourcedir, 1);
-	exit(exitval);
-	/* NOTREACHED */
-}
-
-static int
-linkit(const char *target, const char *source, int isdir)
-{
-	struct stat sb;
-	const char *p;
-	char path[MAXPATHLEN];
-	int ch, exists, first;
-
-	if (!sflag) {
-		/* If target doesn't exist, quit now. */
-		if (stat(target, &sb)) {
-			warn("%s", target);
-			return (1);
-		}
-	}
-
-	/* If the source is a directory (and not a symlink if hflag),
-	   append the target's name. */
-	if (isdir ||
-	    (!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
-	    (!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
-		if ((p = strrchr(target, '/')) == NULL)
-			p = target;
-		else
-			++p;
-		(void)snprintf(path, sizeof(path), "%s/%s", source, p);
-		source = path;
-	}
-
-	exists = !lstat(source, &sb);
-
-	/*
-	 * If the file exists, then unlink it forcibly if -f was specified
-	 * and interactively if -i was specified.
-	 */
-	if (fflag && exists) {
-		if (unlink(source)) {
-			warn("%s", source);
-			return (1);
-		}
-	} else if (iflag && exists) {
-		fflush(stdout);
-		(void)fprintf(stderr, "replace %s? ", source);
-
-		first = ch = getchar();
-		while (ch != '\n' && ch != EOF)
-			ch = getchar();
-		if (first != 'y' && first != 'Y') {
-			(void)fprintf(stderr, "not replaced\n");
-			return (1);
-		}
-
-		if (unlink(source)) {
-			warn("%s", source);
-			return (1);
-		}
-	}
-
-	/* Attempt the link. */
-	if ((*linkf)(target, source)) {
-		warn("%s", source);
-		return (1);
-	}
-	if (vflag)
-		(void)printf("%s %c> %s\n", source, linkch, target);
-
-	return (0);
-}
-
-static void
-usage(void)
-{
-
-	(void)fprintf(stderr,
-	    "usage:\t%s [-fhinsv] file1 file2\n\t%s [-fhinsv] file ... directory\n",
-	    getprogname(), getprogname());
-	exit(1);
-	/* NOTREACHED */
-}
diff --git a/toolbox/upstream-netbsd/bin/mv/mv.c b/toolbox/upstream-netbsd/bin/mv/mv.c
deleted file mode 100644
index 4be6c30..0000000
--- a/toolbox/upstream-netbsd/bin/mv/mv.c
+++ /dev/null
@@ -1,396 +0,0 @@
-/* $NetBSD: mv.c,v 1.43 2011/08/29 14:46:54 joerg Exp $ */
-
-/*
- * Copyright (c) 1989, 1993, 1994
- *	The Regents of the University of California.  All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Ken Smith of The State University of New York at Buffalo.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)mv.c	8.2 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: mv.c,v 1.43 2011/08/29 14:46:54 joerg Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/time.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <sys/extattr.h>
-
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <grp.h>
-#include <locale.h>
-#include <pwd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "pathnames.h"
-
-static int fflg, iflg, vflg;
-static int stdin_ok;
-
-static int	copy(char *, char *);
-static int	do_move(char *, char *);
-static int	fastcopy(char *, char *, struct stat *);
-__dead static void	usage(void);
-
-int
-main(int argc, char *argv[])
-{
-	int ch, len, rval;
-	char *p, *endp;
-	struct stat sb;
-	char path[MAXPATHLEN + 1];
-	size_t baselen;
-
-	setprogname(argv[0]);
-	(void)setlocale(LC_ALL, "");
-
-	while ((ch = getopt(argc, argv, "ifv")) != -1)
-		switch (ch) {
-		case 'i':
-			fflg = 0;
-			iflg = 1;
-			break;
-		case 'f':
-			iflg = 0;
-			fflg = 1;
-			break;
-		case 'v':
-			vflg = 1;
-			break;
-		default:
-			usage();
-		}
-	argc -= optind;
-	argv += optind;
-
-	if (argc < 2)
-		usage();
-
-	stdin_ok = isatty(STDIN_FILENO);
-
-	/*
-	 * If the stat on the target fails or the target isn't a directory,
-	 * try the move.  More than 2 arguments is an error in this case.
-	 */
-	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
-		if (argc > 2)
-			usage();
-		exit(do_move(argv[0], argv[1]));
-	}
-
-	/* It's a directory, move each file into it. */
-	baselen = strlcpy(path, argv[argc - 1], sizeof(path));
-	if (baselen >= sizeof(path))
-		errx(1, "%s: destination pathname too long", argv[argc - 1]);
-	endp = &path[baselen];
-	if (!baselen || *(endp - 1) != '/') {
-		*endp++ = '/';
-		++baselen;
-	}
-	for (rval = 0; --argc; ++argv) {
-		p = *argv + strlen(*argv) - 1;
-		while (*p == '/' && p != *argv)
-			*p-- = '\0';
-		if ((p = strrchr(*argv, '/')) == NULL)
-			p = *argv;
-		else
-			++p;
-
-		if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
-			warnx("%s: destination pathname too long", *argv);
-			rval = 1;
-		} else {
-			memmove(endp, p, len + 1);
-			if (do_move(*argv, path))
-				rval = 1;
-		}
-	}
-	exit(rval);
-	/* NOTREACHED */
-}
-
-static int
-do_move(char *from, char *to)
-{
-	struct stat sb;
-	char modep[15];
-
-	/*
-	 * (1)	If the destination path exists, the -f option is not specified
-	 *	and either of the following conditions are true:
-	 *
-	 *	(a) The permissions of the destination path do not permit
-	 *	    writing and the standard input is a terminal.
-	 *	(b) The -i option is specified.
-	 *
-	 *	the mv utility shall write a prompt to standard error and
-	 *	read a line from standard input.  If the response is not
-	 *	affirmative, mv shall do nothing more with the current
-	 *	source file...
-	 */
-	if (!fflg && !access(to, F_OK)) {
-		int ask = 1;
-		int ch;
-
-		if (iflg) {
-			if (access(from, F_OK)) {
-				warn("rename %s", from);
-				return (1);
-			}
-			(void)fprintf(stderr, "overwrite %s? ", to);
-		} else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
-			if (access(from, F_OK)) {
-				warn("rename %s", from);
-				return (1);
-			}
-			strmode(sb.st_mode, modep);
-			(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
-			    modep + 1, modep[9] == ' ' ? "" : " ",
-			    user_from_uid(sb.st_uid, 0),
-			    group_from_gid(sb.st_gid, 0), to);
-		} else
-			ask = 0;
-		if (ask) {
-			if ((ch = getchar()) != EOF && ch != '\n') {
-				int ch2;
-				while ((ch2 = getchar()) != EOF && ch2 != '\n')
-					continue;
-			}
-			if (ch != 'y' && ch != 'Y')
-				return (0);
-		}
-	}
-
-	/*
-	 * (2)	If rename() succeeds, mv shall do nothing more with the
-	 *	current source file.  If it fails for any other reason than
-	 *	EXDEV, mv shall write a diagnostic message to the standard
-	 *	error and do nothing more with the current source file.
-	 *
-	 * (3)	If the destination path exists, and it is a file of type
-	 *	directory and source_file is not a file of type directory,
-	 *	or it is a file not of type directory, and source file is
-	 *	a file of type directory, mv shall write a diagnostic
-	 *	message to standard error, and do nothing more with the
-	 *	current source file...
-	 */
-	if (!rename(from, to)) {
-		if (vflg)
-			printf("%s -> %s\n", from, to);
-		return (0);
-	}
-
-	if (errno != EXDEV) {
-		warn("rename %s to %s", from, to);
-		return (1);
-	}
-
-	/*
-	 * (4)	If the destination path exists, mv shall attempt to remove it.
-	 *	If this fails for any reason, mv shall write a diagnostic
-	 *	message to the standard error and do nothing more with the
-	 *	current source file...
-	 */
-	if (!lstat(to, &sb)) {
-		if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
-			warn("can't remove %s", to);
-			return (1);
-		}
-	}
-
-	/*
-	 * (5)	The file hierarchy rooted in source_file shall be duplicated
-	 *	as a file hierarchy rooted in the destination path...
-	 */
-	if (lstat(from, &sb)) {
-		warn("%s", from);
-		return (1);
-	}
-
-	return (S_ISREG(sb.st_mode) ?
-	    fastcopy(from, to, &sb) : copy(from, to));
-}
-
-static int
-fastcopy(char *from, char *to, struct stat *sbp)
-{
-	struct timeval tval[2];
-	static blksize_t blen;
-	static char *bp;
-	int nread, from_fd, to_fd;
-
-	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
-		warn("%s", from);
-		return (1);
-	}
-	if ((to_fd =
-	    open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
-		warn("%s", to);
-		(void)close(from_fd);
-		return (1);
-	}
-	if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
-		warn(NULL);
-		blen = 0;
-		(void)close(from_fd);
-		(void)close(to_fd);
-		return (1);
-	}
-	while ((nread = read(from_fd, bp, blen)) > 0)
-		if (write(to_fd, bp, nread) != nread) {
-			warn("%s", to);
-			goto err;
-		}
-	if (nread < 0) {
-		warn("%s", from);
-err:		if (unlink(to))
-			warn("%s: remove", to);
-		(void)close(from_fd);
-		(void)close(to_fd);
-		return (1);
-	}
-
-#ifndef __ANDROID__
-	if (fcpxattr(from_fd, to_fd) == -1)
-		warn("%s: error copying extended attributes", to);
-#endif
-
-	(void)close(from_fd);
-#ifdef BSD4_4
-	TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
-	TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
-#else
-	tval[0].tv_sec = sbp->st_atime;
-	tval[1].tv_sec = sbp->st_mtime;
-	tval[0].tv_usec = 0;
-	tval[1].tv_usec = 0;
-#endif
-#ifdef __SVR4
-	if (utimes(to, tval))
-#else
-	if (futimes(to_fd, tval))
-#endif
-		warn("%s: set times", to);
-	if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
-		if (errno != EPERM)
-			warn("%s: set owner/group", to);
-		sbp->st_mode &= ~(S_ISUID | S_ISGID);
-	}
-	if (fchmod(to_fd, sbp->st_mode))
-		warn("%s: set mode", to);
-#ifndef __ANDROID__
-	if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
-		warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
-#endif
-
-	if (close(to_fd)) {
-		warn("%s", to);
-		return (1);
-	}
-
-	if (unlink(from)) {
-		warn("%s: remove", from);
-		return (1);
-	}
-
-	if (vflg)
-		printf("%s -> %s\n", from, to);
-
-	return (0);
-}
-
-static int
-copy(char *from, char *to)
-{
-	pid_t pid;
-	int status;
-
-	if ((pid = vfork()) == 0) {
-		execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to, NULL);
-		warn("%s", _PATH_CP);
-		_exit(1);
-	}
-	if (waitpid(pid, &status, 0) == -1) {
-		warn("%s: waitpid", _PATH_CP);
-		return (1);
-	}
-	if (!WIFEXITED(status)) {
-		warnx("%s: did not terminate normally", _PATH_CP);
-		return (1);
-	}
-	if (WEXITSTATUS(status)) {
-		warnx("%s: terminated with %d (non-zero) status",
-		    _PATH_CP, WEXITSTATUS(status));
-		return (1);
-	}
-	if (!(pid = vfork())) {
-		execl(_PATH_RM, "mv", "-rf", "--", from, NULL);
-		warn("%s", _PATH_RM);
-		_exit(1);
-	}
-	if (waitpid(pid, &status, 0) == -1) {
-		warn("%s: waitpid", _PATH_RM);
-		return (1);
-	}
-	if (!WIFEXITED(status)) {
-		warnx("%s: did not terminate normally", _PATH_RM);
-		return (1);
-	}
-	if (WEXITSTATUS(status)) {
-		warnx("%s: terminated with %d (non-zero) status",
-		    _PATH_RM, WEXITSTATUS(status));
-		return (1);
-	}
-	return (0);
-}
-
-static void
-usage(void)
-{
-	(void)fprintf(stderr, "usage: %s [-fiv] source target\n"
-	    "       %s [-fiv] source ... directory\n", getprogname(),
-	    getprogname());
-	exit(1);
-	/* NOTREACHED */
-}
diff --git a/toolbox/upstream-netbsd/bin/mv/pathnames.h b/toolbox/upstream-netbsd/bin/mv/pathnames.h
deleted file mode 100644
index 7838946..0000000
--- a/toolbox/upstream-netbsd/bin/mv/pathnames.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/*	$NetBSD: pathnames.h,v 1.8 2004/08/19 22:26:07 christos Exp $	*/
-
-/*
- * Copyright (c) 1989, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- *
- *	@(#)pathnames.h	8.1 (Berkeley) 5/31/93
- */
-
-#ifdef __ANDROID__
-#define	_PATH_RM	"/system/bin/rm"
-#define	_PATH_CP	"/system/bin/cp"
-#else
-#ifdef RESCUEDIR
-#define	_PATH_RM	RESCUEDIR "/rm"
-#define	_PATH_CP	RESCUEDIR "/cp"
-#else
-#define	_PATH_RM	"/bin/rm"
-#define	_PATH_CP	"/bin/cp"
-#endif
-#endif
diff --git a/toolbox/upstream-netbsd/bin/rm/rm.c b/toolbox/upstream-netbsd/bin/rm/rm.c
deleted file mode 100644
index f183810..0000000
--- a/toolbox/upstream-netbsd/bin/rm/rm.c
+++ /dev/null
@@ -1,625 +0,0 @@
-/* $NetBSD: rm.c,v 1.53 2013/04/26 18:43:22 christos Exp $ */
-
-/*-
- * Copyright (c) 1990, 1993, 1994, 2003
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)rm.c	8.8 (Berkeley) 4/27/95";
-#else
-__RCSID("$NetBSD: rm.c,v 1.53 2013/04/26 18:43:22 christos Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <err.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <fts.h>
-#include <grp.h>
-#include <locale.h>
-#include <pwd.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-static int dflag, eval, fflag, iflag, Pflag, stdin_ok, vflag, Wflag;
-static int xflag;
-static sig_atomic_t pinfo;
-
-static int	check(char *, char *, struct stat *);
-static void	checkdot(char **);
-static void	progress(int);
-static void	rm_file(char **);
-static int	rm_overwrite(char *, struct stat *);
-static void	rm_tree(char **);
-__dead static void	usage(void);
-
-/*
- * For the sake of the `-f' flag, check whether an error number indicates the
- * failure of an operation due to an non-existent file, either per se (ENOENT)
- * or because its filename argument was illegal (ENAMETOOLONG, ENOTDIR).
- */
-#define NONEXISTENT(x) \
-    ((x) == ENOENT || (x) == ENAMETOOLONG || (x) == ENOTDIR)
-
-/*
- * rm --
- *	This rm is different from historic rm's, but is expected to match
- *	POSIX 1003.2 behavior.  The most visible difference is that -f
- *	has two specific effects now, ignore non-existent files and force
- * 	file removal.
- */
-int
-main(int argc, char *argv[])
-{
-	int ch, rflag;
-
-	setprogname(argv[0]);
-	(void)setlocale(LC_ALL, "");
-
-	Pflag = rflag = xflag = 0;
-	while ((ch = getopt(argc, argv, "dfiPRrvWx")) != -1)
-		switch (ch) {
-		case 'd':
-			dflag = 1;
-			break;
-		case 'f':
-			fflag = 1;
-			iflag = 0;
-			break;
-		case 'i':
-			fflag = 0;
-			iflag = 1;
-			break;
-		case 'P':
-			Pflag = 1;
-			break;
-		case 'R':
-		case 'r':			/* Compatibility. */
-			rflag = 1;
-			break;
-		case 'v':
-			vflag = 1;
-			break;
-		case 'x':
-			xflag = 1;
-			break;
-#ifndef __ANDROID__
-		case 'W':
-			Wflag = 1;
-			break;
-#endif
-		case '?':
-		default:
-			usage();
-		}
-	argc -= optind;
-	argv += optind;
-
-	if (argc < 1) {
-		if (fflag)
-			return 0;
-		usage();
-	}
-
-	(void)signal(SIGINFO, progress);
-
-	checkdot(argv);
-
-	if (*argv) {
-		stdin_ok = isatty(STDIN_FILENO);
-
-		if (rflag)
-			rm_tree(argv);
-		else
-			rm_file(argv);
-	}
-
-	exit(eval);
-	/* NOTREACHED */
-}
-
-static void
-rm_tree(char **argv)
-{
-	FTS *fts;
-	FTSENT *p;
-	int flags, needstat, rval;
-			
-	/*
-	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
-	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
-	 */
-	needstat = !fflag && !iflag && stdin_ok;
-
-	/*
-	 * If the -i option is specified, the user can skip on the pre-order
-	 * visit.  The fts_number field flags skipped directories.
-	 */
-#define	SKIPPED	1
-
-	flags = FTS_PHYSICAL;
-	if (!needstat)
-		flags |= FTS_NOSTAT;
-#ifndef __ANDROID__
-	if (Wflag)
-		flags |= FTS_WHITEOUT;
-#endif
-	if (xflag)
-		flags |= FTS_XDEV;
-	if ((fts = fts_open(argv, flags, NULL)) == NULL)
-		err(1, "fts_open failed");
-	while ((p = fts_read(fts)) != NULL) {
-	
-		switch (p->fts_info) {
-		case FTS_DNR:
-			if (!fflag || p->fts_errno != ENOENT) {
-				warnx("%s: %s", p->fts_path,
-						strerror(p->fts_errno));
-				eval = 1;
-			}
-			continue;
-		case FTS_ERR:
-			errx(EXIT_FAILURE, "%s: %s", p->fts_path,
-					strerror(p->fts_errno));
-			/* NOTREACHED */
-		case FTS_NS:
-			/*
-			 * FTS_NS: assume that if can't stat the file, it
-			 * can't be unlinked.
-			 */
-			if (fflag && NONEXISTENT(p->fts_errno))
-				continue;
-			if (needstat) {
-				warnx("%s: %s", p->fts_path,
-						strerror(p->fts_errno));
-				eval = 1;
-				continue;
-			}
-			break;
-		case FTS_D:
-			/* Pre-order: give user chance to skip. */
-			if (!fflag && !check(p->fts_path, p->fts_accpath,
-			    p->fts_statp)) {
-				(void)fts_set(fts, p, FTS_SKIP);
-				p->fts_number = SKIPPED;
-			}
-			continue;
-		case FTS_DP:
-			/* Post-order: see if user skipped. */
-			if (p->fts_number == SKIPPED)
-				continue;
-			break;
-		default:
-			if (!fflag &&
-			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
-				continue;
-		}
-
-		rval = 0;
-		/*
-		 * If we can't read or search the directory, may still be
-		 * able to remove it.  Don't print out the un{read,search}able
-		 * message unless the remove fails.
-		 */
-		switch (p->fts_info) {
-		case FTS_DP:
-		case FTS_DNR:
-			rval = rmdir(p->fts_accpath);
-			if (rval != 0 && fflag && errno == ENOENT)
-				continue;
-			break;
-
-#ifndef __ANDROID__
-		case FTS_W:
-			rval = undelete(p->fts_accpath);
-			if (rval != 0 && fflag && errno == ENOENT)
-				continue;
-			break;
-#endif
-
-		default:
-			if (Pflag) {
-				if (rm_overwrite(p->fts_accpath, NULL))
-					continue;
-			}
-			rval = unlink(p->fts_accpath);
-			if (rval != 0 && fflag && NONEXISTENT(errno))
-				continue;
-			break;
-		}
-		if (rval != 0) {
-			warn("%s", p->fts_path);
-			eval = 1;
-		} else if (vflag || pinfo) {
-			pinfo = 0;
-			(void)printf("%s\n", p->fts_path);
-		}
-	}
-	if (errno)
-		err(1, "fts_read");
-	fts_close(fts);
-}
-
-static void
-rm_file(char **argv)
-{
-	struct stat sb;
-	int rval;
-	char *f;
-
-	/*
-	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
-	 * to remove a directory is an error, so must always stat the file.
-	 */
-	while ((f = *argv++) != NULL) {
-		/* Assume if can't stat the file, can't unlink it. */
-		if (lstat(f, &sb)) {
-#ifndef __ANDROID__
-			if (Wflag) {
-				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
-			} else {
-#endif
-				if (!fflag || !NONEXISTENT(errno)) {
-					warn("%s", f);
-					eval = 1;
-				}
-				continue;
-#ifndef __ANDROID__
-			}
-		} else if (Wflag) {
-			warnx("%s: %s", f, strerror(EEXIST));
-			eval = 1;
-			continue;
-#endif
-		}
-
-		if (S_ISDIR(sb.st_mode) && !dflag) {
-			warnx("%s: is a directory", f);
-			eval = 1;
-			continue;
-		}
-		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
-			continue;
-#ifndef __ANDROID__
-		if (S_ISWHT(sb.st_mode))
-			rval = undelete(f);
-		else if (S_ISDIR(sb.st_mode))
-#else
-		if (S_ISDIR(sb.st_mode))
-#endif
-			rval = rmdir(f);
-		else {
-			if (Pflag) {
-				if (rm_overwrite(f, &sb))
-					continue;
-			}
-			rval = unlink(f);
-		}
-		if (rval && (!fflag || !NONEXISTENT(errno))) {
-			warn("%s", f);
-			eval = 1;
-		}
-		if (vflag && rval == 0)
-			(void)printf("%s\n", f);
-	}
-}
-
-/*
- * rm_overwrite --
- *	Overwrite the file 3 times with varying bit patterns.
- *
- * This is an expensive way to keep people from recovering files from your
- * non-snapshotted FFS filesystems using fsdb(8).  Really.  No more.  Only
- * regular files are deleted, directories (and therefore names) will remain.
- * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
- * System V file system).  In a logging file system, you'll have to have
- * kernel support.
- *
- * A note on standards:  U.S. DoD 5220.22-M "National Industrial Security
- * Program Operating Manual" ("NISPOM") is often cited as a reference
- * for clearing and sanitizing magnetic media.  In fact, a matrix of
- * "clearing" and "sanitization" methods for various media was given in
- * Chapter 8 of the original 1995 version of NISPOM.  However, that
- * matrix was *removed from the document* when Chapter 8 was rewritten
- * in Change 2 to the document in 2001.  Recently, the Defense Security
- * Service has made a revised clearing and sanitization matrix available
- * in Microsoft Word format on the DSS web site.  The standardization
- * status of this matrix is unclear.  Furthermore, one must be very
- * careful when referring to this matrix: it is intended for the "clearing"
- * prior to reuse or "sanitization" prior to disposal of *entire media*,
- * not individual files and the only non-physically-destructive method of
- * "sanitization" that is permitted for magnetic disks of any kind is
- * specifically noted to be prohibited for media that have contained
- * Top Secret data.
- *
- * It is impossible to actually conform to the exact procedure given in
- * the matrix if one is overwriting a file, not an entire disk, because
- * the procedure requires examination and comparison of the disk's defect
- * lists.  Any program that claims to securely erase *files* while 
- * conforming to the standard, then, is not correct.  We do as much of
- * what the standard requires as can actually be done when erasing a
- * file, rather than an entire disk; but that does not make us conformant.
- *
- * Furthermore, the presence of track caches, disk and controller write
- * caches, and so forth make it extremely difficult to ensure that data
- * have actually been written to the disk, particularly when one tries
- * to repeatedly overwrite the same sectors in quick succession.  We call
- * fsync(), but controllers with nonvolatile cache, as well as IDE disks
- * that just plain lie about the stable storage of data, will defeat this.
- *
- * Finally, widely respected research suggests that the given procedure
- * is nowhere near sufficient to prevent the recovery of data using special
- * forensic equipment and techniques that are well-known.  This is 
- * presumably one reason that the matrix requires physical media destruction,
- * rather than any technique of the sort attempted here, for secret data.
- *
- * Caveat Emptor.
- *
- * rm_overwrite will return 0 on success.
- */
-
-static int
-rm_overwrite(char *file, struct stat *sbp)
-{
-	struct stat sb, sb2;
-	int fd, randint;
-	char randchar;
-
-	fd = -1;
-	if (sbp == NULL) {
-		if (lstat(file, &sb))
-			goto err;
-		sbp = &sb;
-	}
-	if (!S_ISREG(sbp->st_mode))
-		return 0;
-
-	/* flags to try to defeat hidden caching by forcing seeks */
-	if ((fd = open(file, O_RDWR|O_SYNC|O_RSYNC|O_NOFOLLOW, 0)) == -1)
-		goto err;
-
-	if (fstat(fd, &sb2)) {
-		goto err;
-	}
-
-	if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino ||
-	    !S_ISREG(sb2.st_mode)) {
-		errno = EPERM;
-		goto err;
-	}
-
-#define RAND_BYTES	1
-#define THIS_BYTE	0
-
-#define	WRITE_PASS(mode, byte) do {					\
-	off_t len;							\
-	size_t wlen, i;							\
-	char buf[8 * 1024];						\
-									\
-	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))			\
-		goto err;						\
-									\
-	if (mode == THIS_BYTE)						\
-		memset(buf, byte, sizeof(buf));				\
-	for (len = sbp->st_size; len > 0; len -= wlen) {		\
-		if (mode == RAND_BYTES) {				\
-			for (i = 0; i < sizeof(buf); 			\
-			    i+= sizeof(u_int32_t))			\
-				*(int *)(buf + i) = arc4random();	\
-		}							\
-		wlen = len < (off_t)sizeof(buf) ? (size_t)len : sizeof(buf); \
-		if ((size_t)write(fd, buf, wlen) != wlen)		\
-			goto err;					\
-	}								\
-	sync();		/* another poke at hidden caches */		\
-} while (/* CONSTCOND */ 0)
-
-#define READ_PASS(byte) do {						\
-	off_t len;							\
-	size_t rlen;							\
-	char pattern[8 * 1024];						\
-	char buf[8 * 1024];						\
-									\
-	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))			\
-		goto err;						\
-									\
-	memset(pattern, byte, sizeof(pattern));				\
-	for(len = sbp->st_size; len > 0; len -= rlen) {			\
-		rlen = len < (off_t)sizeof(buf) ? (size_t)len : sizeof(buf); \
-		if((size_t)read(fd, buf, rlen) != rlen)			\
-			goto err;					\
-		if(memcmp(buf, pattern, rlen))				\
-			goto err;					\
-	}								\
-	sync();		/* another poke at hidden caches */		\
-} while (/* CONSTCOND */ 0)
-
-	/*
-	 * DSS sanitization matrix "clear" for magnetic disks: 
-	 * option 'c' "Overwrite all addressable locations with a single 
-	 * character."
-	 */
-	randint = arc4random();
-	randchar = *(char *)&randint;
-	WRITE_PASS(THIS_BYTE, randchar);
-
-	/*
-	 * DSS sanitization matrix "sanitize" for magnetic disks: 
-	 * option 'd', sub 2 "Overwrite all addressable locations with a
-	 * character, then its complement.  Verify "complement" character
-	 * was written successfully to all addressable locations, then
-	 * overwrite all addressable locations with random characters; or
-	 * verify third overwrite of random characters."  The rest of the
-	 * text in d-sub-2 specifies requirements for overwriting spared
-	 * sectors; we cannot conform to it when erasing only a file, thus
-	 * we do not conform to the standard.
-	 */
-
-	/* 1. "a character" */
-	WRITE_PASS(THIS_BYTE, 0xff);
-
-	/* 2. "its complement" */
-	WRITE_PASS(THIS_BYTE, 0x00);
-
-	/* 3. "Verify 'complement' character" */
-	READ_PASS(0x00);
-
-	/* 4. "overwrite all addressable locations with random characters" */
-
-	WRITE_PASS(RAND_BYTES, 0x00);
-
-	/*
-	 * As the file might be huge, and we note that this revision of
-	 * the matrix says "random characters", not "a random character"
-	 * as the original did, we do not verify the random-character
-	 * write; the "or" in the standard allows this.
-	 */
-
-	if (close(fd) == -1) {
-		fd = -1;
-		goto err;
-	}
-
-	return 0;
-
-err:	eval = 1;
-	warn("%s", file);
-	if (fd != -1)
-		close(fd);
-	return 1;
-}
-
-static int
-check(char *path, char *name, struct stat *sp)
-{
-	int ch, first;
-	char modep[15];
-
-	/* Check -i first. */
-	if (iflag)
-		(void)fprintf(stderr, "remove '%s'? ", path);
-	else {
-		/*
-		 * If it's not a symbolic link and it's unwritable and we're
-		 * talking to a terminal, ask.  Symbolic links are excluded
-		 * because their permissions are meaningless.  Check stdin_ok
-		 * first because we may not have stat'ed the file.
-		 */
-		if (!stdin_ok || S_ISLNK(sp->st_mode) ||
-		    !(access(name, W_OK) && (errno != ETXTBSY)))
-			return (1);
-		strmode(sp->st_mode, modep);
-		if (Pflag) {
-			warnx(
-			    "%s: -P was specified but file could not"
-			    " be overwritten", path);
-			return 0;
-		}
-		(void)fprintf(stderr, "override %s%s%s:%s for '%s'? ",
-		    modep + 1, modep[9] == ' ' ? "" : " ",
-		    user_from_uid(sp->st_uid, 0),
-		    group_from_gid(sp->st_gid, 0), path);
-	}
-	(void)fflush(stderr);
-
-	first = ch = getchar();
-	while (ch != '\n' && ch != EOF)
-		ch = getchar();
-	return (first == 'y' || first == 'Y');
-}
-
-/*
- * POSIX.2 requires that if "." or ".." are specified as the basename
- * portion of an operand, a diagnostic message be written to standard
- * error and nothing more be done with such operands.
- *
- * Since POSIX.2 defines basename as the final portion of a path after
- * trailing slashes have been removed, we'll remove them here.
- */
-#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
-static void
-checkdot(char **argv)
-{
-	char *p, **save, **t;
-	int complained;
-
-	complained = 0;
-	for (t = argv; *t;) {
-		/* strip trailing slashes */
-		p = strrchr(*t, '\0');
-		while (--p > *t && *p == '/')
-			*p = '\0';
-
-		/* extract basename */
-		if ((p = strrchr(*t, '/')) != NULL)
-			++p;
-		else
-			p = *t;
-
-		if (ISDOT(p)) {
-			if (!complained++)
-				warnx("\".\" and \"..\" may not be removed");
-			eval = 1;
-			for (save = t; (t[0] = t[1]) != NULL; ++t)
-				continue;
-			t = save;
-		} else
-			++t;
-	}
-}
-
-static void
-usage(void)
-{
-
-	(void)fprintf(stderr, "usage: %s [-f|-i] [-dPRrvWx] file ...\n",
-	    getprogname());
-	exit(1);
-	/* NOTREACHED */
-}
-
-static void
-progress(int sig __unused)
-{
-	
-	pinfo++;
-}
diff --git a/toolbox/upstream-netbsd/bin/rmdir/rmdir.c b/toolbox/upstream-netbsd/bin/rmdir/rmdir.c
deleted file mode 100644
index 03261ce..0000000
--- a/toolbox/upstream-netbsd/bin/rmdir/rmdir.c
+++ /dev/null
@@ -1,121 +0,0 @@
-/* $NetBSD: rmdir.c,v 1.26 2011/08/29 14:49:38 joerg Exp $ */
-
-/*-
- * Copyright (c) 1992, 1993, 1994
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)rmdir.c	8.3 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: rmdir.c,v 1.26 2011/08/29 14:49:38 joerg Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/param.h>
-
-#include <err.h>
-#include <locale.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-static int	rm_path(char *);
-__dead static void	usage(void);
-
-int
-main(int argc, char *argv[])
-{
-	int ch, errors, pflag;
-
-	setprogname(argv[0]);
-	(void)setlocale(LC_ALL, "");
-
-	pflag = 0;
-	while ((ch = getopt(argc, argv, "p")) != -1)
-		switch(ch) {
-		case 'p':
-			pflag = 1;
-			break;
-		case '?':
-		default:
-			usage();
-		}
-	argc -= optind;
-	argv += optind;
-
-	if (argc == 0)
-		usage();
-
-	for (errors = 0; *argv; argv++) {
-		/* We rely on the kernel to ignore trailing '/' characters. */
-		if (rmdir(*argv) < 0) {
-			warn("%s", *argv);
-			errors = 1;
-		} else if (pflag)
-			errors |= rm_path(*argv);
-	}
-
-	exit(errors);
-	/* NOTREACHED */
-}
-
-static int
-rm_path(char *path)
-{
-	char *p;
-
-	while ((p = strrchr(path, '/')) != NULL) {
-		*p = 0;
-		if (p[1] == 0)
-			/* Ignore trailing '/' on deleted name */
-			continue;
-
-		if (rmdir(path) < 0) {
-			warn("%s", path);
-			return (1);
-		}
-	}
-
-	return (0);
-}
-
-static void
-usage(void)
-{
-	(void)fprintf(stderr, "usage: %s [-p] directory ...\n", getprogname());
-	exit(1);
-	/* NOTREACHED */
-}
diff --git a/toolbox/upstream-netbsd/bin/sleep/sleep.c b/toolbox/upstream-netbsd/bin/sleep/sleep.c
deleted file mode 100644
index 4349af4..0000000
--- a/toolbox/upstream-netbsd/bin/sleep/sleep.c
+++ /dev/null
@@ -1,159 +0,0 @@
-/* $NetBSD: sleep.c,v 1.24 2011/08/29 14:51:19 joerg Exp $ */
-
-/*
- * Copyright (c) 1988, 1993, 1994
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
-#else
-__RCSID("$NetBSD: sleep.c,v 1.24 2011/08/29 14:51:19 joerg Exp $");
-#endif
-#endif /* not lint */
-
-#include <ctype.h>
-#include <err.h>
-#include <locale.h>
-#include <math.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
-#include <unistd.h>
-
-__dead static void alarmhandle(int);
-__dead static void usage(void);
-
-static volatile sig_atomic_t report_requested;
-static void
-report_request(int signo __unused)
-{
-
-	report_requested = 1;
-}
-
-int
-main(int argc, char *argv[])
-{
-	char *arg, *temp;
-	double fval, ival, val;
-	struct timespec ntime;
-	time_t original;
-	int ch, fracflag, rv;
-
-	setprogname(argv[0]);
-	(void)setlocale(LC_ALL, "");
-
-	(void)signal(SIGALRM, alarmhandle);
-
-	while ((ch = getopt(argc, argv, "")) != -1)
-		switch(ch) {
-		default:
-			usage();
-		}
-	argc -= optind;
-	argv += optind;
-
-	if (argc != 1)
-		usage();
-
-	/*
-	 * Okay, why not just use atof for everything? Why bother
-	 * checking if there is a fraction in use? Because the old
-	 * sleep handled the full range of integers, that's why, and a
-	 * double can't handle a large long. This is fairly useless
-	 * given how large a number a double can hold on most
-	 * machines, but now we won't ever have trouble. If you want
-	 * 1000000000.9 seconds of sleep, well, that's your
-	 * problem. Why use an isdigit() check instead of checking for
-	 * a period? Because doing it this way means locales will be
-	 * handled transparently by the atof code.
-	 */
-	fracflag = 0;
-	arg = *argv;
-	for (temp = arg; *temp != '\0'; temp++)
-		if (!isdigit((unsigned char)*temp))
-			fracflag++;
-
-	if (fracflag) {
-		val = atof(arg);
-		if (val <= 0)
-			usage();
-		ival = floor(val);
-		fval = (1000000000 * (val-ival));
-		ntime.tv_sec = ival;
-		ntime.tv_nsec = fval;
-	}
-	else {
-		ntime.tv_sec = atol(arg);
-		if (ntime.tv_sec <= 0)
-			return EXIT_SUCCESS;
-		ntime.tv_nsec = 0;
-	}
-
-	original = ntime.tv_sec;
-	signal(SIGINFO, report_request);
-	while ((rv = nanosleep(&ntime, &ntime)) != 0) {
-		if (report_requested) {
-		/* Reporting does not bother with nanoseconds. */
-			warnx("about %d second(s) left out of the original %d",
-			(int)ntime.tv_sec, (int)original);
-			report_requested = 0;
-		} else
-			break;
-	}
-
-	if (rv == -1)
-		err(EXIT_FAILURE, "nanosleep failed");
-
-	return EXIT_SUCCESS;
-	/* NOTREACHED */
-}
-
-static void
-usage(void)
-{
-	(void)fprintf(stderr, "usage: %s seconds\n", getprogname());
-	exit(EXIT_FAILURE);
-	/* NOTREACHED */
-}
-
-/* ARGSUSED */
-static void
-alarmhandle(int i)
-{
-	_exit(EXIT_SUCCESS);
-	/* NOTREACHED */
-}
diff --git a/toolbox/upstream-netbsd/bin/sync/sync.c b/toolbox/upstream-netbsd/bin/sync/sync.c
deleted file mode 100644
index 2b9c367..0000000
--- a/toolbox/upstream-netbsd/bin/sync/sync.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/* $NetBSD: sync.c,v 1.13 2008/07/20 00:52:40 lukem Exp $ */
-
-/*
- * Copyright (c) 1987, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1987, 1993\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)sync.c	8.1 (Berkeley) 5/31/93";
-#else
-__RCSID("$NetBSD: sync.c,v 1.13 2008/07/20 00:52:40 lukem Exp $");
-#endif
-#endif /* not lint */
-
-#include <stdlib.h>
-#include <unistd.h>
-
-int main(int, char *[]);
-
-/* ARGSUSED */
-int
-main(int argc, char *argv[])
-{
-	setprogname(argv[0]);
-	sync();
-	exit(0);
-	/* NOTREACHED */
-}
diff --git a/toolbox/upstream-netbsd/sbin/chown/chown.c b/toolbox/upstream-netbsd/sbin/chown/chown.c
deleted file mode 100644
index ee46eee..0000000
--- a/toolbox/upstream-netbsd/sbin/chown/chown.c
+++ /dev/null
@@ -1,302 +0,0 @@
-/*	$NetBSD: chown.c,v 1.8 2012/10/24 01:12:51 enami Exp $	*/
-
-/*
- * Copyright (c) 1988, 1993, 1994, 2003
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994, 2003\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-#if 0
-static char sccsid[] = "@(#)chown.c	8.8 (Berkeley) 4/4/94";
-#else
-__RCSID("$NetBSD: chown.c,v 1.8 2012/10/24 01:12:51 enami Exp $");
-#endif
-#endif /* not lint */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include <ctype.h>
-#include <dirent.h>
-#include <err.h>
-#include <errno.h>
-#include <locale.h>
-#include <fts.h>
-#include <grp.h>
-#include <pwd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <getopt.h>
-
-static void	a_gid(const char *);
-static void	a_uid(const char *);
-static id_t	id(const char *, const char *);
-__dead static void	usage(void);
-
-static uid_t uid;
-static gid_t gid;
-static int ischown;
-static const char *myname;
-
-struct option chown_longopts[] = {
-	{ "reference",		required_argument,	0,
-						1 },
-	{ NULL,			0,			0,
-						0 },
-};
-
-int
-main(int argc, char **argv)
-{
-	FTS *ftsp;
-	FTSENT *p;
-	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval, vflag;
-	char *cp, *reference;
-	int (*change_owner)(const char *, uid_t, gid_t);
-
-	setprogname(*argv);
-
-	(void)setlocale(LC_ALL, "");
-
-	myname = getprogname();
-	ischown = (myname[2] == 'o');
-	reference = NULL;
-
-	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
-	while ((ch = getopt_long(argc, argv, "HLPRfhv",
-	    chown_longopts, NULL)) != -1)
-		switch (ch) {
-		case 1:
-			reference = optarg;
-			break;
-		case 'H':
-			Hflag = 1;
-			Lflag = 0;
-			break;
-		case 'L':
-			Lflag = 1;
-			Hflag = 0;
-			break;
-		case 'P':
-			Hflag = Lflag = 0;
-			break;
-		case 'R':
-			Rflag = 1;
-			break;
-		case 'f':
-			fflag = 1;
-			break;
-		case 'h':
-			/*
-			 * In System V the -h option causes chown/chgrp to
-			 * change the owner/group of the symbolic link.
-			 * 4.4BSD's symbolic links didn't have owners/groups,
-			 * so it was an undocumented noop.
-			 * In NetBSD 1.3, lchown(2) is introduced.
-			 */
-			hflag = 1;
-			break;
-		case 'v':
-			vflag = 1;
-			break;
-		case '?':
-		default:
-			usage();
-		}
-	argv += optind;
-	argc -= optind;
-
-	if (argc == 0 || (argc == 1 && reference == NULL))
-		usage();
-
-	fts_options = FTS_PHYSICAL;
-	if (Rflag) {
-		if (Hflag)
-			fts_options |= FTS_COMFOLLOW;
-		if (Lflag) {
-			if (hflag)
-				errx(EXIT_FAILURE,
-				    "the -L and -h options "
-				    "may not be specified together.");
-			fts_options &= ~FTS_PHYSICAL;
-			fts_options |= FTS_LOGICAL;
-		}
-	} else if (!hflag)
-		fts_options |= FTS_COMFOLLOW;
-
-	uid = (uid_t)-1;
-	gid = (gid_t)-1;
-	if (reference == NULL) {
-		if (ischown) {
-			if ((cp = strchr(*argv, ':')) != NULL) {
-				*cp++ = '\0';
-				a_gid(cp);
-			}
-#ifdef SUPPORT_DOT
-			else if ((cp = strrchr(*argv, '.')) != NULL) {
-				if (uid_from_user(*argv, &uid) == -1) {
-					*cp++ = '\0';
-					a_gid(cp);
-				}
-			}
-#endif
-			a_uid(*argv);
-		} else
-			a_gid(*argv);
-		argv++;
-	} else {
-		struct stat st;
-
-		if (stat(reference, &st) == -1)
-			err(EXIT_FAILURE, "Cannot stat `%s'", reference);
-		if (ischown)
-			uid = st.st_uid;
-		gid = st.st_gid;
-	}
-
-	if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
-		err(EXIT_FAILURE, "fts_open");
-
-	for (rval = EXIT_SUCCESS; (p = fts_read(ftsp)) != NULL;) {
-		change_owner = chown;
-		switch (p->fts_info) {
-		case FTS_D:
-			if (!Rflag)		/* Change it at FTS_DP. */
-				fts_set(ftsp, p, FTS_SKIP);
-			continue;
-		case FTS_DNR:			/* Warn, chown, continue. */
-			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
-			rval = EXIT_FAILURE;
-			break;
-		case FTS_ERR:			/* Warn, continue. */
-		case FTS_NS:
-			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
-			rval = EXIT_FAILURE;
-			continue;
-		case FTS_SL:			/* Ignore unless -h. */
-			/*
-			 * All symlinks we found while doing a physical
-			 * walk end up here.
-			 */
-			if (!hflag)
-				continue;
-			/*
-			 * Note that if we follow a symlink, fts_info is
-			 * not FTS_SL but FTS_F or whatever.  And we should
-			 * use lchown only for FTS_SL and should use chown
-			 * for others.
-			 */
-			change_owner = lchown;
-			break;
-		case FTS_SLNONE:		/* Ignore. */
-			/*
-			 * The only symlinks that end up here are ones that
-			 * don't point to anything.  Note that if we are
-			 * doing a phisycal walk, we never reach here unless
-			 * we asked to follow explicitly.
-			 */
-			continue;
-		default:
-			break;
-		}
-
-		if ((*change_owner)(p->fts_accpath, uid, gid) && !fflag) {
-			warn("%s", p->fts_path);
-			rval = EXIT_FAILURE;
-		} else {
-			if (vflag)
-				printf("%s\n", p->fts_path);
-		}
-	}
-	if (errno)
-		err(EXIT_FAILURE, "fts_read");
-	exit(rval);
-	/* NOTREACHED */
-}
-
-static void
-a_gid(const char *s)
-{
-	struct group *gr;
-
-	if (*s == '\0')			/* Argument was "uid[:.]". */
-		return;
-	gr = *s == '#' ? NULL : getgrnam(s);
-	if (gr == NULL)
-		gid = id(s, "group");
-	else
-		gid = gr->gr_gid;
-	return;
-}
-
-static void
-a_uid(const char *s)
-{
-	if (*s == '\0')			/* Argument was "[:.]gid". */
-		return;
-	if (*s == '#' || uid_from_user(s, &uid) == -1) {
-		uid = id(s, "user");
-	}
-	return;
-}
-
-static id_t
-id(const char *name, const char *type)
-{
-	id_t val;
-	char *ep;
-
-	errno = 0;
-	if (*name == '#')
-		name++;
-	val = (id_t)strtoul(name, &ep, 10);
-	if (errno)
-		err(EXIT_FAILURE, "%s", name);
-	if (*ep != '\0')
-		errx(EXIT_FAILURE, "%s: invalid %s name", name, type);
-	return (val);
-}
-
-static void
-usage(void)
-{
-
-	(void)fprintf(stderr,
-	    "Usage: %s [-R [-H | -L | -P]] [-fhv] %s file ...\n"
-	    "\t%s [-R [-H | -L | -P]] [-fhv] --reference=rfile file ...\n",
-	    myname, ischown ? "owner:group|owner|:group" : "group",
-	    myname);
-	exit(EXIT_FAILURE);
-}
diff --git a/toolbox/upstream-netbsd/usr.bin/grep/file.c b/toolbox/upstream-netbsd/usr.bin/grep/file.c
index da03d71..cf4a0fa 100644
--- a/toolbox/upstream-netbsd/usr.bin/grep/file.c
+++ b/toolbox/upstream-netbsd/usr.bin/grep/file.c
@@ -78,7 +78,9 @@
 grep_refill(struct file *f)
 {
 	ssize_t nr;
+#ifndef __ANDROID__
 	int bzerr;
+#endif
 
 	bufpos = buffer;
 	bufrem = 0;
diff --git a/toolbox/upstream-netbsd/usr.bin/printenv/printenv.c b/toolbox/upstream-netbsd/usr.bin/printenv/printenv.c
deleted file mode 100644
index e15384f..0000000
--- a/toolbox/upstream-netbsd/usr.bin/printenv/printenv.c
+++ /dev/null
@@ -1,102 +0,0 @@
-/*	$NetBSD: printenv.c,v 1.12 2011/09/06 18:26:55 joerg Exp $	*/
-
-/*
- * Copyright (c) 1987, 1993
- *    The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. 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.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
- */
-
-#include <sys/cdefs.h>
-#ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1987, 1993\
- The Regents of the University of California.  All rights reserved.");
-#endif /* not lint */
-
-#ifndef lint
-/*static char sccsid[] = "from: @(#)printenv.c	8.2 (Berkeley) 5/4/95";*/
-__RCSID("$NetBSD: printenv.c,v 1.12 2011/09/06 18:26:55 joerg Exp $");
-#endif /* not lint */
-
-#include <sys/types.h>
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <err.h>
-
-__dead static void usage(void);
-
-/*
- * printenv
- *
- * Bill Joy, UCB
- * February, 1979
- */
-int
-main(int argc, char *argv[])
-{
-	extern char **environ;
-	char *cp, **ep;
-	size_t len;
-	int ch;
-
-	while ((ch = getopt(argc, argv, "")) != -1)
-		switch(ch) {
-		case '?':
-		default:
-			usage();
-		}
-	argc -= optind;
-	argv += optind;
-
-	if (argc == 0) {
-		for (ep = environ; *ep; ep++)
-			(void)printf("%s\n", *ep);
-		exit(0);
-	}
-	if (argc != 1)
-		usage();
-	if (strchr(*argv, '=') != NULL)
-		errx(1, "Invalid environment variable %s", *argv);
-	len = strlen(*argv);
-	for (ep = environ; *ep; ep++)
-		if (!memcmp(*ep, *argv, len)) {
-			cp = *ep + len;
-			if (!*cp || *cp == '=') {
-				(void)printf("%s\n", *cp ? cp + 1 : cp);
-				exit(0);
-			}
-		}
-	exit(1);
-}
-
-static void
-usage(void)
-{
-	(void)fprintf(stderr, "Usage: printenv [name]\n");
-	exit(1);
-}
diff --git a/toolbox/uptime.c b/toolbox/uptime.c
index 3fb4606..2dd8084 100644
--- a/toolbox/uptime.c
+++ b/toolbox/uptime.c
@@ -36,7 +36,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <time.h>
-
+#include <unistd.h>
 
 static void format_time(int time, char* buffer) {
     int seconds, minutes, hours, days;
diff --git a/toolbox/vmstat.c b/toolbox/vmstat.c
deleted file mode 100644
index 4086ed0..0000000
--- a/toolbox/vmstat.c
+++ /dev/null
@@ -1,247 +0,0 @@
-/*
- * Copyright (c) 2008, 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.
- *  * Neither the name of Google, Inc. nor the names of its contributors
- *    may be used to endorse or promote products derived from this
- *    software without specific prior written permission.
- *
- * 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.
- */
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/param.h>
-#include <unistd.h>
-
-struct state {
-    long procs_r;
-    long procs_b;
-
-    long mem_free;
-    long mem_mapped;
-    long mem_anon;
-    long mem_slab;
-
-    long sys_in;
-    long sys_cs;
-    long sys_flt;
-
-    long cpu_us;
-    long cpu_ni;
-    long cpu_sy;
-    long cpu_id;
-    long cpu_wa;
-    long cpu_ir;
-    long cpu_si;
-};
-
-#define MAX_LINE 256
-
-char line[MAX_LINE];
-
-static void read_state(struct state *s);
-static int read_meminfo(struct state *s);
-static int read_stat(struct state *s);
-static int read_vmstat(struct state *s);
-static void print_header(void);
-static void print_line(struct state *old, struct state *new);
-static void usage(char *cmd);
-
-int vmstat_main(int argc, char *argv[]) {
-    struct state s[2];
-    int iterations, delay, header_interval;
-    int toggle, count;
-    int i;
-
-    iterations = -1;
-    delay = 1;
-    header_interval = 20;
-
-    for (i = 1; i < argc; i++) {
-        if (!strcmp(argv[i], "-n")) { 
-            if (i >= argc - 1) {
-                fprintf(stderr, "Option -n requires an argument.\n");
-                exit(EXIT_FAILURE);
-            }
-            iterations = atoi(argv[++i]);
-            continue;
-        }
-        if (!strcmp(argv[i], "-d")) {
-            if (i >= argc - 1) {
-                fprintf(stderr, "Option -d requires an argument.\n");
-                exit(EXIT_FAILURE);
-            }
-            delay = atoi(argv[++i]);
-            continue;
-        }
-        if (!strcmp(argv[i], "-r")) {
-            if (i >= argc - 1) {
-                fprintf(stderr, "Option -r requires an argument.\n");
-                exit(EXIT_FAILURE);
-            }
-            header_interval = atoi(argv[++i]);
-            continue;
-        }
-        if (!strcmp(argv[i], "-h")) {
-            usage(argv[0]);
-            exit(EXIT_SUCCESS);
-        }
-        fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
-        usage(argv[0]);
-	exit(EXIT_FAILURE);
-    }
-
-    toggle = 0;
-    count = 0;
-
-    if (!header_interval)
-        print_header();
-    read_state(&s[1 - toggle]);
-    while ((iterations < 0) || (iterations-- > 0)) {
-        sleep(delay);
-        read_state(&s[toggle]);
-        if (header_interval) {
-            if (count == 0)
-                print_header();
-            count = (count + 1) % header_interval;
-        }
-        print_line(&s[1 - toggle], &s[toggle]);
-        toggle = 1 - toggle;
-    }
-
-    return 0;
-}
-
-static void read_state(struct state *s) {
-    int error;
-
-    error = read_meminfo(s);
-    if (error) {
-        fprintf(stderr, "vmstat: could not read /proc/meminfo: %s\n", strerror(error));
-        exit(EXIT_FAILURE);
-    }
-
-    error = read_stat(s);
-    if (error) {
-        fprintf(stderr, "vmstat: could not read /proc/stat: %s\n", strerror(error));
-        exit(EXIT_FAILURE);
-    }
-
-    error = read_vmstat(s);
-    if (error) {
-        fprintf(stderr, "vmstat: could not read /proc/vmstat: %s\n", strerror(error));
-        exit(EXIT_FAILURE);
-    }
-}
-
-static int read_meminfo(struct state *s) {
-    FILE *f;
-
-    f = fopen("/proc/meminfo", "r");
-    if (!f) return errno;
-
-    while (fgets(line, MAX_LINE, f)) {
-        sscanf(line, "MemFree: %ld kB", &s->mem_free);
-        sscanf(line, "AnonPages: %ld kB", &s->mem_anon);
-        sscanf(line, "Mapped: %ld kB", &s->mem_mapped);
-        sscanf(line, "Slab: %ld kB", &s->mem_slab);
-    }
-
-    fclose(f);
-
-    return 0;
-}
-
-static int read_stat(struct state *s) {
-    FILE *f;
-
-    f = fopen("/proc/stat", "r");
-    if (!f) return errno;
-
-    while (fgets(line, MAX_LINE, f)) {
-        if (!strncmp(line, "cpu ", 4)) {
-            sscanf(line, "cpu  %ld %ld %ld %ld %ld %ld %ld",
-                &s->cpu_us, &s->cpu_ni, &s->cpu_sy, &s->cpu_id, &s->cpu_wa,
-                &s->cpu_ir, &s->cpu_si);
-        }
-        sscanf(line, "intr %ld", &s->sys_in);
-        sscanf(line, "ctxt %ld", &s->sys_cs);
-        sscanf(line, "procs_running %ld", &s->procs_r);
-        sscanf(line, "procs_blocked %ld", &s->procs_b);
-    }
-
-    fclose(f);
-
-    return 0;
-}
-
-static int read_vmstat(struct state *s) {
-    FILE *f;
-
-    f = fopen("/proc/vmstat", "r");
-    if (!f) return errno;
-
-    while (fgets(line, MAX_LINE, f)) {
-        sscanf(line, "pgmajfault %ld", &s->sys_flt);
-    }
-
-    fclose(f);
-
-    return 0;
-}
-
-static void print_header(void) {
-    printf("%-5s  %-27s  %-14s  %-17s\n", "procs", "memory", "system", "cpu");
-    printf("%2s %2s  %6s %6s %6s %6s  %4s %4s %4s  %2s %2s %2s %2s %2s %2s\n", "r", "b", "free", "mapped", "anon", "slab", "in", "cs", "flt", "us", "ni", "sy", "id", "wa", "ir");
-}
-
-/* Jiffies to percent conversion */
-#define JP(jif) ((jif) * 100 / (HZ))
-#define NORM(var) ((var) = (((var) > 99) ? (99) : (var)))
-
-static void print_line(struct state *old, struct state *new) {
-    int us, ni, sy, id, wa, ir;
-    us = JP(new->cpu_us - old->cpu_us); NORM(us);
-    ni = JP(new->cpu_ni - old->cpu_ni); NORM(ni);
-    sy = JP(new->cpu_sy - old->cpu_sy); NORM(sy);
-    id = JP(new->cpu_id - old->cpu_id); NORM(id);
-    wa = JP(new->cpu_wa - old->cpu_wa); NORM(wa);
-    ir = JP(new->cpu_ir - old->cpu_ir); NORM(ir);
-    printf("%2ld %2ld  %6ld %6ld %6ld %6ld  %4ld %4ld %4ld  %2d %2d %2d %2d %2d %2d\n",
-        new->procs_r ? (new->procs_r - 1) : 0, new->procs_b,
-        new->mem_free, new->mem_mapped, new->mem_anon, new->mem_slab,
-        new->sys_in - old->sys_in, new->sys_cs - old->sys_cs, new->sys_flt - old->sys_flt,
-        us, ni, sy, id, wa, ir);
-}
-
-static void usage(char *cmd) {
-    fprintf(stderr, "Usage: %s [ -h ] [ -n iterations ] [ -d delay ] [ -r header_repeat ]\n"
-                    "    -n iterations     How many rows of data to print.\n"
-                    "    -d delay          How long to sleep between rows.\n"
-                    "    -r header_repeat  How many rows to print before repeating\n"
-                    "                      the header.  Zero means never repeat.\n"
-                    "    -h                Displays this help screen.\n",
-        cmd);
-}
diff --git a/toolbox/watchprops.c b/toolbox/watchprops.c
index 0d05aba..cd62922 100644
--- a/toolbox/watchprops.c
+++ b/toolbox/watchprops.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #include <errno.h>