|
3 | 3 | import android.os.Bundle; |
4 | 4 | import android.support.annotation.Keep; |
5 | 5 |
|
| 6 | +import com.stericson.roottools.RootTools; |
| 7 | + |
6 | 8 | import org.efidroid.efidroidmanager.AppConstants; |
| 9 | +import org.efidroid.efidroidmanager.LokiTool; |
7 | 10 | import org.efidroid.efidroidmanager.R; |
8 | 11 | import org.efidroid.efidroidmanager.RootToolsEx; |
9 | 12 | import org.efidroid.efidroidmanager.Util; |
|
17 | 20 | import org.json.JSONObject; |
18 | 21 |
|
19 | 22 | import java.io.BufferedInputStream; |
| 23 | +import java.io.FileNotFoundException; |
20 | 24 | import java.io.FileOutputStream; |
21 | 25 | import java.io.InputStream; |
22 | 26 | import java.io.OutputStream; |
@@ -114,6 +118,16 @@ private String downloadUpdate(String urlString) throws Exception { |
114 | 118 | return downloadDir; |
115 | 119 | } |
116 | 120 |
|
| 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 | + |
117 | 131 | private void doInstall(String updateDir) throws Exception { |
118 | 132 | // get esp parent directory |
119 | 133 | String espParent = mDeviceInfo.getESPDir(false); |
@@ -161,13 +175,36 @@ private void doInstall(String updateDir) throws Exception { |
161 | 175 | } |
162 | 176 | } |
163 | 177 |
|
164 | | - // install |
| 178 | + // dump bootloader to temporary folder |
| 179 | + String bootloaderImage = null; |
165 | 180 | 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 | + } |
168 | 186 |
|
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); |
171 | 208 | } |
172 | 209 | } |
173 | 210 |
|
|
0 commit comments