1 /*******************************************************************************
2  *
3  * Module Name: dmwalk - AML disassembly tree walk
4  *
5  ******************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, 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 "acparser.h"
155 #include "amlcode.h"
156 #include "acdebug.h"
157 #include "acconvert.h"
158 
159 
160 #ifdef ACPI_DISASSEMBLER
161 
162 #define _COMPONENT          ACPI_CA_DEBUGGER
163         ACPI_MODULE_NAME    ("dmwalk")
164 
165 
166 #define DB_FULL_OP_INFO     "[%4.4s] @%5.5X #%4.4X:  "
167 
168 /* Stub for non-compiler code */
169 
170 #ifndef ACPI_ASL_COMPILER
171 void
172 AcpiDmEmitExternals (
173     void)
174 {
175     return;
176 }
177 
178 void
179 AcpiDmEmitExternal (
180     ACPI_PARSE_OBJECT       *NameOp,
181     ACPI_PARSE_OBJECT       *TypeOp)
182 {
183     return;
184 }
185 #endif
186 
187 /* Local prototypes */
188 
189 static ACPI_STATUS
190 AcpiDmDescendingOp (
191     ACPI_PARSE_OBJECT       *Op,
192     UINT32                  Level,
193     void                    *Context);
194 
195 static ACPI_STATUS
196 AcpiDmAscendingOp (
197     ACPI_PARSE_OBJECT       *Op,
198     UINT32                  Level,
199     void                    *Context);
200 
201 
202 /*******************************************************************************
203  *
204  * FUNCTION:    AcpiDmDisassemble
205  *
206  * PARAMETERS:  WalkState       - Current state
207  *              Origin          - Starting object
208  *              NumOpcodes      - Max number of opcodes to be displayed
209  *
210  * RETURN:      None
211  *
212  * DESCRIPTION: Disassemble parser object and its children. This is the
213  *              main entry point of the disassembler.
214  *
215  ******************************************************************************/
216 
217 void
218 AcpiDmDisassemble (
219     ACPI_WALK_STATE         *WalkState,
220     ACPI_PARSE_OBJECT       *Origin,
221     UINT32                  NumOpcodes)
222 {
223     ACPI_PARSE_OBJECT       *Op = Origin;
224     ACPI_OP_WALK_INFO       Info;
225 
226 
227     if (!Op)
228     {
229         return;
230     }
231 
232     memset (&Info, 0, sizeof (ACPI_OP_WALK_INFO));
233     Info.WalkState = WalkState;
234     Info.StartAml = Op->Common.Aml - sizeof (ACPI_TABLE_HEADER);
235     Info.AmlOffset = Op->Common.Aml - Info.StartAml;
236 
237     AcpiDmWalkParseTree (Op, AcpiDmDescendingOp, AcpiDmAscendingOp, &Info);
238     return;
239 }
240 
241 
242 /*******************************************************************************
243  *
244  * FUNCTION:    AcpiDmWalkParseTree
245  *
246  * PARAMETERS:  Op                      - Root Op object
247  *              DescendingCallback      - Called during tree descent
248  *              AscendingCallback       - Called during tree ascent
249  *              Context                 - To be passed to the callbacks
250  *
251  * RETURN:      Status from callback(s)
252  *
253  * DESCRIPTION: Walk the entire parse tree.
254  *
255  ******************************************************************************/
256 
257 void
258 AcpiDmWalkParseTree (
259     ACPI_PARSE_OBJECT       *Op,
260     ASL_WALK_CALLBACK       DescendingCallback,
261     ASL_WALK_CALLBACK       AscendingCallback,
262     void                    *Context)
263 {
264     BOOLEAN                 NodePreviouslyVisited;
265     ACPI_PARSE_OBJECT       *StartOp = Op;
266     ACPI_STATUS             Status;
267     ACPI_PARSE_OBJECT       *Next;
268     ACPI_OP_WALK_INFO       *Info = Context;
269 
270 
271     Info->Level = 0;
272     NodePreviouslyVisited = FALSE;
273 
274     while (Op)
275     {
276         if (NodePreviouslyVisited)
277         {
278             if (AscendingCallback)
279             {
280                 Status = AscendingCallback (Op, Info->Level, Context);
281                 if (ACPI_FAILURE (Status))
282                 {
283                     return;
284                 }
285             }
286         }
287         else
288         {
289             /* Let the callback process the node */
290 
291             Status = DescendingCallback (Op, Info->Level, Context);
292             if (ACPI_SUCCESS (Status))
293             {
294                 /* Visit children first, once */
295 
296                 Next = AcpiPsGetArg (Op, 0);
297                 if (Next)
298                 {
299                     Info->Level++;
300                     Op = Next;
301                     continue;
302                 }
303             }
304             else if (Status != AE_CTRL_DEPTH)
305             {
306                 /* Exit immediately on any error */
307 
308                 return;
309             }
310         }
311 
312         /* Terminate walk at start op */
313 
314         if (Op == StartOp)
315         {
316             break;
317         }
318 
319         /* No more children, re-visit this node */
320 
321         if (!NodePreviouslyVisited)
322         {
323             NodePreviouslyVisited = TRUE;
324             continue;
325         }
326 
327         /* No more children, visit peers */
328 
329         if (Op->Common.Next)
330         {
331             Op = Op->Common.Next;
332             NodePreviouslyVisited = FALSE;
333         }
334         else
335         {
336             /* No peers, re-visit parent */
337 
338             if (Info->Level != 0 )
339             {
340                 Info->Level--;
341             }
342 
343             Op = Op->Common.Parent;
344             NodePreviouslyVisited = TRUE;
345         }
346     }
347 
348     /* If we get here, the walk completed with no errors */
349 
350     return;
351 }
352 
353 
354 /*******************************************************************************
355  *
356  * FUNCTION:    AcpiDmBlockType
357  *
358  * PARAMETERS:  Op              - Object to be examined
359  *
360  * RETURN:      BlockType - not a block, parens, braces, or even both.
361  *
362  * DESCRIPTION: Type of block for this op (parens or braces)
363  *
364  ******************************************************************************/
365 
366 UINT32
367 AcpiDmBlockType (
368     ACPI_PARSE_OBJECT       *Op)
369 {
370     const ACPI_OPCODE_INFO  *OpInfo;
371 
372 
373     if (!Op)
374     {
375         return (BLOCK_NONE);
376     }
377 
378     switch (Op->Common.AmlOpcode)
379     {
380     case AML_ELSE_OP:
381 
382         return (BLOCK_BRACE);
383 
384     case AML_METHOD_OP:
385     case AML_DEVICE_OP:
386     case AML_SCOPE_OP:
387     case AML_PROCESSOR_OP:
388     case AML_POWER_RESOURCE_OP:
389     case AML_THERMAL_ZONE_OP:
390     case AML_IF_OP:
391     case AML_WHILE_OP:
392     case AML_FIELD_OP:
393     case AML_INDEX_FIELD_OP:
394     case AML_BANK_FIELD_OP:
395 
396         return (BLOCK_PAREN | BLOCK_BRACE);
397 
398     case AML_BUFFER_OP:
399 
400         if ((Op->Common.DisasmOpcode == ACPI_DASM_UNICODE) ||
401             (Op->Common.DisasmOpcode == ACPI_DASM_UUID) ||
402             (Op->Common.DisasmOpcode == ACPI_DASM_PLD_METHOD))
403         {
404             return (BLOCK_NONE);
405         }
406 
407         /*lint -fallthrough */
408 
409     case AML_PACKAGE_OP:
410     case AML_VARIABLE_PACKAGE_OP:
411 
412         return (BLOCK_PAREN | BLOCK_BRACE);
413 
414     case AML_EVENT_OP:
415 
416         return (BLOCK_PAREN);
417 
418     case AML_INT_METHODCALL_OP:
419 
420         if (Op->Common.Parent &&
421             ((Op->Common.Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
422              (Op->Common.Parent->Common.AmlOpcode == AML_VARIABLE_PACKAGE_OP)))
423         {
424             /* This is a reference to a method, not an invocation */
425 
426             return (BLOCK_NONE);
427         }
428 
429         /*lint -fallthrough */
430 
431     default:
432 
433         OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
434         if (OpInfo->Flags & AML_HAS_ARGS)
435         {
436             return (BLOCK_PAREN);
437         }
438 
439         return (BLOCK_NONE);
440     }
441 }
442 
443 
444 /*******************************************************************************
445  *
446  * FUNCTION:    AcpiDmListType
447  *
448  * PARAMETERS:  Op              - Object to be examined
449  *
450  * RETURN:      ListType - has commas or not.
451  *
452  * DESCRIPTION: Type of block for this op (parens or braces)
453  *
454  ******************************************************************************/
455 
456 UINT32
457 AcpiDmListType (
458     ACPI_PARSE_OBJECT       *Op)
459 {
460     const ACPI_OPCODE_INFO  *OpInfo;
461 
462 
463     if (!Op)
464     {
465         return (BLOCK_NONE);
466     }
467 
468     switch (Op->Common.AmlOpcode)
469     {
470 
471     case AML_ELSE_OP:
472     case AML_METHOD_OP:
473     case AML_DEVICE_OP:
474     case AML_SCOPE_OP:
475     case AML_POWER_RESOURCE_OP:
476     case AML_PROCESSOR_OP:
477     case AML_THERMAL_ZONE_OP:
478     case AML_IF_OP:
479     case AML_WHILE_OP:
480     case AML_FIELD_OP:
481     case AML_INDEX_FIELD_OP:
482     case AML_BANK_FIELD_OP:
483 
484         return (BLOCK_NONE);
485 
486     case AML_BUFFER_OP:
487     case AML_PACKAGE_OP:
488     case AML_VARIABLE_PACKAGE_OP:
489 
490         return (BLOCK_COMMA_LIST);
491 
492     default:
493 
494         OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
495         if (OpInfo->Flags & AML_HAS_ARGS)
496         {
497             return (BLOCK_COMMA_LIST);
498         }
499 
500         return (BLOCK_NONE);
501     }
502 }
503 
504 
505 /*******************************************************************************
506  *
507  * FUNCTION:    AcpiDmDescendingOp
508  *
509  * PARAMETERS:  ASL_WALK_CALLBACK
510  *
511  * RETURN:      Status
512  *
513  * DESCRIPTION: First visitation of a parse object during tree descent.
514  *              Decode opcode name and begin parameter list(s), if any.
515  *
516  ******************************************************************************/
517 
518 static ACPI_STATUS
519 AcpiDmDescendingOp (
520     ACPI_PARSE_OBJECT       *Op,
521     UINT32                  Level,
522     void                    *Context)
523 {
524     ACPI_OP_WALK_INFO       *Info = Context;
525     const ACPI_OPCODE_INFO  *OpInfo;
526     UINT32                  Name;
527     ACPI_PARSE_OBJECT       *NextOp;
528     ACPI_PARSE_OBJECT       *NextOp2;
529     UINT32                  AmlOffset;
530 
531 
532     /* Determine which file this parse node is contained in. */
533 
534     if (Gbl_CaptureComments)
535     {
536         ASL_CV_LABEL_FILENODE (Op);
537 
538         if (Level != 0 && ASL_CV_FILE_HAS_SWITCHED (Op))
539         {
540             ASL_CV_SWITCH_FILES (Level, Op);
541         }
542 
543         /* If this parse node has regular comments, print them here. */
544 
545         ASL_CV_PRINT_ONE_COMMENT (Op, AML_COMMENT_STANDARD, NULL, Level);
546     }
547 
548     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
549 
550     /* Listing support to dump the AML code after the ASL statement */
551 
552     if (AcpiGbl_DmOpt_Listing)
553     {
554         /* We only care about these classes of objects */
555 
556         if ((OpInfo->Class == AML_CLASS_NAMED_OBJECT) ||
557             (OpInfo->Class == AML_CLASS_CONTROL) ||
558             (OpInfo->Class == AML_CLASS_CREATE) ||
559             ((OpInfo->Class == AML_CLASS_EXECUTE) && (!Op->Common.Next)))
560         {
561             if (AcpiGbl_DmOpt_Listing && Info->PreviousAml)
562             {
563                 /* Dump the AML byte code for the previous Op */
564 
565                 if (Op->Common.Aml > Info->PreviousAml)
566                 {
567                     AcpiOsPrintf ("\n");
568                     AcpiUtDumpBuffer (
569                         (Info->StartAml + Info->AmlOffset),
570                         (Op->Common.Aml - Info->PreviousAml),
571                         DB_BYTE_DISPLAY, Info->AmlOffset);
572                     AcpiOsPrintf ("\n");
573                 }
574 
575                 Info->AmlOffset = (Op->Common.Aml - Info->StartAml);
576             }
577 
578             Info->PreviousAml = Op->Common.Aml;
579         }
580     }
581 
582     if (Op->Common.DisasmFlags & ACPI_PARSEOP_IGNORE)
583     {
584         /* Ignore this op -- it was handled elsewhere */
585 
586         return (AE_CTRL_DEPTH);
587     }
588 
589     if (AcpiDmIsTempName(Op))
590     {
591         /* Ignore compiler generated temporary names */
592 
593         return (AE_CTRL_DEPTH);
594     }
595 
596     if (Op->Common.DisasmOpcode == ACPI_DASM_IGNORE_SINGLE)
597     {
598         /* Ignore this op, but not it's children */
599 
600         return (AE_OK);
601     }
602 
603     if (Op->Common.AmlOpcode == AML_IF_OP)
604     {
605         NextOp = AcpiPsGetDepthNext (NULL, Op);
606         if (NextOp)
607         {
608             NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
609 
610             /* Don't emit the actual embedded externals unless asked */
611 
612             if (!AcpiGbl_DmEmitExternalOpcodes)
613             {
614                 /*
615                  * A Zero predicate indicates the possibility of one or more
616                  * External() opcodes within the If() block.
617                  */
618                 if (NextOp->Common.AmlOpcode == AML_ZERO_OP)
619                 {
620                     NextOp2 = NextOp->Common.Next;
621 
622                     if (NextOp2 &&
623                         (NextOp2->Common.AmlOpcode == AML_EXTERNAL_OP))
624                     {
625                         /* Ignore the If 0 block and all children */
626 
627                         Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
628                         return (AE_CTRL_DEPTH);
629                     }
630                 }
631             }
632         }
633     }
634 
635     /* Level 0 is at the Definition Block level */
636 
637     if (Level == 0)
638     {
639         /* In verbose mode, print the AML offset, opcode and depth count */
640 
641         if (Info->WalkState)
642         {
643             AmlOffset = (UINT32) ACPI_PTR_DIFF (Op->Common.Aml,
644                 Info->WalkState->ParserState.AmlStart);
645             if (AcpiGbl_DmOpt_Verbose)
646             {
647                 AcpiOsPrintf (DB_FULL_OP_INFO,
648                     (Info->WalkState->MethodNode ?
649                         Info->WalkState->MethodNode->Name.Ascii : "   "),
650                     AmlOffset, (UINT32) Op->Common.AmlOpcode);
651             }
652         }
653 
654         if (Op->Common.AmlOpcode == AML_SCOPE_OP)
655         {
656             /* This is the beginning of the Definition Block */
657 
658             AcpiOsPrintf ("{\n");
659 
660             /* Emit all External() declarations here */
661 
662             if (!AcpiGbl_DmEmitExternalOpcodes)
663             {
664                 AcpiDmEmitExternals ();
665             }
666 
667             return (AE_OK);
668         }
669     }
670     else if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
671          (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST)) &&
672          (!(Op->Common.DisasmFlags & ACPI_PARSEOP_ELSEIF)) &&
673          (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
674     {
675         /*
676          * This is a first-level element of a term list,
677          * indent a new line
678          */
679         switch (Op->Common.AmlOpcode)
680         {
681         case AML_NOOP_OP:
682             /*
683              * Optionally just ignore this opcode. Some tables use
684              * NoOp opcodes for "padding" out packages that the BIOS
685              * changes dynamically. This can leave hundreds or
686              * thousands of NoOp opcodes that if disassembled,
687              * cannot be compiled because they are syntactically
688              * incorrect.
689              */
690             if (AcpiGbl_IgnoreNoopOperator)
691             {
692                 Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
693                 return (AE_OK);
694             }
695 
696             /* Fallthrough */
697 
698         default:
699 
700             AcpiDmIndent (Level);
701             break;
702         }
703 
704         Info->LastLevel = Level;
705         Info->Count = 0;
706     }
707 
708     /*
709      * This is an inexpensive mechanism to try and keep lines from getting
710      * too long. When the limit is hit, start a new line at the previous
711      * indent plus one. A better but more expensive mechanism would be to
712      * keep track of the current column.
713      */
714     Info->Count++;
715     if (Info->Count /* +Info->LastLevel */ > 12)
716     {
717         Info->Count = 0;
718         AcpiOsPrintf ("\n");
719         AcpiDmIndent (Info->LastLevel + 1);
720     }
721 
722     /* If ASL+ is enabled, check for a C-style operator */
723 
724     if (AcpiDmCheckForSymbolicOpcode (Op, Info))
725     {
726         return (AE_OK);
727     }
728 
729     /* Print the opcode name */
730 
731     AcpiDmDisassembleOneOp (NULL, Info, Op);
732 
733     if ((Op->Common.DisasmOpcode == ACPI_DASM_LNOT_PREFIX) ||
734         (Op->Common.AmlOpcode == AML_INT_CONNECTION_OP))
735     {
736         return (AE_OK);
737     }
738 
739     if ((Op->Common.AmlOpcode == AML_NAME_OP) ||
740         (Op->Common.AmlOpcode == AML_RETURN_OP))
741     {
742         Info->Level--;
743     }
744 
745     if (Op->Common.AmlOpcode == AML_EXTERNAL_OP)
746     {
747         Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
748         return (AE_CTRL_DEPTH);
749     }
750 
751     /* Start the opcode argument list if necessary */
752 
753     if ((OpInfo->Flags & AML_HAS_ARGS) ||
754         (Op->Common.AmlOpcode == AML_EVENT_OP))
755     {
756         /* This opcode has an argument list */
757 
758         if (AcpiDmBlockType (Op) & BLOCK_PAREN)
759         {
760             AcpiOsPrintf (" (");
761             if (!(AcpiDmBlockType (Op) & BLOCK_BRACE))
762             {
763                 ASL_CV_PRINT_ONE_COMMENT (Op, AMLCOMMENT_INLINE, " ", 0);
764             }
765         }
766 
767         /* If this is a named opcode, print the associated name value */
768 
769         if (OpInfo->Flags & AML_NAMED)
770         {
771             switch (Op->Common.AmlOpcode)
772             {
773             case AML_ALIAS_OP:
774 
775                 NextOp = AcpiPsGetDepthNext (NULL, Op);
776                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
777                 AcpiDmNamestring (NextOp->Common.Value.Name);
778                 AcpiOsPrintf (", ");
779 
780                 /*lint -fallthrough */
781 
782             default:
783 
784                 Name = AcpiPsGetName (Op);
785                 if (Op->Named.Path)
786                 {
787                     AcpiDmNamestring ((char *) Op->Named.Path);
788                 }
789                 else
790                 {
791                     AcpiDmDumpName (Name);
792                 }
793 
794                 if (Op->Common.AmlOpcode != AML_INT_NAMEDFIELD_OP)
795                 {
796                     if (AcpiGbl_DmOpt_Verbose)
797                     {
798                         (void) AcpiPsDisplayObjectPathname (NULL, Op);
799                     }
800                 }
801                 break;
802             }
803 
804             switch (Op->Common.AmlOpcode)
805             {
806             case AML_METHOD_OP:
807 
808                 AcpiDmMethodFlags (Op);
809                 ASL_CV_CLOSE_PAREN (Op, Level);
810 
811                 /* Emit description comment for Method() with a predefined ACPI name */
812 
813                 AcpiDmPredefinedDescription (Op);
814                 break;
815 
816             case AML_NAME_OP:
817 
818                 /* Check for _HID and related EISAID() */
819 
820                 AcpiDmCheckForHardwareId (Op);
821                 AcpiOsPrintf (", ");
822                 ASL_CV_PRINT_ONE_COMMENT (Op, AML_NAMECOMMENT, NULL, 0);
823                 break;
824 
825             case AML_REGION_OP:
826 
827                 AcpiDmRegionFlags (Op);
828                 break;
829 
830             case AML_POWER_RESOURCE_OP:
831 
832                 /* Mark the next two Ops as part of the parameter list */
833 
834                 AcpiOsPrintf (", ");
835                 NextOp = AcpiPsGetDepthNext (NULL, Op);
836                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
837 
838                 NextOp = NextOp->Common.Next;
839                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
840                 return (AE_OK);
841 
842             case AML_PROCESSOR_OP:
843 
844                 /* Mark the next three Ops as part of the parameter list */
845 
846                 AcpiOsPrintf (", ");
847                 NextOp = AcpiPsGetDepthNext (NULL, Op);
848                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
849 
850                 NextOp = NextOp->Common.Next;
851                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
852 
853                 NextOp = NextOp->Common.Next;
854                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
855                 return (AE_OK);
856 
857             case AML_MUTEX_OP:
858             case AML_DATA_REGION_OP:
859 
860                 AcpiOsPrintf (", ");
861                 return (AE_OK);
862 
863             case AML_EVENT_OP:
864             case AML_ALIAS_OP:
865 
866                 return (AE_OK);
867 
868             case AML_SCOPE_OP:
869             case AML_DEVICE_OP:
870             case AML_THERMAL_ZONE_OP:
871 
872                 ASL_CV_CLOSE_PAREN (Op, Level);
873                 break;
874 
875             default:
876 
877                 AcpiOsPrintf ("*** Unhandled named opcode %X\n",
878                     Op->Common.AmlOpcode);
879                 break;
880             }
881         }
882 
883         else switch (Op->Common.AmlOpcode)
884         {
885         case AML_FIELD_OP:
886         case AML_BANK_FIELD_OP:
887         case AML_INDEX_FIELD_OP:
888 
889             Info->BitOffset = 0;
890 
891             /* Name of the parent OperationRegion */
892 
893             NextOp = AcpiPsGetDepthNext (NULL, Op);
894             AcpiDmNamestring (NextOp->Common.Value.Name);
895             AcpiOsPrintf (", ");
896             NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
897 
898             switch (Op->Common.AmlOpcode)
899             {
900             case AML_BANK_FIELD_OP:
901 
902                 /* Namestring - Bank Name */
903 
904                 NextOp = AcpiPsGetDepthNext (NULL, NextOp);
905                 AcpiDmNamestring (NextOp->Common.Value.Name);
906                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
907                 AcpiOsPrintf (", ");
908 
909                 /*
910                  * Bank Value. This is a TermArg in the middle of the parameter
911                  * list, must handle it here.
912                  *
913                  * Disassemble the TermArg parse tree. ACPI_PARSEOP_PARAMETER_LIST
914                  * eliminates newline in the output.
915                  */
916                 NextOp = NextOp->Common.Next;
917 
918                 Info->Flags = ACPI_PARSEOP_PARAMETER_LIST;
919                 AcpiDmWalkParseTree (NextOp, AcpiDmDescendingOp,
920                     AcpiDmAscendingOp, Info);
921                 Info->Flags = 0;
922                 Info->Level = Level;
923 
924                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
925                 AcpiOsPrintf (", ");
926                 break;
927 
928             case AML_INDEX_FIELD_OP:
929 
930                 /* Namestring - Data Name */
931 
932                 NextOp = AcpiPsGetDepthNext (NULL, NextOp);
933                 AcpiDmNamestring (NextOp->Common.Value.Name);
934                 AcpiOsPrintf (", ");
935                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
936                 break;
937 
938             default:
939 
940                 break;
941             }
942 
943             AcpiDmFieldFlags (NextOp);
944             break;
945 
946         case AML_BUFFER_OP:
947 
948             /* The next op is the size parameter */
949 
950             NextOp = AcpiPsGetDepthNext (NULL, Op);
951             if (!NextOp)
952             {
953                 /* Single-step support */
954 
955                 return (AE_OK);
956             }
957 
958             if (Op->Common.DisasmOpcode == ACPI_DASM_RESOURCE)
959             {
960                 /*
961                  * We have a resource list. Don't need to output
962                  * the buffer size Op. Open up a new block
963                  */
964                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;
965                 NextOp = NextOp->Common.Next;
966                 ASL_CV_CLOSE_PAREN (Op, Level);
967 
968                 /* Emit description comment for Name() with a predefined ACPI name */
969 
970                 AcpiDmPredefinedDescription (Op->Asl.Parent);
971 
972                 AcpiOsPrintf ("\n");
973                 AcpiDmIndent (Info->Level);
974                 AcpiOsPrintf ("{\n");
975                 return (AE_OK);
976             }
977 
978             /* Normal Buffer, mark size as in the parameter list */
979 
980             NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
981             return (AE_OK);
982 
983         case AML_IF_OP:
984         case AML_VARIABLE_PACKAGE_OP:
985         case AML_WHILE_OP:
986 
987             /* The next op is the size or predicate parameter */
988 
989             NextOp = AcpiPsGetDepthNext (NULL, Op);
990             if (NextOp)
991             {
992                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
993             }
994             return (AE_OK);
995 
996         case AML_PACKAGE_OP:
997 
998             /* The next op is the size parameter */
999 
1000             NextOp = AcpiPsGetDepthNext (NULL, Op);
1001             if (NextOp)
1002             {
1003                 NextOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;
1004             }
1005             return (AE_OK);
1006 
1007         case AML_MATCH_OP:
1008 
1009             AcpiDmMatchOp (Op);
1010             break;
1011 
1012         default:
1013 
1014             break;
1015         }
1016 
1017         if (AcpiDmBlockType (Op) & BLOCK_BRACE)
1018         {
1019             AcpiOsPrintf ("\n");
1020             AcpiDmIndent (Level);
1021             AcpiOsPrintf ("{\n");
1022         }
1023     }
1024 
1025     return (AE_OK);
1026 }
1027 
1028 
1029 /*******************************************************************************
1030  *
1031  * FUNCTION:    AcpiDmAscendingOp
1032  *
1033  * PARAMETERS:  ASL_WALK_CALLBACK
1034  *
1035  * RETURN:      Status
1036  *
1037  * DESCRIPTION: Second visitation of a parse object, during ascent of parse
1038  *              tree. Close out any parameter lists and complete the opcode.
1039  *
1040  ******************************************************************************/
1041 
1042 static ACPI_STATUS
1043 AcpiDmAscendingOp (
1044     ACPI_PARSE_OBJECT       *Op,
1045     UINT32                  Level,
1046     void                    *Context)
1047 {
1048     ACPI_OP_WALK_INFO       *Info = Context;
1049     ACPI_PARSE_OBJECT       *ParentOp;
1050 
1051 
1052     /* Point the Op's filename pointer to the proper file */
1053 
1054     if (Gbl_CaptureComments)
1055     {
1056         ASL_CV_LABEL_FILENODE (Op);
1057 
1058         /* Switch the output of these files if necessary */
1059 
1060         if (ASL_CV_FILE_HAS_SWITCHED (Op))
1061         {
1062             ASL_CV_SWITCH_FILES (Level, Op);
1063         }
1064     }
1065 
1066     if (Op->Common.DisasmFlags & ACPI_PARSEOP_IGNORE ||
1067         Op->Common.DisasmOpcode == ACPI_DASM_IGNORE_SINGLE)
1068     {
1069         /* Ignore this op -- it was handled elsewhere */
1070 
1071         return (AE_OK);
1072     }
1073 
1074     if ((Level == 0) && (Op->Common.AmlOpcode == AML_SCOPE_OP))
1075     {
1076         /* Indicates the end of the current descriptor block (table) */
1077 
1078         ASL_CV_CLOSE_BRACE (Op, Level);
1079 
1080         /* Print any comments that are at the end of the file here */
1081 
1082         if (Gbl_CaptureComments && AcpiGbl_LastListHead)
1083         {
1084             AcpiOsPrintf ("\n");
1085             ASL_CV_PRINT_ONE_COMMENT_LIST (AcpiGbl_LastListHead, 0);
1086         }
1087         AcpiOsPrintf ("\n\n");
1088 
1089         return (AE_OK);
1090     }
1091 
1092     switch (AcpiDmBlockType (Op))
1093     {
1094     case BLOCK_PAREN:
1095 
1096         /* Completed an op that has arguments, add closing paren if needed */
1097 
1098         AcpiDmCloseOperator (Op);
1099 
1100         if (Op->Common.AmlOpcode == AML_NAME_OP)
1101         {
1102             /* Emit description comment for Name() with a predefined ACPI name */
1103 
1104             AcpiDmPredefinedDescription (Op);
1105         }
1106         else
1107         {
1108             /* For Create* operators, attempt to emit resource tag description */
1109 
1110             AcpiDmFieldPredefinedDescription (Op);
1111         }
1112 
1113         /* Decode Notify() values */
1114 
1115         if (Op->Common.AmlOpcode == AML_NOTIFY_OP)
1116         {
1117             AcpiDmNotifyDescription (Op);
1118         }
1119 
1120         AcpiDmDisplayTargetPathname (Op);
1121 
1122         /* Could be a nested operator, check if comma required */
1123 
1124         if (!AcpiDmCommaIfListMember (Op))
1125         {
1126             if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
1127                  (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST)) &&
1128                  (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
1129             {
1130                 /*
1131                  * This is a first-level element of a term list
1132                  * start a new line
1133                  */
1134                 if (!(Info->Flags & ACPI_PARSEOP_PARAMETER_LIST))
1135                 {
1136                     AcpiOsPrintf ("\n");
1137                 }
1138             }
1139         }
1140         break;
1141 
1142     case BLOCK_BRACE:
1143     case (BLOCK_BRACE | BLOCK_PAREN):
1144 
1145         /* Completed an op that has a term list, add closing brace */
1146 
1147         if (Op->Common.DisasmFlags & ACPI_PARSEOP_EMPTY_TERMLIST)
1148         {
1149             ASL_CV_CLOSE_BRACE (Op, Level);
1150         }
1151         else
1152         {
1153             AcpiDmIndent (Level);
1154             ASL_CV_CLOSE_BRACE (Op, Level);
1155         }
1156 
1157         AcpiDmCommaIfListMember (Op);
1158 
1159         if (AcpiDmBlockType (Op->Common.Parent) != BLOCK_PAREN)
1160         {
1161             AcpiOsPrintf ("\n");
1162             if (!(Op->Common.DisasmFlags & ACPI_PARSEOP_EMPTY_TERMLIST))
1163             {
1164                 if ((Op->Common.AmlOpcode == AML_IF_OP)  &&
1165                     (Op->Common.Next) &&
1166                     (Op->Common.Next->Common.AmlOpcode == AML_ELSE_OP))
1167                 {
1168                     break;
1169                 }
1170 
1171                 if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
1172                     (!Op->Common.Next))
1173                 {
1174                     break;
1175                 }
1176                 AcpiOsPrintf ("\n");
1177             }
1178         }
1179         break;
1180 
1181     case BLOCK_NONE:
1182     default:
1183 
1184         /* Could be a nested operator, check if comma required */
1185 
1186         if (!AcpiDmCommaIfListMember (Op))
1187         {
1188             if ((AcpiDmBlockType (Op->Common.Parent) & BLOCK_BRACE) &&
1189                  (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST)) &&
1190                  (Op->Common.AmlOpcode != AML_INT_BYTELIST_OP))
1191             {
1192                 /*
1193                  * This is a first-level element of a term list
1194                  * start a new line
1195                  */
1196                 AcpiOsPrintf ("\n");
1197             }
1198         }
1199         else if (Op->Common.Parent)
1200         {
1201             switch (Op->Common.Parent->Common.AmlOpcode)
1202             {
1203             case AML_PACKAGE_OP:
1204             case AML_VARIABLE_PACKAGE_OP:
1205 
1206                 if (!(Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST))
1207                 {
1208                     AcpiOsPrintf ("\n");
1209                 }
1210                 break;
1211 
1212             default:
1213 
1214                 break;
1215             }
1216         }
1217         break;
1218     }
1219 
1220     if (Op->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST)
1221     {
1222         if ((Op->Common.Next) &&
1223             (Op->Common.Next->Common.DisasmFlags & ACPI_PARSEOP_PARAMETER_LIST))
1224         {
1225             return (AE_OK);
1226         }
1227 
1228         /*
1229          * The parent Op is guaranteed to be valid because of the flag
1230          * ACPI_PARSEOP_PARAMETER_LIST -- which means that this op is part of
1231          * a parameter list and thus has a valid parent.
1232          */
1233         ParentOp = Op->Common.Parent;
1234 
1235         /*
1236          * Just completed a parameter node for something like "Buffer (param)".
1237          * Close the paren and open up the term list block with a brace.
1238          *
1239          * Switch predicates don't have a Next node but require a closing paren
1240          * and opening brace.
1241          */
1242         if (Op->Common.Next || Op->Common.DisasmOpcode == ACPI_DASM_SWITCH_PREDICATE)
1243         {
1244             ASL_CV_CLOSE_PAREN (Op, Level);
1245 
1246             /*
1247              * Emit a description comment for a Name() operator that is a
1248              * predefined ACPI name. Must check the grandparent.
1249              */
1250             ParentOp = ParentOp->Common.Parent;
1251             if (ParentOp &&
1252                 (ParentOp->Asl.AmlOpcode == AML_NAME_OP))
1253             {
1254                 AcpiDmPredefinedDescription (ParentOp);
1255             }
1256 
1257             /* Correct the indentation level for Switch and Case predicates */
1258 
1259             if (Op->Common.DisasmOpcode == ACPI_DASM_SWITCH_PREDICATE)
1260             {
1261                 --Level;
1262             }
1263 
1264             AcpiOsPrintf ("\n");
1265             AcpiDmIndent (Level - 1);
1266             AcpiOsPrintf ("{\n");
1267         }
1268         else
1269         {
1270             ParentOp->Common.DisasmFlags |= ACPI_PARSEOP_EMPTY_TERMLIST;
1271             ASL_CV_CLOSE_PAREN (Op, Level);
1272             AcpiOsPrintf ("{");
1273         }
1274     }
1275 
1276     if ((Op->Common.AmlOpcode == AML_NAME_OP) ||
1277         (Op->Common.AmlOpcode == AML_RETURN_OP))
1278     {
1279         Info->Level++;
1280     }
1281 
1282     /*
1283      * For ASL+, check for and emit a C-style symbol. If valid, the
1284      * symbol string has been deferred until after the first operand
1285      */
1286     if (AcpiGbl_CstyleDisassembly)
1287     {
1288         if (Op->Asl.OperatorSymbol)
1289         {
1290             AcpiOsPrintf ("%s", Op->Asl.OperatorSymbol);
1291             Op->Asl.OperatorSymbol = NULL;
1292         }
1293     }
1294 
1295     return (AE_OK);
1296 }
1297 
1298 #endif  /* ACPI_DISASSEMBLER */
1299