blob: 1f30d0df42363a026b53aabd9dacd1dd0fedd0e7 [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");
79 m.EnableVerbose(true);
80 m.LoadListedModules(false);
81 modules_loaded = m.GetModuleCount();
82 umount2(module_dir.c_str(), MNT_DETACH);
83 LOGINFO("Modules Loaded: %d\n", modules_loaded);
84 }
85 return modules_loaded;
86}
87
88std::vector<string> KernelModuleLoader::Skip_Loaded_Kernel_Modules() {
89 std::vector<string> kernel_modules = kernel_modules_requested;
90 std::vector<string> loaded_modules;
91 std::string kernel_module_file = "/proc/modules";
92 if (TWFunc::read_file(kernel_module_file, loaded_modules) < 0)
93 LOGINFO("failed to get loaded kernel modules\n");
Magendanz37920d92021-12-12 14:53:41 -080094 LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size());
bigbiff850fa282021-10-09 12:37:29 -040095 if (loaded_modules.size() == 0)
96 return kernel_modules;
97 for (auto&& module_line:loaded_modules) {
98 auto module = TWFunc::Split_String(module_line, " ")[0];
99 std::string full_module_name = module + ".ko";
100 auto found = std::find(kernel_modules.begin(), kernel_modules.end(), full_module_name);
101 if (found != kernel_modules.end()) {
102 LOGINFO("found module to dedupe: %s\n", (*found).c_str());
103 kernel_modules.erase(found);
104 }
105 }
106 return kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400107}
108
109bool KernelModuleLoader::Write_Module_List(std::string module_dir) {
110 DIR* d;
111 struct dirent* de;
112 std::vector<std::string> kernel_modules;
bigbiff22851b92021-09-01 16:46:57 -0400113 d = opendir(module_dir.c_str());
bigbiff850fa282021-10-09 12:37:29 -0400114 auto deduped_modules = Skip_Loaded_Kernel_Modules();
115 if (deduped_modules.size() == 0) {
116 LOGINFO("Requested modules are loaded\n");
117 return false;
118 }
bigbiff22851b92021-09-01 16:46:57 -0400119 if (d != nullptr) {
120 while ((de = readdir(d)) != nullptr) {
121 std::string kernel_module = de->d_name;
122 if (de->d_type == DT_REG) {
123 if (android::base::EndsWith(kernel_module, ".ko")) {
124 for (auto&& requested:kernel_modules_requested) {
125 if (kernel_module == requested) {
126 kernel_modules.push_back(kernel_module);
bigbiff850fa282021-10-09 12:37:29 -0400127 continue;
128 }
bigbiff22851b92021-09-01 16:46:57 -0400129 }
130 continue;
131 }
132 }
133 }
bigbiff850fa282021-10-09 12:37:29 -0400134 std::string module_file = module_dir + "/modules.load.twrp";
bigbiff22851b92021-09-01 16:46:57 -0400135 TWFunc::write_to_file(module_file, kernel_modules);
bigbiff850fa282021-10-09 12:37:29 -0400136 closedir(d);
bigbiff22851b92021-09-01 16:46:57 -0400137 }
138 return true;
139}
140
141bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) {
bigbiff850fa282021-10-09 12:37:29 -0400142 std::string ramdisk_dir = "/tmp" + module_dir;
143 DIR* d;
bigbiff22851b92021-09-01 16:46:57 -0400144 struct dirent* de;
bigbiff850fa282021-10-09 12:37:29 -0400145 d = opendir(module_dir.c_str());
146 if (d != nullptr) {
147 while ((de = readdir(d)) != nullptr) {
148 std::string kernel_module = de->d_name;
149 if (de->d_type == DT_REG) {
150 std::string src = module_dir + "/" + de->d_name;
151 std::string dest = ramdisk_dir + "/" + de->d_name;
152 if (TWFunc::copy_file(src, dest, 0700, false) != 0) {
153 return false;
154 }
155 }
156 }
157 closedir(d);
158 } else {
159 LOGINFO("Unable to open module directory: %s. Skipping\n", module_dir.c_str());
160 return false;
161 }
162 return true;
163}