xref: /freebsd/sys/contrib/dev/acpica/components/debugger/dbutils.c (revision 49b49cda41feabe3439f7318e8bf40e3896c7bf4)
1 /*******************************************************************************
2  *
3  * Module Name: dbutils - AML debugger utilities
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 <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acnamesp.h>
47 #include <contrib/dev/acpica/include/acdebug.h>
48 
49 
50 #define _COMPONENT          ACPI_CA_DEBUGGER
51         ACPI_MODULE_NAME    ("dbutils")
52 
53 
54 /* Local prototypes */
55 
56 #ifdef ACPI_OBSOLETE_FUNCTIONS
57 ACPI_STATUS
58 AcpiDbSecondPassParse (
59     ACPI_PARSE_OBJECT       *Root);
60 
61 void
62 AcpiDbDumpBuffer (
63     UINT32                  Address);
64 #endif
65 
66 static char                 *Gbl_HexToAscii = "0123456789ABCDEF";
67 
68 
69 /*******************************************************************************
70  *
71  * FUNCTION:    AcpiDbMatchArgument
72  *
73  * PARAMETERS:  UserArgument            - User command line
74  *              Arguments               - Array of commands to match against
75  *
76  * RETURN:      Index into command array or ACPI_TYPE_NOT_FOUND if not found
77  *
78  * DESCRIPTION: Search command array for a command match
79  *
80  ******************************************************************************/
81 
82 ACPI_OBJECT_TYPE
83 AcpiDbMatchArgument (
84     char                    *UserArgument,
85     ACPI_DB_ARGUMENT_INFO   *Arguments)
86 {
87     UINT32                  i;
88 
89 
90     if (!UserArgument || UserArgument[0] == 0)
91     {
92         return (ACPI_TYPE_NOT_FOUND);
93     }
94 
95     for (i = 0; Arguments[i].Name; i++)
96     {
97         if (strstr (Arguments[i].Name, UserArgument) == Arguments[i].Name)
98         {
99             return (i);
100         }
101     }
102 
103     /* Argument not recognized */
104 
105     return (ACPI_TYPE_NOT_FOUND);
106 }
107 
108 
109 /*******************************************************************************
110  *
111  * FUNCTION:    AcpiDbSetOutputDestination
112  *
113  * PARAMETERS:  OutputFlags         - Current flags word
114  *
115  * RETURN:      None
116  *
117  * DESCRIPTION: Set the current destination for debugger output. Also sets
118  *              the debug output level accordingly.
119  *
120  ******************************************************************************/
121 
122 void
123 AcpiDbSetOutputDestination (
124     UINT32                  OutputFlags)
125 {
126 
127     AcpiGbl_DbOutputFlags = (UINT8) OutputFlags;
128 
129     if ((OutputFlags & ACPI_DB_REDIRECTABLE_OUTPUT) &&
130         AcpiGbl_DbOutputToFile)
131     {
132         AcpiDbgLevel = AcpiGbl_DbDebugLevel;
133     }
134     else
135     {
136         AcpiDbgLevel = AcpiGbl_DbConsoleDebugLevel;
137     }
138 }
139 
140 
141 /*******************************************************************************
142  *
143  * FUNCTION:    AcpiDbDumpExternalObject
144  *
145  * PARAMETERS:  ObjDesc         - External ACPI object to dump
146  *              Level           - Nesting level.
147  *
148  * RETURN:      None
149  *
150  * DESCRIPTION: Dump the contents of an ACPI external object
151  *
152  ******************************************************************************/
153 
154 void
155 AcpiDbDumpExternalObject (
156     ACPI_OBJECT             *ObjDesc,
157     UINT32                  Level)
158 {
159     UINT32                  i;
160 
161 
162     if (!ObjDesc)
163     {
164         AcpiOsPrintf ("[Null Object]\n");
165         return;
166     }
167 
168     for (i = 0; i < Level; i++)
169     {
170         AcpiOsPrintf ("  ");
171     }
172 
173     switch (ObjDesc->Type)
174     {
175     case ACPI_TYPE_ANY:
176 
177         AcpiOsPrintf ("[Null Object] (Type=0)\n");
178         break;
179 
180     case ACPI_TYPE_INTEGER:
181 
182         AcpiOsPrintf ("[Integer] = %8.8X%8.8X\n",
183             ACPI_FORMAT_UINT64 (ObjDesc->Integer.Value));
184         break;
185 
186     case ACPI_TYPE_STRING:
187 
188         AcpiOsPrintf ("[String] Length %.2X = ", ObjDesc->String.Length);
189         AcpiUtPrintString (ObjDesc->String.Pointer, ACPI_UINT8_MAX);
190         AcpiOsPrintf ("\n");
191         break;
192 
193     case ACPI_TYPE_BUFFER:
194 
195         AcpiOsPrintf ("[Buffer] Length %.2X = ", ObjDesc->Buffer.Length);
196         if (ObjDesc->Buffer.Length)
197         {
198             if (ObjDesc->Buffer.Length > 16)
199             {
200                 AcpiOsPrintf ("\n");
201             }
202             AcpiUtDebugDumpBuffer (
203                 ACPI_CAST_PTR (UINT8, ObjDesc->Buffer.Pointer),
204                 ObjDesc->Buffer.Length, DB_BYTE_DISPLAY, _COMPONENT);
205         }
206         else
207         {
208             AcpiOsPrintf ("\n");
209         }
210         break;
211 
212     case ACPI_TYPE_PACKAGE:
213 
214         AcpiOsPrintf ("[Package] Contains %u Elements:\n",
215             ObjDesc->Package.Count);
216 
217         for (i = 0; i < ObjDesc->Package.Count; i++)
218         {
219             AcpiDbDumpExternalObject (
220                 &ObjDesc->Package.Elements[i], Level+1);
221         }
222         break;
223 
224     case ACPI_TYPE_LOCAL_REFERENCE:
225 
226         AcpiOsPrintf ("[Object Reference] = ");
227         AcpiDbDisplayInternalObject (ObjDesc->Reference.Handle, NULL);
228         break;
229 
230     case ACPI_TYPE_PROCESSOR:
231 
232         AcpiOsPrintf ("[Processor]\n");
233         break;
234 
235     case ACPI_TYPE_POWER:
236 
237         AcpiOsPrintf ("[Power Resource]\n");
238         break;
239 
240     default:
241 
242         AcpiOsPrintf ("[Unknown Type] %X\n", ObjDesc->Type);
243         break;
244     }
245 }
246 
247 
248 /*******************************************************************************
249  *
250  * FUNCTION:    AcpiDbPrepNamestring
251  *
252  * PARAMETERS:  Name            - String to prepare
253  *
254  * RETURN:      None
255  *
256  * DESCRIPTION: Translate all forward slashes and dots to backslashes.
257  *
258  ******************************************************************************/
259 
260 void
261 AcpiDbPrepNamestring (
262     char                    *Name)
263 {
264 
265     if (!Name)
266     {
267         return;
268     }
269 
270     AcpiUtStrupr (Name);
271 
272     /* Convert a leading forward slash to a backslash */
273 
274     if (*Name == '/')
275     {
276         *Name = '\\';
277     }
278 
279     /* Ignore a leading backslash, this is the root prefix */
280 
281     if (ACPI_IS_ROOT_PREFIX (*Name))
282     {
283         Name++;
284     }
285 
286     /* Convert all slash path separators to dots */
287 
288     while (*Name)
289     {
290         if ((*Name == '/') ||
291             (*Name == '\\'))
292         {
293             *Name = '.';
294         }
295 
296         Name++;
297     }
298 }
299 
300 
301 /*******************************************************************************
302  *
303  * FUNCTION:    AcpiDbLocalNsLookup
304  *
305  * PARAMETERS:  Name            - Name to lookup
306  *
307  * RETURN:      Pointer to a namespace node, null on failure
308  *
309  * DESCRIPTION: Lookup a name in the ACPI namespace
310  *
311  * Note: Currently begins search from the root. Could be enhanced to use
312  * the current prefix (scope) node as the search beginning point.
313  *
314  ******************************************************************************/
315 
316 ACPI_NAMESPACE_NODE *
317 AcpiDbLocalNsLookup (
318     char                    *Name)
319 {
320     char                    *InternalPath;
321     ACPI_STATUS             Status;
322     ACPI_NAMESPACE_NODE     *Node = NULL;
323 
324 
325     AcpiDbPrepNamestring (Name);
326 
327     /* Build an internal namestring */
328 
329     Status = AcpiNsInternalizeName (Name, &InternalPath);
330     if (ACPI_FAILURE (Status))
331     {
332         AcpiOsPrintf ("Invalid namestring: %s\n", Name);
333         return (NULL);
334     }
335 
336     /*
337      * Lookup the name.
338      * (Uses root node as the search starting point)
339      */
340     Status = AcpiNsLookup (NULL, InternalPath, ACPI_TYPE_ANY,
341         ACPI_IMODE_EXECUTE, ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE,
342         NULL, &Node);
343     if (ACPI_FAILURE (Status))
344     {
345         AcpiOsPrintf ("Could not locate name: %s, %s\n",
346             Name, AcpiFormatException (Status));
347     }
348 
349     ACPI_FREE (InternalPath);
350     return (Node);
351 }
352 
353 
354 /*******************************************************************************
355  *
356  * FUNCTION:    AcpiDbUint32ToHexString
357  *
358  * PARAMETERS:  Value           - The value to be converted to string
359  *              Buffer          - Buffer for result (not less than 11 bytes)
360  *
361  * RETURN:      None
362  *
363  * DESCRIPTION: Convert the unsigned 32-bit value to the hexadecimal image
364  *
365  * NOTE: It is the caller's responsibility to ensure that the length of buffer
366  *       is sufficient.
367  *
368  ******************************************************************************/
369 
370 void
371 AcpiDbUint32ToHexString (
372     UINT32                  Value,
373     char                    *Buffer)
374 {
375     int                     i;
376 
377 
378     if (Value == 0)
379     {
380         strcpy (Buffer, "0");
381         return;
382     }
383 
384     Buffer[8] = '\0';
385 
386     for (i = 7; i >= 0; i--)
387     {
388         Buffer[i] = Gbl_HexToAscii [Value & 0x0F];
389         Value = Value >> 4;
390     }
391 }
392 
393 
394 #ifdef ACPI_OBSOLETE_FUNCTIONS
395 /*******************************************************************************
396  *
397  * FUNCTION:    AcpiDbSecondPassParse
398  *
399  * PARAMETERS:  Root            - Root of the parse tree
400  *
401  * RETURN:      Status
402  *
403  * DESCRIPTION: Second pass parse of the ACPI tables. We need to wait until
404  *              second pass to parse the control methods
405  *
406  ******************************************************************************/
407 
408 ACPI_STATUS
409 AcpiDbSecondPassParse (
410     ACPI_PARSE_OBJECT       *Root)
411 {
412     ACPI_PARSE_OBJECT       *Op = Root;
413     ACPI_PARSE_OBJECT       *Method;
414     ACPI_PARSE_OBJECT       *SearchOp;
415     ACPI_PARSE_OBJECT       *StartOp;
416     ACPI_STATUS             Status = AE_OK;
417     UINT32                  BaseAmlOffset;
418     ACPI_WALK_STATE         *WalkState;
419 
420 
421     ACPI_FUNCTION_ENTRY ();
422 
423 
424     AcpiOsPrintf ("Pass two parse ....\n");
425 
426     while (Op)
427     {
428         if (Op->Common.AmlOpcode == AML_METHOD_OP)
429         {
430             Method = Op;
431 
432             /* Create a new walk state for the parse */
433 
434             WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);
435             if (!WalkState)
436             {
437                 return (AE_NO_MEMORY);
438             }
439 
440             /* Init the Walk State */
441 
442             WalkState->ParserState.Aml          =
443             WalkState->ParserState.AmlStart     = Method->Named.Data;
444             WalkState->ParserState.AmlEnd       =
445             WalkState->ParserState.PkgEnd       = Method->Named.Data +
446                                                   Method->Named.Length;
447             WalkState->ParserState.StartScope   = Op;
448 
449             WalkState->DescendingCallback       = AcpiDsLoad1BeginOp;
450             WalkState->AscendingCallback        = AcpiDsLoad1EndOp;
451 
452             /* Perform the AML parse */
453 
454             Status = AcpiPsParseAml (WalkState);
455 
456             BaseAmlOffset = (Method->Common.Value.Arg)->Common.AmlOffset + 1;
457             StartOp = (Method->Common.Value.Arg)->Common.Next;
458             SearchOp = StartOp;
459 
460             while (SearchOp)
461             {
462                 SearchOp->Common.AmlOffset += BaseAmlOffset;
463                 SearchOp = AcpiPsGetDepthNext (StartOp, SearchOp);
464             }
465         }
466 
467         if (Op->Common.AmlOpcode == AML_REGION_OP)
468         {
469             /* TBD: [Investigate] this isn't quite the right thing to do! */
470             /*
471              *
472              * Method = (ACPI_DEFERRED_OP *) Op;
473              * Status = AcpiPsParseAml (Op, Method->Body, Method->BodyLength);
474              */
475         }
476 
477         if (ACPI_FAILURE (Status))
478         {
479             break;
480         }
481 
482         Op = AcpiPsGetDepthNext (Root, Op);
483     }
484 
485     return (Status);
486 }
487 
488 
489 /*******************************************************************************
490  *
491  * FUNCTION:    AcpiDbDumpBuffer
492  *
493  * PARAMETERS:  Address             - Pointer to the buffer
494  *
495  * RETURN:      None
496  *
497  * DESCRIPTION: Print a portion of a buffer
498  *
499  ******************************************************************************/
500 
501 void
502 AcpiDbDumpBuffer (
503     UINT32                  Address)
504 {
505 
506     AcpiOsPrintf ("\nLocation %X:\n", Address);
507 
508     AcpiDbgLevel |= ACPI_LV_TABLES;
509     AcpiUtDebugDumpBuffer (ACPI_TO_POINTER (Address), 64, DB_BYTE_DISPLAY,
510         ACPI_UINT32_MAX);
511 }
512 #endif
513