Skip to content

Commit 9e95e7f

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 9e95e7f

File tree

8 files changed

+169
-5
lines changed

8 files changed

+169
-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, jclass type,
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, jclass type,
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.efidroid.efidroidmanager;
2+
3+
import android.util.Log;
4+
5+
import com.stericson.roottools.RootTools;
6+
7+
import static com.android.volley.VolleyLog.TAG;
8+
9+
public class LokiTool {
10+
public enum PartitionLabel {
11+
BOOT("boot"),
12+
RECOVERY("recovery");
13+
14+
private final String mLabel;
15+
16+
PartitionLabel(String s) {
17+
mLabel = s;
18+
}
19+
20+
public String getLabel() {
21+
return mLabel;
22+
}
23+
}
24+
25+
static {
26+
System.loadLibrary("loki-wrapper");
27+
}
28+
29+
private LokiTool() {
30+
}
31+
32+
private static native int lokiPatch(String partitionLabel, String bootloaderImage, String inImage, String outImage);
33+
34+
private static native int lokiFlash(String partitionLabel, String image);
35+
36+
public static boolean patchImage(PartitionLabel label, String bootloaderImage, String inImage, String outImage) {
37+
return lokiPatch(label.getLabel(), bootloaderImage, inImage, outImage) == 0;
38+
}
39+
40+
public static boolean flashImage(PartitionLabel label, String image) {
41+
// root required for flashing
42+
boolean isRootAccess = RootTools.isAccessGiven();
43+
if (!isRootAccess) {
44+
Log.e(TAG, "flashImage: cannot get root privileges");
45+
return false;
46+
}
47+
return lokiFlash(label.getLabel(), image) == 0;
48+
}
49+
}

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

Lines changed: 42 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,16 @@ private String downloadUpdate(String urlString) throws Exception {
114118
return downloadDir;
115119
}
116120

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

164-
// install
178+
// dump bootloader to temporary folder
179+
String bootloaderImage = null;
165180
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
166-
if (!entry.isUEFI())
167-
continue;
181+
if (entry.isLokiABoot()) {
182+
bootloaderImage = getService().getCacheDir().getCanonicalPath() + "/bootloader.img";
183+
RootToolsEx.createPartitionBackup(getService(), entry.getBlkDevice(), bootloaderImage, -1);
184+
}
185+
}
168186

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

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)