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