-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathlibirecovery.c
3420 lines (2951 loc) · 101 KB
/
libirecovery.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libirecovery.c
* Communication to iBoot/iBSS on Apple iOS devices via USB
*
* Copyright (c) 2011-2020 Nikias Bassen <nikias@gmx.li>
* Copyright (c) 2012-2020 Martin Szulecki <martin.szulecki@libimobiledevice.org>
* Copyright (c) 2010 Chronic-Dev Team
* Copyright (c) 2010 Joshua Hill
* Copyright (c) 2008-2011 Nicolas Haunold
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <libimobiledevice-glue/collection.h>
#include <libimobiledevice-glue/thread.h>
#ifndef USE_DUMMY
#ifndef WIN32
#ifndef HAVE_IOKIT
#include <libusb.h>
#if (defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)) || (defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x01000102))
#define HAVE_LIBUSB_HOTPLUG_API 1
#endif
#else
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/IOCFPlugIn.h>
#endif
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <setupapi.h>
#ifndef sleep
#define sleep(n) Sleep(1000 * n)
#endif
#endif
#endif
#ifdef WIN32
#define IRECV_API __declspec( dllexport )
#else
#ifdef HAVE_FVISIBILITY
#define IRECV_API __attribute__((visibility("default")))
#else
#define IRECV_API
#endif
#endif
#include "libirecovery.h"
struct irecv_client_private {
int debug;
int usb_config;
int usb_interface;
int usb_alt_interface;
unsigned int mode;
struct irecv_device_info device_info;
#ifndef USE_DUMMY
#ifndef WIN32
#ifndef HAVE_IOKIT
libusb_device_handle* handle;
#else
IOUSBDeviceInterface320 **handle;
IOUSBInterfaceInterface300 **usbInterface;
#endif
#else
HANDLE handle;
HANDLE hDFU;
HANDLE hIB;
LPSTR iBootPath;
LPSTR DfuPath;
#endif
irecv_event_cb_t progress_callback;
irecv_event_cb_t received_callback;
irecv_event_cb_t connected_callback;
irecv_event_cb_t precommand_callback;
irecv_event_cb_t postcommand_callback;
irecv_event_cb_t disconnected_callback;
#endif
};
#define USB_TIMEOUT 10000
#define APPLE_VENDOR_ID 0x05AC
#define BUFFER_SIZE 0x1000
#define debug(...) if(libirecovery_debug) fprintf(stderr, __VA_ARGS__)
static int libirecovery_debug = 0;
#ifndef USE_DUMMY
#ifndef WIN32
#ifndef HAVE_IOKIT
static libusb_context* libirecovery_context = NULL;
#endif
#endif
#endif
static struct irecv_device irecv_devices[] = {
/* iPhone */
{ "iPhone1,1", "m68ap", 0x00, 0x8900, "iPhone 2G" },
{ "iPhone1,2", "n82ap", 0x04, 0x8900, "iPhone 3G" },
{ "iPhone2,1", "n88ap", 0x00, 0x8920, "iPhone 3Gs" },
{ "iPhone3,1", "n90ap", 0x00, 0x8930, "iPhone 4 (GSM)" },
{ "iPhone3,2", "n90bap", 0x04, 0x8930, "iPhone 4 (GSM) R2 2012" },
{ "iPhone3,3", "n92ap", 0x06, 0x8930, "iPhone 4 (CDMA)" },
{ "iPhone4,1", "n94ap", 0x08, 0x8940, "iPhone 4s" },
{ "iPhone5,1", "n41ap", 0x00, 0x8950, "iPhone 5 (GSM)" },
{ "iPhone5,2", "n42ap", 0x02, 0x8950, "iPhone 5 (Global)" },
{ "iPhone5,3", "n48ap", 0x0a, 0x8950, "iPhone 5c (GSM)" },
{ "iPhone5,4", "n49ap", 0x0e, 0x8950, "iPhone 5c (Global)" },
{ "iPhone6,1", "n51ap", 0x00, 0x8960, "iPhone 5s (GSM)" },
{ "iPhone6,2", "n53ap", 0x02, 0x8960, "iPhone 5s (Global)" },
{ "iPhone7,1", "n56ap", 0x04, 0x7000, "iPhone 6 Plus" },
{ "iPhone7,2", "n61ap", 0x06, 0x7000, "iPhone 6" },
{ "iPhone8,1", "n71ap", 0x04, 0x8000, "iPhone 6s" },
{ "iPhone8,1", "n71map", 0x04, 0x8003, "iPhone 6s" },
{ "iPhone8,2", "n66ap", 0x06, 0x8000, "iPhone 6s Plus" },
{ "iPhone8,2", "n66map", 0x06, 0x8003, "iPhone 6s Plus" },
{ "iPhone8,4", "n69ap", 0x02, 0x8003, "iPhone SE (1st gen)" },
{ "iPhone8,4", "n69uap", 0x02, 0x8000, "iPhone SE (1st gen)" },
{ "iPhone9,1", "d10ap", 0x08, 0x8010, "iPhone 7 (Global)" },
{ "iPhone9,2", "d11ap", 0x0a, 0x8010, "iPhone 7 Plus (Global)" },
{ "iPhone9,3", "d101ap", 0x0c, 0x8010, "iPhone 7 (GSM)" },
{ "iPhone9,4", "d111ap", 0x0e, 0x8010, "iPhone 7 Plus (GSM)" },
{ "iPhone10,1", "d20ap", 0x02, 0x8015, "iPhone 8 (Global)" },
{ "iPhone10,2", "d21ap", 0x04, 0x8015, "iPhone 8 Plus (Global)" },
{ "iPhone10,3", "d22ap", 0x06, 0x8015, "iPhone X (Global)" },
{ "iPhone10,4", "d201ap", 0x0a, 0x8015, "iPhone 8 (GSM)" },
{ "iPhone10,5", "d211ap", 0x0c, 0x8015, "iPhone 8 Plus (GSM)" },
{ "iPhone10,6", "d221ap", 0x0e, 0x8015, "iPhone X (GSM)" },
{ "iPhone11,2", "d321ap", 0x0e, 0x8020, "iPhone XS" },
{ "iPhone11,4", "d331ap", 0x0a, 0x8020, "iPhone XS Max (China)" },
{ "iPhone11,6", "d331pap", 0x1a, 0x8020, "iPhone XS Max" },
{ "iPhone11,8", "n841ap", 0x0c, 0x8020, "iPhone XR" },
{ "iPhone12,1", "n104ap", 0x04, 0x8030, "iPhone 11" },
{ "iPhone12,3", "d421ap", 0x06, 0x8030, "iPhone 11 Pro" },
{ "iPhone12,5", "d431ap", 0x02, 0x8030, "iPhone 11 Pro Max" },
{ "iPhone12,8", "d79ap", 0x10, 0x8030, "iPhone SE (2nd gen)" },
{ "iPhone13,1", "d52gap", 0x0A, 0x8101, "iPhone 12 mini" },
{ "iPhone13,2", "d53gap", 0x0C, 0x8101, "iPhone 12" },
{ "iPhone13,3", "d53pap", 0x0E, 0x8101, "iPhone 12 Pro" },
{ "iPhone13,4", "d54pap", 0x08, 0x8101, "iPhone 12 Pro Max" },
{ "iPhone14,2", "d63ap", 0x0C, 0x8110, "iPhone 13 Pro" },
{ "iPhone14,3", "d64ap", 0x0E, 0x8110, "iPhone 13 Pro Max" },
{ "iPhone14,4", "d16ap", 0x08, 0x8110, "iPhone 13 mini" },
{ "iPhone14,5", "d17ap", 0x0A, 0x8110, "iPhone 13" },
{ "iPhone14,6", "d49ap", 0x10, 0x8110, "iPhone SE (3rd gen)" },
{ "iPhone14,7", "d27ap", 0x18, 0x8110, "iPhone 14" },
{ "iPhone14,8", "d28ap", 0x1A, 0x8110, "iPhone 14 Plus" },
{ "iPhone15,2", "d73ap", 0x0C, 0x8120, "iPhone 14 Pro" },
{ "iPhone15,3", "d74ap", 0x0E, 0x8120, "iPhone 14 Pro Max" },
/* iPod */
{ "iPod1,1", "n45ap", 0x02, 0x8900, "iPod Touch (1st gen)" },
{ "iPod2,1", "n72ap", 0x00, 0x8720, "iPod Touch (2nd gen)" },
{ "iPod3,1", "n18ap", 0x02, 0x8922, "iPod Touch (3rd gen)" },
{ "iPod4,1", "n81ap", 0x08, 0x8930, "iPod Touch (4th gen)" },
{ "iPod5,1", "n78ap", 0x00, 0x8942, "iPod Touch (5th gen)" },
{ "iPod7,1", "n102ap", 0x10, 0x7000, "iPod Touch (6th gen)" },
{ "iPod9,1", "n112ap", 0x16, 0x8010, "iPod Touch (7th gen)" },
/* iPad */
{ "iPad1,1", "k48ap", 0x02, 0x8930, "iPad" },
{ "iPad2,1", "k93ap", 0x04, 0x8940, "iPad 2 (WiFi)" },
{ "iPad2,2", "k94ap", 0x06, 0x8940, "iPad 2 (GSM)" },
{ "iPad2,3", "k95ap", 0x02, 0x8940, "iPad 2 (CDMA)" },
{ "iPad2,4", "k93aap", 0x06, 0x8942, "iPad 2 (WiFi) R2 2012" },
{ "iPad2,5", "p105ap", 0x0a, 0x8942, "iPad mini (WiFi)" },
{ "iPad2,6", "p106ap", 0x0c, 0x8942, "iPad mini (GSM)" },
{ "iPad2,7", "p107ap", 0x0e, 0x8942, "iPad mini (Global)" },
{ "iPad3,1", "j1ap", 0x00, 0x8945, "iPad (3rd gen, WiFi)" },
{ "iPad3,2", "j2ap", 0x02, 0x8945, "iPad (3rd gen, CDMA)" },
{ "iPad3,3", "j2aap", 0x04, 0x8945, "iPad (3rd gen, GSM)" },
{ "iPad3,4", "p101ap", 0x00, 0x8955, "iPad (4th gen, WiFi)" },
{ "iPad3,5", "p102ap", 0x02, 0x8955, "iPad (4th gen, GSM)" },
{ "iPad3,6", "p103ap", 0x04, 0x8955, "iPad (4th gen, Global)" },
{ "iPad4,1", "j71ap", 0x10, 0x8960, "iPad Air (WiFi)" },
{ "iPad4,2", "j72ap", 0x12, 0x8960, "iPad Air (Cellular)" },
{ "iPad4,3", "j73ap", 0x14, 0x8960, "iPad Air (China)" },
{ "iPad4,4", "j85ap", 0x0a, 0x8960, "iPad mini 2 (WiFi)" },
{ "iPad4,5", "j86ap", 0x0c, 0x8960, "iPad mini 2 (Cellular)" },
{ "iPad4,6", "j87ap", 0x0e, 0x8960, "iPad mini 2 (China)" },
{ "iPad4,7", "j85map", 0x32, 0x8960, "iPad mini 3 (WiFi)" },
{ "iPad4,8", "j86map", 0x34, 0x8960, "iPad mini 3 (Cellular)" },
{ "iPad4,9", "j87map", 0x36, 0x8960, "iPad mini 3 (China)" },
{ "iPad5,1", "j96ap", 0x08, 0x7000, "iPad mini 4 (WiFi)" },
{ "iPad5,2", "j97ap", 0x0A, 0x7000, "iPad mini 4 (Cellular)" },
{ "iPad5,3", "j81ap", 0x06, 0x7001, "iPad Air 2 (WiFi)" },
{ "iPad5,4", "j82ap", 0x02, 0x7001, "iPad Air 2 (Cellular)" },
{ "iPad6,3", "j127ap", 0x08, 0x8001, "iPad Pro 9.7-inch (WiFi)" },
{ "iPad6,4", "j128ap", 0x0a, 0x8001, "iPad Pro 9.7-inch (Cellular)" },
{ "iPad6,7", "j98aap", 0x10, 0x8001, "iPad Pro 12.9-inch (1st gen, WiFi)" },
{ "iPad6,8", "j99aap", 0x12, 0x8001, "iPad Pro 12.9-inch (1st gen, Cellular)" },
{ "iPad6,11", "j71sap", 0x10, 0x8000, "iPad (5th gen, WiFi)" },
{ "iPad6,11", "j71tap", 0x10, 0x8003, "iPad (5th gen, WiFi)" },
{ "iPad6,12", "j72sap", 0x12, 0x8000, "iPad (5th gen, Cellular)" },
{ "iPad6,12", "j72tap", 0x12, 0x8003, "iPad (5th gen, Cellular)" },
{ "iPad7,1", "j120ap", 0x0C, 0x8011, "iPad Pro 12.9-inch (2nd gen, WiFi)" },
{ "iPad7,2", "j121ap", 0x0E, 0x8011, "iPad Pro 12.9-inch (2nd gen, Cellular)" },
{ "iPad7,3", "j207ap", 0x04, 0x8011, "iPad Pro 10.5-inch (WiFi)" },
{ "iPad7,4", "j208ap", 0x06, 0x8011, "iPad Pro 10.5-inch (Cellular)" },
{ "iPad7,5", "j71bap", 0x18, 0x8010, "iPad (6th gen, WiFi)" },
{ "iPad7,6", "j72bap", 0x1A, 0x8010, "iPad (6th gen, Cellular)" },
{ "iPad7,11", "j171ap", 0x1C, 0x8010, "iPad (7th gen, WiFi)" },
{ "iPad7,12", "j172ap", 0x1E, 0x8010, "iPad (7th gen, Cellular)" },
{ "iPad8,1", "j317ap", 0x0C, 0x8027, "iPad Pro 11-inch (1st gen, WiFi)" },
{ "iPad8,2", "j317xap", 0x1C, 0x8027, "iPad Pro 11-inch (1st gen, WiFi, 1TB)" },
{ "iPad8,3", "j318ap", 0x0E, 0x8027, "iPad Pro 11-inch (1st gen, Cellular)" },
{ "iPad8,4", "j318xap", 0x1E, 0x8027, "iPad Pro 11-inch (1st gen, Cellular, 1TB)" },
{ "iPad8,5", "j320ap", 0x08, 0x8027, "iPad Pro 12.9-inch (3rd gen, WiFi)" },
{ "iPad8,6", "j320xap", 0x18, 0x8027, "iPad Pro 12.9-inch (3rd gen, WiFi, 1TB)" },
{ "iPad8,7", "j321ap", 0x0A, 0x8027, "iPad Pro 12.9-inch (3rd gen, Cellular)" },
{ "iPad8,8", "j321xap", 0x1A, 0x8027, "iPad Pro 12.9-inch (3rd gen, Cellular, 1TB)" },
{ "iPad8,9", "j417ap", 0x3C, 0x8027, "iPad Pro 11-inch (2nd gen, WiFi)" },
{ "iPad8,10", "j418ap", 0x3E, 0x8027, "iPad Pro 11-inch (2nd gen, Cellular)" },
{ "iPad8,11", "j420ap", 0x38, 0x8027, "iPad Pro 12.9-inch (4th gen, WiFi)" },
{ "iPad8,12", "j421ap", 0x3A, 0x8027, "iPad Pro 12.9-inch (4th gen, Cellular)" },
{ "iPad11,1", "j210ap", 0x14, 0x8020, "iPad mini (5th gen, WiFi)" },
{ "iPad11,2", "j211ap", 0x16, 0x8020, "iPad mini (5th gen, Cellular)" },
{ "iPad11,3", "j217ap", 0x1C, 0x8020, "iPad Air (3rd gen, WiFi)" },
{ "iPad11,4", "j218ap", 0x1E, 0x8020, "iPad Air (3rd gen, Cellular)" },
{ "iPad11,6", "j171aap", 0x24, 0x8020, "iPad (8th gen, WiFi)" },
{ "iPad11,7", "j172aap", 0x26, 0x8020, "iPad (8th gen, Cellular)" },
{ "iPad12,1", "j181ap", 0x18, 0x8030, "iPad (9th gen, WiFi)" },
{ "iPad12,2", "j182ap", 0x1A, 0x8030, "iPad (9th gen, Cellular)" },
{ "iPad13,1", "j307ap", 0x04, 0x8101, "iPad Air (4th gen, WiFi)" },
{ "iPad13,2", "j308ap", 0x06, 0x8101, "iPad Air (4th gen, Cellular)" },
{ "iPad13,4", "j517ap", 0x08, 0x8103, "iPad Pro 11-inch (3rd gen, WiFi)" },
{ "iPad13,5", "j517xap", 0x0A, 0x8103, "iPad Pro 11-inch (3rd gen, WiFi, 2TB)" },
{ "iPad13,6", "j518ap", 0x0C, 0x8103, "iPad Pro 11-inch (3rd gen, Cellular)" },
{ "iPad13,7", "j518xap", 0x0E, 0x8103, "iPad Pro 11-inch (3rd gen, Cellular, 2TB)" },
{ "iPad13,8", "j522ap", 0x18, 0x8103, "iPad Pro 12.9-inch (5th gen, WiFi)" },
{ "iPad13,9", "j522xap", 0x1A, 0x8103, "iPad Pro 12.9-inch (5th gen, WiFi, 2TB)" },
{ "iPad13,10", "j523ap", 0x1C, 0x8103, "iPad Pro 12.9-inch (5th gen, Cellular)" },
{ "iPad13,11", "j523xap", 0x1E, 0x8103, "iPad Pro 12.9-inch (5th gen, Cellular, 2TB)" },
{ "iPad13,16", "j407ap", 0x10, 0x8103, "iPad Air (5th gen, WiFi)" },
{ "iPad13,17", "j408ap", 0x12, 0x8103, "iPad Air (5th gen, Cellular)" },
{ "iPad13,18", "j271ap", 0x14, 0x8101, "iPad (10th gen, WiFi)" },
{ "iPad13,19", "j272ap", 0x16, 0x8101, "iPad (10th gen, Cellular)" },
{ "iPad14,1", "j310ap", 0x04, 0x8110, "iPad mini (6th gen, WiFi)" },
{ "iPad14,2", "j311ap", 0x06, 0x8110, "iPad mini (6th gen, Cellular)" },
{ "iPad14,3", "j617ap", 0x08, 0x8112, "iPad Pro 11-inch (4th gen, WiFi)" },
{ "iPad14,4", "j618ap", 0x0A, 0x8112, "iPad Pro 11-inch (4th gen, Cellular)" },
{ "iPad14,5", "j620ap", 0x0C, 0x8112, "iPad Pro 12.9-inch (6th gen, WiFi)" },
{ "iPad14,6", "j621ap", 0x0E, 0x8112, "iPad Pro 12.9-inch (6th gen, Cellular)" },
/* Apple TV */
{ "AppleTV2,1", "k66ap", 0x10, 0x8930, "Apple TV 2" },
{ "AppleTV3,1", "j33ap", 0x08, 0x8942, "Apple TV 3" },
{ "AppleTV3,2", "j33iap", 0x00, 0x8947, "Apple TV 3 (2013)" },
{ "AppleTV5,3", "j42dap", 0x34, 0x7000, "Apple TV 4" },
{ "AppleTV6,2", "j105aap", 0x02, 0x8011, "Apple TV 4K" },
{ "AppleTV11,1", "j305ap", 0x08, 0x8020, "Apple TV 4K (2nd gen)" },
{ "AppleTV14,1", "j255ap", 0x02, 0x8110, "Apple TV 4K (3rd gen)" },
/* HomePod */
{ "AudioAccessory1,1", "b238aap", 0x38, 0x7000, "HomePod" },
{ "AudioAccessory1,2", "b238ap", 0x1A, 0x7000, "HomePod" },
{ "AudioAccessory5,1", "b520ap", 0x22, 0x8006, "HomePod mini" },
/* Apple Watch */
{ "Watch1,1", "n27aap", 0x02, 0x7002, "Apple Watch 38mm (1st gen)" },
{ "Watch1,2", "n28aap", 0x04, 0x7002, "Apple Watch 42mm (1st gen)" },
{ "Watch2,6", "n27dap", 0x02, 0x8002, "Apple Watch Series 1 (38mm)" },
{ "Watch2,7", "n28dap", 0x04, 0x8002, "Apple Watch Series 1 (42mm)" },
{ "Watch2,3", "n74ap", 0x0C, 0x8002, "Apple Watch Series 2 (38mm)" },
{ "Watch2,4", "n75ap", 0x0E, 0x8002, "Apple Watch Series 2 (42mm)" },
{ "Watch3,1", "n111sap", 0x1C, 0x8004, "Apple Watch Series 3 (38mm Cellular)" },
{ "Watch3,2", "n111bap", 0x1E, 0x8004, "Apple Watch Series 3 (42mm Cellular)" },
{ "Watch3,3", "n121sap", 0x18, 0x8004, "Apple Watch Series 3 (38mm)" },
{ "Watch3,4", "n121bap", 0x1A, 0x8004, "Apple Watch Series 3 (42mm)" },
{ "Watch4,1", "n131sap", 0x08, 0x8006, "Apple Watch Series 4 (40mm)" },
{ "Watch4,2", "n131bap", 0x0A, 0x8006, "Apple Watch Series 4 (44mm)" },
{ "Watch4,3", "n141sap", 0x0C, 0x8006, "Apple Watch Series 4 (40mm Cellular)" },
{ "Watch4,4", "n141bap", 0x0E, 0x8006, "Apple Watch Series 4 (44mm Cellular)" },
{ "Watch5,1", "n144sap", 0x10, 0x8006, "Apple Watch Series 5 (40mm)" },
{ "Watch5,2", "n144bap", 0x12, 0x8006, "Apple Watch Series 5 (44mm)" },
{ "Watch5,3", "n146sap", 0x14, 0x8006, "Apple Watch Series 5 (40mm Cellular)" },
{ "Watch5,4", "n146bap", 0x16, 0x8006, "Apple Watch Series 5 (44mm Cellular)" },
{ "Watch5,9", "n140sap", 0x28, 0x8006, "Apple Watch SE (40mm)" },
{ "Watch5,10", "n140bap", 0x2A, 0x8006, "Apple Watch SE (44mm)" },
{ "Watch5,11", "n142sap", 0x2C, 0x8006, "Apple Watch SE (40mm Cellular)" },
{ "Watch5,12", "n142bap", 0x2E, 0x8006, "Apple Watch SE (44mm Cellular)" },
{ "Watch6,1", "n157sap", 0x08, 0x8301, "Apple Watch Series 6 (40mm)" },
{ "Watch6,2", "n157bap", 0x0A, 0x8301, "Apple Watch Series 6 (44mm)" },
{ "Watch6,3", "n158sap", 0x0C, 0x8301, "Apple Watch Series 6 (40mm Cellular)" },
{ "Watch6,4", "n158bap", 0x0E, 0x8301, "Apple Watch Series 6 (44mm Cellular)" },
{ "Watch6,6", "n187sap", 0x10, 0x8301, "Apple Watch Series 7 (41mm)" },
{ "Watch6,7", "n187bap", 0x12, 0x8301, "Apple Watch Series 7 (45mm)" },
{ "Watch6,8", "n188sap", 0x14, 0x8301, "Apple Watch Series 7 (41mm Cellular)" },
{ "Watch6,9", "n188bap", 0x16, 0x8301, "Apple Watch Series 7 (45mm Cellular)" },
{ "Watch6,10", "n143sap", 0x28, 0x8301, "Apple Watch SE 2 (40mm)" },
{ "Watch6,11", "n143bap", 0x2A, 0x8301, "Apple Watch SE 2 (44mm)" },
{ "Watch6,12", "n149sap", 0x2C, 0x8301, "Apple Watch SE 2 (40mm Cellular)" },
{ "Watch6,13", "n149bap", 0x2E, 0x8301, "Apple Watch SE 2 (44mm Cellular)" },
{ "Watch6,14", "n197sap", 0x30, 0x8301, "Apple Watch Series 8 (41mm)" },
{ "Watch6,15", "n197bap", 0x32, 0x8301, "Apple Watch Series 8 (45mm)" },
{ "Watch6,16", "n198sap", 0x34, 0x8301, "Apple Watch Series 8 (41mm Cellular)" },
{ "Watch6,17", "n198bap", 0x36, 0x8301, "Apple Watch Series 8 (45mm Cellular)" },
{ "Watch6,18", "n199ap", 0x26, 0x8301, "Apple Watch Ultra" },
/* Apple Silicon Macs */
{ "ADP3,2", "j273aap", 0x42, 0x8027, "Developer Transition Kit (2020)" },
{ "Macmini9,1", "j274ap", 0x22, 0x8103, "Mac mini (M1, 2020)" },
{ "MacBookPro17,1", "j293ap", 0x24, 0x8103, "MacBook Pro (M1, 13-inch, 2020)" },
{ "MacBookPro18,1", "j316sap", 0x0A, 0x6000, "MacBook Pro (M1 Pro, 16-inch, 2021)" },
{ "MacBookPro18,2", "j316cap", 0x0A, 0x6001, "MacBook Pro (M1 Max, 16-inch, 2021)" },
{ "MacBookPro18,3", "j314sap", 0x08, 0x6000, "MacBook Pro (M1 Pro, 14-inch, 2021)" },
{ "MacBookPro18,4", "j314cap", 0x08, 0x6001, "MacBook Pro (M1 Max, 14-inch, 2021)" },
{ "MacBookAir10,1", "j313ap", 0x26, 0x8103, "MacBook Air (M1, 2020)" },
{ "iMac21,1", "j456ap", 0x28, 0x8103, "iMac 24-inch (M1, Two Ports, 2021)" },
{ "iMac21,2", "j457ap", 0x2A, 0x8103, "iMac 24-inch (M1, Four Ports, 2021)" },
{ "Mac13,1", "j375cap", 0x04, 0x6001, "Mac Studio (M1 Max, 2022)" },
{ "Mac13,2", "j375dap", 0x0C, 0x6002, "Mac Studio (M1 Ultra, 2022)" },
{ "Mac14,2", "j413ap", 0x28, 0x8112, "MacBook Air (M2, 2022)" },
{ "Mac14,7", "j493ap", 0x2A, 0x8112, "MacBook Pro (M2, 13-inch, 2022)" },
/* Apple Silicon VMs (supported by Virtualization.framework on macOS 12) */
{ "VirtualMac2,1", "vma2macosap", 0x20, 0xFE00, "Apple Virtual Machine 1" },
/* Apple T2 Coprocessor */
{ "iBridge2,1", "j137ap", 0x0A, 0x8012, "Apple T2 iMacPro1,1 (j137)" },
{ "iBridge2,3", "j680ap", 0x0B, 0x8012, "Apple T2 MacBookPro15,1 (j680)" },
{ "iBridge2,4", "j132ap", 0x0C, 0x8012, "Apple T2 MacBookPro15,2 (j132)" },
{ "iBridge2,5", "j174ap", 0x0E, 0x8012, "Apple T2 Macmini8,1 (j174)" },
{ "iBridge2,6", "j160ap", 0x0F, 0x8012, "Apple T2 MacPro7,1 (j160)" },
{ "iBridge2,7", "j780ap", 0x07, 0x8012, "Apple T2 MacBookPro15,3 (j780)" },
{ "iBridge2,8", "j140kap", 0x17, 0x8012, "Apple T2 MacBookAir8,1 (j140k)" },
{ "iBridge2,10", "j213ap", 0x18, 0x8012, "Apple T2 MacBookPro15,4 (j213)" },
{ "iBridge2,12", "j140aap", 0x37, 0x8012, "Apple T2 MacBookAir8,2 (j140a)" },
{ "iBridge2,14", "j152fap", 0x3A, 0x8012, "Apple T2 MacBookPro16,1 (j152f)" },
{ "iBridge2,15", "j230kap", 0x3F, 0x8012, "Apple T2 MacBookAir9,1 (j230k)" },
{ "iBridge2,16", "j214kap", 0x3E, 0x8012, "Apple T2 MacBookPro16,2 (j214k)" },
{ "iBridge2,19", "j185ap", 0x22, 0x8012, "Apple T2 iMac20,1 (j185)" },
{ "iBridge2,20", "j185fap", 0x23, 0x8012, "Apple T2 iMac20,2 (j185f)" },
{ "iBridge2,21", "j223ap", 0x3B, 0x8012, "Apple T2 MacBookPro16,3 (j223)" },
{ "iBridge2,22", "j215ap", 0x38, 0x8012, "Apple T2 MacBookPro16,4 (j215)" },
/* Apple Displays */
{ "AppleDisplay2,1", "j327ap", 0x22, 0x8030, "Studio Display" },
{ NULL, NULL, -1, -1, NULL }
};
#ifndef USE_DUMMY
static unsigned int crc32_lookup_t1[256] = {
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
};
#define crc32_step(a,b) \
a = (crc32_lookup_t1[(a & 0xFF) ^ ((unsigned char)b)] ^ (a >> 8))
static THREAD_T th_event_handler = THREAD_T_NULL;
struct collection listeners;
static mutex_t listener_mutex;
struct collection devices;
static mutex_t device_mutex;
#ifndef WIN32
#ifdef HAVE_IOKIT
static CFRunLoopRef iokit_runloop = NULL;
#else
static libusb_context* irecv_hotplug_ctx = NULL;
#endif
#endif
static void _irecv_init(void)
{
char* dbglvl = getenv("LIBIRECOVERY_DEBUG_LEVEL");
if (dbglvl) {
libirecovery_debug = strtol(dbglvl, NULL, 0);
irecv_set_debug_level(libirecovery_debug);
}
#ifndef USE_DUMMY
#ifndef WIN32
#ifndef HAVE_IOKIT
libusb_init(&libirecovery_context);
#endif
#endif
collection_init(&listeners);
mutex_init(&listener_mutex);
#endif
}
static void _irecv_deinit(void)
{
#ifndef USE_DUMMY
#ifndef WIN32
#ifndef HAVE_IOKIT
if (libirecovery_context != NULL) {
libusb_exit(libirecovery_context);
libirecovery_context = NULL;
}
#endif
#endif
collection_free(&listeners);
mutex_destroy(&listener_mutex);
#endif
}
static thread_once_t init_once = THREAD_ONCE_INIT;
static thread_once_t deinit_once = THREAD_ONCE_INIT;
#ifndef HAVE_ATTRIBUTE_CONSTRUCTOR
#if defined(__llvm__) || defined(__GNUC__)
#define HAVE_ATTRIBUTE_CONSTRUCTOR
#endif
#endif
#ifdef HAVE_ATTRIBUTE_CONSTRUCTOR
static void __attribute__((constructor)) libirecovery_initialize(void)
{
thread_once(&init_once, _irecv_init);
}
static void __attribute__((destructor)) libirecovery_deinitialize(void)
{
thread_once(&deinit_once, _irecv_deinit);
}
#elif defined(WIN32)
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason) {
case DLL_PROCESS_ATTACH:
thread_once(&init_once, _irecv_init);
break;
case DLL_PROCESS_DETACH:
thread_once(&deinit_once, _irecv_deinit);
break;
default:
break;
}
return 1;
}
#else
#warning No compiler support for constructor/destructor attributes, some features might not be available.
#endif
#ifdef HAVE_IOKIT
static int iokit_get_string_descriptor_ascii(irecv_client_t client, uint8_t desc_index, unsigned char * buffer, int size) {
IOReturn result;
IOUSBDevRequest request;
unsigned char descriptor[256];
request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
request.bRequest = kUSBRqGetDescriptor;
request.wValue = (kUSBStringDesc << 8); // | desc_index;
request.wIndex = 0; // All languages 0x409; // language
request.wLength = sizeof(descriptor) - 1;
request.pData = descriptor;
request.wLenDone = 0;
result = (*client->handle)->DeviceRequest(client->handle, &request);
if (result == kIOReturnNoDevice)
return IRECV_E_NO_DEVICE;
if (result == kIOReturnNotOpen)
return IRECV_E_USB_STATUS;
if (result != kIOReturnSuccess)
return IRECV_E_UNKNOWN_ERROR;
if (descriptor[0] >= 4) { // && descriptor[2] == 0x9 && descriptor[3] == 0x4) {
request.wValue = (kUSBStringDesc << 8) | desc_index;
request.wIndex = descriptor[2] + (descriptor[3] << 8);
request.wLenDone = 0;
result = (*client->handle)->DeviceRequest(client->handle, &request);
if (result == kIOReturnNoDevice)
return IRECV_E_NO_DEVICE;
if (result == kIOReturnNotOpen)
return IRECV_E_USB_STATUS;
if (result != kIOReturnSuccess)
return IRECV_E_UNKNOWN_ERROR;
int i = 2, j = 0;
for ( ; i < descriptor[0]; i += 2, j += 1) {
buffer[j] = descriptor[i];
}
buffer[j] = 0;
return request.wLenDone;
}
return IRECV_E_UNKNOWN_ERROR;
}
#endif
static int irecv_get_string_descriptor_ascii(irecv_client_t client, uint8_t desc_index, unsigned char * buffer, int size) {
#ifndef WIN32
#ifdef HAVE_IOKIT
return iokit_get_string_descriptor_ascii(client, desc_index, buffer, size);
#else
return libusb_get_string_descriptor_ascii(client->handle, desc_index, buffer, size);
#endif
#else
irecv_error_t ret;
unsigned short langid = 0;
unsigned char data[255];
int di, si;
memset(data, 0, sizeof(data));
memset(buffer, 0, size);
ret = irecv_usb_control_transfer(client, 0x80, 0x06, (0x03 << 8) | desc_index, langid, data, sizeof(data), USB_TIMEOUT);
if (ret < 0) return ret;
if (data[1] != 0x03) return IRECV_E_UNKNOWN_ERROR;
if (data[0] > ret) return IRECV_E_UNKNOWN_ERROR;
for (di = 0, si = 2; si < data[0]; si += 2) {
if (di >= (size - 1)) break;
if (data[si + 1]) {
/* high byte */
buffer[di++] = '?';
} else {
buffer[di++] = data[si];
}
}
buffer[di] = 0;
return di;
#endif
}
static void irecv_load_device_info_from_iboot_string(irecv_client_t client, const char* iboot_string)
{
if (!client || !iboot_string) {
return;
}
memset(&client->device_info, '\0', sizeof(struct irecv_device_info));
client->device_info.serial_string = strdup(iboot_string);
char* ptr;
ptr = strstr(iboot_string, "CPID:");
if (ptr != NULL) {
sscanf(ptr, "CPID:%x", &client->device_info.cpid);
}
ptr = strstr(iboot_string, "CPRV:");
if (ptr != NULL) {
sscanf(ptr, "CPRV:%x", &client->device_info.cprv);
}
ptr = strstr(iboot_string, "CPFM:");
if (ptr != NULL) {
sscanf(ptr, "CPFM:%x", &client->device_info.cpfm);
}
ptr = strstr(iboot_string, "SCEP:");
if (ptr != NULL) {
sscanf(ptr, "SCEP:%x", &client->device_info.scep);
}
ptr = strstr(iboot_string, "BDID:");
if (ptr != NULL) {
sscanf(ptr, "BDID:%x", &client->device_info.bdid);
}
ptr = strstr(iboot_string, "ECID:");
if (ptr != NULL) {
sscanf(ptr, "ECID:%" SCNx64, &client->device_info.ecid);
}
ptr = strstr(iboot_string, "IBFL:");
if (ptr != NULL) {
sscanf(ptr, "IBFL:%x", &client->device_info.ibfl);
}
char tmp[256];
tmp[0] = '\0';
ptr = strstr(iboot_string, "SRNM:[");
if(ptr != NULL) {
sscanf(ptr, "SRNM:[%s]", tmp);
ptr = strrchr(tmp, ']');
if(ptr != NULL) {
*ptr = '\0';
}
client->device_info.srnm = strdup(tmp);
}
tmp[0] = '\0';
ptr = strstr(iboot_string, "IMEI:[");
if(ptr != NULL) {
sscanf(ptr, "IMEI:[%s]", tmp);
ptr = strrchr(tmp, ']');
if(ptr != NULL) {
*ptr = '\0';
}
client->device_info.imei = strdup(tmp);
}
tmp[0] = '\0';
ptr = strstr(iboot_string, "SRTG:[");
if(ptr != NULL) {
sscanf(ptr, "SRTG:[%s]", tmp);
ptr = strrchr(tmp, ']');
if(ptr != NULL) {
*ptr = '\0';
}
client->device_info.srtg = strdup(tmp);
}
}
static void irecv_copy_nonce_with_tag(irecv_client_t client, const char* tag, unsigned char** nonce, unsigned int* nonce_size)
{
if (!client || !tag) {
return;
}
char buf[255];
int len;
*nonce = NULL;
*nonce_size = 0;
len = irecv_get_string_descriptor_ascii(client, 1, (unsigned char*) buf, 255);
if (len < 0) {
debug("%s: got length: %d\n", __func__, len);
return;
}
buf[len] = 0;
int taglen = strlen(tag);
int nlen = 0;
char* nonce_string = NULL;
char* p = buf;
char* colon = NULL;
do {
colon = strchr(p, ':');
if (!colon)
break;
if (colon-taglen < p) {
break;
}
char *space = strchr(colon, ' ');
if (strncmp(colon-taglen, tag, taglen) == 0) {
p = colon+1;
if (!space) {
nlen = strlen(p);
} else {
nlen = space-p;
}
nonce_string = p;
nlen/=2;
break;
} else {
if (!space) {
break;
} else {
p = space+1;
}
}
} while (colon);
if (nlen == 0) {
debug("%s: WARNING: couldn't find tag %s in string %s\n", __func__, tag, buf);
return;
}
unsigned char *nn = malloc(nlen);
if (!nn) {
return;
}
int i = 0;
for (i = 0; i < nlen; i++) {
int val = 0;
if (sscanf(nonce_string+(i*2), "%02X", &val) == 1) {
nn[i] = (unsigned char)val;
} else {
debug("%s: ERROR: unexpected data in nonce result (%2s)\n", __func__, nonce_string+(i*2));
break;
}
}
if (i != nlen) {
debug("%s: ERROR: unable to parse nonce\n", __func__);
free(nn);
return;
}
*nonce = nn;
*nonce_size = nlen;
}
#ifdef WIN32
static const GUID GUID_DEVINTERFACE_IBOOT = {0xED82A167L, 0xD61A, 0x4AF6, {0x9A, 0xB6, 0x11, 0xE5, 0x22, 0x36, 0xC5, 0x76}};
static const GUID GUID_DEVINTERFACE_DFU = {0xB8085869L, 0xFEB9, 0x404B, {0x8C, 0xB1, 0x1E, 0x5C, 0x14, 0xFA, 0x8C, 0x54}};
typedef struct usb_control_request {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
char data[];
} usb_control_request;
irecv_error_t mobiledevice_openpipes(irecv_client_t client);
void mobiledevice_closepipes(irecv_client_t client);
irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid);
irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid) {
int found = 0;
SP_DEVICE_INTERFACE_DATA currentInterface;
HDEVINFO usbDevices;
DWORD i;
irecv_client_t _client = (irecv_client_t) malloc(sizeof(struct irecv_client_private));
memset(_client, 0, sizeof(struct irecv_client_private));
/* get DFU paths */
usbDevices = SetupDiGetClassDevs(&GUID_DEVINTERFACE_DFU, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
memset(¤tInterface, '\0', sizeof(SP_DEVICE_INTERFACE_DATA));
currentInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for(i = 0; usbDevices && SetupDiEnumDeviceInterfaces(usbDevices, NULL, &GUID_DEVINTERFACE_DFU, i, ¤tInterface); i++) {
free(_client->DfuPath);
_client->DfuPath = NULL;
_client->handle = NULL;
DWORD requiredSize = 0;
PSP_DEVICE_INTERFACE_DETAIL_DATA details;
SetupDiGetDeviceInterfaceDetail(usbDevices, ¤tInterface, NULL, 0, &requiredSize, NULL);
details = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(requiredSize);
details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if(!SetupDiGetDeviceInterfaceDetail(usbDevices, ¤tInterface, details, requiredSize, NULL, NULL)) {
free(details);
continue;
} else {
LPSTR result = (LPSTR) malloc(requiredSize - sizeof(DWORD));
memcpy((void*) result, details->DevicePath, requiredSize - sizeof(DWORD));
free(details);
_client->DfuPath = result;
if (mobiledevice_openpipes(_client) != IRECV_E_SUCCESS) {
mobiledevice_closepipes(_client);
continue;
}
if (ecid == IRECV_K_WTF_MODE) {
if (_client->mode != IRECV_K_WTF_MODE) {
/* special ecid case, ignore !IRECV_K_WTF_MODE */
continue;
} else {
ecid = 0;
}
}
if ((ecid != 0) && (_client->mode == IRECV_K_WTF_MODE)) {
/* we can't get ecid in WTF mode */
mobiledevice_closepipes(_client);
continue;
}
char serial_str[256];
serial_str[0] = '\0';
char *p = result;
while ((p = strstr(p, "\\usb"))) {
if (sscanf(p, "\\usb#vid_%*04x&pid_%*04x#%s", serial_str) == 1)
break;
p += 4;
}
if (serial_str[0] == '\0') {
mobiledevice_closepipes(_client);
continue;
}
p = strchr(serial_str, '#');
if (p) {
*p = '\0';
}
unsigned int j;
for (j = 0; j < strlen(serial_str); j++) {
if (serial_str[j] == '_') {
serial_str[j] = ' ';
} else {
serial_str[j] = toupper(serial_str[j]);
}
}
irecv_load_device_info_from_iboot_string(_client, serial_str);
irecv_copy_nonce_with_tag(_client, "NONC", &_client->device_info.ap_nonce, &_client->device_info.ap_nonce_size);
irecv_copy_nonce_with_tag(_client, "SNON", &_client->device_info.sep_nonce, &_client->device_info.sep_nonce_size);
if (ecid != 0) {
if (_client->device_info.ecid != ecid) {
mobiledevice_closepipes(_client);
continue;
}
debug("found device with ECID %016" PRIx64 "\n", (uint64_t)ecid);
}
found = 1;
break;
}
}
SetupDiDestroyDeviceInfoList(usbDevices);
if (found) {
*client = _client;
return IRECV_E_SUCCESS;
}
/* get iBoot path */
usbDevices = SetupDiGetClassDevs(&GUID_DEVINTERFACE_IBOOT, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
memset(¤tInterface, '\0', sizeof(SP_DEVICE_INTERFACE_DATA));
currentInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for(i = 0; usbDevices && SetupDiEnumDeviceInterfaces(usbDevices, NULL, &GUID_DEVINTERFACE_IBOOT, i, ¤tInterface); i++) {
free(_client->iBootPath);
_client->iBootPath = NULL;
_client->handle = NULL;
DWORD requiredSize = 0;
PSP_DEVICE_INTERFACE_DETAIL_DATA details;
SetupDiGetDeviceInterfaceDetail(usbDevices, ¤tInterface, NULL, 0, &requiredSize, NULL);
details = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(requiredSize);
details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if(!SetupDiGetDeviceInterfaceDetail(usbDevices, ¤tInterface, details, requiredSize, NULL, NULL)) {
free(details);
continue;
} else {
LPSTR result = (LPSTR) malloc(requiredSize - sizeof(DWORD));
memcpy((void*) result, details->DevicePath, requiredSize - sizeof(DWORD));
free(details);
_client->iBootPath = result;
if (mobiledevice_openpipes(_client) != IRECV_E_SUCCESS) {
mobiledevice_closepipes(_client);
continue;
}
if ((ecid != 0) && (_client->mode == IRECV_K_WTF_MODE)) {
/* we can't get ecid in WTF mode */
mobiledevice_closepipes(_client);
continue;
}
char serial_str[256];
serial_str[0] = '\0';
char *p = result;
while ((p = strstr(p, "\\usb"))) {
if (sscanf(p, "\\usb#vid_%*04x&pid_%*04x#%s", serial_str) == 1)
break;
p += 4;
}
if (serial_str[0] == '\0') {
mobiledevice_closepipes(_client);
continue;
}
p = strchr(serial_str, '#');
if (p) {
*p = '\0';
}
unsigned int j;
for (j = 0; j < strlen(serial_str); j++) {
if (serial_str[j] == '_') {
serial_str[j] = ' ';
} else {
serial_str[j] = toupper(serial_str[j]);
}
}
irecv_load_device_info_from_iboot_string(_client, serial_str);
irecv_copy_nonce_with_tag(_client, "NONC", &_client->device_info.ap_nonce, &_client->device_info.ap_nonce_size);
irecv_copy_nonce_with_tag(_client, "SNON", &_client->device_info.sep_nonce, &_client->device_info.sep_nonce_size);
if (ecid != 0) {
if (_client->device_info.ecid != ecid) {
mobiledevice_closepipes(_client);
continue;
}
debug("found device with ECID %016" PRIx64 "\n", (uint64_t)ecid);
}
found = 1;
break;
}
}
SetupDiDestroyDeviceInfoList(usbDevices);
if (!found) {
irecv_close(_client);
return IRECV_E_UNABLE_TO_CONNECT;
}
*client = _client;
return IRECV_E_SUCCESS;
}
irecv_error_t mobiledevice_openpipes(irecv_client_t client) {
if (client->iBootPath && !(client->hIB = CreateFile(client->iBootPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL))) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}
if (client->DfuPath && !(client->hDFU = CreateFile(client->DfuPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL))) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}
client->mode = 0;
if (client->iBootPath == NULL) {
if (strncmp(client->DfuPath, "\\\\?\\usb#vid_05ac&pid_", 21) == 0) {
sscanf(client->DfuPath+21, "%x#", &client->mode);
}
client->handle = client->hDFU;
} else {
if (strncmp(client->iBootPath, "\\\\?\\usb#vid_05ac&pid_", 21) == 0) {
sscanf(client->iBootPath+21, "%x#", &client->mode);
}
client->handle = client->hIB;
}
if (client->mode == 0) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}
return IRECV_E_SUCCESS;
}