2 /******************************************************************************
4 * Module Name: aslfiles - file I/O suppoert
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
117 #include "aslcompiler.h"
120 #define _COMPONENT ACPI_COMPILER
121 ACPI_MODULE_NAME ("aslfiles")
123 /* Local prototypes */
126 FlOpenIncludeWithPrefix (
131 #ifdef ACPI_OBSOLETE_FUNCTIONS
133 FlParseInputPathname (
134 char *InputFilename);
138 /*******************************************************************************
146 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
149 ******************************************************************************/
156 AePrintErrorLog (ASL_FILE_STDOUT);
159 /* Print error summary to the debug file */
161 AePrintErrorLog (ASL_FILE_STDERR);
168 /*******************************************************************************
170 * FUNCTION: FlFileError
172 * PARAMETERS: FileId - Index into file info array
173 * ErrorId - Index into error message array
177 * DESCRIPTION: Decode errno to an error message and add the entire error
180 ******************************************************************************/
188 sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
190 AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
194 /*******************************************************************************
196 * FUNCTION: FlOpenFile
198 * PARAMETERS: FileId - Index into file info array
199 * Filename - file pathname to open
200 * Mode - Open mode for fopen
204 * DESCRIPTION: Open a file.
205 * NOTE: Aborts compiler on any error.
207 ******************************************************************************/
218 File = fopen (Filename, Mode);
220 Gbl_Files[FileId].Filename = Filename;
221 Gbl_Files[FileId].Handle = File;
225 FlFileError (FileId, ASL_MSG_OPEN);
231 /*******************************************************************************
233 * FUNCTION: FlGetFileSize
235 * PARAMETERS: FileId - Index into file info array
239 * DESCRIPTION: Get current file size. Uses seek-to-EOF. File must be open.
241 ******************************************************************************/
251 fp = Gbl_Files[FileId].Handle;
253 fseek (fp, 0, SEEK_END);
254 FileSize = (UINT32) ftell (fp);
255 fseek (fp, 0, SEEK_SET);
261 /*******************************************************************************
263 * FUNCTION: FlReadFile
265 * PARAMETERS: FileId - Index into file info array
266 * Buffer - Where to place the data
267 * Length - Amount to read
269 * RETURN: Status. AE_ERROR indicates EOF.
271 * DESCRIPTION: Read data from an open file.
272 * NOTE: Aborts compiler on any error.
274 ******************************************************************************/
285 /* Read and check for error */
287 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
288 if (Actual != Length)
290 if (feof (Gbl_Files[FileId].Handle))
292 /* End-of-file, just return error */
297 FlFileError (FileId, ASL_MSG_READ);
305 /*******************************************************************************
307 * FUNCTION: FlWriteFile
309 * PARAMETERS: FileId - Index into file info array
310 * Buffer - Data to write
311 * Length - Amount of data to write
315 * DESCRIPTION: Write data to an open file.
316 * NOTE: Aborts compiler on any error.
318 ******************************************************************************/
329 /* Write and check for error */
331 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
332 if (Actual != Length)
334 FlFileError (FileId, ASL_MSG_WRITE);
340 /*******************************************************************************
342 * FUNCTION: FlPrintFile
344 * PARAMETERS: FileId - Index into file info array
345 * Format - Printf format string
346 * ... - Printf arguments
350 * DESCRIPTION: Formatted write to an open file.
351 * NOTE: Aborts compiler on any error.
353 ******************************************************************************/
365 va_start (Args, Format);
367 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
372 FlFileError (FileId, ASL_MSG_WRITE);
378 /*******************************************************************************
380 * FUNCTION: FlSeekFile
382 * PARAMETERS: FileId - Index into file info array
383 * Offset - Absolute byte offset in file
387 * DESCRIPTION: Seek to absolute offset
388 * NOTE: Aborts compiler on any error.
390 ******************************************************************************/
400 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
403 FlFileError (FileId, ASL_MSG_SEEK);
409 /*******************************************************************************
411 * FUNCTION: FlCloseFile
413 * PARAMETERS: FileId - Index into file info array
417 * DESCRIPTION: Close an open file. Aborts compiler on error
419 ******************************************************************************/
428 if (!Gbl_Files[FileId].Handle)
433 Error = fclose (Gbl_Files[FileId].Handle);
434 Gbl_Files[FileId].Handle = NULL;
438 FlFileError (FileId, ASL_MSG_CLOSE);
446 /*******************************************************************************
448 * FUNCTION: FlSetLineNumber
450 * PARAMETERS: Op - Parse node for the LINE asl statement
454 * DESCRIPTION: Set the current line number
456 ******************************************************************************/
460 ACPI_PARSE_OBJECT *Op)
463 Gbl_CurrentLineNumber = (UINT32) Op->Asl.Value.Integer;
464 Gbl_LogicalLineNumber = (UINT32) Op->Asl.Value.Integer;
468 /*******************************************************************************
470 * FUNCTION: FlAddIncludeDirectory
472 * PARAMETERS: Dir - Directory pathname string
476 * DESCRIPTION: Add a directory the list of include prefix directories.
478 ******************************************************************************/
481 FlAddIncludeDirectory (
484 ASL_INCLUDE_DIR *NewDir;
485 ASL_INCLUDE_DIR *NextDir;
486 ASL_INCLUDE_DIR *PrevDir = NULL;
487 UINT32 NeedsSeparator = 0;
491 DirLength = strlen (Dir);
497 /* Make sure that the pathname ends with a path separator */
499 if ((Dir[DirLength-1] != '/') &&
500 (Dir[DirLength-1] != '\\'))
505 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
506 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
507 strcpy (NewDir->Dir, Dir);
510 strcat (NewDir->Dir, "/");
514 * Preserve command line ordering of -I options by adding new elements
515 * at the end of the list
517 NextDir = Gbl_IncludeDirList;
521 NextDir = NextDir->Next;
526 PrevDir->Next = NewDir;
530 Gbl_IncludeDirList = NewDir;
535 /*******************************************************************************
537 * FUNCTION: FlOpenIncludeWithPrefix
539 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
541 * Filename - The include filename from the source ASL.
543 * RETURN: Valid file descriptor if successful. Null otherwise.
545 * DESCRIPTION: Open an include file and push it on the input file stack.
547 ******************************************************************************/
550 FlOpenIncludeWithPrefix (
558 /* Build the full pathname to the file */
560 Pathname = ACPI_ALLOCATE (strlen (PrefixDir) + strlen (Filename) + 1);
562 strcpy (Pathname, PrefixDir);
563 strcat (Pathname, Filename);
565 DbgPrint (ASL_PARSE_OUTPUT, "\nAttempt to open include file: path %s\n\n",
568 /* Attempt to open the file, push if successful */
570 IncludeFile = fopen (Pathname, "r");
573 /* Push the include file on the open input file stack */
575 AslPushInputFileStack (IncludeFile, Pathname);
576 return (IncludeFile);
579 ACPI_FREE (Pathname);
584 /*******************************************************************************
586 * FUNCTION: FlOpenIncludeFile
588 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
592 * DESCRIPTION: Open an include file and push it on the input file stack.
594 ******************************************************************************/
598 ACPI_PARSE_OBJECT *Op)
601 ASL_INCLUDE_DIR *NextDir;
604 /* Op must be valid */
608 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
609 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
610 Gbl_InputByteCount, Gbl_CurrentColumn,
611 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
617 * Flush out the "include ()" statement on this line, start
618 * the actual include file on the next line
620 ResetCurrentLineBuffer ();
621 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
622 Gbl_CurrentLineOffset++;
625 /* Attempt to open the include file */
627 /* If the file specifies an absolute path, just open it */
629 if ((Op->Asl.Value.String[0] == '/') ||
630 (Op->Asl.Value.String[0] == '\\') ||
631 (Op->Asl.Value.String[1] == ':'))
633 IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
642 * The include filename is not an absolute path.
644 * First, search for the file within the "local" directory -- meaning
645 * the same directory that contains the source file.
647 * Construct the file pathname from the global directory name.
649 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
656 * Second, search for the file within the (possibly multiple) directories
657 * specified by the -I option on the command line.
659 NextDir = Gbl_IncludeDirList;
662 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
668 NextDir = NextDir->Next;
671 /* We could not open the include file after trying very hard */
674 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
675 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
679 /*******************************************************************************
681 * FUNCTION: FlOpenInputFile
683 * PARAMETERS: InputFilename - The user-specified ASL source file to be
688 * DESCRIPTION: Open the specified input file, and save the directory path to
689 * the file so that include files can be opened in
690 * the same directory.
692 ******************************************************************************/
699 /* Open the input ASL file, text mode */
701 FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
702 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
708 /*******************************************************************************
710 * FUNCTION: FlOpenAmlOutputFile
712 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
716 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
717 * is created in the same directory as the parent input file.
719 ******************************************************************************/
722 FlOpenAmlOutputFile (
723 char *FilenamePrefix)
728 /* Output filename usually comes from the ASL itself */
730 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
733 /* Create the output AML filename */
735 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
738 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
739 0, 0, 0, 0, NULL, NULL);
744 /* Open the output AML file in binary mode */
746 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
751 /*******************************************************************************
753 * FUNCTION: FlOpenMiscOutputFiles
755 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
759 * DESCRIPTION: Create and open the various output files needed, depending on
760 * the command line options
762 ******************************************************************************/
765 FlOpenMiscOutputFiles (
766 char *FilenamePrefix)
771 /* Create/Open a hex output file if asked */
773 if (Gbl_HexOutputFlag)
775 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
778 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
779 0, 0, 0, 0, NULL, NULL);
783 /* Open the hex file, text mode */
785 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
787 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
788 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
791 /* Create/Open a debug output file if asked */
795 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
798 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
799 0, 0, 0, 0, NULL, NULL);
803 /* Open the debug file as STDERR, text mode */
805 /* TBD: hide this behind a FlReopenFile function */
807 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
808 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
809 freopen (Filename, "w+t", stderr);
811 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
812 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
815 /* Create/Open a listing output file if asked */
819 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
822 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
823 0, 0, 0, 0, NULL, NULL);
827 /* Open the listing file, text mode */
829 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
831 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
832 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
835 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
840 /* Create/Open a combined source output file */
842 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
845 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
846 0, 0, 0, 0, NULL, NULL);
851 * Open the source output file, binary mode (so that LF does not get
852 * expanded to CR/LF on some systems, messing up our seek
855 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
857 /* Create/Open a assembly code source output file if asked */
859 if (Gbl_AsmOutputFlag)
861 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
864 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
865 0, 0, 0, 0, NULL, NULL);
869 /* Open the assembly code source file, text mode */
871 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
873 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
874 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
877 /* Create/Open a C code source output file if asked */
879 if (Gbl_C_OutputFlag)
881 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
884 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
885 0, 0, 0, 0, NULL, NULL);
889 /* Open the C code source file, text mode */
891 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
893 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
894 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
895 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
898 /* Create/Open a assembly include output file if asked */
900 if (Gbl_AsmIncludeOutputFlag)
902 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
905 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
906 0, 0, 0, 0, NULL, NULL);
910 /* Open the assembly include file, text mode */
912 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
914 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
915 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
918 /* Create/Open a C include output file if asked */
920 if (Gbl_C_IncludeOutputFlag)
922 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
925 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
926 0, 0, 0, 0, NULL, NULL);
930 /* Open the C include file, text mode */
932 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
934 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
935 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
936 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
939 /* Create a namespace output file if asked */
941 if (Gbl_NsOutputFlag)
943 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
946 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
947 0, 0, 0, 0, NULL, NULL);
951 /* Open the namespace file, text mode */
953 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
955 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
956 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
963 #ifdef ACPI_OBSOLETE_FUNCTIONS
964 /*******************************************************************************
966 * FUNCTION: FlParseInputPathname
968 * PARAMETERS: InputFilename - The user-specified ASL source file to be
973 * DESCRIPTION: Split the input path into a directory and filename part
974 * 1) Directory part used to open include files
975 * 2) Filename part used to generate output filenames
977 ******************************************************************************/
980 FlParseInputPathname (
991 /* Get the path to the input filename's directory */
993 Gbl_DirectoryPath = strdup (InputFilename);
994 if (!Gbl_DirectoryPath)
996 return (AE_NO_MEMORY);
999 Substring = strrchr (Gbl_DirectoryPath, '\\');
1002 Substring = strrchr (Gbl_DirectoryPath, '/');
1005 Substring = strrchr (Gbl_DirectoryPath, ':');
1011 Gbl_DirectoryPath[0] = 0;
1012 if (Gbl_UseDefaultAmlFilename)
1014 Gbl_OutputFilenamePrefix = strdup (InputFilename);
1019 if (Gbl_UseDefaultAmlFilename)
1021 Gbl_OutputFilenamePrefix = strdup (Substring + 1);