Chameleon

Chameleon Commit Details

Date:2011-07-06 11:13:18 (8 years 4 months ago)
Author:armel cadet-petit
Commit:1126
Parents: 1125
Message:re-up. the good acpipatcher.
Changes:
A/branches/cparm/i386/modules/ACPIPatcher/aml_generator.h
A/branches/cparm/i386/modules/ACPIPatcher/acpi_patcher.h
A/branches/cparm/i386/modules/ACPIPatcher/Makefile
A/branches/cparm/i386/modules/ACPIPatcher
A/branches/cparm/i386/modules/ACPIPatcher/aml_generator.c
A/branches/cparm/i386/modules/ACPIPatcher/acpi_patcher.c
A/branches/cparm/i386/modules/ACPIPatcher/AcpiPatcher.c

File differences

branches/cparm/i386/modules/ACPIPatcher/aml_generator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
* aml_generator.c
* Chameleon
*
* Created by Mozodojo on 20/07/10.
* Copyright 2010 mozo. All rights reserved.
*
*/
#include "aml_generator.h"
bool aml_add_to_parent(struct aml_chunk* parent, struct aml_chunk* node)
{
if (parent && node)
{
switch (parent->Type)
{
case AML_CHUNK_NONE:
case AML_CHUNK_BYTE:
case AML_CHUNK_WORD:
case AML_CHUNK_DWORD:
case AML_CHUNK_QWORD:
case AML_CHUNK_ALIAS:
verbose("aml_add_to_parent: Node isn't supports child nodes!\n");
return false;
case AML_CHUNK_NAME:
if (parent->First)
{
verbose("aml_add_to_parent: Name node could have only one child node!\n");
return false;
}
break;
default:
break;
}
if (!parent->First)
parent->First = node;
if (parent->Last)
parent->Last->Next = node;
parent->Last = node;
return true;
}
return false;
}
struct aml_chunk* aml_create_node(struct aml_chunk* parent)
{
struct aml_chunk* node = (struct aml_chunk*)malloc(sizeof(struct aml_chunk));
aml_add_to_parent(parent, node);
return node;
}
void aml_destroy_node(struct aml_chunk* node)
{
// Delete child nodes
struct aml_chunk* child = node->First;
while (child)
{
struct aml_chunk* next = child->Next;
if (child->Buffer)
free(child->Buffer);
free(child);
child = next;
}
// Free node
if (node->Buffer)
free(node->Buffer);
free(node);
}
struct aml_chunk* aml_add_buffer(struct aml_chunk* parent, const char* buffer, unsigned int size)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_NONE;
node->Length = size;
node->Buffer = malloc(node->Length);
memcpy(node->Buffer, buffer, node->Length);
}
return node;
}
struct aml_chunk* aml_add_byte(struct aml_chunk* parent, unsigned char value)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_BYTE;
node->Length = 1;
node->Buffer = malloc(node->Length);
node->Buffer[0] = value;
}
return node;
}
struct aml_chunk* aml_add_word(struct aml_chunk* parent, unsigned int value)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_WORD;
node->Length = 2;
node->Buffer = malloc(node->Length);
node->Buffer[0] = value & 0xff;
node->Buffer[1] = value >> 8;
}
return node;
}
struct aml_chunk* aml_add_dword(struct aml_chunk* parent, unsigned long value)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_DWORD;
node->Length = 4;
node->Buffer = malloc(node->Length);
node->Buffer[0] = value & 0xff;
node->Buffer[1] = (value >> 8) & 0xff;
node->Buffer[2] = (value >> 16) & 0xff;
node->Buffer[3] = (value >> 24) & 0xff;
}
return node;
}
struct aml_chunk* aml_add_qword(struct aml_chunk* parent, unsigned long long value)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_QWORD;
node->Length = 8;
node->Buffer = malloc(node->Length);
node->Buffer[0] = value & 0xff;
node->Buffer[1] = (value >> 8) & 0xff;
node->Buffer[2] = (value >> 16) & 0xff;
node->Buffer[3] = (value >> 24) & 0xff;
node->Buffer[4] = (value >> 32) & 0xff;
node->Buffer[5] = (value >> 40) & 0xff;
node->Buffer[6] = (value >> 48) & 0xff;
node->Buffer[7] = (value >> 56) & 0xff;
}
return node;
}
unsigned int aml_fill_simple_name(char* buffer, const char* name)
{
if (strlen(name) < 4)
{
verbose("aml_fill_simple_name: simple name %s has incorrect lengh! Must be 4\n", name);
return 0;
}
memcpy(buffer, name, 4);
return 4;
}
unsigned int aml_fill_name(struct aml_chunk* node, const char* name)
{
if (!node)
return 0;
int len = strlen(name), offset = 0, count = len / 4;
if ((len % 4) > 1 || count == 0)
{
verbose("aml_fill_name: pathname %s has incorrect length! Must be 4, 8, 12, 16 etc.\n", name);
return 0;
}
unsigned int root = 0;
if ((len % 4) == 1 && name[0] == '\\')
root++;
if (count == 1)
{
node->Length = 4 + root;
node->Buffer = malloc(node->Length);
memcpy(node->Buffer, name, 4 + root);
return node->Length;
}
if (count == 2)
{
node->Length = 2 + 8;
node->Buffer = malloc(node->Length);
node->Buffer[offset++] = 0x5c; // Root Char
node->Buffer[offset++] = 0x2e; // Double name
memcpy(node->Buffer+offset, name + root, 8);
return node->Length;
}
node->Length = 3 + count*4;
node->Buffer = malloc(node->Length);
node->Buffer[offset++] = 0x5c; // Root Char
node->Buffer[offset++] = 0x2f; // Multi name
node->Buffer[offset++] = count; // Names count
memcpy(node->Buffer+offset, name + root, count*4);
return node->Length;
}
struct aml_chunk* aml_add_scope(struct aml_chunk* parent, const char* name)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_SCOPE;
aml_fill_name(node, name);
}
return node;
}
struct aml_chunk* aml_add_name(struct aml_chunk* parent, const char* name)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_NAME;
aml_fill_name(node, name);
}
return node;
}
struct aml_chunk* aml_add_package(struct aml_chunk* parent)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_PACKAGE;
node->Length = 1;
node->Buffer = malloc(node->Length);
}
return node;
}
struct aml_chunk* aml_add_package_with_value(struct aml_chunk* parent, uint64_t *lst, int num)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_PACKAGE;
node->Length = 1;
node->Buffer = malloc(node->Length);
aml_add_multiple_value(node,lst,num);
}
return node;
}
void aml_add_value(struct aml_chunk* parent, uint64_t val)
{
if (parent)
{
if (val > 0xffffffff)
aml_add_qword(parent,val);
else if (val > 0xffff)
aml_add_dword(parent,val);
else if (val > 0xff)
aml_add_word(parent,val);
else
aml_add_byte(parent, val);
}
}
void aml_add_multiple_value(struct aml_chunk* parent, uint64_t *lst, int num)
{
if (parent)
{
int i;
for (i=0; i<(num / 8); i++) {
aml_add_value(parent, lst[i]);
}
}
}
struct aml_chunk* aml_add_alias(struct aml_chunk* parent, const char* name1, const char* name2)
{
struct aml_chunk* node = aml_create_node(parent);
if (node)
{
node->Type = AML_CHUNK_ALIAS;
node->Length = 8;
node->Buffer = malloc(node->Length);
aml_fill_simple_name(node->Buffer, name1);
aml_fill_simple_name(node->Buffer+4, name2);
}
return node;
}
unsigned char aml_get_size_length(unsigned int size)
{
if (size + 1 <= 0x3f)
return 1;
else if (size + 2 <= 0x3fff)
return 2;
else if (size + 3 <= 0x3fffff)
return 3;
return 4;
}
unsigned int aml_calculate_size(struct aml_chunk* node)
{
if (node)
{
node->Size = 0;
// Calculate child nodes size
struct aml_chunk* child = node->First;
unsigned char child_count = 0;
while (child)
{
child_count++;
node->Size += aml_calculate_size(child);
child = child->Next;
}
switch (node->Type)
{
case AML_CHUNK_NONE:
node->Size += node->Length;
break;
case AML_CHUNK_SCOPE:
node->Size += 1 + node->Length;
node->Size += aml_get_size_length(node->Size);
break;
case AML_CHUNK_PACKAGE:
node->Buffer[0] = child_count;
node->Size += 1 + node->Length;
node->Size += aml_get_size_length(node->Size);
break;
case AML_CHUNK_BYTE:
if (node->Buffer[0] == 0x0 || node->Buffer[0] == 0x1)
{
node->Size += node->Length;
}
else
{
node->Size += 1 + node->Length;
}
break;
case AML_CHUNK_WORD:
case AML_CHUNK_DWORD:
case AML_CHUNK_QWORD:
case AML_CHUNK_ALIAS:
case AML_CHUNK_NAME:
node->Size += 1 + node->Length;
break;
}
return node->Size;
}
return 0;
}
unsigned int aml_write_byte(unsigned char value, char* buffer, unsigned int offset)
{
buffer[offset++] = value;
return offset;
}
unsigned int aml_write_word(unsigned int value, char* buffer, unsigned int offset)
{
buffer[offset++] = value & 0xff;
buffer[offset++] = value >> 8;
return offset;
}
unsigned int aml_write_dword(unsigned long value, char* buffer, unsigned int offset)
{
buffer[offset++] = value & 0xff;
buffer[offset++] = (value >> 8) & 0xff;
buffer[offset++] = (value >> 16) & 0xff;
buffer[offset++] = (value >> 24) & 0xff;
return offset;
}
unsigned int aml_write_qword(unsigned long long value, char* buffer, unsigned int offset)
{
buffer[offset++] = value & 0xff;
buffer[offset++] = (value >> 8) & 0xff;
buffer[offset++] = (value >> 16) & 0xff;
buffer[offset++] = (value >> 24) & 0xff;
buffer[offset++] = (value >> 32) & 0xff;
buffer[offset++] = (value >> 40) & 0xff;
buffer[offset++] = (value >> 48) & 0xff;
buffer[offset++] = (value >> 56) & 0xff;
return offset;
}
unsigned int aml_write_buffer(const char* value, unsigned int size, char* buffer, unsigned int offset)
{
if (size > 0)
{
memcpy(buffer + offset, value, size);
}
return offset + size;
}
void aml_add_ressource_t_fxhw(struct aml_chunk* parent, uint64_t *lst)
{
if (parent) {
char resource_template[] =
{
0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F,
0x01, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x79, 0x00
};
char *buffer = resource_template;
unsigned int offset = aml_write_byte(lst[0], buffer, 8);
offset = aml_write_byte(lst[1], buffer, offset);
offset = aml_write_byte(lst[2], buffer, offset);
aml_write_qword(lst[3], buffer, offset);
aml_add_buffer(parent, buffer, sizeof(resource_template));
}
}
unsigned int aml_write_size(unsigned int size, char* buffer, unsigned int offset)
{
if (size <= 0x3f)
{
buffer[offset++] = size;
}
else if (size <= 0x3fff)
{
buffer[offset++] = 0x40 | (size & 0xf);
buffer[offset++] = (size >> 4) & 0xff;
}
else if (size <= 0x3fffff)
{
buffer[offset++] = 0x80 | (size & 0xf);
buffer[offset++] = (size >> 4) & 0xff;
buffer[offset++] = (size >> 12) & 0xff;
}
else
{
buffer[offset++] = 0xc0 | (size & 0xf);
buffer[offset++] = (size >> 4) & 0xff;
buffer[offset++] = (size >> 12) & 0xff;
buffer[offset++] = (size >> 20) & 0xff;
}
return offset;
}
unsigned int aml_write_node(struct aml_chunk* node, char* buffer, unsigned int offset)
{
if (node && buffer)
{
unsigned int old = offset;
switch (node->Type)
{
case AML_CHUNK_NONE:
offset = aml_write_buffer(node->Buffer, node->Length, buffer, offset);
break;
case AML_CHUNK_SCOPE:
case AML_CHUNK_PACKAGE:
offset = aml_write_byte(node->Type, buffer, offset);
offset = aml_write_size(node->Size-1, buffer, offset);
offset = aml_write_buffer(node->Buffer, node->Length, buffer, offset);
break;
case AML_CHUNK_BYTE:
if (node->Buffer[0] == 0x0 || node->Buffer[0] == 0x1)
{
offset = aml_write_buffer(node->Buffer, node->Length, buffer, offset);
}
else
{
offset = aml_write_byte(node->Type, buffer, offset);
offset = aml_write_buffer(node->Buffer, node->Length, buffer, offset);
}
break;
case AML_CHUNK_WORD:
case AML_CHUNK_DWORD:
case AML_CHUNK_QWORD:
case AML_CHUNK_ALIAS:
case AML_CHUNK_NAME:
offset = aml_write_byte(node->Type, buffer, offset);
offset = aml_write_buffer(node->Buffer, node->Length, buffer, offset);
break;
default:
break;
}
struct aml_chunk* child = node->First;
while (child)
{
offset = aml_write_node(child, buffer, offset);
child = child->Next;
}
if (offset - old != node->Size)
verbose("Node size incorrect: 0x%x\n", node->Type);
}
return offset;
}
branches/cparm/i386/modules/ACPIPatcher/acpi_patcher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
/*
* Copyright 2008 mackerintel
*/
/*
* Copyright (c) 2010 cparm <armelcadetpetit@gmail.com>. All rights reserved.
*
*/
#include "libsaio.h"
#include "boot.h"
#include "bootstruct.h"
#include "acpi.h"
#include "efi_tables.h"
#include "fake_efi.h"
#include "acpi_patcher.h"
#include "platform.h"
#include "cpu.h"
#include "aml_generator.h"
#include "xml.h"
#include "smp.h"
uint64_t acpi10_p;
uint64_t acpi20_p;
#ifndef DEBUG_ACPI
#define DEBUG_ACPI 0
#endif
#if DEBUG_ACPI==2
#define DBG(x...) {printf(x); sleep(1);}
#elif DEBUG_ACPI==1
#define DBG(x...) printf(x)
#else
#define DBG(x...)
#endif
extern EFI_STATUS addConfigurationTable();
extern EFI_GUID gEfiAcpiTableGuid;
extern EFI_GUID gEfiAcpi20TableGuid;
#define tableSign(table, sgn) (table[0]==sgn[0] && table[1]==sgn[1] && table[2]==sgn[2] && table[3]==sgn[3])
#define CMOS_WRITE_BYTE(x, y)cmos_write_byte(x, y)
#define CMOS_READ_BYTE(x)cmos_read_byte(x)
#define RSDP_CHECKSUM_LENGTH 20
/*-
*FOR biosacpi_search_rsdp AND biosacpi_find_rsdp
*
* Copyright (c) 2001 Michael Smith <msmith@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*
* $FreeBSD: src/sys/boot/i386/libi386/biosacpi.c,v 1.7 2003/08/25 23:28:31 obrien Exp $
* $DragonFly: src/sys/boot/pc32/libi386/biosacpi.c,v 1.5 2007/01/17 17:31:19 y0netan1 Exp $
*/
static struct acpi_2_rsdp *
biosacpi_search_rsdp(char *base, int length, int rev)
{
struct acpi_2_rsdp *rsdp;
intofs;
/* search on 16-byte boundaries */
for (ofs = 0; ofs < length; ofs += 16) {
rsdp = (struct acpi_2_rsdp*)ptov(base + ofs);
/* compare signature, validate checksum */
if (!strncmp(rsdp->Signature, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP))) {
uint8_t csum = checksum8(rsdp, RSDP_CHECKSUM_LENGTH);
if(csum == 0)
{
/* Only assume this is a 2.0 or better table if the revision is greater than 0
* NOTE: ACPI 3.0 spec only seems to say that 1.0 tables have revision 1
* and that the current revision is 2.. I am going to assume that rev > 0 is 2.0.
*/
if((rsdp->Revision > 0) && rev > 0)
{
uint8_t csum2 = checksum8(rsdp, sizeof(struct acpi_2_rsdp));
if(csum2 == 0)
return(rsdp);
}
// Only return the table if it is a true version 1.0 table (Revision 0)
if((rsdp->Revision == 0) && rev == 0)
return(rsdp);
}
}
}
return(NULL);
}
/*
* Find the RSDP in low memory. See section 5.2.2 of the ACPI spec.
*/
static struct acpi_2_rsdp *
biosacpi_find_rsdp(int rev)
{
struct acpi_2_rsdp *rsdp;
uint16_t*addr;
/* EBDA is the 1 KB addressed by the 16 bit pointer at 0x40E. */
addr = (uint16_t *)ptov(EBDA_SEG_ADDR);
if ((rsdp = biosacpi_search_rsdp((char *)(*addr << 4), EBDA_SEG_LEN, rev)) != NULL)
return (rsdp);
unsigned mem_lower = ((CMOS_READ_BYTE(CMOS_BASE_MEMORY+1) << 8)
| CMOS_READ_BYTE(CMOS_BASE_MEMORY)) << 10;
if ((rsdp = biosacpi_search_rsdp((char *)mem_lower, EBDA_SEG_LEN, rev)) != NULL)
return (rsdp);
/* Check the upper memory BIOS space, 0xe0000 - 0xfffff. */
if ((rsdp = biosacpi_search_rsdp((char *)0xe0000, 0x20000, rev)) != NULL)
return (rsdp);
return (NULL);
}
#define __RES(s, u)\
inline unsigned u\
resolve_##s(unsigned u defaultentry, char *str, int base) \
{\
unsigned u entry = defaultentry;\
if (str && (strcmp(str,"Default") != 0)) {\
entry = strtoul((const char *)str, NULL,base);\
}\
return entry;\
}
__RES(pss, long)
__RES(cst, int)
static void setchecksum(struct acpi_common_header *header)
{
header->Checksum = 0;
header->Checksum = 256-checksum8(header, header->Length);
}
static void setRsdpchecksum(struct acpi_2_rsdp *rsdp)
{
rsdp->Checksum = 0;
rsdp->Checksum = 256-checksum8(rsdp, 20);
}
static void setRsdpXchecksum(struct acpi_2_rsdp *rsdp)
{
rsdp->ExtendedChecksum = 0;
rsdp->ExtendedChecksum = 256-checksum8(rsdp, rsdp->Length);
}
static struct acpi_2_rsdp * gen_rsdp_v2_from_v1(struct acpi_2_rsdp *rsdp)
{
struct acpi_2_rsdp * rsdp_conv = (struct acpi_2_rsdp *)AllocateKernelMemory(sizeof(struct acpi_2_rsdp));
memcpy(rsdp_conv, rsdp, 20);
/* Add/change fields */
rsdp_conv->Revision = 2; /* ACPI version 3 */
rsdp_conv->Length = sizeof(struct acpi_2_rsdp);
/* Correct checksums */
setRsdpchecksum(rsdp_conv);
setRsdpXchecksum(rsdp_conv);
rsdp = rsdp_conv;
return rsdp_conv;
}
static struct acpi_2_xsdt * gen_xsdt_from_rsdt(struct acpi_2_rsdt *rsdt)
{
int i,rsdt_entries_num=(rsdt->Length-sizeof(struct acpi_2_rsdt))/4;
uint32_t *rsdt_entries=(uint32_t *)(rsdt+1);
struct acpi_2_xsdt * xsdt_conv=(struct acpi_2_xsdt *)AllocateKernelMemory(sizeof(struct acpi_2_xsdt)+(rsdt_entries_num * 8));
memcpy(xsdt_conv, rsdt, sizeof(struct acpi_2_rsdt));
xsdt_conv->Signature[0] = 'X';
xsdt_conv->Signature[1] = 'S';
xsdt_conv->Signature[2] = 'D';
xsdt_conv->Signature[3] = 'T';
xsdt_conv->Length = sizeof(struct acpi_2_xsdt)+(rsdt_entries_num * 8);
uint64_t *xsdt_conv_entries=(uint64_t *)(xsdt_conv+1);
for (i=0;i<rsdt_entries_num;i++)
{
xsdt_conv_entries[i] = (uint64_t)rsdt_entries[i];
}
setchecksum((struct acpi_common_header *)xsdt_conv);
return xsdt_conv;
}
static void update_rsdp_with_xsdt(struct acpi_2_rsdp *rsdp, struct acpi_2_xsdt *xsdt)
{
rsdp->XsdtAddress = (uint32_t)xsdt;
setRsdpXchecksum(rsdp);
}
static void update_rsdp_with_rsdt(struct acpi_2_rsdp *rsdp, struct acpi_2_rsdt *rsdt)
{
rsdp->RsdtAddress = (uint32_t)rsdt;
setRsdpchecksum(rsdp);
}
void *loadSSDTTable(int ssdt_number)
{
int fd = -1;
char dirspec[512];
char filename[512];
const char * overriden_pathname=NULL;
int len=0;
// Check booting partition
// Rek: if user specified a full path name then take it in consideration
if (getValueForKey(kSSDT, &overriden_pathname, &len,
&bootInfo->bootConfig))
{
sprintf(filename, "%s-%d.aml", overriden_pathname, ssdt_number); // start searching root
}
else
sprintf(filename, "SSDT-%d.aml", ssdt_number);
sprintf(dirspec, "/%s", filename); // start searching root
fd=open (dirspec);
if (fd<0)
{// Check Extra on booting partition
sprintf(dirspec,"/Extra/%s",filename);
fd=open (dirspec);
if (fd<0)
{// Fall back to booter partition
sprintf(dirspec,"bt(0,0)/Extra/%s",filename);
fd=open (dirspec);
if (fd<0)
{
DBG("SSDT Table not found: %s\n", filename);
return NULL;
}
}
}
void *tableAddr=(void*)AllocateKernelMemory(file_size (fd));
if (tableAddr)
{
if (read (fd, tableAddr, file_size (fd))!=file_size (fd))
{
printf("Couldn't read table %s\n",dirspec);
free (tableAddr);
close (fd);
return NULL;
}
printf("Valid SSDT Table found: %s\n", filename);
DBG("Table %s read and stored at: %x\n", dirspec, tableAddr);
close (fd);
return tableAddr;
}
printf("Couldn't allocate memory for table %s\n", dirspec);
close (fd);
return NULL;
}
void *loadACPITable(char *key)
{
int fd = -1;
char dirspec[512];
char filename[512];
const char * overriden_pathname=NULL;
int len=0;
DBG("Searching for %s.aml file ...\n", key);
// Check booting partition
// Rek: if user specified a full path name then take it in consideration
if (getValueForKey(key, &overriden_pathname, &len,
&bootInfo->bootConfig))
{
sprintf(filename, "%s", overriden_pathname);
}
else
sprintf(filename, "%s.aml", key);
sprintf(dirspec, "/%s", filename); // start searching root
fd=open (dirspec);
if (fd<0)
{
// Check Extra on booting partition
sprintf(dirspec,"/Extra/%s",filename);
fd=open (dirspec);
if (fd<0)
{// Fall back to booter partition
sprintf(dirspec,"bt(0,0)/Extra/%s",filename);
fd=open (dirspec);
if (fd<0)
{
DBG("ACPI Table not found: %s\n", key);
return NULL;
}
}
}
void *tableAddr=(void*)AllocateKernelMemory(file_size (fd));
if (tableAddr)
{
if (read (fd, tableAddr, file_size (fd))!=file_size (fd))
{
printf("Couldn't read table %s\n",key);
free (tableAddr);
close (fd);
return NULL;
}
verbose("Valid ACPI Table found: %s\n", key);
//DBG("Table %s read and stored at: %x\n", key, tableAddr);
close (fd);
return tableAddr;
}
printf("Couldn't allocate memory for table %s\n", key);
close (fd);
return NULL;
}
uint8_tacpi_cpu_count = 0;
char* acpi_cpu_name[32];
void get_acpi_cpu_names(unsigned char* dsdt, uint32_t length)
{
uint32_t i;
for (i=0; i<length-7; i++)
{
if (dsdt[i] == 0x5B && dsdt[i+1] == 0x83) // ProcessorOP
{
uint32_t offset = i + 3 + (dsdt[i+2] >> 6);
bool add_name = true;
uint8_t j;
for (j=0; j<4; j++)
{
char c = dsdt[offset+j];
if (!aml_isvalidchar(c))
{
add_name = false;
verbose("Invalid character found in ProcessorOP 0x%x!\n", c);
break;
}
}
if (add_name)
{
acpi_cpu_name[acpi_cpu_count] = malloc(4);
memcpy(acpi_cpu_name[acpi_cpu_count], dsdt+offset, 4);
i = offset + 5;
verbose("Found ACPI CPU: %c%c%c%c\n", acpi_cpu_name[acpi_cpu_count][0], acpi_cpu_name[acpi_cpu_count][1], acpi_cpu_name[acpi_cpu_count][2], acpi_cpu_name[acpi_cpu_count][3]);
if (++acpi_cpu_count == 32) return;
}
}
}
}
struct acpi_2_ssdt *generate_cst_ssdt(struct acpi_2_fadt* fadt)
{
char ssdt_header[] =
{
0x53, 0x53, 0x44, 0x54, 0xE7, 0x00, 0x00, 0x00, /* SSDT.... */
0x01, 0x17, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x41, /* ..PmRefA */
0x43, 0x70, 0x75, 0x43, 0x73, 0x74, 0x00, 0x00, /* CpuCst.. */
0x00, 0x10, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* ....INTL */
0x31, 0x03, 0x10, 0x20 /* 1.._*/
};
char cstate_resource_template[] =
{
0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F,
0x01, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x79, 0x00
};
if (Platform->CPU.Vendor != 0x756E6547) {
verbose ("Not an Intel platform: C-States will not be generated !!!\n");
return NULL;
}
if (fadt == NULL) {
verbose ("FACP not exists: C-States will not be generated !!!\n");
return NULL;
}
struct acpi_2_dsdt* dsdt = (void*)fadt->DSDT;
if (dsdt == NULL) {
verbose ("DSDT not found: C-States will not be generated !!!\n");
return NULL;
}
if (acpi_cpu_count == 0)
get_acpi_cpu_names((void*)dsdt, dsdt->Length);
if (acpi_cpu_count > 0)
{
bool c2_enabled = fadt->C2_Latency < 100;
bool c3_enabled = fadt->C3_Latency < 1000;
bool c4_enabled = false;
getBoolForKey(kEnableC4State, &c4_enabled, &bootInfo->bootConfig);
unsigned char cstates_count = 1 + (c2_enabled ? 1 : 0) + ((c3_enabled || c4_enabled) ? 1 : 0);
char *Lat = NULL, *Pw = NULL, *tmpstr =NULL;
int base = 16;
TagPtr personality = XMLCastDict(XMLGetProperty(bootInfo->bootConfig.dictionary, (const char*)"C-States"));
if ((tmpstr = XMLCastString(XMLGetProperty(personality, (const char*)"Base")))) {
int mybase = strtol(tmpstr, NULL, 10);
if (mybase == 8 || mybase == 10 || mybase == 16 )
base = mybase;
}
struct aml_chunk* root = aml_create_node(NULL);
aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
struct aml_chunk* name = aml_add_name(scop, "CST_");
struct aml_chunk* pack = aml_add_package(name);
aml_add_byte(pack, cstates_count);
struct aml_chunk* tmpl = aml_add_package(pack);
TagPtr match_Status = XMLGetProperty(personality, (const char*)"C1");
if (match_Status) {
Pw = XMLCastString(XMLGetProperty(match_Status, (const char*)"Power"));
Lat = XMLCastString(XMLGetProperty(match_Status, (const char*)"Latency"));
}
cstate_resource_template[11] = 0x00; // C1
aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
aml_add_byte(tmpl, 0x01); // C1
aml_add_byte(tmpl, (unsigned char)resolve_cst(0x01, Lat, base)); // Latency
aml_add_word(tmpl, resolve_cst(0x03e8, Pw, base)); // Power
// C2
if (c2_enabled)
{
tmpl = aml_add_package(pack);
Lat = NULL;
Pw = NULL;
match_Status = XMLGetProperty(personality, (const char*)"C2");
if (match_Status) {
Pw = XMLCastString(XMLGetProperty(match_Status, (const char*)"Power"));
Lat = XMLCastString(XMLGetProperty(match_Status, (const char*)"Latency"));
}
cstate_resource_template[11] = 0x10; // C2
aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
aml_add_byte(tmpl, 0x02); // C2
aml_add_word(tmpl, resolve_cst(fadt->C2_Latency, Lat, base)); // Latency
aml_add_word(tmpl, resolve_cst(0x01f4, Pw, base)); // Power
}
// C4
if (c4_enabled)
{
tmpl = aml_add_package(pack);
Lat = NULL;
Pw = NULL;
match_Status = XMLGetProperty(personality, (const char*)"C4");
if (match_Status) {
Pw = XMLCastString(XMLGetProperty(match_Status, (const char*)"Power"));
Lat = XMLCastString(XMLGetProperty(match_Status, (const char*)"Latency"));
}
cstate_resource_template[11] = 0x30; // C4
aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
aml_add_byte(tmpl, 0x04); // C4
aml_add_word(tmpl, resolve_cst(fadt->C3_Latency / 2, Lat, base)); // TODO: right latency for C4
aml_add_word(tmpl, resolve_cst(0xfa, Pw, base)); // Power
}
// C3
else if (c3_enabled)
{
tmpl = aml_add_package(pack);
Lat = NULL;
Pw = NULL;
match_Status = XMLGetProperty(personality, (const char*)"C3");
if (match_Status) {
Pw = XMLCastString(XMLGetProperty(match_Status, (const char*)"Power"));
Lat = XMLCastString(XMLGetProperty(match_Status, (const char*)"Latency"));
}
cstate_resource_template[11] = 0x20; // C3
aml_add_buffer(tmpl, cstate_resource_template, sizeof(cstate_resource_template));
aml_add_byte(tmpl, 0x03); // C3
aml_add_word(tmpl, resolve_cst(fadt->C3_Latency , Lat, base));
aml_add_word(tmpl, resolve_cst(0x015e, Pw, base)); // Power
}
// Aliaces
int i;
for (i = 0; i < acpi_cpu_count; i++)
{
char name[9];
sprintf(name, "_PR_%c%c%c%c", acpi_cpu_name[i][0], acpi_cpu_name[i][1], acpi_cpu_name[i][2], acpi_cpu_name[i][3]);
scop = aml_add_scope(root, name);
aml_add_alias(scop, "CST_", "_CST");
}
aml_calculate_size(root);
struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
aml_write_node(root, (void*)ssdt, 0);
ssdt->Length = root->Size;
setchecksum((struct acpi_common_header *)ssdt);
aml_destroy_node(root);
verbose ("SSDT with CPU C-States generated successfully\n");
return ssdt;
}
else
{
verbose ("ACPI CPUs not found: C-States not generated !!!\n");
}
return NULL;
}
struct acpi_2_ssdt *generate_pss_ssdt(struct acpi_2_dsdt* dsdt)
{
char ssdt_header[] =
{
0x53, 0x53, 0x44, 0x54, 0x7E, 0x00, 0x00, 0x00, /* SSDT.... */
0x01, 0x6A, 0x50, 0x6D, 0x52, 0x65, 0x66, 0x00, /* ..PmRef. */
0x43, 0x70, 0x75, 0x50, 0x6D, 0x00, 0x00, 0x00, /* CpuPm... */
0x00, 0x30, 0x00, 0x00, 0x49, 0x4E, 0x54, 0x4C, /* .0..INTL */
0x31, 0x03, 0x10, 0x20,/* 1.._*/
};
if (Platform->CPU.Vendor != 0x756E6547) {
verbose ("Not an Intel platform: P-States will not be generated !!!\n");
return NULL;
}
if (!(Platform->CPU.Features & CPUID_FEATURE_MSR)) {
verbose ("Unsupported CPU: P-States will not be generated !!!\n");
return NULL;
}
if (acpi_cpu_count == 0)
get_acpi_cpu_names((void*)dsdt, dsdt->Length);
if (acpi_cpu_count > 0)
{
struct p_state initial, maximum, minimum, p_states[32];
uint8_t p_states_count = 0;
// Retrieving P-States, ported from code by superhai (c)
switch (Platform->CPU.Family) {
case 0x06:
{
switch (Platform->CPU.Model)
{
case CPUID_MODEL_DOTHAN: // ?
case CPUID_MODEL_YONAH: // Yonah
case CPUID_MODEL_MEROM: // Merom
case CPUID_MODEL_PENRYN: // Penryn
case CPUID_MODEL_ATOM: // Intel Atom (45nm)
{
bool cpu_dynamic_fsb = false;
if (rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 27))
{
wrmsr64(MSR_IA32_EXT_CONFIG, (rdmsr64(MSR_IA32_EXT_CONFIG) | (1 << 28)));
delay(1);
cpu_dynamic_fsb = rdmsr64(MSR_IA32_EXT_CONFIG) & (1 << 28);
}
bool cpu_noninteger_bus_ratio = (rdmsr64(MSR_IA32_PERF_STATUS) & (1ULL << 46));
initial.Control = rdmsr64(MSR_IA32_PERF_STATUS);
maximum.Control = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 32) & 0x1F3F) | (0x4000 * cpu_noninteger_bus_ratio);
maximum.CID = ((maximum.FID & 0x1F) << 1) | cpu_noninteger_bus_ratio;
minimum.FID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 24) & 0x1F) | (0x80 * cpu_dynamic_fsb);
minimum.VID = ((rdmsr64(MSR_IA32_PERF_STATUS) >> 48) & 0x3F);
if (minimum.FID == 0)
{
uint64_t msr;
uint8_t i;
// Probe for lowest fid
for (i = maximum.FID; i >= 0x6; i--)
{
msr = rdmsr64(MSR_IA32_PERF_CONTROL);
wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (i << 8) | minimum.VID);
intel_waitforsts();
minimum.FID = (rdmsr64(MSR_IA32_PERF_STATUS) >> 8) & 0x1F;
delay(1);
}
msr = rdmsr64(MSR_IA32_PERF_CONTROL);
wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
intel_waitforsts();
}
if (minimum.VID == maximum.VID)
{
uint64_t msr;
uint8_t i;
// Probe for lowest vid
for (i = maximum.VID; i > 0xA; i--)
{
msr = rdmsr64(MSR_IA32_PERF_CONTROL);
wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (minimum.FID << 8) | i);
intel_waitforsts();
minimum.VID = rdmsr64(MSR_IA32_PERF_STATUS) & 0x3F;
delay(1);
}
msr = rdmsr64(MSR_IA32_PERF_CONTROL);
wrmsr64(MSR_IA32_PERF_CONTROL, (msr & 0xFFFFFFFFFFFF0000ULL) | (maximum.FID << 8) | maximum.VID);
intel_waitforsts();
}
minimum.CID = ((minimum.FID & 0x1F) << 1) >> cpu_dynamic_fsb;
// Sanity check
if (maximum.CID < minimum.CID)
{
DBG("Insane FID values!");
p_states_count = 0;
}
else
{
// Finalize P-States
// Find how many P-States machine supports
p_states_count = maximum.CID - minimum.CID + 1;
if (p_states_count > 32)
p_states_count = 32;
uint8_t vidstep;
uint8_t i = 0, u, invalid = 0;
vidstep = ((maximum.VID << 2) - (minimum.VID << 2)) / (p_states_count - 1);
for (u = 0; u < p_states_count; u++)
{
i = u - invalid;
p_states[i].CID = maximum.CID - u;
p_states[i].FID = (p_states[i].CID >> 1);
if (p_states[i].FID < 0x6)
{
if (cpu_dynamic_fsb)
p_states[i].FID = (p_states[i].FID << 1) | 0x80;
}
else if (cpu_noninteger_bus_ratio)
{
p_states[i].FID = p_states[i].FID | (0x40 * (p_states[i].CID & 0x1));
}
if (i && p_states[i].FID == p_states[i-1].FID)
invalid++;
p_states[i].VID = ((maximum.VID << 2) - (vidstep * u)) >> 2;
uint32_t multiplier = p_states[i].FID & 0x1f;// = 0x08
bool half = p_states[i].FID & 0x40;// = 0x01
bool dfsb = p_states[i].FID & 0x80;// = 0x00
uint32_t fsb = Platform->CPU.FSBFrequency / 1000000; // = 400
uint32_t halffsb = (fsb + 1) >> 1;// = 200
uint32_t frequency = (multiplier * fsb);// = 3200
p_states[i].Frequency = (frequency + (half * halffsb)) >> dfsb;// = 3200 + 200 = 3400
}
p_states_count -= invalid;
}
break;
}
case CPUID_MODEL_FIELDS:
case CPUID_MODEL_DALES:
case CPUID_MODEL_DALES_32NM:
case CPUID_MODEL_NEHALEM:
case CPUID_MODEL_NEHALEM_EX:
case CPUID_MODEL_WESTMERE:
case CPUID_MODEL_WESTMERE_EX:
case CPUID_MODEL_SANDYBRIDGE:
case CPUID_MODEL_JAKETOWN:
{
maximum.Control = rdmsr64(MSR_IA32_PERF_STATUS) & 0xff; // Seems it always contains maximum multiplier value (with turbo, that's we need)...
minimum.Control = (rdmsr64(MSR_PLATFORM_INFO) >> 40) & 0xff;
verbose("P-States: min 0x%x, max 0x%x\n", minimum.Control, maximum.Control);
// Sanity check
if (maximum.Control < minimum.Control)
{
DBG("Insane control values!");
p_states_count = 0;
}
else
{
uint8_t i;
p_states_count = 0;
for (i = maximum.Control; i >= minimum.Control; i--)
{
p_states[p_states_count].Control = i;
p_states[p_states_count].CID = p_states[p_states_count].Control << 1;
p_states[p_states_count].Frequency = (Platform->CPU.FSBFrequency / 1000000) * i;
p_states_count++;
}
}
break;
}
default:
verbose ("Unsupported CPU: P-States not generated !!!\n");
break;
}
}
default:
break;
}
// Generating SSDT
if (p_states_count)
{
int i;
struct aml_chunk* root = aml_create_node(NULL);
aml_add_buffer(root, ssdt_header, sizeof(ssdt_header)); // SSDT header
struct aml_chunk* scop = aml_add_scope(root, "\\_PR_");
struct aml_chunk* name = aml_add_name(scop, "PSS_");
struct aml_chunk* pack = aml_add_package(name);
uint8_t minPSratio = (p_states[p_states_count-1].Frequency / (Platform->CPU.FSBFrequency / 10000000 ));
uint8_t maxPSratio = (p_states[0].Frequency / (Platform->CPU.FSBFrequency / 10000000 ));
uint8_t cpu_div = Platform->CPU.CurrDiv;
uint8_t cpu_ratio = 0;
if (cpu_div)
cpu_ratio = (Platform->CPU.CurrCoef * 10) + 5;
else
cpu_ratio = Platform->CPU.CurrCoef * 10;
int user_max_ratio = 0;
getIntForKey(kMaxRatio, &user_max_ratio, &bootInfo->bootConfig);
if (user_max_ratio >= minPSratio && maxPSratio >= user_max_ratio) {
uint8_t maxcurrdiv = 0, maxcurrcoef = (int)(user_max_ratio / 10);
uint8_t maxdiv = user_max_ratio - (maxcurrcoef * 10);
if (maxdiv > 0)
maxcurrdiv = 1;
if (maxcurrdiv)
cpu_ratio = (maxcurrcoef * 10) + 5;
else
cpu_ratio = maxcurrcoef * 10;
}
int user_min_ratio = 0;
getIntForKey(kMinRatio, &user_min_ratio, &bootInfo->bootConfig);
if (user_min_ratio >= minPSratio && cpu_ratio >= user_min_ratio) {
uint8_t mincurrdiv = 0, mincurrcoef = (int)(user_min_ratio / 10);
uint8_t mindiv = user_min_ratio - (mincurrcoef * 10);
if (mindiv > 0)
mincurrdiv = 1;
if (mincurrdiv)
minPSratio = (mincurrcoef * 10) + 5;
else
minPSratio = mincurrcoef * 10;
}
if (maxPSratio >= cpu_ratio && cpu_ratio >= minPSratio)maxPSratio = cpu_ratio;
TagPtr personality = XMLCastDict(XMLGetProperty(bootInfo->bootConfig.dictionary, (const char*)"P-States"));
char* MatchStat = 0;
int dropPSS = 0, Pstatus = 0, base = 16;
int expert = 0;/* Default: 0 , mean mixed mode | expert mode : 1 , mean add only p-states found in boot.plist*/
char *tmpstr = XMLCastString(XMLGetProperty(personality, (const char*)"Mode"));
if (strcmp(tmpstr,"Expert") == 0) {
p_states_count = (XMLTagCount(personality)) - 1 ; // - 1 = - ("Mode" tag)
expert = 1;
}
if ((tmpstr = XMLCastString(XMLGetProperty(personality, (const char*)"Base")))) {
if (expert) p_states_count--; // -= ("Base" tag)
int mybase = strtol(tmpstr, NULL, 10);
if (mybase == 8 || mybase == 10 || mybase == 16 )
base = mybase;
}
for (i = 0; i < p_states_count; i++)
{
sprintf(MatchStat, "%d",i);
TagPtr match_Status = XMLGetProperty(personality, (const char*)MatchStat);
char *Lat1 = NULL, *clk = NULL, *Pw = NULL, *Lat2 = NULL, *Ctrl = NULL ;
if (match_Status) {
clk = XMLCastString(XMLGetProperty(match_Status, (const char*)"CoreFreq"));
Pw = XMLCastString(XMLGetProperty(match_Status, (const char*)"Power"));
Lat1 = XMLCastString(XMLGetProperty(match_Status, (const char*)"Transition Latency"));
Lat2 = XMLCastString(XMLGetProperty(match_Status, (const char*)"Bus Master Latency"));
Ctrl = XMLCastString(XMLGetProperty(match_Status, (const char*)"Control"));
} else if (expert)
continue;
unsigned long Frequency = 0x00000000;
if (!expert) Frequency = p_states[i].Frequency;
if (clk)
Frequency = strtoul((const char *)clk, NULL,base);
if (!Frequency || Frequency > p_states[0].Frequency ) continue;
uint8_t curr_ratio = (Frequency / (Platform->CPU.FSBFrequency / 10000000 ));
if (curr_ratio > maxPSratio || minPSratio > curr_ratio)
goto dropPstate;
struct aml_chunk* pstt = aml_add_package(pack);
aml_add_dword(pstt, Frequency); // CoreFreq (in MHz).
aml_add_dword(pstt, resolve_pss(0x00000000, Pw, base)); // Power (in milliWatts)
aml_add_dword(pstt, resolve_pss(0x0000000A, Lat1, base)); // Transition Latency (in microseconds).
aml_add_dword(pstt, resolve_pss(0x0000000A, Lat2, base)); // Bus Master Latency (in microseconds).
unsigned long Control = 0x00000000;
if (!expert) Control = p_states[i].Control;
aml_add_dword(pstt, resolve_pss(Control, Ctrl, base)); // Control
Pstatus++;
aml_add_dword(pstt, Pstatus); // Status
continue;
dropPstate:
#if DEBUG_ACPI
verbose("state with cpu frequency :%d and ratio :%d will be dropped\n",p_states[i].Frequency,curr_ratio);
#endif
dropPSS++;
}
// Add aliaces
for (i = 0; i < acpi_cpu_count; i++)
{
char name[9];
sprintf(name, "_PR_%c%c%c%c", acpi_cpu_name[i][0], acpi_cpu_name[i][1], acpi_cpu_name[i][2], acpi_cpu_name[i][3]);
scop = aml_add_scope(root, name);
aml_add_alias(scop, "PSS_", "_PSS");
}
aml_calculate_size(root);
struct acpi_2_ssdt *ssdt = (struct acpi_2_ssdt *)AllocateKernelMemory(root->Size);
aml_write_node(root, (void*)ssdt, 0);
ssdt->Length = root->Size;
setchecksum((struct acpi_common_header *)ssdt);
aml_destroy_node(root);
verbose ("SSDT with CPU P-States generated successfully");
if (dropPSS)
verbose(", %d P-state(s) dropped",dropPSS);
verbose("\n");
return ssdt;
}
}
else
{
verbose("ACPI CPUs not found: P-States not generated !!!\n");
}
return NULL;
}
struct acpi_2_gas FillGASStruct(uint32_t Address, uint8_t Length)
{
struct acpi_2_gas TmpGAS;
TmpGAS.Address_Space_ID = 1; /* I/O Address */
if (Address == 0)
{
TmpGAS.Register_Bit_Width = 0;
} else {
TmpGAS.Register_Bit_Width = Length * 8;
}
TmpGAS.Register_Bit_Offset = 0;
TmpGAS.Access_Size = 0; /* Not set for Legacy reasons... */
TmpGAS.Address = (uint64_t)Address;
return(TmpGAS);
}
struct acpi_2_fadt *
patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt, bool UpdateFADT)
{
struct acpi_2_fadt *fadt_mod;
struct acpi_2_fadt *fadt_file = (struct acpi_2_fadt *)loadACPITable(kFADT);
bool fadt_rev2_needed = false;
bool fix_restart = false;
const char * value;
bool aspmOff = true;
bool msiOff = true;
// Restart Fix
if (Platform->CPU.Vendor == 0x756E6547) {/* Intel */
fix_restart = true;
getBoolForKey(kRestartFix, &fix_restart, &bootInfo->bootConfig);
} else {
verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
}
if (fix_restart)
fadt_rev2_needed = true;
// Allocate new fadt table
if ((UpdateFADT) && (((fadt_file) && (fadt_file->Length < sizeof(struct acpi_2_fadt))) ||
((!fadt_file) && (fadt->Length < sizeof(struct acpi_2_fadt)))))
{
getBoolForKey(kDisableMSI, &msiOff, &bootInfo->bootConfig) ;
getBoolForKey(kDisableASPM, &aspmOff, &bootInfo->bootConfig);
if (fadt_file)
{
if (fadt_file->Length < 0xF4)
{
fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0xF4);
memcpy(fadt_mod, fadt_file, fadt_file->Length);
fadt_mod->Length = 0xF4;
}
else
{
fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt_file->Length);
memcpy(fadt_mod, fadt_file, fadt_file->Length);
}
}
else
{
if (fadt->Length < 0xF4)
{
fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0xF4);
memcpy(fadt_mod, fadt, fadt->Length);
fadt_mod->Length = 0xF4;
}
else
{
fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
memcpy(fadt_mod, fadt, fadt->Length);
}
}
fadt_mod->Revision = 0x04; // FADT rev 4
fadt_mod->RESET_REG = FillGASStruct(0, 0);
fadt_mod->Reset_Value = 0;
fadt_mod->Reserved[0] = 0;
fadt_mod->Reserved[1] = 0;
fadt_mod->Reserved[2] = 0;
fadt_mod->X_PM1a_EVT_BLK = FillGASStruct(fadt_mod->PM1A_Event_Block_Address, fadt_mod->PM1_Event_Block_Length);
fadt_mod->X_PM1b_EVT_BLK = FillGASStruct(fadt_mod->PM1B_Event_Block_Address, fadt_mod->PM1_Event_Block_Length);
fadt_mod->X_PM1a_CNT_BLK = FillGASStruct(fadt_mod->PM1A_Control_Block_Address, fadt_mod->PM1_Control_Block_Length);
fadt_mod->X_PM1b_CNT_BLK = FillGASStruct(fadt_mod->PM1B_Control_Block_Address, fadt_mod->PM1_Control_Block_Length);
fadt_mod->X_PM2_CNT_BLK = FillGASStruct(fadt_mod->PM2_Control_Block_Address, fadt_mod->PM2_Control_Block_Length);
fadt_mod->X_PM_TMR_BLK = FillGASStruct(fadt_mod->PM_Timer_Block_Address, fadt_mod->PM_Timer_Block_Length);
fadt_mod->X_GPE0_BLK = FillGASStruct(fadt_mod->GPE0_Block_Address, fadt_mod->GPE0_Block_Length);
fadt_mod->X_GPE1_BLK = FillGASStruct(fadt_mod->GPE1_Block_Address, fadt_mod->GPE1_Block_Length);
verbose("Converted ACPI V%d FADT to ACPI V4 FADT\n", (fadt) ? fadt->Revision : fadt->Revision);
} else {
if (fadt_file) {
if (fadt_file->Length < 0x84 && fadt_rev2_needed)
{
fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
memcpy(fadt_mod, fadt_file, fadt_file->Length);
fadt_mod->Length = 0x84;
fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
} else {
fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt_file->Length);
memcpy(fadt_mod, fadt_file, fadt_file->Length);
}
} else {
if (fadt->Length < 0x84 && fadt_rev2_needed)
{
fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
memcpy(fadt_mod, fadt, fadt->Length);
fadt_mod->Length = 0x84;
fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
} else {
fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
memcpy(fadt_mod, fadt, fadt->Length);
}
}
}
uint8_t Type = PMProfileError;
// Determine system type / PM_Model
// Fix System-type if needed (should never happen)
if (Platform->Type > MaxSupportedPMProfile)
{
if(fadt_mod->PM_Profile <= MaxSupportedPMProfile)
Platform->Type = fadt_mod->PM_Profile; // get the fadt if correct
else
Platform->Type = 1;/* Set a fixed value (Desktop) */
}
// If needed, set System-type from PM_Profile (if valid) else set PM_Profile with a fixed the System-type
// Give prior to the FADT pm profile, allow to also control this value with a patched FADT table
if (fadt_mod->PM_Profile != Platform->Type)
{
bool val = false;
getBoolForKey("PreferInternalProfileDetect", &val, &bootInfo->bootConfig); // if true Give prior to the profile resolved trought the CPU model
val = Platform->CPU.isServer ;
if (fadt_mod->PM_Profile <= MaxSupportedPMProfile && !val) {
Platform->Type = fadt_mod->PM_Profile;
} else {
fadt_mod->PM_Profile = Platform->Type;
}
}
// Set PM_Profile and System-type if user wanted this value to be forced
if ( (value=getStringForKey("SystemType", &bootInfo->bootConfig))!=NULL) {
if ((Type = (unsigned char) strtoul(value, NULL, 10) ) <= MaxSupportedPMProfile)
{
verbose("FADT: changing Preferred_PM_Profile from 0x%02x to 0x%02x\n", fadt->PM_Profile, Type);
fadt_mod->PM_Profile = Platform->Type = Type;
} else verbose("Error: system-type must be 0..6. Defaulting to %d !\n", Platform->Type);
}
// Patch FADT to fix restart
if (fix_restart)
{
fadt_mod->Flags|= 0x400;
fadt_mod->RESET_REG = FillGASStruct(0x0cf9, 1);
fadt_mod->Reset_Value = 0x06;
verbose("FADT: Restart Fix applied !\n");
}
if (aspmOff) {
fadt_mod->Boot_Flags |= 1 << 4;
} else {
fadt_mod->Boot_Flags &= 0xFFEF;
}
if (msiOff) {
fadt_mod->Boot_Flags |= 1 << 3;
} else {
fadt_mod->Boot_Flags &= 0xFFF7;
}
fadt_mod->FIRMWARE_CTRL=(uint32_t)fadt->FIRMWARE_CTRL;
if ((uint32_t)(&(fadt_mod->X_FIRMWARE_CTRL))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
fadt_mod->X_FIRMWARE_CTRL=(uint32_t)fadt->FIRMWARE_CTRL;
Platform->hardware_signature = ((struct acpi_2_facs *)fadt->FIRMWARE_CTRL)->hardware_signature;
DBG("setting hardware_signature to %x \n",Platform->hardware_signature);
// Patch DSDT Address if we have loaded a DSDT table
if(new_dsdt)
{
fadt_mod->DSDT=(uint32_t)new_dsdt;
if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
fadt_mod->X_DSDT=(uint32_t)new_dsdt;
}
// Correct the checksum
setchecksum((struct acpi_common_header *)fadt_mod);
return fadt_mod;
}
/* Setup ACPI */
EFI_STATUS setupAcpi(void)
{
int version;
EFI_STATUS Status = EFI_ABORTED;
void *new_dsdt=NULL, *new_hpet=NULL, *new_sbst=NULL, *new_ecdt=NULL, *new_asft=NULL, *new_dmar=NULL, *new_apic=NULL, *new_mcfg=NULL, *new_ssdts[14];
struct acpi_2_ssdt *new_ssdt[17]; // 15 + 2 additional tables for pss & cst
struct acpi_2_fadt *fadt; // will be used in CST generator
struct acpi_2_fadt *fadt_mod=NULL;
bool oem_dsdt=false, oem_ssdt=false, oem_hpet=false, oem_sbst=false, oem_ecdt=false, oem_asft=false, oem_dmar=false, oem_apic=false, oem_mcfg=false;
bool update_acpi=false, gen_xsdt=false;
bool hpet_replaced=false, sbst_replaced=false, ecdt_replaced=false, asft_replaced=false, dmar_replaced=false, apic_replaced=false, mcfg_replaced=false;
bool hpet_added=false, sbst_added=false, ecdt_added=false, asft_added=false, dmar_added=false, apic_added=false, mcfg_added=false;
bool gen_csta=false, gen_psta=false, speed_step=false;
bool quick_ssdt= false;
// Quick ssdt search,
// first try to find ssdt-0.aml, if the file do not exist we
// stop searching here, else ssdt-0.aml is loaded and we try
// to find ssdt-1.aml, etc .........
int curssdt=0, loadtotssdt=0, totssdt=0, newtotssdt=0;
{
bool tmpval;
oem_dsdt=getBoolForKey(kOEMDSDT, &tmpval, &bootInfo->bootConfig)&&tmpval;
oem_ssdt=getBoolForKey(kOEMSSDT, &tmpval, &bootInfo->bootConfig)&&tmpval;
oem_hpet=getBoolForKey(kOEMHPET, &tmpval, &bootInfo->bootConfig)&&tmpval;
oem_sbst=getBoolForKey(kOEMSBST, &tmpval, &bootInfo->bootConfig)&&tmpval;
oem_ecdt=getBoolForKey(kOEMECDT, &tmpval, &bootInfo->bootConfig)&&tmpval;
oem_asft=getBoolForKey(kOEMASFT, &tmpval, &bootInfo->bootConfig)&&tmpval;
oem_dmar=getBoolForKey(kOEMDMAR, &tmpval, &bootInfo->bootConfig)&&tmpval;
oem_apic=getBoolForKey(kOEMAPIC, &tmpval, &bootInfo->bootConfig)&&tmpval;
oem_mcfg=getBoolForKey(kOEMMCFG, &tmpval, &bootInfo->bootConfig)&&tmpval;
gen_csta=getBoolForKey(kGenerateCStates, &tmpval, &bootInfo->bootConfig)&&tmpval;
gen_psta=getBoolForKey(kGeneratePStates, &tmpval, &bootInfo->bootConfig)&&tmpval;
update_acpi=getBoolForKey(kUpdateACPI, &tmpval, &bootInfo->bootConfig)&&tmpval;
quick_ssdt=getBoolForKey(kQSSDT, &tmpval, &bootInfo->bootConfig)&&tmpval;
speed_step=getBoolForKey(kSpeedstep, &tmpval, &bootInfo->bootConfig)&&tmpval;
}
if (speed_step) {
gen_psta= true;
gen_csta= true;
}
// Load replacement ACPI tables
if (!oem_dsdt)
new_dsdt=loadACPITable(kDSDT);
if (!oem_hpet)
new_hpet=loadACPITable(kHPET);
if (!oem_sbst)
new_sbst=loadACPITable(kSBST);
if (!oem_ecdt)
new_ecdt=loadACPITable(kECDT);
if (!oem_asft)
new_asft=loadACPITable(kASFT);
if (!oem_dmar)
new_dmar=loadACPITable(kDMAR);
if (!oem_apic)
new_apic=loadACPITable(kAPIC);
if (!oem_mcfg)
new_mcfg=loadACPITable(kMCFG);
if (!oem_ssdt)
{
for (curssdt=0;curssdt<15;curssdt++)
{
new_ssdt[curssdt]=loadSSDTTable(curssdt);
if (new_ssdt[curssdt])
loadtotssdt++;
else if (quick_ssdt)
break;
}
curssdt=0;
}
DBG("New ACPI tables Loaded in memory\n");
TagPtr DropTables_p = XMLCastDict(XMLGetProperty(bootInfo->bootConfig.dictionary, (const char*)"ACPIDropTables"));
// Do the same procedure for both versions of ACPI
for (version=0; version<2; version++) {
struct acpi_2_rsdp *rsdp_mod, *rsdp_conv=(struct acpi_2_rsdp *)0;
struct acpi_2_rsdt *rsdt, *rsdt_mod;
struct acpi_2_xsdt *xsdt_conv = (struct acpi_2_xsdt *)0;
int rsdplength;
// Find original rsdp
struct acpi_2_rsdp *rsdp=(struct acpi_2_rsdp *)(version?biosacpi_find_rsdp(2):biosacpi_find_rsdp(0));
if ((update_acpi) && (rsdp->Revision == 0))
{
rsdp_conv = gen_rsdp_v2_from_v1(rsdp);
gen_xsdt = true;
version = 1;
addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
verbose("Converted ACPI RSD PTR version 1 to version 3\n");
}
if (!rsdp)
{
DBG("No ACPI version %d found. Ignoring\n", version+1);
if (version)
addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
else
addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
continue;
}
rsdplength=version?rsdp->Length:20;
DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
/* FIXME: no check that memory allocation succeeded
* Copy and patch RSDP,RSDT, XSDT and FADT
* For more info see ACPI Specification pages 110 and following
*/
if (gen_xsdt)
{
rsdp_mod=rsdp_conv;
} else {
rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
memcpy(rsdp_mod, rsdp, rsdplength);
}
rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
{
int dropoffset=0, i;
rsdt_mod=(struct acpi_2_rsdt *)AllocateKernelMemory(rsdt->Length);
memcpy (rsdt_mod, rsdt, rsdt->Length);
update_rsdp_with_rsdt(rsdp_mod, rsdt_mod);
int rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
uint32_t *rsdt_entries=(uint32_t *)(rsdt_mod+1);
if (gen_xsdt)
{
xsdt_conv=gen_xsdt_from_rsdt(rsdt_mod);
update_rsdp_with_xsdt(rsdp, xsdt_conv);
verbose("Converted RSDT table to XSDT table\n");
}
for (i=0;i<rsdt_entries_num;i++)
{
char *table=(char *)(rsdt_entries[i]);
if (!table)
continue;
DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
rsdt_entries[i-dropoffset]=rsdt_entries[i];
char table4[4];
strlcpy(table4, table, sizeof(table4)+1);
TagPtr match_drop = XMLGetProperty(DropTables_p, (const char*)table4);
if ( match_drop ) {
char *tmpstr = XMLCastString(match_drop);
if (strcmp(tmpstr,"No") != 0) {
dropoffset++;
DBG("%s table dropped\n",table4);
continue;
}
}
if ((!(oem_hpet)) && tableSign(table, "HPET"))
{
DBG("HPET found\n");
if (new_hpet)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_hpet;
hpet_replaced=true;
}
continue;
}
if ((!(oem_sbst)) && tableSign(table, "SBST"))
{
DBG("SBST found\n");
if (new_sbst)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_sbst;
sbst_replaced=true;
}
continue;
}
if ((!(oem_ecdt)) && tableSign(table, "ECDT"))
{
DBG("ECDT found\n");
if (new_ecdt)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_ecdt;
ecdt_replaced=true;
}
continue;
}
if ((!(oem_asft)) && tableSign(table, "ASF!"))
{
DBG("ASF! found\n");
if (new_asft)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_asft;
asft_replaced=true;
}
continue;
}
if ((!(oem_dmar)) && tableSign(table, "DMAR"))
{
DBG("DMAR found\n");
if (new_dmar)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_dmar;
dmar_replaced=true;
}
continue;
}
if ((!(oem_apic)) && tableSign(table, "APIC"))
{
DBG("APIC found\n");
if (new_apic)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_apic;
apic_replaced=true;
}
continue;
}
if ((!(oem_mcfg)) && tableSign(table, "MCFG"))
{
DBG("MCFG found\n");
if (new_mcfg)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_mcfg;
mcfg_replaced=true;
}
continue;
}
if ((!(oem_ssdt)) && tableSign(table, "SSDT"))
{
DBG("SSDT %d found", curssdt);
if (new_ssdts[curssdt])
{
DBG(" and replaced");
rsdt_entries[i-dropoffset]=(uint32_t)new_ssdts[curssdt];
totssdt++;
}
DBG("\n");
curssdt++;
continue;
}
if ((!(oem_dsdt)) && tableSign(table, "DSDT"))
{
DBG("DSDT found\n");
rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
continue;
}
if (tableSign(table, "FACP"))
{
fadt=(struct acpi_2_fadt *)rsdt_entries[i];
DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
{
printf("FADT incorrect. Not modified\n");
continue;
}
verbose("Attempting to patch FADT entry of RSDT\n");
fadt_mod = patch_fadt(fadt, new_dsdt, update_acpi);
rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
if (!oem_ssdt)
{
// Generate _CST SSDT
if ( gen_csta && (new_ssdt[loadtotssdt] = generate_cst_ssdt(fadt_mod)))
{
gen_csta= false;
loadtotssdt++;
}
// Generating _PSS SSDT
if (gen_psta && (new_ssdt[loadtotssdt] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
{
gen_psta= false;
loadtotssdt++;
}
}
continue;
}
}
DBG("\n");
if ((!oem_hpet) && (!hpet_replaced))
{
if (new_hpet)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_hpet;
hpet_added=true;
i++;
}
}
if ((!oem_sbst) && (!sbst_replaced))
{
if (new_sbst)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_sbst;
sbst_added=true;
i++;
}
}
if ((!oem_ecdt) && (!ecdt_replaced))
{
if (new_ecdt)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_ecdt;
ecdt_added=true;
i++;
}
}
if ((!oem_asft) && (!asft_replaced))
{
if (new_asft)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_asft;
asft_added=true;
i++;
}
}
if ((!oem_dmar) && (!dmar_replaced))
{
if (new_dmar)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_dmar;
dmar_added=true;
i++;
}
}
if ((!oem_apic) && (!apic_replaced))
{
if (new_apic)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_apic;
apic_added=true;
i++;
}
}
if ((!oem_mcfg) && (!mcfg_replaced))
{
if (new_mcfg)
{
rsdt_entries[i-dropoffset]=(uint32_t)new_mcfg;
mcfg_added=true;
i++;
}
}
if (!oem_ssdt)
{
while ((totssdt < loadtotssdt) && (curssdt < 17))
{
if (new_ssdt[curssdt])
{
DBG("adding SSDT %d\n", curssdt);
rsdt_entries[i-dropoffset]=(uint32_t)new_ssdt[curssdt];
totssdt++;
newtotssdt++;
i++;
} else if (quick_ssdt) {
break;
}
curssdt++;
}
}
// Correct the checksum of RSDT
rsdt_mod->Length-=4*dropoffset;
rsdt_mod->Length+=4*newtotssdt;
if (hpet_added)
rsdt_mod->Length+=4;
if (sbst_added)
rsdt_mod->Length+=4;
if (ecdt_added)
rsdt_mod->Length+=4;
if (asft_added)
rsdt_mod->Length+=4;
if (dmar_added)
rsdt_mod->Length+=4;
if (apic_added)
rsdt_mod->Length+=4;
if (mcfg_added)
rsdt_mod->Length+=4;
DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
setchecksum((struct acpi_common_header *)rsdt_mod);
DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
}
else
{
rsdp_mod->RsdtAddress=0;
printf("RSDT not found or RSDT incorrect\n");
}
if (version)
{
struct acpi_2_xsdt *xsdt, *xsdt_mod;
// FIXME: handle 64-bit address correctly
if (gen_xsdt)
xsdt=xsdt_conv;
else
xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
xsdt->Length);
if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
{
int dropoffset=0, i;
curssdt=0, totssdt=0, newtotssdt=0;
hpet_replaced=false, hpet_added=false;
sbst_replaced=false, sbst_added=false;
ecdt_replaced=false, ecdt_added=false;
asft_replaced=false, asft_added=false;
dmar_replaced=false, dmar_added=false;
apic_replaced=false, apic_added=false;
mcfg_replaced=false, mcfg_added=false;
if (gen_xsdt)
xsdt_mod=xsdt;
else
{
xsdt_mod=(struct acpi_2_xsdt*)AllocateKernelMemory(xsdt->Length);
memcpy(xsdt_mod, xsdt, xsdt->Length);
}
update_rsdp_with_xsdt(rsdp_mod, xsdt_mod);
int xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
uint64_t *xsdt_entries=(uint64_t *)(xsdt_mod+1);
for (i=0;i<xsdt_entries_num;i++)
{
char *table=(char *)((uint32_t)(xsdt_entries[i]));
if (!table)
continue;
xsdt_entries[i-dropoffset]=xsdt_entries[i];
char table4[4];
strlcpy(table4, table, sizeof(table4)+1);
TagPtr match_drop = XMLGetProperty(DropTables_p, (const char*)table4);
if ( match_drop ) {
char *tmpstr = XMLCastString(match_drop);
if (strcmp(tmpstr,"No") != 0) {
dropoffset++;
DBG("%s table dropped\n",table4);
continue;
}
}
if ((!(oem_hpet)) && tableSign(table, "HPET"))
{
DBG("HPET found\n");
if (new_hpet)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_hpet;
hpet_replaced=true;
}
continue;
}
if ((!(oem_sbst)) && tableSign(table, "SBST"))
{
DBG("SBST found\n");
if (new_sbst)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_sbst;
sbst_replaced=true;
}
continue;
}
if ((!(oem_ecdt)) && tableSign(table, "ECDT"))
{
DBG("ECDT found\n");
if (new_ecdt)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_ecdt;
ecdt_replaced=true;
}
continue;
}
if ((!(oem_asft)) && tableSign(table, "ASF!"))
{
DBG("ASF! found\n");
if (new_asft)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_asft;
asft_replaced=true;
}
continue;
}
if ((!(oem_dmar)) && tableSign(table, "DMAR"))
{
DBG("DMAR found\n");
if (new_dmar)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_dmar;
dmar_replaced=true;
}
continue;
}
if ((!(oem_apic)) && tableSign(table, "APIC"))
{
DBG("APIC found\n");
if (new_apic)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_apic;
apic_replaced=true;
}
continue;
}
if ((!(oem_mcfg)) && tableSign(table, "MCFG"))
{
DBG("MCFG found\n");
if (new_mcfg)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_mcfg;
mcfg_replaced=true;
}
continue;
}
if ((!(oem_ssdt)) && tableSign(table, "SSDT"))
{
DBG("SSDT %d found", curssdt);
if (new_ssdts[curssdt])
{
DBG(" and replaced");
xsdt_entries[i-dropoffset]=(uint32_t)new_ssdts[curssdt];
totssdt++;
}
DBG("\n");
curssdt++;
continue;
}
if ((!(oem_dsdt)) && tableSign(table, "DSDT"))
{
DBG("DSDT found\n");
xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
continue;
}
if (tableSign(table, "FACP"))
{
fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
fadt->Length);
if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
{
verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
goto drop_xsdt;
}
verbose("Attempting to patch FADT entry of XSDT\n");
fadt_mod = patch_fadt(fadt, new_dsdt, update_acpi);
xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
//DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
if (!oem_ssdt)
{
// Generate _CST SSDT
if ( gen_csta && (new_ssdt[loadtotssdt] = generate_cst_ssdt(fadt_mod)))
{
gen_csta= false;
loadtotssdt++;
}
// Generating _PSS SSDT
if (gen_psta && (new_ssdt[loadtotssdt] = generate_pss_ssdt((void*)fadt_mod->DSDT)))
{
gen_psta= false;
loadtotssdt++;
}
}
continue;
}
//DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
}
if ((!oem_hpet) && (!hpet_replaced))
{
if (new_hpet)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_hpet;
hpet_added=true;
i++;
}
}
if ((!oem_sbst) && (!sbst_replaced))
{
if (new_sbst)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_sbst;
sbst_added=true;
i++;
}
}
if ((!oem_ecdt) && (!ecdt_replaced))
{
if (new_ecdt)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_ecdt;
ecdt_added=true;
i++;
}
}
if ((!oem_asft) && (!asft_replaced))
{
if (new_asft)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_asft;
asft_added=true;
i++;
}
}
if ((!oem_dmar) && (!dmar_replaced))
{
if (new_dmar)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_dmar;
dmar_added=true;
i++;
}
}
if ((!oem_apic) && (!apic_replaced))
{
if (new_apic)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_apic;
apic_added=true;
i++;
}
}
if ((!oem_mcfg) && (!mcfg_replaced))
{
if (new_mcfg)
{
xsdt_entries[i-dropoffset]=(uint32_t)new_mcfg;
mcfg_added=true;
i++;
}
}
if (!oem_ssdt)
{
while ((totssdt < loadtotssdt) && (curssdt < 17))
{
if (new_ssdt[curssdt])
{
DBG("adding SSDT %d\n", curssdt);
xsdt_entries[i-dropoffset]=(uint32_t)new_ssdt[curssdt];
totssdt++;
newtotssdt++;
i++;
} else if (quick_ssdt){
break;
}
curssdt++;
}
}
// Correct the checksum of XSDT
xsdt_mod->Length-=8*dropoffset;
xsdt_mod->Length+=8*newtotssdt;
if (hpet_added)
xsdt_mod->Length+=8;
if (sbst_added)
xsdt_mod->Length+=8;
if (ecdt_added)
xsdt_mod->Length+=8;
if (asft_added)
xsdt_mod->Length+=8;
if (dmar_added)
xsdt_mod->Length+=8;
if (apic_added)
xsdt_mod->Length+=8;
if (mcfg_added)
xsdt_mod->Length+=8;
setchecksum((struct acpi_common_header *)xsdt_mod);
}
else
{
drop_xsdt:
DBG("About to drop XSDT\n");
/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
* A Better strategy would be to generate
*/
rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
verbose("XSDT not found or XSDT incorrect\n");
}
}
// Correct the checksum of RSDP
DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
setRsdpchecksum(rsdp_mod);
DBG("New checksum %d\n", rsdp_mod->Checksum);
if (version)
{
DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
setRsdpXchecksum(rsdp_mod);
DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
}
verbose("Patched ACPI version %d\n", version+1);
if (version)
{
/* XXX aserebln why uint32 cast if pointer is uint64 ? */
acpi20_p = (uint32_t)rsdp_mod;
if (acpi20_p)
Status = addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
}
else
{
/* XXX aserebln why uint32 cast if pointer is uint64 ? */
acpi10_p = (uint32_t)rsdp_mod;
if (acpi10_p)
Status = addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
}
}
#if DEBUG_DSDT
printf("Press a key to continue... (DEBUG_DSDT)\n");
getc();
#endif
//return (Status == EFI_SUCCESS) ? 1 : 0;
return Status ;
}
branches/cparm/i386/modules/ACPIPatcher/AcpiPatcher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Copyright (c) 2009 Evan Lojewski. All rights reserved.
*
*/
/*
* Copyright (c) 2010 cparm <armelcadetpetit@gmail.com>. All rights reserved.
*
*/
#include "libsaio.h"
#include "modules.h"
#include "boot.h"
#include "bootstruct.h"
#include "pci_root.h"
#include "acpi_patcher.h"
#define kEnableAcpi"EnableAcpiModule"
void AcpiPatcher_setupEfiConfigurationTable_hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)
{
EFI_STATUS *ret = (EFI_STATUS *)arg1;
// Setup ACPI (mackerintel's patch)
*ret = setupAcpi();
}
void is_ACPI_Patcher_Registred_Hook(void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6){}
void AcpiPatcher_start()
{
bool enable = true;
getBoolForKey(kEnableAcpi, &enable, &bootInfo->bootConfig);
enable = (execute_hook("isACPIRegistred", NULL, NULL, NULL, NULL, NULL, NULL) != EFI_SUCCESS);
if (enable) {
register_hook_callback("setupEfiConfigurationTable", &AcpiPatcher_setupEfiConfigurationTable_hook);
register_hook_callback("isACPIRegistred", &is_ACPI_Patcher_Registred_Hook);
}
}
branches/cparm/i386/modules/ACPIPatcher/aml_generator.h
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
/*
* aml_generator.h
* Chameleon
*
* Created by Mozodojo on 20/07/10.
* Copyright 2010 mozo. All rights reserved.
*
*/
#ifndef __LIBSAIO_AML_GENERATOR_H
#define __LIBSAIO_AML_GENERATOR_H
#include "libsaio.h"
#defineAML_CHUNK_NONE0xff
#defineAML_CHUNK_ZERO0x00
#defineAML_CHUNK_ONE0x01
#defineAML_CHUNK_ALIAS0x06
#defineAML_CHUNK_NAME0x08
#defineAML_CHUNK_BYTE0x0A
#defineAML_CHUNK_WORD0x0B
#defineAML_CHUNK_DWORD0x0C
#defineAML_CHUNK_STRING0x0D
#defineAML_CHUNK_QWORD0x0E
#defineAML_CHUNK_SCOPE0x10
#defineAML_CHUNK_PACKAGE0x12
struct aml_chunk
{
unsigned charType;
unsigned intLength;
char*Buffer;
unsigned intSize;
struct aml_chunk*Next;
struct aml_chunk*First;
struct aml_chunk*Last;
};
static inline bool aml_isvalidchar(char c)
{
return isupper(c) || isdigit(c) || c == '_';
};
bool aml_add_to_parent(struct aml_chunk* parent, struct aml_chunk* node);
struct aml_chunk* aml_create_node(struct aml_chunk* parent);
void aml_destroy_node(struct aml_chunk* node);
struct aml_chunk* aml_add_buffer(struct aml_chunk* parent, const char* buffer, unsigned int size);
struct aml_chunk* aml_add_byte(struct aml_chunk* parent, unsigned char value);
struct aml_chunk* aml_add_word(struct aml_chunk* parent, unsigned int value);
struct aml_chunk* aml_add_dword(struct aml_chunk* parent, unsigned long value);
struct aml_chunk* aml_add_qword(struct aml_chunk* parent, unsigned long long value);
struct aml_chunk* aml_add_scope(struct aml_chunk* parent, const char* name);
struct aml_chunk* aml_add_name(struct aml_chunk* parent, const char* name);
struct aml_chunk* aml_add_package(struct aml_chunk* parent);
struct aml_chunk* aml_add_alias(struct aml_chunk* parent, const char* name1, const char* name2);
unsigned int aml_calculate_size(struct aml_chunk* node);
unsigned int aml_write_node(struct aml_chunk* node, char* buffer, unsigned int offset);
struct aml_chunk* aml_add_package_with_value(struct aml_chunk* parent, uint64_t *lst, int num);
void aml_add_multiple_value(struct aml_chunk* parent, uint64_t *lst, int num);
void aml_add_ressource_t_fxhw(struct aml_chunk* parent, uint64_t *lst);
void aml_add_value(struct aml_chunk* parent, uint64_t val);
#endif /* !__LIBSAIO_AML_GENERATOR_H */
branches/cparm/i386/modules/ACPIPatcher/acpi_patcher.h
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
/*
* Copyright 2008 mackerintel
*/
/*
* Copyright (c) 2010 cparm <armelcadetpetit@gmail.com>. All rights reserved.
*
*/
#ifndef __LIBSAIO_ACPI_PATCHER_H
#define __LIBSAIO_ACPI_PATCHER_H
#include "libsaio.h"
#include "efi.h"
#define kDSDT"DSDT"/* acpi_patcher.c */
#define kRestartFix"RestartFix" /* acpi_patcher.c */
#define kGeneratePStates"GeneratePStates"/* acpi_patcher.c */
#define kGenerateCStates"GenerateCStates"/* acpi_patcher.c */
#define kMaxRatio"MaxBusRatio"/* acpi_patcher.c */
#define kMinRatio"MinBusRatio"/* acpi_patcher.c */
#definekQSSDT"quickSSDTsearch"/* acpi_patcher.c */
#definekSpeedstep"EnableSpeedStep"/* acpi_patcher.c */
#define kEnableC4State"EnableC4State"/* acpi_patcher.c */
#define kFACS"FACS"/* acpi_patcher.c */
#define kSSDT"SSDT"/* acpi_patcher.c */
#define kHPET"HPET"/* acpi_patcher.c */
#define kSBST"SBST"/* acpi_patcher.c */
#define kECDT"ECDT"/* acpi_patcher.c */
#define kASFT"ASFT"/* acpi_patcher.c */
#define kDMAR"DMAR"/* acpi_patcher.c */
#define kFADT"FADT"/* acpi_patcher.c */
#define kAPIC"APIC"/* acpi_patcher.c */
#define kMCFG"MCFG"/* acpi_patcher.c */
#define kOEMDSDT"oemDSDT"/* acpi_patcher.c */
#define kOEMSSDT"oemSSDT"/* acpi_patcher.c */
#define kOEMHPET"oemHPET"/* acpi_patcher.c */
#define kOEMSBST"oemSBST"/* acpi_patcher.c */
#define kOEMECDT"oemECDT"/* acpi_patcher.c */
#define kOEMASFT"oemASFT"/* acpi_patcher.c */
#define kOEMDMAR"oemDMAR"/* acpi_patcher.c */
#define kOEMFADT"oemFADT"/* acpi_patcher.c */
#define kOEMAPIC"oemAPIC"/* acpi_patcher.c */
#define kOEMMCFG"oemMCFG"/* acpi_patcher.c */
#define kUpdateACPI"UpdateACPI" /* acpi_patcher.c */
#define kDisableASPM"DisableASPM"/* acpi_patcher.c */
#define kDisableMSI"DisableMSI"/* acpi_patcher.c */
#if UNUSED
#define kGenerateFACS"GenerateFACS"/* acpi_patcher.c */
#define kOEMFACS"oemFACS"/* acpi_patcher.c */
#endif
extern EFI_STATUS setupAcpi();
struct p_state
{
union
{
uint16_t Control;
struct
{
uint8_t VID;// Voltage ID
uint8_t FID;// Frequency ID
};
};
uint8_tCID;// Compare ID
uint32_tFrequency;
};
/*
* SIGNATURE_16, SIGNATURE_32, SIGNATURE_64 are extracted from the edk2 project (Base.h), and are under the following license:
*
* Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.
* Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
* This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php.
* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/
#define SIGNATURE_16(A, B) ((A) | (B << 8))
#define SIGNATURE_32(A, B, C, D) (SIGNATURE_16 (A, B) | (SIGNATURE_16 (C, D) << 16))
#define SIGNATURE_64(A, B, C, D, E, F, G, H) \
(SIGNATURE_32 (A, B, C, D) | ((uint64_t) (SIGNATURE_32 (E, F, G, H)) << 32))
#define ACPI_SIGNATURE_UINT64_LE SIGNATURE_64('R','S','D',' ','P','T','R',' ')
/* Per ACPI 3.0a spec */
// TODO Migrate
struct acpi_2_rsdp {
char Signature[8];
uint8_t Checksum;
char OEMID[6];
uint8_t Revision;
uint32_t RsdtAddress;
uint32_t Length;
uint64_t XsdtAddress;
uint8_t ExtendedChecksum;
char Reserved[3];
} __attribute__((packed));
#define ACPI_HEADER_CORE\
char Signature[4];\
uint32_t Length;\
uint8_t Revision;\
uint8_t Checksum;\
char OEMID[6];\
char OEMTableId[8];\
uint32_t OEMRevision;\
uint32_t CreatorId;\
uint32_t CreatorRevision;
struct acpi_common_header {
ACPI_HEADER_CORE
} __attribute__((packed));
// TODO Migrate
struct acpi_2_rsdt {
ACPI_HEADER_CORE
} __attribute__((packed));
// TODO Migrate
struct acpi_2_xsdt {
ACPI_HEADER_CORE
} __attribute__((packed));
// TODO Migrate
struct acpi_2_gas {
uint8_tAddress_Space_ID;
uint8_tRegister_Bit_Width;
uint8_tRegister_Bit_Offset;
uint8_tAccess_Size;
uint64_tAddress;
} __attribute__((packed));
// TODO Migrate
struct acpi_2_ssdt {
ACPI_HEADER_CORE
} __attribute__((packed));
// TODO Migrate
struct acpi_2_dsdt {
ACPI_HEADER_CORE
} __attribute__((packed));
// TODO Migrate
struct acpi_2_fadt {
ACPI_HEADER_CORE
uint32_t FIRMWARE_CTRL;
uint32_t DSDT;
uint8_t Model;// JrCs
uint8_t PM_Profile;// JrCs
uint16_tSCI_Interrupt;
uint32_tSMI_Command_Port;
uint8_tACPI_Enable;
uint8_tACPI_Disable;
uint8_tS4BIOS_Command;
uint8_tPState_Control;
uint32_tPM1A_Event_Block_Address;
uint32_tPM1B_Event_Block_Address;
uint32_tPM1A_Control_Block_Address;
uint32_tPM1B_Control_Block_Address;
uint32_tPM2_Control_Block_Address;
uint32_tPM_Timer_Block_Address;
uint32_tGPE0_Block_Address;
uint32_tGPE1_Block_Address;
uint8_tPM1_Event_Block_Length;
uint8_tPM1_Control_Block_Length;
uint8_tPM2_Control_Block_Length;
uint8_tPM_Timer_Block_Length;
uint8_tGPE0_Block_Length;
uint8_tGPE1_Block_Length;
uint8_tGPE1_Base_Offset;
uint8_tCST_Support;
uint16_tC2_Latency;
uint16_tC3_Latency;
uint16_tCPU_Cache_Size;
uint16_tCache_Flush_Stride;
uint8_tDuty_Cycle_Offset;
uint8_tDuty_Cycle_Width;
uint8_tRTC_Day_Alarm_Index;
uint8_tRTC_Month_Alarm_Index;
uint8_tRTC_Century_Index;
uint16_tBoot_Flags;
uint8_tReserved0;
/* Begin Asere */
//Reset Fix
uint32_tFlags;
struct acpi_2_gasRESET_REG;
uint8_tReset_Value;
uint8_tReserved[3];
uint64_tX_FIRMWARE_CTRL;
uint64_tX_DSDT;
#if UNUSED
/* End Asere */
/*We absolutely don't care about theese fields*/
uint8_tnotimp2[96];
#else
struct acpi_2_gasX_PM1a_EVT_BLK;
struct acpi_2_gasX_PM1b_EVT_BLK;
struct acpi_2_gasX_PM1a_CNT_BLK;
struct acpi_2_gasX_PM1b_CNT_BLK;
struct acpi_2_gasX_PM2_CNT_BLK;
struct acpi_2_gasX_PM_TMR_BLK;
struct acpi_2_gasX_GPE0_BLK;
struct acpi_2_gasX_GPE1_BLK;
#endif
} __attribute__((packed));
struct acpi_2_facs {
charSignature[4];
uint32_t Length;
uint32_thardware_signature;
uint32_tfirmware_waking_vector;
uint32_tglobal_lock;
uint32_tflags;
uint64_tx_firmware_waking_vector;
uint8_tversion;
uint8_tReserved[3];
uint32_tospm_flags;
uint8_tReserved1[24];
} __attribute__ ((packed));
#endif /* !__LIBSAIO_ACPI_PATCHER_H */
branches/cparm/i386/modules/ACPIPatcher/Makefile
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
MODULE_NAME = AcpiPatcher
MODULE_VERSION = "1.0.0"
MODULE_COMPAT_VERSION = "1.0.0"
MODULE_START = _$(MODULE_NAME)_start
MODULE_DEPENDENCIES =
DIR = AcpiPatcher
include ../../MakePaths.dir
OBJROOT=../../../obj/i386/modules/$(DIR)
SYMROOT=../../../sym/i386/modules/
DSTROOT=../../../dst/i386/modules/
UTILDIR = ../../util
LIBSADIR = ../../libsa
LIBSAIODIR = ../../libsaio
BOOT2DIR = ../../boot2
INSTALLDIR = $(DSTROOT)/System/Library/Frameworks/System.framework/Versions/B/PrivateHeaders/standalone
OPTIM = -Os -Oz
DEBUG = -DNOTHING
CFLAGS= $(RC_CFLAGS) $(OPTIM) $(MORECPP) -arch i386 -g -Wmost \
-D__ARCHITECTURE__=\"i386\" -DSAIO_INTERNAL_USER \
-DRCZ_COMPRESSED_FILE_SUPPORT $(DEBUG) \
-fno-builtin $(OMIT_FRAME_POINTER_CFLAG) \
-mpreferred-stack-boundary=2 -fno-align-functions -fno-stack-protector \
-march=pentium4 -msse2 -mfpmath=sse -msoft-float -fno-common #-mdynamic-no-pic -Wextra -Wpacked -Wpadded -Wunreachable-code -Winline -Wunsafe-loop-optimizations -funsafe-loop-optimizations -Wconversion
DEFINES=
CONFIG = hd
INC = -I. -I.. -I$(SYMROOT) -I$(UTILDIR) -I$(LIBSADIR) -I$(LIBSAIODIR) -I$(BOOT2DIR)
ifneq "" "$(wildcard /bin/mkdirs)"
MKDIRS = /bin/mkdirs
else
MKDIRS = /bin/mkdir -p
endif
AS = as
LD = ld
# LIBS= -lc_static
LIBS=
VPATH = $(OBJROOT):$(SYMROOT)
ACPI_PATCHER_OBJS = AcpiPatcher.o aml_generator.o acpi_patcher.o
SFILES =
CFILES =
HFILES =
EXPORTED_HFILES =
INSTALLED_HFILES =
OTHERFILES = Makefile
ALLSRC = $(SFILES) $(CFILES) \
$(HFILES) $(OTHERFILES)
DIRS_NEEDED = $(OBJROOT) $(SYMROOT)
all embedtheme: ${ACPI_PATCHER_OBJS} dylib
dylib:
ld -arch i386 \
-undefined dynamic_lookup \
-alias $(MODULE_START) start \
-dylib -read_only_relocs suppress \
-S -x -Z -dead_strip_dylibs \
-no_uuid \
-current_version $(MODULE_VERSION) -compatibility_version $(MODULE_COMPAT_VERSION) \
-final_output $(MODULE_NAME) \
$(OBJROOT)/AcpiPatcher.o \
$(OBJROOT)/aml_generator.o \
$(OBJROOT)/acpi_patcher.o \
-o $(SYMROOT)/$(MODULE_NAME).dylib
AcpiPatcher.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEFINES) -c "AcpiPatcher.c" $(INC) -o "$(OBJROOT)/AcpiPatcher.o"
aml_generator.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEFINES) -c "aml_generator.c" $(INC) -o "$(OBJROOT)/aml_generator.o"
acpi_patcher.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEFINES) -c "acpi_patcher.c" $(INC) -o "$(OBJROOT)/acpi_patcher.o"
include ../../MakeInc.dir
# dependencies
-include $(OBJROOT)/Makedep

Archive Download the corresponding diff file

Revision: 1126