Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / tools / acpiexec / aemain.c
blob3562ff9141ae6a784196f382f0ef2942af10794f
1 /******************************************************************************
3 * Module Name: aemain - Main routine for the AcpiExec utility
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include "aecommon.h"
46 #define _COMPONENT ACPI_TOOLS
47 ACPI_MODULE_NAME ("aemain")
51 * Main routine for the ACPI user-space execution utility.
53 * Portability note: The utility depends upon the host for command-line
54 * wildcard support - it is not implemented locally. For example:
56 * Linux/Unix systems: Shell expands wildcards automatically.
58 * Windows: The setargv.obj module must be linked in to automatically
59 * expand wildcards.
62 extern BOOLEAN AcpiGbl_DebugTimeout;
64 /* Local prototypes */
66 static int
67 AeDoOptions (
68 int argc,
69 char **argv);
71 static ACPI_STATUS
72 AcpiDbRunBatchMode (
73 void);
76 #define AE_BUFFER_SIZE 1024
77 #define ASL_MAX_FILES 256
79 /* Execution modes */
81 #define AE_MODE_COMMAND_LOOP 0 /* Normal command execution loop */
82 #define AE_MODE_BATCH_MULTIPLE 1 /* -b option to execute a command line */
83 #define AE_MODE_BATCH_SINGLE 2 /* -m option to execute a single control method */
86 /* Globals */
88 UINT8 AcpiGbl_RegionFillValue = 0;
89 BOOLEAN AcpiGbl_IgnoreErrors = FALSE;
90 BOOLEAN AcpiGbl_DbOpt_NoRegionSupport = FALSE;
91 UINT8 AcpiGbl_UseHwReducedFadt = FALSE;
92 BOOLEAN AcpiGbl_DoInterfaceTests = 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;
97 #define ACPIEXEC_NAME "AML Execution/Debug Utility"
98 #define AE_SUPPORTED_OPTIONS "?b:d:e:f:ghm^orv^:x:"
101 /******************************************************************************
103 * FUNCTION: usage
105 * PARAMETERS: None
107 * RETURN: None
109 * DESCRIPTION: Print a usage message
111 *****************************************************************************/
113 static void
114 usage (
115 void)
118 ACPI_USAGE_HEADER ("acpiexec [options] AMLfile1 AMLfile2 ...");
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");
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 ("-dt", "Disable allocation tracking (performance)");
130 printf ("\n");
132 ACPI_OPTION ("-ef", "Enable display of final memory statistics");
133 ACPI_OPTION ("-ei", "Enable additional tests for ACPICA interfaces");
134 ACPI_OPTION ("-em", "Enable Interpreter Serialized Mode");
135 ACPI_OPTION ("-es", "Enable Interpreter Slack Mode");
136 ACPI_OPTION ("-et", "Enable debug semaphore timeout");
137 printf ("\n");
139 ACPI_OPTION ("-f <Value>", "Operation Region initialization fill value");
140 ACPI_OPTION ("-r", "Use hardware-reduced FADT V5");
141 ACPI_OPTION ("-v", "Display version information");
142 ACPI_OPTION ("-vi", "Verbose initialization output");
143 ACPI_OPTION ("-vr", "Verbose region handler output");
144 ACPI_OPTION ("-x <DebugLevel>", "Debug output level");
148 /******************************************************************************
150 * FUNCTION: AeDoOptions
152 * PARAMETERS: argc/argv - Standard argc/argv
154 * RETURN: Status
156 * DESCRIPTION: Command line option processing
158 *****************************************************************************/
160 static int
161 AeDoOptions (
162 int argc,
163 char **argv)
165 int j;
168 while ((j = AcpiGetopt (argc, argv, AE_SUPPORTED_OPTIONS)) != EOF) switch (j)
170 case 'b':
172 if (strlen (AcpiGbl_Optarg) > (AE_BUFFER_SIZE -1))
174 printf ("**** The length of command line (%u) exceeded maximum (%u)\n",
175 (UINT32) strlen (AcpiGbl_Optarg), (AE_BUFFER_SIZE -1));
176 return (-1);
178 AcpiGbl_ExecutionMode = AE_MODE_BATCH_MULTIPLE;
179 strcpy (BatchBuffer, AcpiGbl_Optarg);
180 break;
182 case 'd':
184 switch (AcpiGbl_Optarg[0])
186 case 'a':
188 AcpiGbl_IgnoreErrors = TRUE;
189 break;
191 case 'i':
193 AcpiGbl_DbOpt_ini_methods = FALSE;
194 break;
196 case 'o':
198 AcpiGbl_DbOpt_NoRegionSupport = TRUE;
199 break;
201 case 'r':
203 AcpiGbl_DisableAutoRepair = TRUE;
204 break;
206 case 't':
208 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
209 AcpiGbl_DisableMemTracking = TRUE;
210 #endif
211 break;
213 default:
215 printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
216 return (-1);
218 break;
220 case 'e':
222 switch (AcpiGbl_Optarg[0])
224 case 'f':
226 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
227 AcpiGbl_DisplayFinalMemStats = TRUE;
228 #endif
229 break;
231 case 'i':
233 AcpiGbl_DoInterfaceTests = TRUE;
234 break;
236 case 'm':
238 AcpiGbl_AllMethodsSerialized = TRUE;
239 printf ("Enabling AML Interpreter serialized mode\n");
240 break;
242 case 's':
244 AcpiGbl_EnableInterpreterSlack = TRUE;
245 printf ("Enabling AML Interpreter slack mode\n");
246 break;
248 case 't':
250 AcpiGbl_DebugTimeout = TRUE;
251 break;
253 default:
255 printf ("Unknown option: -e%s\n", AcpiGbl_Optarg);
256 return (-1);
258 break;
260 case 'f':
262 AcpiGbl_RegionFillValue = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
263 break;
265 case 'g':
267 AcpiGbl_DbOpt_tables = TRUE;
268 AcpiGbl_DbFilename = NULL;
269 break;
271 case 'h':
272 case '?':
274 usage();
275 return (0);
277 case 'm':
279 AcpiGbl_ExecutionMode = AE_MODE_BATCH_SINGLE;
280 switch (AcpiGbl_Optarg[0])
282 case '^':
284 strcpy (BatchBuffer, "MAIN");
285 break;
287 default:
289 strcpy (BatchBuffer, AcpiGbl_Optarg);
290 break;
292 break;
294 case 'o':
296 AcpiGbl_DbOpt_disasm = TRUE;
297 AcpiGbl_DbOpt_stats = TRUE;
298 break;
300 case 'r':
302 AcpiGbl_UseHwReducedFadt = TRUE;
303 printf ("Using ACPI 5.0 Hardware Reduced Mode via version 5 FADT\n");
304 break;
306 case 'v':
308 switch (AcpiGbl_Optarg[0])
310 case '^': /* -v: (Version): signon already emitted, just exit */
312 exit (0);
314 case 'i':
316 AcpiDbgLevel |= ACPI_LV_INIT_NAMES;
317 break;
319 case 'r':
321 AcpiGbl_DisplayRegionAccess = TRUE;
322 break;
324 default:
326 printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
327 return (-1);
329 break;
331 case 'x':
333 AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 0);
334 AcpiGbl_DbConsoleDebugLevel = AcpiDbgLevel;
335 printf ("Debug Level: 0x%8.8X\n", AcpiDbgLevel);
336 break;
338 default:
340 usage();
341 return (-1);
344 return (0);
348 /******************************************************************************
350 * FUNCTION: main
352 * PARAMETERS: argc, argv
354 * RETURN: Status
356 * DESCRIPTION: Main routine for AcpiExec utility
358 *****************************************************************************/
360 int ACPI_SYSTEM_XFACE
361 main (
362 int argc,
363 char **argv)
365 ACPI_STATUS Status;
366 UINT32 InitFlags;
367 ACPI_TABLE_HEADER *Table = NULL;
368 UINT32 TableCount;
369 AE_TABLE_DESC *TableDesc;
372 ACPI_DEBUG_INITIALIZE (); /* For debug version only */
374 printf (ACPI_COMMON_SIGNON (ACPIEXEC_NAME));
375 if (argc < 2)
377 usage ();
378 return (0);
381 signal (SIGINT, AeCtrlCHandler);
383 /* Init globals */
385 AcpiDbgLevel = ACPI_NORMAL_DEFAULT;
386 AcpiDbgLayer = 0xFFFFFFFF;
388 /* Init ACPI and start debugger thread */
390 Status = AcpiInitializeSubsystem ();
391 AE_CHECK_OK (AcpiInitializeSubsystem, Status);
392 if (ACPI_FAILURE (Status))
394 goto ErrorExit;
397 /* Get the command line options */
399 if (AeDoOptions (argc, argv))
401 goto ErrorExit;
404 /* The remaining arguments are filenames for ACPI tables */
406 if (!argv[AcpiGbl_Optind])
408 goto EnterDebugger;
411 AcpiGbl_DbOpt_tables = TRUE;
412 TableCount = 0;
414 /* Get each of the ACPI table files on the command line */
416 while (argv[AcpiGbl_Optind])
418 /* Get one entire table */
420 Status = AcpiDbReadTableFromFile (argv[AcpiGbl_Optind], &Table);
421 if (ACPI_FAILURE (Status))
423 printf ("**** Could not get table from file %s, %s\n",
424 argv[AcpiGbl_Optind], AcpiFormatException (Status));
425 goto ErrorExit;
428 /* Ignore non-AML tables, we can't use them. Except for an FADT */
430 if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_FADT) &&
431 !AcpiUtIsAmlTable (Table))
433 ACPI_INFO ((AE_INFO,
434 "Table [%4.4s] is not an AML table, ignoring",
435 Table->Signature));
436 AcpiOsFree (Table);
438 else
440 /* Allocate and link a table descriptor */
442 TableDesc = AcpiOsAllocate (sizeof (AE_TABLE_DESC));
443 TableDesc->Table = Table;
444 TableDesc->Next = AeTableListHead;
445 AeTableListHead = TableDesc;
447 TableCount++;
450 AcpiGbl_Optind++;
453 /* Build a local RSDT with all tables and let ACPICA process the RSDT */
455 Status = AeBuildLocalTables (TableCount, AeTableListHead);
456 if (ACPI_FAILURE (Status))
458 goto ErrorExit;
461 Status = AeInstallTables ();
462 if (ACPI_FAILURE (Status))
464 printf ("**** Could not load ACPI tables, %s\n",
465 AcpiFormatException (Status));
466 goto EnterDebugger;
470 * Install most of the handlers.
471 * Override some default region handlers, especially SystemMemory
473 Status = AeInstallEarlyHandlers ();
474 if (ACPI_FAILURE (Status))
476 goto EnterDebugger;
479 /* Setup initialization flags for ACPICA */
481 InitFlags = (ACPI_NO_HANDLER_INIT | ACPI_NO_ACPI_ENABLE);
482 if (!AcpiGbl_DbOpt_ini_methods)
484 InitFlags |= (ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
488 * Main initialization for ACPICA subsystem
489 * TBD: Need a way to call this after the ACPI table "LOAD" command
491 Status = AcpiEnableSubsystem (InitFlags);
492 if (ACPI_FAILURE (Status))
494 printf ("**** Could not EnableSubsystem, %s\n",
495 AcpiFormatException (Status));
496 goto EnterDebugger;
500 * Install handlers for "device driver" space IDs (EC,SMBus, etc.)
501 * and fixed event handlers
503 AeInstallLateHandlers ();
505 /* Finish the ACPICA initialization */
507 Status = AcpiInitializeObjects (InitFlags);
508 if (ACPI_FAILURE (Status))
510 printf ("**** Could not InitializeObjects, %s\n",
511 AcpiFormatException (Status));
512 goto EnterDebugger;
515 AeMiscellaneousTests ();
518 EnterDebugger:
520 /* Exit if error above and we are in one of the batch modes */
522 if (ACPI_FAILURE (Status) && (AcpiGbl_ExecutionMode > 0))
524 goto ErrorExit;
527 /* Run a batch command or enter the command loop */
529 switch (AcpiGbl_ExecutionMode)
531 default:
532 case AE_MODE_COMMAND_LOOP:
534 AcpiDbUserCommands (ACPI_DEBUGGER_COMMAND_PROMPT, NULL);
535 break;
537 case AE_MODE_BATCH_MULTIPLE:
539 AcpiDbRunBatchMode ();
540 break;
542 case AE_MODE_BATCH_SINGLE:
544 AcpiDbExecute (BatchBuffer, NULL, NULL, EX_NO_SINGLE_STEP);
545 Status = AcpiTerminate ();
546 break;
549 return (0);
552 ErrorExit:
554 (void) AcpiOsTerminate ();
555 return (-1);
559 /******************************************************************************
561 * FUNCTION: AcpiDbRunBatchMode
563 * PARAMETERS: BatchCommandLine - A semicolon separated list of commands
564 * to be executed.
565 * Use only commas to separate elements of
566 * particular command.
567 * RETURN: Status
569 * DESCRIPTION: For each command of list separated by ';' prepare the command
570 * buffer and pass it to AcpiDbCommandDispatch.
572 *****************************************************************************/
574 static ACPI_STATUS
575 AcpiDbRunBatchMode (
576 void)
578 ACPI_STATUS Status;
579 char *Ptr = BatchBuffer;
580 char *Cmd = Ptr;
581 UINT8 Run = 0;
584 AcpiGbl_MethodExecuting = FALSE;
585 AcpiGbl_StepToNextCall = FALSE;
587 while (*Ptr)
589 if (*Ptr == ',')
591 /* Convert commas to spaces */
592 *Ptr = ' ';
594 else if (*Ptr == ';')
596 *Ptr = '\0';
597 Run = 1;
600 Ptr++;
602 if (Run || (*Ptr == '\0'))
604 (void) AcpiDbCommandDispatch (Cmd, NULL, NULL);
605 Run = 0;
606 Cmd = Ptr;
610 Status = AcpiTerminate ();
611 return (Status);