Support fixing Magisk environment

This commit is contained in:
topjohnwu 2018-05-13 18:14:10 +08:00
parent dde0a4a7c8
commit 630f2b7d19
10 changed files with 308 additions and 186 deletions

View File

@ -8,7 +8,7 @@ android {
applicationId "com.topjohnwu.magisk" applicationId "com.topjohnwu.magisk"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion rootProject.ext.compileSdkVersion targetSdkVersion rootProject.ext.compileSdkVersion
versionCode 115 versionCode 117
versionName "5.7.0" versionName "5.7.0"
javaCompileOptions { javaCompileOptions {
annotationProcessorOptions { annotationProcessorOptions {
@ -46,7 +46,7 @@ dependencies {
implementation "com.android.support:design:${rootProject.ext.supportLibVersion}" implementation "com.android.support:design:${rootProject.ext.supportLibVersion}"
implementation "com.android.support:support-v4:${rootProject.ext.supportLibVersion}" implementation "com.android.support:support-v4:${rootProject.ext.supportLibVersion}"
implementation 'com.jakewharton:butterknife:8.8.1' implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'com.atlassian.commonmark:commonmark:0.10.0' implementation 'com.atlassian.commonmark:commonmark:0.11.0'
implementation 'org.kamranzafar:jtar:2.3' implementation 'org.kamranzafar:jtar:2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
} }

View File

@ -259,17 +259,22 @@ public class MagiskFragment extends Fragment
} }
} }
if (!shownDialog && (mm.remoteMagiskVersionCode > mm.magiskVersionCode
|| mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE)) {
install();
}
magiskUpdateIcon.setImageResource(image); magiskUpdateIcon.setImageResource(image);
magiskUpdateIcon.setColorFilter(color); magiskUpdateIcon.setColorFilter(color);
magiskUpdateIcon.setVisibility(View.VISIBLE); magiskUpdateIcon.setVisibility(View.VISIBLE);
magiskUpdateProgress.setVisibility(View.GONE); magiskUpdateProgress.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);
if (!shownDialog) {
if (mm.remoteMagiskVersionCode > mm.magiskVersionCode
|| mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
install();
} else if (mm.remoteMagiskVersionCode >= Const.MAGISK_VER.FIX_ENV &&
!Utils.cmdResult("env_check")) {
ShowUI.envFixDialog(getActivity());
}
}
} }
private void updateSafetyNetUI(int response) { private void updateSafetyNetUI(int response) {

View File

@ -105,10 +105,11 @@ public class MagiskManager extends Shell.ContainerApp {
Shell.setInitializer(new Shell.Initializer() { Shell.setInitializer(new Shell.Initializer() {
@Override @Override
public void onRootShellInit(@NonNull Shell shell) { public void onRootShellInit(@NonNull Shell shell) {
try (InputStream utils = getAssets().open(Const.UTIL_FUNCTIONS); try (InputStream magiskUtils = getAssets().open(Const.UTIL_FUNCTIONS);
InputStream magiskDB = getResources().openRawResource(R.raw.magiskdb)) { InputStream managerUtils = getResources().openRawResource(R.raw.utils)
shell.loadInputStream(null, null, utils); ) {
shell.loadInputStream(null, null, magiskDB); shell.loadInputStream(null, null, magiskUtils);
shell.loadInputStream(null, null, managerUtils);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -1,13 +1,16 @@
package com.topjohnwu.magisk.asyncs; package com.topjohnwu.magisk.asyncs;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.widget.Toast;
import com.topjohnwu.magisk.FlashActivity; import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.container.TarEntry; import com.topjohnwu.magisk.container.TarEntry;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
@ -30,6 +33,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.AbstractList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -37,61 +41,52 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
private static final int PATCH_MODE = 0; private static final int PATCH_MODE = 0;
private static final int DIRECT_MODE = 1; private static final int DIRECT_MODE = 1;
private static final int FIX_ENV_MODE = 2;
private Uri mBootImg, mZip; private Uri mBootImg, mZip;
private List<String> console, logs; private List<String> console, logs;
private String mBootLocation; private String mBootLocation;
private int mode; private int mode;
private File install; private File install;
private ProgressDialog dialog;
private boolean highCompression;
private InstallMagisk(Activity context, List<String> console, List<String> logs, Uri zip) { public InstallMagisk(Activity context, Uri zip) {
super(context); super(context);
this.console = console;
this.logs = logs;
mZip = zip; mZip = zip;
mode = FIX_ENV_MODE;
} }
public InstallMagisk(Activity context, List<String> console, List<String> logs, Uri zip, Uri boot) { private InstallMagisk(Activity context, List<String> console, List<String> logs, Uri zip) {
this(context, zip);
this.console = console;
this.logs = logs;
}
public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri zip, Uri boot) {
this(context, console, logs, zip); this(context, console, logs, zip);
mBootImg = boot; mBootImg = boot;
highCompression = false;
mode = PATCH_MODE; mode = PATCH_MODE;
} }
public InstallMagisk(Activity context, List<String> console, List<String> logs, Uri zip, String boot) { public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri zip, String boot) {
this(context, console, logs, zip); this(context, console, logs, zip);
mBootLocation = boot; mBootLocation = boot;
mode = DIRECT_MODE; mode = DIRECT_MODE;
} }
@Override @Override
protected Boolean doInBackground(Void... voids) { protected void onPreExecute() {
MagiskManager mm = MagiskManager.get(); if (mode == FIX_ENV_MODE) {
dialog = ProgressDialog.show(getActivity(),
install = new File( "Additional Setup", "Running environment setup...");
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? console = new NOPList<>();
mm.createDeviceProtectedStorageContext() : mm) }
.getFilesDir().getParent()
, "install");
Shell.Sync.sh("rm -rf " + install);
List<String> abis = Arrays.asList(Build.SUPPORTED_ABIS);
String arch;
if (mm.remoteMagiskVersionCode >= Const.MAGISK_VER.SEPOL_REFACTOR) {
// 32-bit only
if (abis.contains("x86")) arch = "x86";
else arch = "arm";
} else {
if (abis.contains("x86_64")) arch = "x64";
else if (abis.contains("arm64-v8a")) arch = "arm64";
else if (abis.contains("x86")) arch = "x86";
else arch = "arm";
} }
console.add("- Device platform: " + Build.SUPPORTED_ABIS[0]); private void extractFiles(String arch) throws IOException {
MagiskManager mm = MagiskManager.get();
try {
// Unzip files
console.add("- Extracting files"); console.add("- Extracting files");
try (InputStream in = mm.getContentResolver().openInputStream(mZip)) { try (InputStream in = mm.getContentResolver().openInputStream(mZip)) {
if (in == null) throw new FileNotFoundException(); if (in == null) throw new FileNotFoundException();
@ -108,14 +103,16 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
console.add("! Invalid Uri"); console.add("! Invalid Uri");
throw e; throw e;
} catch (Exception e) { } catch (IOException e) {
console.add("! Cannot unzip zip"); console.add("! Cannot unzip zip");
throw e; throw e;
} }
Shell.Sync.sh("chmod 755 " + install + "/*"); Shell.Sync.sh(Utils.fmt("chmod -R 755 %s/*; %s/magiskinit -x magisk %s/magisk",
install, install, install));
}
File boot = new File(install, "boot.img"); private boolean dumpBoot(File boot) throws IOException {
boolean highCompression = false; MagiskManager mm = MagiskManager.get();
switch (mode) { switch (mode) {
case PATCH_MODE: case PATCH_MODE:
// Copy boot image to local // Copy boot image to local
@ -166,6 +163,11 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
default: default:
return false; return false;
} }
return true;
}
private boolean patchBoot(File boot, SuFile patched_boot) throws IOException {
MagiskManager mm = MagiskManager.get();
boolean isSigned; boolean isSigned;
try (InputStream in = new FileInputStream(boot)) { try (InputStream in = new FileInputStream(boot)) {
@ -173,7 +175,7 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
if (isSigned) { if (isSigned) {
console.add("- Boot image is signed with AVB 1.0"); console.add("- Boot image is signed with AVB 1.0");
} }
} catch (Exception e) { } catch (IOException e) {
console.add("! Unable to check signature"); console.add("! Unable to check signature");
throw e; throw e;
} }
@ -193,8 +195,6 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
"rm -rf magisk.apk bin *.img update-binary", "rm -rf magisk.apk bin *.img update-binary",
"cd /"); "cd /");
SuFile patched_boot = new SuFile(install.getParent(), "new-boot.img");
if (isSigned) { if (isSigned) {
console.add("- Signing boot image with test keys"); console.add("- Signing boot image with test keys");
File signed = new File(install.getParent(), "signed.img"); File signed = new File(install.getParent(), "signed.img");
@ -205,6 +205,11 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
} }
Shell.Sync.sh("mv -f " + signed + " " + patched_boot); Shell.Sync.sh("mv -f " + signed + " " + patched_boot);
} }
return true;
}
private void outputBoot(SuFile patched_boot) throws IOException {
MagiskManager mm = MagiskManager.get();
switch (mode) { switch (mode) {
case PATCH_MODE: case PATCH_MODE:
@ -232,21 +237,70 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
console.add("*********************************"); console.add("*********************************");
break; break;
case DIRECT_MODE: case DIRECT_MODE:
String binPath = mm.remoteMagiskVersionCode >= 1464 ? "/data/adb/magisk" : "/data/magisk"; String binPath = mm.remoteMagiskVersionCode >= Const.MAGISK_VER.HIDDEN_PATH ? "/data/adb/magisk" : "/data/magisk";
Shell.Sync.su(console, logs, Shell.Sync.su(console, logs,
Utils.fmt("rm -rf %s/*; mkdir -p %s; chmod 700 /data/adb", binPath, binPath), Utils.fmt("rm -rf %s/*; mkdir -p %s; chmod 700 /data/adb", binPath, binPath),
Utils.fmt("cp -af %s/* %s; rm -rf %s", install, binPath, install), Utils.fmt("cp -af %s/* %s; rm -rf %s", install, binPath, install),
Utils.fmt("flash_boot_image %s %s", patched_boot, mBootLocation), Utils.fmt("flash_boot_image %s %s", patched_boot, mBootLocation),
mm.remoteMagiskVersionCode >= 1464 ? "[ -L /data/magisk.img ] || cp /data/magisk.img /data/adb/magisk.img" : "",
mm.keepVerity ? "" : "patch_dtbo_image"); mm.keepVerity ? "" : "patch_dtbo_image");
break; break;
default: }
return false; patched_boot.delete();
} }
patched_boot.delete(); @Override
protected Boolean doInBackground(Void... voids) {
MagiskManager mm = MagiskManager.get();
if (mode == FIX_ENV_MODE) {
install = new File("/data/adb/magisk");
Shell.Sync.sh("rm -rf " + install + "/*");
} else {
install = new File(
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
mm.createDeviceProtectedStorageContext() : mm)
.getFilesDir().getParent()
, "install");
Shell.Sync.sh("rm -rf " + install);
}
List<String> abis = Arrays.asList(Build.SUPPORTED_ABIS);
String arch;
if (mm.remoteMagiskVersionCode >= Const.MAGISK_VER.SEPOL_REFACTOR) {
// 32-bit only
if (abis.contains("x86")) arch = "x86";
else arch = "arm";
} else {
if (abis.contains("x86_64")) arch = "x64";
else if (abis.contains("arm64-v8a")) arch = "arm64";
else if (abis.contains("x86")) arch = "x86";
else arch = "arm";
}
console.add("- Device platform: " + Build.SUPPORTED_ABIS[0]);
try {
extractFiles(arch);
if (mode == FIX_ENV_MODE) {
Shell.Sync.sh(
"cd " + install,
"sh update-binary extract",
"rm -f update-binary magisk.apk",
"cd /",
"rm -rf /sbin/.core/busybox/*",
"/sbin/.core/mirror/bin/busybox --install -s /sbin/.core/busybox"
);
} else {
File boot = new File(install, "boot.img");
SuFile patched_boot = new SuFile(install.getParent(), "new-boot.img");
if (!dumpBoot(boot) || !patchBoot(boot, patched_boot))
return false;
outputBoot(patched_boot);
console.add("- All done!"); console.add("- All done!");
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
@ -256,6 +310,11 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
@Override @Override
protected void onPostExecute(Boolean result) { protected void onPostExecute(Boolean result) {
if (mode == FIX_ENV_MODE) {
dialog.dismiss();
MagiskManager.toast(result ? R.string.setup_done : R.string.setup_fail, Toast.LENGTH_LONG);
} else {
// Running in FlashActivity
FlashActivity activity = (FlashActivity) getActivity(); FlashActivity activity = (FlashActivity) getActivity();
if (!result) { if (!result) {
Shell.Async.sh("rm -rf " + install); Shell.Async.sh("rm -rf " + install);
@ -265,3 +324,19 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
activity.buttonPanel.setVisibility(View.VISIBLE); activity.buttonPanel.setVisibility(View.VISIBLE);
} }
} }
private static class NOPList<E> extends AbstractList<E> {
@Override
public E get(int index) {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public void add(int index, E element) {}
}
}

View File

@ -80,6 +80,7 @@ public class Const {
public static final int HIDDEN_PATH = 1460; public static final int HIDDEN_PATH = 1460;
public static final int REMOVE_LEGACY_LINK = 1630; public static final int REMOVE_LEGACY_LINK = 1630;
public static final int SEPOL_REFACTOR = 1640; public static final int SEPOL_REFACTOR = 1640;
public static final int FIX_ENV = 1650;
} }
public static class ID { public static class ID {

View File

@ -17,6 +17,7 @@ import com.topjohnwu.magisk.FlashActivity;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.SplashActivity; import com.topjohnwu.magisk.SplashActivity;
import com.topjohnwu.magisk.asyncs.InstallMagisk;
import com.topjohnwu.magisk.asyncs.RestoreImages; import com.topjohnwu.magisk.asyncs.RestoreImages;
import com.topjohnwu.magisk.components.AlertDialogBuilder; import com.topjohnwu.magisk.components.AlertDialogBuilder;
import com.topjohnwu.magisk.receivers.DownloadReceiver; import com.topjohnwu.magisk.receivers.DownloadReceiver;
@ -100,6 +101,26 @@ public class ShowUI {
notificationManager.notify(Const.ID.DTBO_NOTIFICATION_ID, builder.build()); notificationManager.notify(Const.ID.DTBO_NOTIFICATION_ID, builder.build());
} }
public static void envFixDialog(Activity activity) {
MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.fmt("Magisk-v%s(%d).zip",
mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode);
new AlertDialogBuilder(activity)
.setTitle(R.string.env_fix_title)
.setMessage(R.string.env_fix_msg)
.setCancelable(true)
.setPositiveButton(R.string.yes, (d, i) -> {
Utils.dlAndReceive(activity, new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
new InstallMagisk(activity, uri).exec();
}
}, mm.magiskLink, filename);
})
.setNegativeButton(R.string.no_thanks, null)
.show();
}
public static void magiskInstallDialog(Activity activity) { public static void magiskInstallDialog(Activity activity) {
MagiskManager mm = Utils.getMagiskManager(activity); MagiskManager mm = Utils.getMagiskManager(activity);
String filename = Utils.fmt("Magisk-v%s(%d).zip", String filename = Utils.fmt("Magisk-v%s(%d).zip",

View File

@ -51,6 +51,10 @@ public class Utils {
return ShellUtils.fastCmd(Shell.getShell(), cmd); return ShellUtils.fastCmd(Shell.getShell(), cmd);
} }
public static boolean cmdResult(String cmd) {
return ShellUtils.fastCmdResult(Shell.getShell(), cmd);
}
public static void uninstallPkg(String pkg) { public static void uninstallPkg(String pkg) {
Shell.Sync.su("db_clean " + Const.USER_ID, "pm uninstall " + pkg); Shell.Sync.su("db_clean " + Const.USER_ID, "pm uninstall " + pkg);
} }

View File

@ -1,6 +1,8 @@
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.io.SuFile;
import com.topjohnwu.superuser.io.SuFileOutputStream;
import com.topjohnwu.utils.JarMap; import com.topjohnwu.utils.JarMap;
import com.topjohnwu.utils.SignAPK; import com.topjohnwu.utils.SignAPK;
@ -9,6 +11,7 @@ import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
@ -16,13 +19,13 @@ import java.util.zip.ZipInputStream;
public class ZipUtils { public class ZipUtils {
public static void unzip(File zip, File folder, String path, boolean junkPath) throws Exception { public static void unzip(File zip, File folder, String path, boolean junkPath) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(zip)); InputStream in = new BufferedInputStream(new FileInputStream(zip));
unzip(in, folder, path, junkPath); unzip(in, folder, path, junkPath);
in.close(); in.close();
} }
public static void unzip(InputStream zip, File folder, String path, boolean junkPath) throws Exception { public static void unzip(InputStream zip, File folder, String path, boolean junkPath) throws IOException {
try { try {
ZipInputStream zipfile = new ZipInputStream(zip); ZipInputStream zipfile = new ZipInputStream(zip);
ZipEntry entry; ZipEntry entry;
@ -37,13 +40,14 @@ public class ZipUtils {
} else { } else {
name = entry.getName(); name = entry.getName();
} }
File dest = new File(folder, name); SuFile dest = new SuFile(folder, name);
dest.getParentFile().mkdirs(); dest.getParentFile().mkdirs();
try (FileOutputStream out = new FileOutputStream(dest)) { try (OutputStream out = new SuFileOutputStream(dest)) {
ShellUtils.pump(zipfile, out); ShellUtils.pump(zipfile, out);
} }
} }
} catch(Exception e) { }
catch(IOException e) {
e.printStackTrace(); e.printStackTrace();
throw e; throw e;
} }

View File

@ -36,3 +36,10 @@ db_setup() {
chown $USER.$USER $DIR chown $USER.$USER $DIR
chmod 666 $DIR/* chmod 666 $DIR/*
} }
env_check() {
for file in busybox magisk magiskboot magiskinit util_functions.sh boot_patch.sh; do
[ -e /data/adb/magisk/$file ] || return 1
done
return 0
}

View File

@ -125,6 +125,10 @@
<string name="su_db_corrupt">SU database is corrupted, will recreate new db</string> <string name="su_db_corrupt">SU database is corrupted, will recreate new db</string>
<string name="cannot_check_sn_title">Cannot check SafetyNet</string> <string name="cannot_check_sn_title">Cannot check SafetyNet</string>
<string name="cannot_check_sn_notice">Due to some changes in Google Play Services, it is not possible to check SafetyNet on repackaged Magisk Manager</string> <string name="cannot_check_sn_notice">Due to some changes in Google Play Services, it is not possible to check SafetyNet on repackaged Magisk Manager</string>
<string name="setup_done">Setup done</string>
<string name="setup_fail">Setup failed</string>
<string name="env_fix_title">Require Additional Setup</string>
<string name="env_fix_msg">Your device need additional setup for Magisk to work properly. It will download the Magisk setup zip, do you want to proceed now?</string>
<!--Settings Activity --> <!--Settings Activity -->
<string name="settings_general_category">General</string> <string name="settings_general_category">General</string>