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