DtPlatformDxe.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 (
  56. &DriverHandle,
  57. &gEfiDevicePathProtocolGuid,
  58. &mDtPlatformDxeVendorDevicePath,
  59. NULL
  60. );
  61. if (EFI_ERROR (Status)) {
  62. return Status;
  63. }
  64. HiiHandle = HiiAddPackages (
  65. &gDtPlatformFormSetGuid,
  66. DriverHandle,
  67. DtPlatformDxeStrings,
  68. DtPlatformHiiBin,
  69. NULL
  70. );
  71. if (HiiHandle == NULL) {
  72. gBS->UninstallMultipleProtocolInterfaces (
  73. DriverHandle,
  74. &gEfiDevicePathProtocolGuid,
  75. &mDtPlatformDxeVendorDevicePath,
  76. NULL
  77. );
  78. return EFI_OUT_OF_RESOURCES;
  79. }
  80. return EFI_SUCCESS;
  81. }
  82. /**
  83. The entry point for DtPlatformDxe driver.
  84. @param[in] ImageHandle The image handle of the driver.
  85. @param[in] SystemTable The system table.
  86. @retval EFI_ALREADY_STARTED The driver already exists in system.
  87. @retval EFI_OUT_OF_RESOURCES Fail to execute entry point due to lack of
  88. resources.
  89. @retval EFI_SUCCESS All the related protocols are installed on
  90. the driver.
  91. **/
  92. EFI_STATUS
  93. EFIAPI
  94. DtPlatformDxeEntryPoint (
  95. IN EFI_HANDLE ImageHandle,
  96. IN EFI_SYSTEM_TABLE *SystemTable
  97. )
  98. {
  99. EFI_STATUS Status;
  100. DT_ACPI_VARSTORE_DATA DtAcpiPref;
  101. UINTN BufferSize;
  102. VOID *Dtb;
  103. UINTN DtbSize;
  104. Dtb = NULL;
  105. Status = DtPlatformLoadDtb (&Dtb, &DtbSize);
  106. if (EFI_ERROR (Status)) {
  107. DEBUG ((
  108. DEBUG_WARN,
  109. "%a: no DTB blob could be loaded, defaulting to ACPI (Status == %r)\n",
  110. __FUNCTION__,
  111. Status
  112. ));
  113. DtAcpiPref.Pref = DT_ACPI_SELECT_ACPI;
  114. } else {
  115. //
  116. // Get the current DT/ACPI preference from the DtAcpiPref variable.
  117. //
  118. BufferSize = sizeof (DtAcpiPref);
  119. Status = gRT->GetVariable (
  120. DT_ACPI_VARIABLE_NAME,
  121. &gDtPlatformFormSetGuid,
  122. NULL,
  123. &BufferSize,
  124. &DtAcpiPref
  125. );
  126. if (EFI_ERROR (Status)) {
  127. DEBUG ((
  128. DEBUG_WARN,
  129. "%a: no DT/ACPI preference found, defaulting to %a\n",
  130. __FUNCTION__,
  131. PcdGetBool (PcdDefaultDtPref) ? "DT" : "ACPI"
  132. ));
  133. DtAcpiPref.Pref = PcdGetBool (PcdDefaultDtPref) ? DT_ACPI_SELECT_DT
  134. : DT_ACPI_SELECT_ACPI;
  135. }
  136. }
  137. if (!EFI_ERROR (Status) &&
  138. (DtAcpiPref.Pref != DT_ACPI_SELECT_ACPI) &&
  139. (DtAcpiPref.Pref != DT_ACPI_SELECT_DT))
  140. {
  141. DEBUG ((
  142. DEBUG_WARN,
  143. "%a: invalid value for %s, defaulting to %a\n",
  144. __FUNCTION__,
  145. DT_ACPI_VARIABLE_NAME,
  146. PcdGetBool (PcdDefaultDtPref) ? "DT" : "ACPI"
  147. ));
  148. DtAcpiPref.Pref = PcdGetBool (PcdDefaultDtPref) ? DT_ACPI_SELECT_DT
  149. : DT_ACPI_SELECT_ACPI;
  150. Status = EFI_INVALID_PARAMETER; // trigger setvar below
  151. }
  152. //
  153. // Write the newly selected default value back to the variable store.
  154. //
  155. if (EFI_ERROR (Status)) {
  156. Status = gRT->SetVariable (
  157. DT_ACPI_VARIABLE_NAME,
  158. &gDtPlatformFormSetGuid,
  159. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
  160. sizeof (DtAcpiPref),
  161. &DtAcpiPref
  162. );
  163. if (EFI_ERROR (Status)) {
  164. goto FreeDtb;
  165. }
  166. }
  167. if (DtAcpiPref.Pref == DT_ACPI_SELECT_ACPI) {
  168. //
  169. // ACPI was selected: install the gEdkiiPlatformHasAcpiGuid GUID as a
  170. // NULL protocol to unlock dispatch of ACPI related drivers.
  171. //
  172. Status = gBS->InstallMultipleProtocolInterfaces (
  173. &ImageHandle,
  174. &gEdkiiPlatformHasAcpiGuid,
  175. NULL,
  176. NULL
  177. );
  178. if (EFI_ERROR (Status)) {
  179. DEBUG ((
  180. DEBUG_ERROR,
  181. "%a: failed to install gEdkiiPlatformHasAcpiGuid as a protocol\n",
  182. __FUNCTION__
  183. ));
  184. goto FreeDtb;
  185. }
  186. } else if (DtAcpiPref.Pref == DT_ACPI_SELECT_DT) {
  187. //
  188. // DT was selected: copy the blob into newly allocated memory and install
  189. // a reference to it as the FDT configuration table.
  190. //
  191. Status = gBS->InstallConfigurationTable (&gFdtTableGuid, Dtb);
  192. if (EFI_ERROR (Status)) {
  193. DEBUG ((
  194. DEBUG_ERROR,
  195. "%a: failed to install FDT configuration table\n",
  196. __FUNCTION__
  197. ));
  198. goto FreeDtb;
  199. }
  200. } else {
  201. ASSERT (FALSE);
  202. }
  203. //
  204. // No point in installing the HII pages if ACPI is the only description
  205. // we have
  206. //
  207. if (Dtb == NULL) {
  208. return EFI_SUCCESS;
  209. }
  210. //
  211. // Note that we don't uninstall the gEdkiiPlatformHasAcpiGuid protocol nor
  212. // the FDT configuration table if the following call fails. While that will
  213. // cause loading of this driver to fail, proceeding with ACPI and DT both
  214. // disabled will guarantee a failed boot, and so it is better to leave them
  215. // installed in that case.
  216. //
  217. return InstallHiiPages ();
  218. FreeDtb:
  219. if (Dtb != NULL) {
  220. FreePool (Dtb);
  221. }
  222. return Status;
  223. }