kernel_module_loader: Load Modules as early as possible

 * This fixes unusually checking for unneeded directories for modules

 * Load Modules early, as it may required by decryption process

 * Tests: fastboot boot, ramdisk boot, fastbootd boot

Change-Id: I2aae88b6dfc0c98867209d4e4eb7e9d503682199
Signed-off-by: Mohd Faraz <androiabledroid@gmail.com>
diff --git a/kernel_module_loader.cpp b/kernel_module_loader.cpp
index 73304a6..292d18f 100644
--- a/kernel_module_loader.cpp
+++ b/kernel_module_loader.cpp
@@ -1,8 +1,24 @@
 #include "kernel_module_loader.hpp"
 #include "common.h"
+#include "variables.h"
 
 const std::vector<std::string> kernel_modules_requested = TWFunc::split_string(EXPAND(TW_LOAD_VENDOR_MODULES), ' ', true);
 
+BOOT_MODE KernelModuleLoader::Get_Boot_Mode() {
+	std::string cmdline;
+	std::string bootconfig;
+	android::base::ReadFileToString("/proc/bootconfig", &bootconfig);
+	android::base::ReadFileToString("/proc/cmdline", &cmdline);
+
+	if (cmdline.find("twrpfastboot=1") != std::string::npos && (bootconfig.find("androidboot.force_normal_boot = \"1\"") != std::string::npos ||
+		cmdline.find("androidboot.force_normal_boot=1") != std::string::npos))
+		return RECOVERY_FASTBOOT_MODE;
+	else if (android::base::GetProperty(TW_FASTBOOT_MODE_PROP, "0") == "1")
+		return FASTBOOTD_MODE;
+
+	return RECOVERY_IN_BOOT_MODE;
+}
+
 bool KernelModuleLoader::Load_Vendor_Modules() {
 	// check /lib/modules (ramdisk vendor_boot)
 	// check /lib/modules/N.N (ramdisk vendor_boot)
@@ -12,6 +28,7 @@
 	// check /vendor/lib/modules/N.N (vendor mounted)
 	// check /vendor/lib/modules/N.N-gki (vendor mounted)
 	// check /vendor_dlkm/lib/modules (vendor_dlkm mounted)
+	if (android::base::GetBoolProperty(TW_MODULES_MOUNTED_PROP, false)) return true;
 	int modules_loaded = 0;
 
 	LOGINFO("Attempting to load modules\n");
@@ -43,14 +60,23 @@
 	vendor_module_dirs.push_back(vendor_base_dir + gki);
 #endif
 
-	for (auto&& module_dir:module_dirs) {
-		modules_loaded += Try_And_Load_Modules(module_dir, false);
-		if (modules_loaded >= expected_module_count) goto exit;
-	}
+	switch(Get_Boot_Mode()) {
+		case RECOVERY_FASTBOOT_MODE:
+			/* On bootmode: once, there is not always stock kernel
+			 * so try only with twrp prebuilt modules.
+			 */
+			for (auto&& module_dir:vendor_module_dirs) {
+				modules_loaded += Try_And_Load_Modules(module_dir, false);
+				if (modules_loaded >= expected_module_count) goto exit;
+			}
+			break;
 
-	for (auto&& module_dir:vendor_module_dirs) {
-		modules_loaded += Try_And_Load_Modules(module_dir, false);
-		if (modules_loaded >= expected_module_count) goto exit;
+		case FASTBOOTD_MODE:
+		case RECOVERY_IN_BOOT_MODE:
+			/* In both mode vendor_boot or vendor modules are used
+			 * Because Ramdisk is flashed in both.
+			 */
+			break;
 	}
 
 	if (ven) {
@@ -76,7 +102,7 @@
 	if (ven_dlkm)
 		ven_dlkm->UnMount(false);
 
-	android::base::SetProperty("twrp.modules.loaded", "true");
+	android::base::SetProperty(TW_MODULES_MOUNTED_PROP, "true");
 
 	return true;
 }