2 /******************************************************************************
4 * Module Name: aslfiles - file I/O suppoert
7 *****************************************************************************/
9 /******************************************************************************
13 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14 * All rights reserved.
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
74 * 3.4. Intel retains all right, title, and interest in and to the Original
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
118 #include "aslcompiler.h"
121 #define _COMPONENT ACPI_COMPILER
122 ACPI_MODULE_NAME ("aslfiles")
125 /*******************************************************************************
133 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
136 ******************************************************************************/
142 AePrintErrorLog (ASL_FILE_STDOUT);
145 /* Print error summary to the debug file */
147 AePrintErrorLog (ASL_FILE_STDERR);
154 /*******************************************************************************
156 * FUNCTION: FlOpenLocalFile
158 * PARAMETERS: LocalName - Single filename (not a pathname)
159 * Mode - Open mode for fopen
161 * RETURN: File descriptor
163 * DESCRIPTION: Build a complete pathname for the input filename and open
166 ******************************************************************************/
174 strcpy (StringBuffer, Gbl_DirectoryPath);
175 strcat (StringBuffer, LocalName);
177 DbgPrint (ASL_PARSE_OUTPUT, "FlOpenLocalFile: %s\n", StringBuffer);
178 return (fopen (StringBuffer, (const char *) Mode));
182 /*******************************************************************************
184 * FUNCTION: FlFileError
186 * PARAMETERS: FileId - Index into file info array
187 * ErrorId - Index into error message array
191 * DESCRIPTION: Decode errno to an error message and add the entire error
194 ******************************************************************************/
202 sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename, strerror (errno));
203 AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
207 /*******************************************************************************
209 * FUNCTION: FlOpenFile
211 * PARAMETERS: FileId - Index into file info array
212 * Filename - file pathname to open
213 * Mode - Open mode for fopen
215 * RETURN: File descriptor
217 * DESCRIPTION: Open a file.
218 * NOTE: Aborts compiler on any error.
220 ******************************************************************************/
231 File = fopen (Filename, Mode);
233 Gbl_Files[FileId].Filename = Filename;
234 Gbl_Files[FileId].Handle = File;
238 FlFileError (FileId, ASL_MSG_OPEN);
244 /*******************************************************************************
246 * FUNCTION: FlReadFile
248 * PARAMETERS: FileId - Index into file info array
249 * Buffer - Where to place the data
250 * Length - Amount to read
252 * RETURN: Status. AE_ERROR indicates EOF.
254 * DESCRIPTION: Read data from an open file.
255 * NOTE: Aborts compiler on any error.
257 ******************************************************************************/
268 /* Read and check for error */
270 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
271 if (Actual != Length)
273 if (feof (Gbl_Files[FileId].Handle))
275 /* End-of-file, just return error */
280 FlFileError (FileId, ASL_MSG_READ);
288 /*******************************************************************************
290 * FUNCTION: FlWriteFile
292 * PARAMETERS: FileId - Index into file info array
293 * Buffer - Data to write
294 * Length - Amount of data to write
298 * DESCRIPTION: Write data to an open file.
299 * NOTE: Aborts compiler on any error.
301 ******************************************************************************/
312 /* Write and check for error */
314 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
315 if (Actual != Length)
317 FlFileError (FileId, ASL_MSG_WRITE);
323 /*******************************************************************************
325 * FUNCTION: FlPrintFile
327 * PARAMETERS: FileId - Index into file info array
328 * Format - Printf format string
329 * ... - Printf arguments
333 * DESCRIPTION: Formatted write to an open file.
334 * NOTE: Aborts compiler on any error.
336 ******************************************************************************/
348 va_start (Args, Format);
350 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
353 FlFileError (FileId, ASL_MSG_WRITE);
359 /*******************************************************************************
361 * FUNCTION: FlSeekFile
363 * PARAMETERS: FileId - Index into file info array
364 * Offset - Absolute byte offset in file
368 * DESCRIPTION: Seek to absolute offset
369 * NOTE: Aborts compiler on any error.
371 ******************************************************************************/
381 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
384 FlFileError (FileId, ASL_MSG_SEEK);
390 /*******************************************************************************
392 * FUNCTION: FlCloseFile
394 * PARAMETERS: FileId - Index into file info array
398 * DESCRIPTION: Close an open file. Aborts compiler on error
400 ******************************************************************************/
409 if (!Gbl_Files[FileId].Handle)
414 Error = fclose (Gbl_Files[FileId].Handle);
415 Gbl_Files[FileId].Handle = NULL;
419 FlFileError (FileId, ASL_MSG_CLOSE);
427 /*******************************************************************************
429 * FUNCTION: FlSetLineNumber
431 * PARAMETERS: Op - Parse node for the LINE asl statement
435 * DESCRIPTION: Set the current line number
437 ******************************************************************************/
441 ACPI_PARSE_OBJECT *Op)
444 Gbl_CurrentLineNumber = (UINT32) Op->Asl.Value.Integer;
445 Gbl_LogicalLineNumber = (UINT32) Op->Asl.Value.Integer;
449 /*******************************************************************************
451 * FUNCTION: FlOpenIncludeFile
453 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
457 * DESCRIPTION: Open an include file and push it on the input file stack.
459 ******************************************************************************/
463 ACPI_PARSE_OBJECT *Op)
468 /* Op must be valid */
472 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
473 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
474 Gbl_InputByteCount, Gbl_CurrentColumn,
475 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
481 * Flush out the "include ()" statement on this line, start
482 * the actual include file on the next line
484 ResetCurrentLineBuffer ();
485 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
486 Gbl_CurrentLineOffset++;
488 /* Prepend the directory pathname and open the include file */
490 DbgPrint (ASL_PARSE_OUTPUT, "\nOpen include file: path %s\n\n", Op->Asl.Value.String);
491 IncFile = FlOpenLocalFile (Op->Asl.Value.String, "r");
494 sprintf (MsgBuffer, "%s (%s)", Op->Asl.Value.String, strerror (errno));
495 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
499 /* Push the include file on the open input file stack */
501 AslPushInputFileStack (IncFile, Op->Asl.Value.String);
505 /*******************************************************************************
507 * FUNCTION: FlParseInputPathname
509 * PARAMETERS: InputFilename - The user-specified ASL source file to be
514 * DESCRIPTION: Split the input path into a directory and filename part
515 * 1) Directory part used to open include files
516 * 2) Filename part used to generate output filenames
518 ******************************************************************************/
521 FlParseInputPathname (
532 /* Get the path to the input filename's directory */
534 Gbl_DirectoryPath = strdup (InputFilename);
535 if (!Gbl_DirectoryPath)
537 return (AE_NO_MEMORY);
540 Substring = strrchr (Gbl_DirectoryPath, '\\');
543 Substring = strrchr (Gbl_DirectoryPath, '/');
546 Substring = strrchr (Gbl_DirectoryPath, ':');
552 Gbl_DirectoryPath[0] = 0;
553 if (Gbl_UseDefaultAmlFilename)
555 Gbl_OutputFilenamePrefix = strdup (InputFilename);
560 if (Gbl_UseDefaultAmlFilename)
562 Gbl_OutputFilenamePrefix = strdup (Substring + 1);
571 /*******************************************************************************
573 * FUNCTION: FlOpenInputFile
575 * PARAMETERS: InputFilename - The user-specified ASL source file to be
580 * DESCRIPTION: Open the specified input file, and save the directory path to
581 * the file so that include files can be opened in
582 * the same directory.
584 ******************************************************************************/
592 /* Open the input ASL file, text mode */
594 FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
595 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
601 /*******************************************************************************
603 * FUNCTION: FlOpenAmlOutputFile
605 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
609 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
610 * is created in the same directory as the parent input file.
612 ******************************************************************************/
615 FlOpenAmlOutputFile (
616 char *FilenamePrefix)
621 /* Output filename usually comes from the ASL itself */
623 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
626 /* Create the output AML filename */
628 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
631 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME, 0, 0, 0, 0, NULL, NULL);
636 /* Open the output AML file in binary mode */
638 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
643 /*******************************************************************************
645 * FUNCTION: FlOpenMiscOutputFiles
647 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
651 * DESCRIPTION: Create and open the various output files needed, depending on
652 * the command line options
654 ******************************************************************************/
657 FlOpenMiscOutputFiles (
658 char *FilenamePrefix)
663 /* Create/Open a combined source output file */
665 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
668 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
673 * Open the source output file, binary mode (so that LF does not get
674 * expanded to CR/LF on some systems, messing up our seek
677 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
679 /* Create/Open a listing output file if asked */
683 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
686 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
690 /* Open the listing file, text mode */
692 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
694 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
695 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
698 /* Create/Open a assembly code source output file if asked */
700 if (Gbl_AsmOutputFlag)
702 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
705 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
709 /* Open the assembly code source file, text mode */
711 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
713 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
714 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
717 /* Create/Open a C code source output file if asked */
719 if (Gbl_C_OutputFlag)
721 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
724 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
728 /* Open the C code source file, text mode */
730 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
732 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
733 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
734 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
737 /* Create/Open a assembly include output file if asked */
739 if (Gbl_AsmIncludeOutputFlag)
741 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
744 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
748 /* Open the assembly include file, text mode */
750 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
752 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
753 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
756 /* Create/Open a C include output file if asked */
758 if (Gbl_C_IncludeOutputFlag)
760 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
763 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
767 /* Open the C include file, text mode */
769 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
771 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
772 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
773 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
776 /* Create/Open a hex output file if asked */
778 if (Gbl_HexOutputFlag)
780 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
783 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
787 /* Open the hex file, text mode */
789 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
791 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
792 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
795 /* Create a namespace output file if asked */
797 if (Gbl_NsOutputFlag)
799 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
802 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME, 0, 0, 0, 0, NULL, NULL);
806 /* Open the namespace file, text mode */
808 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
810 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
811 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
814 /* Create/Open a debug output file if asked */
818 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
821 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME, 0, 0, 0, 0, NULL, NULL);
825 /* Open the debug file as STDERR, text mode */
827 /* TBD: hide this behind a FlReopenFile function */
829 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
830 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle = freopen (Filename, "w+t", stderr);
832 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
833 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);