-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FSTabEntry: getFfMgrFlags() returns list of flags. Rewrite isMultiboo…
…t(), isUEFI(), getESP() using it Move ABIs retrieving code to separate method getABIs() in Util Add universal image patching interface Add image patching (if needed) before flashing. Add LOKI as git submodule Add LOKI image patcher using NDK
- Loading branch information
Showing
13 changed files
with
287 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "sub_projects/Loki"] | ||
path = sub_projects/Loki | ||
url = https://github.com/efidroid/modules_loki.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
LOCAL_PATH := $(call my-dir) | ||
|
||
# LOKI usable only for ARMv7-based phones | ||
ifeq ($(TARGET_ARCH_ABI), armeabi-v7a) | ||
LOKI_PATH := $(abspath $(LOCAL_PATH)/../../../../sub_projects/Loki) | ||
|
||
include $(CLEAR_VARS) | ||
LOCAL_MODULE := loki_wrapper | ||
LOCAL_SRC_FILES := loki_wrapper.c | ||
LOCAL_STATIC_LIBRARIES := libloki_static | ||
LOCAL_C_INCLUDES += $(LOKI_PATH) | ||
include $(BUILD_SHARED_LIBRARY) | ||
|
||
include $(LOKI_PATH)/Android.mk | ||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <jni.h> | ||
#include <loki.h> | ||
|
||
JNIEXPORT jboolean JNICALL | ||
Java_org_efidroid_efidroidmanager_patching_LokiPatcher_nativePatchImage(JNIEnv *env, | ||
jclass class, | ||
jstring imageType_, | ||
jstring aBootImage_, | ||
jstring in_, jstring out_) { | ||
const char *imageType = (*env)->GetStringUTFChars(env, imageType_, 0); | ||
const char *aBootImage = (*env)->GetStringUTFChars(env, aBootImage_, 0); | ||
const char *in = (*env)->GetStringUTFChars(env, in_, 0); | ||
const char *out = (*env)->GetStringUTFChars(env, out_, 0); | ||
|
||
// loki_patch() returns '0' on successful exit | ||
int result = loki_patch(imageType, aBootImage, in, out); | ||
|
||
(*env)->ReleaseStringUTFChars(env, imageType_, imageType); | ||
(*env)->ReleaseStringUTFChars(env, aBootImage_, aBootImage); | ||
(*env)->ReleaseStringUTFChars(env, in_, in); | ||
(*env)->ReleaseStringUTFChars(env, out_, out); | ||
|
||
return (jboolean)(result == 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/org/efidroid/efidroidmanager/patching/DummyPatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.efidroid.efidroidmanager.patching; | ||
|
||
|
||
import android.content.Context; | ||
|
||
import org.efidroid.efidroidmanager.models.DeviceInfo; | ||
import org.efidroid.efidroidmanager.types.FSTabEntry; | ||
|
||
final class DummyPatcher extends Patcher { | ||
DummyPatcher(DeviceInfo deviceInfo, Context context) { | ||
super(deviceInfo, context); | ||
} | ||
|
||
@Override | ||
public void prepareEnvironment(String updateDir) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public boolean isPatchRequired(FSTabEntry entry) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void patchImage(FSTabEntry destEntry, String image) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public void cleanupEnvironment() { | ||
|
||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
app/src/main/java/org/efidroid/efidroidmanager/patching/LokiPatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.efidroid.efidroidmanager.patching; | ||
|
||
import android.content.Context; | ||
|
||
import com.stericson.roottools.RootTools; | ||
|
||
import org.efidroid.efidroidmanager.RootToolsEx; | ||
import org.efidroid.efidroidmanager.Util; | ||
import org.efidroid.efidroidmanager.models.DeviceInfo; | ||
import org.efidroid.efidroidmanager.types.FSTabEntry; | ||
|
||
import java.util.List; | ||
|
||
class LokiPatcher extends Patcher { | ||
private enum ImageType { | ||
BOOT("boot","lokiboot"), | ||
RECOVERY("recovery", "lokirecovery"); | ||
|
||
private final String mName, mFlag; | ||
|
||
ImageType(String name, String flag) { | ||
mName = name; | ||
mFlag = flag; | ||
} | ||
|
||
String getName() { return mName; } | ||
|
||
String getFlag() { return mFlag; } | ||
} | ||
|
||
static { | ||
// LOKI usable only for ARMv7-based phones | ||
List<String> abis = Util.getABIs(); | ||
if (abis.contains("armeabi-v7a")) { | ||
System.loadLibrary("loki_wrapper"); | ||
for (ImageType imageType : ImageType.values()) { | ||
PatcherStorage.registerPatcher(imageType.getFlag(), LokiPatcher.class); | ||
} | ||
} | ||
} | ||
|
||
private static final String ABootFlag = "lokiaboot"; | ||
|
||
private String aBootImage; | ||
|
||
public LokiPatcher(DeviceInfo deviceInfo, Context context) { | ||
super(deviceInfo, context); | ||
} | ||
|
||
@Override | ||
public void prepareEnvironment(String updateDir) throws Exception { | ||
FSTabEntry aBootEntry = null; | ||
for (FSTabEntry entry : getDeviceInfo().getFSTab().getFSTabEntries()) { | ||
if (entry.getFfMgrFlags().contains(ABootFlag)) { | ||
aBootEntry = entry; | ||
} | ||
} | ||
if (aBootEntry == null) { | ||
throw new Exception("ABoot "+ABootFlag+" entry not found (bad multiboot.fstab)"); | ||
} | ||
aBootImage = updateDir + "/aboot.img"; | ||
RootToolsEx.dd(aBootEntry.getBlkDevice(),aBootImage); | ||
} | ||
|
||
@Override | ||
public boolean isPatchRequired(FSTabEntry entry) { | ||
for (ImageType imageType : ImageType.values()) { | ||
if (entry.getFfMgrFlags().contains(imageType.getFlag())) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static native boolean nativePatchImage(String imageType, String aBootImage, String in, String out); | ||
|
||
@Override | ||
public void patchImage(FSTabEntry destEntry, String image) throws Exception { | ||
boolean isSuccessfulPatch = false; | ||
String outputImage = null; | ||
for (ImageType imageType : ImageType.values()) { | ||
if (destEntry.getFfMgrFlags().contains(imageType.getFlag())) { | ||
outputImage = image.substring(0, image.lastIndexOf('/')) + "/" + imageType.getName() + ".img"; | ||
isSuccessfulPatch = nativePatchImage(imageType.getName(), aBootImage, image, outputImage); | ||
} | ||
} | ||
if (isSuccessfulPatch) { | ||
RootTools.copyFile(outputImage, image, false, true); | ||
RootTools.deleteFileOrDirectory(outputImage, false); | ||
} else { | ||
throw new Exception("Image patch error"); | ||
} | ||
} | ||
|
||
@Override | ||
public void cleanupEnvironment() { | ||
RootTools.deleteFileOrDirectory(aBootImage, false); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/org/efidroid/efidroidmanager/patching/Patcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.efidroid.efidroidmanager.patching; | ||
|
||
import android.content.Context; | ||
|
||
import org.efidroid.efidroidmanager.models.DeviceInfo; | ||
import org.efidroid.efidroidmanager.types.FSTabEntry; | ||
|
||
public abstract class Patcher { | ||
private final Context mContext; | ||
private final DeviceInfo mDeviceInfo; | ||
|
||
Patcher(DeviceInfo deviceInfo, Context context) { | ||
mContext = context; | ||
mDeviceInfo = deviceInfo; | ||
} | ||
|
||
protected Context getContext() { return mContext; } | ||
|
||
protected DeviceInfo getDeviceInfo() { return mDeviceInfo; } | ||
|
||
public abstract void prepareEnvironment(String updateDir) throws Exception; | ||
|
||
public abstract boolean isPatchRequired(FSTabEntry entry); | ||
|
||
// replaces original image with patched | ||
public abstract void patchImage(FSTabEntry destEntry, String image) throws Exception; | ||
|
||
public abstract void cleanupEnvironment(); | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/main/java/org/efidroid/efidroidmanager/patching/PatcherStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.efidroid.efidroidmanager.patching; | ||
|
||
import android.content.Context; | ||
|
||
import org.efidroid.efidroidmanager.models.DeviceInfo; | ||
import org.efidroid.efidroidmanager.types.FSTabEntry; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class PatcherStorage { | ||
// FSTab flag to patcher | ||
private static final Map<String, Class<? extends Patcher>> patchers = new HashMap<>(); | ||
|
||
static void registerPatcher(String flag, Class<? extends Patcher> patcher) { | ||
patchers.put(flag,patcher); | ||
} | ||
|
||
// selects patcher using FSTab flags. If no patcher selected, returns a DummyPatcher instance | ||
public static Patcher selectPatcher(DeviceInfo deviceInfo, Context context) throws Exception{ | ||
for (FSTabEntry entry : deviceInfo.getFSTab().getFSTabEntries()) { | ||
for (String flag : patchers.keySet()) { | ||
if (entry.getFfMgrFlags().contains(flag)) { | ||
Class<? extends Patcher> patcherClass = patchers.get(flag); | ||
if (patcherClass==null) { | ||
throw new Exception("Patcher registered but not found"); | ||
} | ||
try { | ||
Constructor constructor = patcherClass.getDeclaredConstructor(DeviceInfo.class, Context.class); | ||
return (Patcher) constructor.newInstance(deviceInfo, context); | ||
} catch (NoSuchMethodException e) { | ||
throw new Exception("Cannot instantiate patcher"); | ||
} | ||
} | ||
} | ||
} | ||
return new DummyPatcher(deviceInfo, context); | ||
} | ||
|
||
private PatcherStorage() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.