Home | History | Annotate | Line # | Download | only in acpidump
apmain.c revision 1.1.1.9
      1 /******************************************************************************
      2  *
      3  * Module Name: apmain - Main module for the acpidump utility
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2017, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions, and the following disclaimer,
     16  *    without modification.
     17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  *    including a substantially similar Disclaimer requirement for further
     21  *    binary redistribution.
     22  * 3. Neither the names of the above-listed copyright holders nor the names
     23  *    of any contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * Alternatively, this software may be distributed under the terms of the
     27  * GNU General Public License ("GPL") version 2 as published by the Free
     28  * Software Foundation.
     29  *
     30  * NO WARRANTY
     31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGES.
     42  */
     43 
     44 #define _DECLARE_GLOBALS
     45 #include "acpidump.h"
     46 
     47 
     48 /*
     49  * acpidump - A portable utility for obtaining system ACPI tables and dumping
     50  * them in an ASCII hex format suitable for binary extraction via acpixtract.
     51  *
     52  * Obtaining the system ACPI tables is an OS-specific operation.
     53  *
     54  * This utility can be ported to any host operating system by providing a
     55  * module containing system-specific versions of these interfaces:
     56  *
     57  *      AcpiOsGetTableByAddress
     58  *      AcpiOsGetTableByIndex
     59  *      AcpiOsGetTableByName
     60  *
     61  * See the ACPICA Reference Guide for the exact definitions of these
     62  * interfaces. Also, see these ACPICA source code modules for example
     63  * implementations:
     64  *
     65  *      source/os_specific/service_layers/oswintbl.c
     66  *      source/os_specific/service_layers/oslinuxtbl.c
     67  */
     68 
     69 
     70 /* Local prototypes */
     71 
     72 static void
     73 ApDisplayUsage (
     74     void);
     75 
     76 static int
     77 ApDoOptions (
     78     int                     argc,
     79     char                    **argv);
     80 
     81 static int
     82 ApInsertAction (
     83     char                    *Argument,
     84     UINT32                  ToBeDone);
     85 
     86 
     87 /* Table for deferred actions from command line options */
     88 
     89 AP_DUMP_ACTION              ActionTable [AP_MAX_ACTIONS];
     90 UINT32                      CurrentAction = 0;
     91 
     92 
     93 #define AP_UTILITY_NAME             "ACPI Binary Table Dump Utility"
     94 #define AP_SUPPORTED_OPTIONS        "?a:bc:f:hn:o:r:svxz"
     95 
     96 
     97 /******************************************************************************
     98  *
     99  * FUNCTION:    ApDisplayUsage
    100  *
    101  * DESCRIPTION: Usage message for the AcpiDump utility
    102  *
    103  ******************************************************************************/
    104 
    105 static void
    106 ApDisplayUsage (
    107     void)
    108 {
    109 
    110     ACPI_USAGE_HEADER ("acpidump [options]");
    111 
    112     ACPI_OPTION ("-b",                      "Dump tables to binary files");
    113     ACPI_OPTION ("-h -?",                   "This help message");
    114     ACPI_OPTION ("-o <File>",               "Redirect output to file");
    115     ACPI_OPTION ("-r <Address>",            "Dump tables from specified RSDP");
    116     ACPI_OPTION ("-s",                      "Print table summaries only");
    117     ACPI_OPTION ("-v",                      "Display version information");
    118     ACPI_OPTION ("-z",                      "Verbose mode");
    119 
    120     ACPI_USAGE_TEXT ("\nTable Options:\n");
    121 
    122     ACPI_OPTION ("-a <Address>",            "Get table via a physical address");
    123     ACPI_OPTION ("-c <on|off>",             "Turning on/off customized table dumping");
    124     ACPI_OPTION ("-f <BinaryFile>",         "Get table via a binary file");
    125     ACPI_OPTION ("-n <Signature>",          "Get table via a name/signature");
    126     ACPI_OPTION ("-x",                      "Use RSDT instead of XSDT");
    127 
    128     ACPI_USAGE_TEXT (
    129         "\n"
    130         "Invocation without parameters dumps all available tables\n"
    131         "Multiple mixed instances of -a, -f, and -n are supported\n\n");
    132 }
    133 
    134 
    135 /******************************************************************************
    136  *
    137  * FUNCTION:    ApInsertAction
    138  *
    139  * PARAMETERS:  Argument            - Pointer to the argument for this action
    140  *              ToBeDone            - What to do to process this action
    141  *
    142  * RETURN:      Status
    143  *
    144  * DESCRIPTION: Add an action item to the action table
    145  *
    146  ******************************************************************************/
    147 
    148 static int
    149 ApInsertAction (
    150     char                    *Argument,
    151     UINT32                  ToBeDone)
    152 {
    153 
    154     /* Insert action and check for table overflow */
    155 
    156     ActionTable [CurrentAction].Argument = Argument;
    157     ActionTable [CurrentAction].ToBeDone = ToBeDone;
    158 
    159     CurrentAction++;
    160     if (CurrentAction > AP_MAX_ACTIONS)
    161     {
    162         fprintf (stderr, "Too many table options (max %u)\n", AP_MAX_ACTIONS);
    163         return (-1);
    164     }
    165 
    166     return (0);
    167 }
    168 
    169 
    170 /******************************************************************************
    171  *
    172  * FUNCTION:    ApDoOptions
    173  *
    174  * PARAMETERS:  argc/argv           - Standard argc/argv
    175  *
    176  * RETURN:      Status
    177  *
    178  * DESCRIPTION: Command line option processing. The main actions for getting
    179  *              and dumping tables are deferred via the action table.
    180  *
    181  *****************************************************************************/
    182 
    183 static int
    184 ApDoOptions (
    185     int                     argc,
    186     char                    **argv)
    187 {
    188     int                     j;
    189     ACPI_STATUS             Status;
    190 
    191 
    192     /* Command line options */
    193 
    194     while ((j = AcpiGetopt (argc, argv, AP_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch (j)
    195     {
    196     /*
    197      * Global options
    198      */
    199     case 'b':   /* Dump all input tables to binary files */
    200 
    201         Gbl_BinaryMode = TRUE;
    202         continue;
    203 
    204     case 'c':   /* Dump customized tables */
    205 
    206         if (!strcmp (AcpiGbl_Optarg, "on"))
    207         {
    208             Gbl_DumpCustomizedTables = TRUE;
    209         }
    210         else if (!strcmp (AcpiGbl_Optarg, "off"))
    211         {
    212             Gbl_DumpCustomizedTables = FALSE;
    213         }
    214         else
    215         {
    216             fprintf (stderr, "%s: Cannot handle this switch, please use on|off\n",
    217                 AcpiGbl_Optarg);
    218             return (-1);
    219         }
    220         continue;
    221 
    222     case 'h':
    223     case '?':
    224 
    225         ApDisplayUsage ();
    226         return (1);
    227 
    228     case 'o':   /* Redirect output to a single file */
    229 
    230         if (ApOpenOutputFile (AcpiGbl_Optarg))
    231         {
    232             return (-1);
    233         }
    234         continue;
    235 
    236     case 'r':   /* Dump tables from specified RSDP */
    237 
    238         Status = AcpiUtStrtoul64 (AcpiGbl_Optarg, &Gbl_RsdpBase);
    239         if (ACPI_FAILURE (Status))
    240         {
    241             fprintf (stderr, "%s: Could not convert to a physical address\n",
    242                 AcpiGbl_Optarg);
    243             return (-1);
    244         }
    245         continue;
    246 
    247     case 's':   /* Print table summaries only */
    248 
    249         Gbl_SummaryMode = TRUE;
    250         continue;
    251 
    252     case 'x':   /* Do not use XSDT */
    253 
    254         if (!AcpiGbl_DoNotUseXsdt)
    255         {
    256             AcpiGbl_DoNotUseXsdt = TRUE;
    257         }
    258         else
    259         {
    260             Gbl_DoNotDumpXsdt = TRUE;
    261         }
    262         continue;
    263 
    264     case 'v':   /* Revision/version */
    265 
    266         AcpiOsPrintf (ACPI_COMMON_SIGNON (AP_UTILITY_NAME));
    267         return (1);
    268 
    269     case 'z':   /* Verbose mode */
    270 
    271         Gbl_VerboseMode = TRUE;
    272         fprintf (stderr, ACPI_COMMON_SIGNON (AP_UTILITY_NAME));
    273         continue;
    274 
    275     /*
    276      * Table options
    277      */
    278     case 'a':   /* Get table by physical address */
    279 
    280         if (ApInsertAction (AcpiGbl_Optarg, AP_DUMP_TABLE_BY_ADDRESS))
    281         {
    282             return (-1);
    283         }
    284         break;
    285 
    286     case 'f':   /* Get table from a file */
    287 
    288         if (ApInsertAction (AcpiGbl_Optarg, AP_DUMP_TABLE_BY_FILE))
    289         {
    290             return (-1);
    291         }
    292         break;
    293 
    294     case 'n':   /* Get table by input name (signature) */
    295 
    296         if (ApInsertAction (AcpiGbl_Optarg, AP_DUMP_TABLE_BY_NAME))
    297         {
    298             return (-1);
    299         }
    300         break;
    301 
    302     default:
    303 
    304         ApDisplayUsage ();
    305         return (-1);
    306     }
    307 
    308     /* If there are no actions, this means "get/dump all tables" */
    309 
    310     if (CurrentAction == 0)
    311     {
    312         if (ApInsertAction (NULL, AP_DUMP_ALL_TABLES))
    313         {
    314             return (-1);
    315         }
    316     }
    317 
    318     return (0);
    319 }
    320 
    321 
    322 /******************************************************************************
    323  *
    324  * FUNCTION:    main
    325  *
    326  * PARAMETERS:  argc/argv           - Standard argc/argv
    327  *
    328  * RETURN:      Status
    329  *
    330  * DESCRIPTION: C main function for acpidump utility
    331  *
    332  ******************************************************************************/
    333 
    334 #if !defined(_GNU_EFI) && !defined(_EDK2_EFI)
    335 int ACPI_SYSTEM_XFACE
    336 main (
    337     int                     argc,
    338     char                    *argv[])
    339 #else
    340 int ACPI_SYSTEM_XFACE
    341 acpi_main (
    342     int                     argc,
    343     char                    *argv[])
    344 #endif
    345 {
    346     int                     Status = 0;
    347     AP_DUMP_ACTION          *Action;
    348     UINT32                  FileSize;
    349     UINT32                  i;
    350 
    351 
    352     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
    353     AcpiOsInitialize ();
    354     Gbl_OutputFile = ACPI_FILE_OUT;
    355     AcpiGbl_IntegerByteWidth = 8;
    356 
    357     /* Process command line options */
    358 
    359     Status = ApDoOptions (argc, argv);
    360     if (Status > 0)
    361     {
    362         return (0);
    363     }
    364     if (Status < 0)
    365     {
    366         return (Status);
    367     }
    368 
    369     /* Get/dump ACPI table(s) as requested */
    370 
    371     for (i = 0; i < CurrentAction; i++)
    372     {
    373         Action = &ActionTable[i];
    374         switch (Action->ToBeDone)
    375         {
    376         case AP_DUMP_ALL_TABLES:
    377 
    378             Status = ApDumpAllTables ();
    379             break;
    380 
    381         case AP_DUMP_TABLE_BY_ADDRESS:
    382 
    383             Status = ApDumpTableByAddress (Action->Argument);
    384             break;
    385 
    386         case AP_DUMP_TABLE_BY_NAME:
    387 
    388             Status = ApDumpTableByName (Action->Argument);
    389             break;
    390 
    391         case AP_DUMP_TABLE_BY_FILE:
    392 
    393             Status = ApDumpTableFromFile (Action->Argument);
    394             break;
    395 
    396         default:
    397 
    398             fprintf (stderr, "Internal error, invalid action: 0x%X\n",
    399                 Action->ToBeDone);
    400             return (-1);
    401         }
    402 
    403         if (Status)
    404         {
    405             return (Status);
    406         }
    407     }
    408 
    409     if (Gbl_OutputFilename)
    410     {
    411         if (Gbl_VerboseMode)
    412         {
    413             /* Summary for the output file */
    414 
    415             FileSize = CmGetFileSize (Gbl_OutputFile);
    416             fprintf (stderr, "Output file %s contains 0x%X (%u) bytes\n\n",
    417                 Gbl_OutputFilename, FileSize, FileSize);
    418         }
    419 
    420         fclose (Gbl_OutputFile);
    421     }
    422 
    423     return (Status);
    424 }
    425