forked from TWRP-Test/android_bootable_recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_module_loader.cpp
More file actions
272 lines (237 loc) · 8.66 KB
/
Copy pathkernel_module_loader.cpp
File metadata and controls
272 lines (237 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "kernel_module_loader.hpp"
#include "common.h"
#include "variables.h"
#ifdef TW_INCLUDE_CRYPTO
#include <android-base/file.h>
#include <android-base/strings.h>
#include <sys/mount.h>
static void ResetProp(const std::string& key, const std::string& value) {
if (value.empty())
return;
std::string cmd = "/system/bin/resetprop " + key + " " + value;
TWFunc::Exec_Cmd(cmd);
}
#endif
const std::vector<std::string> kernel_modules_requested = TWFunc::split_string(EXPAND(TW_LOAD_VENDOR_MODULES), ' ', true);
BOOT_MODE KernelModuleLoader::Get_Boot_Mode() {
std::string cmdline;
std::string bootconfig;
android::base::ReadFileToString("/proc/bootconfig", &bootconfig);
android::base::ReadFileToString("/proc/cmdline", &cmdline);
if (cmdline.find("twrpfastboot=1") != std::string::npos && (bootconfig.find("androidboot.force_normal_boot = \"1\"") != std::string::npos ||
cmdline.find("androidboot.force_normal_boot=1") != std::string::npos))
return RECOVERY_FASTBOOT_MODE;
else if (android::base::GetProperty(TW_FASTBOOT_MODE_PROP, "0") == "1")
return FASTBOOTD_MODE;
return RECOVERY_IN_BOOT_MODE;
}
bool KernelModuleLoader::Load_Vendor_Modules() {
// check /lib/modules (ramdisk vendor_boot)
// check /lib/modules/N.N (ramdisk vendor_boot)
// check /lib/modules/N.N-gki (ramdisk vendor_boot)
// check /vendor/lib/modules (ramdisk)
// check /vendor/lib/modules/1.1 (ramdisk prebuilt modules)
// check /vendor/lib/modules/N.N (vendor mounted)
// check /vendor/lib/modules/N.N-gki (vendor mounted)
// check /vendor_dlkm/lib/modules (vendor_dlkm mounted)
if (android::base::GetBoolProperty(TW_MODULES_MOUNTED_PROP, false)) return true;
int modules_loaded = 0;
#ifdef TW_INCLUDE_CRYPTO
std::string vendor_patch;
std::string system_patch;
TWPartition* sysroot = PartitionManager.Find_Partition_By_Path("/system_root");
#endif
LOGINFO("Attempting to load modules\n");
std::string vendor_base_dir(VENDOR_MODULE_DIR);
std::string base_dir(VENDOR_BOOT_MODULE_DIR);
std::string vendor_dlkm_base_dir(VENDOR_DLKM_MODULE_DIR);
std::vector<std::string> module_dirs;
std::vector<std::string> vendor_module_dirs;
TWPartition* ven = PartitionManager.Find_Partition_By_Path("/vendor");
TWPartition* ven_dlkm = PartitionManager.Find_Partition_By_Path("/vendor_dlkm");
vendor_module_dirs.push_back(VENDOR_MODULE_DIR);
vendor_module_dirs.push_back(vendor_base_dir + "/1.1");
module_dirs.push_back(base_dir);
struct utsname uts;
if (uname(&uts)) {
LOGERR("Unable to query kernel for version info\n");
}
std::string rls(uts.release);
std::vector<std::string> release = TWFunc::split_string(rls, '.', true);
int expected_module_count = kernel_modules_requested.size();
module_dirs.push_back(base_dir + "/" + release[0] + "." + release[1]);
#ifndef TW_LOAD_VENDOR_MODULES_EXCLUDE_GKI
std::string gki = "/" + release[0] + "." + release[1] + "-gki";
module_dirs.push_back(base_dir + gki);
vendor_module_dirs.push_back(vendor_base_dir + gki);
#endif
switch(Get_Boot_Mode()) {
case RECOVERY_FASTBOOT_MODE:
/* On bootmode: once, there is not always stock kernel
* so try only with twrp prebuilt modules.
*/
for (auto&& module_dir:vendor_module_dirs) {
modules_loaded += Try_And_Load_Modules(module_dir, false);
if (modules_loaded >= expected_module_count) goto exit;
}
break;
case FASTBOOTD_MODE:
case RECOVERY_IN_BOOT_MODE:
#ifdef TW_LOAD_VENDOR_BOOT_MODULES
for (auto&& module_dir:module_dirs) {
modules_loaded += Try_And_Load_Modules(module_dir, false);
if (modules_loaded >= expected_module_count) goto exit;
}
#endif
/* In both mode vendor_boot or vendor modules are used
* Because Ramdisk is flashed in both.
*/
break;
}
#ifdef TW_LOAD_PREBUILT_MODULES_AT_FIRST
for (auto&& module_dir:vendor_module_dirs) {
modules_loaded += Try_And_Load_Modules(module_dir, true);
if (modules_loaded >= expected_module_count) goto exit;
}
#endif
if (ven) {
LOGINFO("Checking mounted /vendor\n");
ven->Mount(true);
}
#ifdef TW_INCLUDE_CRYPTO
LOGINFO("Beginning override security patch...\n");
vendor_patch = TWFunc::Partition_Property_Get("ro.vendor.build.security_patch", PartitionManager, "/vendor", "build.prop");
if (!vendor_patch.empty()) {
ResetProp("ro.vendor.build.security_patch", vendor_patch);
}
if (sysroot) {
sysroot->Mount(true);
} else {
TWFunc::Recursive_Mkdir("/system_root");
mount("/system", "/system_root", "", MS_BIND, NULL);
}
system_patch = TWFunc::Partition_Property_Get("ro.build.version.security_patch", PartitionManager, "/system_root", "build.prop");
if (!system_patch.empty()) {
ResetProp("ro.build.version.security_patch", system_patch);
}
LOGINFO("Ending override security patch...\n");
#endif
if (ven_dlkm) {
LOGINFO("Checking mounted /vendor_dlkm\n");
ven_dlkm->Mount(true);
}
for (auto&& module_dir:vendor_module_dirs) {
modules_loaded += Try_And_Load_Modules(module_dir, true);
if (modules_loaded >= expected_module_count) goto exit;
}
modules_loaded += Try_And_Load_Modules(vendor_dlkm_base_dir, true);
if (modules_loaded >= expected_module_count) goto exit;
exit:
if (ven)
ven->UnMount(false);
if (ven_dlkm)
ven_dlkm->UnMount(false, MNT_DETACH);
#ifdef TW_INCLUDE_CRYPTO
if (sysroot)
sysroot->UnMount(false);
#endif
android::base::SetProperty(TW_MODULES_MOUNTED_PROP, "true");
return true;
}
int KernelModuleLoader::Try_And_Load_Modules(std::string module_dir, bool vendor_is_mounted) {
LOGINFO("Checking directory: %s\n", module_dir.c_str());
int modules_loaded = 0;
std::string dest_module_dir;
dest_module_dir = "/tmp" + module_dir;
TWFunc::Recursive_Mkdir(dest_module_dir);
Copy_Modules_To_Tmpfs(module_dir);
if (!Write_Module_List(dest_module_dir))
return kernel_modules_requested.size();
if (!vendor_is_mounted && module_dir == "/vendor/lib/modules") {
module_dir = "/lib/modules";
}
LOGINFO("mounting %s on %s\n", dest_module_dir.c_str(), module_dir.c_str());
if (mount(dest_module_dir.c_str(), module_dir.c_str(), "", MS_BIND, NULL) == 0) {
Modprobe m({module_dir}, "modules.load.twrp", false);
m.LoadListedModules(false);
modules_loaded = m.GetModuleCount();
PartitionManager.UnMount_By_Path(module_dir.c_str(), false, MNT_DETACH);
LOGINFO("Modules Loaded: %d\n", modules_loaded);
}
LOGINFO("Loaded %d modules from %s\n", modules_loaded, module_dir.c_str());
return modules_loaded;
}
std::vector<string> KernelModuleLoader::Skip_Loaded_Kernel_Modules() {
std::vector<string> kernel_modules = kernel_modules_requested;
std::vector<string> loaded_modules;
std::string kernel_module_file = "/proc/modules";
if (TWFunc::read_file(kernel_module_file, loaded_modules) < 0)
LOGINFO("failed to get loaded kernel modules\n");
LOGINFO("number of modules loaded by init: %zu\n", loaded_modules.size());
if (loaded_modules.size() == 0)
return kernel_modules;
for (auto&& module_line:loaded_modules) {
auto module = TWFunc::Split_String(module_line, " ")[0];
std::string full_module_name = module + ".ko";
auto found = std::find(kernel_modules.begin(), kernel_modules.end(), full_module_name);
if (found != kernel_modules.end()) {
LOGINFO("found module to dedupe: %s\n", (*found).c_str());
kernel_modules.erase(found);
}
}
return kernel_modules;
}
bool KernelModuleLoader::Write_Module_List(std::string module_dir) {
DIR* d;
struct dirent* de;
std::vector<std::string> kernel_modules;
d = opendir(module_dir.c_str());
auto deduped_modules = Skip_Loaded_Kernel_Modules();
if (deduped_modules.size() == 0) {
LOGINFO("Requested modules are loaded\n");
return false;
}
if (d != nullptr) {
while ((de = readdir(d)) != nullptr) {
std::string kernel_module = de->d_name;
if (de->d_type == DT_REG) {
if (android::base::EndsWith(kernel_module, ".ko")) {
for (auto&& requested:kernel_modules_requested) {
if (kernel_module == requested) {
kernel_modules.push_back(kernel_module);
continue;
}
}
continue;
}
}
}
std::string module_file = module_dir + "/modules.load.twrp";
TWFunc::write_to_file(module_file, kernel_modules);
closedir(d);
}
return true;
}
bool KernelModuleLoader::Copy_Modules_To_Tmpfs(std::string module_dir) {
std::string ramdisk_dir = "/tmp" + module_dir;
DIR* d;
struct dirent* de;
d = opendir(module_dir.c_str());
if (d != nullptr) {
while ((de = readdir(d)) != nullptr) {
std::string kernel_module = de->d_name;
if (de->d_type == DT_REG) {
std::string src = module_dir + "/" + de->d_name;
std::string dest = ramdisk_dir + "/" + de->d_name;
if (TWFunc::copy_file(src, dest, 0700, false) != 0) {
return false;
}
}
}
closedir(d);
} else {
LOGINFO("Unable to open module directory: %s. Skipping\n", module_dir.c_str());
return false;
}
return true;
}