apex: mount up apex files into /sbin for library access

This patch uses the loop device to mount files under
/sbin/ from /system/apex in order for the device to
use libraries or other files store inside these files.

Each apex file is mounted over a loop device into a
directory correpsonding to the apex filename under
/sbin/.

Once apex mounting is completed, TWRP will set a
property twrp.apex.loaded to true so that init
can use them as a LD_LIBARY_PATH source.
Change-Id: I69f14a969123ac9cf1afc85b6cf76836cb092fb2

Change-Id: Ica6a7d8e479bcaec8bec4483b5e8d2b45ee105fb
diff --git a/Android.mk b/Android.mk
index 0bbad69..8fa5139 100755
--- a/Android.mk
+++ b/Android.mk
@@ -78,7 +78,8 @@
     twrpDigestDriver.cpp \
     openrecoveryscript.cpp \
     tarWrite.c \
-    twrpAdbBuFifo.cpp
+    twrpAdbBuFifo.cpp \
+    twrpApex.cpp
 
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 29; echo $$?),0)
     LOCAL_STATIC_LIBRARIES += libavb
@@ -206,6 +207,10 @@
     LOCAL_CFLAGS += -DPRODUCT_USE_DYNAMIC_PARTITIONS=1
 endif
 
+ifeq ($(TW_NO_BIND_SYSTEM),true)
+    LOCAL_CFLAGS += -DTW_NO_BIND_SYSTEM
+endif
+
 LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
 
 ifeq ($(TARGET_RECOVERY_TWRP_LIB),)
diff --git a/partition.cpp b/partition.cpp
index 9bea8e7..eec423a 100755
--- a/partition.cpp
+++ b/partition.cpp
@@ -1575,12 +1575,13 @@
 		TWFunc::Exec_Cmd(Command);
 	}
 
+ #ifndef TW_NO_BIND_SYSTEM
 	if (Mount_Point == "/system_root") {
 		unlink("/system");
 		mkdir("/system", 0755);
 		mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
 	}
-
+ #endif
 	return true;
 }
 
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 848d00a..b76c0ec 100755
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -3277,7 +3277,12 @@
         return false;
     }
 
+	while (access(fstabEntry.blk_device.c_str(), F_OK) != 0) {
+		usleep(100);
+	}
+
 	twrpPart->Set_Block_Device(fstabEntry.blk_device);
+	twrpPart->Mount(true);
 	twrpPart->Update_Size(true);
 	twrpPart->Change_Mount_Read_Only(true);
 	twrpPart->Set_Can_Be_Backed_Up(false);
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index 2225262..f547139 100755
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -484,7 +484,7 @@
 #toybox links
 toybox_links := acpi base64 basename bc blockdev cal cat chcon chgrp chmod chown chroot chrt cksum clear \
     cmp comm cp cpio cut date dd devmem df diff dirname dmesg dos2unix du echo env expand expr fallocate \
-    false file find flock fmt free fsync getconf getenforce groups gunzip gzip head hostname hwclock i2cdetect \
+    false file find flock fmt free fsync getconf getenforce getprop groups gunzip gzip head hostname hwclock i2cdetect \
     i2cdump i2cget i2cset iconv id ifconfig inotifyd insmod install ionice iorenice kill killall ln load_policy \
     log logname losetup ls lsmod lsof lspci lsusb md5sum microcom mkdir mkfifo mknod mkswap mktemp modinfo modprobe \
     more mount mountpoint mv nc netcat netstat nice nl nohup nproc nsenter od paste patch pgrep pidof pkill pmap \
diff --git a/twrp.cpp b/twrp.cpp
index 6d6e5ba..1b97f0b 100755
--- a/twrp.cpp
+++ b/twrp.cpp
@@ -51,6 +51,7 @@
 #include "openrecoveryscript.hpp"
 #include "variables.h"
 #include "twrpAdbBuFifo.hpp"
+#include "twrpApex.hpp"
 #ifdef TW_USE_NEW_MINADBD
 // #include "minadbd/minadbd.h"
 #else
@@ -376,12 +377,17 @@
 	// Check if system has never been changed
 	TWPartition* sys = PartitionManager.Find_Partition_By_Path(PartitionManager.Get_Android_Root_Path());
 	TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
-
 	if (sys) {
 		if (sys->Get_Super_Status()) {
 			if (!PartitionManager.Prepare_All_Super_Volumes()) {
 				LOGERR("Unable to prepare super volumes.\n");
 			}
+			sys->Mount(true);
+			twrpApex apex;
+			if (!apex.loadApexImages()) {
+				LOGERR("Unable to load apex images from %s\n", APEX_DIR);
+			}
+			property_set("twrp.apex.loaded", "true");
 		} else {
 			if ((DataManager::GetIntValue("tw_mount_system_ro") == 0 && sys->Check_Lifetime_Writes() == 0) || DataManager::GetIntValue("tw_mount_system_ro") == 2) {
 				if (DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) {
diff --git a/twrpApex.cpp b/twrpApex.cpp
new file mode 100755
index 0000000..33ec4dc
--- /dev/null
+++ b/twrpApex.cpp
@@ -0,0 +1,135 @@
+#include "twrpApex.hpp"

+

+namespace fs = std::filesystem;

+

+bool twrpApex::loadApexImages() {

+	std::vector<std::string> apexFiles;

+	if (access(APEX_DIR, F_OK) != 0) {

+		LOGERR("Unable to open %s\n", APEX_DIR);

+		return false;

+	}

+	for (const auto& entry : fs::directory_iterator(APEX_DIR)) {

+	   if (entry.is_regular_file()) {

+		   apexFiles.push_back(entry.path().string());

+	   }

+	}

+

+	if (!createLoopBackDevices(apexFiles.size())) {

+		LOGERR("unable to create loop devices to mount apex files\n");

+		return false;

+	}

+

+	size_t apexFileCount = 0;

+	for (auto&& apexFile : apexFiles) {

+		std::string fileToMount = unzipImage(apexFile);

+		loadApexImage(fileToMount, apexFileCount++);

+	}

+	return true;

+}

+

+std::string twrpApex::unzipImage(std::string file) {

+	ZipArchiveHandle handle;

+	int32_t ret = OpenArchive(file.c_str(), &handle);

+	if (ret != 0) {

+		LOGERR("unable to open zip archive %s\n", file.c_str());

+		CloseArchive(handle);

+		return nullptr;

+	}

+

+	ZipEntry entry;

+	ZipString zip_string(APEX_PAYLOAD);

+	ret = FindEntry(handle, zip_string, &entry);

+	if (ret != 0) {

+		LOGERR("unable to find %s in zip\n", APEX_PAYLOAD);

+		CloseArchive(handle);

+		return nullptr;

+	}

+

+	std::string baseFile = basename(file.c_str());

+	std::string path = "/sbin/" + baseFile;

+	int fd = open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);

+	ret = ExtractEntryToFile(handle, &entry, fd);

+	if (ret != 0) {

+		LOGERR("unable to extract %s\n", path.c_str());

+		close(fd);

+		CloseArchive(handle);

+		return nullptr;

+	}

+

+	close(fd);

+	CloseArchive(handle);

+	return path;

+}

+

+bool twrpApex::createLoopBackDevices(size_t count) {

+	size_t existing_loop_device_count = 0;

+

+	for (const auto& entry : fs::directory_iterator(LOOP_BLOCK_DEVICE_DIR)) {

+	   if (entry.is_block_file() && entry.path().string().find("loop") != std::string::npos) {

+		   existing_loop_device_count++;

+	   }

+	}

+

+	if (existing_loop_device_count < count) {

+		size_t devices_to_create = count - existing_loop_device_count;

+		for (size_t i = existing_loop_device_count; i < (devices_to_create + existing_loop_device_count); ++i) {

+			std::string loop_device = LOOP_BLOCK_DEVICE_DIR;

+			loop_device = loop_device + "loop" + std::to_string(i);

+			int ret = mknod(loop_device.c_str(), S_IFBLK | S_IRUSR | S_IWUSR , makedev(7, i));

+			if (ret != 0) {

+				LOGERR("unable to create loop device: %s\n", loop_device.c_str());

+				return false;

+			}

+		}

+	}

+	return true;

+}

+

+bool twrpApex::loadApexImage(std::string fileToMount, size_t loop_device_number) {

+	struct loop_info64 info;

+

+	int fd = open(fileToMount.c_str(), O_RDONLY);

+	if (fd < 0) {

+		LOGERR("unable to open apex file: %s\n", fileToMount.c_str());

+		return false;

+	}

+

+	std::string loop_device = "/dev/block/loop" + std::to_string(loop_device_number);

+	int loop_fd = open(loop_device.c_str(), O_RDONLY);

+	if (loop_fd < 0) {

+		LOGERR("unable to open loop device: %s\n", loop_device.c_str());

+		close(fd);

+		return false;

+	}

+

+	if (ioctl(loop_fd, LOOP_SET_FD, fd) < 0) {

+		LOGERR("failed to mount %s to loop device %s\n", fileToMount.c_str(), loop_device.c_str());

+		close(fd);

+		close(loop_fd);

+		return false;

+	}

+

+	close(fd);

+

+	memset(&info, 0, sizeof(struct loop_info64));

+	if (ioctl(loop_fd, LOOP_SET_STATUS64, &info)) {

+		LOGERR("failed to mount loop: %s: %s\n", fileToMount.c_str(), strerror(errno));

+		close(loop_fd);

+		return false;

+	}

+	close(loop_fd);

+	std::string bind_mount = fileToMount + "-mount";

+	int ret = mkdir(bind_mount.c_str(), 0666);

+	if (ret != 0) {

+		LOGERR("unable to create mount directory: %s\n", bind_mount.c_str());

+		return false;

+	}

+

+	ret = mount(loop_device.c_str(), bind_mount.c_str(), "ext4", MS_RDONLY, nullptr);

+	if (ret != 0) {

+		LOGERR("unable to mount loop device %s to %s\n", loop_device.c_str(), bind_mount.c_str());

+		return false;

+	}

+

+	return true;

+}

diff --git a/twrpApex.hpp b/twrpApex.hpp
new file mode 100755
index 0000000..fb91675
--- /dev/null
+++ b/twrpApex.hpp
@@ -0,0 +1,35 @@
+
+#ifndef TWRPAPEX_HPP
+#define TWRPAPEX_HPP
+
+#include <string>
+#include <vector>
+#include <filesystem>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <linux/loop.h>
+#include <sys/mount.h>
+#include <sys/sysmacros.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <ziparchive/zip_archive.h>
+#include "twcommon.h"
+
+
+#define APEX_DIR "/system_root/system/apex"
+#define APEX_PAYLOAD "apex_payload.img"
+#define LOOP_BLOCK_DEVICE_DIR "/dev/block/"
+
+class twrpApex {
+public:
+	bool loadApexImages();
+
+private:
+	std::string unzipImage(std::string file);
+	bool createLoopBackDevices(size_t count);
+	bool loadApexImage(std::string fileToMount, size_t loop_device_number);
+};
+#endif