Home | History | Annotate | Line # | Download | only in utilities
utresrc.c revision 1.1.1.13
      1       1.1    jruoho /*******************************************************************************
      2       1.1    jruoho  *
      3   1.1.1.3  christos  * Module Name: utresrc - Resource management utilities
      4       1.1    jruoho  *
      5       1.1    jruoho  ******************************************************************************/
      6       1.1    jruoho 
      7   1.1.1.2    jruoho /*
      8  1.1.1.13  christos  * Copyright (C) 2000 - 2021, Intel Corp.
      9       1.1    jruoho  * All rights reserved.
     10       1.1    jruoho  *
     11   1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12   1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13   1.1.1.2    jruoho  * are met:
     14   1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15   1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16   1.1.1.2    jruoho  *    without modification.
     17   1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18   1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19   1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20   1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21   1.1.1.2    jruoho  *    binary redistribution.
     22   1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23   1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24   1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25   1.1.1.2    jruoho  *
     26   1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27   1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28   1.1.1.2    jruoho  * Software Foundation.
     29   1.1.1.2    jruoho  *
     30   1.1.1.2    jruoho  * NO WARRANTY
     31   1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32   1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1.1.13  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34   1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35   1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36   1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37   1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38   1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39   1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40   1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41   1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42   1.1.1.2    jruoho  */
     43       1.1    jruoho 
     44       1.1    jruoho #include "acpi.h"
     45       1.1    jruoho #include "accommon.h"
     46   1.1.1.3  christos #include "acresrc.h"
     47       1.1    jruoho 
     48       1.1    jruoho 
     49       1.1    jruoho #define _COMPONENT          ACPI_UTILITIES
     50       1.1    jruoho         ACPI_MODULE_NAME    ("utresrc")
     51       1.1    jruoho 
     52       1.1    jruoho 
     53       1.1    jruoho /*
     54       1.1    jruoho  * Base sizes of the raw AML resource descriptors, indexed by resource type.
     55       1.1    jruoho  * Zero indicates a reserved (and therefore invalid) resource type.
     56       1.1    jruoho  */
     57       1.1    jruoho const UINT8                 AcpiGbl_ResourceAmlSizes[] =
     58       1.1    jruoho {
     59       1.1    jruoho     /* Small descriptors */
     60       1.1    jruoho 
     61       1.1    jruoho     0,
     62       1.1    jruoho     0,
     63       1.1    jruoho     0,
     64       1.1    jruoho     0,
     65       1.1    jruoho     ACPI_AML_SIZE_SMALL (AML_RESOURCE_IRQ),
     66       1.1    jruoho     ACPI_AML_SIZE_SMALL (AML_RESOURCE_DMA),
     67       1.1    jruoho     ACPI_AML_SIZE_SMALL (AML_RESOURCE_START_DEPENDENT),
     68       1.1    jruoho     ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_DEPENDENT),
     69       1.1    jruoho     ACPI_AML_SIZE_SMALL (AML_RESOURCE_IO),
     70       1.1    jruoho     ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_IO),
     71   1.1.1.3  christos     ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_DMA),
     72       1.1    jruoho     0,
     73       1.1    jruoho     0,
     74       1.1    jruoho     0,
     75       1.1    jruoho     ACPI_AML_SIZE_SMALL (AML_RESOURCE_VENDOR_SMALL),
     76       1.1    jruoho     ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_TAG),
     77       1.1    jruoho 
     78       1.1    jruoho     /* Large descriptors */
     79       1.1    jruoho 
     80       1.1    jruoho     0,
     81       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY24),
     82       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_GENERIC_REGISTER),
     83       1.1    jruoho     0,
     84       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_VENDOR_LARGE),
     85       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY32),
     86       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_FIXED_MEMORY32),
     87       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS32),
     88       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS16),
     89       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_IRQ),
     90       1.1    jruoho     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS64),
     91   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_ADDRESS64),
     92   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_GPIO),
     93   1.1.1.9  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_FUNCTION),
     94   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_COMMON_SERIALBUS),
     95   1.1.1.9  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_CONFIG),
     96   1.1.1.9  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP),
     97   1.1.1.9  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP_FUNCTION),
     98   1.1.1.9  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP_CONFIG),
     99   1.1.1.3  christos };
    100   1.1.1.3  christos 
    101   1.1.1.3  christos const UINT8                 AcpiGbl_ResourceAmlSerialBusSizes[] =
    102   1.1.1.3  christos {
    103   1.1.1.3  christos     0,
    104   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_I2C_SERIALBUS),
    105   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_SPI_SERIALBUS),
    106   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_UART_SERIALBUS),
    107  1.1.1.13  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_CSI2_SERIALBUS),
    108       1.1    jruoho };
    109       1.1    jruoho 
    110       1.1    jruoho 
    111       1.1    jruoho /*
    112       1.1    jruoho  * Resource types, used to validate the resource length field.
    113       1.1    jruoho  * The length of fixed-length types must match exactly, variable
    114       1.1    jruoho  * lengths must meet the minimum required length, etc.
    115       1.1    jruoho  * Zero indicates a reserved (and therefore invalid) resource type.
    116       1.1    jruoho  */
    117       1.1    jruoho static const UINT8          AcpiGbl_ResourceTypes[] =
    118       1.1    jruoho {
    119       1.1    jruoho     /* Small descriptors */
    120       1.1    jruoho 
    121       1.1    jruoho     0,
    122       1.1    jruoho     0,
    123       1.1    jruoho     0,
    124       1.1    jruoho     0,
    125   1.1.1.3  christos     ACPI_SMALL_VARIABLE_LENGTH,     /* 04 IRQ */
    126   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 05 DMA */
    127   1.1.1.3  christos     ACPI_SMALL_VARIABLE_LENGTH,     /* 06 StartDependentFunctions */
    128   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 07 EndDependentFunctions */
    129   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 08 IO */
    130   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 09 FixedIO */
    131   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 0A FixedDMA */
    132       1.1    jruoho     0,
    133       1.1    jruoho     0,
    134       1.1    jruoho     0,
    135   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 0E VendorShort */
    136   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 0F EndTag */
    137       1.1    jruoho 
    138       1.1    jruoho     /* Large descriptors */
    139       1.1    jruoho 
    140       1.1    jruoho     0,
    141   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 01 Memory24 */
    142   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 02 GenericRegister */
    143       1.1    jruoho     0,
    144   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 04 VendorLong */
    145   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 05 Memory32 */
    146   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 06 Memory32Fixed */
    147   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 07 Dword* address */
    148   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 08 Word* address */
    149   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 09 ExtendedIRQ */
    150   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 0A Qword* address */
    151   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 0B Extended* address */
    152   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 0C Gpio* */
    153   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 0D PinFunction */
    154   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 0E *SerialBus */
    155   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 0F PinConfig */
    156   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 10 PinGroup */
    157   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 11 PinGroupFunction */
    158   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 12 PinGroupConfig */
    159       1.1    jruoho };
    160       1.1    jruoho 
    161       1.1    jruoho 
    162       1.1    jruoho /*******************************************************************************
    163       1.1    jruoho  *
    164       1.1    jruoho  * FUNCTION:    AcpiUtWalkAmlResources
    165       1.1    jruoho  *
    166   1.1.1.3  christos  * PARAMETERS:  WalkState           - Current walk info
    167   1.1.1.3  christos  * PARAMETERS:  Aml                 - Pointer to the raw AML resource template
    168   1.1.1.3  christos  *              AmlLength           - Length of the entire template
    169   1.1.1.3  christos  *              UserFunction        - Called once for each descriptor found. If
    170   1.1.1.3  christos  *                                    NULL, a pointer to the EndTag is returned
    171   1.1.1.3  christos  *              Context             - Passed to UserFunction
    172       1.1    jruoho  *
    173       1.1    jruoho  * RETURN:      Status
    174       1.1    jruoho  *
    175       1.1    jruoho  * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
    176       1.1    jruoho  *              once for each resource found.
    177       1.1    jruoho  *
    178       1.1    jruoho  ******************************************************************************/
    179       1.1    jruoho 
    180       1.1    jruoho ACPI_STATUS
    181       1.1    jruoho AcpiUtWalkAmlResources (
    182   1.1.1.3  christos     ACPI_WALK_STATE         *WalkState,
    183       1.1    jruoho     UINT8                   *Aml,
    184       1.1    jruoho     ACPI_SIZE               AmlLength,
    185       1.1    jruoho     ACPI_WALK_AML_CALLBACK  UserFunction,
    186   1.1.1.3  christos     void                    **Context)
    187       1.1    jruoho {
    188       1.1    jruoho     ACPI_STATUS             Status;
    189       1.1    jruoho     UINT8                   *EndAml;
    190       1.1    jruoho     UINT8                   ResourceIndex;
    191       1.1    jruoho     UINT32                  Length;
    192       1.1    jruoho     UINT32                  Offset = 0;
    193   1.1.1.3  christos     UINT8                   EndTag[2] = {0x79, 0x00};
    194       1.1    jruoho 
    195       1.1    jruoho 
    196       1.1    jruoho     ACPI_FUNCTION_TRACE (UtWalkAmlResources);
    197       1.1    jruoho 
    198       1.1    jruoho 
    199   1.1.1.9  christos     /* The absolute minimum resource template is one EndTag descriptor */
    200   1.1.1.9  christos 
    201       1.1    jruoho     if (AmlLength < sizeof (AML_RESOURCE_END_TAG))
    202       1.1    jruoho     {
    203       1.1    jruoho         return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
    204       1.1    jruoho     }
    205       1.1    jruoho 
    206       1.1    jruoho     /* Point to the end of the resource template buffer */
    207       1.1    jruoho 
    208       1.1    jruoho     EndAml = Aml + AmlLength;
    209       1.1    jruoho 
    210       1.1    jruoho     /* Walk the byte list, abort on any invalid descriptor type or length */
    211       1.1    jruoho 
    212       1.1    jruoho     while (Aml < EndAml)
    213       1.1    jruoho     {
    214       1.1    jruoho         /* Validate the Resource Type and Resource Length */
    215       1.1    jruoho 
    216   1.1.1.3  christos         Status = AcpiUtValidateResource (WalkState, Aml, &ResourceIndex);
    217       1.1    jruoho         if (ACPI_FAILURE (Status))
    218       1.1    jruoho         {
    219   1.1.1.3  christos             /*
    220   1.1.1.6  christos              * Exit on failure. Cannot continue because the descriptor
    221   1.1.1.6  christos              * length may be bogus also.
    222   1.1.1.3  christos              */
    223       1.1    jruoho             return_ACPI_STATUS (Status);
    224       1.1    jruoho         }
    225       1.1    jruoho 
    226       1.1    jruoho         /* Get the length of this descriptor */
    227       1.1    jruoho 
    228       1.1    jruoho         Length = AcpiUtGetDescriptorLength (Aml);
    229       1.1    jruoho 
    230       1.1    jruoho         /* Invoke the user function */
    231       1.1    jruoho 
    232       1.1    jruoho         if (UserFunction)
    233       1.1    jruoho         {
    234   1.1.1.9  christos             Status = UserFunction (
    235   1.1.1.9  christos                 Aml, Length, Offset, ResourceIndex, Context);
    236       1.1    jruoho             if (ACPI_FAILURE (Status))
    237       1.1    jruoho             {
    238   1.1.1.3  christos                 return_ACPI_STATUS (Status);
    239       1.1    jruoho             }
    240       1.1    jruoho         }
    241       1.1    jruoho 
    242       1.1    jruoho         /* An EndTag descriptor terminates this resource template */
    243       1.1    jruoho 
    244       1.1    jruoho         if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_END_TAG)
    245       1.1    jruoho         {
    246       1.1    jruoho             /*
    247       1.1    jruoho              * There must be at least one more byte in the buffer for
    248       1.1    jruoho              * the 2nd byte of the EndTag
    249       1.1    jruoho              */
    250       1.1    jruoho             if ((Aml + 1) >= EndAml)
    251       1.1    jruoho             {
    252       1.1    jruoho                 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
    253       1.1    jruoho             }
    254       1.1    jruoho 
    255   1.1.1.8  christos             /*
    256   1.1.1.9  christos              * Don't attempt to perform any validation on the 2nd byte.
    257   1.1.1.9  christos              * Although all known ASL compilers insert a zero for the 2nd
    258   1.1.1.9  christos              * byte, it can also be a checksum (as per the ACPI spec),
    259   1.1.1.9  christos              * and this is occasionally seen in the field. July 2017.
    260   1.1.1.8  christos              */
    261   1.1.1.8  christos 
    262       1.1    jruoho             /* Return the pointer to the EndTag if requested */
    263       1.1    jruoho 
    264       1.1    jruoho             if (!UserFunction)
    265       1.1    jruoho             {
    266   1.1.1.3  christos                 *Context = Aml;
    267       1.1    jruoho             }
    268       1.1    jruoho 
    269       1.1    jruoho             /* Normal exit */
    270       1.1    jruoho 
    271       1.1    jruoho             return_ACPI_STATUS (AE_OK);
    272       1.1    jruoho         }
    273       1.1    jruoho 
    274       1.1    jruoho         Aml += Length;
    275       1.1    jruoho         Offset += Length;
    276       1.1    jruoho     }
    277       1.1    jruoho 
    278       1.1    jruoho     /* Did not find an EndTag descriptor */
    279       1.1    jruoho 
    280   1.1.1.3  christos     if (UserFunction)
    281   1.1.1.3  christos     {
    282   1.1.1.3  christos         /* Insert an EndTag anyway. AcpiRsGetListLength always leaves room */
    283   1.1.1.3  christos 
    284   1.1.1.3  christos         (void) AcpiUtValidateResource (WalkState, EndTag, &ResourceIndex);
    285   1.1.1.3  christos         Status = UserFunction (EndTag, 2, Offset, ResourceIndex, Context);
    286   1.1.1.3  christos         if (ACPI_FAILURE (Status))
    287   1.1.1.3  christos         {
    288   1.1.1.3  christos             return_ACPI_STATUS (Status);
    289   1.1.1.3  christos         }
    290   1.1.1.3  christos     }
    291   1.1.1.3  christos 
    292   1.1.1.3  christos     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
    293       1.1    jruoho }
    294       1.1    jruoho 
    295       1.1    jruoho 
    296       1.1    jruoho /*******************************************************************************
    297       1.1    jruoho  *
    298       1.1    jruoho  * FUNCTION:    AcpiUtValidateResource
    299       1.1    jruoho  *
    300   1.1.1.3  christos  * PARAMETERS:  WalkState           - Current walk info
    301   1.1.1.3  christos  *              Aml                 - Pointer to the raw AML resource descriptor
    302   1.1.1.3  christos  *              ReturnIndex         - Where the resource index is returned. NULL
    303   1.1.1.3  christos  *                                    if the index is not required.
    304       1.1    jruoho  *
    305       1.1    jruoho  * RETURN:      Status, and optionally the Index into the global resource tables
    306       1.1    jruoho  *
    307       1.1    jruoho  * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
    308       1.1    jruoho  *              Type and Resource Length. Returns an index into the global
    309       1.1    jruoho  *              resource information/dispatch tables for later use.
    310       1.1    jruoho  *
    311       1.1    jruoho  ******************************************************************************/
    312       1.1    jruoho 
    313       1.1    jruoho ACPI_STATUS
    314       1.1    jruoho AcpiUtValidateResource (
    315   1.1.1.3  christos     ACPI_WALK_STATE         *WalkState,
    316       1.1    jruoho     void                    *Aml,
    317       1.1    jruoho     UINT8                   *ReturnIndex)
    318       1.1    jruoho {
    319   1.1.1.3  christos     AML_RESOURCE            *AmlResource;
    320       1.1    jruoho     UINT8                   ResourceType;
    321       1.1    jruoho     UINT8                   ResourceIndex;
    322       1.1    jruoho     ACPI_RS_LENGTH          ResourceLength;
    323       1.1    jruoho     ACPI_RS_LENGTH          MinimumResourceLength;
    324       1.1    jruoho 
    325       1.1    jruoho 
    326       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    327       1.1    jruoho 
    328       1.1    jruoho 
    329       1.1    jruoho     /*
    330       1.1    jruoho      * 1) Validate the ResourceType field (Byte 0)
    331       1.1    jruoho      */
    332       1.1    jruoho     ResourceType = ACPI_GET8 (Aml);
    333       1.1    jruoho 
    334       1.1    jruoho     /*
    335       1.1    jruoho      * Byte 0 contains the descriptor name (Resource Type)
    336       1.1    jruoho      * Examine the large/small bit in the resource header
    337       1.1    jruoho      */
    338       1.1    jruoho     if (ResourceType & ACPI_RESOURCE_NAME_LARGE)
    339       1.1    jruoho     {
    340       1.1    jruoho         /* Verify the large resource type (name) against the max */
    341       1.1    jruoho 
    342       1.1    jruoho         if (ResourceType > ACPI_RESOURCE_NAME_LARGE_MAX)
    343       1.1    jruoho         {
    344   1.1.1.3  christos             goto InvalidResource;
    345       1.1    jruoho         }
    346       1.1    jruoho 
    347       1.1    jruoho         /*
    348       1.1    jruoho          * Large Resource Type -- bits 6:0 contain the name
    349       1.1    jruoho          * Translate range 0x80-0x8B to index range 0x10-0x1B
    350       1.1    jruoho          */
    351       1.1    jruoho         ResourceIndex = (UINT8) (ResourceType - 0x70);
    352       1.1    jruoho     }
    353       1.1    jruoho     else
    354       1.1    jruoho     {
    355       1.1    jruoho         /*
    356       1.1    jruoho          * Small Resource Type -- bits 6:3 contain the name
    357       1.1    jruoho          * Shift range to index range 0x00-0x0F
    358       1.1    jruoho          */
    359       1.1    jruoho         ResourceIndex = (UINT8)
    360       1.1    jruoho             ((ResourceType & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
    361       1.1    jruoho     }
    362       1.1    jruoho 
    363   1.1.1.3  christos     /*
    364   1.1.1.6  christos      * Check validity of the resource type, via AcpiGbl_ResourceTypes.
    365   1.1.1.6  christos      * Zero indicates an invalid resource.
    366   1.1.1.3  christos      */
    367       1.1    jruoho     if (!AcpiGbl_ResourceTypes[ResourceIndex])
    368       1.1    jruoho     {
    369   1.1.1.3  christos         goto InvalidResource;
    370       1.1    jruoho     }
    371       1.1    jruoho 
    372       1.1    jruoho     /*
    373   1.1.1.3  christos      * Validate the ResourceLength field. This ensures that the length
    374   1.1.1.3  christos      * is at least reasonable, and guarantees that it is non-zero.
    375       1.1    jruoho      */
    376       1.1    jruoho     ResourceLength = AcpiUtGetResourceLength (Aml);
    377       1.1    jruoho     MinimumResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
    378       1.1    jruoho 
    379       1.1    jruoho     /* Validate based upon the type of resource - fixed length or variable */
    380       1.1    jruoho 
    381       1.1    jruoho     switch (AcpiGbl_ResourceTypes[ResourceIndex])
    382       1.1    jruoho     {
    383       1.1    jruoho     case ACPI_FIXED_LENGTH:
    384       1.1    jruoho 
    385       1.1    jruoho         /* Fixed length resource, length must match exactly */
    386       1.1    jruoho 
    387       1.1    jruoho         if (ResourceLength != MinimumResourceLength)
    388       1.1    jruoho         {
    389   1.1.1.3  christos             goto BadResourceLength;
    390       1.1    jruoho         }
    391       1.1    jruoho         break;
    392       1.1    jruoho 
    393       1.1    jruoho     case ACPI_VARIABLE_LENGTH:
    394       1.1    jruoho 
    395       1.1    jruoho         /* Variable length resource, length must be at least the minimum */
    396       1.1    jruoho 
    397       1.1    jruoho         if (ResourceLength < MinimumResourceLength)
    398       1.1    jruoho         {
    399   1.1.1.3  christos             goto BadResourceLength;
    400       1.1    jruoho         }
    401       1.1    jruoho         break;
    402       1.1    jruoho 
    403       1.1    jruoho     case ACPI_SMALL_VARIABLE_LENGTH:
    404       1.1    jruoho 
    405       1.1    jruoho         /* Small variable length resource, length can be (Min) or (Min-1) */
    406       1.1    jruoho 
    407       1.1    jruoho         if ((ResourceLength > MinimumResourceLength) ||
    408       1.1    jruoho             (ResourceLength < (MinimumResourceLength - 1)))
    409       1.1    jruoho         {
    410   1.1.1.3  christos             goto BadResourceLength;
    411       1.1    jruoho         }
    412       1.1    jruoho         break;
    413       1.1    jruoho 
    414       1.1    jruoho     default:
    415       1.1    jruoho 
    416       1.1    jruoho         /* Shouldn't happen (because of validation earlier), but be sure */
    417       1.1    jruoho 
    418   1.1.1.3  christos         goto InvalidResource;
    419   1.1.1.3  christos     }
    420   1.1.1.3  christos 
    421   1.1.1.3  christos     AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
    422   1.1.1.3  christos     if (ResourceType == ACPI_RESOURCE_NAME_SERIAL_BUS)
    423   1.1.1.3  christos     {
    424   1.1.1.3  christos         /* Validate the BusType field */
    425   1.1.1.3  christos 
    426   1.1.1.3  christos         if ((AmlResource->CommonSerialBus.Type == 0) ||
    427   1.1.1.3  christos             (AmlResource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
    428   1.1.1.3  christos         {
    429   1.1.1.3  christos             if (WalkState)
    430   1.1.1.3  christos             {
    431   1.1.1.3  christos                 ACPI_ERROR ((AE_INFO,
    432   1.1.1.3  christos                     "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
    433   1.1.1.3  christos                     AmlResource->CommonSerialBus.Type));
    434   1.1.1.3  christos             }
    435   1.1.1.3  christos             return (AE_AML_INVALID_RESOURCE_TYPE);
    436   1.1.1.3  christos         }
    437       1.1    jruoho     }
    438       1.1    jruoho 
    439       1.1    jruoho     /* Optionally return the resource table index */
    440       1.1    jruoho 
    441       1.1    jruoho     if (ReturnIndex)
    442       1.1    jruoho     {
    443       1.1    jruoho         *ReturnIndex = ResourceIndex;
    444       1.1    jruoho     }
    445       1.1    jruoho 
    446       1.1    jruoho     return (AE_OK);
    447   1.1.1.3  christos 
    448   1.1.1.3  christos 
    449   1.1.1.3  christos InvalidResource:
    450   1.1.1.3  christos 
    451   1.1.1.3  christos     if (WalkState)
    452   1.1.1.3  christos     {
    453   1.1.1.3  christos         ACPI_ERROR ((AE_INFO,
    454   1.1.1.3  christos             "Invalid/unsupported resource descriptor: Type 0x%2.2X",
    455   1.1.1.3  christos             ResourceType));
    456   1.1.1.3  christos     }
    457   1.1.1.3  christos     return (AE_AML_INVALID_RESOURCE_TYPE);
    458   1.1.1.3  christos 
    459   1.1.1.3  christos BadResourceLength:
    460   1.1.1.3  christos 
    461   1.1.1.3  christos     if (WalkState)
    462   1.1.1.3  christos     {
    463   1.1.1.3  christos         ACPI_ERROR ((AE_INFO,
    464   1.1.1.3  christos             "Invalid resource descriptor length: Type "
    465   1.1.1.3  christos             "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
    466   1.1.1.3  christos             ResourceType, ResourceLength, MinimumResourceLength));
    467   1.1.1.3  christos     }
    468   1.1.1.3  christos     return (AE_AML_BAD_RESOURCE_LENGTH);
    469       1.1    jruoho }
    470       1.1    jruoho 
    471       1.1    jruoho 
    472       1.1    jruoho /*******************************************************************************
    473       1.1    jruoho  *
    474       1.1    jruoho  * FUNCTION:    AcpiUtGetResourceType
    475       1.1    jruoho  *
    476       1.1    jruoho  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
    477       1.1    jruoho  *
    478       1.1    jruoho  * RETURN:      The Resource Type with no extraneous bits (except the
    479       1.1    jruoho  *              Large/Small descriptor bit -- this is left alone)
    480       1.1    jruoho  *
    481       1.1    jruoho  * DESCRIPTION: Extract the Resource Type/Name from the first byte of
    482       1.1    jruoho  *              a resource descriptor.
    483       1.1    jruoho  *
    484       1.1    jruoho  ******************************************************************************/
    485       1.1    jruoho 
    486       1.1    jruoho UINT8
    487       1.1    jruoho AcpiUtGetResourceType (
    488       1.1    jruoho     void                    *Aml)
    489       1.1    jruoho {
    490       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    491       1.1    jruoho 
    492       1.1    jruoho 
    493       1.1    jruoho     /*
    494       1.1    jruoho      * Byte 0 contains the descriptor name (Resource Type)
    495       1.1    jruoho      * Examine the large/small bit in the resource header
    496       1.1    jruoho      */
    497       1.1    jruoho     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
    498       1.1    jruoho     {
    499       1.1    jruoho         /* Large Resource Type -- bits 6:0 contain the name */
    500       1.1    jruoho 
    501       1.1    jruoho         return (ACPI_GET8 (Aml));
    502       1.1    jruoho     }
    503       1.1    jruoho     else
    504       1.1    jruoho     {
    505       1.1    jruoho         /* Small Resource Type -- bits 6:3 contain the name */
    506       1.1    jruoho 
    507       1.1    jruoho         return ((UINT8) (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
    508       1.1    jruoho     }
    509       1.1    jruoho }
    510       1.1    jruoho 
    511       1.1    jruoho 
    512       1.1    jruoho /*******************************************************************************
    513       1.1    jruoho  *
    514       1.1    jruoho  * FUNCTION:    AcpiUtGetResourceLength
    515       1.1    jruoho  *
    516       1.1    jruoho  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
    517       1.1    jruoho  *
    518       1.1    jruoho  * RETURN:      Byte Length
    519       1.1    jruoho  *
    520       1.1    jruoho  * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
    521       1.1    jruoho  *              definition, this does not include the size of the descriptor
    522       1.1    jruoho  *              header or the length field itself.
    523       1.1    jruoho  *
    524       1.1    jruoho  ******************************************************************************/
    525       1.1    jruoho 
    526       1.1    jruoho UINT16
    527       1.1    jruoho AcpiUtGetResourceLength (
    528       1.1    jruoho     void                    *Aml)
    529       1.1    jruoho {
    530       1.1    jruoho     ACPI_RS_LENGTH          ResourceLength;
    531       1.1    jruoho 
    532       1.1    jruoho 
    533       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    534       1.1    jruoho 
    535       1.1    jruoho 
    536       1.1    jruoho     /*
    537       1.1    jruoho      * Byte 0 contains the descriptor name (Resource Type)
    538       1.1    jruoho      * Examine the large/small bit in the resource header
    539       1.1    jruoho      */
    540       1.1    jruoho     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
    541       1.1    jruoho     {
    542       1.1    jruoho         /* Large Resource type -- bytes 1-2 contain the 16-bit length */
    543       1.1    jruoho 
    544       1.1    jruoho         ACPI_MOVE_16_TO_16 (&ResourceLength, ACPI_ADD_PTR (UINT8, Aml, 1));
    545       1.1    jruoho 
    546       1.1    jruoho     }
    547       1.1    jruoho     else
    548       1.1    jruoho     {
    549       1.1    jruoho         /* Small Resource type -- bits 2:0 of byte 0 contain the length */
    550       1.1    jruoho 
    551       1.1    jruoho         ResourceLength = (UINT16) (ACPI_GET8 (Aml) &
    552   1.1.1.6  christos             ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
    553       1.1    jruoho     }
    554       1.1    jruoho 
    555       1.1    jruoho     return (ResourceLength);
    556       1.1    jruoho }
    557       1.1    jruoho 
    558       1.1    jruoho 
    559       1.1    jruoho /*******************************************************************************
    560       1.1    jruoho  *
    561       1.1    jruoho  * FUNCTION:    AcpiUtGetResourceHeaderLength
    562       1.1    jruoho  *
    563       1.1    jruoho  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
    564       1.1    jruoho  *
    565       1.1    jruoho  * RETURN:      Length of the AML header (depends on large/small descriptor)
    566       1.1    jruoho  *
    567       1.1    jruoho  * DESCRIPTION: Get the length of the header for this resource.
    568       1.1    jruoho  *
    569       1.1    jruoho  ******************************************************************************/
    570       1.1    jruoho 
    571       1.1    jruoho UINT8
    572       1.1    jruoho AcpiUtGetResourceHeaderLength (
    573       1.1    jruoho     void                    *Aml)
    574       1.1    jruoho {
    575       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    576       1.1    jruoho 
    577       1.1    jruoho 
    578       1.1    jruoho     /* Examine the large/small bit in the resource header */
    579       1.1    jruoho 
    580       1.1    jruoho     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
    581       1.1    jruoho     {
    582       1.1    jruoho         return (sizeof (AML_RESOURCE_LARGE_HEADER));
    583       1.1    jruoho     }
    584       1.1    jruoho     else
    585       1.1    jruoho     {
    586       1.1    jruoho         return (sizeof (AML_RESOURCE_SMALL_HEADER));
    587       1.1    jruoho     }
    588       1.1    jruoho }
    589       1.1    jruoho 
    590       1.1    jruoho 
    591       1.1    jruoho /*******************************************************************************
    592       1.1    jruoho  *
    593       1.1    jruoho  * FUNCTION:    AcpiUtGetDescriptorLength
    594       1.1    jruoho  *
    595       1.1    jruoho  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
    596       1.1    jruoho  *
    597       1.1    jruoho  * RETURN:      Byte length
    598       1.1    jruoho  *
    599       1.1    jruoho  * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
    600       1.1    jruoho  *              length of the descriptor header and the length field itself.
    601       1.1    jruoho  *              Used to walk descriptor lists.
    602       1.1    jruoho  *
    603       1.1    jruoho  ******************************************************************************/
    604       1.1    jruoho 
    605       1.1    jruoho UINT32
    606       1.1    jruoho AcpiUtGetDescriptorLength (
    607       1.1    jruoho     void                    *Aml)
    608       1.1    jruoho {
    609       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    610       1.1    jruoho 
    611       1.1    jruoho 
    612       1.1    jruoho     /*
    613       1.1    jruoho      * Get the Resource Length (does not include header length) and add
    614       1.1    jruoho      * the header length (depends on if this is a small or large resource)
    615       1.1    jruoho      */
    616       1.1    jruoho     return (AcpiUtGetResourceLength (Aml) +
    617   1.1.1.6  christos         AcpiUtGetResourceHeaderLength (Aml));
    618       1.1    jruoho }
    619       1.1    jruoho 
    620       1.1    jruoho 
    621       1.1    jruoho /*******************************************************************************
    622       1.1    jruoho  *
    623       1.1    jruoho  * FUNCTION:    AcpiUtGetResourceEndTag
    624       1.1    jruoho  *
    625       1.1    jruoho  * PARAMETERS:  ObjDesc         - The resource template buffer object
    626       1.1    jruoho  *              EndTag          - Where the pointer to the EndTag is returned
    627       1.1    jruoho  *
    628       1.1    jruoho  * RETURN:      Status, pointer to the end tag
    629       1.1    jruoho  *
    630       1.1    jruoho  * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
    631       1.1    jruoho  *              Note: allows a buffer length of zero.
    632       1.1    jruoho  *
    633       1.1    jruoho  ******************************************************************************/
    634       1.1    jruoho 
    635       1.1    jruoho ACPI_STATUS
    636       1.1    jruoho AcpiUtGetResourceEndTag (
    637       1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    638       1.1    jruoho     UINT8                   **EndTag)
    639       1.1    jruoho {
    640       1.1    jruoho     ACPI_STATUS             Status;
    641       1.1    jruoho 
    642       1.1    jruoho 
    643       1.1    jruoho     ACPI_FUNCTION_TRACE (UtGetResourceEndTag);
    644       1.1    jruoho 
    645       1.1    jruoho 
    646       1.1    jruoho     /* Allow a buffer length of zero */
    647       1.1    jruoho 
    648       1.1    jruoho     if (!ObjDesc->Buffer.Length)
    649       1.1    jruoho     {
    650       1.1    jruoho         *EndTag = ObjDesc->Buffer.Pointer;
    651       1.1    jruoho         return_ACPI_STATUS (AE_OK);
    652       1.1    jruoho     }
    653       1.1    jruoho 
    654       1.1    jruoho     /* Validate the template and get a pointer to the EndTag */
    655       1.1    jruoho 
    656   1.1.1.3  christos     Status = AcpiUtWalkAmlResources (NULL, ObjDesc->Buffer.Pointer,
    657   1.1.1.6  christos         ObjDesc->Buffer.Length, NULL, (void **) EndTag);
    658       1.1    jruoho 
    659       1.1    jruoho     return_ACPI_STATUS (Status);
    660       1.1    jruoho }
    661