diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index 988d37632..82f7d877a 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -17,7 +17,9 @@ import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.ShellUtils; +import com.topjohnwu.superuser.io.SuFile; import com.topjohnwu.superuser.io.SuFileInputStream; +import com.topjohnwu.superuser.io.SuFileOutputStream; import com.topjohnwu.utils.SignBoot; import org.kamranzafar.jtar.TarInputStream; @@ -214,6 +216,20 @@ public class InstallMagisk extends ParallelTask { } } + private void postOTA() { + SuFile bootctl = new SuFile(Const.MAGISK_PATH + "/.core/bootctl"); + try (InputStream in = mm.getResources().openRawResource(R.raw.bootctl); + OutputStream out = new SuFileOutputStream(bootctl)) { + ShellUtils.pump(in, out); + Shell.Sync.su("post_ota " + bootctl.getParent()); + console.add("***************************************"); + console.add(" Next reboot will boot to second slot!"); + console.add("***************************************"); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Override protected Boolean doInBackground(Void... voids) { if (mode == FIX_ENV_MODE) { @@ -240,13 +256,16 @@ public class InstallMagisk extends ParallelTask { mBoot = ShellUtils.fastCmd("find_boot_image", "echo \"$BOOTIMAGE\""); break; case SECOND_SLOT_MODE: + String slot = ShellUtils.fastCmd("echo $SLOT"); + String target = (TextUtils.equals(slot, "_a") ? "_b" : "_a"); + console.add("- Target slot: " + target); console.add("- Detecting target 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"); + mBoot = ShellUtils.fastCmd( + "SLOT=" + target, + "find_boot_image", + "SLOT=" + slot, + "echo \"$BOOTIMAGE\"" + ); break; case FIX_ENV_MODE: mBoot = ""; @@ -257,7 +276,8 @@ public class InstallMagisk extends ParallelTask { return false; } - console.add("- Target image: " + mBoot); + if (mode == DIRECT_MODE || mode == SECOND_SLOT_MODE) + console.add("- Target image: " + mBoot); List abis = Arrays.asList(Build.SUPPORTED_ABIS); String arch; @@ -284,6 +304,8 @@ public class InstallMagisk extends ParallelTask { if (patched == null) return false; outputBoot(patched); + if (mode == SECOND_SLOT_MODE) + postOTA(); console.add("- All done!"); } } catch (Exception e) { diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java b/app/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java index 3add96f34..66f95ed76 100644 --- a/app/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java +++ b/app/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java @@ -26,6 +26,7 @@ import com.topjohnwu.magisk.receivers.DownloadReceiver; import com.topjohnwu.magisk.receivers.ManagerUpdate; import com.topjohnwu.magisk.receivers.RebootReceiver; import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellUtils; import java.util.ArrayList; import java.util.List; @@ -134,6 +135,10 @@ public class ShowUI { if (Shell.rootAccess()) { options.add(mm.getString(R.string.direct_install)); } + 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)); + } new AlertDialog.Builder(activity) .setTitle(R.string.select_method) .setItems( diff --git a/app/src/full/res/raw/bootctl b/app/src/full/res/raw/bootctl new file mode 100644 index 000000000..74f359ada Binary files /dev/null and b/app/src/full/res/raw/bootctl differ diff --git a/app/src/full/res/raw/utils.sh b/app/src/full/res/raw/utils.sh index e88ab9b03..ecdfa9ac6 100644 --- a/app/src/full/res/raw/utils.sh +++ b/app/src/full/res/raw/utils.sh @@ -73,11 +73,11 @@ mm_patch_dtbo() { } restore_imgs() { - SHA1=`cat /.backup/.sha1` - [ -z $SHA1 ] && SHA1=`grep_prop #STOCKSHA1` + local SHA1=`cat /.backup/.sha1` + [ -z $SHA1 ] && local SHA1=`grep_prop #STOCKSHA1` [ -z $SHA1 ] && return 1 - STOCKBOOT=/data/stock_boot_${SHA1}.img.gz - STOCKDTBO=/data/stock_dtbo.img.gz + local STOCKBOOT=/data/stock_boot_${SHA1}.img.gz + local STOCKDTBO=/data/stock_dtbo.img.gz [ -f $STOCKBOOT ] || return 1 find_boot_image @@ -93,3 +93,14 @@ restore_imgs() { fi return 1 } + +post_ota() { + cd $1 + chmod 755 bootctl + ./bootctl hal-info || return + [ `./bootctl get-current-slot` -eq 0 ] && SLOT_NUM=1 || SLOT_NUM=0 + ./bootctl set-active-boot-slot $SLOT_NUM + echo '${0%/*}/../bootctl mark-boot-successful;rm -f ${0%/*}/../bootctl $0' > post-fs-data.d/post_ota.sh + chmod 755 post-fs-data.d/post_ota.sh + cd / +}