1 /*******************************************************************************
3 * Module Name: utresrc - Resource managment utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2006, 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")
50 #if defined(ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
52 * Strings used to decode resource descriptors.
53 * Used by both the disasssembler and the debugger resource dump routines
55 const char *acpi_gbl_BMdecode[2] = {
60 const char *acpi_gbl_config_decode[4] = {
61 "0 - Good Configuration",
62 "1 - Acceptable Configuration",
63 "2 - Suboptimal Configuration",
64 "3 - ***Invalid Configuration***",
67 const char *acpi_gbl_consume_decode[2] = {
72 const char *acpi_gbl_DECdecode[2] = {
77 const char *acpi_gbl_HEdecode[2] = {
82 const char *acpi_gbl_io_decode[2] = {
87 const char *acpi_gbl_LLdecode[2] = {
92 const char *acpi_gbl_max_decode[2] = {
97 const char *acpi_gbl_MEMdecode[4] = {
104 const char *acpi_gbl_min_decode[2] = {
109 const char *acpi_gbl_MTPdecode[4] = {
110 "address_range_memory",
111 "address_range_reserved",
112 "address_range_aCPI",
116 const char *acpi_gbl_RNGdecode[4] = {
118 "non_iSAonly_ranges",
123 const char *acpi_gbl_RWdecode[2] = {
128 const char *acpi_gbl_SHRdecode[2] = {
133 const char *acpi_gbl_SIZdecode[4] = {
140 const char *acpi_gbl_TRSdecode[2] = {
145 const char *acpi_gbl_TTPdecode[2] = {
150 const char *acpi_gbl_TYPdecode[4] = {
160 * Base sizes of the raw AML resource descriptors, indexed by resource type.
161 * Zero indicates a reserved (and therefore invalid) resource type.
163 const u8 acpi_gbl_resource_aml_sizes[] = {
164 /* Small descriptors */
170 ACPI_AML_SIZE_SMALL(struct aml_resource_irq),
171 ACPI_AML_SIZE_SMALL(struct aml_resource_dma),
172 ACPI_AML_SIZE_SMALL(struct aml_resource_start_dependent),
173 ACPI_AML_SIZE_SMALL(struct aml_resource_end_dependent),
174 ACPI_AML_SIZE_SMALL(struct aml_resource_io),
175 ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_io),
180 ACPI_AML_SIZE_SMALL(struct aml_resource_vendor_small),
181 ACPI_AML_SIZE_SMALL(struct aml_resource_end_tag),
183 /* Large descriptors */
186 ACPI_AML_SIZE_LARGE(struct aml_resource_memory24),
187 ACPI_AML_SIZE_LARGE(struct aml_resource_generic_register),
189 ACPI_AML_SIZE_LARGE(struct aml_resource_vendor_large),
190 ACPI_AML_SIZE_LARGE(struct aml_resource_memory32),
191 ACPI_AML_SIZE_LARGE(struct aml_resource_fixed_memory32),
192 ACPI_AML_SIZE_LARGE(struct aml_resource_address32),
193 ACPI_AML_SIZE_LARGE(struct aml_resource_address16),
194 ACPI_AML_SIZE_LARGE(struct aml_resource_extended_irq),
195 ACPI_AML_SIZE_LARGE(struct aml_resource_address64),
196 ACPI_AML_SIZE_LARGE(struct aml_resource_extended_address64)
200 * Resource types, used to validate the resource length field.
201 * The length of fixed-length types must match exactly, variable
202 * lengths must meet the minimum required length, etc.
203 * Zero indicates a reserved (and therefore invalid) resource type.
205 static const u8 acpi_gbl_resource_types[] = {
206 /* Small descriptors */
212 ACPI_SMALL_VARIABLE_LENGTH,
214 ACPI_SMALL_VARIABLE_LENGTH,
222 ACPI_VARIABLE_LENGTH,
225 /* Large descriptors */
231 ACPI_VARIABLE_LENGTH,
234 ACPI_VARIABLE_LENGTH,
235 ACPI_VARIABLE_LENGTH,
236 ACPI_VARIABLE_LENGTH,
237 ACPI_VARIABLE_LENGTH,
241 /*******************************************************************************
243 * FUNCTION: acpi_ut_validate_resource
245 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
246 * return_index - Where the resource index is returned. NULL
247 * if the index is not required.
249 * RETURN: Status, and optionally the Index into the global resource tables
251 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
252 * Type and Resource Length. Returns an index into the global
253 * resource information/dispatch tables for later use.
255 ******************************************************************************/
257 acpi_status acpi_ut_validate_resource(void *aml, u8 * return_index)
261 acpi_rs_length resource_length;
262 acpi_rs_length minimum_resource_length;
264 ACPI_FUNCTION_ENTRY();
267 * 1) Validate the resource_type field (Byte 0)
269 resource_type = ACPI_GET8(aml);
272 * Byte 0 contains the descriptor name (Resource Type)
273 * Examine the large/small bit in the resource header
275 if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
276 /* Verify the large resource type (name) against the max */
278 if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
279 return (AE_AML_INVALID_RESOURCE_TYPE);
283 * Large Resource Type -- bits 6:0 contain the name
284 * Translate range 0x80-0x8B to index range 0x10-0x1B
286 resource_index = (u8) (resource_type - 0x70);
289 * Small Resource Type -- bits 6:3 contain the name
290 * Shift range to index range 0x00-0x0F
292 resource_index = (u8)
293 ((resource_type & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
296 /* Check validity of the resource type, zero indicates name is invalid */
298 if (!acpi_gbl_resource_types[resource_index]) {
299 return (AE_AML_INVALID_RESOURCE_TYPE);
303 * 2) Validate the resource_length field. This ensures that the length
304 * is at least reasonable, and guarantees that it is non-zero.
306 resource_length = acpi_ut_get_resource_length(aml);
307 minimum_resource_length = acpi_gbl_resource_aml_sizes[resource_index];
309 /* Validate based upon the type of resource - fixed length or variable */
311 switch (acpi_gbl_resource_types[resource_index]) {
312 case ACPI_FIXED_LENGTH:
314 /* Fixed length resource, length must match exactly */
316 if (resource_length != minimum_resource_length) {
317 return (AE_AML_BAD_RESOURCE_LENGTH);
321 case ACPI_VARIABLE_LENGTH:
323 /* Variable length resource, length must be at least the minimum */
325 if (resource_length < minimum_resource_length) {
326 return (AE_AML_BAD_RESOURCE_LENGTH);
330 case ACPI_SMALL_VARIABLE_LENGTH:
332 /* Small variable length resource, length can be (Min) or (Min-1) */
334 if ((resource_length > minimum_resource_length) ||
335 (resource_length < (minimum_resource_length - 1))) {
336 return (AE_AML_BAD_RESOURCE_LENGTH);
342 /* Shouldn't happen (because of validation earlier), but be sure */
344 return (AE_AML_INVALID_RESOURCE_TYPE);
347 /* Optionally return the resource table index */
350 *return_index = resource_index;
356 /*******************************************************************************
358 * FUNCTION: acpi_ut_get_resource_type
360 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
362 * RETURN: The Resource Type with no extraneous bits (except the
363 * Large/Small descriptor bit -- this is left alone)
365 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
366 * a resource descriptor.
368 ******************************************************************************/
370 u8 acpi_ut_get_resource_type(void *aml)
372 ACPI_FUNCTION_ENTRY();
375 * Byte 0 contains the descriptor name (Resource Type)
376 * Examine the large/small bit in the resource header
378 if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
379 /* Large Resource Type -- bits 6:0 contain the name */
381 return (ACPI_GET8(aml));
383 /* Small Resource Type -- bits 6:3 contain the name */
385 return ((u8) (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
389 /*******************************************************************************
391 * FUNCTION: acpi_ut_get_resource_length
393 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
395 * RETURN: Byte Length
397 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
398 * definition, this does not include the size of the descriptor
399 * header or the length field itself.
401 ******************************************************************************/
403 u16 acpi_ut_get_resource_length(void *aml)
405 acpi_rs_length resource_length;
407 ACPI_FUNCTION_ENTRY();
410 * Byte 0 contains the descriptor name (Resource Type)
411 * Examine the large/small bit in the resource header
413 if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
414 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
416 ACPI_MOVE_16_TO_16(&resource_length, ACPI_ADD_PTR(u8, aml, 1));
419 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
421 resource_length = (u16) (ACPI_GET8(aml) &
422 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
425 return (resource_length);
428 /*******************************************************************************
430 * FUNCTION: acpi_ut_get_resource_header_length
432 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
434 * RETURN: Length of the AML header (depends on large/small descriptor)
436 * DESCRIPTION: Get the length of the header for this resource.
438 ******************************************************************************/
440 u8 acpi_ut_get_resource_header_length(void *aml)
442 ACPI_FUNCTION_ENTRY();
444 /* Examine the large/small bit in the resource header */
446 if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
447 return (sizeof(struct aml_resource_large_header));
449 return (sizeof(struct aml_resource_small_header));
453 /*******************************************************************************
455 * FUNCTION: acpi_ut_get_descriptor_length
457 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
459 * RETURN: Byte length
461 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
462 * length of the descriptor header and the length field itself.
463 * Used to walk descriptor lists.
465 ******************************************************************************/
467 u32 acpi_ut_get_descriptor_length(void *aml)
469 ACPI_FUNCTION_ENTRY();
472 * Get the Resource Length (does not include header length) and add
473 * the header length (depends on if this is a small or large resource)
475 return (acpi_ut_get_resource_length(aml) +
476 acpi_ut_get_resource_header_length(aml));
479 /*******************************************************************************
481 * FUNCTION: acpi_ut_get_resource_end_tag
483 * PARAMETERS: obj_desc - The resource template buffer object
484 * end_tag - Where the pointer to the end_tag is returned
486 * RETURN: Status, pointer to the end tag
488 * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
489 * Note: allows a buffer length of zero.
491 ******************************************************************************/
494 acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc,
501 ACPI_FUNCTION_TRACE("ut_get_resource_end_tag");
503 /* Get start and end pointers */
505 aml = obj_desc->buffer.pointer;
506 end_aml = aml + obj_desc->buffer.length;
508 /* Allow a buffer length of zero */
510 if (!obj_desc->buffer.length) {
512 return_ACPI_STATUS(AE_OK);
515 /* Walk the resource template, one descriptor per iteration */
517 while (aml < end_aml) {
518 /* Validate the Resource Type and Resource Length */
520 status = acpi_ut_validate_resource(aml, NULL);
521 if (ACPI_FAILURE(status)) {
522 return_ACPI_STATUS(status);
525 /* end_tag resource indicates the end of the resource template */
527 if (acpi_ut_get_resource_type(aml) ==
528 ACPI_RESOURCE_NAME_END_TAG) {
530 * There must be at least one more byte in the buffer for
531 * the 2nd byte of the end_tag
533 if ((aml + 1) >= end_aml) {
534 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
537 /* Return the pointer to the end_tag */
540 return_ACPI_STATUS(AE_OK);
544 * Point to the next resource descriptor in the AML buffer. The
545 * descriptor length is guaranteed to be non-zero by resource
548 aml += acpi_ut_get_descriptor_length(aml);
551 /* Did not find an end_tag resource descriptor */
553 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);