Sync ACPICA with Intel's version 20150818.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / debugger / dbutils.c
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 "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 static char                 *Gbl_HexToAscii = "0123456789ABCDEF";
69
70
71 /*******************************************************************************
72  *
73  * FUNCTION:    AcpiDbMatchArgument
74  *
75  * PARAMETERS:  UserArgument            - User command line
76  *              Arguments               - Array of commands to match against
77  *
78  * RETURN:      Index into command array or ACPI_TYPE_NOT_FOUND if not found
79  *
80  * DESCRIPTION: Search command array for a command match
81  *
82  ******************************************************************************/
83
84 ACPI_OBJECT_TYPE
85 AcpiDbMatchArgument (
86     char                    *UserArgument,
87     ACPI_DB_ARGUMENT_INFO   *Arguments)
88 {
89     UINT32                  i;
90
91
92     if (!UserArgument || UserArgument[0] == 0)
93     {
94         return (ACPI_TYPE_NOT_FOUND);
95     }
96
97     for (i = 0; Arguments[i].Name; i++)
98     {
99         if (strstr (Arguments[i].Name, 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             AcpiUtDebugDumpBuffer (
205                 ACPI_CAST_PTR (UINT8, ObjDesc->Buffer.Pointer),
206                 ObjDesc->Buffer.Length, DB_BYTE_DISPLAY, _COMPONENT);
207         }
208         else
209         {
210             AcpiOsPrintf ("\n");
211         }
212         break;
213
214     case ACPI_TYPE_PACKAGE:
215
216         AcpiOsPrintf ("[Package] Contains %u Elements:\n",
217             ObjDesc->Package.Count);
218
219         for (i = 0; i < ObjDesc->Package.Count; i++)
220         {
221             AcpiDbDumpExternalObject (
222                 &ObjDesc->Package.Elements[i], Level+1);
223         }
224         break;
225
226     case ACPI_TYPE_LOCAL_REFERENCE:
227
228         AcpiOsPrintf ("[Object Reference] = ");
229         AcpiDbDisplayInternalObject (ObjDesc->Reference.Handle, NULL);
230         break;
231
232     case ACPI_TYPE_PROCESSOR:
233
234         AcpiOsPrintf ("[Processor]\n");
235         break;
236
237     case ACPI_TYPE_POWER:
238
239         AcpiOsPrintf ("[Power Resource]\n");
240         break;
241
242     default:
243
244         AcpiOsPrintf ("[Unknown Type] %X\n", ObjDesc->Type);
245         break;
246     }
247 }
248
249
250 /*******************************************************************************
251  *
252  * FUNCTION:    AcpiDbPrepNamestring
253  *
254  * PARAMETERS:  Name            - String to prepare
255  *
256  * RETURN:      None
257  *
258  * DESCRIPTION: Translate all forward slashes and dots to backslashes.
259  *
260  ******************************************************************************/
261
262 void
263 AcpiDbPrepNamestring (
264     char                    *Name)
265 {
266
267     if (!Name)
268     {
269         return;
270     }
271
272     AcpiUtStrupr (Name);
273
274     /* Convert a leading forward slash to a backslash */
275
276     if (*Name == '/')
277     {
278         *Name = '\\';
279     }
280
281     /* Ignore a leading backslash, this is the root prefix */
282
283     if (ACPI_IS_ROOT_PREFIX (*Name))
284     {
285         Name++;
286     }
287
288     /* Convert all slash path separators to dots */
289
290     while (*Name)
291     {
292         if ((*Name == '/') ||
293             (*Name == '\\'))
294         {
295             *Name = '.';
296         }
297
298         Name++;
299     }
300 }
301
302
303 /*******************************************************************************
304  *
305  * FUNCTION:    AcpiDbLocalNsLookup
306  *
307  * PARAMETERS:  Name            - Name to lookup
308  *
309  * RETURN:      Pointer to a namespace node, null on failure
310  *
311  * DESCRIPTION: Lookup a name in the ACPI namespace
312  *
313  * Note: Currently begins search from the root. Could be enhanced to use
314  * the current prefix (scope) node as the search beginning point.
315  *
316  ******************************************************************************/
317
318 ACPI_NAMESPACE_NODE *
319 AcpiDbLocalNsLookup (
320     char                    *Name)
321 {
322     char                    *InternalPath;
323     ACPI_STATUS             Status;
324     ACPI_NAMESPACE_NODE     *Node = NULL;
325
326
327     AcpiDbPrepNamestring (Name);
328
329     /* Build an internal namestring */
330
331     Status = AcpiNsInternalizeName (Name, &InternalPath);
332     if (ACPI_FAILURE (Status))
333     {
334         AcpiOsPrintf ("Invalid namestring: %s\n", Name);
335         return (NULL);
336     }
337
338     /*
339      * Lookup the name.
340      * (Uses root node as the search starting point)
341      */
342     Status = AcpiNsLookup (NULL, InternalPath, ACPI_TYPE_ANY,
343         ACPI_IMODE_EXECUTE, ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE,
344         NULL, &Node);
345     if (ACPI_FAILURE (Status))
346     {
347         AcpiOsPrintf ("Could not locate name: %s, %s\n",
348             Name, AcpiFormatException (Status));
349     }
350
351     ACPI_FREE (InternalPath);
352     return (Node);
353 }
354
355
356 /*******************************************************************************
357  *
358  * FUNCTION:    AcpiDbUint32ToHexString
359  *
360  * PARAMETERS:  Value           - The value to be converted to string
361  *              Buffer          - Buffer for result (not less than 11 bytes)
362  *
363  * RETURN:      None
364  *
365  * DESCRIPTION: Convert the unsigned 32-bit value to the hexadecimal image
366  *
367  * NOTE: It is the caller's responsibility to ensure that the length of buffer
368  *       is sufficient.
369  *
370  ******************************************************************************/
371
372 void
373 AcpiDbUint32ToHexString (
374     UINT32                  Value,
375     char                    *Buffer)
376 {
377     int                     i;
378
379
380     if (Value == 0)
381     {
382         strcpy (Buffer, "0");
383         return;
384     }
385
386     Buffer[8] = '\0';
387
388     for (i = 7; i >= 0; i--)
389     {
390         Buffer[i] = Gbl_HexToAscii [Value & 0x0F];
391         Value = Value >> 4;
392     }
393 }
394
395
396 #ifdef ACPI_OBSOLETE_FUNCTIONS
397 /*******************************************************************************
398  *
399  * FUNCTION:    AcpiDbSecondPassParse
400  *
401  * PARAMETERS:  Root            - Root of the parse tree
402  *
403  * RETURN:      Status
404  *
405  * DESCRIPTION: Second pass parse of the ACPI tables. We need to wait until
406  *              second pass to parse the control methods
407  *
408  ******************************************************************************/
409
410 ACPI_STATUS
411 AcpiDbSecondPassParse (
412     ACPI_PARSE_OBJECT       *Root)
413 {
414     ACPI_PARSE_OBJECT       *Op = Root;
415     ACPI_PARSE_OBJECT       *Method;
416     ACPI_PARSE_OBJECT       *SearchOp;
417     ACPI_PARSE_OBJECT       *StartOp;
418     ACPI_STATUS             Status = AE_OK;
419     UINT32                  BaseAmlOffset;
420     ACPI_WALK_STATE         *WalkState;
421
422
423     ACPI_FUNCTION_ENTRY ();
424
425
426     AcpiOsPrintf ("Pass two parse ....\n");
427
428     while (Op)
429     {
430         if (Op->Common.AmlOpcode == AML_METHOD_OP)
431         {
432             Method = Op;
433
434             /* Create a new walk state for the parse */
435
436             WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);
437             if (!WalkState)
438             {
439                 return (AE_NO_MEMORY);
440             }
441
442             /* Init the Walk State */
443
444             WalkState->ParserState.Aml          =
445             WalkState->ParserState.AmlStart     = Method->Named.Data;
446             WalkState->ParserState.AmlEnd       =
447             WalkState->ParserState.PkgEnd       = Method->Named.Data +
448                                                   Method->Named.Length;
449             WalkState->ParserState.StartScope   = Op;
450
451             WalkState->DescendingCallback       = AcpiDsLoad1BeginOp;
452             WalkState->AscendingCallback        = AcpiDsLoad1EndOp;
453
454             /* Perform the AML parse */
455
456             Status = AcpiPsParseAml (WalkState);
457
458             BaseAmlOffset = (Method->Common.Value.Arg)->Common.AmlOffset + 1;
459             StartOp = (Method->Common.Value.Arg)->Common.Next;
460             SearchOp = StartOp;
461
462             while (SearchOp)
463             {
464                 SearchOp->Common.AmlOffset += BaseAmlOffset;
465                 SearchOp = AcpiPsGetDepthNext (StartOp, SearchOp);
466             }
467         }
468
469         if (Op->Common.AmlOpcode == AML_REGION_OP)
470         {
471             /* TBD: [Investigate] this isn't quite the right thing to do! */
472             /*
473              *
474              * Method = (ACPI_DEFERRED_OP *) Op;
475              * Status = AcpiPsParseAml (Op, Method->Body, Method->BodyLength);
476              */
477         }
478
479         if (ACPI_FAILURE (Status))
480         {
481             break;
482         }
483
484         Op = AcpiPsGetDepthNext (Root, Op);
485     }
486
487     return (Status);
488 }
489
490
491 /*******************************************************************************
492  *
493  * FUNCTION:    AcpiDbDumpBuffer
494  *
495  * PARAMETERS:  Address             - Pointer to the buffer
496  *
497  * RETURN:      None
498  *
499  * DESCRIPTION: Print a portion of a buffer
500  *
501  ******************************************************************************/
502
503 void
504 AcpiDbDumpBuffer (
505     UINT32                  Address)
506 {
507
508     AcpiOsPrintf ("\nLocation %X:\n", Address);
509
510     AcpiDbgLevel |= ACPI_LV_TABLES;
511     AcpiUtDebugDumpBuffer (ACPI_TO_POINTER (Address), 64, DB_BYTE_DISPLAY,
512         ACPI_UINT32_MAX);
513 }
514 #endif
515
516 #endif /* ACPI_DEBUGGER */