1 /******************************************************************************
3 * Module Name: oslinuxtbl - Linux OSL for obtaining ACPI tables
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
46 #define _COMPONENT ACPI_OS_SERVICES
47 ACPI_MODULE_NAME("oslinuxtbl")
52 /* List of information about obtained ACPI tables */
53 typedef struct osl_table_info {
54 struct osl_table_info *next;
56 char signature[ACPI_NAME_SIZE];
60 /* Local prototypes */
62 static acpi_status osl_table_initialize(void);
65 osl_table_name_from_file(char *filename, char *signature, u32 *instance);
67 static acpi_status osl_add_table_to_list(char *signature, u32 instance);
70 osl_read_table_from_file(char *filename,
71 acpi_size file_offset,
72 char *signature, struct acpi_table_header **table);
75 osl_map_table(acpi_size address,
76 char *signature, struct acpi_table_header **table);
78 static void osl_unmap_table(struct acpi_table_header *table);
80 static acpi_physical_address
81 osl_find_rsdp_via_efi_by_keyword(FILE * file, const char *keyword);
83 static acpi_physical_address osl_find_rsdp_via_efi(void);
85 static acpi_status osl_load_rsdp(void);
87 static acpi_status osl_list_customized_tables(char *directory);
90 osl_get_customized_table(char *pathname,
93 struct acpi_table_header **table,
94 acpi_physical_address * address);
96 static acpi_status osl_list_bios_tables(void);
99 osl_get_bios_table(char *signature,
101 struct acpi_table_header **table,
102 acpi_physical_address * address);
104 static acpi_status osl_get_last_status(acpi_status default_status);
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"
112 /* Should we get dynamically loaded SSDTs from DYNAMIC_TABLE_DIR? */
114 u8 gbl_dump_dynamic_tables = TRUE;
116 /* Initialization flags */
118 u8 gbl_table_list_initialized = FALSE;
120 /* Local copies of main ACPI tables */
122 struct acpi_table_rsdp gbl_rsdp;
123 struct acpi_table_fadt *gbl_fadt = NULL;
124 struct acpi_table_rsdt *gbl_rsdt = NULL;
125 struct acpi_table_xsdt *gbl_xsdt = NULL;
127 /* Table addresses */
129 acpi_physical_address gbl_fadt_address = 0;
130 acpi_physical_address gbl_rsdp_address = 0;
132 /* Revision of RSD PTR */
136 struct osl_table_info *gbl_table_list_head = NULL;
137 u32 gbl_table_count = 0;
139 /******************************************************************************
141 * FUNCTION: osl_get_last_status
143 * PARAMETERS: default_status - Default error status to return
145 * RETURN: Status; Converted from errno.
147 * DESCRIPTION: Get last errno and conver it to acpi_status.
149 *****************************************************************************/
151 static acpi_status osl_get_last_status(acpi_status default_status)
162 return (AE_NOT_FOUND);
166 return (AE_NO_MEMORY);
170 return (default_status);
174 /******************************************************************************
176 * FUNCTION: acpi_os_get_table_by_address
178 * PARAMETERS: address - Physical address of the ACPI table
179 * table - Where a pointer to the table is returned
181 * RETURN: Status; Table buffer is returned if AE_OK.
182 * AE_NOT_FOUND: A valid table was not found at the address
184 * DESCRIPTION: Get an ACPI table via a physical memory address.
186 *****************************************************************************/
189 acpi_os_get_table_by_address(acpi_physical_address address,
190 struct acpi_table_header ** table)
193 struct acpi_table_header *mapped_table;
194 struct acpi_table_header *local_table = NULL;
195 acpi_status status = AE_OK;
197 /* Get main ACPI tables from memory on first invocation of this function */
199 status = osl_table_initialize();
200 if (ACPI_FAILURE(status)) {
204 /* Map the table and validate it */
206 status = osl_map_table(address, NULL, &mapped_table);
207 if (ACPI_FAILURE(status)) {
211 /* Copy table to local buffer and return it */
213 table_length = ap_get_table_length(mapped_table);
214 if (table_length == 0) {
215 status = AE_BAD_HEADER;
219 local_table = calloc(1, table_length);
221 status = AE_NO_MEMORY;
225 memcpy(local_table, mapped_table, table_length);
228 osl_unmap_table(mapped_table);
229 *table = local_table;
233 /******************************************************************************
235 * FUNCTION: acpi_os_get_table_by_name
237 * PARAMETERS: signature - ACPI Signature for desired table. Must be
238 * a null terminated 4-character string.
239 * instance - Multiple table support for SSDT/UEFI (0...n)
240 * Must be 0 for other tables.
241 * table - Where a pointer to the table is returned
242 * address - Where the table physical address is returned
244 * RETURN: Status; Table buffer and physical address returned if AE_OK.
245 * AE_LIMIT: Instance is beyond valid limit
246 * AE_NOT_FOUND: A table with the signature was not found
248 * NOTE: Assumes the input signature is uppercase.
250 *****************************************************************************/
253 acpi_os_get_table_by_name(char *signature,
255 struct acpi_table_header ** table,
256 acpi_physical_address * address)
260 /* Get main ACPI tables from memory on first invocation of this function */
262 status = osl_table_initialize();
263 if (ACPI_FAILURE(status)) {
267 /* Not a main ACPI table, attempt to extract it from the RSDT/XSDT */
269 if (!gbl_dump_customized_tables) {
271 /* Attempt to get the table from the memory */
274 osl_get_bios_table(signature, instance, table, address);
276 /* Attempt to get the table from the static directory */
278 status = osl_get_customized_table(STATIC_TABLE_DIR, signature,
279 instance, table, address);
282 if (ACPI_FAILURE(status) && status == AE_LIMIT) {
283 if (gbl_dump_dynamic_tables) {
285 /* Attempt to get a dynamic table */
288 osl_get_customized_table(DYNAMIC_TABLE_DIR,
289 signature, instance, table,
297 /******************************************************************************
299 * FUNCTION: osl_add_table_to_list
301 * PARAMETERS: signature - Table signature
302 * instance - Table instance
304 * RETURN: Status; Successfully added if AE_OK.
305 * AE_NO_MEMORY: Memory allocation error
307 * DESCRIPTION: Insert a table structure into OSL table list.
309 *****************************************************************************/
311 static acpi_status osl_add_table_to_list(char *signature, u32 instance)
313 struct osl_table_info *new_info;
314 struct osl_table_info *next;
315 u32 next_instance = 0;
318 new_info = calloc(1, sizeof(struct osl_table_info));
320 return (AE_NO_MEMORY);
323 ACPI_MOVE_NAME(new_info->signature, signature);
325 if (!gbl_table_list_head) {
326 gbl_table_list_head = new_info;
328 next = gbl_table_list_head;
330 if (ACPI_COMPARE_NAME(next->signature, signature)) {
331 if (next->instance == instance) {
334 if (next->instance >= next_instance) {
335 next_instance = next->instance + 1;
344 next->next = new_info;
350 "%4.4s: Warning unmatched table instance %d, expected %d\n",
351 signature, instance, next_instance);
353 instance = next_instance;
356 new_info->instance = instance;
362 /******************************************************************************
364 * FUNCTION: acpi_os_get_table_by_index
366 * PARAMETERS: index - Which table to get
367 * table - Where a pointer to the table is returned
368 * instance - Where a pointer to the table instance no. is
370 * address - Where the table physical address is returned
372 * RETURN: Status; Table buffer and physical address returned if AE_OK.
373 * AE_LIMIT: Index is beyond valid limit
375 * DESCRIPTION: Get an ACPI table via an index value (0 through n). Returns
376 * AE_LIMIT when an invalid index is reached. Index is not
377 * necessarily an index into the RSDT/XSDT.
379 *****************************************************************************/
382 acpi_os_get_table_by_index(u32 index,
383 struct acpi_table_header ** table,
384 u32 *instance, acpi_physical_address * address)
386 struct osl_table_info *info;
390 /* Get main ACPI tables from memory on first invocation of this function */
392 status = osl_table_initialize();
393 if (ACPI_FAILURE(status)) {
399 if (index >= gbl_table_count) {
403 /* Point to the table list entry specified by the Index argument */
405 info = gbl_table_list_head;
406 for (i = 0; i < index; i++) {
410 /* Now we can just get the table via the signature */
412 status = acpi_os_get_table_by_name(info->signature, info->instance,
415 if (ACPI_SUCCESS(status)) {
416 *instance = info->instance;
421 /******************************************************************************
423 * FUNCTION: osl_find_rsdp_via_efi_by_keyword
425 * PARAMETERS: keyword - Character string indicating ACPI GUID version
428 * RETURN: RSDP address if found
430 * DESCRIPTION: Find RSDP address via EFI using keyword indicating the ACPI
433 *****************************************************************************/
435 static acpi_physical_address
436 osl_find_rsdp_via_efi_by_keyword(FILE * file, const char *keyword)
439 unsigned long long address = 0;
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) {
450 return ((acpi_physical_address) (address));
453 /******************************************************************************
455 * FUNCTION: osl_find_rsdp_via_efi
459 * RETURN: RSDP address if found
461 * DESCRIPTION: Find RSDP address via EFI.
463 *****************************************************************************/
465 static acpi_physical_address osl_find_rsdp_via_efi(void)
468 acpi_physical_address address = 0;
470 file = fopen(EFI_SYSTAB, "r");
472 address = osl_find_rsdp_via_efi_by_keyword(file, "ACPI20");
475 osl_find_rsdp_via_efi_by_keyword(file, "ACPI");
483 /******************************************************************************
485 * FUNCTION: osl_load_rsdp
491 * DESCRIPTION: Scan and load RSDP.
493 *****************************************************************************/
495 static acpi_status osl_load_rsdp(void)
497 struct acpi_table_header *mapped_table;
499 acpi_physical_address rsdp_base;
502 /* Get RSDP from memory */
504 rsdp_size = sizeof(struct acpi_table_rsdp);
506 rsdp_base = gbl_rsdp_base;
508 rsdp_base = osl_find_rsdp_via_efi();
512 rsdp_base = ACPI_HI_RSDP_WINDOW_BASE;
513 rsdp_size = ACPI_HI_RSDP_WINDOW_SIZE;
516 rsdp_address = acpi_os_map_memory(rsdp_base, rsdp_size);
518 return (osl_get_last_status(AE_BAD_ADDRESS));
521 /* Search low memory for the RSDP */
523 mapped_table = ACPI_CAST_PTR(struct acpi_table_header,
524 acpi_tb_scan_memory_for_rsdp(rsdp_address,
527 acpi_os_unmap_memory(rsdp_address, rsdp_size);
528 return (AE_NOT_FOUND);
532 rsdp_base + (ACPI_CAST8(mapped_table) - rsdp_address);
534 memcpy(&gbl_rsdp, mapped_table, sizeof(struct acpi_table_rsdp));
535 acpi_os_unmap_memory(rsdp_address, rsdp_size);
540 /******************************************************************************
542 * FUNCTION: osl_can_use_xsdt
546 * RETURN: TRUE if XSDT is allowed to be used.
548 * DESCRIPTION: This function collects logic that can be used to determine if
549 * XSDT should be used instead of RSDT.
551 *****************************************************************************/
553 static u8 osl_can_use_xsdt(void)
555 if (gbl_revision && !acpi_gbl_do_not_use_xsdt) {
562 /******************************************************************************
564 * FUNCTION: osl_table_initialize
570 * DESCRIPTION: Initialize ACPI table data. Get and store main ACPI tables to
571 * local variables. Main ACPI tables include RSDT, FADT, RSDT,
574 *****************************************************************************/
576 static acpi_status osl_table_initialize(void)
579 acpi_physical_address address;
581 if (gbl_table_list_initialized) {
585 if (!gbl_dump_customized_tables) {
587 /* Get RSDP from memory */
589 status = osl_load_rsdp();
590 if (ACPI_FAILURE(status)) {
594 /* Get XSDT from memory */
596 if (gbl_rsdp.revision && !gbl_do_not_dump_xsdt) {
603 status = osl_get_bios_table(ACPI_SIG_XSDT, 0,
608 if (ACPI_FAILURE(status)) {
613 /* Get RSDT from memory */
615 if (gbl_rsdp.rsdt_physical_address) {
621 status = osl_get_bios_table(ACPI_SIG_RSDT, 0,
626 if (ACPI_FAILURE(status)) {
631 /* Get FADT from memory */
638 status = osl_get_bios_table(ACPI_SIG_FADT, 0,
643 if (ACPI_FAILURE(status)) {
647 /* Add mandatory tables to global table list first */
649 status = osl_add_table_to_list(ACPI_RSDP_NAME, 0);
650 if (ACPI_FAILURE(status)) {
654 status = osl_add_table_to_list(ACPI_SIG_RSDT, 0);
655 if (ACPI_FAILURE(status)) {
659 if (gbl_revision == 2) {
660 status = osl_add_table_to_list(ACPI_SIG_XSDT, 0);
661 if (ACPI_FAILURE(status)) {
666 status = osl_add_table_to_list(ACPI_SIG_DSDT, 0);
667 if (ACPI_FAILURE(status)) {
671 status = osl_add_table_to_list(ACPI_SIG_FACS, 0);
672 if (ACPI_FAILURE(status)) {
676 /* Add all tables found in the memory */
678 status = osl_list_bios_tables();
679 if (ACPI_FAILURE(status)) {
683 /* Add all tables found in the static directory */
685 status = osl_list_customized_tables(STATIC_TABLE_DIR);
686 if (ACPI_FAILURE(status)) {
691 if (gbl_dump_dynamic_tables) {
693 /* Add all dynamically loaded tables in the dynamic directory */
695 status = osl_list_customized_tables(DYNAMIC_TABLE_DIR);
696 if (ACPI_FAILURE(status)) {
701 gbl_table_list_initialized = TRUE;
705 /******************************************************************************
707 * FUNCTION: osl_list_bios_tables
711 * RETURN: Status; Table list is initialized if AE_OK.
713 * DESCRIPTION: Add ACPI tables to the table list from memory.
715 * NOTE: This works on Linux as table customization does not modify the
716 * addresses stored in RSDP/RSDT/XSDT/FADT.
718 *****************************************************************************/
720 static acpi_status osl_list_bios_tables(void)
722 struct acpi_table_header *mapped_table = NULL;
726 acpi_physical_address table_address = 0;
727 acpi_status status = AE_OK;
730 if (osl_can_use_xsdt()) {
731 item_size = sizeof(u64);
733 ACPI_CAST8(gbl_xsdt) + sizeof(struct acpi_table_header);
735 (u8)((gbl_xsdt->header.length -
736 sizeof(struct acpi_table_header))
738 } else { /* Use RSDT if XSDT is not available */
740 item_size = sizeof(u32);
742 ACPI_CAST8(gbl_rsdt) + sizeof(struct acpi_table_header);
744 (u8)((gbl_rsdt->header.length -
745 sizeof(struct acpi_table_header))
749 /* Search RSDT/XSDT for the requested table */
751 for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
752 if (osl_can_use_xsdt()) {
754 (acpi_physical_address) (*ACPI_CAST64(table_data));
757 (acpi_physical_address) (*ACPI_CAST32(table_data));
760 /* Skip NULL entries in RSDT/XSDT */
762 if (!table_address) {
766 status = osl_map_table(table_address, NULL, &mapped_table);
767 if (ACPI_FAILURE(status)) {
771 osl_add_table_to_list(mapped_table->signature, 0);
772 osl_unmap_table(mapped_table);
778 /******************************************************************************
780 * FUNCTION: osl_get_bios_table
782 * PARAMETERS: signature - ACPI Signature for common table. Must be
783 * a null terminated 4-character string.
784 * instance - Multiple table support for SSDT/UEFI (0...n)
785 * Must be 0 for other tables.
786 * table - Where a pointer to the table is returned
787 * address - Where the table physical address is returned
789 * RETURN: Status; Table buffer and physical address returned if AE_OK.
790 * AE_LIMIT: Instance is beyond valid limit
791 * AE_NOT_FOUND: A table with the signature was not found
793 * DESCRIPTION: Get a BIOS provided ACPI table
795 * NOTE: Assumes the input signature is uppercase.
797 *****************************************************************************/
800 osl_get_bios_table(char *signature,
802 struct acpi_table_header **table,
803 acpi_physical_address * address)
805 struct acpi_table_header *local_table = NULL;
806 struct acpi_table_header *mapped_table = NULL;
810 u32 current_instance = 0;
811 acpi_physical_address table_address = 0;
812 u32 table_length = 0;
813 acpi_status status = AE_OK;
816 /* Handle special tables whose addresses are not in RSDT/XSDT */
818 if (ACPI_COMPARE_NAME(signature, ACPI_RSDP_NAME) ||
819 ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT) ||
820 ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT) ||
821 ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT) ||
822 ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
828 * Get the appropriate address, either 32-bit or 64-bit. Be very
829 * careful about the FADT length and validate table addresses.
830 * Note: The 64-bit addresses have priority.
832 if (ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT)) {
833 if ((gbl_fadt->header.length >= MIN_FADT_FOR_XDSDT) &&
836 (acpi_physical_address) gbl_fadt->Xdsdt;
838 if ((gbl_fadt->header.length >= MIN_FADT_FOR_DSDT)
841 (acpi_physical_address) gbl_fadt->dsdt;
843 } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
844 if ((gbl_fadt->header.length >= MIN_FADT_FOR_XFACS) &&
847 (acpi_physical_address) gbl_fadt->Xfacs;
849 if ((gbl_fadt->header.length >= MIN_FADT_FOR_FACS)
852 (acpi_physical_address) gbl_fadt->facs;
854 } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT)) {
856 return (AE_BAD_SIGNATURE);
859 (acpi_physical_address) gbl_rsdp.
860 xsdt_physical_address;
861 } else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT)) {
863 (acpi_physical_address) gbl_rsdp.
864 rsdt_physical_address;
867 (acpi_physical_address) gbl_rsdp_address;
868 signature = ACPI_SIG_RSDP;
871 /* Now we can get the requested special table */
873 status = osl_map_table(table_address, signature, &mapped_table);
874 if (ACPI_FAILURE(status)) {
878 table_length = ap_get_table_length(mapped_table);
879 } else { /* Case for a normal ACPI table */
881 if (osl_can_use_xsdt()) {
882 item_size = sizeof(u64);
884 ACPI_CAST8(gbl_xsdt) +
885 sizeof(struct acpi_table_header);
887 (u8)((gbl_xsdt->header.length -
888 sizeof(struct acpi_table_header))
890 } else { /* Use RSDT if XSDT is not available */
892 item_size = sizeof(u32);
894 ACPI_CAST8(gbl_rsdt) +
895 sizeof(struct acpi_table_header);
897 (u8)((gbl_rsdt->header.length -
898 sizeof(struct acpi_table_header))
902 /* Search RSDT/XSDT for the requested table */
904 for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
905 if (osl_can_use_xsdt()) {
907 (acpi_physical_address) (*ACPI_CAST64
911 (acpi_physical_address) (*ACPI_CAST32
915 /* Skip NULL entries in RSDT/XSDT */
917 if (!table_address) {
922 osl_map_table(table_address, NULL, &mapped_table);
923 if (ACPI_FAILURE(status)) {
926 table_length = mapped_table->length;
928 /* Does this table match the requested signature? */
930 if (!ACPI_COMPARE_NAME
931 (mapped_table->signature, signature)) {
932 osl_unmap_table(mapped_table);
937 /* Match table instance (for SSDT/UEFI tables) */
939 if (current_instance != instance) {
940 osl_unmap_table(mapped_table);
954 if (table_length == 0) {
955 status = AE_BAD_HEADER;
959 /* Copy table to local buffer and return it */
961 local_table = calloc(1, table_length);
963 status = AE_NO_MEMORY;
967 memcpy(local_table, mapped_table, table_length);
968 *address = table_address;
969 *table = local_table;
972 osl_unmap_table(mapped_table);
976 /******************************************************************************
978 * FUNCTION: osl_list_customized_tables
980 * PARAMETERS: directory - Directory that contains the tables
982 * RETURN: Status; Table list is initialized if AE_OK.
984 * DESCRIPTION: Add ACPI tables to the table list from a directory.
986 *****************************************************************************/
988 static acpi_status osl_list_customized_tables(char *directory)
992 char temp_name[ACPI_NAME_SIZE];
994 acpi_status status = AE_OK;
996 /* Open the requested directory */
998 table_dir = acpi_os_open_directory(directory, "*", REQUEST_FILE_ONLY);
1000 return (osl_get_last_status(AE_NOT_FOUND));
1003 /* Examine all entries in this directory */
1005 while ((filename = acpi_os_get_next_filename(table_dir))) {
1007 /* Extract table name and instance number */
1010 osl_table_name_from_file(filename, temp_name, &instance);
1012 /* Ignore meaningless files */
1014 if (ACPI_FAILURE(status)) {
1018 /* Add new info node to global table list */
1020 status = osl_add_table_to_list(temp_name, instance);
1021 if (ACPI_FAILURE(status)) {
1026 acpi_os_close_directory(table_dir);
1030 /******************************************************************************
1032 * FUNCTION: osl_map_table
1034 * PARAMETERS: address - Address of the table in memory
1035 * signature - Optional ACPI Signature for desired table.
1036 * Null terminated 4-character string.
1037 * table - Where a pointer to the mapped table is
1040 * RETURN: Status; Mapped table is returned if AE_OK.
1041 * AE_NOT_FOUND: A valid table was not found at the address
1043 * DESCRIPTION: Map entire ACPI table into caller's address space.
1045 *****************************************************************************/
1048 osl_map_table(acpi_size address,
1049 char *signature, struct acpi_table_header **table)
1051 struct acpi_table_header *mapped_table;
1055 return (AE_BAD_ADDRESS);
1059 * Map the header so we can get the table length.
1060 * Use sizeof (struct acpi_table_header) as:
1061 * 1. it is bigger than 24 to include RSDP->Length
1062 * 2. it is smaller than sizeof (struct acpi_table_rsdp)
1065 acpi_os_map_memory(address, sizeof(struct acpi_table_header));
1066 if (!mapped_table) {
1067 fprintf(stderr, "Could not map table header at 0x%8.8X%8.8X\n",
1068 ACPI_FORMAT_UINT64(address));
1069 return (osl_get_last_status(AE_BAD_ADDRESS));
1072 /* If specified, signature must match */
1075 if (ACPI_VALIDATE_RSDP_SIG(signature)) {
1076 if (!ACPI_VALIDATE_RSDP_SIG(mapped_table->signature)) {
1077 acpi_os_unmap_memory(mapped_table,
1079 acpi_table_header));
1080 return (AE_BAD_SIGNATURE);
1083 if (!ACPI_COMPARE_NAME(signature, mapped_table->signature))
1085 acpi_os_unmap_memory(mapped_table,
1086 sizeof(struct acpi_table_header));
1087 return (AE_BAD_SIGNATURE);
1091 /* Map the entire table */
1093 length = ap_get_table_length(mapped_table);
1094 acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));
1096 return (AE_BAD_HEADER);
1099 mapped_table = acpi_os_map_memory(address, length);
1100 if (!mapped_table) {
1102 "Could not map table at 0x%8.8X%8.8X length %8.8X\n",
1103 ACPI_FORMAT_UINT64(address), length);
1104 return (osl_get_last_status(AE_INVALID_TABLE_LENGTH));
1107 (void)ap_is_valid_checksum(mapped_table);
1109 *table = mapped_table;
1113 /******************************************************************************
1115 * FUNCTION: osl_unmap_table
1117 * PARAMETERS: table - A pointer to the mapped table
1121 * DESCRIPTION: Unmap entire ACPI table.
1123 *****************************************************************************/
1125 static void osl_unmap_table(struct acpi_table_header *table)
1128 acpi_os_unmap_memory(table, ap_get_table_length(table));
1132 /******************************************************************************
1134 * FUNCTION: osl_table_name_from_file
1136 * PARAMETERS: filename - File that contains the desired table
1137 * signature - Pointer to 4-character buffer to store
1138 * extracted table signature.
1139 * instance - Pointer to integer to store extracted
1140 * table instance number.
1142 * RETURN: Status; Table name is extracted if AE_OK.
1144 * DESCRIPTION: Extract table signature and instance number from a table file
1147 *****************************************************************************/
1150 osl_table_name_from_file(char *filename, char *signature, u32 *instance)
1153 /* Ignore meaningless files */
1155 if (strlen(filename) < ACPI_NAME_SIZE) {
1156 return (AE_BAD_SIGNATURE);
1159 /* Extract instance number */
1161 if (isdigit((int)filename[ACPI_NAME_SIZE])) {
1162 sscanf(&filename[ACPI_NAME_SIZE], "%u", instance);
1163 } else if (strlen(filename) != ACPI_NAME_SIZE) {
1164 return (AE_BAD_SIGNATURE);
1169 /* Extract signature */
1171 ACPI_MOVE_NAME(signature, filename);
1175 /******************************************************************************
1177 * FUNCTION: osl_read_table_from_file
1179 * PARAMETERS: filename - File that contains the desired table
1180 * file_offset - Offset of the table in file
1181 * signature - Optional ACPI Signature for desired table.
1182 * A null terminated 4-character string.
1183 * table - Where a pointer to the table is returned
1185 * RETURN: Status; Table buffer is returned if AE_OK.
1187 * DESCRIPTION: Read a ACPI table from a file.
1189 *****************************************************************************/
1192 osl_read_table_from_file(char *filename,
1193 acpi_size file_offset,
1194 char *signature, struct acpi_table_header **table)
1197 struct acpi_table_header header;
1198 struct acpi_table_header *local_table = NULL;
1201 acpi_status status = AE_OK;
1205 table_file = fopen(filename, "rb");
1206 if (table_file == NULL) {
1207 fprintf(stderr, "Could not open table file: %s\n", filename);
1208 return (osl_get_last_status(AE_NOT_FOUND));
1211 fseek(table_file, file_offset, SEEK_SET);
1213 /* Read the Table header to get the table length */
1215 count = fread(&header, 1, sizeof(struct acpi_table_header), table_file);
1216 if (count != sizeof(struct acpi_table_header)) {
1217 fprintf(stderr, "Could not read table header: %s\n", filename);
1218 status = AE_BAD_HEADER;
1222 /* If signature is specified, it must match the table */
1225 if (ACPI_VALIDATE_RSDP_SIG(signature)) {
1226 if (!ACPI_VALIDATE_RSDP_SIG(header.signature)) {
1228 "Incorrect RSDP signature: found %8.8s\n",
1230 status = AE_BAD_SIGNATURE;
1233 } else if (!ACPI_COMPARE_NAME(signature, header.signature)) {
1235 "Incorrect signature: Expecting %4.4s, found %4.4s\n",
1236 signature, header.signature);
1237 status = AE_BAD_SIGNATURE;
1242 table_length = ap_get_table_length(&header);
1243 if (table_length == 0) {
1244 status = AE_BAD_HEADER;
1248 /* Read the entire table into a local buffer */
1250 local_table = calloc(1, table_length);
1253 "%4.4s: Could not allocate buffer for table of length %X\n",
1254 header.signature, table_length);
1255 status = AE_NO_MEMORY;
1259 fseek(table_file, file_offset, SEEK_SET);
1261 count = fread(local_table, 1, table_length, table_file);
1262 if (count != table_length) {
1263 fprintf(stderr, "%4.4s: Could not read table content\n",
1265 status = AE_INVALID_TABLE_LENGTH;
1269 /* Validate checksum */
1271 (void)ap_is_valid_checksum(local_table);
1275 *table = local_table;
1279 /******************************************************************************
1281 * FUNCTION: osl_get_customized_table
1283 * PARAMETERS: pathname - Directory to find Linux customized table
1284 * signature - ACPI Signature for desired table. Must be
1285 * a null terminated 4-character string.
1286 * instance - Multiple table support for SSDT/UEFI (0...n)
1287 * Must be 0 for other tables.
1288 * table - Where a pointer to the table is returned
1289 * address - Where the table physical address is returned
1291 * RETURN: Status; Table buffer is returned if AE_OK.
1292 * AE_LIMIT: Instance is beyond valid limit
1293 * AE_NOT_FOUND: A table with the signature was not found
1295 * DESCRIPTION: Get an OS customized table.
1297 *****************************************************************************/
1300 osl_get_customized_table(char *pathname,
1303 struct acpi_table_header **table,
1304 acpi_physical_address * address)
1307 u32 current_instance = 0;
1308 char temp_name[ACPI_NAME_SIZE];
1309 char table_filename[PATH_MAX];
1313 /* Open the directory for customized tables */
1315 table_dir = acpi_os_open_directory(pathname, "*", REQUEST_FILE_ONLY);
1317 return (osl_get_last_status(AE_NOT_FOUND));
1320 /* Attempt to find the table in the directory */
1322 while ((filename = acpi_os_get_next_filename(table_dir))) {
1324 /* Ignore meaningless files */
1326 if (!ACPI_COMPARE_NAME(filename, signature)) {
1330 /* Extract table name and instance number */
1333 osl_table_name_from_file(filename, temp_name,
1336 /* Ignore meaningless files */
1338 if (ACPI_FAILURE(status) || current_instance != instance) {
1342 /* Create the table pathname */
1344 if (instance != 0) {
1345 sprintf(table_filename, "%s/%4.4s%d", pathname,
1346 temp_name, instance);
1348 sprintf(table_filename, "%s/%4.4s", pathname,
1354 acpi_os_close_directory(table_dir);
1360 /* There is no physical address saved for customized tables, use zero */
1363 status = osl_read_table_from_file(table_filename, 0, NULL, table);