Home | History | Annotate | Line # | Download | only in compiler
aslfileio.c revision 1.2.10.3
      1  1.2.10.2       tls /******************************************************************************
      2  1.2.10.2       tls  *
      3  1.2.10.2       tls  * Module Name: aslfileio - File I/O support
      4  1.2.10.2       tls  *
      5  1.2.10.2       tls  *****************************************************************************/
      6  1.2.10.2       tls 
      7  1.2.10.2       tls /*
      8  1.2.10.3  jdolecek  * Copyright (C) 2000 - 2017, Intel Corp.
      9  1.2.10.2       tls  * All rights reserved.
     10  1.2.10.2       tls  *
     11  1.2.10.2       tls  * Redistribution and use in source and binary forms, with or without
     12  1.2.10.2       tls  * modification, are permitted provided that the following conditions
     13  1.2.10.2       tls  * are met:
     14  1.2.10.2       tls  * 1. Redistributions of source code must retain the above copyright
     15  1.2.10.2       tls  *    notice, this list of conditions, and the following disclaimer,
     16  1.2.10.2       tls  *    without modification.
     17  1.2.10.2       tls  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  1.2.10.2       tls  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  1.2.10.2       tls  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  1.2.10.2       tls  *    including a substantially similar Disclaimer requirement for further
     21  1.2.10.2       tls  *    binary redistribution.
     22  1.2.10.2       tls  * 3. Neither the names of the above-listed copyright holders nor the names
     23  1.2.10.2       tls  *    of any contributors may be used to endorse or promote products derived
     24  1.2.10.2       tls  *    from this software without specific prior written permission.
     25  1.2.10.2       tls  *
     26  1.2.10.2       tls  * Alternatively, this software may be distributed under the terms of the
     27  1.2.10.2       tls  * GNU General Public License ("GPL") version 2 as published by the Free
     28  1.2.10.2       tls  * Software Foundation.
     29  1.2.10.2       tls  *
     30  1.2.10.2       tls  * NO WARRANTY
     31  1.2.10.2       tls  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  1.2.10.2       tls  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.2.10.2       tls  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  1.2.10.2       tls  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  1.2.10.2       tls  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  1.2.10.2       tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  1.2.10.2       tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  1.2.10.2       tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  1.2.10.2       tls  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  1.2.10.2       tls  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  1.2.10.2       tls  * POSSIBILITY OF SUCH DAMAGES.
     42  1.2.10.2       tls  */
     43  1.2.10.2       tls 
     44  1.2.10.2       tls #include "aslcompiler.h"
     45  1.2.10.3  jdolecek #include "acapps.h"
     46  1.2.10.2       tls 
     47  1.2.10.2       tls #define _COMPONENT          ACPI_COMPILER
     48  1.2.10.2       tls         ACPI_MODULE_NAME    ("aslfileio")
     49  1.2.10.2       tls 
     50  1.2.10.2       tls 
     51  1.2.10.2       tls /*******************************************************************************
     52  1.2.10.2       tls  *
     53  1.2.10.2       tls  * FUNCTION:    FlFileError
     54  1.2.10.2       tls  *
     55  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
     56  1.2.10.2       tls  *              ErrorId             - Index into error message array
     57  1.2.10.2       tls  *
     58  1.2.10.2       tls  * RETURN:      None
     59  1.2.10.2       tls  *
     60  1.2.10.2       tls  * DESCRIPTION: Decode errno to an error message and add the entire error
     61  1.2.10.2       tls  *              to the error log.
     62  1.2.10.2       tls  *
     63  1.2.10.2       tls  ******************************************************************************/
     64  1.2.10.2       tls 
     65  1.2.10.2       tls void
     66  1.2.10.2       tls FlFileError (
     67  1.2.10.2       tls     UINT32                  FileId,
     68  1.2.10.2       tls     UINT8                   ErrorId)
     69  1.2.10.2       tls {
     70  1.2.10.2       tls 
     71  1.2.10.3  jdolecek     snprintf (MsgBuffer, sizeof(MsgBuffer), "\"%s\" (%s) - %s", Gbl_Files[FileId].Filename,
     72  1.2.10.3  jdolecek         Gbl_Files[FileId].Description, strerror (errno));
     73  1.2.10.3  jdolecek 
     74  1.2.10.2       tls     AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
     75  1.2.10.2       tls }
     76  1.2.10.2       tls 
     77  1.2.10.2       tls 
     78  1.2.10.2       tls /*******************************************************************************
     79  1.2.10.2       tls  *
     80  1.2.10.2       tls  * FUNCTION:    FlOpenFile
     81  1.2.10.2       tls  *
     82  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
     83  1.2.10.2       tls  *              Filename            - file pathname to open
     84  1.2.10.2       tls  *              Mode                - Open mode for fopen
     85  1.2.10.2       tls  *
     86  1.2.10.2       tls  * RETURN:      None
     87  1.2.10.2       tls  *
     88  1.2.10.2       tls  * DESCRIPTION: Open a file.
     89  1.2.10.2       tls  *              NOTE: Aborts compiler on any error.
     90  1.2.10.2       tls  *
     91  1.2.10.2       tls  ******************************************************************************/
     92  1.2.10.2       tls 
     93  1.2.10.2       tls void
     94  1.2.10.2       tls FlOpenFile (
     95  1.2.10.2       tls     UINT32                  FileId,
     96  1.2.10.2       tls     char                    *Filename,
     97  1.2.10.2       tls     char                    *Mode)
     98  1.2.10.2       tls {
     99  1.2.10.2       tls     FILE                    *File;
    100  1.2.10.2       tls 
    101  1.2.10.2       tls 
    102  1.2.10.3  jdolecek     Gbl_Files[FileId].Filename = Filename;
    103  1.2.10.3  jdolecek     Gbl_Files[FileId].Handle = NULL;
    104  1.2.10.3  jdolecek 
    105  1.2.10.2       tls     File = fopen (Filename, Mode);
    106  1.2.10.2       tls     if (!File)
    107  1.2.10.2       tls     {
    108  1.2.10.2       tls         FlFileError (FileId, ASL_MSG_OPEN);
    109  1.2.10.2       tls         AslAbort ();
    110  1.2.10.2       tls     }
    111  1.2.10.2       tls 
    112  1.2.10.3  jdolecek     Gbl_Files[FileId].Handle = File;
    113  1.2.10.2       tls }
    114  1.2.10.2       tls 
    115  1.2.10.2       tls 
    116  1.2.10.2       tls /*******************************************************************************
    117  1.2.10.2       tls  *
    118  1.2.10.2       tls  * FUNCTION:    FlGetFileSize
    119  1.2.10.2       tls  *
    120  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
    121  1.2.10.2       tls  *
    122  1.2.10.2       tls  * RETURN:      File Size
    123  1.2.10.2       tls  *
    124  1.2.10.3  jdolecek  * DESCRIPTION: Get current file size. Uses common seek-to-EOF function.
    125  1.2.10.3  jdolecek  *              File must be open. Aborts compiler on error.
    126  1.2.10.2       tls  *
    127  1.2.10.2       tls  ******************************************************************************/
    128  1.2.10.2       tls 
    129  1.2.10.2       tls UINT32
    130  1.2.10.2       tls FlGetFileSize (
    131  1.2.10.2       tls     UINT32                  FileId)
    132  1.2.10.2       tls {
    133  1.2.10.2       tls     UINT32                  FileSize;
    134  1.2.10.2       tls 
    135  1.2.10.2       tls 
    136  1.2.10.3  jdolecek     FileSize = CmGetFileSize (Gbl_Files[FileId].Handle);
    137  1.2.10.3  jdolecek     if (FileSize == ACPI_UINT32_MAX)
    138  1.2.10.3  jdolecek     {
    139  1.2.10.3  jdolecek         AslAbort();
    140  1.2.10.3  jdolecek     }
    141  1.2.10.2       tls 
    142  1.2.10.2       tls     return (FileSize);
    143  1.2.10.2       tls }
    144  1.2.10.2       tls 
    145  1.2.10.2       tls 
    146  1.2.10.2       tls /*******************************************************************************
    147  1.2.10.2       tls  *
    148  1.2.10.2       tls  * FUNCTION:    FlReadFile
    149  1.2.10.2       tls  *
    150  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
    151  1.2.10.2       tls  *              Buffer              - Where to place the data
    152  1.2.10.2       tls  *              Length              - Amount to read
    153  1.2.10.2       tls  *
    154  1.2.10.2       tls  * RETURN:      Status. AE_ERROR indicates EOF.
    155  1.2.10.2       tls  *
    156  1.2.10.2       tls  * DESCRIPTION: Read data from an open file.
    157  1.2.10.2       tls  *              NOTE: Aborts compiler on any error.
    158  1.2.10.2       tls  *
    159  1.2.10.2       tls  ******************************************************************************/
    160  1.2.10.2       tls 
    161  1.2.10.2       tls ACPI_STATUS
    162  1.2.10.2       tls FlReadFile (
    163  1.2.10.2       tls     UINT32                  FileId,
    164  1.2.10.2       tls     void                    *Buffer,
    165  1.2.10.2       tls     UINT32                  Length)
    166  1.2.10.2       tls {
    167  1.2.10.2       tls     UINT32                  Actual;
    168  1.2.10.2       tls 
    169  1.2.10.2       tls 
    170  1.2.10.2       tls     /* Read and check for error */
    171  1.2.10.2       tls 
    172  1.2.10.2       tls     Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
    173  1.2.10.2       tls     if (Actual < Length)
    174  1.2.10.2       tls     {
    175  1.2.10.2       tls         if (feof (Gbl_Files[FileId].Handle))
    176  1.2.10.2       tls         {
    177  1.2.10.2       tls             /* End-of-file, just return error */
    178  1.2.10.2       tls 
    179  1.2.10.2       tls             return (AE_ERROR);
    180  1.2.10.2       tls         }
    181  1.2.10.2       tls 
    182  1.2.10.2       tls         FlFileError (FileId, ASL_MSG_READ);
    183  1.2.10.2       tls         AslAbort ();
    184  1.2.10.2       tls     }
    185  1.2.10.2       tls 
    186  1.2.10.2       tls     return (AE_OK);
    187  1.2.10.2       tls }
    188  1.2.10.2       tls 
    189  1.2.10.2       tls 
    190  1.2.10.2       tls /*******************************************************************************
    191  1.2.10.2       tls  *
    192  1.2.10.2       tls  * FUNCTION:    FlWriteFile
    193  1.2.10.2       tls  *
    194  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
    195  1.2.10.2       tls  *              Buffer              - Data to write
    196  1.2.10.2       tls  *              Length              - Amount of data to write
    197  1.2.10.2       tls  *
    198  1.2.10.2       tls  * RETURN:      None
    199  1.2.10.2       tls  *
    200  1.2.10.2       tls  * DESCRIPTION: Write data to an open file.
    201  1.2.10.2       tls  *              NOTE: Aborts compiler on any error.
    202  1.2.10.2       tls  *
    203  1.2.10.2       tls  ******************************************************************************/
    204  1.2.10.2       tls 
    205  1.2.10.2       tls void
    206  1.2.10.2       tls FlWriteFile (
    207  1.2.10.2       tls     UINT32                  FileId,
    208  1.2.10.2       tls     void                    *Buffer,
    209  1.2.10.2       tls     UINT32                  Length)
    210  1.2.10.2       tls {
    211  1.2.10.2       tls     UINT32                  Actual;
    212  1.2.10.2       tls 
    213  1.2.10.2       tls 
    214  1.2.10.2       tls     /* Write and check for error */
    215  1.2.10.2       tls 
    216  1.2.10.2       tls     Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
    217  1.2.10.2       tls     if (Actual != Length)
    218  1.2.10.2       tls     {
    219  1.2.10.2       tls         FlFileError (FileId, ASL_MSG_WRITE);
    220  1.2.10.2       tls         AslAbort ();
    221  1.2.10.2       tls     }
    222  1.2.10.3  jdolecek 
    223  1.2.10.3  jdolecek     if ((FileId == ASL_FILE_PREPROCESSOR) && Gbl_PreprocessorOutputFlag)
    224  1.2.10.3  jdolecek     {
    225  1.2.10.3  jdolecek         /* Duplicate the output to the user preprocessor (.i) file */
    226  1.2.10.3  jdolecek 
    227  1.2.10.3  jdolecek         Actual = fwrite ((char *) Buffer, 1, Length,
    228  1.2.10.3  jdolecek             Gbl_Files[ASL_FILE_PREPROCESSOR_USER].Handle);
    229  1.2.10.3  jdolecek         if (Actual != Length)
    230  1.2.10.3  jdolecek         {
    231  1.2.10.3  jdolecek             FlFileError (FileId, ASL_MSG_WRITE);
    232  1.2.10.3  jdolecek             AslAbort ();
    233  1.2.10.3  jdolecek         }
    234  1.2.10.3  jdolecek     }
    235  1.2.10.2       tls }
    236  1.2.10.2       tls 
    237  1.2.10.2       tls 
    238  1.2.10.2       tls /*******************************************************************************
    239  1.2.10.2       tls  *
    240  1.2.10.2       tls  * FUNCTION:    FlPrintFile
    241  1.2.10.2       tls  *
    242  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
    243  1.2.10.2       tls  *              Format              - Printf format string
    244  1.2.10.2       tls  *              ...                 - Printf arguments
    245  1.2.10.2       tls  *
    246  1.2.10.2       tls  * RETURN:      None
    247  1.2.10.2       tls  *
    248  1.2.10.2       tls  * DESCRIPTION: Formatted write to an open file.
    249  1.2.10.2       tls  *              NOTE: Aborts compiler on any error.
    250  1.2.10.2       tls  *
    251  1.2.10.2       tls  ******************************************************************************/
    252  1.2.10.2       tls 
    253  1.2.10.2       tls void
    254  1.2.10.2       tls FlPrintFile (
    255  1.2.10.2       tls     UINT32                  FileId,
    256  1.2.10.2       tls     char                    *Format,
    257  1.2.10.2       tls     ...)
    258  1.2.10.2       tls {
    259  1.2.10.2       tls     INT32                   Actual;
    260  1.2.10.2       tls     va_list                 Args;
    261  1.2.10.2       tls 
    262  1.2.10.2       tls 
    263  1.2.10.2       tls     va_start (Args, Format);
    264  1.2.10.2       tls     Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
    265  1.2.10.2       tls     va_end (Args);
    266  1.2.10.2       tls 
    267  1.2.10.2       tls     if (Actual == -1)
    268  1.2.10.2       tls     {
    269  1.2.10.2       tls         FlFileError (FileId, ASL_MSG_WRITE);
    270  1.2.10.2       tls         AslAbort ();
    271  1.2.10.2       tls     }
    272  1.2.10.3  jdolecek 
    273  1.2.10.3  jdolecek     if ((FileId == ASL_FILE_PREPROCESSOR) &&
    274  1.2.10.3  jdolecek         Gbl_PreprocessorOutputFlag)
    275  1.2.10.3  jdolecek     {
    276  1.2.10.3  jdolecek         /*
    277  1.2.10.3  jdolecek          * Duplicate the output to the user preprocessor (.i) file,
    278  1.2.10.3  jdolecek          * except: no #line directives.
    279  1.2.10.3  jdolecek          */
    280  1.2.10.3  jdolecek         if (!strncmp (Format, "#line", 5))
    281  1.2.10.3  jdolecek         {
    282  1.2.10.3  jdolecek             return;
    283  1.2.10.3  jdolecek         }
    284  1.2.10.3  jdolecek 
    285  1.2.10.3  jdolecek         va_start (Args, Format);
    286  1.2.10.3  jdolecek         Actual = vfprintf (Gbl_Files[ASL_FILE_PREPROCESSOR_USER].Handle,
    287  1.2.10.3  jdolecek             Format, Args);
    288  1.2.10.3  jdolecek         va_end (Args);
    289  1.2.10.3  jdolecek 
    290  1.2.10.3  jdolecek         if (Actual == -1)
    291  1.2.10.3  jdolecek         {
    292  1.2.10.3  jdolecek             FlFileError (FileId, ASL_MSG_WRITE);
    293  1.2.10.3  jdolecek             AslAbort ();
    294  1.2.10.3  jdolecek         }
    295  1.2.10.3  jdolecek     }
    296  1.2.10.2       tls }
    297  1.2.10.2       tls 
    298  1.2.10.2       tls 
    299  1.2.10.2       tls /*******************************************************************************
    300  1.2.10.2       tls  *
    301  1.2.10.2       tls  * FUNCTION:    FlSeekFile
    302  1.2.10.2       tls  *
    303  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
    304  1.2.10.2       tls  *              Offset              - Absolute byte offset in file
    305  1.2.10.2       tls  *
    306  1.2.10.2       tls  * RETURN:      None
    307  1.2.10.2       tls  *
    308  1.2.10.2       tls  * DESCRIPTION: Seek to absolute offset.
    309  1.2.10.2       tls  *              NOTE: Aborts compiler on any error.
    310  1.2.10.2       tls  *
    311  1.2.10.2       tls  ******************************************************************************/
    312  1.2.10.2       tls 
    313  1.2.10.2       tls void
    314  1.2.10.2       tls FlSeekFile (
    315  1.2.10.2       tls     UINT32                  FileId,
    316  1.2.10.2       tls     long                    Offset)
    317  1.2.10.2       tls {
    318  1.2.10.2       tls     int                     Error;
    319  1.2.10.2       tls 
    320  1.2.10.2       tls 
    321  1.2.10.2       tls     Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
    322  1.2.10.2       tls     if (Error)
    323  1.2.10.2       tls     {
    324  1.2.10.2       tls         FlFileError (FileId, ASL_MSG_SEEK);
    325  1.2.10.2       tls         AslAbort ();
    326  1.2.10.2       tls     }
    327  1.2.10.2       tls }
    328  1.2.10.2       tls 
    329  1.2.10.2       tls 
    330  1.2.10.2       tls /*******************************************************************************
    331  1.2.10.2       tls  *
    332  1.2.10.2       tls  * FUNCTION:    FlCloseFile
    333  1.2.10.2       tls  *
    334  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
    335  1.2.10.2       tls  *
    336  1.2.10.2       tls  * RETURN:      None
    337  1.2.10.2       tls  *
    338  1.2.10.2       tls  * DESCRIPTION: Close an open file. Aborts compiler on error
    339  1.2.10.2       tls  *
    340  1.2.10.2       tls  ******************************************************************************/
    341  1.2.10.2       tls 
    342  1.2.10.2       tls void
    343  1.2.10.2       tls FlCloseFile (
    344  1.2.10.2       tls     UINT32                  FileId)
    345  1.2.10.2       tls {
    346  1.2.10.2       tls     int                     Error;
    347  1.2.10.2       tls 
    348  1.2.10.2       tls 
    349  1.2.10.2       tls     if (!Gbl_Files[FileId].Handle)
    350  1.2.10.2       tls     {
    351  1.2.10.2       tls         return;
    352  1.2.10.2       tls     }
    353  1.2.10.2       tls 
    354  1.2.10.2       tls     Error = fclose (Gbl_Files[FileId].Handle);
    355  1.2.10.2       tls     if (Error)
    356  1.2.10.2       tls     {
    357  1.2.10.2       tls         FlFileError (FileId, ASL_MSG_CLOSE);
    358  1.2.10.2       tls         AslAbort ();
    359  1.2.10.2       tls     }
    360  1.2.10.2       tls 
    361  1.2.10.3  jdolecek     /* Do not clear/free the filename string */
    362  1.2.10.3  jdolecek 
    363  1.2.10.2       tls     Gbl_Files[FileId].Handle = NULL;
    364  1.2.10.2       tls     return;
    365  1.2.10.2       tls }
    366  1.2.10.2       tls 
    367  1.2.10.2       tls 
    368  1.2.10.2       tls /*******************************************************************************
    369  1.2.10.2       tls  *
    370  1.2.10.2       tls  * FUNCTION:    FlDeleteFile
    371  1.2.10.2       tls  *
    372  1.2.10.2       tls  * PARAMETERS:  FileId              - Index into file info array
    373  1.2.10.2       tls  *
    374  1.2.10.2       tls  * RETURN:      None
    375  1.2.10.2       tls  *
    376  1.2.10.2       tls  * DESCRIPTION: Delete a file.
    377  1.2.10.2       tls  *
    378  1.2.10.2       tls  ******************************************************************************/
    379  1.2.10.2       tls 
    380  1.2.10.2       tls void
    381  1.2.10.2       tls FlDeleteFile (
    382  1.2.10.2       tls     UINT32                  FileId)
    383  1.2.10.2       tls {
    384  1.2.10.2       tls     ASL_FILE_INFO           *Info = &Gbl_Files[FileId];
    385  1.2.10.2       tls 
    386  1.2.10.2       tls 
    387  1.2.10.2       tls     if (!Info->Filename)
    388  1.2.10.2       tls     {
    389  1.2.10.2       tls         return;
    390  1.2.10.2       tls     }
    391  1.2.10.2       tls 
    392  1.2.10.2       tls     if (remove (Info->Filename))
    393  1.2.10.2       tls     {
    394  1.2.10.2       tls         printf ("%s (%s file) ",
    395  1.2.10.2       tls             Info->Filename, Info->Description);
    396  1.2.10.2       tls         perror ("Could not delete");
    397  1.2.10.2       tls     }
    398  1.2.10.2       tls 
    399  1.2.10.2       tls     Info->Filename = NULL;
    400  1.2.10.2       tls     return;
    401  1.2.10.2       tls }
    402