xref: /freebsd/sys/contrib/dev/acpica/components/disassembler/dmbuffer.c (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
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 - 2020, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acutils.h>
155 #include <contrib/dev/acpica/include/acdisasm.h>
156 #include <contrib/dev/acpica/include/acparser.h>
157 #include <contrib/dev/acpica/include/amlcode.h>
158 #include <contrib/dev/acpica/include/acinterp.h>
159 
160 
161 #define _COMPONENT          ACPI_CA_DEBUGGER
162         ACPI_MODULE_NAME    ("dmbuffer")
163 
164 /* Local prototypes */
165 
166 static void
167 AcpiDmUuid (
168     ACPI_PARSE_OBJECT       *Op);
169 
170 static void
171 AcpiDmUnicode (
172     ACPI_PARSE_OBJECT       *Op);
173 
174 static void
175 AcpiDmGetHardwareIdType (
176     ACPI_PARSE_OBJECT       *Op);
177 
178 static void
179 AcpiDmPldBuffer (
180     UINT32                  Level,
181     UINT8                   *ByteData,
182     UINT32                  ByteCount);
183 
184 static const char *
185 AcpiDmFindNameByIndex (
186     UINT64                  Index,
187     const char              **List);
188 
189 
190 #define ACPI_BUFFER_BYTES_PER_LINE      8
191 
192 
193 /*******************************************************************************
194  *
195  * FUNCTION:    AcpiDmDisasmByteList
196  *
197  * PARAMETERS:  Level               - Current source code indentation level
198  *              ByteData            - Pointer to the byte list
199  *              ByteCount           - Length of the byte list
200  *
201  * RETURN:      None
202  *
203  * DESCRIPTION: Dump an AML "ByteList" in Hex format. 8 bytes per line, prefixed
204  *              with the hex buffer offset.
205  *
206  ******************************************************************************/
207 
208 void
209 AcpiDmDisasmByteList (
210     UINT32                  Level,
211     UINT8                   *ByteData,
212     UINT32                  ByteCount)
213 {
214     UINT32                  i;
215     UINT32                  j;
216     UINT32                  CurrentIndex;
217     UINT8                   BufChar;
218 
219 
220     if (!ByteCount)
221     {
222         return;
223     }
224 
225     for (i = 0; i < ByteCount; i += ACPI_BUFFER_BYTES_PER_LINE)
226     {
227         /* Line indent and offset prefix for each new line */
228 
229         AcpiDmIndent (Level);
230         if (ByteCount > ACPI_BUFFER_BYTES_PER_LINE)
231         {
232             AcpiOsPrintf ("/* %04X */ ", i);
233         }
234 
235         /* Dump the actual hex values */
236 
237         for (j = 0; j < ACPI_BUFFER_BYTES_PER_LINE; j++)
238         {
239             CurrentIndex = i + j;
240             if (CurrentIndex >= ByteCount)
241             {
242                 /* Dump fill spaces */
243 
244                 AcpiOsPrintf ("      ");
245                 continue;
246             }
247 
248             AcpiOsPrintf (" 0x%2.2X", ByteData[CurrentIndex]);
249 
250             /* Add comma if there are more bytes to display */
251 
252             if (CurrentIndex < (ByteCount - 1))
253             {
254                 AcpiOsPrintf (",");
255             }
256             else
257             {
258                 AcpiOsPrintf (" ");
259             }
260         }
261 
262         /* Dump the ASCII equivalents within a comment */
263 
264         AcpiOsPrintf ("  // ");
265         for (j = 0; j < ACPI_BUFFER_BYTES_PER_LINE; j++)
266         {
267             CurrentIndex = i + j;
268             if (CurrentIndex >= ByteCount)
269             {
270                 break;
271             }
272 
273             BufChar = ByteData[CurrentIndex];
274             if (isprint (BufChar))
275             {
276                 AcpiOsPrintf ("%c", BufChar);
277             }
278             else
279             {
280                 AcpiOsPrintf (".");
281             }
282         }
283 
284         /* Finished with this line */
285 
286         AcpiOsPrintf ("\n");
287     }
288 }
289 
290 
291 /*******************************************************************************
292  *
293  * FUNCTION:    AcpiDmByteList
294  *
295  * PARAMETERS:  Info            - Parse tree walk info
296  *              Op              - Byte list op
297  *
298  * RETURN:      None
299  *
300  * DESCRIPTION: Dump a buffer byte list, handling the various types of buffers.
301  *              Buffer type must be already set in the Op DisasmOpcode.
302  *
303  ******************************************************************************/
304 
305 void
306 AcpiDmByteList (
307     ACPI_OP_WALK_INFO       *Info,
308     ACPI_PARSE_OBJECT       *Op)
309 {
310     UINT8                   *ByteData;
311     UINT32                  ByteCount;
312 
313 
314     ByteData = Op->Named.Data;
315     ByteCount = (UINT32) Op->Common.Value.Integer;
316 
317     /*
318      * The byte list belongs to a buffer, and can be produced by either
319      * a ResourceTemplate, Unicode, quoted string, or a plain byte list.
320      */
321     switch (Op->Common.Parent->Common.DisasmOpcode)
322     {
323     case ACPI_DASM_RESOURCE:
324 
325         AcpiDmResourceTemplate (
326             Info, Op->Common.Parent, ByteData, ByteCount);
327         break;
328 
329     case ACPI_DASM_STRING:
330 
331         AcpiDmIndent (Info->Level);
332         AcpiUtPrintString ((char *) ByteData, ACPI_UINT16_MAX);
333         AcpiOsPrintf ("\n");
334         break;
335 
336     case ACPI_DASM_UUID:
337 
338         AcpiDmUuid (Op);
339         break;
340 
341     case ACPI_DASM_UNICODE:
342 
343         AcpiDmUnicode (Op);
344         break;
345 
346     case ACPI_DASM_PLD_METHOD:
347 #if 0
348         AcpiDmDisasmByteList (Info->Level, ByteData, ByteCount);
349 #endif
350         AcpiDmPldBuffer (Info->Level, ByteData, ByteCount);
351         break;
352 
353     case ACPI_DASM_BUFFER:
354     default:
355         /*
356          * Not a resource, string, or unicode string.
357          * Just dump the buffer
358          */
359         AcpiDmDisasmByteList (Info->Level, ByteData, ByteCount);
360         break;
361     }
362 }
363 
364 
365 /*******************************************************************************
366  *
367  * FUNCTION:    AcpiDmIsUuidBuffer
368  *
369  * PARAMETERS:  Op              - Buffer Object to be examined
370  *
371  * RETURN:      TRUE if buffer contains a UUID
372  *
373  * DESCRIPTION: Determine if a buffer Op contains a UUID
374  *
375  * To help determine whether the buffer is a UUID versus a raw data buffer,
376  * there a are a couple bytes we can look at:
377  *
378  *    xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
379  *
380  * The variant covered by the UUID specification is indicated by the two most
381  * significant bits of N being 1 0 (i.e., the hexadecimal N will always be
382  * 8, 9, A, or B).
383  *
384  * The variant covered by the UUID specification has five versions. For this
385  * variant, the four bits of M indicates the UUID version (i.e., the
386  * hexadecimal M will be either 1, 2, 3, 4, or 5).
387  *
388  ******************************************************************************/
389 
390 BOOLEAN
391 AcpiDmIsUuidBuffer (
392     ACPI_PARSE_OBJECT       *Op)
393 {
394     UINT8                   *ByteData;
395     UINT32                  ByteCount;
396     ACPI_PARSE_OBJECT       *SizeOp;
397     ACPI_PARSE_OBJECT       *NextOp;
398 
399 
400     /* Buffer size is the buffer argument */
401 
402     SizeOp = Op->Common.Value.Arg;
403 
404     /* Next, the initializer byte list to examine */
405 
406     NextOp = SizeOp->Common.Next;
407     if (!NextOp)
408     {
409         return (FALSE);
410     }
411 
412     /* Extract the byte list info */
413 
414     ByteData = NextOp->Named.Data;
415     ByteCount = (UINT32) NextOp->Common.Value.Integer;
416 
417     /* Byte count must be exactly 16 */
418 
419     if (ByteCount != UUID_BUFFER_LENGTH)
420     {
421         return (FALSE);
422     }
423 
424     /* Check for valid "M" and "N" values (see function header above) */
425 
426     if (((ByteData[7] & 0xF0) == 0x00) || /* M={1,2,3,4,5} */
427         ((ByteData[7] & 0xF0) > 0x50)  ||
428         ((ByteData[8] & 0xF0) < 0x80)  || /* N={8,9,A,B} */
429         ((ByteData[8] & 0xF0) > 0xB0))
430     {
431         return (FALSE);
432     }
433 
434     /* Ignore the Size argument in the disassembly of this buffer op */
435 
436     SizeOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
437     return (TRUE);
438 }
439 
440 
441 /*******************************************************************************
442  *
443  * FUNCTION:    AcpiDmUuid
444  *
445  * PARAMETERS:  Op              - Byte List op containing a UUID
446  *
447  * RETURN:      None
448  *
449  * DESCRIPTION: Dump a buffer containing a UUID as a standard ASCII string.
450  *
451  * Output Format:
452  * In its canonical form, the UUID is represented by a string containing 32
453  * lowercase hexadecimal digits, displayed in 5 groups separated by hyphens.
454  * The complete form is 8-4-4-4-12 for a total of 36 characters (32
455  * alphanumeric characters representing hex digits and 4 hyphens). In bytes,
456  * 4-2-2-2-6. Example:
457  *
458  *    ToUUID ("107ededd-d381-4fd7-8da9-08e9a6c79644")
459  *
460  ******************************************************************************/
461 
462 static void
463 AcpiDmUuid (
464     ACPI_PARSE_OBJECT       *Op)
465 {
466     UINT8                   *Data;
467     const char              *Description;
468 
469 
470     Data = ACPI_CAST_PTR (UINT8, Op->Named.Data);
471 
472     /* Emit the 36-byte UUID string in the proper format/order */
473 
474     AcpiOsPrintf (
475         "\"%2.2x%2.2x%2.2x%2.2x-"
476         "%2.2x%2.2x-"
477         "%2.2x%2.2x-"
478         "%2.2x%2.2x-"
479         "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x\")",
480         Data[3], Data[2], Data[1], Data[0],
481         Data[5], Data[4],
482         Data[7], Data[6],
483         Data[8], Data[9],
484         Data[10], Data[11], Data[12], Data[13], Data[14], Data[15]);
485 
486     /* Dump the UUID description string if available */
487 
488     Description = AcpiAhMatchUuid (Data);
489     if (Description)
490     {
491         AcpiOsPrintf (" /* %s */", Description);
492     }
493 }
494 
495 
496 /*******************************************************************************
497  *
498  * FUNCTION:    AcpiDmIsUnicodeBuffer
499  *
500  * PARAMETERS:  Op              - Buffer Object to be examined
501  *
502  * RETURN:      TRUE if buffer contains a UNICODE string
503  *
504  * DESCRIPTION: Determine if a buffer Op contains a Unicode string
505  *
506  ******************************************************************************/
507 
508 BOOLEAN
509 AcpiDmIsUnicodeBuffer (
510     ACPI_PARSE_OBJECT       *Op)
511 {
512     UINT8                   *ByteData;
513     UINT32                  ByteCount;
514     UINT32                  WordCount;
515     ACPI_PARSE_OBJECT       *SizeOp;
516     ACPI_PARSE_OBJECT       *NextOp;
517     UINT32                  i;
518 
519 
520     /* Buffer size is the buffer argument */
521 
522     SizeOp = Op->Common.Value.Arg;
523 
524     /* Next, the initializer byte list to examine */
525 
526     NextOp = SizeOp->Common.Next;
527     if (!NextOp)
528     {
529         return (FALSE);
530     }
531 
532     /* Extract the byte list info */
533 
534     ByteData = NextOp->Named.Data;
535     ByteCount = (UINT32) NextOp->Common.Value.Integer;
536     WordCount = ACPI_DIV_2 (ByteCount);
537 
538     /*
539      * Unicode string must have an even number of bytes and last
540      * word must be zero
541      */
542     if ((!ByteCount)     ||
543          (ByteCount < 4) ||
544          (ByteCount & 1) ||
545         ((UINT16 *) (void *) ByteData)[WordCount - 1] != 0)
546     {
547         return (FALSE);
548     }
549 
550     /*
551      * For each word, 1st byte must be printable ascii, and the
552      * 2nd byte must be zero. This does not allow for escape
553      * sequences, but it is the most secure way to detect a
554      * unicode string.
555      */
556     for (i = 0; i < (ByteCount - 2); i += 2)
557     {
558         if ((ByteData[i] == 0) ||
559             !(isprint (ByteData[i])) ||
560             (ByteData[(ACPI_SIZE) i + 1] != 0))
561         {
562             return (FALSE);
563         }
564     }
565 
566     /* Ignore the Size argument in the disassembly of this buffer op */
567 
568     SizeOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
569     return (TRUE);
570 }
571 
572 
573 /*******************************************************************************
574  *
575  * FUNCTION:    AcpiDmIsStringBuffer
576  *
577  * PARAMETERS:  Op              - Buffer Object to be examined
578  *
579  * RETURN:      TRUE if buffer contains a ASCII string, FALSE otherwise
580  *
581  * DESCRIPTION: Determine if a buffer Op contains a ASCII string
582  *
583  ******************************************************************************/
584 
585 BOOLEAN
586 AcpiDmIsStringBuffer (
587     ACPI_PARSE_OBJECT       *Op)
588 {
589     UINT8                   *ByteData;
590     UINT32                  ByteCount;
591     ACPI_PARSE_OBJECT       *SizeOp;
592     ACPI_PARSE_OBJECT       *NextOp;
593     UINT32                  i;
594 
595 
596     /* Buffer size is the buffer argument */
597 
598     SizeOp = Op->Common.Value.Arg;
599 
600     /* Next, the initializer byte list to examine */
601 
602     NextOp = SizeOp->Common.Next;
603     if (!NextOp)
604     {
605         return (FALSE);
606     }
607 
608     /* Extract the byte list info */
609 
610     ByteData = NextOp->Named.Data;
611     ByteCount = (UINT32) NextOp->Common.Value.Integer;
612 
613     /* Last byte must be the null terminator */
614 
615     if ((!ByteCount)     ||
616          (ByteCount < 2) ||
617          (ByteData[ByteCount-1] != 0))
618     {
619         return (FALSE);
620     }
621 
622     /*
623      * Check for a possible standalone resource EndTag, ignore it
624      * here. However, this sequence is also the string "Y", but
625      * this seems rare enough to be acceptable.
626      */
627     if ((ByteCount == 2) && (ByteData[0] == 0x79))
628     {
629         return (FALSE);
630     }
631 
632     /* Check all bytes for ASCII */
633 
634     for (i = 0; i < (ByteCount - 1); i++)
635     {
636         /*
637          * TBD: allow some escapes (non-ascii chars).
638          * they will be handled in the string output routine
639          */
640 
641         /* Not a string if not printable ascii */
642 
643         if (!isprint (ByteData[i]))
644         {
645             return (FALSE);
646         }
647     }
648 
649     return (TRUE);
650 }
651 
652 
653 /*******************************************************************************
654  *
655  * FUNCTION:    AcpiDmIsPldBuffer
656  *
657  * PARAMETERS:  Op                  - Buffer Object to be examined
658  *
659  * RETURN:      TRUE if buffer appears to contain data produced via the
660  *              ToPLD macro, FALSE otherwise
661  *
662  * DESCRIPTION: Determine if a buffer Op contains a _PLD structure
663  *
664  ******************************************************************************/
665 
666 BOOLEAN
667 AcpiDmIsPldBuffer (
668     ACPI_PARSE_OBJECT       *Op)
669 {
670     ACPI_NAMESPACE_NODE     *Node;
671     ACPI_PARSE_OBJECT       *SizeOp;
672     ACPI_PARSE_OBJECT       *ByteListOp;
673     ACPI_PARSE_OBJECT       *ParentOp;
674     UINT64                  BufferSize;
675     UINT64                  InitializerSize;
676 
677 
678     /*
679      * Get the BufferSize argument - Buffer(BufferSize)
680      * If the buffer was generated by the ToPld macro, it must
681      * be a BYTE constant.
682      */
683     SizeOp = Op->Common.Value.Arg;
684     if (SizeOp->Common.AmlOpcode != AML_BYTE_OP)
685     {
686         return (FALSE);
687     }
688 
689     /* Check the declared BufferSize, two possibilities */
690 
691     BufferSize = SizeOp->Common.Value.Integer;
692     if ((BufferSize != ACPI_PLD_REV1_BUFFER_SIZE) &&
693         (BufferSize != ACPI_PLD_REV2_BUFFER_SIZE))
694     {
695         return (FALSE);
696     }
697 
698     /*
699      * Check the initializer list length. This is the actual
700      * number of bytes in the buffer as counted by the AML parser.
701      * The declared BufferSize can be larger than the actual length.
702      * However, for the ToPLD macro, the BufferSize will be the same
703      * as the initializer list length.
704      */
705     ByteListOp = SizeOp->Common.Next;
706     if (!ByteListOp)
707     {
708         return (FALSE); /* Zero-length buffer case */
709     }
710 
711     InitializerSize = ByteListOp->Common.Value.Integer;
712     if ((InitializerSize != ACPI_PLD_REV1_BUFFER_SIZE) &&
713         (InitializerSize != ACPI_PLD_REV2_BUFFER_SIZE))
714     {
715         return (FALSE);
716     }
717 
718     /* Final size check */
719 
720     if (BufferSize != InitializerSize)
721     {
722         return (FALSE);
723     }
724 
725     /* Now examine the buffer parent */
726 
727     ParentOp = Op->Common.Parent;
728     if (!ParentOp)
729     {
730         return (FALSE);
731     }
732 
733     /* Check for form: Name(_PLD, Buffer() {}). Not legal, however */
734 
735     if (ParentOp->Common.AmlOpcode == AML_NAME_OP)
736     {
737         Node = ParentOp->Common.Node;
738 
739         if (ACPI_COMPARE_NAMESEG (Node->Name.Ascii, METHOD_NAME__PLD))
740         {
741             /* Ignore the Size argument in the disassembly of this buffer op */
742 
743             SizeOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
744             return (TRUE);
745         }
746 
747         return (FALSE);
748     }
749 
750     /*
751      * Check for proper form: Name(_PLD, Package() {ToPLD()})
752      *
753      * Note: All other forms such as
754      *      Return (Package() {ToPLD()})
755      *      Local0 = ToPLD()
756      * etc. are not converted back to the ToPLD macro, because
757      * there is really no deterministic way to disassemble the buffer
758      * back to the ToPLD macro, other than trying to find the "_PLD"
759      * name
760      */
761     if (ParentOp->Common.AmlOpcode == AML_PACKAGE_OP)
762     {
763         ParentOp = ParentOp->Common.Parent;
764         if (!ParentOp)
765         {
766             return (FALSE);
767         }
768 
769         if (ParentOp->Common.AmlOpcode == AML_NAME_OP)
770         {
771             Node = ParentOp->Common.Node;
772 
773             if (ACPI_COMPARE_NAMESEG (Node->Name.Ascii, METHOD_NAME__PLD))
774             {
775                 /* Ignore the Size argument in the disassembly of this buffer op */
776 
777                 SizeOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
778                 return (TRUE);
779             }
780         }
781     }
782 
783     return (FALSE);
784 }
785 
786 
787 /*******************************************************************************
788  *
789  * FUNCTION:    AcpiDmFindNameByIndex
790  *
791  * PARAMETERS:  Index               - Index of array to check
792  *              List                - Array to reference
793  *
794  * RETURN:      String from List or empty string
795  *
796  * DESCRIPTION: Finds and returns the char string located at the given index
797  *              position in List.
798  *
799  ******************************************************************************/
800 
801 static const char *
802 AcpiDmFindNameByIndex (
803     UINT64                  Index,
804     const char              **List)
805 {
806     const char              *NameString;
807     UINT32                  i;
808 
809 
810     /* Bounds check */
811 
812     NameString = List[0];
813     i = 0;
814 
815     while (NameString)
816     {
817         i++;
818         NameString = List[i];
819     }
820 
821     if (Index >= i)
822     {
823         /* TBD: Add error msg */
824 
825         return ("");
826     }
827 
828     return (List[Index]);
829 }
830 
831 
832 /*******************************************************************************
833  *
834  * FUNCTION:    AcpiDmPldBuffer
835  *
836  * PARAMETERS:  Level               - Current source code indentation level
837  *              ByteData            - Pointer to the byte list
838  *              ByteCount           - Length of the byte list
839  *
840  * RETURN:      None
841  *
842  * DESCRIPTION: Dump and format the contents of a _PLD buffer object
843  *
844  ******************************************************************************/
845 
846 #define ACPI_PLD_OUTPUT08   "%*.s%-22s = 0x%X,\n", ACPI_MUL_4 (Level), " "
847 #define ACPI_PLD_OUTPUT08P  "%*.s%-22s = 0x%X)\n", ACPI_MUL_4 (Level), " "
848 #define ACPI_PLD_OUTPUT16   "%*.s%-22s = 0x%X,\n", ACPI_MUL_4 (Level), " "
849 #define ACPI_PLD_OUTPUT16P  "%*.s%-22s = 0x%X)\n", ACPI_MUL_4 (Level), " "
850 #define ACPI_PLD_OUTPUT24   "%*.s%-22s = 0x%X,\n", ACPI_MUL_4 (Level), " "
851 #define ACPI_PLD_OUTPUTSTR  "%*.s%-22s = \"%s\",\n", ACPI_MUL_4 (Level), " "
852 
853 static void
854 AcpiDmPldBuffer (
855     UINT32                  Level,
856     UINT8                   *ByteData,
857     UINT32                  ByteCount)
858 {
859     ACPI_PLD_INFO           *PldInfo;
860     ACPI_STATUS             Status;
861 
862 
863     /* Check for valid byte count */
864 
865     if (ByteCount < ACPI_PLD_REV1_BUFFER_SIZE)
866     {
867         return;
868     }
869 
870     /* Convert _PLD buffer to local _PLD struct */
871 
872     Status = AcpiDecodePldBuffer (ByteData, ByteCount, &PldInfo);
873     if (ACPI_FAILURE (Status))
874     {
875         return;
876     }
877 
878     AcpiOsPrintf ("\n");
879 
880     /* First 32-bit dword */
881 
882     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Revision", PldInfo->Revision);
883     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_IgnoreColor", PldInfo->IgnoreColor);
884     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Red", PldInfo->Red);
885     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Green", PldInfo->Green);
886     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Blue", PldInfo->Blue);
887 
888     /* Second 32-bit dword */
889 
890     AcpiOsPrintf (ACPI_PLD_OUTPUT16,  "PLD_Width", PldInfo->Width);
891     AcpiOsPrintf (ACPI_PLD_OUTPUT16,  "PLD_Height", PldInfo->Height);
892 
893     /* Third 32-bit dword */
894 
895     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_UserVisible", PldInfo->UserVisible);
896     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Dock", PldInfo->Dock);
897     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Lid", PldInfo->Lid);
898     AcpiOsPrintf (ACPI_PLD_OUTPUTSTR, "PLD_Panel",
899         AcpiDmFindNameByIndex(PldInfo->Panel, AcpiGbl_PldPanelList));
900 
901     AcpiOsPrintf (ACPI_PLD_OUTPUTSTR, "PLD_VerticalPosition",
902         AcpiDmFindNameByIndex(PldInfo->VerticalPosition, AcpiGbl_PldVerticalPositionList));
903 
904     AcpiOsPrintf (ACPI_PLD_OUTPUTSTR, "PLD_HorizontalPosition",
905         AcpiDmFindNameByIndex(PldInfo->HorizontalPosition, AcpiGbl_PldHorizontalPositionList));
906 
907     AcpiOsPrintf (ACPI_PLD_OUTPUTSTR, "PLD_Shape",
908         AcpiDmFindNameByIndex(PldInfo->Shape, AcpiGbl_PldShapeList));
909     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_GroupOrientation", PldInfo->GroupOrientation);
910 
911     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_GroupToken", PldInfo->GroupToken);
912     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_GroupPosition", PldInfo->GroupPosition);
913     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Bay", PldInfo->Bay);
914 
915     /* Fourth 32-bit dword */
916 
917     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Ejectable", PldInfo->Ejectable);
918     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_EjectRequired", PldInfo->OspmEjectRequired);
919     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_CabinetNumber", PldInfo->CabinetNumber);
920     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_CardCageNumber", PldInfo->CardCageNumber);
921     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Reference", PldInfo->Reference);
922     AcpiOsPrintf (ACPI_PLD_OUTPUT08,  "PLD_Rotation", PldInfo->Rotation);
923 
924     if (ByteCount >= ACPI_PLD_REV2_BUFFER_SIZE)
925     {
926         AcpiOsPrintf (ACPI_PLD_OUTPUT08, "PLD_Order", PldInfo->Order);
927 
928         /* Fifth 32-bit dword */
929 
930         AcpiOsPrintf (ACPI_PLD_OUTPUT16,  "PLD_VerticalOffset", PldInfo->VerticalOffset);
931         AcpiOsPrintf (ACPI_PLD_OUTPUT16P, "PLD_HorizontalOffset", PldInfo->HorizontalOffset);
932     }
933     else /* Rev 1 buffer */
934     {
935         AcpiOsPrintf (ACPI_PLD_OUTPUT08P, "PLD_Order", PldInfo->Order);
936     }
937 
938     ACPI_FREE (PldInfo);
939 }
940 
941 
942 /*******************************************************************************
943  *
944  * FUNCTION:    AcpiDmUnicode
945  *
946  * PARAMETERS:  Op              - Byte List op containing Unicode string
947  *
948  * RETURN:      None
949  *
950  * DESCRIPTION: Dump Unicode string as a standard ASCII string. (Remove
951  *              the extra zero bytes).
952  *
953  ******************************************************************************/
954 
955 static void
956 AcpiDmUnicode (
957     ACPI_PARSE_OBJECT       *Op)
958 {
959     UINT16                  *WordData;
960     UINT32                  WordCount;
961     UINT32                  i;
962     int                     OutputValue;
963 
964 
965     /* Extract the buffer info as a WORD buffer */
966 
967     WordData = ACPI_CAST_PTR (UINT16, Op->Named.Data);
968     WordCount = ACPI_DIV_2 (((UINT32) Op->Common.Value.Integer));
969 
970     /* Write every other byte as an ASCII character */
971 
972     AcpiOsPrintf ("\"");
973     for (i = 0; i < (WordCount - 1); i++)
974     {
975         OutputValue = (int) WordData[i];
976 
977         /* Handle values that must be escaped */
978 
979         if ((OutputValue == '\"') ||
980             (OutputValue == '\\'))
981         {
982             AcpiOsPrintf ("\\%c", OutputValue);
983         }
984         else if (!isprint (OutputValue))
985         {
986             AcpiOsPrintf ("\\x%2.2X", OutputValue);
987         }
988         else
989         {
990             AcpiOsPrintf ("%c", OutputValue);
991         }
992     }
993 
994     AcpiOsPrintf ("\")");
995 }
996 
997 
998 /*******************************************************************************
999  *
1000  * FUNCTION:    AcpiDmGetHardwareIdType
1001  *
1002  * PARAMETERS:  Op              - Op to be examined
1003  *
1004  * RETURN:      None
1005  *
1006  * DESCRIPTION: Determine the type of the argument to a _HID or _CID
1007  *              1) Strings are allowed
1008  *              2) If Integer, determine if it is a valid EISAID
1009  *
1010  ******************************************************************************/
1011 
1012 static void
1013 AcpiDmGetHardwareIdType (
1014     ACPI_PARSE_OBJECT       *Op)
1015 {
1016     UINT32                  BigEndianId;
1017     UINT32                  Prefix[3];
1018     UINT32                  i;
1019 
1020 
1021     switch (Op->Common.AmlOpcode)
1022     {
1023     case AML_STRING_OP:
1024 
1025         /* Mark this string as an _HID/_CID string */
1026 
1027         Op->Common.DisasmOpcode = ACPI_DASM_HID_STRING;
1028         break;
1029 
1030     case AML_WORD_OP:
1031     case AML_DWORD_OP:
1032 
1033         /* Determine if a Word/Dword is a valid encoded EISAID */
1034 
1035         /* Swap from little-endian to big-endian to simplify conversion */
1036 
1037         BigEndianId = AcpiUtDwordByteSwap ((UINT32) Op->Common.Value.Integer);
1038 
1039         /* Create the 3 leading ASCII letters */
1040 
1041         Prefix[0] = ((BigEndianId >> 26) & 0x1F) + 0x40;
1042         Prefix[1] = ((BigEndianId >> 21) & 0x1F) + 0x40;
1043         Prefix[2] = ((BigEndianId >> 16) & 0x1F) + 0x40;
1044 
1045         /* Verify that all 3 are ascii and alpha */
1046 
1047         for (i = 0; i < 3; i++)
1048         {
1049             if (!ACPI_IS_ASCII (Prefix[i]) ||
1050                 !isalpha (Prefix[i]))
1051             {
1052                 return;
1053             }
1054         }
1055 
1056         /* Mark this node as convertible to an EISA ID string */
1057 
1058         Op->Common.DisasmOpcode = ACPI_DASM_EISAID;
1059         break;
1060 
1061     default:
1062         break;
1063     }
1064 }
1065 
1066 
1067 /*******************************************************************************
1068  *
1069  * FUNCTION:    AcpiDmCheckForHardwareId
1070  *
1071  * PARAMETERS:  Op              - Op to be examined
1072  *
1073  * RETURN:      None
1074  *
1075  * DESCRIPTION: Determine if a Name() Op is a _HID/_CID.
1076  *
1077  ******************************************************************************/
1078 
1079 void
1080 AcpiDmCheckForHardwareId (
1081     ACPI_PARSE_OBJECT       *Op)
1082 {
1083     UINT32                  Name;
1084     ACPI_PARSE_OBJECT       *NextOp;
1085 
1086 
1087     /* Get the NameSegment */
1088 
1089     Name = AcpiPsGetName (Op);
1090     if (!Name)
1091     {
1092         return;
1093     }
1094 
1095     NextOp = AcpiPsGetDepthNext (NULL, Op);
1096     if (!NextOp)
1097     {
1098         return;
1099     }
1100 
1101     /* Check for _HID - has one argument */
1102 
1103     if (ACPI_COMPARE_NAMESEG (&Name, METHOD_NAME__HID))
1104     {
1105         AcpiDmGetHardwareIdType (NextOp);
1106         return;
1107     }
1108 
1109     /* Exit if not _CID */
1110 
1111     if (!ACPI_COMPARE_NAMESEG (&Name, METHOD_NAME__CID))
1112     {
1113         return;
1114     }
1115 
1116     /* _CID can contain a single argument or a package */
1117 
1118     if (NextOp->Common.AmlOpcode != AML_PACKAGE_OP)
1119     {
1120         AcpiDmGetHardwareIdType (NextOp);
1121         return;
1122     }
1123 
1124     /* _CID with Package: get the package length, check all elements */
1125 
1126     NextOp = AcpiPsGetDepthNext (NULL, NextOp);
1127     if (!NextOp)
1128     {
1129         return;
1130     }
1131 
1132     /* Don't need to use the length, just walk the peer list */
1133 
1134     NextOp = NextOp->Common.Next;
1135     while (NextOp)
1136     {
1137         AcpiDmGetHardwareIdType (NextOp);
1138         NextOp = NextOp->Common.Next;
1139     }
1140 }
1141 
1142 
1143 /*******************************************************************************
1144  *
1145  * FUNCTION:    AcpiDmDecompressEisaId
1146  *
1147  * PARAMETERS:  EncodedId       - Raw encoded EISA ID.
1148  *
1149  * RETURN:      None
1150  *
1151  * DESCRIPTION: Convert an encoded EISAID back to the original ASCII String
1152  *              and emit the correct ASL statement. If the ID is known, emit
1153  *              a description of the ID as a comment.
1154  *
1155  ******************************************************************************/
1156 
1157 void
1158 AcpiDmDecompressEisaId (
1159     UINT32                  EncodedId)
1160 {
1161     char                    IdBuffer[ACPI_EISAID_STRING_SIZE];
1162     const AH_DEVICE_ID      *Info;
1163 
1164 
1165     /* Convert EISAID to a string an emit the statement */
1166 
1167     AcpiExEisaIdToString (IdBuffer, EncodedId);
1168     AcpiOsPrintf ("EisaId (\"%s\")", IdBuffer);
1169 
1170     /* If we know about the ID, emit the description */
1171 
1172     Info = AcpiAhMatchHardwareId (IdBuffer);
1173     if (Info)
1174     {
1175         AcpiOsPrintf (" /* %s */", Info->Description);
1176     }
1177 }
1178