kernel: Sync ACPICA with Intel's version 20140214.
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / aslfileio.c
1 /******************************************************************************
2  *
3  * Module Name: aslfileio - File I/O support
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
25  *
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.
29  *
30  * NO WARRANTY
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.
42  */
43
44 #include "aslcompiler.h"
45
46 #define _COMPONENT          ACPI_COMPILER
47         ACPI_MODULE_NAME    ("aslfileio")
48
49 long
50 UtGetFileSize (
51     FILE                    *fp);
52
53
54 /*******************************************************************************
55  *
56  * FUNCTION:    FlFileError
57  *
58  * PARAMETERS:  FileId              - Index into file info array
59  *              ErrorId             - Index into error message array
60  *
61  * RETURN:      None
62  *
63  * DESCRIPTION: Decode errno to an error message and add the entire error
64  *              to the error log.
65  *
66  ******************************************************************************/
67
68 void
69 FlFileError (
70     UINT32                  FileId,
71     UINT8                   ErrorId)
72 {
73
74     sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
75         strerror (errno));
76     AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
77 }
78
79
80 /*******************************************************************************
81  *
82  * FUNCTION:    FlOpenFile
83  *
84  * PARAMETERS:  FileId              - Index into file info array
85  *              Filename            - file pathname to open
86  *              Mode                - Open mode for fopen
87  *
88  * RETURN:      None
89  *
90  * DESCRIPTION: Open a file.
91  *              NOTE: Aborts compiler on any error.
92  *
93  ******************************************************************************/
94
95 void
96 FlOpenFile (
97     UINT32                  FileId,
98     char                    *Filename,
99     char                    *Mode)
100 {
101     FILE                    *File;
102
103
104     File = fopen (Filename, Mode);
105     if (!File)
106     {
107         FlFileError (FileId, ASL_MSG_OPEN);
108         AslAbort ();
109     }
110
111     Gbl_Files[FileId].Filename = Filename;
112     Gbl_Files[FileId].Handle   = File;
113 }
114
115
116 /*******************************************************************************
117  *
118  * FUNCTION:    UtGetFileSize
119  *
120  * PARAMETERS:  fp              - Open file handle
121  *
122  * RETURN:      File Size. -1 on error.
123  *
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.
127  *
128  ******************************************************************************/
129
130 long
131 UtGetFileSize (
132     FILE                    *fp)
133 {
134     long                    FileSize;
135     long                    CurrentOffset;
136
137
138     CurrentOffset = ftell (fp);
139     if (CurrentOffset < 0)
140     {
141         goto OffsetError;
142     }
143
144     if (fseek (fp, 0, SEEK_END))
145     {
146         goto SeekError;
147     }
148
149     FileSize = ftell (fp);
150     if (FileSize < 0)
151     {
152         goto OffsetError;
153     }
154
155     /* Restore file pointer */
156
157     if (fseek (fp, CurrentOffset, SEEK_SET))
158     {
159         goto SeekError;
160     }
161
162     return (FileSize);
163
164
165 OffsetError:
166     perror ("Could not get file offset");
167     return (-1);
168
169 SeekError:
170     perror ("Could not seek file");
171     return (-1);
172 }
173
174
175 /*******************************************************************************
176  *
177  * FUNCTION:    FlGetFileSize
178  *
179  * PARAMETERS:  FileId              - Index into file info array
180  *
181  * RETURN:      File Size
182  *
183  * DESCRIPTION: Get current file size. Uses common seek-to-EOF function.
184  *              File must be open. Aborts compiler on error.
185  *
186  ******************************************************************************/
187
188 UINT32
189 FlGetFileSize (
190     UINT32                  FileId)
191 {
192     long                    FileSize;
193
194
195     FileSize = UtGetFileSize (Gbl_Files[FileId].Handle);
196     if (FileSize == -1)
197     {
198         AslAbort();
199     }
200
201     return ((UINT32) FileSize);
202 }
203
204
205 /*******************************************************************************
206  *
207  * FUNCTION:    FlReadFile
208  *
209  * PARAMETERS:  FileId              - Index into file info array
210  *              Buffer              - Where to place the data
211  *              Length              - Amount to read
212  *
213  * RETURN:      Status. AE_ERROR indicates EOF.
214  *
215  * DESCRIPTION: Read data from an open file.
216  *              NOTE: Aborts compiler on any error.
217  *
218  ******************************************************************************/
219
220 ACPI_STATUS
221 FlReadFile (
222     UINT32                  FileId,
223     void                    *Buffer,
224     UINT32                  Length)
225 {
226     UINT32                  Actual;
227
228
229     /* Read and check for error */
230
231     Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
232     if (Actual < Length)
233     {
234         if (feof (Gbl_Files[FileId].Handle))
235         {
236             /* End-of-file, just return error */
237
238             return (AE_ERROR);
239         }
240
241         FlFileError (FileId, ASL_MSG_READ);
242         AslAbort ();
243     }
244
245     return (AE_OK);
246 }
247
248
249 /*******************************************************************************
250  *
251  * FUNCTION:    FlWriteFile
252  *
253  * PARAMETERS:  FileId              - Index into file info array
254  *              Buffer              - Data to write
255  *              Length              - Amount of data to write
256  *
257  * RETURN:      None
258  *
259  * DESCRIPTION: Write data to an open file.
260  *              NOTE: Aborts compiler on any error.
261  *
262  ******************************************************************************/
263
264 void
265 FlWriteFile (
266     UINT32                  FileId,
267     void                    *Buffer,
268     UINT32                  Length)
269 {
270     UINT32                  Actual;
271
272
273     /* Write and check for error */
274
275     Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
276     if (Actual != Length)
277     {
278         FlFileError (FileId, ASL_MSG_WRITE);
279         AslAbort ();
280     }
281 }
282
283
284 /*******************************************************************************
285  *
286  * FUNCTION:    FlPrintFile
287  *
288  * PARAMETERS:  FileId              - Index into file info array
289  *              Format              - Printf format string
290  *              ...                 - Printf arguments
291  *
292  * RETURN:      None
293  *
294  * DESCRIPTION: Formatted write to an open file.
295  *              NOTE: Aborts compiler on any error.
296  *
297  ******************************************************************************/
298
299 void
300 FlPrintFile (
301     UINT32                  FileId,
302     char                    *Format,
303     ...)
304 {
305     INT32                   Actual;
306     va_list                 Args;
307
308
309     va_start (Args, Format);
310
311     Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
312     va_end (Args);
313
314     if (Actual == -1)
315     {
316         FlFileError (FileId, ASL_MSG_WRITE);
317         AslAbort ();
318     }
319 }
320
321
322 /*******************************************************************************
323  *
324  * FUNCTION:    FlSeekFile
325  *
326  * PARAMETERS:  FileId              - Index into file info array
327  *              Offset              - Absolute byte offset in file
328  *
329  * RETURN:      None
330  *
331  * DESCRIPTION: Seek to absolute offset.
332  *              NOTE: Aborts compiler on any error.
333  *
334  ******************************************************************************/
335
336 void
337 FlSeekFile (
338     UINT32                  FileId,
339     long                    Offset)
340 {
341     int                     Error;
342
343
344     Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
345     if (Error)
346     {
347         FlFileError (FileId, ASL_MSG_SEEK);
348         AslAbort ();
349     }
350 }
351
352
353 /*******************************************************************************
354  *
355  * FUNCTION:    FlCloseFile
356  *
357  * PARAMETERS:  FileId              - Index into file info array
358  *
359  * RETURN:      None
360  *
361  * DESCRIPTION: Close an open file. Aborts compiler on error
362  *
363  ******************************************************************************/
364
365 void
366 FlCloseFile (
367     UINT32                  FileId)
368 {
369     int                     Error;
370
371
372     if (!Gbl_Files[FileId].Handle)
373     {
374         return;
375     }
376
377     Error = fclose (Gbl_Files[FileId].Handle);
378     if (Error)
379     {
380         FlFileError (FileId, ASL_MSG_CLOSE);
381         AslAbort ();
382     }
383
384     Gbl_Files[FileId].Handle = NULL;
385     return;
386 }
387
388
389 /*******************************************************************************
390  *
391  * FUNCTION:    FlDeleteFile
392  *
393  * PARAMETERS:  FileId              - Index into file info array
394  *
395  * RETURN:      None
396  *
397  * DESCRIPTION: Delete a file.
398  *
399  ******************************************************************************/
400
401 void
402 FlDeleteFile (
403     UINT32                  FileId)
404 {
405     ASL_FILE_INFO           *Info = &Gbl_Files[FileId];
406
407
408     if (!Info->Filename)
409     {
410         return;
411     }
412
413     if (remove (Info->Filename))
414     {
415         printf ("%s (%s file) ",
416             Info->Filename, Info->Description);
417         perror ("Could not delete");
418     }
419
420     Info->Filename = NULL;
421     return;
422 }