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