blob: 9ab01539f5130eb271409fc754d96017cd45550f [file] [log] [blame]
bigbiff22851b92021-09-01 16:46:57 -04001#include "kernel_module_loader.hpp"
Adithya Ra327aa72021-12-19 00:49:54 +05302#include "common.h"
bigbiff22851b92021-09-01 16:46:57 -04003
bigbiffe3aa02e2021-10-01 13:07:38 -04004const std::vector<std::string> kernel_modules_requested = TWFunc::split_string(EXPAND(TW_LOAD_VENDOR_MODULES), ' ', true);
5
bigbiff850fa282021-10-09 12:37:29 -04006bool KernelModuleLoader::Load_Vendor_Modules() {
7 // check /lib/modules (ramdisk vendor_boot)
8 // check /lib/modules/N.N (ramdisk vendor_boot)
9 // check /lib/modules/N.N-gki (ramdisk vendor_boot)
10 // check /vendor/lib/modules (ramdisk)
11 // check /vendor/lib/modules/1.1 (ramdisk prebuilt modules)
12 // check /vendor/lib/modules/N.N (vendor mounted)
13 // check /vendor/lib/modules/N.N-gki (vendor mounted)
14 int modules_loaded = 0;
bigbiff22851b92021-09-01 16:46:57 -040015
bigbiff850fa282021-10-09 12:37:29 -040016 LOGINFO("Attempting to load modules\n");
17 std::string vendor_base_dir(VENDOR_MODULE_DIR);
18 std::string base_dir(VENDOR_BOOT_MODULE_DIR);
19 std::vector<std::string> module_dirs;
20 std::vector<std::string> vendor_module_dirs;
bigbiff22851b92021-09-01 16:46:57 -040021
bigbiff850fa282021-10-09 12:37:29 -040022 TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
23 vendor_module_dirs.push_back(VENDOR_MODULE_DIR);
24 vendor_module_dirs.push_back(vendor_base_dir + "/1.1");
bigbiff22851b92021-09-01 16:46:57 -040025
bigbiff850fa282021-10-09 12:37:29 -040026 module_dirs.push_back(base_dir);
bigbiff22851b92021-09-01 16:46:57 -040027
bigbiff850fa282021-10-09 12:37:29 -040028 struct utsname uts;
29 if (uname(&uts)) {
30 LOGERR("Unable to query kernel for version info\n");
31 }
bigbiff22851b92021-09-01 16:46:57 -040032
bigbiff850fa282021-10-09 12:37:29 -040033 std::string rls(uts.release);
34 std::vector<std::string> release = TWFunc::split_string(rls, '.', true);
35 int expected_module_count = kernel_modules_requested.size();
36 module_dirs.push_back(base_dir + "/" + release[0] + "." + release[1]);
bigbiff22851b92021-09-01 16:46:57 -040037
bigbiff850fa282021-10-09 12:37:29 -040038 for (auto&& module_dir:module_dirs) {
39 modules_loaded += Try_And_Load_Modules(module_dir, false);
40 if (modules_loaded >= expected_module_count) goto exit;
41 }
bigbiff22851b92021-09-01 16:46:57 -040042
bigbiff850fa282021-10-09 12:37:29 -040043 for (auto&& module_dir:vendor_module_dirs) {
44 modules_loaded += Try_And_Load_Modules(module_dir, false);
45 if (modules_loaded >= expected_module_count) goto exit;
46 }
bigbiff22851b92021-09-01 16:46:57 -040047
bigbiff850fa282021-10-09 12:37:29 -040048 if (ven) {
49 LOGINFO("Checking mounted /vendor\n");
50 ven->Mount(true);
51 }
bigbiff22851b92021-09-01 16:46:57 -040052
bigbiff850fa282021-10-09 12:37:29 -040053 for (auto&& module_dir:vendor_module_dirs) {
54 modules_loaded += Try_And_Load_Modules(module_dir, true);
55 if (modules_loaded >= expected_module_count) goto exit;
56 }
bigbiff22851b92021-09-01 16:46:57 -040057
bigbiffe3aa02e2021-10-01 13:07:38 -040058exit:
bigbiff850fa282021-10-09 12:37:29 -040059 if (ven)
60 ven->UnMount(false);
bigbiff22851b92021-09-01 16:46:57 -040061
Adithya Ra327aa72021-12-19 00:49:54 +053062 android::base::SetProperty("twrp.modules.loaded", "true");
63
64 TWFunc::Wait_For_Battery(3s);
65
bigbiff22851b92021-09-01 16:46:57 -040066 return true;
67}
68
bigbiff850fa282021-10-09 12:37:29 -040069int KernelModuleLoader::Try_And_Load_Modules(std::string module_dir, bool vendor_is_mounted) {
70 LOGINFO("Checking directory: %s\n", module_dir.c_str());
71 int modules_loaded = 0;
72 std::string dest_module_dir;
73 dest_module_dir = "/tmp" + module_dir;
74 TWFunc::Recursive_Mkdir(dest_module_dir);
75 Copy_Modules_To_Tmpfs(module_dir);
76 if (!Write_Module_List(dest_module_dir))
77 return kernel_modules_requested.size();
78 if (!vendor_is_mounted && module_dir == "/vendor/lib/modules") {
79 module_dir = "/lib/modules";
80 }
81 LOGINFO("mounting %s on %s\n", dest_module_dir.c_str(), module_dir.c_str());
82 if (mount(dest_module_dir.c_str(), module_dir.c_str(), "", MS_BIND, NULL) == 0) {
83 Modprobe m({module_dir}, "modules.load.twrp");
bigbiff850fa282021-10-09 12:37:29 -040084 m.LoadListedModules(false);
85 modules_loaded = m.GetModuleCount();
86 umount2(module_dir.c_str(), MNT_DETACH);
87 LOGINFO("Modules Loaded: %d\n", modules_loaded);
88 }
89 return modules_loaded;
90}
91
92std::vector<string> KernelModuleLoader::Skip_Loaded_Kernel_Modules() {
93 std::vector<string> kernel_modules = kernel_modules_requested;
94 std::vector<string> loaded_modules;
95 std::string kernel_module_file = "/proc/modules";
96 if (TWFunc::read_file(kernel_module_file, loaded_modules) < 0)
97 LOGINFO("failed to get loaded kernel modules\n");
Magendanz84164b92021-12-12 14:53:41 -080098 LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size());
bigbiff850fa282021-10-09 12:37:29 -040099 if (loaded_modules.size() == 0)
100 return kernel_modules;
101 for (auto&& module_line:loaded_modules) {
102 auto module = TWFunc::Split_String(module_line, " ")[0];
103 std::string full_module_name = module + ".ko";
104 auto found = std::find(kernel_modules.begin(), kernel_modules.end(), full_module_name);
105 if (found != kernel_modules.end()) {
106 LOGINFO("found module to dedupe: %s\n", (*found).c_str());
107 kernel_modules.erase(found);
108 }
109 }
110 return kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400111}
112
113bool KernelModuleLoader::Write_Module_List(std::string module_dir) {
114 DIR* d;
115 struct dirent* de;
116 std::vector<std::string> kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400117 d = opendir(module_dir.c_str());
bigbiff850fa282021-10-09 12:37:29 -0400118 auto deduped_modules = Skip_Loaded_Kernel_Modules();
119 if (deduped_modules.size() == 0) {
120 LOGINFO("Requested modules are loaded\n");
121 return false;
122 }
bigbiff22851b92021-09-01 16:46:57 -0400123 if (d != nullptr) {
124 while ((de = readdir(d)) != nullptr) {
125 std::string kernel_module = de->d_name;
126 if (de->d_type == DT_REG) {
127 if (android::base::EndsWith(kernel_module, ".ko")) {
128 for (auto&& requested:kernel_modules_requested) {
129 if (kernel_module == requested) {
130 kernel_modules.push_back(kernel_module);
bigbiff850fa282021-10-09 12:37:29 -0400131 continue;
132 }
bigbiff22851b92021-09-01 16:46:57 -0400133 }
134 continue;
135 }
136 }
137 }
bigbiff850fa282021-10-09 12:37:29 -0400138 std::string module_file = module_dir + "/modules.load.twrp";
bigbiff22851b92021-09-01 16:46:57 -0400139 TWFunc::write_to_file(module_file, kernel_modules);
bigbiff850fa282021-10-09 12:37:29 -0400140 closedir(d);
bigbiff22851b92021-09-01 16:46:57 -0400141 }
142 return true;
143}
144
145bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) {
bigbiff850fa282021-10-09 12:37:29 -0400146 std::string ramdisk_dir = "/tmp" + module_dir;
147 DIR* d;
bigbiff22851b92021-09-01 16:46:57 -0400148 struct dirent* de;
bigbiff850fa282021-10-09 12:37:29 -0400149 d = opendir(module_dir.c_str());
150 if (d != nullptr) {
151 while ((de = readdir(d)) != nullptr) {
152 std::string kernel_module = de->d_name;
153 if (de->d_type == DT_REG) {
154 std::string src = module_dir + "/" + de->d_name;
155 std::string dest = ramdisk_dir + "/" + de->d_name;
156 if (TWFunc::copy_file(src, dest, 0700, false) != 0) {
157 return false;
158 }
159 }
160 }
161 closedir(d);
162 } else {
163 LOGINFO("Unable to open module directory: %s. Skipping\n", module_dir.c_str());
164 return false;
165 }
166 return true;
167}