]> git.proxmox.com Git - mirror_edk2.git/blame - PrmPkg/Samples/PrmSampleContextBufferModule/Library/DxeContextBufferModuleConfigLib/DxeContextBufferModuleConfigLib.c
PrmPkg: Remove ALLOCATE_CONTEXT_BUFFER_IN_FW build flag
[mirror_edk2.git] / PrmPkg / Samples / PrmSampleContextBufferModule / Library / DxeContextBufferModuleConfigLib / DxeContextBufferModuleConfigLib.c
CommitLineData
7c41ec47
MK
1/** @file\r
2\r
3 The boot services environment configuration library for the Context Buffer Sample PRM module.\r
4\r
5 Copyright (c) Microsoft Corporation\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include <Library/BaseLib.h>\r
11#include <Library/BaseMemoryLib.h>\r
12#include <Library/DebugLib.h>\r
13#include <Library/MemoryAllocationLib.h>\r
14#include <Library/UefiBootServicesTableLib.h>\r
15#include <Protocol/PrmConfig.h>\r
16#include <Samples/PrmSampleContextBufferModule/Include/StaticData.h>\r
17\r
18#include <PrmContextBuffer.h>\r
19#include <PrmDataBuffer.h>\r
20\r
21STATIC EFI_HANDLE mPrmConfigProtocolHandle;\r
22\r
23// {5a6cf42b-8bb4-472c-a233-5c4dc4033dc7}\r
24STATIC CONST EFI_GUID mPrmModuleGuid = {0x5a6cf42b, 0x8bb4, 0x472c, {0xa2, 0x33, 0x5c, 0x4d, 0xc4, 0x03, 0x3d, 0xc7}};\r
25\r
26// {e1466081-7562-430f-896b-b0e523dc335a}\r
27STATIC CONST EFI_GUID mDumpStaticDataBufferPrmHandlerGuid = {0xe1466081, 0x7562, 0x430f, {0x89, 0x6b, 0xb0, 0xe5, 0x23, 0xdc, 0x33, 0x5a}};\r
28\r
29/**\r
30 Populates the static data buffer for this PRM module.\r
31\r
32 @param[out] StaticDataBuffer A pointer to the static data buffer.\r
33\r
34 @retval EFI_SUCCESS The static data buffer was populated successfully.\r
35 @retval EFI_INVALID_PARAMETER The StaticDataBuffer pointer argument is NULL.\r
36\r
37**/\r
38EFI_STATUS\r
39PopulateStaticDataBuffer (\r
40 OUT STATIC_DATA_SAMPLE_CONTEXT_BUFFER_MODULE *StaticDataBuffer\r
41 )\r
42{\r
43 if (StaticDataBuffer == NULL) {\r
44 return EFI_INVALID_PARAMETER;\r
45 }\r
46\r
47 //\r
48 // Note: In a real-world module these values would likely come from somewhere\r
49 // like a Setup menu option, PCD, binary data, runtime device info, etc. Ideally,\r
50 // this configuration library would be provided an API to get what it needs (the data)\r
51 // and not be concerned with how the data is provided. This makes the PRM module more\r
52 // portable across systems.\r
53 //\r
54 StaticDataBuffer->Policy1Enabled = TRUE;\r
55 StaticDataBuffer->Policy2Enabled = FALSE;\r
56 SetMem (StaticDataBuffer->SomeValueArray, ARRAY_SIZE (StaticDataBuffer->SomeValueArray), 'D');\r
57\r
58 return EFI_SUCCESS;\r
59}\r
60\r
61/**\r
62 Allocates and populates the static data buffer for this PRM module.\r
63\r
64 @param[out] StaticDataBuffer A pointer to a pointer to the static data buffer.\r
65\r
66 @retval EFI_SUCCESS The static data buffer was allocated and filled successfully.\r
67 @retval EFI_INVALID_PARAMETER The StaticDataBuffer pointer argument is NULL.\r
68 @retval EFI_OUT_OF_RESOURCES Insufficient memory resources to allocate the static data buffer.\r
69\r
70**/\r
71EFI_STATUS\r
72GetStaticDataBuffer (\r
73 OUT PRM_DATA_BUFFER **StaticDataBuffer\r
74 )\r
75{\r
76 EFI_STATUS Status;\r
77 PRM_DATA_BUFFER *DataBuffer;\r
78 UINTN DataBufferLength;\r
79\r
80 if (StaticDataBuffer == NULL) {\r
81 return EFI_INVALID_PARAMETER;\r
82 }\r
83 *StaticDataBuffer = NULL;\r
84\r
85 //\r
86 // Length of the data buffer = Buffer Header Size + Buffer Data Size\r
87 //\r
88 DataBufferLength = sizeof (PRM_DATA_BUFFER_HEADER) + sizeof (STATIC_DATA_SAMPLE_CONTEXT_BUFFER_MODULE);\r
89\r
90 DataBuffer = AllocateRuntimeZeroPool (DataBufferLength);\r
91 if (DataBuffer == NULL) {\r
92 return EFI_OUT_OF_RESOURCES;\r
93 }\r
94\r
95 //\r
96 // Initialize the data buffer header\r
97 //\r
98 DataBuffer->Header.Signature = PRM_DATA_BUFFER_HEADER_SIGNATURE;\r
99 DataBuffer->Header.Length = (UINT32) DataBufferLength;\r
100\r
101 Status = PopulateStaticDataBuffer ((STATIC_DATA_SAMPLE_CONTEXT_BUFFER_MODULE *) &DataBuffer->Data[0]);\r
102 ASSERT_EFI_ERROR (Status);\r
103\r
104 *StaticDataBuffer = DataBuffer;\r
105 return EFI_SUCCESS;\r
106}\r
107\r
108/**\r
109 Constructor of the PRM configuration library.\r
110\r
111 @param[in] ImageHandle The image handle of the driver.\r
112 @param[in] SystemTable The EFI System Table pointer.\r
113\r
114 @retval EFI_SUCCESS The shell command handlers were installed successfully.\r
115 @retval EFI_UNSUPPORTED The shell level required was not found.\r
116**/\r
117EFI_STATUS\r
118EFIAPI\r
119ContextBufferModuleConfigLibConstructor (\r
120 IN EFI_HANDLE ImageHandle,\r
121 IN EFI_SYSTEM_TABLE *SystemTable\r
122 )\r
123{\r
124 EFI_STATUS Status;\r
125 PRM_CONTEXT_BUFFER *PrmContextBuffer;\r
126 PRM_DATA_BUFFER *StaticDataBuffer;\r
127 PRM_CONFIG_PROTOCOL *PrmConfigProtocol;\r
128\r
129 PrmContextBuffer = NULL;\r
130 StaticDataBuffer = NULL;\r
131 PrmConfigProtocol = NULL;\r
132\r
133 /*\r
134 In this sample PRM module, the protocol describing this sample module's resources is simply\r
135 installed in the constructor.\r
136\r
137 However, if some data is not available until later, this constructor could register a callback\r
138 on the dependency for the data to be available (e.g. ability to communicate with some device)\r
139 and then install the protocol. The requirement is that the protocol is installed before end of DXE.\r
140 */\r
141\r
142 //\r
143 // Allocate and populate the static data buffer\r
144 //\r
145 Status = GetStaticDataBuffer (&StaticDataBuffer);\r
146 ASSERT_EFI_ERROR (Status);\r
147 if (EFI_ERROR (Status) || StaticDataBuffer == NULL) {\r
148 goto Done;\r
149 }\r
150\r
151 //\r
152 // Allocate and populate the context buffer\r
153 //\r
0797989c 154\r
e8467976
MK
155 //\r
156 // This context buffer is not actually used by PRM handler at OS runtime. The OS will allocate\r
157 // the actual context buffer passed to the PRM handler.\r
158 //\r
159 // This context buffer is used internally in the firmware to associate a PRM handler with a\r
160 // a static data buffer and a runtime MMIO ranges array so those can be placed into the\r
161 // PRM_HANDLER_INFORMATION_STRUCT and PRM_MODULE_INFORMATION_STRUCT respectively for the PRM handler.\r
162 //\r
163 PrmContextBuffer = AllocateZeroPool (sizeof (*PrmContextBuffer));\r
7c41ec47
MK
164 ASSERT (PrmContextBuffer != NULL);\r
165 if (PrmContextBuffer == NULL) {\r
166 Status = EFI_OUT_OF_RESOURCES;\r
167 goto Done;\r
168 }\r
169 CopyGuid (&PrmContextBuffer->HandlerGuid, &mDumpStaticDataBufferPrmHandlerGuid);\r
170 PrmContextBuffer->Signature = PRM_CONTEXT_BUFFER_SIGNATURE;\r
171 PrmContextBuffer->Version = PRM_CONTEXT_BUFFER_INTERFACE_VERSION;\r
172 PrmContextBuffer->StaticDataBuffer = StaticDataBuffer;\r
173\r
174 PrmConfigProtocol = AllocateZeroPool (sizeof (*PrmConfigProtocol));\r
175 ASSERT (PrmConfigProtocol != NULL);\r
176 if (PrmConfigProtocol == NULL) {\r
177 Status = EFI_OUT_OF_RESOURCES;\r
178 goto Done;\r
179 }\r
180 CopyGuid (&PrmConfigProtocol->ModuleContextBuffers.ModuleGuid, &mPrmModuleGuid);\r
181 PrmConfigProtocol->ModuleContextBuffers.BufferCount = 1;\r
182 PrmConfigProtocol->ModuleContextBuffers.Buffer = PrmContextBuffer;\r
183\r
184 //\r
185 // Install the PRM Configuration Protocol for this module. This indicates the configuration\r
186 // library has completed resource initialization for the PRM module.\r
187 //\r
188 Status = gBS->InstallProtocolInterface (\r
189 &mPrmConfigProtocolHandle,\r
190 &gPrmConfigProtocolGuid,\r
191 EFI_NATIVE_INTERFACE,\r
192 (VOID *) PrmConfigProtocol\r
193 );\r
194\r
195Done:\r
196 if (EFI_ERROR (Status)) {\r
197 if (StaticDataBuffer != NULL) {\r
198 FreePool (StaticDataBuffer);\r
199 }\r
200 if (PrmContextBuffer != NULL) {\r
201 FreePool (PrmContextBuffer);\r
202 }\r
203 if (PrmConfigProtocol != NULL) {\r
204 FreePool (PrmConfigProtocol);\r
205 }\r
206 }\r
207\r
208 return Status;\r
209}\r