Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / contrib / dev / acpica-unix / disassembler / dmbuffer.c
1 /*******************************************************************************
2  *
3  * Module Name: dmbuffer - AML disassembler, buffer and string support
4  *
5  ******************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights.  You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code.  No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision.  In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change.  Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee.  Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution.  In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government.  In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115
116
117 #include "acpi.h"
118 #include "accommon.h"
119 #include "acdisasm.h"
120 #include "acparser.h"
121 #include "amlcode.h"
122
123
124 #ifdef ACPI_DISASSEMBLER
125
126 #define _COMPONENT          ACPI_CA_DEBUGGER
127         ACPI_MODULE_NAME    ("dmbuffer")
128
129 /* Local prototypes */
130
131 static void
132 AcpiDmUnicode (
133     ACPI_PARSE_OBJECT       *Op);
134
135 static void
136 AcpiDmIsEisaIdElement (
137     ACPI_PARSE_OBJECT       *Op);
138
139
140 /*******************************************************************************
141  *
142  * FUNCTION:    AcpiDmDisasmByteList
143  *
144  * PARAMETERS:  Level               - Current source code indentation level
145  *              ByteData            - Pointer to the byte list
146  *              ByteCount           - Length of the byte list
147  *
148  * RETURN:      None
149  *
150  * DESCRIPTION: Dump an AML "ByteList" in Hex format. 8 bytes per line, prefixed
151  *              with the hex buffer offset.
152  *
153  ******************************************************************************/
154
155 void
156 AcpiDmDisasmByteList (
157     UINT32                  Level,
158     UINT8                   *ByteData,
159     UINT32                  ByteCount)
160 {
161     UINT32                  i;
162
163
164     if (!ByteCount)
165     {
166         return;
167     }
168
169     /* Dump the byte list */
170
171     for (i = 0; i < ByteCount; i++)
172     {
173         /* New line every 8 bytes */
174
175         if (((i % 8) == 0) && (i < ByteCount))
176         {
177             if (i > 0)
178             {
179                 AcpiOsPrintf ("\n");
180             }
181
182             AcpiDmIndent (Level);
183             if (ByteCount > 7)
184             {
185                 AcpiOsPrintf ("/* %04X */    ", i);
186             }
187         }
188
189         AcpiOsPrintf ("0x%2.2X", (UINT32) ByteData[i]);
190
191         /* Add comma if there are more bytes to display */
192
193         if (i < (ByteCount -1))
194         {
195             AcpiOsPrintf (", ");
196         }
197     }
198
199     if (Level)
200     {
201         AcpiOsPrintf ("\n");
202     }
203 }
204
205
206 /*******************************************************************************
207  *
208  * FUNCTION:    AcpiDmByteList
209  *
210  * PARAMETERS:  Info            - Parse tree walk info
211  *              Op              - Byte list op
212  *
213  * RETURN:      None
214  *
215  * DESCRIPTION: Dump a buffer byte list, handling the various types of buffers.
216  *              Buffer type must be already set in the Op DisasmOpcode.
217  *
218  ******************************************************************************/
219
220 void
221 AcpiDmByteList (
222     ACPI_OP_WALK_INFO       *Info,
223     ACPI_PARSE_OBJECT       *Op)
224 {
225     UINT8                   *ByteData;
226     UINT32                  ByteCount;
227
228
229     ByteData = Op->Named.Data;
230     ByteCount = (UINT32) Op->Common.Value.Integer;
231
232     /*
233      * The byte list belongs to a buffer, and can be produced by either
234      * a ResourceTemplate, Unicode, quoted string, or a plain byte list.
235      */
236     switch (Op->Common.Parent->Common.DisasmOpcode)
237     {
238     case ACPI_DASM_RESOURCE:
239
240         AcpiDmResourceTemplate (Info, Op->Common.Parent, ByteData, ByteCount);
241         break;
242
243     case ACPI_DASM_STRING:
244
245         AcpiDmIndent (Info->Level);
246         AcpiUtPrintString ((char *) ByteData, ACPI_UINT8_MAX);
247         AcpiOsPrintf ("\n");
248         break;
249
250     case ACPI_DASM_UNICODE:
251
252         AcpiDmUnicode (Op);
253         break;
254
255     case ACPI_DASM_BUFFER:
256     default:
257
258         /*
259          * Not a resource, string, or unicode string.
260          * Just dump the buffer
261          */
262         AcpiDmDisasmByteList (Info->Level, ByteData, ByteCount);
263         break;
264     }
265 }
266
267
268 /*******************************************************************************
269  *
270  * FUNCTION:    AcpiDmIsUnicodeBuffer
271  *
272  * PARAMETERS:  Op              - Buffer Object to be examined
273  *
274  * RETURN:      TRUE if buffer contains a UNICODE string
275  *
276  * DESCRIPTION: Determine if a buffer Op contains a Unicode string
277  *
278  ******************************************************************************/
279
280 BOOLEAN
281 AcpiDmIsUnicodeBuffer (
282     ACPI_PARSE_OBJECT       *Op)
283 {
284     UINT8                   *ByteData;
285     UINT32                  ByteCount;
286     UINT32                  WordCount;
287     ACPI_PARSE_OBJECT       *SizeOp;
288     ACPI_PARSE_OBJECT       *NextOp;
289     UINT32                  i;
290
291
292     /* Buffer size is the buffer argument */
293
294     SizeOp = Op->Common.Value.Arg;
295
296     /* Next, the initializer byte list to examine */
297
298     NextOp = SizeOp->Common.Next;
299     if (!NextOp)
300     {
301         return (FALSE);
302     }
303
304     /* Extract the byte list info */
305
306     ByteData = NextOp->Named.Data;
307     ByteCount = (UINT32) NextOp->Common.Value.Integer;
308     WordCount = ACPI_DIV_2 (ByteCount);
309
310     /*
311      * Unicode string must have an even number of bytes and last
312      * word must be zero
313      */
314     if ((!ByteCount)     ||
315          (ByteCount < 4) ||
316          (ByteCount & 1) ||
317         ((UINT16 *) (void *) ByteData)[WordCount - 1] != 0)
318     {
319         return (FALSE);
320     }
321
322     /* For each word, 1st byte must be ascii, 2nd byte must be zero */
323
324     for (i = 0; i < (ByteCount - 2); i += 2)
325     {
326         if ((!ACPI_IS_PRINT (ByteData[i])) ||
327             (ByteData[(ACPI_SIZE) i + 1] != 0))
328         {
329             return (FALSE);
330         }
331     }
332
333     /* Ignore the Size argument in the disassembly of this buffer op */
334
335     SizeOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
336     return (TRUE);
337 }
338
339
340 /*******************************************************************************
341  *
342  * FUNCTION:    AcpiDmIsStringBuffer
343  *
344  * PARAMETERS:  Op              - Buffer Object to be examined
345  *
346  * RETURN:      TRUE if buffer contains a ASCII string, FALSE otherwise
347  *
348  * DESCRIPTION: Determine if a buffer Op contains a ASCII string
349  *
350  ******************************************************************************/
351
352 BOOLEAN
353 AcpiDmIsStringBuffer (
354     ACPI_PARSE_OBJECT       *Op)
355 {
356     UINT8                   *ByteData;
357     UINT32                  ByteCount;
358     ACPI_PARSE_OBJECT       *SizeOp;
359     ACPI_PARSE_OBJECT       *NextOp;
360     UINT32                  i;
361
362
363     /* Buffer size is the buffer argument */
364
365     SizeOp = Op->Common.Value.Arg;
366
367     /* Next, the initializer byte list to examine */
368
369     NextOp = SizeOp->Common.Next;
370     if (!NextOp)
371     {
372         return (FALSE);
373     }
374
375     /* Extract the byte list info */
376
377     ByteData = NextOp->Named.Data;
378     ByteCount = (UINT32) NextOp->Common.Value.Integer;
379
380     /* Last byte must be the null terminator */
381
382     if ((!ByteCount)     ||
383          (ByteCount < 2) ||
384          (ByteData[ByteCount-1] != 0))
385     {
386         return (FALSE);
387     }
388
389     for (i = 0; i < (ByteCount - 1); i++)
390     {
391         /* TBD: allow some escapes (non-ascii chars).
392          * they will be handled in the string output routine
393          */
394
395         if (!ACPI_IS_PRINT (ByteData[i]))
396         {
397             return (FALSE);
398         }
399     }
400
401     return (TRUE);
402 }
403
404
405 /*******************************************************************************
406  *
407  * FUNCTION:    AcpiDmUnicode
408  *
409  * PARAMETERS:  Op              - Byte List op containing Unicode string
410  *
411  * RETURN:      None
412  *
413  * DESCRIPTION: Dump Unicode string as a standard ASCII string.  (Remove
414  *              the extra zero bytes).
415  *
416  ******************************************************************************/
417
418 static void
419 AcpiDmUnicode (
420     ACPI_PARSE_OBJECT       *Op)
421 {
422     UINT16                  *WordData;
423     UINT32                  WordCount;
424     UINT32                  i;
425
426
427     /* Extract the buffer info as a WORD buffer */
428
429     WordData = ACPI_CAST_PTR (UINT16, Op->Named.Data);
430     WordCount = ACPI_DIV_2 (((UINT32) Op->Common.Value.Integer));
431
432
433     AcpiOsPrintf ("\"");
434
435     /* Write every other byte as an ASCII character */
436
437     for (i = 0; i < (WordCount - 1); i++)
438     {
439         AcpiOsPrintf ("%c", (int) WordData[i]);
440     }
441
442     AcpiOsPrintf ("\")");
443 }
444
445
446 /*******************************************************************************
447  *
448  * FUNCTION:    AcpiDmIsEisaIdElement
449  *
450  * PARAMETERS:  Op              - Op to be examined
451  *
452  * RETURN:      None
453  *
454  * DESCRIPTION: Determine if an Op (argument to _HID or _CID) can be converted
455  *              to an EISA ID.
456  *
457  ******************************************************************************/
458
459 static void
460 AcpiDmIsEisaIdElement (
461     ACPI_PARSE_OBJECT       *Op)
462 {
463     UINT32                  BigEndianId;
464     UINT32                  Prefix[3];
465     UINT32                  i;
466
467
468     /* The parameter must be either a word or a dword */
469
470     if ((Op->Common.AmlOpcode != AML_DWORD_OP) &&
471         (Op->Common.AmlOpcode != AML_WORD_OP))
472     {
473         return;
474     }
475
476     /* Swap from little-endian to big-endian to simplify conversion */
477
478     BigEndianId = AcpiUtDwordByteSwap ((UINT32) Op->Common.Value.Integer);
479
480     /* Create the 3 leading ASCII letters */
481
482     Prefix[0] = ((BigEndianId >> 26) & 0x1F) + 0x40;
483     Prefix[1] = ((BigEndianId >> 21) & 0x1F) + 0x40;
484     Prefix[2] = ((BigEndianId >> 16) & 0x1F) + 0x40;
485
486     /* Verify that all 3 are ascii and alpha */
487
488     for (i = 0; i < 3; i++)
489     {
490         if (!ACPI_IS_ASCII (Prefix[i]) ||
491             !ACPI_IS_ALPHA (Prefix[i]))
492         {
493             return;
494         }
495     }
496
497     /* OK - mark this node as convertable to an EISA ID */
498
499     Op->Common.DisasmOpcode = ACPI_DASM_EISAID;
500 }
501
502
503 /*******************************************************************************
504  *
505  * FUNCTION:    AcpiDmIsEisaId
506  *
507  * PARAMETERS:  Op              - Op to be examined
508  *
509  * RETURN:      None
510  *
511  * DESCRIPTION: Determine if a Name() Op can be converted to an EisaId.
512  *
513  ******************************************************************************/
514
515 void
516 AcpiDmIsEisaId (
517     ACPI_PARSE_OBJECT       *Op)
518 {
519     UINT32                  Name;
520     ACPI_PARSE_OBJECT       *NextOp;
521
522
523     /* Get the NameSegment */
524
525     Name = AcpiPsGetName (Op);
526     if (!Name)
527     {
528         return;
529     }
530
531     NextOp = AcpiPsGetDepthNext (NULL, Op);
532     if (!NextOp)
533     {
534         return;
535     }
536
537     /* Check for _HID - has one argument */
538
539     if (ACPI_COMPARE_NAME (&Name, METHOD_NAME__HID))
540     {
541         AcpiDmIsEisaIdElement (NextOp);
542         return;
543     }
544
545     /* Exit if not _CID */
546
547     if (!ACPI_COMPARE_NAME (&Name, METHOD_NAME__CID))
548     {
549         return;
550     }
551
552     /* _CID can contain a single argument or a package */
553
554     if (NextOp->Common.AmlOpcode != AML_PACKAGE_OP)
555     {
556         AcpiDmIsEisaIdElement (NextOp);
557         return;
558     }
559
560     /* _CID with Package: get the package length */
561
562     NextOp = AcpiPsGetDepthNext (NULL, NextOp);
563
564     /* Don't need to use the length, just walk the peer list */
565
566     NextOp = NextOp->Common.Next;
567     while (NextOp)
568     {
569         AcpiDmIsEisaIdElement (NextOp);
570         NextOp = NextOp->Common.Next;
571     }
572 }
573
574
575 /*******************************************************************************
576  *
577  * FUNCTION:    AcpiDmEisaId
578  *
579  * PARAMETERS:  EncodedId       - Raw encoded EISA ID.
580  *
581  * RETURN:      None
582  *
583  * DESCRIPTION: Convert an encoded EISAID back to the original ASCII String.
584  *
585  ******************************************************************************/
586
587 void
588 AcpiDmEisaId (
589     UINT32                  EncodedId)
590 {
591     UINT32                  BigEndianId;
592
593
594     /* Swap from little-endian to big-endian to simplify conversion */
595
596     BigEndianId = AcpiUtDwordByteSwap (EncodedId);
597
598
599     /* Split to form "AAANNNN" string */
600
601     AcpiOsPrintf ("EisaId (\"%c%c%c%4.4X\")",
602
603         /* Three Alpha characters (AAA), 5 bits each */
604
605         (int) ((BigEndianId >> 26) & 0x1F) + 0x40,
606         (int) ((BigEndianId >> 21) & 0x1F) + 0x40,
607         (int) ((BigEndianId >> 16) & 0x1F) + 0x40,
608
609         /* Numeric part (NNNN) is simply the lower 16 bits */
610
611         (UINT32) (BigEndianId & 0xFFFF));
612 }
613
614 #endif