1 /******************************************************************************
2  *
3  * Module Name: aemain - Main routine for the AcpiExec utility
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, 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 #include "errno.h"
46 
47 #define _COMPONENT          ACPI_TOOLS
48         ACPI_MODULE_NAME    ("aemain")
49 
50 
51 /*
52  * Main routine for the ACPI user-space execution utility.
53  *
54  * Portability note: The utility depends upon the host for command-line
55  * wildcard support - it is not implemented locally. For example:
56  *
57  * Linux/Unix systems: Shell expands wildcards automatically.
58  *
59  * Windows: The setargv.obj module must be linked in to automatically
60  * expand wildcards.
61  */
62 extern BOOLEAN              AcpiGbl_DebugTimeout;
63 
64 /* Local prototypes */
65 
66 static int
67 AeDoOptions (
68     int                     argc,
69     char                    **argv);
70 
71 static ACPI_STATUS
72 AcpiDbRunBatchMode (
73     void);
74 
75 
76 #define AE_BUFFER_SIZE              1024
77 #define ASL_MAX_FILES               256
78 
79 /* Execution modes */
80 
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 */
84 
85 
86 /* Globals */
87 
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 BOOLEAN                     AcpiGbl_LoadTestTables = FALSE;
94 BOOLEAN                     AcpiGbl_AeLoadOnly = FALSE;
95 static UINT8                AcpiGbl_ExecutionMode = AE_MODE_COMMAND_LOOP;
96 static char                 BatchBuffer[AE_BUFFER_SIZE];    /* Batch command buffer */
97 
98 #define ACPIEXEC_NAME               "AML Execution/Debug Utility"
99 #define AE_SUPPORTED_OPTIONS        "?b:d:e:f^ghi:lm^rv^:x:"
100 
101 
102 /* Stubs for the disassembler */
103 
104 void
105 MpSaveGpioInfo (
106     ACPI_PARSE_OBJECT       *Op,
107     AML_RESOURCE            *Resource,
108     UINT32                  PinCount,
109     UINT16                  *PinList,
110     char                    *DeviceName)
111 {
112 }
113 
114 void
115 MpSaveSerialInfo (
116     ACPI_PARSE_OBJECT       *Op,
117     AML_RESOURCE            *Resource,
118     char                    *DeviceName)
119 {
120 }
121 
122 
123 /******************************************************************************
124  *
125  * FUNCTION:    usage
126  *
127  * PARAMETERS:  None
128  *
129  * RETURN:      None
130  *
131  * DESCRIPTION: Print a usage message
132  *
133  *****************************************************************************/
134 
135 static void
136 usage (
137     void)
138 {
139 
140     ACPI_USAGE_HEADER ("acpiexec [options] AMLfile1 AMLfile2 ...");
141 
142     ACPI_OPTION ("-b \"CommandLine\"",  "Batch mode command line execution (cmd1;cmd2;...)");
143     ACPI_OPTION ("-h -?",               "Display this help message");
144     ACPI_OPTION ("-m [Method]",         "Batch mode method execution. Default=MAIN");
145     printf ("\n");
146 
147     ACPI_OPTION ("-da",                 "Disable method abort on error");
148     ACPI_OPTION ("-di",                 "Disable execution of STA/INI methods during init");
149     ACPI_OPTION ("-do",                 "Disable Operation Region address simulation");
150     ACPI_OPTION ("-dr",                 "Disable repair of method return values");
151     ACPI_OPTION ("-ds",                 "Disable method auto-serialization");
152     ACPI_OPTION ("-dt",                 "Disable allocation tracking (performance)");
153     printf ("\n");
154 
155     ACPI_OPTION ("-ef",                 "Enable display of final memory statistics");
156     ACPI_OPTION ("-ei",                 "Enable additional tests for ACPICA interfaces");
157     ACPI_OPTION ("-el",                 "Enable loading of additional test tables");
158     ACPI_OPTION ("-es",                 "Enable Interpreter Slack Mode");
159     ACPI_OPTION ("-et",                 "Enable debug semaphore timeout");
160     printf ("\n");
161 
162     ACPI_OPTION ("-fi <File>",          "Specify namespace initialization file");
163     ACPI_OPTION ("-fv <Value>",         "Operation Region initialization fill value");
164     printf ("\n");
165 
166     ACPI_OPTION ("-i <Count>",          "Maximum iterations for AML while loops");
167     ACPI_OPTION ("-l",                  "Load tables and namespace only");
168     ACPI_OPTION ("-r",                  "Use hardware-reduced FADT V5");
169     ACPI_OPTION ("-v",                  "Display version information");
170     ACPI_OPTION ("-vi",                 "Verbose initialization output");
171     ACPI_OPTION ("-vr",                 "Verbose region handler output");
172     ACPI_OPTION ("-x <DebugLevel>",     "Debug output level");
173 
174     printf ("\n  From within the interactive mode, use '?' or \"help\" to see\n"
175         "  a list of available AML Debugger commands\n");
176 }
177 
178 
179 /******************************************************************************
180  *
181  * FUNCTION:    AeDoOptions
182  *
183  * PARAMETERS:  argc/argv           - Standard argc/argv
184  *
185  * RETURN:      Status
186  *
187  * DESCRIPTION: Command line option processing
188  *
189  *****************************************************************************/
190 
191 static int
192 AeDoOptions (
193     int                     argc,
194     char                    **argv)
195 {
196     int                     j;
197     UINT32                  Temp;
198 
199 
200     while ((j = AcpiGetopt (argc, argv, AE_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch (j)
201     {
202     case 'b':
203 
204         if (strlen (AcpiGbl_Optarg) > (AE_BUFFER_SIZE -1))
205         {
206             printf ("**** The length of command line (%u) exceeded maximum (%u)\n",
207                 (UINT32) strlen (AcpiGbl_Optarg), (AE_BUFFER_SIZE -1));
208             return (-1);
209         }
210         AcpiGbl_ExecutionMode = AE_MODE_BATCH_MULTIPLE;
211         strcpy (BatchBuffer, AcpiGbl_Optarg);
212         break;
213 
214     case 'd':
215 
216         switch (AcpiGbl_Optarg[0])
217         {
218         case 'a':
219 
220             AcpiGbl_IgnoreErrors = TRUE;
221             break;
222 
223         case 'i':
224 
225             AcpiGbl_DbOpt_NoIniMethods = TRUE;
226             break;
227 
228         case 'o':
229 
230             AcpiGbl_DbOpt_NoRegionSupport = TRUE;
231             break;
232 
233         case 'r':
234 
235             AcpiGbl_DisableAutoRepair = TRUE;
236             break;
237 
238         case 's':
239 
240             AcpiGbl_AutoSerializeMethods = FALSE;
241             break;
242 
243         case 't':
244 
245             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
246                 AcpiGbl_DisableMemTracking = TRUE;
247             #endif
248             break;
249 
250         default:
251 
252             printf ("Unknown option: -d%s\n", AcpiGbl_Optarg);
253             return (-1);
254         }
255         break;
256 
257     case 'e':
258 
259         switch (AcpiGbl_Optarg[0])
260         {
261         case 'f':
262 
263             #ifdef ACPI_DBG_TRACK_ALLOCATIONS
264                 AcpiGbl_DisplayFinalMemStats = TRUE;
265             #endif
266             break;
267 
268         case 'i':
269 
270             AcpiGbl_DoInterfaceTests = TRUE;
271             break;
272 
273         case 'l':
274 
275             AcpiGbl_LoadTestTables = TRUE;
276             break;
277 
278         case 's':
279 
280             AcpiGbl_EnableInterpreterSlack = TRUE;
281             printf ("Enabling AML Interpreter slack mode\n");
282             break;
283 
284         case 't':
285 
286             AcpiGbl_DebugTimeout = TRUE;
287             break;
288 
289         default:
290 
291             printf ("Unknown option: -e%s\n", AcpiGbl_Optarg);
292             return (-1);
293         }
294         break;
295 
296     case 'f':
297 
298         switch (AcpiGbl_Optarg[0])
299         {
300         case 'v':   /* -fv: region fill value */
301 
302             if (AcpiGetoptArgument (argc, argv))
303             {
304                 return (-1);
305             }
306 
307             AcpiGbl_RegionFillValue = (UINT8) strtoul (AcpiGbl_Optarg, NULL, 0);
308             break;
309 
310         case 'i':   /* -fi: specify initialization file */
311 
312             if (AcpiGetoptArgument (argc, argv))
313             {
314                 return (-1);
315             }
316 
317             if (AeOpenInitializationFile (AcpiGbl_Optarg))
318             {
319                 return (-1);
320             }
321             break;
322 
323         default:
324 
325             printf ("Unknown option: -f%s\n", AcpiGbl_Optarg);
326             return (-1);
327         }
328         break;
329 
330     case 'g':
331 
332         AcpiGbl_DbFilename = NULL;
333         break;
334 
335     case 'h':
336     case '?':
337 
338         usage();
339         return (1);
340 
341     case 'i':
342 
343         Temp = strtoul (AcpiGbl_Optarg, NULL, 0);
344         if (!Temp || (Temp > ACPI_UINT16_MAX))
345         {
346             printf ("%s: Invalid max loops value\n", AcpiGbl_Optarg);
347             return (-1);
348         }
349 
350         AcpiGbl_MaxLoopIterations = (UINT16) Temp;
351         printf ("Max Loop Iterations is %u (0x%X)\n",
352             AcpiGbl_MaxLoopIterations, AcpiGbl_MaxLoopIterations);
353         break;
354 
355     case 'l':
356 
357         AcpiGbl_AeLoadOnly = TRUE;
358         break;
359 
360     case 'm':
361 
362         AcpiGbl_ExecutionMode = AE_MODE_BATCH_SINGLE;
363         switch (AcpiGbl_Optarg[0])
364         {
365         case '^':
366 
367             strcpy (BatchBuffer, "MAIN");
368             break;
369 
370         default:
371 
372             strcpy (BatchBuffer, AcpiGbl_Optarg);
373             break;
374         }
375         break;
376 
377     case 'r':
378 
379         AcpiGbl_UseHwReducedFadt = TRUE;
380         printf ("Using ACPI 5.0 Hardware Reduced Mode via version 5 FADT\n");
381         break;
382 
383     case 'v':
384 
385         switch (AcpiGbl_Optarg[0])
386         {
387         case '^':  /* -v: (Version): signon already emitted, just exit */
388 
389             (void) AcpiOsTerminate ();
390             return (1);
391 
392         case 'i':
393 
394             AcpiDbgLevel |= ACPI_LV_INIT_NAMES;
395             break;
396 
397         case 'r':
398 
399             AcpiGbl_DisplayRegionAccess = TRUE;
400             break;
401 
402         default:
403 
404             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
405             return (-1);
406         }
407         break;
408 
409     case 'x':
410 
411         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 0);
412         AcpiGbl_DbConsoleDebugLevel = AcpiDbgLevel;
413         printf ("Debug Level: 0x%8.8X\n", AcpiDbgLevel);
414         break;
415 
416     default:
417 
418         usage();
419         return (-1);
420     }
421 
422     return (0);
423 }
424 
425 
426 /******************************************************************************
427  *
428  * FUNCTION:    main
429  *
430  * PARAMETERS:  argc, argv
431  *
432  * RETURN:      Status
433  *
434  * DESCRIPTION: Main routine for AcpiExec utility
435  *
436  *****************************************************************************/
437 
438 int ACPI_SYSTEM_XFACE
439 main (
440     int                     argc,
441     char                    **argv)
442 {
443     ACPI_NEW_TABLE_DESC     *ListHead = NULL;
444     ACPI_STATUS             Status;
445     UINT32                  InitFlags;
446     int                     ExitCode = 0;
447 
448 
449     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
450     signal (SIGINT, AeCtrlCHandler);
451 
452     /* Init debug globals */
453 
454     AcpiDbgLevel = ACPI_NORMAL_DEFAULT;
455     AcpiDbgLayer = 0xFFFFFFFF;
456 
457     /* Init ACPICA and start debugger thread */
458 
459     Status = AcpiInitializeSubsystem ();
460     ACPI_CHECK_OK (AcpiInitializeSubsystem, Status);
461     if (ACPI_FAILURE (Status))
462     {
463         goto ErrorExit;
464     }
465 
466     /* ACPICA runtime configuration */
467 
468     AcpiGbl_MaxLoopIterations = 400;
469 
470 
471     /* Initialize the AML debugger */
472 
473     Status = AcpiInitializeDebugger ();
474     ACPI_CHECK_OK (AcpiInitializeDebugger, Status);
475     if (ACPI_FAILURE (Status))
476     {
477         goto ErrorExit;
478     }
479 
480     printf (ACPI_COMMON_SIGNON (ACPIEXEC_NAME));
481     if (argc < 2)
482     {
483         usage ();
484         (void) AcpiOsTerminate ();
485         return (0);
486     }
487 
488     /* Get the command line options */
489 
490     ExitCode = AeDoOptions (argc, argv);
491     if (ExitCode)
492     {
493         if (ExitCode > 0)
494         {
495             ExitCode = 0;
496         }
497 
498         goto ErrorExit;
499     }
500 
501     /* The remaining arguments are filenames for ACPI tables */
502 
503     if (!argv[AcpiGbl_Optind])
504     {
505         goto EnterDebugger;
506     }
507 
508     AcpiGbl_CstyleDisassembly = FALSE; /* Not supported for AcpiExec */
509 
510     /* Get each of the ACPI table files on the command line */
511 
512     while (argv[AcpiGbl_Optind])
513     {
514         /* Get all ACPI AML tables in this file */
515 
516         Status = AcpiAcGetAllTablesFromFile (argv[AcpiGbl_Optind],
517             ACPI_GET_ONLY_AML_TABLES, &ListHead);
518         if (ACPI_FAILURE (Status))
519         {
520             ExitCode = -1;
521             goto ErrorExit;
522         }
523 
524         AcpiGbl_Optind++;
525     }
526 
527     printf ("\n");
528 
529     /* Build a local RSDT with all tables and let ACPICA process the RSDT */
530 
531     Status = AeBuildLocalTables (ListHead);
532     if (ACPI_FAILURE (Status))
533     {
534         goto ErrorExit;
535     }
536 
537     Status = AeInstallTables ();
538 
539     /*
540      * Exit namespace initialization for the "load namespace only" option.
541      * No control methods will be executed. However, still enter the
542      * the debugger.
543      */
544     if (AcpiGbl_AeLoadOnly)
545     {
546         goto EnterDebugger;
547     }
548 
549     if (ACPI_FAILURE (Status))
550     {
551         printf ("**** Could not load ACPI tables, %s\n",
552             AcpiFormatException (Status));
553         goto EnterDebugger;
554     }
555 
556     /*
557      * Install most of the handlers.
558      * Override some default region handlers, especially SystemMemory
559      */
560     Status = AeInstallEarlyHandlers ();
561     if (ACPI_FAILURE (Status))
562     {
563         goto EnterDebugger;
564     }
565 
566     /* Setup initialization flags for ACPICA */
567 
568     InitFlags = (ACPI_NO_HANDLER_INIT | ACPI_NO_ACPI_ENABLE);
569     if (AcpiGbl_DbOpt_NoIniMethods)
570     {
571         InitFlags |= (ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
572     }
573 
574     /*
575      * Main initialization for ACPICA subsystem
576      * TBD: Need a way to call this after the ACPI table "LOAD" command
577      */
578     Status = AcpiEnableSubsystem (InitFlags);
579     if (ACPI_FAILURE (Status))
580     {
581         printf ("**** Could not EnableSubsystem, %s\n",
582             AcpiFormatException (Status));
583         goto EnterDebugger;
584     }
585 
586     /*
587      * Install handlers for "device driver" space IDs (EC,SMBus, etc.)
588      * and fixed event handlers
589      */
590     AeInstallLateHandlers ();
591 
592     /* Finish the ACPICA initialization */
593 
594     Status = AcpiInitializeObjects (InitFlags);
595     if (ACPI_FAILURE (Status))
596     {
597         printf ("**** Could not InitializeObjects, %s\n",
598             AcpiFormatException (Status));
599         goto EnterDebugger;
600     }
601 
602     AeMiscellaneousTests ();
603 
604 
605 EnterDebugger:
606 
607     /* Exit if error above and we are in one of the batch modes */
608 
609     if (ACPI_FAILURE (Status) && (AcpiGbl_ExecutionMode > 0))
610     {
611         goto ErrorExit;
612     }
613 
614     /* Run a batch command or enter the command loop */
615 
616     switch (AcpiGbl_ExecutionMode)
617     {
618     default:
619     case AE_MODE_COMMAND_LOOP:
620 
621         AcpiDbUserCommands (ACPI_DEBUGGER_COMMAND_PROMPT, NULL);
622         break;
623 
624     case AE_MODE_BATCH_MULTIPLE:
625 
626         AcpiDbRunBatchMode ();
627         break;
628 
629     case AE_MODE_BATCH_SINGLE:
630 
631         AcpiDbExecute (BatchBuffer, NULL, NULL, EX_NO_SINGLE_STEP);
632 
633         /* Shut down the debugger */
634 
635         AcpiTerminateDebugger ();
636         Status = AcpiTerminate ();
637         break;
638     }
639 
640     return (0);
641 
642 
643 ErrorExit:
644     (void) AcpiOsTerminate ();
645     return (ExitCode);
646 }
647 
648 
649 /******************************************************************************
650  *
651  * FUNCTION:    AcpiDbRunBatchMode
652  *
653  * PARAMETERS:  BatchCommandLine    - A semicolon separated list of commands
654  *                                    to be executed.
655  *                                    Use only commas to separate elements of
656  *                                    particular command.
657  * RETURN:      Status
658  *
659  * DESCRIPTION: For each command of list separated by ';' prepare the command
660  *              buffer and pass it to AcpiDbCommandDispatch.
661  *
662  *****************************************************************************/
663 
664 static ACPI_STATUS
665 AcpiDbRunBatchMode (
666     void)
667 {
668     ACPI_STATUS             Status;
669     char                    *Ptr = BatchBuffer;
670     char                    *Cmd = Ptr;
671     UINT8                   Run = 0;
672 
673 
674     AcpiGbl_MethodExecuting = FALSE;
675     AcpiGbl_StepToNextCall = FALSE;
676 
677     while (*Ptr)
678     {
679         if (*Ptr == ',')
680         {
681             /* Convert commas to spaces */
682             *Ptr = ' ';
683         }
684         else if (*Ptr == ';')
685         {
686             *Ptr = '\0';
687             Run = 1;
688         }
689 
690         Ptr++;
691 
692         if (Run || (*Ptr == '\0'))
693         {
694             (void) AcpiDbCommandDispatch (Cmd, NULL, NULL);
695             Run = 0;
696             Cmd = Ptr;
697         }
698     }
699 
700     /* Shut down the debugger */
701 
702     AcpiTerminateDebugger ();
703     Status = AcpiTerminate ();
704     return (Status);
705 }
706