bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 1 | #include "kernel_module_loader.hpp" |
Adithya R | a327aa7 | 2021-12-19 00:49:54 +0530 | [diff] [blame] | 2 | #include "common.h" |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 3 | |
bigbiff | e3aa02e | 2021-10-01 13:07:38 -0400 | [diff] [blame] | 4 | const std::vector<std::string> kernel_modules_requested = TWFunc::split_string(EXPAND(TW_LOAD_VENDOR_MODULES), ' ', true); |
| 5 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 6 | bool 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) |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 14 | // check /vendor_dlkm/lib/modules (vendor_dlkm mounted) |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 15 | int modules_loaded = 0; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 16 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 17 | LOGINFO("Attempting to load modules\n"); |
| 18 | std::string vendor_base_dir(VENDOR_MODULE_DIR); |
| 19 | std::string base_dir(VENDOR_BOOT_MODULE_DIR); |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 20 | std::string vendor_dlkm_base_dir(VENDOR_DLKM_MODULE_DIR); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 21 | std::vector<std::string> module_dirs; |
| 22 | std::vector<std::string> vendor_module_dirs; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 23 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 24 | TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor"); |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 25 | TWPartition* ven_dlkm = PartitionManager.Find_Partition_By_Path("/vendor_dlkm"); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 26 | vendor_module_dirs.push_back(VENDOR_MODULE_DIR); |
| 27 | vendor_module_dirs.push_back(vendor_base_dir + "/1.1"); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 28 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 29 | module_dirs.push_back(base_dir); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 30 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 31 | struct utsname uts; |
| 32 | if (uname(&uts)) { |
| 33 | LOGERR("Unable to query kernel for version info\n"); |
| 34 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 35 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 36 | 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 Throwback | fd464d5 | 2023-08-18 10:54:41 -0400 | [diff] [blame] | 40 | #ifndef TW_LOAD_VENDOR_MODULES_EXCLUDE_GKI |
sekaiacg | e86b9c7 | 2021-12-31 19:30:44 +0800 | [diff] [blame] | 41 | 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 Throwback | fd464d5 | 2023-08-18 10:54:41 -0400 | [diff] [blame] | 44 | #endif |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 45 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 46 | 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 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 50 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 51 | 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 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 55 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 56 | if (ven) { |
| 57 | LOGINFO("Checking mounted /vendor\n"); |
| 58 | ven->Mount(true); |
| 59 | } |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 60 | if (ven_dlkm) { |
| 61 | LOGINFO("Checking mounted /vendor_dlkm\n"); |
| 62 | ven_dlkm->Mount(true); |
| 63 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 64 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 65 | 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 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 69 | |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 70 | modules_loaded += Try_And_Load_Modules(vendor_dlkm_base_dir, true); |
| 71 | if (modules_loaded >= expected_module_count) goto exit; |
| 72 | |
bigbiff | e3aa02e | 2021-10-01 13:07:38 -0400 | [diff] [blame] | 73 | exit: |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 74 | if (ven) |
| 75 | ven->UnMount(false); |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 76 | if (ven_dlkm) |
| 77 | ven_dlkm->UnMount(false); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 78 | |
Adithya R | a327aa7 | 2021-12-19 00:49:54 +0530 | [diff] [blame] | 79 | android::base::SetProperty("twrp.modules.loaded", "true"); |
| 80 | |
sekaiacg | 997ff73 | 2022-06-24 11:04:49 +0800 | [diff] [blame] | 81 | #ifdef TW_BATTERY_SYSFS_WAIT_SECONDS |
| 82 | TWFunc::Wait_For_Battery(std::chrono::seconds(TW_BATTERY_SYSFS_WAIT_SECONDS)); |
| 83 | #endif |
Adithya R | a327aa7 | 2021-12-19 00:49:54 +0530 | [diff] [blame] | 84 | |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 88 | int KernelModuleLoader::Try_And_Load_Modules(std::string module_dir, bool vendor_is_mounted) { |
| 89 | LOGINFO("Checking directory: %s\n", module_dir.c_str()); |
| 90 | int modules_loaded = 0; |
| 91 | std::string dest_module_dir; |
| 92 | dest_module_dir = "/tmp" + module_dir; |
| 93 | TWFunc::Recursive_Mkdir(dest_module_dir); |
| 94 | Copy_Modules_To_Tmpfs(module_dir); |
| 95 | if (!Write_Module_List(dest_module_dir)) |
| 96 | return kernel_modules_requested.size(); |
| 97 | if (!vendor_is_mounted && module_dir == "/vendor/lib/modules") { |
| 98 | module_dir = "/lib/modules"; |
| 99 | } |
| 100 | LOGINFO("mounting %s on %s\n", dest_module_dir.c_str(), module_dir.c_str()); |
| 101 | if (mount(dest_module_dir.c_str(), module_dir.c_str(), "", MS_BIND, NULL) == 0) { |
nijel8 | 4b18532 | 2022-01-03 14:28:39 -0500 | [diff] [blame] | 102 | Modprobe m({module_dir}, "modules.load.twrp", false); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 103 | m.LoadListedModules(false); |
| 104 | modules_loaded = m.GetModuleCount(); |
| 105 | umount2(module_dir.c_str(), MNT_DETACH); |
| 106 | LOGINFO("Modules Loaded: %d\n", modules_loaded); |
| 107 | } |
| 108 | return modules_loaded; |
| 109 | } |
| 110 | |
| 111 | std::vector<string> KernelModuleLoader::Skip_Loaded_Kernel_Modules() { |
| 112 | std::vector<string> kernel_modules = kernel_modules_requested; |
| 113 | std::vector<string> loaded_modules; |
| 114 | std::string kernel_module_file = "/proc/modules"; |
| 115 | if (TWFunc::read_file(kernel_module_file, loaded_modules) < 0) |
| 116 | LOGINFO("failed to get loaded kernel modules\n"); |
Magendanz | 84164b9 | 2021-12-12 14:53:41 -0800 | [diff] [blame] | 117 | LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size()); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 118 | if (loaded_modules.size() == 0) |
| 119 | return kernel_modules; |
| 120 | for (auto&& module_line:loaded_modules) { |
| 121 | auto module = TWFunc::Split_String(module_line, " ")[0]; |
| 122 | std::string full_module_name = module + ".ko"; |
| 123 | auto found = std::find(kernel_modules.begin(), kernel_modules.end(), full_module_name); |
| 124 | if (found != kernel_modules.end()) { |
| 125 | LOGINFO("found module to dedupe: %s\n", (*found).c_str()); |
| 126 | kernel_modules.erase(found); |
| 127 | } |
| 128 | } |
| 129 | return kernel_modules; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | bool KernelModuleLoader::Write_Module_List(std::string module_dir) { |
| 133 | DIR* d; |
| 134 | struct dirent* de; |
| 135 | std::vector<std::string> kernel_modules; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 136 | d = opendir(module_dir.c_str()); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 137 | auto deduped_modules = Skip_Loaded_Kernel_Modules(); |
| 138 | if (deduped_modules.size() == 0) { |
| 139 | LOGINFO("Requested modules are loaded\n"); |
| 140 | return false; |
| 141 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 142 | if (d != nullptr) { |
| 143 | while ((de = readdir(d)) != nullptr) { |
| 144 | std::string kernel_module = de->d_name; |
| 145 | if (de->d_type == DT_REG) { |
| 146 | if (android::base::EndsWith(kernel_module, ".ko")) { |
| 147 | for (auto&& requested:kernel_modules_requested) { |
| 148 | if (kernel_module == requested) { |
| 149 | kernel_modules.push_back(kernel_module); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 150 | continue; |
| 151 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 152 | } |
| 153 | continue; |
| 154 | } |
| 155 | } |
| 156 | } |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 157 | std::string module_file = module_dir + "/modules.load.twrp"; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 158 | TWFunc::write_to_file(module_file, kernel_modules); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 159 | closedir(d); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) { |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 165 | std::string ramdisk_dir = "/tmp" + module_dir; |
| 166 | DIR* d; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 167 | struct dirent* de; |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 168 | d = opendir(module_dir.c_str()); |
| 169 | if (d != nullptr) { |
| 170 | while ((de = readdir(d)) != nullptr) { |
| 171 | std::string kernel_module = de->d_name; |
| 172 | if (de->d_type == DT_REG) { |
| 173 | std::string src = module_dir + "/" + de->d_name; |
| 174 | std::string dest = ramdisk_dir + "/" + de->d_name; |
| 175 | if (TWFunc::copy_file(src, dest, 0700, false) != 0) { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | closedir(d); |
| 181 | } else { |
| 182 | LOGINFO("Unable to open module directory: %s. Skipping\n", module_dir.c_str()); |
| 183 | return false; |
| 184 | } |
| 185 | return true; |
| 186 | } |