checkdirs() was being passed the wrong mount point, resulting in a panic
[dragonfly.git] / sys / contrib / dev / acpica-unix-20050309 / tools / acpiexec / aeexec.c
1 /******************************************************************************
2  *
3  * Module Name: aeexec - Support routines for AcpiExec utility
4  *              $Revision: 83 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2005, 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
118 #include "acpi.h"
119 #include "acparser.h"
120 #include "amlcode.h"
121 #include "acnamesp.h"
122 #include "acdebug.h"
123 #include "actables.h"
124 #include "aecommon.h"
125
126 #include <stdio.h>
127 #include <signal.h>
128
129
130 #define _COMPONENT          ACPI_TOOLS
131         ACPI_MODULE_NAME    ("aeexec")
132
133
134 ACPI_PARSE_OBJECT           *AcpiGbl_ParsedNamespaceRoot;
135 ACPI_PARSE_OBJECT           *root;
136 UINT8                       *AmlStart;
137 UINT32                      AmlLength;
138 UINT8                       *DsdtPtr;
139 UINT32                      AcpiDsdtLength;
140
141 DEBUG_REGIONS               AeRegions;
142 RSDP_DESCRIPTOR             LocalRsdp;
143
144 unsigned char Ssdt1Code[] =
145 {
146     0x53,0x53,0x44,0x54,0x30,0x00,0x00,0x00,  /* 00000000    "SSDT0..." */
147     0x01,0xB8,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    "..Intel." */
148     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
149     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
150     0x24,0x04,0x03,0x20,0x14,0x0B,0x5F,0x54,  /* 00000020    "$.. .._T" */
151     0x39,0x38,0x00,0x70,0x0A,0x04,0x60,0xA4,  /* 00000028    "98.p..`." */
152 };
153
154 unsigned char Ssdt2Code[] =
155 {
156     0x53,0x53,0x44,0x54,0x30,0x00,0x00,0x00,  /* 00000000    "SSDT0..." */
157     0x01,0xB7,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    "..Intel." */
158     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
159     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
160     0x24,0x04,0x03,0x20,0x14,0x0B,0x5F,0x54,  /* 00000020    "$.. .._T" */
161     0x39,0x39,0x00,0x70,0x0A,0x04,0x60,0xA4,  /* 00000028    "99.p..`." */
162 };
163
164 unsigned char Oem1Code[] =
165 {
166     0x4F,0x45,0x4D,0x31,0x38,0x00,0x00,0x00,  /* 00000000    "OEM18..." */
167     0x01,0x4B,0x49,0x6E,0x74,0x65,0x6C,0x00,  /* 00000008    ".KIntel." */
168     0x4D,0x61,0x6E,0x79,0x00,0x00,0x00,0x00,  /* 00000010    "Many...." */
169     0x01,0x00,0x00,0x00,0x49,0x4E,0x54,0x4C,  /* 00000018    "....INTL" */
170     0x18,0x09,0x03,0x20,0x08,0x5F,0x58,0x54,  /* 00000020    "... ._XT" */
171     0x32,0x0A,0x04,0x14,0x0C,0x5F,0x58,0x54,  /* 00000028    "2...._XT" */
172     0x31,0x00,0x70,0x01,0x5F,0x58,0x54,0x32,  /* 00000030    "1.p._XT2" */
173
174 };
175
176 /*
177  * We need a local FADT so that the hardware subcomponent will function,
178  * even though the underlying OSD HW access functions don't do
179  * anything.
180  */
181 RSDP_DESCRIPTOR             LocalRSDP;
182 FADT_DESCRIPTOR_REV1        LocalFADT;
183 FACS_DESCRIPTOR_REV1        LocalFACS;
184 ACPI_TABLE_HEADER           LocalDSDT;
185 ACPI_TABLE_HEADER           LocalTEST;
186 ACPI_TABLE_HEADER           LocalBADTABLE;
187
188 RSDT_DESCRIPTOR_REV1        *LocalRSDT;
189
190 #define RSDT_TABLES         7
191 #define RSDT_SIZE           (sizeof (RSDT_DESCRIPTOR_REV1) + ((RSDT_TABLES -1) * sizeof (UINT32)))
192
193
194 /******************************************************************************
195  *
196  * FUNCTION:    AeCtrlCHandler
197  *
198  * PARAMETERS:
199  *
200  * RETURN:      none
201  *
202  * DESCRIPTION: Control-C handler.  Abort running control method if any.
203  *
204  *****************************************************************************/
205
206 void __cdecl
207 AeCtrlCHandler (
208     int                     Sig)
209 {
210
211     signal (SIGINT, SIG_IGN);
212
213     AcpiOsPrintf ("Caught a ctrl-c\n\n");
214
215     if (AcpiGbl_MethodExecuting)
216     {
217         AcpiGbl_AbortMethod = TRUE;
218         signal (SIGINT, AeCtrlCHandler);
219     }
220     else
221     {
222         exit (0);
223     }
224 }
225
226
227 /******************************************************************************
228  *
229  * FUNCTION:    AeBuildLocalTables
230  *
231  * PARAMETERS:
232  *
233  * RETURN:      Status
234  *
235  * DESCRIPTION:
236  *
237  *****************************************************************************/
238
239 ACPI_STATUS
240 AeBuildLocalTables (
241     ACPI_TABLE_HEADER       *UserTable)
242 {
243
244
245     /* Build an RSDT */
246
247     LocalRSDT = AcpiOsAllocate (RSDT_SIZE);
248     if (!LocalRSDT)
249     {
250         return AE_NO_MEMORY;
251     }
252
253     ACPI_MEMSET (LocalRSDT, 0, RSDT_SIZE);
254     ACPI_STRNCPY (LocalRSDT->Signature, RSDT_SIG, 4);
255     LocalRSDT->Length = RSDT_SIZE;
256
257     LocalRSDT->TableOffsetEntry[0] = ACPI_PTR_TO_PHYSADDR (&LocalTEST);
258     LocalRSDT->TableOffsetEntry[1] = ACPI_PTR_TO_PHYSADDR (&LocalBADTABLE);
259     LocalRSDT->TableOffsetEntry[2] = ACPI_PTR_TO_PHYSADDR (&LocalFADT);
260     LocalRSDT->TableOffsetEntry[3] = ACPI_PTR_TO_PHYSADDR (&LocalTEST);  /* Just a placeholder for a user SSDT */
261
262     /* Install two SSDTs to test multiple table support */
263
264     LocalRSDT->TableOffsetEntry[4] = ACPI_PTR_TO_PHYSADDR (&Ssdt1Code);
265     LocalRSDT->TableOffsetEntry[5] = ACPI_PTR_TO_PHYSADDR (&Ssdt2Code);
266
267     /* Install the OEM1 table to test LoadTable */
268
269     LocalRSDT->TableOffsetEntry[6] = ACPI_PTR_TO_PHYSADDR (&Oem1Code);
270
271     /* Build an RSDP */
272
273     ACPI_MEMSET (&LocalRSDP, 0, sizeof (RSDP_DESCRIPTOR));
274     ACPI_STRNCPY (LocalRSDP.Signature, RSDP_SIG, 8);
275     LocalRSDP.Revision            = 1;
276     LocalRSDP.RsdtPhysicalAddress = ACPI_PTR_TO_PHYSADDR (LocalRSDT);
277
278     AcpiGbl_RSDP = &LocalRSDP;
279
280     /*
281      * Examine the incoming user table.  At this point, it has been verified
282      * to be either a DSDT, SSDT, or a PSDT, but they must be handled differently
283      */
284     if (!ACPI_STRNCMP ((char *) UserTable->Signature, DSDT_SIG, 4))
285     {
286         /* User DSDT is installed directly into the FADT */
287
288         AcpiGbl_DSDT = UserTable;
289     }
290     else
291     {
292         /* Build a local DSDT because incoming table is an SSDT or PSDT */
293
294         ACPI_MEMSET (&LocalDSDT, 0, sizeof (ACPI_TABLE_HEADER));
295         ACPI_STRNCPY (LocalDSDT.Signature, DSDT_SIG, 4);
296         LocalDSDT.Revision   = 1;
297         LocalDSDT.Length     = sizeof (ACPI_TABLE_HEADER);
298         LocalDSDT.Checksum   = (UINT8) (0 - AcpiTbChecksum (&LocalDSDT, LocalDSDT.Length));
299
300         AcpiGbl_DSDT = &LocalDSDT;
301
302         /* Install incoming table (SSDT or PSDT) directly into the RSDT */
303
304         LocalRSDT->TableOffsetEntry[3] = ACPI_PTR_TO_PHYSADDR (UserTable);
305     }
306
307     /* Set checksums for both RSDT and RSDP */
308
309     LocalRSDT->Checksum = (UINT8) (0 - AcpiTbChecksum (LocalRSDT, LocalRSDT->Length));
310     LocalRSDP.Checksum  = (UINT8) (0 - AcpiTbChecksum (&LocalRSDP, ACPI_RSDP_CHECKSUM_LENGTH));
311
312     /* Build a FADT so we can test the hardware/event init */
313
314     ACPI_MEMSET (&LocalFADT, 0, sizeof (FADT_DESCRIPTOR_REV1));
315     ACPI_STRNCPY (LocalFADT.Signature, FADT_SIG, 4);
316
317     LocalFADT.FirmwareCtrl      = ACPI_PTR_TO_PHYSADDR (&LocalFACS);
318     LocalFADT.Dsdt              = ACPI_PTR_TO_PHYSADDR (AcpiGbl_DSDT);
319     LocalFADT.Revision          = 1;
320     LocalFADT.Length            = sizeof (FADT_DESCRIPTOR_REV1);
321     LocalFADT.Gpe0BlkLen        = 16;
322     LocalFADT.Gpe1BlkLen        = 6;
323     LocalFADT.Gpe1Base          = 96;
324
325     LocalFADT.Pm1EvtLen         = 4;
326     LocalFADT.Pm1CntLen         = 4;
327     LocalFADT.PmTmLen           = 8;
328
329     LocalFADT.Gpe0Blk           = 0x12340000;
330     LocalFADT.Gpe1Blk           = 0x56780000;
331
332     LocalFADT.Pm1aEvtBlk        = 0x1aaa0000;
333     LocalFADT.Pm1bEvtBlk        = 0;
334     LocalFADT.PmTmrBlk          = 0xA0;
335     LocalFADT.Pm1aCntBlk        = 0xB0;
336
337     /* Complete the FADT with the checksum */
338
339     LocalFADT.Checksum = (UINT8) (0 - AcpiTbChecksum (&LocalFADT, LocalFADT.Length));
340
341     /* Build a FACS */
342
343     ACPI_MEMSET (&LocalFACS, 0, sizeof (FACS_DESCRIPTOR_REV1));
344     ACPI_STRNCPY (LocalFACS.Signature, FACS_SIG, 4);
345     LocalFACS.Length = sizeof (FACS_DESCRIPTOR_REV1);
346     LocalFACS.GlobalLock = 0x11AA0011;
347
348     /* Build a fake table so that we make sure that the CA core ignores it */
349
350     ACPI_MEMSET (&LocalTEST, 0, sizeof (ACPI_TABLE_HEADER));
351     ACPI_STRNCPY (LocalTEST.Signature, "TEST", 4);
352
353     LocalTEST.Revision   = 1;
354     LocalTEST.Length     = sizeof (ACPI_TABLE_HEADER);
355
356     /* Build a fake table with a bad signature so that we make sure that the CA core ignores it */
357
358     ACPI_MEMSET (&LocalBADTABLE, 0, sizeof (ACPI_TABLE_HEADER));
359     ACPI_STRNCPY (LocalBADTABLE.Signature, "BAD!", 4);
360
361     LocalBADTABLE.Revision   = 1;
362     LocalBADTABLE.Length     = sizeof (ACPI_TABLE_HEADER);
363
364     return (AE_OK);
365 }
366
367
368 /******************************************************************************
369  *
370  * FUNCTION:    AeInstallTables
371  *
372  * PARAMETERS:
373  *
374  * RETURN:      Status
375  *
376  * DESCRIPTION:
377  *
378  *****************************************************************************/
379
380 ACPI_STATUS
381 AeInstallTables (void)
382 {
383     ACPI_STATUS             Status;
384
385
386     Status = AcpiLoadTables ();
387
388 #if 0
389     Status = AcpiLoadTable ((ACPI_TABLE_HEADER *) &LocalFADT);
390     if (ACPI_FAILURE (Status))
391     {
392         printf ("**** Could not load local FADT, %s\n", AcpiFormatException (Status));
393         return (Status);
394     }
395
396     Status = AcpiLoadTable ((ACPI_TABLE_HEADER *) &LocalFACS);
397     if (ACPI_FAILURE (Status))
398     {
399         printf ("**** Could not load local FACS, %s\n", AcpiFormatException (Status));
400         return (Status);
401     }
402 #endif
403
404     return (Status);
405 }
406
407
408 /******************************************************************************
409  *
410  * FUNCTION:    AeLocalGetRootPointer
411  *
412  * PARAMETERS:
413  *
414  * RETURN:      Status
415  *
416  * DESCRIPTION: Return a local RSDP, used to dynamically load tables via the
417  *              standard ACPI mechanism.
418  *
419  *****************************************************************************/
420
421 ACPI_STATUS
422 AeLocalGetRootPointer (
423     UINT32                  Flags,
424     ACPI_POINTER            *Address)
425 {
426
427     Address->PointerType     = ACPI_LOGICAL_POINTER;
428     Address->Pointer.Logical = &LocalRSDP;
429     return (AE_OK);
430 }
431
432
433 /******************************************************************************
434  *
435  * FUNCTION:    AeRegionHandler
436  *
437  * PARAMETERS:  Standard region handler parameters
438  *
439  * RETURN:      Status
440  *
441  * DESCRIPTION: Test handler - Handles some dummy regions via memory that can
442  *              be manipulated in Ring 3.
443  *
444  *****************************************************************************/
445
446 ACPI_STATUS
447 AeRegionHandler (
448     UINT32                  Function,
449     ACPI_PHYSICAL_ADDRESS   Address,
450     UINT32                  BitWidth,
451     ACPI_INTEGER            *Value,
452     void                    *HandlerContext,
453     void                    *RegionContext)
454 {
455
456     ACPI_OPERAND_OBJECT     *RegionObject = (ACPI_OPERAND_OBJECT*) RegionContext;
457     ACPI_PHYSICAL_ADDRESS   BaseAddress;
458     ACPI_SIZE               Length;
459     BOOLEAN                 BufferExists;
460     REGION                  *RegionElement;
461     void                    *BufferValue;
462     UINT32                  ByteWidth;
463     UINT32                  i;
464
465
466     ACPI_FUNCTION_NAME ("AeRegionHandler");
467
468     /*
469      * If the object is not a region, simply return
470      */
471     if (RegionObject->Region.Type != ACPI_TYPE_REGION)
472     {
473         return AE_OK;
474     }
475
476     /*
477      * Find the region's address space and length before searching
478      * the linked list.
479      */
480     BaseAddress = RegionObject->Region.Address;
481     Length = (ACPI_SIZE) RegionObject->Region.Length;
482
483     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION, "Operation Region request on %s at 0x%X\n",
484             AcpiUtGetRegionName (RegionObject->Region.SpaceId),
485             Address));
486
487     if (RegionObject->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
488     {
489         Length = 0;
490
491         switch (Function & ACPI_IO_MASK)
492         {
493         case ACPI_READ:
494             switch (Function >> 16)
495             {
496             case AML_FIELD_ATTRIB_SMB_QUICK:
497             case AML_FIELD_ATTRIB_SMB_SEND_RCV:
498             case AML_FIELD_ATTRIB_SMB_BYTE:
499                 Length = 1;
500                 break;
501
502             case AML_FIELD_ATTRIB_SMB_WORD:
503             case AML_FIELD_ATTRIB_SMB_WORD_CALL:
504                 Length = 2;
505                 break;
506
507             case AML_FIELD_ATTRIB_SMB_BLOCK:
508             case AML_FIELD_ATTRIB_SMB_BLOCK_CALL:
509                 Length = 32;
510                 break;
511
512             default:
513                 break;
514             }
515             break;
516
517         case ACPI_WRITE:
518             switch (Function >> 16)
519             {
520             case AML_FIELD_ATTRIB_SMB_QUICK:
521             case AML_FIELD_ATTRIB_SMB_SEND_RCV:
522             case AML_FIELD_ATTRIB_SMB_BYTE:
523             case AML_FIELD_ATTRIB_SMB_WORD:
524             case AML_FIELD_ATTRIB_SMB_BLOCK:
525                 Length = 0;
526                 break;
527
528             case AML_FIELD_ATTRIB_SMB_WORD_CALL:
529                 Length = 2;
530                 break;
531
532             case AML_FIELD_ATTRIB_SMB_BLOCK_CALL:
533                 Length = 32;
534                 break;
535
536             default:
537                 break;
538             }
539             break;
540
541         default:
542             break;
543         }
544
545         for (i = 0; i < Length; i++)
546         {
547             ((UINT8 *) Value)[i+2] = (UINT8) (0xA0 + i);
548         }
549
550         ((UINT8 *) Value)[0] = 0x7A;
551         ((UINT8 *) Value)[1] = (UINT8) Length;
552
553         return AE_OK;
554     }
555
556     /*
557      * Search through the linked list for this region's buffer
558      */
559     BufferExists = FALSE;
560     RegionElement = AeRegions.RegionList;
561
562     if (AeRegions.NumberOfRegions)
563     {
564         while (!BufferExists && RegionElement)
565         {
566             if (RegionElement->Address == BaseAddress &&
567                 RegionElement->Length == Length)
568             {
569                 BufferExists = TRUE;
570             }
571             else
572             {
573                 RegionElement = RegionElement->NextRegion;
574             }
575         }
576     }
577
578     /*
579      * If the Region buffer does not exist, create it now
580      */
581     if (!BufferExists)
582     {
583         /*
584          * Do the memory allocations first
585          */
586         RegionElement = AcpiOsAllocate (sizeof (REGION));
587         if (!RegionElement)
588         {
589             return AE_NO_MEMORY;
590         }
591
592         RegionElement->Buffer = AcpiOsAllocate (Length);
593         if (!RegionElement->Buffer)
594         {
595             AcpiOsFree (RegionElement);
596             return AE_NO_MEMORY;
597         }
598
599         ACPI_MEMSET (RegionElement->Buffer, 0, Length);
600         RegionElement->Address      = BaseAddress;
601         RegionElement->Length       = Length;
602         RegionElement->NextRegion   = NULL;
603
604         /*
605          * Increment the number of regions and put this one
606          *  at the head of the list as it will probably get accessed
607          *  more often anyway.
608          */
609         AeRegions.NumberOfRegions += 1;
610
611         if (NULL != AeRegions.RegionList)
612         {
613             RegionElement->NextRegion = AeRegions.RegionList->NextRegion;
614         }
615
616         AeRegions.RegionList = RegionElement;
617     }
618
619     /*
620      * Calculate the size of the memory copy
621      */
622     ByteWidth = (BitWidth / 8);
623
624     if (BitWidth % 8)
625     {
626         ByteWidth += 1;
627     }
628
629     /*
630      * The buffer exists and is pointed to by RegionElement.
631      * We now need to verify the request is valid and perform the operation.
632      *
633      * NOTE: RegionElement->Length is in bytes, therefore it we compare against
634      * ByteWidth (see above)
635      */
636     if (((ACPI_INTEGER) Address + ByteWidth) >
637         ((ACPI_INTEGER)(RegionElement->Address) + RegionElement->Length))
638     {
639         ACPI_DEBUG_PRINT ((ACPI_DB_WARN, "Request on [%4.4s] is beyond region limit Req-%lX+%lX, Base=%lX, Len-%lX\n",
640                 &((RegionObject->Region.Node)->Name), (UINT32) Address, ByteWidth, (UINT32)(RegionElement->Address),
641                 RegionElement->Length));
642
643         return AE_AML_REGION_LIMIT;
644     }
645
646     /*
647      * Get BufferValue to point to the "address" in the buffer
648      */
649     BufferValue = ((UINT8 *) RegionElement->Buffer +
650                     ((ACPI_INTEGER) Address - (ACPI_INTEGER) RegionElement->Address));
651
652     /*
653      * Perform a read or write to the buffer space
654      */
655     switch (Function)
656     {
657     case ACPI_READ:
658         /*
659          * Set the pointer Value to whatever is in the buffer
660          */
661         ACPI_MEMCPY (Value, BufferValue, ByteWidth);
662         break;
663
664     case ACPI_WRITE:
665         /*
666          * Write the contents of Value to the buffer
667          */
668         ACPI_MEMCPY (BufferValue, Value, ByteWidth);
669         break;
670
671     default:
672         return AE_BAD_PARAMETER;
673     }
674     return AE_OK;
675 }
676
677
678 /******************************************************************************
679  *
680  * FUNCTION:    AeRegionInit
681  *
682  * PARAMETERS:  None
683  *
684  * RETURN:      Status
685  *
686  * DESCRIPTION: Opregion init function.
687  *
688  *****************************************************************************/
689
690 ACPI_STATUS
691 AeRegionInit (
692     ACPI_HANDLE                 RegionHandle,
693     UINT32                      Function,
694     void                        *HandlerContext,
695     void                        **RegionContext)
696 {
697     /*
698      * Real simple, set the RegionContext to the RegionHandle
699      */
700     *RegionContext = RegionHandle;
701
702     return AE_OK;
703 }
704
705
706 /******************************************************************************
707  *
708  * FUNCTION:    AeNotifyHandler
709  *
710  * PARAMETERS:  Standard notify handler parameters
711  *
712  * RETURN:      Status
713  *
714  * DESCRIPTION: System notify handler for AcpiExec utility.  Used by the ASL
715  *              test suite(s) to communicate errors and other information to
716  *              this utility via the Notify() operator.
717  *
718  *****************************************************************************/
719
720 void
721 AeNotifyHandler (
722     ACPI_HANDLE                 Device,
723     UINT32                      Value,
724     void                        *Context)
725 {
726
727     switch (Value)
728     {
729 #if 0
730     case 0:
731         printf ("**** Method Error 0x%X: Results not equal\n", Value);
732         if (AcpiGbl_DebugFile)
733         {
734             AcpiOsPrintf ("**** Method Error: Results not equal\n");
735         }
736         break;
737
738
739     case 1:
740         printf ("**** Method Error: Incorrect numeric result\n");
741         if (AcpiGbl_DebugFile)
742         {
743             AcpiOsPrintf ("**** Method Error: Incorrect numeric result\n");
744         }
745         break;
746
747
748     case 2:
749         printf ("**** Method Error: An operand was overwritten\n");
750         if (AcpiGbl_DebugFile)
751         {
752             AcpiOsPrintf ("**** Method Error: An operand was overwritten\n");
753         }
754         break;
755
756 #endif
757
758     default:
759         printf ("**** Received a Notify on Device [%4.4s] %p value 0x%X\n",
760             AcpiUtGetNodeName (Device), Device, Value);
761         if (AcpiGbl_DebugFile)
762         {
763             AcpiOsPrintf ("**** Received a notify, value 0x%X\n", Value);
764         }
765         break;
766     }
767
768 }
769
770
771 /******************************************************************************
772  *
773  * FUNCTION:    AeExceptionHandler
774  *
775  * PARAMETERS:  Standard exception handler parameters
776  *
777  * RETURN:      Status
778  *
779  * DESCRIPTION: System exception handler for AcpiExec utility.
780  *
781  *****************************************************************************/
782
783 ACPI_STATUS
784 AeExceptionHandler (
785     ACPI_STATUS             AmlStatus,
786     ACPI_NAME               Name,
787     UINT16                  Opcode,
788     UINT32                  AmlOffset,
789     void                    *Context)
790 {
791     ACPI_STATUS             Status;
792     ACPI_BUFFER             ReturnObj;
793     ACPI_OBJECT_LIST        ArgList;
794     ACPI_OBJECT             Arg[2];
795     const char              *Exception;
796
797
798     Exception = AcpiFormatException (AmlStatus);
799     AcpiOsPrintf (
800         "**** AcpiExec Exception: %s during execution of method [%4.4s] Opcode [%s] @%X\n",
801         Exception, &Name, AcpiPsGetOpcodeName (Opcode), AmlOffset);
802
803     /*
804      * Invoke the _ERR method if present
805      *
806      * Setup parameter object
807      */
808     ArgList.Count = 2;
809     ArgList.Pointer = Arg;
810
811     Arg[0].Type = ACPI_TYPE_INTEGER;
812     Arg[0].Integer.Value = AmlStatus;
813
814     Arg[1].Type = ACPI_TYPE_STRING;
815     Arg[1].String.Pointer = (char *) Exception;
816     Arg[1].String.Length = ACPI_STRLEN (Exception);
817
818     /* Setup return buffer */
819
820     ReturnObj.Pointer = NULL;
821     ReturnObj.Length = ACPI_ALLOCATE_BUFFER;
822
823     Status = AcpiEvaluateObject (NULL, "\\_ERR", &ArgList, &ReturnObj);
824     if (ACPI_SUCCESS (Status) &&
825         ReturnObj.Pointer)
826     {
827         /* Override original status */
828
829         AmlStatus = (ACPI_STATUS)
830             ((ACPI_OBJECT *) ReturnObj.Pointer)->Integer.Value;
831
832         AcpiOsFree (ReturnObj.Pointer);
833     }
834     return (AmlStatus);
835 }
836
837
838 /******************************************************************************
839  *
840  * FUNCTION:    AeInstallHandlers
841  *
842  * PARAMETERS:  None
843  *
844  * RETURN:      Status
845  *
846  * DESCRIPTION: Install handlers for the AcpiExec utility.
847  *              NOTE: Currently only a system notify handler is installed.
848  *
849  *****************************************************************************/
850
851 ACPI_ADR_SPACE_TYPE         SpaceId[] = {0, 1, 2, 3, 4, 0x80};
852 #define AEXEC_NUM_REGIONS   6
853
854 ACPI_STATUS
855 AeInstallHandlers (void)
856 {
857     ACPI_STATUS             Status;
858     UINT32                  i;
859     ACPI_HANDLE             Handle;
860
861
862     ACPI_FUNCTION_NAME ("AeInstallHandlers");
863
864
865     Status = AcpiInstallExceptionHandler (AeExceptionHandler);
866     if (ACPI_FAILURE (Status))
867     {
868         printf ("Could not install exception handler, %s\n",
869             AcpiFormatException (Status));
870     }
871
872     Status = AcpiInstallNotifyHandler (ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
873                                         AeNotifyHandler, NULL);
874     if (ACPI_FAILURE (Status))
875     {
876         printf ("Could not install a global notify handler, %s\n",
877             AcpiFormatException (Status));
878     }
879
880     Status = AcpiGetHandle (NULL, "\\_SB_", &Handle);
881     if (ACPI_SUCCESS (Status))
882     {
883         Status = AcpiInstallNotifyHandler (Handle, ACPI_SYSTEM_NOTIFY,
884                                             AeNotifyHandler, NULL);
885         if (ACPI_FAILURE (Status))
886         {
887             printf ("Could not install a notify handler, %s\n",
888                 AcpiFormatException (Status));
889         }
890
891         Status = AcpiRemoveNotifyHandler (Handle, ACPI_SYSTEM_NOTIFY,
892                                             AeNotifyHandler);
893         if (ACPI_FAILURE (Status))
894         {
895             printf ("Could not remove a notify handler, %s\n",
896                 AcpiFormatException (Status));
897         }
898
899         Status = AcpiInstallNotifyHandler (Handle, ACPI_ALL_NOTIFY,
900                                             AeNotifyHandler, NULL);
901         Status = AcpiRemoveNotifyHandler (Handle, ACPI_ALL_NOTIFY,
902                                             AeNotifyHandler);
903         Status = AcpiInstallNotifyHandler (Handle, ACPI_ALL_NOTIFY,
904                                             AeNotifyHandler, NULL);
905         if (ACPI_FAILURE (Status))
906         {
907             printf ("Could not install a notify handler, %s\n",
908                 AcpiFormatException (Status));
909         }
910
911     }
912
913     for (i = 0; i < AEXEC_NUM_REGIONS; i++)
914     {
915         if (i == 2)
916         {
917             continue;
918         }
919
920         Status = AcpiRemoveAddressSpaceHandler (AcpiGbl_RootNode,
921                         SpaceId[i], AeRegionHandler);
922
923         /* Install handler at the root object.
924          * TBD: all default handlers should be installed here!
925          */
926         Status = AcpiInstallAddressSpaceHandler (AcpiGbl_RootNode,
927                         SpaceId[i], AeRegionHandler, AeRegionInit, NULL);
928         if (ACPI_FAILURE (Status))
929         {
930             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
931                 "Could not install an OpRegion handler for %s space(%d), %s\n",
932                 AcpiUtGetRegionName((UINT8) SpaceId[i]), SpaceId[i], AcpiFormatException (Status)));
933             return (Status);
934         }
935     }
936
937
938     /*
939      * Initialize the global Region Handler space
940      * MCW 3/23/00
941      */
942     AeRegions.NumberOfRegions = 0;
943     AeRegions.RegionList = NULL;
944
945     return Status;
946 }
947
948