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