--- /dev/null
+/** @file\r
+\r
+ The boot services environment configuration library for the Context Buffer Sample PRM module.\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/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Protocol/PrmConfig.h>\r
+#include <Samples/PrmSampleContextBufferModule/Include/StaticData.h>\r
+\r
+#include <PrmContextBuffer.h>\r
+#include <PrmDataBuffer.h>\r
+\r
+STATIC EFI_HANDLE mPrmConfigProtocolHandle;\r
+\r
+// {5a6cf42b-8bb4-472c-a233-5c4dc4033dc7}\r
+STATIC CONST EFI_GUID mPrmModuleGuid = {0x5a6cf42b, 0x8bb4, 0x472c, {0xa2, 0x33, 0x5c, 0x4d, 0xc4, 0x03, 0x3d, 0xc7}};\r
+\r
+// {e1466081-7562-430f-896b-b0e523dc335a}\r
+STATIC CONST EFI_GUID mDumpStaticDataBufferPrmHandlerGuid = {0xe1466081, 0x7562, 0x430f, {0x89, 0x6b, 0xb0, 0xe5, 0x23, 0xdc, 0x33, 0x5a}};\r
+\r
+/**\r
+ Populates the static data buffer for this PRM module.\r
+\r
+ @param[out] StaticDataBuffer A pointer to the static data buffer.\r
+\r
+ @retval EFI_SUCCESS The static data buffer was populated successfully.\r
+ @retval EFI_INVALID_PARAMETER The StaticDataBuffer pointer argument is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+PopulateStaticDataBuffer (\r
+ OUT STATIC_DATA_SAMPLE_CONTEXT_BUFFER_MODULE *StaticDataBuffer\r
+ )\r
+{\r
+ if (StaticDataBuffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Note: In a real-world module these values would likely come from somewhere\r
+ // like a Setup menu option, PCD, binary data, runtime device info, etc. Ideally,\r
+ // this configuration library would be provided an API to get what it needs (the data)\r
+ // and not be concerned with how the data is provided. This makes the PRM module more\r
+ // portable across systems.\r
+ //\r
+ StaticDataBuffer->Policy1Enabled = TRUE;\r
+ StaticDataBuffer->Policy2Enabled = FALSE;\r
+ SetMem (StaticDataBuffer->SomeValueArray, ARRAY_SIZE (StaticDataBuffer->SomeValueArray), 'D');\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Allocates and populates the static data buffer for this PRM module.\r
+\r
+ @param[out] StaticDataBuffer A pointer to a pointer to the static data buffer.\r
+\r
+ @retval EFI_SUCCESS The static data buffer was allocated and filled successfully.\r
+ @retval EFI_INVALID_PARAMETER The StaticDataBuffer pointer argument is NULL.\r
+ @retval EFI_OUT_OF_RESOURCES Insufficient memory resources to allocate the static data buffer.\r
+\r
+**/\r
+EFI_STATUS\r
+GetStaticDataBuffer (\r
+ OUT PRM_DATA_BUFFER **StaticDataBuffer\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ PRM_DATA_BUFFER *DataBuffer;\r
+ UINTN DataBufferLength;\r
+\r
+ if (StaticDataBuffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ *StaticDataBuffer = NULL;\r
+\r
+ //\r
+ // Length of the data buffer = Buffer Header Size + Buffer Data Size\r
+ //\r
+ DataBufferLength = sizeof (PRM_DATA_BUFFER_HEADER) + sizeof (STATIC_DATA_SAMPLE_CONTEXT_BUFFER_MODULE);\r
+\r
+ DataBuffer = AllocateRuntimeZeroPool (DataBufferLength);\r
+ if (DataBuffer == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ //\r
+ // Initialize the data buffer header\r
+ //\r
+ DataBuffer->Header.Signature = PRM_DATA_BUFFER_HEADER_SIGNATURE;\r
+ DataBuffer->Header.Length = (UINT32) DataBufferLength;\r
+\r
+ Status = PopulateStaticDataBuffer ((STATIC_DATA_SAMPLE_CONTEXT_BUFFER_MODULE *) &DataBuffer->Data[0]);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ *StaticDataBuffer = DataBuffer;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Constructor of the PRM configuration library.\r
+\r
+ @param[in] ImageHandle The image handle of the driver.\r
+ @param[in] SystemTable The EFI System Table pointer.\r
+\r
+ @retval EFI_SUCCESS The shell command handlers were installed successfully.\r
+ @retval EFI_UNSUPPORTED The shell level required was not found.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ContextBufferModuleConfigLibConstructor (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ PRM_CONTEXT_BUFFER *PrmContextBuffer;\r
+ PRM_DATA_BUFFER *StaticDataBuffer;\r
+ PRM_CONFIG_PROTOCOL *PrmConfigProtocol;\r
+\r
+ PrmContextBuffer = NULL;\r
+ StaticDataBuffer = NULL;\r
+ PrmConfigProtocol = NULL;\r
+\r
+ /*\r
+ In this sample PRM module, the protocol describing this sample module's resources is simply\r
+ installed in the constructor.\r
+\r
+ However, if some data is not available until later, this constructor could register a callback\r
+ on the dependency for the data to be available (e.g. ability to communicate with some device)\r
+ and then install the protocol. The requirement is that the protocol is installed before end of DXE.\r
+ */\r
+\r
+ //\r
+ // Allocate and populate the static data buffer\r
+ //\r
+ Status = GetStaticDataBuffer (&StaticDataBuffer);\r
+ ASSERT_EFI_ERROR (Status);\r
+ if (EFI_ERROR (Status) || StaticDataBuffer == NULL) {\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Allocate and populate the context buffer\r
+ //\r
+ // This sample module uses a single context buffer for all the handlers\r
+ // Todo: This can be done more elegantly in the future. Likely though a library service.\r
+ //\r
+ PrmContextBuffer = AllocateRuntimeZeroPool (sizeof (*PrmContextBuffer));\r
+ ASSERT (PrmContextBuffer != NULL);\r
+ if (PrmContextBuffer == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+ CopyGuid (&PrmContextBuffer->HandlerGuid, &mDumpStaticDataBufferPrmHandlerGuid);\r
+ PrmContextBuffer->Signature = PRM_CONTEXT_BUFFER_SIGNATURE;\r
+ PrmContextBuffer->Version = PRM_CONTEXT_BUFFER_INTERFACE_VERSION;\r
+ PrmContextBuffer->StaticDataBuffer = StaticDataBuffer;\r
+\r
+ PrmConfigProtocol = AllocateZeroPool (sizeof (*PrmConfigProtocol));\r
+ ASSERT (PrmConfigProtocol != NULL);\r
+ if (PrmConfigProtocol == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+ CopyGuid (&PrmConfigProtocol->ModuleContextBuffers.ModuleGuid, &mPrmModuleGuid);\r
+ PrmConfigProtocol->ModuleContextBuffers.BufferCount = 1;\r
+ PrmConfigProtocol->ModuleContextBuffers.Buffer = PrmContextBuffer;\r
+\r
+ //\r
+ // Install the PRM Configuration Protocol for this module. This indicates the configuration\r
+ // library has completed resource initialization for the PRM module.\r
+ //\r
+ Status = gBS->InstallProtocolInterface (\r
+ &mPrmConfigProtocolHandle,\r
+ &gPrmConfigProtocolGuid,\r
+ EFI_NATIVE_INTERFACE,\r
+ (VOID *) PrmConfigProtocol\r
+ );\r
+\r
+Done:\r
+ if (EFI_ERROR (Status)) {\r
+ if (StaticDataBuffer != NULL) {\r
+ FreePool (StaticDataBuffer);\r
+ }\r
+ if (PrmContextBuffer != NULL) {\r
+ FreePool (PrmContextBuffer);\r
+ }\r
+ if (PrmConfigProtocol != NULL) {\r
+ FreePool (PrmConfigProtocol);\r
+ }\r
+ }\r
+\r
+ return Status;\r
+}\r
--- /dev/null
+/** @file\r
+\r
+ This PRM Module demonstrates how to configure the module data resources in the firmware boot environment\r
+ and access those resources in a PRM handler at OS runtime.\r
+\r
+ Copyright (c) Microsoft Corporation\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <PrmModule.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/PrintLib.h>\r
+#include <Library/UefiLib.h>\r
+\r
+#include <Samples/PrmSampleContextBufferModule/Include/StaticData.h>\r
+\r
+//\r
+// PRM Handler GUIDs\r
+//\r
+\r
+// {e1466081-7562-430f-896b-b0e523dc335a}\r
+#define DUMP_STATIC_DATA_BUFFER_PRM_HANDLER_GUID {0xe1466081, 0x7562, 0x430f, {0x89, 0x6b, 0xb0, 0xe5, 0x23, 0xdc, 0x33, 0x5a}}\r
+\r
+/**\r
+ Dumps the contents of a given buffer.\r
+\r
+ @param[in] OsServiceDebugPrint A pointer to the debug print OS service.\r
+ @param[in] Buffer A pointer to the buffer that should be dumped.\r
+ @param[in] BufferSize The size of Buffer in bytes.\r
+\r
+**/\r
+STATIC\r
+VOID\r
+DumpBuffer (\r
+ IN PRM_OS_SERVICE_DEBUG_PRINT OsServiceDebugPrint,\r
+ IN CONST VOID *Buffer,\r
+ IN UINTN BufferSize\r
+ )\r
+{\r
+ UINTN Count;\r
+ CONST UINT8 *Char = Buffer;\r
+ CHAR8 DebugMessage[16];\r
+\r
+ if (OsServiceDebugPrint == NULL || Buffer == NULL) {\r
+ return;\r
+ }\r
+\r
+ OsServiceDebugPrint (" ");\r
+ for (Count = 0; Count < BufferSize; Count++)\r
+ {\r
+ if (Count && !(Count % 16)) {\r
+ OsServiceDebugPrint ("\n ");\r
+ }\r
+ AsciiSPrint (\r
+ &DebugMessage[0],\r
+ ARRAY_SIZE (DebugMessage),\r
+ "%02X ",\r
+ Char[Count]\r
+ );\r
+ OsServiceDebugPrint (&DebugMessage[0]);\r
+ }\r
+ OsServiceDebugPrint ("\n\n");\r
+}\r
+\r
+/**\r
+ Prints the contents of this PRM module's static data buffer.\r
+\r
+ @param[in] OsServiceDebugPrint A pointer to the debug print OS service.\r
+ @param[in] StaticDataBuffer A pointer to the static buffer.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+PrintStaticDataBuffer (\r
+ IN PRM_OS_SERVICE_DEBUG_PRINT OsServiceDebugPrint,\r
+ IN CONST STATIC_DATA_SAMPLE_CONTEXT_BUFFER_MODULE *StaticDataBuffer\r
+ )\r
+{\r
+ CHAR8 DebugMessage[256];\r
+\r
+ if (OsServiceDebugPrint == NULL || StaticDataBuffer == NULL) {\r
+ return;\r
+ }\r
+\r
+ AsciiSPrint (\r
+ &DebugMessage[0],\r
+ ARRAY_SIZE (DebugMessage),\r
+ " Policy1Enabled = 0x%x.\n",\r
+ StaticDataBuffer->Policy1Enabled\r
+ );\r
+ OsServiceDebugPrint (&DebugMessage[0]);\r
+\r
+ AsciiSPrint (\r
+ &DebugMessage[0],\r
+ ARRAY_SIZE (DebugMessage),\r
+ " Policy2Enabled = 0x%x.\n",\r
+ StaticDataBuffer->Policy2Enabled\r
+ );\r
+ OsServiceDebugPrint (&DebugMessage[0]);\r
+\r
+ OsServiceDebugPrint (" Dumping SomeValueArray:\n");\r
+ DumpBuffer (\r
+ OsServiceDebugPrint,\r
+ (CONST VOID *) &StaticDataBuffer->SomeValueArray[0],\r
+ ARRAY_SIZE (StaticDataBuffer->SomeValueArray)\r
+ );\r
+}\r
+\r
+/**\r
+ A sample Platform Runtime Mechanism (PRM) handler.\r
+\r
+ This sample handler attempts to read the contents of the static data buffer that were configured\r
+ during the firmware boot environment and print those contents at OS runtime.\r
+\r
+ @param[in] OsServices An array of pointers to OS provided services for PRM handlers\r
+ @param[in] Context Handler context info\r
+\r
+ @retval EFI_STATUS The PRM handler executed successfully.\r
+ @retval Others An error occurred in the PRM handler.\r
+\r
+**/\r
+PRM_HANDLER_EXPORT (DumpStaticDataBufferPrmHandler)\r
+{\r
+ PRM_OS_SERVICE_DEBUG_PRINT OsServiceDebugPrint;\r
+\r
+ if (ContextBuffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ // In the POC, the OS debug print service is assumed to be at the beginning of ParameterBuffer\r
+ OsServiceDebugPrint = *((PRM_OS_SERVICE_DEBUG_PRINT *) ParameterBuffer);\r
+ if (OsServiceDebugPrint == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ OsServiceDebugPrint ("Context Buffer DumpStaticDataBufferPrmHandler entry.\n");\r
+\r
+ if (ContextBuffer->StaticDataBuffer == NULL) {\r
+ OsServiceDebugPrint ("The static buffer is not allocated!\n");\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ OsServiceDebugPrint (" Printing the contents of the static data buffer:\n");\r
+\r
+ //\r
+ // Verify PRM data buffer signature is valid\r
+ //\r
+ if (\r
+ ContextBuffer->Signature != PRM_CONTEXT_BUFFER_SIGNATURE ||\r
+ ContextBuffer->StaticDataBuffer->Header.Signature != PRM_DATA_BUFFER_HEADER_SIGNATURE) {\r
+ OsServiceDebugPrint (" A buffer signature is invalid!\n");\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ PrintStaticDataBuffer (\r
+ OsServiceDebugPrint,\r
+ (CONST STATIC_DATA_SAMPLE_CONTEXT_BUFFER_MODULE *) &(ContextBuffer->StaticDataBuffer->Data[0])\r
+ );\r
+\r
+ OsServiceDebugPrint ("Context Buffer DumpStaticDataBufferPrmHandler exit.\n");\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+//\r
+// Register the PRM export information for this PRM Module\r
+//\r
+PRM_MODULE_EXPORT (\r
+ PRM_HANDLER_EXPORT_ENTRY (DUMP_STATIC_DATA_BUFFER_PRM_HANDLER_GUID, DumpStaticDataBufferPrmHandler)\r
+ );\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+PrmSampleContextBufferModuleInit (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ return EFI_SUCCESS;\r
+}\r