Bring in acpica-unix-20031203. As with other contrib imports, this import
[dragonfly.git] / sys / contrib / dev / acpica-unix-20031203 / common / a16find.c
1 /******************************************************************************
2  *
3  * Module Name: a16find - 16-bit (real mode) routines to find ACPI
4  *                        tables in memory
5  *              $Revision: 27 $
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
14  * All rights reserved.
15  *
16  * 2. License
17  *
18  * 2.1. This is your license from Intel Corp. under its intellectual property
19  * rights.  You may have additional license terms from the party that provided
20  * you this software, covering your right to use that party's intellectual
21  * property rights.
22  *
23  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24  * copy of the source code appearing in this file ("Covered Code") an
25  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26  * base code distributed originally by Intel ("Original Intel Code") to copy,
27  * make derivatives, distribute, use and display any portion of the Covered
28  * Code in any form, with the right to sublicense such rights; and
29  *
30  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31  * license (with the right to sublicense), under only those claims of Intel
32  * patents that are infringed by the Original Intel Code, to make, use, sell,
33  * offer to sell, and import the Covered Code and derivative works thereof
34  * solely to the minimum extent necessary to exercise the above copyright
35  * license, and in no event shall the patent license extend to any additions
36  * to or modifications of the Original Intel Code.  No other license or right
37  * is granted directly or by implication, estoppel or otherwise;
38  *
39  * The above copyright and patent license is granted only if the following
40  * conditions are met:
41  *
42  * 3. Conditions
43  *
44  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45  * Redistribution of source code of any substantial portion of the Covered
46  * Code or modification with rights to further distribute source must include
47  * the above Copyright Notice, the above License, this list of Conditions,
48  * and the following Disclaimer and Export Compliance provision.  In addition,
49  * Licensee must cause all Covered Code to which Licensee contributes to
50  * contain a file documenting the changes Licensee made to create that Covered
51  * Code and the date of any change.  Licensee must include in that file the
52  * documentation of any changes made by any predecessor Licensee.  Licensee
53  * must include a prominent statement that the modification is derived,
54  * directly or indirectly, from Original Intel Code.
55  *
56  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57  * Redistribution of source code of any substantial portion of the Covered
58  * Code or modification without rights to further distribute source must
59  * include the following Disclaimer and Export Compliance provision in the
60  * documentation and/or other materials provided with distribution.  In
61  * addition, Licensee may not authorize further sublicense of source of any
62  * portion of the Covered Code, and must include terms to the effect that the
63  * license from Licensee to its licensee is limited to the intellectual
64  * property embodied in the software Licensee provides to its licensee, and
65  * not to intellectual property embodied in modifications its licensee may
66  * make.
67  *
68  * 3.3. Redistribution of Executable. Redistribution in executable form of any
69  * substantial portion of the Covered Code or modification must reproduce the
70  * above Copyright Notice, and the following Disclaimer and Export Compliance
71  * provision in the documentation and/or other materials provided with the
72  * distribution.
73  *
74  * 3.4. Intel retains all right, title, and interest in and to the Original
75  * Intel Code.
76  *
77  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78  * Intel shall be used in advertising or otherwise to promote the sale, use or
79  * other dealings in products derived from or relating to the Covered Code
80  * without prior written authorization from Intel.
81  *
82  * 4. Disclaimer and Export Compliance
83  *
84  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90  * PARTICULAR PURPOSE.
91  *
92  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99  * LIMITED REMEDY.
100  *
101  * 4.3. Licensee shall not export, either directly or indirectly, any of this
102  * software or system incorporating such software without first obtaining any
103  * required license or other approval from the U. S. Department of Commerce or
104  * any other agency or department of the United States Government.  In the
105  * event Licensee exports any such software from the United States or
106  * re-exports any such software from a foreign destination, Licensee shall
107  * ensure that the distribution and export/re-export of the software is in
108  * compliance with all laws, regulations, orders, or other restrictions of the
109  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110  * any of its subsidiaries will export/re-export any technical data, process,
111  * software, or service, directly or indirectly, to any country for which the
112  * United States government or any agency thereof requires an export license,
113  * other governmental approval, or letter of assurance, without first obtaining
114  * such license, approval or letter.
115  *
116  *****************************************************************************/
117
118 #include <stdio.h>
119 #include <stdlib.h>
120 #include <string.h>
121 #include "acpi.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     ACPI_NATIVE_UINT         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, RSDP_SIG, (size_t) 8) == 0)
317         {
318
319             /* TBD: Checksum check is invalid for X descriptor */
320
321 /*            if (AcpiTbChecksum (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, RSDP_SIG, (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_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid table header found in table named %s (Type %d)\n",
433                             AcpiGbl_TableData[TableType].Name, (UINT32) TableType));
434
435         return_ACPI_STATUS (AE_BAD_DATA);
436     }
437
438     /* Now get the rest of the table */
439
440     *TableGlobalPtr = ACPI_MEM_ALLOCATE (AcpiTblHeader.Length);
441     if (!*TableGlobalPtr)
442     {
443         AcpiOsPrintf ("Could not allocate buffer for Acpi table of length 0x%X\n",
444                         (UINT32) ((FACS_DESCRIPTOR *) &AcpiTblHeader)->Length);
445         return_ACPI_STATUS (AE_NO_MEMORY);
446     }
447
448     TableInfo->Pointer      = *TableGlobalPtr;
449
450     CopyExtendedToReal (*TableGlobalPtr, PhysAddr, AcpiTblHeader.Length);
451
452     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "%s=%p, (TableGlobalPtr=%p)\n", AcpiGbl_TableData[TableType].Signature,
453                     *TableGlobalPtr, TableGlobalPtr));
454
455     if (AcpiGbl_DbOpt_verbose)
456     {
457         AcpiUtDumpBuffer (*TableGlobalPtr, 48, 0, 0);
458     }
459
460     /*
461      * Validate checksum for _most_ tables,
462      * even the ones whose signature we don't recognize
463      */
464     if (TableType != ACPI_TABLE_FACS)
465     {
466         /* But don't abort if the checksum is wrong */
467         /* TBD: make this a configuration option? */
468
469         AcpiTbVerifyTableChecksum (*TableGlobalPtr);
470     }
471
472     return_ACPI_STATUS (Status);
473 }
474
475
476 /******************************************************************************
477  *
478  * FUNCTION:    AfGetAllTables
479  *
480  * PARAMETERS:  Root            - Root of the parse tree
481  *
482  * RETURN:      None
483  *
484  * DESCRIPTION:
485  *
486  *****************************************************************************/
487
488 ACPI_STATUS
489 AfGetAllTables (
490     UINT32                  NumberOfTables,
491     char                    *TablePtr)
492 {
493     ACPI_STATUS             Status = AE_OK;
494     UINT32                  Index;
495     ACPI_TABLE_DESC         TableInfo;
496
497
498     ACPI_FUNCTION_TRACE ("AfGetAllTables");
499
500
501     if (AcpiGbl_DbOpt_verbose)
502     {
503         AcpiOsPrintf ("Number of tables: %d\n", (UINT32) NumberOfTables);
504     }
505
506     /*
507      * Loop through all table pointers found in RSDT.
508      * This will NOT include the FACS and DSDT - we must get
509      * them after the loop
510      */
511     for (Index = 0; Index < NumberOfTables; Index++)
512     {
513         /* Get the table via the RSDT */
514
515         CopyExtendedToReal (&AcpiTblHeader, ACPI_GET_ADDRESS (AcpiGbl_XSDT->TableOffsetEntry[Index]),
516                             sizeof (ACPI_TABLE_HEADER));
517
518         TableInfo.Pointer       = &AcpiTblHeader;
519         TableInfo.Length        = (ACPI_SIZE) AcpiTblHeader.Length;
520         TableInfo.Allocation    = ACPI_MEM_ALLOCATED;
521
522         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Table pointer: %X\n",
523             (UINT32) ACPI_GET_ADDRESS (AcpiGbl_XSDT->TableOffsetEntry[Index])));
524
525         Status = AfRecognizeTable (NULL, ACPI_GET_ADDRESS (AcpiGbl_XSDT->TableOffsetEntry[Index]), &TableInfo);
526         if (ACPI_SUCCESS (Status))
527         {
528             AcpiTbInitTableDescriptor (TableInfo.Type, &TableInfo);
529         }
530
531        /* Ignore errors, just move on to next table */
532     }
533
534     if (!AcpiGbl_FADT)
535     {
536         AcpiOsPrintf ("FADT was not found, cannot obtain FACS and DSDT!\n");
537         return (AE_NO_ACPI_TABLES);
538     }
539
540     Status = AcpiTbConvertTableFadt ();
541     if (ACPI_FAILURE (Status))
542     {
543         return_ACPI_STATUS (Status);
544     }
545
546     /*
547      * Get the FACS
548      */
549     CopyExtendedToReal (&AcpiTblHeader, ACPI_GET_ADDRESS (AcpiGbl_FADT->XFirmwareCtrl),
550                         sizeof (ACPI_TABLE_HEADER));
551     AcpiGbl_FACS = ACPI_MEM_ALLOCATE (AcpiTblHeader.Length);
552     if (!AcpiGbl_FACS)
553     {
554         AcpiOsPrintf ("Could not allocate buffer for FADT length 0x%X\n",
555                         (UINT32) AcpiTblHeader.Length);
556         return_ACPI_STATUS (AE_NO_MEMORY);
557     }
558
559     CopyExtendedToReal (AcpiGbl_FACS, ACPI_GET_ADDRESS (AcpiGbl_FADT->XFirmwareCtrl), AcpiTblHeader.Length);
560
561     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "FACS at %p (Phys %8.8X) length %X FADT at%p\n",
562                     AcpiGbl_FACS, (UINT32) ACPI_GET_ADDRESS (AcpiGbl_FADT->XFirmwareCtrl),
563                     (UINT32) AcpiTblHeader.Length, AcpiGbl_FADT));
564     if (AcpiGbl_DbOpt_verbose)
565     {
566         AcpiUtDumpBuffer ((char *) AcpiGbl_FADT, sizeof (ACPI_TABLE_HEADER), 0, 0);
567     }
568
569     TableInfo.Type          = ACPI_TABLE_FADT;
570     TableInfo.Pointer       = (void *) AcpiGbl_FADT;
571     TableInfo.Length        = (ACPI_SIZE) AcpiTblHeader.Length;
572     TableInfo.Allocation    = ACPI_MEM_ALLOCATED;
573
574     /* There is no checksum for the FACS, nothing to verify */
575
576     AcpiTbInitTableDescriptor (TableInfo.Type, &TableInfo);
577
578     AcpiTbBuildCommonFacs (&TableInfo);
579
580     /*
581      * Get the DSDT
582      */
583     CopyExtendedToReal (&AcpiTblHeader, ACPI_GET_ADDRESS (AcpiGbl_FADT->XDsdt), sizeof (ACPI_TABLE_HEADER));
584     AcpiGbl_DSDT = ACPI_MEM_ALLOCATE (AcpiTblHeader.Length);
585     if (!AcpiGbl_DSDT)
586     {
587         AcpiOsPrintf ("Could not allocate buffer for DSDT length 0x%X\n", (UINT32) AcpiTblHeader.Length);
588         return_ACPI_STATUS (AE_NO_MEMORY);
589     }
590
591     CopyExtendedToReal (AcpiGbl_DSDT, ACPI_GET_ADDRESS (AcpiGbl_FADT->XDsdt), AcpiTblHeader.Length);
592
593     AcpiOsPrintf ("DSDT at %p (Phys %8.8X) length %X FADT at %p\n",
594                     AcpiGbl_DSDT, (UINT32) ACPI_GET_ADDRESS (AcpiGbl_FADT->XDsdt),
595                     (UINT32) AcpiTblHeader.Length, AcpiGbl_FADT);
596     if (AcpiGbl_DbOpt_verbose)
597     {
598         AcpiUtDumpBuffer ((char *) AcpiGbl_DSDT, sizeof (ACPI_TABLE_HEADER), 0, 0);
599     }
600
601     TableInfo.Type          = ACPI_TABLE_DSDT;
602     TableInfo.Pointer       = (void *) AcpiGbl_DSDT;
603     TableInfo.Length        = (ACPI_SIZE) AcpiTblHeader.Length;
604     TableInfo.Allocation    = ACPI_MEM_ALLOCATED;
605
606     AcpiTbInitTableDescriptor (TableInfo.Type, &TableInfo);
607     AcpiTbVerifyTableChecksum ((ACPI_TABLE_HEADER *) AcpiGbl_DSDT);
608
609     return_ACPI_STATUS (AE_OK);
610 }
611
612
613 ACPI_STATUS
614 AfGetRsdt (void)
615 {
616     BOOLEAN                 Found;
617     UINT32                  PhysicalAddress;
618     UINT32                  SignatureLength;
619     char                    *TableSignature;
620     ACPI_STATUS             Status;
621     ACPI_TABLE_DESC         TableInfo;
622
623
624     ACPI_FUNCTION_TRACE ("AfGetRsdt");
625
626     if (AcpiGbl_XSDT)
627     {
628         return (AE_OK);
629     }
630
631     Found = AfFindRsdp (&AcpiGbl_RSDP);
632     if (!Found)
633     {
634         AcpiOsPrintf ("Could not find RSDP in the low megabyte\n");
635         return (AE_NO_ACPI_TABLES);
636     }
637
638     /*
639      * For RSDP revision 0 or 1, we use the RSDT.
640      * For RSDP revision 2 (and above), we use the XSDT
641      */
642     if (AcpiGbl_RSDP->Revision < 2)
643     {
644         PhysicalAddress = AcpiGbl_RSDP->RsdtPhysicalAddress;
645         TableSignature = RSDT_SIG;
646         SignatureLength = sizeof (RSDT_SIG) -1;
647
648         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Found ACPI 1.0 RSDP\n"));
649     }
650     else
651     {
652         PhysicalAddress = ACPI_GET_ADDRESS (AcpiGbl_RSDP->XsdtPhysicalAddress);
653         TableSignature = XSDT_SIG;
654         SignatureLength = sizeof (XSDT_SIG) -1;
655
656         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Found ACPI 2.0 RSDP\n"));
657     }
658
659     if (AcpiGbl_DbOpt_verbose)
660     {
661         AcpiUtDumpBuffer ((char *) AcpiGbl_RSDP, sizeof (RSDP_DESCRIPTOR), 0, ACPI_UINT32_MAX);
662     }
663
664     /* Get the RSDT/XSDT header to determine the table length */
665
666     CopyExtendedToReal (&AcpiTblHeader, PhysicalAddress, sizeof (ACPI_TABLE_HEADER));
667
668     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "RSDT/XSDT at %8.8X\n", (UINT32) PhysicalAddress));
669     if (AcpiGbl_DbOpt_verbose)
670     {
671         AcpiUtDumpBuffer ((char *) &AcpiTblHeader, sizeof (ACPI_TABLE_HEADER), 0, ACPI_UINT32_MAX);
672     }
673
674     /* Validate the table header */
675
676     Status = AcpiTbValidateTableHeader (&AcpiTblHeader);
677     if (ACPI_FAILURE (Status))
678     {
679         /* Table failed verification, map all errors to BAD_DATA */
680
681         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid RSDT table header\n"));
682         return (AE_BAD_DATA);
683     }
684
685     /* Allocate a buffer for the entire table */
686
687     AcpiGbl_XSDT = (void *) malloc ((size_t) AcpiTblHeader.Length);
688     if (!AcpiGbl_XSDT)
689     {
690         AcpiOsPrintf ("Could not allocate buffer for RSDT length 0x%X\n",
691             (UINT32) AcpiTblHeader.Length);
692         return AE_NO_MEMORY;
693     }
694
695     /* Get the entire RSDT/XSDT */
696
697     CopyExtendedToReal (AcpiGbl_XSDT, PhysicalAddress, AcpiTblHeader.Length);
698     AcpiOsPrintf ("%s at %p (Phys %8.8X)\n",
699         TableSignature, AcpiGbl_XSDT, (UINT32) PhysicalAddress);
700
701     if (AcpiGbl_DbOpt_verbose)
702     {
703         AcpiUtDumpBuffer ((char *) &AcpiTblHeader, sizeof (ACPI_TABLE_HEADER), 0, 0);
704     }
705
706     /* Convert to common format XSDT */
707
708     TableInfo.Pointer       = (ACPI_TABLE_HEADER *) AcpiGbl_XSDT;
709     TableInfo.Length        = (ACPI_SIZE) AcpiTblHeader.Length;
710     TableInfo.Allocation    = ACPI_MEM_ALLOCATED;
711
712     AcpiGbl_RsdtTableCount = AcpiTbGetTableCount (AcpiGbl_RSDP, TableInfo.Pointer);
713
714     Status = AcpiTbConvertToXsdt (&TableInfo);
715     if (ACPI_FAILURE (Status))
716     {
717         goto ErrorExit;
718     }
719
720     AcpiGbl_XSDT = (XSDT_DESCRIPTOR *) TableInfo.Pointer;
721
722 ErrorExit:
723     return (Status);
724 }
725
726
727 void
728 AfDumpRsdt (void)
729 {
730     UINT32                  i;
731     UINT32                  NumTables;
732     UINT32                  PhysicalAddress;
733     ACPI_TABLE_HEADER       **Table;
734     ACPI_TABLE_HEADER       ThisTable;
735
736
737     NumTables = (AcpiGbl_XSDT->Length - sizeof (ACPI_TABLE_HEADER)) / 8;
738
739     AcpiOsPrintf ("%d Tables defined in RSDT/XSDT:\n", NumTables);
740
741     for (i = 0; i < NumTables; i++)
742     {
743         PhysicalAddress = (UINT32) AcpiGbl_XSDT->TableOffsetEntry[i].Lo;
744         CopyExtendedToReal (&ThisTable, PhysicalAddress, sizeof (ACPI_TABLE_HEADER));
745         AcpiOsPrintf ("[%4.4s] ", ThisTable.Signature);
746         ((char *) Table) += 8;
747     }
748
749     AcpiOsPrintf ("\n");
750 }
751
752 ACPI_STATUS
753 AfGetTable (
754     ACPI_TABLE_HEADER       *TableHeader,
755     UINT32                  PhysicalAddress,
756     UINT8                   **TablePtr)
757 {
758
759     /* Allocate a buffer for the entire table */
760
761     *TablePtr = (void *) malloc ((size_t) TableHeader->Length);
762     if (!*TablePtr)
763     {
764         AcpiOsPrintf ("Could not allocate buffer for table length 0x%X\n",
765             (UINT32) TableHeader->Length);
766         return AE_NO_MEMORY;
767     }
768
769     /* Get the entire table */
770
771     CopyExtendedToReal (*TablePtr, PhysicalAddress, TableHeader->Length);
772     AcpiOsPrintf ("%4.4s at %p (Phys %8.8X)\n",
773         TableHeader->Signature, *TablePtr, (UINT32) PhysicalAddress);
774
775     if (AcpiGbl_DbOpt_verbose)
776     {
777         AcpiUtDumpBuffer ((char *) TableHeader, sizeof (ACPI_TABLE_HEADER), 0, 0);
778     }
779
780     return (AE_OK);
781 }
782
783
784 ACPI_STATUS
785 AfGetTableFromXsdt (
786     char                    *TableName,
787     UINT8                   **TablePtr)
788 {
789     UINT32                  i;
790     UINT32                  NumTables;
791     UINT32                  PhysicalAddress;
792     ACPI_TABLE_HEADER       ThisTable;
793
794
795     NumTables = (AcpiGbl_XSDT->Length - sizeof (ACPI_TABLE_HEADER)) / 8;
796
797     for (i = 0; i < NumTables; i++)
798     {
799         PhysicalAddress = (UINT32) AcpiGbl_XSDT->TableOffsetEntry[i].Lo;
800         CopyExtendedToReal (&ThisTable, PhysicalAddress, sizeof (ACPI_TABLE_HEADER));
801
802         if (!ACPI_STRNCMP (TableName, ThisTable.Signature, ACPI_NAME_SIZE))
803         {
804             AfGetTable (&ThisTable, PhysicalAddress, TablePtr);
805             return (AE_OK);
806         }
807     }
808
809     return (AE_NOT_FOUND);
810 }
811
812
813 /******************************************************************************
814  *
815  * FUNCTION:    AfFindTable
816  *
817  * PARAMETERS:
818  *
819  * RETURN:      Status
820  *
821  * DESCRIPTION: Load the DSDT from the file pointer
822  *
823  *****************************************************************************/
824
825 ACPI_STATUS
826 AfFindTable (
827     char                    *TableName,
828     UINT8                   **TablePtr,
829     UINT32                  *TableLength)
830 {
831     ACPI_STATUS             Status;
832
833
834     ACPI_FUNCTION_TRACE ("AfFindTable");
835
836
837     if (!AcpiGbl_DSDT)
838     {
839         Status = AfGetRsdt ();
840         if (ACPI_FAILURE (Status))
841         {
842             return (Status);
843         }
844
845         AfDumpRsdt ();
846
847         /* Get the rest of the required tables (DSDT, FADT) */
848
849         Status = AfGetAllTables (AcpiGbl_RsdtTableCount, NULL);
850         if (ACPI_FAILURE (Status))
851         {
852             goto ErrorExit;
853         }
854
855         ACPI_DEBUG_PRINT ((ACPI_DB_INIT, "ACPI Tables successfully acquired\n"));
856     }
857
858
859     if (!ACPI_STRNCMP (TableName, DSDT_SIG, ACPI_NAME_SIZE))
860     {
861         *TablePtr = (UINT8 *) AcpiGbl_DSDT;
862         *TableLength = AcpiGbl_DSDT->Length;
863     }
864     else if (!ACPI_STRNCMP (TableName, FADT_SIG, ACPI_NAME_SIZE))
865     {
866         *TablePtr = (UINT8 *) AcpiGbl_FADT;
867         *TableLength = AcpiGbl_FADT->Length;
868     }
869     else if (!ACPI_STRNCMP (TableName, FACS_SIG, ACPI_NAME_SIZE))
870     {
871         *TablePtr = (UINT8 *) AcpiGbl_FACS;
872         *TableLength = AcpiGbl_FACS->Length;
873     }
874     else if (!ACPI_STRNCMP (TableName, RSDT_SIG, ACPI_NAME_SIZE))
875     {
876         *TablePtr = (UINT8 *) AcpiGbl_XSDT;
877         *TableLength = AcpiGbl_XSDT->Length;
878     }
879     else if (!ACPI_STRNCMP (TableName, SSDT_SIG, ACPI_NAME_SIZE))
880     {
881         AcpiOsPrintf ("Unsupported table signature: [%4.4s]\n", TableName);
882         *TablePtr = NULL;
883         return AE_SUPPORT;
884     }
885     else
886     {
887         Status = AfGetTableFromXsdt (TableName, TablePtr);
888         if (ACPI_FAILURE (Status))
889         {
890             goto ErrorExit;
891         }
892
893         *TableLength = (*((ACPI_TABLE_HEADER **)TablePtr))->Length;
894     }
895
896     return AE_OK;
897
898 ErrorExit:
899
900     ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Failure during ACPI Table initialization: %s\n",
901             AcpiFormatException (Status)));
902     return (Status);
903 }
904
905 #ifdef _HPET
906 UINT8           Hbuf[1024];
907
908 void
909 AfGetHpet (void)
910 {
911     HPET_DESCRIPTION_TABLE  *NewTable;
912     ACPI_TABLE_HEADER       TableHeader;
913     ACPI_STATUS             Status;
914     UINT16                  i;
915     UINT32                  Value;
916     UINT32                  Value2;
917
918
919     /* Get HPET  TEMP! */
920
921     ACPI_STRNCPY (TableHeader.Signature, "HPET", 4);
922     Status = AcpiOsTableOverride (&TableHeader, (ACPI_TABLE_HEADER **) &NewTable);
923     if (ACPI_FAILURE (Status))
924     {
925         return;
926     }
927
928     AcpiDbgLevel |= ACPI_LV_TABLES;
929     AcpiOsPrintf ("HPET table :\n");
930     AcpiUtDumpBuffer ((char *) NewTable, NewTable->Header.Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
931
932     for (i = 0; i < 1024; i++)
933     {
934         Hbuf[i] = (UINT8) i;
935     }
936
937     /* enable HPET */
938
939     CopyExtendedToReal (&Value, 0xD0, 4);
940     AcpiOsPrintf ("Reg 0xD0: %8.8X\n", Value);
941
942     Value = dIn32 (0xD0);
943     AcpiOsPrintf ("Port 0xD0: %8.8X\n", Value);
944     Value |= ACPI_ENABLE_HPET;
945
946     vOut32 (0xD0, Value);
947     Value2 = dIn32 (0xD0);
948     AcpiOsPrintf ("Port 0xD0: Wrote: %8.8X got %8.8X\n", Value, Value2);
949
950     AcpiOsPrintf ("HPET block(at %8.8X):\n", NewTable->BaseAddress[2]);
951     CopyExtendedToReal (Hbuf, NewTable->BaseAddress[2], 1024);
952     AcpiUtDumpBuffer ((char *) Hbuf, 1024, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
953 }
954 #endif /* _HPET */
955
956 #endif /* IA16 */
957