This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
unME11.py
1439 lines (1238 loc) · 59.8 KB
/
unME11.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 python2
import os, sys, struct, hashlib, platform
import subprocess
try:
import HuffDec11
HuffDecoder11 = HuffDec11.HuffDecoder()
except:
HuffDecoder11 = None
try:
import HuffDec12
HuffDecoder12 = HuffDec12.HuffDecoder()
except:
HuffDecoder12 = None
class Globals(object):
HuffDecoder = HuffDecoder11
dumpManifest = True # Dump CPD manifest
dumpMeta = True # Dump modules metadata
dumpRaw = False # Dump raw modules data (compressed/encrypted)
dumpChunks = False # Dump HUFF chunks
g = Globals()
class Error(Exception): pass
def BitFields(obj, val, bitDef):
def bitf(val, lo, hi): return (val & ((2<<hi)-1)) >> lo
for name, lo, hi in bitDef: setattr(obj, name, bitf(val, lo, hi))
def ListTrueBools(obj, bitDef):
return [v[0] for v in filter(lambda x: x[1] == x[2] and getattr(obj, x[0]), bitDef)]
class StructReader(object):
def __init__(self, ab, base=0, isLE=True):
self.ab = ab
self.base = base
self.o = self.base
self.cE = "<" if isLE else ">"
def sizeLeft(self):
return len(self.ab) - self.o
def getData(self, o, cb):
o += self.base
if o < len(self.ab) and cb >= 0 and o + cb <= len(self.ab):
return self.ab[o:o+cb]
def read(self, obj, stDef, o=None):
if o is None: o = self.o
self.o += self.base
for fldDef in stDef: # Walk field definitions
name = fldDef[0]
fmt = self.cE + fldDef[1]
val, = struct.unpack_from(fmt, self.ab, o)
if 3 == len(fldDef):
expected = fldDef[2]
if isinstance(expected, (list, tuple)):
if not val in expected:
print >>sys.stderr, "- %s.%s: not %s in %s" % (obj.__class__.__name__, name, val, expected)
else:
if val != expected:
print >>sys.stderr, "- %s.%s:" % (obj.__class__.__name__, name),
if isinstance(val, str): print >>sys.stderr, "Got %s, expected %s" % (val.encode("hex"), expected.encode("hex"))
else: print >>sys.stderr, "Got [%s], expected [%s]" % (repr(val), repr(expected))
else: assert val == expected
setattr(obj, name, val)
o += struct.calcsize(fmt)
self.o = o
def done(self):
assert len(self.ab) == self.o
aPubKeyHash = [v.decode("hex") for v in (
"EA6FA86514FA887C9044218EDB4D70BB3BCC7C2D37587EA8F760BAFBE158C587",
"A24E0682EDC8870DCA947C01603D19818AF714BEE9F39D2872D79B8C422F3890",
"EA3E9C34C8FD6BDEA277F0A8C6AC5A37E8E39256469C89D279FA86A7317B21AE",
"C8E7AA2C5F691F63A892BC044CD3935C5E77C6CB71C8E8627BE4987DFB730856",
"3D512A6DB7C855E9F6328DB8B2C259A2C0F291BB6E3EC74A2FB811AD84C5D404",
"04A6F35B14628879050AB0B3459326DDF946AE4E5EFD7BB1930883F57F68D084",
"980F9572AC1B5BDC9A5F3E89F2503A624C9C5BDF97B72D9031DCCDAB11A9F7A8",
"C468E6BA739856797BAF70910861BDE4C3BA95C956B1DCE24B738D614F1211BA",
)]
#***************************************************************************
#***************************************************************************
#***************************************************************************
args_lzma = {
"Windows": ["lzma", "d", "-si", "-so"],
"Linux": ["lzma", "-d"],
"Darwin": ["lzma", "-d"], # "brew install xz" or "sudo port install xz"
}[platform.system()]
def LZMA_decompress(compdata):
process = subprocess.Popen(args_lzma, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, errout = process.communicate(compdata)
retcode = process.poll()
if retcode: raise Error(errout)
return output
def decompress(data, compType, length):
if compType is None:
return data
elif "lzma" == compType:
if not data.startswith("36004000".decode("hex")):
print >>sys.stderr, "- Bad LZMA[0x%X] header %s" % (len(data), data[:17].encode("hex"))
return None
assert data.startswith("36004000".decode("hex"))
assert '\0\0\0' == data[14:17]
return LZMA_decompress(data[:14] + data[17:])
elif "huff" == compType:
return g.HuffDecoder.decompress(data, length) if g.HuffDecoder else None
else:
raise Error("Invalid compType %s" % compType)
RESTART_NOT_ALLOWED = 0
RESTART_IMMEDIATLY = 1
RESTART_ON_NEXT_BOOT = 2
# MODULE_TYPES
PROCESS_TYPE = 0
SHARED_LIBRARY_TYPE = 1
DATA_TYPE = 2
IUNIT_TYPE = 3 # v12
# PARTITION_TYPES
FPT_AREATYPE_GENERIC = 1
FPT_AREATYPE_CODE = 0
FPT_AREATYPE_ROM = 1
FPT_AREATYPE_DATA = 1
# COMPRESSION_TYPE
COMP_TYPE_NOT_COMPRESSED = 0
COMP_TYPE_HUFFMAN = 1
COMP_TYPE_LZMA = 2
dCompType = {
COMP_TYPE_NOT_COMPRESSED : None,
COMP_TYPE_HUFFMAN: "huff",
COMP_TYPE_LZMA: "lzma",
}
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Extension(object):
NAME = None
TYPE = None
LIST = None
def Banner(self, noSize=False):
nItems = "" if noSize or (self.LIST is None) else "[%d]" % len(getattr(self, self.LIST))
return ". Ext#%d %s%s:" % (self.TYPE, self.NAME, nItems)
def PrintItems(self, flog):
for i,e in enumerate(getattr(self, self.LIST)): print >>flog, "%6d: %s" % (i+1, e)
def LoadItems(self, stR, cls, cnt=None):
if cnt is None:
lst = []
while stR.o < len(stR.ab): lst.append(cls(stR))
else:
lst = [cls(stR) for i in xrange(cnt)]
stR.done()
setattr(self, self.LIST, lst)
#***************************************************************************
#***************************************************************************
#***************************************************************************
#***************************************************************************
#***************************************************************************
#***************************************************************************
class System_Info_Ext(Extension): # 0 : used in Mainfist
NAME = "SystemInfo"
TYPE = 0 # for system info extension
LIST = "indParts"
SYSTEM_INFO_EXTENSION = (
("uma_size", "L", ), # Minimum UMA size required for this SKU in bytes
("chipset_version", "L", ), # Chipset version
("img_default_hash", "32s", ), # SHA2 hash of a 'defaults' file added to the image. (/intel.cfg). The load manager is responsible for verifying the hash of this file and creating the default files at the first system boot.
("pageable_uma_size", "L", ), # Size of pageable space within UMA in bytes. Must be divisible by 4K.
("reserved_0", "Q", 0), #
("reserved_1", "L", 0), #
# INDEPENDENT_PARTITION_ENTRY[]
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.SYSTEM_INFO_EXTENSION)
self.img_default_hash = self.img_default_hash[::-1] # Reverse?
self.LoadItems(stR, Independent_Partition_Entry)
def dump(self, flog=sys.stdout):
print >>flog, "%s uma_size:0x%X, chipset_version:0x%X, pageable_uma_size:0x%X defaults_h:%s" % (self.Banner(), self.uma_size, self.chipset_version, self.pageable_uma_size, self.img_default_hash.encode("hex"))
self.PrintItems(flog)
#***************************************************************************
class Independent_Partition_Entry:
INDEPENDENT_PARTITION_ENTRY = (
("name", "4s", ), #
("version", "L", ), #
("user_id", "H", ), #
("reserved", "H", ), #
)
def __init__(self, stR):
stR.read(self, self.INDEPENDENT_PARTITION_ENTRY)
self.name = self.name.rstrip('\0')
def __str__(self):
return "[%-4s] user_id:0x%04X ver:0x%08X %X" % (self.name, self.user_id, self.version, self.reserved)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Init_Script_Ext(Extension): # 1 : used in Mainfist
NAME = "InitScript"
TYPE = 1 # for initialization script extension
LIST = "scripts"
# length: In bytes; equals (16 + 52*n) for this version where n is the number of modules in the initialization script
INIT_SCRIPT = (
("reserved", "L", 0), # Reserved for future use.
("number_of_modules", "L", ), # Number of modules in this initialization script. Cannot be more than MAX_MODULES (this is a configuration parameter defining the maximum number of modules supported by the system set at system build time).
# INIT_SCRIPT_ENTRY[] # initialization script extension entries
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.INIT_SCRIPT)
clsSize, remainder = divmod(stR.sizeLeft(), self.number_of_modules)
if remainder: raise Error("Init_Script_Ext data size == %d is not miltiple of nItems == %d" % stR.sizeLeft(), self.number_of_modules)
cls = {24: Init_Script_Entry, 28: Init_Script_Entry_v12}[clsSize]
self.LoadItems(stR, cls, self.number_of_modules)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
self.PrintItems(flog)
#***************************************************************************
class Init_Script_Entry:
INIT_SCRIPT_ENTRY = (
("partition_name", "4s", ), # Manifest Partition Name. This field identifies the manifest in which this module's hash will be found irregardles of manifest's physical location (i.e. FTP manifest may be physically located in NFTP flash partition during FW update).
("name", "12s", ), # Module Name
("bf_init_flags", "L", ), # Flags used govern initialization flow.
("bf_boot_type", "L", ), # Boot path flag bits to indicate which boot path(s) this module is applicable to. Bit 0 - Normal Bit 1 - HAP Bit 2 - HMRFPO Bit 3 - Temp Disable Bit 4 - Recovery Bit 5 - Safe Mode Bit 6 - FW Update Bits 7:31 - Reserved
)
def __init__(self, stR):
self.unk = None
stR.read(self, self.INIT_SCRIPT_ENTRY)
self.partition_name = self.partition_name.rstrip('\0')
self.name = self.name.rstrip('\0')
self.init_flags = Init_Script_Flags(self.bf_init_flags)
self.boot_type = Init_Script_Boot_Type(self.bf_boot_type)
def __str__(self):
return "%4s:%-12s Init: %08X (%s) Boot: %08X (%s)" % (self.partition_name, self.name, self.bf_init_flags, self.init_flags, self.bf_boot_type, self.boot_type)
#***************************************************************************
class Init_Script_Entry_v12:
INIT_SCRIPT_ENTRY = (
("partition_name", "4s", ), # Manifest Partition Name. This field identifies the manifest in which this module's hash will be found irregardles of manifest's physical location (i.e. FTP manifest may be physically located in NFTP flash partition during FW update).
("name", "12s", ), # Module Name
("bf_init_flags", "L", ), # Flags used govern initialization flow.
("bf_boot_type", "L", ), # Boot path flag bits to indicate which boot path(s) this module is applicable to. Bit 0 - Normal Bit 1 - HAP Bit 2 - HMRFPO Bit 3 - Temp Disable Bit 4 - Recovery Bit 5 - Safe Mode Bit 6 - FW Update Bits 7:31 - Reserved
("unk", "L", ), #
)
def __init__(self, stR):
stR.read(self, self.INIT_SCRIPT_ENTRY)
self.partition_name = self.partition_name.rstrip('\0')
self.name = self.name.rstrip('\0')
self.init_flags = Init_Script_Flags(self.bf_init_flags)
self.boot_type = Init_Script_Boot_Type(self.bf_boot_type)
def __str__(self):
return "%4s:%-12s Init: %08X (%s) Boot: %08X (%s) Unk: %X" % (self.partition_name, self.name, self.bf_init_flags, self.init_flags, self.bf_boot_type, self.boot_type, self.unk)
#***************************************************************************
class Init_Script_Flags: # !!! Not sure...
dRestart = {
RESTART_NOT_ALLOWED : "Not Allowed", # 0
RESTART_IMMEDIATLY : "Immediatly", # 1
RESTART_ON_NEXT_BOOT: "On Next Boot", # 2
}
INIT_SCRIPT_FLAGS = ( # BitFields
("Ibl", 0, 0),
("IsRemovable", 1, 1),
("InitImmediately", 2, 2),
("RestartPolicy", 3, 15),
("Cm0_u", 16, 16),
("Cm0_nu", 17, 17),
("Cm3", 18, 18),
# ("reserved", 19, 31),
)
def __init__(self, dw):
BitFields(self, dw, self.INIT_SCRIPT_FLAGS)
def __str__(self):
r = ListTrueBools(self, self.INIT_SCRIPT_FLAGS)
if self.RestartPolicy: r.append("Restart %s" % self.dRestart[self.RestartPolicy])
return ", ".join(r)
#***************************************************************************
class Init_Script_Boot_Type:
INIT_SCRIPT_BOOT_TYPE = ( # BitFields
("Normal", 0, 0),
("HAP", 1, 1),
("HMRFPO", 2, 2),
("TmpDisable", 3, 3),
("Recovery", 4, 4),
("SafeMode", 5, 5),
("FWUpdate", 6, 6),
# ("reserved", 7, 31),
)
def __init__(self, dw):
BitFields(self, dw, self.INIT_SCRIPT_BOOT_TYPE)
def __str__(self):
return ", ".join(ListTrueBools(self, self.INIT_SCRIPT_BOOT_TYPE))
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Feature_Permissions_Ext(Extension): # 2 : used in Mainfist
NAME = "FeaturePermissions"
TYPE = 2 # for feature permission extension
LIST = "permissions"
# length: In bytes; equals (12 + 2*n) for this version where n is the number of features in this extension
FEATURE_PERMISSIONS_EXTENSION = (
("num_of_features", "L", ), # Number of features feature numbering always starts from 0.
# FEATURE_PERMISSION_ENTRY[] # feature permission extension entries
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.FEATURE_PERMISSIONS_EXTENSION)
self.LoadItems(stR, Feature_Permission_Entry, self.num_of_features)
def dump(self, flog=sys.stdout):
print >>flog, "%s [%s]" % (self.Banner(), ", ".join("0x%04X" % e.user_id for e in self.permissions))
#***************************************************************************
class Feature_Permission_Entry:
FEATURE_PERMISSION_ENTRY = (
("user_id", "H", ), # User ID that may change feature state for feature 0.
("reserved", "H", 0), #
)
def __init__(self, stR):
stR.read(self, self.FEATURE_PERMISSION_ENTRY)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Partition_Info_Ext(Extension): # 3 : used in Mainfist
NAME = "PartitionInfo"
TYPE = 3 # for partition info extension
LIST = "modules"
# length: In bytes; equals (92 + 52*n) for this version where n is the number of modules in the manifest
MANIFEST_PARTITION_INFO_EXT = (
("partition_name", "4s", ), # Name of the partition
("partition_length", "L", ), # Length of complete partition before any process have been removed by the OEM or the firmware update process
("partition_hash", "32s", ), # SHA256 hash of the original complete partition covering everything in the partition except for the manifest (directory binaries and LUT)
("version_control_number", "L", ), # The version control number (VCN) is incremented whenever a change is made to the FW that makes it incompatible from an update perspective with previously released versions of the FW.
("partition_version", "L", ), # minor number
("data_format_version", "L", ), #
("instance_id", "L", ), #
("flags", "L", ), # Support multiple instances Y/N. Used for independently updated partitions that may have multiple instances (such as WLAN uCode or Localization)
("reserved", "16s", ('\0'*16, '\xFF'*16,)), # set to 0xff
("unknown0", "l", (0, 1, 3, -1)), # Was 0xffffffff
# MANIFEST_MODULE_INFO_EXT[] # Module info extension entries
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.MANIFEST_PARTITION_INFO_EXT)
self.partition_name = self.partition_name.rstrip('\0')
self.partition_hash = self.partition_hash[::-1] # Reverse?
self.LoadItems(stR, Module_Info)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner(True)
print >>flog, " Name: [%s]" % self.partition_name
print >>flog, " Length: %08X" % self.partition_length
print >>flog, " Hash: %s" % self.partition_hash.encode("hex")
print >>flog, " VCN: %d" % self.version_control_number
print >>flog, " Ver: %X, %X" % (self.partition_version, self.data_format_version)
print >>flog, " Instance ID: %d" % self.instance_id
print >>flog, " Flags: %d" % self.flags
print >>flog, " Unknown: %d" % self.unknown0
print >>flog, " Modules[%d]:" % len(self.modules)
self.PrintItems(flog)
#***************************************************************************
class Module_Info:
dModType = {
PROCESS_TYPE: "Proc", # 0
SHARED_LIBRARY_TYPE: "Lib ", # 1
DATA_TYPE: "Data", # 2
IUNIT_TYPE: "iUnt", # 3 v12
}
MANIFEST_MODULE_INFO_EXT = (
("name", "12s", ), # Character array. If name length is shorter than field size the name is padded with 0 bytes
("type", "B", (0,1,2,3)), # 0 - Process; 1 - Shared Library; 2 - Data; 3 - iUnit
("reserved0", "B", ), #
("reserved1", "H", (0, 0xFFFF)), # set to 0xffff
("metadata_size", "L", ), #
("metadata_hash", "32s" ), # For a process/shared library this is the SHA256 of the module metadata file; for a data module this is the SHA256 hash of the module binary itself
)
def __init__(self, stR):
stR.read(self, self.MANIFEST_MODULE_INFO_EXT)
self.name = self.name.rstrip('\0')
self.metadata_hash = self.metadata_hash[::-1] # Reverse
def __str__(self):
return "%-4s, Meta cb:%4X h:%s %s" % (self.dModType[self.type], self.metadata_size, self.metadata_hash.encode("hex"), self.name)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Shared_Lib_Ext(Extension): # 4 : used in Metadata
NAME = "SharedLib"
TYPE = 4 # for shared library extension
# length: In bytes equals 52 for this version
SHARED_LIB_EXTENSION = (
("context_size", "L", ), # Size in bytes of the shared library context
("total_alloc_virtual_space", "L", ), # Including padding pages for library growth. Currently set to a temporary value. This needs to be updated once the SHARED_CONTEXT_SIZE symbol is defined in the build process.
("code_base_address", "L", ), # Base address for the library private code in VAS. Must be 4KB aligned.
("tls_size", "L", ), # Size of Thread-Local-Storage used by the shared library.
("reserved", "L", ), # reserved bytes set to 0xffffffff
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.SHARED_LIB_EXTENSION)
stR.done()
def dump(self, flog=sys.stdout):
print >>flog, "%s context_size:0x%X, total_alloc_virtual_space:0x%X, code_base_address:0x%X, tls_size:0x%x" % (self.Banner(), self.context_size, self.total_alloc_virtual_space, self.code_base_address, self.tls_size)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Man_Process_Ext(Extension): # 5 : used in Metadata
NAME = "Process"
TYPE = 5 # for process attribute extension
# length: In bytes equals 160 + 2*n for this version where n is the number of group IDs entries in the extension
MAN_PROCESS_EXTENSION = (
("bf_flags", "L", ), # Flags
("main_thread_id", "L", ), # TID for main thread. Optional for IBL processes only. Must be 0 for other processes.
("priv_code_base_address", "L", ), # Base address for code. Address is in LAS for Bringup/Kernel VAS for other processes. Must be 4KB aligned
("uncompressed_priv_code_size","L", ), # Size of uncompressed process code. Does not include code for shared library.
("cm0_heap_size", "L", ), # Size of Thread-Local-Storage for the process
("bss_size", "L", ), #
("default_heap_size", "L", ), #
("main_thread_entry", "L", ), # VAS of entry point function for the process main thread
("allowed_sys_calls", "12s", ), # Bitmask of allowed system calls by the process
("user_id", "H", ), # Runtime User ID for process
("reserved_0", "L", ), # Temporary placeholder for thread base
("reserved_1", "H", 0), # Must be 0
("reserved_2", "Q", ), #
# group_ids['H'] # Group ID for process
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.MAN_PROCESS_EXTENSION)
abGIDs = stR.ab[stR.o:]
self.group_ids = list(struct.unpack("<%dH" % (len(abGIDs) / 2), abGIDs))
self.flags = Man_Process_Flags(self.bf_flags)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
print >>flog, " flags: %s" % self.flags
print >>flog, " main_thread_id: 0x%X" % self.main_thread_id
print >>flog, " priv_code_base_address: 0x%08X" % self.priv_code_base_address
print >>flog, " uncompressed_priv_code_size: 0x%X" % self.uncompressed_priv_code_size
print >>flog, " cm0_heap_size: 0x%X" % self.cm0_heap_size
print >>flog, " bss_size: 0x%X" % self.bss_size
print >>flog, " default_heap_size: 0x%X" % self.default_heap_size
print >>flog, " main_thread_entry: 0x%08X" % self.main_thread_entry
print >>flog, " allowed_sys_calls: %s" % self.allowed_sys_calls.encode("hex")
print >>flog, " user_id: 0x%04X" % self.user_id
print >>flog, " group_ids[%d]: [%s]" % (len(self.group_ids), ", ".join("0x%04X" % gid for gid in self.group_ids))
#***************************************************************************
class Man_Process_Flags:
MAN_PROCESS_FLAGS = ( # BitFields
("fault_tolerant", 0, 0), # Kernel exception policy: 0 - Reset System, 1 - Terminate Process
("permanent_process", 1, 1), # permanent process Y/N. A permanent process' code/rodata sections are not removed from RAM when it terminates normally in order to optimize its reload flow.
("single_instance", 2, 2), # Single Instance Y/N. When the process is spawned if it is already running in the system the spawn will fail.
("trusted_snd_rev_sender", 3, 3), # Trusted SendReceive Sender Y/N. If set this process is allowed to send IPC_SendReceive messages to any process (not only public).
("trusted_notify_sender", 4, 4), # Trusted Notify Sender Y/N. If set this process is allowed to send IPC_Notify notifications to any process (not only public).
("public_snd_rev_receiver", 5, 5), # Public SendReceive Receiver Y/N. If set any other process is allowed to send IPC_SendReceive messages to it (not only trusted).
("public_notify_receiver", 6, 6), # Public Notify Receiver Y/N. If set any other process is allowed to IPC_Notify notifications messages to it (not only trusted).
#("reserved", 7, 31), # reserved. Set to 0
)
def __init__(self, dw):
BitFields(self, dw, self.MAN_PROCESS_FLAGS)
def __str__(self):
return ", ".join(ListTrueBools(self, self.MAN_PROCESS_FLAGS))
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Threads_Ext(Extension): # 6 : used in Metadata
NAME = "Threads"
TYPE = 6 # for threads extension
LIST = "threads"
def __init__(self, ab):
self.LoadItems(StructReader(ab), Thread_Entry)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
self.PrintItems(flog)
#***************************************************************************
class Thread_Entry:
THREAD_ENTRY = (
("stack_size", "L", ), # Size of main thread stack in bytes (not including guard page including space reserved for TLS). Must be divisible by 4K with the following exception: if the default heap size is smaller than 4K the last thread's stack size may have any size.
("flags", "L", ), # Bit0 - set to 0 for live thread 1 for CM0-U-only thread; Bits 1-31 - reserved must be 0
("scheduling_policy", "L", ), # Bits 0-7: Scheduling Policy, 0 -> fixed priority; Bits 8-31: Scheduling attributes. For a fixed priority policy this is the scheduling priority of the thread.
("reserved", "L", ), #
)
def __init__(self, stR):
stR.read(self, self.THREAD_ENTRY)
def __str__(self):
return "stack_size:0x%08X, flags:%X, scheduling_policy:%08X" % (self.stack_size, self.flags, self.scheduling_policy)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Device_Ids_Ext(Extension): # 7 : used in Metadata
NAME = "DeviceIds"
TYPE = 7 # for device ids extension
LIST = "device_id_group"
def __init__(self, ab):
self.LoadItems(StructReader(ab), Device_Entry)
def dump(self, flog=sys.stdout):
print >>flog, "%s [%s]" % (self.Banner(), ", ".join("%08X" % v.device_id for v in self.device_id_group))
#***************************************************************************
class Device_Entry:
DEVICE_ENTRY = (
("device_id", "L", ), #
("reserved", "L", ), #
)
def __init__(self, stR):
stR.read(self, self.DEVICE_ENTRY)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Mmio_Ranges_Ext(Extension): # 8 : used in Metadata
NAME = "MmioRanges"
TYPE = 8 # for mmio ranges extension
LIST = "mmio_range_defs"
def __init__(self, ab):
self.LoadItems(StructReader(ab), Mmio_Range_Def)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
# self.PrintItems(flog)
for i,e in enumerate(getattr(self, self.LIST)): print >>flog, " %s" % (e)
#***************************************************************************
class Mmio_Range_Def:
MMIO_RANGE_DEF = (
("base", "L", ), # Base address of the MMIO range
("size", "L", ), # Limit in bytes of the MMIO range
("flags", "L", ), # Read access Y/N
)
def __init__(self, stR):
stR.read(self, self.MMIO_RANGE_DEF)
def __str__(self):
return "base:%08X, size:%08X, flags:%08X" % (self.base, self.size, self.flags)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Special_File_Producer_Ext(Extension): # 9 : used in Metadata
NAME = "SpecialFileProducer"
TYPE = 9 # for special file producer extension
LIST = "files"
SPECIAL_FILE_PRODUCER_EXTENSION = (
("major_number", "H", ), #
("flags", "H", ), #
# SPECIAL_FILE_DEF[]
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.SPECIAL_FILE_PRODUCER_EXTENSION)
self.LoadItems(stR, Special_File_Def)
def dump(self, flog=sys.stdout):
print >>flog, "%s major_number=0x%04X" % (self.Banner(), self.major_number)
self.PrintItems(flog)
#***************************************************************************
class Special_File_Def:
SPECIAL_FILE_DEF = (
("name", "12s", ), #
("access_mode", "H", ), #
("user_id", "H", ), #
("group_id", "H", ), #
("minor_number", "B", ), #
("reserved0", "B", ), #
("reserved1", "L", ), #
)
def __init__(self, stR):
stR.read(self, self.SPECIAL_FILE_DEF)
self.name = self.name.rstrip('\0')
def __str__(self):
return "%-12s access_mode:%04o, user_id:0x%04X group_id:0x%04X minor_number:%02X" % (self.name, self.access_mode, self.user_id, self.group_id, self.minor_number)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Mod_Attr_Ext(Extension): # 10 : used in Metadata
NAME = "ModAttr"
TYPE = 10 # for this module attribute extension
# length: In bytes; equals 56 for this version
dCompType = {
COMP_TYPE_NOT_COMPRESSED:" ",
COMP_TYPE_HUFFMAN:"Huff",
COMP_TYPE_LZMA:"LZMA",
}
MOD_ATTR_EXTENSION = (
("compression_type", "B", (0,1,2,)), # 0 - Uncompressed; 1 - Huffman Compressed; 2 - LZMA Compressed
("encrypted", "B", (0,1)), # Used as "encrypted" flag
("reserved1", "B", 0), # Must be 0
("reserved2", "B", 0), # Must be 0
("uncompressed_size", "L", ), # Uncompressed image size must be divisible by 4K
("compressed_size", "L", ), # Compressed image size. This is applicable for LZMA compressed modules only. For other modules should be the same as uncompressed_size field.
("module_number", "H", ), # Module number unique in the scope of the vendor.
("vendor_id", "H", 0x8086), # Vendor ID (PCI style). For Intel modules must be 0x8086.
("image_hash", "32s", ), # SHA2 Hash of uncompressed image
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.MOD_ATTR_EXTENSION)
self.image_hash = self.image_hash[::-1] # Reverse
stR.done()
def dump(self, flog=sys.stdout):
print >>flog, "%s %s enc=%d %08X->%08X id:%04X.%04X h:%s" % (self.Banner(), self.dCompType[self.compression_type], self.encrypted, self.compressed_size, self.uncompressed_size, self.module_number, self.vendor_id, self.image_hash.encode("hex"))
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Locked_Ranges_Ext(Extension): # 11 : used in Metadata
NAME = "LockedRanges"
TYPE = 11 # for unknown 11 extension
LIST = "ranges"
def __init__(self, ab):
self.LoadItems(StructReader(ab), Locked_Range)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
self.PrintItems(flog)
#***************************************************************************
class Locked_Range:
LOCKED_RANGE = (
("base", "L", ), # Base address in VAS of range to be locked. Must be divisible in 4KB.
("size", "L", ), # Size of range to be locked. Must be divisible in 4KB.
)
def __init__(self, stR):
stR.read(self, self.LOCKED_RANGE)
def __str__(self):
return "base:0x%08X, size:%X" % (self.base, self.size)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Client_System_Info_Ext(Extension): # 12 : used in Manifest
NAME = "ClientSystemInfo"
TYPE = 12 # for client system info extension
CLIENT_SYSTEM_INFO_EXTENSION = (
("fw_sku_caps", "L", ), #
("fw_sku_caps_reserved", "28s", '\xFF'*28), #
("bf_fw_sku_attributes", "Q", ), # Bits 0:3 - CSE region size in multiples of 0.5 MB Bits 4:6 - firmware sku; 0 for 5.0MB 1 for 1.5MB 2 for slim sku. Bit 7 - Patsberg support Y/N Bit 8 - M3 support Y/N Bit 9 - M0 support Y/N Bits 10:11 - reserved Bits 12:15 - Si class (all H M L) Bits 16:63 - reserved
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.CLIENT_SYSTEM_INFO_EXTENSION)
self.attr = Client_System_Sku_Attributes(self.bf_fw_sku_attributes)
stR.done()
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
print >>flog, " fw_sku_caps: %x" % self.fw_sku_caps
print >>flog, " fw_sku_attributes: %s" % self.attr
#***************************************************************************
class Client_System_Sku_Attributes:
dFirmwareSKU = {
0: "5.0MB",
1: "1.5MB",
2: "Slim",
3: "SPS",
}
CLIENT_SYSTEM_SKU_ATTRIBUTES = ( # BitFields
("CSE_region_size", 0, 3), # Bits 0:3 - CSE region size in multiples of 0.5 MB
("firmware_sku", 4, 6), # Bits 4:6 - firmware sku; 0 for 5.0MB 1 for 1.5MB 2 for slim sku.
("Patsberg", 7, 7), # Bit 7 - Patsberg support Y/N
("M3", 8, 8), # Bit 8 - M3 support Y/N
("M0", 9, 9), # Bit 9 - M0 support Y/N
# ("reserved0", 10, 11), # Bits 10:11 - reserved
("Si_class", 12, 15), # Bits 12:15 - Si class (all H M L)
# ("reserved1", 16, 63), # Bits 16:63 - reserved
)
def __init__(self, qw):
BitFields(self, qw, self.CLIENT_SYSTEM_SKU_ATTRIBUTES)
def __str__(self):
return "CSE region size: %.2f, firmware sku: %s, Si class: %X, %s" % (0.5*self.CSE_region_size, self.dFirmwareSKU[self.firmware_sku], self.Si_class, ", ".join(ListTrueBools(self, self.CLIENT_SYSTEM_SKU_ATTRIBUTES)))
#***************************************************************************
#***************************************************************************
#***************************************************************************
class User_Info_Ext(Extension): # 13 : used in Manifest
NAME = "UserInfo"
TYPE = 13 # for user info extension
LIST = "users"
def __init__(self, ab):
try:
self.LoadItems(StructReader(ab), User_Info_Entry)
except:
self.LoadItems(StructReader(ab), User_Info_Entry_new)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
self.PrintItems(flog)
#***************************************************************************
class User_Info_Entry:
USER_INFO_ENTRY = (
("user_id", "H", ), # User ID.
("reserved", "H", (0,1)), # Must be 0.
("non_volatile_storage_quota","L", ), # Maximum size of non-volatile storage area.
("ram_storage_quota", "L", ), # Maximum size of RAM storage area.
("wop_quota", "L", ), # Quota to use in wear-out prevention algorithm; in most cases this should match the non-volatile storage quota; however it is possible to virtually add quota to a user to allow it to perform more write operations on expense of another user. At build time the build system will check that the sum of all users WOP quota is not more than the sum of all users non-volatile storage quota.
("working_dir", "36s", ), # Starting directory for the user. Used when accessing files with a relative path. Character array; if name length is shorter than field size the name is padded with 0 bytes.
)
def __init__(self, stR):
stR.read(self, self.USER_INFO_ENTRY)
self.working_dir = self.working_dir.rstrip('\0')
assert self.working_dir.find('\0') < 0
def __str__(self):
return "user id:0x%04X, NV quota:%8X, RAM quota:%8X, WOP quota:%8X, working dir: [%s]" % (self.user_id, self.non_volatile_storage_quota, self.ram_storage_quota, self.wop_quota, self.working_dir)
#***************************************************************************
class User_Info_Entry_new:
USER_INFO_ENTRY = (
("user_id", "H", ), # User ID.
("reserved", "H", 0), # Must be 0.
("non_volatile_storage_quota","L", ), # Maximum size of non-volatile storage area.
("ram_storage_quota", "L", ), # Maximum size of RAM storage area.
("wop_quota", "L", ), # Quota to use in wear-out prevention algorithm; in most cases this should match the non-volatile storage quota; however it is possible to virtually add quota to a user to allow it to perform more write operations on expense of another user. At build time the build system will check that the sum of all users WOP quota is not more than the sum of all users non-volatile storage quota.
)
def __init__(self, stR):
stR.read(self, self.USER_INFO_ENTRY)
def __str__(self):
return "user id:0x%04X, NV quota:%8X, RAM quota:%8X, WOP quota:%8X" % (self.user_id, self.non_volatile_storage_quota, self.ram_storage_quota, self.wop_quota)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Package_Info_Ext(Extension): # 15 : used in TXE Mainfist
NAME = "PackageInfo"
TYPE = 15 # for partition info extension
LIST = "modules"
SIGNED_PACKAGE_INFO_EXT = (
("package_name", "4s", ), # Name of the partition
("version_control_number", "L", ), # The version control number (VCN) is incremented whenever a change is made to the FW that makes it incompatible from an update perspective with previously released versions of the FW.
("usage_bitmap", "16s", ), # Bitmap of usages depicted by this manifest, indicating which key is used to sign the manifest
("svn", "L", ), # Secure Version Number
("unknown", "L", ), #
("reserved", "12s", '\x00'*12), # Must be 0
# SIGNED_PACKAGE_INFO_EXT_ENTRY[] # Module info extension entries
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.SIGNED_PACKAGE_INFO_EXT)
self.package_name = self.package_name.rstrip('\0')
self.LoadItems(stR, Package_Info_Ext_Entry)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner(True)
print >>flog, " Name: [%s]" % self.package_name
print >>flog, " VCN: %d" % self.version_control_number
print >>flog, " Usage Bitmap: %s" % self.usage_bitmap.encode("hex")
print >>flog, " svn: %d" % self.svn
print >>flog, " unknown: 0x%X" % self.unknown
print >>flog, " Modules[%d]:" % len(self.modules)
self.PrintItems(flog)
#***************************************************************************
class Package_Info_Ext_Entry:
dModType = {
PROCESS_TYPE: "Proc", # 0
SHARED_LIBRARY_TYPE: "Lib ", # 1
DATA_TYPE: "Data", # 2
IUNIT_TYPE: "iUnt", # 3 v12
}
dHashAlgorithm = {
1: "SHA1",
2: "SHA256",
}
SIGNED_PACKAGE_INFO_EXT_ENTRY = (
("name", "12s", ), # Character array. If name length is shorter than field size the name is padded with 0 bytes
("type", "B", (0,1,2,3)), # 0 - Process; 1 - Shared Library; 2 - Data; 3 - iUnit
("hash_algorithm", "B", 2), # 0 - Reserved; 1 - SHA1; 2 - SHA256
("hash_size", "H", 32), # Size of Hash in bytes = N; BXT to support only SHA256. So N=32.
("metadata_size", "L", ), # Size of metadata file
("metadata_hash", "32s" ), # The SHA2 of the module metadata file
)
def __init__(self, stR):
stR.read(self, self.SIGNED_PACKAGE_INFO_EXT_ENTRY)
self.name = self.name.rstrip('\0')
self.metadata_hash = self.metadata_hash[::-1] # Reverse
def __str__(self):
return "%-4s, Meta cb:%4X h=%s[%d]:%s %s" % (self.dModType[self.type], self.metadata_size, self.dHashAlgorithm[self.hash_algorithm], self.hash_size, self.metadata_hash.encode("hex"), self.name)
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Unk_16_Ext(Extension): # 16 : used in Manifest (for iUnit)
NAME = "Unk_iUnit_16"
TYPE = 16 # for iUnit extension
UNK_IUNIT_16_EXT = (
("v0_1", "L", 1), #
("unk16", "16s", '\0'*16), #
("v2_3", "L", 3), #
("v3", "L", ), #
("v4_1", "L", 1), #
("h", "32s", ), #
("reserved", "24s", '\0'*24), #
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.UNK_IUNIT_16_EXT)
# self.h = self.h[::-1] # Reverse?
stR.done()
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
print >>flog, " %X %X %X %X h=%s" % (self.v0_1, self.v2_3, self.v3, self.v4_1, self.h.encode("hex"))
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Unk_18_Ext(Extension): # 18 : used in Manifest
NAME = "Unk_18"
TYPE = 18 # for user info extension
LIST = "records"
UNK_18_EXT = (
("items", "L", ), #
("unk", "16s", ), #
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.UNK_18_EXT)
self.LoadItems(stR, Unk_18_Ext_Entry)
def dump(self, flog=sys.stdout):
print >>flog, self.Banner()
print >>flog, " Records[%d] %s:" % (self.items, self.unk.encode("hex"))
self.PrintItems(flog)
#***************************************************************************
class Unk_18_Ext_Entry:
UNK_18_EXT_ENTRY = (
("ab", "56s", ), #
)
def __init__(self, stR):
stR.read(self, self.UNK_18_EXT_ENTRY)
def __str__(self):
return self.ab.encode("hex")
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Unk_22_Ext(Extension): # 22 : used in Manifest (v12)
NAME = "Unk_22"
TYPE = 22 # for v12 extension
UNK_22_EXT = (
("name", "4s", ), #
("unk24", "24s", ), #
("h", "32s", ), #
("reserved", "20s", '\0'*20), #
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.UNK_22_EXT)
# self.h = self.h[::-1] # Reverse?
stR.done()
def dump(self, flog=sys.stdout):
print >>flog, "%s [%s] u=%s h=%s" % (self.Banner(), self.name, self.unk24.encode("hex"), self.h.encode("hex"))
#***************************************************************************
#***************************************************************************
#***************************************************************************
class Unk_50_Ext(Extension): # 50 : used in Manifest (HP)
NAME = "Unk_50"
TYPE = 50 # for iUnit extension
UNK_50_EXT = (
("name", "4s", ), #
("dw0", "L", 0), #
)
def __init__(self, ab):
stR = StructReader(ab)
stR.read(self, self.UNK_50_EXT)
stR.done()
def dump(self, flog=sys.stdout):
print >>flog, "%s [%s]" % (self.Banner(), self.name)
#***************************************************************************
#***************************************************************************
#***************************************************************************
aExtHandlers = (
System_Info_Ext, # 0
Init_Script_Ext, # 1
Feature_Permissions_Ext, # 2
Partition_Info_Ext, # 3
Shared_Lib_Ext, # 4
Man_Process_Ext, # 5
Threads_Ext, # 6
Device_Ids_Ext, # 7
Mmio_Ranges_Ext, # 8
Special_File_Producer_Ext, # 9
Mod_Attr_Ext, # 10
Locked_Ranges_Ext, # 11
Client_System_Info_Ext, # 12
User_Info_Ext, # 13
# None, # 14
Package_Info_Ext, # 15
Unk_16_Ext, # 16
Unk_18_Ext, # 18
Unk_22_Ext, # 22
Unk_50_Ext, # 50
)
dExtHandlers = {ext.TYPE: ext for ext in aExtHandlers}
def Ext_ParseAll(obj, ab, o=0):
def EnumTags(ab, o=0):
while o < len(ab):
tag, cb = struct.unpack_from("<LL", ab, o)
assert cb >= 8
assert o+cb <= len(ab)
yield tag, ab[o+8:o+cb]
o += cb