blob: 73304a6e43342cf1b83005b0b97b71b231213d00 [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]);
Captain Throwback5a98ce92023-08-18 10:54:41 -040040#ifndef TW_LOAD_VENDOR_MODULES_EXCLUDE_GKI
sekaiacge86b9c72021-12-31 19:30:44 +080041 std::string gki = "/" + release[0] + "." + release[1] + "-gki";
42 module_dirs.push_back(base_dir + gki);
43 vendor_module_dirs.push_back(vendor_base_dir + gki);
Captain Throwback5a98ce92023-08-18 10:54:41 -040044#endif
bigbiff22851b92021-09-01 16:46:57 -040045
bigbiff850fa282021-10-09 12:37:29 -040046 for (auto&& module_dir:module_dirs) {
47 modules_loaded += Try_And_Load_Modules(module_dir, false);
48 if (modules_loaded >= expected_module_count) goto exit;
49 }
bigbiff22851b92021-09-01 16:46:57 -040050
bigbiff850fa282021-10-09 12:37:29 -040051 for (auto&& module_dir:vendor_module_dirs) {
52 modules_loaded += Try_And_Load_Modules(module_dir, false);
53 if (modules_loaded >= expected_module_count) goto exit;
54 }
bigbiff22851b92021-09-01 16:46:57 -040055
bigbiff850fa282021-10-09 12:37:29 -040056 if (ven) {
57 LOGINFO("Checking mounted /vendor\n");
58 ven->Mount(true);
59 }
bigbiff8c52c872022-04-10 17:34:46 -040060 if (ven_dlkm) {
61 LOGINFO("Checking mounted /vendor_dlkm\n");
62 ven_dlkm->Mount(true);
63 }
bigbiff22851b92021-09-01 16:46:57 -040064
bigbiff850fa282021-10-09 12:37:29 -040065 for (auto&& module_dir:vendor_module_dirs) {
66 modules_loaded += Try_And_Load_Modules(module_dir, true);
67 if (modules_loaded >= expected_module_count) goto exit;
68 }
bigbiff22851b92021-09-01 16:46:57 -040069
bigbiff8c52c872022-04-10 17:34:46 -040070 modules_loaded += Try_And_Load_Modules(vendor_dlkm_base_dir, true);
71 if (modules_loaded >= expected_module_count) goto exit;
72
bigbiffe3aa02e2021-10-01 13:07:38 -040073exit:
bigbiff850fa282021-10-09 12:37:29 -040074 if (ven)
75 ven->UnMount(false);
bigbiff8c52c872022-04-10 17:34:46 -040076 if (ven_dlkm)
77 ven_dlkm->UnMount(false);
bigbiff22851b92021-09-01 16:46:57 -040078
Adithya Ra327aa72021-12-19 00:49:54 +053079 android::base::SetProperty("twrp.modules.loaded", "true");
80
bigbiff22851b92021-09-01 16:46:57 -040081 return true;
82}
83
bigbiff850fa282021-10-09 12:37:29 -040084int KernelModuleLoader::Try_And_Load_Modules(std::string module_dir, bool vendor_is_mounted) {
85 LOGINFO("Checking directory: %s\n", module_dir.c_str());
86 int modules_loaded = 0;
87 std::string dest_module_dir;
88 dest_module_dir = "/tmp" + module_dir;
89 TWFunc::Recursive_Mkdir(dest_module_dir);
90 Copy_Modules_To_Tmpfs(module_dir);
91 if (!Write_Module_List(dest_module_dir))
92 return kernel_modules_requested.size();
93 if (!vendor_is_mounted && module_dir == "/vendor/lib/modules") {
94 module_dir = "/lib/modules";
95 }
96 LOGINFO("mounting %s on %s\n", dest_module_dir.c_str(), module_dir.c_str());
97 if (mount(dest_module_dir.c_str(), module_dir.c_str(), "", MS_BIND, NULL) == 0) {
nijel84b185322022-01-03 14:28:39 -050098 Modprobe m({module_dir}, "modules.load.twrp", false);
bigbiff850fa282021-10-09 12:37:29 -040099 m.LoadListedModules(false);
100 modules_loaded = m.GetModuleCount();
101 umount2(module_dir.c_str(), MNT_DETACH);
102 LOGINFO("Modules Loaded: %d\n", modules_loaded);
103 }
104 return modules_loaded;
105}
106
107std::vector<string> KernelModuleLoader::Skip_Loaded_Kernel_Modules() {
108 std::vector<string> kernel_modules = kernel_modules_requested;
109 std::vector<string> loaded_modules;
110 std::string kernel_module_file = "/proc/modules";
111 if (TWFunc::read_file(kernel_module_file, loaded_modules) < 0)
112 LOGINFO("failed to get loaded kernel modules\n");
Magendanz84164b92021-12-12 14:53:41 -0800113 LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size());
bigbiff850fa282021-10-09 12:37:29 -0400114 if (loaded_modules.size() == 0)
115 return kernel_modules;
116 for (auto&& module_line:loaded_modules) {
117 auto module = TWFunc::Split_String(module_line, " ")[0];
118 std::string full_module_name = module + ".ko";
119 auto found = std::find(kernel_modules.begin(), kernel_modules.end(), full_module_name);
120 if (found != kernel_modules.end()) {
121 LOGINFO("found module to dedupe: %s\n", (*found).c_str());
122 kernel_modules.erase(found);
123 }
124 }
125 return kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400126}
127
128bool KernelModuleLoader::Write_Module_List(std::string module_dir) {
129 DIR* d;
130 struct dirent* de;
131 std::vector<std::string> kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400132 d = opendir(module_dir.c_str());
bigbiff850fa282021-10-09 12:37:29 -0400133 auto deduped_modules = Skip_Loaded_Kernel_Modules();
134 if (deduped_modules.size() == 0) {
135 LOGINFO("Requested modules are loaded\n");
136 return false;
137 }
bigbiff22851b92021-09-01 16:46:57 -0400138 if (d != nullptr) {
139 while ((de = readdir(d)) != nullptr) {
140 std::string kernel_module = de->d_name;
141 if (de->d_type == DT_REG) {
142 if (android::base::EndsWith(kernel_module, ".ko")) {
143 for (auto&& requested:kernel_modules_requested) {
144 if (kernel_module == requested) {
145 kernel_modules.push_back(kernel_module);
bigbiff850fa282021-10-09 12:37:29 -0400146 continue;
147 }
bigbiff22851b92021-09-01 16:46:57 -0400148 }
149 continue;
150 }
151 }
152 }
bigbiff850fa282021-10-09 12:37:29 -0400153 std::string module_file = module_dir + "/modules.load.twrp";
bigbiff22851b92021-09-01 16:46:57 -0400154 TWFunc::write_to_file(module_file, kernel_modules);
bigbiff850fa282021-10-09 12:37:29 -0400155 closedir(d);
bigbiff22851b92021-09-01 16:46:57 -0400156 }
157 return true;
158}
159
160bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) {
bigbiff850fa282021-10-09 12:37:29 -0400161 std::string ramdisk_dir = "/tmp" + module_dir;
162 DIR* d;
bigbiff22851b92021-09-01 16:46:57 -0400163 struct dirent* de;
bigbiff850fa282021-10-09 12:37:29 -0400164 d = opendir(module_dir.c_str());
165 if (d != nullptr) {
166 while ((de = readdir(d)) != nullptr) {
167 std::string kernel_module = de->d_name;
168 if (de->d_type == DT_REG) {
169 std::string src = module_dir + "/" + de->d_name;
170 std::string dest = ramdisk_dir + "/" + de->d_name;
171 if (TWFunc::copy_file(src, dest, 0700, false) != 0) {
172 return false;
173 }
174 }
175 }
176 closedir(d);
177 } else {
178 LOGINFO("Unable to open module directory: %s. Skipping\n", module_dir.c_str());
179 return false;
180 }
181 return true;
182}