]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Library/PrePiExtractGuidedSectionLib/PrePiExtractGuidedSectionLib.c
e94f5424ef1d5a49fc6bc9b2b68fd3579e80e78d
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiExtractGuidedSectionLib / PrePiExtractGuidedSectionLib.c
1 /** @file
2
3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiPei.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/ExtractGuidedSectionLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/PrePiLib.h>
21
22 #define PRE_PI_EXTRACT_GUIDED_SECTION_DATA_GUID { 0x385A982C, 0x2F49, 0x4043, { 0xA5, 0x1E, 0x49, 0x01, 0x02, 0x5C, 0x8B, 0x6B }}
23
24 typedef struct {
25 UINT32 NumberOfExtractHandler;
26 GUID *ExtractHandlerGuidTable;
27 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;
28 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;
29 } PRE_PI_EXTRACT_GUIDED_SECTION_DATA;
30
31 PRE_PI_EXTRACT_GUIDED_SECTION_DATA *
32 GetSavedData (
33 VOID
34 )
35 {
36 EFI_HOB_GUID_TYPE *GuidHob;
37 GUID SavedDataGuid = PRE_PI_EXTRACT_GUIDED_SECTION_DATA_GUID;
38
39 GuidHob = GetFirstGuidHob(&SavedDataGuid);
40 GuidHob++;
41
42 return (PRE_PI_EXTRACT_GUIDED_SECTION_DATA *)GuidHob;
43 }
44
45 RETURN_STATUS
46 EFIAPI
47 ExtractGuidedSectionRegisterHandlers (
48 IN CONST GUID *SectionGuid,
49 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
50 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
51 )
52 {
53 PRE_PI_EXTRACT_GUIDED_SECTION_DATA *SavedData;
54 UINT32 Index;
55 //
56 // Check input parameter.
57 //
58 if (SectionGuid == NULL) {
59 return RETURN_INVALID_PARAMETER;
60 }
61
62 SavedData = GetSavedData();
63
64 //
65 // Search the match registered GetInfo handler for the input guided section.
66 //
67 for (Index = 0; Index < SavedData->NumberOfExtractHandler; Index ++) {
68 if (CompareGuid (&SavedData->ExtractHandlerGuidTable[Index], SectionGuid)) {
69 break;
70 }
71 }
72
73 //
74 // If the guided handler has been registered before, only update its handler.
75 //
76 if (Index < SavedData->NumberOfExtractHandler) {
77 SavedData->ExtractDecodeHandlerTable [Index] = DecodeHandler;
78 SavedData->ExtractGetInfoHandlerTable [Index] = GetInfoHandler;
79 return RETURN_SUCCESS;
80 }
81
82 //
83 // Check the global table is enough to contain new Handler.
84 //
85 if (SavedData->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
86 return RETURN_OUT_OF_RESOURCES;
87 }
88
89 //
90 // Register new Handler and guid value.
91 //
92 CopyGuid (&SavedData->ExtractHandlerGuidTable [SavedData->NumberOfExtractHandler], SectionGuid);
93 SavedData->ExtractDecodeHandlerTable [SavedData->NumberOfExtractHandler] = DecodeHandler;
94 SavedData->ExtractGetInfoHandlerTable [SavedData->NumberOfExtractHandler++] = GetInfoHandler;
95
96 return RETURN_SUCCESS;
97 }
98
99 UINTN
100 EFIAPI
101 ExtractGuidedSectionGetGuidList (
102 IN OUT GUID **ExtractHandlerGuidTable
103 )
104 {
105 PRE_PI_EXTRACT_GUIDED_SECTION_DATA *SavedData;
106
107 ASSERT(ExtractHandlerGuidTable != NULL);
108
109 SavedData = GetSavedData();
110
111 *ExtractHandlerGuidTable = SavedData->ExtractHandlerGuidTable;
112 return SavedData->NumberOfExtractHandler;
113 }
114
115 RETURN_STATUS
116 EFIAPI
117 ExtractGuidedSectionGetInfo (
118 IN CONST VOID *InputSection,
119 OUT UINT32 *OutputBufferSize,
120 OUT UINT32 *ScratchBufferSize,
121 OUT UINT16 *SectionAttribute
122 )
123 {
124 PRE_PI_EXTRACT_GUIDED_SECTION_DATA *SavedData;
125 UINT32 Index;
126 EFI_GUID *SectionDefinitionGuid;
127
128 if (InputSection == NULL) {
129 return RETURN_INVALID_PARAMETER;
130 }
131
132 ASSERT (OutputBufferSize != NULL);
133 ASSERT (ScratchBufferSize != NULL);
134 ASSERT (SectionAttribute != NULL);
135
136 SavedData = GetSavedData();
137
138 if (IS_SECTION2 (InputSection)) {
139 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid);
140 } else {
141 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid);
142 }
143
144 //
145 // Search the match registered GetInfo handler for the input guided section.
146 //
147 for (Index = 0; Index < SavedData->NumberOfExtractHandler; Index ++) {
148 if (CompareGuid (&SavedData->ExtractHandlerGuidTable[Index], SectionDefinitionGuid)) {
149 break;
150 }
151 }
152
153 //
154 // Not found, the input guided section is not supported.
155 //
156 if (Index == SavedData->NumberOfExtractHandler) {
157 return RETURN_INVALID_PARAMETER;
158 }
159
160 //
161 // Call the match handler to getinfo for the input section data.
162 //
163 return SavedData->ExtractGetInfoHandlerTable [Index] (
164 InputSection,
165 OutputBufferSize,
166 ScratchBufferSize,
167 SectionAttribute
168 );
169 }
170
171 RETURN_STATUS
172 EFIAPI
173 ExtractGuidedSectionDecode (
174 IN CONST VOID *InputSection,
175 OUT VOID **OutputBuffer,
176 OUT VOID *ScratchBuffer, OPTIONAL
177 OUT UINT32 *AuthenticationStatus
178 )
179 {
180 PRE_PI_EXTRACT_GUIDED_SECTION_DATA *SavedData;
181 UINT32 Index;
182 EFI_GUID *SectionDefinitionGuid;
183
184 if (InputSection == NULL) {
185 return RETURN_INVALID_PARAMETER;
186 }
187
188 ASSERT (OutputBuffer != NULL);
189 ASSERT (AuthenticationStatus != NULL);
190
191 SavedData = GetSavedData();
192
193 if (IS_SECTION2 (InputSection)) {
194 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid);
195 } else {
196 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid);
197 }
198
199 //
200 // Search the match registered GetInfo handler for the input guided section.
201 //
202 for (Index = 0; Index < SavedData->NumberOfExtractHandler; Index ++) {
203 if (CompareGuid (&SavedData->ExtractHandlerGuidTable[Index], SectionDefinitionGuid)) {
204 break;
205 }
206 }
207
208 //
209 // Not found, the input guided section is not supported.
210 //
211 if (Index == SavedData->NumberOfExtractHandler) {
212 return RETURN_INVALID_PARAMETER;
213 }
214
215 //
216 // Call the match handler to getinfo for the input section data.
217 //
218 return SavedData->ExtractDecodeHandlerTable [Index] (
219 InputSection,
220 OutputBuffer,
221 ScratchBuffer,
222 AuthenticationStatus
223 );
224 }
225
226 RETURN_STATUS
227 EFIAPI
228 ExtractGuidedSectionLibConstructor (
229 VOID
230 )
231 {
232 PRE_PI_EXTRACT_GUIDED_SECTION_DATA SavedData;
233 GUID HobGuid = PRE_PI_EXTRACT_GUIDED_SECTION_DATA_GUID;
234
235 //
236 // Allocate global pool space to store the registered handler and its guid value.
237 //
238 SavedData.ExtractHandlerGuidTable = (GUID *)AllocatePool(PcdGet32(PcdMaximumGuidedExtractHandler) * sizeof(GUID));
239 if (SavedData.ExtractHandlerGuidTable == NULL) {
240 return RETURN_OUT_OF_RESOURCES;
241 }
242
243 SavedData.ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *)AllocatePool(PcdGet32(PcdMaximumGuidedExtractHandler) * sizeof(EXTRACT_GUIDED_SECTION_DECODE_HANDLER));
244 if (SavedData.ExtractDecodeHandlerTable == NULL) {
245 return RETURN_OUT_OF_RESOURCES;
246 }
247
248 SavedData.ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *)AllocatePool(PcdGet32(PcdMaximumGuidedExtractHandler) * sizeof(EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER));
249 if (SavedData.ExtractGetInfoHandlerTable == NULL) {
250 return RETURN_OUT_OF_RESOURCES;
251 }
252
253 //
254 // the initialized number is Zero.
255 //
256 SavedData.NumberOfExtractHandler = 0;
257
258 BuildGuidDataHob(&HobGuid, &SavedData, sizeof(SavedData));
259
260 return RETURN_SUCCESS;
261 }