DtPlatformDxe.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /** @file
  2. *
  3. * Copyright (c) 2017, Linaro, Ltd. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause-Patent
  6. *
  7. **/
  8. #include <Library/BaseLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/DevicePathLib.h>
  11. #include <Library/DtPlatformDtbLoaderLib.h>
  12. #include <Library/HiiLib.h>
  13. #include <Library/MemoryAllocationLib.h>
  14. #include <Library/UefiBootServicesTableLib.h>
  15. #include <Library/UefiDriverEntryPoint.h>
  16. #include <Library/UefiRuntimeServicesTableLib.h>
  17. #include "DtPlatformDxe.h"
  18. extern UINT8 DtPlatformHiiBin[];
  19. extern UINT8 DtPlatformDxeStrings[];
  20. typedef struct {
  21. VENDOR_DEVICE_PATH VendorDevicePath;
  22. EFI_DEVICE_PATH_PROTOCOL End;
  23. } HII_VENDOR_DEVICE_PATH;
  24. STATIC HII_VENDOR_DEVICE_PATH mDtPlatformDxeVendorDevicePath = {
  25. {
  26. {
  27. HARDWARE_DEVICE_PATH,
  28. HW_VENDOR_DP,
  29. {
  30. (UINT8) (sizeof (VENDOR_DEVICE_PATH)),
  31. (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
  32. }
  33. },
  34. DT_PLATFORM_FORMSET_GUID
  35. },
  36. {
  37. END_DEVICE_PATH_TYPE,
  38. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  39. {
  40. (UINT8) (END_DEVICE_PATH_LENGTH),
  41. (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
  42. }
  43. }
  44. };
  45. STATIC
  46. EFI_STATUS
  47. InstallHiiPages (
  48. VOID
  49. )
  50. {
  51. EFI_STATUS Status;
  52. EFI_HII_HANDLE HiiHandle;
  53. EFI_HANDLE DriverHandle;
  54. DriverHandle = NULL;
  55. Status = gBS->InstallMultipleProtocolInterfaces (&DriverHandle,
  56. &gEfiDevicePathProtocolGuid,
  57. &mDtPlatformDxeVendorDevicePath,
  58. NULL);
  59. if (EFI_ERROR (Status)) {
  60. return Status;
  61. }
  62. HiiHandle = HiiAddPackages (&gDtPlatformFormSetGuid,
  63. DriverHandle,
  64. DtPlatformDxeStrings,
  65. DtPlatformHiiBin,
  66. NULL);
  67. if (HiiHandle == NULL) {
  68. gBS->UninstallMultipleProtocolInterfaces (DriverHandle,
  69. &gEfiDevicePathProtocolGuid,
  70. &mDtPlatformDxeVendorDevicePath,
  71. NULL);
  72. return EFI_OUT_OF_RESOURCES;
  73. }
  74. return EFI_SUCCESS;
  75. }
  76. /**
  77. The entry point for DtPlatformDxe driver.
  78. @param[in] ImageHandle The image handle of the driver.
  79. @param[in] SystemTable The system table.
  80. @retval EFI_ALREADY_STARTED The driver already exists in system.
  81. @retval EFI_OUT_OF_RESOURCES Fail to execute entry point due to lack of
  82. resources.
  83. @retval EFI_SUCCES All the related protocols are installed on
  84. the driver.
  85. **/
  86. EFI_STATUS
  87. EFIAPI
  88. DtPlatformDxeEntryPoint (
  89. IN EFI_HANDLE ImageHandle,
  90. IN EFI_SYSTEM_TABLE *SystemTable
  91. )
  92. {
  93. EFI_STATUS Status;
  94. DT_ACPI_VARSTORE_DATA DtAcpiPref;
  95. UINTN BufferSize;
  96. VOID *Dtb;
  97. UINTN DtbSize;
  98. Dtb = NULL;
  99. Status = DtPlatformLoadDtb (&Dtb, &DtbSize);
  100. if (EFI_ERROR (Status)) {
  101. DEBUG ((DEBUG_WARN,
  102. "%a: no DTB blob could be loaded, defaulting to ACPI (Status == %r)\n",
  103. __FUNCTION__, Status));
  104. DtAcpiPref.Pref = DT_ACPI_SELECT_ACPI;
  105. } else {
  106. //
  107. // Get the current DT/ACPI preference from the DtAcpiPref variable.
  108. //
  109. BufferSize = sizeof (DtAcpiPref);
  110. Status = gRT->GetVariable(DT_ACPI_VARIABLE_NAME, &gDtPlatformFormSetGuid,
  111. NULL, &BufferSize, &DtAcpiPref);
  112. if (EFI_ERROR (Status)) {
  113. DEBUG ((DEBUG_WARN, "%a: no DT/ACPI preference found, defaulting to DT\n",
  114. __FUNCTION__));
  115. DtAcpiPref.Pref = DT_ACPI_SELECT_DT;
  116. }
  117. }
  118. if (!EFI_ERROR (Status) &&
  119. DtAcpiPref.Pref != DT_ACPI_SELECT_ACPI &&
  120. DtAcpiPref.Pref != DT_ACPI_SELECT_DT) {
  121. DEBUG ((DEBUG_WARN, "%a: invalid value for %s, defaulting to DT\n",
  122. __FUNCTION__, DT_ACPI_VARIABLE_NAME));
  123. DtAcpiPref.Pref = DT_ACPI_SELECT_DT;
  124. Status = EFI_INVALID_PARAMETER; // trigger setvar below
  125. }
  126. //
  127. // Write the newly selected default value back to the variable store.
  128. //
  129. if (EFI_ERROR (Status)) {
  130. Status = gRT->SetVariable(DT_ACPI_VARIABLE_NAME, &gDtPlatformFormSetGuid,
  131. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
  132. sizeof (DtAcpiPref), &DtAcpiPref);
  133. if (EFI_ERROR (Status)) {
  134. goto FreeDtb;
  135. }
  136. }
  137. if (DtAcpiPref.Pref == DT_ACPI_SELECT_ACPI) {
  138. //
  139. // ACPI was selected: install the gEdkiiPlatformHasAcpiGuid GUID as a
  140. // NULL protocol to unlock dispatch of ACPI related drivers.
  141. //
  142. Status = gBS->InstallMultipleProtocolInterfaces (&ImageHandle,
  143. &gEdkiiPlatformHasAcpiGuid, NULL, NULL);
  144. if (EFI_ERROR (Status)) {
  145. DEBUG ((DEBUG_ERROR,
  146. "%a: failed to install gEdkiiPlatformHasAcpiGuid as a protocol\n",
  147. __FUNCTION__));
  148. goto FreeDtb;
  149. }
  150. } else if (DtAcpiPref.Pref == DT_ACPI_SELECT_DT) {
  151. //
  152. // DT was selected: copy the blob into newly allocated memory and install
  153. // a reference to it as the FDT configuration table.
  154. //
  155. Status = gBS->InstallConfigurationTable (&gFdtTableGuid, Dtb);
  156. if (EFI_ERROR (Status)) {
  157. DEBUG ((DEBUG_ERROR, "%a: failed to install FDT configuration table\n",
  158. __FUNCTION__));
  159. goto FreeDtb;
  160. }
  161. } else {
  162. ASSERT (FALSE);
  163. }
  164. //
  165. // No point in installing the HII pages if ACPI is the only description
  166. // we have
  167. //
  168. if (Dtb == NULL) {
  169. return EFI_SUCCESS;
  170. }
  171. //
  172. // Note that we don't uninstall the gEdkiiPlatformHasAcpiGuid protocol nor
  173. // the FDT configuration table if the following call fails. While that will
  174. // cause loading of this driver to fail, proceeding with ACPI and DT both
  175. // disabled will guarantee a failed boot, and so it is better to leave them
  176. // installed in that case.
  177. //
  178. return InstallHiiPages ();
  179. FreeDtb:
  180. if (Dtb != NULL) {
  181. FreePool (Dtb);
  182. }
  183. return Status;
  184. }