diff --git a/src/full/java/com/topjohnwu/magisk/FlashActivity.java b/src/full/java/com/topjohnwu/magisk/FlashActivity.java index e7bccc0a3..3acb070b2 100644 --- a/src/full/java/com/topjohnwu/magisk/FlashActivity.java +++ b/src/full/java/com/topjohnwu/magisk/FlashActivity.java @@ -113,13 +113,15 @@ public class FlashActivity extends Activity { case Const.Value.FLASH_ZIP: new FlashZip(this, uri, console, logs).exec(); break; - case Const.Value.PATCH_BOOT: - new InstallMagisk(this, console, logs, uri, (Uri) intent.getParcelableExtra(Const.Key.FLASH_SET_BOOT)) - .exec(); - break; case Const.Value.FLASH_MAGISK: - new InstallMagisk(this, console, logs, uri, intent.getStringExtra(Const.Key.FLASH_SET_BOOT)) - .exec(); + new InstallMagisk(this, console, logs, uri, InstallMagisk.DIRECT_MODE).exec(); + break; + case Const.Value.FLASH_SECOND_SLOT: + new InstallMagisk(this, console, logs, uri, InstallMagisk.SECOND_SLOT_MODE).exec(); + break; + case Const.Value.PATCH_BOOT: + new InstallMagisk(this, console, logs, uri, + intent.getParcelableExtra(Const.Key.FLASH_SET_BOOT)).exec(); break; } } diff --git a/src/full/java/com/topjohnwu/magisk/MagiskManager.java b/src/full/java/com/topjohnwu/magisk/MagiskManager.java index 9421c0675..16ea4f552 100644 --- a/src/full/java/com/topjohnwu/magisk/MagiskManager.java +++ b/src/full/java/com/topjohnwu/magisk/MagiskManager.java @@ -59,7 +59,6 @@ public class MagiskManager extends Application implements Shell.Container { public String remoteManagerVersionString; public int remoteManagerVersionCode = -1; public String managerLink; - public String bootBlock = null; public boolean keepVerity = false; public boolean keepEnc = false; @@ -193,8 +192,6 @@ public class MagiskManager extends Application implements Shell.Container { "resetprop -p " : "getprop ") + Const.MAGISKHIDE_PROP); magiskHide = s == null || Integer.parseInt(s) != 0; } catch (Exception ignored) {} - - bootBlock = ShellUtils.fastCmd("echo \"$BOOTIMAGE\""); } public void getDefaultInstallFlags() { diff --git a/src/full/java/com/topjohnwu/magisk/SplashActivity.java b/src/full/java/com/topjohnwu/magisk/SplashActivity.java index d05297286..2bb1e3c50 100644 --- a/src/full/java/com/topjohnwu/magisk/SplashActivity.java +++ b/src/full/java/com/topjohnwu/magisk/SplashActivity.java @@ -60,8 +60,6 @@ public class SplashActivity extends Activity { mm.setupUpdateCheck(); // Fire asynctasks loadModuleTask.exec(); - // Check dtbo status - RootUtils.patchDTBO(); } // Write back default values diff --git a/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index 9708ad8f8..31c61459b 100644 --- a/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -39,8 +39,9 @@ import java.util.List; public class InstallMagisk extends ParallelTask { private static final int PATCH_MODE = 0; - private static final int DIRECT_MODE = 1; + public static final int DIRECT_MODE = 1; private static final int FIX_ENV_MODE = 2; + public static final int SECOND_SLOT_MODE = 3; private Uri bootUri, mZip; private List console, logs; @@ -57,22 +58,16 @@ public class InstallMagisk extends ParallelTask { mode = FIX_ENV_MODE; } - private InstallMagisk(Activity context, List console, List logs, Uri zip) { + public InstallMagisk(Activity context, List console, List logs, Uri zip, int mode) { this(context, zip); this.console = console; this.logs = logs; + this.mode = mode; } public InstallMagisk(FlashActivity context, List console, List logs, Uri zip, Uri boot) { - this(context, console, logs, zip); + this(context, console, logs, zip, PATCH_MODE); bootUri = boot; - mode = PATCH_MODE; - } - - public InstallMagisk(FlashActivity context, List console, List logs, Uri zip, String boot) { - this(context, console, logs, zip); - mBoot = boot; - mode = DIRECT_MODE; } @Override @@ -209,6 +204,7 @@ public class InstallMagisk extends ParallelTask { console.add(" " + dest + " "); console.add("*********************************"); break; + case SECOND_SLOT_MODE: case DIRECT_MODE: String binPath = mm.remoteMagiskVersionCode >= Const.MAGISK_VER.HIDDEN_PATH ? "/data/adb/magisk" : "/data/magisk"; @@ -237,11 +233,32 @@ public class InstallMagisk extends ParallelTask { installDir.mkdirs(); } - if (mode == PATCH_MODE) { - mBoot = new File(installDir, "boot.img").getAbsolutePath(); - if (!dumpBoot()) - return false; + switch (mode) { + case PATCH_MODE: + mBoot = new File(installDir, "boot.img").getAbsolutePath(); + if (!dumpBoot()) + return false; + break; + case DIRECT_MODE: + console.add("- Detecting ramdisk/boot image"); + mBoot = ShellUtils.fastCmd("find_boot_image", "echo \"$BOOTIMAGE\""); + break; + case SECOND_SLOT_MODE: + console.add("- Detecting ramdisk/boot image"); + char slot[] = ShellUtils.fastCmd("echo $SLOT").toCharArray(); + if (slot[1] == 'a') slot[1] = 'b'; + else slot[1] = 'a'; + mBoot = ShellUtils.fastCmd("SLOT=" + String.valueOf(slot), + "find_boot_image", "echo \"$BOOTIMAGE\""); + Shell.Async.su("mount_partitions"); + break; } + if (mBoot == null) { + console.add("- Unable to detect ramdisk/boot image"); + return false; + } + + console.add("- Use ramdisk/boot image: " + mBoot); List abis = Arrays.asList(Build.SUPPORTED_ABIS); String arch; diff --git a/src/full/java/com/topjohnwu/magisk/receivers/BootReceiver.java b/src/full/java/com/topjohnwu/magisk/receivers/BootReceiver.java index 189ed45e1..6dd45497f 100644 --- a/src/full/java/com/topjohnwu/magisk/receivers/BootReceiver.java +++ b/src/full/java/com/topjohnwu/magisk/receivers/BootReceiver.java @@ -9,7 +9,8 @@ import com.topjohnwu.magisk.services.OnBootIntentService; public class BootReceiver extends BroadcastReceiver { - private void startIntentService(Context context) { + @Override + public void onReceive(Context context, Intent intent) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(new Intent(context, OnBootIntentService.class)); } else { @@ -17,9 +18,4 @@ public class BootReceiver extends BroadcastReceiver { } } - @Override - public void onReceive(Context context, Intent intent) { - startIntentService(context); - } - } diff --git a/src/full/java/com/topjohnwu/magisk/services/OnBootIntentService.java b/src/full/java/com/topjohnwu/magisk/services/OnBootIntentService.java index 0e2a6dfbf..c4204c3ea 100644 --- a/src/full/java/com/topjohnwu/magisk/services/OnBootIntentService.java +++ b/src/full/java/com/topjohnwu/magisk/services/OnBootIntentService.java @@ -9,8 +9,6 @@ import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.RootUtils; -import com.topjohnwu.magisk.utils.Utils; -import com.topjohnwu.superuser.Shell; public class OnBootIntentService extends IntentService { @@ -22,12 +20,12 @@ public class OnBootIntentService extends IntentService { public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationCompat.Builder builder = - new NotificationCompat.Builder(this, Const.ID.NOTIFICATION_CHANNEL); - builder.setSmallIcon(R.drawable.ic_magisk_outline) - .setContentTitle("onBoot") - .setContentText("Running onBoot operations..."); - startForeground(Const.ID.ONBOOT_NOTIFICATION_ID, builder.build()); + startForeground(Const.ID.ONBOOT_NOTIFICATION_ID, + new NotificationCompat.Builder(this, Const.ID.NOTIFICATION_CHANNEL) + .setSmallIcon(R.drawable.ic_magisk_outline) + .setContentTitle("onBoot") + .setContentText("Running onBoot operations...") + .build()); } } @@ -40,11 +38,7 @@ public class OnBootIntentService extends IntentService { * Check for dtbo status every boot time, and prompt user * to reboot if dtbo wasn't patched and patched by Magisk Manager. * */ - MagiskManager mm = Utils.getMagiskManager(this); - mm.loadMagiskInfo(); - mm.getDefaultInstallFlags(); - if (Shell.rootAccess()) { - RootUtils.patchDTBO(); - } + MagiskManager.get().loadMagiskInfo(); + RootUtils.patchDTBO(); } } diff --git a/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java b/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java index 614fc544f..00729a04d 100644 --- a/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java +++ b/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java @@ -8,16 +8,18 @@ import com.topjohnwu.superuser.io.SuFile; public class RootUtils { public static void init() { - Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk"); - SuFile file = new SuFile("/sbin/.core/img"); - if (file.exists()) { - Const.MAGISK_PATH = file; - } else if ((file = new SuFile("/dev/magisk/img")).exists()) { - Const.MAGISK_PATH = file; - } else { - Const.MAGISK_PATH = new SuFile("/magisk"); + if (Shell.rootAccess()) { + Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk"); + SuFile file = new SuFile("/sbin/.core/img"); + if (file.exists()) { + Const.MAGISK_PATH = file; + } else if ((file = new SuFile("/dev/magisk/img")).exists()) { + Const.MAGISK_PATH = file; + } else { + Const.MAGISK_PATH = new SuFile("/magisk"); + } + Const.MAGISK_HOST_FILE = new SuFile(Const.MAGISK_PATH + "/.core/hosts"); } - Const.MAGISK_HOST_FILE = new SuFile(Const.MAGISK_PATH + "/.core/hosts"); } public static void uninstallPkg(String pkg) { @@ -25,10 +27,11 @@ public class RootUtils { } public static void patchDTBO() { - MagiskManager mm = MagiskManager.get(); - if (mm.magiskVersionCode >= Const.MAGISK_VER.DTBO_SUPPORT && !mm.keepVerity) { - if (ShellUtils.fastCmdResult(Shell.getShell(), "patch_dtbo_image")) { - ShowUI.dtboPatchedNotification(); + if (Shell.rootAccess()) { + MagiskManager mm = MagiskManager.get(); + if (mm.magiskVersionCode >= Const.MAGISK_VER.DTBO_SUPPORT) { + if (Boolean.parseBoolean(ShellUtils.fastCmd("mm_patch_dtbo"))) + ShowUI.dtboPatchedNotification(); } } } diff --git a/src/full/java/com/topjohnwu/magisk/utils/ShellInitializer.java b/src/full/java/com/topjohnwu/magisk/utils/ShellInitializer.java index f11d6a324..c30da58c0 100644 --- a/src/full/java/com/topjohnwu/magisk/utils/ShellInitializer.java +++ b/src/full/java/com/topjohnwu/magisk/utils/ShellInitializer.java @@ -23,8 +23,6 @@ public class ShellInitializer extends Shell.Initializer { } shell.run(null, null, "mount_partitions", - "find_boot_image", - "find_dtbo_image", "get_flags", "run_migrations"); return true; diff --git a/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java b/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java index 43d97a5ea..aeac6a5c6 100644 --- a/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java +++ b/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java @@ -137,17 +137,15 @@ public class ShowUI { if (Shell.rootAccess()) { options.add(mm.getString(R.string.direct_install)); } - String s = ShellUtils.fastCmd("echo $SLOT"); - if (s != null) { + String s = ShellUtils.fastCmd("grep_prop ro.build.ab_update"); + if (s != null && Boolean.parseBoolean(s)) { options.add(mm.getString(R.string.install_second_slot)); } - char[] slot = s == null ? null : s.toCharArray(); new AlertDialog.Builder(activity) .setTitle(R.string.select_method) .setItems( options.toArray(new String [0]), (dialog, idx) -> { - String boot; DownloadReceiver receiver = null; switch (idx) { case 1: @@ -191,41 +189,23 @@ public class ShowUI { }; break; case 2: - boot = mm.bootBlock; - if (boot == null) - return; receiver = new DownloadReceiver() { @Override public void onDownloadDone(Context context, Uri uri) { Intent intent = new Intent(mm, FlashActivity.class); - intent.setData(uri) - .putExtra(Const.Key.FLASH_SET_BOOT, boot) - .putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_MAGISK); + intent.setData(uri).putExtra(Const.Key.FLASH_ACTION, + Const.Value.FLASH_MAGISK); activity.startActivity(intent); } }; break; case 3: - assert (slot != null); - // Choose the other slot - if (slot[1] == 'a') slot[1] = 'b'; - else slot[1] = 'a'; - // Then find the boot image again - boot = ShellUtils.fastCmd( - "SLOT=" + String.valueOf(slot), - "find_boot_image", - "echo \"$BOOTIMAGE\"" - ); - Shell.Async.su("mount_partitions"); - if (boot == null) - return; receiver = new DownloadReceiver() { @Override public void onDownloadDone(Context context, Uri uri) { Intent intent = new Intent(mm, FlashActivity.class); - intent.setData(uri) - .putExtra(Const.Key.FLASH_SET_BOOT, boot) - .putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_MAGISK); + intent.setData(uri).putExtra(Const.Key.FLASH_ACTION, + Const.Value.FLASH_SECOND_SLOT); activity.startActivity(intent); } }; diff --git a/src/full/res/raw/utils.sh b/src/full/res/raw/utils.sh index 15c5e103f..9d6e32ecf 100644 --- a/src/full/res/raw/utils.sh +++ b/src/full/res/raw/utils.sh @@ -39,7 +39,16 @@ db_setup() { env_check() { for file in busybox magisk magiskboot magiskinit util_functions.sh boot_patch.sh; do - [ -e /data/adb/magisk/$file ] || return 1 + [ -f /data/adb/magisk/$file ] || return 1 done return 0 } + +mm_patch_dtbo() { + if $KEEPVERITY; then + echo false + else + find_dtbo_image + patch_dtbo_image >/dev/null 2>&1 && echo true || echo false + fi +} diff --git a/src/main/java/com/topjohnwu/magisk/utils/Const.java b/src/main/java/com/topjohnwu/magisk/utils/Const.java index 5081d03d0..528a6689e 100644 --- a/src/main/java/com/topjohnwu/magisk/utils/Const.java +++ b/src/main/java/com/topjohnwu/magisk/utils/Const.java @@ -153,6 +153,7 @@ public class Const { public static final String FLASH_ZIP = "flash"; public static final String PATCH_BOOT = "patch"; public static final String FLASH_MAGISK = "magisk"; + public static final String FLASH_SECOND_SLOT = "slot"; public static final int[] timeoutList = {0, -1, 10, 20, 30, 60}; public static final int ORDER_NAME = 0; public static final int ORDER_DATE = 1;