1 /******************************************************************************
3 * Module Name: aslfiles - File support functions
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include "aslcompiler.h"
46 #include "dtcompiler.h"
48 #define _COMPONENT ACPI_COMPILER
49 ACPI_MODULE_NAME ("aslfiles")
51 /* Local prototypes */
54 FlOpenIncludeWithPrefix (
56 ACPI_PARSE_OBJECT *Op,
60 #ifdef ACPI_OBSOLETE_FUNCTIONS
62 FlParseInputPathname (
67 /*******************************************************************************
69 * FUNCTION: FlSetLineNumber
71 * PARAMETERS: Op - Parse node for the LINE asl statement
75 * DESCRIPTION: Set the current line number
77 ******************************************************************************/
84 DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New line number %u (old %u)\n",
85 LineNumber, Gbl_LogicalLineNumber);
87 Gbl_CurrentLineNumber = LineNumber;
91 /*******************************************************************************
93 * FUNCTION: FlSetFilename
95 * PARAMETERS: Op - Parse node for the LINE asl statement
99 * DESCRIPTION: Set the current filename
101 ******************************************************************************/
108 DbgPrint (ASL_PARSE_OUTPUT, "\n#line: New filename %s (old %s)\n",
109 Filename, Gbl_Files[ASL_FILE_INPUT].Filename);
111 /* No need to free any existing filename */
113 Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
117 /*******************************************************************************
119 * FUNCTION: FlAddIncludeDirectory
121 * PARAMETERS: Dir - Directory pathname string
125 * DESCRIPTION: Add a directory the list of include prefix directories.
127 ******************************************************************************/
130 FlAddIncludeDirectory (
133 ASL_INCLUDE_DIR *NewDir;
134 ASL_INCLUDE_DIR *NextDir;
135 ASL_INCLUDE_DIR *PrevDir = NULL;
136 UINT32 NeedsSeparator = 0;
140 DirLength = strlen (Dir);
146 /* Make sure that the pathname ends with a path separator */
148 if ((Dir[DirLength-1] != '/') &&
149 (Dir[DirLength-1] != '\\'))
154 NewDir = ACPI_ALLOCATE_ZEROED (sizeof (ASL_INCLUDE_DIR));
155 NewDir->Dir = ACPI_ALLOCATE (DirLength + 1 + NeedsSeparator);
156 strcpy (NewDir->Dir, Dir);
159 strcat (NewDir->Dir, "/");
163 * Preserve command line ordering of -I options by adding new elements
164 * at the end of the list
166 NextDir = Gbl_IncludeDirList;
170 NextDir = NextDir->Next;
175 PrevDir->Next = NewDir;
179 Gbl_IncludeDirList = NewDir;
184 /*******************************************************************************
186 * FUNCTION: FlMergePathnames
188 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be NULL or
189 * a zero length string.
190 * FilePathname - The include filename from the source ASL.
192 * RETURN: Merged pathname string
194 * DESCRIPTION: Merge two pathnames that (probably) have common elements, to
195 * arrive at a minimal length string. Merge can occur if the
196 * FilePathname is relative to the PrefixDir.
198 ******************************************************************************/
210 DbgPrint (ASL_PARSE_OUTPUT, "Include: Prefix path - \"%s\"\n"
211 "Include: FilePathname - \"%s\"\n",
212 PrefixDir, FilePathname);
215 * If there is no prefix directory or if the file pathname is absolute,
216 * just return the original file pathname
218 if (!PrefixDir || (!*PrefixDir) ||
219 (*FilePathname == '/') ||
220 (FilePathname[1] == ':'))
222 Pathname = UtStringCacheCalloc (strlen (FilePathname) + 1);
223 strcpy (Pathname, FilePathname);
224 goto ConvertBackslashes;
227 /* Need a local copy of the prefix directory path */
229 CommonPath = UtStringCacheCalloc (strlen (PrefixDir) + 1);
230 strcpy (CommonPath, PrefixDir);
233 * Walk forward through the file path, and simultaneously backward
234 * through the prefix directory path until there are no more
235 * relative references at the start of the file path.
237 while (*FilePathname && (!strncmp (FilePathname, "../", 3)))
239 /* Remove last element of the prefix directory path */
241 LastElement = strrchr (CommonPath, '/');
244 goto ConcatenatePaths;
247 *LastElement = 0; /* Terminate CommonPath string */
248 FilePathname += 3; /* Point to next path element */
252 * Remove the last element of the prefix directory path (it is the same as
253 * the first element of the file pathname), and build the final merged
256 LastElement = strrchr (CommonPath, '/');
262 /* Build the final merged pathname */
265 Pathname = UtStringCacheCalloc (strlen (CommonPath) + strlen (FilePathname) + 2);
266 if (LastElement && *CommonPath)
268 strcpy (Pathname, CommonPath);
269 strcat (Pathname, "/");
271 strcat (Pathname, FilePathname);
273 /* Convert all backslashes to normal slashes */
276 UtConvertBackslashes (Pathname);
278 DbgPrint (ASL_PARSE_OUTPUT, "Include: Merged Pathname - \"%s\"\n",
284 /*******************************************************************************
286 * FUNCTION: FlOpenIncludeWithPrefix
288 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
290 * Filename - The include filename from the source ASL.
292 * RETURN: Valid file descriptor if successful. Null otherwise.
294 * DESCRIPTION: Open an include file and push it on the input file stack.
296 ******************************************************************************/
299 FlOpenIncludeWithPrefix (
301 ACPI_PARSE_OBJECT *Op,
306 UINT32 OriginalLineNumber;
309 /* Build the full pathname to the file */
311 Pathname = FlMergePathnames (PrefixDir, Filename);
313 DbgPrint (ASL_PARSE_OUTPUT, "Include: Opening file - \"%s\"\n\n",
316 /* Attempt to open the file, push if successful */
318 IncludeFile = fopen (Pathname, "r");
321 fprintf (stderr, "Could not open include file %s\n", Pathname);
322 ACPI_FREE (Pathname);
327 * Check the entire include file for any # preprocessor directives.
328 * This is because there may be some confusion between the #include
329 * preprocessor directive and the ASL Include statement. A file included
330 * by the ASL include cannot contain preprocessor directives because
331 * the preprocessor has already run by the time the ASL include is
332 * recognized (by the compiler, not the preprocessor.)
334 * Note: DtGetNextLine strips/ignores comments.
335 * Save current line number since DtGetNextLine modifies it.
337 Gbl_CurrentLineNumber--;
338 OriginalLineNumber = Gbl_CurrentLineNumber;
339 while (DtGetNextLine (IncludeFile, DT_ALLOW_MULTILINE_QUOTES) != ASL_EOF)
341 if (Gbl_CurrentLineBuffer[0] == '#')
343 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE,
344 Op, "use #include instead");
347 Gbl_CurrentLineNumber = OriginalLineNumber;
349 /* Must seek back to the start of the file */
351 fseek (IncludeFile, 0, SEEK_SET);
353 /* Push the include file on the open input file stack */
355 AslPushInputFileStack (IncludeFile, Pathname);
356 return (IncludeFile);
360 /*******************************************************************************
362 * FUNCTION: FlOpenIncludeFile
364 * PARAMETERS: Op - Parse node for the INCLUDE ASL statement
368 * DESCRIPTION: Open an include file and push it on the input file stack.
370 ******************************************************************************/
374 ACPI_PARSE_OBJECT *Op)
377 ASL_INCLUDE_DIR *NextDir;
380 /* Op must be valid */
384 AslCommonError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN,
385 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
386 Gbl_InputByteCount, Gbl_CurrentColumn,
387 Gbl_Files[ASL_FILE_INPUT].Filename, " - Null parse node");
393 * Flush out the "include ()" statement on this line, start
394 * the actual include file on the next line
396 AslResetCurrentLineBuffer ();
397 FlPrintFile (ASL_FILE_SOURCE_OUTPUT, "\n");
398 Gbl_CurrentLineOffset++;
401 /* Attempt to open the include file */
403 /* If the file specifies an absolute path, just open it */
405 if ((Op->Asl.Value.String[0] == '/') ||
406 (Op->Asl.Value.String[0] == '\\') ||
407 (Op->Asl.Value.String[1] == ':'))
409 IncludeFile = FlOpenIncludeWithPrefix ("", Op, Op->Asl.Value.String);
418 * The include filename is not an absolute path.
420 * First, search for the file within the "local" directory -- meaning
421 * the same directory that contains the source file.
423 * Construct the file pathname from the global directory name.
425 IncludeFile = FlOpenIncludeWithPrefix (Gbl_DirectoryPath, Op, Op->Asl.Value.String);
432 * Second, search for the file within the (possibly multiple) directories
433 * specified by the -I option on the command line.
435 NextDir = Gbl_IncludeDirList;
438 IncludeFile = FlOpenIncludeWithPrefix (NextDir->Dir, Op, Op->Asl.Value.String);
444 NextDir = NextDir->Next;
447 /* We could not open the include file after trying very hard */
450 sprintf (MsgBuffer, "%s, %s", Op->Asl.Value.String, strerror (errno));
451 AslError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, Op, MsgBuffer);
455 /*******************************************************************************
457 * FUNCTION: FlOpenInputFile
459 * PARAMETERS: InputFilename - The user-specified ASL source file to be
464 * DESCRIPTION: Open the specified input file, and save the directory path to
465 * the file so that include files can be opened in
466 * the same directory.
468 ******************************************************************************/
475 /* Open the input ASL file, text mode */
477 FlOpenFile (ASL_FILE_INPUT, InputFilename, "rt");
478 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
484 /*******************************************************************************
486 * FUNCTION: FlOpenAmlOutputFile
488 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
492 * DESCRIPTION: Create the output filename (*.AML) and open the file. The file
493 * is created in the same directory as the parent input file.
495 ******************************************************************************/
498 FlOpenAmlOutputFile (
499 char *FilenamePrefix)
504 /* Output filename usually comes from the ASL itself */
506 Filename = Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
509 /* Create the output AML filename */
511 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_AML_CODE);
514 AslCommonError (ASL_ERROR, ASL_MSG_OUTPUT_FILENAME,
515 0, 0, 0, 0, NULL, NULL);
519 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = Filename;
522 /* Open the output AML file in binary mode */
524 FlOpenFile (ASL_FILE_AML_OUTPUT, Filename, "w+b");
529 /*******************************************************************************
531 * FUNCTION: FlOpenMiscOutputFiles
533 * PARAMETERS: FilenamePrefix - The user-specified ASL source file
537 * DESCRIPTION: Create and open the various output files needed, depending on
538 * the command line options
540 ******************************************************************************/
543 FlOpenMiscOutputFiles (
544 char *FilenamePrefix)
549 /* All done for disassembler */
551 if (Gbl_FileType == ASL_INPUT_TYPE_ACPI_TABLE)
556 /* Create/Open a hex output file if asked */
558 if (Gbl_HexOutputFlag)
560 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_HEX_DUMP);
563 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
564 0, 0, 0, 0, NULL, NULL);
568 /* Open the hex file, text mode */
570 FlOpenFile (ASL_FILE_HEX_OUTPUT, Filename, "w+t");
572 AslCompilerSignon (ASL_FILE_HEX_OUTPUT);
573 AslCompilerFileHeader (ASL_FILE_HEX_OUTPUT);
576 /* Create/Open a debug output file if asked */
580 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_DEBUG);
583 AslCommonError (ASL_ERROR, ASL_MSG_DEBUG_FILENAME,
584 0, 0, 0, 0, NULL, NULL);
588 /* Open the debug file as STDERR, text mode */
590 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Filename = Filename;
591 Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle =
592 freopen (Filename, "w+t", stderr);
594 if (!Gbl_Files[ASL_FILE_DEBUG_OUTPUT].Handle)
597 * A problem with freopen is that on error, we no longer
598 * have stderr and cannot emit normal error messages.
599 * Emit error to stdout, close files, and exit.
602 "\nCould not open debug output file: %s\n\n", Filename);
608 AslCompilerSignon (ASL_FILE_DEBUG_OUTPUT);
609 AslCompilerFileHeader (ASL_FILE_DEBUG_OUTPUT);
612 /* Create/Open a listing output file if asked */
616 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_LISTING);
619 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
620 0, 0, 0, 0, NULL, NULL);
624 /* Open the listing file, text mode */
626 FlOpenFile (ASL_FILE_LISTING_OUTPUT, Filename, "w+t");
628 AslCompilerSignon (ASL_FILE_LISTING_OUTPUT);
629 AslCompilerFileHeader (ASL_FILE_LISTING_OUTPUT);
632 /* Create the preprocessor output temp file if preprocessor enabled */
634 if (Gbl_PreprocessFlag)
636 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROCESSOR);
639 AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
640 0, 0, 0, 0, NULL, NULL);
644 FlOpenFile (ASL_FILE_PREPROCESSOR, Filename, "w+t");
648 * Create the "user" preprocessor output file if -li flag set.
649 * Note, this file contains no embedded #line directives.
651 if (Gbl_PreprocessorOutputFlag)
653 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_PREPROC_USER);
656 AslCommonError (ASL_ERROR, ASL_MSG_PREPROCESSOR_FILENAME,
657 0, 0, 0, 0, NULL, NULL);
661 FlOpenFile (ASL_FILE_PREPROCESSOR_USER, Filename, "w+t");
664 /* All done for data table compiler */
666 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
671 /* Create/Open a combined source output file */
673 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_SOURCE);
676 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
677 0, 0, 0, 0, NULL, NULL);
682 * Open the source output file, binary mode (so that LF does not get
683 * expanded to CR/LF on some systems, messing up our seek
686 FlOpenFile (ASL_FILE_SOURCE_OUTPUT, Filename, "w+b");
690 // AslCompilerin = Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle;
692 /* Create/Open a assembly code source output file if asked */
694 if (Gbl_AsmOutputFlag)
696 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_SOURCE);
699 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
700 0, 0, 0, 0, NULL, NULL);
704 /* Open the assembly code source file, text mode */
706 FlOpenFile (ASL_FILE_ASM_SOURCE_OUTPUT, Filename, "w+t");
708 AslCompilerSignon (ASL_FILE_ASM_SOURCE_OUTPUT);
709 AslCompilerFileHeader (ASL_FILE_ASM_SOURCE_OUTPUT);
712 /* Create/Open a C code source output file if asked */
714 if (Gbl_C_OutputFlag)
716 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_SOURCE);
719 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
720 0, 0, 0, 0, NULL, NULL);
724 /* Open the C code source file, text mode */
726 FlOpenFile (ASL_FILE_C_SOURCE_OUTPUT, Filename, "w+t");
728 FlPrintFile (ASL_FILE_C_SOURCE_OUTPUT, "/*\n");
729 AslCompilerSignon (ASL_FILE_C_SOURCE_OUTPUT);
730 AslCompilerFileHeader (ASL_FILE_C_SOURCE_OUTPUT);
733 /* Create/Open a C code source output file for the offset table if asked */
735 if (Gbl_C_OffsetTableFlag)
737 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_OFFSET);
740 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
741 0, 0, 0, 0, NULL, NULL);
745 /* Open the C code source file, text mode */
747 FlOpenFile (ASL_FILE_C_OFFSET_OUTPUT, Filename, "w+t");
749 FlPrintFile (ASL_FILE_C_OFFSET_OUTPUT, "/*\n");
750 AslCompilerSignon (ASL_FILE_C_OFFSET_OUTPUT);
751 AslCompilerFileHeader (ASL_FILE_C_OFFSET_OUTPUT);
754 /* Create/Open a assembly include output file if asked */
756 if (Gbl_AsmIncludeOutputFlag)
758 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_ASM_INCLUDE);
761 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
762 0, 0, 0, 0, NULL, NULL);
766 /* Open the assembly include file, text mode */
768 FlOpenFile (ASL_FILE_ASM_INCLUDE_OUTPUT, Filename, "w+t");
770 AslCompilerSignon (ASL_FILE_ASM_INCLUDE_OUTPUT);
771 AslCompilerFileHeader (ASL_FILE_ASM_INCLUDE_OUTPUT);
774 /* Create/Open a C include output file if asked */
776 if (Gbl_C_IncludeOutputFlag)
778 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_C_INCLUDE);
781 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
782 0, 0, 0, 0, NULL, NULL);
786 /* Open the C include file, text mode */
788 FlOpenFile (ASL_FILE_C_INCLUDE_OUTPUT, Filename, "w+t");
790 FlPrintFile (ASL_FILE_C_INCLUDE_OUTPUT, "/*\n");
791 AslCompilerSignon (ASL_FILE_C_INCLUDE_OUTPUT);
792 AslCompilerFileHeader (ASL_FILE_C_INCLUDE_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,
803 0, 0, 0, 0, NULL, NULL);
807 /* Open the namespace file, text mode */
809 FlOpenFile (ASL_FILE_NAMESPACE_OUTPUT, Filename, "w+t");
811 AslCompilerSignon (ASL_FILE_NAMESPACE_OUTPUT);
812 AslCompilerFileHeader (ASL_FILE_NAMESPACE_OUTPUT);
815 /* Create/Open a map file if requested */
819 Filename = FlGenerateFilename (FilenamePrefix, FILE_SUFFIX_MAP);
822 AslCommonError (ASL_ERROR, ASL_MSG_LISTING_FILENAME,
823 0, 0, 0, 0, NULL, NULL);
827 /* Open the hex file, text mode (closed at compiler exit) */
829 FlOpenFile (ASL_FILE_MAP_OUTPUT, Filename, "w+t");
831 AslCompilerSignon (ASL_FILE_MAP_OUTPUT);
832 AslCompilerFileHeader (ASL_FILE_MAP_OUTPUT);
839 #ifdef ACPI_OBSOLETE_FUNCTIONS
840 /*******************************************************************************
842 * FUNCTION: FlParseInputPathname
844 * PARAMETERS: InputFilename - The user-specified ASL source file to be
849 * DESCRIPTION: Split the input path into a directory and filename part
850 * 1) Directory part used to open include files
851 * 2) Filename part used to generate output filenames
853 ******************************************************************************/
856 FlParseInputPathname (
867 /* Get the path to the input filename's directory */
869 Gbl_DirectoryPath = strdup (InputFilename);
870 if (!Gbl_DirectoryPath)
872 return (AE_NO_MEMORY);
875 Substring = strrchr (Gbl_DirectoryPath, '\\');
878 Substring = strrchr (Gbl_DirectoryPath, '/');
881 Substring = strrchr (Gbl_DirectoryPath, ':');
887 Gbl_DirectoryPath[0] = 0;
888 if (Gbl_UseDefaultAmlFilename)
890 Gbl_OutputFilenamePrefix = strdup (InputFilename);
895 if (Gbl_UseDefaultAmlFilename)
897 Gbl_OutputFilenamePrefix = strdup (Substring + 1);
902 UtConvertBackslashes (Gbl_OutputFilenamePrefix);