AcpiLib.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /** @file
  2. *
  3. * Copyright (c) 2014-2015, ARM Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause-Patent
  6. *
  7. **/
  8. #include <Uefi.h>
  9. #include <Library/AcpiLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Protocol/AcpiTable.h>
  13. #include <Protocol/FirmwareVolume2.h>
  14. #include <IndustryStandard/Acpi.h>
  15. /**
  16. Locate and Install the ACPI tables from the Firmware Volume if it verifies
  17. the function condition.
  18. @param AcpiFile Guid of the ACPI file into the Firmware Volume
  19. @param CheckAcpiTableFunction Function that checks if the ACPI table should be installed
  20. @return EFI_SUCCESS The function completed successfully.
  21. @return EFI_NOT_FOUND The protocol could not be located.
  22. @return EFI_OUT_OF_RESOURCES There are not enough resources to find the protocol.
  23. **/
  24. EFI_STATUS
  25. LocateAndInstallAcpiFromFvConditional (
  26. IN CONST EFI_GUID *AcpiFile,
  27. IN EFI_LOCATE_ACPI_CHECK CheckAcpiTableFunction
  28. )
  29. {
  30. EFI_STATUS Status;
  31. EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol;
  32. EFI_HANDLE *HandleBuffer;
  33. UINTN NumberOfHandles;
  34. UINT32 FvStatus;
  35. UINTN Index;
  36. EFI_FIRMWARE_VOLUME2_PROTOCOL *FvInstance;
  37. INTN SectionInstance;
  38. UINTN SectionSize;
  39. EFI_ACPI_COMMON_HEADER *AcpiTable;
  40. UINTN AcpiTableSize;
  41. UINTN AcpiTableKey;
  42. BOOLEAN Valid;
  43. // Ensure the ACPI Table is present
  44. Status = gBS->LocateProtocol (
  45. &gEfiAcpiTableProtocolGuid,
  46. NULL,
  47. (VOID **)&AcpiProtocol
  48. );
  49. if (EFI_ERROR (Status)) {
  50. return Status;
  51. }
  52. FvStatus = 0;
  53. SectionInstance = 0;
  54. // Locate all the Firmware Volume protocols.
  55. Status = gBS->LocateHandleBuffer (
  56. ByProtocol,
  57. &gEfiFirmwareVolume2ProtocolGuid,
  58. NULL,
  59. &NumberOfHandles,
  60. &HandleBuffer
  61. );
  62. if (EFI_ERROR (Status)) {
  63. return Status;
  64. }
  65. // Looking for FV with ACPI storage file
  66. for (Index = 0; Index < NumberOfHandles; Index++) {
  67. //
  68. // Get the protocol on this handle
  69. // This should not fail because of LocateHandleBuffer
  70. //
  71. Status = gBS->HandleProtocol (
  72. HandleBuffer[Index],
  73. &gEfiFirmwareVolume2ProtocolGuid,
  74. (VOID **)&FvInstance
  75. );
  76. if (EFI_ERROR (Status)) {
  77. goto FREE_HANDLE_BUFFER;
  78. }
  79. while (Status == EFI_SUCCESS) {
  80. // AcpiTable must be allocated by ReadSection (ie: AcpiTable == NULL)
  81. AcpiTable = NULL;
  82. // See if it has the ACPI storage file
  83. Status = FvInstance->ReadSection (
  84. FvInstance,
  85. AcpiFile,
  86. EFI_SECTION_RAW,
  87. SectionInstance,
  88. (VOID **)&AcpiTable,
  89. &SectionSize,
  90. &FvStatus
  91. );
  92. if (!EFI_ERROR (Status)) {
  93. AcpiTableKey = 0;
  94. AcpiTableSize = ((EFI_ACPI_DESCRIPTION_HEADER *)AcpiTable)->Length;
  95. ASSERT (SectionSize >= AcpiTableSize);
  96. DEBUG ((
  97. DEBUG_ERROR,
  98. "- Found '%c%c%c%c' ACPI Table\n",
  99. (((EFI_ACPI_DESCRIPTION_HEADER *)AcpiTable)->Signature & 0xFF),
  100. ((((EFI_ACPI_DESCRIPTION_HEADER *)AcpiTable)->Signature >> 8) & 0xFF),
  101. ((((EFI_ACPI_DESCRIPTION_HEADER *)AcpiTable)->Signature >> 16) & 0xFF),
  102. ((((EFI_ACPI_DESCRIPTION_HEADER *)AcpiTable)->Signature >> 24) & 0xFF)
  103. ));
  104. // Is the ACPI table valid?
  105. if (CheckAcpiTableFunction) {
  106. Valid = CheckAcpiTableFunction ((EFI_ACPI_DESCRIPTION_HEADER *)AcpiTable);
  107. } else {
  108. Valid = TRUE;
  109. }
  110. // Install the ACPI Table
  111. if (Valid) {
  112. Status = AcpiProtocol->InstallAcpiTable (
  113. AcpiProtocol,
  114. AcpiTable,
  115. AcpiTableSize,
  116. &AcpiTableKey
  117. );
  118. }
  119. // Free memory allocated by ReadSection
  120. gBS->FreePool (AcpiTable);
  121. if (EFI_ERROR (Status)) {
  122. break;
  123. }
  124. // Increment the section instance
  125. SectionInstance++;
  126. }
  127. }
  128. }
  129. FREE_HANDLE_BUFFER:
  130. //
  131. // Free any allocated buffers
  132. //
  133. gBS->FreePool (HandleBuffer);
  134. return EFI_SUCCESS;
  135. }
  136. /**
  137. Locate and Install the ACPI tables from the Firmware Volume
  138. @param AcpiFile Guid of the ACPI file into the Firmware Volume
  139. @return EFI_SUCCESS The function completed successfully.
  140. @return EFI_NOT_FOUND The protocol could not be located.
  141. @return EFI_OUT_OF_RESOURCES There are not enough resources to find the protocol.
  142. **/
  143. EFI_STATUS
  144. LocateAndInstallAcpiFromFv (
  145. IN CONST EFI_GUID *AcpiFile
  146. )
  147. {
  148. return LocateAndInstallAcpiFromFvConditional (AcpiFile, NULL);
  149. }