Refactor magiskboot

This commit is contained in:
topjohnwu
2017-12-21 03:36:18 +08:00
parent e8dd1b292f
commit a3c49de6a5
16 changed files with 351 additions and 347 deletions

View File

@ -9,19 +9,21 @@
#include "cpio.h"
static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) {
cpio_entry *f;
vec_for_each(v, f) {
cpio_entry *e;
vec_for_each(v, e) {
if (!e) continue;
if (!keepverity) {
if (strstr(f->filename, "fstab") != NULL && S_ISREG(f->mode)) {
patch_verity(&f->data, &f->filesize, 1);
} else if (strcmp(f->filename, "verity_key") == 0) {
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) {
fprintf(stderr, "Remove [verity_key]\n");
f->remove = 1;
cpio_free(e);
vec_cur(v) = NULL;
}
}
if (!keepforceencrypt) {
if (strstr(f->filename, "fstab") != NULL && S_ISREG(f->mode)) {
patch_encryption(&f->data, &f->filesize);
if (strstr(e->filename, "fstab") != NULL && S_ISREG(e->mode)) {
patch_encryption(&e->data, &e->filesize);
}
}
}
@ -32,33 +34,28 @@ static void cpio_patch(struct vector *v, int keepverity, int keepforceencrypt) {
#define OTHER_PATCH 0x2
static int cpio_test(struct vector *v) {
int ret = STOCK_BOOT;
cpio_entry *f;
const char *OTHER_LIST[] = { "sbin/launch_daemonsu.sh", "sbin/su", "init.xposed.rc", "boot/sbin/launch_daemonsu.sh", NULL };
const char *MAGISK_LIST[] = { ".backup/.magisk", "init.magisk.rc", "overlay/init.magisk.rc", NULL };
vec_for_each(v, f) {
for (int i = 0; OTHER_LIST[i]; ++i) {
if (strcmp(f->filename, OTHER_LIST[i]) == 0) {
// Already find other files, abort
return OTHER_PATCH;
}
}
for (int i = 0; MAGISK_LIST[i]; ++i) {
if (strcmp(f->filename, MAGISK_LIST[i]) == 0)
ret = MAGISK_PATCH;
}
}
cpio_vec_destroy(v);
return ret;
for (int i = 0; OTHER_LIST[i]; ++i)
if (cpio_find(v, OTHER_LIST[i]) > 0)
return OTHER_PATCH;
for (int i = 0; MAGISK_LIST[i]; ++i)
if (cpio_find(v, MAGISK_LIST[i]) > 0)
return MAGISK_PATCH;
return STOCK_BOOT;
}
static char *cpio_stocksha1(struct vector *v) {
cpio_entry *f;
static char *cpio_sha1(struct vector *v) {
cpio_entry *e;
char sha1[41];
vec_for_each(v, f) {
if (strcmp(f->filename, "init.magisk.rc") == 0
|| strcmp(f->filename, "overlay/init.magisk.rc") == 0) {
for (void *pos = f->data; pos < f->data + f->filesize; pos = strchr(pos + 1, '\n') + 1) {
vec_for_each(v, e) {
if (!e) continue;
if (strcmp(e->filename, "init.magisk.rc") == 0
|| strcmp(e->filename, "overlay/init.magisk.rc") == 0) {
for (void *pos = e->data; pos < e->data + e->filesize; pos = strchr(pos + 1, '\n') + 1) {
if (memcmp(pos, "# STOCKSHA1=", 12) == 0) {
pos += 12;
memcpy(sha1, pos, 40);
@ -66,61 +63,47 @@ static char *cpio_stocksha1(struct vector *v) {
return strdup(sha1);
}
}
} else if (strcmp(f->filename, ".backup/.sha1") == 0) {
return f->data;
} else if (strcmp(e->filename, ".backup/.sha1") == 0) {
return e->data;
}
}
return NULL;
}
static struct vector *cpio_backup(struct vector *v, const char *orig, const char *keepverity,
const char *keepforceencrypt, const char *sha1) {
struct vector o_body, *o = &o_body, *ret;
static void cpio_backup(struct vector *v, struct vector *bak, const char *orig, const char *sha1) {
struct vector o_body, *o = &o_body;
cpio_entry *m, *n, *rem, *cksm;
char buf[PATH_MAX];
int res, backup;
ret = xcalloc(sizeof(*ret), 1);
vec_init(o);
vec_init(ret);
m = xcalloc(sizeof(*m), 1);
m->filename = strdup(".backup");
m->mode = S_IFDIR;
vec_push_back(ret, m);
m = xcalloc(sizeof(*m), 1);
m->filename = strdup(".backup/.magisk");
m->mode = S_IFREG;
m->data = xmalloc(50);
snprintf(m->data, 50, "KEEPVERITY=%s\nKEEPFORCEENCRYPT=%s\n", keepverity, keepforceencrypt);
m->filesize = strlen(m->data) + 1;
vec_push_back(ret, m);
vec_push_back(bak, m);
rem = xcalloc(sizeof(*rem), 1);
rem->filename = strdup(".backup/.rmlist");
rem->mode = S_IFREG;
vec_push_back(ret, rem);
if (sha1) {
fprintf(stderr, "Save SHA1: [%s] -> [.backup/.sha1]\n", sha1);
cksm = xcalloc(sizeof(*cksm), 1);
vec_push_back(ret, cksm);
vec_push_back(bak, cksm);
cksm->filename = strdup(".backup/.sha1");
cksm->mode = S_IFREG;
cksm->data = strdup(sha1);
cksm->filesize = strlen(sha1) + 1;
}
vec_init(o);
parse_cpio(o, orig);
// Remove possible backups in original ramdisk
cpio_rm(o, 1, ".backup");
cpio_rm(v, 1, ".backup");
// Sort both vectors before comparing
vec_sort(v, cpio_cmp);
vec_sort(o, cpio_cmp);
vec_sort(v, cpio_cmp);
// Start comparing
size_t i = 0, j = 0;
@ -153,7 +136,6 @@ static struct vector *cpio_backup(struct vector *v, const char *orig, const char
} else {
// Someting new in ramdisk, record in rem
++j;
if (n->remove) continue;
rem->data = xrealloc(rem->data, rem->filesize + strlen(n->filename) + 1);
memcpy(rem->data + rem->filesize, n->filename, strlen(n->filename) + 1);
rem->filesize += strlen(n->filename) + 1;
@ -161,51 +143,49 @@ static struct vector *cpio_backup(struct vector *v, const char *orig, const char
}
if (backup) {
sprintf(buf, ".backup/%s", m->filename);
fprintf(stderr, "[%s] -> [%s]\n", m->filename, buf);
free(m->filename);
m->filename = strdup(buf);
fprintf(stderr, "[%s] -> [%s]\n", buf, m->filename);
vec_push_back(ret, m);
vec_push_back(bak, m);
// NULL the original entry, so it won't be freed
vec_entry(o)[i - 1] = NULL;
}
}
if (rem->filesize == 0)
rem->remove = 1;
if (rem->filesize)
vec_push_back(bak, rem);
else
cpio_free(rem);
// Cleanup
cpio_vec_destroy(o);
return ret;
}
static void cpio_restore(struct vector *v) {
cpio_entry *f, *n;
vec_for_each(v, f) {
if (strncmp(f->filename, ".backup", 7) == 0) {
f->remove = 1;
if (f->filename[7] == '\0') continue;
if (f->filename[8] == '.') {
if (strcmp(f->filename, ".backup/.rmlist") == 0) {
for (int pos = 0; pos < f->filesize; pos += strlen(f->data + pos) + 1)
cpio_rm(v, 0, f->data + pos);
cpio_entry *e;
vec_for_each(v, e) {
if (!e) continue;
if (strncmp(e->filename, ".backup", 7) == 0) {
if (e->filename[7] == '\0') continue;
if (e->filename[8] == '.') {
if (strcmp(e->filename, ".backup/.rmlist") == 0) {
for (int pos = 0; pos < e->filesize; pos += strlen(e->data + pos) + 1)
cpio_rm(v, 0, e->data + pos);
}
continue;
} else {
n = xcalloc(sizeof(*n), 1);
memcpy(n, f, sizeof(*f));
n->filename = strdup(f->filename + 8);
n->data = f->data;
f->data = NULL;
n->remove = 0;
fprintf(stderr, "Restore [%s] -> [%s]\n", f->filename, n->filename);
cpio_vec_insert(v, n);
fprintf(stderr, "Restore [%s] -> [%s]\n", e->filename, e->filename + 8);
vec_cur(v) = NULL;
char *new_name = strdup(e->filename + 8);
free(e->filename);
e->filename = new_name;
cpio_vec_insert(v, e);
}
}
if (strncmp(f->filename, "overlay", 7) == 0)
f->remove = 1;
}
// Some known stuff we can remove
cpio_rm(v, 1, ".backup");
cpio_rm(v, 1, "overlay");
cpio_rm(v, 0, "sbin/magic_mask.sh");
cpio_rm(v, 0, "init.magisk.rc");
cpio_rm(v, 0, "magisk");
@ -235,25 +215,16 @@ static void restore_high_compress(struct vector *v, const char *incpio) {
}
static void enable_high_compress(struct vector *v, struct vector *b, const char *incpio) {
cpio_entry *e, *magiskinit, *init;
cpio_entry *init, *magiskinit;
// Swap magiskinit with original init
vec_for_each(b, e) {
if (strcmp(e->filename, ".backup/init") == 0) {
free(e->filename);
e->filename = strdup("init");
init = e;
vec_for_each(v, e) {
if (strcmp(e->filename, "init") == 0) {
magiskinit = e;
vec_cur(v) = init;
break;
}
}
vec_cur(b) = NULL;
break;
}
}
int i = cpio_find(b, ".backup/init"), j = cpio_find(v, "init");
init = vec_entry(b)[i];
magiskinit = vec_entry(v)[j];
free(init->filename);
init->filename = strdup("init");
vec_entry(v)[j] = init;
vec_entry(b)[i] = NULL;
dump_cpio(v, incpio);
cpio_vec_destroy(v);
@ -269,64 +240,93 @@ static void enable_high_compress(struct vector *v, struct vector *b, const char
cpio_add(v, 0, "ramdisk.cpio.xz", incpio);
}
int cpio_commands(const char *command, int argc, char *argv[]) {
int cpio_commands(int argc, char *argv[]) {
char *incpio = argv[0];
++argv;
--argc;
struct vector v;
vec_init(&v);
parse_cpio(&v, incpio);
if (strcmp(command, "test") == 0) {
exit(cpio_test(&v));
} else if (strcmp(command, "restore") == 0) {
restore_high_compress(&v, incpio);
cpio_restore(&v);
} else if (strcmp(command, "stocksha1") == 0) {
printf("%s\n", cpio_stocksha1(&v));
return 0;
} else if (argc >= 4 && strcmp(command, "backup") == 0) {
struct vector *back;
cpio_entry *e;
back = cpio_backup(&v, argv[0], argv[2], argv[3], argc > 4 ? argv[4] : NULL);
int cmdc;
char *cmdv[6];
// Enable high compression mode
if (strcmp(argv[1], "true") == 0)
enable_high_compress(&v, back, incpio);
while (argc) {
cmdc = 0;
for (char *tok = strtok(argv[0], " "); tok; tok = strtok(NULL, " "))
cmdv[cmdc++] = tok;
vec_for_each(back, e)
if (e) vec_push_back(&v, e);
} else if (argc > 0 && strcmp(command, "rm") == 0) {
int recursive = 0;
if (argc == 2 && strcmp(argv[0], "-r") == 0) {
recursive = 1;
++argv;
}
cpio_rm(&v, recursive, argv[0]);
} else if (argc == 2 && strcmp(command, "mv") == 0) {
if (cpio_mv(&v, argv[0], argv[1]))
return 1;
} else if (argc == 2 && strcmp(command, "patch") == 0) {
cpio_patch(&v, strcmp(argv[0], "true") == 0, strcmp(argv[1], "true") == 0);
} else if (strcmp(command, "extract") == 0) {
if (argc == 2) {
return cpio_extract(&v, argv[0], argv[1]);
} else {
cpio_extract_all(&v);
if (strcmp(cmdv[0], "test") == 0) {
exit(cpio_test(&v));
} else if (strcmp(cmdv[0], "restore") == 0) {
restore_high_compress(&v, incpio);
cpio_restore(&v);
} else if (strcmp(cmdv[0], "sha1") == 0) {
char *sha1 = cpio_sha1(&v);
if (sha1)
printf("%s\n", sha1);
return 0;
} else if (cmdc >= 2 && strcmp(cmdv[0], "backup") == 0) {
struct vector back;
vec_init(&back);
cpio_backup(&v, &back, cmdv[1], cmdc > 2 ? cmdv[2] : NULL);
cpio_entry *e;
vec_for_each(&back, e)
if (e) vec_push_back(&v, e);
vec_destroy(&back);
} else if (cmdc >= 5 && strcmp(cmdv[0], "magisk") == 0) {
cpio_patch(&v, strcmp(cmdv[3], "true") == 0, strcmp(cmdv[4], "true") == 0);
struct vector back;
vec_init(&back);
cpio_backup(&v, &back, cmdv[1], cmdc > 5 ? cmdv[5] : NULL);
cpio_entry *e;
e = xcalloc(sizeof(*e), 1);
e->filename = strdup(".backup/.magisk");
e->mode = S_IFREG;
e->data = xmalloc(50);
snprintf(e->data, 50, "KEEPVERITY=%s\nKEEPFORCEENCRYPT=%s\n", cmdv[3], cmdv[4]);
e->filesize = strlen(e->data) + 1;
vec_push_back(&back, e);
// Enable high compression mode
if (strcmp(cmdv[2], "true") == 0)
enable_high_compress(&v, &back, incpio);
vec_for_each(&back, e)
if (e) vec_push_back(&v, e);
vec_destroy(&back);
} else if (cmdc >= 2 && strcmp(cmdv[0], "rm") == 0) {
int recur = cmdc > 2 && strcmp(cmdv[1], "-r") == 0;
cpio_rm(&v, recur, cmdv[1 + recur]);
} else if (cmdc == 3 && strcmp(cmdv[0], "mv") == 0) {
cpio_mv(&v, cmdv[1], cmdv[2]);
} else if (cmdc == 3 && strcmp(cmdv[0], "patch") == 0) {
cpio_patch(&v, strcmp(cmdv[1], "true") == 0, strcmp(cmdv[2], "true") == 0);
} else if (strcmp(cmdv[0], "extract") == 0) {
if (cmdc == 3) {
return cpio_extract(&v, cmdv[1], cmdv[2]);
} else {
cpio_extract_all(&v);
return 0;
}
} else if (cmdc == 3 && strcmp(cmdv[0], "mkdir") == 0) {
cpio_mkdir(&v, strtoul(cmdv[1], NULL, 8), cmdv[2]);
} else if (cmdc == 3 && strcmp(cmdv[0], "ln") == 0) {
cpio_ln(&v, cmdv[1], cmdv[2]);
} else if (cmdc == 4 && strcmp(cmdv[0], "add") == 0) {
cpio_add(&v, strtoul(cmdv[1], NULL, 8), cmdv[2], cmdv[3]);
} else {
return 1;
}
} else if (argc == 2 && strcmp(command, "mkdir") == 0) {
cpio_mkdir(&v, strtoul(argv[0], NULL, 8), argv[1]);
} else if (argc == 2 && strcmp(command, "ln") == 0) {
cpio_ln(&v, argv[0], argv[1]);
} else if (argc == 3 && strcmp(command, "add") == 0) {
cpio_add(&v, strtoul(argv[0], NULL, 8), argv[1], argv[2]);
} else {
return 1;
--argc;
++argv;
}
dump_cpio(&v, incpio);
cpio_vec_destroy(&v);
exit(0);
return 0;
}