1 /*******************************************************************************
2  *
3  * Module Name: dmresrc.c - Resource Descriptor disassembly
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 "amlcode.h"
47 #include "acdisasm.h"
48 
49 #ifdef ACPI_DISASSEMBLER
50 
51 #define _COMPONENT          ACPI_CA_DEBUGGER
52         ACPI_MODULE_NAME    ("dbresrc")
53 
54 
55 /* Dispatch tables for Resource disassembly functions */
56 
57 static ACPI_RESOURCE_HANDLER    AcpiGbl_DmResourceDispatch [] =
58 {
59     /* Small descriptors */
60 
61     NULL,                           /* 0x00, Reserved */
62     NULL,                           /* 0x01, Reserved */
63     NULL,                           /* 0x02, Reserved */
64     NULL,                           /* 0x03, Reserved */
65     AcpiDmIrqDescriptor,            /* 0x04, ACPI_RESOURCE_NAME_IRQ_FORMAT */
66     AcpiDmDmaDescriptor,            /* 0x05, ACPI_RESOURCE_NAME_DMA_FORMAT */
67     AcpiDmStartDependentDescriptor, /* 0x06, ACPI_RESOURCE_NAME_START_DEPENDENT */
68     AcpiDmEndDependentDescriptor,   /* 0x07, ACPI_RESOURCE_NAME_END_DEPENDENT */
69     AcpiDmIoDescriptor,             /* 0x08, ACPI_RESOURCE_NAME_IO_PORT */
70     AcpiDmFixedIoDescriptor,        /* 0x09, ACPI_RESOURCE_NAME_FIXED_IO_PORT */
71     AcpiDmFixedDmaDescriptor,       /* 0x0A, ACPI_RESOURCE_NAME_FIXED_DMA */
72     NULL,                           /* 0x0B, Reserved */
73     NULL,                           /* 0x0C, Reserved */
74     NULL,                           /* 0x0D, Reserved */
75     AcpiDmVendorSmallDescriptor,    /* 0x0E, ACPI_RESOURCE_NAME_SMALL_VENDOR */
76     NULL,                           /* 0x0F, ACPI_RESOURCE_NAME_END_TAG (not used) */
77 
78     /* Large descriptors */
79 
80     NULL,                           /* 0x00, Reserved */
81     AcpiDmMemory24Descriptor,       /* 0x01, ACPI_RESOURCE_NAME_MEMORY_24 */
82     AcpiDmGenericRegisterDescriptor,/* 0x02, ACPI_RESOURCE_NAME_GENERIC_REGISTER */
83     NULL,                           /* 0x03, Reserved */
84     AcpiDmVendorLargeDescriptor,    /* 0x04, ACPI_RESOURCE_NAME_LARGE_VENDOR */
85     AcpiDmMemory32Descriptor,       /* 0x05, ACPI_RESOURCE_NAME_MEMORY_32 */
86     AcpiDmFixedMemory32Descriptor,  /* 0x06, ACPI_RESOURCE_NAME_FIXED_MEMORY_32 */
87     AcpiDmDwordDescriptor,          /* 0x07, ACPI_RESOURCE_NAME_DWORD_ADDRESS_SPACE */
88     AcpiDmWordDescriptor,           /* 0x08, ACPI_RESOURCE_NAME_WORD_ADDRESS_SPACE */
89     AcpiDmInterruptDescriptor,      /* 0x09, ACPI_RESOURCE_NAME_EXTENDED_XRUPT */
90     AcpiDmQwordDescriptor,          /* 0x0A, ACPI_RESOURCE_NAME_QWORD_ADDRESS_SPACE */
91     AcpiDmExtendedDescriptor,       /* 0x0B, ACPI_RESOURCE_NAME_EXTENDED_ADDRESS_SPACE */
92     AcpiDmGpioDescriptor,           /* 0x0C, ACPI_RESOURCE_NAME_GPIO */
93     NULL,                           /* 0x0D, Reserved */
94     AcpiDmSerialBusDescriptor       /* 0x0E, ACPI_RESOURCE_NAME_SERIAL_BUS */
95 };
96 
97 
98 /* Only used for single-threaded applications */
99 /* TBD: remove when name is passed as parameter to the dump functions */
100 
101 static UINT32               ResourceName;
102 
103 
104 /*******************************************************************************
105  *
106  * FUNCTION:    AcpiDmDescriptorName
107  *
108  * PARAMETERS:  None
109  *
110  * RETURN:      None
111  *
112  * DESCRIPTION: Emit a name for the descriptor if one is present (indicated
113  *              by the name being changed from the default name.) A name is only
114  *              emitted if a reference to the descriptor has been made somewhere
115  *              in the original ASL code.
116  *
117  ******************************************************************************/
118 
119 void
120 AcpiDmDescriptorName (
121     void)
122 {
123 
124     if (ResourceName == ACPI_DEFAULT_RESNAME)
125     {
126         return;
127     }
128 
129     AcpiOsPrintf ("%4.4s", (char *) &ResourceName);
130 }
131 
132 
133 /*******************************************************************************
134  *
135  * FUNCTION:    AcpiDmDumpInteger*
136  *
137  * PARAMETERS:  Value               - Value to emit
138  *              Name                - Associated name (emitted as a comment)
139  *
140  * RETURN:      None
141  *
142  * DESCRIPTION: Integer output helper functions
143  *
144  ******************************************************************************/
145 
146 void
147 AcpiDmDumpInteger8 (
148     UINT8                   Value,
149     char                    *Name)
150 {
151     AcpiOsPrintf ("0x%2.2X,               // %s\n", Value, Name);
152 }
153 
154 void
155 AcpiDmDumpInteger16 (
156     UINT16                  Value,
157     char                    *Name)
158 {
159     AcpiOsPrintf ("0x%4.4X,             // %s\n", Value, Name);
160 }
161 
162 void
163 AcpiDmDumpInteger32 (
164     UINT32                  Value,
165     char                    *Name)
166 {
167     AcpiOsPrintf ("0x%8.8X,         // %s\n", Value, Name);
168 }
169 
170 void
171 AcpiDmDumpInteger64 (
172     UINT64                  Value,
173     char                    *Name)
174 {
175     AcpiOsPrintf ("0x%8.8X%8.8X, // %s\n", ACPI_FORMAT_UINT64 (Value), Name);
176 }
177 
178 
179 /*******************************************************************************
180  *
181  * FUNCTION:    AcpiDmBitList
182  *
183  * PARAMETERS:  Mask            - 16-bit value corresponding to 16 interrupt
184  *                                or DMA values
185  *
186  * RETURN:      None
187  *
188  * DESCRIPTION: Dump a bit mask as a list of individual interrupt/DMA levels.
189  *
190  ******************************************************************************/
191 
192 void
193 AcpiDmBitList (
194     UINT16                  Mask)
195 {
196     UINT32                  i;
197     BOOLEAN                 Previous = FALSE;
198 
199 
200     /* Open the initializer list */
201 
202     AcpiOsPrintf ("{");
203 
204     /* Examine each bit */
205 
206     for (i = 0; i < 16; i++)
207     {
208         /* Only interested in bits that are set to 1 */
209 
210         if (Mask & 1)
211         {
212             if (Previous)
213             {
214                 AcpiOsPrintf (",");
215             }
216 
217             Previous = TRUE;
218             AcpiOsPrintf ("%u", i);
219         }
220 
221         Mask >>= 1;
222     }
223 
224     /* Close list */
225 
226     AcpiOsPrintf ("}\n");
227 }
228 
229 
230 /*******************************************************************************
231  *
232  * FUNCTION:    AcpiDmResourceTemplate
233  *
234  * PARAMETERS:  Info            - Curent parse tree walk info
235  *              ByteData        - Pointer to the byte list data
236  *              ByteCount       - Length of the byte list
237  *
238  * RETURN:      None
239  *
240  * DESCRIPTION: Dump the contents of a Resource Template containing a set of
241  *              Resource Descriptors.
242  *
243  ******************************************************************************/
244 
245 void
246 AcpiDmResourceTemplate (
247     ACPI_OP_WALK_INFO       *Info,
248     ACPI_PARSE_OBJECT       *Op,
249     UINT8                   *ByteData,
250     UINT32                  ByteCount)
251 {
252     ACPI_STATUS             Status;
253     UINT32                  CurrentByteOffset;
254     UINT8                   ResourceType;
255     UINT32                  ResourceLength;
256     void                    *Aml;
257     UINT32                  Level;
258     BOOLEAN                 DependentFns = FALSE;
259     UINT8                   ResourceIndex;
260     ACPI_NAMESPACE_NODE     *Node;
261 
262 
263     if (Op->Asl.AmlOpcode != AML_FIELD_OP)
264     {
265         Info->MappingOp = Op;
266     }
267 
268     Level = Info->Level;
269     ResourceName = ACPI_DEFAULT_RESNAME;
270     Node = Op->Common.Node;
271     if (Node)
272     {
273         Node = Node->Child;
274     }
275 
276     for (CurrentByteOffset = 0; CurrentByteOffset < ByteCount;)
277     {
278         Aml = &ByteData[CurrentByteOffset];
279 
280         /* Get the descriptor type and length */
281 
282         ResourceType = AcpiUtGetResourceType (Aml);
283         ResourceLength = AcpiUtGetResourceLength (Aml);
284 
285         /* Validate the Resource Type and Resource Length */
286 
287         Status = AcpiUtValidateResource (NULL, Aml, &ResourceIndex);
288         if (ACPI_FAILURE (Status))
289         {
290             AcpiOsPrintf (
291                 "/*** Could not validate Resource, type (%X) %s***/\n",
292                 ResourceType, AcpiFormatException (Status));
293             return;
294         }
295 
296         /* Point to next descriptor */
297 
298         CurrentByteOffset += AcpiUtGetDescriptorLength (Aml);
299 
300         /* Descriptor pre-processing */
301 
302         switch (ResourceType)
303         {
304         case ACPI_RESOURCE_NAME_START_DEPENDENT:
305 
306             /* Finish a previous StartDependentFns */
307 
308             if (DependentFns)
309             {
310                 Level--;
311                 AcpiDmIndent (Level);
312                 AcpiOsPrintf ("}\n");
313             }
314             break;
315 
316         case ACPI_RESOURCE_NAME_END_DEPENDENT:
317 
318             Level--;
319             DependentFns = FALSE;
320             break;
321 
322         case ACPI_RESOURCE_NAME_END_TAG:
323 
324             /* Normal exit, the resource list is finished */
325 
326             if (DependentFns)
327             {
328                 /*
329                  * Close an open StartDependentDescriptor. This indicates a
330                  * missing EndDependentDescriptor.
331                  */
332                 Level--;
333                 DependentFns = FALSE;
334 
335                 /* Go ahead and insert EndDependentFn() */
336 
337                 AcpiDmEndDependentDescriptor (Info, Aml, ResourceLength, Level);
338 
339                 AcpiDmIndent (Level);
340                 AcpiOsPrintf (
341                     "/*** Disassembler: inserted "
342                     "missing EndDependentFn () ***/\n");
343             }
344             return;
345 
346         default:
347 
348             break;
349         }
350 
351         /* Disassemble the resource structure */
352 
353         if (Node)
354         {
355             ResourceName = Node->Name.Integer;
356             Node = Node->Peer;
357         }
358 
359         AcpiGbl_DmResourceDispatch [ResourceIndex] (
360             Info, Aml, ResourceLength, Level);
361 
362         /* Descriptor post-processing */
363 
364         if (ResourceType == ACPI_RESOURCE_NAME_START_DEPENDENT)
365         {
366             DependentFns = TRUE;
367             Level++;
368         }
369     }
370 }
371 
372 
373 /*******************************************************************************
374  *
375  * FUNCTION:    AcpiDmIsResourceTemplate
376  *
377  * PARAMETERS:  WalkState           - Current walk info
378  *              Op                  - Buffer Op to be examined
379  *
380  * RETURN:      Status. AE_OK if valid template
381  *
382  * DESCRIPTION: Walk a byte list to determine if it consists of a valid set
383  *              of resource descriptors. Nothing is output.
384  *
385  ******************************************************************************/
386 
387 ACPI_STATUS
388 AcpiDmIsResourceTemplate (
389     ACPI_WALK_STATE         *WalkState,
390     ACPI_PARSE_OBJECT       *Op)
391 {
392     ACPI_STATUS             Status;
393     ACPI_PARSE_OBJECT       *NextOp;
394     UINT8                   *Aml;
395     UINT8                   *EndAml;
396     ACPI_SIZE               Length;
397 
398 
399     /* This op must be a buffer */
400 
401     if (Op->Common.AmlOpcode != AML_BUFFER_OP)
402     {
403         return (AE_TYPE);
404     }
405 
406     /* Get the ByteData list and length */
407 
408     NextOp = Op->Common.Value.Arg;
409     if (!NextOp)
410     {
411         AcpiOsPrintf ("NULL byte list in buffer\n");
412         return (AE_TYPE);
413     }
414 
415     NextOp = NextOp->Common.Next;
416     if (!NextOp)
417     {
418         return (AE_TYPE);
419     }
420 
421     Aml = NextOp->Named.Data;
422     Length = (ACPI_SIZE) NextOp->Common.Value.Integer;
423 
424     /* Walk the byte list, abort on any invalid descriptor type or length */
425 
426     Status = AcpiUtWalkAmlResources (WalkState, Aml, Length,
427         NULL, ACPI_CAST_INDIRECT_PTR (void, &EndAml));
428     if (ACPI_FAILURE (Status))
429     {
430         return (AE_TYPE);
431     }
432 
433     /*
434      * For the resource template to be valid, one EndTag must appear
435      * at the very end of the ByteList, not before. (For proper disassembly
436      * of a ResourceTemplate, the buffer must not have any extra data after
437      * the EndTag.)
438      */
439     if ((Aml + Length - sizeof (AML_RESOURCE_END_TAG)) != EndAml)
440     {
441         return (AE_AML_NO_RESOURCE_END_TAG);
442     }
443 
444     /*
445      * All resource descriptors are valid, therefore this list appears
446      * to be a valid resource template
447      */
448     return (AE_OK);
449 }
450 
451 #endif
452