bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 1 | #include "kernel_module_loader.hpp" |
| 2 | |
bigbiff | e3aa02e | 2021-10-01 13:07:38 -0400 | [diff] [blame] | 3 | const std::vector<std::string> kernel_modules_requested = TWFunc::split_string(EXPAND(TW_LOAD_VENDOR_MODULES), ' ', true); |
| 4 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 5 | bool 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; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 14 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 15 | 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; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 20 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 21 | 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"); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 24 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 25 | module_dirs.push_back(base_dir); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 26 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 27 | struct utsname uts; |
| 28 | if (uname(&uts)) { |
| 29 | LOGERR("Unable to query kernel for version info\n"); |
| 30 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 31 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 32 | 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]); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 36 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 37 | 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 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 41 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 42 | 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 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 46 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 47 | if (ven) { |
| 48 | LOGINFO("Checking mounted /vendor\n"); |
| 49 | ven->Mount(true); |
| 50 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 51 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 52 | 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 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 56 | |
bigbiff | e3aa02e | 2021-10-01 13:07:38 -0400 | [diff] [blame] | 57 | exit: |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 58 | if (ven) |
| 59 | ven->UnMount(false); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 64 | int 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"); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 79 | 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 | |
| 87 | std::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"); |
Magendanz | 84164b9 | 2021-12-12 14:53:41 -0800 | [diff] [blame] | 93 | LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size()); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 94 | 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; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool KernelModuleLoader::Write_Module_List(std::string module_dir) { |
| 109 | DIR* d; |
| 110 | struct dirent* de; |
| 111 | std::vector<std::string> kernel_modules; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 112 | d = opendir(module_dir.c_str()); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 113 | auto deduped_modules = Skip_Loaded_Kernel_Modules(); |
| 114 | if (deduped_modules.size() == 0) { |
| 115 | LOGINFO("Requested modules are loaded\n"); |
| 116 | return false; |
| 117 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 118 | 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); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 126 | continue; |
| 127 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 128 | } |
| 129 | continue; |
| 130 | } |
| 131 | } |
| 132 | } |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 133 | std::string module_file = module_dir + "/modules.load.twrp"; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 134 | TWFunc::write_to_file(module_file, kernel_modules); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 135 | closedir(d); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) { |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 141 | std::string ramdisk_dir = "/tmp" + module_dir; |
| 142 | DIR* d; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 143 | struct dirent* de; |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 144 | 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 | } |