1 /*******************************************************************************
3 * Module Name: rslist - Linked list utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <acpi/acpi.h>
45 #include <acpi/acresrc.h>
47 #define _COMPONENT ACPI_RESOURCES
48 ACPI_MODULE_NAME("rslist")
50 /* Local prototypes */
51 static struct acpi_rsconvert_info *acpi_rs_get_conversion_info(u8
54 static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml);
56 /*******************************************************************************
58 * FUNCTION: acpi_rs_validate_resource_length
60 * PARAMETERS: Aml - Pointer to the AML resource descriptor
62 * RETURN: Status - AE_OK if the resource length appears valid
64 * DESCRIPTION: Validate the resource_length. Fixed-length descriptors must
65 * have the exact length; variable-length descriptors must be
66 * at least as long as the minimum. Certain Small descriptors
67 * can vary in size by at most one byte.
69 ******************************************************************************/
71 static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml)
73 struct acpi_resource_info *resource_info;
74 u16 minimum_aml_resource_length;
77 ACPI_FUNCTION_ENTRY();
79 /* Get the size and type info about this resource descriptor */
82 acpi_rs_get_resource_info(aml->small_header.descriptor_type);
84 return (AE_AML_INVALID_RESOURCE_TYPE);
87 resource_length = acpi_ut_get_resource_length(aml);
88 minimum_aml_resource_length =
89 resource_info->minimum_aml_resource_length;
91 /* Validate based upon the type of resource, fixed length or variable */
93 if (resource_info->length_type == ACPI_FIXED_LENGTH) {
94 /* Fixed length resource, length must match exactly */
96 if (resource_length != minimum_aml_resource_length) {
97 return (AE_AML_BAD_RESOURCE_LENGTH);
99 } else if (resource_info->length_type == ACPI_VARIABLE_LENGTH) {
100 /* Variable length resource, must be at least the minimum */
102 if (resource_length < minimum_aml_resource_length) {
103 return (AE_AML_BAD_RESOURCE_LENGTH);
106 /* Small variable length resource, allowed to be (Min) or (Min-1) */
108 if ((resource_length > minimum_aml_resource_length) ||
109 (resource_length < (minimum_aml_resource_length - 1))) {
110 return (AE_AML_BAD_RESOURCE_LENGTH);
117 /*******************************************************************************
119 * FUNCTION: acpi_rs_get_conversion_info
121 * PARAMETERS: resource_type - Byte 0 of a resource descriptor
123 * RETURN: Pointer to the resource conversion info table
125 * DESCRIPTION: Get the conversion table associated with this resource type
127 ******************************************************************************/
129 static struct acpi_rsconvert_info *acpi_rs_get_conversion_info(u8 resource_type)
131 ACPI_FUNCTION_ENTRY();
133 /* Determine if this is a small or large resource */
135 if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
136 /* Large Resource Type -- bits 6:0 contain the name */
138 if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
142 return (acpi_gbl_lg_get_resource_dispatch[(resource_type &
143 ACPI_RESOURCE_NAME_LARGE_MASK)]);
145 /* Small Resource Type -- bits 6:3 contain the name */
147 return (acpi_gbl_sm_get_resource_dispatch[((resource_type &
148 ACPI_RESOURCE_NAME_SMALL_MASK)
153 /*******************************************************************************
155 * FUNCTION: acpi_rs_convert_aml_to_resources
157 * PARAMETERS: aml_buffer - Pointer to the resource byte stream
158 * aml_buffer_length - Length of aml_buffer
159 * output_buffer - Pointer to the buffer that will
160 * contain the output structures
164 * DESCRIPTION: Takes the resource byte stream and parses it, creating a
165 * linked list of resources in the caller's output buffer
167 ******************************************************************************/
170 acpi_rs_convert_aml_to_resources(u8 * aml_buffer,
171 u32 aml_buffer_length, u8 * output_buffer)
173 u8 *buffer = output_buffer;
175 acpi_size bytes_parsed = 0;
176 struct acpi_resource *resource;
177 acpi_rsdesc_size descriptor_length;
178 struct acpi_rsconvert_info *info;
180 ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
182 /* Loop until end-of-buffer or an end_tag is found */
184 while (bytes_parsed < aml_buffer_length) {
185 /* Get the conversion table associated with this Descriptor Type */
187 info = acpi_rs_get_conversion_info(*aml_buffer);
189 /* No table indicates an invalid resource type */
191 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
194 descriptor_length = acpi_ut_get_descriptor_length(aml_buffer);
197 * Perform limited validation of the resource length, based upon
198 * what we know about the resource type
201 acpi_rs_validate_resource_length(ACPI_CAST_PTR
204 if (ACPI_FAILURE(status)) {
205 return_ACPI_STATUS(status);
208 /* Convert the AML byte stream resource to a local resource struct */
211 acpi_rs_convert_aml_to_resource(ACPI_CAST_PTR
212 (struct acpi_resource,
218 if (ACPI_FAILURE(status)) {
219 ACPI_REPORT_ERROR(("Could not convert AML resource (type %X) to resource, %s\n", *aml_buffer, acpi_format_exception(status)));
220 return_ACPI_STATUS(status);
223 /* Set the aligned length of the new resource descriptor */
225 resource = ACPI_CAST_PTR(struct acpi_resource, buffer);
227 (u32) ACPI_ALIGN_RESOURCE_SIZE(resource->length);
229 /* Normal exit on completion of an end_tag resource descriptor */
231 if (acpi_ut_get_resource_type(aml_buffer) ==
232 ACPI_RESOURCE_NAME_END_TAG) {
233 return_ACPI_STATUS(AE_OK);
236 /* Update counter and point to the next input resource */
238 bytes_parsed += descriptor_length;
239 aml_buffer += descriptor_length;
241 /* Point to the next structure in the output buffer */
243 buffer += resource->length;
246 /* Completed buffer, but did not find an end_tag resource descriptor */
248 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
251 /*******************************************************************************
253 * FUNCTION: acpi_rs_convert_resources_to_aml
255 * PARAMETERS: Resource - Pointer to the resource linked list
256 * aml_size_needed - Calculated size of the byte stream
257 * needed from calling acpi_rs_get_aml_length()
258 * The size of the output_buffer is
259 * guaranteed to be >= aml_size_needed
260 * output_buffer - Pointer to the buffer that will
261 * contain the byte stream
265 * DESCRIPTION: Takes the resource linked list and parses it, creating a
266 * byte stream of resources in the caller's output buffer
268 ******************************************************************************/
271 acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
272 acpi_size aml_size_needed, u8 * output_buffer)
274 u8 *aml_buffer = output_buffer;
275 u8 *end_aml_buffer = output_buffer + aml_size_needed;
278 ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
280 /* Walk the resource descriptor list, convert each descriptor */
282 while (aml_buffer < end_aml_buffer) {
283 /* Validate the Resource Type */
285 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
286 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
287 "Invalid descriptor type (%X) in resource list\n",
289 return_ACPI_STATUS(AE_BAD_DATA);
292 /* Perform the conversion */
294 status = acpi_rs_convert_resource_to_aml(resource,
298 acpi_gbl_set_resource_dispatch
300 if (ACPI_FAILURE(status)) {
301 ACPI_REPORT_ERROR(("Could not convert resource (type %X) to AML, %s\n", resource->type, acpi_format_exception(status)));
302 return_ACPI_STATUS(status);
305 /* Perform final sanity check on the new AML resource descriptor */
308 acpi_rs_validate_resource_length(ACPI_CAST_PTR
311 if (ACPI_FAILURE(status)) {
312 return_ACPI_STATUS(status);
315 /* Check for end-of-list, normal exit */
317 if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
318 /* An End Tag indicates the end of the input Resource Template */
320 return_ACPI_STATUS(AE_OK);
324 * Extract the total length of the new descriptor and set the
325 * aml_buffer to point to the next (output) resource descriptor
327 aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
329 /* Point to the next input resource descriptor */
332 ACPI_PTR_ADD(struct acpi_resource, resource,
335 /* Check for end-of-list, normal exit */
339 /* Completed buffer, but did not find an end_tag resource descriptor */
341 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);