1 /*******************************************************************************
3 * Module Name: utresrc - Resource managment 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/amlresrc.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utmisc")
51 * Base sizes of the raw AML resource descriptors, indexed by resource type.
52 * Zero indicates a reserved (and therefore invalid) resource type.
54 const u8 acpi_gbl_resource_aml_sizes[] = {
55 /* Small descriptors */
61 ACPI_AML_SIZE_SMALL(struct aml_resource_irq),
62 ACPI_AML_SIZE_SMALL(struct aml_resource_dma),
63 ACPI_AML_SIZE_SMALL(struct aml_resource_start_dependent),
64 ACPI_AML_SIZE_SMALL(struct aml_resource_end_dependent),
65 ACPI_AML_SIZE_SMALL(struct aml_resource_io),
66 ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_io),
71 ACPI_AML_SIZE_SMALL(struct aml_resource_vendor_small),
72 ACPI_AML_SIZE_SMALL(struct aml_resource_end_tag),
74 /* Large descriptors */
77 ACPI_AML_SIZE_LARGE(struct aml_resource_memory24),
78 ACPI_AML_SIZE_LARGE(struct aml_resource_generic_register),
80 ACPI_AML_SIZE_LARGE(struct aml_resource_vendor_large),
81 ACPI_AML_SIZE_LARGE(struct aml_resource_memory32),
82 ACPI_AML_SIZE_LARGE(struct aml_resource_fixed_memory32),
83 ACPI_AML_SIZE_LARGE(struct aml_resource_address32),
84 ACPI_AML_SIZE_LARGE(struct aml_resource_address16),
85 ACPI_AML_SIZE_LARGE(struct aml_resource_extended_irq),
86 ACPI_AML_SIZE_LARGE(struct aml_resource_address64),
87 ACPI_AML_SIZE_LARGE(struct aml_resource_extended_address64)
91 * Resource types, used to validate the resource length field.
92 * The length of fixed-length types must match exactly, variable
93 * lengths must meet the minimum required length, etc.
94 * Zero indicates a reserved (and therefore invalid) resource type.
96 static const u8 acpi_gbl_resource_types[] = {
97 /* Small descriptors */
103 ACPI_SMALL_VARIABLE_LENGTH,
105 ACPI_SMALL_VARIABLE_LENGTH,
113 ACPI_VARIABLE_LENGTH,
116 /* Large descriptors */
122 ACPI_VARIABLE_LENGTH,
125 ACPI_VARIABLE_LENGTH,
126 ACPI_VARIABLE_LENGTH,
127 ACPI_VARIABLE_LENGTH,
128 ACPI_VARIABLE_LENGTH,
132 /*******************************************************************************
134 * FUNCTION: acpi_ut_validate_resource
136 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
137 * return_index - Where the resource index is returned. NULL
138 * if the index is not required.
140 * RETURN: Status, and optionally the Index into the global resource tables
142 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
143 * Type and Resource Length. Returns an index into the global
144 * resource information/dispatch tables for later use.
146 ******************************************************************************/
148 acpi_status acpi_ut_validate_resource(void *aml, u8 * return_index)
152 acpi_rs_length resource_length;
153 acpi_rs_length minimum_resource_length;
155 ACPI_FUNCTION_ENTRY();
158 * 1) Validate the resource_type field (Byte 0)
160 resource_type = ACPI_GET8(aml);
163 * Byte 0 contains the descriptor name (Resource Type)
164 * Examine the large/small bit in the resource header
166 if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
167 /* Verify the large resource type (name) against the max */
169 if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
170 return (AE_AML_INVALID_RESOURCE_TYPE);
174 * Large Resource Type -- bits 6:0 contain the name
175 * Translate range 0x80-0x8B to index range 0x10-0x1B
177 resource_index = (u8) (resource_type - 0x70);
180 * Small Resource Type -- bits 6:3 contain the name
181 * Shift range to index range 0x00-0x0F
183 resource_index = (u8)
184 ((resource_type & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
187 /* Check validity of the resource type, zero indicates name is invalid */
189 if (!acpi_gbl_resource_types[resource_index]) {
190 return (AE_AML_INVALID_RESOURCE_TYPE);
194 * 2) Validate the resource_length field. This ensures that the length
195 * is at least reasonable, and guarantees that it is non-zero.
197 resource_length = acpi_ut_get_resource_length(aml);
198 minimum_resource_length = acpi_gbl_resource_aml_sizes[resource_index];
200 /* Validate based upon the type of resource - fixed length or variable */
202 switch (acpi_gbl_resource_types[resource_index]) {
203 case ACPI_FIXED_LENGTH:
205 /* Fixed length resource, length must match exactly */
207 if (resource_length != minimum_resource_length) {
208 return (AE_AML_BAD_RESOURCE_LENGTH);
212 case ACPI_VARIABLE_LENGTH:
214 /* Variable length resource, length must be at least the minimum */
216 if (resource_length < minimum_resource_length) {
217 return (AE_AML_BAD_RESOURCE_LENGTH);
221 case ACPI_SMALL_VARIABLE_LENGTH:
223 /* Small variable length resource, length can be (Min) or (Min-1) */
225 if ((resource_length > minimum_resource_length) ||
226 (resource_length < (minimum_resource_length - 1))) {
227 return (AE_AML_BAD_RESOURCE_LENGTH);
233 /* Shouldn't happen (because of validation earlier), but be sure */
235 return (AE_AML_INVALID_RESOURCE_TYPE);
238 /* Optionally return the resource table index */
241 *return_index = resource_index;
247 /*******************************************************************************
249 * FUNCTION: acpi_ut_get_resource_type
251 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
253 * RETURN: The Resource Type with no extraneous bits (except the
254 * Large/Small descriptor bit -- this is left alone)
256 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
257 * a resource descriptor.
259 ******************************************************************************/
261 u8 acpi_ut_get_resource_type(void *aml)
263 ACPI_FUNCTION_ENTRY();
266 * Byte 0 contains the descriptor name (Resource Type)
267 * Examine the large/small bit in the resource header
269 if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
270 /* Large Resource Type -- bits 6:0 contain the name */
272 return (ACPI_GET8(aml));
274 /* Small Resource Type -- bits 6:3 contain the name */
276 return ((u8) (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
280 /*******************************************************************************
282 * FUNCTION: acpi_ut_get_resource_length
284 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
286 * RETURN: Byte Length
288 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
289 * definition, this does not include the size of the descriptor
290 * header or the length field itself.
292 ******************************************************************************/
294 u16 acpi_ut_get_resource_length(void *aml)
296 acpi_rs_length resource_length;
298 ACPI_FUNCTION_ENTRY();
301 * Byte 0 contains the descriptor name (Resource Type)
302 * Examine the large/small bit in the resource header
304 if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
305 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
307 ACPI_MOVE_16_TO_16(&resource_length, ACPI_ADD_PTR(u8, aml, 1));
310 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
312 resource_length = (u16) (ACPI_GET8(aml) &
313 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
316 return (resource_length);
319 /*******************************************************************************
321 * FUNCTION: acpi_ut_get_resource_header_length
323 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
325 * RETURN: Length of the AML header (depends on large/small descriptor)
327 * DESCRIPTION: Get the length of the header for this resource.
329 ******************************************************************************/
331 u8 acpi_ut_get_resource_header_length(void *aml)
333 ACPI_FUNCTION_ENTRY();
335 /* Examine the large/small bit in the resource header */
337 if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
338 return (sizeof(struct aml_resource_large_header));
340 return (sizeof(struct aml_resource_small_header));
344 /*******************************************************************************
346 * FUNCTION: acpi_ut_get_descriptor_length
348 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
350 * RETURN: Byte length
352 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
353 * length of the descriptor header and the length field itself.
354 * Used to walk descriptor lists.
356 ******************************************************************************/
358 u32 acpi_ut_get_descriptor_length(void *aml)
360 ACPI_FUNCTION_ENTRY();
363 * Get the Resource Length (does not include header length) and add
364 * the header length (depends on if this is a small or large resource)
366 return (acpi_ut_get_resource_length(aml) +
367 acpi_ut_get_resource_header_length(aml));
370 /*******************************************************************************
372 * FUNCTION: acpi_ut_get_resource_end_tag
374 * PARAMETERS: obj_desc - The resource template buffer object
375 * end_tag - Where the pointer to the end_tag is returned
377 * RETURN: Status, pointer to the end tag
379 * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
381 ******************************************************************************/
384 acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc,
391 ACPI_FUNCTION_TRACE("ut_get_resource_end_tag");
393 /* Get start and end pointers */
395 aml = obj_desc->buffer.pointer;
396 end_aml = aml + obj_desc->buffer.length;
398 /* Walk the resource template, one descriptor per iteration */
400 while (aml < end_aml) {
401 /* Validate the Resource Type and Resource Length */
403 status = acpi_ut_validate_resource(aml, NULL);
404 if (ACPI_FAILURE(status)) {
405 return_ACPI_STATUS(status);
408 /* end_tag resource indicates the end of the resource template */
410 if (acpi_ut_get_resource_type(aml) ==
411 ACPI_RESOURCE_NAME_END_TAG) {
412 /* Return the pointer to the end_tag */
415 return_ACPI_STATUS(AE_OK);
419 * Point to the next resource descriptor in the AML buffer. The
420 * descriptor length is guaranteed to be non-zero by resource
423 aml += acpi_ut_get_descriptor_length(aml);
426 /* Did not find an end_tag resource descriptor */
428 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);