1 /******************************************************************************
3 * Module Name: aslfileio - File I/O support
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"
47 #define _COMPONENT ACPI_COMPILER
48 ACPI_MODULE_NAME ("aslfileio")
51 /*******************************************************************************
53 * FUNCTION: FlFileError
55 * PARAMETERS: FileId - Index into file info array
56 * ErrorId - Index into error message array
60 * DESCRIPTION: Decode errno to an error message and add the entire error
63 ******************************************************************************/
71 sprintf (MsgBuffer, "\"%s\" (%s) - %s", Gbl_Files[FileId].Filename,
72 Gbl_Files[FileId].Description, strerror (errno));
74 AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
78 /*******************************************************************************
80 * FUNCTION: FlOpenFile
82 * PARAMETERS: FileId - Index into file info array
83 * Filename - file pathname to open
84 * Mode - Open mode for fopen
88 * DESCRIPTION: Open a file.
89 * NOTE: Aborts compiler on any error.
91 ******************************************************************************/
102 Gbl_Files[FileId].Filename = Filename;
103 Gbl_Files[FileId].Handle = NULL;
105 File = fopen (Filename, Mode);
108 FlFileError (FileId, ASL_MSG_OPEN);
112 Gbl_Files[FileId].Handle = File;
116 /*******************************************************************************
118 * FUNCTION: FlGetFileSize
120 * PARAMETERS: FileId - Index into file info array
124 * DESCRIPTION: Get current file size. Uses common seek-to-EOF function.
125 * File must be open. Aborts compiler on error.
127 ******************************************************************************/
136 FileSize = CmGetFileSize (Gbl_Files[FileId].Handle);
137 if (FileSize == ACPI_UINT32_MAX)
146 /*******************************************************************************
148 * FUNCTION: FlReadFile
150 * PARAMETERS: FileId - Index into file info array
151 * Buffer - Where to place the data
152 * Length - Amount to read
154 * RETURN: Status. AE_ERROR indicates EOF.
156 * DESCRIPTION: Read data from an open file.
157 * NOTE: Aborts compiler on any error.
159 ******************************************************************************/
170 /* Read and check for error */
172 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
175 if (feof (Gbl_Files[FileId].Handle))
177 /* End-of-file, just return error */
182 FlFileError (FileId, ASL_MSG_READ);
190 /*******************************************************************************
192 * FUNCTION: FlWriteFile
194 * PARAMETERS: FileId - Index into file info array
195 * Buffer - Data to write
196 * Length - Amount of data to write
200 * DESCRIPTION: Write data to an open file.
201 * NOTE: Aborts compiler on any error.
203 ******************************************************************************/
214 /* Write and check for error */
216 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
217 if (Actual != Length)
219 FlFileError (FileId, ASL_MSG_WRITE);
223 if ((FileId == ASL_FILE_PREPROCESSOR) && Gbl_PreprocessorOutputFlag)
225 /* Duplicate the output to the user preprocessor (.i) file */
227 Actual = fwrite ((char *) Buffer, 1, Length,
228 Gbl_Files[ASL_FILE_PREPROCESSOR_USER].Handle);
229 if (Actual != Length)
231 FlFileError (FileId, ASL_MSG_WRITE);
238 /*******************************************************************************
240 * FUNCTION: FlPrintFile
242 * PARAMETERS: FileId - Index into file info array
243 * Format - Printf format string
244 * ... - Printf arguments
248 * DESCRIPTION: Formatted write to an open file.
249 * NOTE: Aborts compiler on any error.
251 ******************************************************************************/
263 va_start (Args, Format);
264 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
269 FlFileError (FileId, ASL_MSG_WRITE);
273 if ((FileId == ASL_FILE_PREPROCESSOR) &&
274 Gbl_PreprocessorOutputFlag)
277 * Duplicate the output to the user preprocessor (.i) file,
278 * except: no #line directives.
280 if (!strncmp (Format, "#line", 5))
285 va_start (Args, Format);
286 Actual = vfprintf (Gbl_Files[ASL_FILE_PREPROCESSOR_USER].Handle,
292 FlFileError (FileId, ASL_MSG_WRITE);
299 /*******************************************************************************
301 * FUNCTION: FlSeekFile
303 * PARAMETERS: FileId - Index into file info array
304 * Offset - Absolute byte offset in file
308 * DESCRIPTION: Seek to absolute offset.
309 * NOTE: Aborts compiler on any error.
311 ******************************************************************************/
321 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
324 FlFileError (FileId, ASL_MSG_SEEK);
330 /*******************************************************************************
332 * FUNCTION: FlCloseFile
334 * PARAMETERS: FileId - Index into file info array
338 * DESCRIPTION: Close an open file. Aborts compiler on error
340 ******************************************************************************/
349 if (!Gbl_Files[FileId].Handle)
354 Error = fclose (Gbl_Files[FileId].Handle);
357 FlFileError (FileId, ASL_MSG_CLOSE);
361 /* Do not clear/free the filename string */
363 Gbl_Files[FileId].Handle = NULL;
368 /*******************************************************************************
370 * FUNCTION: FlDeleteFile
372 * PARAMETERS: FileId - Index into file info array
376 * DESCRIPTION: Delete a file.
378 ******************************************************************************/
384 ASL_FILE_INFO *Info = &Gbl_Files[FileId];
392 if (remove (Info->Filename))
394 printf ("%s (%s file) ",
395 Info->Filename, Info->Description);
396 perror ("Could not delete");
399 Info->Filename = NULL;