2022-05-12 02:03:42 -07:00
|
|
|
#include <base.hpp>
|
2019-02-10 03:57:51 -05:00
|
|
|
|
2021-08-13 04:53:11 -07:00
|
|
|
#include "cpio.hpp"
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "magiskboot.hpp"
|
|
|
|
#include "compress.hpp"
|
2017-12-01 17:17:24 +08:00
|
|
|
|
2019-01-19 23:59:37 -05:00
|
|
|
using namespace std;
|
|
|
|
|
2019-02-23 15:04:15 -05:00
|
|
|
static const char *UNSUPPORT_LIST[] =
|
2020-12-30 22:11:24 -08:00
|
|
|
{ "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc",
|
|
|
|
"boot/sbin/launch_daemonsu.sh" };
|
2019-02-23 15:04:15 -05:00
|
|
|
|
|
|
|
static const char *MAGISK_LIST[] =
|
2020-12-30 22:11:24 -08:00
|
|
|
{ ".backup/.magisk", "init.magisk.rc",
|
|
|
|
"overlay/init.magisk.rc" };
|
2019-02-23 15:04:15 -05:00
|
|
|
|
2021-08-13 04:53:11 -07:00
|
|
|
class magisk_cpio : public cpio {
|
2018-10-24 21:08:06 -04:00
|
|
|
public:
|
2020-12-30 22:11:24 -08:00
|
|
|
void patch();
|
|
|
|
int test();
|
|
|
|
char *sha1();
|
|
|
|
void restore();
|
|
|
|
void backup(const char *orig);
|
2018-10-24 21:08:06 -04:00
|
|
|
};
|
|
|
|
|
2019-09-22 04:17:15 -04:00
|
|
|
bool check_env(const char *name) {
|
2020-12-30 22:11:24 -08:00
|
|
|
const char *val = getenv(name);
|
2021-05-10 18:38:30 -07:00
|
|
|
return val != nullptr && val == "true"sv;
|
2019-09-21 05:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void magisk_cpio::patch() {
|
2020-12-30 22:11:24 -08:00
|
|
|
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) &&
|
2021-01-14 05:59:53 -08:00
|
|
|
!str_starts(cur->first, ".backup") &&
|
|
|
|
!str_contains(cur->first, "twrp") &&
|
2020-12-30 22:11:24 -08:00
|
|
|
!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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2019-09-22 04:17:15 -04:00
|
|
|
#define MAGISK_PATCHED (1 << 0)
|
|
|
|
#define UNSUPPORTED_CPIO (1 << 1)
|
2021-10-30 22:59:20 -03:00
|
|
|
#define SONY_INIT (1 << 2)
|
2019-02-24 20:39:01 -05:00
|
|
|
|
2019-09-22 04:17:15 -04:00
|
|
|
int magisk_cpio::test() {
|
2021-10-30 22:59:20 -03:00
|
|
|
int ret = 0;
|
2021-08-13 03:30:58 -07:00
|
|
|
for (auto file : UNSUPPORT_LIST) {
|
|
|
|
if (exists(file)) {
|
2020-12-30 22:11:24 -08:00
|
|
|
return UNSUPPORTED_CPIO;
|
2021-08-13 03:30:58 -07:00
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
for (auto file : MAGISK_LIST) {
|
|
|
|
if (exists(file)) {
|
2021-10-30 22:59:20 -03:00
|
|
|
ret |= MAGISK_PATCHED;
|
|
|
|
break;
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
}
|
2021-10-30 22:59:20 -03:00
|
|
|
if (exists("init.real"))
|
|
|
|
ret |= SONY_INIT;
|
|
|
|
return ret;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2018-11-15 00:33:20 -05:00
|
|
|
#define for_each_line(line, buf, size) \
|
2022-06-08 23:23:39 -07:00
|
|
|
for (char *line = (char *) buf; line < (char *) buf + size && line[0]; line = strchr(line + 1, '\n') + 1)
|
2018-11-15 00:33:20 -05:00
|
|
|
|
2018-11-07 02:10:38 -05:00
|
|
|
char *magisk_cpio::sha1() {
|
2020-12-30 22:11:24 -08:00
|
|
|
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) {
|
2021-08-13 04:53:11 -07:00
|
|
|
if (str_starts(line, "SHA1=")) {
|
2020-12-30 22:11:24 -08:00
|
|
|
strncpy(sha1, line + 5, 40);
|
|
|
|
sha1[40] = '\0';
|
|
|
|
return strdup(sha1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (e.first == ".backup/.sha1") {
|
|
|
|
return (char *) e.second->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2018-10-24 21:08:06 -04:00
|
|
|
}
|
|
|
|
|
2019-02-22 22:53:20 -05:00
|
|
|
#define for_each_str(str, buf, size) \
|
2022-06-08 23:23:39 -07:00
|
|
|
for (char *str = (char *) buf; str < (char *) buf + size; str += strlen(str) + 1)
|
2019-02-22 22:53:20 -05:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
void magisk_cpio::restore() {
|
2022-06-08 23:23:39 -07:00
|
|
|
// 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()) {
|
2022-06-08 11:50:23 +08:00
|
|
|
fprintf(stderr, "Remove all in ramdisk\n");
|
|
|
|
entries.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:23:39 -07:00
|
|
|
// Remove files
|
|
|
|
rm(bk);
|
|
|
|
rm(mg);
|
|
|
|
if (rl != entries.end()) {
|
|
|
|
for_each_str(file, rl->second->data, rl->second->filesize) {
|
2021-08-13 04:53:11 -07:00
|
|
|
rm(file);
|
|
|
|
}
|
2022-06-08 23:23:39 -07:00
|
|
|
rm(rl);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:23:39 -07:00
|
|
|
// Restore files
|
|
|
|
for (auto it : backups) {
|
|
|
|
const char *name = &it->first[8];
|
|
|
|
mv(it, name);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2018-11-15 00:33:20 -05:00
|
|
|
void magisk_cpio::backup(const char *orig) {
|
2021-08-13 04:53:11 -07:00
|
|
|
entry_map backups;
|
|
|
|
string rm_list;
|
|
|
|
backups.emplace(".backup", new cpio_entry(S_IFDIR));
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2021-08-13 04:53:11 -07:00
|
|
|
magisk_cpio o;
|
2022-06-08 03:58:15 -07:00
|
|
|
if (access(orig, R_OK) == 0)
|
|
|
|
o.load_cpio(orig);
|
2020-12-30 22:11:24 -08:00
|
|
|
|
2021-08-13 04:53:11 -07:00
|
|
|
// Remove existing backups in original ramdisk
|
2020-12-30 22:11:24 -08:00
|
|
|
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;
|
2021-08-13 04:53:11 -07:00
|
|
|
bool do_backup = false;
|
2020-12-30 22:11:24 -08:00
|
|
|
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) {
|
2021-08-13 04:53:11 -07:00
|
|
|
// Something is missing in new ramdisk, do_backup!
|
|
|
|
do_backup = true;
|
2020-12-30 22:11:24 -08:00
|
|
|
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!
|
2021-08-13 04:53:11 -07:00
|
|
|
do_backup = true;
|
2020-12-30 22:11:24 -08:00
|
|
|
fprintf(stderr, "Backup mismatch entry: ");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Something new in ramdisk
|
2021-08-13 04:53:11 -07:00
|
|
|
rm_list += rhs->first;
|
|
|
|
rm_list += (char) '\0';
|
2020-12-30 22:11:24 -08:00
|
|
|
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->first.data());
|
|
|
|
}
|
2021-08-13 04:53:11 -07:00
|
|
|
|
|
|
|
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);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment positions
|
|
|
|
if (res < 0) {
|
|
|
|
++lhs;
|
|
|
|
} else if (res == 0) {
|
|
|
|
++lhs; ++rhs;
|
|
|
|
} else {
|
|
|
|
++rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 04:53:11 -07:00
|
|
|
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);
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
|
|
|
|
2021-08-13 04:53:11 -07:00
|
|
|
if (backups.size() > 1)
|
|
|
|
entries.merge(backups);
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 03:36:18 +08:00
|
|
|
int cpio_commands(int argc, char *argv[]) {
|
2020-12-30 22:11:24 -08:00
|
|
|
char *incpio = argv[0];
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
|
|
|
|
magisk_cpio cpio;
|
|
|
|
if (access(incpio, R_OK) == 0)
|
|
|
|
cpio.load_cpio(incpio);
|
|
|
|
|
|
|
|
int cmdc;
|
|
|
|
char *cmdv[6];
|
|
|
|
|
2021-01-18 12:37:08 -08:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
// Reset
|
2020-12-30 22:11:24 -08:00
|
|
|
cmdc = 0;
|
2021-01-18 12:37:08 -08:00
|
|
|
memset(cmdv, 0, sizeof(cmdv));
|
2020-12-30 22:11:24 -08:00
|
|
|
|
|
|
|
// Split the commands
|
2021-01-18 12:37:08 -08:00
|
|
|
char *tok = strtok(argv[i], " ");
|
|
|
|
while (tok && cmdc < std::size(cmdv)) {
|
|
|
|
if (cmdc == 0 && tok[0] == '#')
|
|
|
|
break;
|
2020-12-30 22:11:24 -08:00
|
|
|
cmdv[cmdc++] = tok;
|
2021-01-18 12:37:08 -08:00
|
|
|
tok = strtok(nullptr, " ");
|
|
|
|
}
|
2020-12-30 22:11:24 -08:00
|
|
|
|
|
|
|
if (cmdc == 0)
|
|
|
|
continue;
|
|
|
|
|
2021-01-18 12:37:08 -08:00
|
|
|
if (cmdv[0] == "test"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
exit(cpio.test());
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdv[0] == "restore"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio.restore();
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdv[0] == "sha1"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
char *sha1 = cpio.sha1();
|
|
|
|
if (sha1) printf("%s\n", sha1);
|
|
|
|
return 0;
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdv[0] == "patch"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio.patch();
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdc == 2 && cmdv[0] == "exists"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
exit(!cpio.exists(cmdv[1]));
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdc == 2 && cmdv[0] == "backup"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio.backup(cmdv[1]);
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdc >= 2 && cmdv[0] == "rm"sv) {
|
|
|
|
bool r = cmdc > 2 && cmdv[1] == "-r"sv;
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio.rm(cmdv[1 + r], r);
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdc == 3 && cmdv[0] == "mv"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio.mv(cmdv[1], cmdv[2]);
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdv[0] == "extract"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
if (cmdc == 3) {
|
|
|
|
return !cpio.extract(cmdv[1], cmdv[2]);
|
|
|
|
} else {
|
|
|
|
cpio.extract();
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdc == 3 && cmdv[0] == "mkdir"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio.mkdir(strtoul(cmdv[1], nullptr, 8), cmdv[2]);
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdc == 3 && cmdv[0] == "ln"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio.ln(cmdv[1], cmdv[2]);
|
2021-01-18 12:37:08 -08:00
|
|
|
} else if (cmdc == 4 && cmdv[0] == "add"sv) {
|
2020-12-30 22:11:24 -08:00
|
|
|
cpio.add(strtoul(cmdv[1], nullptr, 8), cmdv[2], cmdv[3]);
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cpio.dump(incpio);
|
|
|
|
return 0;
|
2019-11-21 06:08:02 -05:00
|
|
|
}
|