2 /******************************************************************************
4 * Module Name: aslfiles - file I/O suppoert
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2009, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
118 #include <contrib/dev/acpica/include/acapps.h>
120 #define _COMPONENT ACPI_COMPILER
121 ACPI_MODULE_NAME ("aslfiles")
123 /* Local prototypes */
132 FlOpenIncludeWithPrefix (
137 #ifdef ACPI_OBSOLETE_FUNCTIONS
139 FlParseInputPathname (
140 char *InputFilename);
144 /*******************************************************************************
152 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
155 ******************************************************************************/
162 AePrintErrorLog (ASL_FILE_STDOUT);
165 /* Print error summary to the debug file */
167 AePrintErrorLog (ASL_FILE_STDERR);
174 /*******************************************************************************
176 * FUNCTION: FlFileError
178 * PARAMETERS: FileId - Index into file info array
179 * ErrorId - Index into error message array
183 * DESCRIPTION: Decode errno to an error message and add the entire error
186 ******************************************************************************/
194 sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
196 AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
200 /*******************************************************************************
202 * FUNCTION: FlOpenFile
204 * PARAMETERS: FileId - Index into file info array
205 * Filename - file pathname to open
206 * Mode - Open mode for fopen
210 * DESCRIPTION: Open a file.
211 * NOTE: Aborts compiler on any error.
213 ******************************************************************************/
224 File = fopen (Filename, Mode);
226 Gbl_Files[FileId].Filename = Filename;
227 Gbl_Files[FileId].Handle = File;
231 FlFileError (FileId, ASL_MSG_OPEN);
237 /*******************************************************************************
239 * FUNCTION: FlReadFile
241 * PARAMETERS: FileId - Index into file info array
242 * Buffer - Where to place the data
243 * Length - Amount to read
245 * RETURN: Status. AE_ERROR indicates EOF.
247 * DESCRIPTION: Read data from an open file.
248 * NOTE: Aborts compiler on any error.
250 ******************************************************************************/
261 /* Read and check for error */
263 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
264 if (Actual != Length)
266 if (feof (Gbl_Files[FileId].Handle))
268 /* End-of-file, just return error */
273 FlFileError (FileId, ASL_MSG_READ);
281 /*******************************************************************************
283 * FUNCTION: FlWriteFile
285 * PARAMETERS: FileId - Index into file info array
286 * Buffer - Data to write
287 * Length - Amount of data to write
291 * DESCRIPTION: Write data to an open file.
292 * NOTE: Aborts compiler on any error.
294 ******************************************************************************/
305 /* Write and check for error */
307 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
308 if (Actual != Length)
310 FlFileError (FileId, ASL_MSG_WRITE);
316 /*******************************************************************************
318 * FUNCTION: FlPrintFile
320 * PARAMETERS: FileId - Index into file info array
321 * Format - Printf format string
322 * ... - Printf arguments
326 * DESCRIPTION: Formatted write to an open file.
327 * NOTE: Aborts compiler on any error.
329 ******************************************************************************/
341 va_start (Args, Format);
343 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
348 FlFileError (FileId, ASL_MSG_WRITE);
354 /*******************************************************************************
356 * FUNCTION: FlSeekFile
358 * PARAMETERS: FileId - Index into file info array
359 * Offset - Absolute byte offset in file
363 * DESCRIPTION: Seek to absolute offset
364 * NOTE: Aborts compiler on any error.
366 ******************************************************************************/
376 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
379 FlFileError (FileId, ASL_MSG_SEEK);
385 /*******************************************************************************
387 * FUNCTION: FlCloseFile
389 * PARAMETERS: FileId - Index into file info array
393 * DESCRIPTION: Close an open file. Aborts compiler on error
395 ******************************************************************************/
404 if (!Gbl_Files[FileId].Handle)
409 Error = fclose (Gbl_Files[FileId].Handle);
410 Gbl_Files[FileId].Handle = NULL;
414 FlFileError (FileId, ASL_MSG_CLOSE);
422 /*******************************************************************************
424 * FUNCTION: FlSetLineNumber
426 * PARAMETERS: Op - Parse node for the LINE asl statement
430 * DESCRIPTION: Set the current line number
432 ******************************************************************************/
436 ACPI_PARSE_OBJECT *Op)
439 Gbl_CurrentLineNumber = (UINT32) Op->Asl.Value.Integer;
440 Gbl_LogicalLineNumber = (UINT32) Op->Asl.Value.Integer;
444 /*******************************************************************************
446 * FUNCTION: FlAddIncludeDirectory
448 * PARAMETERS: Dir - Directory pathname string
452 * DESCRIPTION: Add a directory the list of include prefix directories.
454 ******************************************************************************/
457 FlAddIncludeDirectory (
460 ASL_INCLUDE_DIR *NewDir;
461 ASL_INCLUDE_DIR *NextDir;
462 ASL_INCLUDE_DIR *PrevDir = NULL;
463 UINT32 NeedsSeparator = 0;
467 DirLength = strlen (Dir);
473 /* Make sure that the pathname ends with a path separator */
475 if ((Dir[DirLength-1] != '/') &&
476 (Dir[DirLength-1] != '\\'))
481 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
482 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
483 strcpy (NewDir->Dir, Dir);
486 strcat (NewDir->Dir, "/");
490 * Preserve command line ordering of -I options by adding new elements
491 * at the end of the list
493 NextDir = Gbl_IncludeDirList;
497 NextDir = NextDir->Next;
502 PrevDir->Next = NewDir;
506 Gbl_IncludeDirList = NewDir;
511 /*******************************************************************************
513 * FUNCTION: FlOpenIncludeWithPrefix
515 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
517 * Filename - The include filename from the source ASL.
519 * RETURN: Valid file descriptor if successful. Null otherwise.
521 * DESCRIPTION: Open an include file and push it on the input file stack.
523 ******************************************************************************/
526 FlOpenIncludeWithPrefix (
534 /* Build the full pathname to the file */
536 Pathname = ACPI_ALLOCATE (strlen (PrefixDir) + strlen (Filename) + 1);
538 strcpy (Pathname, PrefixDir);
539 strcat (Pathname, Filename);
541 DbgPrint (ASL_PARSE_OUTPUT, "\nAttempt to open include file: path %s\n\n",
544 /* Attempt to open the file, push if successful */
546 IncludeFile = fopen (Pathname, "r");
549 /* Push the include file on the open input file stack */
551 AslPushInputFileStack (IncludeFile, Pathname);
552 return (IncludeFile);
555 ACPI_FREE (Pathname);
560 /*******************************************************************************
562 * FUNCTION: FlOpenIncludeFile
564 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
568 * DESCRIPTION: Open an include file and push it on the input file stack.
570 ******************************************************************************/
574 ACPI_PARSE_OBJECT *Op)
577 ASL_INCLUDE_DIR *NextDir;
580 /* Op must be valid */
584 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
585 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
586 Gbl_InputByteCount, Gbl_CurrentColumn,
587 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
593 * Flush out the "include ()" statement on this line, start
594 * the actual include file on the next line
596 ResetCurrentLineBuffer ();
597 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
598 Gbl_CurrentLineOffset++;
601 /* Attempt to open the include file */
603 /* If the file specifies an absolute path, just open it */
605 if ((Op->Asl.Value.String[0] == '/') ||
606 (Op->Asl.Value.String[0] == '\\') ||
607 (Op->Asl.Value.String[1] == ':'))
609 IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
618 * The include filename is not an absolute path.
620 * First, search for the file within the "local" directory -- meaning
621 * the same directory that contains the source file.
623 * Construct the file pathname from the global directory name.
625 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
632 * Second, search for the file within the (possibly multiple) directories
633 * specified by the -I option on the command line.
635 NextDir = Gbl_IncludeDirList;
638 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
644 NextDir = NextDir->Next;
647 /* We could not open the include file after trying very hard */
650 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
651 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
655 /*******************************************************************************
657 * FUNCTION: FlOpenInputFile
659 * PARAMETERS: InputFilename - The user-specified ASL source file to be
664 * DESCRIPTION: Open the specified input file, and save the directory path to
665 * the file so that include files can be opened in
666 * the same directory.
668 ******************************************************************************/
675 /* Open the input ASL file, text mode */
677 FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
678 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
684 /*******************************************************************************
686 * FUNCTION: FlOpenAmlOutputFile
688 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
692 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
693 * is created in the same directory as the parent input file.
695 ******************************************************************************/
698 FlOpenAmlOutputFile (
699 char *FilenamePrefix)
704 /* Output filename usually comes from the ASL itself */
706 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
709 /* Create the output AML filename */
711 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
714 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
715 0, 0, 0, 0, NULL, NULL);
720 /* Open the output AML file in binary mode */
722 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
727 /*******************************************************************************
729 * FUNCTION: FlOpenMiscOutputFiles
731 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
735 * DESCRIPTION: Create and open the various output files needed, depending on
736 * the command line options
738 ******************************************************************************/
741 FlOpenMiscOutputFiles (
742 char *FilenamePrefix)
747 /* Create/Open a combined source output file */
749 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
752 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
753 0, 0, 0, 0, NULL, NULL);
758 * Open the source output file, binary mode (so that LF does not get
759 * expanded to CR/LF on some systems, messing up our seek
762 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
764 /* Create/Open a listing output file if asked */
768 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
771 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
772 0, 0, 0, 0, NULL, NULL);
776 /* Open the listing file, text mode */
778 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
780 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
781 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
784 /* Create/Open a assembly code source output file if asked */
786 if (Gbl_AsmOutputFlag)
788 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
791 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
792 0, 0, 0, 0, NULL, NULL);
796 /* Open the assembly code source file, text mode */
798 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
800 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
801 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
804 /* Create/Open a C code source output file if asked */
806 if (Gbl_C_OutputFlag)
808 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
811 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
812 0, 0, 0, 0, NULL, NULL);
816 /* Open the C code source file, text mode */
818 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
820 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
821 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
822 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
825 /* Create/Open a assembly include output file if asked */
827 if (Gbl_AsmIncludeOutputFlag)
829 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
832 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
833 0, 0, 0, 0, NULL, NULL);
837 /* Open the assembly include file, text mode */
839 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
841 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
842 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
845 /* Create/Open a C include output file if asked */
847 if (Gbl_C_IncludeOutputFlag)
849 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
852 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
853 0, 0, 0, 0, NULL, NULL);
857 /* Open the C include file, text mode */
859 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
861 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
862 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
863 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
866 /* Create/Open a hex output file if asked */
868 if (Gbl_HexOutputFlag)
870 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
873 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
874 0, 0, 0, 0, NULL, NULL);
878 /* Open the hex file, text mode */
880 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
882 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
883 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
886 /* Create a namespace output file if asked */
888 if (Gbl_NsOutputFlag)
890 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
893 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
894 0, 0, 0, 0, NULL, NULL);
898 /* Open the namespace file, text mode */
900 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
902 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
903 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
906 /* Create/Open a debug output file if asked */
910 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
913 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
914 0, 0, 0, 0, NULL, NULL);
918 /* Open the debug file as STDERR, text mode */
920 /* TBD: hide this behind a FlReopenFile function */
922 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
923 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
924 freopen (Filename, "w+t", stderr);
926 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
927 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
934 #ifdef ACPI_OBSOLETE_FUNCTIONS
935 /*******************************************************************************
937 * FUNCTION: FlParseInputPathname
939 * PARAMETERS: InputFilename - The user-specified ASL source file to be
944 * DESCRIPTION: Split the input path into a directory and filename part
945 * 1) Directory part used to open include files
946 * 2) Filename part used to generate output filenames
948 ******************************************************************************/
951 FlParseInputPathname (
962 /* Get the path to the input filename's directory */
964 Gbl_DirectoryPath = strdup (InputFilename);
965 if (!Gbl_DirectoryPath)
967 return (AE_NO_MEMORY);
970 Substring = strrchr (Gbl_DirectoryPath, '\\');
973 Substring = strrchr (Gbl_DirectoryPath, '/');
976 Substring = strrchr (Gbl_DirectoryPath, ':');
982 Gbl_DirectoryPath[0] = 0;
983 if (Gbl_UseDefaultAmlFilename)
985 Gbl_OutputFilenamePrefix = strdup (InputFilename);
990 if (Gbl_UseDefaultAmlFilename)
992 Gbl_OutputFilenamePrefix = strdup (Substring + 1);