1 /******************************************************************************
2  *
3  * Module Name: osefitbl - EFI OSL for obtaining ACPI tables
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2023, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include "acpidump.h"
153 
154 
155 #define _COMPONENT          ACPI_OS_SERVICES
156         ACPI_MODULE_NAME    ("osefitbl")
157 
158 
159 #ifndef PATH_MAX
160 #define PATH_MAX 256
161 #endif
162 
163 
164 /* List of information about obtained ACPI tables */
165 
166 typedef struct osl_table_info
167 {
168     struct osl_table_info   *Next;
169     UINT32                  Instance;
170     char                    Signature[ACPI_NAMESEG_SIZE];
171 
172 } OSL_TABLE_INFO;
173 
174 /* Local prototypes */
175 
176 static ACPI_STATUS
177 OslTableInitialize (
178     void);
179 
180 static ACPI_STATUS
181 OslAddTableToList (
182     char                    *Signature,
183     UINT32                  Instance);
184 
185 static ACPI_STATUS
186 OslMapTable (
187     ACPI_PHYSICAL_ADDRESS   Address,
188     char                    *Signature,
189     ACPI_TABLE_HEADER       **Table);
190 
191 static void
192 OslUnmapTable (
193     ACPI_TABLE_HEADER       *Table);
194 
195 static ACPI_STATUS
196 OslLoadRsdp (
197     void);
198 
199 static ACPI_STATUS
200 OslListTables (
201     void);
202 
203 static ACPI_STATUS
204 OslGetTable (
205     char                    *Signature,
206     UINT32                  Instance,
207     ACPI_TABLE_HEADER       **Table,
208     ACPI_PHYSICAL_ADDRESS   *Address);
209 
210 
211 /* File locations */
212 
213 #define EFI_SYSTAB          "/sys/firmware/efi/systab"
214 
215 /* Initialization flags */
216 
217 UINT8                   Gbl_TableListInitialized = FALSE;
218 
219 /* Local copies of main ACPI tables */
220 
221 ACPI_TABLE_RSDP         Gbl_Rsdp;
222 ACPI_TABLE_FADT         *Gbl_Fadt = NULL;
223 ACPI_TABLE_RSDT         *Gbl_Rsdt = NULL;
224 ACPI_TABLE_XSDT         *Gbl_Xsdt = NULL;
225 
226 /* Table addresses */
227 
228 ACPI_PHYSICAL_ADDRESS   Gbl_FadtAddress = 0;
229 ACPI_PHYSICAL_ADDRESS   Gbl_RsdpAddress = 0;
230 
231 /* Revision of RSD PTR */
232 
233 UINT8                   Gbl_Revision = 0;
234 
235 OSL_TABLE_INFO          *Gbl_TableListHead = NULL;
236 UINT32                  Gbl_TableCount = 0;
237 
238 
239 /******************************************************************************
240  *
241  * FUNCTION:    AcpiOsGetTableByAddress
242  *
243  * PARAMETERS:  Address         - Physical address of the ACPI table
244  *              Table           - Where a pointer to the table is returned
245  *
246  * RETURN:      Status; Table buffer is returned if AE_OK.
247  *              AE_NOT_FOUND: A valid table was not found at the address
248  *
249  * DESCRIPTION: Get an ACPI table via a physical memory address.
250  *
251  *****************************************************************************/
252 
253 ACPI_STATUS
AcpiOsGetTableByAddress(ACPI_PHYSICAL_ADDRESS Address,ACPI_TABLE_HEADER ** Table)254 AcpiOsGetTableByAddress (
255     ACPI_PHYSICAL_ADDRESS   Address,
256     ACPI_TABLE_HEADER       **Table)
257 {
258     UINT32                  TableLength;
259     ACPI_TABLE_HEADER       *MappedTable;
260     ACPI_TABLE_HEADER       *LocalTable = NULL;
261     ACPI_STATUS             Status = AE_OK;
262 
263 
264     /* Get main ACPI tables from memory on first invocation of this function */
265 
266     Status = OslTableInitialize ();
267     if (ACPI_FAILURE (Status))
268     {
269         return (Status);
270     }
271 
272     /* Map the table and validate it */
273 
274     Status = OslMapTable (Address, NULL, &MappedTable);
275     if (ACPI_FAILURE (Status))
276     {
277         return (Status);
278     }
279 
280     /* Copy table to local buffer and return it */
281 
282     TableLength = ApGetTableLength (MappedTable);
283     if (TableLength == 0)
284     {
285         Status = AE_BAD_HEADER;
286         goto Exit;
287     }
288 
289     LocalTable = ACPI_ALLOCATE_ZEROED (TableLength);
290     if (!LocalTable)
291     {
292         Status = AE_NO_MEMORY;
293         goto Exit;
294     }
295 
296     memcpy (LocalTable, MappedTable, TableLength);
297 
298 Exit:
299     OslUnmapTable (MappedTable);
300     *Table = LocalTable;
301     return (Status);
302 }
303 
304 
305 /******************************************************************************
306  *
307  * FUNCTION:    AcpiOsGetTableByName
308  *
309  * PARAMETERS:  Signature       - ACPI Signature for desired table. Must be
310  *                                a null terminated 4-character string.
311  *              Instance        - Multiple table support for SSDT/UEFI (0...n)
312  *                                Must be 0 for other tables.
313  *              Table           - Where a pointer to the table is returned
314  *              Address         - Where the table physical address is returned
315  *
316  * RETURN:      Status; Table buffer and physical address returned if AE_OK.
317  *              AE_LIMIT: Instance is beyond valid limit
318  *              AE_NOT_FOUND: A table with the signature was not found
319  *
320  * NOTE:        Assumes the input signature is uppercase.
321  *
322  *****************************************************************************/
323 
324 ACPI_STATUS
AcpiOsGetTableByName(char * Signature,UINT32 Instance,ACPI_TABLE_HEADER ** Table,ACPI_PHYSICAL_ADDRESS * Address)325 AcpiOsGetTableByName (
326     char                    *Signature,
327     UINT32                  Instance,
328     ACPI_TABLE_HEADER       **Table,
329     ACPI_PHYSICAL_ADDRESS   *Address)
330 {
331     ACPI_STATUS             Status;
332 
333 
334     /* Get main ACPI tables from memory on first invocation of this function */
335 
336     Status = OslTableInitialize ();
337     if (ACPI_FAILURE (Status))
338     {
339         return (Status);
340     }
341 
342     /* Not a main ACPI table, attempt to extract it from the RSDT/XSDT */
343 
344     Status = OslGetTable (Signature, Instance, Table, Address);
345 
346     return (Status);
347 }
348 
349 
350 /******************************************************************************
351  *
352  * FUNCTION:    OslAddTableToList
353  *
354  * PARAMETERS:  Signature       - Table signature
355  *              Instance        - Table instance
356  *
357  * RETURN:      Status; Successfully added if AE_OK.
358  *              AE_NO_MEMORY: Memory allocation error
359  *
360  * DESCRIPTION: Insert a table structure into OSL table list.
361  *
362  *****************************************************************************/
363 
364 static ACPI_STATUS
OslAddTableToList(char * Signature,UINT32 Instance)365 OslAddTableToList (
366     char                    *Signature,
367     UINT32                  Instance)
368 {
369     OSL_TABLE_INFO          *NewInfo;
370     OSL_TABLE_INFO          *Next;
371     UINT32                  NextInstance = 0;
372     BOOLEAN                 Found = FALSE;
373 
374 
375     NewInfo = ACPI_ALLOCATE_ZEROED (sizeof (OSL_TABLE_INFO));
376     if (!NewInfo)
377     {
378         return (AE_NO_MEMORY);
379     }
380 
381     ACPI_COPY_NAMESEG (NewInfo->Signature, Signature);
382 
383     if (!Gbl_TableListHead)
384     {
385         Gbl_TableListHead = NewInfo;
386     }
387     else
388     {
389         Next = Gbl_TableListHead;
390         while (1)
391         {
392             if (ACPI_COMPARE_NAMESEG (Next->Signature, Signature))
393             {
394                 if (Next->Instance == Instance)
395                 {
396                     Found = TRUE;
397                 }
398                 if (Next->Instance >= NextInstance)
399                 {
400                     NextInstance = Next->Instance + 1;
401                 }
402             }
403 
404             if (!Next->Next)
405             {
406                 break;
407             }
408             Next = Next->Next;
409         }
410         Next->Next = NewInfo;
411     }
412 
413     if (Found)
414     {
415         if (Instance)
416         {
417             fprintf (stderr,
418                 "%4.4s: Warning unmatched table instance %d, expected %d\n",
419                 Signature, Instance, NextInstance);
420         }
421         Instance = NextInstance;
422     }
423 
424     NewInfo->Instance = Instance;
425     Gbl_TableCount++;
426 
427     return (AE_OK);
428 }
429 
430 
431 /******************************************************************************
432  *
433  * FUNCTION:    AcpiOsGetTableByIndex
434  *
435  * PARAMETERS:  Index           - Which table to get
436  *              Table           - Where a pointer to the table is returned
437  *              Instance        - Where a pointer to the table instance no. is
438  *                                returned
439  *              Address         - Where the table physical address is returned
440  *
441  * RETURN:      Status; Table buffer and physical address returned if AE_OK.
442  *              AE_LIMIT: Index is beyond valid limit
443  *
444  * DESCRIPTION: Get an ACPI table via an index value (0 through n). Returns
445  *              AE_LIMIT when an invalid index is reached. Index is not
446  *              necessarily an index into the RSDT/XSDT.
447  *
448  *****************************************************************************/
449 
450 ACPI_STATUS
AcpiOsGetTableByIndex(UINT32 Index,ACPI_TABLE_HEADER ** Table,UINT32 * Instance,ACPI_PHYSICAL_ADDRESS * Address)451 AcpiOsGetTableByIndex (
452     UINT32                  Index,
453     ACPI_TABLE_HEADER       **Table,
454     UINT32                  *Instance,
455     ACPI_PHYSICAL_ADDRESS   *Address)
456 {
457     OSL_TABLE_INFO          *Info;
458     ACPI_STATUS             Status;
459     UINT32                  i;
460 
461 
462     /* Get main ACPI tables from memory on first invocation of this function */
463 
464     Status = OslTableInitialize ();
465     if (ACPI_FAILURE (Status))
466     {
467         return (Status);
468     }
469 
470     /* Validate Index */
471 
472     if (Index >= Gbl_TableCount)
473     {
474         return (AE_LIMIT);
475     }
476 
477     /* Point to the table list entry specified by the Index argument */
478 
479     Info = Gbl_TableListHead;
480     for (i = 0; i < Index; i++)
481     {
482         Info = Info->Next;
483     }
484 
485     /* Now we can just get the table via the signature */
486 
487     Status = AcpiOsGetTableByName (Info->Signature, Info->Instance,
488         Table, Address);
489 
490     if (ACPI_SUCCESS (Status))
491     {
492         *Instance = Info->Instance;
493     }
494     return (Status);
495 }
496 
497 
498 /******************************************************************************
499  *
500  * FUNCTION:    OslLoadRsdp
501  *
502  * PARAMETERS:  None
503  *
504  * RETURN:      Status
505  *
506  * DESCRIPTION: Scan and load RSDP.
507  *
508  *****************************************************************************/
509 
510 static ACPI_STATUS
OslLoadRsdp(void)511 OslLoadRsdp (
512     void)
513 {
514     ACPI_TABLE_HEADER       *MappedTable;
515     UINT8                   *RsdpAddress;
516     ACPI_PHYSICAL_ADDRESS   RsdpBase;
517     ACPI_SIZE               RsdpSize;
518 
519 
520     /* Get RSDP from memory */
521 
522     RsdpSize = sizeof (ACPI_TABLE_RSDP);
523     if (Gbl_RsdpBase)
524     {
525         RsdpBase = Gbl_RsdpBase;
526     }
527     else
528     {
529         RsdpBase = AcpiOsGetRootPointer ();
530     }
531 
532     if (!RsdpBase)
533     {
534         RsdpBase = ACPI_HI_RSDP_WINDOW_BASE;
535         RsdpSize = ACPI_HI_RSDP_WINDOW_SIZE;
536     }
537 
538     RsdpAddress = AcpiOsMapMemory (RsdpBase, RsdpSize);
539     if (!RsdpAddress)
540     {
541         return (AE_BAD_ADDRESS);
542     }
543 
544     /* Search low memory for the RSDP */
545 
546     MappedTable = ACPI_CAST_PTR (ACPI_TABLE_HEADER,
547         AcpiTbScanMemoryForRsdp (RsdpAddress, (UINT32) RsdpSize));
548     if (!MappedTable)
549     {
550         AcpiOsUnmapMemory (RsdpAddress, RsdpSize);
551         return (AE_NOT_FOUND);
552     }
553 
554     Gbl_RsdpAddress = RsdpBase + (ACPI_CAST8 (MappedTable) - RsdpAddress);
555 
556     memcpy (&Gbl_Rsdp, MappedTable, sizeof (ACPI_TABLE_RSDP));
557     AcpiOsUnmapMemory (RsdpAddress, RsdpSize);
558 
559     return (AE_OK);
560 }
561 
562 
563 /******************************************************************************
564  *
565  * FUNCTION:    OslCanUseXsdt
566  *
567  * PARAMETERS:  None
568  *
569  * RETURN:      TRUE if XSDT is allowed to be used.
570  *
571  * DESCRIPTION: This function collects logic that can be used to determine if
572  *              XSDT should be used instead of RSDT.
573  *
574  *****************************************************************************/
575 
576 static BOOLEAN
OslCanUseXsdt(void)577 OslCanUseXsdt (
578     void)
579 {
580     if (Gbl_Revision && !AcpiGbl_DoNotUseXsdt)
581     {
582         return (TRUE);
583     }
584     else
585     {
586         return (FALSE);
587     }
588 }
589 
590 
591 /******************************************************************************
592  *
593  * FUNCTION:    OslTableInitialize
594  *
595  * PARAMETERS:  None
596  *
597  * RETURN:      Status
598  *
599  * DESCRIPTION: Initialize ACPI table data. Get and store main ACPI tables to
600  *              local variables. Main ACPI tables include RSDT, FADT, RSDT,
601  *              and/or XSDT.
602  *
603  *****************************************************************************/
604 
605 static ACPI_STATUS
OslTableInitialize(void)606 OslTableInitialize (
607     void)
608 {
609     ACPI_STATUS             Status;
610     ACPI_PHYSICAL_ADDRESS   Address;
611 
612 
613     if (Gbl_TableListInitialized)
614     {
615         return (AE_OK);
616     }
617 
618     /* Get RSDP from memory */
619 
620     Status = OslLoadRsdp ();
621     if (ACPI_FAILURE (Status))
622     {
623         return (Status);
624     }
625 
626     /* Get XSDT from memory */
627 
628     if (Gbl_Rsdp.Revision && !Gbl_DoNotDumpXsdt)
629     {
630         if (Gbl_Xsdt)
631         {
632             ACPI_FREE (Gbl_Xsdt);
633             Gbl_Xsdt = NULL;
634         }
635 
636         Gbl_Revision = 2;
637         Status = OslGetTable (ACPI_SIG_XSDT, 0,
638             ACPI_CAST_PTR (ACPI_TABLE_HEADER *, &Gbl_Xsdt), &Address);
639         if (ACPI_FAILURE (Status))
640         {
641             return (Status);
642         }
643     }
644 
645     /* Get RSDT from memory */
646 
647     if (Gbl_Rsdp.RsdtPhysicalAddress)
648     {
649         if (Gbl_Rsdt)
650         {
651             ACPI_FREE (Gbl_Rsdt);
652             Gbl_Rsdt = NULL;
653         }
654 
655         Status = OslGetTable (ACPI_SIG_RSDT, 0,
656             ACPI_CAST_PTR (ACPI_TABLE_HEADER *, &Gbl_Rsdt), &Address);
657         if (ACPI_FAILURE (Status))
658         {
659             return (Status);
660         }
661     }
662 
663     /* Get FADT from memory */
664 
665     if (Gbl_Fadt)
666     {
667         ACPI_FREE (Gbl_Fadt);
668         Gbl_Fadt = NULL;
669     }
670 
671     Status = OslGetTable (ACPI_SIG_FADT, 0,
672         ACPI_CAST_PTR (ACPI_TABLE_HEADER *, &Gbl_Fadt), &Gbl_FadtAddress);
673     if (ACPI_FAILURE (Status))
674     {
675         return (Status);
676     }
677 
678     /* Add mandatory tables to global table list first */
679 
680     Status = OslAddTableToList (ACPI_RSDP_NAME, 0);
681     if (ACPI_FAILURE (Status))
682     {
683         return (Status);
684     }
685 
686     Status = OslAddTableToList (ACPI_SIG_RSDT, 0);
687     if (ACPI_FAILURE (Status))
688     {
689         return (Status);
690     }
691 
692     if (Gbl_Revision == 2)
693     {
694         Status = OslAddTableToList (ACPI_SIG_XSDT, 0);
695         if (ACPI_FAILURE (Status))
696         {
697             return (Status);
698         }
699     }
700 
701     Status = OslAddTableToList (ACPI_SIG_DSDT, 0);
702     if (ACPI_FAILURE (Status))
703     {
704         return (Status);
705     }
706 
707     Status = OslAddTableToList (ACPI_SIG_FACS, 0);
708     if (ACPI_FAILURE (Status))
709     {
710         return (Status);
711     }
712 
713     /* Add all tables found in the memory */
714 
715     Status = OslListTables ();
716     if (ACPI_FAILURE (Status))
717     {
718         return (Status);
719     }
720 
721     Gbl_TableListInitialized = TRUE;
722     return (AE_OK);
723 }
724 
725 
726 /******************************************************************************
727  *
728  * FUNCTION:    OslListTables
729  *
730  * PARAMETERS:  None
731  *
732  * RETURN:      Status; Table list is initialized if AE_OK.
733  *
734  * DESCRIPTION: Add ACPI tables to the table list from memory.
735  *
736  *****************************************************************************/
737 
738 static ACPI_STATUS
OslListTables(void)739 OslListTables (
740     void)
741 {
742     ACPI_TABLE_HEADER       *MappedTable = NULL;
743     UINT8                   *TableData;
744     UINT8                   NumberOfTables;
745     UINT8                   ItemSize;
746     ACPI_PHYSICAL_ADDRESS   TableAddress = 0;
747     ACPI_STATUS             Status = AE_OK;
748     UINT32                  i;
749 
750 
751     if (OslCanUseXsdt ())
752     {
753         ItemSize = sizeof (UINT64);
754         TableData = ACPI_CAST8 (Gbl_Xsdt) + sizeof (ACPI_TABLE_HEADER);
755         NumberOfTables =
756             (UINT8) ((Gbl_Xsdt->Header.Length - sizeof (ACPI_TABLE_HEADER))
757             / ItemSize);
758     }
759     else /* Use RSDT if XSDT is not available */
760     {
761         ItemSize = sizeof (UINT32);
762         TableData = ACPI_CAST8 (Gbl_Rsdt) + sizeof (ACPI_TABLE_HEADER);
763         NumberOfTables =
764             (UINT8) ((Gbl_Rsdt->Header.Length - sizeof (ACPI_TABLE_HEADER))
765             / ItemSize);
766     }
767 
768     /* Search RSDT/XSDT for the requested table */
769 
770     for (i = 0; i < NumberOfTables; ++i, TableData += ItemSize)
771     {
772         if (OslCanUseXsdt ())
773         {
774             TableAddress =
775                 (ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST64 (TableData));
776         }
777         else
778         {
779             TableAddress =
780                 (ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST32 (TableData));
781         }
782 
783         /* Skip NULL entries in RSDT/XSDT */
784 
785         if (TableAddress == 0)
786         {
787             continue;
788         }
789 
790         Status = OslMapTable (TableAddress, NULL, &MappedTable);
791         if (ACPI_FAILURE (Status))
792         {
793             return (Status);
794         }
795 
796         OslAddTableToList (MappedTable->Signature, 0);
797         OslUnmapTable (MappedTable);
798     }
799 
800     return (AE_OK);
801 }
802 
803 
804 /******************************************************************************
805  *
806  * FUNCTION:    OslGetTable
807  *
808  * PARAMETERS:  Signature       - ACPI Signature for common table. Must be
809  *                                a null terminated 4-character string.
810  *              Instance        - Multiple table support for SSDT/UEFI (0...n)
811  *                                Must be 0 for other tables.
812  *              Table           - Where a pointer to the table is returned
813  *              Address         - Where the table physical address is returned
814  *
815  * RETURN:      Status; Table buffer and physical address returned if AE_OK.
816  *              AE_LIMIT: Instance is beyond valid limit
817  *              AE_NOT_FOUND: A table with the signature was not found
818  *
819  * DESCRIPTION: Get a BIOS provided ACPI table
820  *
821  * NOTE:        Assumes the input signature is uppercase.
822  *
823  *****************************************************************************/
824 
825 static ACPI_STATUS
OslGetTable(char * Signature,UINT32 Instance,ACPI_TABLE_HEADER ** Table,ACPI_PHYSICAL_ADDRESS * Address)826 OslGetTable (
827     char                    *Signature,
828     UINT32                  Instance,
829     ACPI_TABLE_HEADER       **Table,
830     ACPI_PHYSICAL_ADDRESS   *Address)
831 {
832     ACPI_TABLE_HEADER       *LocalTable = NULL;
833     ACPI_TABLE_HEADER       *MappedTable = NULL;
834     UINT8                   *TableData;
835     UINT8                   NumberOfTables;
836     UINT8                   ItemSize;
837     UINT32                  CurrentInstance = 0;
838     ACPI_PHYSICAL_ADDRESS   TableAddress;
839     ACPI_PHYSICAL_ADDRESS   FirstTableAddress = 0;
840     UINT32                  TableLength = 0;
841     ACPI_STATUS             Status = AE_OK;
842     UINT32                  i;
843 
844 
845     /* Handle special tables whose addresses are not in RSDT/XSDT */
846 
847     if (ACPI_COMPARE_NAMESEG (Signature, ACPI_RSDP_NAME) ||
848         ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_RSDT) ||
849         ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_XSDT) ||
850         ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_DSDT) ||
851         ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_FACS))
852     {
853 
854 FindNextInstance:
855 
856         TableAddress = 0;
857 
858         /*
859          * Get the appropriate address, either 32-bit or 64-bit. Be very
860          * careful about the FADT length and validate table addresses.
861          * Note: The 64-bit addresses have priority.
862          */
863         if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_DSDT))
864         {
865             if (CurrentInstance < 2)
866             {
867                 if ((Gbl_Fadt->Header.Length >= MIN_FADT_FOR_XDSDT) &&
868                     Gbl_Fadt->XDsdt && CurrentInstance == 0)
869                 {
870                     TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_Fadt->XDsdt;
871                 }
872                 else if ((Gbl_Fadt->Header.Length >= MIN_FADT_FOR_DSDT) &&
873                     Gbl_Fadt->Dsdt != FirstTableAddress)
874                 {
875                     TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_Fadt->Dsdt;
876                 }
877             }
878         }
879         else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_FACS))
880         {
881             if (CurrentInstance < 2)
882             {
883                 if ((Gbl_Fadt->Header.Length >= MIN_FADT_FOR_XFACS) &&
884                     Gbl_Fadt->XFacs && CurrentInstance == 0)
885                 {
886                     TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_Fadt->XFacs;
887                 }
888                 else if ((Gbl_Fadt->Header.Length >= MIN_FADT_FOR_FACS) &&
889                     Gbl_Fadt->Facs != FirstTableAddress)
890                 {
891                     TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_Fadt->Facs;
892                 }
893             }
894         }
895         else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_XSDT))
896         {
897             if (!Gbl_Revision)
898             {
899                 return (AE_BAD_SIGNATURE);
900             }
901             if (CurrentInstance == 0)
902             {
903                 TableAddress =
904                     (ACPI_PHYSICAL_ADDRESS) Gbl_Rsdp.XsdtPhysicalAddress;
905             }
906         }
907         else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_RSDT))
908         {
909             if (CurrentInstance == 0)
910             {
911                 TableAddress =
912                     (ACPI_PHYSICAL_ADDRESS) Gbl_Rsdp.RsdtPhysicalAddress;
913             }
914         }
915         else
916         {
917             if (CurrentInstance == 0)
918             {
919                 TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_RsdpAddress;
920                 Signature = ACPI_SIG_RSDP;
921             }
922         }
923 
924         if (TableAddress == 0)
925         {
926             goto ExitFindTable;
927         }
928 
929         /* Now we can get the requested special table */
930 
931         Status = OslMapTable (TableAddress, Signature, &MappedTable);
932         if (ACPI_FAILURE (Status))
933         {
934             return (Status);
935         }
936 
937         TableLength = ApGetTableLength (MappedTable);
938         if (FirstTableAddress == 0)
939         {
940             FirstTableAddress = TableAddress;
941         }
942 
943         /* Match table instance */
944 
945         if (CurrentInstance != Instance)
946         {
947             OslUnmapTable (MappedTable);
948             MappedTable = NULL;
949             CurrentInstance++;
950             goto FindNextInstance;
951         }
952     }
953     else /* Case for a normal ACPI table */
954     {
955         if (OslCanUseXsdt ())
956         {
957             ItemSize = sizeof (UINT64);
958             TableData = ACPI_CAST8 (Gbl_Xsdt) + sizeof (ACPI_TABLE_HEADER);
959             NumberOfTables =
960                 (UINT8) ((Gbl_Xsdt->Header.Length - sizeof (ACPI_TABLE_HEADER))
961                 / ItemSize);
962         }
963         else /* Use RSDT if XSDT is not available */
964         {
965             ItemSize = sizeof (UINT32);
966             TableData = ACPI_CAST8 (Gbl_Rsdt) + sizeof (ACPI_TABLE_HEADER);
967             NumberOfTables =
968                 (UINT8) ((Gbl_Rsdt->Header.Length - sizeof (ACPI_TABLE_HEADER))
969                 / ItemSize);
970         }
971 
972         /* Search RSDT/XSDT for the requested table */
973 
974         for (i = 0; i < NumberOfTables; ++i, TableData += ItemSize)
975         {
976             if (OslCanUseXsdt ())
977             {
978                 TableAddress =
979                     (ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST64 (TableData));
980             }
981             else
982             {
983                 TableAddress =
984                     (ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST32 (TableData));
985             }
986 
987             /* Skip NULL entries in RSDT/XSDT */
988 
989             if (TableAddress == 0)
990             {
991                 continue;
992             }
993 
994             Status = OslMapTable (TableAddress, NULL, &MappedTable);
995             if (ACPI_FAILURE (Status))
996             {
997                 return (Status);
998             }
999             TableLength = MappedTable->Length;
1000 
1001             /* Does this table match the requested signature? */
1002 
1003             if (!ACPI_COMPARE_NAMESEG (MappedTable->Signature, Signature))
1004             {
1005                 OslUnmapTable (MappedTable);
1006                 MappedTable = NULL;
1007                 continue;
1008             }
1009 
1010             /* Match table instance (for SSDT/UEFI tables) */
1011 
1012             if (CurrentInstance != Instance)
1013             {
1014                 OslUnmapTable (MappedTable);
1015                 MappedTable = NULL;
1016                 CurrentInstance++;
1017                 continue;
1018             }
1019 
1020             break;
1021         }
1022     }
1023 
1024 ExitFindTable:
1025 
1026     if (!MappedTable)
1027     {
1028         return (AE_LIMIT);
1029     }
1030 
1031     if (TableLength == 0)
1032     {
1033         Status = AE_BAD_HEADER;
1034         goto Exit;
1035     }
1036 
1037     /* Copy table to local buffer and return it */
1038 
1039     LocalTable = ACPI_ALLOCATE_ZEROED (TableLength);
1040     if (!LocalTable)
1041     {
1042         Status = AE_NO_MEMORY;
1043         goto Exit;
1044     }
1045 
1046     memcpy (LocalTable, MappedTable, TableLength);
1047     *Address = TableAddress;
1048     *Table = LocalTable;
1049 
1050 Exit:
1051     OslUnmapTable (MappedTable);
1052     return (Status);
1053 }
1054 
1055 
1056 /******************************************************************************
1057  *
1058  * FUNCTION:    OslMapTable
1059  *
1060  * PARAMETERS:  Address             - Address of the table in memory
1061  *              Signature           - Optional ACPI Signature for desired table.
1062  *                                    Null terminated 4-character string.
1063  *              Table               - Where a pointer to the mapped table is
1064  *                                    returned
1065  *
1066  * RETURN:      Status; Mapped table is returned if AE_OK.
1067  *              AE_NOT_FOUND: A valid table was not found at the address
1068  *
1069  * DESCRIPTION: Map entire ACPI table into caller's address space.
1070  *
1071  *****************************************************************************/
1072 
1073 static ACPI_STATUS
OslMapTable(ACPI_PHYSICAL_ADDRESS Address,char * Signature,ACPI_TABLE_HEADER ** Table)1074 OslMapTable (
1075     ACPI_PHYSICAL_ADDRESS   Address,
1076     char                    *Signature,
1077     ACPI_TABLE_HEADER       **Table)
1078 {
1079     ACPI_TABLE_HEADER       *MappedTable;
1080     UINT32                  Length;
1081 
1082 
1083     if (!Address)
1084     {
1085         return (AE_BAD_ADDRESS);
1086     }
1087 
1088     /*
1089      * Map the header so we can get the table length.
1090      * Use sizeof (ACPI_TABLE_HEADER) as:
1091      * 1. it is bigger than 24 to include RSDP->Length
1092      * 2. it is smaller than sizeof (ACPI_TABLE_RSDP)
1093      */
1094     MappedTable = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));
1095     if (!MappedTable)
1096     {
1097         fprintf (stderr, "Could not map table header at 0x%8.8X%8.8X\n",
1098             ACPI_FORMAT_UINT64 (Address));
1099         return (AE_BAD_ADDRESS);
1100     }
1101 
1102     /* If specified, signature must match */
1103 
1104     if (Signature)
1105     {
1106         if (ACPI_VALIDATE_RSDP_SIG (Signature))
1107         {
1108             if (!ACPI_VALIDATE_RSDP_SIG (MappedTable->Signature))
1109             {
1110                 AcpiOsUnmapMemory (MappedTable, sizeof (ACPI_TABLE_HEADER));
1111                 return (AE_BAD_SIGNATURE);
1112             }
1113         }
1114         else if (!ACPI_COMPARE_NAMESEG (Signature, MappedTable->Signature))
1115         {
1116             AcpiOsUnmapMemory (MappedTable, sizeof (ACPI_TABLE_HEADER));
1117             return (AE_BAD_SIGNATURE);
1118         }
1119     }
1120 
1121     /* Map the entire table */
1122 
1123     Length = ApGetTableLength (MappedTable);
1124     AcpiOsUnmapMemory (MappedTable, sizeof (ACPI_TABLE_HEADER));
1125     if (Length == 0)
1126     {
1127         return (AE_BAD_HEADER);
1128     }
1129 
1130     MappedTable = AcpiOsMapMemory (Address, Length);
1131     if (!MappedTable)
1132     {
1133         fprintf (stderr, "Could not map table at 0x%8.8X%8.8X length %8.8X\n",
1134             ACPI_FORMAT_UINT64 (Address), Length);
1135         return (AE_INVALID_TABLE_LENGTH);
1136     }
1137 
1138     (void) ApIsValidChecksum (MappedTable);
1139 
1140     *Table = MappedTable;
1141     return (AE_OK);
1142 }
1143 
1144 
1145 /******************************************************************************
1146  *
1147  * FUNCTION:    OslUnmapTable
1148  *
1149  * PARAMETERS:  Table               - A pointer to the mapped table
1150  *
1151  * RETURN:      None
1152  *
1153  * DESCRIPTION: Unmap entire ACPI table.
1154  *
1155  *****************************************************************************/
1156 
1157 static void
OslUnmapTable(ACPI_TABLE_HEADER * Table)1158 OslUnmapTable (
1159     ACPI_TABLE_HEADER       *Table)
1160 {
1161     if (Table)
1162     {
1163         AcpiOsUnmapMemory (Table, ApGetTableLength (Table));
1164     }
1165 }
1166