1 /******************************************************************************
2  *
3  * Module Name: nsxfname - Public interfaces to the ACPI subsystem
4  *                         ACPI Namespace oriented interfaces
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2017, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44 
45 #define EXPORT_ACPI_INTERFACES
46 
47 #include "acpi.h"
48 #include "accommon.h"
49 #include "acnamesp.h"
50 #include "acparser.h"
51 #include "amlcode.h"
52 
53 
54 #define _COMPONENT          ACPI_NAMESPACE
55         ACPI_MODULE_NAME    ("nsxfname")
56 
57 /* Local prototypes */
58 
59 static char *
60 AcpiNsCopyDeviceId (
61     ACPI_PNP_DEVICE_ID      *Dest,
62     ACPI_PNP_DEVICE_ID      *Source,
63     char                    *StringArea);
64 
65 
66 /******************************************************************************
67  *
68  * FUNCTION:    AcpiGetHandle
69  *
70  * PARAMETERS:  Parent          - Object to search under (search scope).
71  *              Pathname        - Pointer to an asciiz string containing the
72  *                                name
73  *              RetHandle       - Where the return handle is returned
74  *
75  * RETURN:      Status
76  *
77  * DESCRIPTION: This routine will search for a caller specified name in the
78  *              name space. The caller can restrict the search region by
79  *              specifying a non NULL parent. The parent value is itself a
80  *              namespace handle.
81  *
82  ******************************************************************************/
83 
84 ACPI_STATUS
85 AcpiGetHandle (
86     ACPI_HANDLE             Parent,
87     ACPI_STRING             Pathname,
88     ACPI_HANDLE             *RetHandle)
89 {
90     ACPI_STATUS             Status;
91     ACPI_NAMESPACE_NODE     *Node = NULL;
92     ACPI_NAMESPACE_NODE     *PrefixNode = NULL;
93 
94 
95     ACPI_FUNCTION_ENTRY ();
96 
97 
98     /* Parameter Validation */
99 
100     if (!RetHandle || !Pathname)
101     {
102         return (AE_BAD_PARAMETER);
103     }
104 
105     /* Convert a parent handle to a prefix node */
106 
107     if (Parent)
108     {
109         PrefixNode = AcpiNsValidateHandle (Parent);
110         if (!PrefixNode)
111         {
112             return (AE_BAD_PARAMETER);
113         }
114     }
115 
116     /*
117      * Valid cases are:
118      * 1) Fully qualified pathname
119      * 2) Parent + Relative pathname
120      *
121      * Error for <null Parent + relative path>
122      */
123     if (ACPI_IS_ROOT_PREFIX (Pathname[0]))
124     {
125         /* Pathname is fully qualified (starts with '\') */
126 
127         /* Special case for root-only, since we can't search for it */
128 
129         if (!strcmp (Pathname, ACPI_NS_ROOT_PATH))
130         {
131             *RetHandle = ACPI_CAST_PTR (ACPI_HANDLE, AcpiGbl_RootNode);
132             return (AE_OK);
133         }
134     }
135     else if (!PrefixNode)
136     {
137         /* Relative path with null prefix is disallowed */
138 
139         return (AE_BAD_PARAMETER);
140     }
141 
142     /* Find the Node and convert to a handle */
143 
144     Status = AcpiNsGetNode (PrefixNode, Pathname, ACPI_NS_NO_UPSEARCH, &Node);
145     if (ACPI_SUCCESS (Status))
146     {
147         *RetHandle = ACPI_CAST_PTR (ACPI_HANDLE, Node);
148     }
149 
150     return (Status);
151 }
152 
153 ACPI_EXPORT_SYMBOL (AcpiGetHandle)
154 
155 
156 /******************************************************************************
157  *
158  * FUNCTION:    AcpiGetName
159  *
160  * PARAMETERS:  Handle          - Handle to be converted to a pathname
161  *              NameType        - Full pathname or single segment
162  *              Buffer          - Buffer for returned path
163  *
164  * RETURN:      Pointer to a string containing the fully qualified Name.
165  *
166  * DESCRIPTION: This routine returns the fully qualified name associated with
167  *              the Handle parameter. This and the AcpiPathnameToHandle are
168  *              complementary functions.
169  *
170  ******************************************************************************/
171 
172 ACPI_STATUS
173 AcpiGetName (
174     ACPI_HANDLE             Handle,
175     UINT32                  NameType,
176     ACPI_BUFFER             *Buffer)
177 {
178     ACPI_STATUS             Status;
179 
180 
181     /* Parameter validation */
182 
183     if (NameType > ACPI_NAME_TYPE_MAX)
184     {
185         return (AE_BAD_PARAMETER);
186     }
187 
188     Status = AcpiUtValidateBuffer (Buffer);
189     if (ACPI_FAILURE (Status))
190     {
191         return (Status);
192     }
193 
194     /*
195      * Wants the single segment ACPI name.
196      * Validate handle and convert to a namespace Node
197      */
198     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
199     if (ACPI_FAILURE (Status))
200     {
201         return (Status);
202     }
203 
204     if (NameType == ACPI_FULL_PATHNAME ||
205         NameType == ACPI_FULL_PATHNAME_NO_TRAILING)
206     {
207         /* Get the full pathname (From the namespace root) */
208 
209         Status = AcpiNsHandleToPathname (Handle, Buffer,
210             NameType == ACPI_FULL_PATHNAME ? FALSE : TRUE);
211     }
212     else
213     {
214         /* Get the single name */
215 
216         Status = AcpiNsHandleToName (Handle, Buffer);
217     }
218 
219     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
220     return (Status);
221 }
222 
223 ACPI_EXPORT_SYMBOL (AcpiGetName)
224 
225 
226 /******************************************************************************
227  *
228  * FUNCTION:    AcpiNsCopyDeviceId
229  *
230  * PARAMETERS:  Dest                - Pointer to the destination PNP_DEVICE_ID
231  *              Source              - Pointer to the source PNP_DEVICE_ID
232  *              StringArea          - Pointer to where to copy the dest string
233  *
234  * RETURN:      Pointer to the next string area
235  *
236  * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
237  *
238  ******************************************************************************/
239 
240 static char *
241 AcpiNsCopyDeviceId (
242     ACPI_PNP_DEVICE_ID      *Dest,
243     ACPI_PNP_DEVICE_ID      *Source,
244     char                    *StringArea)
245 {
246     /* Create the destination PNP_DEVICE_ID */
247 
248     Dest->String = StringArea;
249     Dest->Length = Source->Length;
250 
251     /* Copy actual string and return a pointer to the next string area */
252 
253     memcpy (StringArea, Source->String, Source->Length);
254     return (StringArea + Source->Length);
255 }
256 
257 
258 /******************************************************************************
259  *
260  * FUNCTION:    AcpiGetObjectInfo
261  *
262  * PARAMETERS:  Handle              - Object Handle
263  *              ReturnBuffer        - Where the info is returned
264  *
265  * RETURN:      Status
266  *
267  * DESCRIPTION: Returns information about an object as gleaned from the
268  *              namespace node and possibly by running several standard
269  *              control methods (Such as in the case of a device.)
270  *
271  * For Device and Processor objects, run the Device _HID, _UID, _CID, _STA,
272  * _CLS, _ADR, _SxW, and _SxD methods.
273  *
274  * Note: Allocates the return buffer, must be freed by the caller.
275  *
276  * Note: This interface is intended to be used during the initial device
277  * discovery namespace traversal. Therefore, no complex methods can be
278  * executed, especially those that access operation regions. Therefore, do
279  * not add any additional methods that could cause problems in this area.
280  * this was the fate of the _SUB method which was found to cause such
281  * problems and was removed (11/2015).
282  *
283  ******************************************************************************/
284 
285 ACPI_STATUS
286 AcpiGetObjectInfo (
287     ACPI_HANDLE             Handle,
288     ACPI_DEVICE_INFO        **ReturnBuffer)
289 {
290     ACPI_NAMESPACE_NODE     *Node;
291     ACPI_DEVICE_INFO        *Info;
292     ACPI_PNP_DEVICE_ID_LIST *CidList = NULL;
293     ACPI_PNP_DEVICE_ID      *Hid = NULL;
294     ACPI_PNP_DEVICE_ID      *Uid = NULL;
295     ACPI_PNP_DEVICE_ID      *Cls = NULL;
296     char                    *NextIdString;
297     ACPI_OBJECT_TYPE        Type;
298     ACPI_NAME               Name;
299     UINT8                   ParamCount= 0;
300     UINT16                  Valid = 0;
301     UINT32                  InfoSize;
302     UINT32                  i;
303     ACPI_STATUS             Status;
304 
305 
306     /* Parameter validation */
307 
308     if (!Handle || !ReturnBuffer)
309     {
310         return (AE_BAD_PARAMETER);
311     }
312 
313     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
314     if (ACPI_FAILURE (Status))
315     {
316         return (Status);
317     }
318 
319     Node = AcpiNsValidateHandle (Handle);
320     if (!Node)
321     {
322         (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
323         return (AE_BAD_PARAMETER);
324     }
325 
326     /* Get the namespace node data while the namespace is locked */
327 
328     InfoSize = sizeof (ACPI_DEVICE_INFO);
329     Type = Node->Type;
330     Name = Node->Name.Integer;
331 
332     if (Node->Type == ACPI_TYPE_METHOD)
333     {
334         ParamCount = Node->Object->Method.ParamCount;
335     }
336 
337     Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
338     if (ACPI_FAILURE (Status))
339     {
340         return (Status);
341     }
342 
343     if ((Type == ACPI_TYPE_DEVICE) ||
344         (Type == ACPI_TYPE_PROCESSOR))
345     {
346         /*
347          * Get extra info for ACPI Device/Processor objects only:
348          * Run the Device _HID, _UID, _CLS, and _CID methods.
349          *
350          * Note: none of these methods are required, so they may or may
351          * not be present for this device. The Info->Valid bitfield is used
352          * to indicate which methods were found and run successfully.
353          */
354 
355         /* Execute the Device._HID method */
356 
357         Status = AcpiUtExecute_HID (Node, &Hid);
358         if (ACPI_SUCCESS (Status))
359         {
360             InfoSize += Hid->Length;
361             Valid |= ACPI_VALID_HID;
362         }
363 
364         /* Execute the Device._UID method */
365 
366         Status = AcpiUtExecute_UID (Node, &Uid);
367         if (ACPI_SUCCESS (Status))
368         {
369             InfoSize += Uid->Length;
370             Valid |= ACPI_VALID_UID;
371         }
372 
373         /* Execute the Device._CID method */
374 
375         Status = AcpiUtExecute_CID (Node, &CidList);
376         if (ACPI_SUCCESS (Status))
377         {
378             /* Add size of CID strings and CID pointer array */
379 
380             InfoSize += (CidList->ListSize - sizeof (ACPI_PNP_DEVICE_ID_LIST));
381             Valid |= ACPI_VALID_CID;
382         }
383 
384         /* Execute the Device._CLS method */
385 
386         Status = AcpiUtExecute_CLS (Node, &Cls);
387         if (ACPI_SUCCESS (Status))
388         {
389             InfoSize += Cls->Length;
390             Valid |= ACPI_VALID_CLS;
391         }
392     }
393 
394     /*
395      * Now that we have the variable-length data, we can allocate the
396      * return buffer
397      */
398     Info = ACPI_ALLOCATE_ZEROED (InfoSize);
399     if (!Info)
400     {
401         Status = AE_NO_MEMORY;
402         goto Cleanup;
403     }
404 
405     /* Get the fixed-length data */
406 
407     if ((Type == ACPI_TYPE_DEVICE) ||
408         (Type == ACPI_TYPE_PROCESSOR))
409     {
410         /*
411          * Get extra info for ACPI Device/Processor objects only:
412          * Run the _STA, _ADR and, SxW, and _SxD methods.
413          *
414          * Notes: none of these methods are required, so they may or may
415          * not be present for this device. The Info->Valid bitfield is used
416          * to indicate which methods were found and run successfully.
417          *
418          * For _STA, if the method does not exist, then (as per the ACPI
419          * specification), the returned CurrentStatus flags will indicate
420          * that the device is present/functional/enabled. Otherwise, the
421          * CurrentStatus flags reflect the value returned from _STA.
422          */
423 
424         /* Execute the Device._STA method */
425 
426         Status = AcpiUtExecute_STA (Node, &Info->CurrentStatus);
427         if (ACPI_SUCCESS (Status))
428         {
429             Valid |= ACPI_VALID_STA;
430         }
431 
432         /* Execute the Device._ADR method */
433 
434         Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR, Node,
435             &Info->Address);
436         if (ACPI_SUCCESS (Status))
437         {
438             Valid |= ACPI_VALID_ADR;
439         }
440 
441         /* Execute the Device._SxW methods */
442 
443         Status = AcpiUtExecutePowerMethods (Node,
444             AcpiGbl_LowestDstateNames, ACPI_NUM_SxW_METHODS,
445             Info->LowestDstates);
446         if (ACPI_SUCCESS (Status))
447         {
448             Valid |= ACPI_VALID_SXWS;
449         }
450 
451         /* Execute the Device._SxD methods */
452 
453         Status = AcpiUtExecutePowerMethods (Node,
454             AcpiGbl_HighestDstateNames, ACPI_NUM_SxD_METHODS,
455             Info->HighestDstates);
456         if (ACPI_SUCCESS (Status))
457         {
458             Valid |= ACPI_VALID_SXDS;
459         }
460     }
461 
462     /*
463      * Create a pointer to the string area of the return buffer.
464      * Point to the end of the base ACPI_DEVICE_INFO structure.
465      */
466     NextIdString = ACPI_CAST_PTR (char, Info->CompatibleIdList.Ids);
467     if (CidList)
468     {
469         /* Point past the CID PNP_DEVICE_ID array */
470 
471         NextIdString += ((ACPI_SIZE) CidList->Count * sizeof (ACPI_PNP_DEVICE_ID));
472     }
473 
474     /*
475      * Copy the HID, UID, and CIDs to the return buffer. The variable-length
476      * strings are copied to the reserved area at the end of the buffer.
477      *
478      * For HID and CID, check if the ID is a PCI Root Bridge.
479      */
480     if (Hid)
481     {
482         NextIdString = AcpiNsCopyDeviceId (&Info->HardwareId,
483             Hid, NextIdString);
484 
485         if (AcpiUtIsPciRootBridge (Hid->String))
486         {
487             Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
488         }
489     }
490 
491     if (Uid)
492     {
493         NextIdString = AcpiNsCopyDeviceId (&Info->UniqueId,
494             Uid, NextIdString);
495     }
496 
497     if (CidList)
498     {
499         Info->CompatibleIdList.Count = CidList->Count;
500         Info->CompatibleIdList.ListSize = CidList->ListSize;
501 
502         /* Copy each CID */
503 
504         for (i = 0; i < CidList->Count; i++)
505         {
506             NextIdString = AcpiNsCopyDeviceId (&Info->CompatibleIdList.Ids[i],
507                 &CidList->Ids[i], NextIdString);
508 
509             if (AcpiUtIsPciRootBridge (CidList->Ids[i].String))
510             {
511                 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
512             }
513         }
514     }
515 
516     if (Cls)
517     {
518         NextIdString = AcpiNsCopyDeviceId (&Info->ClassCode,
519             Cls, NextIdString);
520     }
521 
522     /* Copy the fixed-length data */
523 
524     Info->InfoSize = InfoSize;
525     Info->Type = Type;
526     Info->Name = Name;
527     Info->ParamCount = ParamCount;
528     Info->Valid = Valid;
529 
530     *ReturnBuffer = Info;
531     Status = AE_OK;
532 
533 
534 Cleanup:
535     if (Hid)
536     {
537         ACPI_FREE (Hid);
538     }
539     if (Uid)
540     {
541         ACPI_FREE (Uid);
542     }
543     if (CidList)
544     {
545         ACPI_FREE (CidList);
546     }
547     if (Cls)
548     {
549         ACPI_FREE (Cls);
550     }
551     return (Status);
552 }
553 
554 ACPI_EXPORT_SYMBOL (AcpiGetObjectInfo)
555 
556 
557 /******************************************************************************
558  *
559  * FUNCTION:    AcpiInstallMethod
560  *
561  * PARAMETERS:  Buffer         - An ACPI table containing one control method
562  *
563  * RETURN:      Status
564  *
565  * DESCRIPTION: Install a control method into the namespace. If the method
566  *              name already exists in the namespace, it is overwritten. The
567  *              input buffer must contain a valid DSDT or SSDT containing a
568  *              single control method.
569  *
570  ******************************************************************************/
571 
572 ACPI_STATUS
573 AcpiInstallMethod (
574     UINT8                   *Buffer)
575 {
576     ACPI_TABLE_HEADER       *Table = ACPI_CAST_PTR (ACPI_TABLE_HEADER, Buffer);
577     UINT8                   *AmlBuffer;
578     UINT8                   *AmlStart;
579     char                    *Path;
580     ACPI_NAMESPACE_NODE     *Node;
581     ACPI_OPERAND_OBJECT     *MethodObj;
582     ACPI_PARSE_STATE        ParserState;
583     UINT32                  AmlLength;
584     UINT16                  Opcode;
585     UINT8                   MethodFlags;
586     ACPI_STATUS             Status;
587 
588 
589     /* Parameter validation */
590 
591     if (!Buffer)
592     {
593         return (AE_BAD_PARAMETER);
594     }
595 
596     /* Table must be a DSDT or SSDT */
597 
598     if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) &&
599         !ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
600     {
601         return (AE_BAD_HEADER);
602     }
603 
604     /* First AML opcode in the table must be a control method */
605 
606     ParserState.Aml = Buffer + sizeof (ACPI_TABLE_HEADER);
607     Opcode = AcpiPsPeekOpcode (&ParserState);
608     if (Opcode != AML_METHOD_OP)
609     {
610         return (AE_BAD_PARAMETER);
611     }
612 
613     /* Extract method information from the raw AML */
614 
615     ParserState.Aml += AcpiPsGetOpcodeSize (Opcode);
616     ParserState.PkgEnd = AcpiPsGetNextPackageEnd (&ParserState);
617     Path = AcpiPsGetNextNamestring (&ParserState);
618 
619     MethodFlags = *ParserState.Aml++;
620     AmlStart = ParserState.Aml;
621     AmlLength = ACPI_PTR_DIFF (ParserState.PkgEnd, AmlStart);
622 
623     /*
624      * Allocate resources up-front. We don't want to have to delete a new
625      * node from the namespace if we cannot allocate memory.
626      */
627     AmlBuffer = ACPI_ALLOCATE (AmlLength);
628     if (!AmlBuffer)
629     {
630         return (AE_NO_MEMORY);
631     }
632 
633     MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
634     if (!MethodObj)
635     {
636         ACPI_FREE (AmlBuffer);
637         return (AE_NO_MEMORY);
638     }
639 
640     /* Lock namespace for AcpiNsLookup, we may be creating a new node */
641 
642     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
643     if (ACPI_FAILURE (Status))
644     {
645         goto ErrorExit;
646     }
647 
648     /* The lookup either returns an existing node or creates a new one */
649 
650     Status = AcpiNsLookup (NULL, Path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
651         ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND, NULL, &Node);
652 
653     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
654 
655     if (ACPI_FAILURE (Status)) /* NsLookup */
656     {
657         if (Status != AE_ALREADY_EXISTS)
658         {
659             goto ErrorExit;
660         }
661 
662         /* Node existed previously, make sure it is a method node */
663 
664         if (Node->Type != ACPI_TYPE_METHOD)
665         {
666             Status = AE_TYPE;
667             goto ErrorExit;
668         }
669     }
670 
671     /* Copy the method AML to the local buffer */
672 
673     memcpy (AmlBuffer, AmlStart, AmlLength);
674 
675     /* Initialize the method object with the new method's information */
676 
677     MethodObj->Method.AmlStart = AmlBuffer;
678     MethodObj->Method.AmlLength = AmlLength;
679 
680     MethodObj->Method.ParamCount = (UINT8)
681         (MethodFlags & AML_METHOD_ARG_COUNT);
682 
683     if (MethodFlags & AML_METHOD_SERIALIZED)
684     {
685         MethodObj->Method.InfoFlags = ACPI_METHOD_SERIALIZED;
686 
687         MethodObj->Method.SyncLevel = (UINT8)
688             ((MethodFlags & AML_METHOD_SYNC_LEVEL) >> 4);
689     }
690 
691     /*
692      * Now that it is complete, we can attach the new method object to
693      * the method Node (detaches/deletes any existing object)
694      */
695     Status = AcpiNsAttachObject (Node, MethodObj, ACPI_TYPE_METHOD);
696 
697     /*
698      * Flag indicates AML buffer is dynamic, must be deleted later.
699      * Must be set only after attach above.
700      */
701     Node->Flags |= ANOBJ_ALLOCATED_BUFFER;
702 
703     /* Remove local reference to the method object */
704 
705     AcpiUtRemoveReference (MethodObj);
706     return (Status);
707 
708 
709 ErrorExit:
710 
711     ACPI_FREE (AmlBuffer);
712     ACPI_FREE (MethodObj);
713     return (Status);
714 }
715 
716 ACPI_EXPORT_SYMBOL (AcpiInstallMethod)
717