1/*******************************************************************************
2 *
3 * Module Name: rslist - Linked list utilities
4 *
5 ******************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2023, 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 MERCHANTABILITY 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#include "acpi.h"
45#include "accommon.h"
46#include "acresrc.h"
47
48#define _COMPONENT          ACPI_RESOURCES
49        ACPI_MODULE_NAME    ("rslist")
50
51
52/*******************************************************************************
53 *
54 * FUNCTION:    AcpiRsConvertAmlToResources
55 *
56 * PARAMETERS:  ACPI_WALK_AML_CALLBACK
57 *              ResourcePtr             - Pointer to the buffer that will
58 *                                        contain the output structures
59 *
60 * RETURN:      Status
61 *
62 * DESCRIPTION: Convert an AML resource to an internal representation of the
63 *              resource that is aligned and easier to access.
64 *
65 ******************************************************************************/
66
67ACPI_STATUS
68AcpiRsConvertAmlToResources (
69    UINT8                   *Aml,
70    UINT32                  Length,
71    UINT32                  Offset,
72    UINT8                   ResourceIndex,
73    void                    **Context)
74{
75    ACPI_RESOURCE           **ResourcePtr = ACPI_CAST_INDIRECT_PTR (
76                                ACPI_RESOURCE, Context);
77    ACPI_RESOURCE           *Resource;
78    AML_RESOURCE            *AmlResource;
79    ACPI_RSCONVERT_INFO     *ConversionTable;
80    ACPI_STATUS             Status;
81
82
83    ACPI_FUNCTION_TRACE (RsConvertAmlToResources);
84
85
86    /*
87     * Check that the input buffer and all subsequent pointers into it
88     * are aligned on a native word boundary. Most important on IA64
89     */
90    Resource = *ResourcePtr;
91    if (ACPI_IS_MISALIGNED (Resource))
92    {
93        ACPI_WARNING ((AE_INFO,
94            "Misaligned resource pointer %p", Resource));
95    }
96
97    /* Get the appropriate conversion info table */
98
99    AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
100
101    if (AcpiUtGetResourceType (Aml) ==
102        ACPI_RESOURCE_NAME_SERIAL_BUS)
103    {
104        /* Avoid undefined behavior: member access within misaligned address */
105
106        AML_RESOURCE_COMMON_SERIALBUS CommonSerialBus;
107        memcpy(&CommonSerialBus, AmlResource, sizeof(CommonSerialBus));
108
109        if (CommonSerialBus.Type >
110            AML_RESOURCE_MAX_SERIALBUSTYPE)
111        {
112            ConversionTable = NULL;
113        }
114        else
115        {
116            /* This is an I2C, SPI, UART, or CSI2 SerialBus descriptor */
117
118            ConversionTable = AcpiGbl_ConvertResourceSerialBusDispatch [
119                CommonSerialBus.Type];
120        }
121    }
122    else
123    {
124        ConversionTable = AcpiGbl_GetResourceDispatch[ResourceIndex];
125    }
126
127    if (!ConversionTable)
128    {
129        ACPI_ERROR ((AE_INFO,
130            "Invalid/unsupported resource descriptor: Type 0x%2.2X",
131            ResourceIndex));
132        return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
133    }
134
135     /* Convert the AML byte stream resource to a local resource struct */
136
137    Status = AcpiRsConvertAmlToResource (
138        Resource, AmlResource, ConversionTable);
139    if (ACPI_FAILURE (Status))
140    {
141        ACPI_EXCEPTION ((AE_INFO, Status,
142            "Could not convert AML resource (Type 0x%X)", *Aml));
143        return_ACPI_STATUS (Status);
144    }
145
146    if (!Resource->Length)
147    {
148        ACPI_EXCEPTION ((AE_INFO, Status,
149            "Zero-length resource returned from RsConvertAmlToResource"));
150    }
151
152    ACPI_DEBUG_PRINT ((ACPI_DB_RESOURCES,
153        "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
154        AcpiUtGetResourceType (Aml), Length,
155        Resource->Length));
156
157    /* Point to the next structure in the output buffer */
158
159    *ResourcePtr = ACPI_NEXT_RESOURCE (Resource);
160    return_ACPI_STATUS (AE_OK);
161}
162
163
164/*******************************************************************************
165 *
166 * FUNCTION:    AcpiRsConvertResourcesToAml
167 *
168 * PARAMETERS:  Resource            - Pointer to the resource linked list
169 *              AmlSizeNeeded       - Calculated size of the byte stream
170 *                                    needed from calling AcpiRsGetAmlLength()
171 *                                    The size of the OutputBuffer is
172 *                                    guaranteed to be >= AmlSizeNeeded
173 *              OutputBuffer        - Pointer to the buffer that will
174 *                                    contain the byte stream
175 *
176 * RETURN:      Status
177 *
178 * DESCRIPTION: Takes the resource linked list and parses it, creating a
179 *              byte stream of resources in the caller's output buffer
180 *
181 ******************************************************************************/
182
183ACPI_STATUS
184AcpiRsConvertResourcesToAml (
185    ACPI_RESOURCE           *Resource,
186    ACPI_SIZE               AmlSizeNeeded,
187    UINT8                   *OutputBuffer)
188{
189    UINT8                   *Aml = OutputBuffer;
190    UINT8                   *EndAml = OutputBuffer + AmlSizeNeeded;
191    ACPI_RSCONVERT_INFO     *ConversionTable;
192    ACPI_STATUS             Status;
193
194
195    ACPI_FUNCTION_TRACE (RsConvertResourcesToAml);
196
197
198    /* Walk the resource descriptor list, convert each descriptor */
199
200    while (Aml < EndAml)
201    {
202        /* Validate the (internal) Resource Type */
203
204        if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
205        {
206            ACPI_ERROR ((AE_INFO,
207                "Invalid descriptor type (0x%X) in resource list",
208                Resource->Type));
209            return_ACPI_STATUS (AE_BAD_DATA);
210        }
211
212        /* Sanity check the length. It must not be zero, or we loop forever */
213
214        if (!Resource->Length)
215        {
216            ACPI_ERROR ((AE_INFO,
217                "Invalid zero length descriptor in resource list\n"));
218            return_ACPI_STATUS (AE_AML_BAD_RESOURCE_LENGTH);
219        }
220
221        /* Perform the conversion */
222
223        if (Resource->Type == ACPI_RESOURCE_TYPE_SERIAL_BUS)
224        {
225            if (Resource->Data.CommonSerialBus.Type >
226                AML_RESOURCE_MAX_SERIALBUSTYPE)
227            {
228                ConversionTable = NULL;
229            }
230            else
231            {
232                /* This is an I2C, SPI, UART or CSI2 SerialBus descriptor */
233
234                ConversionTable = AcpiGbl_ConvertResourceSerialBusDispatch[
235                    Resource->Data.CommonSerialBus.Type];
236            }
237        }
238        else
239        {
240            ConversionTable = AcpiGbl_SetResourceDispatch[Resource->Type];
241        }
242
243        if (!ConversionTable)
244        {
245            ACPI_ERROR ((AE_INFO,
246                "Invalid/unsupported resource descriptor: Type 0x%2.2X",
247                Resource->Type));
248            return_ACPI_STATUS (AE_AML_INVALID_RESOURCE_TYPE);
249        }
250
251        Status = AcpiRsConvertResourceToAml (Resource,
252            ACPI_CAST_PTR (AML_RESOURCE, Aml), ConversionTable);
253        if (ACPI_FAILURE (Status))
254        {
255            ACPI_EXCEPTION ((AE_INFO, Status,
256                "Could not convert resource (type 0x%X) to AML",
257                Resource->Type));
258            return_ACPI_STATUS (Status);
259        }
260
261        /* Perform final sanity check on the new AML resource descriptor */
262
263        Status = AcpiUtValidateResource (
264            NULL, ACPI_CAST_PTR (AML_RESOURCE, Aml), NULL);
265        if (ACPI_FAILURE (Status))
266        {
267            return_ACPI_STATUS (Status);
268        }
269
270        /* Check for end-of-list, normal exit */
271
272        if (Resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
273        {
274            /* An End Tag indicates the end of the input Resource Template */
275
276            return_ACPI_STATUS (AE_OK);
277        }
278
279        /*
280         * Extract the total length of the new descriptor and set the
281         * Aml to point to the next (output) resource descriptor
282         */
283        Aml += AcpiUtGetDescriptorLength (Aml);
284
285        /* Point to the next input resource descriptor */
286
287        Resource = ACPI_NEXT_RESOURCE (Resource);
288    }
289
290    /* Completed buffer, but did not find an EndTag resource descriptor */
291
292    return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
293}
294