Skip to content

Commit 3c1b210

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 3c1b210

File tree

9 files changed

+147
-4
lines changed

9 files changed

+147
-4
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.efidroid.efidroidmanager;
2+
3+
public class LokiTool {
4+
public enum PartitionLabel {
5+
BOOT("boot"),
6+
RECOVERY("recovery");
7+
8+
String mLabel;
9+
PartitionLabel(String s) {
10+
mLabel=s;
11+
}
12+
public String getLabel() {
13+
return mLabel;
14+
}
15+
}
16+
17+
static {
18+
System.loadLibrary("loki-wrapper");
19+
}
20+
21+
private LokiTool() {}
22+
23+
private static native int lokiPatch(String partitionLabel, String bootloaderImage,
24+
String inImage, String outImage);
25+
private static native int lokiFlash(String partitionLabel, String image);
26+
27+
public static boolean patchImage(PartitionLabel label, String bootloaderImage,
28+
String inImage, String outImage) {
29+
return lokiPatch(label.getLabel(),bootloaderImage,inImage,outImage) == 0;
30+
}
31+
32+
public static boolean flashImage(PartitionLabel label, String image) {
33+
return lokiFlash(label.getLabel(), image) == 0;
34+
}
35+
}

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.support.annotation.Keep;
55

66
import org.efidroid.efidroidmanager.AppConstants;
7+
import org.efidroid.efidroidmanager.LokiTool;
78
import org.efidroid.efidroidmanager.R;
89
import org.efidroid.efidroidmanager.RootToolsEx;
910
import org.efidroid.efidroidmanager.Util;
@@ -17,6 +18,7 @@
1718
import org.json.JSONObject;
1819

1920
import java.io.BufferedInputStream;
21+
import java.io.FileNotFoundException;
2022
import java.io.FileOutputStream;
2123
import java.io.InputStream;
2224
import java.io.OutputStream;
@@ -114,6 +116,17 @@ private String downloadUpdate(String urlString) throws Exception {
114116
return downloadDir;
115117
}
116118

119+
private void lokiPatchAndFlash(LokiTool.PartitionLabel partition,
120+
String bootloaderImage, String file) throws Exception {
121+
if (bootloaderImage == null)
122+
throw new FileNotFoundException("Cannot find backed up bootloader");
123+
String patchedFile = file + ".lok";
124+
if (!LokiTool.patchImage(partition,bootloaderImage,file,patchedFile))
125+
throw new Exception("Loki patching error");
126+
if (!LokiTool.flashImage(partition,patchedFile))
127+
throw new Exception("Loki flashing patched image error");
128+
}
129+
117130
private void doInstall(String updateDir) throws Exception {
118131
// get esp parent directory
119132
String espParent = mDeviceInfo.getESPDir(false);
@@ -128,7 +141,7 @@ private void doInstall(String updateDir) throws Exception {
128141
if (!mInstallationStatus.isInstalled() || mInstallationStatus.isBroken()) {
129142
// create backups
130143
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
131-
if (!entry.isUEFI())
144+
if (!(entry.isUEFI() || entry.isLokiAboot())) //also backup bootloader partition
132145
continue;
133146

134147
if (!RootToolsEx.isFile(updateDir + "/" + entry.getName() + ".img"))
@@ -161,13 +174,30 @@ private void doInstall(String updateDir) throws Exception {
161174
}
162175
}
163176

177+
//fetch backed up bootloader image name
178+
String bootloaderImage = null;
179+
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
180+
if (entry.isLokiAboot()) {
181+
bootloaderImage = espDir + "/partition_" + entry.getName() + ".img";
182+
}
183+
}
184+
164185
// install
165186
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
166187
if (!entry.isUEFI())
167188
continue;
168189

169190
String file = updateDir + "/" + entry.getName() + ".img";
170-
RootToolsEx.dd(file, entry.getBlkDevice());
191+
192+
//if needed, apply LOKI patch to boot and recovery partitions
193+
if (entry.isLokiPatch() &&
194+
entry.getName().equals(LokiTool.PartitionLabel.BOOT.getLabel()))
195+
lokiPatchAndFlash(LokiTool.PartitionLabel.BOOT,bootloaderImage,file);
196+
else if (entry.isLokiPatch() &&
197+
entry.getName().equals(LokiTool.PartitionLabel.RECOVERY.getLabel()))
198+
lokiPatchAndFlash(LokiTool.PartitionLabel.RECOVERY,bootloaderImage,file);
199+
else
200+
RootToolsEx.dd(file, entry.getBlkDevice());
171201
}
172202
}
173203

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private void doUninstall() throws Exception {
5454
String espDir = espParent + "/UEFIESP";
5555
RootToolsEx.mkdir(espDir, true);
5656

57-
// restore backups
57+
// restore backups, do not restore bootloader
5858
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
5959
if (!entry.isUEFI())
6060
continue;
@@ -64,7 +64,7 @@ private void doUninstall() throws Exception {
6464

6565
// remove backups
6666
for (FSTabEntry entry : mDeviceInfo.getFSTab().getFSTabEntries()) {
67-
if (!entry.isUEFI())
67+
if (!(entry.isUEFI() || entry.isLokiAboot()))
6868
continue;
6969

7070
RootTools.deleteFileOrDirectory(espDir + "/partition_" + entry.getName() + ".img", false);

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)