Skip to content

Commit e67fd8e

Browse files
committed
Add LOKI support
add LOKI as submodules add "lokiaboot" and "lokipatch" partition properties to fstab patch partitions marked as "lokipatch" before flashing
1 parent e4b2cdc commit e67fd8e

File tree

8 files changed

+167
-5
lines changed

8 files changed

+167
-5
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "sub_projects/Loki"]
2+
path = sub_projects/Loki
3+
url = https://github.com/efidroid/modules_loki.git

app/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ android {
2525
buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"
2626
buildConfigField "String", "USERNAME", "\""+System.getProperty("user.name")+"\""
2727
buildConfigField "String", "HOSTNAME", "\""+InetAddress.getLocalHost().getHostName()+"\""
28+
29+
ndk {
30+
abiFilters "armeabi-v7a"
31+
}
2832
}
2933
buildTypes {
3034
release {
@@ -36,6 +40,11 @@ android {
3640
versionNameSuffix "-debug"
3741
}
3842
}
43+
externalNativeBuild {
44+
ndkBuild {
45+
path 'src/main/cpp/Android.mk'
46+
}
47+
}
3948
}
4049

4150
dependencies {

app/src/main/cpp/Android.mk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LOCAL_PATH := $(call my-dir)
2+
3+
include $(CLEAR_VARS)
4+
LOCAL_MODULE := loki-wrapper
5+
LOCAL_SRC_FILES := loki_wrapper.c
6+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../sub_projects/Loki
7+
LOCAL_STATIC_LIBRARIES := libloki_static
8+
include $(BUILD_SHARED_LIBRARY)
9+
10+
include $(LOCAL_PATH)/../../../../sub_projects/Loki/Android.mk

app/src/main/cpp/loki_wrapper.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <jni.h>
2+
#include <loki.h>
3+
4+
JNIEXPORT jint JNICALL
5+
Java_org_efidroid_efidroidmanager_LokiTool_lokiPatch(JNIEnv *env, jobject instance,
6+
jstring partitionLabel_, jstring abootImage_,
7+
jstring inImage_, jstring outImage_) {
8+
const char *partitionLabel = (*env)->GetStringUTFChars(env, partitionLabel_, 0);
9+
const char *abootImage = (*env)->GetStringUTFChars(env, abootImage_, 0);
10+
const char *inImage = (*env)->GetStringUTFChars(env, inImage_, 0);
11+
const char *outImage = (*env)->GetStringUTFChars(env, outImage_, 0);
12+
13+
int ret = loki_patch(partitionLabel,abootImage,inImage,outImage);
14+
15+
(*env)->ReleaseStringUTFChars(env, partitionLabel_, partitionLabel);
16+
(*env)->ReleaseStringUTFChars(env, abootImage_, abootImage);
17+
(*env)->ReleaseStringUTFChars(env, inImage_, inImage);
18+
(*env)->ReleaseStringUTFChars(env, outImage_, outImage);
19+
20+
return ret;
21+
}
22+
23+
JNIEXPORT jint JNICALL
24+
Java_org_efidroid_efidroidmanager_LokiTool_lokiFlash(JNIEnv *env, jobject instance,
25+
jstring partitionLabel_, jstring image_) {
26+
const char *partitionLabel = (*env)->GetStringUTFChars(env, partitionLabel_, 0);
27+
const char *image = (*env)->GetStringUTFChars(env, image_, 0);
28+
29+
int ret = loki_flash(partitionLabel, image);
30+
31+
(*env)->ReleaseStringUTFChars(env, partitionLabel_, partitionLabel);
32+
(*env)->ReleaseStringUTFChars(env, image_, image);
33+
34+
return ret;
35+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.efidroid.efidroidmanager;
2+
3+
import android.util.Log;
4+
5+
import com.stericson.roottools.RootTools;
6+
import static com.android.volley.VolleyLog.TAG;
7+
8+
public class LokiTool {
9+
public enum PartitionLabel {
10+
BOOT("boot"),
11+
RECOVERY("recovery");
12+
13+
String mLabel;
14+
PartitionLabel(String s) {
15+
mLabel=s;
16+
}
17+
public String getLabel() {
18+
return mLabel;
19+
}
20+
}
21+
22+
static {
23+
System.loadLibrary("loki-wrapper");
24+
}
25+
26+
private LokiTool() {}
27+
28+
private static native int lokiPatch(String partitionLabel, String bootloaderImage,
29+
String inImage, String outImage);
30+
private static native int lokiFlash(String partitionLabel, String image);
31+
32+
public static boolean patchImage(PartitionLabel label, String bootloaderImage,
33+
String inImage, String outImage) {
34+
return lokiPatch(label.getLabel(),bootloaderImage,inImage,outImage) == 0;
35+
}
36+
37+
public static boolean flashImage(PartitionLabel label, String image) {
38+
//root required for flashing
39+
boolean isRootAccess = RootTools.isAccessGiven();
40+
if (!isRootAccess) {
41+
Log.e(TAG, "flashImage: cannot get root privileges");
42+
return false;
43+
}
44+
return lokiFlash(label.getLabel(), image) == 0;
45+
}
46+
}

app/src/main/java/org/efidroid/efidroidmanager/tasks/EFIDroidInstallServiceTask.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import android.os.Bundle;
44
import android.support.annotation.Keep;
55

6+
import com.stericson.roottools.RootTools;
7+
68
import org.efidroid.efidroidmanager.AppConstants;
9+
import org.efidroid.efidroidmanager.LokiTool;
710
import org.efidroid.efidroidmanager.R;
811
import org.efidroid.efidroidmanager.RootToolsEx;
912
import org.efidroid.efidroidmanager.Util;
@@ -17,6 +20,7 @@
1720
import org.json.JSONObject;
1821

1922
import java.io.BufferedInputStream;
23+
import java.io.FileNotFoundException;
2024
import java.io.FileOutputStream;
2125
import java.io.InputStream;
2226
import java.io.OutputStream;
@@ -114,6 +118,17 @@ private String downloadUpdate(String urlString) throws Exception {
114118
return downloadDir;
115119
}
116120

121+
private static void lokiPatchAndFlash(LokiTool.PartitionLabel partition,
122+
String bootloaderImage, String fileToPatch) throws Exception {
123+
if (bootloaderImage == null)
124+
throw new FileNotFoundException("Cannot find backed up bootloader");
125+
String patchedFile = fileToPatch + ".lok";
126+
if (!LokiTool.patchImage(partition,bootloaderImage,fileToPatch,patchedFile))
127+
throw new Exception("Loki patching error");
128+
if (!LokiTool.flashImage(partition,patchedFile))
129+
throw new Exception("Loki flashing patched image error");
130+
}
131+
117132
private void doInstall(String updateDir) throws Exception {
118133
// get esp parent directory
119134
String espParent = mDeviceInfo.getESPDir(false);
@@ -161,13 +176,36 @@ private void doInstall(String updateDir) throws Exception {
161176
}
162177
}
163178

164-
// install
179+
//dump bootloader to temporary folder
180+
String bootloaderImage = null;
165181
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
166-
if (!entry.isUEFI())
167-
continue;
182+
if (entry.isLokiAboot()) {
183+
bootloaderImage = getService().getCacheDir().getCanonicalPath() + "/bootloader.img";
184+
RootToolsEx.createPartitionBackup(getService(),entry.getBlkDevice(),bootloaderImage,0);
185+
}
186+
}
168187

169-
String file = updateDir + "/" + entry.getName() + ".img";
170-
RootToolsEx.dd(file, entry.getBlkDevice());
188+
// install
189+
try {
190+
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
191+
if (!entry.isUEFI())
192+
continue;
193+
194+
String file = updateDir + "/" + entry.getName() + ".img";
195+
196+
//if needed, apply LOKI patch to boot and recovery partitions
197+
if (entry.isLokiPatch() &&
198+
entry.getName().equals(LokiTool.PartitionLabel.BOOT.getLabel()))
199+
lokiPatchAndFlash(LokiTool.PartitionLabel.BOOT, bootloaderImage, file);
200+
else if (entry.isLokiPatch() &&
201+
entry.getName().equals(LokiTool.PartitionLabel.RECOVERY.getLabel()))
202+
lokiPatchAndFlash(LokiTool.PartitionLabel.RECOVERY, bootloaderImage, file);
203+
else
204+
RootToolsEx.dd(file, entry.getBlkDevice());
205+
}
206+
} finally {
207+
//remove bootloader dump
208+
RootTools.deleteFileOrDirectory(bootloaderImage, true);
171209
}
172210
}
173211

app/src/main/java/org/efidroid/efidroidmanager/types/FSTabEntry.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ public boolean isMultiboot() {
105105
return false;
106106
}
107107

108+
public boolean isLokiAboot() {
109+
String[] parts = mFfMgrFlags.split(",");
110+
for (String part : parts) {
111+
if (part.equals("lokiaboot")) {
112+
return true;
113+
}
114+
}
115+
return false;
116+
}
117+
118+
public boolean isLokiPatch() {
119+
String[] parts = mFfMgrFlags.split(",");
120+
for (String part : parts) {
121+
if (part.equals("lokipatch")) {
122+
return true;
123+
}
124+
}
125+
return false;
126+
}
127+
108128
public boolean isUEFI() {
109129
String[] parts = mFfMgrFlags.split(",");
110130
for (String part : parts) {

sub_projects/Loki

Submodule Loki added at 784e86f

0 commit comments

Comments
 (0)