forked from platomav/MEAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MEA.py
3381 lines (2867 loc) · 149 KB
/
MEA.py
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
#!/usr/bin/env python3
"""
ME Analyzer
Intel Engine Firmware Analysis Tool
Copyright (C) 2014-2017 Plato Mavropoulos
"""
title = 'ME Analyzer v1.15.0'
import os
import re
import sys
import struct
import ctypes
import shutil
import hashlib
import inspect
import binascii
import tempfile
import colorama
import traceback
import subprocess
# Initialize and setup Colorama
colorama.init()
col_r = colorama.Fore.RED + colorama.Style.BRIGHT
col_g = colorama.Fore.GREEN + colorama.Style.BRIGHT
col_y = colorama.Fore.YELLOW + colorama.Style.BRIGHT
col_m = colorama.Fore.MAGENTA + colorama.Style.BRIGHT
col_e = colorama.Fore.RESET + colorama.Style.RESET_ALL
# Detect OS Platform
mea_os = sys.platform
if mea_os == 'win32' :
cl_wipe = 'cls'
uf_exec = 'UEFIFind.exe'
os_dir = '\\'
elif mea_os.startswith('linux') or mea_os == 'darwin' :
cl_wipe = 'clear'
uf_exec = 'UEFIFind'
os_dir = '//'
else :
print(col_r + '\nError: ' + col_e + 'Unsupported platform: %s\n' % mea_os)
input('Press enter to exit')
colorama.deinit()
sys.exit(-1)
char = ctypes.c_char
uint8_t = ctypes.c_ubyte
uint16_t = ctypes.c_ushort
uint32_t = ctypes.c_uint
uint64_t = ctypes.c_uint64
class MEA_Param :
def __init__(self, source) :
self.all = ['-?','-skip','-multi','-ubupre','-ubu','-extr','-msg','-hid','-adir','-aecho','-emod','-dsku','-pdb','-enuf','-dbname','-mass','-dfpt']
self.win = ['-ubupre','-ubu','-extr','-msg','-hid','-aecho','-adir'] # Windows only
if mea_os == 'win32' :
self.val = self.all
else :
self.val = [item for item in self.all if item not in self.win]
self.help_scr = False
self.skip_intro = False
self.multi = False
self.ubu_mea_pre = False
self.ubu_mea = False
self.extr_mea = False
self.print_msg = False
self.alt_dir = False
self.hid_find = False
self.alt_msg_echo = False
self.me11_mod_extr = False
self.me11_sku_disp = False
self.fpt_disp = False
self.db_print_new = False
self.enable_uf = False
self.give_db_name = False
self.mass_scan = False
for i in source :
if i == '-?' : self.help_scr = True # Displays MEA help text for end-users.
if i == '-skip' : self.skip_intro = True # Skips the MEA options intro screen.
if i == '-multi' : self.multi = True # Checks multiple files, copies those with messages to new folder.
if i == '-emod' : self.me11_mod_extr = True # Storing & Separation of ME x86 modules.
if i == '-dsku' : self.me11_sku_disp = True # Forces MEA to print ME x86 Debug SKU detection.
if i == '-pdb' : self.db_print_new = True # Writes input firmware's DB entries to file.
if i == '-enuf' : self.enable_uf = True # Enables UEFIFind Engine GUID Detection.
if i == '-dbname' : self.give_db_name = True # Rename input file based on DB structured name.
if i == '-mass' : self.mass_scan = True # Scans all files of a given directory, no limit.
if i == '-dfpt' : self.fpt_disp = True # Displays details about the $FPT header.
if mea_os == 'win32' : # Windows only options
if i == '-adir' : self.alt_dir = True # Sets UEFIFind to the previous directory.
if i == '-ubupre' : self.ubu_mea_pre = True # UBU Pre-Menu mode, 9 --> Engine FW found, 8 --> Engine FW not found.
if i == '-ubu' : self.ubu_mea = True # UBU mode, prints everything without some headers.
if i == '-extr' : self.extr_mea = True # UEFI Strip mode, prints special one-line outputs.
if i == '-msg' : self.print_msg = True # Prints all messages without any headers.
if i == '-hid' : self.hid_find = True # Forces MEA to display any firmware found. Works with -msg.
if i == '-aecho' : self.alt_msg_echo = True # Enables an alternative display of empty lines. Works with -msg and -hid.
if self.ubu_mea_pre or self.ubu_mea or self.extr_mea or self.print_msg or self.mass_scan \
or self.db_print_new : self.skip_intro = True
# Engine Structures
class FPT_Pre_Header(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("ROMB_Instr_0", uint32_t), # 0x00
("ROMB_Instr_1", uint32_t), # 0x04
("ROMB_Instr_2", uint32_t), # 0x08
("ROMB_Instr_3", uint32_t), # 0x0C
# 0x10
]
# noinspection PyTypeChecker
class FPT_Header(ctypes.LittleEndianStructure) : # Flash Partition Table
_pack_ = 1
_fields_ = [
("Tag", char*4), # 0x00
("NumPartitions", uint32_t), # 0x04
("Version", uint8_t), # 0x08
("EntryType", uint8_t), # 0x09
("Length", uint8_t), # 0x0A
("Checksum", uint8_t), # 0x0B
("FlashCycleLife", uint16_t), # 0x0C
("FlashCycleLimit", uint16_t), # 0x0E
("UMASize", uint32_t), # 0x10
("Flags", uint32_t), # 0x14
("FitMajor", uint16_t), # 0x18
("FitMinor", uint16_t), # 0x1A
("FitHotfix", uint16_t), # 0x1C
("FitBuild", uint16_t), # 0x1E
# 0x20
]
# noinspection PyTypeChecker
class FPT_Entry(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Name", char*4), # 0x00
("Owner", char*4), # 0x04
("Offset", uint32_t), # 0x08
("Size", uint32_t), # 0x0C
("StartTokens", uint32_t), # 0x10
("MaxTokens", uint32_t), # 0x14
("ScratchSectors", uint32_t), # 0x18
("Flags", uint32_t), # 0x1C
# 0x20
]
# noinspection PyTypeChecker
class MN2_Manifest(ctypes.LittleEndianStructure) : # Manifest ($MAN/$MN2)
_pack_ = 1
_fields_ = [
("Type", uint32_t), # 0x00
("HeaderLength", uint32_t), # 0x04 (*4)
("HeaderVersion", uint32_t), # 0x08
("Flags", uint32_t), # 0x0C
("VEN_ID", uint32_t), # 0x10 (0x8086)
("Day", uint8_t), # 0x14
("Month", uint8_t), # 0x15
("Year", uint16_t), # 0x16
("Size", uint32_t), # 0x18 (*4)
("Tag", char*4), # 0x1C
("NumModules", uint32_t), # 0x20 (Reserved at $CPD)
("Major", uint16_t), # 0x24
("Minor", uint16_t), # 0x26
("Hotfix", uint16_t), # 0x28
("Build", uint16_t), # 0x2A
("SVN_9", uint8_t), # 0x2C (ME9+)
("Reserved0", uint8_t*3), # 0x2D
("SVN_8", uint8_t), # 0x30 (ME8, Reserved at $CPD)
("Reserved1", uint8_t*3), # 0x31
("VCN", uint8_t), # 0x34 (ME8-10, Reserved at $CPD)
("Reserved2", uint8_t*16), # 0x35
("KeySize", uint32_t), # 0x78
("ScratchSize", uint32_t), # 0x7C
("RsaPubKey", uint32_t*64), # 0x80
("RsaPubExp", uint32_t), # 0x180
("RsaSig", uint32_t*64), # 0x184
# 0x284
]
# noinspection PyTypeChecker
class MME_Header_Old(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Tag", char*4), # 0x00
("Guid", uint8_t*16), # 0x04
("MajorVersion", uint16_t), # 0x14
("MinorVersion", uint16_t), # 0x16
("HotfixVersion", uint16_t), # 0x18
("BuildVersion", uint16_t), # 0x1A
("Name", char*16), # 0x1C
("Hash", uint8_t*20), # 0x2C
("Size", uint32_t), # 0x40
("Flags", uint32_t), # 0x44
("Unk48_4C", uint32_t), # 0x48
("Unk4C_50", uint32_t), # 0x4C
# 0x50
]
# noinspection PyTypeChecker
class MME_Header_New(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Tag", char*4), # 0x00
("Name", char*16), # 0x04
("Hash", uint8_t*32), # 0x14
("ModBase", uint32_t), # 0x34
("Offset_MN2", uint32_t), # 0x38 (from $MN2)
("SizeUncomp", uint32_t), # 0x3C
("SizeComp", uint32_t), # 0x40
("MemorySize", uint32_t), # 0x44
("PreUmaSize", uint32_t), # 0x48
("EntryPoint", uint32_t), # 0x4C
("Flags", uint32_t), # 0x50
("Unk54", uint32_t), # 0x54
("Unk58", uint32_t), # 0x58
("Unk5C", uint32_t), # 0x5C
# 0x60
]
# noinspection PyTypeChecker
class MCP_Header(ctypes.LittleEndianStructure) : # Multi Chip Package
_pack_ = 1
_fields_ = [
("Tag", char*4), # 0x00
("HeaderSize", uint32_t), # 0x04 (*4)
("CodeSize", uint32_t), # 0x08
("Offset_Code_MN2", uint32_t), # 0x0C (Code start from $MN2)
("Offset_Part_FPT", uint32_t), # 0x10 (Partition start from $FPT)
("Hash", uint8_t*32), # 0x14
("Unknown34_38", uint32_t), # 0x34 (00000100)
("Unknown38_3C", uint32_t), # 0x38 (08000000)
("Unknown3C_40", uint32_t), # 0x3C (01000000)
("Unknown40_44", uint32_t), # 0x40 (01000000)
# 0x44
]
# noinspection PyTypeChecker
class CPD_Header(ctypes.LittleEndianStructure) : # Code Partition Directory
_pack_ = 1
_fields_ = [
("Tag", char*4), # 0x00
("NumModules", uint32_t), # 0x04
("HeaderVersion", uint8_t), # 0x08
("EntryVersion", uint8_t), # 0x09
("HeaderLength", uint8_t), # 0x0A
("Checksum", uint8_t), # 0x0B
("PartitionName", char*4), # 0x0C
# 0x10
]
# noinspection PyTypeChecker
class CPD_Entry(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Name", char*12), # 0x00
("OffsetAttrib", uint32_t), # 0x0C (LE --> 0:24 Offset from $CPD, 25 Compressed No/Yes, 26:31 Reserved)
("Size", uint32_t), # 0x10 (Uncompressed for LZMA/Huffman, Compressed at CPD_Ext_0A instead)
("Reserved", uint32_t), # 0x14
# 0x18
]
# noinspection PyTypeChecker
class CPD_Ext_00(ctypes.LittleEndianStructure) : # System Info
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("MinUMASize", uint32_t), # 0x08
("ChipsetVersion", uint32_t), # 0x0C
("IMGDefaultHash", uint32_t*8), # 0x10
("PageableUMASize", uint32_t), # 0x30
("Reserved0", uint64_t), # 0x34
("Reserved1", uint32_t), # 0x3C
# 0x40
]
# noinspection PyTypeChecker
class CPD_Ext_00_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Name", char*4), # 0x00
("Version", uint32_t), # 0x04
("UserID", uint16_t), # 0x08
("Reserved", uint16_t), # 0x0A
# 0x0C
]
# noinspection PyTypeChecker
class CPD_Ext_01(ctypes.LittleEndianStructure) : # Init Script
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("Reserved", uint32_t), # 0x08
("ModuleCount", uint32_t), # 0x0C
# 0x10
]
# noinspection PyTypeChecker
class CPD_Ext_01_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("PartitionName", char*4), # 0x00
("ModuleName", char*12), # 0x0C
("InitFlowFlags", uint32_t), # 0x10
("BootTypeFlags", uint32_t), # 0x14 (LE --> 0 Normal, 1 HAP, 2 HMRFPO, 3 Temp Disable, 4 Recovery, 5 Safe Mode, 6 FW Update, 7:31 Reserved)
# 0x18
]
# noinspection PyTypeChecker
class CPD_Ext_02(ctypes.LittleEndianStructure) : # Feature Permissions
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("FeatureCount", uint32_t), # 0x08
# 0x0C
]
# noinspection PyTypeChecker
class CPD_Ext_02_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("UserID", uint16_t), # 0x00
("Reserved", uint16_t), # 0x02
# 0x04
]
# noinspection PyTypeChecker
class CPD_Ext_03(ctypes.LittleEndianStructure) : # Partition Info
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("PartitionName", char*4), # 0x08
("PartitionSize", uint32_t), # 0x0C
("Hash", uint32_t*8), # 0x10
("VCN", uint32_t), # 0x30
("PartitionVer", uint32_t), # 0x34
("DataFormatVer", uint32_t), # 0x38
("InstanceID", uint32_t), # 0x3C
("Flags", uint32_t), # 0x40
("Reserved", uint32_t*5), # 0x40
# 0x58
]
# noinspection PyTypeChecker
class CPD_Ext_03_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Name", char*12), # 0x00
("Type", uint8_t), # 0x0C (0 Process, 1 Shared Library, 2 Data)
("Compression", uint8_t), # 0x0D (0 Uncompressed --> always, 1 Huffman, 2 LZMA)
("Reserved", uint16_t), # 0x0E (FFFF)
("MetadataSize", uint32_t), # 0x10
("MetadataHash", uint32_t*8), # 0x14
# 0x34
]
# noinspection PyTypeChecker
class CPD_Ext_04(ctypes.LittleEndianStructure) : # Shared Lib
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("ContextSize", uint32_t), # 0x08
("TotAlocVirtSpc", uint32_t), # 0x0C
("CodeBaseAddress", uint32_t), # 0x10
("TLSSize", uint32_t), # 0x14
("Reserved", uint32_t), # 0x18
# 0x1C
]
# noinspection PyTypeChecker
class CPD_Ext_05(ctypes.LittleEndianStructure) : # Process Manifest
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("Flags", uint32_t), # 0x08
("CodeBaseAddress", uint32_t), # 0x0C
("CodeSizeUncomp", uint32_t), # 0x10
("CM0HeapSize", uint32_t), # 0x14
("BSSSize", uint32_t), # 0x18
("DefaultHeapSize", uint32_t), # 0x1C
("MainThreadEntry", uint32_t), # 0x20
("AllowedSysCalls", uint32_t*3), # 0x24
("UserID", uint16_t), # 0x30
("Reserved0", uint32_t), # 0x32
("Reserved1", uint16_t), # 0x36
("Reserved2", uint64_t), # 0x38
("GroupID", uint16_t*3), # 0x40
# 0x46
]
# noinspection PyTypeChecker
class CPD_Ext_06(ctypes.LittleEndianStructure) : # Threads
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
# 0x08
]
# noinspection PyTypeChecker
class CPD_Ext_06_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("StackSize", uint32_t), # 0x00
("Flags", uint32_t), # 0x04
("SchedulPolicy", uint32_t), # 0x08
("Reserved", uint32_t), # 0x0C
# 0x10
]
# noinspection PyTypeChecker
class CPD_Ext_07(ctypes.LittleEndianStructure) : # Device IDs
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
# 0x08
]
# noinspection PyTypeChecker
class CPD_Ext_07_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("DeviceID", uint32_t), # 0x00
("Reserved", uint32_t), # 0x04
# 0x08
]
# noinspection PyTypeChecker
class CPD_Ext_08(ctypes.LittleEndianStructure) : # MMIO Ranges
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
# 0x8
]
# noinspection PyTypeChecker
class CPD_Ext_08_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("BaseAddress", uint32_t), # 0x00
("SizeLimit", uint32_t), # 0x04
("Flags", uint32_t), # 0x08
# 0x0C
]
# noinspection PyTypeChecker
class CPD_Ext_09(ctypes.LittleEndianStructure) : # Special File Producer
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("MajorNumber", uint16_t), # 0x08
("Flags", uint16_t), # 0x0A
# 0x0C
]
# noinspection PyTypeChecker
class CPD_Ext_09_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Name", char*12), # 0x00
("AccessMode", uint16_t), # 0x0C
("UserID", uint16_t), # 0x0E
("GroupID", uint16_t), # 0x10
("MinorNumber", uint8_t), # 0x12
("Reserved0", uint8_t), # 0x13
("Reserved1", uint32_t), # 0x14
# 0x18
]
# noinspection PyTypeChecker
class CPD_Ext_0A(ctypes.LittleEndianStructure) : # Module Attributes
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("Compression", uint8_t), # 0x08 (0 Uncompressed, 1 Huffman, 2 LZMA)
("Encryption", uint8_t), # 0x09 (0 No, 1 Yes, unknown if LE MSB or entire Byte)
("Reserved0", uint8_t), # 0x0A
("Reserved1", uint8_t), # 0x0B
("SizeUncomp", uint32_t), # 0x0C
("SizeComp", uint32_t), # 0x10 (LZMA & Huffman w/o EOM alignment)
("DEV_ID", uint16_t), # 0x10
("VEN_ID", uint16_t), # 0x10 (0x8086)
("Hash", uint32_t*8), # 0x14
# 0x38
]
# noinspection PyTypeChecker
class CPD_Ext_0B(ctypes.LittleEndianStructure) : # Locked Ranges
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
# 0x08
]
# noinspection PyTypeChecker
class CPD_Ext_0B_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("RangeBase", uint32_t), # 0x00
("RangeSize", uint32_t), # 0x04
# 0x08
]
# noinspection PyTypeChecker
class CPD_Ext_0C(ctypes.LittleEndianStructure) : # Client System Info
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("FWSKUCaps", uint32_t), # 0x08
("FWSKUCapsReserv", uint32_t*7), # 0x0C
("FWSKUAttrib", uint64_t), # 0x28
# 0x30
]
# noinspection PyTypeChecker
class CPD_Ext_0D(ctypes.LittleEndianStructure) : # User Info (vfs)
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
# 0x8
]
# noinspection PyTypeChecker
class CPD_Ext_0D_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("UserID", uint16_t), # 0x00
("Reserved", uint16_t), # 0x02
("NVStorageQuota", uint32_t), # 0x04
("RAMStorageQuota", uint32_t), # 0x08
("WOPQuota", uint32_t), # 0x0C
("WorkingDir", uint32_t*9), # 0x10
# 0x44
]
# noinspection PyTypeChecker
class CPD_Ext_0E(ctypes.LittleEndianStructure) : # Key Manifest (TXE)
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("Unknown08_0C", uint32_t), # 0x08
("Unknown0C_24", uint32_t*6), # 0x0C
# 0x24
]
# noinspection PyTypeChecker
class CPD_Ext_0E_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Unknown00_04", uint32_t), # 0x00
("Unknown04_20", uint32_t*7), # 0x04
("Unknown20_24", uint32_t*7), # 0x20
("Unknown24_44", uint32_t*7), # 0x24 (SHA256 Hash)
# 0x44
]
# noinspection PyTypeChecker
class CPD_Ext_0F(ctypes.LittleEndianStructure) : # Package Info (TXE)
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("PartitionName", char*4), # 0x08
("VCN", uint32_t), # 0x0C
("UsageBitmap", uint32_t*4), # 0x10
("SVN", uint32_t), # 0x20
("Reserved", uint32_t*4), # 0x24
# 0x34
]
# noinspection PyTypeChecker
class CPD_Ext_0F_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Name", char*12), # 0x00
("Type", uint8_t), # 0x0C (0 Process, 1 Shared Library, 2 Data, 3 TBD)
("HashAlgorithm", uint8_t), # 0x0D (0 Reserved, 1 SHA1, 2 SHA256)
("HashSize", uint16_t), # 0x0E (0x20, only SHA256 for BXT)
("MetadataSize", uint32_t), # 0x10
("MetadataHash", uint32_t*8), # 0x14
# 0x34
]
# noinspection PyTypeChecker
class CPD_Ext_12(ctypes.LittleEndianStructure) : # ??? (TXE)
_pack_ = 1
_fields_ = [
("Tag", uint32_t), # 0x00
("Size", uint32_t), # 0x04
("ModuleCount", uint32_t), # 0x08
("Reserved", uint32_t*4), # 0x0C
# 0x1C
]
# noinspection PyTypeChecker
class CPD_Ext_12_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Unknown00_04", uint32_t), # 0x00
("Unknown04_08", uint32_t), # 0x04
("Unknown08_0C", uint32_t), # 0x08
("Unknown0C_10", uint32_t), # 0x0C
("Unknown10_18", uint32_t*2), # 0x10 (FFFFFFFFFFFFFFFF)
("Unknown18_1C", uint32_t), # 0x18
("Unknown1C_20", uint32_t), # 0x1C
("Unknown20_28", uint32_t*2), # 0x20 (FFFFFFFFFFFFFFFF)
("Unknown28_2C", uint32_t), # 0x28
("Unknown2C_30", uint32_t), # 0x2C
("Unknown30_38", uint32_t*2), # 0x30 (FFFFFFFFFFFFFFFF)
# 0x38
]
# noinspection PyTypeChecker
class CPD_Ext_15(ctypes.LittleEndianStructure) : # Secure Token (TXE, no sample)
_pack_ = 1
_fields_ = [
("Unknown00_04", uint32_t), # 0x00
# 0x??
]
# noinspection PyTypeChecker
class CPD_Ext_15_Mod(ctypes.LittleEndianStructure) :
_pack_ = 1
_fields_ = [
("Unknown00_04", uint32_t), # 0x00
# 0x??
]
# Inspired from Igor Skochinsky's me_unpack
def get_struct(str_, off, struct):
my_struct = struct()
struct_len = ctypes.sizeof(my_struct)
str_data = str_[off:off + struct_len]
fit_len = min(len(str_data), struct_len)
if (off > file_end) or (fit_len < struct_len) :
err_stor.append(col_r + "Error: Offset 0x%0.2X out of bounds, incomplete image!" % off + col_e)
for error in err_stor : print(error)
if param.multi : multi_drop()
else: f.close()
mea_exit(1)
ctypes.memmove(ctypes.addressof(my_struct), str_data, fit_len)
return my_struct
# MEA Version Header
def mea_hdr_init() :
if not param.extr_mea and not param.print_msg :
db_rev = "None"
try :
fw_db = db_open()
for line in fw_db :
if 'Revision' in line :
db_line = line.split()
db_rev = db_line[2]
fw_db.close()
except :
pass
return db_rev
def mea_hdr(db_rev) :
print("\n-------[ %s ]-------" % title)
print(" Database %s" % db_rev)
def mea_help() :
text = "\nUsage: MEA [FilePath] {Options}\n\n{Options}\n\n"
text += "-? : Displays help & usage screen\n"
text += "-skip : Skips options intro screen\n"
text += "-multi : Scans multiple files and copies on messages\n"
text += "-mass : Scans all files of a given directory\n"
text += "-enuf : Enables UEFIFind Engine GUID detection\n"
text += "-pdb : Writes input firmware's DB entries to file\n"
text += "-dbname : Renames input file based on DB name\n"
text += "-dfpt : Shows info about the $FPT header (Research)\n"
text += "-dsku : Shows verbose info for ME 11+ SKU (Research)\n"
text += "-emod : Stores and Separates ME 11+ modules (Research)"
if mea_os == 'win32' :
text += "\n-adir : Sets UEFIFind to the previous directory\n"
text += "-ubu : SoniX/LS_29's UEFI BIOS Updater mode\n"
text += "-ubupre : SoniX/LS_29's UEFI BIOS Updater Pre-Menu mode\n"
text += "-extr : Lordkag's UEFIStrip mode\n"
text += "-msg : Prints only messages without headers\n"
text += "-hid : Displays all firmware even without messages (-msg)\n"
text += "-aecho : Alternative display of empty lines (-msg, -hid)"
print(text)
mea_exit(0)
# https://stackoverflow.com/a/22881871
def get_script_dir(follow_symlinks=True) :
if getattr(sys, 'frozen', False) :
path = os.path.abspath(sys.executable)
else :
path = inspect.getabsfile(get_script_dir)
if follow_symlinks :
path = os.path.realpath(path)
return os.path.dirname(path)
# https://stackoverflow.com/a/781074
def show_exception_and_exit(exc_type, exc_value, tb) :
print(col_r + '\nError: MEA just crashed, please report the following:\n')
traceback.print_exception(exc_type, exc_value, tb)
input(col_e + "\nPress enter to exit")
colorama.deinit() # Stop Colorama
sys.exit(-1)
def mea_exit(code=0) :
colorama.deinit() # Stop Colorama
if param.ubu_mea_pre or param.ubu_mea or param.extr_mea or param.print_msg : sys.exit(code)
input("\nPress enter to exit")
sys.exit(code)
# Calculate SHA-1 hash of text
def sha1_text(text) :
return hashlib.sha1(text).hexdigest()
# Must be called at the end of analysis to gather all available messages, if any
def multi_drop() :
if err_stor or warn_stor or note_stor : # Any note, warning or error copies the file
f.close()
suffix = 0
file_name = os.path.basename(file_in)
check_dir = mea_dir + os_dir + '__CHECK__' + os_dir
if not os.path.isdir(check_dir) : os.mkdir(check_dir)
while os.path.exists(check_dir + file_name) :
suffix += 1
file_name += '_%s' % suffix
shutil.copyfile(file_in, check_dir + file_name)
def db_open() :
fw_db = open(db_path, "r")
return fw_db
def check_upd(key) :
upd_key_found = False
vlp = [0]*4
fw_db = db_open()
for line in fw_db :
if len(line) < 2 or line[:3] == "***" :
continue # Skip empty lines or comments
elif key in line :
upd_key_found = True
wlp = line.strip().split('__') # whole line parts
vlp = wlp[1].strip().split('.') # version line parts
for i in range(len(vlp)) : vlp[i] = int(vlp[i])
break
fw_db.close()
if upd_key_found : return vlp[0],vlp[1],vlp[2],vlp[3]
else : return 0,0,0,0
# Split & space bytes at every 2 characters
def str_split_as_bytes(input_bytes) :
return ' '.join([input_bytes[i:i + 2] for i in range(0, len(input_bytes), 2)])
# General MEA Messages
def gen_msg(msg_type,msg,command) :
if command == 'del' : del err_stor[:]
if not param.print_msg: print('\n' + msg)
if (not err_stor) and (not warn_stor) and (not note_stor): msg_type.append(msg)
else: msg_type.append('\n' + msg)
# Detect SPI with Intel Flash Descriptor
def spi_fd_init() :
fd_match = (re.compile(br'\xFF\xFF\xFF\xFF\x5A\xA5\xF0\x0F')).search(reading) # 16xFF + Z¥π. detection (PCH)
if fd_match is None :
fd_match = (re.compile(br'\x5A\xA5\xF0\x0F.{172}\xFF{16}', re.DOTALL)).search(reading) # Z¥π. + [0xAC] + 16xFF fallback (ICH)
start_substruct = 0x0
end_substruct = 0xBC - 0x10 # 0xBC for [0xAC] + 16xFF sanity check, 0x10 extra before ICH FD Regions
else :
start_substruct = 0xC
end_substruct = 0x0
if fd_match is not None :
(start_fd_match, end_fd_match) = fd_match.span()
return True, start_fd_match - start_substruct, end_fd_match - end_substruct
else :
return False, 0, 0
# Analyze Intel FD after Reading, Major, Variant
def spi_fd(action,start_fd_match,end_fd_match) :
if action == 'unlocked' :
# 0xh FF FF = 0b 1111 1111 1111 1111 --> All 8 (0-7) regions Read/Write unlocked by CPU/BIOS
if (variant == 'ME' and major <= 10) or (variant == 'TXE' and major <= 2) : # CPU/BIOS, ME, GBE check
fd_bytes = reading[end_fd_match + 0x4E:end_fd_match + 0x50] + reading[end_fd_match + 0x52:end_fd_match + 0x54] \
+ reading[end_fd_match + 0x56:end_fd_match + 0x58]
fd_bytes = binascii.b2a_hex(fd_bytes).decode('utf-8').upper()
if fd_bytes == 'FFFFFFFFFFFF' : return 2 # Unlocked FD
else : return 1 # Locked FD
elif variant == 'ME' and major > 10 : # CPU/BIOS, ME, GBE, EC check
fd_bytes = reading[end_fd_match + 0x6D:end_fd_match + 0x70] + reading[end_fd_match + 0x71:end_fd_match + 0x74] \
+ reading[end_fd_match + 0x75:end_fd_match + 0x78] + reading[end_fd_match + 0x7D:end_fd_match + 0x80]
fd_bytes = binascii.b2a_hex(fd_bytes).decode('utf-8').upper()
if fd_bytes == 'FFFFFFFFFFFFFFFFFFFFFFFF' : return 2 # Unlocked FD
else : return 1 # Locked FD
elif variant == 'TXE' and major > 2 :
fd_bytes = reading[end_fd_match + 0x6D:end_fd_match + 0x70] + reading[end_fd_match + 0x71:end_fd_match + 0x74]
fd_bytes = binascii.b2a_hex(fd_bytes).decode('utf-8').upper()
if fd_bytes == 'FFFFFFFFFFFF' : return 2 # Unlocked FD
else : return 1 # Locked FD
elif action == 'region' :
me_fd_base = int.from_bytes(reading[end_fd_match + 0x34:end_fd_match + 0x36], 'little')
me_fd_limit = int.from_bytes(reading[end_fd_match + 0x36:end_fd_match + 0x38], 'little')
if me_fd_limit != 0 :
me_fd_start = me_fd_base * 0x1000 + start_fd_match # fd_match required in case FD is not at the start of image
me_fd_size = (me_fd_limit + 1 - me_fd_base) * 0x1000 # The +1 is required to include last Region byte
return True,me_fd_start,me_fd_size # FD found, ME Region exists
else :
return False,0,0 # FD found, ME Region missing
def fw_ver(major,minor,hotfix,build) :
if variant == "SPS" :
version = "%s.%s.%s.%s" % ("{0:02d}".format(major), "{0:02d}".format(minor), "{0:02d}".format(hotfix), "{0:03d}".format(build)) # xx.xx.xx.xxx
else :
version = "%s.%s.%s.%s" % (major, minor, hotfix, build)
return version
def fuj_umem_ver(me_fd_start) :
rgn_fuj_hdr = reading[me_fd_start:me_fd_start + 0x4]
rgn_fuj_hdr = binascii.b2a_hex(rgn_fuj_hdr).decode('utf-8').upper()
version = "NaN"
if rgn_fuj_hdr == "554DC94D" : # Futjitsu Compressed ME Region with header UMEM
major = int(binascii.b2a_hex(reading[me_fd_start + 0xB:me_fd_start + 0xD][::-1]), 16)
minor = int(binascii.b2a_hex(reading[me_fd_start + 0xD:me_fd_start + 0xF][::-1]), 16)
hotfix = int(binascii.b2a_hex(reading[me_fd_start + 0xF:me_fd_start + 0x11][::-1]), 16)
build = int(binascii.b2a_hex(reading[me_fd_start + 0x11:me_fd_start + 0x13][::-1]), 16)
version = "%s.%s.%s.%s" % (major, minor, hotfix, build)
return version
# Taken directly from Lordkag's UEFI Strip!
def switch_guid (guid, transform) :
vol = ''
if transform == "GUID2HEX" :
vol = guid[6:8] + guid[4:6] + guid[2:4] + guid[:2] + guid[11:13] + guid[9:11] + guid[16:18]
vol += guid[14:16] + guid[19:23] + guid[24:]
elif transform == "HEX2GUID" :
vol = guid[6:8] + guid[4:6] + guid[2:4] + guid[:2] + "-" + guid[10:12] + guid[8:10] + "-"
vol += guid[14:16] + guid[12:14] + "-" + guid[16:20] + "-" + guid[20:]
return vol.upper()
# Check if Fixed Offset Variables (FOVD/NVKR) section is dirty
def fovd_clean (fovdtype) :
fovd_match = None
fovd_data = b''
if fovdtype == "new" : fovd_match = (re.compile(br'\x46\x4F\x56\x44\x4B\x52\x49\x44')).search(reading) # FOVDKRID detection
elif fovdtype == "old" : fovd_match = (re.compile(br'\x4E\x56\x4B\x52\x4B\x52\x49\x44')).search(reading) # NVKRKRID detection
if fovd_match is not None :
(start_fovd_match, end_fovd_match) = fovd_match.span()
fovd_start = int.from_bytes(reading[end_fovd_match:end_fovd_match + 0x4], 'little')
fovd_size = int.from_bytes(reading[end_fovd_match + 0x4:end_fovd_match + 0x8], 'little')
if fovdtype == "new" : fovd_data = reading[fpt_start + fovd_start:fpt_start + fovd_start + fovd_size]
elif fovdtype == "old" :
fovd_size = int.from_bytes(reading[fovd_start + 0x19:fovd_start + 0x1C], 'little')
fovd_data = reading[fpt_start + fovd_start + 0x1C:fpt_start + fovd_start + 0x1C + fovd_size]
if fovd_data == b'\xFF' * fovd_size : return True
else : return False
else : return True
# Analyze Engine x86 FTPR $CPD Offset & Extensions
# noinspection PyUnusedLocal
def ext_anl(start_man_match, variant) :
vcn = -1
cpd_num = -1
fw_0C_lbg = -1
fw_0C_sku1 = -1
fw_0C_sku2 = -1
cpd_offset = -1
cpd_mod_attr = []
ext_tag_all = list(range(16)) # $CPD Extensions 00-0F
mn2_hdr = get_struct(reading, start_man_match - 0x1B, MN2_Manifest)
if mn2_hdr.Tag == b'$MN2' : # Sanity check
# Scan backwards for FTPR $CPD (should be <= 500B, works with both RGN --> $FPT & UPD --> 0x0)
for offset in range(start_man_match + 2,0,-4): # Start from MN2 (no $) to catch $CPD at 1, before "for" break at 0
if b'$CPD' in reading[offset -1:offset -1 + 4] and b'FTPR' in reading[offset -1 + 0xC:offset -1 + 0x10] :
cpd_offset = offset-1 # Catch UPD $CPD at offset 0 (offset-1 = 1-1 = 0)
break # Stop at first detected FTPR $CPD
# FTPR $CPD detected
if cpd_offset > -1 :
cpd_hdr = get_struct(reading, cpd_offset, CPD_Header)
cpd_num = cpd_hdr.NumModules
# Analyze FTPR Manifest & Metadata (must be before Module analysis)
for entry in range(0, cpd_num, 2) :
cpd_entry_hdr = get_struct(reading, cpd_offset + 0x10 + entry * 0x18, CPD_Entry)
cpd_off_attr = format(cpd_entry_hdr.OffsetAttrib, '032b') # 32 bits (LE)
cpd_mod_off = int(cpd_off_attr[7:], 2) # $CPD Entry Offset Attribute Address (from $CPD, 25 bits)
cpd_mod_comp = int(cpd_off_attr[6], 2) # $CPD Entry Offset Attribute Compressed (0 No, 1 Yes)
cpd_mod_res = int(cpd_off_attr[:6], 2) # $CPD Entry Offset Attribute Reserved (0, 6 bits)
cpd_entry_offset = cpd_offset + cpd_mod_off
cpd_entry_name = cpd_entry_hdr.Name
if b'.man' in cpd_entry_name : cpd_ext_offset = cpd_entry_offset + mn2_hdr.HeaderLength * 4 # Skip $MN2 at FTPR.man
elif b'.met' in cpd_entry_name : cpd_ext_offset = cpd_entry_offset # Metadata is always Uncompressed
else : break # Sanity check, no Modules allowed here
# Analyze all FTPR Manifest & Metadata Extensions
ext_tag = int.from_bytes(reading[cpd_ext_offset:cpd_ext_offset + 0x4], 'little') # Initial Extension Tag
while ext_tag in ext_tag_all :
if ext_tag == 3 : # Unique, FTPR.man (ME)
ext_hdr = get_struct(reading, cpd_ext_offset, CPD_Ext_03)
vcn = ext_hdr.VCN
elif ext_tag == 10 : # Unique, .met
ext_hdr = get_struct(reading, cpd_ext_offset, CPD_Ext_0A)
mod_comp_type = ext_hdr.Compression # Metadata's Module Compression Type (0-2)
mod_encr_type = ext_hdr.Encryption # Metadata's Module Encryption Type (0-1)
mod_comp_size = ext_hdr.SizeComp # Metadata's Module Compressed Size ($CPD Entry's Module Size is always Uncompressed)
mod_uncomp_size = ext_hdr.SizeUncomp # Metadata's Module Uncompressed Size (equal to $CPD Entry's Module Size)
cpd_mod_attr.append([cpd_entry_name.decode('utf-8')[:-4], mod_comp_type, mod_encr_type, 0, mod_comp_size, mod_uncomp_size, 0])
elif ext_tag == 15 : # Unique, FTPR.man (TXE)
ext_hdr = get_struct(reading, cpd_ext_offset, CPD_Ext_0F)
vcn = ext_hdr.VCN
elif ext_tag == 12 : # Unique, FTPR.man
ext_hdr = get_struct(reading, cpd_ext_offset, CPD_Ext_0C)
fw_sku_attr = format(ext_hdr.FWSKUAttrib, '032b') # 32 bits (LE)
fw_0C_cse = int(fw_sku_attr[28:32], 2) # CSE Size * 0.5MB (0)
fw_0C_sku1 = int(fw_sku_attr[25:28], 2) # SKU Type (0 COR, 1 CON, 2 SLM)
fw_0C_lbg = int(fw_sku_attr[24], 2) # Lewisburg support (0 11.x, 1 11.20)
fw_0C_m3 = int(fw_sku_attr[23], 2) # M3 support (0 CON & SLM, 1 COR)
fw_0C_m0 = int(fw_sku_attr[22], 2) # M0 support (1 CON & SLM & COR)
fw_0C_sku2 = int(fw_sku_attr[20:22], 2) # Unknown/SKU Platform (0 for H/LP <= 11.0.0.1202, 0 for H >= 11.0.0.1205, 1 for LP >= 11.0.0.1205)
fw_0C_sicl = int(fw_sku_attr[16:20], 2) # Si Class H M L (2 CON & SLM, 4 COR)
fw_0C_res2 = int(fw_sku_attr[:16], 2) # Reserved (0)
cpd_ext_offset += int.from_bytes(reading[cpd_ext_offset + 0x4:cpd_ext_offset + 0x8], 'little')
if cpd_ext_offset + 1 > cpd_entry_offset + cpd_entry_hdr.Size : break # Stop Extension scanning at the end of .man/.met
ext_tag = int.from_bytes(reading[cpd_ext_offset:cpd_ext_offset + 0x4], 'little') # Next Extension Tag
# Analyze FTPR Modules (must be after Metadata analysis)
for entry in range(1, cpd_num, 2) :
cpd_entry_hdr = get_struct(reading, cpd_offset + 0x10 + entry * 0x18, CPD_Entry)
cpd_off_attr = format(cpd_entry_hdr.OffsetAttrib, '032b') # 32 bits (LE)
cpd_mod_off = int(cpd_off_attr[7:], 2) # $CPD Entry Offset Attribute Address (from $CPD, 25 bits)
cpd_entry_name = cpd_entry_hdr.Name
cpd_entry_offset = cpd_offset + cpd_mod_off
if b'.man' in cpd_entry_name or b'.met' in cpd_entry_name : break # Sanity check, no Manifest/Metadata allowed here
# Set Module's starting offset at Module Attributes list
for mod in range(len(cpd_mod_attr)) :
if cpd_entry_hdr.Name.decode('utf-8') == cpd_mod_attr[mod][0] :
mod_comp_size = cpd_mod_attr[mod][4] # Store Module Compressed Size for Empty check
cpd_mod_attr[mod][3] = cpd_entry_offset # Fill Module Starting Offset from $CPD Entry
mod_data = reading[cpd_entry_offset:cpd_entry_offset + mod_comp_size] # Store Module data for Empty check
if mod_data == b'\xFF' * mod_comp_size : cpd_mod_attr[mod][6] = 1 # Determine if Module is Empty (me_cleaner)
break
return cpd_offset, cpd_mod_attr, vcn, fw_0C_sku1, fw_0C_lbg, fw_0C_sku2
# Analyze & Store Engine x86 FTPR Modules
def mod_anl(action, release, cpd_offset, cpd_mod_attr) :
ker_start = -1