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 ACPI_GET_RESOURCE_HANDLER
acpi_rs_get_resource_handler(u8 resource_type
);
53 static acpi_status
acpi_rs_validate_resource_length(union aml_resource
*aml
);
55 /*******************************************************************************
57 * FUNCTION: acpi_rs_validate_resource_length
59 * PARAMETERS: Aml - Pointer to the AML resource descriptor
61 * RETURN: Status - AE_OK if the resource length appears valid
63 * DESCRIPTION: Validate the resource_length. Fixed-length descriptors must
64 * have the exact length; variable-length descriptors must be
65 * at least as long as the minimum. Certain Small descriptors
66 * can vary in size by at most one byte.
68 ******************************************************************************/
70 static acpi_status
acpi_rs_validate_resource_length(union aml_resource
*aml
)
72 struct acpi_resource_info
*resource_info
;
73 u16 minimum_aml_resource_length
;
76 ACPI_FUNCTION_ENTRY();
78 /* Get the size and type info about this resource descriptor */
81 acpi_rs_get_resource_info(aml
->small_header
.descriptor_type
);
83 return (AE_AML_INVALID_RESOURCE_TYPE
);
86 resource_length
= acpi_rs_get_resource_length(aml
);
87 minimum_aml_resource_length
=
88 resource_info
->minimum_aml_resource_length
;
90 /* Validate based upon the type of resource, fixed length or variable */
92 if (resource_info
->length_type
== ACPI_FIXED_LENGTH
) {
93 /* Fixed length resource, length must match exactly */
95 if (resource_length
!= minimum_aml_resource_length
) {
96 return (AE_AML_BAD_RESOURCE_LENGTH
);
98 } else if (resource_info
->length_type
== ACPI_VARIABLE_LENGTH
) {
99 /* Variable length resource, must be at least the minimum */
101 if (resource_length
< minimum_aml_resource_length
) {
102 return (AE_AML_BAD_RESOURCE_LENGTH
);
105 /* Small variable length resource, allowed to be (Min) or (Min-1) */
107 if ((resource_length
> minimum_aml_resource_length
) ||
108 (resource_length
< (minimum_aml_resource_length
- 1))) {
109 return (AE_AML_BAD_RESOURCE_LENGTH
);
116 /*******************************************************************************
118 * FUNCTION: acpi_rs_get_resource_handler
120 * PARAMETERS: resource_type - Byte 0 of a resource descriptor
122 * RETURN: Pointer to the resource conversion handler
124 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
125 * a resource descriptor.
127 ******************************************************************************/
129 static ACPI_GET_RESOURCE_HANDLER
acpi_rs_get_resource_handler(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
;
178 u32 descriptor_length
;
179 ACPI_GET_RESOURCE_HANDLER handler
;
181 ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
183 /* Loop until end-of-buffer or an end_tag is found */
185 while (bytes_parsed
< aml_buffer_length
) {
186 /* Get the handler associated with this Descriptor Type */
188 handler
= acpi_rs_get_resource_handler(*aml_buffer
);
190 /* No handler indicates invalid resource type */
192 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE
);
196 acpi_rs_get_resource_length(ACPI_CAST_PTR
201 acpi_rs_get_descriptor_length(ACPI_CAST_PTR
206 * Perform limited validation of the resource length, based upon
207 * what we know about the resource type
210 acpi_rs_validate_resource_length(ACPI_CAST_PTR
213 if (ACPI_FAILURE(status
)) {
214 return_ACPI_STATUS(status
);
217 /* Convert a byte stream resource to local resource struct */
219 status
= handler(ACPI_CAST_PTR(union aml_resource
, aml_buffer
),
221 ACPI_CAST_PTR(struct acpi_resource
, buffer
));
222 if (ACPI_FAILURE(status
)) {
223 ACPI_REPORT_ERROR(("Could not convert AML resource (type %X) to resource, %s\n", *aml_buffer
, acpi_format_exception(status
)));
224 return_ACPI_STATUS(status
);
227 /* Set the aligned length of the new resource descriptor */
229 resource
= ACPI_CAST_PTR(struct acpi_resource
, buffer
);
231 (u32
) ACPI_ALIGN_RESOURCE_SIZE(resource
->length
);
233 /* Normal exit on completion of an end_tag resource descriptor */
235 if (acpi_rs_get_resource_type(*aml_buffer
) ==
236 ACPI_RESOURCE_NAME_END_TAG
) {
237 return_ACPI_STATUS(AE_OK
);
240 /* Update counter and point to the next input resource */
242 bytes_parsed
+= descriptor_length
;
243 aml_buffer
+= descriptor_length
;
245 /* Point to the next structure in the output buffer */
247 buffer
+= resource
->length
;
250 /* Completed buffer, but did not find an end_tag resource descriptor */
252 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG
);
255 /*******************************************************************************
257 * FUNCTION: acpi_rs_convert_resources_to_aml
259 * PARAMETERS: Resource - Pointer to the resource linked list
260 * aml_size_needed - Calculated size of the byte stream
261 * needed from calling acpi_rs_get_aml_length()
262 * The size of the output_buffer is
263 * guaranteed to be >= aml_size_needed
264 * output_buffer - Pointer to the buffer that will
265 * contain the byte stream
269 * DESCRIPTION: Takes the resource linked list and parses it, creating a
270 * byte stream of resources in the caller's output buffer
272 ******************************************************************************/
275 acpi_rs_convert_resources_to_aml(struct acpi_resource
*resource
,
276 acpi_size aml_size_needed
, u8
* output_buffer
)
278 u8
*aml_buffer
= output_buffer
;
281 ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
283 /* Convert each resource descriptor in the list */
286 /* Validate Resource Descriptor Type before dispatch */
288 if (resource
->type
> ACPI_RESOURCE_TYPE_MAX
) {
289 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
290 "Invalid descriptor type (%X) in resource list\n",
292 return_ACPI_STATUS(AE_BAD_DATA
);
295 /* Perform the conversion per resource type */
298 acpi_gbl_set_resource_dispatch
[resource
->type
] (resource
,
303 if (ACPI_FAILURE(status
)) {
304 ACPI_REPORT_ERROR(("Could not convert resource (type %X) to AML, %s\n", resource
->type
, acpi_format_exception(status
)));
305 return_ACPI_STATUS(status
);
308 /* Perform final sanity check on the new AML resource descriptor */
311 acpi_rs_validate_resource_length(ACPI_CAST_PTR
314 if (ACPI_FAILURE(status
)) {
315 return_ACPI_STATUS(status
);
318 /* Check for end-of-list, normal exit */
320 if (resource
->type
== ACPI_RESOURCE_TYPE_END_TAG
) {
321 /* An End Tag indicates the end of the input Resource Template */
323 return_ACPI_STATUS(AE_OK
);
326 /* Extract the total length of the new descriptor */
327 /* Set the aml_buffer to point to the next (output) resource descriptor */
330 acpi_rs_get_descriptor_length(ACPI_CAST_PTR
334 /* Point to the next input resource descriptor */
337 ACPI_PTR_ADD(struct acpi_resource
, resource
,