-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathaml.rs
More file actions
2716 lines (2414 loc) · 76.6 KB
/
aml.rs
File metadata and controls
2716 lines (2414 loc) · 76.6 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 © 2019 Intel Corporation
// Copyright © 2023 Rivos, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
extern crate alloc;
use crate::{gas, Aml, AmlSink};
use alloc::string::String;
use alloc::{vec, vec::Vec};
// AML byte stream defines
const ZEROOP: u8 = 0x00;
const ONEOP: u8 = 0x01;
const NAMEOP: u8 = 0x08;
const BYTEPREFIX: u8 = 0x0a;
const WORDPREFIX: u8 = 0x0b;
const DWORDPREFIX: u8 = 0x0c;
const STRINGOP: u8 = 0x0d;
const QWORDPREFIX: u8 = 0x0e;
const SCOPEOP: u8 = 0x10;
const BUFFEROP: u8 = 0x11;
const PACKAGEOP: u8 = 0x12;
const VARPACKAGEOP: u8 = 0x13;
const METHODOP: u8 = 0x14;
const IRQNOFLAGSDESC: u8 = 0x22;
const IRQDESC: u8 = 0x23;
const DUALNAMEPREFIX: u8 = 0x2e;
const MULTINAMEPREFIX: u8 = 0x2f;
const NAMECHARBASE: u8 = 0x40;
const EXTOPPREFIX: u8 = 0x5b;
const MUTEXOP: u8 = 0x01;
const CREATEFIELDOP: u8 = 0x13;
const ACQUIREOP: u8 = 0x23;
const RELEASEOP: u8 = 0x27;
const OPREGIONOP: u8 = 0x80;
const FIELDOP: u8 = 0x81;
const DEVICEOP: u8 = 0x82;
const POWERRESOURCEOP: u8 = 0x84;
const LOCAL0OP: u8 = 0x60;
const ARG0OP: u8 = 0x68;
const STOREOP: u8 = 0x70;
const ADDOP: u8 = 0x72;
const CONCATOP: u8 = 0x73;
const SUBTRACTOP: u8 = 0x74;
const MULTIPLYOP: u8 = 0x77;
const SHIFTLEFTOP: u8 = 0x79;
const SHIFTRIGHTOP: u8 = 0x7a;
const ANDOP: u8 = 0x7b;
const NANDOP: u8 = 0x7c;
const OROP: u8 = 0x7d;
const NOROP: u8 = 0x7e;
const XOROP: u8 = 0x7f;
const DEREFOFOP: u8 = 0x83;
const CONCATRESOP: u8 = 0x84;
const MODOP: u8 = 0x85;
const NOTIFYOP: u8 = 0x86;
const SIZEOFOP: u8 = 0x87;
const INDEXOP: u8 = 0x88;
const CREATEDWFIELDOP: u8 = 0x8a;
const OBJECTTYPEOP: u8 = 0x8e;
const CREATEQWFIELDOP: u8 = 0x8f;
const LNOTOP: u8 = 0x92;
const LEQUALOP: u8 = 0x93;
const LGREATEROP: u8 = 0x94;
const LLESSOP: u8 = 0x95;
const TOBUFFEROP: u8 = 0x96;
const TOINTEGEROP: u8 = 0x99;
const TOSTRINGOP: u8 = 0x9c;
const MIDOP: u8 = 0x9e;
const IFOP: u8 = 0xa0;
const ELSEOP: u8 = 0xa1;
const WHILEOP: u8 = 0xa2;
const RETURNOP: u8 = 0xa4;
const ONESOP: u8 = 0xff;
// AML resouce data fields
const IOPORTDESC: u8 = 0x47;
const ENDTAG: u8 = 0x79;
const REGDESC: u8 = 0x82;
const MEMORY32FIXEDDESC: u8 = 0x86;
const DWORDADDRSPACEDESC: u8 = 0x87;
const WORDADDRSPACEDESC: u8 = 0x88;
const EXTIRQDESC: u8 = 0x89;
const QWORDADDRSPACEDESC: u8 = 0x8A;
/// Zero object in ASL.
pub const ZERO: Zero = Zero {};
pub struct Zero {}
impl Aml for Zero {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(ZEROOP);
}
}
/// One object in ASL.
pub const ONE: One = One {};
pub struct One {}
impl Aml for One {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(ONEOP);
}
}
/// Ones object represents all bits 1.
pub const ONES: Ones = Ones {};
pub struct Ones {}
impl Aml for Ones {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(ONESOP);
}
}
/// Represents Namestring to construct ACPI objects like
/// Name/Device/Method/Scope and so on...
pub struct Path {
root: bool,
name_parts: Vec<[u8; 4]>,
}
impl Aml for Path {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
if self.root {
sink.byte(b'\\');
}
match self.name_parts.len() {
0 => panic!("Name cannot be empty"),
1 => {}
2 => {
sink.byte(DUALNAMEPREFIX);
}
n => {
sink.byte(MULTINAMEPREFIX);
sink.byte(n as u8);
}
};
for part in self.name_parts.clone().iter_mut() {
sink.vec(part);
}
}
}
impl Path {
/// Per ACPI Spec, the Namestring split by "." has 4 bytes long. So any name
/// not has 4 bytes will not be accepted.
pub fn new(name: &str) -> Self {
let root = name.starts_with('\\');
let offset = root as usize;
let mut name_parts = Vec::new();
for part in name[offset..].split('.') {
assert_eq!(part.len(), 4);
let mut name_part = [0u8; 4];
name_part.copy_from_slice(part.as_bytes());
name_parts.push(name_part);
}
Path { root, name_parts }
}
}
impl From<&str> for Path {
fn from(s: &str) -> Self {
Path::new(s)
}
}
pub type Byte = u8;
impl Aml for Byte {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
match *self {
0 => ZERO.to_aml_bytes(sink),
1 => ONE.to_aml_bytes(sink),
_ => {
sink.byte(BYTEPREFIX);
sink.byte(*self);
}
}
}
}
pub type Word = u16;
impl Aml for Word {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
if *self <= Byte::MAX.into() {
(*self as Byte).to_aml_bytes(sink);
} else {
sink.byte(WORDPREFIX);
sink.word(*self);
}
}
}
pub type DWord = u32;
impl Aml for DWord {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
if *self <= Word::MAX.into() {
(*self as Word).to_aml_bytes(sink);
} else {
sink.byte(DWORDPREFIX);
sink.dword(*self);
}
}
}
pub type QWord = u64;
impl Aml for QWord {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
if *self <= DWord::MAX.into() {
(*self as DWord).to_aml_bytes(sink);
} else {
sink.byte(QWORDPREFIX);
sink.qword(*self);
}
}
}
/// Name object. bytes represents the raw AML data for it.
pub struct Name {
bytes: Vec<u8>,
}
impl Aml for Name {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.vec(&self.bytes);
}
}
impl Name {
/// Create Name object:
///
/// * `path` - The namestring.
/// * `inner` - AML objects contained in this namespace.
pub fn new(path: Path, inner: &dyn Aml) -> Self {
let mut bytes = vec![NAMEOP];
path.to_aml_bytes(&mut bytes);
inner.to_aml_bytes(&mut bytes);
Name { bytes }
}
/// Create Field name object
///
/// * 'field_name' - name string
pub fn new_field_name(field_name: &str) -> Self {
let mut bytes: Vec<u8> = Vec::new();
bytes.extend_from_slice(field_name.as_bytes());
Name { bytes }
}
}
/// Package object. 'children' represents the ACPI objects contained in this package.
pub struct Package<'a> {
children: Vec<&'a dyn Aml>,
}
impl Aml for Package<'_> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
let mut bytes = vec![self.children.len() as u8];
for child in &self.children {
child.to_aml_bytes(&mut bytes);
}
let pkg_length = create_pkg_length(bytes.len(), true);
sink.byte(PACKAGEOP);
sink.vec(&pkg_length);
sink.vec(&bytes);
}
}
impl<'a> Package<'a> {
/// Create Package object:
pub fn new(children: Vec<&'a dyn Aml>) -> Self {
Package { children }
}
}
/// Package object, but can be built dynamically
pub struct PackageBuilder {
data: Vec<u8>,
elements: usize,
}
impl Aml for PackageBuilder {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
let pkg_length = create_pkg_length(self.data.len() + 1, true);
sink.byte(PACKAGEOP);
sink.vec(&pkg_length);
sink.byte(self.elements as u8);
sink.vec(&self.data);
}
}
impl AmlSink for PackageBuilder {
fn byte(&mut self, byte: u8) {
self.data.push(byte);
}
fn vec(&mut self, v: &[u8]) {
self.data.extend_from_slice(v);
}
}
impl PackageBuilder {
/// Create new PackageBuilder
pub fn new() -> Self {
Self {
data: Vec::new(),
elements: 0,
}
}
pub fn add_element(&mut self, aml: &dyn Aml) {
aml.to_aml_bytes(self);
self.elements += 1;
}
}
impl Default for PackageBuilder {
fn default() -> Self {
Self::new()
}
}
/// Variable Package Term
pub struct VarPackageTerm<'a> {
data: &'a dyn Aml,
}
impl Aml for VarPackageTerm<'_> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
let mut bytes = Vec::new();
self.data.to_aml_bytes(&mut bytes);
let pkg_length = create_pkg_length(bytes.len(), true);
sink.byte(VARPACKAGEOP);
sink.vec(&pkg_length);
sink.vec(&bytes);
}
}
impl<'a> VarPackageTerm<'a> {
/// Create Variable Package Term
pub fn new(data: &'a dyn Aml) -> Self {
VarPackageTerm { data }
}
}
/*
From the ACPI spec for PkgLength:
"The high 2 bits of the first byte reveal how many follow bytes are in the PkgLength. If the
PkgLength has only one byte, bit 0 through 5 are used to encode the package length (in other
words, values 0-63). If the package length value is more than 63, more than one byte must be
used for the encoding in which case bit 4 and 5 of the PkgLeadByte are reserved and must be zero.
If the multiple bytes encoding is used, bits 0-3 of the PkgLeadByte become the least significant 4
bits of the resulting package length value. The next ByteData will become the next least
significant 8 bits of the resulting value and so on, up to 3 ByteData bytes. Thus, the maximum
package length is 2**28."
*/
/* Also used for NamedField but in that case the length is not included in itself */
fn create_pkg_length(len: usize, include_self: bool) -> Vec<u8> {
let mut result = Vec::with_capacity(4);
/* PkgLength is inclusive and includes the length bytes */
let length_length = if len < (2usize.pow(6) - 1) {
1
} else if len < (2usize.pow(12) - 2) {
2
} else if len < (2usize.pow(20) - 3) {
3
} else {
4
};
let length = len + if include_self { length_length } else { 0 };
match length_length {
1 => result.push(length as u8),
2 => {
result.push((1u8 << 6) | (length & 0xf) as u8);
result.push((length >> 4) as u8)
}
3 => {
result.push((2u8 << 6) | (length & 0xf) as u8);
result.push((length >> 4) as u8);
result.push((length >> 12) as u8);
}
_ => {
result.push((3u8 << 6) | (length & 0xf) as u8);
result.push((length >> 4) as u8);
result.push((length >> 12) as u8);
result.push((length >> 20) as u8);
}
}
result
}
/// EISAName object. 'value' means the encoded u32 EisaIdString.
pub struct EISAName {
value: DWord,
}
impl EISAName {
/// Per ACPI Spec, the EisaIdString must be a String
/// object of the form UUUNNNN, where U is an uppercase letter
/// and N is a hexadecimal digit. No asterisks or other characters
/// are allowed in the string.
pub fn new(name: &str) -> Self {
assert_eq!(name.len(), 7);
let data = name.as_bytes();
let value: u32 = ((u32::from(data[0].checked_sub(NAMECHARBASE).unwrap()) << 26)
| (u32::from(data[1].checked_sub(NAMECHARBASE).unwrap()) << 21)
| (u32::from(data[2].checked_sub(NAMECHARBASE).unwrap()) << 16)
| (name.chars().nth(3).unwrap().to_digit(16).unwrap() << 12)
| (name.chars().nth(4).unwrap().to_digit(16).unwrap() << 8)
| (name.chars().nth(5).unwrap().to_digit(16).unwrap() << 4)
| name.chars().nth(6).unwrap().to_digit(16).unwrap())
.swap_bytes();
EISAName { value }
}
}
impl Aml for EISAName {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
self.value.to_aml_bytes(sink);
}
}
pub type Usize = usize;
impl Aml for Usize {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
#[cfg(target_pointer_width = "16")]
(*self as u16).to_aml_bytes(sink);
#[cfg(target_pointer_width = "32")]
(*self as u32).to_aml_bytes(sink);
#[cfg(target_pointer_width = "64")]
(*self as u64).to_aml_bytes(sink);
}
}
fn create_aml_string(v: &str, sink: &mut dyn AmlSink) {
sink.byte(STRINGOP);
sink.vec(v.as_bytes());
sink.byte(0x0); /* NullChar */
}
/// implement Aml trait for 'str' so that 'str' can be directly append to the aml vector
pub type AmlStr = &'static str;
impl Aml for AmlStr {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
create_aml_string(self, sink);
}
}
/// implement Aml trait for 'String'. So purpose with str.
pub type AmlString = String;
impl Aml for AmlString {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
create_aml_string(self, sink);
}
}
/// ResouceTemplate object. 'children' represents the ACPI objects in it.
pub struct ResourceTemplate<'a> {
children: Vec<&'a dyn Aml>,
}
impl Aml for ResourceTemplate<'_> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
let mut bytes = Vec::new();
// Add buffer data
for child in &self.children {
child.to_aml_bytes(&mut bytes);
}
// Mark with end and mark checksum as as always valid
bytes.push(ENDTAG);
bytes.push(0); /* zero checksum byte */
// Buffer length is an encoded integer including buffer data
// and EndTag and checksum byte
let mut buffer_length = Vec::with_capacity(4);
bytes.len().to_aml_bytes(&mut buffer_length);
// PkgLength is everything else
let pkg_length = create_pkg_length(bytes.len() + buffer_length.len(), true);
sink.byte(BUFFEROP);
sink.vec(&pkg_length);
sink.vec(&buffer_length);
sink.vec(&bytes);
}
}
impl<'a> ResourceTemplate<'a> {
/// Create ResouceTemplate object
pub fn new(children: Vec<&'a dyn Aml>) -> Self {
ResourceTemplate { children }
}
}
/// Memory32Fixed object with read_write accessing type, and the base address/length.
pub struct Memory32Fixed {
read_write: bool, /* true for read & write, false for read only */
base: u32,
length: u32,
}
impl Memory32Fixed {
/// Create Memory32Fixed object.
pub fn new(read_write: bool, base: u32, length: u32) -> Self {
Memory32Fixed {
read_write,
base,
length,
}
}
}
impl Aml for Memory32Fixed {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(MEMORY32FIXEDDESC); /* 32bit Fixed Memory Range Descriptor */
for byte in 9u16.to_le_bytes() {
sink.byte(byte);
}
// 9 bytes of payload
sink.byte(self.read_write as u8);
sink.dword(self.base);
sink.dword(self.length);
}
}
#[derive(Copy, Clone)]
enum AddressSpaceType {
Memory,
IO,
BusNumber,
}
/// AddressSpaceCacheable represent cache types for AddressSpace object
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum AddressSpaceCacheable {
NotCacheable,
Cacheable,
WriteCombining,
PreFetchable,
}
#[deprecated = "Spelling error - use AddressSpaceCacheable"]
pub type AddressSpaceCachable = AddressSpaceCacheable;
/// AddressSpace structure with type, resouce range and flags to
/// construct Memory/IO/BusNumber objects
pub struct AddressSpace<T> {
type_: AddressSpaceType,
min: T,
max: T,
type_flags: u8,
translation: Option<T>,
}
impl<T: Default> AddressSpace<T> {
/// Create DWordMemory/QWordMemory object
pub fn new_memory(
cacheable: AddressSpaceCacheable,
read_write: bool,
min: T,
max: T,
translation: Option<T>,
) -> Self {
AddressSpace {
type_: AddressSpaceType::Memory,
min,
max,
type_flags: ((cacheable as u8) << 1) | read_write as u8,
translation,
}
}
/// Create WordIO/DWordIO/QWordIO object
pub fn new_io(min: T, max: T, translation: Option<T>) -> Self {
AddressSpace {
type_: AddressSpaceType::IO,
min,
max,
type_flags: 3, /* EntireRange */
translation,
}
}
/// Create WordBusNumber object
pub fn new_bus_number(min: T, max: T) -> Self {
AddressSpace {
type_: AddressSpaceType::BusNumber,
min,
max,
type_flags: 0,
translation: None,
}
}
fn push_header(&self, sink: &mut dyn AmlSink, descriptor: u8, length: usize) {
sink.byte(descriptor); /* Word Address Space Descriptor */
for byte in (length as u16).to_le_bytes() {
sink.byte(byte);
}
sink.byte(self.type_ as u8); /* type */
let generic_flags = (1 << 2) /* Min Fixed */ | (1 << 3); /* Max Fixed */
sink.byte(generic_flags);
sink.byte(self.type_flags);
}
}
impl Aml for AddressSpace<u16> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
self.push_header(
sink,
WORDADDRSPACEDESC, /* Word Address Space Descriptor */
3 + 5 * core::mem::size_of::<u16>(), /* 3 bytes of header + 5 u16 fields */
);
sink.word(0); /* Granularity */
sink.word(self.min); /* Min */
sink.word(self.max); /* Max */
sink.word(self.translation.unwrap_or(0));
let len = self.max - self.min + 1;
sink.word(len); /* Length */
}
}
impl Aml for AddressSpace<u32> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
self.push_header(
sink,
DWORDADDRSPACEDESC, /* DWord Address Space Descriptor */
3 + 5 * core::mem::size_of::<u32>(), /* 3 bytes of header + 5 u32 fields */
);
sink.dword(0); /* Granularity */
sink.dword(self.min); /* Min */
sink.dword(self.max); /* Max */
sink.dword(self.translation.unwrap_or(0)); /* Translation */
let len = self.max - self.min + 1;
sink.dword(len); /* Length */
}
}
impl Aml for AddressSpace<u64> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
self.push_header(
sink,
QWORDADDRSPACEDESC, /* QWord Address Space Descriptor */
3 + 5 * core::mem::size_of::<u64>(), /* 3 bytes of header + 5 u64 fields */
);
sink.qword(0); /* Granularity */
sink.qword(self.min); /* Min */
sink.qword(self.max); /* Max */
sink.qword(self.translation.unwrap_or(0)); /* Translation */
let len = self.max - self.min + 1;
sink.qword(len); /* Length */
}
}
/// IO resouce object with the IO range, alignment and length
pub struct IO {
min: u16,
max: u16,
alignment: u8,
length: u8,
}
impl IO {
/// Create IO object
pub fn new(min: u16, max: u16, alignment: u8, length: u8) -> Self {
IO {
min,
max,
alignment,
length,
}
}
}
impl Aml for IO {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(IOPORTDESC); /* IO Port Descriptor */
sink.byte(1); /* IODecode16 */
sink.word(self.min);
sink.word(self.max);
sink.byte(self.alignment);
sink.byte(self.length);
}
}
/// Interrupt resouce object with the interrupt characters.
pub struct Interrupt {
consumer: bool,
edge_triggered: bool,
active_low: bool,
shared: bool,
number: u32,
}
impl Interrupt {
/// Create Interrupt object
pub fn new(
consumer: bool,
edge_triggered: bool,
active_low: bool,
shared: bool,
number: u32,
) -> Self {
Interrupt {
consumer,
edge_triggered,
active_low,
shared,
number,
}
}
}
impl Aml for Interrupt {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(EXTIRQDESC); /* Extended IRQ Descriptor */
sink.word(6);
let flags = ((self.shared as u8) << 3)
| ((self.active_low as u8) << 2)
| ((self.edge_triggered as u8) << 1)
| self.consumer as u8;
sink.byte(flags);
sink.byte(1); /* count */
sink.dword(self.number);
}
}
/// IRQ resource object.
pub struct Irq {
edge_triggered: bool,
active_low: bool,
shared: bool,
number: u8,
}
impl Irq {
/// Create IRQ object
pub fn new(edge_triggered: bool, active_low: bool, shared: bool, number: u8) -> Self {
Self {
edge_triggered,
active_low,
shared,
number,
}
}
}
impl Aml for Irq {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(IRQDESC); /* IRQ Descriptor */
write_irq_mask_bytes(self.number, sink);
let flags = ((self.shared as u8) << 4)
| ((self.active_low as u8) << 3)
| (self.edge_triggered as u8);
sink.byte(flags);
}
}
/// IRQNoFlags resource object.
pub struct IrqNoFlags {
number: u8,
}
impl IrqNoFlags {
/// Create IRQNoFlags object
pub fn new(number: u8) -> Self {
Self { number }
}
}
impl Aml for IrqNoFlags {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(IRQNOFLAGSDESC); /* IRQNoFlags Descriptor */
write_irq_mask_bytes(self.number, sink);
}
}
fn write_irq_mask_bytes(number: u8, sink: &mut dyn AmlSink) {
assert!(number <= 15);
if number < 8 {
sink.byte(1 << number);
sink.byte(0);
} else {
sink.byte(0);
sink.byte(1 << (number - 8));
}
}
/// Register resource object
pub struct Register {
reg: gas::GAS,
}
impl Register {
pub fn new(reg: gas::GAS) -> Self {
Self { reg }
}
}
impl Aml for Register {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
sink.byte(REGDESC); /* Register Descriptor */
sink.word(0x12); // length
self.reg.to_aml_bytes(sink);
}
}
/// Device object with its device name and children objects in it.
pub struct Device<'a> {
path: Path,
children: Vec<&'a dyn Aml>,
}
impl Aml for Device<'_> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
let mut bytes = Vec::new();
self.path.to_aml_bytes(&mut bytes);
for child in &self.children {
child.to_aml_bytes(&mut bytes);
}
let pkg_length = create_pkg_length(bytes.len(), true);
sink.byte(EXTOPPREFIX); /* ExtOpPrefix */
sink.byte(DEVICEOP); /* DeviceOp */
sink.vec(&pkg_length);
sink.vec(&bytes);
}
}
impl<'a> Device<'a> {
/// Create Device object
pub fn new(path: Path, children: Vec<&'a dyn Aml>) -> Self {
Device { path, children }
}
}
/// Scope object with its name and children objects in it.
pub struct Scope<'a> {
path: Path,
children: Vec<&'a dyn Aml>,
}
impl Aml for Scope<'_> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
let mut bytes = Vec::new();
self.path.to_aml_bytes(&mut bytes);
for child in &self.children {
child.to_aml_bytes(&mut bytes);
}
let pkg_length = create_pkg_length(bytes.len(), true);
sink.byte(SCOPEOP);
sink.vec(&pkg_length);
sink.vec(&bytes);
}
}
impl<'a> Scope<'a> {
/// Create Scope object
pub fn new(path: Path, children: Vec<&'a dyn Aml>) -> Self {
Scope { path, children }
}
/// Create raw bytes representing a Scope from its children in raw bytes
pub fn raw(path: Path, mut children: Vec<u8>) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.push(SCOPEOP);
path.to_aml_bytes(&mut bytes);
bytes.append(&mut children);
let n = bytes.len(); // n >= 1
let pkg_length = create_pkg_length(n - 1, true);
let m = pkg_length.len();
// move everything after the SCOPEOP over and copy in pkg_length
bytes.resize(n + m, 0xFF);
bytes.as_mut_slice().copy_within(1..n, m + 1);
bytes.as_mut_slice()[1..m + 1].copy_from_slice(pkg_length.as_slice());
bytes
}
}
/// Method object with its name, children objects, arguments and serialized character.
pub struct Method<'a> {
path: Path,
children: Vec<&'a dyn Aml>,
args: u8,
serialized: bool,
}
impl<'a> Method<'a> {
/// Create Method object.
pub fn new(path: Path, args: u8, serialized: bool, children: Vec<&'a dyn Aml>) -> Self {
Method {
path,
children,
args,
serialized,
}
}
}
impl Aml for Method<'_> {
fn to_aml_bytes(&self, sink: &mut dyn AmlSink) {
let mut bytes = Vec::new();
self.path.to_aml_bytes(&mut bytes);
let flags: u8 = (self.args & 0x7) | ((self.serialized as u8) << 3);
bytes.push(flags);
for child in &self.children {
child.to_aml_bytes(&mut bytes);
}
let pkg_length = create_pkg_length(bytes.len(), true);
sink.byte(METHODOP);
sink.vec(&pkg_length);
sink.vec(&bytes);
}
}
/// FieldAccessType defines the field accessing types.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FieldAccessType {
Any,
Byte,
Word,
DWord,
QWord,
Buffer,
}
/// FieldLockRule defines the rules whether to use the Global Lock.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FieldLockRule {
NoLock = 0,
Lock = 1,
}
/// FieldUpdateRule defines the rules to update the field.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FieldUpdateRule {
Preserve = 0,
WriteAsOnes = 1,
WriteAsZeroes = 2,
}
/// FieldEntry defines the field entry.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FieldEntry {
Named([u8; 4], usize),
Reserved(usize),
}
/// Field object with the region name, field entries, access type and update rules.
pub struct Field {
path: Path,
fields: Vec<FieldEntry>,
access_type: FieldAccessType,
lock_rule: FieldLockRule,
update_rule: FieldUpdateRule,