rslist.c 11.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*******************************************************************************
 *
 * Module Name: rslist - Linked list utilities
 *
 ******************************************************************************/

/*
 * Copyright (C) 2000 - 2005, R. Byron Moore
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
#include <acpi/acresrc.h>

#define _COMPONENT          ACPI_RESOURCES
L
Len Brown 已提交
48
ACPI_MODULE_NAME("rslist")
L
Linus Torvalds 已提交
49

R
Robert Moore 已提交
50
/* Local prototypes */
B
Bob Moore 已提交
51 52
static struct acpi_rsconvert_info *acpi_rs_get_conversion_info(u8
							       resource_type);
R
Robert Moore 已提交
53

B
Bob Moore 已提交
54
static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml);
R
Robert Moore 已提交
55

L
Linus Torvalds 已提交
56 57
/*******************************************************************************
 *
B
Bob Moore 已提交
58
 * FUNCTION:    acpi_rs_validate_resource_length
L
Linus Torvalds 已提交
59
 *
B
Bob Moore 已提交
60
 * PARAMETERS:  Aml                 - Pointer to the AML resource descriptor
L
Linus Torvalds 已提交
61
 *
B
Bob Moore 已提交
62
 * RETURN:      Status - AE_OK if the resource length appears valid
L
Linus Torvalds 已提交
63
 *
B
Bob Moore 已提交
64 65 66 67
 * DESCRIPTION: Validate the resource_length. Fixed-length descriptors must
 *              have the exact length; variable-length descriptors must be
 *              at least as long as the minimum. Certain Small descriptors
 *              can vary in size by at most one byte.
L
Linus Torvalds 已提交
68 69 70
 *
 ******************************************************************************/

B
Bob Moore 已提交
71
static acpi_status acpi_rs_validate_resource_length(union aml_resource *aml)
R
Robert Moore 已提交
72
{
B
Bob Moore 已提交
73 74 75 76
	struct acpi_resource_info *resource_info;
	u16 minimum_aml_resource_length;
	u16 resource_length;

L
Len Brown 已提交
77
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
78

B
Bob Moore 已提交
79
	/* Get the size and type info about this resource descriptor */
R
Robert Moore 已提交
80

B
Bob Moore 已提交
81 82 83 84 85 86
	resource_info =
	    acpi_rs_get_resource_info(aml->small_header.descriptor_type);
	if (!resource_info) {
		return (AE_AML_INVALID_RESOURCE_TYPE);
	}

B
Bob Moore 已提交
87
	resource_length = acpi_ut_get_resource_length(aml);
B
Bob Moore 已提交
88 89 90 91 92 93 94
	minimum_aml_resource_length =
	    resource_info->minimum_aml_resource_length;

	/* Validate based upon the type of resource, fixed length or variable */

	if (resource_info->length_type == ACPI_FIXED_LENGTH) {
		/* Fixed length resource, length must match exactly */
R
Robert Moore 已提交
95

B
Bob Moore 已提交
96 97 98 99 100 101 102 103 104
		if (resource_length != minimum_aml_resource_length) {
			return (AE_AML_BAD_RESOURCE_LENGTH);
		}
	} else if (resource_info->length_type == ACPI_VARIABLE_LENGTH) {
		/* Variable length resource, must be at least the minimum */

		if (resource_length < minimum_aml_resource_length) {
			return (AE_AML_BAD_RESOURCE_LENGTH);
		}
R
Robert Moore 已提交
105
	} else {
B
Bob Moore 已提交
106
		/* Small variable length resource, allowed to be (Min) or (Min-1) */
R
Robert Moore 已提交
107

B
Bob Moore 已提交
108 109 110 111
		if ((resource_length > minimum_aml_resource_length) ||
		    (resource_length < (minimum_aml_resource_length - 1))) {
			return (AE_AML_BAD_RESOURCE_LENGTH);
		}
R
Robert Moore 已提交
112
	}
B
Bob Moore 已提交
113 114

	return (AE_OK);
R
Robert Moore 已提交
115 116 117 118
}

/*******************************************************************************
 *
B
Bob Moore 已提交
119
 * FUNCTION:    acpi_rs_get_conversion_info
R
Robert Moore 已提交
120 121 122
 *
 * PARAMETERS:  resource_type       - Byte 0 of a resource descriptor
 *
B
Bob Moore 已提交
123
 * RETURN:      Pointer to the resource conversion info table
R
Robert Moore 已提交
124
 *
B
Bob Moore 已提交
125
 * DESCRIPTION: Get the conversion table associated with this resource type
R
Robert Moore 已提交
126 127
 *
 ******************************************************************************/
L
Linus Torvalds 已提交
128

B
Bob Moore 已提交
129
static struct acpi_rsconvert_info *acpi_rs_get_conversion_info(u8 resource_type)
R
Robert Moore 已提交
130 131
{
	ACPI_FUNCTION_ENTRY();
R
Robert Moore 已提交
132

R
Robert Moore 已提交
133
	/* Determine if this is a small or large resource */
L
Linus Torvalds 已提交
134

B
Bob Moore 已提交
135
	if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
R
Robert Moore 已提交
136
		/* Large Resource Type -- bits 6:0 contain the name */
L
Linus Torvalds 已提交
137

B
Bob Moore 已提交
138
		if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
R
Robert Moore 已提交
139 140
			return (NULL);
		}
R
Robert Moore 已提交
141

B
Bob Moore 已提交
142 143
		return (acpi_gbl_lg_get_resource_dispatch[(resource_type &
							   ACPI_RESOURCE_NAME_LARGE_MASK)]);
R
Robert Moore 已提交
144 145
	} else {
		/* Small Resource Type -- bits 6:3 contain the name */
L
Linus Torvalds 已提交
146

B
Bob Moore 已提交
147 148 149
		return (acpi_gbl_sm_get_resource_dispatch[((resource_type &
							    ACPI_RESOURCE_NAME_SMALL_MASK)
							   >> 3)]);
L
Linus Torvalds 已提交
150 151 152 153 154
	}
}

/*******************************************************************************
 *
B
Bob Moore 已提交
155
 * FUNCTION:    acpi_rs_convert_aml_to_resources
L
Linus Torvalds 已提交
156
 *
B
Bob Moore 已提交
157 158 159 160
 * PARAMETERS:  aml_buffer          - Pointer to the resource byte stream
 *              aml_buffer_length   - Length of aml_buffer
 *              output_buffer       - Pointer to the buffer that will
 *                                    contain the output structures
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Takes the resource byte stream and parses it, creating a
 *              linked list of resources in the caller's output buffer
 *
 ******************************************************************************/

acpi_status
B
Bob Moore 已提交
170 171
acpi_rs_convert_aml_to_resources(u8 * aml_buffer,
				 u32 aml_buffer_length, u8 * output_buffer)
L
Linus Torvalds 已提交
172
{
R
Robert Moore 已提交
173
	u8 *buffer = output_buffer;
L
Len Brown 已提交
174 175 176
	acpi_status status;
	acpi_size bytes_parsed = 0;
	struct acpi_resource *resource;
B
Bob Moore 已提交
177 178
	acpi_rsdesc_size descriptor_length;
	struct acpi_rsconvert_info *info;
L
Len Brown 已提交
179

B
Bob Moore 已提交
180
	ACPI_FUNCTION_TRACE("rs_convert_aml_to_resources");
L
Len Brown 已提交
181

R
Robert Moore 已提交
182 183
	/* Loop until end-of-buffer or an end_tag is found */

B
Bob Moore 已提交
184
	while (bytes_parsed < aml_buffer_length) {
B
Bob Moore 已提交
185
		/* Get the conversion table associated with this Descriptor Type */
R
Robert Moore 已提交
186

B
Bob Moore 已提交
187 188 189
		info = acpi_rs_get_conversion_info(*aml_buffer);
		if (!info) {
			/* No table indicates an invalid resource type */
R
Robert Moore 已提交
190

B
Bob Moore 已提交
191 192
			return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
		}
R
Robert Moore 已提交
193

B
Bob Moore 已提交
194
		descriptor_length = acpi_ut_get_descriptor_length(aml_buffer);
B
Bob Moore 已提交
195 196 197 198 199 200 201 202 203 204 205

		/*
		 * Perform limited validation of the resource length, based upon
		 * what we know about the resource type
		 */
		status =
		    acpi_rs_validate_resource_length(ACPI_CAST_PTR
						     (union aml_resource,
						      aml_buffer));
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
206 207
		}

B
Bob Moore 已提交
208
		/* Convert the AML byte stream resource to a local resource struct */
B
Bob Moore 已提交
209

B
Bob Moore 已提交
210 211 212 213 214 215 216 217
		status =
		    acpi_rs_convert_aml_to_resource(ACPI_CAST_PTR
						    (struct acpi_resource,
						     buffer),
						    ACPI_CAST_PTR(union
								  aml_resource,
								  aml_buffer),
						    info);
L
Len Brown 已提交
218
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
219
			ACPI_REPORT_ERROR(("Could not convert AML resource (type %X) to resource, %s\n", *aml_buffer, acpi_format_exception(status)));
L
Len Brown 已提交
220
			return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
221 222
		}

R
Robert Moore 已提交
223
		/* Set the aligned length of the new resource descriptor */
R
Robert Moore 已提交
224

R
Robert Moore 已提交
225 226 227
		resource = ACPI_CAST_PTR(struct acpi_resource, buffer);
		resource->length =
		    (u32) ACPI_ALIGN_RESOURCE_SIZE(resource->length);
L
Linus Torvalds 已提交
228

R
Robert Moore 已提交
229
		/* Normal exit on completion of an end_tag resource descriptor */
R
Robert Moore 已提交
230

B
Bob Moore 已提交
231
		if (acpi_ut_get_resource_type(aml_buffer) ==
B
Bob Moore 已提交
232
		    ACPI_RESOURCE_NAME_END_TAG) {
R
Robert Moore 已提交
233 234 235 236 237
			return_ACPI_STATUS(AE_OK);
		}

		/* Update counter and point to the next input resource */

B
Bob Moore 已提交
238 239
		bytes_parsed += descriptor_length;
		aml_buffer += descriptor_length;
L
Linus Torvalds 已提交
240

R
Robert Moore 已提交
241
		/* Point to the next structure in the output buffer */
R
Robert Moore 已提交
242

B
Bob Moore 已提交
243
		buffer += resource->length;
R
Robert Moore 已提交
244
	}
L
Linus Torvalds 已提交
245

R
Robert Moore 已提交
246
	/* Completed buffer, but did not find an end_tag resource descriptor */
L
Linus Torvalds 已提交
247

R
Robert Moore 已提交
248
	return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
L
Linus Torvalds 已提交
249 250 251 252
}

/*******************************************************************************
 *
B
Bob Moore 已提交
253
 * FUNCTION:    acpi_rs_convert_resources_to_aml
L
Linus Torvalds 已提交
254
 *
B
Bob Moore 已提交
255 256 257 258 259 260 261
 * PARAMETERS:  Resource            - Pointer to the resource linked list
 *              aml_size_needed     - Calculated size of the byte stream
 *                                    needed from calling acpi_rs_get_aml_length()
 *                                    The size of the output_buffer is
 *                                    guaranteed to be >= aml_size_needed
 *              output_buffer       - Pointer to the buffer that will
 *                                    contain the byte stream
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Takes the resource linked list and parses it, creating a
 *              byte stream of resources in the caller's output buffer
 *
 ******************************************************************************/

acpi_status
B
Bob Moore 已提交
271 272
acpi_rs_convert_resources_to_aml(struct acpi_resource *resource,
				 acpi_size aml_size_needed, u8 * output_buffer)
L
Linus Torvalds 已提交
273
{
B
Bob Moore 已提交
274
	u8 *aml_buffer = output_buffer;
B
Bob Moore 已提交
275
	u8 *end_aml_buffer = output_buffer + aml_size_needed;
R
Robert Moore 已提交
276
	acpi_status status;
L
Linus Torvalds 已提交
277

B
Bob Moore 已提交
278
	ACPI_FUNCTION_TRACE("rs_convert_resources_to_aml");
L
Linus Torvalds 已提交
279

B
Bob Moore 已提交
280
	/* Walk the resource descriptor list, convert each descriptor */
L
Linus Torvalds 已提交
281

B
Bob Moore 已提交
282 283
	while (aml_buffer < end_aml_buffer) {
		/* Validate the Resource Type */
R
Robert Moore 已提交
284

B
Bob Moore 已提交
285
		if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
L
Len Brown 已提交
286 287
			ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
					  "Invalid descriptor type (%X) in resource list\n",
R
Robert Moore 已提交
288 289
					  resource->type));
			return_ACPI_STATUS(AE_BAD_DATA);
R
Robert Moore 已提交
290
		}
L
Linus Torvalds 已提交
291

B
Bob Moore 已提交
292
		/* Perform the conversion */
B
Bob Moore 已提交
293

B
Bob Moore 已提交
294 295 296 297 298 299
		status = acpi_rs_convert_resource_to_aml(resource,
							 ACPI_CAST_PTR(union
								       aml_resource,
								       aml_buffer),
							 acpi_gbl_set_resource_dispatch
							 [resource->type]);
B
Bob Moore 已提交
300 301 302 303 304 305
		if (ACPI_FAILURE(status)) {
			ACPI_REPORT_ERROR(("Could not convert resource (type %X) to AML, %s\n", resource->type, acpi_format_exception(status)));
			return_ACPI_STATUS(status);
		}

		/* Perform final sanity check on the new AML resource descriptor */
R
Robert Moore 已提交
306

B
Bob Moore 已提交
307 308 309 310
		status =
		    acpi_rs_validate_resource_length(ACPI_CAST_PTR
						     (union aml_resource,
						      aml_buffer));
L
Len Brown 已提交
311 312
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
313 314
		}

B
Bob Moore 已提交
315
		/* Check for end-of-list, normal exit */
R
Robert Moore 已提交
316

B
Bob Moore 已提交
317 318
		if (resource->type == ACPI_RESOURCE_TYPE_END_TAG) {
			/* An End Tag indicates the end of the input Resource Template */
R
Robert Moore 已提交
319 320 321 322

			return_ACPI_STATUS(AE_OK);
		}

B
Bob Moore 已提交
323 324 325 326 327
		/*
		 * Extract the total length of the new descriptor and set the
		 * aml_buffer to point to the next (output) resource descriptor
		 */
		aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
L
Linus Torvalds 已提交
328

B
Bob Moore 已提交
329
		/* Point to the next input resource descriptor */
R
Robert Moore 已提交
330

B
Bob Moore 已提交
331 332 333
		resource =
		    ACPI_PTR_ADD(struct acpi_resource, resource,
				 resource->length);
B
Bob Moore 已提交
334 335 336

		/* Check for end-of-list, normal exit */

L
Linus Torvalds 已提交
337
	}
B
Bob Moore 已提交
338 339 340 341

	/* Completed buffer, but did not find an end_tag resource descriptor */

	return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
L
Linus Torvalds 已提交
342
}