Bring back installing to second slot after OTA on A/B devices

This commit is contained in:
topjohnwu 2018-07-21 01:59:28 +08:00
parent 4ffc388491
commit 07140d33a7
4 changed files with 49 additions and 11 deletions

View File

@ -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<Void, Void, Boolean> {
}
}
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<Void, Void, Boolean> {
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<Void, Void, Boolean> {
return false;
}
console.add("- Target image: " + mBoot);
if (mode == DIRECT_MODE || mode == SECOND_SLOT_MODE)
console.add("- Target image: " + mBoot);
List<String> abis = Arrays.asList(Build.SUPPORTED_ABIS);
String arch;
@ -284,6 +304,8 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
if (patched == null)
return false;
outputBoot(patched);
if (mode == SECOND_SLOT_MODE)
postOTA();
console.add("- All done!");
}
} catch (Exception e) {

View File

@ -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(

Binary file not shown.

View File

@ -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 /
}