AcpiLib.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 ((EFI_D_ERROR, "- Found '%c%c%c%c' ACPI Table\n",
  97. (((EFI_ACPI_DESCRIPTION_HEADER *) AcpiTable)->Signature & 0xFF),
  98. ((((EFI_ACPI_DESCRIPTION_HEADER *) AcpiTable)->Signature >> 8) & 0xFF),
  99. ((((EFI_ACPI_DESCRIPTION_HEADER *) AcpiTable)->Signature >> 16) & 0xFF),
  100. ((((EFI_ACPI_DESCRIPTION_HEADER *) AcpiTable)->Signature >> 24) & 0xFF)));
  101. // Is the ACPI table valid?
  102. if (CheckAcpiTableFunction) {
  103. Valid = CheckAcpiTableFunction ((EFI_ACPI_DESCRIPTION_HEADER *)AcpiTable);
  104. } else {
  105. Valid = TRUE;
  106. }
  107. // Install the ACPI Table
  108. if (Valid) {
  109. Status = AcpiProtocol->InstallAcpiTable (
  110. AcpiProtocol,
  111. AcpiTable,
  112. AcpiTableSize,
  113. &AcpiTableKey
  114. );
  115. }
  116. // Free memory allocated by ReadSection
  117. gBS->FreePool (AcpiTable);
  118. if (EFI_ERROR (Status)) {
  119. break;
  120. }
  121. // Increment the section instance
  122. SectionInstance++;
  123. }
  124. }
  125. }
  126. FREE_HANDLE_BUFFER:
  127. //
  128. // Free any allocated buffers
  129. //
  130. gBS->FreePool (HandleBuffer);
  131. return EFI_SUCCESS;
  132. }
  133. /**
  134. Locate and Install the ACPI tables from the Firmware Volume
  135. @param AcpiFile Guid of the ACPI file into the Firmware Volume
  136. @return EFI_SUCCESS The function completed successfully.
  137. @return EFI_NOT_FOUND The protocol could not be located.
  138. @return EFI_OUT_OF_RESOURCES There are not enough resources to find the protocol.
  139. **/
  140. EFI_STATUS
  141. LocateAndInstallAcpiFromFv (
  142. IN CONST EFI_GUID* AcpiFile
  143. )
  144. {
  145. return LocateAndInstallAcpiFromFvConditional (AcpiFile, NULL);
  146. }