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#include "acpidump.h"
45
46#define _COMPONENT ACPI_OS_SERVICES
47ACPI_MODULE_NAME("oslinuxtbl")
48
49#ifndef PATH_MAX
50#define PATH_MAX 256
51#endif
52
53typedef struct osl_table_info {
54 struct osl_table_info *next;
55 u32 instance;
56 char signature[ACPI_NAME_SIZE];
57
58} osl_table_info;
59
60
61
62static acpi_status osl_table_initialize(void);
63
64static acpi_status
65osl_table_name_from_file(char *filename, char *signature, u32 *instance);
66
67static acpi_status osl_add_table_to_list(char *signature, u32 instance);
68
69static acpi_status
70osl_read_table_from_file(char *filename,
71 acpi_size file_offset,
72 char *signature, struct acpi_table_header **table);
73
74static acpi_status
75osl_map_table(acpi_size address,
76 char *signature, struct acpi_table_header **table);
77
78static void osl_unmap_table(struct acpi_table_header *table);
79
80static acpi_physical_address
81osl_find_rsdp_via_efi_by_keyword(FILE * file, const char *keyword);
82
83static acpi_physical_address osl_find_rsdp_via_efi(void);
84
85static acpi_status osl_load_rsdp(void);
86
87static acpi_status osl_list_customized_tables(char *directory);
88
89static acpi_status
90osl_get_customized_table(char *pathname,
91 char *signature,
92 u32 instance,
93 struct acpi_table_header **table,
94 acpi_physical_address *address);
95
96static acpi_status osl_list_bios_tables(void);
97
98static acpi_status
99osl_get_bios_table(char *signature,
100 u32 instance,
101 struct acpi_table_header **table,
102 acpi_physical_address *address);
103
104static acpi_status osl_get_last_status(acpi_status default_status);
105
106
107
108#define DYNAMIC_TABLE_DIR "/sys/firmware/acpi/tables/dynamic"
109#define STATIC_TABLE_DIR "/sys/firmware/acpi/tables"
110#define EFI_SYSTAB "/sys/firmware/efi/systab"
111
112
113
114u8 gbl_dump_dynamic_tables = TRUE;
115
116
117
118u8 gbl_table_list_initialized = FALSE;
119
120
121
122struct acpi_table_rsdp gbl_rsdp;
123struct acpi_table_fadt *gbl_fadt = NULL;
124struct acpi_table_rsdt *gbl_rsdt = NULL;
125struct acpi_table_xsdt *gbl_xsdt = NULL;
126
127
128
129acpi_physical_address gbl_fadt_address = 0;
130acpi_physical_address gbl_rsdp_address = 0;
131
132
133
134u8 gbl_revision = 0;
135
136struct osl_table_info *gbl_table_list_head = NULL;
137u32 gbl_table_count = 0;
138
139
140
141
142
143
144
145
146
147
148
149
150
151static acpi_status osl_get_last_status(acpi_status default_status)
152{
153
154 switch (errno) {
155 case EACCES:
156 case EPERM:
157
158 return (AE_ACCESS);
159
160 case ENOENT:
161
162 return (AE_NOT_FOUND);
163
164 case ENOMEM:
165
166 return (AE_NO_MEMORY);
167
168 default:
169
170 return (default_status);
171 }
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188acpi_status
189acpi_os_get_table_by_address(acpi_physical_address address,
190 struct acpi_table_header **table)
191{
192 u32 table_length;
193 struct acpi_table_header *mapped_table;
194 struct acpi_table_header *local_table = NULL;
195 acpi_status status = AE_OK;
196
197
198
199 status = osl_table_initialize();
200 if (ACPI_FAILURE(status)) {
201 return (status);
202 }
203
204
205
206 status = osl_map_table(address, NULL, &mapped_table);
207 if (ACPI_FAILURE(status)) {
208 return (status);
209 }
210
211
212
213 table_length = ap_get_table_length(mapped_table);
214 if (table_length == 0) {
215 status = AE_BAD_HEADER;
216 goto exit;
217 }
218
219 local_table = calloc(1, table_length);
220 if (!local_table) {
221 status = AE_NO_MEMORY;
222 goto exit;
223 }
224
225 memcpy(local_table, mapped_table, table_length);
226
227exit:
228 osl_unmap_table(mapped_table);
229 *table = local_table;
230 return (status);
231}
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252acpi_status
253acpi_os_get_table_by_name(char *signature,
254 u32 instance,
255 struct acpi_table_header **table,
256 acpi_physical_address *address)
257{
258 acpi_status status;
259
260
261
262 status = osl_table_initialize();
263 if (ACPI_FAILURE(status)) {
264 return (status);
265 }
266
267
268
269 if (!gbl_dump_customized_tables) {
270
271
272
273 status =
274 osl_get_bios_table(signature, instance, table, address);
275 } else {
276
277
278 status = osl_get_customized_table(STATIC_TABLE_DIR, signature,
279 instance, table, address);
280 }
281
282 if (ACPI_FAILURE(status) && status == AE_LIMIT) {
283 if (gbl_dump_dynamic_tables) {
284
285
286
287 status =
288 osl_get_customized_table(DYNAMIC_TABLE_DIR,
289 signature, instance, table,
290 address);
291 }
292 }
293
294 return (status);
295}
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311static acpi_status osl_add_table_to_list(char *signature, u32 instance)
312{
313 struct osl_table_info *new_info;
314 struct osl_table_info *next;
315 u32 next_instance = 0;
316 u8 found = FALSE;
317
318 new_info = calloc(1, sizeof(struct osl_table_info));
319 if (!new_info) {
320 return (AE_NO_MEMORY);
321 }
322
323 ACPI_MOVE_NAME(new_info->signature, signature);
324
325 if (!gbl_table_list_head) {
326 gbl_table_list_head = new_info;
327 } else {
328 next = gbl_table_list_head;
329 while (1) {
330 if (ACPI_COMPARE_NAME(next->signature, signature)) {
331 if (next->instance == instance) {
332 found = TRUE;
333 }
334 if (next->instance >= next_instance) {
335 next_instance = next->instance + 1;
336 }
337 }
338
339 if (!next->next) {
340 break;
341 }
342 next = next->next;
343 }
344 next->next = new_info;
345 }
346
347 if (found) {
348 if (instance) {
349 fprintf(stderr,
350 "%4.4s: Warning unmatched table instance %d, expected %d\n",
351 signature, instance, next_instance);
352 }
353 instance = next_instance;
354 }
355
356 new_info->instance = instance;
357 gbl_table_count++;
358
359 return (AE_OK);
360}
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381acpi_status
382acpi_os_get_table_by_index(u32 index,
383 struct acpi_table_header **table,
384 u32 *instance, acpi_physical_address *address)
385{
386 struct osl_table_info *info;
387 acpi_status status;
388 u32 i;
389
390
391
392 status = osl_table_initialize();
393 if (ACPI_FAILURE(status)) {
394 return (status);
395 }
396
397
398
399 if (index >= gbl_table_count) {
400 return (AE_LIMIT);
401 }
402
403
404
405 info = gbl_table_list_head;
406 for (i = 0; i < index; i++) {
407 info = info->next;
408 }
409
410
411
412 status = acpi_os_get_table_by_name(info->signature, info->instance,
413 table, address);
414
415 if (ACPI_SUCCESS(status)) {
416 *instance = info->instance;
417 }
418 return (status);
419}
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435static acpi_physical_address
436osl_find_rsdp_via_efi_by_keyword(FILE * file, const char *keyword)
437{
438 char buffer[80];
439 unsigned long long address = 0;
440 char format[32];
441
442 snprintf(format, 32, "%s=%s", keyword, "%llx");
443 fseek(file, 0, SEEK_SET);
444 while (fgets(buffer, 80, file)) {
445 if (sscanf(buffer, format, &address) == 1) {
446 break;
447 }
448 }
449
450 return ((acpi_physical_address)(address));
451}
452
453
454
455
456
457
458
459
460
461
462
463
464
465static acpi_physical_address osl_find_rsdp_via_efi(void)
466{
467 FILE *file;
468 acpi_physical_address address = 0;
469
470 file = fopen(EFI_SYSTAB, "r");
471 if (file) {
472 address = osl_find_rsdp_via_efi_by_keyword(file, "ACPI20");
473 if (!address) {
474 address =
475 osl_find_rsdp_via_efi_by_keyword(file, "ACPI");
476 }
477 fclose(file);
478 }
479
480 return (address);
481}
482
483
484
485
486
487
488
489
490
491
492
493
494
495static acpi_status osl_load_rsdp(void)
496{
497 struct acpi_table_header *mapped_table;
498 u8 *rsdp_address;
499 acpi_physical_address rsdp_base;
500 acpi_size rsdp_size;
501
502
503
504 rsdp_size = sizeof(struct acpi_table_rsdp);
505 if (gbl_rsdp_base) {
506 rsdp_base = gbl_rsdp_base;
507 } else {
508 rsdp_base = osl_find_rsdp_via_efi();
509 }
510
511 if (!rsdp_base) {
512 rsdp_base = ACPI_HI_RSDP_WINDOW_BASE;
513 rsdp_size = ACPI_HI_RSDP_WINDOW_SIZE;
514 }
515
516 rsdp_address = acpi_os_map_memory(rsdp_base, rsdp_size);
517 if (!rsdp_address) {
518 return (osl_get_last_status(AE_BAD_ADDRESS));
519 }
520
521
522
523 mapped_table = ACPI_CAST_PTR(struct acpi_table_header,
524 acpi_tb_scan_memory_for_rsdp(rsdp_address,
525 rsdp_size));
526 if (!mapped_table) {
527 acpi_os_unmap_memory(rsdp_address, rsdp_size);
528 return (AE_NOT_FOUND);
529 }
530
531 gbl_rsdp_address =
532 rsdp_base + (ACPI_CAST8(mapped_table) - rsdp_address);
533
534 memcpy(&gbl_rsdp, mapped_table, sizeof(struct acpi_table_rsdp));
535 acpi_os_unmap_memory(rsdp_address, rsdp_size);
536
537 return (AE_OK);
538}
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553static u8 osl_can_use_xsdt(void)
554{
555 if (gbl_revision && !acpi_gbl_do_not_use_xsdt) {
556 return (TRUE);
557 } else {
558 return (FALSE);
559 }
560}
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576static acpi_status osl_table_initialize(void)
577{
578 acpi_status status;
579 acpi_physical_address address;
580
581 if (gbl_table_list_initialized) {
582 return (AE_OK);
583 }
584
585 if (!gbl_dump_customized_tables) {
586
587
588
589 status = osl_load_rsdp();
590 if (ACPI_FAILURE(status)) {
591 return (status);
592 }
593
594
595
596 if (gbl_rsdp.revision && !gbl_do_not_dump_xsdt) {
597 if (gbl_xsdt) {
598 free(gbl_xsdt);
599 gbl_xsdt = NULL;
600 }
601
602 gbl_revision = 2;
603 status = osl_get_bios_table(ACPI_SIG_XSDT, 0,
604 ACPI_CAST_PTR(struct
605 acpi_table_header
606 *, &gbl_xsdt),
607 &address);
608 if (ACPI_FAILURE(status)) {
609 return (status);
610 }
611 }
612
613
614
615 if (gbl_rsdp.rsdt_physical_address) {
616 if (gbl_rsdt) {
617 free(gbl_rsdt);
618 gbl_rsdt = NULL;
619 }
620
621 status = osl_get_bios_table(ACPI_SIG_RSDT, 0,
622 ACPI_CAST_PTR(struct
623 acpi_table_header
624 *, &gbl_rsdt),
625 &address);
626 if (ACPI_FAILURE(status)) {
627 return (status);
628 }
629 }
630
631
632
633 if (gbl_fadt) {
634 free(gbl_fadt);
635 gbl_fadt = NULL;
636 }
637
638 status = osl_get_bios_table(ACPI_SIG_FADT, 0,
639 ACPI_CAST_PTR(struct
640 acpi_table_header *,
641 &gbl_fadt),
642 &gbl_fadt_address);
643 if (ACPI_FAILURE(status)) {
644 return (status);
645 }
646
647
648
649 status = osl_add_table_to_list(ACPI_RSDP_NAME, 0);
650 if (ACPI_FAILURE(status)) {
651 return (status);
652 }
653
654 status = osl_add_table_to_list(ACPI_SIG_RSDT, 0);
655 if (ACPI_FAILURE(status)) {
656 return (status);
657 }
658
659 if (gbl_revision == 2) {
660 status = osl_add_table_to_list(ACPI_SIG_XSDT, 0);
661 if (ACPI_FAILURE(status)) {
662 return (status);
663 }
664 }
665
666 status = osl_add_table_to_list(ACPI_SIG_DSDT, 0);
667 if (ACPI_FAILURE(status)) {
668 return (status);
669 }
670
671 status = osl_add_table_to_list(ACPI_SIG_FACS, 0);
672 if (ACPI_FAILURE(status)) {
673 return (status);
674 }
675
676
677
678 status = osl_list_bios_tables();
679 if (ACPI_FAILURE(status)) {
680 return (status);
681 }
682 } else {
683
684
685 status = osl_list_customized_tables(STATIC_TABLE_DIR);
686 if (ACPI_FAILURE(status)) {
687 return (status);
688 }
689 }
690
691 if (gbl_dump_dynamic_tables) {
692
693
694
695 status = osl_list_customized_tables(DYNAMIC_TABLE_DIR);
696 if (ACPI_FAILURE(status)) {
697 return (status);
698 }
699 }
700
701 gbl_table_list_initialized = TRUE;
702 return (AE_OK);
703}
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720static acpi_status osl_list_bios_tables(void)
721{
722 struct acpi_table_header *mapped_table = NULL;
723 u8 *table_data;
724 u8 number_of_tables;
725 u8 item_size;
726 acpi_physical_address table_address = 0;
727 acpi_status status = AE_OK;
728 u32 i;
729
730 if (osl_can_use_xsdt()) {
731 item_size = sizeof(u64);
732 table_data =
733 ACPI_CAST8(gbl_xsdt) + sizeof(struct acpi_table_header);
734 number_of_tables =
735 (u8)((gbl_xsdt->header.length -
736 sizeof(struct acpi_table_header))
737 / item_size);
738 } else {
739
740 item_size = sizeof(u32);
741 table_data =
742 ACPI_CAST8(gbl_rsdt) + sizeof(struct acpi_table_header);
743 number_of_tables =
744 (u8)((gbl_rsdt->header.length -
745 sizeof(struct acpi_table_header))
746 / item_size);
747 }
748
749
750
751 for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
752 if (osl_can_use_xsdt()) {
753 table_address =
754 (acpi_physical_address)(*ACPI_CAST64(table_data));
755 } else {
756 table_address =
757 (acpi_physical_address)(*ACPI_CAST32(table_data));
758 }
759
760
761
762 if (table_address == 0) {
763 continue;
764 }
765
766 status = osl_map_table(table_address, NULL, &mapped_table);
767 if (ACPI_FAILURE(status)) {
768 return (status);
769 }
770
771 osl_add_table_to_list(mapped_table->signature, 0);
772 osl_unmap_table(mapped_table);
773 }
774
775 return (AE_OK);
776}
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799static acpi_status
800osl_get_bios_table(char *signature,
801 u32 instance,
802 struct acpi_table_header **table,
803 acpi_physical_address *address)
804{
805 struct acpi_table_header *local_table = NULL;
806 struct acpi_table_header *mapped_table = NULL;
807 u8 *table_data;
808 u8 number_of_tables;
809 u8 item_size;
810 u32 current_instance = 0;
811 acpi_physical_address table_address;
812 acpi_physical_address first_table_address = 0;
813 u32 table_length = 0;
814 acpi_status status = AE_OK;
815 u32 i;
816
817
818
819 if (ACPI_COMPARE_NAME(signature, ACPI_RSDP_NAME) ||
820 ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT) ||
821 ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT) ||
822 ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT) ||
823 ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
824
825find_next_instance:
826
827 table_address = 0;
828
829
830
831
832
833
834 if (ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT)) {
835 if (current_instance < 2) {
836 if ((gbl_fadt->header.length >=
837 MIN_FADT_FOR_XDSDT) && gbl_fadt->Xdsdt
838 && current_instance == 0) {
839 table_address =
840 (acpi_physical_address)gbl_fadt->
841 Xdsdt;
842 } else
843 if ((gbl_fadt->header.length >=
844 MIN_FADT_FOR_DSDT)
845 && gbl_fadt->dsdt !=
846 first_table_address) {
847 table_address =
848 (acpi_physical_address)gbl_fadt->
849 dsdt;
850 }
851 }
852 } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
853 if (current_instance < 2) {
854 if ((gbl_fadt->header.length >=
855 MIN_FADT_FOR_XFACS) && gbl_fadt->Xfacs
856 && current_instance == 0) {
857 table_address =
858 (acpi_physical_address)gbl_fadt->
859 Xfacs;
860 } else
861 if ((gbl_fadt->header.length >=
862 MIN_FADT_FOR_FACS)
863 && gbl_fadt->facs !=
864 first_table_address) {
865 table_address =
866 (acpi_physical_address)gbl_fadt->
867 facs;
868 }
869 }
870 } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT)) {
871 if (!gbl_revision) {
872 return (AE_BAD_SIGNATURE);
873 }
874 if (current_instance == 0) {
875 table_address =
876 (acpi_physical_address)gbl_rsdp.
877 xsdt_physical_address;
878 }
879 } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT)) {
880 if (current_instance == 0) {
881 table_address =
882 (acpi_physical_address)gbl_rsdp.
883 rsdt_physical_address;
884 }
885 } else {
886 if (current_instance == 0) {
887 table_address =
888 (acpi_physical_address)gbl_rsdp_address;
889 signature = ACPI_SIG_RSDP;
890 }
891 }
892
893 if (table_address == 0) {
894 goto exit_find_table;
895 }
896
897
898
899 status = osl_map_table(table_address, signature, &mapped_table);
900 if (ACPI_FAILURE(status)) {
901 return (status);
902 }
903
904 table_length = ap_get_table_length(mapped_table);
905 if (first_table_address == 0) {
906 first_table_address = table_address;
907 }
908
909
910
911 if (current_instance != instance) {
912 osl_unmap_table(mapped_table);
913 mapped_table = NULL;
914 current_instance++;
915 goto find_next_instance;
916 }
917 } else {
918
919 if (osl_can_use_xsdt()) {
920 item_size = sizeof(u64);
921 table_data =
922 ACPI_CAST8(gbl_xsdt) +
923 sizeof(struct acpi_table_header);
924 number_of_tables =
925 (u8)((gbl_xsdt->header.length -
926 sizeof(struct acpi_table_header))
927 / item_size);
928 } else {
929
930 item_size = sizeof(u32);
931 table_data =
932 ACPI_CAST8(gbl_rsdt) +
933 sizeof(struct acpi_table_header);
934 number_of_tables =
935 (u8)((gbl_rsdt->header.length -
936 sizeof(struct acpi_table_header))
937 / item_size);
938 }
939
940
941
942 for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
943 if (osl_can_use_xsdt()) {
944 table_address =
945 (acpi_physical_address)(*ACPI_CAST64
946 (table_data));
947 } else {
948 table_address =
949 (acpi_physical_address)(*ACPI_CAST32
950 (table_data));
951 }
952
953
954
955 if (table_address == 0) {
956 continue;
957 }
958
959 status =
960 osl_map_table(table_address, NULL, &mapped_table);
961 if (ACPI_FAILURE(status)) {
962 return (status);
963 }
964 table_length = mapped_table->length;
965
966
967
968 if (!ACPI_COMPARE_NAME
969 (mapped_table->signature, signature)) {
970 osl_unmap_table(mapped_table);
971 mapped_table = NULL;
972 continue;
973 }
974
975
976
977 if (current_instance != instance) {
978 osl_unmap_table(mapped_table);
979 mapped_table = NULL;
980 current_instance++;
981 continue;
982 }
983
984 break;
985 }
986 }
987
988exit_find_table:
989
990 if (!mapped_table) {
991 return (AE_LIMIT);
992 }
993
994 if (table_length == 0) {
995 status = AE_BAD_HEADER;
996 goto exit;
997 }
998
999
1000
1001 local_table = calloc(1, table_length);
1002 if (!local_table) {
1003 status = AE_NO_MEMORY;
1004 goto exit;
1005 }
1006
1007 memcpy(local_table, mapped_table, table_length);
1008 *address = table_address;
1009 *table = local_table;
1010
1011exit:
1012 osl_unmap_table(mapped_table);
1013 return (status);
1014}
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028static acpi_status osl_list_customized_tables(char *directory)
1029{
1030 void *table_dir;
1031 u32 instance;
1032 char temp_name[ACPI_NAME_SIZE];
1033 char *filename;
1034 acpi_status status = AE_OK;
1035
1036
1037
1038 table_dir = acpi_os_open_directory(directory, "*", REQUEST_FILE_ONLY);
1039 if (!table_dir) {
1040 return (osl_get_last_status(AE_NOT_FOUND));
1041 }
1042
1043
1044
1045 while ((filename = acpi_os_get_next_filename(table_dir))) {
1046
1047
1048
1049 status =
1050 osl_table_name_from_file(filename, temp_name, &instance);
1051
1052
1053
1054 if (ACPI_FAILURE(status)) {
1055 continue;
1056 }
1057
1058
1059
1060 status = osl_add_table_to_list(temp_name, instance);
1061 if (ACPI_FAILURE(status)) {
1062 break;
1063 }
1064 }
1065
1066 acpi_os_close_directory(table_dir);
1067 return (status);
1068}
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087static acpi_status
1088osl_map_table(acpi_size address,
1089 char *signature, struct acpi_table_header **table)
1090{
1091 struct acpi_table_header *mapped_table;
1092 u32 length;
1093
1094 if (!address) {
1095 return (AE_BAD_ADDRESS);
1096 }
1097
1098
1099
1100
1101
1102
1103
1104 mapped_table =
1105 acpi_os_map_memory(address, sizeof(struct acpi_table_header));
1106 if (!mapped_table) {
1107 fprintf(stderr, "Could not map table header at 0x%8.8X%8.8X\n",
1108 ACPI_FORMAT_UINT64(address));
1109 return (osl_get_last_status(AE_BAD_ADDRESS));
1110 }
1111
1112
1113
1114 if (signature) {
1115 if (ACPI_VALIDATE_RSDP_SIG(signature)) {
1116 if (!ACPI_VALIDATE_RSDP_SIG(mapped_table->signature)) {
1117 acpi_os_unmap_memory(mapped_table,
1118 sizeof(struct
1119 acpi_table_header));
1120 return (AE_BAD_SIGNATURE);
1121 }
1122 } else
1123 if (!ACPI_COMPARE_NAME(signature, mapped_table->signature))
1124 {
1125 acpi_os_unmap_memory(mapped_table,
1126 sizeof(struct acpi_table_header));
1127 return (AE_BAD_SIGNATURE);
1128 }
1129 }
1130
1131
1132
1133 length = ap_get_table_length(mapped_table);
1134 acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));
1135 if (length == 0) {
1136 return (AE_BAD_HEADER);
1137 }
1138
1139 mapped_table = acpi_os_map_memory(address, length);
1140 if (!mapped_table) {
1141 fprintf(stderr,
1142 "Could not map table at 0x%8.8X%8.8X length %8.8X\n",
1143 ACPI_FORMAT_UINT64(address), length);
1144 return (osl_get_last_status(AE_INVALID_TABLE_LENGTH));
1145 }
1146
1147 (void)ap_is_valid_checksum(mapped_table);
1148
1149 *table = mapped_table;
1150 return (AE_OK);
1151}
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165static void osl_unmap_table(struct acpi_table_header *table)
1166{
1167 if (table) {
1168 acpi_os_unmap_memory(table, ap_get_table_length(table));
1169 }
1170}
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189static acpi_status
1190osl_table_name_from_file(char *filename, char *signature, u32 *instance)
1191{
1192
1193
1194
1195 if (strlen(filename) < ACPI_NAME_SIZE) {
1196 return (AE_BAD_SIGNATURE);
1197 }
1198
1199
1200
1201 if (isdigit((int)filename[ACPI_NAME_SIZE])) {
1202 sscanf(&filename[ACPI_NAME_SIZE], "%u", instance);
1203 } else if (strlen(filename) != ACPI_NAME_SIZE) {
1204 return (AE_BAD_SIGNATURE);
1205 } else {
1206 *instance = 0;
1207 }
1208
1209
1210
1211 ACPI_MOVE_NAME(signature, filename);
1212 return (AE_OK);
1213}
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231static acpi_status
1232osl_read_table_from_file(char *filename,
1233 acpi_size file_offset,
1234 char *signature, struct acpi_table_header **table)
1235{
1236 FILE *table_file;
1237 struct acpi_table_header header;
1238 struct acpi_table_header *local_table = NULL;
1239 u32 table_length;
1240 s32 count;
1241 acpi_status status = AE_OK;
1242
1243
1244
1245 table_file = fopen(filename, "rb");
1246 if (table_file == NULL) {
1247 fprintf(stderr, "Could not open table file: %s\n", filename);
1248 return (osl_get_last_status(AE_NOT_FOUND));
1249 }
1250
1251 fseek(table_file, file_offset, SEEK_SET);
1252
1253
1254
1255 count = fread(&header, 1, sizeof(struct acpi_table_header), table_file);
1256 if (count != sizeof(struct acpi_table_header)) {
1257 fprintf(stderr, "Could not read table header: %s\n", filename);
1258 status = AE_BAD_HEADER;
1259 goto exit;
1260 }
1261
1262
1263
1264 if (signature) {
1265 if (ACPI_VALIDATE_RSDP_SIG(signature)) {
1266 if (!ACPI_VALIDATE_RSDP_SIG(header.signature)) {
1267 fprintf(stderr,
1268 "Incorrect RSDP signature: found %8.8s\n",
1269 header.signature);
1270 status = AE_BAD_SIGNATURE;
1271 goto exit;
1272 }
1273 } else if (!ACPI_COMPARE_NAME(signature, header.signature)) {
1274 fprintf(stderr,
1275 "Incorrect signature: Expecting %4.4s, found %4.4s\n",
1276 signature, header.signature);
1277 status = AE_BAD_SIGNATURE;
1278 goto exit;
1279 }
1280 }
1281
1282 table_length = ap_get_table_length(&header);
1283 if (table_length == 0) {
1284 status = AE_BAD_HEADER;
1285 goto exit;
1286 }
1287
1288
1289
1290 local_table = calloc(1, table_length);
1291 if (!local_table) {
1292 fprintf(stderr,
1293 "%4.4s: Could not allocate buffer for table of length %X\n",
1294 header.signature, table_length);
1295 status = AE_NO_MEMORY;
1296 goto exit;
1297 }
1298
1299 fseek(table_file, file_offset, SEEK_SET);
1300
1301 count = fread(local_table, 1, table_length, table_file);
1302 if (count != table_length) {
1303 fprintf(stderr, "%4.4s: Could not read table content\n",
1304 header.signature);
1305 status = AE_INVALID_TABLE_LENGTH;
1306 goto exit;
1307 }
1308
1309
1310
1311 (void)ap_is_valid_checksum(local_table);
1312
1313exit:
1314 fclose(table_file);
1315 *table = local_table;
1316 return (status);
1317}
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339static acpi_status
1340osl_get_customized_table(char *pathname,
1341 char *signature,
1342 u32 instance,
1343 struct acpi_table_header **table,
1344 acpi_physical_address *address)
1345{
1346 void *table_dir;
1347 u32 current_instance = 0;
1348 char temp_name[ACPI_NAME_SIZE];
1349 char table_filename[PATH_MAX];
1350 char *filename;
1351 acpi_status status;
1352
1353
1354
1355 table_dir = acpi_os_open_directory(pathname, "*", REQUEST_FILE_ONLY);
1356 if (!table_dir) {
1357 return (osl_get_last_status(AE_NOT_FOUND));
1358 }
1359
1360
1361
1362 while ((filename = acpi_os_get_next_filename(table_dir))) {
1363
1364
1365
1366 if (!ACPI_COMPARE_NAME(filename, signature)) {
1367 continue;
1368 }
1369
1370
1371
1372 status =
1373 osl_table_name_from_file(filename, temp_name,
1374 ¤t_instance);
1375
1376
1377
1378 if (ACPI_FAILURE(status) || current_instance != instance) {
1379 continue;
1380 }
1381
1382
1383
1384 if (instance != 0) {
1385 sprintf(table_filename, "%s/%4.4s%d", pathname,
1386 temp_name, instance);
1387 } else {
1388 sprintf(table_filename, "%s/%4.4s", pathname,
1389 temp_name);
1390 }
1391 break;
1392 }
1393
1394 acpi_os_close_directory(table_dir);
1395
1396 if (!filename) {
1397 return (AE_LIMIT);
1398 }
1399
1400
1401
1402 *address = 0;
1403 status = osl_read_table_from_file(table_filename, 0, NULL, table);
1404
1405 return (status);
1406}
1407