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