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" |
Mohd Faraz | fe8bcd4 | 2023-08-26 20:50:26 +0530 | [diff] [blame] | 3 | #include "variables.h" |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 4 | |
bigbiff | e3aa02e | 2021-10-01 13:07:38 -0400 | [diff] [blame] | 5 | const std::vector<std::string> kernel_modules_requested = TWFunc::split_string(EXPAND(TW_LOAD_VENDOR_MODULES), ' ', true); |
| 6 | |
Mohd Faraz | fe8bcd4 | 2023-08-26 20:50:26 +0530 | [diff] [blame] | 7 | BOOT_MODE KernelModuleLoader::Get_Boot_Mode() { |
| 8 | std::string cmdline; |
| 9 | std::string bootconfig; |
| 10 | android::base::ReadFileToString("/proc/bootconfig", &bootconfig); |
| 11 | android::base::ReadFileToString("/proc/cmdline", &cmdline); |
| 12 | |
| 13 | if (cmdline.find("twrpfastboot=1") != std::string::npos && (bootconfig.find("androidboot.force_normal_boot = \"1\"") != std::string::npos || |
| 14 | cmdline.find("androidboot.force_normal_boot=1") != std::string::npos)) |
| 15 | return RECOVERY_FASTBOOT_MODE; |
| 16 | else if (android::base::GetProperty(TW_FASTBOOT_MODE_PROP, "0") == "1") |
| 17 | return FASTBOOTD_MODE; |
| 18 | |
| 19 | return RECOVERY_IN_BOOT_MODE; |
| 20 | } |
| 21 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 22 | bool KernelModuleLoader::Load_Vendor_Modules() { |
| 23 | // check /lib/modules (ramdisk vendor_boot) |
| 24 | // check /lib/modules/N.N (ramdisk vendor_boot) |
| 25 | // check /lib/modules/N.N-gki (ramdisk vendor_boot) |
| 26 | // check /vendor/lib/modules (ramdisk) |
| 27 | // check /vendor/lib/modules/1.1 (ramdisk prebuilt modules) |
| 28 | // check /vendor/lib/modules/N.N (vendor mounted) |
| 29 | // check /vendor/lib/modules/N.N-gki (vendor mounted) |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 30 | // check /vendor_dlkm/lib/modules (vendor_dlkm mounted) |
Mohd Faraz | fe8bcd4 | 2023-08-26 20:50:26 +0530 | [diff] [blame] | 31 | if (android::base::GetBoolProperty(TW_MODULES_MOUNTED_PROP, false)) return true; |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 32 | int modules_loaded = 0; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 33 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 34 | LOGINFO("Attempting to load modules\n"); |
| 35 | std::string vendor_base_dir(VENDOR_MODULE_DIR); |
| 36 | std::string base_dir(VENDOR_BOOT_MODULE_DIR); |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 37 | std::string vendor_dlkm_base_dir(VENDOR_DLKM_MODULE_DIR); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 38 | std::vector<std::string> module_dirs; |
| 39 | std::vector<std::string> vendor_module_dirs; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 40 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 41 | TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor"); |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 42 | TWPartition* ven_dlkm = PartitionManager.Find_Partition_By_Path("/vendor_dlkm"); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 43 | vendor_module_dirs.push_back(VENDOR_MODULE_DIR); |
| 44 | vendor_module_dirs.push_back(vendor_base_dir + "/1.1"); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 45 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 46 | module_dirs.push_back(base_dir); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 47 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 48 | struct utsname uts; |
| 49 | if (uname(&uts)) { |
| 50 | LOGERR("Unable to query kernel for version info\n"); |
| 51 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 52 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 53 | std::string rls(uts.release); |
| 54 | std::vector<std::string> release = TWFunc::split_string(rls, '.', true); |
| 55 | int expected_module_count = kernel_modules_requested.size(); |
| 56 | module_dirs.push_back(base_dir + "/" + release[0] + "." + release[1]); |
Captain Throwback | fd464d5 | 2023-08-18 10:54:41 -0400 | [diff] [blame] | 57 | #ifndef TW_LOAD_VENDOR_MODULES_EXCLUDE_GKI |
sekaiacg | e86b9c7 | 2021-12-31 19:30:44 +0800 | [diff] [blame] | 58 | std::string gki = "/" + release[0] + "." + release[1] + "-gki"; |
| 59 | module_dirs.push_back(base_dir + gki); |
| 60 | vendor_module_dirs.push_back(vendor_base_dir + gki); |
Captain Throwback | fd464d5 | 2023-08-18 10:54:41 -0400 | [diff] [blame] | 61 | #endif |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 62 | |
Mohd Faraz | fe8bcd4 | 2023-08-26 20:50:26 +0530 | [diff] [blame] | 63 | switch(Get_Boot_Mode()) { |
| 64 | case RECOVERY_FASTBOOT_MODE: |
| 65 | /* On bootmode: once, there is not always stock kernel |
| 66 | * so try only with twrp prebuilt modules. |
| 67 | */ |
| 68 | for (auto&& module_dir:vendor_module_dirs) { |
| 69 | modules_loaded += Try_And_Load_Modules(module_dir, false); |
| 70 | if (modules_loaded >= expected_module_count) goto exit; |
| 71 | } |
| 72 | break; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 73 | |
Mohd Faraz | fe8bcd4 | 2023-08-26 20:50:26 +0530 | [diff] [blame] | 74 | case FASTBOOTD_MODE: |
| 75 | case RECOVERY_IN_BOOT_MODE: |
| 76 | /* In both mode vendor_boot or vendor modules are used |
| 77 | * Because Ramdisk is flashed in both. |
| 78 | */ |
| 79 | break; |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 80 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 81 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 82 | if (ven) { |
| 83 | LOGINFO("Checking mounted /vendor\n"); |
| 84 | ven->Mount(true); |
| 85 | } |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 86 | if (ven_dlkm) { |
| 87 | LOGINFO("Checking mounted /vendor_dlkm\n"); |
| 88 | ven_dlkm->Mount(true); |
| 89 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 90 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 91 | for (auto&& module_dir:vendor_module_dirs) { |
| 92 | modules_loaded += Try_And_Load_Modules(module_dir, true); |
| 93 | if (modules_loaded >= expected_module_count) goto exit; |
| 94 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 95 | |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 96 | modules_loaded += Try_And_Load_Modules(vendor_dlkm_base_dir, true); |
| 97 | if (modules_loaded >= expected_module_count) goto exit; |
| 98 | |
bigbiff | e3aa02e | 2021-10-01 13:07:38 -0400 | [diff] [blame] | 99 | exit: |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 100 | if (ven) |
| 101 | ven->UnMount(false); |
bigbiff | 8c52c87 | 2022-04-10 17:34:46 -0400 | [diff] [blame] | 102 | if (ven_dlkm) |
| 103 | ven_dlkm->UnMount(false); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 104 | |
Mohd Faraz | fe8bcd4 | 2023-08-26 20:50:26 +0530 | [diff] [blame] | 105 | android::base::SetProperty(TW_MODULES_MOUNTED_PROP, "true"); |
Adithya R | a327aa7 | 2021-12-19 00:49:54 +0530 | [diff] [blame] | 106 | |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 110 | int KernelModuleLoader::Try_And_Load_Modules(std::string module_dir, bool vendor_is_mounted) { |
| 111 | LOGINFO("Checking directory: %s\n", module_dir.c_str()); |
| 112 | int modules_loaded = 0; |
| 113 | std::string dest_module_dir; |
| 114 | dest_module_dir = "/tmp" + module_dir; |
| 115 | TWFunc::Recursive_Mkdir(dest_module_dir); |
| 116 | Copy_Modules_To_Tmpfs(module_dir); |
| 117 | if (!Write_Module_List(dest_module_dir)) |
| 118 | return kernel_modules_requested.size(); |
| 119 | if (!vendor_is_mounted && module_dir == "/vendor/lib/modules") { |
| 120 | module_dir = "/lib/modules"; |
| 121 | } |
| 122 | LOGINFO("mounting %s on %s\n", dest_module_dir.c_str(), module_dir.c_str()); |
| 123 | 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] | 124 | Modprobe m({module_dir}, "modules.load.twrp", false); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 125 | m.LoadListedModules(false); |
| 126 | modules_loaded = m.GetModuleCount(); |
| 127 | umount2(module_dir.c_str(), MNT_DETACH); |
| 128 | LOGINFO("Modules Loaded: %d\n", modules_loaded); |
| 129 | } |
| 130 | return modules_loaded; |
| 131 | } |
| 132 | |
| 133 | std::vector<string> KernelModuleLoader::Skip_Loaded_Kernel_Modules() { |
| 134 | std::vector<string> kernel_modules = kernel_modules_requested; |
| 135 | std::vector<string> loaded_modules; |
| 136 | std::string kernel_module_file = "/proc/modules"; |
| 137 | if (TWFunc::read_file(kernel_module_file, loaded_modules) < 0) |
| 138 | LOGINFO("failed to get loaded kernel modules\n"); |
Magendanz | 84164b9 | 2021-12-12 14:53:41 -0800 | [diff] [blame] | 139 | LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size()); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 140 | if (loaded_modules.size() == 0) |
| 141 | return kernel_modules; |
| 142 | for (auto&& module_line:loaded_modules) { |
| 143 | auto module = TWFunc::Split_String(module_line, " ")[0]; |
| 144 | std::string full_module_name = module + ".ko"; |
| 145 | auto found = std::find(kernel_modules.begin(), kernel_modules.end(), full_module_name); |
| 146 | if (found != kernel_modules.end()) { |
| 147 | LOGINFO("found module to dedupe: %s\n", (*found).c_str()); |
| 148 | kernel_modules.erase(found); |
| 149 | } |
| 150 | } |
| 151 | return kernel_modules; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool KernelModuleLoader::Write_Module_List(std::string module_dir) { |
| 155 | DIR* d; |
| 156 | struct dirent* de; |
| 157 | std::vector<std::string> kernel_modules; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 158 | d = opendir(module_dir.c_str()); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 159 | auto deduped_modules = Skip_Loaded_Kernel_Modules(); |
| 160 | if (deduped_modules.size() == 0) { |
| 161 | LOGINFO("Requested modules are loaded\n"); |
| 162 | return false; |
| 163 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 164 | if (d != nullptr) { |
| 165 | while ((de = readdir(d)) != nullptr) { |
| 166 | std::string kernel_module = de->d_name; |
| 167 | if (de->d_type == DT_REG) { |
| 168 | if (android::base::EndsWith(kernel_module, ".ko")) { |
| 169 | for (auto&& requested:kernel_modules_requested) { |
| 170 | if (kernel_module == requested) { |
| 171 | kernel_modules.push_back(kernel_module); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 172 | continue; |
| 173 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 174 | } |
| 175 | continue; |
| 176 | } |
| 177 | } |
| 178 | } |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 179 | std::string module_file = module_dir + "/modules.load.twrp"; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 180 | TWFunc::write_to_file(module_file, kernel_modules); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 181 | closedir(d); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) { |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 187 | std::string ramdisk_dir = "/tmp" + module_dir; |
| 188 | DIR* d; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 189 | struct dirent* de; |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 190 | d = opendir(module_dir.c_str()); |
| 191 | if (d != nullptr) { |
| 192 | while ((de = readdir(d)) != nullptr) { |
| 193 | std::string kernel_module = de->d_name; |
| 194 | if (de->d_type == DT_REG) { |
| 195 | std::string src = module_dir + "/" + de->d_name; |
| 196 | std::string dest = ramdisk_dir + "/" + de->d_name; |
| 197 | if (TWFunc::copy_file(src, dest, 0700, false) != 0) { |
| 198 | return false; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | closedir(d); |
| 203 | } else { |
| 204 | LOGINFO("Unable to open module directory: %s. Skipping\n", module_dir.c_str()); |
| 205 | return false; |
| 206 | } |
| 207 | return true; |
| 208 | } |