22b7f2ed3590a6ff9f4b813a5d50a6db01caa130
[dragonfly.git] / sys / contrib / dev / acpica-unix / common / a16find.c
1 /******************************************************************************
2  *
3  * Module Name: a16find - 16-bit (real mode) routines to find ACPI
4  *                        tables in memory
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <string.h>
120 #include "acpi.h"
121 #include "accommon.h"
122 #include "amlcode.h"
123 #include "acparser.h"
124 #include "actables.h"
125
126 #include "16bit.h"
127
128
129 #define _COMPONENT          ACPI_TABLES
130         ACPI_MODULE_NAME    ("a16find")
131
132 /*
133  * This module finds ACPI tables located in memory.
134  * It is only generated for the 16-bit (real-mode) version of the utility.
135  */
136
137 #define ACPI_TABLE_FILE_SUFFIX      ".dat"
138
139
140 #if ACPI_MACHINE_WIDTH == 16
141
142 UINT32 ACPI_INTERNAL_VAR_XFACE dIn32 (UINT16 port);
143 void ACPI_INTERNAL_VAR_XFACE vOut32 (UINT16 port, UINT32 Val);
144 #define ACPI_ENABLE_HPET 0x00020000
145
146 char                        FilenameBuf[20];
147 ACPI_TABLE_HEADER           AcpiTblHeader;
148 UINT32                      TableSize;
149
150 UINT32                      AcpiGbl_SystemFlags;
151 UINT32                      AcpiGbl_RsdpOriginalLocation;
152
153 #if 0
154 /******************************************************************************
155  *
156  * FUNCTION:    AfWriteBuffer
157  *
158  * PARAMETERS:
159  *
160  * RETURN:
161  *
162  * DESCRIPTION: Open a file and write out a single buffer
163  *
164  ******************************************************************************/
165
166 ACPI_NATIVE_INT
167 AfWriteBuffer (
168     char                *Filename,
169     char                *Buffer,
170     UINT32              Length)
171 {
172     FILE                *fp;
173     ACPI_NATIVE_INT     Actual;
174
175
176     fp = fopen (Filename, "wb");
177     if (!fp)
178     {
179         printf ("Couldn't open %s\n", Filename);
180         return -1;
181     }
182
183     Actual = fwrite (Buffer, (size_t) Length, 1, fp);
184     fclose (fp);
185     return Actual;
186 }
187
188
189 /******************************************************************************
190  *
191  * FUNCTION:    AfGenerateFilename
192  *
193  * PARAMETERS:
194  *
195  * RETURN:
196  *
197  * DESCRIPTION: Build an output filename from an ACPI table ID string
198  *
199  ******************************************************************************/
200
201 char *
202 AfGenerateFilename (char *TableId)
203 {
204     UINT16                  i;
205
206
207     for (i = 0; i < 8 && TableId[i] != ' ' && TableId[i] != 0; i++)
208     {
209         FilenameBuf [i] = TableId[i];
210     }
211
212     FilenameBuf [i] = 0;
213     strcat (FilenameBuf, ACPI_TABLE_FILE_SUFFIX);
214     return FilenameBuf;
215 }
216
217
218 /******************************************************************************
219  *
220  * FUNCTION:    AfDumpTables
221  *
222  * PARAMETERS:
223  *
224  * RETURN:
225  *
226  * DESCRIPTION: Dump the loaded tables to a file (or files)
227  *
228  ******************************************************************************/
229
230 void
231 AfDumpTables (void)
232 {
233     char                    *Filename;
234
235
236     if (!AcpiGbl_DSDT)
237     {
238         AcpiOsPrintf ("No DSDT!\n");
239         return;
240     }
241
242
243     Filename = AfGenerateFilename (AcpiGbl_DSDT->OemTableId);
244         AfWriteBuffer (Filename,
245             (char *) AcpiGbl_DSDT, AcpiGbl_DSDT->Length);
246
247     AcpiOsPrintf ("DSDT AML written to \"%s\"\n", Filename);
248 }
249
250 #endif
251
252 void pascal
253 cprint (
254     UINT32      value)
255 {
256
257     AcpiOsPrintf ("Seq: 0x%8.8X\n", value);
258 }
259
260
261 /******************************************************************************
262  *
263  * FUNCTION:   CopyExtendedToReal
264  *
265  * PARAMETERS:
266  *
267  * RETURN:
268  *
269  * DESCRIPTION: Copy memory above 1meg
270  *
271  ******************************************************************************/
272
273 int
274 CopyExtendedToReal (
275     void        *Destination,
276     UINT32      PhysicalSource,
277     UINT32      Size)
278 {
279     int         RetVal;
280
281
282     RetVal = FlatMove32 (GET_PHYSICAL_ADDRESS (Destination),
283                             PhysicalSource, (UINT16) Size);
284
285     AcpiOsPrintf ("FlatMove return: 0x%hX From %X To %X Len %X\n",
286         (int) RetVal, PhysicalSource, GET_PHYSICAL_ADDRESS (Destination), Size);
287     return (RetVal);
288 }
289
290
291 /******************************************************************************
292  *
293  * FUNCTION:    AfFindRsdp
294  *
295  * PARAMETERS:
296  *
297  * RETURN:
298  *
299  * DESCRIPTION: Scan memory to find the RSDP
300  *
301  ******************************************************************************/
302
303 BOOLEAN
304 AfFindRsdp (RSDP_DESCRIPTOR **RSDP)
305 {
306     PTR_OVL             Rove;
307
308
309     PTR_OVL_BUILD_PTR (Rove, 0, 0);
310
311
312     /* Scan low memory */
313
314     do
315     {
316         if (strncmp (Rove.ptr, ACPI_SIG_RSDP, (size_t) 8) == 0)
317         {
318
319             /* TBD: Checksum check is invalid for X descriptor */
320
321 /*            if (AcpiTbSumTable (Rove.ptr, sizeof(RSDP_DESCRIPTOR)) != 0)
322             {
323  */
324             AcpiOsPrintf ("RSDP found at %p (Lo block)\n", Rove.ptr);
325             *RSDP = Rove.ptr;
326             return (TRUE);
327         }
328
329         Rove.ovl.base++;
330     }
331     while (Rove.ovl.base < 0x3F);
332
333     /* Scan high memory */
334
335     PTR_OVL_BUILD_PTR (Rove, 0xE000, 0);
336     do
337     {
338         if (strncmp (Rove.ptr, ACPI_SIG_RSDP, (size_t) 8) == 0)
339         {
340              AcpiOsPrintf ("RSDP found at %p (Hi block)\n", Rove.ptr);
341             *RSDP = Rove.ptr;
342             return (TRUE);
343         }
344
345         Rove.ovl.base++;
346     }
347     while (Rove.ovl.base < 0xFFFF);
348
349     return (FALSE);
350 }
351
352
353 /******************************************************************************
354  *
355  * FUNCTION:    AfRecognizeTable
356  *
357  * PARAMETERS:  TablePtr            - Input buffer pointer, optional
358  *              TableInfo           - Return value from AcpiTbGetTable
359  *
360  * RETURN:      Status
361  *
362  * DESCRIPTION: Check a table signature for a match against known table types
363  *
364  ******************************************************************************/
365
366 ACPI_STATUS
367 AfRecognizeTable (
368     char                    *TablePtr,
369     UINT32                  PhysAddr,
370     ACPI_TABLE_DESC         *TableInfo)
371 {
372     ACPI_TABLE_HEADER       *TableHeader;
373     ACPI_STATUS             Status;
374     ACPI_TABLE_TYPE         TableType = 0;
375     void                    **TableGlobalPtr;
376     UINT32                  i;
377
378
379     ACPI_FUNCTION_TRACE (AfRecognizeTable);
380
381
382     /* Ensure that we have a valid table pointer */
383
384     TableHeader = (ACPI_TABLE_HEADER *) TableInfo->Pointer;
385     if (!TableHeader)
386     {
387         return_ACPI_STATUS (AE_BAD_PARAMETER);
388     }
389
390     /* Search for a signature match among the known table types */
391
392     Status = AE_SUPPORT;
393     for (i = 1; i < NUM_ACPI_TABLE_TYPES; i++)       /* Start at one -> Skip RSDP */
394     {
395         if (!ACPI_STRNCMP (TableHeader->Signature,
396                     AcpiGbl_TableData[i].Signature,
397                     AcpiGbl_TableData[i].SigLength))
398         {
399             AcpiOsPrintf ("Found table [%s] Length 0x%X\n",
400                 AcpiGbl_TableData[i].Signature, (UINT32) TableHeader->Length);
401
402             TableType       = i;
403             TableGlobalPtr  = AcpiGbl_TableData[i].GlobalPtr;
404             Status          = AE_OK;
405             break;
406         }
407     }
408
409     /* Return the table type via the info struct */
410
411     TableInfo->Type     = (UINT8) TableType;
412     TableInfo->Length   = (ACPI_SIZE) TableHeader->Length;
413
414     /*
415      * An AE_SUPPORT means that the table was not recognized.
416      * We ignore this table;  just print a debug message
417      */
418     if (Status == AE_SUPPORT)
419     {
420         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
421             "Unsupported table %s (Type %d) was found and discarded\n",
422             AcpiGbl_TableData[TableType].Name, (UINT32) TableType));
423
424         return_ACPI_STATUS (Status);
425     }
426
427    Status = AcpiTbValidateTableHeader (TableHeader);
428     if (ACPI_FAILURE (Status))
429     {
430         /* Table failed verification, map all errors to BAD_DATA */
431
432         ACPI_ERROR ((AE_INFO,
433             "Invalid table header found in table named %s (Type %d)",
434             AcpiGbl_TableData[TableType].Name, (UINT32) TableType));
435
436         return_ACPI_STATUS (AE_BAD_DATA);
437     }
438
439     /* Now get the rest of the table */
440
441     *TableGlobalPtr = ACPI_ALLOCATE (AcpiTblHeader.Length);
442     if (!*TableGlobalPtr)
443     {
444         AcpiOsPrintf (
445             "Could not allocate buffer for Acpi table of length 0x%X\n",
446             (UINT32) ((FACS_DESCRIPTOR *) &AcpiTblHeader)->Length);
447         return_ACPI_STATUS (AE_NO_MEMORY);
448     }
449
450     TableInfo->Pointer = *TableGlobalPtr;
451
452     CopyExtendedToReal (*TableGlobalPtr, PhysAddr, AcpiTblHeader.Length);
453
454     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "%s=%p, (TableGlobalPtr=%p)\n",
455         AcpiGbl_TableData[TableType].Signature,
456         *TableGlobalPtr, TableGlobalPtr));
457
458     if (AcpiGbl_DbOpt_verbose)
459     {
460         AcpiUtDumpBuffer (*TableGlobalPtr, 48, 0, 0);
461     }
462
463     /*
464      * Validate checksum for _most_ tables,
465      * even the ones whose signature we don't recognize
466      */
467     if (TableType != ACPI_TABLE_FACS)
468     {
469         /* But don't abort if the checksum is wrong */
470         /* TBD: make this a configuration option? */
471
472         AcpiTbVerifyTableChecksum (*TableGlobalPtr);
473     }
474
475     return_ACPI_STATUS (Status);
476 }
477
478
479 /******************************************************************************
480  *
481  * FUNCTION:    AfGetAllTables
482  *
483  * PARAMETERS:  Root            - Root of the parse tree
484  *
485  * RETURN:      None
486  *
487  * DESCRIPTION:
488  *
489  *****************************************************************************/
490
491 ACPI_STATUS
492 AfGetAllTables (
493     UINT32                  NumberOfTables,
494     char                    *TablePtr)
495 {
496     ACPI_STATUS             Status = AE_OK;
497     UINT32                  Index;
498     ACPI_TABLE_DESC         TableInfo;
499
500
501     ACPI_FUNCTION_TRACE (AfGetAllTables);
502
503
504     if (AcpiGbl_DbOpt_verbose)
505     {
506         AcpiOsPrintf ("Number of tables: %d\n", (UINT32) NumberOfTables);
507     }
508
509     /*
510      * Loop through all table pointers found in RSDT.
511      * This will NOT include the FACS and DSDT - we must get
512      * them after the loop
513      */
514     for (Index = 0; Index < NumberOfTables; Index++)
515     {
516         /* Get the table via the RSDT */
517
518         CopyExtendedToReal (&AcpiTblHeader, ACPI_GET_ADDRESS (AcpiGbl_XSDT->TableOffsetEntry[Index]),
519                             sizeof (ACPI_TABLE_HEADER));
520
521         TableInfo.Pointer       = &AcpiTblHeader;
522         TableInfo.Length        = (ACPI_SIZE) AcpiTblHeader.Length;
523         TableInfo.Allocation    = ACPI_MEM_ALLOCATED;
524
525         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Table pointer: %X\n",
526             (UINT32) ACPI_GET_ADDRESS (AcpiGbl_XSDT->TableOffsetEntry[Index])));
527
528         Status = AfRecognizeTable (NULL, ACPI_GET_ADDRESS (AcpiGbl_XSDT->TableOffsetEntry[Index]), &TableInfo);
529         if (ACPI_SUCCESS (Status))
530         {
531             AcpiTbInitTableDescriptor (TableInfo.Type, &TableInfo);
532         }
533
534        /* Ignore errors, just move on to next table */
535     }
536
537     if (!AcpiGbl_FADT)
538     {
539         AcpiOsPrintf ("FADT was not found, cannot obtain FACS and DSDT!\n");
540         return (AE_NO_ACPI_TABLES);
541     }
542
543     Status = AcpiTbConvertTableFadt ();
544     if (ACPI_FAILURE (Status))
545     {
546         return_ACPI_STATUS (Status);
547     }
548
549     /*
550      * Get the FACS
551      */
552     CopyExtendedToReal (&AcpiTblHeader, ACPI_GET_ADDRESS (AcpiGbl_FADT->XFirmwareCtrl),
553                         sizeof (ACPI_TABLE_HEADER));
554     AcpiGbl_FACS = ACPI_ALLOCATE (AcpiTblHeader.Length);
555     if (!AcpiGbl_FACS)
556     {
557         AcpiOsPrintf ("Could not allocate buffer for FADT length 0x%X\n",
558                         (UINT32) AcpiTblHeader.Length);
559         return_ACPI_STATUS (AE_NO_MEMORY);
560     }
561
562     CopyExtendedToReal (AcpiGbl_FACS, ACPI_GET_ADDRESS (AcpiGbl_FADT->XFirmwareCtrl), AcpiTblHeader.Length);
563
564     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "FACS at %p (Phys %8.8X) length %X FADT at%p\n",
565                     AcpiGbl_FACS, (UINT32) ACPI_GET_ADDRESS (AcpiGbl_FADT->XFirmwareCtrl),
566                     (UINT32) AcpiTblHeader.Length, AcpiGbl_FADT));
567     if (AcpiGbl_DbOpt_verbose)
568     {
569         AcpiUtDumpBuffer ((char *) AcpiGbl_FADT, sizeof (ACPI_TABLE_HEADER), 0, 0);
570     }
571
572     TableInfo.Type          = ACPI_TABLE_FADT;
573     TableInfo.Pointer       = (void *) AcpiGbl_FADT;
574     TableInfo.Length        = (ACPI_SIZE) AcpiTblHeader.Length;
575     TableInfo.Allocation    = ACPI_MEM_ALLOCATED;
576
577     /* There is no checksum for the FACS, nothing to verify */
578
579     AcpiTbInitTableDescriptor (TableInfo.Type, &TableInfo);
580
581     AcpiTbBuildCommonFacs (&TableInfo);
582
583     /*
584      * Get the DSDT
585      */
586     CopyExtendedToReal (&AcpiTblHeader, ACPI_GET_ADDRESS (AcpiGbl_FADT->XDsdt), sizeof (ACPI_TABLE_HEADER));
587     AcpiGbl_DSDT = ACPI_ALLOCATE (AcpiTblHeader.Length);
588     if (!AcpiGbl_DSDT)
589     {
590         AcpiOsPrintf ("Could not allocate buffer for DSDT length 0x%X\n", (UINT32) AcpiTblHeader.Length);
591         return_ACPI_STATUS (AE_NO_MEMORY);
592     }
593
594     CopyExtendedToReal (AcpiGbl_DSDT, ACPI_GET_ADDRESS (AcpiGbl_FADT->XDsdt), AcpiTblHeader.Length);
595
596     AcpiOsPrintf ("DSDT at %p (Phys %8.8X) length %X FADT at %p\n",
597                     AcpiGbl_DSDT, (UINT32) ACPI_GET_ADDRESS (AcpiGbl_FADT->XDsdt),
598                     (UINT32) AcpiTblHeader.Length, AcpiGbl_FADT);
599     if (AcpiGbl_DbOpt_verbose)
600     {
601         AcpiUtDumpBuffer ((char *) AcpiGbl_DSDT, sizeof (ACPI_TABLE_HEADER), 0, 0);
602     }
603
604     TableInfo.Type          = ACPI_TABLE_DSDT;
605     TableInfo.Pointer       = (void *) AcpiGbl_DSDT;
606     TableInfo.Length        = (ACPI_SIZE) AcpiTblHeader.Length;
607     TableInfo.Allocation    = ACPI_MEM_ALLOCATED;
608
609     AcpiTbInitTableDescriptor (TableInfo.Type, &TableInfo);
610     AcpiTbVerifyTableChecksum ((ACPI_TABLE_HEADER *) AcpiGbl_DSDT);
611
612     return_ACPI_STATUS (AE_OK);
613 }
614
615
616 ACPI_STATUS
617 AfGetRsdt (void)
618 {
619     BOOLEAN                 Found;
620     UINT32                  PhysicalAddress;
621     UINT32                  SignatureLength;
622     char                    *TableSignature;
623     ACPI_STATUS             Status;
624     ACPI_TABLE_DESC         TableInfo;
625
626
627     ACPI_FUNCTION_TRACE (AfGetRsdt);
628
629     if (AcpiGbl_XSDT)
630     {
631         return (AE_OK);
632     }
633
634     Found = AfFindRsdp (&AcpiGbl_RSDP);
635     if (!Found)
636     {
637         AcpiOsPrintf ("Could not find RSDP in the low megabyte\n");
638         return (AE_NO_ACPI_TABLES);
639     }
640
641     /* Use XSDT if it is present */
642
643     if ((AcpiGbl_RSDP->Revision >= 2) &&
644         ACPI_GET_ADDRESS (AcpiGbl_RSDP->XsdtPhysicalAddress))
645     {
646         PhysicalAddress = ACPI_GET_ADDRESS (AcpiGbl_RSDP->XsdtPhysicalAddress);
647         TableSignature = ACPI_SIG_XSDT;
648         SignatureLength = sizeof (ACPI_SIG_XSDT) -1;
649         AcpiGbl_RootTableType = ACPI_TABLE_TYPE_XSDT;
650
651         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Found XSDT\n"));
652     }
653     else
654     {
655         /* No XSDT, use the RSDT */
656
657         PhysicalAddress = AcpiGbl_RSDP->RsdtPhysicalAddress;
658         TableSignature = ACPI_SIG_RSDT;
659         SignatureLength = sizeof (ACPI_SIG_RSDT) -1;
660         AcpiGbl_RootTableType = ACPI_TABLE_TYPE_RSDT;
661
662         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Found RSDT\n"));
663     }
664
665     if (AcpiGbl_DbOpt_verbose)
666     {
667         AcpiUtDumpBuffer ((char *) AcpiGbl_RSDP, sizeof (RSDP_DESCRIPTOR), 0, ACPI_UINT32_MAX);
668     }
669
670     /* Get the RSDT/XSDT header to determine the table length */
671
672     CopyExtendedToReal (&AcpiTblHeader, PhysicalAddress, sizeof (ACPI_TABLE_HEADER));
673
674     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "RSDT/XSDT at %8.8X\n", (UINT32) PhysicalAddress));
675     if (AcpiGbl_DbOpt_verbose)
676     {
677         AcpiUtDumpBuffer ((char *) &AcpiTblHeader, sizeof (ACPI_TABLE_HEADER), 0, ACPI_UINT32_MAX);
678     }
679
680     /* Validate the table header */
681
682     Status = AcpiTbValidateTableHeader (&AcpiTblHeader);
683     if (ACPI_FAILURE (Status))
684     {
685         /* Table failed verification, map all errors to BAD_DATA */
686
687         ACPI_ERROR ((AE_INFO, "Invalid RSDT table header"));
688         return (AE_BAD_DATA);
689     }
690
691     /* Allocate a buffer for the entire table */
692
693     AcpiGbl_XSDT = (void *) malloc ((size_t) AcpiTblHeader.Length);
694     if (!AcpiGbl_XSDT)
695     {
696         AcpiOsPrintf ("Could not allocate buffer for RSDT length 0x%X\n",
697             (UINT32) AcpiTblHeader.Length);
698         return AE_NO_MEMORY;
699     }
700
701     /* Get the entire RSDT/XSDT */
702
703     CopyExtendedToReal (AcpiGbl_XSDT, PhysicalAddress, AcpiTblHeader.Length);
704     AcpiOsPrintf ("%s at %p (Phys %8.8X)\n",
705         TableSignature, AcpiGbl_XSDT, (UINT32) PhysicalAddress);
706
707     if (AcpiGbl_DbOpt_verbose)
708     {
709         AcpiUtDumpBuffer ((char *) &AcpiTblHeader, sizeof (ACPI_TABLE_HEADER), 0, 0);
710     }
711
712     /* Convert to common format XSDT */
713
714     TableInfo.Pointer       = (ACPI_TABLE_HEADER *) AcpiGbl_XSDT;
715     TableInfo.Length        = (ACPI_SIZE) AcpiTblHeader.Length;
716     TableInfo.Allocation    = ACPI_MEM_ALLOCATED;
717
718     AcpiGbl_RsdtTableCount = AcpiTbGetTableCount (AcpiGbl_RSDP, TableInfo.Pointer);
719
720     Status = AcpiTbConvertToXsdt (&TableInfo);
721     if (ACPI_FAILURE (Status))
722     {
723         goto ErrorExit;
724     }
725
726     AcpiGbl_XSDT = (XSDT_DESCRIPTOR *) TableInfo.Pointer;
727
728 ErrorExit:
729     return (Status);
730 }
731
732
733 void
734 AfDumpRsdt (void)
735 {
736     UINT32                  i;
737     UINT32                  NumTables;
738     UINT32                  PhysicalAddress;
739     ACPI_TABLE_HEADER       **Table;
740     ACPI_TABLE_HEADER       ThisTable;
741
742
743     NumTables = (AcpiGbl_XSDT->Length - sizeof (ACPI_TABLE_HEADER)) / 8;
744
745     AcpiOsPrintf ("%d Tables defined in RSDT/XSDT:\n", NumTables);
746
747     for (i = 0; i < NumTables; i++)
748     {
749         PhysicalAddress = (UINT32) AcpiGbl_XSDT->TableOffsetEntry[i].Lo;
750         CopyExtendedToReal (&ThisTable, PhysicalAddress, sizeof (ACPI_TABLE_HEADER));
751         AcpiOsPrintf ("[%4.4s] ", ThisTable.Signature);
752         ((char *) Table) += 8;
753     }
754
755     AcpiOsPrintf ("\n");
756 }
757
758 ACPI_STATUS
759 AfGetTable (
760     ACPI_TABLE_HEADER       *TableHeader,
761     UINT32                  PhysicalAddress,
762     UINT8                   **TablePtr)
763 {
764
765     /* Allocate a buffer for the entire table */
766
767     *TablePtr = (void *) malloc ((size_t) TableHeader->Length);
768     if (!*TablePtr)
769     {
770         AcpiOsPrintf ("Could not allocate buffer for table length 0x%X\n",
771             (UINT32) TableHeader->Length);
772         return AE_NO_MEMORY;
773     }
774
775     /* Get the entire table */
776
777     CopyExtendedToReal (*TablePtr, PhysicalAddress, TableHeader->Length);
778     AcpiOsPrintf ("%4.4s at %p (Phys %8.8X)\n",
779         TableHeader->Signature, *TablePtr, (UINT32) PhysicalAddress);
780
781     if (AcpiGbl_DbOpt_verbose)
782     {
783         AcpiUtDumpBuffer ((char *) TableHeader, sizeof (ACPI_TABLE_HEADER), 0, 0);
784     }
785
786     return (AE_OK);
787 }
788
789
790 ACPI_STATUS
791 AfGetTableFromXsdt (
792     char                    *TableName,
793     UINT8                   **TablePtr)
794 {
795     UINT32                  i;
796     UINT32                  NumTables;
797     UINT32                  PhysicalAddress;
798     ACPI_TABLE_HEADER       ThisTable;
799
800
801     NumTables = (AcpiGbl_XSDT->Length - sizeof (ACPI_TABLE_HEADER)) / 8;
802
803     for (i = 0; i < NumTables; i++)
804     {
805         PhysicalAddress = (UINT32) AcpiGbl_XSDT->TableOffsetEntry[i].Lo;
806         CopyExtendedToReal (&ThisTable, PhysicalAddress, sizeof (ACPI_TABLE_HEADER));
807
808         if (ACPI_COMPARE_NAME (TableName, ThisTable.Signature))
809         {
810             AfGetTable (&ThisTable, PhysicalAddress, TablePtr);
811             return (AE_OK);
812         }
813     }
814
815     return (AE_NOT_FOUND);
816 }
817
818
819 /******************************************************************************
820  *
821  * FUNCTION:    AfFindTable
822  *
823  * PARAMETERS:
824  *
825  * RETURN:      Status
826  *
827  * DESCRIPTION: Load the DSDT from the file pointer
828  *
829  *****************************************************************************/
830
831 ACPI_STATUS
832 AfFindTable (
833     char                    *TableName,
834     UINT8                   **TablePtr,
835     UINT32                  *TableLength)
836 {
837     ACPI_STATUS             Status;
838
839
840     ACPI_FUNCTION_TRACE (AfFindTable);
841
842
843     if (!AcpiGbl_DSDT)
844     {
845         Status = AfGetRsdt ();
846         if (ACPI_FAILURE (Status))
847         {
848             return (Status);
849         }
850
851         AfDumpRsdt ();
852
853         /* Get the rest of the required tables (DSDT, FADT) */
854
855         Status = AfGetAllTables (AcpiGbl_RsdtTableCount, NULL);
856         if (ACPI_FAILURE (Status))
857         {
858             goto ErrorExit;
859         }
860
861         ACPI_DEBUG_PRINT ((ACPI_DB_INIT, "ACPI Tables successfully acquired\n"));
862     }
863
864
865     if (ACPI_COMPARE_NAME (TableName, ACPI_SIG_DSDT))
866     {
867         *TablePtr = (UINT8 *) AcpiGbl_DSDT;
868         *TableLength = AcpiGbl_DSDT->Length;
869     }
870     else if (ACPI_COMPARE_NAME (TableName, ACPI_SIG_FADT))
871     {
872         *TablePtr = (UINT8 *) AcpiGbl_FADT;
873         *TableLength = AcpiGbl_FADT->Length;
874     }
875     else if (ACPI_COMPARE_NAME (TableName, ACPI_SIG_FACS))
876     {
877         *TablePtr = (UINT8 *) AcpiGbl_FACS;
878         *TableLength = AcpiGbl_FACS->Length;
879     }
880     else if (ACPI_COMPARE_NAME (TableName, ACPI_SIG_RSDT))
881     {
882         *TablePtr = (UINT8 *) AcpiGbl_XSDT;
883         *TableLength = AcpiGbl_XSDT->Length;
884     }
885     else if (ACPI_COMPARE_NAME (TableName, ACPI_SIG_SSDT))
886     {
887         AcpiOsPrintf ("Unsupported table signature: [%4.4s]\n", TableName);
888         *TablePtr = NULL;
889         return AE_SUPPORT;
890     }
891     else
892     {
893         Status = AfGetTableFromXsdt (TableName, TablePtr);
894         if (ACPI_FAILURE (Status))
895         {
896             goto ErrorExit;
897         }
898
899         *TableLength = (*((ACPI_TABLE_HEADER **)TablePtr))->Length;
900     }
901
902     return AE_OK;
903
904 ErrorExit:
905
906     ACPI_EXCEPTION ((AE_INFO, Status, "During ACPI Table initialization");
907     return (Status);
908 }
909
910 #ifdef _HPET
911 UINT8           Hbuf[1024];
912
913 void
914 AfGetHpet (void)
915 {
916     HPET_DESCRIPTION_TABLE  *NewTable;
917     ACPI_TABLE_HEADER       TableHeader;
918     ACPI_STATUS             Status;
919     UINT16                  i;
920     UINT32                  Value;
921     UINT32                  Value2;
922
923
924     /* Get HPET  TEMP! */
925
926     ACPI_STRNCPY (TableHeader.Signature, "HPET", 4);
927     Status = AcpiOsTableOverride (&TableHeader, (ACPI_TABLE_HEADER **) &NewTable);
928     if (ACPI_FAILURE (Status))
929     {
930         return;
931     }
932
933     AcpiDbgLevel |= ACPI_LV_TABLES;
934     AcpiOsPrintf ("HPET table :\n");
935     AcpiUtDumpBuffer ((char *) NewTable, NewTable->Header.Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
936
937     for (i = 0; i < 1024; i++)
938     {
939         Hbuf[i] = (UINT8) i;
940     }
941
942     /* enable HPET */
943
944     CopyExtendedToReal (&Value, 0xD0, 4);
945     AcpiOsPrintf ("Reg 0xD0: %8.8X\n", Value);
946
947     Value = dIn32 (0xD0);
948     AcpiOsPrintf ("Port 0xD0: %8.8X\n", Value);
949     Value |= ACPI_ENABLE_HPET;
950
951     vOut32 (0xD0, Value);
952     Value2 = dIn32 (0xD0);
953     AcpiOsPrintf ("Port 0xD0: Wrote: %8.8X got %8.8X\n", Value, Value2);
954
955     AcpiOsPrintf ("HPET block(at %8.8X):\n", NewTable->BaseAddress[2]);
956     CopyExtendedToReal (Hbuf, NewTable->BaseAddress[2], 1024);
957     AcpiUtDumpBuffer ((char *) Hbuf, 1024, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
958 }
959 #endif /* _HPET */
960
961 #endif /* IA16 */
962