blob: aadfd935d89b29b6b71f2371b85fb4cdb4a5e73c [file] [log] [blame]
bigbiff22851b92021-09-01 16:46:57 -04001#include "kernel_module_loader.hpp"
2
bigbiffe3aa02e2021-10-01 13:07:38 -04003const std::vector<std::string> kernel_modules_requested = TWFunc::split_string(EXPAND(TW_LOAD_VENDOR_MODULES), ' ', true);
4
bigbiff850fa282021-10-09 12:37:29 -04005bool KernelModuleLoader::Load_Vendor_Modules() {
6 // check /lib/modules (ramdisk vendor_boot)
7 // check /lib/modules/N.N (ramdisk vendor_boot)
8 // check /lib/modules/N.N-gki (ramdisk vendor_boot)
9 // check /vendor/lib/modules (ramdisk)
10 // check /vendor/lib/modules/1.1 (ramdisk prebuilt modules)
11 // check /vendor/lib/modules/N.N (vendor mounted)
12 // check /vendor/lib/modules/N.N-gki (vendor mounted)
13 int modules_loaded = 0;
bigbiff22851b92021-09-01 16:46:57 -040014
bigbiff850fa282021-10-09 12:37:29 -040015 LOGINFO("Attempting to load modules\n");
16 std::string vendor_base_dir(VENDOR_MODULE_DIR);
17 std::string base_dir(VENDOR_BOOT_MODULE_DIR);
18 std::vector<std::string> module_dirs;
19 std::vector<std::string> vendor_module_dirs;
bigbiff22851b92021-09-01 16:46:57 -040020
bigbiff850fa282021-10-09 12:37:29 -040021 TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
22 vendor_module_dirs.push_back(VENDOR_MODULE_DIR);
23 vendor_module_dirs.push_back(vendor_base_dir + "/1.1");
bigbiff22851b92021-09-01 16:46:57 -040024
bigbiff850fa282021-10-09 12:37:29 -040025 module_dirs.push_back(base_dir);
bigbiff22851b92021-09-01 16:46:57 -040026
bigbiff850fa282021-10-09 12:37:29 -040027 struct utsname uts;
28 if (uname(&uts)) {
29 LOGERR("Unable to query kernel for version info\n");
30 }
bigbiff22851b92021-09-01 16:46:57 -040031
bigbiff850fa282021-10-09 12:37:29 -040032 std::string rls(uts.release);
33 std::vector<std::string> release = TWFunc::split_string(rls, '.', true);
34 int expected_module_count = kernel_modules_requested.size();
35 module_dirs.push_back(base_dir + "/" + release[0] + "." + release[1]);
bigbiff22851b92021-09-01 16:46:57 -040036
bigbiff850fa282021-10-09 12:37:29 -040037 for (auto&& module_dir:module_dirs) {
38 modules_loaded += Try_And_Load_Modules(module_dir, false);
39 if (modules_loaded >= expected_module_count) goto exit;
40 }
bigbiff22851b92021-09-01 16:46:57 -040041
bigbiff850fa282021-10-09 12:37:29 -040042 for (auto&& module_dir:vendor_module_dirs) {
43 modules_loaded += Try_And_Load_Modules(module_dir, false);
44 if (modules_loaded >= expected_module_count) goto exit;
45 }
bigbiff22851b92021-09-01 16:46:57 -040046
bigbiff850fa282021-10-09 12:37:29 -040047 if (ven) {
48 LOGINFO("Checking mounted /vendor\n");
49 ven->Mount(true);
50 }
bigbiff22851b92021-09-01 16:46:57 -040051
bigbiff850fa282021-10-09 12:37:29 -040052 for (auto&& module_dir:vendor_module_dirs) {
53 modules_loaded += Try_And_Load_Modules(module_dir, true);
54 if (modules_loaded >= expected_module_count) goto exit;
55 }
bigbiff22851b92021-09-01 16:46:57 -040056
bigbiffe3aa02e2021-10-01 13:07:38 -040057exit:
bigbiff850fa282021-10-09 12:37:29 -040058 if (ven)
59 ven->UnMount(false);
bigbiff22851b92021-09-01 16:46:57 -040060
61 return true;
62}
63
bigbiff850fa282021-10-09 12:37:29 -040064int KernelModuleLoader::Try_And_Load_Modules(std::string module_dir, bool vendor_is_mounted) {
65 LOGINFO("Checking directory: %s\n", module_dir.c_str());
66 int modules_loaded = 0;
67 std::string dest_module_dir;
68 dest_module_dir = "/tmp" + module_dir;
69 TWFunc::Recursive_Mkdir(dest_module_dir);
70 Copy_Modules_To_Tmpfs(module_dir);
71 if (!Write_Module_List(dest_module_dir))
72 return kernel_modules_requested.size();
73 if (!vendor_is_mounted && module_dir == "/vendor/lib/modules") {
74 module_dir = "/lib/modules";
75 }
76 LOGINFO("mounting %s on %s\n", dest_module_dir.c_str(), module_dir.c_str());
77 if (mount(dest_module_dir.c_str(), module_dir.c_str(), "", MS_BIND, NULL) == 0) {
78 Modprobe m({module_dir}, "modules.load.twrp");
bigbiff850fa282021-10-09 12:37:29 -040079 m.LoadListedModules(false);
80 modules_loaded = m.GetModuleCount();
81 umount2(module_dir.c_str(), MNT_DETACH);
82 LOGINFO("Modules Loaded: %d\n", modules_loaded);
83 }
84 return modules_loaded;
85}
86
87std::vector<string> KernelModuleLoader::Skip_Loaded_Kernel_Modules() {
88 std::vector<string> kernel_modules = kernel_modules_requested;
89 std::vector<string> loaded_modules;
90 std::string kernel_module_file = "/proc/modules";
91 if (TWFunc::read_file(kernel_module_file, loaded_modules) < 0)
92 LOGINFO("failed to get loaded kernel modules\n");
Magendanz84164b92021-12-12 14:53:41 -080093 LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size());
bigbiff850fa282021-10-09 12:37:29 -040094 if (loaded_modules.size() == 0)
95 return kernel_modules;
96 for (auto&& module_line:loaded_modules) {
97 auto module = TWFunc::Split_String(module_line, " ")[0];
98 std::string full_module_name = module + ".ko";
99 auto found = std::find(kernel_modules.begin(), kernel_modules.end(), full_module_name);
100 if (found != kernel_modules.end()) {
101 LOGINFO("found module to dedupe: %s\n", (*found).c_str());
102 kernel_modules.erase(found);
103 }
104 }
105 return kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400106}
107
108bool KernelModuleLoader::Write_Module_List(std::string module_dir) {
109 DIR* d;
110 struct dirent* de;
111 std::vector<std::string> kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400112 d = opendir(module_dir.c_str());
bigbiff850fa282021-10-09 12:37:29 -0400113 auto deduped_modules = Skip_Loaded_Kernel_Modules();
114 if (deduped_modules.size() == 0) {
115 LOGINFO("Requested modules are loaded\n");
116 return false;
117 }
bigbiff22851b92021-09-01 16:46:57 -0400118 if (d != nullptr) {
119 while ((de = readdir(d)) != nullptr) {
120 std::string kernel_module = de->d_name;
121 if (de->d_type == DT_REG) {
122 if (android::base::EndsWith(kernel_module, ".ko")) {
123 for (auto&& requested:kernel_modules_requested) {
124 if (kernel_module == requested) {
125 kernel_modules.push_back(kernel_module);
bigbiff850fa282021-10-09 12:37:29 -0400126 continue;
127 }
bigbiff22851b92021-09-01 16:46:57 -0400128 }
129 continue;
130 }
131 }
132 }
bigbiff850fa282021-10-09 12:37:29 -0400133 std::string module_file = module_dir + "/modules.load.twrp";
bigbiff22851b92021-09-01 16:46:57 -0400134 TWFunc::write_to_file(module_file, kernel_modules);
bigbiff850fa282021-10-09 12:37:29 -0400135 closedir(d);
bigbiff22851b92021-09-01 16:46:57 -0400136 }
137 return true;
138}
139
140bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) {
bigbiff850fa282021-10-09 12:37:29 -0400141 std::string ramdisk_dir = "/tmp" + module_dir;
142 DIR* d;
bigbiff22851b92021-09-01 16:46:57 -0400143 struct dirent* de;
bigbiff850fa282021-10-09 12:37:29 -0400144 d = opendir(module_dir.c_str());
145 if (d != nullptr) {
146 while ((de = readdir(d)) != nullptr) {
147 std::string kernel_module = de->d_name;
148 if (de->d_type == DT_REG) {
149 std::string src = module_dir + "/" + de->d_name;
150 std::string dest = ramdisk_dir + "/" + de->d_name;
151 if (TWFunc::copy_file(src, dest, 0700, false) != 0) {
152 return false;
153 }
154 }
155 }
156 closedir(d);
157 } else {
158 LOGINFO("Unable to open module directory: %s. Skipping\n", module_dir.c_str());
159 return false;
160 }
161 return true;
162}