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"); |
| 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 | |
| 88 | std::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"); |
| 94 | LOGINFO("number of modules loaded by init: %lu\n", loaded_modules.size()); |
| 95 | 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; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool KernelModuleLoader::Write_Module_List(std::string module_dir) { |
| 110 | DIR* d; |
| 111 | struct dirent* de; |
| 112 | std::vector<std::string> kernel_modules; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 113 | d = opendir(module_dir.c_str()); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 114 | auto deduped_modules = Skip_Loaded_Kernel_Modules(); |
| 115 | if (deduped_modules.size() == 0) { |
| 116 | LOGINFO("Requested modules are loaded\n"); |
| 117 | return false; |
| 118 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 119 | 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); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 127 | continue; |
| 128 | } |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 129 | } |
| 130 | continue; |
| 131 | } |
| 132 | } |
| 133 | } |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 134 | std::string module_file = module_dir + "/modules.load.twrp"; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 135 | TWFunc::write_to_file(module_file, kernel_modules); |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 136 | closedir(d); |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) { |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 142 | std::string ramdisk_dir = "/tmp" + module_dir; |
| 143 | DIR* d; |
bigbiff | 22851b9 | 2021-09-01 16:46:57 -0400 | [diff] [blame] | 144 | struct dirent* de; |
bigbiff | 850fa28 | 2021-10-09 12:37:29 -0400 | [diff] [blame] | 145 | 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 | } |