xref: /freebsd/sys/contrib/dev/acpica/components/utilities/utresrc.c (revision ca86bcf2531c7b149c95244a67853d44323e7855)
1 /*******************************************************************************
2  *
3  * Module Name: utresrc - Resource management utilities
4  *
5  ******************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acresrc.h>
155 
156 
157 #define _COMPONENT          ACPI_UTILITIES
158         ACPI_MODULE_NAME    ("utresrc")
159 
160 
161 #if defined(ACPI_DEBUG_OUTPUT) || defined (ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
162 
163 /*
164  * Strings used to decode resource descriptors.
165  * Used by both the disassembler and the debugger resource dump routines
166  */
167 const char                      *AcpiGbl_BmDecode[] =
168 {
169     "NotBusMaster",
170     "BusMaster"
171 };
172 
173 const char                      *AcpiGbl_ConfigDecode[] =
174 {
175     "0 - Good Configuration",
176     "1 - Acceptable Configuration",
177     "2 - Suboptimal Configuration",
178     "3 - ***Invalid Configuration***",
179 };
180 
181 const char                      *AcpiGbl_ConsumeDecode[] =
182 {
183     "ResourceProducer",
184     "ResourceConsumer"
185 };
186 
187 const char                      *AcpiGbl_DecDecode[] =
188 {
189     "PosDecode",
190     "SubDecode"
191 };
192 
193 const char                      *AcpiGbl_HeDecode[] =
194 {
195     "Level",
196     "Edge"
197 };
198 
199 const char                      *AcpiGbl_IoDecode[] =
200 {
201     "Decode10",
202     "Decode16"
203 };
204 
205 const char                      *AcpiGbl_LlDecode[] =
206 {
207     "ActiveHigh",
208     "ActiveLow",
209     "ActiveBoth",
210     "Reserved"
211 };
212 
213 const char                      *AcpiGbl_MaxDecode[] =
214 {
215     "MaxNotFixed",
216     "MaxFixed"
217 };
218 
219 const char                      *AcpiGbl_MemDecode[] =
220 {
221     "NonCacheable",
222     "Cacheable",
223     "WriteCombining",
224     "Prefetchable"
225 };
226 
227 const char                      *AcpiGbl_MinDecode[] =
228 {
229     "MinNotFixed",
230     "MinFixed"
231 };
232 
233 const char                      *AcpiGbl_MtpDecode[] =
234 {
235     "AddressRangeMemory",
236     "AddressRangeReserved",
237     "AddressRangeACPI",
238     "AddressRangeNVS"
239 };
240 
241 const char                      *AcpiGbl_RngDecode[] =
242 {
243     "InvalidRanges",
244     "NonISAOnlyRanges",
245     "ISAOnlyRanges",
246     "EntireRange"
247 };
248 
249 const char                      *AcpiGbl_RwDecode[] =
250 {
251     "ReadOnly",
252     "ReadWrite"
253 };
254 
255 const char                      *AcpiGbl_ShrDecode[] =
256 {
257     "Exclusive",
258     "Shared",
259     "ExclusiveAndWake",         /* ACPI 5.0 */
260     "SharedAndWake"             /* ACPI 5.0 */
261 };
262 
263 const char                      *AcpiGbl_SizDecode[] =
264 {
265     "Transfer8",
266     "Transfer8_16",
267     "Transfer16",
268     "InvalidSize"
269 };
270 
271 const char                      *AcpiGbl_TrsDecode[] =
272 {
273     "DenseTranslation",
274     "SparseTranslation"
275 };
276 
277 const char                      *AcpiGbl_TtpDecode[] =
278 {
279     "TypeStatic",
280     "TypeTranslation"
281 };
282 
283 const char                      *AcpiGbl_TypDecode[] =
284 {
285     "Compatibility",
286     "TypeA",
287     "TypeB",
288     "TypeF"
289 };
290 
291 const char                      *AcpiGbl_PpcDecode[] =
292 {
293     "PullDefault",
294     "PullUp",
295     "PullDown",
296     "PullNone"
297 };
298 
299 const char                      *AcpiGbl_IorDecode[] =
300 {
301     "IoRestrictionNone",
302     "IoRestrictionInputOnly",
303     "IoRestrictionOutputOnly",
304     "IoRestrictionNoneAndPreserve"
305 };
306 
307 const char                      *AcpiGbl_DtsDecode[] =
308 {
309     "Width8bit",
310     "Width16bit",
311     "Width32bit",
312     "Width64bit",
313     "Width128bit",
314     "Width256bit",
315 };
316 
317 /* GPIO connection type */
318 
319 const char                      *AcpiGbl_CtDecode[] =
320 {
321     "Interrupt",
322     "I/O"
323 };
324 
325 /* Serial bus type */
326 
327 const char                      *AcpiGbl_SbtDecode[] =
328 {
329     "/* UNKNOWN serial bus type */",
330     "I2C",
331     "SPI",
332     "UART"
333 };
334 
335 /* I2C serial bus access mode */
336 
337 const char                      *AcpiGbl_AmDecode[] =
338 {
339     "AddressingMode7Bit",
340     "AddressingMode10Bit"
341 };
342 
343 /* I2C serial bus slave mode */
344 
345 const char                      *AcpiGbl_SmDecode[] =
346 {
347     "ControllerInitiated",
348     "DeviceInitiated"
349 };
350 
351 /* SPI serial bus wire mode */
352 
353 const char                      *AcpiGbl_WmDecode[] =
354 {
355     "FourWireMode",
356     "ThreeWireMode"
357 };
358 
359 /* SPI serial clock phase */
360 
361 const char                      *AcpiGbl_CphDecode[] =
362 {
363     "ClockPhaseFirst",
364     "ClockPhaseSecond"
365 };
366 
367 /* SPI serial bus clock polarity */
368 
369 const char                      *AcpiGbl_CpoDecode[] =
370 {
371     "ClockPolarityLow",
372     "ClockPolarityHigh"
373 };
374 
375 /* SPI serial bus device polarity */
376 
377 const char                      *AcpiGbl_DpDecode[] =
378 {
379     "PolarityLow",
380     "PolarityHigh"
381 };
382 
383 /* UART serial bus endian */
384 
385 const char                      *AcpiGbl_EdDecode[] =
386 {
387     "LittleEndian",
388     "BigEndian"
389 };
390 
391 /* UART serial bus bits per byte */
392 
393 const char                      *AcpiGbl_BpbDecode[] =
394 {
395     "DataBitsFive",
396     "DataBitsSix",
397     "DataBitsSeven",
398     "DataBitsEight",
399     "DataBitsNine",
400     "/* UNKNOWN Bits per byte */",
401     "/* UNKNOWN Bits per byte */",
402     "/* UNKNOWN Bits per byte */"
403 };
404 
405 /* UART serial bus stop bits */
406 
407 const char                      *AcpiGbl_SbDecode[] =
408 {
409     "StopBitsZero",
410     "StopBitsOne",
411     "StopBitsOnePlusHalf",
412     "StopBitsTwo"
413 };
414 
415 /* UART serial bus flow control */
416 
417 const char                      *AcpiGbl_FcDecode[] =
418 {
419     "FlowControlNone",
420     "FlowControlHardware",
421     "FlowControlXON",
422     "/* UNKNOWN flow control keyword */"
423 };
424 
425 /* UART serial bus parity type */
426 
427 const char                      *AcpiGbl_PtDecode[] =
428 {
429     "ParityTypeNone",
430     "ParityTypeEven",
431     "ParityTypeOdd",
432     "ParityTypeMark",
433     "ParityTypeSpace",
434     "/* UNKNOWN parity keyword */",
435     "/* UNKNOWN parity keyword */",
436     "/* UNKNOWN parity keyword */"
437 };
438 
439 #endif
440 
441 
442 /*
443  * Base sizes of the raw AML resource descriptors, indexed by resource type.
444  * Zero indicates a reserved (and therefore invalid) resource type.
445  */
446 const UINT8                 AcpiGbl_ResourceAmlSizes[] =
447 {
448     /* Small descriptors */
449 
450     0,
451     0,
452     0,
453     0,
454     ACPI_AML_SIZE_SMALL (AML_RESOURCE_IRQ),
455     ACPI_AML_SIZE_SMALL (AML_RESOURCE_DMA),
456     ACPI_AML_SIZE_SMALL (AML_RESOURCE_START_DEPENDENT),
457     ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_DEPENDENT),
458     ACPI_AML_SIZE_SMALL (AML_RESOURCE_IO),
459     ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_IO),
460     ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_DMA),
461     0,
462     0,
463     0,
464     ACPI_AML_SIZE_SMALL (AML_RESOURCE_VENDOR_SMALL),
465     ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_TAG),
466 
467     /* Large descriptors */
468 
469     0,
470     ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY24),
471     ACPI_AML_SIZE_LARGE (AML_RESOURCE_GENERIC_REGISTER),
472     0,
473     ACPI_AML_SIZE_LARGE (AML_RESOURCE_VENDOR_LARGE),
474     ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY32),
475     ACPI_AML_SIZE_LARGE (AML_RESOURCE_FIXED_MEMORY32),
476     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS32),
477     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS16),
478     ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_IRQ),
479     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS64),
480     ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_ADDRESS64),
481     ACPI_AML_SIZE_LARGE (AML_RESOURCE_GPIO),
482     0,
483     ACPI_AML_SIZE_LARGE (AML_RESOURCE_COMMON_SERIALBUS),
484 };
485 
486 const UINT8                 AcpiGbl_ResourceAmlSerialBusSizes[] =
487 {
488     0,
489     ACPI_AML_SIZE_LARGE (AML_RESOURCE_I2C_SERIALBUS),
490     ACPI_AML_SIZE_LARGE (AML_RESOURCE_SPI_SERIALBUS),
491     ACPI_AML_SIZE_LARGE (AML_RESOURCE_UART_SERIALBUS),
492 };
493 
494 
495 /*
496  * Resource types, used to validate the resource length field.
497  * The length of fixed-length types must match exactly, variable
498  * lengths must meet the minimum required length, etc.
499  * Zero indicates a reserved (and therefore invalid) resource type.
500  */
501 static const UINT8          AcpiGbl_ResourceTypes[] =
502 {
503     /* Small descriptors */
504 
505     0,
506     0,
507     0,
508     0,
509     ACPI_SMALL_VARIABLE_LENGTH,     /* 04 IRQ */
510     ACPI_FIXED_LENGTH,              /* 05 DMA */
511     ACPI_SMALL_VARIABLE_LENGTH,     /* 06 StartDependentFunctions */
512     ACPI_FIXED_LENGTH,              /* 07 EndDependentFunctions */
513     ACPI_FIXED_LENGTH,              /* 08 IO */
514     ACPI_FIXED_LENGTH,              /* 09 FixedIO */
515     ACPI_FIXED_LENGTH,              /* 0A FixedDMA */
516     0,
517     0,
518     0,
519     ACPI_VARIABLE_LENGTH,           /* 0E VendorShort */
520     ACPI_FIXED_LENGTH,              /* 0F EndTag */
521 
522     /* Large descriptors */
523 
524     0,
525     ACPI_FIXED_LENGTH,              /* 01 Memory24 */
526     ACPI_FIXED_LENGTH,              /* 02 GenericRegister */
527     0,
528     ACPI_VARIABLE_LENGTH,           /* 04 VendorLong */
529     ACPI_FIXED_LENGTH,              /* 05 Memory32 */
530     ACPI_FIXED_LENGTH,              /* 06 Memory32Fixed */
531     ACPI_VARIABLE_LENGTH,           /* 07 Dword* address */
532     ACPI_VARIABLE_LENGTH,           /* 08 Word* address */
533     ACPI_VARIABLE_LENGTH,           /* 09 ExtendedIRQ */
534     ACPI_VARIABLE_LENGTH,           /* 0A Qword* address */
535     ACPI_FIXED_LENGTH,              /* 0B Extended* address */
536     ACPI_VARIABLE_LENGTH,           /* 0C Gpio* */
537     0,
538     ACPI_VARIABLE_LENGTH            /* 0E *SerialBus */
539 };
540 
541 
542 /*******************************************************************************
543  *
544  * FUNCTION:    AcpiUtWalkAmlResources
545  *
546  * PARAMETERS:  WalkState           - Current walk info
547  * PARAMETERS:  Aml                 - Pointer to the raw AML resource template
548  *              AmlLength           - Length of the entire template
549  *              UserFunction        - Called once for each descriptor found. If
550  *                                    NULL, a pointer to the EndTag is returned
551  *              Context             - Passed to UserFunction
552  *
553  * RETURN:      Status
554  *
555  * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
556  *              once for each resource found.
557  *
558  ******************************************************************************/
559 
560 ACPI_STATUS
561 AcpiUtWalkAmlResources (
562     ACPI_WALK_STATE         *WalkState,
563     UINT8                   *Aml,
564     ACPI_SIZE               AmlLength,
565     ACPI_WALK_AML_CALLBACK  UserFunction,
566     void                    **Context)
567 {
568     ACPI_STATUS             Status;
569     UINT8                   *EndAml;
570     UINT8                   ResourceIndex;
571     UINT32                  Length;
572     UINT32                  Offset = 0;
573     UINT8                   EndTag[2] = {0x79, 0x00};
574 
575 
576     ACPI_FUNCTION_TRACE (UtWalkAmlResources);
577 
578 
579     /*
580      * The absolute minimum resource template is one EndTag descriptor.
581      * However, we will treat a lone EndTag as just a simple buffer.
582      */
583     if (AmlLength < sizeof (AML_RESOURCE_END_TAG))
584     {
585         return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
586     }
587 
588     /* Point to the end of the resource template buffer */
589 
590     EndAml = Aml + AmlLength;
591 
592     /* Walk the byte list, abort on any invalid descriptor type or length */
593 
594     while (Aml < EndAml)
595     {
596         /* Validate the Resource Type and Resource Length */
597 
598         Status = AcpiUtValidateResource (WalkState, Aml, &ResourceIndex);
599         if (ACPI_FAILURE (Status))
600         {
601             /*
602              * Exit on failure. Cannot continue because the descriptor
603              * length may be bogus also.
604              */
605             return_ACPI_STATUS (Status);
606         }
607 
608         /* Get the length of this descriptor */
609 
610         Length = AcpiUtGetDescriptorLength (Aml);
611 
612         /* Invoke the user function */
613 
614         if (UserFunction)
615         {
616             Status = UserFunction (Aml, Length, Offset,
617                 ResourceIndex, Context);
618             if (ACPI_FAILURE (Status))
619             {
620                 return_ACPI_STATUS (Status);
621             }
622         }
623 
624         /* An EndTag descriptor terminates this resource template */
625 
626         if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_END_TAG)
627         {
628             /*
629              * There must be at least one more byte in the buffer for
630              * the 2nd byte of the EndTag
631              */
632             if ((Aml + 1) >= EndAml)
633             {
634                 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
635             }
636 
637             /*
638              * The EndTag opcode must be followed by a zero byte.
639              * Although this byte is technically defined to be a checksum,
640              * in practice, all ASL compilers set this byte to zero.
641              */
642             if (*(Aml + 1) != 0)
643             {
644                 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
645             }
646 
647             /* Return the pointer to the EndTag if requested */
648 
649             if (!UserFunction)
650             {
651                 *Context = Aml;
652             }
653 
654             /* Check if buffer is defined to be longer than the resource length */
655 
656             if (AmlLength > (Offset + Length))
657             {
658                 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
659             }
660 
661             /* Normal exit */
662 
663             return_ACPI_STATUS (AE_OK);
664         }
665 
666         Aml += Length;
667         Offset += Length;
668     }
669 
670     /* Did not find an EndTag descriptor */
671 
672     if (UserFunction)
673     {
674         /* Insert an EndTag anyway. AcpiRsGetListLength always leaves room */
675 
676         (void) AcpiUtValidateResource (WalkState, EndTag, &ResourceIndex);
677         Status = UserFunction (EndTag, 2, Offset, ResourceIndex, Context);
678         if (ACPI_FAILURE (Status))
679         {
680             return_ACPI_STATUS (Status);
681         }
682     }
683 
684     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
685 }
686 
687 
688 /*******************************************************************************
689  *
690  * FUNCTION:    AcpiUtValidateResource
691  *
692  * PARAMETERS:  WalkState           - Current walk info
693  *              Aml                 - Pointer to the raw AML resource descriptor
694  *              ReturnIndex         - Where the resource index is returned. NULL
695  *                                    if the index is not required.
696  *
697  * RETURN:      Status, and optionally the Index into the global resource tables
698  *
699  * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
700  *              Type and Resource Length. Returns an index into the global
701  *              resource information/dispatch tables for later use.
702  *
703  ******************************************************************************/
704 
705 ACPI_STATUS
706 AcpiUtValidateResource (
707     ACPI_WALK_STATE         *WalkState,
708     void                    *Aml,
709     UINT8                   *ReturnIndex)
710 {
711     AML_RESOURCE            *AmlResource;
712     UINT8                   ResourceType;
713     UINT8                   ResourceIndex;
714     ACPI_RS_LENGTH          ResourceLength;
715     ACPI_RS_LENGTH          MinimumResourceLength;
716 
717 
718     ACPI_FUNCTION_ENTRY ();
719 
720 
721     /*
722      * 1) Validate the ResourceType field (Byte 0)
723      */
724     ResourceType = ACPI_GET8 (Aml);
725 
726     /*
727      * Byte 0 contains the descriptor name (Resource Type)
728      * Examine the large/small bit in the resource header
729      */
730     if (ResourceType & ACPI_RESOURCE_NAME_LARGE)
731     {
732         /* Verify the large resource type (name) against the max */
733 
734         if (ResourceType > ACPI_RESOURCE_NAME_LARGE_MAX)
735         {
736             goto InvalidResource;
737         }
738 
739         /*
740          * Large Resource Type -- bits 6:0 contain the name
741          * Translate range 0x80-0x8B to index range 0x10-0x1B
742          */
743         ResourceIndex = (UINT8) (ResourceType - 0x70);
744     }
745     else
746     {
747         /*
748          * Small Resource Type -- bits 6:3 contain the name
749          * Shift range to index range 0x00-0x0F
750          */
751         ResourceIndex = (UINT8)
752             ((ResourceType & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
753     }
754 
755     /*
756      * Check validity of the resource type, via AcpiGbl_ResourceTypes.
757      * Zero indicates an invalid resource.
758      */
759     if (!AcpiGbl_ResourceTypes[ResourceIndex])
760     {
761         goto InvalidResource;
762     }
763 
764     /*
765      * Validate the ResourceLength field. This ensures that the length
766      * is at least reasonable, and guarantees that it is non-zero.
767      */
768     ResourceLength = AcpiUtGetResourceLength (Aml);
769     MinimumResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
770 
771     /* Validate based upon the type of resource - fixed length or variable */
772 
773     switch (AcpiGbl_ResourceTypes[ResourceIndex])
774     {
775     case ACPI_FIXED_LENGTH:
776 
777         /* Fixed length resource, length must match exactly */
778 
779         if (ResourceLength != MinimumResourceLength)
780         {
781             goto BadResourceLength;
782         }
783         break;
784 
785     case ACPI_VARIABLE_LENGTH:
786 
787         /* Variable length resource, length must be at least the minimum */
788 
789         if (ResourceLength < MinimumResourceLength)
790         {
791             goto BadResourceLength;
792         }
793         break;
794 
795     case ACPI_SMALL_VARIABLE_LENGTH:
796 
797         /* Small variable length resource, length can be (Min) or (Min-1) */
798 
799         if ((ResourceLength > MinimumResourceLength) ||
800             (ResourceLength < (MinimumResourceLength - 1)))
801         {
802             goto BadResourceLength;
803         }
804         break;
805 
806     default:
807 
808         /* Shouldn't happen (because of validation earlier), but be sure */
809 
810         goto InvalidResource;
811     }
812 
813     AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
814     if (ResourceType == ACPI_RESOURCE_NAME_SERIAL_BUS)
815     {
816         /* Validate the BusType field */
817 
818         if ((AmlResource->CommonSerialBus.Type == 0) ||
819             (AmlResource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
820         {
821             if (WalkState)
822             {
823                 ACPI_ERROR ((AE_INFO,
824                     "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
825                     AmlResource->CommonSerialBus.Type));
826             }
827             return (AE_AML_INVALID_RESOURCE_TYPE);
828         }
829     }
830 
831     /* Optionally return the resource table index */
832 
833     if (ReturnIndex)
834     {
835         *ReturnIndex = ResourceIndex;
836     }
837 
838     return (AE_OK);
839 
840 
841 InvalidResource:
842 
843     if (WalkState)
844     {
845         ACPI_ERROR ((AE_INFO,
846             "Invalid/unsupported resource descriptor: Type 0x%2.2X",
847             ResourceType));
848     }
849     return (AE_AML_INVALID_RESOURCE_TYPE);
850 
851 BadResourceLength:
852 
853     if (WalkState)
854     {
855         ACPI_ERROR ((AE_INFO,
856             "Invalid resource descriptor length: Type "
857             "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
858             ResourceType, ResourceLength, MinimumResourceLength));
859     }
860     return (AE_AML_BAD_RESOURCE_LENGTH);
861 }
862 
863 
864 /*******************************************************************************
865  *
866  * FUNCTION:    AcpiUtGetResourceType
867  *
868  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
869  *
870  * RETURN:      The Resource Type with no extraneous bits (except the
871  *              Large/Small descriptor bit -- this is left alone)
872  *
873  * DESCRIPTION: Extract the Resource Type/Name from the first byte of
874  *              a resource descriptor.
875  *
876  ******************************************************************************/
877 
878 UINT8
879 AcpiUtGetResourceType (
880     void                    *Aml)
881 {
882     ACPI_FUNCTION_ENTRY ();
883 
884 
885     /*
886      * Byte 0 contains the descriptor name (Resource Type)
887      * Examine the large/small bit in the resource header
888      */
889     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
890     {
891         /* Large Resource Type -- bits 6:0 contain the name */
892 
893         return (ACPI_GET8 (Aml));
894     }
895     else
896     {
897         /* Small Resource Type -- bits 6:3 contain the name */
898 
899         return ((UINT8) (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
900     }
901 }
902 
903 
904 /*******************************************************************************
905  *
906  * FUNCTION:    AcpiUtGetResourceLength
907  *
908  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
909  *
910  * RETURN:      Byte Length
911  *
912  * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
913  *              definition, this does not include the size of the descriptor
914  *              header or the length field itself.
915  *
916  ******************************************************************************/
917 
918 UINT16
919 AcpiUtGetResourceLength (
920     void                    *Aml)
921 {
922     ACPI_RS_LENGTH          ResourceLength;
923 
924 
925     ACPI_FUNCTION_ENTRY ();
926 
927 
928     /*
929      * Byte 0 contains the descriptor name (Resource Type)
930      * Examine the large/small bit in the resource header
931      */
932     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
933     {
934         /* Large Resource type -- bytes 1-2 contain the 16-bit length */
935 
936         ACPI_MOVE_16_TO_16 (&ResourceLength, ACPI_ADD_PTR (UINT8, Aml, 1));
937 
938     }
939     else
940     {
941         /* Small Resource type -- bits 2:0 of byte 0 contain the length */
942 
943         ResourceLength = (UINT16) (ACPI_GET8 (Aml) &
944             ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
945     }
946 
947     return (ResourceLength);
948 }
949 
950 
951 /*******************************************************************************
952  *
953  * FUNCTION:    AcpiUtGetResourceHeaderLength
954  *
955  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
956  *
957  * RETURN:      Length of the AML header (depends on large/small descriptor)
958  *
959  * DESCRIPTION: Get the length of the header for this resource.
960  *
961  ******************************************************************************/
962 
963 UINT8
964 AcpiUtGetResourceHeaderLength (
965     void                    *Aml)
966 {
967     ACPI_FUNCTION_ENTRY ();
968 
969 
970     /* Examine the large/small bit in the resource header */
971 
972     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
973     {
974         return (sizeof (AML_RESOURCE_LARGE_HEADER));
975     }
976     else
977     {
978         return (sizeof (AML_RESOURCE_SMALL_HEADER));
979     }
980 }
981 
982 
983 /*******************************************************************************
984  *
985  * FUNCTION:    AcpiUtGetDescriptorLength
986  *
987  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
988  *
989  * RETURN:      Byte length
990  *
991  * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
992  *              length of the descriptor header and the length field itself.
993  *              Used to walk descriptor lists.
994  *
995  ******************************************************************************/
996 
997 UINT32
998 AcpiUtGetDescriptorLength (
999     void                    *Aml)
1000 {
1001     ACPI_FUNCTION_ENTRY ();
1002 
1003 
1004     /*
1005      * Get the Resource Length (does not include header length) and add
1006      * the header length (depends on if this is a small or large resource)
1007      */
1008     return (AcpiUtGetResourceLength (Aml) +
1009         AcpiUtGetResourceHeaderLength (Aml));
1010 }
1011 
1012 
1013 /*******************************************************************************
1014  *
1015  * FUNCTION:    AcpiUtGetResourceEndTag
1016  *
1017  * PARAMETERS:  ObjDesc         - The resource template buffer object
1018  *              EndTag          - Where the pointer to the EndTag is returned
1019  *
1020  * RETURN:      Status, pointer to the end tag
1021  *
1022  * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
1023  *              Note: allows a buffer length of zero.
1024  *
1025  ******************************************************************************/
1026 
1027 ACPI_STATUS
1028 AcpiUtGetResourceEndTag (
1029     ACPI_OPERAND_OBJECT     *ObjDesc,
1030     UINT8                   **EndTag)
1031 {
1032     ACPI_STATUS             Status;
1033 
1034 
1035     ACPI_FUNCTION_TRACE (UtGetResourceEndTag);
1036 
1037 
1038     /* Allow a buffer length of zero */
1039 
1040     if (!ObjDesc->Buffer.Length)
1041     {
1042         *EndTag = ObjDesc->Buffer.Pointer;
1043         return_ACPI_STATUS (AE_OK);
1044     }
1045 
1046     /* Validate the template and get a pointer to the EndTag */
1047 
1048     Status = AcpiUtWalkAmlResources (NULL, ObjDesc->Buffer.Pointer,
1049         ObjDesc->Buffer.Length, NULL, (void **) EndTag);
1050 
1051     return_ACPI_STATUS (Status);
1052 }
1053