1818
1919package airsquared .blobsaver .app ;
2020
21+ import airsquared .blobsaver .app .natives .Libirecovery ;
22+ import airsquared .blobsaver .app .natives .Libplist ;
2123import com .sun .javafx .PlatformUtil ;
22- import com .sun .jna .Native ;
2324import com .sun .jna .Pointer ;
24- import com .sun .jna .Structure ;
2525import com .sun .jna .ptr .PointerByReference ;
2626import javafx .concurrent .Task ;
2727import javafx .scene .control .Alert ;
2828import javafx .scene .control .ButtonType ;
2929
30- /**
31- * This class provides access to functions in the native library libimobiledevice.
32- * <p>
33- * See the libimobiledevice docs for
34- * <a href="https://www.libimobiledevice.org/docs/html/lockdown_8h.html">lockdown.h</a>
35- * and
36- * <a href="https://www.libimobiledevice.org/docs/html/libimobiledevice_8h.html">libimobiledevice.h</a>
37- * for information on the native functions. For information on the libplist functions,
38- * look at the source code(files plist.c and xplist.c).
39- *
40- * @author airsquared
41- */
42- @ SuppressWarnings ("WeakerAccess" )
43- public class Libimobiledevice {
30+ import static airsquared .blobsaver .app .natives .Libimobiledevice .idevice_free ;
31+ import static airsquared .blobsaver .app .natives .Libimobiledevice .idevice_new ;
32+ import static airsquared .blobsaver .app .natives .Libimobiledevice .lockdownd_client_free ;
33+ import static airsquared .blobsaver .app .natives .Libimobiledevice .lockdownd_client_new ;
34+ import static airsquared .blobsaver .app .natives .Libimobiledevice .lockdownd_enter_recovery ;
35+ import static airsquared .blobsaver .app .natives .Libimobiledevice .lockdownd_get_value ;
36+ import static airsquared .blobsaver .app .natives .Libimobiledevice .lockdownd_pair ;
37+
4438
45- public static long getECID (boolean showErrorAlert ) {
39+ public class LibimobiledeviceUtil {
40+
41+ public static long getECID (boolean showErrorAlert ) throws LibimobiledeviceException {
4642 return Long .parseLong (getKeyFromConnectedDevice ("UniqueChipID" , PlistType .INTEGER , showErrorAlert ));
4743 }
4844
49- public static String getDeviceModelIdentifier (boolean showErrorAlert ) {
45+ public static String getDeviceModelIdentifier (boolean showErrorAlert ) throws LibimobiledeviceException {
5046 return getKeyFromConnectedDevice ("ProductType" , PlistType .STRING , showErrorAlert );
5147 }
5248
53- public static String getBoardConfig (boolean showErrorAlert ) {
49+ public static String getBoardConfig (boolean showErrorAlert ) throws LibimobiledeviceException {
5450 return getKeyFromConnectedDevice ("HardwareModel" , PlistType .STRING , showErrorAlert );
5551 }
5652
57- public static String getKeyFromConnectedDevice (String key , PlistType plistType , boolean showErrorAlert ) {
53+ public static String getKeyFromConnectedDevice (String key , PlistType plistType , boolean showErrorAlert ) throws LibimobiledeviceException {
5854 if (plistType == null ) {
5955 plistType = PlistType .STRING ;
6056 }
@@ -89,7 +85,7 @@ public static String getKeyFromConnectedDevice(String key, PlistType plistType,
8985 }
9086 }
9187
92- public static Pointer lockdowndClientFromConnectedDevice (boolean showErrorAlert ) {
88+ public static Pointer lockdowndClientFromConnectedDevice (boolean showErrorAlert ) throws LibimobiledeviceException {
9389 PointerByReference device = new PointerByReference ();
9490 throwIfNeeded (idevice_new (device , Pointer .NULL ), showErrorAlert , ErrorCodeType .idevice_error_t );
9591 PointerByReference client = new PointerByReference ();
@@ -102,45 +98,45 @@ public static Pointer lockdowndClientFromConnectedDevice(boolean showErrorAlert)
10298 return client .getValue ();
10399 }
104100
105- public static void enterRecovery (boolean showErrorAlert ) {
101+ public static void enterRecovery (boolean showErrorAlert ) throws LibimobiledeviceException {
106102 Pointer client = lockdowndClientFromConnectedDevice (true );
107103 throwIfNeeded (lockdownd_enter_recovery (client ), showErrorAlert , ErrorCodeType .lockdownd_error_t );
108104 lockdownd_client_free (client );
109105 }
110106
111- public static void exitRecovery (Pointer irecvClient , boolean showErrorAlert ) {
112- throwIfNeeded (Libirecovery .irecv_setenv (irecvClient , "auto-boot" , "true" ), showErrorAlert , ErrorCodeType .irecv_error_t );
113- throwIfNeeded (Libirecovery .irecv_saveenv (irecvClient ), showErrorAlert , ErrorCodeType .irecv_error_t );
114- throwIfNeeded (Libirecovery .irecv_reboot (irecvClient ), showErrorAlert , ErrorCodeType .irecv_error_t );
115- throwIfNeeded (Libirecovery .irecv_close (irecvClient ), showErrorAlert , ErrorCodeType .irecv_error_t );
107+ public static void exitRecovery (Pointer irecvClient , boolean showErrorAlert ) throws LibimobiledeviceException {
108+ throwIfNeeded (Libirecovery .setEnv (irecvClient , "auto-boot" , "true" ), showErrorAlert , ErrorCodeType .irecv_error_t );
109+ throwIfNeeded (Libirecovery .saveEnv (irecvClient ), showErrorAlert , ErrorCodeType .irecv_error_t );
110+ throwIfNeeded (Libirecovery .reboot (irecvClient ), showErrorAlert , ErrorCodeType .irecv_error_t );
111+ throwIfNeeded (Libirecovery .close (irecvClient ), showErrorAlert , ErrorCodeType .irecv_error_t );
116112 }
117113
118114 public static Task <String > createGetApnonceTask () {
119115 return new Task <String >() {
120116 @ Override
121- protected String call () {
117+ protected String call () throws LibimobiledeviceException {
122118 updateMessage ("Entering recovery mode...\n \n This can take up to 60 seconds" );
123119 System .out .println ("Entering recovery mode" );
124- Libimobiledevice .enterRecovery (true );
120+ LibimobiledeviceUtil .enterRecovery (true );
125121 PointerByReference irecvClient = new PointerByReference ();
126122 long endTime = System .currentTimeMillis () + 60_000 ; // timeout is 60 seconds
127123 int errorCode = -3 ;
128124 while (errorCode == -3 && System .currentTimeMillis () < endTime ) {
129125 if (!sleep (1000 )) {
130126 return null ;
131127 }
132- errorCode = Libimobiledevice . Libirecovery .irecv_open_with_ecid (irecvClient , 0 );
128+ errorCode = Libirecovery .open (irecvClient );
133129 }
134- throwIfNeeded (errorCode , true , Libimobiledevice . ErrorCodeType .irecv_error_t );
135- Libirecovery .irecv_device_info deviceInfo = Libimobiledevice . Libirecovery .irecv_get_device_info (irecvClient .getValue ());
130+ throwIfNeeded (errorCode , true , ErrorCodeType .irecv_error_t );
131+ Libirecovery .irecv_device_info deviceInfo = Libirecovery .getDeviceInfo (irecvClient .getValue ());
136132 final StringBuilder apnonce = new StringBuilder ();
137133 for (byte b : deviceInfo .ap_nonce .getByteArray (0 , deviceInfo .ap_nonce_size )) {
138134 apnonce .append (String .format ("%02x" , b ));
139135 }
140136 System .out .println ("Got apnonce" );
141137 updateMessage ("Successfully got apnonce, exiting recovery mode..." );
142138 System .out .println ("Exiting recovery mode" );
143- Libimobiledevice .exitRecovery (irecvClient .getValue (), true );
139+ LibimobiledeviceUtil .exitRecovery (irecvClient .getValue (), true );
144140 sleep (3000 );
145141 return apnonce .toString ();
146142 }
@@ -167,78 +163,7 @@ enum ErrorCodeType {
167163 idevice_error_t , lockdownd_error_t , irecv_error_t
168164 }
169165
170- public static native int lockdownd_get_value (Pointer client , Pointer domain , String key , PointerByReference value );
171-
172- public static native int lockdownd_enter_recovery (Pointer client );
173-
174- public static native int idevice_new (PointerByReference device , Pointer udid );
175-
176- public static native int lockdownd_client_new (Pointer device , PointerByReference client , String label );
177-
178- public static native int lockdownd_pair (Pointer lockdownd_client , Pointer lockdownd_pair_record );
179-
180- public static native void lockdownd_client_free (Pointer client );
181-
182- public static native void idevice_free (Pointer idevice );
183-
184- public static class Libplist {
185-
186- public static void getStringVal (Pointer plist , PointerByReference value ) {
187- plist_get_string_val (plist , value );
188- }
189-
190- public static void free (Pointer plist ) {
191- plist_free (plist );
192- }
193-
194- public static void toXml (Pointer plist , PointerByReference plist_xml , PointerByReference length ) {
195- plist_to_xml (plist , plist_xml , length );
196- }
197-
198- private static native void plist_get_string_val (Pointer plist , PointerByReference value );
199-
200- private static native void plist_free (Pointer plist );
201-
202- private static native void plist_to_xml (Pointer plist , PointerByReference plist_xml , PointerByReference length );
203-
204- static {
205- Native .register (Libplist .class , "plist" );
206- }
207- }
208-
209- public static class Libirecovery {
210-
211- public static native int irecv_open_with_ecid (PointerByReference irecv_client , long ecid );
212-
213- public static native int irecv_close (Pointer irecv_client );
214-
215- public static native int irecv_setenv (Pointer irecv_client , String variable , String value );
216-
217- public static native int irecv_saveenv (Pointer irecv_client );
218-
219- public static native int irecv_reboot (Pointer irecv_client );
220-
221- public static native irecv_device_info irecv_get_device_info (Pointer irecv_client );
222-
223- @ SuppressWarnings ({"unused" , "SpellCheckingInspection" })
224- @ Structure .FieldOrder ({"cpid" , "cprv" , "cpfm" , "scep" , "bdid" , "ecid" , "ibfl" , "srnm" , "imei" , "srtg" , "serial_string" , "ap_nonce" , "ap_nonce_size" , "sep_nonce" , "sep_nonce_size" })
225- public static class irecv_device_info extends Structure {
226- public int cpid , cprv , cpfm , scep , bdid ;
227- public long ecid ;
228- public int ibfl ;
229- public String srnm , imei , srtg , serial_string ;
230- public Pointer ap_nonce ;
231- public int ap_nonce_size ;
232- public Pointer sep_nonce ;
233- public int sep_nonce_size ;
234- }
235-
236- static {
237- Native .register (Libirecovery .class , "irecovery" );
238- }
239- }
240-
241- public static void throwIfNeeded (int errorCode , boolean showAlert , ErrorCodeType errorType ) {
166+ public static void throwIfNeeded (int errorCode , boolean showAlert , ErrorCodeType errorType ) throws LibimobiledeviceException {
242167 if (errorCode == 0 ) {
243168 return ;
244169 }
@@ -320,17 +245,8 @@ public static void throwIfNeeded(int errorCode, boolean showAlert, ErrorCodeType
320245 throw new LibimobiledeviceException (exceptionMessage );
321246 }
322247
323- static {
324- try {
325- Native .register (Libimobiledevice .class , "imobiledevice" );
326- } catch (Throwable e ) { // need to catch UnsatisfiedLinkError
327- Utils .showReportableError ("Error: unable to register native methods" , Utils .exceptionToString (e ));
328- throw new LibimobiledeviceException ("Unable to register native methods" , e );
329- }
330- }
331-
332248 @ SuppressWarnings ("unused" )
333- public static class LibimobiledeviceException extends RuntimeException {
249+ public static class LibimobiledeviceException extends Exception {
334250 public LibimobiledeviceException () {
335251 super ();
336252 }
0 commit comments