-
Notifications
You must be signed in to change notification settings - Fork 18
/
ramdisk.cpp
312 lines (275 loc) · 9.65 KB
/
ramdisk.cpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <base.hpp>
#include "cpio.hpp"
#include "magiskboot.hpp"
#include "compress.hpp"
using namespace std;
static const char *UNSUPPORT_LIST[] =
{ "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc",
"boot/sbin/launch_daemonsu.sh" };
static const char *MAGISK_LIST[] =
{ ".backup/.magisk", "init.magisk.rc",
"overlay/init.magisk.rc" };
class magisk_cpio : public cpio {
public:
void patch();
int test();
char *sha1();
void restore();
void backup(const char *orig);
};
bool check_env(const char *name) {
const char *val = getenv(name);
return val != nullptr && val == "true"sv;
}
void magisk_cpio::patch() {
bool keepverity = check_env("KEEPVERITY");
bool keepforceencrypt = check_env("KEEPFORCEENCRYPT");
fprintf(stderr, "Patch with flag KEEPVERITY=[%s] KEEPFORCEENCRYPT=[%s]\n",
keepverity ? "true" : "false", keepforceencrypt ? "true" : "false");
for (auto it = entries.begin(); it != entries.end();) {
auto cur = it++;
bool fstab = (!keepverity || !keepforceencrypt) &&
S_ISREG(cur->second->mode) &&
!str_starts(cur->first, ".backup") &&
!str_contains(cur->first, "twrp") &&
!str_contains(cur->first, "recovery") &&
str_contains(cur->first, "fstab");
if (!keepverity) {
if (fstab) {
fprintf(stderr, "Found fstab file [%s]\n", cur->first.data());
cur->second->filesize = patch_verity(cur->second->data, cur->second->filesize);
} else if (cur->first == "verity_key") {
rm(cur);
continue;
}
}
if (!keepforceencrypt) {
if (fstab) {
cur->second->filesize = patch_encryption(cur->second->data, cur->second->filesize);
}
}
}
}
#define MAGISK_PATCHED (1 << 0)
#define UNSUPPORTED_CPIO (1 << 1)
#define SONY_INIT (1 << 2)
int magisk_cpio::test() {
int ret = 0;
for (auto file : UNSUPPORT_LIST) {
if (exists(file)) {
return UNSUPPORTED_CPIO;
}
}
for (auto file : MAGISK_LIST) {
if (exists(file)) {
ret |= MAGISK_PATCHED;
break;
}
}
if (exists("init.real"))
ret |= SONY_INIT;
return ret;
}
#define for_each_line(line, buf, size) \
for (char *line = (char *) buf; line < (char *) buf + size && line[0]; line = strchr(line + 1, '\n') + 1)
char *magisk_cpio::sha1() {
char sha1[41];
for (auto &e : entries) {
if (e.first == "init.magisk.rc" || e.first == "overlay/init.magisk.rc") {
for_each_line(line, e.second->data, e.second->filesize) {
if (strncmp(line, "#STOCKSHA1=", 11) == 0) {
strncpy(sha1, line + 12, 40);
sha1[40] = '\0';
return strdup(sha1);
}
}
} else if (e.first == ".backup/.magisk") {
for_each_line(line, e.second->data, e.second->filesize) {
if (str_starts(line, "SHA1=")) {
strncpy(sha1, line + 5, 40);
sha1[40] = '\0';
return strdup(sha1);
}
}
} else if (e.first == ".backup/.sha1") {
return (char *) e.second->data;
}
}
return nullptr;
}
#define for_each_str(str, buf, size) \
for (char *str = (char *) buf; str < (char *) buf + size; str += strlen(str) + 1)
void magisk_cpio::restore() {
// Collect files
auto bk = entries.end();
auto rl = entries.end();
auto mg = entries.end();
vector<entry_map::iterator> backups;
for (auto it = entries.begin(); it != entries.end(); ++it) {
if (it->first == ".backup") {
bk = it;
} else if (it->first == ".backup/.rmlist") {
rl = it;
} else if (it->first == ".backup/.magisk") {
mg = it;
} else if (str_starts(it->first, ".backup/")) {
backups.emplace_back(it);
}
}
// If the .backup folder is effectively empty, this means that the boot ramdisk was
// created from scratch by an old broken magiskboot. This is just a hacky workaround.
if (bk != entries.end() && mg != entries.end() && rl == entries.end() && backups.empty()) {
fprintf(stderr, "Remove all in ramdisk\n");
entries.clear();
return;
}
// Remove files
rm(bk);
rm(mg);
if (rl != entries.end()) {
for_each_str(file, rl->second->data, rl->second->filesize) {
rm(file);
}
rm(rl);
}
// Restore files
for (auto it : backups) {
const char *name = &it->first[8];
mv(it, name);
}
}
void magisk_cpio::backup(const char *orig) {
entry_map backups;
string rm_list;
backups.emplace(".backup", new cpio_entry(S_IFDIR));
magisk_cpio o;
if (access(orig, R_OK) == 0)
o.load_cpio(orig);
// Remove existing backups in original ramdisk
o.rm(".backup", true);
rm(".backup", true);
auto lhs = o.entries.begin();
auto rhs = entries.begin();
while (lhs != o.entries.end() || rhs != entries.end()) {
int res;
bool do_backup = false;
if (lhs != o.entries.end() && rhs != entries.end()) {
res = lhs->first.compare(rhs->first);
} else if (lhs == o.entries.end()) {
res = 1;
} else {
res = -1;
}
if (res < 0) {
// Something is missing in new ramdisk, do_backup!
do_backup = true;
fprintf(stderr, "Backup missing entry: ");
} else if (res == 0) {
if (lhs->second->filesize != rhs->second->filesize ||
memcmp(lhs->second->data, rhs->second->data, lhs->second->filesize) != 0) {
// Not the same!
do_backup = true;
fprintf(stderr, "Backup mismatch entry: ");
}
} else {
// Something new in ramdisk
rm_list += rhs->first;
rm_list += (char) '\0';
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->first.data());
}
if (do_backup) {
string name = ".backup/" + lhs->first;
fprintf(stderr, "[%s] -> [%s]\n", lhs->first.data(), name.data());
auto e = lhs->second.release();
backups.emplace(name, e);
}
// Increment positions
if (res < 0) {
++lhs;
} else if (res == 0) {
++lhs; ++rhs;
} else {
++rhs;
}
}
if (!rm_list.empty()) {
auto rm_list_file = new cpio_entry(S_IFREG);
rm_list_file->filesize = rm_list.length();
rm_list_file->data = xmalloc(rm_list.length());
memcpy(rm_list_file->data, rm_list.data(), rm_list.length());
backups.emplace(".backup/.rmlist", rm_list_file);
}
if (backups.size() > 1)
entries.merge(backups);
}
int cpio_commands(int argc, char *argv[]) {
magisk_cpio cpio;
/* pack doesn`t need incpio */
if (argc >= 3 && argv[0] == "pack"sv) {
bool c = argc == 5 && argv[1] == "-c"sv;
cpio.load_cpio(argv[1 + 2*c], c ? argv[2] : "cpio", false);
cpio.dump(argv[2 + 2*c]);
return 0;
}
char *incpio = argv[0];
++argv;
--argc;
if (access(incpio, R_OK) == 0)
cpio.load_cpio(incpio);
unsigned int cmdc;
char *cmdv[6];
for (int i = 0; i < argc; ++i) {
// Reset
cmdc = 0;
memset(cmdv, 0, sizeof(cmdv));
// Split the commands
char *tok = strtok(argv[i], " ");
while (tok && cmdc < std::size(cmdv)) {
if (cmdc == 0 && tok[0] == '#')
break;
cmdv[cmdc++] = tok;
tok = strtok(nullptr, " ");
}
if (cmdc == 0)
continue;
if (cmdv[0] == "test"sv) {
exit(cpio.test());
} else if (cmdv[0] == "restore"sv) {
cpio.restore();
} else if (cmdv[0] == "sha1"sv) {
char *sha1 = cpio.sha1();
if (sha1) printf("%s\n", sha1);
return 0;
} else if (cmdv[0] == "patch"sv) {
cpio.patch();
} else if (cmdc == 2 && cmdv[0] == "exists"sv) {
exit(!cpio.exists(cmdv[1]));
} else if (cmdc == 2 && cmdv[0] == "backup"sv) {
cpio.backup(cmdv[1]);
} else if (cmdc >= 2 && cmdv[0] == "rm"sv) {
bool r = cmdc > 2 && cmdv[1] == "-r"sv;
cpio.rm(cmdv[1 + r], r);
} else if (cmdc == 3 && cmdv[0] == "mv"sv) {
cpio.mv(cmdv[1], cmdv[2]);
} else if (cmdv[0] == "extract"sv) {
if (cmdc == 3) {
return !cpio.extract(cmdv[1], cmdv[2]);
} else {
cpio.extract();
return 0;
}
} else if (cmdv[0] == "sync"sv) {
cpio.load_cpio("ramdisk", "cpio", true);
} else if (cmdc == 3 && cmdv[0] == "mkdir"sv) {
cpio.mkdir(strtoul(cmdv[1], nullptr, 8), cmdv[2]);
} else if (cmdc == 3 && cmdv[0] == "ln"sv) {
cpio.ln(cmdv[1], cmdv[2]);
} else if (cmdc == 4 && cmdv[0] == "add"sv) {
cpio.add(strtoul(cmdv[1], nullptr, 8), cmdv[2], cmdv[3]);
} else {
return 1;
}
}
cpio.dump(incpio);
return 0;
}