Skip to content

Commit 57272fa

Browse files
committed
Add LOKI support
add loki_tool_static precompiled binary (armeabi-v7a, position-independent) add "lokiaboot" and "lokipatch" partition properties to fstab patch partitions marked as "lokipatch" before flashing
1 parent e4b2cdc commit 57272fa

5 files changed

Lines changed: 140 additions & 5 deletions

File tree

22.8 KB
Binary file not shown.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.efidroid.efidroidmanager;
2+
3+
import android.content.Context;
4+
import android.os.Build;
5+
6+
import org.apache.commons.io.FileUtils;
7+
import org.efidroid.efidroidmanager.services.IntentServiceEx;
8+
9+
import java.io.File;
10+
import java.io.FileNotFoundException;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.util.ArrayList;
14+
import java.util.Arrays;
15+
16+
public class LokiTool {
17+
public enum PartitionLabel {
18+
BOOT("boot"),
19+
RECOVERY("recovery");
20+
21+
private final String mLabel;
22+
23+
PartitionLabel(String s) {
24+
mLabel = s;
25+
}
26+
27+
public String getLabel() {
28+
return mLabel;
29+
}
30+
}
31+
32+
private LokiTool() {
33+
}
34+
35+
public static void init(Context context) {
36+
ArrayList<String> abis = new ArrayList<>();
37+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
38+
abis.addAll(Arrays.asList(Build.SUPPORTED_ABIS));
39+
} else {
40+
abis.add(Build.CPU_ABI);
41+
abis.add(Build.CPU_ABI2);
42+
}
43+
// do not initialize on non-arm cpu
44+
if (!abis.contains("armeabi-v7a")) return;
45+
46+
try {
47+
InputStream is = null;
48+
try {
49+
is = context.getAssets().open("armeabi-v7a/loki_tool_static");
50+
} catch (FileNotFoundException ignored) {
51+
}
52+
if (is == null) {
53+
throw new IOException("can't find compatible loki tool");
54+
}
55+
56+
File out = new File(context.getFilesDir(), "/loki_tool_static");
57+
if (!out.exists())
58+
FileUtils.copyInputStreamToFile(is, out);
59+
out.setExecutable(true, false);
60+
out.setReadable(true, false);
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
66+
public static void patchImage(IntentServiceEx service, PartitionLabel label, String bootloaderImage, String inImage, String outImage) throws Exception {
67+
RootToolsEx.runServiceCommand(service, String.format("loki_tool_static patch %s \"%s\" \"%s\" \"%s\"", label.getLabel(), bootloaderImage, inImage, outImage));
68+
}
69+
70+
public static void flashImage(IntentServiceEx service, PartitionLabel label, String image) throws Exception {
71+
RootToolsEx.runServiceCommand(service, String.format("loki_tool_static flash %s \"%s\"", label.getLabel(), image));
72+
}
73+
}

app/src/main/java/org/efidroid/efidroidmanager/RootToolsEx.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ public static void init(Context context) {
705705
RootShell.shellPathExtension = context.getFilesDir().getAbsolutePath();
706706

707707
sp.edit().putInt(AppConstants.SHAREDPREFS_GLOBAL_LAST_APP_VERSION, BuildConfig.VERSION_CODE).apply();
708+
709+
//loki tool init
710+
LokiTool.init(context);
708711
} catch (IOException e) {
709712
e.printStackTrace();
710713
}

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

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

121+
private 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+
try {
126+
LokiTool.patchImage(getService(), partition, bootloaderImage, fileToPatch, patchedFile);
127+
LokiTool.flashImage(getService(), partition, patchedFile);
128+
} finally {
129+
RootTools.deleteFileOrDirectory(patchedFile, false);
130+
}
131+
}
132+
117133
private void doInstall(String updateDir) throws Exception {
118134
// get esp parent directory
119135
String espParent = mDeviceInfo.getESPDir(false);
@@ -161,13 +177,36 @@ private void doInstall(String updateDir) throws Exception {
161177
}
162178
}
163179

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

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

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) {

0 commit comments

Comments
 (0)