ConsolePrefDxe.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <Uefi.h>
  9. #include <IndustryStandard/Acpi.h>
  10. #include <libfdt.h>
  11. #include <Library/BaseLib.h>
  12. #include <Library/DebugLib.h>
  13. #include <Library/DevicePathLib.h>
  14. #include <Library/HiiLib.h>
  15. #include <Library/UefiBootServicesTableLib.h>
  16. #include <Library/UefiBootServicesTableLib.h>
  17. #include <Library/UefiDriverEntryPoint.h>
  18. #include <Library/UefiLib.h>
  19. #include <Library/UefiRuntimeServicesTableLib.h>
  20. #include <Protocol/AcpiTable.h>
  21. #include <Protocol/AcpiSystemDescriptionTable.h>
  22. #include "ConsolePrefDxe.h"
  23. #define SPCR_SIG EFI_ACPI_2_0_SERIAL_PORT_CONSOLE_REDIRECTION_TABLE_SIGNATURE
  24. extern UINT8 ConsolePrefHiiBin[];
  25. extern UINT8 ConsolePrefDxeStrings[];
  26. typedef struct {
  27. VENDOR_DEVICE_PATH VendorDevicePath;
  28. EFI_DEVICE_PATH_PROTOCOL End;
  29. } HII_VENDOR_DEVICE_PATH;
  30. STATIC HII_VENDOR_DEVICE_PATH mConsolePrefDxeVendorDevicePath = {
  31. {
  32. {
  33. HARDWARE_DEVICE_PATH,
  34. HW_VENDOR_DP,
  35. {
  36. (UINT8) (sizeof (VENDOR_DEVICE_PATH)),
  37. (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)
  38. }
  39. },
  40. CONSOLE_PREF_FORMSET_GUID
  41. },
  42. {
  43. END_DEVICE_PATH_TYPE,
  44. END_ENTIRE_DEVICE_PATH_SUBTYPE,
  45. {
  46. (UINT8) (END_DEVICE_PATH_LENGTH),
  47. (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)
  48. }
  49. }
  50. };
  51. STATIC EFI_EVENT mReadyToBootEvent;
  52. STATIC
  53. EFI_STATUS
  54. InstallHiiPages (
  55. VOID
  56. )
  57. {
  58. EFI_STATUS Status;
  59. EFI_HII_HANDLE HiiHandle;
  60. EFI_HANDLE DriverHandle;
  61. DriverHandle = NULL;
  62. Status = gBS->InstallMultipleProtocolInterfaces (&DriverHandle,
  63. &gEfiDevicePathProtocolGuid,
  64. &mConsolePrefDxeVendorDevicePath,
  65. NULL);
  66. if (EFI_ERROR (Status)) {
  67. return Status;
  68. }
  69. HiiHandle = HiiAddPackages (&gConsolePrefFormSetGuid,
  70. DriverHandle,
  71. ConsolePrefDxeStrings,
  72. ConsolePrefHiiBin,
  73. NULL);
  74. if (HiiHandle == NULL) {
  75. gBS->UninstallMultipleProtocolInterfaces (DriverHandle,
  76. &gEfiDevicePathProtocolGuid,
  77. &mConsolePrefDxeVendorDevicePath,
  78. NULL);
  79. return EFI_OUT_OF_RESOURCES;
  80. }
  81. return EFI_SUCCESS;
  82. }
  83. STATIC
  84. VOID
  85. RemoveDtStdoutPath (
  86. VOID
  87. )
  88. {
  89. VOID *Dtb;
  90. INT32 Node;
  91. INT32 Error;
  92. EFI_STATUS Status;
  93. Status = EfiGetSystemConfigurationTable (&gFdtTableGuid, &Dtb);
  94. if (EFI_ERROR (Status)) {
  95. DEBUG ((DEBUG_INFO, "%a: could not retrieve DT blob - %r\n", __FUNCTION__,
  96. Status));
  97. return;
  98. }
  99. Node = fdt_path_offset (Dtb, "/chosen");
  100. if (Node < 0) {
  101. return;
  102. }
  103. Error = fdt_delprop (Dtb, Node, "stdout-path");
  104. if (Error) {
  105. DEBUG ((DEBUG_INFO, "%a: Failed to delete 'stdout-path' property: %a\n",
  106. __FUNCTION__, fdt_strerror (Error)));
  107. }
  108. }
  109. STATIC
  110. VOID
  111. RemoveSpcrTable (
  112. VOID
  113. )
  114. {
  115. EFI_ACPI_SDT_PROTOCOL *Sdt;
  116. EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
  117. EFI_STATUS Status;
  118. UINTN TableIndex;
  119. EFI_ACPI_SDT_HEADER *TableHeader;
  120. EFI_ACPI_TABLE_VERSION TableVersion;
  121. UINTN TableKey;
  122. Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL,
  123. (VOID **)&AcpiTable);
  124. if (EFI_ERROR (Status)) {
  125. return;
  126. }
  127. Status = gBS->LocateProtocol (&gEfiAcpiSdtProtocolGuid, NULL, (VOID **)&Sdt);
  128. if (EFI_ERROR (Status)) {
  129. return;
  130. }
  131. TableIndex = 0;
  132. TableKey = 0;
  133. TableHeader = NULL;
  134. do {
  135. Status = Sdt->GetAcpiTable (TableIndex++, &TableHeader, &TableVersion,
  136. &TableKey);
  137. if (EFI_ERROR (Status)) {
  138. break;
  139. }
  140. if (TableHeader->Signature != SPCR_SIG) {
  141. continue;
  142. }
  143. Status = AcpiTable->UninstallAcpiTable (AcpiTable, TableKey);
  144. if (EFI_ERROR (Status)) {
  145. DEBUG ((DEBUG_WARN, "%a: failed to uninstall SPCR table - %r\n",
  146. __FUNCTION__, Status));
  147. }
  148. break;
  149. } while (TRUE);
  150. }
  151. STATIC
  152. VOID
  153. OnReadyToBoot (
  154. IN EFI_EVENT Event,
  155. IN VOID *Context
  156. )
  157. {
  158. CONSOLE_PREF_VARSTORE_DATA ConsolePref;
  159. UINTN BufferSize;
  160. EFI_STATUS Status;
  161. VOID *Gop;
  162. BufferSize = sizeof (ConsolePref);
  163. Status = gRT->GetVariable (CONSOLE_PREF_VARIABLE_NAME,
  164. &gConsolePrefFormSetGuid, NULL, &BufferSize, &ConsolePref);
  165. if (EFI_ERROR (Status)) {
  166. DEBUG ((DEBUG_ERROR,
  167. "%a: variable '%s' could not be read - bailing!\n", __FUNCTION__,
  168. CONSOLE_PREF_VARIABLE_NAME));
  169. return;
  170. }
  171. if (ConsolePref.Console == CONSOLE_PREF_SERIAL) {
  172. DEBUG ((DEBUG_INFO,
  173. "%a: serial console preferred - doing nothing\n", __FUNCTION__));
  174. return;
  175. }
  176. //
  177. // Check if any GOP instances exist: if so, disable stdout-path and SPCR
  178. //
  179. Status = gBS->LocateProtocol (&gEfiGraphicsOutputProtocolGuid, NULL, &Gop);
  180. if (EFI_ERROR (Status)) {
  181. DEBUG ((DEBUG_INFO,
  182. "%a: no GOP instances found - doing nothing (%r)\n", __FUNCTION__,
  183. Status));
  184. return;
  185. }
  186. RemoveDtStdoutPath ();
  187. RemoveSpcrTable ();
  188. }
  189. /**
  190. The entry point for ConsolePrefDxe driver.
  191. @param[in] ImageHandle The image handle of the driver.
  192. @param[in] SystemTable The system table.
  193. @retval EFI_ALREADY_STARTED The driver already exists in system.
  194. @retval EFI_OUT_OF_RESOURCES Fail to execute entry point due to lack of
  195. resources.
  196. @retval EFI_SUCCES All the related protocols are installed on
  197. the driver.
  198. **/
  199. EFI_STATUS
  200. EFIAPI
  201. ConsolePrefDxeEntryPoint (
  202. IN EFI_HANDLE ImageHandle,
  203. IN EFI_SYSTEM_TABLE *SystemTable
  204. )
  205. {
  206. EFI_STATUS Status;
  207. CONSOLE_PREF_VARSTORE_DATA ConsolePref;
  208. UINTN BufferSize;
  209. //
  210. // Get the current console preference from the ConsolePref variable.
  211. //
  212. BufferSize = sizeof (ConsolePref);
  213. Status = gRT->GetVariable (CONSOLE_PREF_VARIABLE_NAME,
  214. &gConsolePrefFormSetGuid, NULL, &BufferSize, &ConsolePref);
  215. if (EFI_ERROR (Status)) {
  216. DEBUG ((DEBUG_INFO,
  217. "%a: no console preference found, defaulting to graphical\n",
  218. __FUNCTION__));
  219. ConsolePref.Console = CONSOLE_PREF_GRAPHICAL;
  220. }
  221. if (!EFI_ERROR (Status) &&
  222. ConsolePref.Console != CONSOLE_PREF_GRAPHICAL &&
  223. ConsolePref.Console != CONSOLE_PREF_SERIAL) {
  224. DEBUG ((DEBUG_WARN, "%a: invalid value for %s, defaulting to graphical\n",
  225. __FUNCTION__, CONSOLE_PREF_VARIABLE_NAME));
  226. ConsolePref.Console = CONSOLE_PREF_GRAPHICAL;
  227. Status = EFI_INVALID_PARAMETER; // trigger setvar below
  228. }
  229. //
  230. // Write the newly selected value back to the variable store.
  231. //
  232. if (EFI_ERROR (Status)) {
  233. ZeroMem (&ConsolePref.Reserved, sizeof (ConsolePref.Reserved));
  234. Status = gRT->SetVariable (CONSOLE_PREF_VARIABLE_NAME,
  235. &gConsolePrefFormSetGuid,
  236. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
  237. sizeof (ConsolePref), &ConsolePref);
  238. if (EFI_ERROR (Status)) {
  239. DEBUG ((DEBUG_ERROR, "%a: gRT->SetVariable () failed - %r\n",
  240. __FUNCTION__, Status));
  241. return Status;
  242. }
  243. }
  244. Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  245. OnReadyToBoot, NULL, &gEfiEventReadyToBootGuid,
  246. &mReadyToBootEvent);
  247. ASSERT_EFI_ERROR (Status);
  248. return InstallHiiPages ();
  249. }