blob: db5dee8a16ab689e4e7f5b1bab4dbc0c41662920 [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)
bigbiff8c52c872022-04-10 17:34:46 -040014 // check /vendor_dlkm/lib/modules (vendor_dlkm mounted)
bigbiff850fa282021-10-09 12:37:29 -040015 int modules_loaded = 0;
bigbiff22851b92021-09-01 16:46:57 -040016
bigbiff850fa282021-10-09 12:37:29 -040017 LOGINFO("Attempting to load modules\n");
18 std::string vendor_base_dir(VENDOR_MODULE_DIR);
19 std::string base_dir(VENDOR_BOOT_MODULE_DIR);
bigbiff8c52c872022-04-10 17:34:46 -040020 std::string vendor_dlkm_base_dir(VENDOR_DLKM_MODULE_DIR);
bigbiff850fa282021-10-09 12:37:29 -040021 std::vector<std::string> module_dirs;
22 std::vector<std::string> vendor_module_dirs;
bigbiff22851b92021-09-01 16:46:57 -040023
bigbiff850fa282021-10-09 12:37:29 -040024 TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
bigbiff8c52c872022-04-10 17:34:46 -040025 TWPartition* ven_dlkm = PartitionManager.Find_Partition_By_Path("/vendor_dlkm");
bigbiff850fa282021-10-09 12:37:29 -040026 vendor_module_dirs.push_back(VENDOR_MODULE_DIR);
27 vendor_module_dirs.push_back(vendor_base_dir + "/1.1");
bigbiff22851b92021-09-01 16:46:57 -040028
bigbiff850fa282021-10-09 12:37:29 -040029 module_dirs.push_back(base_dir);
bigbiff22851b92021-09-01 16:46:57 -040030
bigbiff850fa282021-10-09 12:37:29 -040031 struct utsname uts;
32 if (uname(&uts)) {
33 LOGERR("Unable to query kernel for version info\n");
34 }
bigbiff22851b92021-09-01 16:46:57 -040035
bigbiff850fa282021-10-09 12:37:29 -040036 std::string rls(uts.release);
37 std::vector<std::string> release = TWFunc::split_string(rls, '.', true);
38 int expected_module_count = kernel_modules_requested.size();
39 module_dirs.push_back(base_dir + "/" + release[0] + "." + release[1]);
sekaiacge86b9c72021-12-31 19:30:44 +080040 std::string gki = "/" + release[0] + "." + release[1] + "-gki";
41 module_dirs.push_back(base_dir + gki);
42 vendor_module_dirs.push_back(vendor_base_dir + gki);
bigbiff22851b92021-09-01 16:46:57 -040043
bigbiff850fa282021-10-09 12:37:29 -040044 for (auto&& module_dir:module_dirs) {
45 modules_loaded += Try_And_Load_Modules(module_dir, false);
46 if (modules_loaded >= expected_module_count) goto exit;
47 }
bigbiff22851b92021-09-01 16:46:57 -040048
bigbiff850fa282021-10-09 12:37:29 -040049 for (auto&& module_dir:vendor_module_dirs) {
50 modules_loaded += Try_And_Load_Modules(module_dir, false);
51 if (modules_loaded >= expected_module_count) goto exit;
52 }
bigbiff22851b92021-09-01 16:46:57 -040053
bigbiff850fa282021-10-09 12:37:29 -040054 if (ven) {
55 LOGINFO("Checking mounted /vendor\n");
56 ven->Mount(true);
57 }
bigbiff8c52c872022-04-10 17:34:46 -040058 if (ven_dlkm) {
59 LOGINFO("Checking mounted /vendor_dlkm\n");
60 ven_dlkm->Mount(true);
61 }
bigbiff22851b92021-09-01 16:46:57 -040062
bigbiff850fa282021-10-09 12:37:29 -040063 for (auto&& module_dir:vendor_module_dirs) {
64 modules_loaded += Try_And_Load_Modules(module_dir, true);
65 if (modules_loaded >= expected_module_count) goto exit;
66 }
bigbiff22851b92021-09-01 16:46:57 -040067
bigbiff8c52c872022-04-10 17:34:46 -040068 modules_loaded += Try_And_Load_Modules(vendor_dlkm_base_dir, true);
69 if (modules_loaded >= expected_module_count) goto exit;
70
bigbiffe3aa02e2021-10-01 13:07:38 -040071exit:
bigbiff850fa282021-10-09 12:37:29 -040072 if (ven)
73 ven->UnMount(false);
bigbiff8c52c872022-04-10 17:34:46 -040074 if (ven_dlkm)
75 ven_dlkm->UnMount(false);
bigbiff22851b92021-09-01 16:46:57 -040076
Adithya Ra327aa72021-12-19 00:49:54 +053077 android::base::SetProperty("twrp.modules.loaded", "true");
78
sekaiacg997ff732022-06-24 11:04:49 +080079#ifdef TW_BATTERY_SYSFS_WAIT_SECONDS
80 TWFunc::Wait_For_Battery(std::chrono::seconds(TW_BATTERY_SYSFS_WAIT_SECONDS));
81#endif
Adithya Ra327aa72021-12-19 00:49:54 +053082
bigbiff22851b92021-09-01 16:46:57 -040083 return true;
84}
85
bigbiff850fa282021-10-09 12:37:29 -040086int KernelModuleLoader::Try_And_Load_Modules(std::string module_dir, bool vendor_is_mounted) {
87 LOGINFO("Checking directory: %s\n", module_dir.c_str());
88 int modules_loaded = 0;
89 std::string dest_module_dir;
90 dest_module_dir = "/tmp" + module_dir;
91 TWFunc::Recursive_Mkdir(dest_module_dir);
92 Copy_Modules_To_Tmpfs(module_dir);
93 if (!Write_Module_List(dest_module_dir))
94 return kernel_modules_requested.size();
95 if (!vendor_is_mounted && module_dir == "/vendor/lib/modules") {
96 module_dir = "/lib/modules";
97 }
98 LOGINFO("mounting %s on %s\n", dest_module_dir.c_str(), module_dir.c_str());
99 if (mount(dest_module_dir.c_str(), module_dir.c_str(), "", MS_BIND, NULL) == 0) {
nijel84b185322022-01-03 14:28:39 -0500100 Modprobe m({module_dir}, "modules.load.twrp", false);
bigbiff850fa282021-10-09 12:37:29 -0400101 m.LoadListedModules(false);
102 modules_loaded = m.GetModuleCount();
103 umount2(module_dir.c_str(), MNT_DETACH);
104 LOGINFO("Modules Loaded: %d\n", modules_loaded);
105 }
106 return modules_loaded;
107}
108
109std::vector<string> KernelModuleLoader::Skip_Loaded_Kernel_Modules() {
110 std::vector<string> kernel_modules = kernel_modules_requested;
111 std::vector<string> loaded_modules;
112 std::string kernel_module_file = "/proc/modules";
113 if (TWFunc::read_file(kernel_module_file, loaded_modules) < 0)
114 LOGINFO("failed to get loaded kernel modules\n");
Magendanz84164b92021-12-12 14:53:41 -0800115 LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size());
bigbiff850fa282021-10-09 12:37:29 -0400116 if (loaded_modules.size() == 0)
117 return kernel_modules;
118 for (auto&& module_line:loaded_modules) {
119 auto module = TWFunc::Split_String(module_line, " ")[0];
120 std::string full_module_name = module + ".ko";
121 auto found = std::find(kernel_modules.begin(), kernel_modules.end(), full_module_name);
122 if (found != kernel_modules.end()) {
123 LOGINFO("found module to dedupe: %s\n", (*found).c_str());
124 kernel_modules.erase(found);
125 }
126 }
127 return kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400128}
129
130bool KernelModuleLoader::Write_Module_List(std::string module_dir) {
131 DIR* d;
132 struct dirent* de;
133 std::vector<std::string> kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400134 d = opendir(module_dir.c_str());
bigbiff850fa282021-10-09 12:37:29 -0400135 auto deduped_modules = Skip_Loaded_Kernel_Modules();
136 if (deduped_modules.size() == 0) {
137 LOGINFO("Requested modules are loaded\n");
138 return false;
139 }
bigbiff22851b92021-09-01 16:46:57 -0400140 if (d != nullptr) {
141 while ((de = readdir(d)) != nullptr) {
142 std::string kernel_module = de->d_name;
143 if (de->d_type == DT_REG) {
144 if (android::base::EndsWith(kernel_module, ".ko")) {
145 for (auto&& requested:kernel_modules_requested) {
146 if (kernel_module == requested) {
147 kernel_modules.push_back(kernel_module);
bigbiff850fa282021-10-09 12:37:29 -0400148 continue;
149 }
bigbiff22851b92021-09-01 16:46:57 -0400150 }
151 continue;
152 }
153 }
154 }
bigbiff850fa282021-10-09 12:37:29 -0400155 std::string module_file = module_dir + "/modules.load.twrp";
bigbiff22851b92021-09-01 16:46:57 -0400156 TWFunc::write_to_file(module_file, kernel_modules);
bigbiff850fa282021-10-09 12:37:29 -0400157 closedir(d);
bigbiff22851b92021-09-01 16:46:57 -0400158 }
159 return true;
160}
161
162bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) {
bigbiff850fa282021-10-09 12:37:29 -0400163 std::string ramdisk_dir = "/tmp" + module_dir;
164 DIR* d;
bigbiff22851b92021-09-01 16:46:57 -0400165 struct dirent* de;
bigbiff850fa282021-10-09 12:37:29 -0400166 d = opendir(module_dir.c_str());
167 if (d != nullptr) {
168 while ((de = readdir(d)) != nullptr) {
169 std::string kernel_module = de->d_name;
170 if (de->d_type == DT_REG) {
171 std::string src = module_dir + "/" + de->d_name;
172 std::string dest = ramdisk_dir + "/" + de->d_name;
173 if (TWFunc::copy_file(src, dest, 0700, false) != 0) {
174 return false;
175 }
176 }
177 }
178 closedir(d);
179 } else {
180 LOGINFO("Unable to open module directory: %s. Skipping\n", module_dir.c_str());
181 return false;
182 }
183 return true;
184}