Update ACPI build wrappers to use new ACPICA(20050309) code.
[dragonfly.git] / sys / contrib / dev / acpica-unix-20050211 / tools / acpiexec / aemain.c
1 /******************************************************************************
2  *
3  * Module Name: aemain - Main routine for the AcpiExec utility
4  *              $Revision: 93 $
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 #include <stdio.h>
118 #include <stdlib.h>
119 #include <string.h>
120 #include <signal.h>
121
122 #include "acpi.h"
123 #include "amlcode.h"
124 #include "acparser.h"
125 #include "acnamesp.h"
126 #include "acinterp.h"
127 #include "acdebug.h"
128 #include "acapps.h"
129
130 #include "aecommon.h"
131
132 #ifdef _DEBUG
133 #if ACPI_MACHINE_WIDTH != 16
134 #include <crtdbg.h>
135 #endif
136 #endif
137
138 #define _COMPONENT          PARSER
139         ACPI_MODULE_NAME    ("aemain")
140
141
142 UINT8 AcpiGbl_BatchMode = FALSE;
143 char  *AcpiGbl_BatchMethodName;
144
145 #if ACPI_MACHINE_WIDTH == 16
146
147 ACPI_STATUS
148 AcpiGetIrqRoutingTable  (
149     ACPI_HANDLE             DeviceHandle,
150     ACPI_BUFFER             *RetBuffer)
151 {
152     return AE_NOT_IMPLEMENTED;
153 }
154 #endif
155
156
157 UINT32
158 AeGpeHandler (
159     void                        *Context)
160 {
161
162
163     AcpiOsPrintf ("Received a GPE at handler\n");
164     return (0);
165 }
166
167 void
168 AfInstallGpeBlock (void)
169 {
170     ACPI_STATUS                 Status;
171     ACPI_HANDLE                 Handle;
172     ACPI_HANDLE                 Handle2 = NULL;
173     ACPI_HANDLE                 Handle3 = NULL;
174     ACPI_GENERIC_ADDRESS        BlockAddress;
175
176
177     Status = AcpiGetHandle (NULL, "\\_GPE", &Handle);
178     if (ACPI_FAILURE (Status))
179     {
180         return;
181     }
182
183     BlockAddress.AddressSpaceId = 0;
184 #if ACPI_MACHINE_WIDTH != 16
185     ACPI_STORE_ADDRESS (BlockAddress.Address, 0x8000000076540000);
186 #else
187     ACPI_STORE_ADDRESS (BlockAddress.Address, 0x76540000);
188 #endif
189
190 //    Status = AcpiInstallGpeBlock (Handle, &BlockAddress, 4, 8);
191
192     /* Above should fail, ignore */
193
194     Status = AcpiGetHandle (NULL, "\\GPE2", &Handle2);
195     if (ACPI_SUCCESS (Status))
196     {
197         Status = AcpiInstallGpeBlock (Handle2, &BlockAddress, 8, 8);
198
199         AcpiInstallGpeHandler (Handle2, 8, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
200         AcpiSetGpeType (Handle2, 8, ACPI_GPE_TYPE_WAKE);
201         AcpiEnableGpe (Handle2, 8, 0);
202     }
203
204     Status = AcpiGetHandle (NULL, "\\GPE3", &Handle3);
205     if (ACPI_SUCCESS (Status))
206     {
207         Status = AcpiInstallGpeBlock (Handle3, &BlockAddress, 8, 11);
208     }
209
210 //    Status = AcpiRemoveGpeBlock (Handle);
211 //    Status = AcpiRemoveGpeBlock (Handle2);
212 //    Status = AcpiRemoveGpeBlock (Handle3);
213
214 }
215
216 /******************************************************************************
217  *
218  * FUNCTION:    usage
219  *
220  * PARAMETERS:  None
221  *
222  * RETURN:      None
223  *
224  * DESCRIPTION: Print a usage message
225  *
226  *****************************************************************************/
227
228 void
229 usage (void)
230 {
231     printf ("Usage: acpiexec [-?dgis] [-x DebugLevel] [-o OutputFile] [-b [Method]] [AcpiTableFile]\n");
232     printf ("Where:\n");
233     printf ("    Input Options\n");
234     printf ("        AcpiTableFile       Get ACPI tables from this file\n");
235     printf ("    Output Options\n");
236     printf ("    Miscellaneous Options\n");
237     printf ("        -?                  Display this message\n");
238     printf ("        -b [Method]         Batch mode method execution\n");
239     printf ("        -i                  Do not run STA/INI methods\n");
240     printf ("        -x DebugLevel       Specify debug output level\n");
241     printf ("        -v                  Verbose init output\n");
242 }
243
244
245 /******************************************************************************
246  *
247  * FUNCTION:    main
248  *
249  * PARAMETERS:  argc, argv
250  *
251  * RETURN:      Status
252  *
253  * DESCRIPTION: Main routine for AcpiDump utility
254  *
255  *****************************************************************************/
256
257 int ACPI_SYSTEM_XFACE
258 main (
259     int                     argc,
260     char                    **argv)
261 {
262     int                     j;
263     ACPI_STATUS             Status;
264     UINT32                  InitFlags;
265     ACPI_BUFFER             ReturnBuf;
266     ACPI_TABLE_HEADER       *Table;
267     char                    Buffer[32];
268
269
270 #ifdef _DEBUG
271 #if ACPI_MACHINE_WIDTH != 16
272     _CrtSetDbgFlag (_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF |
273                     _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
274 #endif
275 #endif
276
277     signal (SIGINT, AeCtrlCHandler);
278
279     /* Init globals */
280
281     AcpiDbgLevel = ACPI_NORMAL_DEFAULT;
282     AcpiDbgLayer = 0xFFFFFFFF;
283
284     /* Init ACPI and start debugger thread */
285
286     AcpiInitializeSubsystem ();
287     AcpiGbl_GlobalLockPresent = TRUE;
288
289     printf ("\nIntel ACPI Component Architecture\nAML Execution/Debug Utility");
290
291 #if ACPI_MACHINE_WIDTH == 16
292     printf (" (16-bit)");
293 #endif
294
295     printf (" version %8.8X", ((UINT32) ACPI_CA_VERSION));
296     printf (" [%s]\n\n",  __DATE__);
297
298     /* Get the command line options */
299
300     while ((j = AcpiGetopt (argc, argv, "?b^dgio:svx:")) != EOF) switch(j)
301     {
302     case 'b':
303         AcpiGbl_BatchMode = TRUE;
304         switch (AcpiGbl_Optarg[0])
305         {
306         case '^':
307             AcpiGbl_BatchMethodName = "MAIN";
308             break;
309         default:
310             AcpiGbl_BatchMethodName = AcpiGbl_Optarg;
311             break;
312         }
313         break;
314
315     case 'd':
316         AcpiGbl_DbOpt_disasm = TRUE;
317         AcpiGbl_DbOpt_stats = TRUE;
318         break;
319
320     case 'g':
321         AcpiGbl_DbOpt_tables = TRUE;
322         AcpiGbl_DbFilename = NULL;
323         break;
324
325     case 'i':
326         AcpiGbl_DbOpt_ini_methods = FALSE;
327         break;
328
329     case 'x':
330         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 0);
331         AcpiGbl_DbConsoleDebugLevel = AcpiDbgLevel;
332         printf ("Debug Level: %lX\n", AcpiDbgLevel);
333         break;
334
335     case 'o':
336         printf ("O option is not implemented\n");
337         break;
338
339     case 's':
340         AcpiGbl_DbOpt_stats = TRUE;
341         break;
342
343     case 'v':
344         AcpiDbgLevel |= ACPI_LV_INIT_NAMES;
345         break;
346
347     case '?':
348     default:
349         usage();
350         return -1;
351     }
352
353
354     InitFlags = (ACPI_NO_HANDLER_INIT | ACPI_NO_ACPI_ENABLE);
355     if (!AcpiGbl_DbOpt_ini_methods)
356     {
357         InitFlags |= (ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
358     }
359
360     /* Standalone filename is the only argument */
361
362     if (argv[AcpiGbl_Optind])
363     {
364         AcpiGbl_DbOpt_tables = TRUE;
365         AcpiGbl_DbFilename = argv[AcpiGbl_Optind];
366
367         Status = AcpiDbReadTableFromFile (AcpiGbl_DbFilename, &Table);
368         if (ACPI_FAILURE (Status))
369         {
370             printf ("**** Could not get input table, %s\n", AcpiFormatException (Status));
371             goto enterloop;
372         }
373
374         AeBuildLocalTables (Table);
375         Status = AeInstallTables ();
376         if (ACPI_FAILURE (Status))
377         {
378             printf ("**** Could not load ACPI tables, %s\n", AcpiFormatException (Status));
379             goto enterloop;
380         }
381
382         Status = AeInstallHandlers ();
383         if (ACPI_FAILURE (Status))
384         {
385             goto enterloop;
386         }
387
388         /*
389          * TBD:
390          * Need a way to call this after the "LOAD" command
391          */
392         Status = AcpiEnableSubsystem (InitFlags);
393         if (ACPI_FAILURE (Status))
394         {
395             printf ("**** Could not EnableSubsystem, %s\n", AcpiFormatException (Status));
396             goto enterloop;
397         }
398
399         Status = AcpiInitializeObjects (InitFlags);
400         if (ACPI_FAILURE (Status))
401         {
402             printf ("**** Could not InitializeObjects, %s\n", AcpiFormatException (Status));
403             goto enterloop;
404         }
405
406
407         ReturnBuf.Length = 32;
408         ReturnBuf.Pointer = Buffer;
409         AcpiGetName (AcpiGbl_RootNode, ACPI_FULL_PATHNAME, &ReturnBuf);
410         AcpiEnableEvent (ACPI_EVENT_GLOBAL, 0);
411
412         AcpiInstallGpeHandler (NULL, 0, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
413         AcpiSetGpeType (NULL, 0, ACPI_GPE_TYPE_WAKE_RUN);
414         AcpiEnableGpe (NULL, 0, ACPI_NOT_ISR);
415         AcpiRemoveGpeHandler (NULL, 0, AeGpeHandler);
416
417         AcpiInstallGpeHandler (NULL, 0, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
418         AcpiSetGpeType (NULL, 0, ACPI_GPE_TYPE_WAKE_RUN);
419         AcpiEnableGpe (NULL, 0, ACPI_NOT_ISR);
420
421         AcpiInstallGpeHandler (NULL, 1, ACPI_GPE_EDGE_TRIGGERED, AeGpeHandler, NULL);
422         AcpiSetGpeType (NULL, 1, ACPI_GPE_TYPE_RUNTIME);
423         AcpiEnableGpe (NULL, 1, ACPI_NOT_ISR);
424
425         AcpiInstallGpeHandler (NULL, 2, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
426         AcpiSetGpeType (NULL, 2, ACPI_GPE_TYPE_WAKE);
427         AcpiEnableGpe (NULL, 2, ACPI_NOT_ISR);
428
429         AcpiInstallGpeHandler (NULL, 3, ACPI_GPE_EDGE_TRIGGERED, AeGpeHandler, NULL);
430         AcpiSetGpeType (NULL, 3, ACPI_GPE_TYPE_WAKE_RUN);
431
432         AcpiInstallGpeHandler (NULL, 4, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
433         AcpiSetGpeType (NULL, 4, ACPI_GPE_TYPE_RUNTIME);
434
435         AcpiInstallGpeHandler (NULL, 5, ACPI_GPE_EDGE_TRIGGERED, AeGpeHandler, NULL);
436         AcpiSetGpeType (NULL, 5, ACPI_GPE_TYPE_WAKE);
437
438         AcpiInstallGpeHandler (NULL, 0x19, ACPI_GPE_LEVEL_TRIGGERED, AeGpeHandler, NULL);
439         AcpiSetGpeType (NULL, 0x19, ACPI_GPE_TYPE_WAKE_RUN);
440         AcpiEnableGpe (NULL, 0x19, ACPI_NOT_ISR);
441
442
443         AfInstallGpeBlock ();
444     }
445
446 #if ACPI_MACHINE_WIDTH == 16
447     else
448     {
449 #include "16bit.h"
450
451         Status = AfFindTable (DSDT_SIG, NULL, NULL);
452         if (ACPI_FAILURE (Status))
453         {
454             goto enterloop;
455         }
456
457
458         if (ACPI_FAILURE (Status))
459         {
460             printf ("**** Could not load ACPI tables, %s\n", AcpiFormatException (Status));
461             goto enterloop;
462         }
463
464
465         Status = AcpiNsLoadNamespace ();
466         if (ACPI_FAILURE (Status))
467         {
468             printf ("**** Could not load ACPI namespace, %s\n", AcpiFormatException (Status));
469             goto enterloop;
470         }
471
472
473         Status = AeInstallHandlers ();
474         if (ACPI_FAILURE (Status))
475         {
476             goto enterloop;
477         }
478         /* TBD:
479          * Need a way to call this after the "LOAD" command
480          */
481         Status = AcpiEnableSubsystem (InitFlags);
482         if (ACPI_FAILURE (Status))
483         {
484             printf ("**** Could not EnableSubsystem, %s\n", AcpiFormatException (Status));
485             goto enterloop;
486         }
487
488         Status = AcpiInitializeObjects (InitFlags);
489         if (ACPI_FAILURE (Status))
490         {
491             printf ("**** Could not InitializeObjects, %s\n", AcpiFormatException (Status));
492             goto enterloop;
493         }
494
495      }
496 #endif
497
498 enterloop:
499
500     if (AcpiGbl_BatchMode)
501     {
502         AcpiDbExecute (AcpiGbl_BatchMethodName, NULL, EX_NO_SINGLE_STEP);
503     }
504     else
505     {
506         /* Enter the debugger command loop */
507
508         AcpiDbUserCommands (ACPI_DEBUGGER_COMMAND_PROMPT, NULL);
509     }
510
511     return 0;
512 }