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