PlatformVariableInitPei.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /** @file
  2. Platform variable initialization PEIM.
  3. This PEIM determines whether to load variable defaults. Ordinarily, the
  4. decision is based on the boot mode, but an OEM hook is provided to override
  5. that. The appropriate HOBs and PCDs are created to signal DXE code to update
  6. the variable default values.
  7. @copyright
  8. Copyright 2012 - 2021 Intel Corporation. <BR>
  9. SPDX-License-Identifier: BSD-2-Clause-Patent
  10. **/
  11. #include "PlatformVariableInitPei.h"
  12. #include <Guid/PlatformVariableCommon.h>
  13. #include <Uefi/UefiInternalFormRepresentation.h>
  14. UINT16 BoardId = BOARD_ID_DEFAULT;
  15. EFI_PEI_PPI_DESCRIPTOR mPpiListPlatformVariableInit = {
  16. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  17. &gPlatformVariableInitPpiGuid,
  18. NULL
  19. };
  20. /**
  21. Apply platform variable defaults.
  22. Create HOBs and set PCDs to prompt the (re-)loading of variable defaults.
  23. Each step is attempted regardless of whether the previous steps succeeded.
  24. If multiple errors occur, only the last error code is returned.
  25. @param[in] Events Bitmap of events that occurred.
  26. @param[in] DefaultId Default store ID, STANDARD or MANUFACTURING.
  27. @retval EFI_SUCCESS All steps completed successfully.
  28. @retval EFI_OUT_OF_RESOURCES One of the HOBs could not be created.
  29. @retval EFI_NOT_FOUND The default data could not be found in FFS.
  30. **/
  31. EFI_STATUS
  32. ApplyPlatformVariableDefaults(
  33. IN UINT8 Events,
  34. IN UINT16 DefaultId
  35. )
  36. {
  37. VOID *Hob;
  38. EFI_STATUS Status;
  39. EFI_STATUS ReturnStatus;
  40. DEBUG((DEBUG_INFO, "Applying platform variable defaults:\n"));
  41. DEBUG((DEBUG_INFO, " Events = 0x%02x\n", Events));
  42. DEBUG((DEBUG_INFO, " DefaultId = 0x%04x\n", DefaultId));
  43. //
  44. // Assume success up front. This will be overwritten if errors occur.
  45. //
  46. ReturnStatus = EFI_SUCCESS;
  47. //
  48. // Send the bitmap of events to the platform variable DXE driver.
  49. //
  50. Hob = BuildGuidDataHob(&gPlatformVariableHobGuid, &Events, sizeof(Events));
  51. if (Hob == NULL) {
  52. DEBUG((DEBUG_ERROR, "Create platform var event HOB: %r!\n", EFI_OUT_OF_RESOURCES));
  53. ReturnStatus = EFI_OUT_OF_RESOURCES;
  54. }
  55. //
  56. // Locate variable default data in FFS and send it to the core variable DXE
  57. // driver to write.
  58. //
  59. Status = CreateDefaultVariableHob(DefaultId, BoardId);
  60. if (EFI_ERROR(Status)) {
  61. DEBUG((DEBUG_ERROR, "create default var HOB: %r!\n", Status));
  62. ReturnStatus = Status;
  63. }
  64. //
  65. // Set the PCD SKU ID.
  66. //
  67. LibPcdSetSku(BoardId);
  68. //
  69. // Set the PCD default store ID.
  70. //
  71. Status = PcdSet16S(PcdSetNvStoreDefaultId, DefaultId);
  72. if (EFI_ERROR(Status)) {
  73. DEBUG((DEBUG_ERROR, "setNVstore default ID PCD: %r!\n", Status));
  74. ReturnStatus = Status;
  75. }
  76. return ReturnStatus;
  77. }
  78. /**
  79. Perform the default variable initializations after variable service is ready.
  80. @param[in] PeiServices General purpose services available to every PEIM.
  81. @param[in] NotifyDescriptor Pointer to Notify PPI descriptor.
  82. @param[in] Interface Pointer to PPI.
  83. @retval EFI_SUCCESS Default setting is initialized into variable.
  84. @retval Other values Can't find the matched default setting.
  85. **/
  86. EFI_STATUS
  87. EFIAPI
  88. PlatformVariablePeiInit(
  89. IN EFI_PEI_SERVICES **PeiServices,
  90. IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
  91. IN VOID *Interface
  92. )
  93. {
  94. EFI_STATUS Status;
  95. UINT8 *SystemConfiguration;
  96. EFI_GUID *SystemConfigurationGuid;
  97. UINTN DataSize;
  98. EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariableServices;
  99. UINT8 Events;
  100. UINT16 DefaultId;
  101. BOOLEAN ApplyDefaults;
  102. SystemConfigurationGuid = PcdGetPtr(PcdSetupVariableGuid);
  103. Events = 0;
  104. DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
  105. if (PlatformVariableHookForHobGeneration(Interface, &Events, &DefaultId)) {
  106. //
  107. // Use events bitmap and default ID returned by PlatformVariableHook.
  108. //
  109. ApplyDefaults = TRUE;
  110. }
  111. else {
  112. //
  113. // If the setup variable does not exist (yet), defaults should be applied.
  114. //
  115. VariableServices = (EFI_PEI_READ_ONLY_VARIABLE2_PPI *)Interface;
  116. SystemConfiguration = NULL;
  117. DataSize = 0;
  118. Status = VariableServices->GetVariable(
  119. VariableServices,
  120. PLATFORM_SETUP_VARIABLE_NAME,
  121. SystemConfigurationGuid,
  122. NULL,
  123. &DataSize,
  124. SystemConfiguration
  125. );
  126. //
  127. // Setup variable is not found. So, set the default setting.
  128. //
  129. if (Status == EFI_NOT_FOUND) {
  130. Events = NULL_VARIABLE_EVENT;
  131. DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
  132. ApplyDefaults = TRUE;
  133. }
  134. else {
  135. ApplyDefaults = FALSE;
  136. }
  137. }
  138. if (ApplyDefaults) {
  139. Status = ApplyPlatformVariableDefaults(Events, DefaultId);
  140. }
  141. else {
  142. //
  143. // Normal case boot flow
  144. //
  145. Events = 0; // no events occurred
  146. BuildGuidDataHob (&gPlatformVariableHobGuid, &Events, sizeof (UINT8));
  147. //
  148. // Patch RP variable value with PC variable in the begining of PEI
  149. //
  150. Status = CreateRPVariableHob (EFI_HII_DEFAULT_CLASS_STANDARD, BoardId);
  151. }
  152. PeiServicesInstallPpi (&mPpiListPlatformVariableInit);
  153. return Status;
  154. }
  155. /**
  156. Variable Init BootMode CallBack
  157. Prepare Knob values based on boot mode
  158. Execute after discovering BootMode
  159. @param[in] PeiServices General purpose services available to every PEIM.
  160. @param[in] NotifyDescriptor Pointer to Notify PPI descriptor.
  161. @param[in] Interface Pointer to PPI.
  162. @retval EFI_SUCCESS Knob Values.
  163. @retval Other values
  164. **/
  165. EFI_STATUS
  166. EFIAPI
  167. VariableInitBootModeCallBack(
  168. IN EFI_PEI_SERVICES **PeiServices,
  169. IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
  170. IN VOID *Interface
  171. ) {
  172. EFI_BOOT_MODE BootMode;
  173. BOOLEAN ApplyDefaults;
  174. UINT8 Events;
  175. UINT16 DefaultId;
  176. EFI_STATUS Status;
  177. Events = 0;
  178. DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
  179. ApplyDefaults = FALSE;
  180. //
  181. // Certain boot modes require defaults to be (re-)applied.
  182. //
  183. Status = PeiServicesGetBootMode(&BootMode);
  184. ASSERT_EFI_ERROR(Status);
  185. if (EFI_ERROR(Status)) {
  186. BootMode = BOOT_WITH_DEFAULT_SETTINGS;
  187. }
  188. if (BootMode == BOOT_WITH_MFG_MODE_SETTINGS) {
  189. Events = MFG_MODE_EVENT;
  190. DefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;
  191. ApplyDefaults = TRUE;
  192. }
  193. else if (BootMode == BOOT_IN_RECOVERY_MODE) {
  194. Events = RECOVERY_MODE_EVENT;
  195. DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
  196. ApplyDefaults = TRUE;
  197. }
  198. else if (BootMode == BOOT_WITH_DEFAULT_SETTINGS) {
  199. Events = CMOS_CLEAR_EVENT;
  200. DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
  201. ApplyDefaults = TRUE;
  202. }
  203. if (ApplyDefaults) {
  204. Status = ApplyPlatformVariableDefaults(Events, DefaultId);
  205. }
  206. return Status;
  207. }
  208. EFI_PEI_NOTIFY_DESCRIPTOR mVariableNotifyList[] = {
  209. {
  210. (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK),
  211. &gEfiPeiReadOnlyVariable2PpiGuid,
  212. PlatformVariablePeiInit
  213. },
  214. {
  215. (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  216. &gUpdateBootModePpiGuid,
  217. VariableInitBootModeCallBack
  218. }
  219. };
  220. EFI_STATUS
  221. EFIAPI
  222. PlatformVariableInitPeiEntry (
  223. IN EFI_PEI_FILE_HANDLE FileHandle,
  224. IN CONST EFI_PEI_SERVICES **PeiServices
  225. )
  226. /*++
  227. --*/
  228. {
  229. EFI_STATUS Status;
  230. PlatformVariableHookForEntry();
  231. // Register notify to set default variable once variable service is ready.
  232. //
  233. Status = PeiServicesNotifyPpi(&mVariableNotifyList[0]);
  234. return Status;
  235. }