2018-09-27 03:11:10 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2017-12-01 17:17:24 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2019-02-10 03:57:51 -05:00
|
|
|
#include <utils.h>
|
2019-02-23 02:42:26 -05:00
|
|
|
#include <cpio.h>
|
2019-02-10 03:57:51 -05:00
|
|
|
|
2017-12-01 17:17:24 +08:00
|
|
|
#include "magiskboot.h"
|
2019-02-23 15:04:15 -05:00
|
|
|
#include "compress.h"
|
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 *ramdisk_xz = "ramdisk.cpio.xz";
|
|
|
|
|
|
|
|
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",
|
2019-02-24 20:39:01 -05:00
|
|
|
"overlay/init.magisk.rc" };
|
2019-02-23 15:04:15 -05:00
|
|
|
|
2019-02-23 02:23:24 -05:00
|
|
|
class magisk_cpio : public cpio_rw {
|
2018-10-24 21:08:06 -04:00
|
|
|
public:
|
2019-02-28 05:46:36 -05:00
|
|
|
magisk_cpio() = default;
|
2019-02-23 02:23:24 -05:00
|
|
|
explicit magisk_cpio(const char *filename) : cpio_rw(filename) {}
|
2018-10-24 21:08:06 -04:00
|
|
|
void patch(bool keepverity, bool keepforceencrypt);
|
|
|
|
int test();
|
2019-02-23 15:04:15 -05:00
|
|
|
char *sha1();
|
2018-10-24 21:08:06 -04:00
|
|
|
void restore();
|
2018-11-15 00:33:20 -05:00
|
|
|
void backup(const char *orig);
|
2019-02-23 15:04:15 -05:00
|
|
|
void compress();
|
2019-02-23 16:53:51 -05:00
|
|
|
void decompress();
|
2018-10-24 21:08:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) {
|
2017-12-29 04:25:03 +08:00
|
|
|
fprintf(stderr, "Patch with flag KEEPVERITY=[%s] KEEPFORCEENCRYPT=[%s]\n",
|
|
|
|
keepverity ? "true" : "false", keepforceencrypt ? "true" : "false");
|
2019-02-24 04:29:15 -05:00
|
|
|
auto next = entries.begin();
|
|
|
|
decltype(next) cur;
|
|
|
|
while (next != entries.end()) {
|
|
|
|
cur = next;
|
|
|
|
++next;
|
2018-11-07 02:10:38 -05:00
|
|
|
bool fstab = (!keepverity || !keepforceencrypt) &&
|
2019-02-24 04:29:15 -05:00
|
|
|
!str_starts(cur->first, ".backup") &&
|
|
|
|
str_contains(cur->first, "fstab") && S_ISREG(cur->second->mode);
|
2017-12-01 17:17:24 +08:00
|
|
|
if (!keepverity) {
|
2018-11-07 02:10:38 -05:00
|
|
|
if (fstab) {
|
2019-02-24 23:09:34 -05:00
|
|
|
patch_verity(&cur->second->data, &cur->second->filesize);
|
2019-02-24 04:29:15 -05:00
|
|
|
} else if (cur->first == "verity_key") {
|
|
|
|
rm(cur);
|
2018-01-10 20:23:01 +08:00
|
|
|
continue;
|
2017-12-01 17:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!keepforceencrypt) {
|
2018-11-07 02:10:38 -05:00
|
|
|
if (fstab) {
|
2019-02-24 04:29:15 -05:00
|
|
|
patch_encryption(&cur->second->data, &cur->second->filesize);
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:39:01 -05:00
|
|
|
#define STOCK_BOOT 0
|
|
|
|
#define MAGISK_PATCHED 1 << 0
|
|
|
|
#define UNSUPPORTED_CPIO 1 << 1
|
|
|
|
#define COMPRESSED_CPIO 1 << 2
|
2018-10-24 21:08:06 -04:00
|
|
|
int magisk_cpio::test() {
|
2019-02-24 20:39:01 -05:00
|
|
|
if (exists(ramdisk_xz))
|
|
|
|
return MAGISK_PATCHED | COMPRESSED_CPIO;
|
|
|
|
|
2018-11-07 02:10:38 -05:00
|
|
|
for (auto file : UNSUPPORT_LIST)
|
2019-02-22 22:53:20 -05:00
|
|
|
if (exists(file))
|
2019-02-24 20:39:01 -05:00
|
|
|
return UNSUPPORTED_CPIO;
|
2018-03-10 15:55:55 +08:00
|
|
|
|
2018-11-07 02:10:38 -05:00
|
|
|
for (auto file : MAGISK_LIST)
|
2019-02-22 22:53:20 -05:00
|
|
|
if (exists(file))
|
2019-02-24 20:39:01 -05:00
|
|
|
return MAGISK_PATCHED;
|
2017-12-21 03:36:18 +08:00
|
|
|
|
|
|
|
return STOCK_BOOT;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2018-11-15 00:33:20 -05: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 02:10:38 -05:00
|
|
|
char *magisk_cpio::sha1() {
|
2017-12-07 01:30:48 +08:00
|
|
|
char sha1[41];
|
2018-11-15 00:33:20 -05:00
|
|
|
char *line;
|
2019-02-22 02:56:18 -05:00
|
|
|
for (auto &e : entries) {
|
2019-02-22 22:53:20 -05:00
|
|
|
if (e.first == "init.magisk.rc" || e.first == "overlay/init.magisk.rc") {
|
|
|
|
for_each_line(line, e.second->data, e.second->filesize) {
|
2018-11-15 00:33:20 -05:00
|
|
|
if (strncmp(line, "#STOCKSHA1=", 11) == 0) {
|
|
|
|
strncpy(sha1, line + 12, 40);
|
|
|
|
sha1[40] = '\0';
|
|
|
|
return strdup(sha1);
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 22:53:20 -05:00
|
|
|
} else if (e.first == ".backup/.magisk") {
|
|
|
|
for_each_line(line, e.second->data, e.second->filesize) {
|
2018-11-15 00:33:20 -05:00
|
|
|
if (strncmp(line, "SHA1=", 5) == 0) {
|
|
|
|
strncpy(sha1, line + 5, 40);
|
2017-12-07 01:30:48 +08:00
|
|
|
sha1[40] = '\0';
|
|
|
|
return strdup(sha1);
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 22:53:20 -05:00
|
|
|
} else if (e.first == ".backup/.sha1") {
|
|
|
|
return (char *) e.second->data;
|
2018-10-24 21:08:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-22 22:53:20 -05:00
|
|
|
#define for_each_str(str, buf, size) \
|
|
|
|
for (str = (char *) buf; str < (char *) buf + size; str = str += strlen(str) + 1)
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
void magisk_cpio::restore() {
|
2019-02-24 20:39:01 -05:00
|
|
|
decompress();
|
2019-02-22 22:53:20 -05:00
|
|
|
char *file;
|
|
|
|
auto next = entries.begin();
|
2019-02-24 04:29:15 -05:00
|
|
|
decltype(next) cur;
|
2019-02-22 22:53:20 -05:00
|
|
|
while (next != entries.end()) {
|
|
|
|
cur = next;
|
|
|
|
++next;
|
|
|
|
if (str_starts(cur->first, ".backup")) {
|
|
|
|
if (cur->first.length() == 7 || cur->first.substr(8) == ".magisk") {
|
|
|
|
rm(cur);
|
|
|
|
} else if (cur->first.substr(8) == ".rmlist") {
|
|
|
|
for_each_str(file, cur->second->data, cur->second->filesize)
|
|
|
|
rm(file, false);
|
|
|
|
rm(cur);
|
2018-10-24 21:08:06 -04:00
|
|
|
} else {
|
2019-02-22 22:53:20 -05:00
|
|
|
mv(cur, &cur->first[8]);
|
2018-10-24 21:08:06 -04:00
|
|
|
}
|
2019-06-27 18:01:00 -03:00
|
|
|
} else if (str_starts(cur->first, "magisk") ||
|
|
|
|
cur->first == "overlay/init.magisk.rc" ||
|
2019-02-22 22:53:20 -05:00
|
|
|
cur->first == "sbin/magic_mask.sh" ||
|
|
|
|
cur->first == "init.magisk.rc") {
|
|
|
|
// Some known stuff we can remove
|
|
|
|
rm(cur);
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 00:33:20 -05:00
|
|
|
void magisk_cpio::backup(const char *orig) {
|
2019-02-28 05:46:36 -05:00
|
|
|
if (access(orig, R_OK))
|
|
|
|
return;
|
2019-02-22 22:53:20 -05:00
|
|
|
entry_map bkup_entries;
|
2019-02-22 02:56:18 -05:00
|
|
|
string remv;
|
2017-12-07 01:30:48 +08:00
|
|
|
|
2019-02-23 15:22:11 -05:00
|
|
|
auto b = new cpio_entry(".backup", S_IFDIR);
|
2019-02-22 22:53:20 -05:00
|
|
|
bkup_entries[b->filename].reset(b);
|
2017-12-07 01:30:48 +08:00
|
|
|
|
2018-11-07 02:10:38 -05:00
|
|
|
magisk_cpio o(orig);
|
2018-10-24 21:08:06 -04:00
|
|
|
|
2017-12-07 01:30:48 +08:00
|
|
|
// Remove possible backups in original ramdisk
|
2018-11-07 02:10:38 -05:00
|
|
|
o.rm(".backup", true);
|
|
|
|
rm(".backup", true);
|
2017-12-07 01:30:48 +08:00
|
|
|
|
2019-02-22 22:53:20 -05:00
|
|
|
auto lhs = o.entries.begin();
|
|
|
|
auto rhs = entries.begin();
|
2017-12-07 01:30:48 +08:00
|
|
|
|
2019-02-22 22:53:20 -05:00
|
|
|
while (lhs != o.entries.end() || rhs != entries.end()) {
|
2018-11-07 02:10:38 -05:00
|
|
|
int res;
|
|
|
|
bool backup = false;
|
2019-02-22 22:53:20 -05:00
|
|
|
if (lhs != o.entries.end() && rhs != entries.end()) {
|
|
|
|
res = lhs->first.compare(rhs->first);
|
|
|
|
} else if (lhs == o.entries.end()) {
|
2017-12-07 01:30:48 +08:00
|
|
|
res = 1;
|
2019-04-29 21:26:43 -04:00
|
|
|
} else {
|
2017-12-07 01:30:48 +08:00
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
// Something is missing in new ramdisk, backup!
|
2018-10-24 21:08:06 -04:00
|
|
|
backup = true;
|
2017-12-07 01:30:48 +08:00
|
|
|
fprintf(stderr, "Backup missing entry: ");
|
|
|
|
} else if (res == 0) {
|
2019-02-22 22:53:20 -05:00
|
|
|
if (lhs->second->filesize != rhs->second->filesize ||
|
|
|
|
memcmp(lhs->second->data, rhs->second->data, lhs->second->filesize) != 0) {
|
2019-02-22 02:56:18 -05:00
|
|
|
// Not the same!
|
|
|
|
backup = true;
|
|
|
|
fprintf(stderr, "Backup mismatch entry: ");
|
|
|
|
}
|
2017-12-07 01:30:48 +08:00
|
|
|
} else {
|
2019-02-22 02:56:18 -05:00
|
|
|
// Something new in ramdisk
|
2019-02-22 22:53:20 -05:00
|
|
|
remv += rhs->first;
|
2019-02-22 02:56:18 -05:00
|
|
|
remv += (char) '\0';
|
2019-02-22 22:53:20 -05:00
|
|
|
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", rhs->first.data());
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
if (backup) {
|
2019-02-22 22:53:20 -05:00
|
|
|
string back_name(".backup/");
|
|
|
|
back_name += lhs->first;
|
|
|
|
fprintf(stderr, "[%s] -> [%s]\n", lhs->first.data(), back_name.data());
|
2019-02-23 02:23:24 -05:00
|
|
|
auto ex = static_cast<cpio_entry*>(lhs->second.release());
|
2019-02-22 22:53:20 -05:00
|
|
|
ex->filename = back_name;
|
|
|
|
bkup_entries[ex->filename].reset(ex);
|
2019-02-22 02:56:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment positions
|
|
|
|
if (res < 0) {
|
2019-02-22 22:53:20 -05:00
|
|
|
++lhs;
|
2019-02-22 02:56:18 -05:00
|
|
|
} else if (res == 0) {
|
2019-02-22 22:53:20 -05:00
|
|
|
++lhs; ++rhs;
|
2019-02-22 02:56:18 -05:00
|
|
|
} else {
|
2019-02-22 22:53:20 -05:00
|
|
|
++rhs;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 02:56:18 -05:00
|
|
|
if (!remv.empty()) {
|
2019-02-23 15:22:11 -05:00
|
|
|
auto rmlist = new cpio_entry(".backup/.rmlist", S_IFREG);
|
2019-02-22 02:56:18 -05:00
|
|
|
rmlist->filesize = remv.length();
|
|
|
|
rmlist->data = xmalloc(remv.length());
|
|
|
|
memcpy(rmlist->data, remv.data(), remv.length());
|
2019-02-22 22:53:20 -05:00
|
|
|
bkup_entries[rmlist->filename].reset(rmlist);
|
2019-02-22 02:56:18 -05:00
|
|
|
}
|
2018-11-15 00:33:20 -05:00
|
|
|
|
2019-02-22 22:53:20 -05:00
|
|
|
if (bkup_entries.size() > 1)
|
|
|
|
entries.merge(bkup_entries);
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2019-02-23 15:04:15 -05:00
|
|
|
void magisk_cpio::compress() {
|
2019-02-23 16:53:51 -05:00
|
|
|
if (exists(ramdisk_xz))
|
|
|
|
return;
|
2019-02-23 15:04:15 -05:00
|
|
|
fprintf(stderr, "Compressing cpio -> [%s]\n", ramdisk_xz);
|
|
|
|
auto init = entries.extract("init");
|
|
|
|
XZEncoder encoder;
|
|
|
|
encoder.set_out(make_unique<BufOutStream>());
|
|
|
|
output(encoder);
|
|
|
|
encoder.finalize();
|
2019-02-24 20:39:01 -05:00
|
|
|
auto backup = entries.extract(".backup");
|
|
|
|
auto config = entries.extract(".backup/.magisk");
|
2019-02-23 15:04:15 -05:00
|
|
|
entries.clear();
|
|
|
|
entries.insert(std::move(init));
|
2019-02-24 20:39:01 -05:00
|
|
|
entries.insert(std::move(backup));
|
|
|
|
entries.insert(std::move(config));
|
2019-02-23 15:22:11 -05:00
|
|
|
auto xz = new cpio_entry(ramdisk_xz, S_IFREG);
|
2019-02-23 15:04:15 -05:00
|
|
|
static_cast<BufOutStream *>(encoder.get_out())->release(xz->data, xz->filesize);
|
|
|
|
insert(xz);
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:53:51 -05:00
|
|
|
void magisk_cpio::decompress() {
|
|
|
|
auto it = entries.find(ramdisk_xz);
|
|
|
|
if (it == entries.end())
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "Decompressing cpio [%s]\n", ramdisk_xz);
|
2019-02-24 20:39:01 -05:00
|
|
|
entries.erase(".backup");
|
|
|
|
entries.erase(".backup/.magisk");
|
2019-02-23 16:53:51 -05:00
|
|
|
LZMADecoder decoder;
|
|
|
|
decoder.set_out(make_unique<BufOutStream>());
|
|
|
|
decoder.write(it->second->data, it->second->filesize);
|
|
|
|
decoder.finalize();
|
|
|
|
entries.erase(it);
|
|
|
|
char *buf;
|
|
|
|
size_t sz;
|
|
|
|
static_cast<BufOutStream *>(decoder.get_out())->getbuf(buf, sz);
|
|
|
|
load_cpio(buf, sz);
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:36:18 +08:00
|
|
|
int cpio_commands(int argc, char *argv[]) {
|
2017-12-01 17:17:24 +08:00
|
|
|
char *incpio = argv[0];
|
|
|
|
++argv;
|
|
|
|
--argc;
|
2017-12-21 03:36:18 +08:00
|
|
|
|
2019-02-28 05:46:36 -05:00
|
|
|
magisk_cpio cpio;
|
|
|
|
if (access(incpio, R_OK) == 0)
|
|
|
|
cpio.load_cpio(incpio);
|
2017-12-04 03:14:04 +08:00
|
|
|
|
2017-12-21 03:36:18 +08:00
|
|
|
int cmdc;
|
|
|
|
char *cmdv[6];
|
2017-12-06 12:51:16 +08:00
|
|
|
|
2017-12-21 03:36:18 +08:00
|
|
|
while (argc) {
|
2018-10-26 17:02:07 -04:00
|
|
|
// Clean up
|
2017-12-21 03:36:18 +08:00
|
|
|
cmdc = 0;
|
2018-10-26 17:02:07 -04:00
|
|
|
memset(cmdv, NULL, sizeof(cmdv));
|
|
|
|
|
|
|
|
// Split the commands
|
|
|
|
for (char *tok = strtok(argv[0], " "); tok; tok = strtok(nullptr, " "))
|
2017-12-21 03:36:18 +08:00
|
|
|
cmdv[cmdc++] = tok;
|
2017-12-06 12:51:16 +08:00
|
|
|
|
2018-10-26 17:02:07 -04:00
|
|
|
if (cmdc == 0)
|
|
|
|
continue;
|
|
|
|
|
2017-12-21 03:36:18 +08:00
|
|
|
if (strcmp(cmdv[0], "test") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
exit(cpio.test());
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (strcmp(cmdv[0], "restore") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.restore();
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (strcmp(cmdv[0], "sha1") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
char *sha1 = cpio.sha1();
|
2018-11-15 00:33:20 -05:00
|
|
|
if (sha1) printf("%s\n", sha1);
|
2017-12-21 03:36:18 +08:00
|
|
|
return 0;
|
2019-02-23 15:04:15 -05:00
|
|
|
} else if (strcmp(cmdv[0], "compress") == 0){
|
|
|
|
cpio.compress();
|
2019-02-23 16:53:51 -05:00
|
|
|
} else if (strcmp(cmdv[0], "decompress") == 0){
|
|
|
|
cpio.decompress();
|
2018-12-05 20:27:48 -05:00
|
|
|
} else if (cmdc == 2 && strcmp(cmdv[0], "exists") == 0) {
|
2019-02-22 22:53:20 -05:00
|
|
|
exit(!cpio.exists(cmdv[1]));
|
2018-12-05 20:27:48 -05:00
|
|
|
} else if (cmdc == 2 && strcmp(cmdv[0], "backup") == 0) {
|
2018-11-15 00:33:20 -05:00
|
|
|
cpio.backup(cmdv[1]);
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (cmdc >= 2 && strcmp(cmdv[0], "rm") == 0) {
|
2018-11-07 02:10:38 -05:00
|
|
|
bool r = cmdc > 2 && strcmp(cmdv[1], "-r") == 0;
|
|
|
|
cpio.rm(cmdv[1 + r], r);
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (cmdc == 3 && strcmp(cmdv[0], "mv") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.mv(cmdv[1], cmdv[2]);
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (cmdc == 3 && strcmp(cmdv[0], "patch") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.patch(strcmp(cmdv[1], "true") == 0, strcmp(cmdv[2], "true") == 0);
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (strcmp(cmdv[0], "extract") == 0) {
|
|
|
|
if (cmdc == 3) {
|
2018-11-15 00:33:20 -05:00
|
|
|
return !cpio.extract(cmdv[1], cmdv[2]);
|
2017-12-21 03:36:18 +08:00
|
|
|
} else {
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.extract();
|
2017-12-21 03:36:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (cmdc == 3 && strcmp(cmdv[0], "mkdir") == 0) {
|
2019-02-28 05:46:36 -05:00
|
|
|
cpio.mkdir(strtoul(cmdv[1], nullptr, 8), cmdv[2]);
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (cmdc == 3 && strcmp(cmdv[0], "ln") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.ln(cmdv[1], cmdv[2]);
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (cmdc == 4 && strcmp(cmdv[0], "add") == 0) {
|
2019-01-19 23:59:37 -05:00
|
|
|
cpio.add(strtoul(cmdv[1], nullptr, 8), cmdv[2], cmdv[3]);
|
2017-12-05 03:32:37 +08:00
|
|
|
} else {
|
2017-12-21 03:36:18 +08:00
|
|
|
return 1;
|
2017-12-05 03:32:37 +08:00
|
|
|
}
|
2017-12-21 03:36:18 +08:00
|
|
|
|
|
|
|
--argc;
|
|
|
|
++argv;
|
2017-12-01 17:17:24 +08:00
|
|
|
}
|
2017-12-04 03:14:04 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.dump(incpio);
|
2017-12-21 03:36:18 +08:00
|
|
|
return 0;
|
2018-10-24 21:08:06 -04:00
|
|
|
}
|