Subversion Repositories Kolibri OS

Rev

Rev 1498 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. /******************************************************************************
  3.  *
  4.  * Module Name: acpixtract - convert ascii ACPI tables to binary
  5.  *
  6.  *****************************************************************************/
  7.  
  8. /******************************************************************************
  9.  *
  10.  * 1. Copyright Notice
  11.  *
  12.  * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
  13.  * All rights reserved.
  14.  *
  15.  * 2. License
  16.  *
  17.  * 2.1. This is your license from Intel Corp. under its intellectual property
  18.  * rights.  You may have additional license terms from the party that provided
  19.  * you this software, covering your right to use that party's intellectual
  20.  * property rights.
  21.  *
  22.  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
  23.  * copy of the source code appearing in this file ("Covered Code") an
  24.  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
  25.  * base code distributed originally by Intel ("Original Intel Code") to copy,
  26.  * make derivatives, distribute, use and display any portion of the Covered
  27.  * Code in any form, with the right to sublicense such rights; and
  28.  *
  29.  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
  30.  * license (with the right to sublicense), under only those claims of Intel
  31.  * patents that are infringed by the Original Intel Code, to make, use, sell,
  32.  * offer to sell, and import the Covered Code and derivative works thereof
  33.  * solely to the minimum extent necessary to exercise the above copyright
  34.  * license, and in no event shall the patent license extend to any additions
  35.  * to or modifications of the Original Intel Code.  No other license or right
  36.  * is granted directly or by implication, estoppel or otherwise;
  37.  *
  38.  * The above copyright and patent license is granted only if the following
  39.  * conditions are met:
  40.  *
  41.  * 3. Conditions
  42.  *
  43.  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
  44.  * Redistribution of source code of any substantial portion of the Covered
  45.  * Code or modification with rights to further distribute source must include
  46.  * the above Copyright Notice, the above License, this list of Conditions,
  47.  * and the following Disclaimer and Export Compliance provision.  In addition,
  48.  * Licensee must cause all Covered Code to which Licensee contributes to
  49.  * contain a file documenting the changes Licensee made to create that Covered
  50.  * Code and the date of any change.  Licensee must include in that file the
  51.  * documentation of any changes made by any predecessor Licensee.  Licensee
  52.  * must include a prominent statement that the modification is derived,
  53.  * directly or indirectly, from Original Intel Code.
  54.  *
  55.  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
  56.  * Redistribution of source code of any substantial portion of the Covered
  57.  * Code or modification without rights to further distribute source must
  58.  * include the following Disclaimer and Export Compliance provision in the
  59.  * documentation and/or other materials provided with distribution.  In
  60.  * addition, Licensee may not authorize further sublicense of source of any
  61.  * portion of the Covered Code, and must include terms to the effect that the
  62.  * license from Licensee to its licensee is limited to the intellectual
  63.  * property embodied in the software Licensee provides to its licensee, and
  64.  * not to intellectual property embodied in modifications its licensee may
  65.  * make.
  66.  *
  67.  * 3.3. Redistribution of Executable. Redistribution in executable form of any
  68.  * substantial portion of the Covered Code or modification must reproduce the
  69.  * above Copyright Notice, and the following Disclaimer and Export Compliance
  70.  * provision in the documentation and/or other materials provided with the
  71.  * distribution.
  72.  *
  73.  * 3.4. Intel retains all right, title, and interest in and to the Original
  74.  * Intel Code.
  75.  *
  76.  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
  77.  * Intel shall be used in advertising or otherwise to promote the sale, use or
  78.  * other dealings in products derived from or relating to the Covered Code
  79.  * without prior written authorization from Intel.
  80.  *
  81.  * 4. Disclaimer and Export Compliance
  82.  *
  83.  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
  84.  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
  85.  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
  86.  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
  87.  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
  88.  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
  89.  * PARTICULAR PURPOSE.
  90.  *
  91.  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
  92.  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
  93.  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
  94.  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
  95.  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
  96.  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
  97.  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
  98.  * LIMITED REMEDY.
  99.  *
  100.  * 4.3. Licensee shall not export, either directly or indirectly, any of this
  101.  * software or system incorporating such software without first obtaining any
  102.  * required license or other approval from the U. S. Department of Commerce or
  103.  * any other agency or department of the United States Government.  In the
  104.  * event Licensee exports any such software from the United States or
  105.  * re-exports any such software from a foreign destination, Licensee shall
  106.  * ensure that the distribution and export/re-export of the software is in
  107.  * compliance with all laws, regulations, orders, or other restrictions of the
  108.  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
  109.  * any of its subsidiaries will export/re-export any technical data, process,
  110.  * software, or service, directly or indirectly, to any country for which the
  111.  * United States government or any agency thereof requires an export license,
  112.  * other governmental approval, or letter of assurance, without first obtaining
  113.  * such license, approval or letter.
  114.  *
  115.  *****************************************************************************/
  116.  
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119. #include <string.h>
  120. #include <ctype.h>
  121.  
  122.  
  123. /* Note: This is a 32-bit program only */
  124.  
  125. #define VERSION             0x20110330
  126. #define FIND_HEADER         0
  127. #define EXTRACT_DATA        1
  128. #define BUFFER_SIZE         256
  129. #define MIN_HEADER_LENGTH   6   /* strlen ("DSDT @") */
  130.  
  131. /* Local prototypes */
  132.  
  133. static void
  134. CheckAscii (
  135.     char                    *Name,
  136.     int                     Count);
  137.  
  138. static void
  139. NormalizeSignature (
  140.     char                    *Signature);
  141.  
  142. static unsigned int
  143. GetNextInstance (
  144.     char                    *InputPathname,
  145.     char                    *Signature);
  146.  
  147. static int
  148. ExtractTables (
  149.     char                    *InputPathname,
  150.     char                    *Signature,
  151.     unsigned int            MinimumInstances);
  152.  
  153. static size_t
  154. GetTableHeader (
  155.     FILE                    *InputFile,
  156.     unsigned char           *OutputData);
  157.  
  158. static unsigned int
  159. CountTableInstances (
  160.     char                    *InputPathname,
  161.     char                    *Signature);
  162.  
  163. static int
  164. ListTables (
  165.     char                    *InputPathname);
  166.  
  167. static size_t
  168. ConvertLine (
  169.     char                    *InputLine,
  170.     unsigned char           *OutputData);
  171.  
  172. static void
  173. DisplayUsage (
  174.     void);
  175.  
  176.  
  177. typedef struct acpi_table_header
  178. {
  179.     char                    Signature[4];
  180.     int                     Length;
  181.     unsigned char           Revision;
  182.     unsigned char           Checksum;
  183.     char                    OemId[6];
  184.     char                    OemTableId[8];
  185.     int                     OemRevision;
  186.     char                    AslCompilerId[4];
  187.     int                     AslCompilerRevision;
  188.  
  189. } ACPI_TABLE_HEADER;
  190.  
  191. struct TableInfo
  192. {
  193.     unsigned int            Signature;
  194.     unsigned int            Instances;
  195.     unsigned int            NextInstance;
  196.     struct TableInfo        *Next;
  197. };
  198.  
  199. static struct TableInfo     *ListHead = NULL;
  200. static char                 Filename[16];
  201. static unsigned char        Data[16];
  202.  
  203.  
  204. /******************************************************************************
  205.  *
  206.  * FUNCTION:    DisplayUsage
  207.  *
  208.  * DESCRIPTION: Usage message
  209.  *
  210.  ******************************************************************************/
  211.  
  212. static void
  213. DisplayUsage (
  214.     void)
  215. {
  216.  
  217.     printf ("Usage: acpixtract [option] <InputFile>\n");
  218.     printf ("\nExtract binary ACPI tables from text acpidump output\n");
  219.     printf ("Default invocation extracts all DSDTs and SSDTs\n");
  220.     printf ("Version %8.8X\n\n", VERSION);
  221.     printf ("Options:\n");
  222.     printf (" -a                    Extract all tables, not just DSDT/SSDT\n");
  223.     printf (" -l                    List table summaries, do not extract\n");
  224.     printf (" -s<Signature>         Extract all tables named <Signature>\n");
  225.     printf ("\n");
  226. }
  227.  
  228.  
  229. /*******************************************************************************
  230.  *
  231.  * FUNCTION:    CheckAscii
  232.  *
  233.  * PARAMETERS:  Name                - Ascii string, at least as long as Count
  234.  *              Count               - Number of characters to check
  235.  *
  236.  * RETURN:      None
  237.  *
  238.  * DESCRIPTION: Ensure that the requested number of characters are printable
  239.  *              Ascii characters. Sets non-printable and null chars to <space>.
  240.  *
  241.  ******************************************************************************/
  242.  
  243. static void
  244. CheckAscii (
  245.     char                    *Name,
  246.     int                     Count)
  247. {
  248.     int                     i;
  249.  
  250.  
  251.     for (i = 0; i < Count; i++)
  252.     {
  253.         if (!Name[i] || !isprint ((int) Name[i]))
  254.         {
  255.             Name[i] = ' ';
  256.         }
  257.     }
  258. }
  259.  
  260.  
  261. /*******************************************************************************
  262.  *
  263.  * FUNCTION:    NormalizeSignature
  264.  *
  265.  * PARAMETERS:  Name                - Ascii string containing an ACPI signature
  266.  *
  267.  * RETURN:      None
  268.  *
  269.  * DESCRIPTION: Change "RSD PTR" to "RSDP"
  270.  *
  271.  ******************************************************************************/
  272.  
  273. static void
  274. NormalizeSignature (
  275.     char                    *Signature)
  276. {
  277.  
  278.     if (!strncmp (Signature, "RSD ", 4))
  279.     {
  280.         Signature[3] = 'P';
  281.     }
  282. }
  283.  
  284.  
  285. /******************************************************************************
  286.  *
  287.  * FUNCTION:    ConvertLine
  288.  *
  289.  * PARAMETERS:  InputLine           - One line from the input acpidump file
  290.  *              OutputData          - Where the converted data is returned
  291.  *
  292.  * RETURN:      The number of bytes actually converted
  293.  *
  294.  * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
  295.  *
  296.  ******************************************************************************/
  297.  
  298. static size_t
  299. ConvertLine (
  300.     char                    *InputLine,
  301.     unsigned char           *OutputData)
  302. {
  303.     char                    *End;
  304.     int                     BytesConverted;
  305.     int                     Converted[16];
  306.     int                     i;
  307.  
  308.  
  309.     /* Terminate the input line at the end of the actual data (for sscanf) */
  310.  
  311.     End = strstr (InputLine + 2, "  ");
  312.     if (!End)
  313.     {
  314.         return (0); /* Don't understand the format */
  315.     }
  316.     *End = 0;
  317.  
  318.     /*
  319.      * Convert one line of table data, of the form:
  320.      * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
  321.      *
  322.      * Example:
  323.      * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08  _SB_LNKD........
  324.      */
  325.     BytesConverted = sscanf (InputLine,
  326.         "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
  327.         &Converted[0],  &Converted[1],  &Converted[2],  &Converted[3],
  328.         &Converted[4],  &Converted[5],  &Converted[6],  &Converted[7],
  329.         &Converted[8],  &Converted[9],  &Converted[10], &Converted[11],
  330.         &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
  331.  
  332.     /* Pack converted data into a byte array */
  333.  
  334.     for (i = 0; i < BytesConverted; i++)
  335.     {
  336.         OutputData[i] = (unsigned char) Converted[i];
  337.     }
  338.  
  339.     return ((size_t) BytesConverted);
  340. }
  341.  
  342.  
  343. /******************************************************************************
  344.  *
  345.  * FUNCTION:    GetTableHeader
  346.  *
  347.  * PARAMETERS:  InputFile           - Handle for the input acpidump file
  348.  *              OutputData          - Where the table header is returned
  349.  *
  350.  * RETURN:      The actual number of bytes converted
  351.  *
  352.  * DESCRIPTION: Extract and convert an ACPI table header
  353.  *
  354.  ******************************************************************************/
  355.  
  356. static size_t
  357. GetTableHeader (
  358.     FILE                    *InputFile,
  359.     unsigned char           *OutputData)
  360. {
  361.     size_t                  BytesConverted;
  362.     size_t                  TotalConverted = 0;
  363.     char                    Buffer[BUFFER_SIZE];
  364.     int                     i;
  365.  
  366.  
  367.     /* Get the full 36 byte header, requires 3 lines */
  368.  
  369.     for (i = 0; i < 3; i++)
  370.     {
  371.         if (!fgets (Buffer, BUFFER_SIZE, InputFile))
  372.         {
  373.             return (TotalConverted);
  374.         }
  375.  
  376.         BytesConverted = ConvertLine (Buffer, OutputData);
  377.         TotalConverted += BytesConverted;
  378.         OutputData += 16;
  379.  
  380.         if (BytesConverted != 16)
  381.         {
  382.             return (TotalConverted);
  383.         }
  384.     }
  385.  
  386.     return (TotalConverted);
  387. }
  388.  
  389.  
  390. /******************************************************************************
  391.  *
  392.  * FUNCTION:    CountTableInstances
  393.  *
  394.  * PARAMETERS:  InputPathname       - Filename for acpidump file
  395.  *              Signature           - Requested signature to count
  396.  *
  397.  * RETURN:      The number of instances of the signature
  398.  *
  399.  * DESCRIPTION: Count the instances of tables with the given signature within
  400.  *              the input acpidump file.
  401.  *
  402.  ******************************************************************************/
  403.  
  404. static unsigned int
  405. CountTableInstances (
  406.     char                    *InputPathname,
  407.     char                    *Signature)
  408. {
  409.     char                    Buffer[BUFFER_SIZE];
  410.     FILE                    *InputFile;
  411.     unsigned int            Instances = 0;
  412.  
  413.  
  414.     InputFile = fopen (InputPathname, "rt");
  415.     if (!InputFile)
  416.     {
  417.         printf ("Could not open %s\n", InputPathname);
  418.         return (0);
  419.     }
  420.  
  421.     /* Count the number of instances of this signature */
  422.  
  423.     while (fgets (Buffer, BUFFER_SIZE, InputFile))
  424.     {
  425.         /* Ignore empty lines and lines that start with a space */
  426.  
  427.         if ((Buffer[0] == ' ') ||
  428.             (Buffer[0] == '\n'))
  429.         {
  430.             continue;
  431.         }
  432.  
  433.         NormalizeSignature (Buffer);
  434.         if (!strncmp (Buffer, Signature, 4))
  435.         {
  436.             Instances++;
  437.         }
  438.     }
  439.  
  440.     fclose (InputFile);
  441.     return (Instances);
  442. }
  443.  
  444.  
  445. /******************************************************************************
  446.  *
  447.  * FUNCTION:    GetNextInstance
  448.  *
  449.  * PARAMETERS:  InputPathname       - Filename for acpidump file
  450.  *              Signature           - Requested ACPI signature
  451.  *
  452.  * RETURN:      The next instance number for this signature. Zero if this
  453.  *              is the first instance of this signature.
  454.  *
  455.  * DESCRIPTION: Get the next instance number of the specified table. If this
  456.  *              is the first instance of the table, create a new instance
  457.  *              block. Note: only SSDT and PSDT tables can have multiple
  458.  *              instances.
  459.  *
  460.  ******************************************************************************/
  461.  
  462. static unsigned int
  463. GetNextInstance (
  464.     char                    *InputPathname,
  465.     char                    *Signature)
  466. {
  467.     struct TableInfo        *Info;
  468.  
  469.  
  470.     Info = ListHead;
  471.     while (Info)
  472.     {
  473.         if (*(unsigned int *) Signature == Info->Signature)
  474.         {
  475.             break;
  476.         }
  477.  
  478.         Info = Info->Next;
  479.     }
  480.  
  481.     if (!Info)
  482.     {
  483.         /* Signature not found, create new table info block */
  484.  
  485.         Info = malloc (sizeof (struct TableInfo));
  486.         if (!Info)
  487.         {
  488.             printf ("Could not allocate memory\n");
  489.             exit (0);
  490.         }
  491.  
  492.         Info->Signature = *(unsigned int *) Signature;
  493.         Info->Instances = CountTableInstances (InputPathname, Signature);
  494.         Info->NextInstance = 1;
  495.         Info->Next = ListHead;
  496.         ListHead = Info;
  497.     }
  498.  
  499.     if (Info->Instances > 1)
  500.     {
  501.         return (Info->NextInstance++);
  502.     }
  503.  
  504.     return (0);
  505. }
  506.  
  507.  
  508. /******************************************************************************
  509.  *
  510.  * FUNCTION:    ExtractTables
  511.  *
  512.  * PARAMETERS:  InputPathname       - Filename for acpidump file
  513.  *              Signature           - Requested ACPI signature to extract.
  514.  *                                    NULL means extract ALL tables.
  515.  *              MinimumInstances    - Min instances that are acceptable
  516.  *
  517.  * RETURN:      Status
  518.  *
  519.  * DESCRIPTION: Convert text ACPI tables to binary
  520.  *
  521.  ******************************************************************************/
  522.  
  523. static int
  524. ExtractTables (
  525.     char                    *InputPathname,
  526.     char                    *Signature,
  527.     unsigned int            MinimumInstances)
  528. {
  529.     char                    Buffer[BUFFER_SIZE];
  530.     FILE                    *InputFile;
  531.     FILE                    *OutputFile = NULL;
  532.     size_t                  BytesWritten;
  533.     size_t                  TotalBytesWritten = 0;
  534.     size_t                  BytesConverted;
  535.     unsigned int            State = FIND_HEADER;
  536.     unsigned int            FoundTable = 0;
  537.     unsigned int            Instances = 0;
  538.     unsigned int            ThisInstance;
  539.     char                    ThisSignature[4];
  540.     int                     Status = 0;
  541.  
  542.  
  543.     /* Open input in text mode, output is in binary mode */
  544.  
  545.     InputFile = fopen (InputPathname, "rt");
  546.     if (!InputFile)
  547.     {
  548.         printf ("Could not open %s\n", InputPathname);
  549.         return (-1);
  550.     }
  551.  
  552.     if (Signature)
  553.     {
  554.         /* Are there enough instances of the table to continue? */
  555.  
  556.         NormalizeSignature (Signature);
  557.  
  558.         Instances = CountTableInstances (InputPathname, Signature);
  559.         if (Instances < MinimumInstances)
  560.         {
  561.             printf ("Table %s was not found in %s\n", Signature, InputPathname);
  562.             Status = -1;
  563.             goto CleanupAndExit;
  564.         }
  565.  
  566.         if (Instances == 0)
  567.         {
  568.             goto CleanupAndExit;
  569.         }
  570.     }
  571.  
  572.     /* Convert all instances of the table to binary */
  573.  
  574.     while (fgets (Buffer, BUFFER_SIZE, InputFile))
  575.     {
  576.         switch (State)
  577.         {
  578.         case FIND_HEADER:
  579.  
  580.             /* Ignore lines that are too short to be header lines */
  581.  
  582.             if (strlen (Buffer) < MIN_HEADER_LENGTH)
  583.             {
  584.                 continue;
  585.             }
  586.  
  587.             /* Ignore empty lines and lines that start with a space */
  588.  
  589.             if ((Buffer[0] == ' ') ||
  590.                 (Buffer[0] == '\n'))
  591.             {
  592.                 continue;
  593.             }
  594.  
  595.             /*
  596.              * Ignore lines that are not of the form <sig> @ <addr>. Examples:
  597.              *
  598.              * DSDT @ 0x737e4000
  599.              * XSDT @ 0x737f2fff
  600.              * RSD PTR @ 0xf6cd0
  601.              * SSDT @ (nil)
  602.              */
  603.             if (!strstr (Buffer, " @ "))
  604.             {
  605.                 continue;
  606.             }
  607.  
  608.             NormalizeSignature (Buffer);
  609.             strncpy (ThisSignature, Buffer, 4);
  610.  
  611.             if (Signature)
  612.             {
  613.                 /* Ignore signatures that don't match */
  614.  
  615.                 if (strncmp (ThisSignature, Signature, 4))
  616.                 {
  617.                     continue;
  618.                 }
  619.             }
  620.  
  621.             /*
  622.              * Get the instance number for this signature. Only the
  623.              * SSDT and PSDT tables can have multiple instances.
  624.              */
  625.             ThisInstance = GetNextInstance (InputPathname, ThisSignature);
  626.  
  627.             /* Build an output filename and create/open the output file */
  628.  
  629.             if (ThisInstance > 0)
  630.             {
  631.                 sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
  632.             }
  633.             else
  634.             {
  635.                 sprintf (Filename, "%4.4s.dat", ThisSignature);
  636.             }
  637.  
  638.             OutputFile = fopen (Filename, "w+b");
  639.             if (!OutputFile)
  640.             {
  641.                 printf ("Could not open %s\n", Filename);
  642.                 Status = -1;
  643.                 goto CleanupAndExit;
  644.             }
  645.  
  646.             State = EXTRACT_DATA;
  647.             TotalBytesWritten = 0;
  648.             FoundTable = 1;
  649.             continue;
  650.  
  651.         case EXTRACT_DATA:
  652.  
  653.             /* Empty line or non-data line terminates the data */
  654.  
  655.             if ((Buffer[0] == '\n') ||
  656.                 (Buffer[0] != ' '))
  657.             {
  658.                 fclose (OutputFile);
  659.                 OutputFile = NULL;
  660.                 State = FIND_HEADER;
  661.  
  662.                 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
  663.                     ThisSignature, (unsigned int) TotalBytesWritten, Filename);
  664.                 continue;
  665.             }
  666.  
  667.             /* Convert the ascii data (one line of text) to binary */
  668.  
  669.             BytesConverted = ConvertLine (Buffer, Data);
  670.  
  671.             /* Write the binary data */
  672.  
  673.             BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
  674.             if (BytesWritten != BytesConverted)
  675.             {
  676.                 printf ("Write error on %s\n", Filename);
  677.                 fclose (OutputFile);
  678.                 OutputFile = NULL;
  679.                 Status = -1;
  680.                 goto CleanupAndExit;
  681.             }
  682.  
  683.             TotalBytesWritten += BytesConverted;
  684.             continue;
  685.  
  686.         default:
  687.             Status = -1;
  688.             goto CleanupAndExit;
  689.         }
  690.     }
  691.  
  692.     if (!FoundTable)
  693.     {
  694.         printf ("Table %s was not found in %s\n", Signature, InputPathname);
  695.     }
  696.  
  697.  
  698. CleanupAndExit:
  699.  
  700.     if (OutputFile)
  701.     {
  702.         fclose (OutputFile);
  703.         if (State == EXTRACT_DATA)
  704.         {
  705.             /* Received an EOF while extracting data */
  706.  
  707.             printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
  708.                 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
  709.         }
  710.     }
  711.  
  712.     fclose (InputFile);
  713.     return (Status);
  714. }
  715.  
  716.  
  717. /******************************************************************************
  718.  *
  719.  * FUNCTION:    ListTables
  720.  *
  721.  * PARAMETERS:  InputPathname       - Filename for acpidump file
  722.  *
  723.  * RETURN:      Status
  724.  *
  725.  * DESCRIPTION: Display info for all ACPI tables found in input. Does not
  726.  *              perform an actual extraction of the tables.
  727.  *
  728.  ******************************************************************************/
  729.  
  730. static int
  731. ListTables (
  732.     char                    *InputPathname)
  733. {
  734.     FILE                    *InputFile;
  735.     char                    Buffer[BUFFER_SIZE];
  736.     size_t                  HeaderSize;
  737.     unsigned char           Header[48];
  738.     int                     TableCount = 0;
  739.     ACPI_TABLE_HEADER       *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
  740.  
  741.  
  742.     /* Open input in text mode, output is in binary mode */
  743.  
  744.     InputFile = fopen (InputPathname, "rt");
  745.     if (!InputFile)
  746.     {
  747.         printf ("Could not open %s\n", InputPathname);
  748.         return (-1);
  749.     }
  750.  
  751.     /* Dump the headers for all tables found in the input file */
  752.  
  753.     printf ("\nSignature Length Revision  OemId     OemTableId"
  754.             "   OemRevision CompilerId CompilerRevision\n\n");
  755.  
  756.     while (fgets (Buffer, BUFFER_SIZE, InputFile))
  757.     {
  758.         /* Ignore empty lines and lines that start with a space */
  759.  
  760.         if ((Buffer[0] == ' ') ||
  761.             (Buffer[0] == '\n'))
  762.         {
  763.             continue;
  764.         }
  765.  
  766.         /* Get the 36 byte header and display the fields */
  767.  
  768.         HeaderSize = GetTableHeader (InputFile, Header);
  769.         if (HeaderSize < 16)
  770.         {
  771.             continue;
  772.         }
  773.  
  774.         /* RSDP has an oddball signature and header */
  775.  
  776.         if (!strncmp (TableHeader->Signature, "RSD PTR ", 8))
  777.         {
  778.             CheckAscii ((char *) &Header[9], 6);
  779.             printf ("%8.4s                   \"%6.6s\"\n", "RSDP", &Header[9]);
  780.             TableCount++;
  781.             continue;
  782.         }
  783.  
  784.         /* Minimum size for table with standard header */
  785.  
  786.         if (HeaderSize < 36)
  787.         {
  788.             continue;
  789.         }
  790.  
  791.         /* Signature and Table length */
  792.  
  793.         TableCount++;
  794.         printf ("%8.4s % 7d", TableHeader->Signature, TableHeader->Length);
  795.  
  796.         /* FACS has only signature and length */
  797.  
  798.         if (!strncmp (TableHeader->Signature, "FACS", 4))
  799.         {
  800.             printf ("\n");
  801.             continue;
  802.         }
  803.  
  804.         /* OEM IDs and Compiler IDs */
  805.  
  806.         CheckAscii (TableHeader->OemId, 6);
  807.         CheckAscii (TableHeader->OemTableId, 8);
  808.         CheckAscii (TableHeader->AslCompilerId, 4);
  809.  
  810.         printf ("     %2.2X    \"%6.6s\"  \"%8.8s\"    %8.8X    \"%4.4s\"     %8.8X\n",
  811.             TableHeader->Revision, TableHeader->OemId,
  812.             TableHeader->OemTableId, TableHeader->OemRevision,
  813.             TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
  814.     }
  815.  
  816.     printf ("\nFound %u ACPI tables [%8.8X]\n", TableCount, VERSION);
  817.     fclose (InputFile);
  818.     return (0);
  819. }
  820.  
  821.  
  822. /******************************************************************************
  823.  *
  824.  * FUNCTION:    main
  825.  *
  826.  * DESCRIPTION: C main function
  827.  *
  828.  ******************************************************************************/
  829.  
  830. int
  831. main (
  832.     int                     argc,
  833.     char                    *argv[])
  834. {
  835.     int                     Status;
  836.  
  837.  
  838.     if (argc < 2)
  839.     {
  840.         DisplayUsage ();
  841.         return (0);
  842.     }
  843.  
  844.     if (argv[1][0] == '-')
  845.     {
  846.         if (argc < 3)
  847.         {
  848.             DisplayUsage ();
  849.             return (0);
  850.         }
  851.  
  852.         switch (argv[1][1])
  853.         {
  854.         case 'a':
  855.  
  856.             /* Extract all tables found */
  857.  
  858.             return (ExtractTables (argv[2], NULL, 0));
  859.  
  860.         case 'l':
  861.  
  862.             /* List tables only, do not extract */
  863.  
  864.             return (ListTables (argv[2]));
  865.  
  866.         case 's':
  867.  
  868.             /* Extract only tables with this signature */
  869.  
  870.             return (ExtractTables (argv[2], &argv[1][2], 1));
  871.  
  872.         default:
  873.             DisplayUsage ();
  874.             return (0);
  875.         }
  876.     }
  877.  
  878.     /*
  879.      * Default output is the DSDT and all SSDTs. One DSDT is required,
  880.      * any SSDTs are optional.
  881.      */
  882.     Status = ExtractTables (argv[1], "DSDT", 1);
  883.     if (Status)
  884.     {
  885.         return (Status);
  886.     }
  887.  
  888.     Status = ExtractTables (argv[1], "SSDT", 0);
  889.     return (Status);
  890. }
  891.  
  892.  
  893.