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>
|
|
|
|
|
|
|
|
#include "magiskboot.h"
|
2018-10-24 21:08:06 -04:00
|
|
|
#include "array.h"
|
2017-12-01 17:17:24 +08:00
|
|
|
#include "cpio.h"
|
2018-09-27 03:11:10 -04:00
|
|
|
#include "utils.h"
|
2017-12-01 17:17:24 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
class magisk_cpio : public cpio {
|
|
|
|
public:
|
|
|
|
magisk_cpio(const char *filename) : cpio(filename) {}
|
|
|
|
void patch(bool keepverity, bool keepforceencrypt);
|
|
|
|
int test();
|
|
|
|
char * sha1();
|
|
|
|
void restore();
|
|
|
|
void backup(Array<cpio_entry*> &bak, const char *orig, const char *sha1);
|
|
|
|
};
|
|
|
|
|
|
|
|
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");
|
2018-10-24 21:08:06 -04:00
|
|
|
for (auto &e : arr) {
|
|
|
|
if (!e)
|
|
|
|
continue;
|
2017-12-01 17:17:24 +08:00
|
|
|
if (!keepverity) {
|
2017-12-21 03:36:18 +08:00
|
|
|
if (strncmp(e->filename, ".backup", 7) && strstr(e->filename, "fstab") && S_ISREG(e->mode)) {
|
|
|
|
patch_verity(&e->data, &e->filesize, 1);
|
|
|
|
} else if (strcmp(e->filename, "verity_key") == 0) {
|
2017-12-01 17:17:24 +08:00
|
|
|
fprintf(stderr, "Remove [verity_key]\n");
|
2018-10-24 21:08:06 -04:00
|
|
|
delete e;
|
|
|
|
e = nullptr;
|
2018-01-10 20:23:01 +08:00
|
|
|
continue;
|
2017-12-01 17:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!keepforceencrypt) {
|
2017-12-21 03:36:18 +08:00
|
|
|
if (strstr(e->filename, "fstab") != NULL && S_ISREG(e->mode)) {
|
|
|
|
patch_encryption(&e->data, &e->filesize);
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
|
2018-08-09 18:13:07 +08:00
|
|
|
#define STOCK_BOOT 0x0
|
|
|
|
#define MAGISK_PATCH 0x1
|
|
|
|
#define UNSUPPORT_PATCH 0x2
|
2018-10-24 21:08:06 -04:00
|
|
|
int magisk_cpio::test() {
|
|
|
|
static const char *UNSUPPORT_LIST[] = { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc",
|
|
|
|
"boot/sbin/launch_daemonsu.sh", nullptr };
|
|
|
|
static const char *MAGISK_LIST[] = { ".backup/.magisk", "init.magisk.rc",
|
|
|
|
"overlay/init.magisk.rc", nullptr };
|
2017-12-21 03:36:18 +08:00
|
|
|
|
2018-08-09 18:13:07 +08:00
|
|
|
for (int i = 0; UNSUPPORT_LIST[i]; ++i)
|
2018-10-24 21:08:06 -04:00
|
|
|
if (find(UNSUPPORT_LIST[i]) >= 0)
|
2018-08-09 18:13:07 +08:00
|
|
|
return UNSUPPORT_PATCH;
|
2018-03-10 15:55:55 +08:00
|
|
|
|
2017-12-21 03:36:18 +08:00
|
|
|
for (int i = 0; MAGISK_LIST[i]; ++i)
|
2018-10-24 21:08:06 -04:00
|
|
|
if (find(MAGISK_LIST[i]) >= 0)
|
2017-12-21 03:36:18 +08:00
|
|
|
return MAGISK_PATCH;
|
|
|
|
|
|
|
|
return STOCK_BOOT;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
char * magisk_cpio::sha1() {
|
2017-12-07 01:30:48 +08:00
|
|
|
char sha1[41];
|
2018-10-24 21:08:06 -04:00
|
|
|
for (auto &e : arr) {
|
2017-12-21 03:36:18 +08:00
|
|
|
if (!e) continue;
|
|
|
|
if (strcmp(e->filename, "init.magisk.rc") == 0
|
|
|
|
|| strcmp(e->filename, "overlay/init.magisk.rc") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
for (char *pos = (char *) e->data; pos < (char *) e->data + e->filesize;
|
|
|
|
pos = strchr(pos + 1, '\n') + 1) {
|
2017-12-07 01:30:48 +08:00
|
|
|
if (memcmp(pos, "# STOCKSHA1=", 12) == 0) {
|
|
|
|
pos += 12;
|
|
|
|
memcpy(sha1, pos, 40);
|
|
|
|
sha1[40] = '\0';
|
|
|
|
return strdup(sha1);
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (strcmp(e->filename, ".backup/.sha1") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
return (char *) e->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void magisk_cpio::restore() {
|
|
|
|
for (auto &e : arr) {
|
|
|
|
if (!e) continue;
|
|
|
|
if (strncmp(e->filename, ".backup", 7) == 0) {
|
|
|
|
if (e->filename[7] == '\0') continue;
|
|
|
|
if (e->filename[8] == '.') {
|
|
|
|
if (strcmp(e->filename + 8, ".rmlist") == 0) {
|
|
|
|
for (int pos = 0; pos < e->filesize; pos += strlen((char *) e->data + pos) + 1)
|
|
|
|
rm(false, (char *) e->data + pos);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mv(e->filename, e->filename + 8);
|
|
|
|
}
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-24 21:08:06 -04:00
|
|
|
|
|
|
|
// Some known stuff we can remove
|
|
|
|
rm(true, ".backup");
|
|
|
|
rm(true, "overlay");
|
|
|
|
rm(false, "sbin/magic_mask.sh");
|
|
|
|
rm(false, "init.magisk.rc");
|
|
|
|
rm(true, "magisk");
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
void magisk_cpio::backup(Array<cpio_entry*> &bak, const char *orig, const char *sha1) {
|
2017-12-07 01:30:48 +08:00
|
|
|
cpio_entry *m, *n, *rem, *cksm;
|
|
|
|
char buf[PATH_MAX];
|
2018-10-24 21:08:06 -04:00
|
|
|
int res;
|
|
|
|
bool backup;
|
2017-12-07 01:30:48 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
m = new cpio_entry();
|
2017-12-07 01:30:48 +08:00
|
|
|
m->filename = strdup(".backup");
|
|
|
|
m->mode = S_IFDIR;
|
2018-10-24 21:08:06 -04:00
|
|
|
bak.push_back(m);
|
2017-12-07 01:30:48 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
rem = new cpio_entry();
|
2017-12-07 01:30:48 +08:00
|
|
|
rem->filename = strdup(".backup/.rmlist");
|
|
|
|
rem->mode = S_IFREG;
|
|
|
|
|
|
|
|
if (sha1) {
|
|
|
|
fprintf(stderr, "Save SHA1: [%s] -> [.backup/.sha1]\n", sha1);
|
2018-10-24 21:08:06 -04:00
|
|
|
cksm = new cpio_entry();
|
|
|
|
bak.push_back(cksm);
|
2017-12-07 01:30:48 +08:00
|
|
|
cksm->filename = strdup(".backup/.sha1");
|
|
|
|
cksm->mode = S_IFREG;
|
|
|
|
cksm->data = strdup(sha1);
|
|
|
|
cksm->filesize = strlen(sha1) + 1;
|
|
|
|
}
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
magisk_cpio o = magisk_cpio(orig);
|
|
|
|
|
2017-12-07 01:30:48 +08:00
|
|
|
// Remove possible backups in original ramdisk
|
2018-10-24 21:08:06 -04:00
|
|
|
o.rm(true, ".backup");
|
|
|
|
rm(true, ".backup");
|
2017-12-07 01:30:48 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
// Sort both CPIOs before comparing
|
|
|
|
o.sort();
|
|
|
|
sort();
|
2017-12-07 01:30:48 +08:00
|
|
|
|
|
|
|
// Start comparing
|
|
|
|
size_t i = 0, j = 0;
|
2018-10-24 21:08:06 -04:00
|
|
|
while(i != o.arr.size() || j != arr.size()) {
|
|
|
|
backup = false;
|
|
|
|
if (i != o.arr.size() && j != arr.size()) {
|
|
|
|
m = o.arr[i];
|
|
|
|
n = arr[j];
|
2017-12-07 01:30:48 +08:00
|
|
|
res = strcmp(m->filename, n->filename);
|
2018-10-24 21:08:06 -04:00
|
|
|
} else if (i == o.arr.size()) {
|
|
|
|
n = arr[j];
|
2017-12-07 01:30:48 +08:00
|
|
|
res = 1;
|
2018-10-24 21:08:06 -04:00
|
|
|
} else if (j == arr.size()) {
|
|
|
|
m = o.arr[i];
|
2017-12-07 01:30:48 +08:00
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
// Something is missing in new ramdisk, backup!
|
|
|
|
++i;
|
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) {
|
|
|
|
++i; ++j;
|
|
|
|
if (m->filesize == n->filesize && memcmp(m->data, n->data, m->filesize) == 0)
|
|
|
|
continue;
|
|
|
|
// Not the same!
|
2018-10-24 21:08:06 -04:00
|
|
|
backup = true;
|
2017-12-07 01:30:48 +08:00
|
|
|
fprintf(stderr, "Backup mismatch entry: ");
|
|
|
|
} else {
|
2017-12-29 04:25:03 +08:00
|
|
|
// Something new in ramdisk, record in rem
|
2017-12-07 01:30:48 +08:00
|
|
|
++j;
|
|
|
|
rem->data = xrealloc(rem->data, rem->filesize + strlen(n->filename) + 1);
|
2018-10-24 21:08:06 -04:00
|
|
|
memcpy((char *) rem->data + rem->filesize, n->filename, strlen(n->filename) + 1);
|
2017-12-07 01:30:48 +08:00
|
|
|
rem->filesize += strlen(n->filename) + 1;
|
|
|
|
fprintf(stderr, "Record new entry: [%s] -> [.backup/.rmlist]\n", n->filename);
|
|
|
|
}
|
|
|
|
if (backup) {
|
|
|
|
sprintf(buf, ".backup/%s", m->filename);
|
2017-12-21 03:36:18 +08:00
|
|
|
fprintf(stderr, "[%s] -> [%s]\n", m->filename, buf);
|
2017-12-07 01:30:48 +08:00
|
|
|
free(m->filename);
|
|
|
|
m->filename = strdup(buf);
|
2018-10-24 21:08:06 -04:00
|
|
|
bak.push_back(m);
|
2017-12-07 01:30:48 +08:00
|
|
|
// NULL the original entry, so it won't be freed
|
2018-10-24 21:08:06 -04:00
|
|
|
o.arr[i - 1] = nullptr;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:36:18 +08:00
|
|
|
if (rem->filesize)
|
2018-10-24 21:08:06 -04:00
|
|
|
bak.push_back(rem);
|
2017-12-21 03:36:18 +08:00
|
|
|
else
|
2018-10-24 21:08:06 -04:00
|
|
|
delete rem;
|
2017-12-07 01:30:48 +08:00
|
|
|
}
|
|
|
|
|
2017-12-06 12:51:16 +08:00
|
|
|
|
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
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
magisk_cpio cpio = magisk_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();
|
2017-12-21 03:36:18 +08:00
|
|
|
if (sha1)
|
|
|
|
printf("%s\n", sha1);
|
2018-10-24 21:08:06 -04:00
|
|
|
free(sha1);
|
2017-12-21 03:36:18 +08:00
|
|
|
return 0;
|
|
|
|
} else if (cmdc >= 2 && strcmp(cmdv[0], "backup") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
auto bak = Array<cpio_entry*>();
|
2018-10-26 17:02:07 -04:00
|
|
|
cpio.backup(bak, cmdv[1], cmdv[2]);
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.insert(bak);
|
2018-08-09 18:13:07 +08:00
|
|
|
} else if (cmdc >= 4 && strcmp(cmdv[0], "magisk") == 0) {
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.patch(strcmp(cmdv[2], "true") == 0, strcmp(cmdv[3], "true") == 0);
|
2017-12-06 12:51:16 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
auto bak = Array<cpio_entry*>();
|
2018-10-26 17:02:07 -04:00
|
|
|
cpio.backup(bak, cmdv[1], cmdv[4]);
|
2017-12-21 03:36:18 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
auto e = new cpio_entry();
|
2017-12-21 03:36:18 +08:00
|
|
|
e->filename = strdup(".backup/.magisk");
|
|
|
|
e->mode = S_IFREG;
|
|
|
|
e->data = xmalloc(50);
|
2018-10-24 21:08:06 -04:00
|
|
|
snprintf((char *) e->data, 50, "KEEPVERITY=%s\nKEEPFORCEENCRYPT=%s\n", cmdv[2], cmdv[3]);
|
|
|
|
e->filesize = strlen((char *) e->data) + 1;
|
2017-12-21 03:36:18 +08:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.insert(bak);
|
|
|
|
cpio.insert(e);
|
2017-12-21 03:36:18 +08:00
|
|
|
} else if (cmdc >= 2 && strcmp(cmdv[0], "rm") == 0) {
|
|
|
|
int recur = cmdc > 2 && strcmp(cmdv[1], "-r") == 0;
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.rm(recur, cmdv[1 + recur]);
|
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-10-24 21:08:06 -04: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) {
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.makedir(strtoul(cmdv[1], NULL, 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) {
|
2018-10-24 21:08:06 -04:00
|
|
|
cpio.add(strtoul(cmdv[1], NULL, 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
|
|
|
}
|