Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*******************************************************************************
  2 *
  3 * Module Name: utresrc - Resource management utilities
  4 *
  5 ******************************************************************************/
  6
  7/*
  8 * Copyright (C) 2000 - 2011, Intel Corp.
  9 * All rights reserved.
 10 *
 11 * Redistribution and use in source and binary forms, with or without
 12 * modification, are permitted provided that the following conditions
 13 * are met:
 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.
 25 *
 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.
 29 *
 30 * NO WARRANTY
 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.
 42 */
 43
 44#include <acpi/acpi.h>
 45#include "accommon.h"
 46#include "amlresrc.h"
 47
 48#define _COMPONENT          ACPI_UTILITIES
 49ACPI_MODULE_NAME("utresrc")
 50#if defined(ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
 51/*
 52 * Strings used to decode resource descriptors.
 53 * Used by both the disassembler and the debugger resource dump routines
 54 */
 55const char *acpi_gbl_bm_decode[] = {
 56	"NotBusMaster",
 57	"BusMaster"
 58};
 59
 60const char *acpi_gbl_config_decode[] = {
 61	"0 - Good Configuration",
 62	"1 - Acceptable Configuration",
 63	"2 - Suboptimal Configuration",
 64	"3 - ***Invalid Configuration***",
 65};
 66
 67const char *acpi_gbl_consume_decode[] = {
 68	"ResourceProducer",
 69	"ResourceConsumer"
 70};
 71
 72const char *acpi_gbl_dec_decode[] = {
 73	"PosDecode",
 74	"SubDecode"
 75};
 76
 77const char *acpi_gbl_he_decode[] = {
 78	"Level",
 79	"Edge"
 80};
 81
 82const char *acpi_gbl_io_decode[] = {
 83	"Decode10",
 84	"Decode16"
 85};
 86
 87const char *acpi_gbl_ll_decode[] = {
 88	"ActiveHigh",
 89	"ActiveLow"
 90};
 91
 92const char *acpi_gbl_max_decode[] = {
 93	"MaxNotFixed",
 94	"MaxFixed"
 95};
 96
 97const char *acpi_gbl_mem_decode[] = {
 98	"NonCacheable",
 99	"Cacheable",
100	"WriteCombining",
101	"Prefetchable"
102};
103
104const char *acpi_gbl_min_decode[] = {
105	"MinNotFixed",
106	"MinFixed"
107};
108
109const char *acpi_gbl_mtp_decode[] = {
110	"AddressRangeMemory",
111	"AddressRangeReserved",
112	"AddressRangeACPI",
113	"AddressRangeNVS"
114};
115
116const char *acpi_gbl_rng_decode[] = {
117	"InvalidRanges",
118	"NonISAOnlyRanges",
119	"ISAOnlyRanges",
120	"EntireRange"
121};
122
123const char *acpi_gbl_rw_decode[] = {
124	"ReadOnly",
125	"ReadWrite"
126};
127
128const char *acpi_gbl_shr_decode[] = {
129	"Exclusive",
130	"Shared"
131};
132
133const char *acpi_gbl_siz_decode[] = {
134	"Transfer8",
135	"Transfer8_16",
136	"Transfer16",
137	"InvalidSize"
138};
139
140const char *acpi_gbl_trs_decode[] = {
141	"DenseTranslation",
142	"SparseTranslation"
143};
144
145const char *acpi_gbl_ttp_decode[] = {
146	"TypeStatic",
147	"TypeTranslation"
148};
149
150const char *acpi_gbl_typ_decode[] = {
151	"Compatibility",
152	"TypeA",
153	"TypeB",
154	"TypeF"
155};
156
157#endif
158
159/*
160 * Base sizes of the raw AML resource descriptors, indexed by resource type.
161 * Zero indicates a reserved (and therefore invalid) resource type.
162 */
163const u8 acpi_gbl_resource_aml_sizes[] = {
164	/* Small descriptors */
165
166	0,
167	0,
168	0,
169	0,
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),
176	0,
177	0,
178	0,
179	0,
180	ACPI_AML_SIZE_SMALL(struct aml_resource_vendor_small),
181	ACPI_AML_SIZE_SMALL(struct aml_resource_end_tag),
182
183	/* Large descriptors */
184
185	0,
186	ACPI_AML_SIZE_LARGE(struct aml_resource_memory24),
187	ACPI_AML_SIZE_LARGE(struct aml_resource_generic_register),
188	0,
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197};
198
199/*
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.
204 */
205static const u8 acpi_gbl_resource_types[] = {
206	/* Small descriptors */
207
208	0,
209	0,
210	0,
211	0,
212	ACPI_SMALL_VARIABLE_LENGTH,
213	ACPI_FIXED_LENGTH,
214	ACPI_SMALL_VARIABLE_LENGTH,
215	ACPI_FIXED_LENGTH,
216	ACPI_FIXED_LENGTH,
217	ACPI_FIXED_LENGTH,
218	0,
219	0,
220	0,
221	0,
222	ACPI_VARIABLE_LENGTH,
223	ACPI_FIXED_LENGTH,
224
225	/* Large descriptors */
226
227	0,
228	ACPI_FIXED_LENGTH,
229	ACPI_FIXED_LENGTH,
230	0,
231	ACPI_VARIABLE_LENGTH,
232	ACPI_FIXED_LENGTH,
233	ACPI_FIXED_LENGTH,
234	ACPI_VARIABLE_LENGTH,
235	ACPI_VARIABLE_LENGTH,
236	ACPI_VARIABLE_LENGTH,
237	ACPI_VARIABLE_LENGTH,
238	ACPI_FIXED_LENGTH
 
 
 
 
 
 
 
 
239};
240
241/*******************************************************************************
242 *
243 * FUNCTION:    acpi_ut_walk_aml_resources
244 *
245 * PARAMETERS:  Aml             - Pointer to the raw AML resource template
246 *              aml_length      - Length of the entire template
247 *              user_function   - Called once for each descriptor found. If
248 *                                NULL, a pointer to the end_tag is returned
249 *              Context         - Passed to user_function
 
250 *
251 * RETURN:      Status
252 *
253 * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
254 *              once for each resource found.
255 *
256 ******************************************************************************/
257
258acpi_status
259acpi_ut_walk_aml_resources(u8 * aml,
 
260			   acpi_size aml_length,
261			   acpi_walk_aml_callback user_function, void **context)
262{
263	acpi_status status;
264	u8 *end_aml;
265	u8 resource_index;
266	u32 length;
267	u32 offset = 0;
 
268
269	ACPI_FUNCTION_TRACE(ut_walk_aml_resources);
270
271	/* The absolute minimum resource template is one end_tag descriptor */
272
273	if (aml_length < sizeof(struct aml_resource_end_tag)) {
274		return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
275	}
276
277	/* Point to the end of the resource template buffer */
278
279	end_aml = aml + aml_length;
280
281	/* Walk the byte list, abort on any invalid descriptor type or length */
282
283	while (aml < end_aml) {
284
285		/* Validate the Resource Type and Resource Length */
286
287		status = acpi_ut_validate_resource(aml, &resource_index);
 
288		if (ACPI_FAILURE(status)) {
 
 
 
 
289			return_ACPI_STATUS(status);
290		}
291
292		/* Get the length of this descriptor */
293
294		length = acpi_ut_get_descriptor_length(aml);
295
296		/* Invoke the user function */
297
298		if (user_function) {
299			status =
300			    user_function(aml, length, offset, resource_index,
301					  context);
302			if (ACPI_FAILURE(status)) {
303				return (status);
304			}
305		}
306
307		/* An end_tag descriptor terminates this resource template */
308
309		if (acpi_ut_get_resource_type(aml) ==
310		    ACPI_RESOURCE_NAME_END_TAG) {
311			/*
312			 * There must be at least one more byte in the buffer for
313			 * the 2nd byte of the end_tag
314			 */
315			if ((aml + 1) >= end_aml) {
316				return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
317			}
318
 
 
 
 
 
 
 
319			/* Return the pointer to the end_tag if requested */
320
321			if (!user_function) {
322				*context = aml;
323			}
324
325			/* Normal exit */
326
327			return_ACPI_STATUS(AE_OK);
328		}
329
330		aml += length;
331		offset += length;
332	}
333
334	/* Did not find an end_tag descriptor */
335
336	return (AE_AML_NO_RESOURCE_END_TAG);
 
 
 
 
 
 
 
 
 
 
 
 
 
337}
338
339/*******************************************************************************
340 *
341 * FUNCTION:    acpi_ut_validate_resource
342 *
343 * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
344 *              return_index    - Where the resource index is returned. NULL
345 *                                if the index is not required.
 
346 *
347 * RETURN:      Status, and optionally the Index into the global resource tables
348 *
349 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
350 *              Type and Resource Length. Returns an index into the global
351 *              resource information/dispatch tables for later use.
352 *
353 ******************************************************************************/
354
355acpi_status acpi_ut_validate_resource(void *aml, u8 * return_index)
 
 
356{
 
357	u8 resource_type;
358	u8 resource_index;
359	acpi_rs_length resource_length;
360	acpi_rs_length minimum_resource_length;
361
362	ACPI_FUNCTION_ENTRY();
363
364	/*
365	 * 1) Validate the resource_type field (Byte 0)
366	 */
367	resource_type = ACPI_GET8(aml);
368
369	/*
370	 * Byte 0 contains the descriptor name (Resource Type)
371	 * Examine the large/small bit in the resource header
372	 */
373	if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
374
375		/* Verify the large resource type (name) against the max */
376
377		if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
378			return (AE_AML_INVALID_RESOURCE_TYPE);
379		}
380
381		/*
382		 * Large Resource Type -- bits 6:0 contain the name
383		 * Translate range 0x80-0x8B to index range 0x10-0x1B
384		 */
385		resource_index = (u8) (resource_type - 0x70);
386	} else {
387		/*
388		 * Small Resource Type -- bits 6:3 contain the name
389		 * Shift range to index range 0x00-0x0F
390		 */
391		resource_index = (u8)
392		    ((resource_type & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
393	}
394
395	/* Check validity of the resource type, zero indicates name is invalid */
396
 
 
397	if (!acpi_gbl_resource_types[resource_index]) {
398		return (AE_AML_INVALID_RESOURCE_TYPE);
399	}
400
401	/*
402	 * 2) Validate the resource_length field. This ensures that the length
403	 *    is at least reasonable, and guarantees that it is non-zero.
404	 */
405	resource_length = acpi_ut_get_resource_length(aml);
406	minimum_resource_length = acpi_gbl_resource_aml_sizes[resource_index];
407
408	/* Validate based upon the type of resource - fixed length or variable */
409
410	switch (acpi_gbl_resource_types[resource_index]) {
411	case ACPI_FIXED_LENGTH:
412
413		/* Fixed length resource, length must match exactly */
414
415		if (resource_length != minimum_resource_length) {
416			return (AE_AML_BAD_RESOURCE_LENGTH);
417		}
418		break;
419
420	case ACPI_VARIABLE_LENGTH:
421
422		/* Variable length resource, length must be at least the minimum */
423
424		if (resource_length < minimum_resource_length) {
425			return (AE_AML_BAD_RESOURCE_LENGTH);
426		}
427		break;
428
429	case ACPI_SMALL_VARIABLE_LENGTH:
430
431		/* Small variable length resource, length can be (Min) or (Min-1) */
432
433		if ((resource_length > minimum_resource_length) ||
434		    (resource_length < (minimum_resource_length - 1))) {
435			return (AE_AML_BAD_RESOURCE_LENGTH);
436		}
437		break;
438
439	default:
440
441		/* Shouldn't happen (because of validation earlier), but be sure */
442
443		return (AE_AML_INVALID_RESOURCE_TYPE);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444	}
445
446	/* Optionally return the resource table index */
447
448	if (return_index) {
449		*return_index = resource_index;
450	}
451
452	return (AE_OK);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
453}
454
455/*******************************************************************************
456 *
457 * FUNCTION:    acpi_ut_get_resource_type
458 *
459 * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
460 *
461 * RETURN:      The Resource Type with no extraneous bits (except the
462 *              Large/Small descriptor bit -- this is left alone)
463 *
464 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
465 *              a resource descriptor.
466 *
467 ******************************************************************************/
468
469u8 acpi_ut_get_resource_type(void *aml)
470{
471	ACPI_FUNCTION_ENTRY();
472
473	/*
474	 * Byte 0 contains the descriptor name (Resource Type)
475	 * Examine the large/small bit in the resource header
476	 */
477	if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
478
479		/* Large Resource Type -- bits 6:0 contain the name */
480
481		return (ACPI_GET8(aml));
482	} else {
483		/* Small Resource Type -- bits 6:3 contain the name */
484
485		return ((u8) (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
486	}
487}
488
489/*******************************************************************************
490 *
491 * FUNCTION:    acpi_ut_get_resource_length
492 *
493 * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
494 *
495 * RETURN:      Byte Length
496 *
497 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
498 *              definition, this does not include the size of the descriptor
499 *              header or the length field itself.
500 *
501 ******************************************************************************/
502
503u16 acpi_ut_get_resource_length(void *aml)
504{
505	acpi_rs_length resource_length;
506
507	ACPI_FUNCTION_ENTRY();
508
509	/*
510	 * Byte 0 contains the descriptor name (Resource Type)
511	 * Examine the large/small bit in the resource header
512	 */
513	if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
514
515		/* Large Resource type -- bytes 1-2 contain the 16-bit length */
516
517		ACPI_MOVE_16_TO_16(&resource_length, ACPI_ADD_PTR(u8, aml, 1));
518
519	} else {
520		/* Small Resource type -- bits 2:0 of byte 0 contain the length */
521
522		resource_length = (u16) (ACPI_GET8(aml) &
523					 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
524	}
525
526	return (resource_length);
527}
528
529/*******************************************************************************
530 *
531 * FUNCTION:    acpi_ut_get_resource_header_length
532 *
533 * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
534 *
535 * RETURN:      Length of the AML header (depends on large/small descriptor)
536 *
537 * DESCRIPTION: Get the length of the header for this resource.
538 *
539 ******************************************************************************/
540
541u8 acpi_ut_get_resource_header_length(void *aml)
542{
543	ACPI_FUNCTION_ENTRY();
544
545	/* Examine the large/small bit in the resource header */
546
547	if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
548		return (sizeof(struct aml_resource_large_header));
549	} else {
550		return (sizeof(struct aml_resource_small_header));
551	}
552}
553
554/*******************************************************************************
555 *
556 * FUNCTION:    acpi_ut_get_descriptor_length
557 *
558 * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
559 *
560 * RETURN:      Byte length
561 *
562 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
563 *              length of the descriptor header and the length field itself.
564 *              Used to walk descriptor lists.
565 *
566 ******************************************************************************/
567
568u32 acpi_ut_get_descriptor_length(void *aml)
569{
570	ACPI_FUNCTION_ENTRY();
571
572	/*
573	 * Get the Resource Length (does not include header length) and add
574	 * the header length (depends on if this is a small or large resource)
575	 */
576	return (acpi_ut_get_resource_length(aml) +
577		acpi_ut_get_resource_header_length(aml));
578}
579
580/*******************************************************************************
581 *
582 * FUNCTION:    acpi_ut_get_resource_end_tag
583 *
584 * PARAMETERS:  obj_desc        - The resource template buffer object
585 *              end_tag         - Where the pointer to the end_tag is returned
586 *
587 * RETURN:      Status, pointer to the end tag
588 *
589 * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
590 *              Note: allows a buffer length of zero.
591 *
592 ******************************************************************************/
593
594acpi_status
595acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc,
596			     u8 ** end_tag)
597{
598	acpi_status status;
599
600	ACPI_FUNCTION_TRACE(ut_get_resource_end_tag);
601
602	/* Allow a buffer length of zero */
603
604	if (!obj_desc->buffer.length) {
605		*end_tag = obj_desc->buffer.pointer;
606		return_ACPI_STATUS(AE_OK);
607	}
608
609	/* Validate the template and get a pointer to the end_tag */
610
611	status = acpi_ut_walk_aml_resources(obj_desc->buffer.pointer,
612					    obj_desc->buffer.length, NULL,
613					    (void **)end_tag);
614
615	return_ACPI_STATUS(status);
616}
v6.8
  1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2/*******************************************************************************
  3 *
  4 * Module Name: utresrc - Resource management utilities
  5 *
  6 ******************************************************************************/
  7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  8#include <acpi/acpi.h>
  9#include "accommon.h"
 10#include "acresrc.h"
 11
 12#define _COMPONENT          ACPI_UTILITIES
 13ACPI_MODULE_NAME("utresrc")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 14
 15/*
 16 * Base sizes of the raw AML resource descriptors, indexed by resource type.
 17 * Zero indicates a reserved (and therefore invalid) resource type.
 18 */
 19const u8 acpi_gbl_resource_aml_sizes[] = {
 20	/* Small descriptors */
 21
 22	0,
 23	0,
 24	0,
 25	0,
 26	ACPI_AML_SIZE_SMALL(struct aml_resource_irq),
 27	ACPI_AML_SIZE_SMALL(struct aml_resource_dma),
 28	ACPI_AML_SIZE_SMALL(struct aml_resource_start_dependent),
 29	ACPI_AML_SIZE_SMALL(struct aml_resource_end_dependent),
 30	ACPI_AML_SIZE_SMALL(struct aml_resource_io),
 31	ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_io),
 32	ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_dma),
 33	0,
 34	0,
 35	0,
 36	ACPI_AML_SIZE_SMALL(struct aml_resource_vendor_small),
 37	ACPI_AML_SIZE_SMALL(struct aml_resource_end_tag),
 38
 39	/* Large descriptors */
 40
 41	0,
 42	ACPI_AML_SIZE_LARGE(struct aml_resource_memory24),
 43	ACPI_AML_SIZE_LARGE(struct aml_resource_generic_register),
 44	0,
 45	ACPI_AML_SIZE_LARGE(struct aml_resource_vendor_large),
 46	ACPI_AML_SIZE_LARGE(struct aml_resource_memory32),
 47	ACPI_AML_SIZE_LARGE(struct aml_resource_fixed_memory32),
 48	ACPI_AML_SIZE_LARGE(struct aml_resource_address32),
 49	ACPI_AML_SIZE_LARGE(struct aml_resource_address16),
 50	ACPI_AML_SIZE_LARGE(struct aml_resource_extended_irq),
 51	ACPI_AML_SIZE_LARGE(struct aml_resource_address64),
 52	ACPI_AML_SIZE_LARGE(struct aml_resource_extended_address64),
 53	ACPI_AML_SIZE_LARGE(struct aml_resource_gpio),
 54	ACPI_AML_SIZE_LARGE(struct aml_resource_pin_function),
 55	ACPI_AML_SIZE_LARGE(struct aml_resource_common_serialbus),
 56	ACPI_AML_SIZE_LARGE(struct aml_resource_pin_config),
 57	ACPI_AML_SIZE_LARGE(struct aml_resource_pin_group),
 58	ACPI_AML_SIZE_LARGE(struct aml_resource_pin_group_function),
 59	ACPI_AML_SIZE_LARGE(struct aml_resource_pin_group_config),
 60	ACPI_AML_SIZE_LARGE(struct aml_resource_clock_input),
 61
 62};
 63
 64const u8 acpi_gbl_resource_aml_serial_bus_sizes[] = {
 65	0,
 66	ACPI_AML_SIZE_LARGE(struct aml_resource_i2c_serialbus),
 67	ACPI_AML_SIZE_LARGE(struct aml_resource_spi_serialbus),
 68	ACPI_AML_SIZE_LARGE(struct aml_resource_uart_serialbus),
 69	ACPI_AML_SIZE_LARGE(struct aml_resource_csi2_serialbus),
 70};
 71
 72/*
 73 * Resource types, used to validate the resource length field.
 74 * The length of fixed-length types must match exactly, variable
 75 * lengths must meet the minimum required length, etc.
 76 * Zero indicates a reserved (and therefore invalid) resource type.
 77 */
 78static const u8 acpi_gbl_resource_types[] = {
 79	/* Small descriptors */
 80
 81	0,
 82	0,
 83	0,
 84	0,
 85	ACPI_SMALL_VARIABLE_LENGTH,	/* 04 IRQ */
 86	ACPI_FIXED_LENGTH,	/* 05 DMA */
 87	ACPI_SMALL_VARIABLE_LENGTH,	/* 06 start_dependent_functions */
 88	ACPI_FIXED_LENGTH,	/* 07 end_dependent_functions */
 89	ACPI_FIXED_LENGTH,	/* 08 IO */
 90	ACPI_FIXED_LENGTH,	/* 09 fixed_IO */
 91	ACPI_FIXED_LENGTH,	/* 0A fixed_DMA */
 92	0,
 93	0,
 94	0,
 95	ACPI_VARIABLE_LENGTH,	/* 0E vendor_short */
 96	ACPI_FIXED_LENGTH,	/* 0F end_tag */
 97
 98	/* Large descriptors */
 99
100	0,
101	ACPI_FIXED_LENGTH,	/* 01 Memory24 */
102	ACPI_FIXED_LENGTH,	/* 02 generic_register */
103	0,
104	ACPI_VARIABLE_LENGTH,	/* 04 vendor_long */
105	ACPI_FIXED_LENGTH,	/* 05 Memory32 */
106	ACPI_FIXED_LENGTH,	/* 06 memory32_fixed */
107	ACPI_VARIABLE_LENGTH,	/* 07 Dword* address */
108	ACPI_VARIABLE_LENGTH,	/* 08 Word* address */
109	ACPI_VARIABLE_LENGTH,	/* 09 extended_IRQ */
110	ACPI_VARIABLE_LENGTH,	/* 0A Qword* address */
111	ACPI_FIXED_LENGTH,	/* 0B Extended* address */
112	ACPI_VARIABLE_LENGTH,	/* 0C Gpio* */
113	ACPI_VARIABLE_LENGTH,	/* 0D pin_function */
114	ACPI_VARIABLE_LENGTH,	/* 0E *serial_bus */
115	ACPI_VARIABLE_LENGTH,	/* 0F pin_config */
116	ACPI_VARIABLE_LENGTH,	/* 10 pin_group */
117	ACPI_VARIABLE_LENGTH,	/* 11 pin_group_function */
118	ACPI_VARIABLE_LENGTH,	/* 12 pin_group_config */
119	ACPI_VARIABLE_LENGTH,	/* 13 clock_input */
120};
121
122/*******************************************************************************
123 *
124 * FUNCTION:    acpi_ut_walk_aml_resources
125 *
126 * PARAMETERS:  walk_state          - Current walk info
127 * PARAMETERS:  aml                 - Pointer to the raw AML resource template
128 *              aml_length          - Length of the entire template
129 *              user_function       - Called once for each descriptor found. If
130 *                                    NULL, a pointer to the end_tag is returned
131 *              context             - Passed to user_function
132 *
133 * RETURN:      Status
134 *
135 * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
136 *              once for each resource found.
137 *
138 ******************************************************************************/
139
140acpi_status
141acpi_ut_walk_aml_resources(struct acpi_walk_state *walk_state,
142			   u8 *aml,
143			   acpi_size aml_length,
144			   acpi_walk_aml_callback user_function, void **context)
145{
146	acpi_status status;
147	u8 *end_aml;
148	u8 resource_index;
149	u32 length;
150	u32 offset = 0;
151	u8 end_tag[2] = { 0x79, 0x00 };
152
153	ACPI_FUNCTION_TRACE(ut_walk_aml_resources);
154
155	/* The absolute minimum resource template is one end_tag descriptor */
156
157	if (aml_length < sizeof(struct aml_resource_end_tag)) {
158		return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
159	}
160
161	/* Point to the end of the resource template buffer */
162
163	end_aml = aml + aml_length;
164
165	/* Walk the byte list, abort on any invalid descriptor type or length */
166
167	while (aml < end_aml) {
168
169		/* Validate the Resource Type and Resource Length */
170
171		status =
172		    acpi_ut_validate_resource(walk_state, aml, &resource_index);
173		if (ACPI_FAILURE(status)) {
174			/*
175			 * Exit on failure. Cannot continue because the descriptor
176			 * length may be bogus also.
177			 */
178			return_ACPI_STATUS(status);
179		}
180
181		/* Get the length of this descriptor */
182
183		length = acpi_ut_get_descriptor_length(aml);
184
185		/* Invoke the user function */
186
187		if (user_function) {
188			status =
189			    user_function(aml, length, offset, resource_index,
190					  context);
191			if (ACPI_FAILURE(status)) {
192				return_ACPI_STATUS(status);
193			}
194		}
195
196		/* An end_tag descriptor terminates this resource template */
197
198		if (acpi_ut_get_resource_type(aml) ==
199		    ACPI_RESOURCE_NAME_END_TAG) {
200			/*
201			 * There must be at least one more byte in the buffer for
202			 * the 2nd byte of the end_tag
203			 */
204			if ((aml + 1) >= end_aml) {
205				return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
206			}
207
208			/*
209			 * Don't attempt to perform any validation on the 2nd byte.
210			 * Although all known ASL compilers insert a zero for the 2nd
211			 * byte, it can also be a checksum (as per the ACPI spec),
212			 * and this is occasionally seen in the field. July 2017.
213			 */
214
215			/* Return the pointer to the end_tag if requested */
216
217			if (!user_function) {
218				*context = aml;
219			}
220
221			/* Normal exit */
222
223			return_ACPI_STATUS(AE_OK);
224		}
225
226		aml += length;
227		offset += length;
228	}
229
230	/* Did not find an end_tag descriptor */
231
232	if (user_function) {
233
234		/* Insert an end_tag anyway. acpi_rs_get_list_length always leaves room */
235
236		(void)acpi_ut_validate_resource(walk_state, end_tag,
237						&resource_index);
238		status =
239		    user_function(end_tag, 2, offset, resource_index, context);
240		if (ACPI_FAILURE(status)) {
241			return_ACPI_STATUS(status);
242		}
243	}
244
245	return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
246}
247
248/*******************************************************************************
249 *
250 * FUNCTION:    acpi_ut_validate_resource
251 *
252 * PARAMETERS:  walk_state          - Current walk info
253 *              aml                 - Pointer to the raw AML resource descriptor
254 *              return_index        - Where the resource index is returned. NULL
255 *                                    if the index is not required.
256 *
257 * RETURN:      Status, and optionally the Index into the global resource tables
258 *
259 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
260 *              Type and Resource Length. Returns an index into the global
261 *              resource information/dispatch tables for later use.
262 *
263 ******************************************************************************/
264
265acpi_status
266acpi_ut_validate_resource(struct acpi_walk_state *walk_state,
267			  void *aml, u8 *return_index)
268{
269	union aml_resource *aml_resource;
270	u8 resource_type;
271	u8 resource_index;
272	acpi_rs_length resource_length;
273	acpi_rs_length minimum_resource_length;
274
275	ACPI_FUNCTION_ENTRY();
276
277	/*
278	 * 1) Validate the resource_type field (Byte 0)
279	 */
280	resource_type = ACPI_GET8(aml);
281
282	/*
283	 * Byte 0 contains the descriptor name (Resource Type)
284	 * Examine the large/small bit in the resource header
285	 */
286	if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
287
288		/* Verify the large resource type (name) against the max */
289
290		if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
291			goto invalid_resource;
292		}
293
294		/*
295		 * Large Resource Type -- bits 6:0 contain the name
296		 * Translate range 0x80-0x8B to index range 0x10-0x1B
297		 */
298		resource_index = (u8) (resource_type - 0x70);
299	} else {
300		/*
301		 * Small Resource Type -- bits 6:3 contain the name
302		 * Shift range to index range 0x00-0x0F
303		 */
304		resource_index = (u8)
305		    ((resource_type & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
306	}
307
308	/*
309	 * Check validity of the resource type, via acpi_gbl_resource_types.
310	 * Zero indicates an invalid resource.
311	 */
312	if (!acpi_gbl_resource_types[resource_index]) {
313		goto invalid_resource;
314	}
315
316	/*
317	 * Validate the resource_length field. This ensures that the length
318	 * is at least reasonable, and guarantees that it is non-zero.
319	 */
320	resource_length = acpi_ut_get_resource_length(aml);
321	minimum_resource_length = acpi_gbl_resource_aml_sizes[resource_index];
322
323	/* Validate based upon the type of resource - fixed length or variable */
324
325	switch (acpi_gbl_resource_types[resource_index]) {
326	case ACPI_FIXED_LENGTH:
327
328		/* Fixed length resource, length must match exactly */
329
330		if (resource_length != minimum_resource_length) {
331			goto bad_resource_length;
332		}
333		break;
334
335	case ACPI_VARIABLE_LENGTH:
336
337		/* Variable length resource, length must be at least the minimum */
338
339		if (resource_length < minimum_resource_length) {
340			goto bad_resource_length;
341		}
342		break;
343
344	case ACPI_SMALL_VARIABLE_LENGTH:
345
346		/* Small variable length resource, length can be (Min) or (Min-1) */
347
348		if ((resource_length > minimum_resource_length) ||
349		    (resource_length < (minimum_resource_length - 1))) {
350			goto bad_resource_length;
351		}
352		break;
353
354	default:
355
356		/* Shouldn't happen (because of validation earlier), but be sure */
357
358		goto invalid_resource;
359	}
360
361	aml_resource = ACPI_CAST_PTR(union aml_resource, aml);
362	if (resource_type == ACPI_RESOURCE_NAME_SERIAL_BUS) {
363
364		/* Avoid undefined behavior: member access within misaligned address */
365
366		struct aml_resource_common_serialbus common_serial_bus;
367		memcpy(&common_serial_bus, aml_resource,
368		       sizeof(common_serial_bus));
369
370		/* Validate the bus_type field */
371
372		if ((common_serial_bus.type == 0) ||
373		    (common_serial_bus.type > AML_RESOURCE_MAX_SERIALBUSTYPE)) {
374			if (walk_state) {
375				ACPI_ERROR((AE_INFO,
376					    "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
377					    common_serial_bus.type));
378			}
379			return (AE_AML_INVALID_RESOURCE_TYPE);
380		}
381	}
382
383	/* Optionally return the resource table index */
384
385	if (return_index) {
386		*return_index = resource_index;
387	}
388
389	return (AE_OK);
390
391invalid_resource:
392
393	if (walk_state) {
394		ACPI_ERROR((AE_INFO,
395			    "Invalid/unsupported resource descriptor: Type 0x%2.2X",
396			    resource_type));
397	}
398	return (AE_AML_INVALID_RESOURCE_TYPE);
399
400bad_resource_length:
401
402	if (walk_state) {
403		ACPI_ERROR((AE_INFO,
404			    "Invalid resource descriptor length: Type "
405			    "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
406			    resource_type, resource_length,
407			    minimum_resource_length));
408	}
409	return (AE_AML_BAD_RESOURCE_LENGTH);
410}
411
412/*******************************************************************************
413 *
414 * FUNCTION:    acpi_ut_get_resource_type
415 *
416 * PARAMETERS:  aml             - Pointer to the raw AML resource descriptor
417 *
418 * RETURN:      The Resource Type with no extraneous bits (except the
419 *              Large/Small descriptor bit -- this is left alone)
420 *
421 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
422 *              a resource descriptor.
423 *
424 ******************************************************************************/
425
426u8 acpi_ut_get_resource_type(void *aml)
427{
428	ACPI_FUNCTION_ENTRY();
429
430	/*
431	 * Byte 0 contains the descriptor name (Resource Type)
432	 * Examine the large/small bit in the resource header
433	 */
434	if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
435
436		/* Large Resource Type -- bits 6:0 contain the name */
437
438		return (ACPI_GET8(aml));
439	} else {
440		/* Small Resource Type -- bits 6:3 contain the name */
441
442		return ((u8) (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
443	}
444}
445
446/*******************************************************************************
447 *
448 * FUNCTION:    acpi_ut_get_resource_length
449 *
450 * PARAMETERS:  aml             - Pointer to the raw AML resource descriptor
451 *
452 * RETURN:      Byte Length
453 *
454 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
455 *              definition, this does not include the size of the descriptor
456 *              header or the length field itself.
457 *
458 ******************************************************************************/
459
460u16 acpi_ut_get_resource_length(void *aml)
461{
462	acpi_rs_length resource_length;
463
464	ACPI_FUNCTION_ENTRY();
465
466	/*
467	 * Byte 0 contains the descriptor name (Resource Type)
468	 * Examine the large/small bit in the resource header
469	 */
470	if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
471
472		/* Large Resource type -- bytes 1-2 contain the 16-bit length */
473
474		ACPI_MOVE_16_TO_16(&resource_length, ACPI_ADD_PTR(u8, aml, 1));
475
476	} else {
477		/* Small Resource type -- bits 2:0 of byte 0 contain the length */
478
479		resource_length = (u16) (ACPI_GET8(aml) &
480					 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
481	}
482
483	return (resource_length);
484}
485
486/*******************************************************************************
487 *
488 * FUNCTION:    acpi_ut_get_resource_header_length
489 *
490 * PARAMETERS:  aml             - Pointer to the raw AML resource descriptor
491 *
492 * RETURN:      Length of the AML header (depends on large/small descriptor)
493 *
494 * DESCRIPTION: Get the length of the header for this resource.
495 *
496 ******************************************************************************/
497
498u8 acpi_ut_get_resource_header_length(void *aml)
499{
500	ACPI_FUNCTION_ENTRY();
501
502	/* Examine the large/small bit in the resource header */
503
504	if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
505		return (sizeof(struct aml_resource_large_header));
506	} else {
507		return (sizeof(struct aml_resource_small_header));
508	}
509}
510
511/*******************************************************************************
512 *
513 * FUNCTION:    acpi_ut_get_descriptor_length
514 *
515 * PARAMETERS:  aml             - Pointer to the raw AML resource descriptor
516 *
517 * RETURN:      Byte length
518 *
519 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
520 *              length of the descriptor header and the length field itself.
521 *              Used to walk descriptor lists.
522 *
523 ******************************************************************************/
524
525u32 acpi_ut_get_descriptor_length(void *aml)
526{
527	ACPI_FUNCTION_ENTRY();
528
529	/*
530	 * Get the Resource Length (does not include header length) and add
531	 * the header length (depends on if this is a small or large resource)
532	 */
533	return (acpi_ut_get_resource_length(aml) +
534		acpi_ut_get_resource_header_length(aml));
535}
536
537/*******************************************************************************
538 *
539 * FUNCTION:    acpi_ut_get_resource_end_tag
540 *
541 * PARAMETERS:  obj_desc        - The resource template buffer object
542 *              end_tag         - Where the pointer to the end_tag is returned
543 *
544 * RETURN:      Status, pointer to the end tag
545 *
546 * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
547 *              Note: allows a buffer length of zero.
548 *
549 ******************************************************************************/
550
551acpi_status
552acpi_ut_get_resource_end_tag(union acpi_operand_object *obj_desc, u8 **end_tag)
 
553{
554	acpi_status status;
555
556	ACPI_FUNCTION_TRACE(ut_get_resource_end_tag);
557
558	/* Allow a buffer length of zero */
559
560	if (!obj_desc->buffer.length) {
561		*end_tag = obj_desc->buffer.pointer;
562		return_ACPI_STATUS(AE_OK);
563	}
564
565	/* Validate the template and get a pointer to the end_tag */
566
567	status = acpi_ut_walk_aml_resources(NULL, obj_desc->buffer.pointer,
568					    obj_desc->buffer.length, NULL,
569					    (void **)end_tag);
570
571	return_ACPI_STATUS(status);
572}