Home | History | Annotate | Line # | Download | only in common
acfileio.c revision 1.1.1.13
      1       1.1  christos /******************************************************************************
      2       1.1  christos  *
      3       1.1  christos  * Module Name: acfileio - Get ACPI tables from file
      4       1.1  christos  *
      5       1.1  christos  *****************************************************************************/
      6       1.1  christos 
      7       1.1  christos /*
      8  1.1.1.12  christos  * Copyright (C) 2000 - 2022, Intel Corp.
      9       1.1  christos  * All rights reserved.
     10       1.1  christos  *
     11       1.1  christos  * Redistribution and use in source and binary forms, with or without
     12       1.1  christos  * modification, are permitted provided that the following conditions
     13       1.1  christos  * are met:
     14       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15       1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16       1.1  christos  *    without modification.
     17       1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18       1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19       1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20       1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21       1.1  christos  *    binary redistribution.
     22       1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23       1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24       1.1  christos  *    from this software without specific prior written permission.
     25       1.1  christos  *
     26       1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27       1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28       1.1  christos  * Software Foundation.
     29       1.1  christos  *
     30       1.1  christos  * NO WARRANTY
     31       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32       1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1.1.10  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34       1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35       1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36       1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37       1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38       1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39       1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40       1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41       1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42       1.1  christos  */
     43       1.1  christos 
     44       1.1  christos #include "acpi.h"
     45       1.1  christos #include "accommon.h"
     46       1.1  christos #include "actables.h"
     47       1.1  christos #include "acutils.h"
     48   1.1.1.3  christos #include "acapps.h"
     49       1.1  christos 
     50       1.1  christos #define _COMPONENT          ACPI_UTILITIES
     51       1.1  christos         ACPI_MODULE_NAME    ("acfileio")
     52       1.1  christos 
     53       1.1  christos 
     54       1.1  christos /* Local prototypes */
     55       1.1  christos 
     56       1.1  christos static ACPI_STATUS
     57       1.1  christos AcGetOneTableFromFile (
     58       1.1  christos     char                    *Filename,
     59       1.1  christos     FILE                    *File,
     60       1.1  christos     UINT8                   GetOnlyAmlTables,
     61       1.1  christos     ACPI_TABLE_HEADER       **Table);
     62       1.1  christos 
     63       1.1  christos static ACPI_STATUS
     64       1.1  christos AcCheckTextModeCorruption (
     65       1.1  christos     ACPI_TABLE_HEADER       *Table);
     66       1.1  christos 
     67       1.1  christos 
     68       1.1  christos /*******************************************************************************
     69       1.1  christos  *
     70   1.1.1.5  christos  * FUNCTION:    AcDeleteTableList
     71   1.1.1.5  christos  *
     72   1.1.1.5  christos  * PARAMETERS:  ListHead            - List to delete
     73   1.1.1.5  christos  *
     74   1.1.1.5  christos  * RETURN:      Status
     75   1.1.1.5  christos  *
     76   1.1.1.5  christos  * DESCRIPTION: Delete a list of tables. This is useful for removing memory
     77   1.1.1.5  christos  *              allocated by AcGetAllTablesFromFile
     78   1.1.1.5  christos  *
     79   1.1.1.5  christos  ******************************************************************************/
     80   1.1.1.5  christos 
     81   1.1.1.5  christos void
     82   1.1.1.5  christos AcDeleteTableList (
     83   1.1.1.5  christos     ACPI_NEW_TABLE_DESC     *ListHead)
     84   1.1.1.5  christos {
     85   1.1.1.5  christos     ACPI_NEW_TABLE_DESC     *Current = ListHead;
     86   1.1.1.5  christos     ACPI_NEW_TABLE_DESC     *Previous = Current;
     87   1.1.1.5  christos 
     88   1.1.1.5  christos 
     89   1.1.1.5  christos     while (Current)
     90   1.1.1.5  christos     {
     91   1.1.1.5  christos         Current = Current->Next;
     92   1.1.1.5  christos         AcpiOsFree (Previous);
     93   1.1.1.5  christos         Previous = Current;
     94   1.1.1.5  christos     }
     95   1.1.1.5  christos }
     96   1.1.1.5  christos 
     97   1.1.1.5  christos 
     98   1.1.1.5  christos /*******************************************************************************
     99   1.1.1.5  christos  *
    100       1.1  christos  * FUNCTION:    AcGetAllTablesFromFile
    101       1.1  christos  *
    102       1.1  christos  * PARAMETERS:  Filename            - Table filename
    103       1.1  christos  *              GetOnlyAmlTables    - TRUE if the tables must be AML tables
    104       1.1  christos  *              ReturnListHead      - Where table list is returned
    105       1.1  christos  *
    106       1.1  christos  * RETURN:      Status
    107       1.1  christos  *
    108       1.1  christos  * DESCRIPTION: Get all ACPI tables from within a single file.
    109       1.1  christos  *
    110       1.1  christos  ******************************************************************************/
    111       1.1  christos 
    112       1.1  christos ACPI_STATUS
    113       1.1  christos AcGetAllTablesFromFile (
    114       1.1  christos     char                    *Filename,
    115       1.1  christos     UINT8                   GetOnlyAmlTables,
    116       1.1  christos     ACPI_NEW_TABLE_DESC     **ReturnListHead)
    117       1.1  christos {
    118       1.1  christos     ACPI_NEW_TABLE_DESC     *ListHead = NULL;
    119       1.1  christos     ACPI_NEW_TABLE_DESC     *ListTail = NULL;
    120       1.1  christos     ACPI_NEW_TABLE_DESC     *TableDesc;
    121       1.1  christos     FILE                    *File;
    122       1.1  christos     ACPI_TABLE_HEADER       *Table = NULL;
    123       1.1  christos     UINT32                  FileSize;
    124       1.1  christos     ACPI_STATUS             Status = AE_OK;
    125       1.1  christos 
    126       1.1  christos 
    127       1.1  christos     File = fopen (Filename, "rb");
    128       1.1  christos     if (!File)
    129       1.1  christos     {
    130   1.1.1.3  christos         fprintf (stderr, "Could not open input file: %s\n", Filename);
    131       1.1  christos         if (errno == ENOENT)
    132       1.1  christos         {
    133       1.1  christos             return (AE_NOT_EXIST);
    134       1.1  christos         }
    135       1.1  christos 
    136       1.1  christos         return (AE_ERROR);
    137       1.1  christos     }
    138       1.1  christos 
    139       1.1  christos     /* Get the file size */
    140       1.1  christos 
    141       1.1  christos     FileSize = CmGetFileSize (File);
    142       1.1  christos     if (FileSize == ACPI_UINT32_MAX)
    143       1.1  christos     {
    144   1.1.1.2  christos         Status = AE_ERROR;
    145   1.1.1.4  christos         goto Exit;
    146       1.1  christos     }
    147       1.1  christos 
    148       1.1  christos     fprintf (stderr,
    149       1.1  christos         "Input file %s, Length 0x%X (%u) bytes\n",
    150       1.1  christos         Filename, FileSize, FileSize);
    151       1.1  christos 
    152       1.1  christos     /* We must have at least one ACPI table header */
    153       1.1  christos 
    154       1.1  christos     if (FileSize < sizeof (ACPI_TABLE_HEADER))
    155       1.1  christos     {
    156   1.1.1.2  christos         Status = AE_BAD_HEADER;
    157   1.1.1.4  christos         goto Exit;
    158       1.1  christos     }
    159       1.1  christos 
    160       1.1  christos     /* Check for an non-binary file */
    161       1.1  christos 
    162       1.1  christos     if (!AcIsFileBinary (File))
    163       1.1  christos     {
    164       1.1  christos         fprintf (stderr,
    165       1.1  christos             "    %s: File does not appear to contain a valid AML table\n",
    166       1.1  christos             Filename);
    167   1.1.1.4  christos         Status = AE_TYPE;
    168   1.1.1.4  christos         goto Exit;
    169       1.1  christos     }
    170       1.1  christos 
    171       1.1  christos     /* Read all tables within the file */
    172       1.1  christos 
    173       1.1  christos     while (ACPI_SUCCESS (Status))
    174       1.1  christos     {
    175       1.1  christos         /* Get one entire ACPI table */
    176       1.1  christos 
    177       1.1  christos         Status = AcGetOneTableFromFile (
    178       1.1  christos             Filename, File, GetOnlyAmlTables, &Table);
    179       1.1  christos 
    180       1.1  christos         if (Status == AE_CTRL_TERMINATE)
    181       1.1  christos         {
    182       1.1  christos             Status = AE_OK;
    183       1.1  christos             break;
    184       1.1  christos         }
    185       1.1  christos         else if (Status == AE_TYPE)
    186       1.1  christos         {
    187   1.1.1.4  christos             Status = AE_OK;
    188   1.1.1.4  christos             goto Exit;
    189       1.1  christos         }
    190       1.1  christos         else if (ACPI_FAILURE (Status))
    191       1.1  christos         {
    192   1.1.1.4  christos             goto Exit;
    193       1.1  christos         }
    194       1.1  christos 
    195       1.1  christos         /* Print table header for iASL/disassembler only */
    196       1.1  christos 
    197       1.1  christos #ifdef ACPI_ASL_COMPILER
    198       1.1  christos 
    199   1.1.1.4  christos         AcpiTbPrintTableHeader (0, Table);
    200       1.1  christos #endif
    201       1.1  christos 
    202       1.1  christos         /* Allocate and link a table descriptor */
    203       1.1  christos 
    204       1.1  christos         TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
    205   1.1.1.4  christos         if (!TableDesc)
    206   1.1.1.4  christos         {
    207   1.1.1.4  christos             AcpiOsFree (Table);
    208   1.1.1.4  christos             Status = AE_NO_MEMORY;
    209   1.1.1.4  christos             goto Exit;
    210   1.1.1.4  christos         }
    211   1.1.1.4  christos 
    212       1.1  christos         TableDesc->Table = Table;
    213       1.1  christos         TableDesc->Next = NULL;
    214       1.1  christos 
    215       1.1  christos         /* Link at the end of the local table list */
    216       1.1  christos 
    217       1.1  christos         if (!ListHead)
    218       1.1  christos         {
    219       1.1  christos             ListHead = TableDesc;
    220       1.1  christos             ListTail = TableDesc;
    221       1.1  christos         }
    222       1.1  christos         else
    223       1.1  christos         {
    224       1.1  christos             ListTail->Next = TableDesc;
    225       1.1  christos             ListTail = TableDesc;
    226       1.1  christos         }
    227       1.1  christos     }
    228       1.1  christos 
    229       1.1  christos     /* Add the local table list to the end of the global list */
    230       1.1  christos 
    231       1.1  christos     if (*ReturnListHead)
    232       1.1  christos     {
    233       1.1  christos         ListTail = *ReturnListHead;
    234       1.1  christos         while (ListTail->Next)
    235       1.1  christos         {
    236       1.1  christos             ListTail = ListTail->Next;
    237       1.1  christos         }
    238       1.1  christos 
    239       1.1  christos         ListTail->Next = ListHead;
    240       1.1  christos     }
    241       1.1  christos     else
    242       1.1  christos     {
    243       1.1  christos         *ReturnListHead = ListHead;
    244       1.1  christos     }
    245       1.1  christos 
    246   1.1.1.4  christos Exit:
    247       1.1  christos     fclose(File);
    248       1.1  christos     return (Status);
    249       1.1  christos }
    250       1.1  christos 
    251       1.1  christos 
    252       1.1  christos /*******************************************************************************
    253       1.1  christos  *
    254       1.1  christos  * FUNCTION:    AcGetOneTableFromFile
    255       1.1  christos  *
    256       1.1  christos  * PARAMETERS:  Filename            - File where table is located
    257       1.1  christos  *              File                - Open FILE pointer to Filename
    258       1.1  christos  *              GetOnlyAmlTables    - TRUE if the tables must be AML tables.
    259       1.1  christos  *              ReturnTable         - Where a pointer to the table is returned
    260       1.1  christos  *
    261       1.1  christos  * RETURN:      Status
    262       1.1  christos  *
    263       1.1  christos  * DESCRIPTION: Read the next ACPI table from a file. Implements support
    264       1.1  christos  *              for multiple tables within a single file. File must already
    265       1.1  christos  *              be open.
    266       1.1  christos  *
    267       1.1  christos  * Note: Loading an RSDP is not supported.
    268       1.1  christos  *
    269       1.1  christos  ******************************************************************************/
    270       1.1  christos 
    271       1.1  christos static ACPI_STATUS
    272       1.1  christos AcGetOneTableFromFile (
    273       1.1  christos     char                    *Filename,
    274       1.1  christos     FILE                    *File,
    275       1.1  christos     UINT8                   GetOnlyAmlTables,
    276       1.1  christos     ACPI_TABLE_HEADER       **ReturnTable)
    277       1.1  christos {
    278       1.1  christos     ACPI_STATUS             Status = AE_OK;
    279       1.1  christos     ACPI_TABLE_HEADER       TableHeader;
    280       1.1  christos     ACPI_TABLE_HEADER       *Table;
    281       1.1  christos     INT32                   Count;
    282  1.1.1.13  christos     UINT32                  TableLength;
    283  1.1.1.13  christos     UINT32                  HeaderLength;
    284  1.1.1.13  christos     long                    TableOffset = 0;
    285       1.1  christos 
    286       1.1  christos     *ReturnTable = NULL;
    287       1.1  christos 
    288       1.1  christos     /* Get the table header to examine signature and length */
    289  1.1.1.13  christos     /*
    290  1.1.1.13  christos      * Special handling for the CDAT table (both the Length field
    291  1.1.1.13  christos      * and the Checksum field are not in the standard positions).
    292  1.1.1.13  christos      * (The table header is non-standard).
    293  1.1.1.13  christos      */
    294  1.1.1.13  christos     if (AcpiGbl_CDAT)
    295  1.1.1.13  christos     {
    296  1.1.1.13  christos         HeaderLength = sizeof (ACPI_TABLE_CDAT);
    297  1.1.1.13  christos     }
    298  1.1.1.13  christos     else
    299  1.1.1.13  christos     {
    300  1.1.1.13  christos         HeaderLength = sizeof (ACPI_TABLE_HEADER);
    301  1.1.1.13  christos     }
    302  1.1.1.13  christos 
    303  1.1.1.13  christos     Status = AcValidateTableHeader (File, TableOffset);
    304  1.1.1.13  christos     if (ACPI_FAILURE (Status))
    305  1.1.1.13  christos     {
    306  1.1.1.13  christos         return (Status);
    307  1.1.1.13  christos     }
    308       1.1  christos 
    309       1.1  christos     TableOffset = ftell (File);
    310  1.1.1.13  christos     Count = fread (&TableHeader, 1, HeaderLength, File);
    311  1.1.1.13  christos     if (Count != (INT32) HeaderLength)
    312       1.1  christos     {
    313       1.1  christos         return (AE_CTRL_TERMINATE);
    314       1.1  christos     }
    315       1.1  christos 
    316       1.1  christos     if (GetOnlyAmlTables)
    317       1.1  christos     {
    318   1.1.1.7  christos         /* Validate the table signature/header (limited ASCII chars) */
    319   1.1.1.7  christos 
    320   1.1.1.3  christos         /*
    321   1.1.1.3  christos          * Table must be an AML table (DSDT/SSDT).
    322   1.1.1.3  christos          * Used for iASL -e option only.
    323   1.1.1.3  christos          */
    324   1.1.1.3  christos         if (!AcpiUtIsAmlTable (&TableHeader))
    325       1.1  christos         {
    326       1.1  christos             fprintf (stderr,
    327       1.1  christos                 "    %s: Table [%4.4s] is not an AML table - ignoring\n",
    328       1.1  christos                 Filename, TableHeader.Signature);
    329       1.1  christos 
    330       1.1  christos             return (AE_TYPE);
    331       1.1  christos         }
    332       1.1  christos     }
    333       1.1  christos 
    334  1.1.1.13  christos     /*
    335  1.1.1.13  christos      * Special handling for the CDAT table (both the Length field
    336  1.1.1.13  christos      * and the Checksum field are not in the standard positions).
    337  1.1.1.13  christos      */
    338  1.1.1.13  christos     if (AcpiGbl_CDAT)
    339  1.1.1.13  christos     {
    340  1.1.1.13  christos         TableLength = ACPI_CAST_PTR (ACPI_TABLE_CDAT, &TableHeader)->Length;
    341  1.1.1.13  christos     }
    342  1.1.1.13  christos     else
    343  1.1.1.13  christos     {
    344  1.1.1.13  christos         TableLength = TableHeader.Length;
    345  1.1.1.13  christos     }
    346  1.1.1.13  christos 
    347       1.1  christos     /* Allocate a buffer for the entire table */
    348       1.1  christos 
    349  1.1.1.13  christos     Table = AcpiOsAllocate ((ACPI_SIZE) TableLength);
    350       1.1  christos     if (!Table)
    351       1.1  christos     {
    352       1.1  christos         return (AE_NO_MEMORY);
    353       1.1  christos     }
    354       1.1  christos 
    355       1.1  christos     /* Read the entire ACPI table, including header */
    356       1.1  christos 
    357       1.1  christos     fseek (File, TableOffset, SEEK_SET);
    358  1.1.1.13  christos     Count = fread (Table, 1, TableLength, File);
    359   1.1.1.7  christos 
    360   1.1.1.7  christos     /*
    361   1.1.1.7  christos      * Checks for data table headers happen later in the execution. Only verify
    362   1.1.1.7  christos      * for Aml tables at this point in the code.
    363   1.1.1.7  christos      */
    364  1.1.1.13  christos     if (GetOnlyAmlTables && Count != (INT32) TableLength)
    365       1.1  christos     {
    366       1.1  christos         Status = AE_ERROR;
    367       1.1  christos         goto ErrorExit;
    368       1.1  christos     }
    369       1.1  christos 
    370  1.1.1.13  christos     /*
    371  1.1.1.13  christos      * Validate the checksum (just issue a warning if incorrect).
    372  1.1.1.13  christos      * Note: CDAT is special cased here because the table does
    373  1.1.1.13  christos      * not have the checksum field in the standard position.
    374  1.1.1.13  christos      */
    375  1.1.1.13  christos     if (AcpiGbl_CDAT)
    376  1.1.1.13  christos     {
    377  1.1.1.13  christos         Status = AcpiUtVerifyCdatChecksum ((ACPI_TABLE_CDAT *) Table, TableLength);
    378  1.1.1.13  christos     } else
    379  1.1.1.13  christos     {
    380  1.1.1.13  christos         Status = AcpiUtVerifyChecksum (Table, TableLength);
    381  1.1.1.13  christos     }
    382       1.1  christos 
    383       1.1  christos     if (ACPI_FAILURE (Status))
    384       1.1  christos     {
    385       1.1  christos         Status = AcCheckTextModeCorruption (Table);
    386       1.1  christos         if (ACPI_FAILURE (Status))
    387       1.1  christos         {
    388       1.1  christos             goto ErrorExit;
    389       1.1  christos         }
    390       1.1  christos     }
    391       1.1  christos 
    392       1.1  christos     *ReturnTable = Table;
    393       1.1  christos     return (AE_OK);
    394       1.1  christos 
    395       1.1  christos 
    396       1.1  christos ErrorExit:
    397       1.1  christos     AcpiOsFree (Table);
    398       1.1  christos     return (Status);
    399       1.1  christos }
    400       1.1  christos 
    401       1.1  christos 
    402       1.1  christos /*******************************************************************************
    403       1.1  christos  *
    404       1.1  christos  * FUNCTION:    AcIsFileBinary
    405       1.1  christos  *
    406       1.1  christos  * PARAMETERS:  File                - Open input file
    407       1.1  christos  *
    408       1.1  christos  * RETURN:      TRUE if file appears to be binary
    409       1.1  christos  *
    410       1.1  christos  * DESCRIPTION: Scan a file for any non-ASCII bytes.
    411       1.1  christos  *
    412       1.1  christos  * Note: Maintains current file position.
    413       1.1  christos  *
    414       1.1  christos  ******************************************************************************/
    415       1.1  christos 
    416       1.1  christos BOOLEAN
    417       1.1  christos AcIsFileBinary (
    418       1.1  christos     FILE                    *File)
    419       1.1  christos {
    420       1.1  christos     UINT8                   Byte;
    421       1.1  christos     BOOLEAN                 IsBinary = FALSE;
    422       1.1  christos     long                    FileOffset;
    423       1.1  christos 
    424       1.1  christos 
    425       1.1  christos     /* Scan entire file for any non-ASCII bytes */
    426       1.1  christos 
    427       1.1  christos     FileOffset = ftell (File);
    428       1.1  christos     while (fread (&Byte, 1, 1, File) == 1)
    429       1.1  christos     {
    430       1.1  christos         if (!isprint (Byte) && !isspace (Byte))
    431       1.1  christos         {
    432       1.1  christos             IsBinary = TRUE;
    433       1.1  christos             goto Exit;
    434       1.1  christos         }
    435       1.1  christos     }
    436       1.1  christos 
    437       1.1  christos Exit:
    438       1.1  christos     fseek (File, FileOffset, SEEK_SET);
    439       1.1  christos     return (IsBinary);
    440       1.1  christos }
    441       1.1  christos 
    442       1.1  christos 
    443       1.1  christos /*******************************************************************************
    444       1.1  christos  *
    445       1.1  christos  * FUNCTION:    AcValidateTableHeader
    446       1.1  christos  *
    447       1.1  christos  * PARAMETERS:  File                - Open input file
    448       1.1  christos  *
    449       1.1  christos  * RETURN:      Status
    450       1.1  christos  *
    451       1.1  christos  * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
    452       1.1  christos  *              tables, via the
    453       1.1  christos  *              following checks on what would be the table header:
    454       1.1  christos  *              1) File must be at least as long as an ACPI_TABLE_HEADER
    455       1.1  christos  *              2) There must be enough room in the file to hold entire table
    456       1.1  christos  *              3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
    457       1.1  christos  *
    458       1.1  christos  * Note: There can be multiple definition blocks per file, so we cannot
    459       1.1  christos  * expect/compare the file size to be equal to the table length. 12/2015.
    460       1.1  christos  *
    461       1.1  christos  * Note: Maintains current file position.
    462       1.1  christos  *
    463       1.1  christos  ******************************************************************************/
    464       1.1  christos 
    465       1.1  christos ACPI_STATUS
    466       1.1  christos AcValidateTableHeader (
    467       1.1  christos     FILE                    *File,
    468       1.1  christos     long                    TableOffset)
    469       1.1  christos {
    470       1.1  christos     ACPI_TABLE_HEADER       TableHeader;
    471  1.1.1.13  christos     ACPI_TABLE_CDAT         *CdatTableHeader = ACPI_CAST_PTR (ACPI_TABLE_CDAT, &TableHeader);
    472  1.1.1.13  christos     UINT32                  HeaderLength;
    473   1.1.1.3  christos     ACPI_SIZE               Actual;
    474       1.1  christos     long                    OriginalOffset;
    475       1.1  christos     UINT32                  FileSize;
    476       1.1  christos     UINT32                  i;
    477       1.1  christos 
    478       1.1  christos 
    479   1.1.1.4  christos     ACPI_FUNCTION_TRACE (AcValidateTableHeader);
    480       1.1  christos 
    481  1.1.1.13  christos     /* Determine the type of table header */
    482  1.1.1.13  christos 
    483  1.1.1.13  christos     if (AcpiGbl_CDAT)
    484  1.1.1.13  christos     {
    485  1.1.1.13  christos         HeaderLength = sizeof (ACPI_TABLE_CDAT);
    486  1.1.1.13  christos     }
    487  1.1.1.13  christos     else
    488  1.1.1.13  christos     {
    489  1.1.1.13  christos         HeaderLength = sizeof (ACPI_TABLE_HEADER);
    490  1.1.1.13  christos     }
    491       1.1  christos 
    492       1.1  christos     /* Read a potential table header */
    493       1.1  christos 
    494       1.1  christos     OriginalOffset = ftell (File);
    495  1.1.1.11  christos     if (fseek (File, TableOffset, SEEK_SET))
    496  1.1.1.11  christos     {
    497  1.1.1.11  christos         fprintf (stderr, "SEEK error\n");
    498  1.1.1.11  christos     }
    499  1.1.1.13  christos     Actual = fread (&TableHeader, 1, HeaderLength, File);
    500  1.1.1.11  christos     if (fseek (File, OriginalOffset, SEEK_SET))
    501  1.1.1.11  christos     {
    502  1.1.1.11  christos         fprintf (stderr, "SEEK error\n");
    503  1.1.1.11  christos     }
    504       1.1  christos 
    505  1.1.1.13  christos     if (Actual < HeaderLength)
    506       1.1  christos     {
    507  1.1.1.10  christos         fprintf (stderr,
    508  1.1.1.10  christos             "Could not read entire table header: Actual %u, Requested %u\n",
    509  1.1.1.13  christos             (UINT32) Actual, HeaderLength);
    510       1.1  christos         return (AE_ERROR);
    511       1.1  christos     }
    512       1.1  christos 
    513       1.1  christos     /* Validate the signature (limited ASCII chars) */
    514       1.1  christos 
    515  1.1.1.13  christos     if (!AcpiGbl_CDAT && !AcpiUtValidNameseg (TableHeader.Signature))
    516       1.1  christos     {
    517  1.1.1.13  christos         /*
    518  1.1.1.13  christos          * The "-ds cdat" option was not used, and the signature is not valid.
    519  1.1.1.13  christos          *
    520  1.1.1.13  christos          * For CDAT we are assuming that there should be at least one non-ASCII
    521  1.1.1.13  christos          * byte in the (normally) 4-character Signature field (at least the
    522  1.1.1.13  christos          * high-order byte should be zero). Otherwise, this is OK.
    523  1.1.1.13  christos          */
    524  1.1.1.13  christos         fprintf (stderr,
    525  1.1.1.13  christos             "\nTable appears to be a CDAT table, which has no signature.\n"
    526  1.1.1.13  christos             "If this is in fact a CDAT table, use the -ds option on the\n"
    527  1.1.1.13  christos             "command line to specify the table type (signature):\n"
    528  1.1.1.13  christos             "\"iasl -d -ds CDAT <file>\" or \"iasl -ds CDAT -T CDAT\"\n\n");
    529  1.1.1.13  christos 
    530       1.1  christos         return (AE_BAD_SIGNATURE);
    531       1.1  christos     }
    532       1.1  christos 
    533       1.1  christos     /* Validate table length against bytes remaining in the file */
    534       1.1  christos 
    535       1.1  christos     FileSize = CmGetFileSize (File);
    536  1.1.1.13  christos     if (!AcpiGbl_CDAT)
    537  1.1.1.13  christos     {
    538  1.1.1.13  christos         /* Standard ACPI table header */
    539  1.1.1.13  christos 
    540  1.1.1.13  christos         if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
    541  1.1.1.13  christos         {
    542  1.1.1.13  christos             fprintf (stderr, "Table [%4.4s] is too long for file - "
    543  1.1.1.13  christos                 "needs: 0x%.2X, remaining in file: 0x%.2X\n",
    544  1.1.1.13  christos                 TableHeader.Signature, TableHeader.Length,
    545  1.1.1.13  christos                 (UINT32) (FileSize - TableOffset));
    546  1.1.1.13  christos             return (AE_BAD_HEADER);
    547  1.1.1.13  christos         }
    548  1.1.1.13  christos     }
    549  1.1.1.13  christos     else if (CdatTableHeader->Length > (UINT32) (FileSize - TableOffset))
    550       1.1  christos     {
    551  1.1.1.13  christos         /* Special header for CDAT table */
    552  1.1.1.13  christos 
    553  1.1.1.13  christos         fprintf (stderr, "Table [CDAT] is too long for file - "
    554       1.1  christos             "needs: 0x%.2X, remaining in file: 0x%.2X\n",
    555  1.1.1.13  christos             CdatTableHeader->Length,
    556       1.1  christos             (UINT32) (FileSize - TableOffset));
    557       1.1  christos         return (AE_BAD_HEADER);
    558       1.1  christos     }
    559       1.1  christos 
    560  1.1.1.13  christos     /* For CDAT table, there are no ASCII fields in the header, we are done */
    561  1.1.1.13  christos 
    562  1.1.1.13  christos     if (AcpiGbl_CDAT)
    563  1.1.1.13  christos     {
    564  1.1.1.13  christos         return (AE_OK);
    565  1.1.1.13  christos     }
    566  1.1.1.13  christos 
    567       1.1  christos     /*
    568  1.1.1.13  christos      * These standard fields must be ASCII: OemId, OemTableId, AslCompilerId.
    569       1.1  christos      * We allow a NULL terminator in OemId and OemTableId.
    570       1.1  christos      */
    571   1.1.1.8  christos     for (i = 0; i < ACPI_NAMESEG_SIZE; i++)
    572       1.1  christos     {
    573       1.1  christos         if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
    574       1.1  christos         {
    575       1.1  christos             goto BadCharacters;
    576       1.1  christos         }
    577       1.1  christos     }
    578       1.1  christos 
    579       1.1  christos     for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
    580       1.1  christos     {
    581       1.1  christos         if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
    582       1.1  christos         {
    583       1.1  christos             goto BadCharacters;
    584       1.1  christos         }
    585       1.1  christos     }
    586       1.1  christos 
    587       1.1  christos     for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
    588       1.1  christos     {
    589       1.1  christos         if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
    590       1.1  christos         {
    591       1.1  christos             goto BadCharacters;
    592       1.1  christos         }
    593       1.1  christos     }
    594       1.1  christos 
    595       1.1  christos     return (AE_OK);
    596       1.1  christos 
    597       1.1  christos 
    598       1.1  christos BadCharacters:
    599       1.1  christos 
    600       1.1  christos     ACPI_WARNING ((AE_INFO,
    601       1.1  christos         "Table header for [%4.4s] has invalid ASCII character(s)",
    602       1.1  christos         TableHeader.Signature));
    603       1.1  christos     return (AE_OK);
    604       1.1  christos }
    605       1.1  christos 
    606       1.1  christos 
    607       1.1  christos /*******************************************************************************
    608       1.1  christos  *
    609       1.1  christos  * FUNCTION:    AcCheckTextModeCorruption
    610       1.1  christos  *
    611       1.1  christos  * PARAMETERS:  Table           - Table buffer starting with table header
    612       1.1  christos  *
    613       1.1  christos  * RETURN:      Status
    614       1.1  christos  *
    615       1.1  christos  * DESCRIPTION: Check table for text mode file corruption where all linefeed
    616       1.1  christos  *              characters (LF) have been replaced by carriage return linefeed
    617       1.1  christos  *              pairs (CR/LF).
    618       1.1  christos  *
    619       1.1  christos  ******************************************************************************/
    620       1.1  christos 
    621       1.1  christos static ACPI_STATUS
    622       1.1  christos AcCheckTextModeCorruption (
    623       1.1  christos     ACPI_TABLE_HEADER       *Table)
    624       1.1  christos {
    625       1.1  christos     UINT32                  i;
    626       1.1  christos     UINT32                  Pairs = 0;
    627       1.1  christos     UINT8                   *Buffer = ACPI_CAST_PTR (UINT8, Table);
    628       1.1  christos 
    629       1.1  christos 
    630       1.1  christos     /* Scan entire table to determine if each LF has been prefixed with a CR */
    631       1.1  christos 
    632       1.1  christos     for (i = 1; i < Table->Length; i++)
    633       1.1  christos     {
    634       1.1  christos         if (Buffer[i] == 0x0A)
    635       1.1  christos         {
    636       1.1  christos             if (Buffer[i - 1] != 0x0D)
    637       1.1  christos             {
    638       1.1  christos                 /* The LF does not have a preceding CR, table not corrupted */
    639       1.1  christos 
    640       1.1  christos                 return (AE_OK);
    641       1.1  christos             }
    642       1.1  christos             else
    643       1.1  christos             {
    644       1.1  christos                 /* Found a CR/LF pair */
    645       1.1  christos 
    646       1.1  christos                 Pairs++;
    647       1.1  christos             }
    648       1.1  christos 
    649       1.1  christos             i++;
    650       1.1  christos         }
    651       1.1  christos     }
    652       1.1  christos 
    653       1.1  christos     if (!Pairs)
    654       1.1  christos     {
    655       1.1  christos         return (AE_OK);
    656       1.1  christos     }
    657       1.1  christos 
    658       1.1  christos     /*
    659       1.1  christos      * Entire table scanned, each CR is part of a CR/LF pair --
    660       1.1  christos      * meaning that the table was treated as a text file somewhere.
    661       1.1  christos      *
    662       1.1  christos      * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
    663       1.1  christos      * original table are left untouched by the text conversion process --
    664       1.1  christos      * meaning that we cannot simply replace CR/LF pairs with LFs.
    665       1.1  christos      */
    666       1.1  christos     AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
    667       1.1  christos     AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
    668       1.1  christos     AcpiOsPrintf ("Table cannot be repaired!\n");
    669       1.1  christos 
    670       1.1  christos     return (AE_BAD_VALUE);
    671       1.1  christos }
    672