-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathacpi.py
More file actions
4740 lines (4215 loc) · 156 KB
/
acpi.py
File metadata and controls
4740 lines (4215 loc) · 156 KB
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
# Copyright (c) 2015, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Intel Corporation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""ACPI module."""
import _acpi
import bits
import bits.cdata
import bits.pyfs
import bitfields
from cpudetect import cpulib
from collections import OrderedDict
import copy
from cStringIO import StringIO
import ctypes
from ctypes import *
import itertools
import os
import string
import struct
import ttypager
import unpack
def _id(v):
return v
class TableParseException(Exception): pass
class AcpiBuffer(str):
def __repr__(self):
return "AcpiBuffer(" + ' '.join("{:02x}".format(ord(c)) for c in self) + ")"
def __str__(self):
return repr(self)
def display_resources(name):
with ttypager.page():
for r in get_objpaths(name):
raw_descriptor = evaluate(r)
print r
print repr(raw_descriptor)
if raw_descriptor is None:
continue
for descriptor in parse_descriptor(raw_descriptor):
print descriptor
print
class small_resource(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('length', ctypes.c_uint8, 3),
('item_name', ctypes.c_uint8, 4),
('rtype', ctypes.c_uint8, 1),
]
class large_resource(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('item_name', ctypes.c_uint8, 7),
('rtype', ctypes.c_uint8, 1),
]
SMALL_RESOURCE, LARGE_RESOURCE = 0, 1
class resource_data(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("small_resource",)
_fields_ = [
('small_resource', small_resource),
('large_resource', large_resource),
]
def parse_descriptor(buf):
large_factory = [
parse_VendorDefinedLargeDescriptor,
parse_ExtendedInterruptDescriptor,
]
small_factory = [
parse_IRQDescriptor,
parse_StartDependentFunctionsDescriptor,
parse_VendorDefinedSmallDescriptor,
]
large_descriptor_dict = {
1 : Memory24BitRangeDescriptor,
2 : GenericRegisterDescriptor,
4 : parse_VendorDefinedLargeDescriptor,
5 : Memory32BitRangeDescriptor,
6 : FixedMemory32BitRangeDescriptor,
7 : DwordAddressSpaceDescriptor,
8 : WordAddressSpaceDescriptor,
9 : parse_ExtendedInterruptDescriptor,
0xA : QwordAddressSpaceDescriptor,
0xB : ExtendedAddressSpaceDescriptor,
0xC : None,
0xE : None,
}
small_descriptor_dict = {
4 : parse_IRQDescriptor,
5 : DMADescriptor,
6 : parse_StartDependentFunctionsDescriptor,
7 : EndDependentFunctionsDescriptor,
8 : IOPortDescriptor,
9 : FixedIOPortDescriptor,
0xA : FixedDMADescriptor,
0xE : parse_VendorDefinedSmallDescriptor,
0xF : EndTagDescriptor,
}
descriptors = list()
current = 0
end = len(buf)
while current < end:
cls = None
res = resource_data.from_buffer_copy(buf, current)
if res.rtype == LARGE_RESOURCE:
cls = large_descriptor_dict.get(res.large_resource.item_name)
elif res.rtype == SMALL_RESOURCE:
cls = small_descriptor_dict.get(res.small_resource.item_name)
if cls is not None:
if cls in large_factory or cls in small_factory:
descriptor = cls(buf[current:]).from_buffer_copy(buf, current)
else:
descriptor = cls.from_buffer_copy(buf, current)
current += descriptor.length
if res.rtype == LARGE_RESOURCE:
current += 3
elif res.rtype == SMALL_RESOURCE:
current += 1
descriptors.append(descriptor)
else:
return AcpiBuffer(buf[current:])
if len(descriptors):
return tuple(d for d in descriptors)
return buf
class IRQDescriptor2(bits.cdata.Struct):
"""IRQ Descriptor (Length=2)"""
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_) + [
('_INT', ctypes.c_uint16),
]
_interrupt_sharing_wakes = {
0x0: "Exclusive",
0x1: "Shared",
0x2: "ExclusiveAndWake",
0x3: "SharedAndWake",
}
_interrupt_polarities = {
0: "Active-High",
1: "Active-Low",
}
_interrupt_modes = {
0 : "Level-Triggered",
1 : "Edge-Triggered",
}
class irq_information_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('_HE', ctypes.c_uint8, 1),
('reserved', ctypes.c_uint8, 2),
('_LL', ctypes.c_uint8, 1),
('_SHR', ctypes.c_uint8, 2),
]
_formats = {
'_HE': unpack.format_table("{}", _interrupt_modes),
'_LL': unpack.format_table("{}", _interrupt_polarities),
'_SHR': unpack.format_table("{}", _interrupt_sharing_wakes),
}
class irq_information(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint8),
('bits', irq_information_bits),
]
class IRQDescriptor3(bits.cdata.Struct):
"""IRQ Descriptor (Length=3)"""
_pack_ = 1
_fields_ = copy.copy(IRQDescriptor2._fields_) +[
('information', irq_information),
]
def parse_IRQDescriptor(buf):
des = small_resource.from_buffer_copy(buf)
if des.length == 2:
return IRQDescriptor2
return IRQDescriptor3
class dma_mask_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('_SIZ', ctypes.c_uint8, 2),
('_BM', ctypes.c_uint8, 1),
('_TYP', ctypes.c_uint8, 2),
]
dma_types = {
0b00: "compatibility mode",
0b01: "Type A",
0b10: "Type B",
0b11: "Type F",
}
logical_device_bus_master_status = {
0: "Logical device is not a bus master",
1: "Logical device is a bus master",
}
transfer_type_preferences = {
0b00: "8-bit only",
0b01: "8- and 16-bit",
0b10: "16-bit only",
}
_formats = {
'_SIZ': unpack.format_table("{}", transfer_type_preferences),
'_BM': unpack.format_table("{}", logical_device_bus_master_status),
'_TYP': unpack.format_table("{}", dma_types),
}
class dma_mask(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint8),
('bits', dma_mask_bits),
]
class DMADescriptor(bits.cdata.Struct):
"""DMA Descriptor"""
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_) +[
('_DMA', ctypes.c_uint8),
('mask', ctypes.c_uint8),
]
class StartDependentFunctionsDescriptor0(bits.cdata.Struct):
"""Start Dependent Functions Descriptor (length=0)"""
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_)
class priority_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('compatibility_priority', ctypes.c_uint8, 2),
('performance_robustness', ctypes.c_uint8, 2),
]
configurations = {
0: "Good configuration",
1: "Acceptable configuration",
2: "Sub-optimal configuration",
}
_formats = {
'compatibility_priority': unpack.format_table("priority[1:0]={}", configurations),
'performance_robustness': unpack.format_table("priority[3:2]={}", configurations),
}
class priority(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint8),
('bits', priority_bits),
]
class StartDependentFunctionsDescriptor1(bits.cdata.Struct):
"""Start Dependent Functions Descriptor (length=1)"""
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_) +[
('priority', priority),
]
def parse_StartDependentFunctionsDescriptor(buf):
des = small_resource.from_buffer_copy(buf)
if des.length == 0:
return StartDependentFunctionsDescriptor0
return StartDependentFunctionsDescriptor1
class EndDependentFunctionsDescriptor(bits.cdata.Struct):
"""End Dependent Functions Descriptor"""
_fields_ = copy.copy(small_resource._fields_)
class ioport_information_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('_DEC', ctypes.c_uint8, 1),
]
_dec_statuses = {
1 : "logical device decodes 16-bit addresses",
0 : "logical device only decodes address bits[9:0]",
}
_formats = {
'_DEC': unpack.format_table("{}", _dec_statuses),
}
class ioport_information(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint8),
('bits', ioport_information_bits),
]
class IOPortDescriptor(bits.cdata.Struct):
"""I/O Port Descriptor"""
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_) +[
('ioport_information', ioport_information),
('_MIN', ctypes.c_uint16),
('_MAX', ctypes.c_uint16),
('_ALN', ctypes.c_uint8),
('_LEN', ctypes.c_uint8),
]
class FixedIOPortDescriptor(bits.cdata.Struct):
"""Fixed Location I/O Port Descriptor"""
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_) + [
('_BAS', ctypes.c_uint16),
('_LEN', ctypes.c_uint8),
]
class FixedDMADescriptor(bits.cdata.Struct):
"""Fixed DMA Descriptor"""
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_) + [
('_DMA', ctypes.c_uint16),
('_TYPE', ctypes.c_uint16),
('_SIZ', ctypes.c_uint8),
]
_dma_transfer_widths = {
0x00: "8-bit",
0x01: "16-bit",
0x02: "32-bit",
0x03: "64-bit",
0x04: "128-bit",
0x05: "256-bit",
}
_formats = {
'_SIZ': unpack.format_table("DMA transfer width={}", _dma_transfer_widths),
}
def VendorDefinedSmallDescriptor_factory(num_vendor_bytes):
"""Vendor-Defined Descriptor"""
class VendorDefinedSmallDescriptor(bits.cdata.Struct):
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_) + [
('vendor_byte_list', ctypes.c_uint8 * num_vendor_bytes),
]
return VendorDefinedSmallDescriptor
def parse_VendorDefinedSmallDescriptor(buf):
des = VendorDefinedSmallDescriptor_factory(0)
num_vendor_bytes = len(buf) - ctypes.sizeof(des)
return VendorDefinedSmallDescriptor_factory(num_vendor_bytes)
class EndTagDescriptor(bits.cdata.Struct):
"""End Tag"""
_pack_ = 1
_fields_ = copy.copy(small_resource._fields_) + [
('checksum', ctypes.c_uint8),
]
class memory_range_information_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('_RW', ctypes.c_uint8, 1),
]
_rw_statuses = {
1: "writeable (read/write)",
0: "non-writeable (read-only)",
}
_formats = {
'_RW': unpack.format_table("{}", _rw_statuses),
}
class memory_range_information(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint8),
('bits', memory_range_information_bits),
]
class Memory24BitRangeDescriptor(bits.cdata.Struct):
"""Memory 24-Bit Range Descriptor"""
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('information', memory_range_information),
('_MIN', ctypes.c_uint16),
('_MAX', ctypes.c_uint16),
('_ALN', ctypes.c_uint16),
('_LEN', ctypes.c_uint16),
]
def VendorDefinedLargeDescriptor_factory(num_vendor_bytes):
"""Vendor-Defined Descriptor"""
class VendorDefinedLargeDescriptor(bits.cdata.Struct):
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('uuid_sub_type', ctypes.c_uint8),
('uuid', bits.cdata.GUID),
('vendor_byte_list', ctypes.c_uint8 * num_vendor_bytes),
]
return VendorDefinedLargeDescriptor
def parse_VendorDefinedLargeDescriptor(buf):
des = VendorDefinedLargeDescriptor_factory(0)
num_vendor_bytes = len(buf) - ctypes.sizeof(des)
return VendorDefinedLargeDescriptor_factory(num_vendor_bytes)
class Memory32BitRangeDescriptor(bits.cdata.Struct):
"""32-Bit Memory Range Descriptor"""
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('information', memory_range_information),
('_MIN', ctypes.c_uint16),
('_MAX', ctypes.c_uint16),
('_ALN', ctypes.c_uint16),
('_LEN', ctypes.c_uint16),
]
class FixedMemory32BitRangeDescriptor(bits.cdata.Struct):
"""32-Bit Fixed Memory Range Descriptor"""
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('information', memory_range_information),
('_BAS', ctypes.c_uint32),
('_LEN', ctypes.c_uint32),
]
def _range_type_str(range_type):
if range_type >= 192 and range_type <= 255:
return 'OEM Defined'
_range_types = {
0: 'Memory range',
1: 'IO range',
2: 'Bus number range',
}
return _range_types.get(range_type, 'Reserved')
_decode_type = {
1: "bridge subtractively decodes (top level bridges only)",
0: "bridge positively decodes",
}
_min_address_fixed = {
1: "specified minimum address is fixed",
0: "specified minimum address is not fixed and can be changed",
}
_max_address_fixed = {
1: "specified maximum address is fixed",
0: "specified maximum address is not fixed",
}
class _resource_flags_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('reserved_0', ctypes.c_uint8, 1),
('_DEC', ctypes.c_uint8, 1),
('_MIF', ctypes.c_uint8, 1),
('_MAF', ctypes.c_uint8, 1),
('reserved_7_4', ctypes.c_uint8, 1),
]
_formats = {
'_DEC': unpack.format_table("{}", _decode_type),
'_MIF': unpack.format_table("{}", _min_address_fixed),
'_MAF': unpack.format_table("{}", _max_address_fixed),
}
class _resource_flags(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint8),
('bits', _resource_flags_bits),
]
class DwordAddressSpaceDescriptor(bits.cdata.Struct):
"""DWord Address Space Descriptor"""
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('range_type', ctypes.c_uint8),
('general_flags', _resource_flags),
('type_specific_flags', ctypes.c_uint8),
('address_space_granularity', ctypes.c_uint32),
('address_range_minimum', ctypes.c_uint32),
('address_range_maximum', ctypes.c_uint32),
('address_translation_offset', ctypes.c_uint32),
('address_length', ctypes.c_uint32),
]
_formats = {
'range_type': unpack.format_function("{:#x}", _range_type_str),
}
class WordAddressSpaceDescriptor(bits.cdata.Struct):
"""Word Address Space Descriptor"""
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('range_type', ctypes.c_uint8),
('general_flags', _resource_flags),
('type_specific_flags', ctypes.c_uint8),
('address_space_granularity', ctypes.c_uint16),
('address_range_minimum', ctypes.c_uint16),
('address_range_maximum', ctypes.c_uint16),
('address_translation_offset', ctypes.c_uint16),
('address_length', ctypes.c_uint16),
]
_formats = {
'range_type': unpack.format_function("{:#x}", _range_type_str),
}
_consumer_producer = {
1: "device consumes this resource",
0: "device produces and consumes this resource",
}
class interrupt_vector_info_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('consumer_producer', ctypes.c_uint8, 1),
('_HE', ctypes.c_uint8, 1),
('_LL', ctypes.c_uint8, 1),
('_SHR', ctypes.c_uint8, 2),
('reserved_7_5', ctypes.c_uint8, 3),
]
_formats = {
'consumer_producer': unpack.format_table("{}", _consumer_producer),
'_HE': unpack.format_table("{}", _interrupt_modes),
'_LL': unpack.format_table("{}", _interrupt_polarities),
'_SHR': unpack.format_table("{}", _interrupt_sharing_wakes),
}
class interrupt_vector_info(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint8),
('bits', interrupt_vector_info_bits),
]
def ExtendedInterruptDescriptor_factory(num_interrupts):
class ExtendedInterruptDescriptor(bits.cdata.Struct):
"""Extended Address Space Descriptor"""
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('interrupt_vector_flags', interrupt_vector_info),
('interrupt_table_length', ctypes.c_uint8),
('interrupt_number', ctypes.c_uint32 * num_interrupts),
]
return ExtendedInterruptDescriptor
def parse_ExtendedInterruptDescriptor(buf):
res = ExtendedInterruptDescriptor_factory(0).from_buffer_copy(buf)
return ExtendedInterruptDescriptor_factory(res.interrupt_table_length)
class QwordAddressSpaceDescriptor(bits.cdata.Struct):
"""QWord Address Space Descriptor"""
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('range_type', ctypes.c_uint8),
('general_flags', _resource_flags),
('type_specific_flags', ctypes.c_uint8),
('address_space_granularity', ctypes.c_uint64),
('address_range_minimum', ctypes.c_uint64),
('address_range_maximum', ctypes.c_uint64),
('address_translation_offset', ctypes.c_uint64),
('address_length', ctypes.c_uint64),
]
_formats = {
'range_type': unpack.format_function("{:#x}", _range_type_str)
}
class ExtendedAddressSpaceDescriptor(bits.cdata.Struct):
"""Extended Address Space Descriptor"""
_pack_ = 1
_fields_ = copy.copy(large_resource._fields_) + [
('length', ctypes.c_uint16),
('resource_type', ctypes.c_uint8),
('general_flags', _resource_flags),
('type_specific_flags', ctypes.c_uint8),
('revision_id', ctypes.c_uint8),
('reserved', ctypes.c_uint8),
('address_range_granularity', ctypes.c_uint64),
('address_range_minimum', ctypes.c_uint64),
('address_range_maximum', ctypes.c_uint64),
('address_translation_offset', ctypes.c_uint64),
('address_length', ctypes.c_uint64),
('type_specific_attribute', ctypes.c_uint64),
]
_formats = {
'resource_type': unpack.format_function("{:#x}", _range_type_str)
}
class AcpiLocalReference(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('ActualType', ctypes.c_uint32),
('NamePath', ctypes.c_char_p)
]
class _adr_pci(bits.cdata.Struct):
"""_ADR encoding for PCI bus"""
_pack_ = 1
_fields_ = [
('function', ctypes.c_uint32, 16),
('device', ctypes.c_uint32, 16),
]
class pci_address(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint32),
('bits', _adr_pci),
]
class PciRoutingTablePIC(bits.cdata.Struct):
"""PCI Routing Table Entry using PIC mode"""
_pack_ = 1
_fields_ = [
('address', pci_address),
('pin', ctypes.c_uint8),
('source', ctypes.c_uint8),
('source_index', ctypes.c_uint32),
]
class PciRoutingTablePICgsi(bits.cdata.Struct):
"""PCI Routing Table Entry using PIC mode and specifying a Global System Interrupt (GSI)"""
_pack_ = 1
_fields_ = [
('address', pci_address),
('pin', ctypes.c_uint8),
('source', ctypes.c_uint8),
('global_system_interrupt', ctypes.c_uint32),
]
class PciRoutingTableAPIC(bits.cdata.Struct):
"""PCI Routing Table Entry using APIC mode"""
_pack_ = 1
_fields_ = [
('address', pci_address),
('pin', ctypes.c_uint8),
('source', AcpiLocalReference),
('source_index', ctypes.c_uint32),
]
def parse_prt(pkg):
"""Parse PCI Routing Table (PRT) Entries"""
if isinstance(pkg, tuple):
if len(pkg) == 4:
if isinstance(pkg[2], AcpiLocalReference):
return PciRoutingTableAPIC(pci_address(pkg[0]), *pkg[1:])
if issubclass(type(pkg[2]), (int, long)):
if pkg[2] == 0:
return PciRoutingTablePICgsi(pci_address(pkg[0]), *pkg[1:])
else:
return PciRoutingTablePIC(pci_address(pkg[0]), *pkg[1:])
return pkg
def make_prt(data):
if data is None:
return None
data = parse_prt(data)
if isinstance(data, tuple):
return tuple(make_prt(v) for v in data)
return data
def display_prt(name="_PRT"):
with ttypager.page():
for path in get_objpaths(name):
print path
for prt in make_prt(evaluate(path)):
print prt
print
class AcpiPower(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('SystemLevel', ctypes.c_uint32),
('ResourceOrder', ctypes.c_uint32)
]
class AcpiProcessor(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('ProcId', ctypes.c_uint32),
('PblkAddress', ctypes.c_uint64),
('PblkLength', ctypes.c_uint32),
]
# ACPI_OBJECT_TYPE values
assert _acpi.ACPI_TYPE_EXTERNAL_MAX == 16, "Internal error: ACPI_OBJECT_TYPE enumeration not updated for new ACPICA"
(
ACPI_TYPE_ANY,
ACPI_TYPE_INTEGER,
ACPI_TYPE_STRING,
ACPI_TYPE_BUFFER,
ACPI_TYPE_PACKAGE,
ACPI_TYPE_FIELD_UNIT,
ACPI_TYPE_DEVICE,
ACPI_TYPE_EVENT,
ACPI_TYPE_METHOD,
ACPI_TYPE_MUTEX,
ACPI_TYPE_REGION,
ACPI_TYPE_POWER,
ACPI_TYPE_PROCESSOR,
ACPI_TYPE_THERMAL,
ACPI_TYPE_BUFFER_FIELD,
ACPI_TYPE_DDB_HANDLE,
ACPI_TYPE_DEBUG_OBJECT,
) = range(_acpi.ACPI_TYPE_EXTERNAL_MAX + 1)
ACPI_TYPE_LOCAL_REFERENCE = 0x14
_acpi_object_types = {
ACPI_TYPE_INTEGER: _id,
ACPI_TYPE_STRING: _id,
ACPI_TYPE_BUFFER: AcpiBuffer,
ACPI_TYPE_PACKAGE: (lambda t: tuple(_acpi_object_to_python(v) for v in t)),
ACPI_TYPE_POWER: (lambda args: AcpiPower(*args)),
ACPI_TYPE_PROCESSOR: (lambda args: AcpiProcessor(*args)),
ACPI_TYPE_LOCAL_REFERENCE: (lambda args: AcpiLocalReference(*args)),
}
def _acpi_object_to_python(acpi_object):
if acpi_object is None:
return None
object_type, value = acpi_object
return _acpi_object_types[object_type](value)
def ctypes_to_python(data):
if data is None:
return None
if isinstance(data, (list, tuple)):
return tuple(ctypes_to_python(v) for v in data)
if issubclass(type(data), (bits.cdata.Struct, bits.cdata.Union)):
return tuple(ctypes_to_python(getattr(data, f[0])) for f in data._fields_)
return data
def make_resources(data):
if data is None:
return None
if isinstance(data, tuple):
return tuple(make_resources(v) for v in data)
if isinstance(data, AcpiBuffer):
return parse_descriptor(data)
return data
def _acpi_object_from_python(obj):
if isinstance(obj, (int, long)):
return (ACPI_TYPE_INTEGER, obj)
# Must check AcpiBuffer before str, since AcpiBuffer derives from str
if isinstance(obj, AcpiBuffer):
return (ACPI_TYPE_BUFFER, obj)
if isinstance(obj, str):
return (ACPI_TYPE_STRING, obj)
if isinstance(obj, AcpiPower):
return (ACPI_TYPE_POWER, obj)
if isinstance(obj, AcpiProcessor):
return (ACPI_TYPE_PROCESSOR, obj)
# Must check tuple after any namedtuples, since namedtuples derive from tuple
if isinstance(obj, tuple):
return (ACPI_TYPE_PACKAGE, tuple(_acpi_object_from_python(arg) for arg in obj))
def evaluate(pathname, *args, **kwargs):
"""Evaluate an ACPI method and return the result.
By default, ACPI method evaluation allows reads and writes of I/O ports.
Pass the keyword argument unsafe_io=False to silently ignore I/O
operations."""
global acpi_unsafe_io
unsafe_io = kwargs.get("unsafe_io")
if unsafe_io is not None:
old_unsafe_io = acpi_unsafe_io
acpi_unsafe_io = unsafe_io
try:
return _acpi_object_to_python(_acpi._eval(pathname, tuple(_acpi_object_from_python(arg) for arg in args)))
finally:
if unsafe_io is not None:
acpi_unsafe_io = old_unsafe_io
acpi_object_types = {
ACPI_TYPE_INTEGER: 'ACPI_TYPE_INTEGER',
ACPI_TYPE_STRING: 'ACPI_TYPE_STRING',
ACPI_TYPE_BUFFER: 'ACPI_TYPE_BUFFER',
ACPI_TYPE_PACKAGE: 'ACPI_TYPE_PACKAGE',
ACPI_TYPE_FIELD_UNIT: 'ACPI_TYPE_FIELD_UNIT',
ACPI_TYPE_DEVICE: 'ACPI_TYPE_DEVICE',
ACPI_TYPE_EVENT: 'ACPI_TYPE_EVENT',
ACPI_TYPE_METHOD: 'ACPI_TYPE_METHOD',
ACPI_TYPE_MUTEX: 'ACPI_TYPE_MUTEX',
ACPI_TYPE_REGION: 'ACPI_TYPE_REGION',
ACPI_TYPE_POWER: 'ACPI_TYPE_POWER',
ACPI_TYPE_PROCESSOR: 'ACPI_TYPE_PROCESSOR',
ACPI_TYPE_THERMAL: 'ACPI_TYPE_THERMAL',
ACPI_TYPE_BUFFER_FIELD: 'ACPI_TYPE_BUFFER_FIELD',
ACPI_TYPE_DDB_HANDLE: 'ACPI_TYPE_DDB_HANDLE',
ACPI_TYPE_DEBUG_OBJECT: 'ACPI_TYPE_DEBUG_OBJECT',
ACPI_TYPE_LOCAL_REFERENCE: 'ACPI_TYPE_LOCAL_REFERENCE',
}
def ObjectInfo_factory(ids_length):
class object_info_flags_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('current_status_valid', ctypes.c_uint8, 1),
('address_valid', ctypes.c_uint8, 1),
('hardware_id_valid', ctypes.c_uint8, 1),
('unique_id_valid', ctypes.c_uint8, 1),
('subsystem_id_valid', ctypes.c_uint8, 1),
('compatibility_id_valid', ctypes.c_uint8, 1),
('highest_dstates_valid', ctypes.c_uint8, 1),
('lowest_dstates_valid', ctypes.c_uint8, 1),
]
class object_info_flags(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint8),
('bits', object_info_flags_bits),
]
class current_status_flags_bits(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('present', ctypes.c_uint32, 1),
('enabled', ctypes.c_uint32, 1),
('visible', ctypes.c_uint32, 1),
('functional', ctypes.c_uint32, 1),
('battery_present', ctypes.c_uint32, 1),
]
class current_status_flags(bits.cdata.Union):
_pack_ = 1
_anonymous_ = ("bits",)
_fields_ = [
('data', ctypes.c_uint32),
('bits', current_status_flags_bits),
]
class ObjectInfo_factory(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('info_size', ctypes.c_uint32),
('name', ctypes.c_char * 4),
('object_type', ctypes.c_uint32),
('parameter_count', ctypes.c_uint8),
('valid', ctypes.c_uint8),
('flags', object_info_flags),
('highest_dstates', ctypes.c_uint8 * 4),
('lowest_dstates', ctypes.c_uint8 * 5),
('current_status', current_status_flags),
('address', ctypes.c_uint64),
('hardware_id', get_string())
('unique_id', get_string())
('subsystem_id', get_string())
('compatibility_id_count', ctypes.c_uint32),
('compatibility_id_length', ctypes.c_uint32),
('ids', ctypes.c_uint8 * ids_length),
]
_formats = {
'object_type': unpack.format_table("{}", acpi_object_types),
}
def get_string():
length, offset = u.unpack("IP")
if not length:
return None
return s.unpack_peek_one("{}x{}s".format(offset - addr, length)).split("\x00", 1)[0]
class ObjectInfo(unpack.Struct):
def __init__(self, data, addr):
super(ObjectInfo, self).__init__()
u = unpack.Unpackable(data)
s = unpack.Unpackable(data)
self.add_field('info_size', u.unpack_one("<I"))
self.add_field('name', u.unpack_one("4s"))
self.add_field('object_type', u.unpack_one("<I"), unpack.format_table("{}", acpi_object_types))
self.add_field('parameter_count', u.unpack_one("B"))
self.add_field('valid', u.unpack_one("B"))
self.add_field('current_status_valid', bool(bitfields.getbits(self.valid, 0)), "valid[0]={}")
self.add_field('address_valid', bool(bitfields.getbits(self.valid, 1)), "valid[1]={}")
self.add_field('hardware_id_valid', bool(bitfields.getbits(self.valid, 2)), "valid[2]={}")
self.add_field('unique_id_valid', bool(bitfields.getbits(self.valid, 3)), "valid[3]={}")
self.add_field('subsystem_id_valid', bool(bitfields.getbits(self.valid, 4)), "valid[4]={}")
self.add_field('compatibility_id_valid', bool(bitfields.getbits(self.valid, 5)), "valid[5]={}")
self.add_field('highest_dstates_valid', bool(bitfields.getbits(self.valid, 6)), "valid[6]={}")
self.add_field('lowest_dstates_valid', bool(bitfields.getbits(self.valid, 7)), "valid[7]={}")
self.add_field('flags', u.unpack_one("B"))
self.add_field('highest_dstates', tuple(u.unpack_one("B") for i in range(4)))
self.add_field('lowest_dstates', tuple(u.unpack_one("B") for i in range(5)))
self.add_field('current_status', u.unpack_one("<I"))
if self.current_status_valid:
self.add_field('present', bool(bitfields.getbits(self.current_status, 0)), "current_status[0]={}")
self.add_field('enabled', bool(bitfields.getbits(self.current_status, 1)), "current_status[1]={}")
self.add_field('visible', bool(bitfields.getbits(self.current_status, 2)), "current_status[2]={}")
self.add_field('functional', bool(bitfields.getbits(self.current_status, 3)), "current_status[3]={}")
self.add_field('battery_present', bool(bitfields.getbits(self.current_status, 4)), "current_status[4]={}")
# Deal with padding before the 8-byte address field
ptralign = struct.calcsize("I0P")
if u.offset % ptralign != 0:
u.skip(ptralign - (u.offset % ptralign))
self.add_field('address', u.unpack_one("<Q"))
def get_string():
length, offset = u.unpack("IP")
if not length:
return None
return s.unpack_peek_one("{}x{}s".format(offset - addr, length)).split("\x00", 1)[0]
self.add_field('hardware_id', get_string())
self.add_field('unique_id', get_string())
self.add_field('subsystem_id', get_string())
self.add_field('compatibility_id_count', u.unpack_one("<I"))
self.add_field('compatibility_id_length', u.unpack_one("<I"))
self.add_field('compatibility_ids', tuple(get_string() for i in range(self.compatibility_id_count)))
def scope(path):
try:
prefix, _ = path.rsplit('.', 1)
return prefix
except ValueError:
return "/"
def parse_table(signature, instance=1):
addr = get_table_addr(signature, instance)
if addr is None:
return None
signature = string.rstrip(signature,"!")
return globals()[signature](addr)
def make_compat_parser(signature):
def parse(printflag=False, instance=1):
table = parse_table(signature, instance)
if table is None:
return None
if printflag:
with ttypager.page():
print table
return table
return parse
class RSDP_v1(bits.cdata.Struct):
_pack_ = 1
_fields_ = [
('signature', ctypes.c_char * 8),
('checksum', ctypes.c_uint8),
('oemid', ctypes.c_char * 6),
('revision', ctypes.c_uint8),
('rsdt_address', ctypes.c_uint32),
]
class RSDP_v2(bits.cdata.Struct):
_pack_ = 1
_fields_ = copy.copy(RSDP_v1._fields_) + [
('length', ctypes.c_uint32),
('xsdt_address', ctypes.c_uint64),
('extended_checksum', ctypes.c_uint8),
('reserved', ctypes.c_uint8 * 3),
]
def RSDP(val):
"""Create class based on decode of an RSDP table from address or filename."""
addr = val
if isinstance(val, str):