Home | History | Annotate | Line # | Download | only in utilities
utresrc.c revision 1.1.1.15
      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.15  christos  * Copyright (C) 2000 - 2023, 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.15  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_CLOCK_INPUT),
    100  1.1.1.15  christos 
    101   1.1.1.3  christos };
    102   1.1.1.3  christos 
    103   1.1.1.3  christos const UINT8                 AcpiGbl_ResourceAmlSerialBusSizes[] =
    104   1.1.1.3  christos {
    105   1.1.1.3  christos     0,
    106   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_I2C_SERIALBUS),
    107   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_SPI_SERIALBUS),
    108   1.1.1.3  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_UART_SERIALBUS),
    109  1.1.1.13  christos     ACPI_AML_SIZE_LARGE (AML_RESOURCE_CSI2_SERIALBUS),
    110       1.1    jruoho };
    111       1.1    jruoho 
    112       1.1    jruoho 
    113       1.1    jruoho /*
    114       1.1    jruoho  * Resource types, used to validate the resource length field.
    115       1.1    jruoho  * The length of fixed-length types must match exactly, variable
    116       1.1    jruoho  * lengths must meet the minimum required length, etc.
    117       1.1    jruoho  * Zero indicates a reserved (and therefore invalid) resource type.
    118       1.1    jruoho  */
    119       1.1    jruoho static const UINT8          AcpiGbl_ResourceTypes[] =
    120       1.1    jruoho {
    121       1.1    jruoho     /* Small descriptors */
    122       1.1    jruoho 
    123       1.1    jruoho     0,
    124       1.1    jruoho     0,
    125       1.1    jruoho     0,
    126       1.1    jruoho     0,
    127   1.1.1.3  christos     ACPI_SMALL_VARIABLE_LENGTH,     /* 04 IRQ */
    128   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 05 DMA */
    129   1.1.1.3  christos     ACPI_SMALL_VARIABLE_LENGTH,     /* 06 StartDependentFunctions */
    130   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 07 EndDependentFunctions */
    131   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 08 IO */
    132   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 09 FixedIO */
    133   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 0A FixedDMA */
    134       1.1    jruoho     0,
    135       1.1    jruoho     0,
    136       1.1    jruoho     0,
    137   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 0E VendorShort */
    138   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 0F EndTag */
    139       1.1    jruoho 
    140       1.1    jruoho     /* Large descriptors */
    141       1.1    jruoho 
    142       1.1    jruoho     0,
    143   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 01 Memory24 */
    144   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 02 GenericRegister */
    145       1.1    jruoho     0,
    146   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 04 VendorLong */
    147   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 05 Memory32 */
    148   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 06 Memory32Fixed */
    149   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 07 Dword* address */
    150   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 08 Word* address */
    151   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 09 ExtendedIRQ */
    152   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 0A Qword* address */
    153   1.1.1.3  christos     ACPI_FIXED_LENGTH,              /* 0B Extended* address */
    154   1.1.1.3  christos     ACPI_VARIABLE_LENGTH,           /* 0C Gpio* */
    155   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 0D PinFunction */
    156   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 0E *SerialBus */
    157   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 0F PinConfig */
    158   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 10 PinGroup */
    159   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 11 PinGroupFunction */
    160   1.1.1.9  christos     ACPI_VARIABLE_LENGTH,           /* 12 PinGroupConfig */
    161  1.1.1.15  christos     ACPI_VARIABLE_LENGTH,           /* 13 ClockInput */
    162       1.1    jruoho };
    163       1.1    jruoho 
    164       1.1    jruoho 
    165       1.1    jruoho /*******************************************************************************
    166       1.1    jruoho  *
    167       1.1    jruoho  * FUNCTION:    AcpiUtWalkAmlResources
    168       1.1    jruoho  *
    169   1.1.1.3  christos  * PARAMETERS:  WalkState           - Current walk info
    170   1.1.1.3  christos  * PARAMETERS:  Aml                 - Pointer to the raw AML resource template
    171   1.1.1.3  christos  *              AmlLength           - Length of the entire template
    172   1.1.1.3  christos  *              UserFunction        - Called once for each descriptor found. If
    173   1.1.1.3  christos  *                                    NULL, a pointer to the EndTag is returned
    174   1.1.1.3  christos  *              Context             - Passed to UserFunction
    175       1.1    jruoho  *
    176       1.1    jruoho  * RETURN:      Status
    177       1.1    jruoho  *
    178       1.1    jruoho  * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
    179       1.1    jruoho  *              once for each resource found.
    180       1.1    jruoho  *
    181       1.1    jruoho  ******************************************************************************/
    182       1.1    jruoho 
    183       1.1    jruoho ACPI_STATUS
    184       1.1    jruoho AcpiUtWalkAmlResources (
    185   1.1.1.3  christos     ACPI_WALK_STATE         *WalkState,
    186       1.1    jruoho     UINT8                   *Aml,
    187       1.1    jruoho     ACPI_SIZE               AmlLength,
    188       1.1    jruoho     ACPI_WALK_AML_CALLBACK  UserFunction,
    189   1.1.1.3  christos     void                    **Context)
    190       1.1    jruoho {
    191       1.1    jruoho     ACPI_STATUS             Status;
    192       1.1    jruoho     UINT8                   *EndAml;
    193       1.1    jruoho     UINT8                   ResourceIndex;
    194       1.1    jruoho     UINT32                  Length;
    195       1.1    jruoho     UINT32                  Offset = 0;
    196   1.1.1.3  christos     UINT8                   EndTag[2] = {0x79, 0x00};
    197       1.1    jruoho 
    198       1.1    jruoho 
    199       1.1    jruoho     ACPI_FUNCTION_TRACE (UtWalkAmlResources);
    200       1.1    jruoho 
    201       1.1    jruoho 
    202   1.1.1.9  christos     /* The absolute minimum resource template is one EndTag descriptor */
    203   1.1.1.9  christos 
    204       1.1    jruoho     if (AmlLength < sizeof (AML_RESOURCE_END_TAG))
    205       1.1    jruoho     {
    206       1.1    jruoho         return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
    207       1.1    jruoho     }
    208       1.1    jruoho 
    209       1.1    jruoho     /* Point to the end of the resource template buffer */
    210       1.1    jruoho 
    211       1.1    jruoho     EndAml = Aml + AmlLength;
    212       1.1    jruoho 
    213       1.1    jruoho     /* Walk the byte list, abort on any invalid descriptor type or length */
    214       1.1    jruoho 
    215       1.1    jruoho     while (Aml < EndAml)
    216       1.1    jruoho     {
    217       1.1    jruoho         /* Validate the Resource Type and Resource Length */
    218       1.1    jruoho 
    219   1.1.1.3  christos         Status = AcpiUtValidateResource (WalkState, Aml, &ResourceIndex);
    220       1.1    jruoho         if (ACPI_FAILURE (Status))
    221       1.1    jruoho         {
    222   1.1.1.3  christos             /*
    223   1.1.1.6  christos              * Exit on failure. Cannot continue because the descriptor
    224   1.1.1.6  christos              * length may be bogus also.
    225   1.1.1.3  christos              */
    226       1.1    jruoho             return_ACPI_STATUS (Status);
    227       1.1    jruoho         }
    228       1.1    jruoho 
    229       1.1    jruoho         /* Get the length of this descriptor */
    230       1.1    jruoho 
    231       1.1    jruoho         Length = AcpiUtGetDescriptorLength (Aml);
    232       1.1    jruoho 
    233       1.1    jruoho         /* Invoke the user function */
    234       1.1    jruoho 
    235       1.1    jruoho         if (UserFunction)
    236       1.1    jruoho         {
    237   1.1.1.9  christos             Status = UserFunction (
    238   1.1.1.9  christos                 Aml, Length, Offset, ResourceIndex, Context);
    239       1.1    jruoho             if (ACPI_FAILURE (Status))
    240       1.1    jruoho             {
    241   1.1.1.3  christos                 return_ACPI_STATUS (Status);
    242       1.1    jruoho             }
    243       1.1    jruoho         }
    244       1.1    jruoho 
    245       1.1    jruoho         /* An EndTag descriptor terminates this resource template */
    246       1.1    jruoho 
    247       1.1    jruoho         if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_END_TAG)
    248       1.1    jruoho         {
    249       1.1    jruoho             /*
    250       1.1    jruoho              * There must be at least one more byte in the buffer for
    251       1.1    jruoho              * the 2nd byte of the EndTag
    252       1.1    jruoho              */
    253       1.1    jruoho             if ((Aml + 1) >= EndAml)
    254       1.1    jruoho             {
    255       1.1    jruoho                 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
    256       1.1    jruoho             }
    257       1.1    jruoho 
    258   1.1.1.8  christos             /*
    259   1.1.1.9  christos              * Don't attempt to perform any validation on the 2nd byte.
    260   1.1.1.9  christos              * Although all known ASL compilers insert a zero for the 2nd
    261   1.1.1.9  christos              * byte, it can also be a checksum (as per the ACPI spec),
    262   1.1.1.9  christos              * and this is occasionally seen in the field. July 2017.
    263   1.1.1.8  christos              */
    264   1.1.1.8  christos 
    265       1.1    jruoho             /* Return the pointer to the EndTag if requested */
    266       1.1    jruoho 
    267       1.1    jruoho             if (!UserFunction)
    268       1.1    jruoho             {
    269   1.1.1.3  christos                 *Context = Aml;
    270       1.1    jruoho             }
    271       1.1    jruoho 
    272       1.1    jruoho             /* Normal exit */
    273       1.1    jruoho 
    274       1.1    jruoho             return_ACPI_STATUS (AE_OK);
    275       1.1    jruoho         }
    276       1.1    jruoho 
    277       1.1    jruoho         Aml += Length;
    278       1.1    jruoho         Offset += Length;
    279       1.1    jruoho     }
    280       1.1    jruoho 
    281       1.1    jruoho     /* Did not find an EndTag descriptor */
    282       1.1    jruoho 
    283   1.1.1.3  christos     if (UserFunction)
    284   1.1.1.3  christos     {
    285   1.1.1.3  christos         /* Insert an EndTag anyway. AcpiRsGetListLength always leaves room */
    286   1.1.1.3  christos 
    287   1.1.1.3  christos         (void) AcpiUtValidateResource (WalkState, EndTag, &ResourceIndex);
    288   1.1.1.3  christos         Status = UserFunction (EndTag, 2, Offset, ResourceIndex, Context);
    289   1.1.1.3  christos         if (ACPI_FAILURE (Status))
    290   1.1.1.3  christos         {
    291   1.1.1.3  christos             return_ACPI_STATUS (Status);
    292   1.1.1.3  christos         }
    293   1.1.1.3  christos     }
    294   1.1.1.3  christos 
    295   1.1.1.3  christos     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
    296       1.1    jruoho }
    297       1.1    jruoho 
    298       1.1    jruoho 
    299       1.1    jruoho /*******************************************************************************
    300       1.1    jruoho  *
    301       1.1    jruoho  * FUNCTION:    AcpiUtValidateResource
    302       1.1    jruoho  *
    303   1.1.1.3  christos  * PARAMETERS:  WalkState           - Current walk info
    304   1.1.1.3  christos  *              Aml                 - Pointer to the raw AML resource descriptor
    305   1.1.1.3  christos  *              ReturnIndex         - Where the resource index is returned. NULL
    306   1.1.1.3  christos  *                                    if the index is not required.
    307       1.1    jruoho  *
    308       1.1    jruoho  * RETURN:      Status, and optionally the Index into the global resource tables
    309       1.1    jruoho  *
    310       1.1    jruoho  * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
    311       1.1    jruoho  *              Type and Resource Length. Returns an index into the global
    312       1.1    jruoho  *              resource information/dispatch tables for later use.
    313       1.1    jruoho  *
    314       1.1    jruoho  ******************************************************************************/
    315       1.1    jruoho 
    316       1.1    jruoho ACPI_STATUS
    317       1.1    jruoho AcpiUtValidateResource (
    318   1.1.1.3  christos     ACPI_WALK_STATE         *WalkState,
    319       1.1    jruoho     void                    *Aml,
    320       1.1    jruoho     UINT8                   *ReturnIndex)
    321       1.1    jruoho {
    322   1.1.1.3  christos     AML_RESOURCE            *AmlResource;
    323       1.1    jruoho     UINT8                   ResourceType;
    324       1.1    jruoho     UINT8                   ResourceIndex;
    325       1.1    jruoho     ACPI_RS_LENGTH          ResourceLength;
    326       1.1    jruoho     ACPI_RS_LENGTH          MinimumResourceLength;
    327       1.1    jruoho 
    328       1.1    jruoho 
    329       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    330       1.1    jruoho 
    331       1.1    jruoho 
    332       1.1    jruoho     /*
    333       1.1    jruoho      * 1) Validate the ResourceType field (Byte 0)
    334       1.1    jruoho      */
    335       1.1    jruoho     ResourceType = ACPI_GET8 (Aml);
    336       1.1    jruoho 
    337       1.1    jruoho     /*
    338       1.1    jruoho      * Byte 0 contains the descriptor name (Resource Type)
    339       1.1    jruoho      * Examine the large/small bit in the resource header
    340       1.1    jruoho      */
    341       1.1    jruoho     if (ResourceType & ACPI_RESOURCE_NAME_LARGE)
    342       1.1    jruoho     {
    343       1.1    jruoho         /* Verify the large resource type (name) against the max */
    344       1.1    jruoho 
    345       1.1    jruoho         if (ResourceType > ACPI_RESOURCE_NAME_LARGE_MAX)
    346       1.1    jruoho         {
    347   1.1.1.3  christos             goto InvalidResource;
    348       1.1    jruoho         }
    349       1.1    jruoho 
    350       1.1    jruoho         /*
    351       1.1    jruoho          * Large Resource Type -- bits 6:0 contain the name
    352       1.1    jruoho          * Translate range 0x80-0x8B to index range 0x10-0x1B
    353       1.1    jruoho          */
    354       1.1    jruoho         ResourceIndex = (UINT8) (ResourceType - 0x70);
    355       1.1    jruoho     }
    356       1.1    jruoho     else
    357       1.1    jruoho     {
    358       1.1    jruoho         /*
    359       1.1    jruoho          * Small Resource Type -- bits 6:3 contain the name
    360       1.1    jruoho          * Shift range to index range 0x00-0x0F
    361       1.1    jruoho          */
    362       1.1    jruoho         ResourceIndex = (UINT8)
    363       1.1    jruoho             ((ResourceType & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
    364       1.1    jruoho     }
    365       1.1    jruoho 
    366   1.1.1.3  christos     /*
    367   1.1.1.6  christos      * Check validity of the resource type, via AcpiGbl_ResourceTypes.
    368   1.1.1.6  christos      * Zero indicates an invalid resource.
    369   1.1.1.3  christos      */
    370       1.1    jruoho     if (!AcpiGbl_ResourceTypes[ResourceIndex])
    371       1.1    jruoho     {
    372   1.1.1.3  christos         goto InvalidResource;
    373       1.1    jruoho     }
    374       1.1    jruoho 
    375       1.1    jruoho     /*
    376   1.1.1.3  christos      * Validate the ResourceLength field. This ensures that the length
    377   1.1.1.3  christos      * is at least reasonable, and guarantees that it is non-zero.
    378       1.1    jruoho      */
    379       1.1    jruoho     ResourceLength = AcpiUtGetResourceLength (Aml);
    380       1.1    jruoho     MinimumResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
    381       1.1    jruoho 
    382       1.1    jruoho     /* Validate based upon the type of resource - fixed length or variable */
    383       1.1    jruoho 
    384       1.1    jruoho     switch (AcpiGbl_ResourceTypes[ResourceIndex])
    385       1.1    jruoho     {
    386       1.1    jruoho     case ACPI_FIXED_LENGTH:
    387       1.1    jruoho 
    388       1.1    jruoho         /* Fixed length resource, length must match exactly */
    389       1.1    jruoho 
    390       1.1    jruoho         if (ResourceLength != MinimumResourceLength)
    391       1.1    jruoho         {
    392   1.1.1.3  christos             goto BadResourceLength;
    393       1.1    jruoho         }
    394       1.1    jruoho         break;
    395       1.1    jruoho 
    396       1.1    jruoho     case ACPI_VARIABLE_LENGTH:
    397       1.1    jruoho 
    398       1.1    jruoho         /* Variable length resource, length must be at least the minimum */
    399       1.1    jruoho 
    400       1.1    jruoho         if (ResourceLength < MinimumResourceLength)
    401       1.1    jruoho         {
    402   1.1.1.3  christos             goto BadResourceLength;
    403       1.1    jruoho         }
    404       1.1    jruoho         break;
    405       1.1    jruoho 
    406       1.1    jruoho     case ACPI_SMALL_VARIABLE_LENGTH:
    407       1.1    jruoho 
    408       1.1    jruoho         /* Small variable length resource, length can be (Min) or (Min-1) */
    409       1.1    jruoho 
    410       1.1    jruoho         if ((ResourceLength > MinimumResourceLength) ||
    411       1.1    jruoho             (ResourceLength < (MinimumResourceLength - 1)))
    412       1.1    jruoho         {
    413   1.1.1.3  christos             goto BadResourceLength;
    414       1.1    jruoho         }
    415       1.1    jruoho         break;
    416       1.1    jruoho 
    417       1.1    jruoho     default:
    418       1.1    jruoho 
    419       1.1    jruoho         /* Shouldn't happen (because of validation earlier), but be sure */
    420       1.1    jruoho 
    421   1.1.1.3  christos         goto InvalidResource;
    422   1.1.1.3  christos     }
    423   1.1.1.3  christos 
    424   1.1.1.3  christos     AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
    425   1.1.1.3  christos     if (ResourceType == ACPI_RESOURCE_NAME_SERIAL_BUS)
    426   1.1.1.3  christos     {
    427  1.1.1.15  christos         /* Avoid undefined behavior: member access within misaligned address */
    428  1.1.1.15  christos 
    429  1.1.1.15  christos         AML_RESOURCE_COMMON_SERIALBUS CommonSerialBus;
    430  1.1.1.15  christos         memcpy(&CommonSerialBus, AmlResource, sizeof(CommonSerialBus));
    431  1.1.1.15  christos 
    432   1.1.1.3  christos         /* Validate the BusType field */
    433   1.1.1.3  christos 
    434  1.1.1.15  christos         if ((CommonSerialBus.Type == 0) ||
    435  1.1.1.15  christos             (CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
    436   1.1.1.3  christos         {
    437   1.1.1.3  christos             if (WalkState)
    438   1.1.1.3  christos             {
    439   1.1.1.3  christos                 ACPI_ERROR ((AE_INFO,
    440   1.1.1.3  christos                     "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
    441  1.1.1.15  christos                     CommonSerialBus.Type));
    442   1.1.1.3  christos             }
    443   1.1.1.3  christos             return (AE_AML_INVALID_RESOURCE_TYPE);
    444   1.1.1.3  christos         }
    445       1.1    jruoho     }
    446       1.1    jruoho 
    447       1.1    jruoho     /* Optionally return the resource table index */
    448       1.1    jruoho 
    449       1.1    jruoho     if (ReturnIndex)
    450       1.1    jruoho     {
    451       1.1    jruoho         *ReturnIndex = ResourceIndex;
    452       1.1    jruoho     }
    453       1.1    jruoho 
    454       1.1    jruoho     return (AE_OK);
    455   1.1.1.3  christos 
    456   1.1.1.3  christos 
    457   1.1.1.3  christos InvalidResource:
    458   1.1.1.3  christos 
    459   1.1.1.3  christos     if (WalkState)
    460   1.1.1.3  christos     {
    461   1.1.1.3  christos         ACPI_ERROR ((AE_INFO,
    462   1.1.1.3  christos             "Invalid/unsupported resource descriptor: Type 0x%2.2X",
    463   1.1.1.3  christos             ResourceType));
    464   1.1.1.3  christos     }
    465   1.1.1.3  christos     return (AE_AML_INVALID_RESOURCE_TYPE);
    466   1.1.1.3  christos 
    467   1.1.1.3  christos BadResourceLength:
    468   1.1.1.3  christos 
    469   1.1.1.3  christos     if (WalkState)
    470   1.1.1.3  christos     {
    471   1.1.1.3  christos         ACPI_ERROR ((AE_INFO,
    472   1.1.1.3  christos             "Invalid resource descriptor length: Type "
    473   1.1.1.3  christos             "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
    474   1.1.1.3  christos             ResourceType, ResourceLength, MinimumResourceLength));
    475   1.1.1.3  christos     }
    476   1.1.1.3  christos     return (AE_AML_BAD_RESOURCE_LENGTH);
    477       1.1    jruoho }
    478       1.1    jruoho 
    479       1.1    jruoho 
    480       1.1    jruoho /*******************************************************************************
    481       1.1    jruoho  *
    482       1.1    jruoho  * FUNCTION:    AcpiUtGetResourceType
    483       1.1    jruoho  *
    484       1.1    jruoho  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
    485       1.1    jruoho  *
    486       1.1    jruoho  * RETURN:      The Resource Type with no extraneous bits (except the
    487       1.1    jruoho  *              Large/Small descriptor bit -- this is left alone)
    488       1.1    jruoho  *
    489       1.1    jruoho  * DESCRIPTION: Extract the Resource Type/Name from the first byte of
    490       1.1    jruoho  *              a resource descriptor.
    491       1.1    jruoho  *
    492       1.1    jruoho  ******************************************************************************/
    493       1.1    jruoho 
    494       1.1    jruoho UINT8
    495       1.1    jruoho AcpiUtGetResourceType (
    496       1.1    jruoho     void                    *Aml)
    497       1.1    jruoho {
    498       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    499       1.1    jruoho 
    500       1.1    jruoho 
    501       1.1    jruoho     /*
    502       1.1    jruoho      * Byte 0 contains the descriptor name (Resource Type)
    503       1.1    jruoho      * Examine the large/small bit in the resource header
    504       1.1    jruoho      */
    505       1.1    jruoho     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
    506       1.1    jruoho     {
    507       1.1    jruoho         /* Large Resource Type -- bits 6:0 contain the name */
    508       1.1    jruoho 
    509       1.1    jruoho         return (ACPI_GET8 (Aml));
    510       1.1    jruoho     }
    511       1.1    jruoho     else
    512       1.1    jruoho     {
    513       1.1    jruoho         /* Small Resource Type -- bits 6:3 contain the name */
    514       1.1    jruoho 
    515       1.1    jruoho         return ((UINT8) (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
    516       1.1    jruoho     }
    517       1.1    jruoho }
    518       1.1    jruoho 
    519       1.1    jruoho 
    520       1.1    jruoho /*******************************************************************************
    521       1.1    jruoho  *
    522       1.1    jruoho  * FUNCTION:    AcpiUtGetResourceLength
    523       1.1    jruoho  *
    524       1.1    jruoho  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
    525       1.1    jruoho  *
    526       1.1    jruoho  * RETURN:      Byte Length
    527       1.1    jruoho  *
    528       1.1    jruoho  * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
    529       1.1    jruoho  *              definition, this does not include the size of the descriptor
    530       1.1    jruoho  *              header or the length field itself.
    531       1.1    jruoho  *
    532       1.1    jruoho  ******************************************************************************/
    533       1.1    jruoho 
    534       1.1    jruoho UINT16
    535       1.1    jruoho AcpiUtGetResourceLength (
    536       1.1    jruoho     void                    *Aml)
    537       1.1    jruoho {
    538       1.1    jruoho     ACPI_RS_LENGTH          ResourceLength;
    539       1.1    jruoho 
    540       1.1    jruoho 
    541       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    542       1.1    jruoho 
    543       1.1    jruoho 
    544       1.1    jruoho     /*
    545       1.1    jruoho      * Byte 0 contains the descriptor name (Resource Type)
    546       1.1    jruoho      * Examine the large/small bit in the resource header
    547       1.1    jruoho      */
    548       1.1    jruoho     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
    549       1.1    jruoho     {
    550       1.1    jruoho         /* Large Resource type -- bytes 1-2 contain the 16-bit length */
    551       1.1    jruoho 
    552       1.1    jruoho         ACPI_MOVE_16_TO_16 (&ResourceLength, ACPI_ADD_PTR (UINT8, Aml, 1));
    553       1.1    jruoho 
    554       1.1    jruoho     }
    555       1.1    jruoho     else
    556       1.1    jruoho     {
    557       1.1    jruoho         /* Small Resource type -- bits 2:0 of byte 0 contain the length */
    558       1.1    jruoho 
    559       1.1    jruoho         ResourceLength = (UINT16) (ACPI_GET8 (Aml) &
    560   1.1.1.6  christos             ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
    561       1.1    jruoho     }
    562       1.1    jruoho 
    563       1.1    jruoho     return (ResourceLength);
    564       1.1    jruoho }
    565       1.1    jruoho 
    566       1.1    jruoho 
    567       1.1    jruoho /*******************************************************************************
    568       1.1    jruoho  *
    569       1.1    jruoho  * FUNCTION:    AcpiUtGetResourceHeaderLength
    570       1.1    jruoho  *
    571       1.1    jruoho  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
    572       1.1    jruoho  *
    573       1.1    jruoho  * RETURN:      Length of the AML header (depends on large/small descriptor)
    574       1.1    jruoho  *
    575       1.1    jruoho  * DESCRIPTION: Get the length of the header for this resource.
    576       1.1    jruoho  *
    577       1.1    jruoho  ******************************************************************************/
    578       1.1    jruoho 
    579       1.1    jruoho UINT8
    580       1.1    jruoho AcpiUtGetResourceHeaderLength (
    581       1.1    jruoho     void                    *Aml)
    582       1.1    jruoho {
    583       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    584       1.1    jruoho 
    585       1.1    jruoho 
    586       1.1    jruoho     /* Examine the large/small bit in the resource header */
    587       1.1    jruoho 
    588       1.1    jruoho     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
    589       1.1    jruoho     {
    590       1.1    jruoho         return (sizeof (AML_RESOURCE_LARGE_HEADER));
    591       1.1    jruoho     }
    592       1.1    jruoho     else
    593       1.1    jruoho     {
    594       1.1    jruoho         return (sizeof (AML_RESOURCE_SMALL_HEADER));
    595       1.1    jruoho     }
    596       1.1    jruoho }
    597       1.1    jruoho 
    598       1.1    jruoho 
    599       1.1    jruoho /*******************************************************************************
    600       1.1    jruoho  *
    601       1.1    jruoho  * FUNCTION:    AcpiUtGetDescriptorLength
    602       1.1    jruoho  *
    603       1.1    jruoho  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
    604       1.1    jruoho  *
    605       1.1    jruoho  * RETURN:      Byte length
    606       1.1    jruoho  *
    607       1.1    jruoho  * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
    608       1.1    jruoho  *              length of the descriptor header and the length field itself.
    609       1.1    jruoho  *              Used to walk descriptor lists.
    610       1.1    jruoho  *
    611       1.1    jruoho  ******************************************************************************/
    612       1.1    jruoho 
    613       1.1    jruoho UINT32
    614       1.1    jruoho AcpiUtGetDescriptorLength (
    615       1.1    jruoho     void                    *Aml)
    616       1.1    jruoho {
    617       1.1    jruoho     ACPI_FUNCTION_ENTRY ();
    618       1.1    jruoho 
    619       1.1    jruoho 
    620       1.1    jruoho     /*
    621       1.1    jruoho      * Get the Resource Length (does not include header length) and add
    622       1.1    jruoho      * the header length (depends on if this is a small or large resource)
    623       1.1    jruoho      */
    624       1.1    jruoho     return (AcpiUtGetResourceLength (Aml) +
    625   1.1.1.6  christos         AcpiUtGetResourceHeaderLength (Aml));
    626       1.1    jruoho }
    627       1.1    jruoho 
    628       1.1    jruoho 
    629       1.1    jruoho /*******************************************************************************
    630       1.1    jruoho  *
    631       1.1    jruoho  * FUNCTION:    AcpiUtGetResourceEndTag
    632       1.1    jruoho  *
    633       1.1    jruoho  * PARAMETERS:  ObjDesc         - The resource template buffer object
    634       1.1    jruoho  *              EndTag          - Where the pointer to the EndTag is returned
    635       1.1    jruoho  *
    636       1.1    jruoho  * RETURN:      Status, pointer to the end tag
    637       1.1    jruoho  *
    638       1.1    jruoho  * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
    639       1.1    jruoho  *              Note: allows a buffer length of zero.
    640       1.1    jruoho  *
    641       1.1    jruoho  ******************************************************************************/
    642       1.1    jruoho 
    643       1.1    jruoho ACPI_STATUS
    644       1.1    jruoho AcpiUtGetResourceEndTag (
    645       1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    646       1.1    jruoho     UINT8                   **EndTag)
    647       1.1    jruoho {
    648       1.1    jruoho     ACPI_STATUS             Status;
    649       1.1    jruoho 
    650       1.1    jruoho 
    651       1.1    jruoho     ACPI_FUNCTION_TRACE (UtGetResourceEndTag);
    652       1.1    jruoho 
    653       1.1    jruoho 
    654       1.1    jruoho     /* Allow a buffer length of zero */
    655       1.1    jruoho 
    656       1.1    jruoho     if (!ObjDesc->Buffer.Length)
    657       1.1    jruoho     {
    658       1.1    jruoho         *EndTag = ObjDesc->Buffer.Pointer;
    659       1.1    jruoho         return_ACPI_STATUS (AE_OK);
    660       1.1    jruoho     }
    661       1.1    jruoho 
    662       1.1    jruoho     /* Validate the template and get a pointer to the EndTag */
    663       1.1    jruoho 
    664   1.1.1.3  christos     Status = AcpiUtWalkAmlResources (NULL, ObjDesc->Buffer.Pointer,
    665   1.1.1.6  christos         ObjDesc->Buffer.Length, NULL, (void **) EndTag);
    666       1.1    jruoho 
    667       1.1    jruoho     return_ACPI_STATUS (Status);
    668       1.1    jruoho }
    669