1/******************************************************************************
2 *
3 * Module Name: anmain - Main routine for the AcpiNames utility
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2011, 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
46#define _COMPONENT          ACPI_TOOLS
47        ACPI_MODULE_NAME    ("anmain")
48
49
50extern ACPI_TABLE_DESC  Tables[];
51
52FILE                    *AcpiGbl_DebugFile;
53static AE_TABLE_DESC    *AeTableListHead = NULL;
54
55
56#define AE_SUPPORTED_OPTIONS    "?h"
57
58
59/******************************************************************************
60 *
61 * FUNCTION:    usage
62 *
63 * PARAMETERS:  None
64 *
65 * RETURN:      None
66 *
67 * DESCRIPTION: Print a usage message
68 *
69 *****************************************************************************/
70
71static void
72usage (
73    void)
74{
75    printf ("Usage: AcpiNames [options] AMLfile\n\n");
76
77    printf ("Where:\n");
78    printf ("   -?                  Display this message\n");
79}
80
81
82/******************************************************************************
83 *
84 * FUNCTION:    NsDumpEntireNamespace
85 *
86 * PARAMETERS:  AmlFilename         - Filename for DSDT or SSDT AML table
87 *
88 * RETURN:      Status (pass/fail)
89 *
90 * DESCRIPTION: Build an ACPI namespace for the input AML table, and dump the
91 *              formatted namespace contents.
92 *
93 *****************************************************************************/
94
95static int
96NsDumpEntireNamespace (
97    char                    *AmlFilename)
98{
99    ACPI_STATUS             Status;
100    ACPI_TABLE_HEADER       *Table = NULL;
101    UINT32                  TableCount = 0;
102    AE_TABLE_DESC           *TableDesc;
103    ACPI_HANDLE             Handle;
104
105
106    /* Open the binary AML file and read the entire table */
107
108    Status = AcpiDbReadTableFromFile (AmlFilename, &Table);
109    if (ACPI_FAILURE (Status))
110    {
111        printf ("**** Could not get input table %s, %s\n", AmlFilename,
112            AcpiFormatException (Status));
113        return (-1);
114    }
115
116    /* Table must be a DSDT. SSDTs are not currently supported */
117
118    if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
119    {
120        printf ("**** Input table signature is [%4.4s], must be [DSDT]\n",
121            Table->Signature);
122        return (-1);
123    }
124
125    /*
126     * Allocate and link a table descriptor (allows for future expansion to
127     * multiple input files)
128     */
129    TableDesc = AcpiOsAllocate (sizeof (AE_TABLE_DESC));
130    TableDesc->Table = Table;
131    TableDesc->Next = AeTableListHead;
132    AeTableListHead = TableDesc;
133
134    TableCount++;
135
136    /*
137     * Build a local XSDT with all tables. Normally, here is where the
138     * RSDP search is performed to find the ACPI tables
139     */
140    Status = AeBuildLocalTables (TableCount, AeTableListHead);
141    if (ACPI_FAILURE (Status))
142    {
143        return (-1);
144    }
145
146    /* Initialize table manager, get XSDT */
147
148    Status = AcpiInitializeTables (Tables, ACPI_MAX_INIT_TABLES, TRUE);
149    if (ACPI_FAILURE (Status))
150    {
151        printf ("**** Could not initialize ACPI table manager, %s\n",
152            AcpiFormatException (Status));
153        return (-1);
154    }
155
156    /* Reallocate root table to dynamic memory */
157
158    Status = AcpiReallocateRootTable ();
159    if (ACPI_FAILURE (Status))
160    {
161        printf ("**** Could not reallocate root table, %s\n",
162            AcpiFormatException (Status));
163        return (-1);
164    }
165
166    /* Load the ACPI namespace */
167
168    Status = AcpiLoadTables ();
169    if (ACPI_FAILURE (Status))
170    {
171        printf ("**** Could not load ACPI tables, %s\n",
172            AcpiFormatException (Status));
173        return (-1);
174    }
175
176    /*
177     * Enable ACPICA. These calls don't do much for this
178     * utility, since we only dump the namespace. There is no
179     * hardware or event manager code underneath.
180     */
181    Status = AcpiEnableSubsystem (
182                ACPI_NO_ACPI_ENABLE |
183                ACPI_NO_ADDRESS_SPACE_INIT |
184                ACPI_NO_EVENT_INIT |
185                ACPI_NO_HANDLER_INIT);
186    if (ACPI_FAILURE (Status))
187    {
188        printf ("**** Could not EnableSubsystem, %s\n",
189            AcpiFormatException (Status));
190        return (-1);
191    }
192
193    Status = AcpiInitializeObjects (
194                ACPI_NO_ADDRESS_SPACE_INIT |
195                ACPI_NO_DEVICE_INIT |
196                ACPI_NO_EVENT_INIT);
197    if (ACPI_FAILURE (Status))
198    {
199        printf ("**** Could not InitializeObjects, %s\n",
200            AcpiFormatException (Status));
201        return (-1);
202    }
203
204    /*
205     * Perform a namespace walk to dump the contents
206     */
207    AcpiOsPrintf ("\nACPI Namespace:\n");
208
209    AcpiNsDumpObjects (ACPI_TYPE_ANY, ACPI_DISPLAY_SUMMARY, ACPI_UINT32_MAX,
210        ACPI_OWNER_ID_MAX, AcpiGbl_RootNode);
211
212
213    /* Example: get a handle to the _GPE scope */
214
215    Status = AcpiGetHandle (NULL, "\\_GPE", &Handle);
216    AE_CHECK_OK (AcpiGetHandle, Status);
217
218    return (0);
219}
220
221
222/******************************************************************************
223 *
224 * FUNCTION:    main
225 *
226 * PARAMETERS:  argc, argv
227 *
228 * RETURN:      Status (pass/fail)
229 *
230 * DESCRIPTION: Main routine for NsDump utility
231 *
232 *****************************************************************************/
233
234int ACPI_SYSTEM_XFACE
235main (
236    int                     argc,
237    char                    **argv)
238{
239    ACPI_STATUS             Status;
240    int                     j;
241
242
243    printf (ACPI_COMMON_SIGNON ("ACPI Namespace Dump Utility"));
244
245    if (argc < 2)
246    {
247        usage ();
248        return (0);
249    }
250
251    /* Init globals and ACPICA */
252
253    AcpiDbgLevel = ACPI_NORMAL_DEFAULT | ACPI_LV_TABLES;
254    AcpiDbgLayer = 0xFFFFFFFF;
255
256    Status = AcpiInitializeSubsystem ();
257    AE_CHECK_OK (AcpiInitializeSubsystem, Status);
258
259    /* Get the command line options */
260
261    while ((j = AcpiGetopt (argc, argv, AE_SUPPORTED_OPTIONS)) != EOF) switch(j)
262    {
263    case '?':
264    case 'h':
265    default:
266        usage();
267        return (0);
268    }
269
270    /*
271     * The next argument is the filename for the DSDT or SSDT.
272     * Open the file, build namespace and dump it.
273     */
274    return (NsDumpEntireNamespace (argv[AcpiGbl_Optind]));
275}
276
277