Skip to content

Commit

Permalink
Add LOKI support
Browse files Browse the repository at this point in the history
add LOKI as submodules
add "lokiaboot" and "lokipatch" partition properties to fstab
patch partitions marked as "lokipatch" before flashing
  • Loading branch information
xakep666 committed Jan 23, 2017
1 parent e4b2cdc commit 38ca410
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
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
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ android {
buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"
buildConfigField "String", "USERNAME", "\""+System.getProperty("user.name")+"\""
buildConfigField "String", "HOSTNAME", "\""+InetAddress.getLocalHost().getHostName()+"\""

ndk {
abiFilters "armeabi-v7a"
}
}
buildTypes {
release {
Expand All @@ -36,6 +40,11 @@ android {
versionNameSuffix "-debug"
}
}
externalNativeBuild {
ndkBuild {
path 'src/main/cpp/Android.mk'
}
}
}

dependencies {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/cpp/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := loki-wrapper
LOCAL_SRC_FILES := loki_wrapper.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../sub_projects/Loki
LOCAL_STATIC_LIBRARIES := libloki_static
include $(BUILD_SHARED_LIBRARY)

include $(LOCAL_PATH)/../../../../sub_projects/Loki/Android.mk
35 changes: 35 additions & 0 deletions app/src/main/cpp/loki_wrapper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <jni.h>
#include <loki.h>

JNIEXPORT jint JNICALL
Java_org_efidroid_efidroidmanager_LokiTool_lokiPatch(JNIEnv *env, jobject instance,
jstring partitionLabel_, jstring abootImage_,
jstring inImage_, jstring outImage_) {
const char *partitionLabel = (*env)->GetStringUTFChars(env, partitionLabel_, 0);
const char *abootImage = (*env)->GetStringUTFChars(env, abootImage_, 0);
const char *inImage = (*env)->GetStringUTFChars(env, inImage_, 0);
const char *outImage = (*env)->GetStringUTFChars(env, outImage_, 0);

int ret = loki_patch(partitionLabel,abootImage,inImage,outImage);

(*env)->ReleaseStringUTFChars(env, partitionLabel_, partitionLabel);
(*env)->ReleaseStringUTFChars(env, abootImage_, abootImage);
(*env)->ReleaseStringUTFChars(env, inImage_, inImage);
(*env)->ReleaseStringUTFChars(env, outImage_, outImage);

return ret;
}

JNIEXPORT jint JNICALL
Java_org_efidroid_efidroidmanager_LokiTool_lokiFlash(JNIEnv *env, jobject instance,
jstring partitionLabel_, jstring image_) {
const char *partitionLabel = (*env)->GetStringUTFChars(env, partitionLabel_, 0);
const char *image = (*env)->GetStringUTFChars(env, image_, 0);

int ret = loki_flash(partitionLabel, image);

(*env)->ReleaseStringUTFChars(env, partitionLabel_, partitionLabel);
(*env)->ReleaseStringUTFChars(env, image_, image);

return ret;
}
46 changes: 46 additions & 0 deletions app/src/main/java/org/efidroid/efidroidmanager/LokiTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.efidroid.efidroidmanager;

import android.util.Log;

import com.stericson.roottools.RootTools;
import static com.android.volley.VolleyLog.TAG;

public class LokiTool {
public enum PartitionLabel {
BOOT("boot"),
RECOVERY("recovery");

String mLabel;
PartitionLabel(String s) {
mLabel=s;
}
public String getLabel() {
return mLabel;
}
}

static {
System.loadLibrary("loki-wrapper");
}

private LokiTool() {}

private static native int lokiPatch(String partitionLabel, String bootloaderImage,
String inImage, String outImage);
private static native int lokiFlash(String partitionLabel, String image);

public static boolean patchImage(PartitionLabel label, String bootloaderImage,
String inImage, String outImage) {
return lokiPatch(label.getLabel(),bootloaderImage,inImage,outImage) == 0;
}

public static boolean flashImage(PartitionLabel label, String image) {
//root required for flashing
boolean isRootAccess = RootTools.isAccessGiven();
if (!isRootAccess) {
Log.e(TAG, "flashImage: cannot get root privileges");
return false;
}
return lokiFlash(label.getLabel(), image) == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import android.os.Bundle;
import android.support.annotation.Keep;

import com.stericson.roottools.RootTools;

import org.efidroid.efidroidmanager.AppConstants;
import org.efidroid.efidroidmanager.LokiTool;
import org.efidroid.efidroidmanager.R;
import org.efidroid.efidroidmanager.RootToolsEx;
import org.efidroid.efidroidmanager.Util;
Expand All @@ -17,6 +20,7 @@
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -114,6 +118,17 @@ private String downloadUpdate(String urlString) throws Exception {
return downloadDir;
}

private static void lokiPatchAndFlash(LokiTool.PartitionLabel partition,
String bootloaderImage, String fileToPatch) throws Exception {
if (bootloaderImage == null)
throw new FileNotFoundException("Cannot find backed up bootloader");
String patchedFile = fileToPatch + ".lok";
if (!LokiTool.patchImage(partition,bootloaderImage,fileToPatch,patchedFile))
throw new Exception("Loki patching error");
if (!LokiTool.flashImage(partition,patchedFile))
throw new Exception("Loki flashing patched image error");
}

private void doInstall(String updateDir) throws Exception {
// get esp parent directory
String espParent = mDeviceInfo.getESPDir(false);
Expand Down Expand Up @@ -161,13 +176,36 @@ private void doInstall(String updateDir) throws Exception {
}
}

// install
//dump bootloader to temporary folder
String bootloaderImage = null;
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
if (!entry.isUEFI())
continue;
if (entry.isLokiAboot()) {
bootloaderImage = getService().getCacheDir().getCanonicalPath() + "/bootloader.img";
RootToolsEx.createPartitionBackup(getService(),entry.getBlkDevice(),bootloaderImage,-1);
}
}

String file = updateDir + "/" + entry.getName() + ".img";
RootToolsEx.dd(file, entry.getBlkDevice());
// install
try {
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
if (!entry.isUEFI())
continue;

String file = updateDir + "/" + entry.getName() + ".img";

//if needed, apply LOKI patch to boot and recovery partitions
if (entry.isLokiPatch() &&
entry.getName().equals(LokiTool.PartitionLabel.BOOT.getLabel()))
lokiPatchAndFlash(LokiTool.PartitionLabel.BOOT, bootloaderImage, file);
else if (entry.isLokiPatch() &&
entry.getName().equals(LokiTool.PartitionLabel.RECOVERY.getLabel()))
lokiPatchAndFlash(LokiTool.PartitionLabel.RECOVERY, bootloaderImage, file);
else
RootToolsEx.dd(file, entry.getBlkDevice());
}
} finally {
//remove bootloader dump
RootTools.deleteFileOrDirectory(bootloaderImage, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ public boolean isMultiboot() {
return false;
}

public boolean isLokiAboot() {
String[] parts = mFfMgrFlags.split(",");
for (String part : parts) {
if (part.equals("lokiaboot")) {
return true;
}
}
return false;
}

public boolean isLokiPatch() {
String[] parts = mFfMgrFlags.split(",");
for (String part : parts) {
if (part.equals("lokipatch")) {
return true;
}
}
return false;
}

public boolean isUEFI() {
String[] parts = mFfMgrFlags.split(",");
for (String part : parts) {
Expand Down
1 change: 1 addition & 0 deletions sub_projects/Loki
Submodule Loki added at 784e86

0 comments on commit 38ca410

Please sign in to comment.