--- /dev/null
+/** @file\r
+\r
+ The PRM Buffer Context library provides a general abstraction for context buffer management.\r
+\r
+ Copyright (c) Microsoft Corporation\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#ifndef PRM_CONTEXT_BUFFER_LIB_H_\r
+#define PRM_CONTEXT_BUFFER_LIB_H_\r
+\r
+#include <Base.h>\r
+#include <PrmContextBuffer.h>\r
+#include <Uefi.h>\r
+\r
+typedef enum {\r
+ ///\r
+ /// Search by the PRM module GUID\r
+ ///\r
+ ByModuleGuid,\r
+ ///\r
+ /// Search by the PRM handler GUID\r
+ ///\r
+ ByHandlerGuid\r
+} PRM_GUID_SEARCH_TYPE;\r
+\r
+/**\r
+ Finds a PRM context buffer for the given PRM handler GUID.\r
+\r
+ Note: PRM_MODULE_CONTEXT_BUFFERS is at the PRM module level while PRM_CONTEXT_BUFFER is at the PRM handler level.\r
+\r
+ @param[in] HandlerGuid A pointer to the PRM handler GUID.\r
+ @param[in] ModuleContextBuffers A pointer to the PRM context buffers structure for the PRM module.\r
+ @param[out] PrmModuleContextBuffer A pointer to a pointer that will be set to the PRM context buffer\r
+ if successfully found.\r
+\r
+ @retval EFI_SUCCESS The PRM context buffer was found.\r
+ @retval EFI_INVALID_PARAMETER A required parameter pointer is NULL.\r
+ @retval EFI_NOT_FOUND The context buffer for the given PRM handler GUID could not be found.\r
+\r
+**/\r
+EFI_STATUS\r
+FindContextBufferInModuleBuffers (\r
+ IN CONST EFI_GUID *HandlerGuid,\r
+ IN CONST PRM_MODULE_CONTEXT_BUFFERS *ModuleContextBuffers,\r
+ OUT CONST PRM_CONTEXT_BUFFER **ContextBuffer\r
+ );\r
+\r
+/**\r
+ Returns a PRM context buffers structure for the given PRM search type.\r
+\r
+ This function allows a caller to get the context buffers structure for a PRM module with either the PRM module\r
+ GUID or the GUID for a PRM handler in the module.\r
+\r
+ Note: PRM_MODULE_CONTEXT_BUFFERS is at the PRM module level while PRM_CONTEXT_BUFFER is at the PRM handler level.\r
+\r
+ @param[in] GuidSearchType The type of GUID passed in the Guid argument.\r
+ @param[in] Guid A pointer to the GUID of a PRM module or PRM handler. The actual GUID type\r
+ will be interpreted based on the value passed in GuidSearchType.\r
+ @param[out] PrmModuleContextBuffers A pointer to a pointer that will be set to the PRM context buffers\r
+ structure if successfully found.\r
+\r
+ @retval EFI_SUCCESS The PRM context buffers structure was found.\r
+ @retval EFI_INVALID_PARAMETER A required parameter pointer is NULL.\r
+ @retval EFI_NOT_FOUND The context buffers for the given GUID could not be found.\r
+\r
+**/\r
+EFI_STATUS\r
+GetModuleContextBuffers (\r
+ IN PRM_GUID_SEARCH_TYPE GuidSearchType,\r
+ IN CONST EFI_GUID *Guid,\r
+ OUT CONST PRM_MODULE_CONTEXT_BUFFERS **PrmModuleContextBuffers\r
+ );\r
+\r
+/**\r
+ Returns a PRM context buffer for the given PRM handler.\r
+\r
+ @param[in] PrmHandlerGuid A pointer to the GUID for the PRM handler.\r
+ @param[in] PrmModuleContextBuffers A pointer to a PRM_MODULE_CONTEXT_BUFFERS structure. If this optional\r
+ parameter is provided, the handler context buffer will be searched for in this\r
+ buffer structure which saves time by not performing a global search for the\r
+ module buffer structure.\r
+ @param[out] PrmContextBuffer A pointer to a pointer that will be set to the PRM context buffer\r
+ if successfully found.\r
+\r
+ @retval EFI_SUCCESS The PRM context buffer was found.\r
+ @retval EFI_INVALID_PARAMETER A required parameter pointer is NULL.\r
+ @retval EFI_NOT_FOUND The context buffer for the PRM handler could not be found.\r
+\r
+**/\r
+EFI_STATUS\r
+GetContextBuffer (\r
+ IN CONST EFI_GUID *PrmHandlerGuid,\r
+ IN CONST PRM_MODULE_CONTEXT_BUFFERS *PrmModuleContextBuffers OPTIONAL,\r
+ OUT CONST PRM_CONTEXT_BUFFER **PrmContextBuffer\r
+ );\r
+\r
+#endif\r
--- /dev/null
+/** @file\r
+\r
+ The PRM Buffer Context library provides a general abstraction for context buffer management.\r
+\r
+ Copyright (c) Microsoft Corporation\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PrmContextBufferLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Protocol/PrmConfig.h>\r
+\r
+#define _DBGMSGID_ "[PRMCONTEXTBUFFERLIB]"\r
+\r
+/**\r
+ Finds a PRM context buffer for the given PRM handler GUID.\r
+\r
+ Note: PRM_MODULE_CONTEXT_BUFFERS is at the PRM module level while PRM_CONTEXT_BUFFER is at the PRM handler level.\r
+\r
+ @param[in] HandlerGuid A pointer to the PRM handler GUID.\r
+ @param[in] ModuleContextBuffers A pointer to the PRM context buffers structure for the PRM module.\r
+ @param[out] PrmModuleContextBuffer A pointer to a pointer that will be set to the PRM context buffer\r
+ if successfully found.\r
+\r
+ @retval EFI_SUCCESS The PRM context buffer was found.\r
+ @retval EFI_INVALID_PARAMETER A required parameter pointer is NULL.\r
+ @retval EFI_NOT_FOUND The context buffer for the given PRM handler GUID could not be found.\r
+\r
+**/\r
+EFI_STATUS\r
+FindContextBufferInModuleBuffers (\r
+ IN CONST EFI_GUID *HandlerGuid,\r
+ IN CONST PRM_MODULE_CONTEXT_BUFFERS *ModuleContextBuffers,\r
+ OUT CONST PRM_CONTEXT_BUFFER **ContextBuffer\r
+ )\r
+{\r
+ UINTN Index;\r
+\r
+ DEBUG ((DEBUG_INFO, " %a %a - Entry.\n", _DBGMSGID_, __FUNCTION__));\r
+\r
+ if (HandlerGuid == NULL || ModuleContextBuffers == NULL || ContextBuffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ for (Index = 0; Index < ModuleContextBuffers->BufferCount; Index++) {\r
+ if (CompareGuid (&ModuleContextBuffers->Buffer[Index].HandlerGuid, HandlerGuid)) {\r
+ *ContextBuffer = &ModuleContextBuffers->Buffer[Index];\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+ Returns a PRM context buffers structure for the given PRM search type.\r
+\r
+ This function allows a caller to get the context buffers structure for a PRM module with either the PRM module\r
+ GUID or the GUID for a PRM handler in the module.\r
+\r
+ Note: PRM_MODULE_CONTEXT_BUFFERS is at the PRM module level while PRM_CONTEXT_BUFFER is at the PRM handler level.\r
+\r
+ @param[in] GuidSearchType The type of GUID passed in the Guid argument.\r
+ @param[in] Guid A pointer to the GUID of a PRM module or PRM handler. The actual GUID type\r
+ will be interpreted based on the value passed in GuidSearchType.\r
+ @param[out] PrmModuleContextBuffers A pointer to a pointer that will be set to the PRM context buffers\r
+ structure if successfully found.\r
+\r
+ @retval EFI_SUCCESS The PRM context buffers structure was found.\r
+ @retval EFI_INVALID_PARAMETER A required parameter pointer is NULL.\r
+ @retval EFI_NOT_FOUND The context buffers for the given GUID could not be found.\r
+\r
+**/\r
+EFI_STATUS\r
+GetModuleContextBuffers (\r
+ IN PRM_GUID_SEARCH_TYPE GuidSearchType,\r
+ IN CONST EFI_GUID *Guid,\r
+ OUT CONST PRM_MODULE_CONTEXT_BUFFERS **PrmModuleContextBuffers\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN HandleCount;\r
+ UINTN Index;\r
+ EFI_HANDLE *HandleBuffer;\r
+ PRM_CONFIG_PROTOCOL *PrmConfigProtocol;\r
+ CONST PRM_CONTEXT_BUFFER *PrmContextBuffer;\r
+\r
+ DEBUG ((DEBUG_INFO, " %a %a - Entry.\n", _DBGMSGID_, __FUNCTION__));\r
+\r
+ if (Guid == NULL || PrmModuleContextBuffers == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ *PrmModuleContextBuffers = NULL;\r
+\r
+ Status = gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gPrmConfigProtocolGuid,\r
+ NULL,\r
+ &HandleCount,\r
+ &HandleBuffer\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ for (Index = 0; Index < HandleCount; Index++) {\r
+ Status = gBS->HandleProtocol (\r
+ HandleBuffer[Index],\r
+ &gPrmConfigProtocolGuid,\r
+ (VOID **) &PrmConfigProtocol\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ if (EFI_ERROR (Status) || PrmConfigProtocol == NULL) {\r
+ continue;\r
+ }\r
+\r
+ if (GuidSearchType == ByModuleGuid) {\r
+ if (CompareGuid (&PrmConfigProtocol->ModuleContextBuffers.ModuleGuid, Guid)) {\r
+ DEBUG ((\r
+ DEBUG_INFO,\r
+ " %a %a: Found a PRM configuration protocol for PRM module %g.\n",\r
+ _DBGMSGID_,\r
+ __FUNCTION__,\r
+ Guid\r
+ ));\r
+\r
+ *PrmModuleContextBuffers = &PrmConfigProtocol->ModuleContextBuffers;\r
+ return EFI_SUCCESS;\r
+ }\r
+ } else {\r
+ Status = FindContextBufferInModuleBuffers (Guid, &PrmConfigProtocol->ModuleContextBuffers, &PrmContextBuffer);\r
+ if (!EFI_ERROR (Status)) {\r
+ *PrmModuleContextBuffers = &PrmConfigProtocol->ModuleContextBuffers;\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ DEBUG ((\r
+ DEBUG_INFO,\r
+ " %a %a: Could not locate a PRM configuration protocol for PRM handler %g.\n",\r
+ _DBGMSGID_,\r
+ __FUNCTION__,\r
+ Guid\r
+ ));\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+ Returns a PRM context buffer for the given PRM handler.\r
+\r
+ @param[in] PrmHandlerGuid A pointer to the GUID for the PRM handler.\r
+ @param[in] PrmModuleContextBuffers A pointer to a PRM_MODULE_CONTEXT_BUFFERS structure. If this optional\r
+ parameter is provided, the handler context buffer will be searched for in this\r
+ buffer structure which saves time by not performing a global search for the\r
+ module buffer structure.\r
+ @param[out] PrmContextBuffer A pointer to a pointer that will be set to the PRM context buffer\r
+ if successfully found.\r
+\r
+ @retval EFI_SUCCESS The PRM context buffer was found.\r
+ @retval EFI_INVALID_PARAMETER A required parameter pointer is NULL.\r
+ @retval EFI_NOT_FOUND The context buffer for the PRM handler could not be found.\r
+\r
+**/\r
+EFI_STATUS\r
+GetContextBuffer (\r
+ IN CONST EFI_GUID *PrmHandlerGuid,\r
+ IN CONST PRM_MODULE_CONTEXT_BUFFERS *PrmModuleContextBuffers OPTIONAL,\r
+ OUT CONST PRM_CONTEXT_BUFFER **PrmContextBuffer\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ CONST PRM_MODULE_CONTEXT_BUFFERS *ContextBuffers;\r
+\r
+ DEBUG ((DEBUG_INFO, " %a %a - Entry.\n", _DBGMSGID_, __FUNCTION__));\r
+\r
+ if (PrmHandlerGuid == NULL || PrmContextBuffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ *PrmContextBuffer = NULL;\r
+\r
+ if (PrmModuleContextBuffers == NULL) {\r
+ Status = GetModuleContextBuffers (ByHandlerGuid, PrmHandlerGuid, &ContextBuffers);\r
+ if (EFI_ERROR (Status)) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ } else {\r
+ ContextBuffers = PrmModuleContextBuffers;\r
+ }\r
+ Status = FindContextBufferInModuleBuffers (PrmHandlerGuid, ContextBuffers, PrmContextBuffer);\r
+\r
+ return Status;\r
+}\r