44import android .support .annotation .Keep ;
55
66import org .efidroid .efidroidmanager .AppConstants ;
7+ import org .efidroid .efidroidmanager .LokiTool ;
78import org .efidroid .efidroidmanager .R ;
89import org .efidroid .efidroidmanager .RootToolsEx ;
910import org .efidroid .efidroidmanager .Util ;
1718import org .json .JSONObject ;
1819
1920import java .io .BufferedInputStream ;
21+ import java .io .FileNotFoundException ;
2022import java .io .FileOutputStream ;
2123import java .io .InputStream ;
2224import 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
0 commit comments