]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/AcpiTableDxe/AcpiSdt.c
MdeModulePkg:
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / AcpiTableDxe / AcpiSdt.c
1 /** @file
2 ACPI Sdt Protocol Driver
3
4 Copyright (c) 2010, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 //
16 // Includes
17 //
18 #include "AcpiTable.h"
19
20 GLOBAL_REMOVE_IF_UNREFERENCED
21 EFI_ACPI_SDT_PROTOCOL mAcpiSdtProtocolTemplate = {
22 EFI_ACPI_TABLE_VERSION_NONE | EFI_ACPI_TABLE_VERSION_1_0B | EFI_ACPI_TABLE_VERSION_2_0 | EFI_ACPI_TABLE_VERSION_3_0 | EFI_ACPI_TABLE_VERSION_4_0,
23 GetAcpiTable2,
24 RegisterNotify,
25 Open,
26 OpenSdt,
27 Close,
28 GetChild,
29 GetOption,
30 SetOption,
31 FindPath
32 };
33
34 /**
35 This function returns ACPI Table instance.
36
37 @return AcpiTableInstance
38 **/
39 EFI_ACPI_TABLE_INSTANCE *
40 SdtGetAcpiTableInstance (
41 VOID
42 )
43 {
44 return mPrivateData;
45 }
46
47 /**
48 This function finds the table specified by the buffer.
49
50 @param[in] Buffer Table buffer to find.
51
52 @return ACPI table list.
53 **/
54 EFI_ACPI_TABLE_LIST *
55 FindTableByBuffer (
56 IN VOID *Buffer
57 )
58 {
59 EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance;
60 LIST_ENTRY *CurrentLink;
61 EFI_ACPI_TABLE_LIST *CurrentTableList;
62 LIST_ENTRY *StartLink;
63
64 //
65 // Get the instance of the ACPI Table
66 //
67 AcpiTableInstance = SdtGetAcpiTableInstance ();
68
69 //
70 // Find the notify
71 //
72 StartLink = &AcpiTableInstance->TableList;
73 CurrentLink = StartLink->ForwardLink;
74
75 while (CurrentLink != StartLink) {
76 CurrentTableList = EFI_ACPI_TABLE_LIST_FROM_LINK (CurrentLink);
77 if (((UINTN)CurrentTableList->PageAddress <= (UINTN)Buffer) &&
78 ((UINTN)CurrentTableList->PageAddress + EFI_PAGES_TO_SIZE(CurrentTableList->NumberOfPages) > (UINTN)Buffer)) {
79 //
80 // Good! Found Table.
81 //
82 return CurrentTableList;
83 }
84
85 CurrentLink = CurrentLink->ForwardLink;
86 }
87
88 return NULL;
89 }
90
91 /**
92 This function updates AML table checksum.
93 It will search the ACPI table installed by ACPI_TABLE protocol.
94
95 @param[in] Buffer A piece of AML code buffer pointer.
96
97 @retval EFI_SUCCESS The table holds the AML buffer is found, and checksum is updated.
98 @retval EFI_NOT_FOUND The table holds the AML buffer is not found.
99 **/
100 EFI_STATUS
101 SdtUpdateAmlChecksum (
102 IN VOID *Buffer
103 )
104 {
105 EFI_ACPI_TABLE_LIST *CurrentTableList;
106
107 CurrentTableList = FindTableByBuffer (Buffer);
108 if (CurrentTableList == NULL) {
109 return EFI_NOT_FOUND;
110 }
111
112 AcpiPlatformChecksum (
113 (VOID *)CurrentTableList->Table,
114 CurrentTableList->Table->Length,
115 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER, Checksum)
116 );
117 return EFI_SUCCESS;
118 }
119
120 /**
121 This function finds MAX AML buffer size.
122 It will search the ACPI table installed by ACPI_TABLE protocol.
123
124 @param[in] Buffer A piece of AML code buffer pointer.
125 @param[out] MaxSize On return it holds the MAX size of buffer.
126
127 @retval EFI_SUCCESS The table holds the AML buffer is found, and MAX size if returned.
128 @retval EFI_NOT_FOUND The table holds the AML buffer is not found.
129 **/
130 EFI_STATUS
131 SdtGetMaxAmlBufferSize (
132 IN VOID *Buffer,
133 OUT UINTN *MaxSize
134 )
135 {
136 EFI_ACPI_TABLE_LIST *CurrentTableList;
137
138 CurrentTableList = FindTableByBuffer (Buffer);
139 if (CurrentTableList == NULL) {
140 return EFI_NOT_FOUND;
141 }
142
143 *MaxSize = (UINTN)CurrentTableList->Table + CurrentTableList->Table->Length - (UINTN)Buffer;
144 return EFI_SUCCESS;
145 }
146
147 /**
148 This function invokes ACPI notification.
149
150 @param[in] AcpiTableInstance Instance to AcpiTable
151 @param[in] Version Version(s) to set.
152 @param[in] Handle Handle of the table.
153 **/
154 VOID
155 SdtNotifyAcpiList (
156 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance,
157 IN EFI_ACPI_TABLE_VERSION Version,
158 IN UINTN Handle
159 )
160 {
161 EFI_ACPI_NOTIFY_LIST *CurrentNotifyList;
162 LIST_ENTRY *CurrentLink;
163 LIST_ENTRY *StartLink;
164 EFI_ACPI_TABLE_LIST *Table;
165 EFI_STATUS Status;
166
167 //
168 // We should not use Table buffer, because it is user input buffer.
169 //
170 Status = FindTableByHandle (
171 Handle,
172 &AcpiTableInstance->TableList,
173 &Table
174 );
175 ASSERT_EFI_ERROR (Status);
176
177 //
178 // Find the notify
179 //
180 StartLink = &AcpiTableInstance->NotifyList;
181 CurrentLink = StartLink->ForwardLink;
182
183 while (CurrentLink != StartLink) {
184 CurrentNotifyList = EFI_ACPI_NOTIFY_LIST_FROM_LINK (CurrentLink);
185
186 //
187 // Inovke notification
188 //
189 CurrentNotifyList->Notification ((EFI_ACPI_SDT_HEADER *)Table->Table, Version, Handle);
190
191 CurrentLink = CurrentLink->ForwardLink;
192 }
193
194 return ;
195 }
196
197 /**
198 Returns a requested ACPI table.
199
200 The GetAcpiTable() function returns a pointer to a buffer containing the ACPI table associated
201 with the Index that was input. The following structures are not considered elements in the list of
202 ACPI tables:
203 - Root System Description Pointer (RSD_PTR)
204 - Root System Description Table (RSDT)
205 - Extended System Description Table (XSDT)
206 Version is updated with a bit map containing all the versions of ACPI of which the table is a
207 member.
208
209 @param[in] Index The zero-based index of the table to retrieve.
210 @param[out] Table Pointer for returning the table buffer.
211 @param[out] Version On return, updated with the ACPI versions to which this table belongs. Type
212 EFI_ACPI_TABLE_VERSION is defined in "Related Definitions" in the
213 EFI_ACPI_SDT_PROTOCOL.
214 @param[out] TableKey On return, points to the table key for the specified ACPI system definition table. This
215 is identical to the table key used in the EFI_ACPI_TABLE_PROTOCOL.
216
217 @retval EFI_SUCCESS The function completed successfully.
218 @retval EFI_NOT_FOUND The requested index is too large and a table was not found.
219 **/
220 EFI_STATUS
221 EFIAPI
222 GetAcpiTable2 (
223 IN UINTN Index,
224 OUT EFI_ACPI_SDT_HEADER **Table,
225 OUT EFI_ACPI_TABLE_VERSION *Version,
226 OUT UINTN *TableKey
227 )
228 {
229 EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance;
230 UINTN TableIndex;
231 LIST_ENTRY *CurrentLink;
232 LIST_ENTRY *StartLink;
233 EFI_ACPI_TABLE_LIST *CurrentTable;
234
235 ASSERT (Table != NULL);
236 ASSERT (Version != NULL);
237 ASSERT (TableKey != NULL);
238
239 //
240 // Get the instance of the ACPI Table
241 //
242 AcpiTableInstance = SdtGetAcpiTableInstance ();
243
244 //
245 // Find the table
246 //
247 StartLink = &AcpiTableInstance->TableList;
248 CurrentLink = StartLink->ForwardLink;
249 TableIndex = 0;
250
251 while (CurrentLink != StartLink) {
252 if (TableIndex == Index) {
253 break;
254 }
255 //
256 // Next one
257 //
258 CurrentLink = CurrentLink->ForwardLink;
259 TableIndex ++;
260 }
261
262 if ((TableIndex != Index) || (CurrentLink == StartLink)) {
263 return EFI_NOT_FOUND;
264 }
265
266 //
267 // Get handle and version
268 //
269 CurrentTable = EFI_ACPI_TABLE_LIST_FROM_LINK (CurrentLink);
270 *TableKey = CurrentTable->Handle;
271 *Version = CurrentTable->Version;
272 *Table = (EFI_ACPI_SDT_HEADER *)CurrentTable->Table;
273
274 return EFI_SUCCESS;
275 }
276
277 /**
278 Register a callback when an ACPI table is installed.
279
280 This function registers a function which will be called whenever a new ACPI table is installed.
281
282 @param[in] Notification Points to the callback function to be registered
283 **/
284 VOID
285 SdtRegisterNotify (
286 IN EFI_ACPI_NOTIFICATION_FN Notification
287 )
288 {
289 EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance;
290 EFI_ACPI_NOTIFY_LIST *CurrentNotifyList;
291
292 //
293 // Get the instance of the ACPI Table
294 //
295 AcpiTableInstance = SdtGetAcpiTableInstance ();
296
297 //
298 // Create a new list entry
299 //
300 CurrentNotifyList = AllocatePool (sizeof (EFI_ACPI_NOTIFY_LIST));
301 ASSERT (CurrentNotifyList != NULL);
302
303 //
304 // Initialize the table contents
305 //
306 CurrentNotifyList->Signature = EFI_ACPI_NOTIFY_LIST_SIGNATURE;
307 CurrentNotifyList->Notification = Notification;
308
309 //
310 // Add the table to the current list of tables
311 //
312 InsertTailList (&AcpiTableInstance->NotifyList, &CurrentNotifyList->Link);
313
314 return ;
315 }
316
317 /**
318 Unregister a callback when an ACPI table is installed.
319
320 This function unregisters a function which will be called whenever a new ACPI table is installed.
321
322 @param[in] Notification Points to the callback function to be unregistered.
323
324 @retval EFI_SUCCESS Callback successfully unregistered.
325 @retval EFI_INVALID_PARAMETER Notification does not match a known registration function.
326 **/
327 EFI_STATUS
328 SdtUnregisterNotify (
329 IN EFI_ACPI_NOTIFICATION_FN Notification
330 )
331 {
332 EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance;
333 EFI_ACPI_NOTIFY_LIST *CurrentNotifyList;
334 LIST_ENTRY *CurrentLink;
335 LIST_ENTRY *StartLink;
336
337 //
338 // Get the instance of the ACPI Table
339 //
340 AcpiTableInstance = SdtGetAcpiTableInstance ();
341
342 //
343 // Find the notify
344 //
345 StartLink = &AcpiTableInstance->NotifyList;
346 CurrentLink = StartLink->ForwardLink;
347
348 while (CurrentLink != StartLink) {
349 CurrentNotifyList = EFI_ACPI_NOTIFY_LIST_FROM_LINK (CurrentLink);
350 if (CurrentNotifyList->Notification == Notification) {
351 //
352 // Good! Found notification.
353 //
354 // Remove it from list and free the node.
355 //
356 RemoveEntryList (&(CurrentNotifyList->Link));
357 FreePool (CurrentNotifyList);
358 return EFI_SUCCESS;
359 }
360
361 CurrentLink = CurrentLink->ForwardLink;
362 }
363
364 //
365 // Not found!
366 //
367 return EFI_INVALID_PARAMETER;
368 }
369
370 /**
371 Register or unregister a callback when an ACPI table is installed.
372
373 This function registers or unregisters a function which will be called whenever a new ACPI table is
374 installed.
375
376 @param[in] Register If TRUE, then the specified function will be registered. If FALSE, then the specified
377 function will be unregistered.
378 @param[in] Notification Points to the callback function to be registered or unregistered.
379
380 @retval EFI_SUCCESS Callback successfully registered or unregistered.
381 @retval EFI_INVALID_PARAMETER Notification is NULL
382 @retval EFI_INVALID_PARAMETER Register is FALSE and Notification does not match a known registration function.
383 **/
384 EFI_STATUS
385 EFIAPI
386 RegisterNotify (
387 IN BOOLEAN Register,
388 IN EFI_ACPI_NOTIFICATION_FN Notification
389 )
390 {
391 //
392 // Check for invalid input parameters
393 //
394 if (Notification == NULL) {
395 return EFI_INVALID_PARAMETER;
396 }
397
398 if (Register) {
399 //
400 // Register a new notify
401 //
402 SdtRegisterNotify (Notification);
403 return EFI_SUCCESS;
404 } else {
405 //
406 // Unregister an old notify
407 //
408 return SdtUnregisterNotify (Notification);
409 }
410 }
411
412 /**
413 Create a handle for the first ACPI opcode in an ACPI system description table.
414
415 @param[in] TableKey The table key for the ACPI table, as returned by GetTable().
416 @param[out] Handle On return, points to the newly created ACPI handle.
417
418 @retval EFI_SUCCESS Handle created successfully.
419 @retval EFI_NOT_FOUND TableKey does not refer to a valid ACPI table.
420 **/
421 EFI_STATUS
422 SdtOpenSdtTable (
423 IN UINTN TableKey,
424 OUT EFI_ACPI_HANDLE *Handle
425 )
426 {
427 EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance;
428 EFI_STATUS Status;
429 EFI_ACPI_TABLE_LIST *Table;
430 EFI_AML_HANDLE *AmlHandle;
431
432 //
433 // Get the instance of the ACPI Table
434 //
435 AcpiTableInstance = SdtGetAcpiTableInstance ();
436
437 //
438 // Find the table
439 //
440 Status = FindTableByHandle (
441 TableKey,
442 &AcpiTableInstance->TableList,
443 &Table
444 );
445 if (EFI_ERROR (Status)) {
446 return EFI_NOT_FOUND;
447 }
448
449 AmlHandle = AllocatePool (sizeof(*AmlHandle));
450 ASSERT (AmlHandle != NULL);
451 AmlHandle->Signature = EFI_AML_ROOT_HANDLE_SIGNATURE;
452 AmlHandle->Buffer = (VOID *)((UINTN)Table->Table + sizeof(EFI_ACPI_SDT_HEADER));
453 AmlHandle->Size = Table->Table->Length - sizeof(EFI_ACPI_SDT_HEADER);
454 AmlHandle->AmlByteEncoding = NULL;
455 AmlHandle->Modified = FALSE;
456
457 //
458 // return the ACPI handle
459 //
460 *Handle = (EFI_ACPI_HANDLE)AmlHandle;
461
462 return EFI_SUCCESS;
463 }
464
465 /**
466 Create a handle for the first ACPI opcode in an ACPI system description table.
467
468 @param[in] TableKey The table key for the ACPI table, as returned by GetTable().
469 @param[out] Handle On return, points to the newly created ACPI handle.
470
471 @retval EFI_SUCCESS Handle created successfully.
472 @retval EFI_NOT_FOUND TableKey does not refer to a valid ACPI table.
473 **/
474 EFI_STATUS
475 EFIAPI
476 OpenSdt (
477 IN UINTN TableKey,
478 OUT EFI_ACPI_HANDLE *Handle
479 )
480 {
481 if (Handle == NULL) {
482 return EFI_INVALID_PARAMETER;
483 }
484
485 return SdtOpenSdtTable (TableKey, Handle);
486 }
487
488 /**
489 Create a handle from an ACPI opcode
490
491 @param[in] Buffer Points to the ACPI opcode.
492 @param[in] BufferSize Max buffer size.
493 @param[out] Handle Upon return, holds the handle.
494
495 @retval EFI_SUCCESS Success
496 @retval EFI_INVALID_PARAMETER Buffer is NULL or Handle is NULL or Buffer points to an
497 invalid opcode.
498
499 **/
500 EFI_STATUS
501 SdtOpenEx (
502 IN VOID *Buffer,
503 IN UINTN BufferSize,
504 OUT EFI_ACPI_HANDLE *Handle
505 )
506 {
507 AML_BYTE_ENCODING *AmlByteEncoding;
508 EFI_AML_HANDLE *AmlHandle;
509
510 AmlByteEncoding = AmlSearchByOpByte (Buffer);
511 if (AmlByteEncoding == NULL) {
512 return EFI_INVALID_PARAMETER;
513 }
514
515 //
516 // Do not open NameString as handle
517 //
518 if ((AmlByteEncoding->Attribute & AML_IS_NAME_CHAR) != 0) {
519 return EFI_INVALID_PARAMETER;
520 }
521
522 //
523 // Good, find it
524 //
525 AmlHandle = AllocatePool (sizeof(*AmlHandle));
526 ASSERT (AmlHandle != NULL);
527
528 AmlHandle->Signature = EFI_AML_HANDLE_SIGNATURE;
529 AmlHandle->Buffer = Buffer;
530 AmlHandle->AmlByteEncoding = AmlByteEncoding;
531 AmlHandle->Modified = FALSE;
532
533 AmlHandle->Size = AmlGetObjectSize (AmlByteEncoding, Buffer, BufferSize);
534 if (AmlHandle->Size == 0) {
535 FreePool (AmlHandle);
536 return EFI_INVALID_PARAMETER;
537 }
538
539 *Handle = (EFI_ACPI_HANDLE)AmlHandle;
540
541 return EFI_SUCCESS;
542 }
543
544 /**
545 Create a handle from an ACPI opcode
546
547 @param[in] Buffer Points to the ACPI opcode.
548 @param[out] Handle Upon return, holds the handle.
549
550 @retval EFI_SUCCESS Success
551 @retval EFI_INVALID_PARAMETER Buffer is NULL or Handle is NULL or Buffer points to an
552 invalid opcode.
553
554 **/
555 EFI_STATUS
556 EFIAPI
557 Open (
558 IN VOID *Buffer,
559 OUT EFI_ACPI_HANDLE *Handle
560 )
561 {
562 EFI_STATUS Status;
563 UINTN MaxSize;
564
565 //
566 // Check for invalid input parameters
567 //
568 if (Buffer == NULL || Handle == NULL) {
569 return EFI_INVALID_PARAMETER;
570 }
571
572 Status = SdtGetMaxAmlBufferSize (Buffer, &MaxSize);
573 if (EFI_ERROR (Status)) {
574 return EFI_INVALID_PARAMETER;
575 }
576
577 return SdtOpenEx (Buffer, MaxSize, Handle);
578 }
579
580 /**
581 Close an ACPI handle.
582
583 @param[in] Handle Returns the handle.
584
585 @retval EFI_SUCCESS Success
586 @retval EFI_INVALID_PARAMETER Handle is NULL or does not refer to a valid ACPI object.
587 **/
588 EFI_STATUS
589 EFIAPI
590 Close (
591 IN EFI_ACPI_HANDLE Handle
592 )
593 {
594 EFI_AML_HANDLE *AmlHandle;
595 EFI_STATUS Status;
596
597 //
598 // Check for invalid input parameters
599 //
600 if (Handle == NULL) {
601 return EFI_INVALID_PARAMETER;
602 }
603
604 AmlHandle = (EFI_AML_HANDLE *)Handle;
605 if ((AmlHandle->Signature != EFI_AML_ROOT_HANDLE_SIGNATURE) &&
606 (AmlHandle->Signature != EFI_AML_HANDLE_SIGNATURE)) {
607 return EFI_INVALID_PARAMETER;
608 }
609
610 //
611 // Update Checksum only if modified
612 //
613 if (AmlHandle->Modified) {
614 Status = SdtUpdateAmlChecksum (AmlHandle->Buffer);
615 if (EFI_ERROR (Status)) {
616 return EFI_INVALID_PARAMETER;
617 }
618 }
619
620 FreePool (AmlHandle);
621
622 return EFI_SUCCESS;
623 }
624
625 /**
626 Retrieve information about an ACPI object.
627
628 @param[in] Handle ACPI object handle.
629 @param[in] Index Index of the data to retrieve from the object. In general, indexes read from left-to-right
630 in the ACPI encoding, with index 0 always being the ACPI opcode.
631 @param[out] DataType Points to the returned data type or EFI_ACPI_DATA_TYPE_NONE if no data exists
632 for the specified index.
633 @param[out] Data Upon return, points to the pointer to the data.
634 @param[out] DataSize Upon return, points to the size of Data.
635
636 @retval EFI_SUCCESS Success.
637 @retval EFI_INVALID_PARAMETER Handle is NULL or does not refer to a valid ACPI object.
638 **/
639 EFI_STATUS
640 EFIAPI
641 GetOption (
642 IN EFI_ACPI_HANDLE Handle,
643 IN UINTN Index,
644 OUT EFI_ACPI_DATA_TYPE *DataType,
645 OUT CONST VOID **Data,
646 OUT UINTN *DataSize
647 )
648 {
649 EFI_AML_HANDLE *AmlHandle;
650 AML_BYTE_ENCODING *AmlByteEncoding;
651 EFI_STATUS Status;
652
653 ASSERT (DataType != NULL);
654 ASSERT (Data != NULL);
655 ASSERT (DataSize != NULL);
656
657 //
658 // Check for invalid input parameters
659 //
660 if (Handle == NULL) {
661 return EFI_INVALID_PARAMETER;
662 }
663
664 AmlHandle = (EFI_AML_HANDLE *)Handle;
665 //
666 // Do not check EFI_AML_ROOT_HANDLE_SIGNATURE because there is no option for Root handle
667 //
668 if (AmlHandle->Signature != EFI_AML_HANDLE_SIGNATURE) {
669 return EFI_INVALID_PARAMETER;
670 }
671
672 AmlByteEncoding = AmlHandle->AmlByteEncoding;
673 if (Index > AmlByteEncoding->MaxIndex) {
674 *DataType = EFI_ACPI_DATA_TYPE_NONE;
675 return EFI_SUCCESS;
676 }
677
678 //
679 // Parse option
680 //
681 Status = AmlParseOptionHandleCommon (AmlHandle, (AML_OP_PARSE_INDEX)Index, DataType, (VOID **)Data, DataSize);
682 if (EFI_ERROR (Status)) {
683 return EFI_INVALID_PARAMETER;
684 }
685
686 return EFI_SUCCESS;
687 }
688
689 /**
690 Change information about an ACPI object.
691
692 @param[in] Handle ACPI object handle.
693 @param[in] Index Index of the data to retrieve from the object. In general, indexes read from left-to-right
694 in the ACPI encoding, with index 0 always being the ACPI opcode.
695 @param[in] Data Points to the data.
696 @param[in] DataSize The size of the Data.
697
698 @retval EFI_SUCCESS Success
699 @retval EFI_INVALID_PARAMETER Handle is NULL or does not refer to a valid ACPI object.
700 @retval EFI_BAD_BUFFER_SIZE Data cannot be accommodated in the space occupied by
701 the option.
702
703 **/
704 EFI_STATUS
705 EFIAPI
706 SetOption (
707 IN EFI_ACPI_HANDLE Handle,
708 IN UINTN Index,
709 IN CONST VOID *Data,
710 IN UINTN DataSize
711 )
712 {
713 EFI_AML_HANDLE *AmlHandle;
714 AML_BYTE_ENCODING *AmlByteEncoding;
715 EFI_STATUS Status;
716 EFI_ACPI_DATA_TYPE DataType;
717 VOID *OrgData;
718 UINTN OrgDataSize;
719
720 ASSERT (Data != NULL);
721
722 //
723 // Check for invalid input parameters
724 //
725 if (Handle == NULL) {
726 return EFI_INVALID_PARAMETER;
727 }
728
729 AmlHandle = (EFI_AML_HANDLE *)Handle;
730 //
731 // Do not check EFI_AML_ROOT_HANDLE_SIGNATURE because there is no option for Root handle
732 //
733 if (AmlHandle->Signature != EFI_AML_HANDLE_SIGNATURE) {
734 return EFI_INVALID_PARAMETER;
735 }
736 AmlByteEncoding = AmlHandle->AmlByteEncoding;
737
738 if (Index > AmlByteEncoding->MaxIndex) {
739 return EFI_INVALID_PARAMETER;
740 }
741
742 //
743 // Parse option
744 //
745 Status = AmlParseOptionHandleCommon (AmlHandle, (AML_OP_PARSE_INDEX)Index, &DataType, &OrgData, &OrgDataSize);
746 if (EFI_ERROR (Status)) {
747 return EFI_INVALID_PARAMETER;
748 }
749 if (DataType == EFI_ACPI_DATA_TYPE_NONE) {
750 return EFI_INVALID_PARAMETER;
751 }
752
753 if (DataSize > OrgDataSize) {
754 return EFI_BAD_BUFFER_SIZE;
755 }
756
757 //
758 // Update
759 //
760 CopyMem (OrgData, Data, DataSize);
761 AmlHandle->Modified = TRUE;
762
763 return EFI_SUCCESS;
764 }
765
766 /**
767 Return the child ACPI objects.
768
769 @param[in] ParentHandle Parent handle.
770 @param[in, out] Handle On entry, points to the previously returned handle or NULL to start with the first
771 handle. On return, points to the next returned ACPI handle or NULL if there are no
772 child objects.
773
774 @retval EFI_SUCCESS Success
775 @retval EFI_INVALID_PARAMETER ParentHandle is NULL or does not refer to a valid ACPI object.
776 **/
777 EFI_STATUS
778 EFIAPI
779 GetChild (
780 IN EFI_ACPI_HANDLE ParentHandle,
781 IN OUT EFI_ACPI_HANDLE *Handle
782 )
783 {
784 EFI_AML_HANDLE *AmlParentHandle;
785 EFI_AML_HANDLE *AmlHandle;
786 VOID *Buffer;
787 EFI_STATUS Status;
788
789 ASSERT (Handle != NULL);
790
791 //
792 // Check for invalid input parameters
793 //
794 if (ParentHandle == NULL) {
795 return EFI_INVALID_PARAMETER;
796 }
797
798 AmlHandle = *Handle;
799 if ((AmlHandle != NULL) && (AmlHandle->Signature != EFI_AML_HANDLE_SIGNATURE)) {
800 return EFI_INVALID_PARAMETER;
801 }
802
803 AmlParentHandle = (EFI_AML_HANDLE *)ParentHandle;
804 if (AmlParentHandle->Signature == EFI_AML_ROOT_HANDLE_SIGNATURE) {
805 //
806 // Root handle
807 //
808 Status = AmlGetChildFromRoot (AmlParentHandle, AmlHandle, &Buffer);
809 } else if (AmlParentHandle->Signature == EFI_AML_HANDLE_SIGNATURE) {
810 //
811 // Non-root handle
812 //
813 Status = AmlGetChildFromNonRoot (AmlParentHandle, AmlHandle, &Buffer);
814 } else {
815 //
816 // Invalid
817 //
818 return EFI_INVALID_PARAMETER;
819 }
820
821 if (EFI_ERROR (Status)) {
822 return EFI_INVALID_PARAMETER;
823 }
824 if (Buffer == NULL) {
825 *Handle = NULL;
826 return EFI_SUCCESS;
827 }
828 return SdtOpenEx (Buffer, (UINTN)AmlParentHandle->Buffer + AmlParentHandle->Size - (UINTN)Buffer, Handle);
829 }
830
831 /**
832 Returns the handle of the ACPI object representing the specified ACPI path
833
834 @param[in] HandleIn Points to the handle of the object representing the starting point for the path search.
835 @param[in] AmlPath Points to the AML path.
836 @param[out] HandleOut On return, points to the ACPI object which represents AcpiPath, relative to
837 HandleIn.
838
839 @retval EFI_SUCCESS Success
840 @retval EFI_INVALID_PARAMETER HandleIn is NULL or does not refer to a valid ACPI object.
841 **/
842 EFI_STATUS
843 SdtFindPathFromNonRoot (
844 IN EFI_ACPI_HANDLE HandleIn,
845 IN UINT8 *AmlPath,
846 OUT EFI_ACPI_HANDLE *HandleOut
847 )
848 {
849 EFI_AML_HANDLE *AmlHandle;
850 VOID *Buffer;
851 EFI_STATUS Status;
852
853 AmlHandle = (EFI_AML_HANDLE *)HandleIn;
854
855 //
856 // For non-root handle, we need search from THIS node instead of ROOT.
857 //
858 Status = AmlFindPath (AmlHandle, AmlPath, &Buffer, FALSE);
859 if (EFI_ERROR (Status)) {
860 return EFI_INVALID_PARAMETER;
861 }
862 if (Buffer == NULL) {
863 *HandleOut = NULL;
864 return EFI_SUCCESS;
865 }
866 return SdtOpenEx (Buffer, (UINTN)AmlHandle->Buffer + AmlHandle->Size - (UINTN)Buffer, HandleOut);
867 }
868
869 /**
870 Duplicate AML handle.
871
872 @param[in] AmlHandle Handle to be duplicated.
873
874 @return Duplicated AML handle.
875 **/
876 EFI_AML_HANDLE *
877 SdtDuplicateHandle (
878 IN EFI_AML_HANDLE *AmlHandle
879 )
880 {
881 EFI_AML_HANDLE *DstAmlHandle;
882
883 DstAmlHandle = AllocatePool (sizeof(*DstAmlHandle));
884 ASSERT (DstAmlHandle != NULL);
885 CopyMem (DstAmlHandle, (VOID *)AmlHandle, sizeof(*DstAmlHandle));
886
887 return DstAmlHandle;
888 }
889
890 /**
891 Returns the handle of the ACPI object representing the specified ACPI path
892
893 @param[in] HandleIn Points to the handle of the object representing the starting point for the path search.
894 @param[in] AmlPath Points to the AML path.
895 @param[out] HandleOut On return, points to the ACPI object which represents AcpiPath, relative to
896 HandleIn.
897
898 @retval EFI_SUCCESS Success
899 @retval EFI_INVALID_PARAMETER HandleIn is NULL or does not refer to a valid ACPI object.
900 **/
901 EFI_STATUS
902 SdtFindPathFromRoot (
903 IN EFI_ACPI_HANDLE HandleIn,
904 IN UINT8 *AmlPath,
905 OUT EFI_ACPI_HANDLE *HandleOut
906 )
907 {
908 EFI_ACPI_HANDLE ChildHandle;
909 EFI_AML_HANDLE *AmlHandle;
910 EFI_STATUS Status;
911 VOID *Buffer;
912
913 AmlHandle = (EFI_AML_HANDLE *)HandleIn;
914
915 //
916 // Handle case that AcpiPath is Root
917 //
918 if (AmlIsRootPath (AmlPath)) {
919 //
920 // Duplicate RootHandle
921 //
922 *HandleOut = (EFI_ACPI_HANDLE)SdtDuplicateHandle (AmlHandle);
923 return EFI_SUCCESS;
924 }
925
926 //
927 // Let children find it.
928 //
929 ChildHandle = NULL;
930 while (TRUE) {
931 Status = GetChild (HandleIn, &ChildHandle);
932 if (EFI_ERROR (Status)) {
933 return EFI_INVALID_PARAMETER;
934 }
935
936 if (ChildHandle == NULL) {
937 //
938 // Not found
939 //
940 *HandleOut = NULL;
941 return EFI_SUCCESS;
942 }
943
944 //
945 // More child
946 //
947 AmlHandle = (EFI_AML_HANDLE *)ChildHandle;
948 Status = AmlFindPath (AmlHandle, AmlPath, &Buffer, TRUE);
949 if (EFI_ERROR (Status)) {
950 return EFI_INVALID_PARAMETER;
951 }
952
953 if (Buffer != NULL) {
954 //
955 // Great! Find it, open
956 //
957 Status = SdtOpenEx (Buffer, (UINTN)AmlHandle->Buffer + AmlHandle->Size - (UINTN)Buffer, HandleOut);
958 if (!EFI_ERROR (Status)) {
959 return EFI_SUCCESS;
960 }
961 //
962 // Not success, try next one
963 //
964 }
965 }
966
967 //
968 // Should not run here
969 //
970 }
971
972 /**
973 Returns the handle of the ACPI object representing the specified ACPI path
974
975 @param[in] HandleIn Points to the handle of the object representing the starting point for the path search.
976 @param[in] AcpiPath Points to the ACPI path, which conforms to the ACPI encoded path format.
977 @param[out] HandleOut On return, points to the ACPI object which represents AcpiPath, relative to
978 HandleIn.
979
980 @retval EFI_SUCCESS Success
981 @retval EFI_INVALID_PARAMETER HandleIn is NULL or does not refer to a valid ACPI object.
982 **/
983 EFI_STATUS
984 EFIAPI
985 FindPath (
986 IN EFI_ACPI_HANDLE HandleIn,
987 IN VOID *AcpiPath,
988 OUT EFI_ACPI_HANDLE *HandleOut
989 )
990 {
991 EFI_AML_HANDLE *AmlHandle;
992 EFI_STATUS Status;
993 UINT8 *AmlPath;
994
995 //
996 // Check for invalid input parameters
997 //
998 if (HandleIn == NULL) {
999 return EFI_INVALID_PARAMETER;
1000 }
1001
1002 AmlHandle = (EFI_AML_HANDLE *)HandleIn;
1003
1004 //
1005 // Convert ASL path to AML path
1006 //
1007 AmlPath = AmlNameFromAslName (AcpiPath);
1008 if (AmlPath == NULL) {
1009 return EFI_INVALID_PARAMETER;
1010 }
1011
1012 DEBUG_CODE_BEGIN ();
1013 DEBUG ((EFI_D_ERROR, "AcpiSdt: FindPath - "));
1014 AmlPrintNameString (AmlPath);
1015 DEBUG ((EFI_D_ERROR, "\n"));
1016 DEBUG_CODE_END ();
1017
1018 if (AmlHandle->Signature == EFI_AML_ROOT_HANDLE_SIGNATURE) {
1019 //
1020 // Root Handle
1021 //
1022 Status = SdtFindPathFromRoot (HandleIn, AmlPath, HandleOut);
1023 } else if (AmlHandle->Signature == EFI_AML_HANDLE_SIGNATURE) {
1024 //
1025 // Non-Root handle
1026 //
1027 Status = SdtFindPathFromNonRoot (HandleIn, AmlPath, HandleOut);
1028 } else {
1029 Status = EFI_INVALID_PARAMETER;
1030 }
1031
1032 FreePool (AmlPath);
1033
1034 return Status;
1035 }
1036
1037 /**
1038 ExitPmAuth Protocol notification event handler.
1039
1040 @param[in] Event Event whose notification function is being invoked.
1041 @param[in] Context Pointer to the notification function's context.
1042 **/
1043 VOID
1044 EFIAPI
1045 ExitPmAuthNotification (
1046 IN EFI_EVENT Event,
1047 IN VOID *Context
1048 )
1049 {
1050 EFI_STATUS Status;
1051 VOID *DxeSmmReadyToLock;
1052
1053 //
1054 // Add more check to locate protocol after got event, because
1055 // the library will signal this event immediately once it is register
1056 // just in case it is already installed.
1057 //
1058 Status = gBS->LocateProtocol (
1059 &gEfiDxeSmmReadyToLockProtocolGuid,
1060 NULL,
1061 &DxeSmmReadyToLock
1062 );
1063 if (EFI_ERROR (Status)) {
1064 return ;
1065 }
1066
1067 //
1068 // Uninstall ACPI SDT protocol, so that we can make sure no one update ACPI table from API level.
1069 //
1070 Status = gBS->UninstallProtocolInterface (
1071 mHandle,
1072 &gEfiAcpiSdtProtocolGuid,
1073 &mPrivateData->AcpiSdtProtocol
1074 );
1075 ASSERT_EFI_ERROR (Status);
1076
1077 //
1078 // Close event, so it will not be invoked again.
1079 //
1080 gBS->CloseEvent (Event);
1081
1082 return ;
1083 }
1084
1085 /**
1086 This function initializes AcpiSdt protocol in ACPI table instance.
1087
1088 @param[in] AcpiTableInstance Instance to construct
1089 **/
1090 VOID
1091 SdtAcpiTableAcpiSdtConstructor (
1092 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance
1093 )
1094 {
1095 VOID *Registration;
1096
1097 InitializeListHead (&AcpiTableInstance->NotifyList);
1098 CopyMem (&AcpiTableInstance->AcpiSdtProtocol, &mAcpiSdtProtocolTemplate, sizeof(mAcpiSdtProtocolTemplate));
1099
1100 //
1101 // Register event for ExitPmAuth, so that we can uninstall ACPI SDT protocol after ExitPmAuth.
1102 //
1103 EfiCreateProtocolNotifyEvent (
1104 &gEfiDxeSmmReadyToLockProtocolGuid,
1105 TPL_CALLBACK,
1106 ExitPmAuthNotification,
1107 NULL,
1108 &Registration
1109 );
1110
1111 return ;
1112 }