From aced0632ec66638eccf7f1fa7f810d4a2ac265c4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 11 Jun 2017 16:51:44 +0800 Subject: [PATCH] Improve image merging --- jni/daemon/bootstages.c | 34 +++++++++++++++++++++---------- jni/utils/misc.c | 45 ++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/jni/daemon/bootstages.c b/jni/daemon/bootstages.c index 5de9fb8a7..f58d07f3a 100644 --- a/jni/daemon/bootstages.c +++ b/jni/daemon/bootstages.c @@ -102,14 +102,14 @@ static int merge_img(const char *source, const char *target) { // resize target to worst case int s_used, s_total, t_used, t_total, n_total; - get_img_size(source, &s_used, &s_total); - get_img_size(target, &t_used, &t_total); + if (get_img_size(source, &s_used, &s_total)) return 1; + if (get_img_size(target, &t_used, &t_total)) return 1; n_total = round_size(s_used + t_used); if (n_total != t_total && resize_img(target, n_total)) return 1; - xmkdir("/cache/source", 0755); - xmkdir("/cache/target", 0755); + mkdir("/cache/source", 0755); + mkdir("/cache/target", 0755); char *s_loop, *t_loop; s_loop = mount_image(source, "/cache/source"); if (s_loop == NULL) return 1; @@ -130,8 +130,10 @@ static int merge_img(const char *source, const char *target) { // Cleanup old module snprintf(buf, PATH_MAX, "/cache/target/%s", entry->d_name); if (access(buf, F_OK) == 0) { - LOGI("merge module %s\n", entry->d_name); + LOGI("Upgrade module: %s\n", entry->d_name); rm_rf(buf); + } else { + LOGI("New module: %s\n", entry->d_name); } } } @@ -523,19 +525,23 @@ void post_fs_data(int client) { } // Magisk Manual Injector support - if (access("/data/local/tmp/magisk", F_OK) == 0) { + if (access("/data/local/tmp/magisk_inject", F_OK) == 0) { rm_rf(DATABIN); - rename("/data/local/tmp/magisk", DATABIN); + rename("/data/local/tmp/magisk_inject", DATABIN); } // Lazy.... use shell blob system("mv /data/magisk/stock_boot* /data;"); // Merge images - if (merge_img("/cache/magisk.img", MAINIMG)) + if (merge_img("/cache/magisk.img", MAINIMG)) { + LOGE("Image merge %s -> %s failed!\n", "/cache/magisk.img", MAINIMG); goto unblock; - if (merge_img("/data/magisk_merge.img", MAINIMG)) + } + if (merge_img("/data/magisk_merge.img", MAINIMG)) { + LOGE("Image merge %s -> %s failed!\n", "/data/magisk_merge.img", MAINIMG); goto unblock; + } int new_img = 0; @@ -735,12 +741,19 @@ void late_start(int client) { // Run scripts after full patch, most reliable way to run scripts LOGI("* Running service.d scripts\n"); exec_common_script("service"); + + // Core only mode + if (access(DISABLEFILE, F_OK) == 0) { + setprop("ro.magisk.disable", "1"); + return; + } + LOGI("* Running module service scripts\n"); exec_module_script("service"); // Install Magisk Manager if exists if (access(MANAGERAPK, F_OK) == 0) { - while(1) { + while (1) { sleep(5); char *const command[] = { "sh", "-c", "CLASSPATH=/system/framework/pm.jar " @@ -765,7 +778,6 @@ void late_start(int client) { #ifdef DEBUG // Stop recording the boot logcat after every boot task is done - extern int debug_log_pid, debug_log_fd; kill(debug_log_pid, SIGTERM); waitpid(debug_log_pid, NULL, 0); close(debug_log_fd); diff --git a/jni/utils/misc.c b/jni/utils/misc.c index 1db66664c..a0765a257 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -277,11 +277,11 @@ int mkdir_p(const char *pathname, mode_t mode) { int bind_mount(const char *from, const char *to) { int ret = xmount(from, to, NULL, MS_BIND, NULL); - #ifdef DEBUG +#ifdef DEBUG LOGD("bind_mount: %s -> %s\n", from, to); - #else +#else LOGI("bind_mount: %s\n", to); - #endif +#endif return ret; } @@ -431,32 +431,39 @@ int get_img_size(const char *img, int *used, int *total) { if (access(img, R_OK) == -1) return 1; char buffer[PATH_MAX]; - snprintf(buffer, sizeof(buffer), "e2fsck -n %s 2>/dev/null | tail -n 1", img); + snprintf(buffer, sizeof(buffer), "e2fsck -yf %s", img); char *const command[] = { "sh", "-c", buffer, NULL }; - int pid, fd = 0; - pid = run_command(0, &fd, "/system/bin/sh", command); - fdgets(buffer, sizeof(buffer), fd); - close(fd); + int pid, fd = 0, ret = 1; + pid = run_command(1, &fd, "/system/bin/sh", command); if (pid == -1) return 1; - waitpid(pid, NULL, 0); - char *tok; - tok = strtok(buffer, ","); - while(tok != NULL) { - if (strstr(tok, "blocks")) + while (fdgets(buffer, sizeof(buffer), fd)) { + if (strstr(buffer, img)) { + char *tok; + tok = strtok(buffer, ","); + while(tok != NULL) { + if (strstr(tok, "blocks")) { + ret = 0; + break; + } + tok = strtok(NULL, ","); + } + if (ret) continue; + sscanf(tok, "%d/%d", used, total); + *used = *used / 256 + 1; + *total /= 256; break; - tok = strtok(NULL, ","); + } } - sscanf(tok, "%d/%d", used, total); - *used = *used / 256 + 1; - *total /= 256; - return 0; + close(fd); + waitpid(pid, NULL, 0); + return ret; } int resize_img(const char *img, int size) { LOGI("Resize %s to %dM\n", img, size); char buffer[PATH_MAX]; - snprintf(buffer, PATH_MAX, "e2fsck -yf %s; resize2fs %s %dM;", img, img, size); + snprintf(buffer, PATH_MAX, "resize2fs %s %dM;", img, size); return system(buffer); }