Sync ACPICA with Intel's version 20170119.
[dragonfly.git] / sys / contrib / dev / acpica / source / tools / acpinames / anmain.c
1 /******************************************************************************
2  *
3  * Module Name: anmain - Main routine for the AcpiNames utility
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2017, 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 "acpinames.h"
45 #include "actables.h"
46 #include "errno.h"
47
48 #define _COMPONENT          ACPI_TOOLS
49         ACPI_MODULE_NAME    ("anmain")
50
51
52 /* Local prototypes */
53
54 static int
55 AnDumpEntireNamespace (
56     ACPI_NEW_TABLE_DESC     *ListHead);
57
58
59 /*
60  * Main routine for the ACPI user-space namespace utility.
61  *
62  * Portability note: The utility depends upon the host for command-line
63  * wildcard support - it is not implemented locally. For example:
64  *
65  * Linux/Unix systems: Shell expands wildcards automatically.
66  *
67  * Windows: The setargv.obj module must be linked in to automatically
68  * expand wildcards.
69  */
70 BOOLEAN                     AcpiGbl_NsLoadOnly = FALSE;
71
72
73 #define AN_UTILITY_NAME             "ACPI Namespace Dump Utility"
74 #define AN_SUPPORTED_OPTIONS        "?hlvx:"
75
76
77 /******************************************************************************
78  *
79  * FUNCTION:    usage
80  *
81  * PARAMETERS:  None
82  *
83  * RETURN:      None
84  *
85  * DESCRIPTION: Print a usage message
86  *
87  *****************************************************************************/
88
89 static void
90 usage (
91     void)
92 {
93
94     ACPI_USAGE_HEADER ("AcpiNames [options] AMLfile");
95     ACPI_OPTION ("-?",                  "Display this message");
96     ACPI_OPTION ("-l",                  "Load namespace only, no display");
97     ACPI_OPTION ("-v",                  "Display version information");
98     ACPI_OPTION ("-x <DebugLevel>",     "Debug output level");
99 }
100
101
102 /******************************************************************************
103  *
104  * FUNCTION:    main
105  *
106  * PARAMETERS:  argc, argv
107  *
108  * RETURN:      Status (pass/fail)
109  *
110  * DESCRIPTION: Main routine for NsDump utility
111  *
112  *****************************************************************************/
113
114 int ACPI_SYSTEM_XFACE
115 main (
116     int                     argc,
117     char                    **argv)
118 {
119     ACPI_NEW_TABLE_DESC     *ListHead = NULL;
120     ACPI_STATUS             Status;
121     int                     j;
122
123
124     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
125
126     /* Init debug globals and ACPICA */
127
128     AcpiDbgLevel = ACPI_NORMAL_DEFAULT | ACPI_LV_TABLES;
129     AcpiDbgLayer = 0xFFFFFFFF;
130
131     Status = AcpiInitializeSubsystem ();
132     ACPI_CHECK_OK (AcpiInitializeSubsystem, Status);
133     if (ACPI_FAILURE (Status))
134     {
135         return (-1);
136     }
137
138     printf (ACPI_COMMON_SIGNON (AN_UTILITY_NAME));
139     if (argc < 2)
140     {
141         usage ();
142         return (0);
143     }
144
145     /* Get the command line options */
146
147     while ((j = AcpiGetopt (argc, argv, AN_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch(j)
148     {
149     case 'l':
150
151         AcpiGbl_NsLoadOnly = TRUE;
152         break;
153
154     case 'v': /* -v: (Version): signon already emitted, just exit */
155
156         return (0);
157
158     case 'x':
159
160         AcpiDbgLevel = strtoul (AcpiGbl_Optarg, NULL, 0);
161         printf ("Debug Level: 0x%8.8X\n", AcpiDbgLevel);
162         break;
163
164     case '?':
165     case 'h':
166     default:
167
168         usage();
169         return (0);
170     }
171
172     /* Get each of the ACPI table files on the command line */
173
174     while (argv[AcpiGbl_Optind])
175     {
176         /* Get all ACPI AML tables in this file */
177
178         Status = AcGetAllTablesFromFile (argv[AcpiGbl_Optind],
179             ACPI_GET_ALL_TABLES, &ListHead);
180         if (ACPI_FAILURE (Status))
181         {
182             return (-1);
183         }
184
185         AcpiGbl_Optind++;
186     }
187
188     printf ("\n");
189
190     /*
191      * The next argument is the filename for the DSDT or SSDT.
192      * Open the file, build namespace and dump it.
193      */
194     return (AnDumpEntireNamespace (ListHead));
195 }
196
197
198 /******************************************************************************
199  *
200  * FUNCTION:    AnDumpEntireNamespace
201  *
202  * PARAMETERS:  AmlFilename         - Filename for DSDT or SSDT AML table
203  *
204  * RETURN:      Status (pass/fail)
205  *
206  * DESCRIPTION: Build an ACPI namespace for the input AML table, and dump the
207  *              formatted namespace contents.
208  *
209  *****************************************************************************/
210
211 static int
212 AnDumpEntireNamespace (
213     ACPI_NEW_TABLE_DESC     *ListHead)
214 {
215     ACPI_STATUS             Status;
216     ACPI_HANDLE             Handle;
217
218
219     /*
220      * Build a local XSDT with all tables. Normally, here is where the
221      * RSDP search is performed to find the ACPI tables
222      */
223     Status = AnBuildLocalTables (ListHead);
224     if (ACPI_FAILURE (Status))
225     {
226         return (-1);
227     }
228
229     /* Initialize table manager, get XSDT */
230
231     Status = AcpiInitializeTables (NULL, ACPI_MAX_INIT_TABLES, TRUE);
232     if (ACPI_FAILURE (Status))
233     {
234         printf ("**** Could not initialize ACPI table manager, %s\n",
235             AcpiFormatException (Status));
236         return (-1);
237     }
238
239     /* Load the ACPI namespace */
240
241     Status = AcpiTbLoadNamespace ();
242     if (Status == AE_CTRL_TERMINATE)
243     {
244         /* At least one table load failed -- terminate with error */
245
246         return (-1);
247     }
248
249     if (ACPI_FAILURE (Status))
250     {
251         printf ("**** While creating namespace, %s\n",
252             AcpiFormatException (Status));
253         return (-1);
254     }
255
256     if (AcpiGbl_NsLoadOnly)
257     {
258         printf ("**** Namespace successfully loaded\n");
259         return (0);
260     }
261
262     /*
263      * Enable ACPICA. These calls don't do much for this
264      * utility, since we only dump the namespace. There is no
265      * hardware or event manager code underneath.
266      */
267     Status = AcpiEnableSubsystem (
268         ACPI_NO_ACPI_ENABLE |
269         ACPI_NO_ADDRESS_SPACE_INIT |
270         ACPI_NO_EVENT_INIT |
271         ACPI_NO_HANDLER_INIT);
272     if (ACPI_FAILURE (Status))
273     {
274         printf ("**** Could not EnableSubsystem, %s\n",
275             AcpiFormatException (Status));
276         return (-1);
277     }
278
279     Status = AcpiInitializeObjects (
280         ACPI_NO_ADDRESS_SPACE_INIT |
281         ACPI_NO_DEVICE_INIT |
282         ACPI_NO_EVENT_INIT);
283     if (ACPI_FAILURE (Status))
284     {
285         printf ("**** Could not InitializeObjects, %s\n",
286             AcpiFormatException (Status));
287         return (-1);
288     }
289
290     /*
291      * Perform a namespace walk to dump the contents
292      */
293     AcpiOsPrintf ("\nACPI Namespace:\n");
294
295     AcpiNsDumpObjects (ACPI_TYPE_ANY, ACPI_DISPLAY_SUMMARY,
296         ACPI_UINT32_MAX, ACPI_OWNER_ID_MAX, AcpiGbl_RootNode);
297
298
299     /* Example: get a handle to the _GPE scope */
300
301     Status = AcpiGetHandle (NULL, "\\_GPE", &Handle);
302     ACPI_CHECK_OK (AcpiGetHandle, Status);
303
304     return (0);
305 }