From 67f8dc494e9dbebb449752cf405e940a6db3d03f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 6 Oct 2020 00:40:57 -0700 Subject: [PATCH] Properly patch vbmeta.img Close #3241 --- app/build.gradle.kts | 5 +-- .../magisk/core/tasks/MagiskInstaller.kt | 34 ++++++++++++++----- build.gradle.kts | 2 +- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 524b5af10..c06475b97 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -82,6 +82,7 @@ dependencies { implementation("com.github.topjohnwu:jtar:1.0.0") implementation("com.github.topjohnwu:indeterminate-checkbox:1.0.7") + implementation("com.github.topjohnwu:lz4-java:1.7.1") implementation("com.jakewharton.timber:timber:4.7.1") val vBAdapt = "4.0.0" @@ -118,7 +119,7 @@ dependencies { implementation("com.squareup.okhttp3:logging-interceptor:${vOkHttp}") implementation("com.squareup.okhttp3:okhttp-dnsoverhttps:${vOkHttp}") - val vMoshi = "1.10.0" + val vMoshi = "1.11.0" implementation("com.squareup.moshi:moshi:${vMoshi}") kapt("com.squareup.moshi:moshi-kotlin-codegen:${vMoshi}") @@ -141,7 +142,7 @@ dependencies { implementation("androidx.work:work-runtime-ktx:2.4.0") implementation("androidx.transition:transition:1.3.1") implementation("androidx.multidex:multidex:2.0.1") - implementation("androidx.core:core-ktx:1.3.1") + implementation("androidx.core:core-ktx:1.3.2") implementation("androidx.localbroadcastmanager:localbroadcastmanager:1.0.0") implementation("com.google.android.material:material:1.2.1") } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index aa93771eb..a9de775ae 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -29,6 +29,7 @@ import com.topjohnwu.superuser.io.SuFileInputStream import com.topjohnwu.superuser.io.SuFileOutputStream import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import net.jpountz.lz4.LZ4FrameInputStream import org.kamranzafar.jtar.TarEntry import org.kamranzafar.jtar.TarHeader import org.kamranzafar.jtar.TarInputStream @@ -38,6 +39,7 @@ import org.koin.core.get import org.koin.core.inject import timber.log.Timber import java.io.* +import java.nio.ByteBuffer import java.util.zip.ZipEntry import java.util.zip.ZipInputStream @@ -97,8 +99,7 @@ abstract class MagiskInstallImpl : KoinComponent { @Suppress("DEPRECATION") private fun extractZip(): Boolean { - val arch: String - arch = if (Build.VERSION.SDK_INT >= 21) { + val arch = if (Build.VERSION.SDK_INT >= 21) { val abis = listOf(*Build.SUPPORTED_ABIS) if (abis.contains("x86")) "x86" else "arm" } else { @@ -163,21 +164,36 @@ abstract class MagiskInstallImpl : KoinComponent { val tarOut = TarOutputStream(output) TarInputStream(input).use { tarIn -> lateinit var entry: TarEntry + + fun decompressedStream() = + if (entry.name.contains(".lz4")) LZ4FrameInputStream(tarIn) else tarIn + while (tarIn.nextEntry?.let { entry = it } != null) { if (entry.name.contains("boot.img") || (Config.recovery && entry.name.contains("recovery.img"))) { - val name = entry.name + val name = entry.name.replace(".lz4", "") console.add("-- Extracting: $name") + val extract = File(installDir, name) - FileOutputStream(extract).use { tarIn.copyTo(it) } - if (name.contains(".lz4")) { - console.add("-- Decompressing: $name") - "./magiskboot decompress $extract".sh() + FileOutputStream(extract).use { decompressedStream().copyTo(it) } + } else if (entry.name.contains("vbmeta.img")) { + val rawData = ByteArrayOutputStream().let { + decompressedStream().copyTo(it) + it.toByteArray() } + // Valid vbmeta.img should be at least 256 bytes + if (rawData.size < 256) + continue + + // Patch flags to AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED + console.add("-- Patching: vbmeta.img") + ByteBuffer.wrap(rawData).putInt(120, 2) + tarOut.putNextEntry(newEntry("vbmeta.img", rawData.size.toLong())) + tarOut.write(rawData) } else { - console.add("-- Copying: " + entry.name) + console.add("-- Copying: ${entry.name}") tarOut.putNextEntry(entry) - tarIn.copyTo(tarOut) + tarIn.copyTo(tarOut, bufferSize = 1024 * 1024) } } val boot = SuFile.open(installDir, "boot.img") diff --git a/build.gradle.kts b/build.gradle.kts index 4d413747d..02049a102 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,7 +19,7 @@ buildscript { dependencies { classpath("com.android.tools.build:gradle:4.0.1") - classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0") + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10") classpath("androidx.navigation:navigation-safe-args-gradle-plugin:${vNav}") // NOTE: Do not place your application dependencies here; they belong