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