Add option to build TWRP with toolbox instead of busybox

Makes a smaller TWRP
Uses mksh for shell

Change-Id: If78d7745dda8919fcb2e9c9b60c6696063c14114
diff --git a/Android.mk b/Android.mk
index c54f8c0..2f1f6fc 100644
--- a/Android.mk
+++ b/Android.mk
@@ -284,6 +284,7 @@
 
 include $(BUILD_EXECUTABLE)
 
+ifneq ($(TW_USE_TOOLBOX), true)
 include $(CLEAR_VARS)
 # Create busybox symlinks... gzip and gunzip are excluded because those need to link to pigz instead
 BUSYBOX_LINKS := $(shell cat external/busybox/busybox-full.links)
@@ -301,6 +302,7 @@
 	$(hide) ln -sf $(BUSYBOX_BINARY) $@
 
 ALL_DEFAULT_INSTALLED_MODULES += $(RECOVERY_BUSYBOX_SYMLINKS)
+endif
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := verifier_test
diff --git a/gui/Android.mk b/gui/Android.mk
index 19bb0bc..6134e3b 100644
--- a/gui/Android.mk
+++ b/gui/Android.mk
@@ -102,6 +102,7 @@
 TWRP_RES_LOC := $(commands_recovery_local_path)/gui/devices
 TWRP_RES_GEN := $(intermediates)/twrp
 
+ifneq ($(TW_USE_TOOLBOX), true)
 $(TWRP_RES_GEN):
 	mkdir -p $(TARGET_RECOVERY_ROOT_OUT)/res/
 	cp -fr $(TWRP_RES_LOC)/common/res/* $(TARGET_RECOVERY_ROOT_OUT)/res/
@@ -110,6 +111,16 @@
 	ln -sf /sbin/busybox $(TARGET_RECOVERY_ROOT_OUT)/sbin/sh
 	ln -sf /sbin/pigz $(TARGET_RECOVERY_ROOT_OUT)/sbin/gzip
 	ln -sf /sbin/unpigz $(TARGET_RECOVERY_ROOT_OUT)/sbin/gunzip
+else
+$(TWRP_RES_GEN):
+	mkdir -p $(TARGET_RECOVERY_ROOT_OUT)/res/
+	cp -fr $(TWRP_RES_LOC)/common/res/* $(TARGET_RECOVERY_ROOT_OUT)/res/
+	cp -fr $(TWRP_RES_LOC)/$(DEVICE_RESOLUTION)/res/* $(TARGET_RECOVERY_ROOT_OUT)/res/
+	mkdir -p $(TARGET_RECOVERY_ROOT_OUT)/sbin/
+	ln -sf /sbin/mksh $(TARGET_RECOVERY_ROOT_OUT)/sbin/sh
+	ln -sf /sbin/pigz $(TARGET_RECOVERY_ROOT_OUT)/sbin/gzip
+	ln -sf /sbin/unpigz $(TARGET_RECOVERY_ROOT_OUT)/sbin/gunzip
+endif
 
 LOCAL_GENERATED_SOURCES := $(TWRP_RES_GEN)
 LOCAL_SRC_FILES := twrp $(TWRP_RES_GEN)
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index b5da5a9..1f784d3 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -13,7 +13,11 @@
 RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/dump_image
 RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/flash_image
 RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/erase_image
-RELINK_SOURCE_FILES += $(TARGET_OUT_OPTIONAL_EXECUTABLES)/busybox
+ifneq ($(TW_USE_TOOLBOX), true)
+	RELINK_SOURCE_FILES += $(TARGET_OUT_OPTIONAL_EXECUTABLES)/busybox
+else
+	RELINK_SOURCE_FILES += $(TARGET_OUT_EXECUTABLES)/mksh
+endif
 RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/pigz
 RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/dosfsck
 RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/dosfslabel
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index afe66a3..7386897 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -20,6 +20,68 @@
 		load_policy
 endif
 
+ifeq ($(TW_USE_TOOLBOX), true)
+	TOOLS += \
+		mount \
+		cat \
+		ps \
+		kill \
+		ln \
+		insmod \
+		rmmod \
+		lsmod \
+		ifconfig \
+		setconsole \
+		rm \
+		mkdir \
+		rmdir \
+		getevent \
+		sendevent \
+		date \
+		wipe \
+		sync \
+		umount \
+		notify \
+		cmp \
+		dmesg \
+		route \
+		hd \
+		dd \
+		df \
+		watchprops \
+		log \
+		sleep \
+		renice \
+		printenv \
+		smd \
+		chmod \
+		chown \
+		newfs_msdos \
+		netstat \
+		ioctl \
+		mv \
+		schedtop \
+		top \
+		iftop \
+		id \
+		uptime \
+		vmstat \
+		nandread \
+		ionice \
+		touch \
+		lsof \
+		du \
+		md5 \
+		clear \
+		swapon \
+		swapoff \
+		mkswap \
+		readlink
+	ifneq ($(TWHAVE_SELINUX), true)
+		TOOLS += ls
+	endif
+endif
+
 LOCAL_SRC_FILES := \
 	toolbox.c \
 	$(patsubst %,%.c,$(TOOLS))