Home | History | Annotate | Line # | Download | only in common
acfileio.c revision 1.1.1.3
      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  christos  * Copyright (C) 2000 - 2016, 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  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY 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  christos  * FUNCTION:    AcGetAllTablesFromFile
     71      1.1  christos  *
     72      1.1  christos  * PARAMETERS:  Filename            - Table filename
     73      1.1  christos  *              GetOnlyAmlTables    - TRUE if the tables must be AML tables
     74      1.1  christos  *              ReturnListHead      - Where table list is returned
     75      1.1  christos  *
     76      1.1  christos  * RETURN:      Status
     77      1.1  christos  *
     78      1.1  christos  * DESCRIPTION: Get all ACPI tables from within a single file.
     79      1.1  christos  *
     80      1.1  christos  ******************************************************************************/
     81      1.1  christos 
     82      1.1  christos ACPI_STATUS
     83      1.1  christos AcGetAllTablesFromFile (
     84      1.1  christos     char                    *Filename,
     85      1.1  christos     UINT8                   GetOnlyAmlTables,
     86      1.1  christos     ACPI_NEW_TABLE_DESC     **ReturnListHead)
     87      1.1  christos {
     88      1.1  christos     ACPI_NEW_TABLE_DESC     *ListHead = NULL;
     89      1.1  christos     ACPI_NEW_TABLE_DESC     *ListTail = NULL;
     90      1.1  christos     ACPI_NEW_TABLE_DESC     *TableDesc;
     91      1.1  christos     FILE                    *File;
     92      1.1  christos     ACPI_TABLE_HEADER       *Table = NULL;
     93      1.1  christos     UINT32                  FileSize;
     94      1.1  christos     ACPI_STATUS             Status = AE_OK;
     95      1.1  christos 
     96      1.1  christos 
     97      1.1  christos     File = fopen (Filename, "rb");
     98      1.1  christos     if (!File)
     99      1.1  christos     {
    100  1.1.1.3  christos         fprintf (stderr, "Could not open input file: %s\n", Filename);
    101      1.1  christos         if (errno == ENOENT)
    102      1.1  christos         {
    103      1.1  christos             return (AE_NOT_EXIST);
    104      1.1  christos         }
    105      1.1  christos 
    106      1.1  christos         return (AE_ERROR);
    107      1.1  christos     }
    108      1.1  christos 
    109      1.1  christos     /* Get the file size */
    110      1.1  christos 
    111      1.1  christos     FileSize = CmGetFileSize (File);
    112      1.1  christos     if (FileSize == ACPI_UINT32_MAX)
    113      1.1  christos     {
    114  1.1.1.2  christos         Status = AE_ERROR;
    115  1.1.1.2  christos         goto ErrorExit;
    116      1.1  christos     }
    117      1.1  christos 
    118      1.1  christos     fprintf (stderr,
    119      1.1  christos         "Input file %s, Length 0x%X (%u) bytes\n",
    120      1.1  christos         Filename, FileSize, FileSize);
    121      1.1  christos 
    122      1.1  christos     /* We must have at least one ACPI table header */
    123      1.1  christos 
    124      1.1  christos     if (FileSize < sizeof (ACPI_TABLE_HEADER))
    125      1.1  christos     {
    126  1.1.1.2  christos         Status = AE_BAD_HEADER;
    127  1.1.1.2  christos         goto ErrorExit;
    128      1.1  christos     }
    129      1.1  christos 
    130      1.1  christos     /* Check for an non-binary file */
    131      1.1  christos 
    132      1.1  christos     if (!AcIsFileBinary (File))
    133      1.1  christos     {
    134      1.1  christos         fprintf (stderr,
    135      1.1  christos             "    %s: File does not appear to contain a valid AML table\n",
    136      1.1  christos             Filename);
    137      1.1  christos         return (AE_TYPE);
    138      1.1  christos     }
    139      1.1  christos 
    140      1.1  christos     /* Read all tables within the file */
    141      1.1  christos 
    142      1.1  christos     while (ACPI_SUCCESS (Status))
    143      1.1  christos     {
    144      1.1  christos         /* Get one entire ACPI table */
    145      1.1  christos 
    146      1.1  christos         Status = AcGetOneTableFromFile (
    147      1.1  christos             Filename, File, GetOnlyAmlTables, &Table);
    148      1.1  christos 
    149      1.1  christos         if (Status == AE_CTRL_TERMINATE)
    150      1.1  christos         {
    151      1.1  christos             Status = AE_OK;
    152      1.1  christos             break;
    153      1.1  christos         }
    154      1.1  christos         else if (Status == AE_TYPE)
    155      1.1  christos         {
    156      1.1  christos             return (AE_OK);
    157      1.1  christos         }
    158      1.1  christos         else if (ACPI_FAILURE (Status))
    159      1.1  christos         {
    160  1.1.1.2  christos             goto ErrorExit;
    161      1.1  christos         }
    162      1.1  christos 
    163      1.1  christos         /* Print table header for iASL/disassembler only */
    164      1.1  christos 
    165      1.1  christos #ifdef ACPI_ASL_COMPILER
    166      1.1  christos 
    167      1.1  christos             AcpiTbPrintTableHeader (0, Table);
    168      1.1  christos #endif
    169      1.1  christos 
    170      1.1  christos         /* Allocate and link a table descriptor */
    171      1.1  christos 
    172      1.1  christos         TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
    173      1.1  christos         TableDesc->Table = Table;
    174      1.1  christos         TableDesc->Next = NULL;
    175      1.1  christos 
    176      1.1  christos         /* Link at the end of the local table list */
    177      1.1  christos 
    178      1.1  christos         if (!ListHead)
    179      1.1  christos         {
    180      1.1  christos             ListHead = TableDesc;
    181      1.1  christos             ListTail = TableDesc;
    182      1.1  christos         }
    183      1.1  christos         else
    184      1.1  christos         {
    185      1.1  christos             ListTail->Next = TableDesc;
    186      1.1  christos             ListTail = TableDesc;
    187      1.1  christos         }
    188      1.1  christos     }
    189      1.1  christos 
    190      1.1  christos     /* Add the local table list to the end of the global list */
    191      1.1  christos 
    192      1.1  christos     if (*ReturnListHead)
    193      1.1  christos     {
    194      1.1  christos         ListTail = *ReturnListHead;
    195      1.1  christos         while (ListTail->Next)
    196      1.1  christos         {
    197      1.1  christos             ListTail = ListTail->Next;
    198      1.1  christos         }
    199      1.1  christos 
    200      1.1  christos         ListTail->Next = ListHead;
    201      1.1  christos     }
    202      1.1  christos     else
    203      1.1  christos     {
    204      1.1  christos         *ReturnListHead = ListHead;
    205      1.1  christos     }
    206      1.1  christos 
    207  1.1.1.2  christos ErrorExit:
    208      1.1  christos     fclose(File);
    209      1.1  christos     return (Status);
    210      1.1  christos }
    211      1.1  christos 
    212      1.1  christos 
    213      1.1  christos /*******************************************************************************
    214      1.1  christos  *
    215      1.1  christos  * FUNCTION:    AcGetOneTableFromFile
    216      1.1  christos  *
    217      1.1  christos  * PARAMETERS:  Filename            - File where table is located
    218      1.1  christos  *              File                - Open FILE pointer to Filename
    219      1.1  christos  *              GetOnlyAmlTables    - TRUE if the tables must be AML tables.
    220      1.1  christos  *              ReturnTable         - Where a pointer to the table is returned
    221      1.1  christos  *
    222      1.1  christos  * RETURN:      Status
    223      1.1  christos  *
    224      1.1  christos  * DESCRIPTION: Read the next ACPI table from a file. Implements support
    225      1.1  christos  *              for multiple tables within a single file. File must already
    226      1.1  christos  *              be open.
    227      1.1  christos  *
    228      1.1  christos  * Note: Loading an RSDP is not supported.
    229      1.1  christos  *
    230      1.1  christos  ******************************************************************************/
    231      1.1  christos 
    232      1.1  christos static ACPI_STATUS
    233      1.1  christos AcGetOneTableFromFile (
    234      1.1  christos     char                    *Filename,
    235      1.1  christos     FILE                    *File,
    236      1.1  christos     UINT8                   GetOnlyAmlTables,
    237      1.1  christos     ACPI_TABLE_HEADER       **ReturnTable)
    238      1.1  christos {
    239      1.1  christos     ACPI_STATUS             Status = AE_OK;
    240      1.1  christos     ACPI_TABLE_HEADER       TableHeader;
    241      1.1  christos     ACPI_TABLE_HEADER       *Table;
    242      1.1  christos     INT32                   Count;
    243      1.1  christos     long                    TableOffset;
    244      1.1  christos 
    245      1.1  christos 
    246      1.1  christos     *ReturnTable = NULL;
    247      1.1  christos 
    248      1.1  christos     /* Get the table header to examine signature and length */
    249      1.1  christos 
    250      1.1  christos     TableOffset = ftell (File);
    251      1.1  christos     Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
    252      1.1  christos     if (Count != sizeof (ACPI_TABLE_HEADER))
    253      1.1  christos     {
    254      1.1  christos         return (AE_CTRL_TERMINATE);
    255      1.1  christos     }
    256      1.1  christos 
    257      1.1  christos     /* Validate the table signature/header (limited ASCII chars) */
    258      1.1  christos 
    259      1.1  christos     Status = AcValidateTableHeader (File, TableOffset);
    260      1.1  christos     if (ACPI_FAILURE (Status))
    261      1.1  christos     {
    262      1.1  christos         return (Status);
    263      1.1  christos     }
    264      1.1  christos 
    265  1.1.1.3  christos 
    266      1.1  christos     if (GetOnlyAmlTables)
    267      1.1  christos     {
    268  1.1.1.3  christos         /*
    269  1.1.1.3  christos          * Table must be an AML table (DSDT/SSDT).
    270  1.1.1.3  christos          * Used for iASL -e option only.
    271  1.1.1.3  christos          */
    272  1.1.1.3  christos         if (!AcpiUtIsAmlTable (&TableHeader))
    273      1.1  christos         {
    274      1.1  christos             fprintf (stderr,
    275      1.1  christos                 "    %s: Table [%4.4s] is not an AML table - ignoring\n",
    276      1.1  christos                 Filename, TableHeader.Signature);
    277      1.1  christos 
    278      1.1  christos             return (AE_TYPE);
    279      1.1  christos         }
    280      1.1  christos     }
    281      1.1  christos 
    282      1.1  christos     /* Allocate a buffer for the entire table */
    283      1.1  christos 
    284  1.1.1.3  christos     Table = AcpiOsAllocate ((ACPI_SIZE) TableHeader.Length);
    285      1.1  christos     if (!Table)
    286      1.1  christos     {
    287      1.1  christos         return (AE_NO_MEMORY);
    288      1.1  christos     }
    289      1.1  christos 
    290      1.1  christos     /* Read the entire ACPI table, including header */
    291      1.1  christos 
    292      1.1  christos     fseek (File, TableOffset, SEEK_SET);
    293      1.1  christos 
    294      1.1  christos     Count = fread (Table, 1, TableHeader.Length, File);
    295      1.1  christos     if (Count != (INT32) TableHeader.Length)
    296      1.1  christos     {
    297      1.1  christos         Status = AE_ERROR;
    298      1.1  christos         goto ErrorExit;
    299      1.1  christos     }
    300      1.1  christos 
    301      1.1  christos     /* Validate the checksum (just issue a warning) */
    302      1.1  christos 
    303      1.1  christos     Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
    304      1.1  christos     if (ACPI_FAILURE (Status))
    305      1.1  christos     {
    306      1.1  christos         Status = AcCheckTextModeCorruption (Table);
    307      1.1  christos         if (ACPI_FAILURE (Status))
    308      1.1  christos         {
    309      1.1  christos             goto ErrorExit;
    310      1.1  christos         }
    311      1.1  christos     }
    312      1.1  christos 
    313      1.1  christos     *ReturnTable = Table;
    314      1.1  christos     return (AE_OK);
    315      1.1  christos 
    316      1.1  christos 
    317      1.1  christos ErrorExit:
    318      1.1  christos     AcpiOsFree (Table);
    319      1.1  christos     return (Status);
    320      1.1  christos }
    321      1.1  christos 
    322      1.1  christos 
    323      1.1  christos /*******************************************************************************
    324      1.1  christos  *
    325      1.1  christos  * FUNCTION:    AcIsFileBinary
    326      1.1  christos  *
    327      1.1  christos  * PARAMETERS:  File                - Open input file
    328      1.1  christos  *
    329      1.1  christos  * RETURN:      TRUE if file appears to be binary
    330      1.1  christos  *
    331      1.1  christos  * DESCRIPTION: Scan a file for any non-ASCII bytes.
    332      1.1  christos  *
    333      1.1  christos  * Note: Maintains current file position.
    334      1.1  christos  *
    335      1.1  christos  ******************************************************************************/
    336      1.1  christos 
    337      1.1  christos BOOLEAN
    338      1.1  christos AcIsFileBinary (
    339      1.1  christos     FILE                    *File)
    340      1.1  christos {
    341      1.1  christos     UINT8                   Byte;
    342      1.1  christos     BOOLEAN                 IsBinary = FALSE;
    343      1.1  christos     long                    FileOffset;
    344      1.1  christos 
    345      1.1  christos 
    346      1.1  christos     /* Scan entire file for any non-ASCII bytes */
    347      1.1  christos 
    348      1.1  christos     FileOffset = ftell (File);
    349      1.1  christos     while (fread (&Byte, 1, 1, File) == 1)
    350      1.1  christos     {
    351      1.1  christos         if (!isprint (Byte) && !isspace (Byte))
    352      1.1  christos         {
    353      1.1  christos             IsBinary = TRUE;
    354      1.1  christos             goto Exit;
    355      1.1  christos         }
    356      1.1  christos     }
    357      1.1  christos 
    358      1.1  christos Exit:
    359      1.1  christos     fseek (File, FileOffset, SEEK_SET);
    360      1.1  christos     return (IsBinary);
    361      1.1  christos }
    362      1.1  christos 
    363      1.1  christos 
    364      1.1  christos /*******************************************************************************
    365      1.1  christos  *
    366      1.1  christos  * FUNCTION:    AcValidateTableHeader
    367      1.1  christos  *
    368      1.1  christos  * PARAMETERS:  File                - Open input file
    369      1.1  christos  *
    370      1.1  christos  * RETURN:      Status
    371      1.1  christos  *
    372      1.1  christos  * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
    373      1.1  christos  *              tables, via the
    374      1.1  christos  *              following checks on what would be the table header:
    375      1.1  christos  *              1) File must be at least as long as an ACPI_TABLE_HEADER
    376      1.1  christos  *              2) There must be enough room in the file to hold entire table
    377      1.1  christos  *              3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
    378      1.1  christos  *
    379      1.1  christos  * Note: There can be multiple definition blocks per file, so we cannot
    380      1.1  christos  * expect/compare the file size to be equal to the table length. 12/2015.
    381      1.1  christos  *
    382      1.1  christos  * Note: Maintains current file position.
    383      1.1  christos  *
    384      1.1  christos  ******************************************************************************/
    385      1.1  christos 
    386      1.1  christos ACPI_STATUS
    387      1.1  christos AcValidateTableHeader (
    388      1.1  christos     FILE                    *File,
    389      1.1  christos     long                    TableOffset)
    390      1.1  christos {
    391      1.1  christos     ACPI_TABLE_HEADER       TableHeader;
    392  1.1.1.3  christos     ACPI_SIZE               Actual;
    393      1.1  christos     long                    OriginalOffset;
    394      1.1  christos     UINT32                  FileSize;
    395      1.1  christos     UINT32                  i;
    396      1.1  christos 
    397      1.1  christos 
    398      1.1  christos     ACPI_FUNCTION_TRACE ("AcValidateTableHeader");
    399      1.1  christos 
    400      1.1  christos 
    401      1.1  christos     /* Read a potential table header */
    402      1.1  christos 
    403      1.1  christos     OriginalOffset = ftell (File);
    404      1.1  christos     fseek (File, TableOffset, SEEK_SET);
    405      1.1  christos 
    406      1.1  christos     Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
    407      1.1  christos     fseek (File, OriginalOffset, SEEK_SET);
    408      1.1  christos 
    409      1.1  christos     if (Actual < sizeof (ACPI_TABLE_HEADER))
    410      1.1  christos     {
    411      1.1  christos         return (AE_ERROR);
    412      1.1  christos     }
    413      1.1  christos 
    414      1.1  christos     /* Validate the signature (limited ASCII chars) */
    415      1.1  christos 
    416  1.1.1.2  christos     if (!AcpiUtValidNameseg (TableHeader.Signature))
    417      1.1  christos     {
    418      1.1  christos         fprintf (stderr, "Invalid table signature: 0x%8.8X\n",
    419      1.1  christos             *ACPI_CAST_PTR (UINT32, TableHeader.Signature));
    420      1.1  christos         return (AE_BAD_SIGNATURE);
    421      1.1  christos     }
    422      1.1  christos 
    423      1.1  christos     /* Validate table length against bytes remaining in the file */
    424      1.1  christos 
    425      1.1  christos     FileSize = CmGetFileSize (File);
    426      1.1  christos     if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
    427      1.1  christos     {
    428      1.1  christos         fprintf (stderr, "Table [%4.4s] is too long for file - "
    429      1.1  christos             "needs: 0x%.2X, remaining in file: 0x%.2X\n",
    430      1.1  christos             TableHeader.Signature, TableHeader.Length,
    431      1.1  christos             (UINT32) (FileSize - TableOffset));
    432      1.1  christos         return (AE_BAD_HEADER);
    433      1.1  christos     }
    434      1.1  christos 
    435      1.1  christos     /*
    436      1.1  christos      * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
    437      1.1  christos      * We allow a NULL terminator in OemId and OemTableId.
    438      1.1  christos      */
    439      1.1  christos     for (i = 0; i < ACPI_NAME_SIZE; i++)
    440      1.1  christos     {
    441      1.1  christos         if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
    442      1.1  christos         {
    443      1.1  christos             goto BadCharacters;
    444      1.1  christos         }
    445      1.1  christos     }
    446      1.1  christos 
    447      1.1  christos     for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
    448      1.1  christos     {
    449      1.1  christos         if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
    450      1.1  christos         {
    451      1.1  christos             goto BadCharacters;
    452      1.1  christos         }
    453      1.1  christos     }
    454      1.1  christos 
    455      1.1  christos     for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
    456      1.1  christos     {
    457      1.1  christos         if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
    458      1.1  christos         {
    459      1.1  christos             goto BadCharacters;
    460      1.1  christos         }
    461      1.1  christos     }
    462      1.1  christos 
    463      1.1  christos     return (AE_OK);
    464      1.1  christos 
    465      1.1  christos 
    466      1.1  christos BadCharacters:
    467      1.1  christos 
    468      1.1  christos     ACPI_WARNING ((AE_INFO,
    469      1.1  christos         "Table header for [%4.4s] has invalid ASCII character(s)",
    470      1.1  christos         TableHeader.Signature));
    471      1.1  christos     return (AE_OK);
    472      1.1  christos }
    473      1.1  christos 
    474      1.1  christos 
    475      1.1  christos /*******************************************************************************
    476      1.1  christos  *
    477      1.1  christos  * FUNCTION:    AcCheckTextModeCorruption
    478      1.1  christos  *
    479      1.1  christos  * PARAMETERS:  Table           - Table buffer starting with table header
    480      1.1  christos  *
    481      1.1  christos  * RETURN:      Status
    482      1.1  christos  *
    483      1.1  christos  * DESCRIPTION: Check table for text mode file corruption where all linefeed
    484      1.1  christos  *              characters (LF) have been replaced by carriage return linefeed
    485      1.1  christos  *              pairs (CR/LF).
    486      1.1  christos  *
    487      1.1  christos  ******************************************************************************/
    488      1.1  christos 
    489      1.1  christos static ACPI_STATUS
    490      1.1  christos AcCheckTextModeCorruption (
    491      1.1  christos     ACPI_TABLE_HEADER       *Table)
    492      1.1  christos {
    493      1.1  christos     UINT32                  i;
    494      1.1  christos     UINT32                  Pairs = 0;
    495      1.1  christos     UINT8                   *Buffer = ACPI_CAST_PTR (UINT8, Table);
    496      1.1  christos 
    497      1.1  christos 
    498      1.1  christos     /* Scan entire table to determine if each LF has been prefixed with a CR */
    499      1.1  christos 
    500      1.1  christos     for (i = 1; i < Table->Length; i++)
    501      1.1  christos     {
    502      1.1  christos         if (Buffer[i] == 0x0A)
    503      1.1  christos         {
    504      1.1  christos             if (Buffer[i - 1] != 0x0D)
    505      1.1  christos             {
    506      1.1  christos                 /* The LF does not have a preceding CR, table not corrupted */
    507      1.1  christos 
    508      1.1  christos                 return (AE_OK);
    509      1.1  christos             }
    510      1.1  christos             else
    511      1.1  christos             {
    512      1.1  christos                 /* Found a CR/LF pair */
    513      1.1  christos 
    514      1.1  christos                 Pairs++;
    515      1.1  christos             }
    516      1.1  christos 
    517      1.1  christos             i++;
    518      1.1  christos         }
    519      1.1  christos     }
    520      1.1  christos 
    521      1.1  christos     if (!Pairs)
    522      1.1  christos     {
    523      1.1  christos         return (AE_OK);
    524      1.1  christos     }
    525      1.1  christos 
    526      1.1  christos     /*
    527      1.1  christos      * Entire table scanned, each CR is part of a CR/LF pair --
    528      1.1  christos      * meaning that the table was treated as a text file somewhere.
    529      1.1  christos      *
    530      1.1  christos      * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
    531      1.1  christos      * original table are left untouched by the text conversion process --
    532      1.1  christos      * meaning that we cannot simply replace CR/LF pairs with LFs.
    533      1.1  christos      */
    534      1.1  christos     AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
    535      1.1  christos     AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
    536      1.1  christos     AcpiOsPrintf ("Table cannot be repaired!\n");
    537      1.1  christos 
    538      1.1  christos     return (AE_BAD_VALUE);
    539      1.1  christos }
    540