3aa9343af28d319f3b1d7b5f1bec211bac1dd642
[dragonfly.git] / sys / contrib / dev / acpica / source / tools / acpiexec / aemain.c
1 /******************************************************************************
2  *
3  * Module Name: aemain - Main routine for the AcpiExec utility
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include "aecommon.h"
45
46 #define _COMPONENT          ACPI_TOOLS
47         ACPI_MODULE_NAME    ("aemain")
48
49
50 /*
51  * Main routine for the ACPI user-space execution utility.
52  *
53  * Portability note: The utility depends upon the host for command-line
54  * wildcard support - it is not implemented locally. For example:
55  *
56  * Linux/Unix systems: Shell expands wildcards automatically.
57  *
58  * Windows: The setargv.obj module must be linked in to automatically
59  * expand wildcards.
60  */
61 extern BOOLEAN              AcpiGbl_DebugTimeout;
62
63 /* Local prototypes */
64
65 static int
66 AeDoOptions (
67     int                     argc,
68     char                    **argv);
69
70 static ACPI_STATUS
71 AcpiDbRunBatchMode (
72     void);
73
74
75 #define AE_BUFFER_SIZE              1024
76 #define ASL_MAX_FILES               256
77
78 /* Execution modes */
79
80 #define AE_MODE_COMMAND_LOOP        0   /* Normal command execution loop */
81 #define AE_MODE_BATCH_MULTIPLE      1   /* -b option to execute a command line */
82 #define AE_MODE_BATCH_SINGLE        2   /* -m option to execute a single control method */
83
84
85 /* Globals */
86
87 UINT8                       AcpiGbl_RegionFillValue = 0;
88 BOOLEAN                     AcpiGbl_IgnoreErrors = FALSE;
89 BOOLEAN                     AcpiGbl_DbOpt_NoRegionSupport = FALSE;
90 UINT8                       AcpiGbl_UseHwReducedFadt = FALSE;
91 BOOLEAN                     AcpiGbl_DoInterfaceTests = FALSE;
92 BOOLEAN                     AcpiGbl_LoadTestTables = FALSE;
93 static UINT8                AcpiGbl_ExecutionMode = AE_MODE_COMMAND_LOOP;
94 static char                 BatchBuffer[AE_BUFFER_SIZE];    /* Batch command buffer */
95 static AE_TABLE_DESC        *AeTableListHead = NULL;
96
97 #define ACPIEXEC_NAME               "AML Execution/Debug Utility"
98 #define AE_SUPPORTED_OPTIONS        "?b:d:e:f:ghm^orv^:x:"
99
100
101 /******************************************************************************
102  *
103  * FUNCTION:    usage
104  *
105  * PARAMETERS:  None
106  *
107  * RETURN:      None
108  *
109  * DESCRIPTION: Print a usage message
110  *
111  *****************************************************************************/
112
113 static void
114 usage (
115     void)
116 {
117
118     ACPI_USAGE_HEADER ("acpiexec [options] AMLfile1 AMLfile2 ...");
119
120     ACPI_OPTION ("-b \"CommandLine\"",  "Batch mode command line execution (cmd1;cmd2;...)");
121     ACPI_OPTION ("-h -?",               "Display this help message");
122     ACPI_OPTION ("-m [Method]",         "Batch mode method execution. Default=MAIN");
123     printf ("\n");
124
125     ACPI_OPTION ("-da",                 "Disable method abort on error");
126     ACPI_OPTION ("-di",                 "Disable execution of STA/INI methods during init");
127     ACPI_OPTION ("-do",                 "Disable Operation Region address simulation");
128     ACPI_OPTION ("-dr",                 "Disable repair of method return values");
129     ACPI_OPTION ("-ds",                 "Disable method auto-serialization");
130     ACPI_OPTION ("-dt",                 "Disable allocation tracking (performance)");
131     printf ("\n");
132
133     ACPI_OPTION ("-ef",                 "Enable display of final memory statistics");
134     ACPI_OPTION ("-ei",                 "Enable additional tests for ACPICA interfaces");
135     ACPI_OPTION ("-el",                 "Enable loading of additional test tables");
136     ACPI_OPTION ("-es",                 "Enable Interpreter Slack Mode");
137     ACPI_OPTION ("-et",                 "Enable debug semaphore timeout");
138     printf ("\n");
139
140     ACPI_OPTION ("-f <Value>",          "Operation Region initialization fill value");
141     ACPI_OPTION ("-r",                  "Use hardware-reduced FADT V5");
142     ACPI_OPTION ("-v",                  "Display version information");
143     ACPI_OPTION ("-vi",                 "Verbose initialization output");
144     ACPI_OPTION ("-vr",                 "Verbose region handler output");
145     ACPI_OPTION ("-x <DebugLevel>",     "Debug output level");
146 }
147
148
149 /******************************************************************************
150  *
151  * FUNCTION:    AeDoOptions
152  *
153  * PARAMETERS:  argc/argv           - Standard argc/argv
154  *
155  * RETURN:      Status
156  *
157  * DESCRIPTION: Command line option processing
158  *
159  *****************************************************************************/
160
161 static int
162 AeDoOptions (
163     int                     argc,
164     char                    **argv)
165 {
166     int                     j;
167
168
169     while ((j = AcpiGetopt (argc, argv, AE_SUPPORTED_OPTIONS)) != EOF) switch (j)
170     {
171     case 'b':
172
173         if (strlen (AcpiGbl_Optarg) > (AE_BUFFER_SIZE -1))
174         {
175             printf ("**** The length of command line (%u) exceeded maximum (%u)\n",
176                 (UINT32) strlen (AcpiGbl_Optarg), (AE_BUFFER_SIZE -1));
177             return (-1);
178         }
179         AcpiGbl_ExecutionMode = AE_MODE_BATCH_MULTIPLE;
180         strcpy (BatchBuffer, AcpiGbl_Optarg);
181         break;
182
183     case 'd':
184
185         switch (AcpiGbl_Optarg[0])
186         {
187         case 'a':
188
189             AcpiGbl_IgnoreErrors = TRUE;
190             break;
191
192         case 'i':
193
194             AcpiGbl_DbOpt_ini_methods = FALSE;
195             break;
196
197         case 'o':
198
199             AcpiGbl_DbOpt_NoRegionSupport = TRUE;
200             break;
201
202         case 'r':
203
204             AcpiGbl_DisableAutoRepair = TRUE;
205             break;
206
207         case 's':
208
209             AcpiGbl_AutoSerializeMethods = FALSE;
210             break;
211
212         case 't':
213
214             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
215                 AcpiGbl_DisableMemTracking = TRUE;
216             #endif
217             break;
218
219         default:
220
221             printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
222             return (-1);
223         }
224         break;
225
226     case 'e':
227
228         switch (AcpiGbl_Optarg[0])
229         {
230         case 'f':
231
232             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
233                 AcpiGbl_DisplayFinalMemStats = TRUE;
234             #endif
235             break;
236
237         case 'i':
238
239             AcpiGbl_DoInterfaceTests = TRUE;
240             break;
241
242         case 'l':
243
244             AcpiGbl_LoadTestTables = TRUE;
245             break;
246
247         case 's':
248
249             AcpiGbl_EnableInterpreterSlack = TRUE;
250             printf ("Enabling AML Interpreter slack mode\n");
251             break;
252
253         case 't':
254
255             AcpiGbl_DebugTimeout = TRUE;
256             break;
257
258         default:
259
260             printf ("Unknown option: -e%s\n", AcpiGbl_Optarg);
261             return (-1);
262         }
263         break;
264
265     case 'f':
266
267         AcpiGbl_RegionFillValue = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
268         break;
269
270     case 'g':
271
272         AcpiGbl_DbOpt_tables = TRUE;
273         AcpiGbl_DbFilename = NULL;
274         break;
275
276     case 'h':
277     case '?':
278
279         usage();
280         return (0);
281
282     case 'm':
283
284         AcpiGbl_ExecutionMode = AE_MODE_BATCH_SINGLE;
285         switch (AcpiGbl_Optarg[0])
286         {
287         case '^':
288
289             strcpy (BatchBuffer, "MAIN");
290             break;
291
292         default:
293
294             strcpy (BatchBuffer, AcpiGbl_Optarg);
295             break;
296         }
297         break;
298
299     case 'o':
300
301         AcpiGbl_DbOpt_disasm = TRUE;
302         AcpiGbl_DbOpt_stats = TRUE;
303         break;
304
305     case 'r':
306
307         AcpiGbl_UseHwReducedFadt = TRUE;
308         printf ("Using ACPI 5.0 Hardware Reduced Mode via version 5 FADT\n");
309         break;
310
311     case 'v':
312
313         switch (AcpiGbl_Optarg[0])
314         {
315         case '^':  /* -v: (Version): signon already emitted, just exit */
316
317             exit (0);
318
319         case 'i':
320
321             AcpiDbgLevel |= ACPI_LV_INIT_NAMES;
322             break;
323
324         case 'r':
325
326             AcpiGbl_DisplayRegionAccess = TRUE;
327             break;
328
329         default:
330
331             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
332             return (-1);
333         }
334         break;
335
336     case 'x':
337
338         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 0);
339         AcpiGbl_DbConsoleDebugLevel = AcpiDbgLevel;
340         printf ("Debug Level: 0x%8.8X\n", AcpiDbgLevel);
341         break;
342
343     default:
344
345         usage();
346         return (-1);
347     }
348
349     return (0);
350 }
351
352
353 /******************************************************************************
354  *
355  * FUNCTION:    main
356  *
357  * PARAMETERS:  argc, argv
358  *
359  * RETURN:      Status
360  *
361  * DESCRIPTION: Main routine for AcpiExec utility
362  *
363  *****************************************************************************/
364
365 int ACPI_SYSTEM_XFACE
366 main (
367     int                     argc,
368     char                    **argv)
369 {
370     ACPI_STATUS             Status;
371     UINT32                  InitFlags;
372     ACPI_TABLE_HEADER       *Table = NULL;
373     UINT32                  TableCount;
374     AE_TABLE_DESC           *TableDesc;
375
376
377     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
378
379     printf (ACPI_COMMON_SIGNON (ACPIEXEC_NAME));
380     if (argc < 2)
381     {
382         usage ();
383         return (0);
384     }
385
386     signal (SIGINT, AeCtrlCHandler);
387
388     /* Init globals */
389
390     AcpiDbgLevel = ACPI_NORMAL_DEFAULT;
391     AcpiDbgLayer = 0xFFFFFFFF;
392
393     /* Init ACPI and start debugger thread */
394
395     Status = AcpiInitializeSubsystem ();
396     AE_CHECK_OK (AcpiInitializeSubsystem, Status);
397     if (ACPI_FAILURE (Status))
398     {
399         goto ErrorExit;
400     }
401
402     /* Get the command line options */
403
404     if (AeDoOptions (argc, argv))
405     {
406         goto ErrorExit;
407     }
408
409     /* The remaining arguments are filenames for ACPI tables */
410
411     if (!argv[AcpiGbl_Optind])
412     {
413         goto EnterDebugger;
414     }
415
416     AcpiGbl_DbOpt_tables = TRUE;
417     TableCount = 0;
418
419     /* Get each of the ACPI table files on the command line */
420
421     while (argv[AcpiGbl_Optind])
422     {
423         /* Get one entire table */
424
425         Status = AcpiDbReadTableFromFile (argv[AcpiGbl_Optind], &Table);
426         if (ACPI_FAILURE (Status))
427         {
428             printf ("**** Could not get table from file %s, %s\n",
429                 argv[AcpiGbl_Optind], AcpiFormatException (Status));
430             goto ErrorExit;
431         }
432
433         /* Ignore non-AML tables, we can't use them. Except for an FADT */
434
435         if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_FADT) &&
436             !AcpiUtIsAmlTable (Table))
437         {
438             ACPI_INFO ((AE_INFO,
439                 "Table [%4.4s] is not an AML table, ignoring",
440                 Table->Signature));
441             AcpiOsFree (Table);
442         }
443         else
444         {
445             /* Allocate and link a table descriptor */
446
447             TableDesc = AcpiOsAllocate (sizeof (AE_TABLE_DESC));
448             TableDesc->Table = Table;
449             TableDesc->Next = AeTableListHead;
450             AeTableListHead = TableDesc;
451
452             TableCount++;
453         }
454
455         AcpiGbl_Optind++;
456     }
457
458     printf ("\n");
459
460     /* Build a local RSDT with all tables and let ACPICA process the RSDT */
461
462     Status = AeBuildLocalTables (TableCount, AeTableListHead);
463     if (ACPI_FAILURE (Status))
464     {
465         goto ErrorExit;
466     }
467
468     Status = AeInstallTables ();
469     if (ACPI_FAILURE (Status))
470     {
471         printf ("**** Could not load ACPI tables, %s\n",
472             AcpiFormatException (Status));
473         goto EnterDebugger;
474     }
475
476     /*
477      * Install most of the handlers.
478      * Override some default region handlers, especially SystemMemory
479      */
480     Status = AeInstallEarlyHandlers ();
481     if (ACPI_FAILURE (Status))
482     {
483         goto EnterDebugger;
484     }
485
486     /* Setup initialization flags for ACPICA */
487
488     InitFlags = (ACPI_NO_HANDLER_INIT | ACPI_NO_ACPI_ENABLE);
489     if (!AcpiGbl_DbOpt_ini_methods)
490     {
491         InitFlags |= (ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
492     }
493
494     /*
495      * Main initialization for ACPICA subsystem
496      * TBD: Need a way to call this after the ACPI table "LOAD" command
497      */
498     Status = AcpiEnableSubsystem (InitFlags);
499     if (ACPI_FAILURE (Status))
500     {
501         printf ("**** Could not EnableSubsystem, %s\n",
502             AcpiFormatException (Status));
503         goto EnterDebugger;
504     }
505
506     /*
507      * Install handlers for "device driver" space IDs (EC,SMBus, etc.)
508      * and fixed event handlers
509      */
510     AeInstallLateHandlers ();
511
512     /* Finish the ACPICA initialization */
513
514     Status = AcpiInitializeObjects (InitFlags);
515     if (ACPI_FAILURE (Status))
516     {
517         printf ("**** Could not InitializeObjects, %s\n",
518             AcpiFormatException (Status));
519         goto EnterDebugger;
520     }
521
522     AeMiscellaneousTests ();
523
524
525 EnterDebugger:
526
527     /* Exit if error above and we are in one of the batch modes */
528
529     if (ACPI_FAILURE (Status) && (AcpiGbl_ExecutionMode > 0))
530     {
531         goto ErrorExit;
532     }
533
534     /* Run a batch command or enter the command loop */
535
536     switch (AcpiGbl_ExecutionMode)
537     {
538     default:
539     case AE_MODE_COMMAND_LOOP:
540
541         AcpiDbUserCommands (ACPI_DEBUGGER_COMMAND_PROMPT, NULL);
542         break;
543
544     case AE_MODE_BATCH_MULTIPLE:
545
546         AcpiDbRunBatchMode ();
547         break;
548
549     case AE_MODE_BATCH_SINGLE:
550
551         AcpiDbExecute (BatchBuffer, NULL, NULL, EX_NO_SINGLE_STEP);
552         Status = AcpiTerminate ();
553         break;
554     }
555
556     return (0);
557
558
559 ErrorExit:
560
561     (void) AcpiOsTerminate ();
562     return (-1);
563 }
564
565
566 /******************************************************************************
567  *
568  * FUNCTION:    AcpiDbRunBatchMode
569  *
570  * PARAMETERS:  BatchCommandLine    - A semicolon separated list of commands
571  *                                    to be executed.
572  *                                    Use only commas to separate elements of
573  *                                    particular command.
574  * RETURN:      Status
575  *
576  * DESCRIPTION: For each command of list separated by ';' prepare the command
577  *              buffer and pass it to AcpiDbCommandDispatch.
578  *
579  *****************************************************************************/
580
581 static ACPI_STATUS
582 AcpiDbRunBatchMode (
583     void)
584 {
585     ACPI_STATUS             Status;
586     char                    *Ptr = BatchBuffer;
587     char                    *Cmd = Ptr;
588     UINT8                   Run = 0;
589
590
591     AcpiGbl_MethodExecuting = FALSE;
592     AcpiGbl_StepToNextCall = FALSE;
593
594     while (*Ptr)
595     {
596         if (*Ptr == ',')
597         {
598             /* Convert commas to spaces */
599             *Ptr = ' ';
600         }
601         else if (*Ptr == ';')
602         {
603             *Ptr = '\0';
604             Run = 1;
605         }
606
607         Ptr++;
608
609         if (Run || (*Ptr == '\0'))
610         {
611             (void) AcpiDbCommandDispatch (Cmd, NULL, NULL);
612             Run = 0;
613             Cmd = Ptr;
614         }
615     }
616
617     Status = AcpiTerminate ();
618     return (Status);
619 }