diff --git a/app/src/full/res/raw/utils.sh b/app/src/full/res/raw/utils.sh index f9fef56fa..6a550b65c 100644 --- a/app/src/full/res/raw/utils.sh +++ b/app/src/full/res/raw/utils.sh @@ -79,8 +79,9 @@ mm_patch_dtbo() { } restore_imgs() { - local SHA1=`cat /.backup/.sha1` - [ -z $SHA1 ] && local SHA1=`grep_prop #STOCKSHA1` + local SHA1=`grep_prop SHA1 /.backup/.magisk` + [ -z $SHA1 ] && local SHA1=`cat /.backup/.sha1` + [ -z $SHA1 ] && local SHA1=`grep_prop #STOCKSHA1 /.backup/.magisk` [ -z $SHA1 ] && return 1 local STOCKBOOT=/data/stock_boot_${SHA1}.img.gz local STOCKDTBO=/data/stock_dtbo.img.gz diff --git a/native/jni/magiskboot/main.cpp b/native/jni/magiskboot/main.cpp index 6e114ee49..ef9020047 100644 --- a/native/jni/magiskboot/main.cpp +++ b/native/jni/magiskboot/main.cpp @@ -57,18 +57,12 @@ static void usage(char *arg0) { " 0:stock 1:Magisk 2:unsupported (phh, SuperSU, Xposed)\n" " patch KEEPVERITY KEEPFORCEENCRYPT\n" " Ramdisk patches. KEEP**** are boolean values\n" - " backup ORIG [SHA1]\n" + " backup ORIG\n" " Create ramdisk backups from ORIG\n" - " SHA1 of stock boot image is optional\n" " restore\n" " Restore ramdisk from ramdisk backup stored within incpio\n" - " magisk ORIG KEEPVERITY KEEPFORCEENCRYPT [SHA1]\n" - " Do Magisk patches and backups all in one step\n" - " Create ramdisk backups from ORIG\n" - " KEEP**** are boolean values\n" - " SHA1 of stock boot image is optional\n" " sha1\n" - " Print stock boot SHA1 if previously stored\n" + " Print stock boot SHA1 if previously backed up in ramdisk\n" "\n" " --dtb- \n" " Do dtb related cmds to (modifications are done directly)\n" diff --git a/native/jni/magiskboot/ramdisk.cpp b/native/jni/magiskboot/ramdisk.cpp index 6de04637b..4699c7928 100644 --- a/native/jni/magiskboot/ramdisk.cpp +++ b/native/jni/magiskboot/ramdisk.cpp @@ -13,7 +13,7 @@ public: int test(); char * sha1(); void restore(); - void backup(Vector &bak, const char *orig, const char *sha1); + void backup(const char *orig); }; void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) { @@ -64,16 +64,26 @@ int magisk_cpio::test() { return STOCK_BOOT; } +#define for_each_line(line, buf, size) \ +for (line = (char *) buf; line < (char *) buf + size && line[0]; line = strchr(line + 1, '\n') + 1) + char *magisk_cpio::sha1() { char sha1[41]; + char *line; for (auto &e : arr) { if (!e) continue; if (e->filename == "init.magisk.rc" || e->filename == "overlay/init.magisk.rc") { - for (char *pos = (char *) e->data; pos < (char *) e->data + e->filesize; - pos = strchr(pos + 1, '\n') + 1) { - if (memcmp(pos, "# STOCKSHA1=", 12) == 0) { - pos += 12; - memcpy(sha1, pos, 40); + 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); sha1[40] = '\0'; return strdup(sha1); } @@ -109,8 +119,9 @@ void magisk_cpio::restore() { rm("magisk", true); } -void magisk_cpio::backup(Vector &bak, const char *orig, const char *sha1) { - cpio_entry *m, *n, *rem, *cksm; +void magisk_cpio::backup(const char *orig) { + Vector bak; + cpio_entry *m, *n, *rem; char buf[PATH_MAX]; m = new cpio_entry(); @@ -122,16 +133,6 @@ void magisk_cpio::backup(Vector &bak, const char *orig, const char rem->filename = ".backup/.rmlist"; rem->mode = S_IFREG; - if (sha1) { - fprintf(stderr, "Save SHA1: [%s] -> [.backup/.sha1]\n", sha1); - cksm = new cpio_entry(); - cksm->filename = ".backup/.sha1"; - cksm->mode = S_IFREG; - cksm->data = strdup(sha1); - cksm->filesize = strlen(sha1) + 1; - bak.push_back(cksm); - } - magisk_cpio o(orig); // Remove possible backups in original ramdisk @@ -193,6 +194,9 @@ void magisk_cpio::backup(Vector &bak, const char *orig, const char bak.push_back(rem); else delete rem; + + if (bak.size() > 1) + insert(bak); } @@ -224,29 +228,10 @@ int cpio_commands(int argc, char *argv[]) { cpio.restore(); } else if (strcmp(cmdv[0], "sha1") == 0) { char *sha1 = cpio.sha1(); - if (sha1) - printf("%s\n", sha1); - free(sha1); + if (sha1) printf("%s\n", sha1); return 0; } else if (cmdc >= 2 && strcmp(cmdv[0], "backup") == 0) { - Vector bak; - cpio.backup(bak, cmdv[1], cmdv[2]); - cpio.insert(bak); - } else if (cmdc >= 4 && strcmp(cmdv[0], "magisk") == 0) { - cpio.patch(strcmp(cmdv[2], "true") == 0, strcmp(cmdv[3], "true") == 0); - - Vector bak; - cpio.backup(bak, cmdv[1], cmdv[4]); - - auto e = new cpio_entry(); - e->filename = ".backup/.magisk"; - e->mode = S_IFREG; - e->data = xmalloc(50); - snprintf((char *) e->data, 50, "KEEPVERITY=%s\nKEEPFORCEENCRYPT=%s\n", cmdv[2], cmdv[3]); - e->filesize = strlen((char *) e->data) + 1; - - cpio.insert(bak); - cpio.insert(e); + cpio.backup(cmdv[1]); } else if (cmdc >= 2 && strcmp(cmdv[0], "rm") == 0) { bool r = cmdc > 2 && strcmp(cmdv[1], "-r") == 0; cpio.rm(cmdv[1 + r], r); @@ -256,7 +241,7 @@ int cpio_commands(int argc, char *argv[]) { cpio.patch(strcmp(cmdv[1], "true") == 0, strcmp(cmdv[2], "true") == 0); } else if (strcmp(cmdv[0], "extract") == 0) { if (cmdc == 3) { - return cpio.extract(cmdv[1], cmdv[2]); + return !cpio.extract(cmdv[1], cmdv[2]); } else { cpio.extract(); return 0; diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index 5b5797483..4fc3add03 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -125,11 +125,17 @@ esac ui_print "- Patching ramdisk" +echo "KEEPVERITY=$KEEPVERITY" > config +echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config +[ ! -z $SHA1 ] && echo "SHA1=$SHA1" >> config + ./magiskboot --cpio ramdisk.cpio \ "add 750 init magiskinit" \ -"magisk ramdisk.cpio.orig $KEEPVERITY $KEEPFORCEENCRYPT $SHA1" +"patch $KEEPVERITY $KEEPFORCEENCRYPT" \ +"backup ramdisk.cpio.orig" \ +"add 000 .backup/.magisk config" -rm -f ramdisk.cpio.orig +rm -f ramdisk.cpio.orig config ########################################################################################## # Binary patches