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