1/******************************************************************************
2 *
3 * Module Name: oslinuxtbl - Linux OSL for obtaining ACPI tables
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2015, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
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/* List of information about obtained ACPI tables */
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/* Local prototypes */
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/* File locations */
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/* Should we get dynamically loaded SSDTs from DYNAMIC_TABLE_DIR? */
113
114u8 gbl_dump_dynamic_tables = TRUE;
115
116/* Initialization flags */
117
118u8 gbl_table_list_initialized = FALSE;
119
120/* Local copies of main ACPI tables */
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/* Table addresses */
128
129acpi_physical_address gbl_fadt_address = 0;
130acpi_physical_address gbl_rsdp_address = 0;
131
132/* Revision of RSD PTR */
133
134u8 gbl_revision = 0;
135
136struct osl_table_info *gbl_table_list_head = NULL;
137u32 gbl_table_count = 0;
138
139/******************************************************************************
140 *
141 * FUNCTION:    osl_get_last_status
142 *
143 * PARAMETERS:  default_status  - Default error status to return
144 *
145 * RETURN:      Status; Converted from errno.
146 *
147 * DESCRIPTION: Get last errno and conver it to acpi_status.
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 * FUNCTION:    acpi_os_get_table_by_address
177 *
178 * PARAMETERS:  address         - Physical address of the ACPI table
179 *              table           - Where a pointer to the table is returned
180 *
181 * RETURN:      Status; Table buffer is returned if AE_OK.
182 *              AE_NOT_FOUND: A valid table was not found at the address
183 *
184 * DESCRIPTION: Get an ACPI table via a physical memory address.
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	/* Get main ACPI tables from memory on first invocation of this function */
198
199	status = osl_table_initialize();
200	if (ACPI_FAILURE(status)) {
201		return (status);
202	}
203
204	/* Map the table and validate it */
205
206	status = osl_map_table(address, NULL, &mapped_table);
207	if (ACPI_FAILURE(status)) {
208		return (status);
209	}
210
211	/* Copy table to local buffer and return it */
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	ACPI_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 * FUNCTION:    acpi_os_get_table_by_name
236 *
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
243 *
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
247 *
248 * NOTE:        Assumes the input signature is uppercase.
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	/* Get main ACPI tables from memory on first invocation of this function */
261
262	status = osl_table_initialize();
263	if (ACPI_FAILURE(status)) {
264		return (status);
265	}
266
267	/* Not a main ACPI table, attempt to extract it from the RSDT/XSDT */
268
269	if (!gbl_dump_customized_tables) {
270
271		/* Attempt to get the table from the memory */
272
273		status =
274		    osl_get_bios_table(signature, instance, table, address);
275	} else {
276		/* Attempt to get the table from the static directory */
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			/* Attempt to get a dynamic table */
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 * FUNCTION:    osl_add_table_to_list
300 *
301 * PARAMETERS:  signature       - Table signature
302 *              instance        - Table instance
303 *
304 * RETURN:      Status; Successfully added if AE_OK.
305 *              AE_NO_MEMORY: Memory allocation error
306 *
307 * DESCRIPTION: Insert a table structure into OSL table list.
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 * FUNCTION:    acpi_os_get_table_by_index
365 *
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
369 *                                returned
370 *              address         - Where the table physical address is returned
371 *
372 * RETURN:      Status; Table buffer and physical address returned if AE_OK.
373 *              AE_LIMIT: Index is beyond valid limit
374 *
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.
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	/* Get main ACPI tables from memory on first invocation of this function */
391
392	status = osl_table_initialize();
393	if (ACPI_FAILURE(status)) {
394		return (status);
395	}
396
397	/* Validate Index */
398
399	if (index >= gbl_table_count) {
400		return (AE_LIMIT);
401	}
402
403	/* Point to the table list entry specified by the Index argument */
404
405	info = gbl_table_list_head;
406	for (i = 0; i < index; i++) {
407		info = info->next;
408	}
409
410	/* Now we can just get the table via the signature */
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 * FUNCTION:    osl_find_rsdp_via_efi_by_keyword
424 *
425 * PARAMETERS:  keyword         - Character string indicating ACPI GUID version
426 *                                in the EFI table
427 *
428 * RETURN:      RSDP address if found
429 *
430 * DESCRIPTION: Find RSDP address via EFI using keyword indicating the ACPI
431 *              GUID version.
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 * FUNCTION:    osl_find_rsdp_via_efi
456 *
457 * PARAMETERS:  None
458 *
459 * RETURN:      RSDP address if found
460 *
461 * DESCRIPTION: Find RSDP address via EFI.
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 * FUNCTION:    osl_load_rsdp
486 *
487 * PARAMETERS:  None
488 *
489 * RETURN:      Status
490 *
491 * DESCRIPTION: Scan and load RSDP.
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	/* Get RSDP from memory */
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	/* Search low memory for the RSDP */
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	ACPI_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 * FUNCTION:    osl_can_use_xsdt
543 *
544 * PARAMETERS:  None
545 *
546 * RETURN:      TRUE if XSDT is allowed to be used.
547 *
548 * DESCRIPTION: This function collects logic that can be used to determine if
549 *              XSDT should be used instead of RSDT.
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 * FUNCTION:    osl_table_initialize
565 *
566 * PARAMETERS:  None
567 *
568 * RETURN:      Status
569 *
570 * DESCRIPTION: Initialize ACPI table data. Get and store main ACPI tables to
571 *              local variables. Main ACPI tables include RSDT, FADT, RSDT,
572 *              and/or XSDT.
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	/* Get RSDP from memory */
586
587	status = osl_load_rsdp();
588	if (ACPI_FAILURE(status)) {
589		return (status);
590	}
591
592	/* Get XSDT from memory */
593
594	if (gbl_rsdp.revision && !gbl_do_not_dump_xsdt) {
595		if (gbl_xsdt) {
596			free(gbl_xsdt);
597			gbl_xsdt = NULL;
598		}
599
600		gbl_revision = 2;
601		status = osl_get_bios_table(ACPI_SIG_XSDT, 0,
602					    ACPI_CAST_PTR(struct
603							  acpi_table_header *,
604							  &gbl_xsdt), &address);
605		if (ACPI_FAILURE(status)) {
606			return (status);
607		}
608	}
609
610	/* Get RSDT from memory */
611
612	if (gbl_rsdp.rsdt_physical_address) {
613		if (gbl_rsdt) {
614			free(gbl_rsdt);
615			gbl_rsdt = NULL;
616		}
617
618		status = osl_get_bios_table(ACPI_SIG_RSDT, 0,
619					    ACPI_CAST_PTR(struct
620							  acpi_table_header *,
621							  &gbl_rsdt), &address);
622		if (ACPI_FAILURE(status)) {
623			return (status);
624		}
625	}
626
627	/* Get FADT from memory */
628
629	if (gbl_fadt) {
630		free(gbl_fadt);
631		gbl_fadt = NULL;
632	}
633
634	status = osl_get_bios_table(ACPI_SIG_FADT, 0,
635				    ACPI_CAST_PTR(struct acpi_table_header *,
636						  &gbl_fadt),
637				    &gbl_fadt_address);
638	if (ACPI_FAILURE(status)) {
639		return (status);
640	}
641
642	if (!gbl_dump_customized_tables) {
643
644		/* Add mandatory tables to global table list first */
645
646		status = osl_add_table_to_list(ACPI_RSDP_NAME, 0);
647		if (ACPI_FAILURE(status)) {
648			return (status);
649		}
650
651		status = osl_add_table_to_list(ACPI_SIG_RSDT, 0);
652		if (ACPI_FAILURE(status)) {
653			return (status);
654		}
655
656		if (gbl_revision == 2) {
657			status = osl_add_table_to_list(ACPI_SIG_XSDT, 0);
658			if (ACPI_FAILURE(status)) {
659				return (status);
660			}
661		}
662
663		status = osl_add_table_to_list(ACPI_SIG_DSDT, 0);
664		if (ACPI_FAILURE(status)) {
665			return (status);
666		}
667
668		status = osl_add_table_to_list(ACPI_SIG_FACS, 0);
669		if (ACPI_FAILURE(status)) {
670			return (status);
671		}
672
673		/* Add all tables found in the memory */
674
675		status = osl_list_bios_tables();
676		if (ACPI_FAILURE(status)) {
677			return (status);
678		}
679	} else {
680		/* Add all tables found in the static directory */
681
682		status = osl_list_customized_tables(STATIC_TABLE_DIR);
683		if (ACPI_FAILURE(status)) {
684			return (status);
685		}
686	}
687
688	if (gbl_dump_dynamic_tables) {
689
690		/* Add all dynamically loaded tables in the dynamic directory */
691
692		status = osl_list_customized_tables(DYNAMIC_TABLE_DIR);
693		if (ACPI_FAILURE(status)) {
694			return (status);
695		}
696	}
697
698	gbl_table_list_initialized = TRUE;
699	return (AE_OK);
700}
701
702/******************************************************************************
703 *
704 * FUNCTION:    osl_list_bios_tables
705 *
706 * PARAMETERS:  None
707 *
708 * RETURN:      Status; Table list is initialized if AE_OK.
709 *
710 * DESCRIPTION: Add ACPI tables to the table list from memory.
711 *
712 * NOTE:        This works on Linux as table customization does not modify the
713 *              addresses stored in RSDP/RSDT/XSDT/FADT.
714 *
715 *****************************************************************************/
716
717static acpi_status osl_list_bios_tables(void)
718{
719	struct acpi_table_header *mapped_table = NULL;
720	u8 *table_data;
721	u8 number_of_tables;
722	u8 item_size;
723	acpi_physical_address table_address = 0;
724	acpi_status status = AE_OK;
725	u32 i;
726
727	if (osl_can_use_xsdt()) {
728		item_size = sizeof(u64);
729		table_data =
730		    ACPI_CAST8(gbl_xsdt) + sizeof(struct acpi_table_header);
731		number_of_tables =
732		    (u8)((gbl_xsdt->header.length -
733			  sizeof(struct acpi_table_header))
734			 / item_size);
735	} else {		/* Use RSDT if XSDT is not available */
736
737		item_size = sizeof(u32);
738		table_data =
739		    ACPI_CAST8(gbl_rsdt) + sizeof(struct acpi_table_header);
740		number_of_tables =
741		    (u8)((gbl_rsdt->header.length -
742			  sizeof(struct acpi_table_header))
743			 / item_size);
744	}
745
746	/* Search RSDT/XSDT for the requested table */
747
748	for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
749		if (osl_can_use_xsdt()) {
750			table_address =
751			    (acpi_physical_address) (*ACPI_CAST64(table_data));
752		} else {
753			table_address =
754			    (acpi_physical_address) (*ACPI_CAST32(table_data));
755		}
756
757		/* Skip NULL entries in RSDT/XSDT */
758
759		if (!table_address) {
760			continue;
761		}
762
763		status = osl_map_table(table_address, NULL, &mapped_table);
764		if (ACPI_FAILURE(status)) {
765			return (status);
766		}
767
768		osl_add_table_to_list(mapped_table->signature, 0);
769		osl_unmap_table(mapped_table);
770	}
771
772	return (AE_OK);
773}
774
775/******************************************************************************
776 *
777 * FUNCTION:    osl_get_bios_table
778 *
779 * PARAMETERS:  signature       - ACPI Signature for common table. Must be
780 *                                a null terminated 4-character string.
781 *              instance        - Multiple table support for SSDT/UEFI (0...n)
782 *                                Must be 0 for other tables.
783 *              table           - Where a pointer to the table is returned
784 *              address         - Where the table physical address is returned
785 *
786 * RETURN:      Status; Table buffer and physical address returned if AE_OK.
787 *              AE_LIMIT: Instance is beyond valid limit
788 *              AE_NOT_FOUND: A table with the signature was not found
789 *
790 * DESCRIPTION: Get a BIOS provided ACPI table
791 *
792 * NOTE:        Assumes the input signature is uppercase.
793 *
794 *****************************************************************************/
795
796static acpi_status
797osl_get_bios_table(char *signature,
798		   u32 instance,
799		   struct acpi_table_header **table,
800		   acpi_physical_address * address)
801{
802	struct acpi_table_header *local_table = NULL;
803	struct acpi_table_header *mapped_table = NULL;
804	u8 *table_data;
805	u8 number_of_tables;
806	u8 item_size;
807	u32 current_instance = 0;
808	acpi_physical_address table_address = 0;
809	u32 table_length = 0;
810	acpi_status status = AE_OK;
811	u32 i;
812
813	/* Handle special tables whose addresses are not in RSDT/XSDT */
814
815	if (ACPI_COMPARE_NAME(signature, ACPI_RSDP_NAME) ||
816	    ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT) ||
817	    ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT) ||
818	    ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT) ||
819	    ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
820		if (instance > 0) {
821			return (AE_LIMIT);
822		}
823
824		/*
825		 * Get the appropriate address, either 32-bit or 64-bit. Be very
826		 * careful about the FADT length and validate table addresses.
827		 * Note: The 64-bit addresses have priority.
828		 */
829		if (ACPI_COMPARE_NAME(signature, ACPI_SIG_DSDT)) {
830			if ((gbl_fadt->header.length >= MIN_FADT_FOR_XDSDT) &&
831			    gbl_fadt->Xdsdt) {
832				table_address =
833				    (acpi_physical_address) gbl_fadt->Xdsdt;
834			} else
835			    if ((gbl_fadt->header.length >= MIN_FADT_FOR_DSDT)
836				&& gbl_fadt->dsdt) {
837				table_address =
838				    (acpi_physical_address) gbl_fadt->dsdt;
839			}
840		} else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_FACS)) {
841			if ((gbl_fadt->header.length >= MIN_FADT_FOR_XFACS) &&
842			    gbl_fadt->Xfacs) {
843				table_address =
844				    (acpi_physical_address) gbl_fadt->Xfacs;
845			} else
846			    if ((gbl_fadt->header.length >= MIN_FADT_FOR_FACS)
847				&& gbl_fadt->facs) {
848				table_address =
849				    (acpi_physical_address) gbl_fadt->facs;
850			}
851		} else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_XSDT)) {
852			if (!gbl_revision) {
853				return (AE_BAD_SIGNATURE);
854			}
855			table_address =
856			    (acpi_physical_address) gbl_rsdp.
857			    xsdt_physical_address;
858		} else if (ACPI_COMPARE_NAME(signature, ACPI_SIG_RSDT)) {
859			table_address =
860			    (acpi_physical_address) gbl_rsdp.
861			    rsdt_physical_address;
862		} else {
863			table_address =
864			    (acpi_physical_address) gbl_rsdp_address;
865			signature = ACPI_SIG_RSDP;
866		}
867
868		/* Now we can get the requested special table */
869
870		status = osl_map_table(table_address, signature, &mapped_table);
871		if (ACPI_FAILURE(status)) {
872			return (status);
873		}
874
875		table_length = ap_get_table_length(mapped_table);
876	} else {		/* Case for a normal ACPI table */
877
878		if (osl_can_use_xsdt()) {
879			item_size = sizeof(u64);
880			table_data =
881			    ACPI_CAST8(gbl_xsdt) +
882			    sizeof(struct acpi_table_header);
883			number_of_tables =
884			    (u8)((gbl_xsdt->header.length -
885				  sizeof(struct acpi_table_header))
886				 / item_size);
887		} else {	/* Use RSDT if XSDT is not available */
888
889			item_size = sizeof(u32);
890			table_data =
891			    ACPI_CAST8(gbl_rsdt) +
892			    sizeof(struct acpi_table_header);
893			number_of_tables =
894			    (u8)((gbl_rsdt->header.length -
895				  sizeof(struct acpi_table_header))
896				 / item_size);
897		}
898
899		/* Search RSDT/XSDT for the requested table */
900
901		for (i = 0; i < number_of_tables; ++i, table_data += item_size) {
902			if (osl_can_use_xsdt()) {
903				table_address =
904				    (acpi_physical_address) (*ACPI_CAST64
905							     (table_data));
906			} else {
907				table_address =
908				    (acpi_physical_address) (*ACPI_CAST32
909							     (table_data));
910			}
911
912			/* Skip NULL entries in RSDT/XSDT */
913
914			if (!table_address) {
915				continue;
916			}
917
918			status =
919			    osl_map_table(table_address, NULL, &mapped_table);
920			if (ACPI_FAILURE(status)) {
921				return (status);
922			}
923			table_length = mapped_table->length;
924
925			/* Does this table match the requested signature? */
926
927			if (!ACPI_COMPARE_NAME
928			    (mapped_table->signature, signature)) {
929				osl_unmap_table(mapped_table);
930				mapped_table = NULL;
931				continue;
932			}
933
934			/* Match table instance (for SSDT/UEFI tables) */
935
936			if (current_instance != instance) {
937				osl_unmap_table(mapped_table);
938				mapped_table = NULL;
939				current_instance++;
940				continue;
941			}
942
943			break;
944		}
945	}
946
947	if (!mapped_table) {
948		return (AE_LIMIT);
949	}
950
951	if (table_length == 0) {
952		status = AE_BAD_HEADER;
953		goto exit;
954	}
955
956	/* Copy table to local buffer and return it */
957
958	local_table = calloc(1, table_length);
959	if (!local_table) {
960		status = AE_NO_MEMORY;
961		goto exit;
962	}
963
964	ACPI_MEMCPY(local_table, mapped_table, table_length);
965	*address = table_address;
966	*table = local_table;
967
968exit:
969	osl_unmap_table(mapped_table);
970	return (status);
971}
972
973/******************************************************************************
974 *
975 * FUNCTION:    osl_list_customized_tables
976 *
977 * PARAMETERS:  directory           - Directory that contains the tables
978 *
979 * RETURN:      Status; Table list is initialized if AE_OK.
980 *
981 * DESCRIPTION: Add ACPI tables to the table list from a directory.
982 *
983 *****************************************************************************/
984
985static acpi_status osl_list_customized_tables(char *directory)
986{
987	void *table_dir;
988	u32 instance;
989	char temp_name[ACPI_NAME_SIZE];
990	char *filename;
991	acpi_status status = AE_OK;
992
993	/* Open the requested directory */
994
995	table_dir = acpi_os_open_directory(directory, "*", REQUEST_FILE_ONLY);
996	if (!table_dir) {
997		return (osl_get_last_status(AE_NOT_FOUND));
998	}
999
1000	/* Examine all entries in this directory */
1001
1002	while ((filename = acpi_os_get_next_filename(table_dir))) {
1003
1004		/* Extract table name and instance number */
1005
1006		status =
1007		    osl_table_name_from_file(filename, temp_name, &instance);
1008
1009		/* Ignore meaningless files */
1010
1011		if (ACPI_FAILURE(status)) {
1012			continue;
1013		}
1014
1015		/* Add new info node to global table list */
1016
1017		status = osl_add_table_to_list(temp_name, instance);
1018		if (ACPI_FAILURE(status)) {
1019			break;
1020		}
1021	}
1022
1023	acpi_os_close_directory(table_dir);
1024	return (status);
1025}
1026
1027/******************************************************************************
1028 *
1029 * FUNCTION:    osl_map_table
1030 *
1031 * PARAMETERS:  address             - Address of the table in memory
1032 *              signature           - Optional ACPI Signature for desired table.
1033 *                                    Null terminated 4-character string.
1034 *              table               - Where a pointer to the mapped table is
1035 *                                    returned
1036 *
1037 * RETURN:      Status; Mapped table is returned if AE_OK.
1038 *              AE_NOT_FOUND: A valid table was not found at the address
1039 *
1040 * DESCRIPTION: Map entire ACPI table into caller's address space.
1041 *
1042 *****************************************************************************/
1043
1044static acpi_status
1045osl_map_table(acpi_size address,
1046	      char *signature, struct acpi_table_header **table)
1047{
1048	struct acpi_table_header *mapped_table;
1049	u32 length;
1050
1051	if (!address) {
1052		return (AE_BAD_ADDRESS);
1053	}
1054
1055	/*
1056	 * Map the header so we can get the table length.
1057	 * Use sizeof (struct acpi_table_header) as:
1058	 * 1. it is bigger than 24 to include RSDP->Length
1059	 * 2. it is smaller than sizeof (struct acpi_table_rsdp)
1060	 */
1061	mapped_table =
1062	    acpi_os_map_memory(address, sizeof(struct acpi_table_header));
1063	if (!mapped_table) {
1064		fprintf(stderr, "Could not map table header at 0x%8.8X%8.8X\n",
1065			ACPI_FORMAT_UINT64(address));
1066		return (osl_get_last_status(AE_BAD_ADDRESS));
1067	}
1068
1069	/* If specified, signature must match */
1070
1071	if (signature) {
1072		if (ACPI_VALIDATE_RSDP_SIG(signature)) {
1073			if (!ACPI_VALIDATE_RSDP_SIG(mapped_table->signature)) {
1074				acpi_os_unmap_memory(mapped_table,
1075						     sizeof(struct
1076							    acpi_table_header));
1077				return (AE_BAD_SIGNATURE);
1078			}
1079		} else
1080		    if (!ACPI_COMPARE_NAME(signature, mapped_table->signature))
1081		{
1082			acpi_os_unmap_memory(mapped_table,
1083					     sizeof(struct acpi_table_header));
1084			return (AE_BAD_SIGNATURE);
1085		}
1086	}
1087
1088	/* Map the entire table */
1089
1090	length = ap_get_table_length(mapped_table);
1091	acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));
1092	if (length == 0) {
1093		return (AE_BAD_HEADER);
1094	}
1095
1096	mapped_table = acpi_os_map_memory(address, length);
1097	if (!mapped_table) {
1098		fprintf(stderr,
1099			"Could not map table at 0x%8.8X%8.8X length %8.8X\n",
1100			ACPI_FORMAT_UINT64(address), length);
1101		return (osl_get_last_status(AE_INVALID_TABLE_LENGTH));
1102	}
1103
1104	(void)ap_is_valid_checksum(mapped_table);
1105
1106	*table = mapped_table;
1107	return (AE_OK);
1108}
1109
1110/******************************************************************************
1111 *
1112 * FUNCTION:    osl_unmap_table
1113 *
1114 * PARAMETERS:  table               - A pointer to the mapped table
1115 *
1116 * RETURN:      None
1117 *
1118 * DESCRIPTION: Unmap entire ACPI table.
1119 *
1120 *****************************************************************************/
1121
1122static void osl_unmap_table(struct acpi_table_header *table)
1123{
1124	if (table) {
1125		acpi_os_unmap_memory(table, ap_get_table_length(table));
1126	}
1127}
1128
1129/******************************************************************************
1130 *
1131 * FUNCTION:    osl_table_name_from_file
1132 *
1133 * PARAMETERS:  filename            - File that contains the desired table
1134 *              signature           - Pointer to 4-character buffer to store
1135 *                                    extracted table signature.
1136 *              instance            - Pointer to integer to store extracted
1137 *                                    table instance number.
1138 *
1139 * RETURN:      Status; Table name is extracted if AE_OK.
1140 *
1141 * DESCRIPTION: Extract table signature and instance number from a table file
1142 *              name.
1143 *
1144 *****************************************************************************/
1145
1146static acpi_status
1147osl_table_name_from_file(char *filename, char *signature, u32 *instance)
1148{
1149
1150	/* Ignore meaningless files */
1151
1152	if (strlen(filename) < ACPI_NAME_SIZE) {
1153		return (AE_BAD_SIGNATURE);
1154	}
1155
1156	/* Extract instance number */
1157
1158	if (isdigit((int)filename[ACPI_NAME_SIZE])) {
1159		sscanf(&filename[ACPI_NAME_SIZE], "%u", instance);
1160	} else if (strlen(filename) != ACPI_NAME_SIZE) {
1161		return (AE_BAD_SIGNATURE);
1162	} else {
1163		*instance = 0;
1164	}
1165
1166	/* Extract signature */
1167
1168	ACPI_MOVE_NAME(signature, filename);
1169	return (AE_OK);
1170}
1171
1172/******************************************************************************
1173 *
1174 * FUNCTION:    osl_read_table_from_file
1175 *
1176 * PARAMETERS:  filename            - File that contains the desired table
1177 *              file_offset         - Offset of the table in file
1178 *              signature           - Optional ACPI Signature for desired table.
1179 *                                    A null terminated 4-character string.
1180 *              table               - Where a pointer to the table is returned
1181 *
1182 * RETURN:      Status; Table buffer is returned if AE_OK.
1183 *
1184 * DESCRIPTION: Read a ACPI table from a file.
1185 *
1186 *****************************************************************************/
1187
1188static acpi_status
1189osl_read_table_from_file(char *filename,
1190			 acpi_size file_offset,
1191			 char *signature, struct acpi_table_header **table)
1192{
1193	FILE *table_file;
1194	struct acpi_table_header header;
1195	struct acpi_table_header *local_table = NULL;
1196	u32 table_length;
1197	s32 count;
1198	acpi_status status = AE_OK;
1199
1200	/* Open the file */
1201
1202	table_file = fopen(filename, "rb");
1203	if (table_file == NULL) {
1204		fprintf(stderr, "Could not open table file: %s\n", filename);
1205		return (osl_get_last_status(AE_NOT_FOUND));
1206	}
1207
1208	fseek(table_file, file_offset, SEEK_SET);
1209
1210	/* Read the Table header to get the table length */
1211
1212	count = fread(&header, 1, sizeof(struct acpi_table_header), table_file);
1213	if (count != sizeof(struct acpi_table_header)) {
1214		fprintf(stderr, "Could not read table header: %s\n", filename);
1215		status = AE_BAD_HEADER;
1216		goto exit;
1217	}
1218
1219	/* If signature is specified, it must match the table */
1220
1221	if (signature) {
1222		if (ACPI_VALIDATE_RSDP_SIG(signature)) {
1223			if (!ACPI_VALIDATE_RSDP_SIG(header.signature)) {
1224				fprintf(stderr,
1225					"Incorrect RSDP signature: found %8.8s\n",
1226					header.signature);
1227				status = AE_BAD_SIGNATURE;
1228				goto exit;
1229			}
1230		} else if (!ACPI_COMPARE_NAME(signature, header.signature)) {
1231			fprintf(stderr,
1232				"Incorrect signature: Expecting %4.4s, found %4.4s\n",
1233				signature, header.signature);
1234			status = AE_BAD_SIGNATURE;
1235			goto exit;
1236		}
1237	}
1238
1239	table_length = ap_get_table_length(&header);
1240	if (table_length == 0) {
1241		status = AE_BAD_HEADER;
1242		goto exit;
1243	}
1244
1245	/* Read the entire table into a local buffer */
1246
1247	local_table = calloc(1, table_length);
1248	if (!local_table) {
1249		fprintf(stderr,
1250			"%4.4s: Could not allocate buffer for table of length %X\n",
1251			header.signature, table_length);
1252		status = AE_NO_MEMORY;
1253		goto exit;
1254	}
1255
1256	fseek(table_file, file_offset, SEEK_SET);
1257
1258	count = fread(local_table, 1, table_length, table_file);
1259	if (count != table_length) {
1260		fprintf(stderr, "%4.4s: Could not read table content\n",
1261			header.signature);
1262		status = AE_INVALID_TABLE_LENGTH;
1263		goto exit;
1264	}
1265
1266	/* Validate checksum */
1267
1268	(void)ap_is_valid_checksum(local_table);
1269
1270exit:
1271	fclose(table_file);
1272	*table = local_table;
1273	return (status);
1274}
1275
1276/******************************************************************************
1277 *
1278 * FUNCTION:    osl_get_customized_table
1279 *
1280 * PARAMETERS:  pathname        - Directory to find Linux customized table
1281 *              signature       - ACPI Signature for desired table. Must be
1282 *                                a null terminated 4-character string.
1283 *              instance        - Multiple table support for SSDT/UEFI (0...n)
1284 *                                Must be 0 for other tables.
1285 *              table           - Where a pointer to the table is returned
1286 *              address         - Where the table physical address is returned
1287 *
1288 * RETURN:      Status; Table buffer is returned if AE_OK.
1289 *              AE_LIMIT: Instance is beyond valid limit
1290 *              AE_NOT_FOUND: A table with the signature was not found
1291 *
1292 * DESCRIPTION: Get an OS customized table.
1293 *
1294 *****************************************************************************/
1295
1296static acpi_status
1297osl_get_customized_table(char *pathname,
1298			 char *signature,
1299			 u32 instance,
1300			 struct acpi_table_header **table,
1301			 acpi_physical_address * address)
1302{
1303	void *table_dir;
1304	u32 current_instance = 0;
1305	char temp_name[ACPI_NAME_SIZE];
1306	char table_filename[PATH_MAX];
1307	char *filename;
1308	acpi_status status;
1309
1310	/* Open the directory for customized tables */
1311
1312	table_dir = acpi_os_open_directory(pathname, "*", REQUEST_FILE_ONLY);
1313	if (!table_dir) {
1314		return (osl_get_last_status(AE_NOT_FOUND));
1315	}
1316
1317	/* Attempt to find the table in the directory */
1318
1319	while ((filename = acpi_os_get_next_filename(table_dir))) {
1320
1321		/* Ignore meaningless files */
1322
1323		if (!ACPI_COMPARE_NAME(filename, signature)) {
1324			continue;
1325		}
1326
1327		/* Extract table name and instance number */
1328
1329		status =
1330		    osl_table_name_from_file(filename, temp_name,
1331					     &current_instance);
1332
1333		/* Ignore meaningless files */
1334
1335		if (ACPI_FAILURE(status) || current_instance != instance) {
1336			continue;
1337		}
1338
1339		/* Create the table pathname */
1340
1341		if (instance != 0) {
1342			sprintf(table_filename, "%s/%4.4s%d", pathname,
1343				temp_name, instance);
1344		} else {
1345			sprintf(table_filename, "%s/%4.4s", pathname,
1346				temp_name);
1347		}
1348		break;
1349	}
1350
1351	acpi_os_close_directory(table_dir);
1352
1353	if (!filename) {
1354		return (AE_LIMIT);
1355	}
1356
1357	/* There is no physical address saved for customized tables, use zero */
1358
1359	*address = 0;
1360	status = osl_read_table_from_file(table_filename, 0, NULL, table);
1361
1362	return (status);
1363}
1364