2018-09-27 09:11:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2017-12-01 10:17:24 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2019-02-10 09:57:51 +01:00
|
|
|
#include <utils.h>
|
|
|
|
|
2017-12-01 10:17:24 +01:00
|
|
|
#include "magiskboot.h"
|
|
|
|
#include "cpio.h"
|
|
|
|
|
2019-01-20 05:59:37 +01:00
|
|
|
using namespace std;
|
|
|
|
|
2018-10-25 03:08:06 +02:00
|
|
|
class magisk_cpio : public cpio {
|
|
|
|
public:
|
2019-01-20 05:59:37 +01:00
|
|
|
explicit magisk_cpio(const char *filename) : cpio(filename) {}
|
2018-10-25 03:08:06 +02:00
|
|
|
void patch(bool keepverity, bool keepforceencrypt);
|
|
|
|
int test();
|
|
|
|
char * sha1();
|
|
|
|
void restore();
|
2018-11-15 06:33:20 +01:00
|
|
|
void backup(const char *orig);
|
2018-10-25 03:08:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) {
|
2017-12-28 21:25:03 +01:00
|
|
|
fprintf(stderr, "Patch with flag KEEPVERITY=[%s] KEEPFORCEENCRYPT=[%s]\n",
|
|
|
|
keepverity ? "true" : "false", keepforceencrypt ? "true" : "false");
|
2019-02-22 08:56:18 +01:00
|
|
|
for (auto &e : entries) {
|
|
|
|
if (!e) continue;
|
2018-11-07 08:10:38 +01:00
|
|
|
bool fstab = (!keepverity || !keepforceencrypt) &&
|
2019-01-20 05:59:37 +01:00
|
|
|
!str_starts(e->filename, ".backup") &&
|
|
|
|
str_contains(e->filename, "fstab") && S_ISREG(e->mode);
|
2017-12-01 10:17:24 +01:00
|
|
|
if (!keepverity) {
|
2018-11-07 08:10:38 +01:00
|
|
|
if (fstab) {
|
2017-12-20 20:36:18 +01:00
|
|
|
patch_verity(&e->data, &e->filesize, 1);
|
2018-11-07 08:10:38 +01:00
|
|
|
} else if (e->filename == "verity_key") {
|
2017-12-01 10:17:24 +01:00
|
|
|
fprintf(stderr, "Remove [verity_key]\n");
|
2019-02-22 08:56:18 +01:00
|
|
|
e.reset();
|
2018-01-10 13:23:01 +01:00
|
|
|
continue;
|
2017-12-01 10:17:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!keepforceencrypt) {
|
2018-11-07 08:10:38 +01:00
|
|
|
if (fstab) {
|
2017-12-20 20:36:18 +01:00
|
|
|
patch_encryption(&e->data, &e->filesize);
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:08:06 +02:00
|
|
|
|
2018-08-09 12:13:07 +02:00
|
|
|
#define STOCK_BOOT 0x0
|
|
|
|
#define MAGISK_PATCH 0x1
|
|
|
|
#define UNSUPPORT_PATCH 0x2
|
2018-10-25 03:08:06 +02:00
|
|
|
int magisk_cpio::test() {
|
|
|
|
static const char *UNSUPPORT_LIST[] = { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc",
|
2018-11-07 08:10:38 +01:00
|
|
|
"boot/sbin/launch_daemonsu.sh" };
|
2018-10-25 03:08:06 +02:00
|
|
|
static const char *MAGISK_LIST[] = { ".backup/.magisk", "init.magisk.rc",
|
2018-11-07 08:10:38 +01:00
|
|
|
"overlay/init.magisk.rc" };
|
2017-12-20 20:36:18 +01:00
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
for (auto file : UNSUPPORT_LIST)
|
|
|
|
if (find(file) >= 0)
|
2018-08-09 12:13:07 +02:00
|
|
|
return UNSUPPORT_PATCH;
|
2018-03-10 08:55:55 +01:00
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
for (auto file : MAGISK_LIST)
|
|
|
|
if (find(file) >= 0)
|
2017-12-20 20:36:18 +01:00
|
|
|
return MAGISK_PATCH;
|
|
|
|
|
|
|
|
return STOCK_BOOT;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 06:33:20 +01:00
|
|
|
#define for_each_line(line, buf, size) \
|
|
|
|
for (line = (char *) buf; line < (char *) buf + size && line[0]; line = strchr(line + 1, '\n') + 1)
|
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
char *magisk_cpio::sha1() {
|
2017-12-06 18:30:48 +01:00
|
|
|
char sha1[41];
|
2018-11-15 06:33:20 +01:00
|
|
|
char *line;
|
2019-02-22 08:56:18 +01:00
|
|
|
for (auto &e : entries) {
|
2017-12-20 20:36:18 +01:00
|
|
|
if (!e) continue;
|
2018-11-07 08:10:38 +01:00
|
|
|
if (e->filename == "init.magisk.rc" || e->filename == "overlay/init.magisk.rc") {
|
2018-11-15 06:33:20 +01:00
|
|
|
for_each_line(line, e->data, e->filesize) {
|
|
|
|
if (strncmp(line, "#STOCKSHA1=", 11) == 0) {
|
|
|
|
strncpy(sha1, line + 12, 40);
|
|
|
|
sha1[40] = '\0';
|
|
|
|
return strdup(sha1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (e->filename == ".backup/.magisk") {
|
|
|
|
for_each_line(line, e->data, e->filesize) {
|
|
|
|
if (strncmp(line, "SHA1=", 5) == 0) {
|
|
|
|
strncpy(sha1, line + 5, 40);
|
2017-12-06 18:30:48 +01:00
|
|
|
sha1[40] = '\0';
|
|
|
|
return strdup(sha1);
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 08:10:38 +01:00
|
|
|
} else if (e->filename == ".backup/.sha1") {
|
2018-10-25 03:08:06 +02:00
|
|
|
return (char *) e->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void magisk_cpio::restore() {
|
2019-02-22 08:56:18 +01:00
|
|
|
for (auto &e : entries) {
|
2018-10-25 03:08:06 +02:00
|
|
|
if (!e) continue;
|
2019-01-20 05:59:37 +01:00
|
|
|
if (str_starts(e->filename, ".backup")) {
|
2018-10-25 03:08:06 +02:00
|
|
|
if (e->filename[7] == '\0') continue;
|
|
|
|
if (e->filename[8] == '.') {
|
2018-11-07 08:10:38 +01:00
|
|
|
if (strcmp(&e->filename[8], ".rmlist") == 0) {
|
2018-10-25 03:08:06 +02:00
|
|
|
for (int pos = 0; pos < e->filesize; pos += strlen((char *) e->data + pos) + 1)
|
2018-11-07 08:10:38 +01:00
|
|
|
rm((char *) e->data + pos, false);
|
2018-10-25 03:08:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-20 05:59:37 +01:00
|
|
|
mv(e->filename.c_str(), &e->filename[8]);
|
2018-10-25 03:08:06 +02:00
|
|
|
}
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-25 03:08:06 +02:00
|
|
|
|
|
|
|
// Some known stuff we can remove
|
2018-11-07 08:10:38 +01:00
|
|
|
rm(".backup", true);
|
|
|
|
rm("overlay", true);
|
|
|
|
rm("sbin/magic_mask.sh", false);
|
|
|
|
rm("init.magisk.rc", false);
|
|
|
|
rm("magisk", true);
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 08:56:18 +01:00
|
|
|
#define lhs o.entries[i]
|
|
|
|
#define rhs entries[j]
|
2018-11-15 06:33:20 +01:00
|
|
|
void magisk_cpio::backup(const char *orig) {
|
2019-02-22 08:56:18 +01:00
|
|
|
vector<unique_ptr<cpio_entry> > bkup_entries;
|
|
|
|
string remv;
|
2017-12-06 18:30:48 +01:00
|
|
|
|
2019-02-22 08:56:18 +01:00
|
|
|
bkup_entries.emplace_back(new cpio_entry(".backup"));
|
|
|
|
bkup_entries.back()->mode = S_IFDIR;
|
2017-12-06 18:30:48 +01:00
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
magisk_cpio o(orig);
|
2018-10-25 03:08:06 +02:00
|
|
|
|
2017-12-06 18:30:48 +01:00
|
|
|
// Remove possible backups in original ramdisk
|
2018-11-07 08:10:38 +01:00
|
|
|
o.rm(".backup", true);
|
|
|
|
rm(".backup", true);
|
2017-12-06 18:30:48 +01:00
|
|
|
|
2018-10-25 03:08:06 +02:00
|
|
|
// Sort both CPIOs before comparing
|
|
|
|
o.sort();
|
|
|
|
sort();
|
2017-12-06 18:30:48 +01:00
|
|
|
|
|
|
|
// Start comparing
|
|
|
|
size_t i = 0, j = 0;
|
2019-02-22 08:56:18 +01:00
|
|
|
while(i != o.entries.size() || j != entries.size()) {
|
2018-11-07 08:10:38 +01:00
|
|
|
int res;
|
|
|
|
bool backup = false;
|
2019-02-22 08:56:18 +01:00
|
|
|
if (i != o.entries.size() && j != entries.size()) {
|
|
|
|
res = lhs->filename.compare(rhs->filename);
|
|
|
|
} else if (i == o.entries.size()) {
|
2017-12-06 18:30:48 +01:00
|
|
|
res = 1;
|
2019-02-22 08:56:18 +01:00
|
|
|
} else if (j == entries.size()) {
|
2017-12-06 18:30:48 +01:00
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
// Something is missing in new ramdisk, backup!
|
2018-10-25 03:08:06 +02:00
|
|
|
backup = true;
|
2017-12-06 18:30:48 +01:00
|
|
|
fprintf(stderr, "Backup missing entry: ");
|
|
|
|
} else if (res == 0) {
|
2019-02-22 08:56:18 +01:00
|
|
|
if (memcmp(lhs->data, rhs->data, lhs->filesize) != 0) {
|
|
|
|
// Not the same!
|
|
|
|
backup = true;
|
|
|
|
fprintf(stderr, "Backup mismatch entry: ");
|
|
|
|
}
|
2017-12-06 18:30:48 +01:00
|
|
|
} else {
|
2019-02-22 08:56:18 +01:00
|
|
|
// Something new in ramdisk
|
|
|
|
remv += rhs->filename;
|
|
|
|
remv += (char) '\0';
|
|
|
|
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->filename.c_str());
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
if (backup) {
|
2019-02-22 08:56:18 +01:00
|
|
|
string back_name = ".backup/" + lhs->filename;
|
|
|
|
fprintf(stderr, "[%s] -> [%s]\n", lhs->filename.c_str(), back_name.c_str());
|
|
|
|
lhs->filename = back_name;
|
|
|
|
bkup_entries.push_back(std::move(lhs));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment positions
|
|
|
|
if (res < 0) {
|
|
|
|
++i;
|
|
|
|
} else if (res == 0) {
|
|
|
|
++i; ++j;
|
|
|
|
} else {
|
|
|
|
++j;
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 08:56:18 +01:00
|
|
|
if (!remv.empty()) {
|
|
|
|
auto rmlist = make_unique<cpio_entry>(".backup/.rmlist");
|
|
|
|
rmlist->mode = S_IFREG;
|
|
|
|
rmlist->filesize = remv.length();
|
|
|
|
rmlist->data = xmalloc(remv.length());
|
|
|
|
memcpy(rmlist->data, remv.data(), remv.length());
|
|
|
|
bkup_entries.push_back(std::move(rmlist));
|
|
|
|
}
|
2018-11-15 06:33:20 +01:00
|
|
|
|
2019-02-22 08:56:18 +01:00
|
|
|
if (bkup_entries.size() > 1) {
|
|
|
|
entries.insert(entries.end(),
|
|
|
|
make_move_iterator(bkup_entries.begin()), make_move_iterator(bkup_entries.end()));
|
|
|
|
}
|
2017-12-06 18:30:48 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 05:51:16 +01:00
|
|
|
|
2017-12-20 20:36:18 +01:00
|
|
|
int cpio_commands(int argc, char *argv[]) {
|
2017-12-01 10:17:24 +01:00
|
|
|
char *incpio = argv[0];
|
|
|
|
++argv;
|
|
|
|
--argc;
|
2017-12-20 20:36:18 +01:00
|
|
|
|
2018-11-07 08:10:38 +01:00
|
|
|
magisk_cpio cpio(incpio);
|
2017-12-03 20:14:04 +01:00
|
|
|
|
2017-12-20 20:36:18 +01:00
|
|
|
int cmdc;
|
|
|
|
char *cmdv[6];
|
2017-12-06 05:51:16 +01:00
|
|
|
|
2017-12-20 20:36:18 +01:00
|
|
|
while (argc) {
|
2018-10-26 23:02:07 +02:00
|
|
|
// Clean up
|
2017-12-20 20:36:18 +01:00
|
|
|
cmdc = 0;
|
2018-10-26 23:02:07 +02:00
|
|
|
memset(cmdv, NULL, sizeof(cmdv));
|
|
|
|
|
|
|
|
// Split the commands
|
|
|
|
for (char *tok = strtok(argv[0], " "); tok; tok = strtok(nullptr, " "))
|
2017-12-20 20:36:18 +01:00
|
|
|
cmdv[cmdc++] = tok;
|
2017-12-06 05:51:16 +01:00
|
|
|
|
2018-10-26 23:02:07 +02:00
|
|
|
if (cmdc == 0)
|
|
|
|
continue;
|
|
|
|
|
2017-12-20 20:36:18 +01:00
|
|
|
if (strcmp(cmdv[0], "test") == 0) {
|
2018-10-25 03:08:06 +02:00
|
|
|
exit(cpio.test());
|
2017-12-20 20:36:18 +01:00
|
|
|
} else if (strcmp(cmdv[0], "restore") == 0) {
|
2018-10-25 03:08:06 +02:00
|
|
|
cpio.restore();
|
2017-12-20 20:36:18 +01:00
|
|
|
} else if (strcmp(cmdv[0], "sha1") == 0) {
|
2018-10-25 03:08:06 +02:00
|
|
|
char *sha1 = cpio.sha1();
|
2018-11-15 06:33:20 +01:00
|
|
|
if (sha1) printf("%s\n", sha1);
|
2017-12-20 20:36:18 +01:00
|
|
|
return 0;
|
2018-12-06 02:27:48 +01:00
|
|
|
} else if (cmdc == 2 && strcmp(cmdv[0], "exists") == 0) {
|
|
|
|
exit(cpio.find(cmdv[1]) < 0);
|
|
|
|
} else if (cmdc == 2 && strcmp(cmdv[0], "backup") == 0) {
|
2018-11-15 06:33:20 +01:00
|
|
|
cpio.backup(cmdv[1]);
|
2017-12-20 20:36:18 +01:00
|
|
|
} else if (cmdc >= 2 && strcmp(cmdv[0], "rm") == 0) {
|
2018-11-07 08:10:38 +01:00
|
|
|
bool r = cmdc > 2 && strcmp(cmdv[1], "-r") == 0;
|
|
|
|
cpio.rm(cmdv[1 + r], r);
|
2017-12-20 20:36:18 +01:00
|
|
|
} else if (cmdc == 3 && strcmp(cmdv[0], "mv") == 0) {
|
2018-10-25 03:08:06 +02:00
|
|
|
cpio.mv(cmdv[1], cmdv[2]);
|
2017-12-20 20:36:18 +01:00
|
|
|
} else if (cmdc == 3 && strcmp(cmdv[0], "patch") == 0) {
|
2018-10-25 03:08:06 +02:00
|
|
|
cpio.patch(strcmp(cmdv[1], "true") == 0, strcmp(cmdv[2], "true") == 0);
|
2017-12-20 20:36:18 +01:00
|
|
|
} else if (strcmp(cmdv[0], "extract") == 0) {
|
|
|
|
if (cmdc == 3) {
|
2018-11-15 06:33:20 +01:00
|
|
|
return !cpio.extract(cmdv[1], cmdv[2]);
|
2017-12-20 20:36:18 +01:00
|
|
|
} else {
|
2018-10-25 03:08:06 +02:00
|
|
|
cpio.extract();
|
2017-12-20 20:36:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (cmdc == 3 && strcmp(cmdv[0], "mkdir") == 0) {
|
2019-01-20 05:59:37 +01:00
|
|
|
cpio.makedir(strtoul(cmdv[1], nullptr, 8), cmdv[2]);
|
2017-12-20 20:36:18 +01:00
|
|
|
} else if (cmdc == 3 && strcmp(cmdv[0], "ln") == 0) {
|
2018-10-25 03:08:06 +02:00
|
|
|
cpio.ln(cmdv[1], cmdv[2]);
|
2017-12-20 20:36:18 +01:00
|
|
|
} else if (cmdc == 4 && strcmp(cmdv[0], "add") == 0) {
|
2019-01-20 05:59:37 +01:00
|
|
|
cpio.add(strtoul(cmdv[1], nullptr, 8), cmdv[2], cmdv[3]);
|
2017-12-04 20:32:37 +01:00
|
|
|
} else {
|
2017-12-20 20:36:18 +01:00
|
|
|
return 1;
|
2017-12-04 20:32:37 +01:00
|
|
|
}
|
2017-12-20 20:36:18 +01:00
|
|
|
|
|
|
|
--argc;
|
|
|
|
++argv;
|
2017-12-01 10:17:24 +01:00
|
|
|
}
|
2017-12-03 20:14:04 +01:00
|
|
|
|
2018-10-25 03:08:06 +02:00
|
|
|
cpio.dump(incpio);
|
2017-12-20 20:36:18 +01:00
|
|
|
return 0;
|
2018-10-25 03:08:06 +02:00
|
|
|
}
|