|
| 1 | +package org.efidroid.efidroidmanager.patching; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import com.stericson.roottools.RootTools; |
| 6 | + |
| 7 | +import org.efidroid.efidroidmanager.RootToolsEx; |
| 8 | +import org.efidroid.efidroidmanager.Util; |
| 9 | +import org.efidroid.efidroidmanager.models.DeviceInfo; |
| 10 | +import org.efidroid.efidroidmanager.types.FSTabEntry; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +class LokiPatcher extends Patcher { |
| 15 | + private enum ImageType { |
| 16 | + BOOT("boot","lokiboot"), |
| 17 | + RECOVERY("recovery", "lokirecovery"); |
| 18 | + |
| 19 | + private final String mName, mFlag; |
| 20 | + |
| 21 | + ImageType(String name, String flag) { |
| 22 | + mName = name; |
| 23 | + mFlag = flag; |
| 24 | + } |
| 25 | + |
| 26 | + String getName() { return mName; } |
| 27 | + |
| 28 | + String getFlag() { return mFlag; } |
| 29 | + } |
| 30 | + |
| 31 | + static { |
| 32 | + // LOKI usable only for ARMv7-based phones |
| 33 | + List<String> abis = Util.getABIs(); |
| 34 | + if (abis.contains("armeabi-v7a")) { |
| 35 | + System.loadLibrary("loki_wrapper"); |
| 36 | + for (ImageType imageType : ImageType.values()) { |
| 37 | + PatcherStorage.registerPatcher(imageType.getFlag(), LokiPatcher.class); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private static final String ABootFlag = "lokiaboot"; |
| 43 | + |
| 44 | + private String aBootImage; |
| 45 | + |
| 46 | + public LokiPatcher(DeviceInfo deviceInfo, Context context) { |
| 47 | + super(deviceInfo, context); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void prepareEnvironment(String updateDir) throws Exception { |
| 52 | + FSTabEntry aBootEntry = null; |
| 53 | + for (FSTabEntry entry : getDeviceInfo().getFSTab().getFSTabEntries()) { |
| 54 | + if (entry.getFfMgrFlags().contains(ABootFlag)) { |
| 55 | + aBootEntry = entry; |
| 56 | + } |
| 57 | + } |
| 58 | + if (aBootEntry == null) { |
| 59 | + throw new Exception("ABoot "+ABootFlag+" entry not found (bad multiboot.fstab)"); |
| 60 | + } |
| 61 | + aBootImage = updateDir + "/aboot.img"; |
| 62 | + RootToolsEx.dd(aBootEntry.getBlkDevice(),aBootImage); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean isPatchRequired(FSTabEntry entry) { |
| 67 | + for (ImageType imageType : ImageType.values()) { |
| 68 | + if (entry.getFfMgrFlags().contains(imageType.getFlag())) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + private static native boolean nativePatchImage(String imageType, String aBootImage, String in, String out); |
| 76 | + |
| 77 | + @Override |
| 78 | + public void patchImage(FSTabEntry destEntry, String image) throws Exception { |
| 79 | + boolean isSuccessfulPatch = false; |
| 80 | + String outputImage = null; |
| 81 | + for (ImageType imageType : ImageType.values()) { |
| 82 | + if (destEntry.getFfMgrFlags().contains(imageType.getFlag())) { |
| 83 | + outputImage = image.substring(0, image.lastIndexOf('/')) + "/" + imageType.getName() + ".img"; |
| 84 | + isSuccessfulPatch = nativePatchImage(imageType.getName(), aBootImage, image, outputImage); |
| 85 | + } |
| 86 | + } |
| 87 | + if (isSuccessfulPatch) { |
| 88 | + RootTools.copyFile(outputImage, image, false, true); |
| 89 | + RootTools.deleteFileOrDirectory(outputImage, false); |
| 90 | + } else { |
| 91 | + throw new Exception("Image patch error"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void cleanupEnvironment() { |
| 97 | + RootTools.deleteFileOrDirectory(aBootImage, false); |
| 98 | + } |
| 99 | +} |
0 commit comments