xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/aslnamesp.c (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
1 /******************************************************************************
2  *
3  * Module Name: aslnamesp - Namespace output file generation
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2023, 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 MERCHANTABILITY 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 "aslcompiler.h"
45 #include "aslcompiler.y.h"
46 #include "acnamesp.h"
47 
48 
49 #define _COMPONENT          ACPI_COMPILER
50         ACPI_MODULE_NAME    ("aslnamesp")
51 
52 /* Local prototypes */
53 
54 static ACPI_STATUS
55 NsDoOneNamespaceObject (
56     ACPI_HANDLE             ObjHandle,
57     UINT32                  Level,
58     void                    *Context,
59     void                    **ReturnValue);
60 
61 static ACPI_STATUS
62 NsDoOnePathname (
63     ACPI_HANDLE             ObjHandle,
64     UINT32                  Level,
65     void                    *Context,
66     void                    **ReturnValue);
67 
68 
69 /*******************************************************************************
70  *
71  * FUNCTION:    NsSetupNamespaceListing
72  *
73  * PARAMETERS:  Handle          - local file handle
74  *
75  * RETURN:      None
76  *
77  * DESCRIPTION: Set the namespace output file to the input handle
78  *
79  ******************************************************************************/
80 
81 void
NsSetupNamespaceListing(void * Handle)82 NsSetupNamespaceListing (
83     void                    *Handle)
84 {
85 
86     AslGbl_NsOutputFlag = TRUE;
87     AslGbl_Files[ASL_FILE_NAMESPACE_OUTPUT].Handle = Handle;
88 }
89 
90 
91 /*******************************************************************************
92  *
93  * FUNCTION:    NsDisplayNamespace
94  *
95  * PARAMETERS:  None
96  *
97  * RETURN:      Status
98  *
99  * DESCRIPTION: Walk the namespace an display information about each node
100  *              in the tree. Information is written to the optional
101  *              namespace output file.
102  *
103  ******************************************************************************/
104 
105 ACPI_STATUS
NsDisplayNamespace(void)106 NsDisplayNamespace (
107     void)
108 {
109     ACPI_STATUS             Status;
110 
111 
112     if (!AslGbl_NsOutputFlag)
113     {
114         return (AE_OK);
115     }
116 
117     AslGbl_NumNamespaceObjects = 0;
118 
119     /* File header */
120 
121     FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "Contents of ACPI Namespace\n\n"
122         "Count  Depth    Name - Type\n\n");
123 
124     /* Walk entire namespace from the root */
125 
126     Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
127         ACPI_UINT32_MAX, FALSE, NsDoOneNamespaceObject, NULL,
128         NULL, NULL);
129     if (ACPI_FAILURE (Status))
130     {
131         return (Status);
132     }
133 
134     /* Print the full pathname for each namespace node in the common namespace */
135 
136     FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
137         "\nNamespace pathnames and where declared:\n"
138         "<NamePath, Object type, Containing file, Line number within file>\n\n");
139 
140     Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
141         ACPI_UINT32_MAX, FALSE, NsDoOnePathname, NULL,
142         NULL, NULL);
143 
144     /*
145      * We just dumped the entire common namespace, we don't want to do it
146      * again for other input files.
147      */
148     AslGbl_NsOutputFlag = FALSE;
149     return (Status);
150 }
151 
152 
153 /*******************************************************************************
154  *
155  * FUNCTION:    NsDoOneNamespaceObject
156  *
157  * PARAMETERS:  ACPI_WALK_CALLBACK
158  *
159  * RETURN:      Status
160  *
161  * DESCRIPTION: Dump a namespace object to the namespace output file.
162  *              Called during the walk of the namespace to dump all objects.
163  *
164  ******************************************************************************/
165 
166 static ACPI_STATUS
NsDoOneNamespaceObject(ACPI_HANDLE ObjHandle,UINT32 Level,void * Context,void ** ReturnValue)167 NsDoOneNamespaceObject (
168     ACPI_HANDLE             ObjHandle,
169     UINT32                  Level,
170     void                    *Context,
171     void                    **ReturnValue)
172 {
173     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
174     ACPI_OPERAND_OBJECT     *ObjDesc;
175     ACPI_PARSE_OBJECT       *Op;
176 
177 
178     AslGbl_NumNamespaceObjects++;
179 
180     FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "%5u  [%u]  %*s %4.4s - %s",
181         AslGbl_NumNamespaceObjects, Level, (Level * 3), " ",
182         &Node->Name.Ascii[0], AcpiUtGetTypeName (Node->Type));
183 
184     Op = Node->Op;
185     ObjDesc = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Node->Object);
186 
187     if (!Op)
188     {
189         FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "\n");
190         return (AE_OK);
191     }
192 
193 
194     if ((ObjDesc) &&
195         (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) == ACPI_DESC_TYPE_OPERAND))
196     {
197         switch (Node->Type)
198         {
199         case ACPI_TYPE_INTEGER:
200 
201             FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
202                 "       [Initial Value   0x%8.8X%8.8X]",
203                 ACPI_FORMAT_UINT64 (ObjDesc->Integer.Value));
204             break;
205 
206         case ACPI_TYPE_STRING:
207 
208             FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
209                 "        [Initial Value   \"%s\"]",
210                 ObjDesc->String.Pointer);
211             break;
212 
213         default:
214 
215             /* Nothing to do for other types */
216 
217             break;
218         }
219 
220     }
221     else
222     {
223         switch (Node->Type)
224         {
225         case ACPI_TYPE_INTEGER:
226 
227             if (Op->Asl.ParseOpcode == PARSEOP_NAME)
228             {
229                 Op = Op->Asl.Child;
230             }
231 
232             if ((Op->Asl.ParseOpcode == PARSEOP_NAMESEG)  ||
233                 (Op->Asl.ParseOpcode == PARSEOP_NAMESTRING))
234             {
235                 Op = Op->Asl.Next;
236             }
237 
238             FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
239                 "       [Initial Value   0x%8.8X%8.8X]",
240                 ACPI_FORMAT_UINT64 (Op->Asl.Value.Integer));
241             break;
242 
243         case ACPI_TYPE_STRING:
244 
245             if (Op->Asl.ParseOpcode == PARSEOP_NAME)
246             {
247                 Op = Op->Asl.Child;
248             }
249 
250             if ((Op->Asl.ParseOpcode == PARSEOP_NAMESEG)  ||
251                 (Op->Asl.ParseOpcode == PARSEOP_NAMESTRING))
252             {
253                 Op = Op->Asl.Next;
254             }
255 
256             FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
257                 "        [Initial Value   \"%s\"]",
258                 Op->Asl.Value.String);
259             break;
260 
261         case ACPI_TYPE_LOCAL_REGION_FIELD:
262 
263             if ((Op->Asl.ParseOpcode == PARSEOP_NAMESEG)  ||
264                 (Op->Asl.ParseOpcode == PARSEOP_NAMESTRING))
265             {
266                 Op = Op->Asl.Child;
267             }
268 
269             FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
270                 "   [Offset 0x%04X   Length 0x%04X bits]",
271                 Op->Asl.Parent->Asl.ExtraValue, (UINT32) Op->Asl.Value.Integer);
272             break;
273 
274         case ACPI_TYPE_BUFFER_FIELD:
275 
276             switch (Op->Asl.ParseOpcode)
277             {
278             case PARSEOP_CREATEBYTEFIELD:
279 
280                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "   [BYTE  ( 8 bit)]");
281                 break;
282 
283             case PARSEOP_CREATEDWORDFIELD:
284 
285                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "   [DWORD (32 bit)]");
286                 break;
287 
288             case PARSEOP_CREATEQWORDFIELD:
289 
290                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "   [QWORD (64 bit)]");
291                 break;
292 
293             case PARSEOP_CREATEWORDFIELD:
294 
295                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "   [WORD  (16 bit)]");
296                 break;
297 
298             case PARSEOP_CREATEBITFIELD:
299 
300                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "   [BIT   ( 1 bit)]");
301                 break;
302 
303             case PARSEOP_CREATEFIELD:
304 
305                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "   [Arbitrary Bit Field]");
306                 break;
307 
308             default:
309 
310                 break;
311 
312             }
313             break;
314 
315         case ACPI_TYPE_PACKAGE:
316 
317             if (Op->Asl.ParseOpcode == PARSEOP_NAME)
318             {
319                 Op = Op->Asl.Child;
320             }
321 
322             if ((Op->Asl.ParseOpcode == PARSEOP_NAMESEG)  ||
323                 (Op->Asl.ParseOpcode == PARSEOP_NAMESTRING))
324             {
325                 Op = Op->Asl.Next;
326             }
327 
328             Op = Op->Asl.Child;
329 
330             if ((Op->Asl.ParseOpcode == PARSEOP_BYTECONST) ||
331                 (Op->Asl.ParseOpcode == PARSEOP_RAW_DATA))
332             {
333                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
334                     "       [Initial Length  0x%.2X elements]",
335                     (UINT32) Op->Asl.Value.Integer);
336             }
337             break;
338 
339         case ACPI_TYPE_BUFFER:
340 
341             if (Op->Asl.ParseOpcode == PARSEOP_NAME)
342             {
343                 Op = Op->Asl.Child;
344             }
345 
346             if ((Op->Asl.ParseOpcode == PARSEOP_NAMESEG)  ||
347                 (Op->Asl.ParseOpcode == PARSEOP_NAMESTRING))
348             {
349                 Op = Op->Asl.Next;
350             }
351 
352             Op = Op->Asl.Child;
353 
354             if (Op && (Op->Asl.ParseOpcode == PARSEOP_INTEGER))
355             {
356                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
357                     "        [Initial Length  0x%.2X bytes]",
358                     (UINT32) Op->Asl.Value.Integer);
359             }
360             break;
361 
362         case ACPI_TYPE_METHOD:
363 
364             FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
365                 "        [Code Length     0x%.4X bytes]",
366                 Op->Asl.AmlSubtreeLength);
367             break;
368 
369         case ACPI_TYPE_LOCAL_RESOURCE:
370 
371             FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
372                 "  [Desc Offset     0x%.4X Bytes]", Node->Value);
373             break;
374 
375         case ACPI_TYPE_LOCAL_RESOURCE_FIELD:
376 
377             FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
378                 "   [Field Offset    0x%.4X Bits 0x%.4X Bytes] ",
379                 Node->Value, Node->Value / 8);
380 
381             if (Node->Flags & ANOBJ_IS_REFERENCED)
382             {
383                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
384                     "Referenced");
385             }
386             else
387             {
388                 FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT,
389                     "Name not referenced");
390             }
391             break;
392 
393         default:
394 
395             /* Nothing to do for other types */
396 
397             break;
398         }
399     }
400 
401     FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "\n");
402     return (AE_OK);
403 }
404 
405 
406 /*******************************************************************************
407  *
408  * FUNCTION:    NsDoOnePathname
409  *
410  * PARAMETERS:  ACPI_WALK_CALLBACK
411  *
412  * RETURN:      Status
413  *
414  * DESCRIPTION: Print the full pathname and additional info for a namespace
415  * node.
416  *
417  ******************************************************************************/
418 
419 static ACPI_STATUS
NsDoOnePathname(ACPI_HANDLE ObjHandle,UINT32 Level,void * Context,void ** ReturnValue)420 NsDoOnePathname (
421     ACPI_HANDLE             ObjHandle,
422     UINT32                  Level,
423     void                    *Context,
424     void                    **ReturnValue)
425 {
426     ACPI_NAMESPACE_NODE     *Node = (ACPI_NAMESPACE_NODE *) ObjHandle;
427     ACPI_STATUS             Status;
428     ACPI_BUFFER             TargetPath;
429 
430 
431     /* Ignore predefined namespace nodes and External declarations */
432 
433     if (!Node->Op || (Node->Flags & ANOBJ_IS_EXTERNAL))
434     {
435         return (AE_OK);
436     }
437 
438     TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
439     Status = AcpiNsHandleToPathname (Node, &TargetPath, FALSE);
440     if (ACPI_FAILURE (Status))
441     {
442         return (Status);
443     }
444 
445     /*
446      * Print the full pathname (and other information)
447      * for each namespace node in the common namespace
448      */
449     FlPrintFile (ASL_FILE_NAMESPACE_OUTPUT, "%-41s %-12s  %s, %u\n",
450         ACPI_CAST_PTR (char, TargetPath.Pointer),
451         AcpiUtGetTypeName (Node->Type),
452         Node->Op->Asl.Filename, Node->Op->Asl.LogicalLineNumber);
453 
454     ACPI_FREE (TargetPath.Pointer);
455     return (AE_OK);
456 }
457