1 /******************************************************************************
3 * Module Name: aslfileio - File I/O support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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 #define _COMPONENT ACPI_COMPILER
47 ACPI_MODULE_NAME ("aslfileio")
54 /*******************************************************************************
56 * FUNCTION: FlFileError
58 * PARAMETERS: FileId - Index into file info array
59 * ErrorId - Index into error message array
63 * DESCRIPTION: Decode errno to an error message and add the entire error
66 ******************************************************************************/
74 sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
76 AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
80 /*******************************************************************************
82 * FUNCTION: FlOpenFile
84 * PARAMETERS: FileId - Index into file info array
85 * Filename - file pathname to open
86 * Mode - Open mode for fopen
90 * DESCRIPTION: Open a file.
91 * NOTE: Aborts compiler on any error.
93 ******************************************************************************/
104 File = fopen (Filename, Mode);
107 FlFileError (FileId, ASL_MSG_OPEN);
111 Gbl_Files[FileId].Filename = Filename;
112 Gbl_Files[FileId].Handle = File;
116 /*******************************************************************************
118 * FUNCTION: UtGetFileSize
120 * PARAMETERS: fp - Open file handle
122 * RETURN: File Size. -1 on error.
124 * DESCRIPTION: Get current file size. Uses seek-to-EOF. File must be open.
125 * TBD: This function should be used to replace other similar
126 * functions in ACPICA.
128 ******************************************************************************/
138 CurrentOffset = ftell (fp);
139 if (CurrentOffset < 0)
144 if (fseek (fp, 0, SEEK_END))
149 FileSize = ftell (fp);
155 /* Restore file pointer */
157 if (fseek (fp, CurrentOffset, SEEK_SET))
166 perror ("Could not get file offset");
170 perror ("Could not seek file");
175 /*******************************************************************************
177 * FUNCTION: FlGetFileSize
179 * PARAMETERS: FileId - Index into file info array
183 * DESCRIPTION: Get current file size. Uses common seek-to-EOF function.
184 * File must be open. Aborts compiler on error.
186 ******************************************************************************/
195 FileSize = UtGetFileSize (Gbl_Files[FileId].Handle);
201 return ((UINT32) FileSize);
205 /*******************************************************************************
207 * FUNCTION: FlReadFile
209 * PARAMETERS: FileId - Index into file info array
210 * Buffer - Where to place the data
211 * Length - Amount to read
213 * RETURN: Status. AE_ERROR indicates EOF.
215 * DESCRIPTION: Read data from an open file.
216 * NOTE: Aborts compiler on any error.
218 ******************************************************************************/
229 /* Read and check for error */
231 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
234 if (feof (Gbl_Files[FileId].Handle))
236 /* End-of-file, just return error */
241 FlFileError (FileId, ASL_MSG_READ);
249 /*******************************************************************************
251 * FUNCTION: FlWriteFile
253 * PARAMETERS: FileId - Index into file info array
254 * Buffer - Data to write
255 * Length - Amount of data to write
259 * DESCRIPTION: Write data to an open file.
260 * NOTE: Aborts compiler on any error.
262 ******************************************************************************/
273 /* Write and check for error */
275 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
276 if (Actual != Length)
278 FlFileError (FileId, ASL_MSG_WRITE);
284 /*******************************************************************************
286 * FUNCTION: FlPrintFile
288 * PARAMETERS: FileId - Index into file info array
289 * Format - Printf format string
290 * ... - Printf arguments
294 * DESCRIPTION: Formatted write to an open file.
295 * NOTE: Aborts compiler on any error.
297 ******************************************************************************/
309 va_start (Args, Format);
311 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
316 FlFileError (FileId, ASL_MSG_WRITE);
322 /*******************************************************************************
324 * FUNCTION: FlSeekFile
326 * PARAMETERS: FileId - Index into file info array
327 * Offset - Absolute byte offset in file
331 * DESCRIPTION: Seek to absolute offset.
332 * NOTE: Aborts compiler on any error.
334 ******************************************************************************/
344 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
347 FlFileError (FileId, ASL_MSG_SEEK);
353 /*******************************************************************************
355 * FUNCTION: FlCloseFile
357 * PARAMETERS: FileId - Index into file info array
361 * DESCRIPTION: Close an open file. Aborts compiler on error
363 ******************************************************************************/
372 if (!Gbl_Files[FileId].Handle)
377 Error = fclose (Gbl_Files[FileId].Handle);
380 FlFileError (FileId, ASL_MSG_CLOSE);
384 Gbl_Files[FileId].Handle = NULL;
389 /*******************************************************************************
391 * FUNCTION: FlDeleteFile
393 * PARAMETERS: FileId - Index into file info array
397 * DESCRIPTION: Delete a file.
399 ******************************************************************************/
405 ASL_FILE_INFO *Info = &Gbl_Files[FileId];
413 if (remove (Info->Filename))
415 printf ("%s (%s file) ",
416 Info->Filename, Info->Description);
417 perror ("Could not delete");
420 Info->Filename = NULL;