1 /******************************************************************************
2  *
3  * Module Name: psobject - Support for parse objects
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2020, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acparser.h"
47 #include "amlcode.h"
48 #include "acconvert.h"
49 #include "acnamesp.h"
50 
51 #define _COMPONENT          ACPI_PARSER
52         ACPI_MODULE_NAME    ("psobject")
53 
54 
55 /* Local prototypes */
56 
57 static ACPI_STATUS
58 AcpiPsGetAmlOpcode (
59     ACPI_WALK_STATE         *WalkState);
60 
61 
62 /*******************************************************************************
63  *
64  * FUNCTION:    AcpiPsGetAmlOpcode
65  *
66  * PARAMETERS:  WalkState           - Current state
67  *
68  * RETURN:      Status
69  *
70  * DESCRIPTION: Extract the next AML opcode from the input stream.
71  *
72  ******************************************************************************/
73 
74 static ACPI_STATUS
75 AcpiPsGetAmlOpcode (
76     ACPI_WALK_STATE         *WalkState)
77 {
78     ACPI_ERROR_ONLY (UINT32 AmlOffset);
79 
80 
81     ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode, WalkState);
82 
83 
84     WalkState->Aml = WalkState->ParserState.Aml;
85     WalkState->Opcode = AcpiPsPeekOpcode (&(WalkState->ParserState));
86 
87     /*
88      * First cut to determine what we have found:
89      * 1) A valid AML opcode
90      * 2) A name string
91      * 3) An unknown/invalid opcode
92      */
93     WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
94 
95     switch (WalkState->OpInfo->Class)
96     {
97     case AML_CLASS_ASCII:
98     case AML_CLASS_PREFIX:
99         /*
100          * Starts with a valid prefix or ASCII char, this is a name
101          * string. Convert the bare name string to a namepath.
102          */
103         WalkState->Opcode = AML_INT_NAMEPATH_OP;
104         WalkState->ArgTypes = ARGP_NAMESTRING;
105         break;
106 
107     case AML_CLASS_UNKNOWN:
108 
109         /* The opcode is unrecognized. Complain and skip unknown opcodes */
110 
111         if (WalkState->PassNumber == 2)
112         {
113             ACPI_ERROR_ONLY(AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->Aml,
114                 WalkState->ParserState.AmlStart));
115 
116             ACPI_ERROR ((AE_INFO,
117                 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
118                 WalkState->Opcode,
119                 (UINT32) (AmlOffset + sizeof (ACPI_TABLE_HEADER))));
120 
121             ACPI_DUMP_BUFFER ((WalkState->ParserState.Aml - 16), 48);
122 
123 #ifdef ACPI_ASL_COMPILER
124             /*
125              * This is executed for the disassembler only. Output goes
126              * to the disassembled ASL output file.
127              */
128             AcpiOsPrintf (
129                 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
130                 WalkState->Opcode,
131                 (UINT32) (AmlOffset + sizeof (ACPI_TABLE_HEADER)));
132 
133             ACPI_ERROR ((AE_INFO,
134                 "Aborting disassembly, AML byte code is corrupt"));
135 
136             /* Dump the context surrounding the invalid opcode */
137 
138             AcpiUtDumpBuffer (((UINT8 *) WalkState->ParserState.Aml - 16),
139                 48, DB_BYTE_DISPLAY,
140                 (AmlOffset + sizeof (ACPI_TABLE_HEADER) - 16));
141             AcpiOsPrintf (" */\n");
142 
143             /*
144              * Just abort the disassembly, cannot continue because the
145              * parser is essentially lost. The disassembler can then
146              * randomly fail because an ill-constructed parse tree
147              * can result.
148              */
149             return_ACPI_STATUS (AE_AML_BAD_OPCODE);
150 #endif
151         }
152 
153         /* Increment past one-byte or two-byte opcode */
154 
155         WalkState->ParserState.Aml++;
156         if (WalkState->Opcode > 0xFF) /* Can only happen if first byte is 0x5B */
157         {
158             WalkState->ParserState.Aml++;
159         }
160 
161         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
162 
163     default:
164 
165         /* Found opcode info, this is a normal opcode */
166 
167         WalkState->ParserState.Aml +=
168             AcpiPsGetOpcodeSize (WalkState->Opcode);
169         WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
170         break;
171     }
172 
173     return_ACPI_STATUS (AE_OK);
174 }
175 
176 
177 /*******************************************************************************
178  *
179  * FUNCTION:    AcpiPsBuildNamedOp
180  *
181  * PARAMETERS:  WalkState           - Current state
182  *              AmlOpStart          - Begin of named Op in AML
183  *              UnnamedOp           - Early Op (not a named Op)
184  *              Op                  - Returned Op
185  *
186  * RETURN:      Status
187  *
188  * DESCRIPTION: Parse a named Op
189  *
190  ******************************************************************************/
191 
192 ACPI_STATUS
193 AcpiPsBuildNamedOp (
194     ACPI_WALK_STATE         *WalkState,
195     UINT8                   *AmlOpStart,
196     ACPI_PARSE_OBJECT       *UnnamedOp,
197     ACPI_PARSE_OBJECT       **Op)
198 {
199     ACPI_STATUS             Status = AE_OK;
200     ACPI_PARSE_OBJECT       *Arg = NULL;
201 
202 
203     ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp, WalkState);
204 
205 
206     UnnamedOp->Common.Value.Arg = NULL;
207     UnnamedOp->Common.ArgListLength = 0;
208     UnnamedOp->Common.AmlOpcode = WalkState->Opcode;
209 
210     /*
211      * Get and append arguments until we find the node that contains
212      * the name (the type ARGP_NAME).
213      */
214     while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
215           (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
216     {
217         ASL_CV_CAPTURE_COMMENTS (WalkState);
218         Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
219             GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
220         if (ACPI_FAILURE (Status))
221         {
222             return_ACPI_STATUS (Status);
223         }
224 
225         AcpiPsAppendArg (UnnamedOp, Arg);
226         INCREMENT_ARG_LIST (WalkState->ArgTypes);
227     }
228 
229     /* are there any inline comments associated with the NameSeg?? If so, save this. */
230 
231     ASL_CV_CAPTURE_COMMENTS (WalkState);
232 
233 #ifdef ACPI_ASL_COMPILER
234     if (AcpiGbl_CurrentInlineComment != NULL)
235     {
236         UnnamedOp->Common.NameComment = AcpiGbl_CurrentInlineComment;
237         AcpiGbl_CurrentInlineComment = NULL;
238     }
239 #endif
240 
241     /*
242      * Make sure that we found a NAME and didn't run out of arguments
243      */
244     if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
245     {
246         return_ACPI_STATUS (AE_AML_NO_OPERAND);
247     }
248 
249     /* We know that this arg is a name, move to next arg */
250 
251     INCREMENT_ARG_LIST (WalkState->ArgTypes);
252 
253     /*
254      * Find the object. This will either insert the object into
255      * the namespace or simply look it up
256      */
257     WalkState->Op = NULL;
258 
259     Status = WalkState->DescendingCallback (WalkState, Op);
260     if (ACPI_FAILURE (Status))
261     {
262         if (Status != AE_CTRL_TERMINATE)
263         {
264             ACPI_EXCEPTION ((AE_INFO, Status, "During name lookup/catalog"));
265         }
266         return_ACPI_STATUS (Status);
267     }
268 
269     if (!*Op)
270     {
271         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
272     }
273 
274     Status = AcpiPsNextParseState (WalkState, *Op, Status);
275     if (ACPI_FAILURE (Status))
276     {
277         if (Status == AE_CTRL_PENDING)
278         {
279             Status = AE_CTRL_PARSE_PENDING;
280         }
281         return_ACPI_STATUS (Status);
282     }
283 
284     AcpiPsAppendArg (*Op, UnnamedOp->Common.Value.Arg);
285 
286 #ifdef ACPI_ASL_COMPILER
287 
288     /* save any comments that might be associated with UnnamedOp. */
289 
290     (*Op)->Common.InlineComment     = UnnamedOp->Common.InlineComment;
291     (*Op)->Common.EndNodeComment    = UnnamedOp->Common.EndNodeComment;
292     (*Op)->Common.CloseBraceComment = UnnamedOp->Common.CloseBraceComment;
293     (*Op)->Common.NameComment       = UnnamedOp->Common.NameComment;
294     (*Op)->Common.CommentList       = UnnamedOp->Common.CommentList;
295     (*Op)->Common.EndBlkComment     = UnnamedOp->Common.EndBlkComment;
296     (*Op)->Common.CvFilename        = UnnamedOp->Common.CvFilename;
297     (*Op)->Common.CvParentFilename  = UnnamedOp->Common.CvParentFilename;
298     (*Op)->Named.Aml                = UnnamedOp->Common.Aml;
299 
300     UnnamedOp->Common.InlineComment     = NULL;
301     UnnamedOp->Common.EndNodeComment    = NULL;
302     UnnamedOp->Common.CloseBraceComment = NULL;
303     UnnamedOp->Common.NameComment       = NULL;
304     UnnamedOp->Common.CommentList       = NULL;
305     UnnamedOp->Common.EndBlkComment     = NULL;
306 #endif
307 
308     if ((*Op)->Common.AmlOpcode == AML_REGION_OP ||
309         (*Op)->Common.AmlOpcode == AML_DATA_REGION_OP)
310     {
311         /*
312          * Defer final parsing of an OperationRegion body, because we don't
313          * have enough info in the first pass to parse it correctly (i.e.,
314          * there may be method calls within the TermArg elements of the body.)
315          *
316          * However, we must continue parsing because the opregion is not a
317          * standalone package -- we don't know where the end is at this point.
318          *
319          * (Length is unknown until parse of the body complete)
320          */
321         (*Op)->Named.Data = AmlOpStart;
322         (*Op)->Named.Length = 0;
323     }
324 
325     return_ACPI_STATUS (AE_OK);
326 }
327 
328 
329 /*******************************************************************************
330  *
331  * FUNCTION:    AcpiPsCreateOp
332  *
333  * PARAMETERS:  WalkState           - Current state
334  *              AmlOpStart          - Op start in AML
335  *              NewOp               - Returned Op
336  *
337  * RETURN:      Status
338  *
339  * DESCRIPTION: Get Op from AML
340  *
341  ******************************************************************************/
342 
343 ACPI_STATUS
344 AcpiPsCreateOp (
345     ACPI_WALK_STATE         *WalkState,
346     UINT8                   *AmlOpStart,
347     ACPI_PARSE_OBJECT       **NewOp)
348 {
349     ACPI_STATUS             Status = AE_OK;
350     ACPI_PARSE_OBJECT       *Op;
351     ACPI_PARSE_OBJECT       *NamedOp = NULL;
352     ACPI_PARSE_OBJECT       *ParentScope;
353     UINT8                   ArgumentCount;
354     const ACPI_OPCODE_INFO  *OpInfo;
355 
356 
357     ACPI_FUNCTION_TRACE_PTR (PsCreateOp, WalkState);
358 
359 
360     Status = AcpiPsGetAmlOpcode (WalkState);
361     if (Status == AE_CTRL_PARSE_CONTINUE)
362     {
363         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
364     }
365     if (ACPI_FAILURE (Status))
366     {
367         return_ACPI_STATUS (Status);
368     }
369 
370     /* Create Op structure and append to parent's argument list */
371 
372     WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
373     Op = AcpiPsAllocOp (WalkState->Opcode, AmlOpStart);
374     if (!Op)
375     {
376         return_ACPI_STATUS (AE_NO_MEMORY);
377     }
378 
379     if (WalkState->OpInfo->Flags & AML_NAMED)
380     {
381         Status = AcpiPsBuildNamedOp (WalkState, AmlOpStart, Op, &NamedOp);
382         AcpiPsFreeOp (Op);
383 
384 #ifdef ACPI_ASL_COMPILER
385         if (AcpiGbl_DisasmFlag && WalkState->Opcode == AML_EXTERNAL_OP &&
386             Status == AE_NOT_FOUND)
387         {
388             /*
389              * If parsing of AML_EXTERNAL_OP's name path fails, then skip
390              * past this opcode and keep parsing. This is a much better
391              * alternative than to abort the entire disassembler. At this
392              * point, the ParserState is at the end of the namepath of the
393              * external declaration opcode. Setting WalkState->Aml to
394              * WalkState->ParserState.Aml + 2 moves increments the
395              * WalkState->Aml past the object type and the paramcount of the
396              * external opcode.
397              */
398             WalkState->Aml = WalkState->ParserState.Aml + 2;
399             WalkState->ParserState.Aml = WalkState->Aml;
400             return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
401         }
402 #endif
403         if (ACPI_FAILURE (Status))
404         {
405             return_ACPI_STATUS (Status);
406         }
407 
408         *NewOp = NamedOp;
409         return_ACPI_STATUS (AE_OK);
410     }
411 
412     /* Not a named opcode, just allocate Op and append to parent */
413 
414     if (WalkState->OpInfo->Flags & AML_CREATE)
415     {
416         /*
417          * Backup to beginning of CreateXXXfield declaration
418          * BodyLength is unknown until we parse the body
419          */
420         Op->Named.Data = AmlOpStart;
421         Op->Named.Length = 0;
422     }
423 
424     if (WalkState->Opcode == AML_BANK_FIELD_OP)
425     {
426         /*
427          * Backup to beginning of BankField declaration
428          * BodyLength is unknown until we parse the body
429          */
430         Op->Named.Data = AmlOpStart;
431         Op->Named.Length = 0;
432     }
433 
434     ParentScope = AcpiPsGetParentScope (&(WalkState->ParserState));
435     AcpiPsAppendArg (ParentScope, Op);
436 
437     if (ParentScope)
438     {
439         OpInfo = AcpiPsGetOpcodeInfo (ParentScope->Common.AmlOpcode);
440         if (OpInfo->Flags & AML_HAS_TARGET)
441         {
442             ArgumentCount = AcpiPsGetArgumentCount (OpInfo->Type);
443             if (ParentScope->Common.ArgListLength > ArgumentCount)
444             {
445                 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
446             }
447         }
448 
449         /*
450          * Special case for both Increment() and Decrement(), where
451          * the lone argument is both a source and a target.
452          */
453         else if ((ParentScope->Common.AmlOpcode == AML_INCREMENT_OP) ||
454                 (ParentScope->Common.AmlOpcode == AML_DECREMENT_OP))
455         {
456             Op->Common.Flags |= ACPI_PARSEOP_TARGET;
457         }
458     }
459 
460     if (WalkState->DescendingCallback != NULL)
461     {
462         /*
463          * Find the object. This will either insert the object into
464          * the namespace or simply look it up
465          */
466         WalkState->Op = *NewOp = Op;
467 
468         Status = WalkState->DescendingCallback (WalkState, &Op);
469         Status = AcpiPsNextParseState (WalkState, Op, Status);
470         if (Status == AE_CTRL_PENDING)
471         {
472             Status = AE_CTRL_PARSE_PENDING;
473         }
474     }
475 
476     return_ACPI_STATUS (Status);
477 }
478 
479 
480 /*******************************************************************************
481  *
482  * FUNCTION:    AcpiPsCompleteOp
483  *
484  * PARAMETERS:  WalkState           - Current state
485  *              Op                  - Returned Op
486  *              Status              - Parse status before complete Op
487  *
488  * RETURN:      Status
489  *
490  * DESCRIPTION: Complete Op
491  *
492  ******************************************************************************/
493 
494 ACPI_STATUS
495 AcpiPsCompleteOp (
496     ACPI_WALK_STATE         *WalkState,
497     ACPI_PARSE_OBJECT       **Op,
498     ACPI_STATUS             Status)
499 {
500     ACPI_STATUS             Status2;
501 
502 
503     ACPI_FUNCTION_TRACE_PTR (PsCompleteOp, WalkState);
504 
505 
506     /*
507      * Finished one argument of the containing scope
508      */
509     WalkState->ParserState.Scope->ParseScope.ArgCount--;
510 
511     /* Close this Op (will result in parse subtree deletion) */
512 
513     Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
514     if (ACPI_FAILURE (Status2))
515     {
516         return_ACPI_STATUS (Status2);
517     }
518 
519     *Op = NULL;
520 
521     switch (Status)
522     {
523     case AE_OK:
524 
525         break;
526 
527     case AE_CTRL_TRANSFER:
528 
529         /* We are about to transfer to a called method */
530 
531         WalkState->PrevOp = NULL;
532         WalkState->PrevArgTypes = WalkState->ArgTypes;
533         return_ACPI_STATUS (Status);
534 
535     case AE_CTRL_END:
536 
537         AcpiPsPopScope (&(WalkState->ParserState), Op,
538             &WalkState->ArgTypes, &WalkState->ArgCount);
539 
540         if (*Op)
541         {
542             WalkState->Op = *Op;
543             WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
544             WalkState->Opcode = (*Op)->Common.AmlOpcode;
545 
546             Status = WalkState->AscendingCallback (WalkState);
547             (void) AcpiPsNextParseState (WalkState, *Op, Status);
548 
549             Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
550             if (ACPI_FAILURE (Status2))
551             {
552                 return_ACPI_STATUS (Status2);
553             }
554         }
555 
556         break;
557 
558     case AE_CTRL_BREAK:
559     case AE_CTRL_CONTINUE:
560 
561         /* Pop off scopes until we find the While */
562 
563         while (!(*Op) || ((*Op)->Common.AmlOpcode != AML_WHILE_OP))
564         {
565             AcpiPsPopScope (&(WalkState->ParserState), Op,
566                 &WalkState->ArgTypes, &WalkState->ArgCount);
567         }
568 
569         /* Close this iteration of the While loop */
570 
571         WalkState->Op = *Op;
572         WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
573         WalkState->Opcode = (*Op)->Common.AmlOpcode;
574 
575         Status = WalkState->AscendingCallback (WalkState);
576         (void) AcpiPsNextParseState (WalkState, *Op, Status);
577 
578         Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
579         if (ACPI_FAILURE (Status2))
580         {
581             return_ACPI_STATUS (Status2);
582         }
583 
584         break;
585 
586     case AE_CTRL_TERMINATE:
587 
588         /* Clean up */
589         do
590         {
591             if (*Op)
592             {
593                 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
594                 if (ACPI_FAILURE (Status2))
595                 {
596                     return_ACPI_STATUS (Status2);
597                 }
598 
599                 AcpiUtDeleteGenericState (
600                     AcpiUtPopGenericState (&WalkState->ControlState));
601             }
602 
603             AcpiPsPopScope (&(WalkState->ParserState), Op,
604                 &WalkState->ArgTypes, &WalkState->ArgCount);
605 
606         } while (*Op);
607 
608         return_ACPI_STATUS (AE_OK);
609 
610     default:  /* All other non-AE_OK status */
611 
612         do
613         {
614             if (*Op)
615             {
616                 /*
617                  * These Opcodes need to be removed from the namespace because they
618                  * get created even if these opcodes cannot be created due to
619                  * errors.
620                  */
621                 if (((*Op)->Common.AmlOpcode == AML_REGION_OP) ||
622                     ((*Op)->Common.AmlOpcode == AML_DATA_REGION_OP))
623                 {
624                     AcpiNsDeleteChildren ((*Op)->Common.Node);
625                     AcpiNsRemoveNode ((*Op)->Common.Node);
626                     (*Op)->Common.Node = NULL;
627                     AcpiPsDeleteParseTree (*Op);
628                 }
629 
630                 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
631                 if (ACPI_FAILURE (Status2))
632                 {
633                     return_ACPI_STATUS (Status2);
634                 }
635             }
636 
637             AcpiPsPopScope (&(WalkState->ParserState), Op,
638                 &WalkState->ArgTypes, &WalkState->ArgCount);
639 
640         } while (*Op);
641 
642 
643 #if 0
644         /*
645          * TBD: Cleanup parse ops on error
646          */
647         if (*Op == NULL)
648         {
649             AcpiPsPopScope (ParserState, Op,
650                 &WalkState->ArgTypes, &WalkState->ArgCount);
651         }
652 #endif
653         WalkState->PrevOp = NULL;
654         WalkState->PrevArgTypes = WalkState->ArgTypes;
655 
656         if (WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL)
657         {
658             /*
659              * There was something that went wrong while executing code at the
660              * module-level. We need to skip parsing whatever caused the
661              * error and keep going. One runtime error during the table load
662              * should not cause the entire table to not be loaded. This is
663              * because there could be correct AML beyond the parts that caused
664              * the runtime error.
665              */
666             ACPI_INFO (("Ignoring error and continuing table load"));
667             return_ACPI_STATUS (AE_OK);
668         }
669         return_ACPI_STATUS (Status);
670     }
671 
672     /* This scope complete? */
673 
674     if (AcpiPsHasCompletedScope (&(WalkState->ParserState)))
675     {
676         AcpiPsPopScope (&(WalkState->ParserState), Op,
677             &WalkState->ArgTypes, &WalkState->ArgCount);
678         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *Op));
679     }
680     else
681     {
682         *Op = NULL;
683     }
684 
685     return_ACPI_STATUS (AE_OK);
686 }
687 
688 
689 /*******************************************************************************
690  *
691  * FUNCTION:    AcpiPsCompleteFinalOp
692  *
693  * PARAMETERS:  WalkState           - Current state
694  *              Op                  - Current Op
695  *              Status              - Current parse status before complete last
696  *                                    Op
697  *
698  * RETURN:      Status
699  *
700  * DESCRIPTION: Complete last Op.
701  *
702  ******************************************************************************/
703 
704 ACPI_STATUS
705 AcpiPsCompleteFinalOp (
706     ACPI_WALK_STATE         *WalkState,
707     ACPI_PARSE_OBJECT       *Op,
708     ACPI_STATUS             Status)
709 {
710     ACPI_STATUS             Status2;
711 
712 
713     ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp, WalkState);
714 
715 
716     /*
717      * Complete the last Op (if not completed), and clear the scope stack.
718      * It is easily possible to end an AML "package" with an unbounded number
719      * of open scopes (such as when several ASL blocks are closed with
720      * sequential closing braces). We want to terminate each one cleanly.
721      */
722     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
723     do
724     {
725         if (Op)
726         {
727             if (WalkState->AscendingCallback != NULL)
728             {
729                 WalkState->Op = Op;
730                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
731                 WalkState->Opcode = Op->Common.AmlOpcode;
732 
733                 Status = WalkState->AscendingCallback (WalkState);
734                 Status = AcpiPsNextParseState (WalkState, Op, Status);
735                 if (Status == AE_CTRL_PENDING)
736                 {
737                     Status = AcpiPsCompleteOp (WalkState, &Op, AE_OK);
738                     if (ACPI_FAILURE (Status))
739                     {
740                         return_ACPI_STATUS (Status);
741                     }
742                 }
743 
744                 if (Status == AE_CTRL_TERMINATE)
745                 {
746                     Status = AE_OK;
747 
748                     /* Clean up */
749                     do
750                     {
751                         if (Op)
752                         {
753                             Status2 = AcpiPsCompleteThisOp (WalkState, Op);
754                             if (ACPI_FAILURE (Status2))
755                             {
756                                 return_ACPI_STATUS (Status2);
757                             }
758                         }
759 
760                         AcpiPsPopScope (&(WalkState->ParserState), &Op,
761                             &WalkState->ArgTypes, &WalkState->ArgCount);
762 
763                     } while (Op);
764 
765                     return_ACPI_STATUS (Status);
766                 }
767 
768                 else if (ACPI_FAILURE (Status))
769                 {
770                     /* First error is most important */
771 
772                     (void) AcpiPsCompleteThisOp (WalkState, Op);
773                     return_ACPI_STATUS (Status);
774                 }
775             }
776 
777             Status2 = AcpiPsCompleteThisOp (WalkState, Op);
778             if (ACPI_FAILURE (Status2))
779             {
780                 return_ACPI_STATUS (Status2);
781             }
782         }
783 
784         AcpiPsPopScope (&(WalkState->ParserState), &Op, &WalkState->ArgTypes,
785             &WalkState->ArgCount);
786 
787     } while (Op);
788 
789     return_ACPI_STATUS (Status);
790 }
791