1 /******************************************************************************
   2  *
   3  * Module Name: nsparse - namespace interface to AML parser
   4  *
   5  *****************************************************************************/
   6 
   7 /*
   8  * Copyright (C) 2000 - 2013, 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 __NSPARSE_C__
  45 
  46 #include "acpi.h"
  47 #include "accommon.h"
  48 #include "acnamesp.h"
  49 #include "acparser.h"
  50 #include "acdispat.h"
  51 #include "actables.h"
  52 
  53 
  54 #define _COMPONENT          ACPI_NAMESPACE
  55         ACPI_MODULE_NAME    ("nsparse")
  56 
  57 
  58 /*******************************************************************************
  59  *
  60  * FUNCTION:    NsOneCompleteParse
  61  *
  62  * PARAMETERS:  PassNumber              - 1 or 2
  63  *              TableDesc               - The table to be parsed.
  64  *
  65  * RETURN:      Status
  66  *
  67  * DESCRIPTION: Perform one complete parse of an ACPI/AML table.
  68  *
  69  ******************************************************************************/
  70 
  71 ACPI_STATUS
  72 AcpiNsOneCompleteParse (
  73     UINT32                  PassNumber,
  74     UINT32                  TableIndex,
  75     ACPI_NAMESPACE_NODE     *StartNode)
  76 {
  77     ACPI_PARSE_OBJECT       *ParseRoot;
  78     ACPI_STATUS             Status;
  79     UINT32                  AmlLength;
  80     UINT8                   *AmlStart;
  81     ACPI_WALK_STATE         *WalkState;
  82     ACPI_TABLE_HEADER       *Table;
  83     ACPI_OWNER_ID           OwnerId;
  84 
  85 
  86     ACPI_FUNCTION_TRACE (NsOneCompleteParse);
  87 
  88 
  89     Status = AcpiTbGetOwnerId (TableIndex, &OwnerId);
  90     if (ACPI_FAILURE (Status))
  91     {
  92         return_ACPI_STATUS (Status);
  93     }
  94 
  95     /* Create and init a Root Node */
  96 
  97     ParseRoot = AcpiPsCreateScopeOp ();
  98     if (!ParseRoot)
  99     {
 100         return_ACPI_STATUS (AE_NO_MEMORY);
 101     }
 102 
 103     /* Create and initialize a new walk state */
 104 
 105     WalkState = AcpiDsCreateWalkState (OwnerId, NULL, NULL, NULL);
 106     if (!WalkState)
 107     {
 108         AcpiPsFreeOp (ParseRoot);
 109         return_ACPI_STATUS (AE_NO_MEMORY);
 110     }
 111 
 112     Status = AcpiGetTableByIndex (TableIndex, &Table);
 113     if (ACPI_FAILURE (Status))
 114     {
 115         AcpiDsDeleteWalkState (WalkState);
 116         AcpiPsFreeOp (ParseRoot);
 117         return_ACPI_STATUS (Status);
 118     }
 119 
 120     /* Table must consist of at least a complete header */
 121 
 122     if (Table->Length < sizeof (ACPI_TABLE_HEADER))
 123     {
 124         Status = AE_BAD_HEADER;
 125     }
 126     else
 127     {
 128         AmlStart = (UINT8 *) Table + sizeof (ACPI_TABLE_HEADER);
 129         AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
 130         Status = AcpiDsInitAmlWalk (WalkState, ParseRoot, NULL,
 131                     AmlStart, AmlLength, NULL, (UINT8) PassNumber);
 132     }
 133 
 134     if (ACPI_FAILURE (Status))
 135     {
 136         AcpiDsDeleteWalkState (WalkState);
 137         goto Cleanup;
 138     }
 139 
 140     /* StartNode is the default location to load the table  */
 141 
 142     if (StartNode && StartNode != AcpiGbl_RootNode)
 143     {
 144         Status = AcpiDsScopeStackPush (StartNode, ACPI_TYPE_METHOD, WalkState);
 145         if (ACPI_FAILURE (Status))
 146         {
 147             AcpiDsDeleteWalkState (WalkState);
 148             goto Cleanup;
 149         }
 150     }
 151 
 152     /* Parse the AML */
 153 
 154     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "*PARSE* pass %u parse\n", PassNumber));
 155     Status = AcpiPsParseAml (WalkState);
 156 
 157 Cleanup:
 158     AcpiPsDeleteParseTree (ParseRoot);
 159     return_ACPI_STATUS (Status);
 160 }
 161 
 162 
 163 /*******************************************************************************
 164  *
 165  * FUNCTION:    AcpiNsParseTable
 166  *
 167  * PARAMETERS:  TableDesc       - An ACPI table descriptor for table to parse
 168  *              StartNode       - Where to enter the table into the namespace
 169  *
 170  * RETURN:      Status
 171  *
 172  * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
 173  *
 174  ******************************************************************************/
 175 
 176 ACPI_STATUS
 177 AcpiNsParseTable (
 178     UINT32                  TableIndex,
 179     ACPI_NAMESPACE_NODE     *StartNode)
 180 {
 181     ACPI_STATUS             Status;
 182 
 183 
 184     ACPI_FUNCTION_TRACE (NsParseTable);
 185 
 186 
 187     /*
 188      * AML Parse, pass 1
 189      *
 190      * In this pass, we load most of the namespace. Control methods
 191      * are not parsed until later. A parse tree is not created. Instead,
 192      * each Parser Op subtree is deleted when it is finished. This saves
 193      * a great deal of memory, and allows a small cache of parse objects
 194      * to service the entire parse. The second pass of the parse then
 195      * performs another complete parse of the AML.
 196      */
 197     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 1\n"));
 198     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS1,
 199                 TableIndex, StartNode);
 200     if (ACPI_FAILURE (Status))
 201     {
 202         return_ACPI_STATUS (Status);
 203     }
 204 
 205     /*
 206      * AML Parse, pass 2
 207      *
 208      * In this pass, we resolve forward references and other things
 209      * that could not be completed during the first pass.
 210      * Another complete parse of the AML is performed, but the
 211      * overhead of this is compensated for by the fact that the
 212      * parse objects are all cached.
 213      */
 214     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Start pass 2\n"));
 215     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2,
 216                 TableIndex, StartNode);
 217     if (ACPI_FAILURE (Status))
 218     {
 219         return_ACPI_STATUS (Status);
 220     }
 221 
 222     return_ACPI_STATUS (Status);
 223 }