56d006b5ad7b4e8a41252a01287dbeff57a0c2b2
[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 #include "acapps.h"
46
47 #define _COMPONENT          ACPI_COMPILER
48         ACPI_MODULE_NAME    ("aslfileio")
49
50
51 /*******************************************************************************
52  *
53  * FUNCTION:    FlFileError
54  *
55  * PARAMETERS:  FileId              - Index into file info array
56  *              ErrorId             - Index into error message array
57  *
58  * RETURN:      None
59  *
60  * DESCRIPTION: Decode errno to an error message and add the entire error
61  *              to the error log.
62  *
63  ******************************************************************************/
64
65 void
66 FlFileError (
67     UINT32                  FileId,
68     UINT8                   ErrorId)
69 {
70
71     sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
72         strerror (errno));
73     AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
74 }
75
76
77 /*******************************************************************************
78  *
79  * FUNCTION:    FlOpenFile
80  *
81  * PARAMETERS:  FileId              - Index into file info array
82  *              Filename            - file pathname to open
83  *              Mode                - Open mode for fopen
84  *
85  * RETURN:      None
86  *
87  * DESCRIPTION: Open a file.
88  *              NOTE: Aborts compiler on any error.
89  *
90  ******************************************************************************/
91
92 void
93 FlOpenFile (
94     UINT32                  FileId,
95     char                    *Filename,
96     char                    *Mode)
97 {
98     FILE                    *File;
99
100
101     File = fopen (Filename, Mode);
102     if (!File)
103     {
104         FlFileError (FileId, ASL_MSG_OPEN);
105         AslAbort ();
106     }
107
108     Gbl_Files[FileId].Filename = Filename;
109     Gbl_Files[FileId].Handle   = File;
110 }
111
112
113 /*******************************************************************************
114  *
115  * FUNCTION:    FlGetFileSize
116  *
117  * PARAMETERS:  FileId              - Index into file info array
118  *
119  * RETURN:      File Size
120  *
121  * DESCRIPTION: Get current file size. Uses common seek-to-EOF function.
122  *              File must be open. Aborts compiler on error.
123  *
124  ******************************************************************************/
125
126 UINT32
127 FlGetFileSize (
128     UINT32                  FileId)
129 {
130     UINT32                  FileSize;
131
132
133     FileSize = CmGetFileSize (Gbl_Files[FileId].Handle);
134     if (FileSize == ACPI_UINT32_MAX)
135     {
136         AslAbort();
137     }
138
139     return (FileSize);
140 }
141
142
143 /*******************************************************************************
144  *
145  * FUNCTION:    FlReadFile
146  *
147  * PARAMETERS:  FileId              - Index into file info array
148  *              Buffer              - Where to place the data
149  *              Length              - Amount to read
150  *
151  * RETURN:      Status. AE_ERROR indicates EOF.
152  *
153  * DESCRIPTION: Read data from an open file.
154  *              NOTE: Aborts compiler on any error.
155  *
156  ******************************************************************************/
157
158 ACPI_STATUS
159 FlReadFile (
160     UINT32                  FileId,
161     void                    *Buffer,
162     UINT32                  Length)
163 {
164     UINT32                  Actual;
165
166
167     /* Read and check for error */
168
169     Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
170     if (Actual < Length)
171     {
172         if (feof (Gbl_Files[FileId].Handle))
173         {
174             /* End-of-file, just return error */
175
176             return (AE_ERROR);
177         }
178
179         FlFileError (FileId, ASL_MSG_READ);
180         AslAbort ();
181     }
182
183     return (AE_OK);
184 }
185
186
187 /*******************************************************************************
188  *
189  * FUNCTION:    FlWriteFile
190  *
191  * PARAMETERS:  FileId              - Index into file info array
192  *              Buffer              - Data to write
193  *              Length              - Amount of data to write
194  *
195  * RETURN:      None
196  *
197  * DESCRIPTION: Write data to an open file.
198  *              NOTE: Aborts compiler on any error.
199  *
200  ******************************************************************************/
201
202 void
203 FlWriteFile (
204     UINT32                  FileId,
205     void                    *Buffer,
206     UINT32                  Length)
207 {
208     UINT32                  Actual;
209
210
211     /* Write and check for error */
212
213     Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
214     if (Actual != Length)
215     {
216         FlFileError (FileId, ASL_MSG_WRITE);
217         AslAbort ();
218     }
219 }
220
221
222 /*******************************************************************************
223  *
224  * FUNCTION:    FlPrintFile
225  *
226  * PARAMETERS:  FileId              - Index into file info array
227  *              Format              - Printf format string
228  *              ...                 - Printf arguments
229  *
230  * RETURN:      None
231  *
232  * DESCRIPTION: Formatted write to an open file.
233  *              NOTE: Aborts compiler on any error.
234  *
235  ******************************************************************************/
236
237 void
238 FlPrintFile (
239     UINT32                  FileId,
240     char                    *Format,
241     ...)
242 {
243     INT32                   Actual;
244     va_list                 Args;
245
246
247     va_start (Args, Format);
248
249     Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
250     va_end (Args);
251
252     if (Actual == -1)
253     {
254         FlFileError (FileId, ASL_MSG_WRITE);
255         AslAbort ();
256     }
257 }
258
259
260 /*******************************************************************************
261  *
262  * FUNCTION:    FlSeekFile
263  *
264  * PARAMETERS:  FileId              - Index into file info array
265  *              Offset              - Absolute byte offset in file
266  *
267  * RETURN:      None
268  *
269  * DESCRIPTION: Seek to absolute offset.
270  *              NOTE: Aborts compiler on any error.
271  *
272  ******************************************************************************/
273
274 void
275 FlSeekFile (
276     UINT32                  FileId,
277     long                    Offset)
278 {
279     int                     Error;
280
281
282     Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
283     if (Error)
284     {
285         FlFileError (FileId, ASL_MSG_SEEK);
286         AslAbort ();
287     }
288 }
289
290
291 /*******************************************************************************
292  *
293  * FUNCTION:    FlCloseFile
294  *
295  * PARAMETERS:  FileId              - Index into file info array
296  *
297  * RETURN:      None
298  *
299  * DESCRIPTION: Close an open file. Aborts compiler on error
300  *
301  ******************************************************************************/
302
303 void
304 FlCloseFile (
305     UINT32                  FileId)
306 {
307     int                     Error;
308
309
310     if (!Gbl_Files[FileId].Handle)
311     {
312         return;
313     }
314
315     Error = fclose (Gbl_Files[FileId].Handle);
316     if (Error)
317     {
318         FlFileError (FileId, ASL_MSG_CLOSE);
319         AslAbort ();
320     }
321
322     Gbl_Files[FileId].Handle = NULL;
323     return;
324 }
325
326
327 /*******************************************************************************
328  *
329  * FUNCTION:    FlDeleteFile
330  *
331  * PARAMETERS:  FileId              - Index into file info array
332  *
333  * RETURN:      None
334  *
335  * DESCRIPTION: Delete a file.
336  *
337  ******************************************************************************/
338
339 void
340 FlDeleteFile (
341     UINT32                  FileId)
342 {
343     ASL_FILE_INFO           *Info = &Gbl_Files[FileId];
344
345
346     if (!Info->Filename)
347     {
348         return;
349     }
350
351     if (remove (Info->Filename))
352     {
353         printf ("%s (%s file) ",
354             Info->Filename, Info->Description);
355         perror ("Could not delete");
356     }
357
358     Info->Filename = NULL;
359     return;
360 }