xref: /freebsd/sys/contrib/dev/acpica/components/utilities/utresrc.c (revision 7697e6c4df7188ee3a2aa66f73c572c27479bdf2)
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 - 2026, 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 /*
162  * Base sizes of the raw AML resource descriptors, indexed by resource type.
163  * Zero indicates a reserved (and therefore invalid) resource type.
164  */
165 const UINT8                 AcpiGbl_ResourceAmlSizes[] =
166 {
167     /* Small descriptors */
168 
169     0,
170     0,
171     0,
172     0,
173     ACPI_AML_SIZE_SMALL (AML_RESOURCE_IRQ),
174     ACPI_AML_SIZE_SMALL (AML_RESOURCE_DMA),
175     ACPI_AML_SIZE_SMALL (AML_RESOURCE_START_DEPENDENT),
176     ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_DEPENDENT),
177     ACPI_AML_SIZE_SMALL (AML_RESOURCE_IO),
178     ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_IO),
179     ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_DMA),
180     0,
181     0,
182     0,
183     ACPI_AML_SIZE_SMALL (AML_RESOURCE_VENDOR_SMALL),
184     ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_TAG),
185 
186     /* Large descriptors */
187 
188     0,
189     ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY24),
190     ACPI_AML_SIZE_LARGE (AML_RESOURCE_GENERIC_REGISTER),
191     0,
192     ACPI_AML_SIZE_LARGE (AML_RESOURCE_VENDOR_LARGE),
193     ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY32),
194     ACPI_AML_SIZE_LARGE (AML_RESOURCE_FIXED_MEMORY32),
195     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS32),
196     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS16),
197     ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_IRQ),
198     ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS64),
199     ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_ADDRESS64),
200     ACPI_AML_SIZE_LARGE (AML_RESOURCE_GPIO),
201     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_FUNCTION),
202     ACPI_AML_SIZE_LARGE (AML_RESOURCE_COMMON_SERIALBUS),
203     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_CONFIG),
204     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP),
205     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP_FUNCTION),
206     ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP_CONFIG),
207     ACPI_AML_SIZE_LARGE (AML_RESOURCE_CLOCK_INPUT),
208 
209 };
210 
211 const UINT8                 AcpiGbl_ResourceAmlSerialBusSizes[] =
212 {
213     0,
214     ACPI_AML_SIZE_LARGE (AML_RESOURCE_I2C_SERIALBUS),
215     ACPI_AML_SIZE_LARGE (AML_RESOURCE_SPI_SERIALBUS),
216     ACPI_AML_SIZE_LARGE (AML_RESOURCE_UART_SERIALBUS),
217     ACPI_AML_SIZE_LARGE (AML_RESOURCE_CSI2_SERIALBUS),
218 };
219 
220 
221 /*
222  * Resource types, used to validate the resource length field.
223  * The length of fixed-length types must match exactly, variable
224  * lengths must meet the minimum required length, etc.
225  * Zero indicates a reserved (and therefore invalid) resource type.
226  */
227 static const UINT8          AcpiGbl_ResourceTypes[] =
228 {
229     /* Small descriptors */
230 
231     0,
232     0,
233     0,
234     0,
235     ACPI_SMALL_VARIABLE_LENGTH,     /* 04 IRQ */
236     ACPI_FIXED_LENGTH,              /* 05 DMA */
237     ACPI_SMALL_VARIABLE_LENGTH,     /* 06 StartDependentFunctions */
238     ACPI_FIXED_LENGTH,              /* 07 EndDependentFunctions */
239     ACPI_FIXED_LENGTH,              /* 08 IO */
240     ACPI_FIXED_LENGTH,              /* 09 FixedIO */
241     ACPI_FIXED_LENGTH,              /* 0A FixedDMA */
242     0,
243     0,
244     0,
245     ACPI_VARIABLE_LENGTH,           /* 0E VendorShort */
246     ACPI_FIXED_LENGTH,              /* 0F EndTag */
247 
248     /* Large descriptors */
249 
250     0,
251     ACPI_FIXED_LENGTH,              /* 01 Memory24 */
252     ACPI_FIXED_LENGTH,              /* 02 GenericRegister */
253     0,
254     ACPI_VARIABLE_LENGTH,           /* 04 VendorLong */
255     ACPI_FIXED_LENGTH,              /* 05 Memory32 */
256     ACPI_FIXED_LENGTH,              /* 06 Memory32Fixed */
257     ACPI_VARIABLE_LENGTH,           /* 07 Dword* address */
258     ACPI_VARIABLE_LENGTH,           /* 08 Word* address */
259     ACPI_VARIABLE_LENGTH,           /* 09 ExtendedIRQ */
260     ACPI_VARIABLE_LENGTH,           /* 0A Qword* address */
261     ACPI_FIXED_LENGTH,              /* 0B Extended* address */
262     ACPI_VARIABLE_LENGTH,           /* 0C Gpio* */
263     ACPI_VARIABLE_LENGTH,           /* 0D PinFunction */
264     ACPI_VARIABLE_LENGTH,           /* 0E *SerialBus */
265     ACPI_VARIABLE_LENGTH,           /* 0F PinConfig */
266     ACPI_VARIABLE_LENGTH,           /* 10 PinGroup */
267     ACPI_VARIABLE_LENGTH,           /* 11 PinGroupFunction */
268     ACPI_VARIABLE_LENGTH,           /* 12 PinGroupConfig */
269     ACPI_VARIABLE_LENGTH,           /* 13 ClockInput */
270 };
271 
272 
273 /*******************************************************************************
274  *
275  * FUNCTION:    AcpiUtWalkAmlResources
276  *
277  * PARAMETERS:  WalkState           - Current walk info
278  * PARAMETERS:  Aml                 - Pointer to the raw AML resource template
279  *              AmlLength           - Length of the entire template
280  *              UserFunction        - Called once for each descriptor found. If
281  *                                    NULL, a pointer to the EndTag is returned
282  *              Context             - Passed to UserFunction
283  *
284  * RETURN:      Status
285  *
286  * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
287  *              once for each resource found.
288  *
289  ******************************************************************************/
290 
291 ACPI_STATUS
AcpiUtWalkAmlResources(ACPI_WALK_STATE * WalkState,UINT8 * Aml,ACPI_SIZE AmlLength,ACPI_WALK_AML_CALLBACK UserFunction,void ** Context)292 AcpiUtWalkAmlResources (
293     ACPI_WALK_STATE         *WalkState,
294     UINT8                   *Aml,
295     ACPI_SIZE               AmlLength,
296     ACPI_WALK_AML_CALLBACK  UserFunction,
297     void                    **Context)
298 {
299     ACPI_STATUS             Status;
300     UINT8                   *EndAml;
301     UINT8                   ResourceIndex;
302     UINT32                  Length;
303     UINT32                  Offset = 0;
304     UINT8                   EndTag[2] = {0x79, 0x00};
305 
306 
307     ACPI_FUNCTION_TRACE (UtWalkAmlResources);
308 
309 
310     /* The absolute minimum resource template is one EndTag descriptor */
311 
312     if (AmlLength < sizeof (AML_RESOURCE_END_TAG))
313     {
314         return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
315     }
316 
317     /* Point to the end of the resource template buffer */
318 
319     EndAml = Aml + AmlLength;
320 
321     /* Walk the byte list, abort on any invalid descriptor type or length */
322 
323     while (Aml < EndAml)
324     {
325         /*
326          * Validate that the remaining buffer space can hold enough
327          * bytes to safely access fields during validation.
328          * For large resource descriptors (bit 7 set), we need enough
329          * bytes to access the Type field in SerialBus resources.
330          * Small resource descriptors only need sizeof(AML_RESOURCE_END_TAG).
331          */
332         if ((ACPI_SIZE) (EndAml - Aml) < sizeof (AML_RESOURCE_END_TAG))
333         {
334             return_ACPI_STATUS (AE_AML_BUFFER_LENGTH);
335         }
336 
337         /*
338          * For large resource descriptors, ensure enough space for
339          * the header plus SerialBus Type field access.
340          */
341         if ((ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE) &&
342             ((ACPI_SIZE) (EndAml - Aml) <
343                 ACPI_OFFSET (AML_RESOURCE_COMMON_SERIALBUS, Type) + 1))
344         {
345             return_ACPI_STATUS (AE_AML_BUFFER_LENGTH);
346         }
347 
348         /* Validate the Resource Type and Resource Length */
349 
350         Status = AcpiUtValidateResource (WalkState, Aml, &ResourceIndex);
351         if (ACPI_FAILURE (Status))
352         {
353             /*
354              * Exit on failure. Cannot continue because the descriptor
355              * length may be bogus also.
356              */
357             return_ACPI_STATUS (Status);
358         }
359 
360         /* Get the length of this descriptor */
361 
362         Length = AcpiUtGetDescriptorLength (Aml);
363 
364         /*
365          * Validate that the descriptor length doesn't exceed the
366          * remaining buffer size to prevent reading beyond the end.
367          */
368         if (Length > (ACPI_SIZE) (EndAml - Aml))
369         {
370             return_ACPI_STATUS (AE_AML_BUFFER_LENGTH);
371         }
372 
373         /* Invoke the user function */
374 
375         if (UserFunction)
376         {
377             Status = UserFunction (
378                 Aml, Length, Offset, ResourceIndex, Context);
379             if (ACPI_FAILURE (Status))
380             {
381                 return_ACPI_STATUS (Status);
382             }
383         }
384 
385         /* An EndTag descriptor terminates this resource template */
386 
387         if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_END_TAG)
388         {
389             /*
390              * There must be at least one more byte in the buffer for
391              * the 2nd byte of the EndTag
392              */
393             if ((Aml + 1) >= EndAml)
394             {
395                 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
396             }
397 
398             /*
399              * Don't attempt to perform any validation on the 2nd byte.
400              * Although all known ASL compilers insert a zero for the 2nd
401              * byte, it can also be a checksum (as per the ACPI spec),
402              * and this is occasionally seen in the field. July 2017.
403              */
404 
405             /* Return the pointer to the EndTag if requested */
406 
407             if (!UserFunction)
408             {
409                 *Context = Aml;
410             }
411 
412             /* Normal exit */
413 
414             return_ACPI_STATUS (AE_OK);
415         }
416 
417         Aml += Length;
418         Offset += Length;
419     }
420 
421     /* Did not find an EndTag descriptor */
422 
423     if (UserFunction)
424     {
425         /* Insert an EndTag anyway. AcpiRsGetListLength always leaves room */
426 
427         (void) AcpiUtValidateResource (WalkState, EndTag, &ResourceIndex);
428         Status = UserFunction (EndTag, 2, Offset, ResourceIndex, Context);
429         if (ACPI_FAILURE (Status))
430         {
431             return_ACPI_STATUS (Status);
432         }
433     }
434 
435     return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
436 }
437 
438 
439 /*******************************************************************************
440  *
441  * FUNCTION:    AcpiUtValidateResource
442  *
443  * PARAMETERS:  WalkState           - Current walk info
444  *              Aml                 - Pointer to the raw AML resource descriptor
445  *              ReturnIndex         - Where the resource index is returned. NULL
446  *                                    if the index is not required.
447  *
448  * RETURN:      Status, and optionally the Index into the global resource tables
449  *
450  * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
451  *              Type and Resource Length. Returns an index into the global
452  *              resource information/dispatch tables for later use.
453  *
454  ******************************************************************************/
455 
456 ACPI_STATUS
AcpiUtValidateResource(ACPI_WALK_STATE * WalkState,void * Aml,UINT8 * ReturnIndex)457 AcpiUtValidateResource (
458     ACPI_WALK_STATE         *WalkState,
459     void                    *Aml,
460     UINT8                   *ReturnIndex)
461 {
462     AML_RESOURCE            *AmlResource;
463     UINT8                   ResourceType;
464     UINT8                   ResourceIndex;
465     ACPI_RS_LENGTH          ResourceLength;
466     ACPI_RS_LENGTH          MinimumResourceLength;
467 
468 
469     ACPI_FUNCTION_ENTRY ();
470 
471 
472     /*
473      * 1) Validate the ResourceType field (Byte 0)
474      */
475     ResourceType = ACPI_GET8 (Aml);
476 
477     /*
478      * Byte 0 contains the descriptor name (Resource Type)
479      * Examine the large/small bit in the resource header
480      */
481     if (ResourceType & ACPI_RESOURCE_NAME_LARGE)
482     {
483         /* Verify the large resource type (name) against the max */
484 
485         if (ResourceType > ACPI_RESOURCE_NAME_LARGE_MAX)
486         {
487             goto InvalidResource;
488         }
489 
490         /*
491          * Large Resource Type -- bits 6:0 contain the name
492          * Translate range 0x80-0x8B to index range 0x10-0x1B
493          */
494         ResourceIndex = (UINT8) (ResourceType - 0x70);
495     }
496     else
497     {
498         /*
499          * Small Resource Type -- bits 6:3 contain the name
500          * Shift range to index range 0x00-0x0F
501          */
502         ResourceIndex = (UINT8)
503             ((ResourceType & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
504     }
505 
506     /*
507      * Check validity of the resource type, via AcpiGbl_ResourceTypes.
508      * Zero indicates an invalid resource.
509      */
510     if (!AcpiGbl_ResourceTypes[ResourceIndex])
511     {
512         goto InvalidResource;
513     }
514 
515     /*
516      * Validate the ResourceLength field. This ensures that the length
517      * is at least reasonable, and guarantees that it is non-zero.
518      */
519     ResourceLength = AcpiUtGetResourceLength (Aml);
520     MinimumResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
521 
522     /* Validate based upon the type of resource - fixed length or variable */
523 
524     switch (AcpiGbl_ResourceTypes[ResourceIndex])
525     {
526     case ACPI_FIXED_LENGTH:
527 
528         /* Fixed length resource, length must match exactly */
529 
530         if (ResourceLength != MinimumResourceLength)
531         {
532             goto BadResourceLength;
533         }
534         break;
535 
536     case ACPI_VARIABLE_LENGTH:
537 
538         /* Variable length resource, length must be at least the minimum */
539 
540         if (ResourceLength < MinimumResourceLength)
541         {
542             goto BadResourceLength;
543         }
544         break;
545 
546     case ACPI_SMALL_VARIABLE_LENGTH:
547 
548         /* Small variable length resource, length can be (Min) or (Min-1) */
549 
550         if ((ResourceLength > MinimumResourceLength) ||
551             (ResourceLength < (MinimumResourceLength - 1)))
552         {
553             goto BadResourceLength;
554         }
555         break;
556 
557     default:
558 
559         /* Shouldn't happen (because of validation earlier), but be sure */
560 
561         goto InvalidResource;
562     }
563 
564     AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
565     if (ResourceType == ACPI_RESOURCE_NAME_SERIAL_BUS)
566     {
567         /* Validate the BusType field */
568 
569         if ((AmlResource->CommonSerialBus.Type == 0) ||
570             (AmlResource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
571         {
572             if (WalkState)
573             {
574                 ACPI_ERROR ((AE_INFO,
575                     "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
576                     AmlResource->CommonSerialBus.Type));
577             }
578             return (AE_AML_INVALID_RESOURCE_TYPE);
579         }
580     }
581 
582     /* Optionally return the resource table index */
583 
584     if (ReturnIndex)
585     {
586         *ReturnIndex = ResourceIndex;
587     }
588 
589     return (AE_OK);
590 
591 
592 InvalidResource:
593 
594     if (WalkState)
595     {
596         ACPI_ERROR ((AE_INFO,
597             "Invalid/unsupported resource descriptor: Type 0x%2.2X",
598             ResourceType));
599     }
600     return (AE_AML_INVALID_RESOURCE_TYPE);
601 
602 BadResourceLength:
603 
604     if (WalkState)
605     {
606         ACPI_ERROR ((AE_INFO,
607             "Invalid resource descriptor length: Type "
608             "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
609             ResourceType, ResourceLength, MinimumResourceLength));
610     }
611     return (AE_AML_BAD_RESOURCE_LENGTH);
612 }
613 
614 
615 /*******************************************************************************
616  *
617  * FUNCTION:    AcpiUtGetResourceType
618  *
619  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
620  *
621  * RETURN:      The Resource Type with no extraneous bits (except the
622  *              Large/Small descriptor bit -- this is left alone)
623  *
624  * DESCRIPTION: Extract the Resource Type/Name from the first byte of
625  *              a resource descriptor.
626  *
627  ******************************************************************************/
628 
629 UINT8
AcpiUtGetResourceType(void * Aml)630 AcpiUtGetResourceType (
631     void                    *Aml)
632 {
633     ACPI_FUNCTION_ENTRY ();
634 
635 
636     /*
637      * Byte 0 contains the descriptor name (Resource Type)
638      * Examine the large/small bit in the resource header
639      */
640     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
641     {
642         /* Large Resource Type -- bits 6:0 contain the name */
643 
644         return (ACPI_GET8 (Aml));
645     }
646     else
647     {
648         /* Small Resource Type -- bits 6:3 contain the name */
649 
650         return ((UINT8) (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
651     }
652 }
653 
654 
655 /*******************************************************************************
656  *
657  * FUNCTION:    AcpiUtGetResourceLength
658  *
659  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
660  *
661  * RETURN:      Byte Length
662  *
663  * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
664  *              definition, this does not include the size of the descriptor
665  *              header or the length field itself.
666  *
667  ******************************************************************************/
668 
669 UINT16
AcpiUtGetResourceLength(void * Aml)670 AcpiUtGetResourceLength (
671     void                    *Aml)
672 {
673     ACPI_RS_LENGTH          ResourceLength;
674 
675 
676     ACPI_FUNCTION_ENTRY ();
677 
678 
679     /*
680      * Byte 0 contains the descriptor name (Resource Type)
681      * Examine the large/small bit in the resource header
682      */
683     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
684     {
685         /* Large Resource type -- bytes 1-2 contain the 16-bit length */
686 
687         ACPI_MOVE_16_TO_16 (&ResourceLength, ACPI_ADD_PTR (UINT8, Aml, 1));
688 
689     }
690     else
691     {
692         /* Small Resource type -- bits 2:0 of byte 0 contain the length */
693 
694         ResourceLength = (UINT16) (ACPI_GET8 (Aml) &
695             ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
696     }
697 
698     return (ResourceLength);
699 }
700 
701 
702 /*******************************************************************************
703  *
704  * FUNCTION:    AcpiUtGetResourceHeaderLength
705  *
706  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
707  *
708  * RETURN:      Length of the AML header (depends on large/small descriptor)
709  *
710  * DESCRIPTION: Get the length of the header for this resource.
711  *
712  ******************************************************************************/
713 
714 UINT8
AcpiUtGetResourceHeaderLength(void * Aml)715 AcpiUtGetResourceHeaderLength (
716     void                    *Aml)
717 {
718     ACPI_FUNCTION_ENTRY ();
719 
720 
721     /* Examine the large/small bit in the resource header */
722 
723     if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
724     {
725         return (sizeof (AML_RESOURCE_LARGE_HEADER));
726     }
727     else
728     {
729         return (sizeof (AML_RESOURCE_SMALL_HEADER));
730     }
731 }
732 
733 
734 /*******************************************************************************
735  *
736  * FUNCTION:    AcpiUtGetDescriptorLength
737  *
738  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
739  *
740  * RETURN:      Byte length
741  *
742  * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
743  *              length of the descriptor header and the length field itself.
744  *              Used to walk descriptor lists.
745  *
746  ******************************************************************************/
747 
748 UINT32
AcpiUtGetDescriptorLength(void * Aml)749 AcpiUtGetDescriptorLength (
750     void                    *Aml)
751 {
752     ACPI_FUNCTION_ENTRY ();
753 
754 
755     /*
756      * Get the Resource Length (does not include header length) and add
757      * the header length (depends on if this is a small or large resource)
758      */
759     return (AcpiUtGetResourceLength (Aml) +
760         AcpiUtGetResourceHeaderLength (Aml));
761 }
762 
763 
764 /*******************************************************************************
765  *
766  * FUNCTION:    AcpiUtGetResourceEndTag
767  *
768  * PARAMETERS:  ObjDesc         - The resource template buffer object
769  *              EndTag          - Where the pointer to the EndTag is returned
770  *
771  * RETURN:      Status, pointer to the end tag
772  *
773  * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
774  *              Note: allows a buffer length of zero.
775  *
776  ******************************************************************************/
777 
778 ACPI_STATUS
AcpiUtGetResourceEndTag(ACPI_OPERAND_OBJECT * ObjDesc,UINT8 ** EndTag)779 AcpiUtGetResourceEndTag (
780     ACPI_OPERAND_OBJECT     *ObjDesc,
781     UINT8                   **EndTag)
782 {
783     ACPI_STATUS             Status;
784 
785 
786     ACPI_FUNCTION_TRACE (UtGetResourceEndTag);
787 
788 
789     /* Allow a buffer length of zero */
790 
791     if (!ObjDesc->Buffer.Length)
792     {
793         *EndTag = ObjDesc->Buffer.Pointer;
794         return_ACPI_STATUS (AE_OK);
795     }
796 
797     /* Validate the template and get a pointer to the EndTag */
798 
799     Status = AcpiUtWalkAmlResources (NULL, ObjDesc->Buffer.Pointer,
800         ObjDesc->Buffer.Length, NULL, (void **) EndTag);
801 
802     return_ACPI_STATUS (Status);
803 }
804