1 /******************************************************************************
2  *
3  * Module Name: aslcodegen - AML code generation
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 "aslcompiler.h"
153 #include "aslcompiler.y.h"
154 #include "amlcode.h"
155 #include "acconvert.h"
156 
157 #define _COMPONENT          ACPI_COMPILER
158         ACPI_MODULE_NAME    ("aslcodegen")
159 
160 /* Local prototypes */
161 
162 static ACPI_STATUS
163 CgAmlWriteWalk (
164     ACPI_PARSE_OBJECT       *Op,
165     UINT32                  Level,
166     void                    *Context);
167 
168 static void
169 CgWriteAmlOpcode (
170     ACPI_PARSE_OBJECT       *Op);
171 
172 static void
173 CgWriteTableHeader (
174     ACPI_PARSE_OBJECT       *Op);
175 
176 static void
177 CgCloseTable (
178     void);
179 
180 static void
181 CgWriteNode (
182     ACPI_PARSE_OBJECT       *Op);
183 
184 
185 /*******************************************************************************
186  *
187  * FUNCTION:    CgGenerateAmlOutput
188  *
189  * PARAMETERS:  None.
190  *
191  * RETURN:      None
192  *
193  * DESCRIPTION: Generate AML code. Currently generates the listing file
194  *              simultaneously.
195  *
196  ******************************************************************************/
197 
198 void
199 CgGenerateAmlOutput (
200     void)
201 {
202 
203     /* Generate the AML output file */
204 
205     FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
206     Gbl_SourceLine = 0;
207     Gbl_NextError = Gbl_ErrorLog;
208 
209     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_DOWNWARD,
210         CgAmlWriteWalk, NULL, NULL);
211 
212     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
213     CgCloseTable ();
214 }
215 
216 
217 /*******************************************************************************
218  *
219  * FUNCTION:    CgAmlWriteWalk
220  *
221  * PARAMETERS:  ASL_WALK_CALLBACK
222  *
223  * RETURN:      Status
224  *
225  * DESCRIPTION: Parse tree walk to generate the AML code.
226  *
227  ******************************************************************************/
228 
229 static ACPI_STATUS
230 CgAmlWriteWalk (
231     ACPI_PARSE_OBJECT       *Op,
232     UINT32                  Level,
233     void                    *Context)
234 {
235 
236     /* Generate the AML for this node */
237 
238     CgWriteNode (Op);
239 
240     if (!Gbl_DebugFlag)
241     {
242         return (AE_OK);
243     }
244 
245     /* Print header at level 0. Alignment assumes 32-bit pointers */
246 
247     if (!Level)
248     {
249         DbgPrint (ASL_TREE_OUTPUT,
250             "\nFinal parse tree used for AML output:\n");
251         DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
252     }
253 
254     /* Dump ParseOp name and possible value */
255 
256     switch (Op->Asl.ParseOpcode)
257     {
258     case PARSEOP_NAMESEG:
259     case PARSEOP_NAMESTRING:
260     case PARSEOP_METHODCALL:
261     case PARSEOP_STRING_LITERAL:
262 
263         UtDumpStringOp (Op, Level);
264         break;
265 
266     default:
267 
268         UtDumpBasicOp (Op, Level);
269         break;
270     }
271 
272     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_DEBUG2,
273         /* 1  */ (UINT32) Op->Asl.Value.Integer,
274         /* 2  */ Op->Asl.ParseOpcode,
275         /* 3  */ Op->Asl.AmlOpcode,
276         /* 4  */ Op->Asl.AmlOpcodeLength,
277         /* 5  */ Op->Asl.AmlPkgLenBytes,
278         /* 6  */ Op->Asl.AmlLength,
279         /* 7  */ Op->Asl.AmlSubtreeLength,
280         /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
281         /* 9  */ Op,
282         /* 10 */ Op->Asl.Parent,
283         /* 11 */ Op->Asl.Child,
284         /* 12 */ Op->Asl.Next,
285         /* 13 */ Op->Asl.CompileFlags,
286         /* 14 */ Op->Asl.AcpiBtype,
287         /* 15 */ Op->Asl.FinalAmlLength,
288         /* 16 */ Op->Asl.Column,
289         /* 17 */ Op->Asl.LineNumber,
290         /* 18 */ Op->Asl.EndLine,
291         /* 19 */ Op->Asl.LogicalLineNumber,
292         /* 20 */ Op->Asl.EndLogicalLine);
293 
294     return (AE_OK);
295 }
296 
297 
298 /*******************************************************************************
299  *
300  * FUNCTION:    CgLocalWriteAmlData
301  *
302  * PARAMETERS:  Op              - Current parse op
303  *              Buffer          - Buffer to write
304  *              Length          - Size of data in buffer
305  *
306  * RETURN:      None
307  *
308  * DESCRIPTION: Write a buffer of AML data to the AML output file.
309  *
310  ******************************************************************************/
311 
312 void
313 CgLocalWriteAmlData (
314     ACPI_PARSE_OBJECT       *Op,
315     void                    *Buffer,
316     UINT32                  Length)
317 {
318 
319     /* Write the raw data to the AML file */
320 
321     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
322 
323     /* Update the final AML length for this node (used for listings) */
324 
325     if (Op)
326     {
327         Op->Asl.FinalAmlLength += Length;
328     }
329 }
330 
331 
332 /*******************************************************************************
333  *
334  * FUNCTION:    CgWriteAmlOpcode
335  *
336  * PARAMETERS:  Op            - Parse node with an AML opcode
337  *
338  * RETURN:      None.
339  *
340  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
341  *
342  ******************************************************************************/
343 
344 static void
345 CgWriteAmlOpcode (
346     ACPI_PARSE_OBJECT       *Op)
347 {
348     UINT8                   PkgLenFirstByte;
349     UINT32                  i;
350     union {
351         UINT16                  Opcode;
352         UINT8                   OpcodeBytes[2];
353     } Aml;
354     union {
355         UINT32                  Len;
356         UINT8                   LenBytes[4];
357     } PkgLen;
358 
359 
360     /* We expect some DEFAULT_ARGs, just ignore them */
361 
362     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
363     {
364         return;
365     }
366 
367     /*
368      * Before printing the bytecode, generate comment byte codes
369      * associated with this node.
370      */
371     if (Gbl_CaptureComments)
372     {
373         CgWriteAmlComment(Op);
374     }
375 
376     switch (Op->Asl.AmlOpcode)
377     {
378     case AML_UNASSIGNED_OPCODE:
379 
380         /* These opcodes should not get here */
381 
382         printf ("Found a node with an unassigned AML opcode\n");
383         FlPrintFile (ASL_FILE_STDERR,
384             "Found a node with an unassigned AML opcode\n");
385         return;
386 
387     case AML_INT_RESERVEDFIELD_OP:
388 
389         /* Special opcodes for within a field definition */
390 
391         Aml.Opcode = AML_FIELD_OFFSET_OP;
392         break;
393 
394     case AML_INT_ACCESSFIELD_OP:
395 
396         Aml.Opcode = AML_FIELD_ACCESS_OP;
397         break;
398 
399     case AML_INT_CONNECTION_OP:
400 
401         Aml.Opcode = AML_FIELD_CONNECTION_OP;
402         break;
403 
404     default:
405 
406         Aml.Opcode = Op->Asl.AmlOpcode;
407         break;
408     }
409 
410 
411     switch (Aml.Opcode)
412     {
413     case AML_PACKAGE_LENGTH:
414 
415         /* Value is the length to be encoded (Used in field definitions) */
416 
417         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
418         break;
419 
420     default:
421 
422         /* Check for two-byte opcode */
423 
424         if (Aml.Opcode > 0x00FF)
425         {
426             /* Write the high byte first */
427 
428             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
429         }
430 
431         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
432 
433         /* Subtreelength doesn't include length of package length bytes */
434 
435         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
436         break;
437     }
438 
439     /* Does this opcode have an associated "PackageLength" field? */
440 
441     if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
442     {
443         if (Op->Asl.AmlPkgLenBytes == 1)
444         {
445             /* Simplest case -- no bytes to follow, just write the count */
446 
447             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
448         }
449         else if (Op->Asl.AmlPkgLenBytes != 0)
450         {
451             /*
452              * Encode the "bytes to follow" in the first byte, top two bits.
453              * The low-order nybble of the length is in the bottom 4 bits
454              */
455             PkgLenFirstByte = (UINT8)
456                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
457                 (PkgLen.LenBytes[0] & 0x0F));
458 
459             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
460 
461             /*
462              * Shift the length over by the 4 bits we just stuffed
463              * in the first byte
464              */
465             PkgLen.Len >>= 4;
466 
467             /*
468              * Now we can write the remaining bytes -
469              * either 1, 2, or 3 bytes
470              */
471             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
472             {
473                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
474             }
475         }
476     }
477 
478     switch (Aml.Opcode)
479     {
480     case AML_BYTE_OP:
481 
482         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
483         break;
484 
485     case AML_WORD_OP:
486 
487         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
488        break;
489 
490     case AML_DWORD_OP:
491 
492         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
493         break;
494 
495     case AML_QWORD_OP:
496 
497         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
498         break;
499 
500     case AML_STRING_OP:
501 
502         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
503         break;
504 
505     default:
506 
507         /* All data opcodes must appear above */
508 
509         break;
510     }
511 }
512 
513 
514 /*******************************************************************************
515  *
516  * FUNCTION:    CgWriteTableHeader
517  *
518  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
519  *
520  * RETURN:      None
521  *
522  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
523  *
524  ******************************************************************************/
525 
526 static void
527 CgWriteTableHeader (
528     ACPI_PARSE_OBJECT       *Op)
529 {
530     ACPI_PARSE_OBJECT       *Child;
531     UINT32                  CommentLength;
532     ACPI_COMMENT_NODE       *Current;
533 
534 
535     /* AML filename */
536 
537     Child = Op->Asl.Child;
538 
539     /* Signature */
540 
541     Child = Child->Asl.Next;
542 
543     /*
544      * For ASL-/ASL+ converter: replace the table signature with
545      * "XXXX" and save the original table signature. This results in an AML
546      * file with the signature "XXXX". The converter should remove this AML
547      * file. In the event where this AML file does not get deleted, the
548      * "XXXX" table signature prevents this AML file from running on the AML
549      * interpreter.
550      */
551     if (Gbl_CaptureComments)
552     {
553         strncpy(AcpiGbl_TableSig, Child->Asl.Value.String, 4);
554         Child->Asl.Value.String = ACPI_SIG_XXXX;
555     }
556 
557     strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
558 
559     /* Revision */
560 
561     Child = Child->Asl.Next;
562     TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
563 
564     /* Command-line Revision override */
565 
566     if (Gbl_RevisionOverride)
567     {
568         TableHeader.Revision = Gbl_RevisionOverride;
569     }
570 
571     /* OEMID */
572 
573     Child = Child->Asl.Next;
574     strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
575 
576     /* OEM TableID */
577 
578     Child = Child->Asl.Next;
579     strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
580 
581     /* OEM Revision */
582 
583     Child = Child->Asl.Next;
584     TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
585 
586     /* Compiler ID */
587 
588     ACPI_MOVE_NAME (TableHeader.AslCompilerId, ASL_CREATOR_ID);
589 
590     /* Compiler version */
591 
592     TableHeader.AslCompilerRevision = ACPI_CA_VERSION;
593 
594     /* Table length. Checksum zero for now, will rewrite later */
595 
596     TableHeader.Length = sizeof (ACPI_TABLE_HEADER) +
597         Op->Asl.AmlSubtreeLength;
598 
599     /* Calculate the comment lengths for this definition block parseOp */
600 
601     if (Gbl_CaptureComments)
602     {
603         CvDbgPrint ("Calculating comment lengths for %s in write header\n",
604             Op->Asl.ParseOpName);
605 
606         /*
607          * Take the filename without extensions, add 3 for the new extension
608          * and another 3 for the a908 bytecode and null terminator.
609          */
610         TableHeader.Length += strrchr (Gbl_ParseTreeRoot->Asl.Filename, '.')
611             - Gbl_ParseTreeRoot->Asl.Filename + 1 + 3 + 3;
612         Op->Asl.AmlSubtreeLength +=
613             strlen (Gbl_ParseTreeRoot->Asl.Filename) + 3;
614     CvDbgPrint ("     Length: %lu\n",
615             strlen (Gbl_ParseTreeRoot->Asl.Filename) + 3);
616 
617         if (Op->Asl.CommentList)
618         {
619             Current = Op->Asl.CommentList;
620             while (Current)
621             {
622                 CommentLength = strlen (Current->Comment)+3;
623                 CvDbgPrint ("Length of standard comment): %d\n", CommentLength);
624                 CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
625                 TableHeader.Length += CommentLength;
626                 Op->Asl.AmlSubtreeLength += CommentLength;
627                 Current = Current->Next;
628             CvDbgPrint ("    Length: %u\n", CommentLength);
629             }
630         }
631         if (Op->Asl.CloseBraceComment)
632         {
633             CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
634             CvDbgPrint ("Length of inline comment +3: %d\n", CommentLength);
635             CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
636             TableHeader.Length += CommentLength;
637             Op->Asl.AmlSubtreeLength += CommentLength;
638         CvDbgPrint ("    Length: %u\n", CommentLength);
639         }
640     }
641 
642     TableHeader.Checksum = 0;
643 
644     Op->Asl.FinalAmlOffset = ftell (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
645 
646     /* Write entire header and clear the table header global */
647 
648     CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
649     memset (&TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
650 }
651 
652 
653 /*******************************************************************************
654  *
655  * FUNCTION:    CgUpdateHeader
656  *
657  * PARAMETERS:  Op                  - Op for the Definition Block
658  *
659  * RETURN:      None.
660  *
661  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
662  *              re-writing the header for the input definition block
663  *
664  ******************************************************************************/
665 
666 static void
667 CgUpdateHeader (
668     ACPI_PARSE_OBJECT       *Op)
669 {
670     signed char             Sum;
671     UINT32                  i;
672     UINT32                  Length;
673     UINT8                   FileByte;
674     UINT8                   Checksum;
675 
676 
677     /* Calculate the checksum over the entire definition block */
678 
679     Sum = 0;
680     Length = sizeof (ACPI_TABLE_HEADER) + Op->Asl.AmlSubtreeLength;
681     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
682 
683     for (i = 0; i < Length; i++)
684     {
685         if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
686         {
687             printf ("EOF while reading checksum bytes\n");
688             return;
689         }
690 
691         Sum = (signed char) (Sum + FileByte);
692     }
693 
694     Checksum = (UINT8) (0 - Sum);
695 
696     /* Re-write the the checksum byte */
697 
698     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
699         ACPI_OFFSET (ACPI_TABLE_HEADER, Checksum));
700 
701     FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
702 }
703 
704 
705 /*******************************************************************************
706  *
707  * FUNCTION:    CgCloseTable
708  *
709  * PARAMETERS:  None.
710  *
711  * RETURN:      None.
712  *
713  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
714  *              re-writing each table header. This allows support for
715  *              multiple definition blocks in a single source file.
716  *
717  ******************************************************************************/
718 
719 static void
720 CgCloseTable (
721     void)
722 {
723     ACPI_PARSE_OBJECT   *Op;
724 
725 
726     /* Process all definition blocks */
727 
728     Op = Gbl_ParseTreeRoot->Asl.Child;
729     while (Op)
730     {
731         CgUpdateHeader (Op);
732         Op = Op->Asl.Next;
733     }
734 }
735 
736 
737 /*******************************************************************************
738  *
739  * FUNCTION:    CgWriteNode
740  *
741  * PARAMETERS:  Op            - Parse node to write.
742  *
743  * RETURN:      None.
744  *
745  * DESCRIPTION: Write the AML that corresponds to a parse node.
746  *
747  ******************************************************************************/
748 
749 static void
750 CgWriteNode (
751     ACPI_PARSE_OBJECT       *Op)
752 {
753     ASL_RESOURCE_NODE       *Rnode;
754 
755 
756     /* Write all comments here. */
757     if (Gbl_CaptureComments)
758     {
759         CgWriteAmlComment(Op);
760     }
761 
762     /* Always check for DEFAULT_ARG and other "Noop" nodes */
763     /* TBD: this may not be the best place for this check */
764 
765     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
766         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
767         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
768     {
769         return;
770     }
771 
772     if ((Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) &&
773         Gbl_DoExternals == FALSE)
774     {
775         return;
776     }
777 
778     Op->Asl.FinalAmlLength = 0;
779 
780     switch (Op->Asl.AmlOpcode)
781     {
782     case AML_RAW_DATA_BYTE:
783     case AML_RAW_DATA_WORD:
784     case AML_RAW_DATA_DWORD:
785     case AML_RAW_DATA_QWORD:
786 
787         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
788         return;
789 
790 
791     case AML_RAW_DATA_BUFFER:
792 
793         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
794         return;
795 
796 
797     case AML_RAW_DATA_CHAIN:
798 
799         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
800         while (Rnode)
801         {
802             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
803             Rnode = Rnode->Next;
804         }
805         return;
806 
807     default:
808 
809         /* Internal data opcodes must all appear above */
810 
811         break;
812     }
813 
814     switch (Op->Asl.ParseOpcode)
815     {
816     case PARSEOP_DEFAULT_ARG:
817 
818         break;
819 
820     case PARSEOP_DEFINITION_BLOCK:
821 
822         CgWriteTableHeader (Op);
823         if (Gbl_CaptureComments)
824         {
825             CgWriteAmlDefBlockComment (Op);
826         }
827         break;
828 
829     case PARSEOP_NAMESEG:
830     case PARSEOP_NAMESTRING:
831     case PARSEOP_METHODCALL:
832 
833         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
834         break;
835 
836     default:
837 
838         CgWriteAmlOpcode (Op);
839         break;
840     }
841 }
842