2 /******************************************************************************
4 * Module Name: aslfiles - file I/O suppoert
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2012, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include "aslcompiler.h"
48 #define _COMPONENT ACPI_COMPILER
49 ACPI_MODULE_NAME ("aslfiles")
51 /* Local prototypes */
54 FlOpenIncludeWithPrefix (
59 #ifdef ACPI_OBSOLETE_FUNCTIONS
61 FlParseInputPathname (
66 /*******************************************************************************
74 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
77 ******************************************************************************/
84 AePrintErrorLog (ASL_FILE_STDOUT);
87 /* Print error summary to the debug file */
89 AePrintErrorLog (ASL_FILE_STDERR);
96 /*******************************************************************************
98 * FUNCTION: FlFileError
100 * PARAMETERS: FileId - Index into file info array
101 * ErrorId - Index into error message array
105 * DESCRIPTION: Decode errno to an error message and add the entire error
108 ******************************************************************************/
116 sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
118 AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
122 /*******************************************************************************
124 * FUNCTION: FlOpenFile
126 * PARAMETERS: FileId - Index into file info array
127 * Filename - file pathname to open
128 * Mode - Open mode for fopen
132 * DESCRIPTION: Open a file.
133 * NOTE: Aborts compiler on any error.
135 ******************************************************************************/
146 File = fopen (Filename, Mode);
148 Gbl_Files[FileId].Filename = Filename;
149 Gbl_Files[FileId].Handle = File;
153 FlFileError (FileId, ASL_MSG_OPEN);
159 /*******************************************************************************
161 * FUNCTION: FlGetFileSize
163 * PARAMETERS: FileId - Index into file info array
167 * DESCRIPTION: Get current file size. Uses seek-to-EOF. File must be open.
169 ******************************************************************************/
180 fp = Gbl_Files[FileId].Handle;
183 fseek (fp, 0, SEEK_END);
184 FileSize = (UINT32) ftell (fp);
186 /* Restore file pointer */
188 fseek (fp, Offset, SEEK_SET);
193 /*******************************************************************************
195 * FUNCTION: FlReadFile
197 * PARAMETERS: FileId - Index into file info array
198 * Buffer - Where to place the data
199 * Length - Amount to read
201 * RETURN: Status. AE_ERROR indicates EOF.
203 * DESCRIPTION: Read data from an open file.
204 * NOTE: Aborts compiler on any error.
206 ******************************************************************************/
217 /* Read and check for error */
219 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
220 if (Actual != Length)
222 if (feof (Gbl_Files[FileId].Handle))
224 /* End-of-file, just return error */
229 FlFileError (FileId, ASL_MSG_READ);
237 /*******************************************************************************
239 * FUNCTION: FlWriteFile
241 * PARAMETERS: FileId - Index into file info array
242 * Buffer - Data to write
243 * Length - Amount of data to write
247 * DESCRIPTION: Write data to an open file.
248 * NOTE: Aborts compiler on any error.
250 ******************************************************************************/
261 /* Write and check for error */
263 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
264 if (Actual != Length)
266 FlFileError (FileId, ASL_MSG_WRITE);
272 /*******************************************************************************
274 * FUNCTION: FlPrintFile
276 * PARAMETERS: FileId - Index into file info array
277 * Format - Printf format string
278 * ... - Printf arguments
282 * DESCRIPTION: Formatted write to an open file.
283 * NOTE: Aborts compiler on any error.
285 ******************************************************************************/
297 va_start (Args, Format);
299 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
304 FlFileError (FileId, ASL_MSG_WRITE);
310 /*******************************************************************************
312 * FUNCTION: FlSeekFile
314 * PARAMETERS: FileId - Index into file info array
315 * Offset - Absolute byte offset in file
319 * DESCRIPTION: Seek to absolute offset
320 * NOTE: Aborts compiler on any error.
322 ******************************************************************************/
332 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
335 FlFileError (FileId, ASL_MSG_SEEK);
341 /*******************************************************************************
343 * FUNCTION: FlCloseFile
345 * PARAMETERS: FileId - Index into file info array
349 * DESCRIPTION: Close an open file. Aborts compiler on error
351 ******************************************************************************/
360 if (!Gbl_Files[FileId].Handle)
365 Error = fclose (Gbl_Files[FileId].Handle);
366 Gbl_Files[FileId].Handle = NULL;
370 FlFileError (FileId, ASL_MSG_CLOSE);
378 /*******************************************************************************
380 * FUNCTION: FlSetLineNumber
382 * PARAMETERS: Op - Parse node for the LINE asl statement
386 * DESCRIPTION: Set the current line number
388 ******************************************************************************/
392 ACPI_PARSE_OBJECT *Op)
395 Gbl_CurrentLineNumber = (UINT32) Op->Asl.Value.Integer;
396 Gbl_LogicalLineNumber = (UINT32) Op->Asl.Value.Integer;
400 /*******************************************************************************
402 * FUNCTION: FlAddIncludeDirectory
404 * PARAMETERS: Dir - Directory pathname string
408 * DESCRIPTION: Add a directory the list of include prefix directories.
410 ******************************************************************************/
413 FlAddIncludeDirectory (
416 ASL_INCLUDE_DIR *NewDir;
417 ASL_INCLUDE_DIR *NextDir;
418 ASL_INCLUDE_DIR *PrevDir = NULL;
419 UINT32 NeedsSeparator = 0;
423 DirLength = strlen (Dir);
429 /* Make sure that the pathname ends with a path separator */
431 if ((Dir[DirLength-1] != '/') &&
432 (Dir[DirLength-1] != '\\'))
437 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
438 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
439 strcpy (NewDir->Dir, Dir);
442 strcat (NewDir->Dir, "/");
446 * Preserve command line ordering of -I options by adding new elements
447 * at the end of the list
449 NextDir = Gbl_IncludeDirList;
453 NextDir = NextDir->Next;
458 PrevDir->Next = NewDir;
462 Gbl_IncludeDirList = NewDir;
467 /*******************************************************************************
469 * FUNCTION: FlOpenIncludeWithPrefix
471 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
473 * Filename - The include filename from the source ASL.
475 * RETURN: Valid file descriptor if successful. Null otherwise.
477 * DESCRIPTION: Open an include file and push it on the input file stack.
479 ******************************************************************************/
482 FlOpenIncludeWithPrefix (
490 /* Build the full pathname to the file */
492 Pathname = ACPI_ALLOCATE (strlen (PrefixDir) + strlen (Filename) + 1);
494 strcpy (Pathname, PrefixDir);
495 strcat (Pathname, Filename);
497 DbgPrint (ASL_PARSE_OUTPUT, "\nAttempt to open include file: path %s\n\n",
500 /* Attempt to open the file, push if successful */
502 IncludeFile = fopen (Pathname, "r");
505 /* Push the include file on the open input file stack */
507 AslPushInputFileStack (IncludeFile, Pathname);
508 return (IncludeFile);
511 ACPI_FREE (Pathname);
516 /*******************************************************************************
518 * FUNCTION: FlOpenIncludeFile
520 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
524 * DESCRIPTION: Open an include file and push it on the input file stack.
526 ******************************************************************************/
530 ACPI_PARSE_OBJECT *Op)
533 ASL_INCLUDE_DIR *NextDir;
536 /* Op must be valid */
540 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
541 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
542 Gbl_InputByteCount, Gbl_CurrentColumn,
543 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
549 * Flush out the "include ()" statement on this line, start
550 * the actual include file on the next line
552 ResetCurrentLineBuffer ();
553 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
554 Gbl_CurrentLineOffset++;
557 /* Attempt to open the include file */
559 /* If the file specifies an absolute path, just open it */
561 if ((Op->Asl.Value.String[0] == '/') ||
562 (Op->Asl.Value.String[0] == '\\') ||
563 (Op->Asl.Value.String[1] == ':'))
565 IncludeFile = FlOpenIncludeWithPrefix ("", Op->Asl.Value.String);
574 * The include filename is not an absolute path.
576 * First, search for the file within the "local" directory -- meaning
577 * the same directory that contains the source file.
579 * Construct the file pathname from the global directory name.
581 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op->Asl.Value.String);
588 * Second, search for the file within the (possibly multiple) directories
589 * specified by the -I option on the command line.
591 NextDir = Gbl_IncludeDirList;
594 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op->Asl.Value.String);
600 NextDir = NextDir->Next;
603 /* We could not open the include file after trying very hard */
606 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
607 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
611 /*******************************************************************************
613 * FUNCTION: FlOpenInputFile
615 * PARAMETERS: InputFilename - The user-specified ASL source file to be
620 * DESCRIPTION: Open the specified input file, and save the directory path to
621 * the file so that include files can be opened in
622 * the same directory.
624 ******************************************************************************/
631 /* Open the input ASL file, text mode */
633 FlOpenFile (ASL_FILE_INPUT, InputFilename, "r");
634 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
640 /*******************************************************************************
642 * FUNCTION: FlOpenAmlOutputFile
644 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
648 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
649 * is created in the same directory as the parent input file.
651 ******************************************************************************/
654 FlOpenAmlOutputFile (
655 char *FilenamePrefix)
660 /* Output filename usually comes from the ASL itself */
662 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
665 /* Create the output AML filename */
667 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
670 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
671 0, 0, 0, 0, NULL, NULL);
676 /* Open the output AML file in binary mode */
678 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
683 /*******************************************************************************
685 * FUNCTION: FlOpenMiscOutputFiles
687 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
691 * DESCRIPTION: Create and open the various output files needed, depending on
692 * the command line options
694 ******************************************************************************/
697 FlOpenMiscOutputFiles (
698 char *FilenamePrefix)
703 /* Create/Open a hex output file if asked */
705 if (Gbl_HexOutputFlag)
707 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
710 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
711 0, 0, 0, 0, NULL, NULL);
715 /* Open the hex file, text mode */
717 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+");
719 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
720 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
723 /* Create/Open a debug output file if asked */
727 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
730 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
731 0, 0, 0, 0, NULL, NULL);
735 /* Open the debug file as STDERR, text mode */
737 /* TBD: hide this behind a FlReopenFile function */
739 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
740 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
741 freopen (Filename, "w+t", stderr);
743 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
744 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
747 /* Create/Open a listing output file if asked */
751 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
754 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
755 0, 0, 0, 0, NULL, NULL);
759 /* Open the listing file, text mode */
761 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+");
763 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
764 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
767 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
772 /* Create/Open a combined source output file */
774 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
777 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
778 0, 0, 0, 0, NULL, NULL);
783 * Open the source output file, binary mode (so that LF does not get
784 * expanded to CR/LF on some systems, messing up our seek
787 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
789 /* Create/Open a assembly code source output file if asked */
791 if (Gbl_AsmOutputFlag)
793 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
796 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
797 0, 0, 0, 0, NULL, NULL);
801 /* Open the assembly code source file, text mode */
803 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+");
805 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
806 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
809 /* Create/Open a C code source output file if asked */
811 if (Gbl_C_OutputFlag)
813 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
816 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
817 0, 0, 0, 0, NULL, NULL);
821 /* Open the C code source file, text mode */
823 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+");
825 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
826 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
827 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
830 /* Create/Open a assembly include output file if asked */
832 if (Gbl_AsmIncludeOutputFlag)
834 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
837 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
838 0, 0, 0, 0, NULL, NULL);
842 /* Open the assembly include file, text mode */
844 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+");
846 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
847 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
850 /* Create/Open a C include output file if asked */
852 if (Gbl_C_IncludeOutputFlag)
854 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
857 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
858 0, 0, 0, 0, NULL, NULL);
862 /* Open the C include file, text mode */
864 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+");
866 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
867 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
868 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_OUTPUT);
871 /* Create a namespace output file if asked */
873 if (Gbl_NsOutputFlag)
875 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_NAMESPACE);
878 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
879 0, 0, 0, 0, NULL, NULL);
883 /* Open the namespace file, text mode */
885 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+");
887 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
888 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
895 #ifdef ACPI_OBSOLETE_FUNCTIONS
896 /*******************************************************************************
898 * FUNCTION: FlParseInputPathname
900 * PARAMETERS: InputFilename - The user-specified ASL source file to be
905 * DESCRIPTION: Split the input path into a directory and filename part
906 * 1) Directory part used to open include files
907 * 2) Filename part used to generate output filenames
909 ******************************************************************************/
912 FlParseInputPathname (
923 /* Get the path to the input filename's directory */
925 Gbl_DirectoryPath = strdup (InputFilename);
926 if (!Gbl_DirectoryPath)
928 return (AE_NO_MEMORY);
931 Substring = strrchr (Gbl_DirectoryPath, '\\');
934 Substring = strrchr (Gbl_DirectoryPath, '/');
937 Substring = strrchr (Gbl_DirectoryPath, ':');
943 Gbl_DirectoryPath[0] = 0;
944 if (Gbl_UseDefaultAmlFilename)
946 Gbl_OutputFilenamePrefix = strdup (InputFilename);
951 if (Gbl_UseDefaultAmlFilename)
953 Gbl_OutputFilenamePrefix = strdup (Substring + 1);