Home | History | Annotate | Line # | Download | only in acpixtract
acpixtract.c revision 1.3.2.1
      1      1.1    jruoho /******************************************************************************
      2      1.1    jruoho  *
      3      1.1    jruoho  * Module Name: acpixtract - convert ascii ACPI tables to binary
      4      1.1    jruoho  *
      5      1.1    jruoho  *****************************************************************************/
      6      1.1    jruoho 
      7      1.2  christos /*
      8  1.3.2.1     skrll  * Copyright (C) 2000 - 2015, Intel Corp.
      9      1.1    jruoho  * All rights reserved.
     10      1.1    jruoho  *
     11      1.2  christos  * Redistribution and use in source and binary forms, with or without
     12      1.2  christos  * modification, are permitted provided that the following conditions
     13      1.2  christos  * are met:
     14      1.2  christos  * 1. Redistributions of source code must retain the above copyright
     15      1.2  christos  *    notice, this list of conditions, and the following disclaimer,
     16      1.2  christos  *    without modification.
     17      1.2  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.2  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.2  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.2  christos  *    including a substantially similar Disclaimer requirement for further
     21      1.2  christos  *    binary redistribution.
     22      1.2  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.2  christos  *    of any contributors may be used to endorse or promote products derived
     24      1.2  christos  *    from this software without specific prior written permission.
     25      1.2  christos  *
     26      1.2  christos  * Alternatively, this software may be distributed under the terms of the
     27      1.2  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.2  christos  * Software Foundation.
     29      1.2  christos  *
     30      1.2  christos  * NO WARRANTY
     31      1.2  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.2  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33      1.2  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34      1.2  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.2  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.2  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.2  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.2  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.2  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.2  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.2  christos  * POSSIBILITY OF SUCH DAMAGES.
     42      1.2  christos  */
     43      1.2  christos 
     44      1.2  christos #include "acpi.h"
     45      1.2  christos #include "accommon.h"
     46      1.2  christos #include "acapps.h"
     47      1.1    jruoho 
     48      1.1    jruoho #include <stdio.h>
     49      1.1    jruoho #include <stdlib.h>
     50      1.1    jruoho #include <string.h>
     51      1.1    jruoho #include <ctype.h>
     52      1.1    jruoho 
     53      1.2  christos /* Local prototypes */
     54      1.1    jruoho 
     55      1.2  christos static void
     56      1.2  christos AxStrlwr (
     57      1.2  christos     char                    *String);
     58      1.1    jruoho 
     59      1.2  christos static void
     60      1.2  christos AxCheckAscii (
     61      1.1    jruoho     char                    *Name,
     62      1.1    jruoho     int                     Count);
     63      1.1    jruoho 
     64      1.2  christos static void
     65      1.2  christos AxNormalizeSignature (
     66      1.1    jruoho     char                    *Signature);
     67      1.1    jruoho 
     68      1.2  christos static unsigned int
     69      1.2  christos AxGetNextInstance (
     70      1.1    jruoho     char                    *InputPathname,
     71      1.1    jruoho     char                    *Signature);
     72      1.1    jruoho 
     73      1.2  christos static size_t
     74      1.2  christos AxGetTableHeader (
     75      1.1    jruoho     FILE                    *InputFile,
     76      1.1    jruoho     unsigned char           *OutputData);
     77      1.1    jruoho 
     78      1.2  christos static unsigned int
     79      1.2  christos AxCountTableInstances (
     80      1.1    jruoho     char                    *InputPathname,
     81      1.1    jruoho     char                    *Signature);
     82      1.1    jruoho 
     83      1.1    jruoho int
     84      1.2  christos AxExtractTables (
     85      1.2  christos     char                    *InputPathname,
     86      1.2  christos     char                    *Signature,
     87      1.2  christos     unsigned int            MinimumInstances);
     88      1.2  christos 
     89      1.2  christos int
     90      1.2  christos AxListTables (
     91      1.1    jruoho     char                    *InputPathname);
     92      1.1    jruoho 
     93      1.2  christos static size_t
     94      1.2  christos AxConvertLine (
     95      1.1    jruoho     char                    *InputLine,
     96      1.1    jruoho     unsigned char           *OutputData);
     97      1.1    jruoho 
     98      1.2  christos static int
     99      1.2  christos AxIsEmptyLine (
    100      1.2  christos     char                    *Buffer);
    101      1.1    jruoho 
    102      1.2  christos typedef struct AxTableInfo
    103      1.2  christos {
    104      1.2  christos     UINT32                  Signature;
    105      1.2  christos     unsigned int            Instances;
    106      1.2  christos     unsigned int            NextInstance;
    107      1.2  christos     struct AxTableInfo      *Next;
    108      1.2  christos 
    109      1.2  christos } AX_TABLE_INFO;
    110      1.1    jruoho 
    111      1.2  christos /* Extraction states */
    112      1.2  christos 
    113      1.2  christos #define AX_STATE_FIND_HEADER        0
    114      1.2  christos #define AX_STATE_EXTRACT_DATA       1
    115      1.2  christos 
    116      1.2  christos /* Miscellaneous constants */
    117      1.1    jruoho 
    118      1.2  christos #define AX_LINE_BUFFER_SIZE         256
    119      1.2  christos #define AX_MIN_TABLE_NAME_LENGTH    6   /* strlen ("DSDT @") */
    120      1.1    jruoho 
    121      1.1    jruoho 
    122      1.2  christos static AX_TABLE_INFO        *AxTableListHead = NULL;
    123      1.2  christos static char                 Filename[16];
    124      1.2  christos static unsigned char        Data[16];
    125      1.2  christos static char                 LineBuffer[AX_LINE_BUFFER_SIZE];
    126      1.2  christos static char                 HeaderBuffer[AX_LINE_BUFFER_SIZE];
    127      1.2  christos static char                 InstanceBuffer[AX_LINE_BUFFER_SIZE];
    128      1.1    jruoho 
    129      1.1    jruoho 
    130      1.2  christos /*******************************************************************************
    131      1.2  christos  *
    132      1.2  christos  * FUNCTION:    AxStrlwr
    133      1.2  christos  *
    134      1.2  christos  * PARAMETERS:  String              - Ascii string
    135      1.1    jruoho  *
    136      1.2  christos  * RETURN:      None
    137      1.1    jruoho  *
    138      1.2  christos  * DESCRIPTION: String lowercase function.
    139      1.1    jruoho  *
    140      1.1    jruoho  ******************************************************************************/
    141      1.1    jruoho 
    142      1.2  christos static void
    143      1.2  christos AxStrlwr (
    144      1.2  christos     char                    *String)
    145      1.1    jruoho {
    146      1.1    jruoho 
    147      1.2  christos     while (*String)
    148      1.2  christos     {
    149      1.2  christos         *String = (char) tolower ((int) *String);
    150      1.2  christos         String++;
    151      1.2  christos     }
    152      1.1    jruoho }
    153      1.1    jruoho 
    154      1.1    jruoho 
    155      1.1    jruoho /*******************************************************************************
    156      1.1    jruoho  *
    157      1.2  christos  * FUNCTION:    AxCheckAscii
    158      1.1    jruoho  *
    159      1.1    jruoho  * PARAMETERS:  Name                - Ascii string, at least as long as Count
    160      1.1    jruoho  *              Count               - Number of characters to check
    161      1.1    jruoho  *
    162      1.1    jruoho  * RETURN:      None
    163      1.1    jruoho  *
    164      1.1    jruoho  * DESCRIPTION: Ensure that the requested number of characters are printable
    165      1.1    jruoho  *              Ascii characters. Sets non-printable and null chars to <space>.
    166      1.1    jruoho  *
    167      1.1    jruoho  ******************************************************************************/
    168      1.1    jruoho 
    169      1.2  christos static void
    170      1.2  christos AxCheckAscii (
    171      1.1    jruoho     char                    *Name,
    172      1.1    jruoho     int                     Count)
    173      1.1    jruoho {
    174      1.1    jruoho     int                     i;
    175      1.1    jruoho 
    176      1.1    jruoho 
    177      1.1    jruoho     for (i = 0; i < Count; i++)
    178      1.1    jruoho     {
    179      1.1    jruoho         if (!Name[i] || !isprint ((int) Name[i]))
    180      1.1    jruoho         {
    181      1.1    jruoho             Name[i] = ' ';
    182      1.1    jruoho         }
    183      1.1    jruoho     }
    184      1.1    jruoho }
    185      1.1    jruoho 
    186      1.1    jruoho 
    187      1.2  christos /******************************************************************************
    188      1.2  christos  *
    189      1.2  christos  * FUNCTION:    AxIsEmptyLine
    190      1.2  christos  *
    191      1.2  christos  * PARAMETERS:  Buffer              - Line from input file
    192      1.2  christos  *
    193      1.2  christos  * RETURN:      TRUE if line is empty (zero or more blanks only)
    194      1.2  christos  *
    195      1.2  christos  * DESCRIPTION: Determine if an input line is empty.
    196      1.2  christos  *
    197      1.2  christos  ******************************************************************************/
    198      1.2  christos 
    199      1.2  christos static int
    200      1.2  christos AxIsEmptyLine (
    201      1.2  christos     char                    *Buffer)
    202      1.2  christos {
    203      1.2  christos 
    204      1.2  christos     /* Skip all spaces */
    205      1.2  christos 
    206      1.2  christos     while (*Buffer == ' ')
    207      1.2  christos     {
    208      1.2  christos         Buffer++;
    209      1.2  christos     }
    210      1.2  christos 
    211      1.2  christos     /* If end-of-line, this line is empty */
    212      1.2  christos 
    213      1.2  christos     if (*Buffer == '\n')
    214      1.2  christos     {
    215      1.2  christos         return (1);
    216      1.2  christos     }
    217      1.2  christos 
    218      1.2  christos     return (0);
    219      1.2  christos }
    220      1.2  christos 
    221      1.2  christos 
    222      1.1    jruoho /*******************************************************************************
    223      1.1    jruoho  *
    224      1.2  christos  * FUNCTION:    AxNormalizeSignature
    225      1.1    jruoho  *
    226      1.1    jruoho  * PARAMETERS:  Name                - Ascii string containing an ACPI signature
    227      1.1    jruoho  *
    228      1.1    jruoho  * RETURN:      None
    229      1.1    jruoho  *
    230      1.1    jruoho  * DESCRIPTION: Change "RSD PTR" to "RSDP"
    231      1.1    jruoho  *
    232      1.1    jruoho  ******************************************************************************/
    233      1.1    jruoho 
    234      1.2  christos static void
    235      1.2  christos AxNormalizeSignature (
    236      1.1    jruoho     char                    *Signature)
    237      1.1    jruoho {
    238      1.1    jruoho 
    239      1.1    jruoho     if (!strncmp (Signature, "RSD ", 4))
    240      1.1    jruoho     {
    241      1.1    jruoho         Signature[3] = 'P';
    242      1.1    jruoho     }
    243      1.1    jruoho }
    244      1.1    jruoho 
    245      1.1    jruoho 
    246      1.1    jruoho /******************************************************************************
    247      1.1    jruoho  *
    248      1.2  christos  * FUNCTION:    AxConvertLine
    249      1.1    jruoho  *
    250      1.1    jruoho  * PARAMETERS:  InputLine           - One line from the input acpidump file
    251      1.1    jruoho  *              OutputData          - Where the converted data is returned
    252      1.1    jruoho  *
    253      1.1    jruoho  * RETURN:      The number of bytes actually converted
    254      1.1    jruoho  *
    255      1.1    jruoho  * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
    256      1.1    jruoho  *
    257      1.1    jruoho  ******************************************************************************/
    258      1.1    jruoho 
    259      1.2  christos static size_t
    260      1.2  christos AxConvertLine (
    261      1.1    jruoho     char                    *InputLine,
    262      1.1    jruoho     unsigned char           *OutputData)
    263      1.1    jruoho {
    264      1.1    jruoho     char                    *End;
    265      1.1    jruoho     int                     BytesConverted;
    266      1.1    jruoho     int                     Converted[16];
    267      1.1    jruoho     int                     i;
    268      1.1    jruoho 
    269      1.1    jruoho 
    270      1.1    jruoho     /* Terminate the input line at the end of the actual data (for sscanf) */
    271      1.1    jruoho 
    272      1.1    jruoho     End = strstr (InputLine + 2, "  ");
    273      1.1    jruoho     if (!End)
    274      1.1    jruoho     {
    275      1.1    jruoho         return (0); /* Don't understand the format */
    276      1.1    jruoho     }
    277      1.1    jruoho     *End = 0;
    278      1.1    jruoho 
    279      1.1    jruoho     /*
    280      1.1    jruoho      * Convert one line of table data, of the form:
    281      1.1    jruoho      * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
    282      1.1    jruoho      *
    283      1.1    jruoho      * Example:
    284      1.1    jruoho      * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08  _SB_LNKD........
    285      1.1    jruoho      */
    286      1.1    jruoho     BytesConverted = sscanf (InputLine,
    287      1.1    jruoho         "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
    288      1.1    jruoho         &Converted[0],  &Converted[1],  &Converted[2],  &Converted[3],
    289      1.1    jruoho         &Converted[4],  &Converted[5],  &Converted[6],  &Converted[7],
    290      1.1    jruoho         &Converted[8],  &Converted[9],  &Converted[10], &Converted[11],
    291      1.1    jruoho         &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
    292      1.1    jruoho 
    293      1.1    jruoho     /* Pack converted data into a byte array */
    294      1.1    jruoho 
    295      1.1    jruoho     for (i = 0; i < BytesConverted; i++)
    296      1.1    jruoho     {
    297      1.1    jruoho         OutputData[i] = (unsigned char) Converted[i];
    298      1.1    jruoho     }
    299      1.1    jruoho 
    300      1.1    jruoho     return ((size_t) BytesConverted);
    301      1.1    jruoho }
    302      1.1    jruoho 
    303      1.1    jruoho 
    304      1.1    jruoho /******************************************************************************
    305      1.1    jruoho  *
    306      1.2  christos  * FUNCTION:    AxGetTableHeader
    307      1.1    jruoho  *
    308      1.1    jruoho  * PARAMETERS:  InputFile           - Handle for the input acpidump file
    309      1.1    jruoho  *              OutputData          - Where the table header is returned
    310      1.1    jruoho  *
    311      1.1    jruoho  * RETURN:      The actual number of bytes converted
    312      1.1    jruoho  *
    313      1.1    jruoho  * DESCRIPTION: Extract and convert an ACPI table header
    314      1.1    jruoho  *
    315      1.1    jruoho  ******************************************************************************/
    316      1.1    jruoho 
    317      1.2  christos static size_t
    318      1.2  christos AxGetTableHeader (
    319      1.1    jruoho     FILE                    *InputFile,
    320      1.1    jruoho     unsigned char           *OutputData)
    321      1.1    jruoho {
    322      1.1    jruoho     size_t                  BytesConverted;
    323      1.1    jruoho     size_t                  TotalConverted = 0;
    324      1.1    jruoho     int                     i;
    325      1.1    jruoho 
    326      1.1    jruoho 
    327      1.2  christos     /* Get the full 36 byte ACPI table header, requires 3 input text lines */
    328      1.1    jruoho 
    329      1.1    jruoho     for (i = 0; i < 3; i++)
    330      1.1    jruoho     {
    331      1.2  christos         if (!fgets (HeaderBuffer, AX_LINE_BUFFER_SIZE, InputFile))
    332      1.1    jruoho         {
    333      1.1    jruoho             return (TotalConverted);
    334      1.1    jruoho         }
    335      1.1    jruoho 
    336      1.2  christos         BytesConverted = AxConvertLine (HeaderBuffer, OutputData);
    337      1.1    jruoho         TotalConverted += BytesConverted;
    338      1.1    jruoho         OutputData += 16;
    339      1.1    jruoho 
    340      1.1    jruoho         if (BytesConverted != 16)
    341      1.1    jruoho         {
    342      1.1    jruoho             return (TotalConverted);
    343      1.1    jruoho         }
    344      1.1    jruoho     }
    345      1.1    jruoho 
    346      1.1    jruoho     return (TotalConverted);
    347      1.1    jruoho }
    348      1.1    jruoho 
    349      1.1    jruoho 
    350      1.1    jruoho /******************************************************************************
    351      1.1    jruoho  *
    352      1.2  christos  * FUNCTION:    AxCountTableInstances
    353      1.1    jruoho  *
    354      1.1    jruoho  * PARAMETERS:  InputPathname       - Filename for acpidump file
    355      1.1    jruoho  *              Signature           - Requested signature to count
    356      1.1    jruoho  *
    357      1.1    jruoho  * RETURN:      The number of instances of the signature
    358      1.1    jruoho  *
    359      1.1    jruoho  * DESCRIPTION: Count the instances of tables with the given signature within
    360      1.1    jruoho  *              the input acpidump file.
    361      1.1    jruoho  *
    362      1.1    jruoho  ******************************************************************************/
    363      1.1    jruoho 
    364      1.2  christos static unsigned int
    365      1.2  christos AxCountTableInstances (
    366      1.1    jruoho     char                    *InputPathname,
    367      1.1    jruoho     char                    *Signature)
    368      1.1    jruoho {
    369      1.1    jruoho     FILE                    *InputFile;
    370      1.1    jruoho     unsigned int            Instances = 0;
    371      1.1    jruoho 
    372      1.1    jruoho 
    373      1.1    jruoho     InputFile = fopen (InputPathname, "rt");
    374      1.1    jruoho     if (!InputFile)
    375      1.1    jruoho     {
    376      1.2  christos         printf ("Could not open file %s\n", InputPathname);
    377      1.1    jruoho         return (0);
    378      1.1    jruoho     }
    379      1.1    jruoho 
    380      1.1    jruoho     /* Count the number of instances of this signature */
    381      1.1    jruoho 
    382      1.2  christos     while (fgets (InstanceBuffer, AX_LINE_BUFFER_SIZE, InputFile))
    383      1.1    jruoho     {
    384      1.1    jruoho         /* Ignore empty lines and lines that start with a space */
    385      1.1    jruoho 
    386      1.2  christos         if (AxIsEmptyLine (InstanceBuffer) ||
    387      1.2  christos             (InstanceBuffer[0] == ' '))
    388      1.1    jruoho         {
    389      1.1    jruoho             continue;
    390      1.1    jruoho         }
    391      1.1    jruoho 
    392      1.2  christos         AxNormalizeSignature (InstanceBuffer);
    393      1.2  christos         if (ACPI_COMPARE_NAME (InstanceBuffer, Signature))
    394      1.1    jruoho         {
    395      1.1    jruoho             Instances++;
    396      1.1    jruoho         }
    397      1.1    jruoho     }
    398      1.1    jruoho 
    399      1.1    jruoho     fclose (InputFile);
    400      1.1    jruoho     return (Instances);
    401      1.1    jruoho }
    402      1.1    jruoho 
    403      1.1    jruoho 
    404      1.1    jruoho /******************************************************************************
    405      1.1    jruoho  *
    406      1.2  christos  * FUNCTION:    AxGetNextInstance
    407      1.1    jruoho  *
    408      1.1    jruoho  * PARAMETERS:  InputPathname       - Filename for acpidump file
    409      1.1    jruoho  *              Signature           - Requested ACPI signature
    410      1.1    jruoho  *
    411      1.1    jruoho  * RETURN:      The next instance number for this signature. Zero if this
    412      1.1    jruoho  *              is the first instance of this signature.
    413      1.1    jruoho  *
    414      1.1    jruoho  * DESCRIPTION: Get the next instance number of the specified table. If this
    415      1.1    jruoho  *              is the first instance of the table, create a new instance
    416      1.1    jruoho  *              block. Note: only SSDT and PSDT tables can have multiple
    417      1.1    jruoho  *              instances.
    418      1.1    jruoho  *
    419      1.1    jruoho  ******************************************************************************/
    420      1.1    jruoho 
    421      1.2  christos static unsigned int
    422      1.2  christos AxGetNextInstance (
    423      1.1    jruoho     char                    *InputPathname,
    424      1.1    jruoho     char                    *Signature)
    425      1.1    jruoho {
    426      1.2  christos     AX_TABLE_INFO           *Info;
    427      1.1    jruoho 
    428      1.1    jruoho 
    429      1.2  christos     Info = AxTableListHead;
    430      1.1    jruoho     while (Info)
    431      1.1    jruoho     {
    432      1.2  christos         if (*(UINT32 *) Signature == Info->Signature)
    433      1.1    jruoho         {
    434      1.1    jruoho             break;
    435      1.1    jruoho         }
    436      1.1    jruoho 
    437      1.1    jruoho         Info = Info->Next;
    438      1.1    jruoho     }
    439      1.1    jruoho 
    440      1.1    jruoho     if (!Info)
    441      1.1    jruoho     {
    442      1.1    jruoho         /* Signature not found, create new table info block */
    443      1.1    jruoho 
    444      1.2  christos         Info = malloc (sizeof (AX_TABLE_INFO));
    445      1.1    jruoho         if (!Info)
    446      1.1    jruoho         {
    447      1.1    jruoho             printf ("Could not allocate memory\n");
    448      1.1    jruoho             exit (0);
    449      1.1    jruoho         }
    450      1.1    jruoho 
    451      1.2  christos         Info->Signature = *(UINT32 *) Signature;
    452      1.2  christos         Info->Instances = AxCountTableInstances (InputPathname, Signature);
    453      1.1    jruoho         Info->NextInstance = 1;
    454      1.2  christos         Info->Next = AxTableListHead;
    455      1.2  christos         AxTableListHead = Info;
    456      1.1    jruoho     }
    457      1.1    jruoho 
    458      1.1    jruoho     if (Info->Instances > 1)
    459      1.1    jruoho     {
    460      1.1    jruoho         return (Info->NextInstance++);
    461      1.1    jruoho     }
    462      1.1    jruoho 
    463      1.1    jruoho     return (0);
    464      1.1    jruoho }
    465      1.1    jruoho 
    466      1.1    jruoho 
    467      1.1    jruoho /******************************************************************************
    468      1.1    jruoho  *
    469      1.2  christos  * FUNCTION:    AxExtractTables
    470      1.1    jruoho  *
    471      1.1    jruoho  * PARAMETERS:  InputPathname       - Filename for acpidump file
    472      1.1    jruoho  *              Signature           - Requested ACPI signature to extract.
    473      1.1    jruoho  *                                    NULL means extract ALL tables.
    474      1.1    jruoho  *              MinimumInstances    - Min instances that are acceptable
    475      1.1    jruoho  *
    476      1.1    jruoho  * RETURN:      Status
    477      1.1    jruoho  *
    478      1.1    jruoho  * DESCRIPTION: Convert text ACPI tables to binary
    479      1.1    jruoho  *
    480      1.1    jruoho  ******************************************************************************/
    481      1.1    jruoho 
    482      1.1    jruoho int
    483      1.2  christos AxExtractTables (
    484      1.1    jruoho     char                    *InputPathname,
    485      1.1    jruoho     char                    *Signature,
    486      1.1    jruoho     unsigned int            MinimumInstances)
    487      1.1    jruoho {
    488      1.1    jruoho     FILE                    *InputFile;
    489      1.1    jruoho     FILE                    *OutputFile = NULL;
    490      1.1    jruoho     size_t                  BytesWritten;
    491      1.1    jruoho     size_t                  TotalBytesWritten = 0;
    492      1.1    jruoho     size_t                  BytesConverted;
    493      1.2  christos     unsigned int            State = AX_STATE_FIND_HEADER;
    494      1.1    jruoho     unsigned int            FoundTable = 0;
    495      1.1    jruoho     unsigned int            Instances = 0;
    496      1.1    jruoho     unsigned int            ThisInstance;
    497      1.1    jruoho     char                    ThisSignature[4];
    498      1.1    jruoho     int                     Status = 0;
    499      1.1    jruoho 
    500      1.1    jruoho 
    501      1.1    jruoho     /* Open input in text mode, output is in binary mode */
    502      1.1    jruoho 
    503      1.1    jruoho     InputFile = fopen (InputPathname, "rt");
    504      1.1    jruoho     if (!InputFile)
    505      1.1    jruoho     {
    506      1.2  christos         printf ("Could not open file %s\n", InputPathname);
    507      1.1    jruoho         return (-1);
    508      1.1    jruoho     }
    509      1.1    jruoho 
    510      1.1    jruoho     if (Signature)
    511      1.1    jruoho     {
    512      1.1    jruoho         /* Are there enough instances of the table to continue? */
    513      1.1    jruoho 
    514      1.2  christos         AxNormalizeSignature (Signature);
    515      1.1    jruoho 
    516      1.2  christos         Instances = AxCountTableInstances (InputPathname, Signature);
    517      1.1    jruoho         if (Instances < MinimumInstances)
    518      1.1    jruoho         {
    519      1.1    jruoho             printf ("Table %s was not found in %s\n", Signature, InputPathname);
    520      1.1    jruoho             Status = -1;
    521      1.1    jruoho             goto CleanupAndExit;
    522      1.1    jruoho         }
    523      1.1    jruoho 
    524      1.1    jruoho         if (Instances == 0)
    525      1.1    jruoho         {
    526      1.1    jruoho             goto CleanupAndExit;
    527      1.1    jruoho         }
    528      1.1    jruoho     }
    529      1.1    jruoho 
    530      1.1    jruoho     /* Convert all instances of the table to binary */
    531      1.1    jruoho 
    532      1.2  christos     while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
    533      1.1    jruoho     {
    534      1.1    jruoho         switch (State)
    535      1.1    jruoho         {
    536      1.2  christos         case AX_STATE_FIND_HEADER:
    537      1.2  christos 
    538      1.2  christos             /* Ignore lines that are too short to be header lines */
    539      1.2  christos 
    540      1.2  christos             if (strlen (LineBuffer) < AX_MIN_TABLE_NAME_LENGTH)
    541      1.2  christos             {
    542      1.2  christos                 continue;
    543      1.2  christos             }
    544      1.1    jruoho 
    545      1.1    jruoho             /* Ignore empty lines and lines that start with a space */
    546      1.1    jruoho 
    547      1.2  christos             if (AxIsEmptyLine (LineBuffer) ||
    548      1.2  christos                 (LineBuffer[0] == ' '))
    549      1.1    jruoho             {
    550      1.1    jruoho                 continue;
    551      1.1    jruoho             }
    552      1.1    jruoho 
    553      1.2  christos             /*
    554      1.2  christos              * Ignore lines that are not of the form <sig> @ <addr>.
    555      1.2  christos              * Examples of lines that must be supported:
    556      1.2  christos              *
    557      1.2  christos              * DSDT @ 0x737e4000
    558      1.2  christos              * XSDT @ 0x737f2fff
    559      1.2  christos              * RSD PTR @ 0xf6cd0
    560      1.2  christos              * SSDT @ (nil)
    561      1.2  christos              */
    562      1.2  christos             if (!strstr (LineBuffer, " @ "))
    563      1.2  christos             {
    564      1.2  christos                 continue;
    565      1.2  christos             }
    566      1.2  christos 
    567      1.2  christos             AxNormalizeSignature (LineBuffer);
    568      1.2  christos             ACPI_MOVE_NAME (ThisSignature, LineBuffer);
    569      1.1    jruoho 
    570      1.1    jruoho             if (Signature)
    571      1.1    jruoho             {
    572      1.1    jruoho                 /* Ignore signatures that don't match */
    573      1.1    jruoho 
    574      1.2  christos                 if (!ACPI_COMPARE_NAME (ThisSignature, Signature))
    575      1.1    jruoho                 {
    576      1.1    jruoho                     continue;
    577      1.1    jruoho                 }
    578      1.1    jruoho             }
    579      1.1    jruoho 
    580      1.1    jruoho             /*
    581      1.1    jruoho              * Get the instance number for this signature. Only the
    582      1.1    jruoho              * SSDT and PSDT tables can have multiple instances.
    583      1.1    jruoho              */
    584      1.2  christos             ThisInstance = AxGetNextInstance (InputPathname, ThisSignature);
    585      1.1    jruoho 
    586      1.1    jruoho             /* Build an output filename and create/open the output file */
    587      1.1    jruoho 
    588      1.1    jruoho             if (ThisInstance > 0)
    589      1.1    jruoho             {
    590      1.2  christos                 snprintf (Filename, sizeof(Filename), "%4.4s%u.dat", ThisSignature, ThisInstance);
    591      1.1    jruoho             }
    592      1.1    jruoho             else
    593      1.1    jruoho             {
    594      1.2  christos                 snprintf (Filename, sizeof(Filename), "%4.4s.dat", ThisSignature);
    595      1.1    jruoho             }
    596      1.1    jruoho 
    597      1.2  christos             AxStrlwr (Filename);
    598      1.1    jruoho             OutputFile = fopen (Filename, "w+b");
    599      1.1    jruoho             if (!OutputFile)
    600      1.1    jruoho             {
    601      1.2  christos                 printf ("Could not open file %s\n", Filename);
    602      1.1    jruoho                 Status = -1;
    603      1.1    jruoho                 goto CleanupAndExit;
    604      1.1    jruoho             }
    605      1.1    jruoho 
    606      1.2  christos             State = AX_STATE_EXTRACT_DATA;
    607      1.1    jruoho             TotalBytesWritten = 0;
    608      1.1    jruoho             FoundTable = 1;
    609      1.1    jruoho             continue;
    610      1.1    jruoho 
    611      1.2  christos         case AX_STATE_EXTRACT_DATA:
    612      1.1    jruoho 
    613      1.1    jruoho             /* Empty line or non-data line terminates the data */
    614      1.1    jruoho 
    615      1.2  christos             if (AxIsEmptyLine (LineBuffer) ||
    616      1.2  christos                 (LineBuffer[0] != ' '))
    617      1.1    jruoho             {
    618      1.1    jruoho                 fclose (OutputFile);
    619      1.1    jruoho                 OutputFile = NULL;
    620      1.2  christos                 State = AX_STATE_FIND_HEADER;
    621      1.1    jruoho 
    622      1.2  christos                 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
    623      1.2  christos                     ThisSignature, (unsigned int) TotalBytesWritten, Filename);
    624      1.1    jruoho                 continue;
    625      1.1    jruoho             }
    626      1.1    jruoho 
    627      1.1    jruoho             /* Convert the ascii data (one line of text) to binary */
    628      1.1    jruoho 
    629      1.2  christos             BytesConverted = AxConvertLine (LineBuffer, Data);
    630      1.1    jruoho 
    631      1.1    jruoho             /* Write the binary data */
    632      1.1    jruoho 
    633      1.1    jruoho             BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
    634      1.1    jruoho             if (BytesWritten != BytesConverted)
    635      1.1    jruoho             {
    636      1.2  christos                 printf ("Error when writing file %s\n", Filename);
    637      1.1    jruoho                 fclose (OutputFile);
    638      1.1    jruoho                 OutputFile = NULL;
    639      1.1    jruoho                 Status = -1;
    640      1.1    jruoho                 goto CleanupAndExit;
    641      1.1    jruoho             }
    642      1.1    jruoho 
    643      1.1    jruoho             TotalBytesWritten += BytesConverted;
    644      1.1    jruoho             continue;
    645      1.1    jruoho 
    646      1.1    jruoho         default:
    647      1.2  christos 
    648      1.1    jruoho             Status = -1;
    649      1.1    jruoho             goto CleanupAndExit;
    650      1.1    jruoho         }
    651      1.1    jruoho     }
    652      1.1    jruoho 
    653      1.1    jruoho     if (!FoundTable)
    654      1.1    jruoho     {
    655      1.1    jruoho         printf ("Table %s was not found in %s\n", Signature, InputPathname);
    656      1.1    jruoho     }
    657      1.1    jruoho 
    658      1.1    jruoho 
    659      1.1    jruoho CleanupAndExit:
    660      1.1    jruoho 
    661      1.1    jruoho     if (OutputFile)
    662      1.1    jruoho     {
    663      1.1    jruoho         fclose (OutputFile);
    664      1.2  christos         if (State == AX_STATE_EXTRACT_DATA)
    665      1.1    jruoho         {
    666      1.1    jruoho             /* Received an EOF while extracting data */
    667      1.1    jruoho 
    668      1.2  christos             printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
    669      1.2  christos                 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
    670      1.1    jruoho         }
    671      1.1    jruoho     }
    672      1.1    jruoho 
    673      1.1    jruoho     fclose (InputFile);
    674      1.1    jruoho     return (Status);
    675      1.1    jruoho }
    676      1.1    jruoho 
    677      1.1    jruoho 
    678      1.1    jruoho /******************************************************************************
    679      1.1    jruoho  *
    680      1.2  christos  * FUNCTION:    AxListTables
    681      1.1    jruoho  *
    682      1.1    jruoho  * PARAMETERS:  InputPathname       - Filename for acpidump file
    683      1.1    jruoho  *
    684      1.1    jruoho  * RETURN:      Status
    685      1.1    jruoho  *
    686      1.1    jruoho  * DESCRIPTION: Display info for all ACPI tables found in input. Does not
    687      1.1    jruoho  *              perform an actual extraction of the tables.
    688      1.1    jruoho  *
    689      1.1    jruoho  ******************************************************************************/
    690      1.1    jruoho 
    691      1.1    jruoho int
    692      1.2  christos AxListTables (
    693      1.1    jruoho     char                    *InputPathname)
    694      1.1    jruoho {
    695      1.1    jruoho     FILE                    *InputFile;
    696      1.1    jruoho     size_t                  HeaderSize;
    697      1.1    jruoho     unsigned char           Header[48];
    698      1.1    jruoho     int                     TableCount = 0;
    699      1.1    jruoho     ACPI_TABLE_HEADER       *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
    700      1.1    jruoho 
    701      1.1    jruoho 
    702      1.1    jruoho     /* Open input in text mode, output is in binary mode */
    703      1.1    jruoho 
    704      1.1    jruoho     InputFile = fopen (InputPathname, "rt");
    705      1.1    jruoho     if (!InputFile)
    706      1.1    jruoho     {
    707      1.2  christos         printf ("Could not open file %s\n", InputPathname);
    708      1.1    jruoho         return (-1);
    709      1.1    jruoho     }
    710      1.1    jruoho 
    711      1.1    jruoho     /* Dump the headers for all tables found in the input file */
    712      1.1    jruoho 
    713      1.2  christos     printf ("\nSignature  Length      Revision   OemId    OemTableId"
    714      1.1    jruoho             "   OemRevision CompilerId CompilerRevision\n\n");
    715      1.1    jruoho 
    716      1.2  christos     while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
    717      1.1    jruoho     {
    718      1.1    jruoho         /* Ignore empty lines and lines that start with a space */
    719      1.1    jruoho 
    720      1.2  christos         if (AxIsEmptyLine (LineBuffer) ||
    721      1.2  christos             (LineBuffer[0] == ' '))
    722      1.1    jruoho         {
    723      1.1    jruoho             continue;
    724      1.1    jruoho         }
    725      1.1    jruoho 
    726      1.1    jruoho         /* Get the 36 byte header and display the fields */
    727      1.1    jruoho 
    728      1.2  christos         HeaderSize = AxGetTableHeader (InputFile, Header);
    729      1.1    jruoho         if (HeaderSize < 16)
    730      1.1    jruoho         {
    731      1.1    jruoho             continue;
    732      1.1    jruoho         }
    733      1.1    jruoho 
    734      1.1    jruoho         /* RSDP has an oddball signature and header */
    735      1.1    jruoho 
    736      1.1    jruoho         if (!strncmp (TableHeader->Signature, "RSD PTR ", 8))
    737      1.1    jruoho         {
    738      1.2  christos             AxCheckAscii ((char *) &Header[9], 6);
    739      1.2  christos             printf ("%7.4s                          \"%6.6s\"\n", "RSDP", &Header[9]);
    740      1.1    jruoho             TableCount++;
    741      1.1    jruoho             continue;
    742      1.1    jruoho         }
    743      1.1    jruoho 
    744      1.1    jruoho         /* Minimum size for table with standard header */
    745      1.1    jruoho 
    746      1.2  christos         if (HeaderSize < sizeof (ACPI_TABLE_HEADER))
    747      1.1    jruoho         {
    748      1.1    jruoho             continue;
    749      1.1    jruoho         }
    750      1.1    jruoho 
    751      1.1    jruoho         /* Signature and Table length */
    752      1.1    jruoho 
    753      1.1    jruoho         TableCount++;
    754      1.2  christos         printf ("%7.4s   0x%8.8X", TableHeader->Signature, TableHeader->Length);
    755      1.1    jruoho 
    756      1.1    jruoho         /* FACS has only signature and length */
    757      1.1    jruoho 
    758      1.2  christos         if (ACPI_COMPARE_NAME (TableHeader->Signature, "FACS"))
    759      1.1    jruoho         {
    760      1.1    jruoho             printf ("\n");
    761      1.1    jruoho             continue;
    762      1.1    jruoho         }
    763      1.1    jruoho 
    764      1.1    jruoho         /* OEM IDs and Compiler IDs */
    765      1.1    jruoho 
    766      1.2  christos         AxCheckAscii (TableHeader->OemId, 6);
    767      1.2  christos         AxCheckAscii (TableHeader->OemTableId, 8);
    768      1.2  christos         AxCheckAscii (TableHeader->AslCompilerId, 4);
    769      1.1    jruoho 
    770      1.2  christos         printf ("     0x%2.2X    \"%6.6s\"  \"%8.8s\"   0x%8.8X    \"%4.4s\"     0x%8.8X\n",
    771      1.1    jruoho             TableHeader->Revision, TableHeader->OemId,
    772      1.1    jruoho             TableHeader->OemTableId, TableHeader->OemRevision,
    773      1.1    jruoho             TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
    774      1.1    jruoho     }
    775      1.1    jruoho 
    776      1.2  christos     printf ("\nFound %u ACPI tables\n", TableCount);
    777      1.1    jruoho     fclose (InputFile);
    778      1.1    jruoho     return (0);
    779      1.1    jruoho }
    780