From c38533e0f83865f2c8f19b2c916085ab02c23017 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 7 Aug 2018 04:41:48 +0800 Subject: [PATCH] Prevent problematic modules causing device stuck in bootloop If boot failed after 2 times, it will enable core only mode (which disables all modules) --- native/jni/core/bootstages.c | 25 +++++++++++++++++++++++++ native/jni/include/magisk.h | 1 + 2 files changed, 26 insertions(+) diff --git a/native/jni/core/bootstages.c b/native/jni/core/bootstages.c index 737606ea0..a9806a44b 100644 --- a/native/jni/core/bootstages.c +++ b/native/jni/core/bootstages.c @@ -529,6 +529,20 @@ void startup() { unblock_boot_process(); } + // Increment boot count + int boot_count = 0; + FILE *cf = xfopen(BOOTCOUNT, "r"); + if (cf) { + fscanf(cf, "%d", &boot_count); + fclose(cf); + } + boot_count++; + if (boot_count > 2) + creat(DISABLEFILE, 0644); + cf = xfopen(BOOTCOUNT, "w"); + fprintf(cf, "%d", boot_count); + fclose(cf); + // No uninstaller or core-only mode if (access(DISABLEFILE, F_OK) != 0) { simple_mount("/system"); @@ -868,4 +882,15 @@ core_only: // All boot stage done, cleanup vec_deep_destroy(&module_list); + + // Wait for boot complete, and clear boot count + while (1) { + char *prop = getprop("sys.boot_completed"); + if (prop != NULL && strcmp(prop, "1") == 0) { + free(prop); + unlink(BOOTCOUNT); + break; + } + sleep(2); + } } diff --git a/native/jni/include/magisk.h b/native/jni/include/magisk.h index 19f4f01bb..752c27b85 100644 --- a/native/jni/include/magisk.h +++ b/native/jni/include/magisk.h @@ -32,6 +32,7 @@ #define DATABIN SECURE_DIR "/magisk" #define MAGISKDB SECURE_DIR "/magisk.db" #define SIMPLEMOUNT SECURE_DIR "/magisk_simple" +#define BOOTCOUNT SECURE_DIR "/.boot_count" #define MANAGERAPK DATABIN "/magisk.apk" #define MAGISKRC "/init.magisk.rc"