PrePiExtractGuidedSectionLib.c 7.9 KB

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