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