Path: blob/master/tools/power/acpi/os_specific/service_layers/oslinuxtbl.c
26292 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.01/******************************************************************************2*3* Module Name: oslinuxtbl - Linux OSL for obtaining ACPI tables4*5* Copyright (C) 2000 - 2025, Intel Corp.6*7*****************************************************************************/89#include "acpidump.h"1011#define _COMPONENT ACPI_OS_SERVICES12ACPI_MODULE_NAME("oslinuxtbl")1314#ifndef PATH_MAX15#define PATH_MAX 25616#endif17/* List of information about obtained ACPI tables */18typedef struct osl_table_info {19struct osl_table_info *next;20u32 instance;21char signature[ACPI_NAMESEG_SIZE] ACPI_NONSTRING;2223} osl_table_info;2425/* Local prototypes */2627static acpi_status osl_table_initialize(void);2829static acpi_status30osl_table_name_from_file(char *filename, char *signature, u32 *instance);3132static acpi_status osl_add_table_to_list(char *signature, u32 instance);3334static acpi_status35osl_read_table_from_file(char *filename,36acpi_size file_offset,37struct acpi_table_header **table);3839static acpi_status40osl_map_table(acpi_size address,41char *signature, struct acpi_table_header **table);4243static void osl_unmap_table(struct acpi_table_header *table);4445static acpi_physical_address46osl_find_rsdp_via_efi_by_keyword(FILE * file, const char *keyword);4748static acpi_physical_address osl_find_rsdp_via_efi(void);4950static acpi_status osl_load_rsdp(void);5152static acpi_status osl_list_customized_tables(char *directory);5354static acpi_status55osl_get_customized_table(char *pathname,56char *signature,57u32 instance,58struct acpi_table_header **table,59acpi_physical_address *address);6061static acpi_status osl_list_bios_tables(void);6263static acpi_status64osl_get_bios_table(char *signature,65u32 instance,66struct acpi_table_header **table,67acpi_physical_address *address);6869static acpi_status osl_get_last_status(acpi_status default_status);7071/* File locations */7273#define DYNAMIC_TABLE_DIR "/sys/firmware/acpi/tables/dynamic"74#define STATIC_TABLE_DIR "/sys/firmware/acpi/tables"75#define EFI_SYSTAB "/sys/firmware/efi/systab"7677/* Should we get dynamically loaded SSDTs from DYNAMIC_TABLE_DIR? */7879u8 gbl_dump_dynamic_tables = TRUE;8081/* Initialization flags */8283u8 gbl_table_list_initialized = FALSE;8485/* Local copies of main ACPI tables */8687struct acpi_table_rsdp gbl_rsdp;88struct acpi_table_fadt *gbl_fadt = NULL;89struct acpi_table_rsdt *gbl_rsdt = NULL;90struct acpi_table_xsdt *gbl_xsdt = NULL;9192/* Table addresses */9394acpi_physical_address gbl_fadt_address = 0;95acpi_physical_address gbl_rsdp_address = 0;9697/* Revision of RSD PTR */9899u8 gbl_revision = 0;100101struct osl_table_info *gbl_table_list_head = NULL;102u32 gbl_table_count = 0;103104/******************************************************************************105*106* FUNCTION: osl_get_last_status107*108* PARAMETERS: default_status - Default error status to return109*110* RETURN: Status; Converted from errno.111*112* DESCRIPTION: Get last errno and convert it to acpi_status.113*114*****************************************************************************/115116static acpi_status osl_get_last_status(acpi_status default_status)117{118119switch (errno) {120case EACCES:121case EPERM:122123return (AE_ACCESS);124125case ENOENT:126127return (AE_NOT_FOUND);128129case ENOMEM:130131return (AE_NO_MEMORY);132133default:134135return (default_status);136}137}138139/******************************************************************************140*141* FUNCTION: acpi_os_get_table_by_address142*143* PARAMETERS: address - Physical address of the ACPI table144* table - Where a pointer to the table is returned145*146* RETURN: Status; Table buffer is returned if AE_OK.147* AE_NOT_FOUND: A valid table was not found at the address148*149* DESCRIPTION: Get an ACPI table via a physical memory address.150*151*****************************************************************************/152153acpi_status154acpi_os_get_table_by_address(acpi_physical_address address,155struct acpi_table_header **table)156{157u32 table_length;158struct acpi_table_header *mapped_table;159struct acpi_table_header *local_table = NULL;160acpi_status status = AE_OK;161162/* Get main ACPI tables from memory on first invocation of this function */163164status = osl_table_initialize();165if (ACPI_FAILURE(status)) {166return (status);167}168169/* Map the table and validate it */170171status = osl_map_table(address, NULL, &mapped_table);172if (ACPI_FAILURE(status)) {173return (status);174}175176/* Copy table to local buffer and return it */177178table_length = ap_get_table_length(mapped_table);179if (table_length == 0) {180status = AE_BAD_HEADER;181goto exit;182}183184local_table = calloc(1, table_length);185if (!local_table) {186status = AE_NO_MEMORY;187goto exit;188}189190memcpy(local_table, mapped_table, table_length);191192exit:193osl_unmap_table(mapped_table);194*table = local_table;195return (status);196}197198/******************************************************************************199*200* FUNCTION: acpi_os_get_table_by_name201*202* PARAMETERS: signature - ACPI Signature for desired table. Must be203* a null terminated 4-character string.204* instance - Multiple table support for SSDT/UEFI (0...n)205* Must be 0 for other tables.206* table - Where a pointer to the table is returned207* address - Where the table physical address is returned208*209* RETURN: Status; Table buffer and physical address returned if AE_OK.210* AE_LIMIT: Instance is beyond valid limit211* AE_NOT_FOUND: A table with the signature was not found212*213* NOTE: Assumes the input signature is uppercase.214*215*****************************************************************************/216217acpi_status218acpi_os_get_table_by_name(char *signature,219u32 instance,220struct acpi_table_header **table,221acpi_physical_address *address)222{223acpi_status status;224225/* Get main ACPI tables from memory on first invocation of this function */226227status = osl_table_initialize();228if (ACPI_FAILURE(status)) {229return (status);230}231232/* Not a main ACPI table, attempt to extract it from the RSDT/XSDT */233234if (!gbl_dump_customized_tables) {235236/* Attempt to get the table from the memory */237238status =239osl_get_bios_table(signature, instance, table, address);240} else {241/* Attempt to get the table from the static directory */242243status = osl_get_customized_table(STATIC_TABLE_DIR, signature,244instance, table, address);245}246247if (ACPI_FAILURE(status) && status == AE_LIMIT) {248if (gbl_dump_dynamic_tables) {249250/* Attempt to get a dynamic table */251252status =253osl_get_customized_table(DYNAMIC_TABLE_DIR,254signature, instance, table,255address);256}257}258259return (status);260}261262/******************************************************************************263*264* FUNCTION: osl_add_table_to_list265*266* PARAMETERS: signature - Table signature267* instance - Table instance268*269* RETURN: Status; Successfully added if AE_OK.270* AE_NO_MEMORY: Memory allocation error271*272* DESCRIPTION: Insert a table structure into OSL table list.273*274*****************************************************************************/275276static acpi_status osl_add_table_to_list(char *signature, u32 instance)277{278struct osl_table_info *new_info;279struct osl_table_info *next;280u32 next_instance = 0;281u8 found = FALSE;282283new_info = calloc(1, sizeof(struct osl_table_info));284if (!new_info) {285return (AE_NO_MEMORY);286}287288ACPI_COPY_NAMESEG(new_info->signature, signature);289290if (!gbl_table_list_head) {291gbl_table_list_head = new_info;292} else {293next = gbl_table_list_head;294while (1) {295if (ACPI_COMPARE_NAMESEG(next->signature, signature)) {296if (next->instance == instance) {297found = TRUE;298}299if (next->instance >= next_instance) {300next_instance = next->instance + 1;301}302}303304if (!next->next) {305break;306}307next = next->next;308}309next->next = new_info;310}311312if (found) {313if (instance) {314fprintf(stderr,315"%4.4s: Warning unmatched table instance %d, expected %d\n",316signature, instance, next_instance);317}318instance = next_instance;319}320321new_info->instance = instance;322gbl_table_count++;323324return (AE_OK);325}326327/******************************************************************************328*329* FUNCTION: acpi_os_get_table_by_index330*331* PARAMETERS: index - Which table to get332* table - Where a pointer to the table is returned333* instance - Where a pointer to the table instance no. is334* returned335* address - Where the table physical address is returned336*337* RETURN: Status; Table buffer and physical address returned if AE_OK.338* AE_LIMIT: Index is beyond valid limit339*340* DESCRIPTION: Get an ACPI table via an index value (0 through n). Returns341* AE_LIMIT when an invalid index is reached. Index is not342* necessarily an index into the RSDT/XSDT.343*344*****************************************************************************/345346acpi_status347acpi_os_get_table_by_index(u32 index,348struct acpi_table_header **table,349u32 *instance, acpi_physical_address *address)350{351struct osl_table_info *info;352acpi_status status;353u32 i;354355/* Get main ACPI tables from memory on first invocation of this function */356357status = osl_table_initialize();358if (ACPI_FAILURE(status)) {359return (status);360}361362/* Validate Index */363364if (index >= gbl_table_count) {365return (AE_LIMIT);366}367368/* Point to the table list entry specified by the Index argument */369370info = gbl_table_list_head;371for (i = 0; i < index; i++) {372info = info->next;373}374375/* Now we can just get the table via the signature */376377status = acpi_os_get_table_by_name(info->signature, info->instance,378table, address);379380if (ACPI_SUCCESS(status)) {381*instance = info->instance;382}383return (status);384}385386/******************************************************************************387*388* FUNCTION: osl_find_rsdp_via_efi_by_keyword389*390* PARAMETERS: keyword - Character string indicating ACPI GUID version391* in the EFI table392*393* RETURN: RSDP address if found394*395* DESCRIPTION: Find RSDP address via EFI using keyword indicating the ACPI396* GUID version.397*398*****************************************************************************/399400static acpi_physical_address401osl_find_rsdp_via_efi_by_keyword(FILE * file, const char *keyword)402{403char buffer[80];404unsigned long long address = 0;405char format[32];406407snprintf(format, 32, "%s=%s", keyword, "%llx");408fseek(file, 0, SEEK_SET);409while (fgets(buffer, 80, file)) {410if (sscanf(buffer, format, &address) == 1) {411break;412}413}414415return ((acpi_physical_address)(address));416}417418/******************************************************************************419*420* FUNCTION: osl_find_rsdp_via_efi421*422* PARAMETERS: None423*424* RETURN: RSDP address if found425*426* DESCRIPTION: Find RSDP address via EFI.427*428*****************************************************************************/429430static acpi_physical_address osl_find_rsdp_via_efi(void)431{432FILE *file;433acpi_physical_address address = 0;434435file = fopen(EFI_SYSTAB, "r");436if (file) {437address = osl_find_rsdp_via_efi_by_keyword(file, "ACPI20");438if (!address) {439address =440osl_find_rsdp_via_efi_by_keyword(file, "ACPI");441}442fclose(file);443}444445return (address);446}447448/******************************************************************************449*450* FUNCTION: osl_load_rsdp451*452* PARAMETERS: None453*454* RETURN: Status455*456* DESCRIPTION: Scan and load RSDP.457*458*****************************************************************************/459460static acpi_status osl_load_rsdp(void)461{462struct acpi_table_header *mapped_table;463u8 *rsdp_address;464acpi_physical_address rsdp_base;465acpi_size rsdp_size;466467/* Get RSDP from memory */468469rsdp_size = sizeof(struct acpi_table_rsdp);470if (gbl_rsdp_base) {471rsdp_base = gbl_rsdp_base;472} else {473rsdp_base = osl_find_rsdp_via_efi();474}475476if (!rsdp_base) {477rsdp_base = ACPI_HI_RSDP_WINDOW_BASE;478rsdp_size = ACPI_HI_RSDP_WINDOW_SIZE;479}480481rsdp_address = acpi_os_map_memory(rsdp_base, rsdp_size);482if (!rsdp_address) {483return (osl_get_last_status(AE_BAD_ADDRESS));484}485486/* Search low memory for the RSDP */487488mapped_table = ACPI_CAST_PTR(struct acpi_table_header,489acpi_tb_scan_memory_for_rsdp(rsdp_address,490rsdp_size));491if (!mapped_table) {492acpi_os_unmap_memory(rsdp_address, rsdp_size);493return (AE_NOT_FOUND);494}495496gbl_rsdp_address =497rsdp_base + (ACPI_CAST8(mapped_table) - rsdp_address);498499memcpy(&gbl_rsdp, mapped_table, sizeof(struct acpi_table_rsdp));500acpi_os_unmap_memory(rsdp_address, rsdp_size);501502return (AE_OK);503}504505/******************************************************************************506*507* FUNCTION: osl_can_use_xsdt508*509* PARAMETERS: None510*511* RETURN: TRUE if XSDT is allowed to be used.512*513* DESCRIPTION: This function collects logic that can be used to determine if514* XSDT should be used instead of RSDT.515*516*****************************************************************************/517518static u8 osl_can_use_xsdt(void)519{520if (gbl_revision && !acpi_gbl_do_not_use_xsdt) {521return (TRUE);522} else {523return (FALSE);524}525}526527/******************************************************************************528*529* FUNCTION: osl_table_initialize530*531* PARAMETERS: None532*533* RETURN: Status534*535* DESCRIPTION: Initialize ACPI table data. Get and store main ACPI tables to536* local variables. Main ACPI tables include RSDT, FADT, RSDT,537* and/or XSDT.538*539*****************************************************************************/540541static acpi_status osl_table_initialize(void)542{543acpi_status status;544acpi_physical_address address;545546if (gbl_table_list_initialized) {547return (AE_OK);548}549550if (!gbl_dump_customized_tables) {551552/* Get RSDP from memory */553554status = osl_load_rsdp();555if (ACPI_FAILURE(status)) {556return (status);557}558559/* Get XSDT from memory */560561if (gbl_rsdp.revision && !gbl_do_not_dump_xsdt) {562if (gbl_xsdt) {563free(gbl_xsdt);564gbl_xsdt = NULL;565}566567gbl_revision = 2;568status = osl_get_bios_table(ACPI_SIG_XSDT, 0,569ACPI_CAST_PTR(struct570acpi_table_header571*, &gbl_xsdt),572&address);573if (ACPI_FAILURE(status)) {574return (status);575}576}577578/* Get RSDT from memory */579580if (gbl_rsdp.rsdt_physical_address) {581if (gbl_rsdt) {582free(gbl_rsdt);583gbl_rsdt = NULL;584}585586status = osl_get_bios_table(ACPI_SIG_RSDT, 0,587ACPI_CAST_PTR(struct588acpi_table_header589*, &gbl_rsdt),590&address);591if (ACPI_FAILURE(status)) {592return (status);593}594}595596/* Get FADT from memory */597598if (gbl_fadt) {599free(gbl_fadt);600gbl_fadt = NULL;601}602603status = osl_get_bios_table(ACPI_SIG_FADT, 0,604ACPI_CAST_PTR(struct605acpi_table_header *,606&gbl_fadt),607&gbl_fadt_address);608if (ACPI_FAILURE(status)) {609return (status);610}611612/* Add mandatory tables to global table list first */613614status = osl_add_table_to_list(ACPI_RSDP_NAME, 0);615if (ACPI_FAILURE(status)) {616return (status);617}618619status = osl_add_table_to_list(ACPI_SIG_RSDT, 0);620if (ACPI_FAILURE(status)) {621return (status);622}623624if (gbl_revision == 2) {625status = osl_add_table_to_list(ACPI_SIG_XSDT, 0);626if (ACPI_FAILURE(status)) {627return (status);628}629}630631status = osl_add_table_to_list(ACPI_SIG_DSDT, 0);632if (ACPI_FAILURE(status)) {633return (status);634}635636status = osl_add_table_to_list(ACPI_SIG_FACS, 0);637if (ACPI_FAILURE(status)) {638return (status);639}640641/* Add all tables found in the memory */642643status = osl_list_bios_tables();644if (ACPI_FAILURE(status)) {645return (status);646}647} else {648/* Add all tables found in the static directory */649650status = osl_list_customized_tables(STATIC_TABLE_DIR);651if (ACPI_FAILURE(status)) {652return (status);653}654}655656if (gbl_dump_dynamic_tables) {657658/* Add all dynamically loaded tables in the dynamic directory */659660status = osl_list_customized_tables(DYNAMIC_TABLE_DIR);661if (ACPI_FAILURE(status)) {662return (status);663}664}665666gbl_table_list_initialized = TRUE;667return (AE_OK);668}669670/******************************************************************************671*672* FUNCTION: osl_list_bios_tables673*674* PARAMETERS: None675*676* RETURN: Status; Table list is initialized if AE_OK.677*678* DESCRIPTION: Add ACPI tables to the table list from memory.679*680* NOTE: This works on Linux as table customization does not modify the681* addresses stored in RSDP/RSDT/XSDT/FADT.682*683*****************************************************************************/684685static acpi_status osl_list_bios_tables(void)686{687struct acpi_table_header *mapped_table = NULL;688u8 *table_data;689u8 number_of_tables;690u8 item_size;691acpi_physical_address table_address = 0;692acpi_status status = AE_OK;693u32 i;694695if (osl_can_use_xsdt()) {696item_size = sizeof(u64);697table_data =698ACPI_CAST8(gbl_xsdt) + sizeof(struct acpi_table_header);699number_of_tables =700(u8)((gbl_xsdt->header.length -701sizeof(struct acpi_table_header))702/ item_size);703} else { /* Use RSDT if XSDT is not available */704705item_size = sizeof(u32);706table_data =707ACPI_CAST8(gbl_rsdt) + sizeof(struct acpi_table_header);708number_of_tables =709(u8)((gbl_rsdt->header.length -710sizeof(struct acpi_table_header))711/ item_size);712}713714/* Search RSDT/XSDT for the requested table */715716for (i = 0; i < number_of_tables; ++i, table_data += item_size) {717if (osl_can_use_xsdt()) {718table_address =719(acpi_physical_address)(*ACPI_CAST64(table_data));720} else {721table_address =722(acpi_physical_address)(*ACPI_CAST32(table_data));723}724725/* Skip NULL entries in RSDT/XSDT */726727if (table_address == 0) {728continue;729}730731status = osl_map_table(table_address, NULL, &mapped_table);732if (ACPI_FAILURE(status)) {733return (status);734}735736osl_add_table_to_list(mapped_table->signature, 0);737osl_unmap_table(mapped_table);738}739740return (AE_OK);741}742743/******************************************************************************744*745* FUNCTION: osl_get_bios_table746*747* PARAMETERS: signature - ACPI Signature for common table. Must be748* a null terminated 4-character string.749* instance - Multiple table support for SSDT/UEFI (0...n)750* Must be 0 for other tables.751* table - Where a pointer to the table is returned752* address - Where the table physical address is returned753*754* RETURN: Status; Table buffer and physical address returned if AE_OK.755* AE_LIMIT: Instance is beyond valid limit756* AE_NOT_FOUND: A table with the signature was not found757*758* DESCRIPTION: Get a BIOS provided ACPI table759*760* NOTE: Assumes the input signature is uppercase.761*762*****************************************************************************/763764static acpi_status765osl_get_bios_table(char *signature,766u32 instance,767struct acpi_table_header **table,768acpi_physical_address *address)769{770struct acpi_table_header *local_table = NULL;771struct acpi_table_header *mapped_table = NULL;772u8 *table_data;773u8 number_of_tables;774u8 item_size;775u32 current_instance = 0;776acpi_physical_address table_address;777acpi_physical_address first_table_address = 0;778u32 table_length = 0;779acpi_status status = AE_OK;780u32 i;781782/* Handle special tables whose addresses are not in RSDT/XSDT */783784if (ACPI_COMPARE_NAMESEG(signature, ACPI_RSDP_NAME) ||785ACPI_COMPARE_NAMESEG(signature, ACPI_SIG_RSDT) ||786ACPI_COMPARE_NAMESEG(signature, ACPI_SIG_XSDT) ||787ACPI_COMPARE_NAMESEG(signature, ACPI_SIG_DSDT) ||788ACPI_COMPARE_NAMESEG(signature, ACPI_SIG_FACS)) {789790find_next_instance:791792table_address = 0;793794/*795* Get the appropriate address, either 32-bit or 64-bit. Be very796* careful about the FADT length and validate table addresses.797* Note: The 64-bit addresses have priority.798*/799if (ACPI_COMPARE_NAMESEG(signature, ACPI_SIG_DSDT)) {800if (current_instance < 2) {801if ((gbl_fadt->header.length >=802MIN_FADT_FOR_XDSDT) && gbl_fadt->Xdsdt803&& current_instance == 0) {804table_address =805(acpi_physical_address)gbl_fadt->806Xdsdt;807} else808if ((gbl_fadt->header.length >=809MIN_FADT_FOR_DSDT)810&& gbl_fadt->dsdt !=811first_table_address) {812table_address =813(acpi_physical_address)gbl_fadt->814dsdt;815}816}817} else if (ACPI_COMPARE_NAMESEG(signature, ACPI_SIG_FACS)) {818if (current_instance < 2) {819if ((gbl_fadt->header.length >=820MIN_FADT_FOR_XFACS) && gbl_fadt->Xfacs821&& current_instance == 0) {822table_address =823(acpi_physical_address)gbl_fadt->824Xfacs;825} else826if ((gbl_fadt->header.length >=827MIN_FADT_FOR_FACS)828&& gbl_fadt->facs !=829first_table_address) {830table_address =831(acpi_physical_address)gbl_fadt->832facs;833}834}835} else if (ACPI_COMPARE_NAMESEG(signature, ACPI_SIG_XSDT)) {836if (!gbl_revision) {837return (AE_BAD_SIGNATURE);838}839if (current_instance == 0) {840table_address =841(acpi_physical_address)gbl_rsdp.842xsdt_physical_address;843}844} else if (ACPI_COMPARE_NAMESEG(signature, ACPI_SIG_RSDT)) {845if (current_instance == 0) {846table_address =847(acpi_physical_address)gbl_rsdp.848rsdt_physical_address;849}850} else {851if (current_instance == 0) {852table_address =853(acpi_physical_address)gbl_rsdp_address;854signature = ACPI_SIG_RSDP;855}856}857858if (table_address == 0) {859goto exit_find_table;860}861862/* Now we can get the requested special table */863864status = osl_map_table(table_address, signature, &mapped_table);865if (ACPI_FAILURE(status)) {866return (status);867}868869table_length = ap_get_table_length(mapped_table);870if (first_table_address == 0) {871first_table_address = table_address;872}873874/* Match table instance */875876if (current_instance != instance) {877osl_unmap_table(mapped_table);878mapped_table = NULL;879current_instance++;880goto find_next_instance;881}882} else { /* Case for a normal ACPI table */883884if (osl_can_use_xsdt()) {885item_size = sizeof(u64);886table_data =887ACPI_CAST8(gbl_xsdt) +888sizeof(struct acpi_table_header);889number_of_tables =890(u8)((gbl_xsdt->header.length -891sizeof(struct acpi_table_header))892/ item_size);893} else { /* Use RSDT if XSDT is not available */894895item_size = sizeof(u32);896table_data =897ACPI_CAST8(gbl_rsdt) +898sizeof(struct acpi_table_header);899number_of_tables =900(u8)((gbl_rsdt->header.length -901sizeof(struct acpi_table_header))902/ item_size);903}904905/* Search RSDT/XSDT for the requested table */906907for (i = 0; i < number_of_tables; ++i, table_data += item_size) {908if (osl_can_use_xsdt()) {909table_address =910(acpi_physical_address)(*ACPI_CAST64911(table_data));912} else {913table_address =914(acpi_physical_address)(*ACPI_CAST32915(table_data));916}917918/* Skip NULL entries in RSDT/XSDT */919920if (table_address == 0) {921continue;922}923924status =925osl_map_table(table_address, NULL, &mapped_table);926if (ACPI_FAILURE(status)) {927return (status);928}929table_length = mapped_table->length;930931/* Does this table match the requested signature? */932933if (!ACPI_COMPARE_NAMESEG934(mapped_table->signature, signature)) {935osl_unmap_table(mapped_table);936mapped_table = NULL;937continue;938}939940/* Match table instance (for SSDT/UEFI tables) */941942if (current_instance != instance) {943osl_unmap_table(mapped_table);944mapped_table = NULL;945current_instance++;946continue;947}948949break;950}951}952953exit_find_table:954955if (!mapped_table) {956return (AE_LIMIT);957}958959if (table_length == 0) {960status = AE_BAD_HEADER;961goto exit;962}963964/* Copy table to local buffer and return it */965966local_table = calloc(1, table_length);967if (!local_table) {968status = AE_NO_MEMORY;969goto exit;970}971972memcpy(local_table, mapped_table, table_length);973*address = table_address;974*table = local_table;975976exit:977osl_unmap_table(mapped_table);978return (status);979}980981/******************************************************************************982*983* FUNCTION: osl_list_customized_tables984*985* PARAMETERS: directory - Directory that contains the tables986*987* RETURN: Status; Table list is initialized if AE_OK.988*989* DESCRIPTION: Add ACPI tables to the table list from a directory.990*991*****************************************************************************/992993static acpi_status osl_list_customized_tables(char *directory)994{995void *table_dir;996u32 instance;997char temp_name[ACPI_NAMESEG_SIZE];998char *filename;999acpi_status status = AE_OK;10001001/* Open the requested directory */10021003table_dir = acpi_os_open_directory(directory, "*", REQUEST_FILE_ONLY);1004if (!table_dir) {1005return (osl_get_last_status(AE_NOT_FOUND));1006}10071008/* Examine all entries in this directory */10091010while ((filename = acpi_os_get_next_filename(table_dir))) {10111012/* Extract table name and instance number */10131014status =1015osl_table_name_from_file(filename, temp_name, &instance);10161017/* Ignore meaningless files */10181019if (ACPI_FAILURE(status)) {1020continue;1021}10221023/* Add new info node to global table list */10241025status = osl_add_table_to_list(temp_name, instance);1026if (ACPI_FAILURE(status)) {1027break;1028}1029}10301031acpi_os_close_directory(table_dir);1032return (status);1033}10341035/******************************************************************************1036*1037* FUNCTION: osl_map_table1038*1039* PARAMETERS: address - Address of the table in memory1040* signature - Optional ACPI Signature for desired table.1041* Null terminated 4-character string.1042* table - Where a pointer to the mapped table is1043* returned1044*1045* RETURN: Status; Mapped table is returned if AE_OK.1046* AE_NOT_FOUND: A valid table was not found at the address1047*1048* DESCRIPTION: Map entire ACPI table into caller's address space.1049*1050*****************************************************************************/10511052static acpi_status1053osl_map_table(acpi_size address,1054char *signature, struct acpi_table_header **table)1055{1056struct acpi_table_header *mapped_table;1057u32 length;10581059if (!address) {1060return (AE_BAD_ADDRESS);1061}10621063/*1064* Map the header so we can get the table length.1065* Use sizeof (struct acpi_table_header) as:1066* 1. it is bigger than 24 to include RSDP->Length1067* 2. it is smaller than sizeof (struct acpi_table_rsdp)1068*/1069mapped_table =1070acpi_os_map_memory(address, sizeof(struct acpi_table_header));1071if (!mapped_table) {1072fprintf(stderr, "Could not map table header at 0x%8.8X%8.8X\n",1073ACPI_FORMAT_UINT64(address));1074return (osl_get_last_status(AE_BAD_ADDRESS));1075}10761077/* If specified, signature must match */10781079if (signature) {1080if (ACPI_VALIDATE_RSDP_SIG(signature)) {1081if (!ACPI_VALIDATE_RSDP_SIG(mapped_table->signature)) {1082acpi_os_unmap_memory(mapped_table,1083sizeof(struct1084acpi_table_header));1085return (AE_BAD_SIGNATURE);1086}1087} else1088if (!ACPI_COMPARE_NAMESEG1089(signature, mapped_table->signature)) {1090acpi_os_unmap_memory(mapped_table,1091sizeof(struct acpi_table_header));1092return (AE_BAD_SIGNATURE);1093}1094}10951096/* Map the entire table */10971098length = ap_get_table_length(mapped_table);1099acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));1100if (length == 0) {1101return (AE_BAD_HEADER);1102}11031104mapped_table = acpi_os_map_memory(address, length);1105if (!mapped_table) {1106fprintf(stderr,1107"Could not map table at 0x%8.8X%8.8X length %8.8X\n",1108ACPI_FORMAT_UINT64(address), length);1109return (osl_get_last_status(AE_INVALID_TABLE_LENGTH));1110}11111112(void)ap_is_valid_checksum(mapped_table);11131114*table = mapped_table;1115return (AE_OK);1116}11171118/******************************************************************************1119*1120* FUNCTION: osl_unmap_table1121*1122* PARAMETERS: table - A pointer to the mapped table1123*1124* RETURN: None1125*1126* DESCRIPTION: Unmap entire ACPI table.1127*1128*****************************************************************************/11291130static void osl_unmap_table(struct acpi_table_header *table)1131{1132if (table) {1133acpi_os_unmap_memory(table, ap_get_table_length(table));1134}1135}11361137/******************************************************************************1138*1139* FUNCTION: osl_table_name_from_file1140*1141* PARAMETERS: filename - File that contains the desired table1142* signature - Pointer to 4-character buffer to store1143* extracted table signature.1144* instance - Pointer to integer to store extracted1145* table instance number.1146*1147* RETURN: Status; Table name is extracted if AE_OK.1148*1149* DESCRIPTION: Extract table signature and instance number from a table file1150* name.1151*1152*****************************************************************************/11531154static acpi_status1155osl_table_name_from_file(char *filename, char *signature, u32 *instance)1156{11571158/* Ignore meaningless files */11591160if (strlen(filename) < ACPI_NAMESEG_SIZE) {1161return (AE_BAD_SIGNATURE);1162}11631164/* Extract instance number */11651166if (isdigit((int)filename[ACPI_NAMESEG_SIZE])) {1167sscanf(&filename[ACPI_NAMESEG_SIZE], "%u", instance);1168} else if (strlen(filename) != ACPI_NAMESEG_SIZE) {1169return (AE_BAD_SIGNATURE);1170} else {1171*instance = 0;1172}11731174/* Extract signature */11751176ACPI_COPY_NAMESEG(signature, filename);1177return (AE_OK);1178}11791180/******************************************************************************1181*1182* FUNCTION: osl_read_table_from_file1183*1184* PARAMETERS: filename - File that contains the desired table1185* file_offset - Offset of the table in file1186* table - Where a pointer to the table is returned1187*1188* RETURN: Status; Table buffer is returned if AE_OK.1189*1190* DESCRIPTION: Read a ACPI table from a file.1191*1192*****************************************************************************/11931194static acpi_status1195osl_read_table_from_file(char *filename,1196acpi_size file_offset,1197struct acpi_table_header **table)1198{1199FILE *table_file;1200struct acpi_table_header header;1201struct acpi_table_header *local_table = NULL;1202u32 table_length;1203s32 count;1204acpi_status status = AE_OK;12051206/* Open the file */12071208table_file = fopen(filename, "rb");1209if (table_file == NULL) {1210fprintf(stderr, "Could not open table file: %s\n", filename);1211return (osl_get_last_status(AE_NOT_FOUND));1212}12131214fseek(table_file, file_offset, SEEK_SET);12151216/* Read the Table header to get the table length */12171218count = fread(&header, 1, sizeof(struct acpi_table_header), table_file);1219if (count != sizeof(struct acpi_table_header)) {1220fprintf(stderr, "Could not read table header: %s\n", filename);1221status = AE_BAD_HEADER;1222goto exit;1223}12241225#ifdef ACPI_OBSOLETE_FUNCTIONS12261227/* If signature is specified, it must match the table */12281229if (signature) {1230if (ACPI_VALIDATE_RSDP_SIG(signature)) {1231if (!ACPI_VALIDATE_RSDP_SIG(header.signature)) {1232fprintf(stderr,1233"Incorrect RSDP signature: found %8.8s\n",1234header.signature);1235status = AE_BAD_SIGNATURE;1236goto exit;1237}1238} else if (!ACPI_COMPARE_NAMESEG(signature, header.signature)) {1239fprintf(stderr,1240"Incorrect signature: Expecting %4.4s, found %4.4s\n",1241signature, header.signature);1242status = AE_BAD_SIGNATURE;1243goto exit;1244}1245}1246#endif12471248table_length = ap_get_table_length(&header);1249if (table_length == 0) {1250status = AE_BAD_HEADER;1251goto exit;1252}12531254/* Read the entire table into a local buffer */12551256local_table = calloc(1, table_length);1257if (!local_table) {1258fprintf(stderr,1259"%4.4s: Could not allocate buffer for table of length %X\n",1260header.signature, table_length);1261status = AE_NO_MEMORY;1262goto exit;1263}12641265fseek(table_file, file_offset, SEEK_SET);12661267count = fread(local_table, 1, table_length, table_file);1268if (count != table_length) {1269fprintf(stderr, "%4.4s: Could not read table content\n",1270header.signature);1271status = AE_INVALID_TABLE_LENGTH;1272goto exit;1273}12741275/* Validate checksum */12761277(void)ap_is_valid_checksum(local_table);12781279exit:1280fclose(table_file);1281*table = local_table;1282return (status);1283}12841285/******************************************************************************1286*1287* FUNCTION: osl_get_customized_table1288*1289* PARAMETERS: pathname - Directory to find Linux customized table1290* signature - ACPI Signature for desired table. Must be1291* a null terminated 4-character string.1292* instance - Multiple table support for SSDT/UEFI (0...n)1293* Must be 0 for other tables.1294* table - Where a pointer to the table is returned1295* address - Where the table physical address is returned1296*1297* RETURN: Status; Table buffer is returned if AE_OK.1298* AE_LIMIT: Instance is beyond valid limit1299* AE_NOT_FOUND: A table with the signature was not found1300*1301* DESCRIPTION: Get an OS customized table.1302*1303*****************************************************************************/13041305static acpi_status1306osl_get_customized_table(char *pathname,1307char *signature,1308u32 instance,1309struct acpi_table_header **table,1310acpi_physical_address *address)1311{1312void *table_dir;1313u32 current_instance = 0;1314char temp_name[ACPI_NAMESEG_SIZE];1315char table_filename[PATH_MAX];1316char *filename;1317acpi_status status;13181319/* Open the directory for customized tables */13201321table_dir = acpi_os_open_directory(pathname, "*", REQUEST_FILE_ONLY);1322if (!table_dir) {1323return (osl_get_last_status(AE_NOT_FOUND));1324}13251326/* Attempt to find the table in the directory */13271328while ((filename = acpi_os_get_next_filename(table_dir))) {13291330/* Ignore meaningless files */13311332if (!ACPI_COMPARE_NAMESEG(filename, signature)) {1333continue;1334}13351336/* Extract table name and instance number */13371338status =1339osl_table_name_from_file(filename, temp_name,1340¤t_instance);13411342/* Ignore meaningless files */13431344if (ACPI_FAILURE(status) || current_instance != instance) {1345continue;1346}13471348/* Create the table pathname */13491350if (instance != 0) {1351sprintf(table_filename, "%s/%4.4s%d", pathname,1352temp_name, instance);1353} else {1354sprintf(table_filename, "%s/%4.4s", pathname,1355temp_name);1356}1357break;1358}13591360acpi_os_close_directory(table_dir);13611362if (!filename) {1363return (AE_LIMIT);1364}13651366/* There is no physical address saved for customized tables, use zero */13671368*address = 0;1369status = osl_read_table_from_file(table_filename, 0, table);13701371return (status);1372}137313741375