Add toolbox_recovery rules

Build special limited toolbox with SELinux tools for recovery

Change-Id: Ifc76a6bd0468a72081f15ede2a68adf48af8a0f2
diff --git a/Android.mk b/Android.mk
index 8fc99b2..bec425a 100644
--- a/Android.mk
+++ b/Android.mk
@@ -271,6 +271,10 @@
 # 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)
 exclude := tune2fs mke2fs mkdosfs gzip gunzip
+ifeq ($(TWHAVE_SELINUX), true)
+	exclude += ls
+	# toolbox will provide ls support with ls -Z capability for listing SELinux contexts
+endif
 RECOVERY_BUSYBOX_SYMLINKS := $(addprefix $(TARGET_RECOVERY_ROOT_OUT)/sbin/,$(filter-out $(exclude),$(notdir $(BUSYBOX_LINKS))))
 $(RECOVERY_BUSYBOX_SYMLINKS): BUSYBOX_BINARY := busybox
 $(RECOVERY_BUSYBOX_SYMLINKS): $(LOCAL_INSTALLED_MODULE)
@@ -338,7 +342,8 @@
     $(commands_recovery_local_path)/libcrecovery/Android.mk \
     $(commands_recovery_local_path)/libblkid/Android.mk \
     $(commands_recovery_local_path)/minuitwrp/Android.mk \
-    $(commands_recovery_local_path)/openaes/Android.mk
+    $(commands_recovery_local_path)/openaes/Android.mk \
+    $(commands_recovery_local_path)/toolbox/Android.mk
 
 ifeq ($(TW_INCLUDE_CRYPTO_SAMSUNG), true)
     include $(commands_recovery_local_path)/crypto/libcrypt_samsung/Android.mk
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index 0169fb8..f1777c6 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -46,6 +46,7 @@
 RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libflashutils.so
 RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libstlport.so
 #RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libmincrypt.so
+RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/toolbox
 ifeq ($(TARGET_USERIMAGES_USE_EXT4), true)
     RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libext4_utils.so
 endif
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
new file mode 100644
index 0000000..ef0593a
--- /dev/null
+++ b/toolbox/Android.mk
@@ -0,0 +1,80 @@
+LOCAL_PATH:= system/core/toolbox/
+include $(CLEAR_VARS)
+
+TOOLS := \
+	start \
+	stop \
+	getprop \
+	setprop
+
+ifeq ($(TWHAVE_SELINUX), true)
+	TOOLS += \
+		ls \
+		getenforce \
+		setenforce \
+		chcon \
+		restorecon \
+		runcon \
+		getsebool \
+		setsebool \
+		load_policy
+endif
+
+LOCAL_SRC_FILES := \
+	dynarray.c \
+	toolbox.c \
+	$(patsubst %,%.c,$(TOOLS))
+
+TOOLS += reboot
+
+ifeq ($(BOARD_USES_BOOTMENU),true)
+	LOCAL_SRC_FILES += ../../../external/bootmenu/libreboot/reboot.c
+else
+	LOCAL_SRC_FILES += reboot.c
+endif
+
+LOCAL_C_INCLUDES := bionic/libc/bionic
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils \
+	liblog \
+	libc
+
+ifeq ($(TWHAVE_SELINUX), true)
+	LOCAL_SHARED_LIBRARIES += libselinux
+endif
+
+LOCAL_MODULE := toolbox_recovery
+LOCAL_MODULE_STEM := toolbox
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
+LOCAL_MODULE_TAGS := optional
+
+# Including this will define $(intermediates).
+#
+include $(BUILD_EXECUTABLE)
+
+$(LOCAL_PATH)/toolbox.c: $(intermediates)/tools.h
+
+TOOLS_H := $(intermediates)/tools.h
+$(TOOLS_H): PRIVATE_TOOLS := $(TOOLS)
+$(TOOLS_H): PRIVATE_CUSTOM_TOOL = echo "/* file generated automatically */" > $@ ; for t in $(PRIVATE_TOOLS) ; do echo "TOOL($$t)" >> $@ ; done
+$(TOOLS_H): $(LOCAL_PATH)/Android.mk
+$(TOOLS_H):
+	$(transform-generated-source)
+
+# Make #!/system/bin/toolbox launchers for each tool.
+#
+SYMLINKS := $(addprefix $(TARGET_RECOVERY_ROOT_OUT)/sbin/,$(TOOLS))
+$(SYMLINKS): TOOLBOX_BINARY := $(LOCAL_MODULE_STEM)
+$(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 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)