BaseExtractGuidedSectionLib.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /** @file
  2. Provide generic extract guided section functions.
  3. Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/PcdLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/ExtractGuidedSectionLib.h>
  11. #define EXTRACT_HANDLER_INFO_SIGNATURE SIGNATURE_32 ('E', 'G', 'S', 'I')
  12. typedef struct {
  13. UINT32 Signature;
  14. UINT32 NumberOfExtractHandler;
  15. GUID *ExtractHandlerGuidTable;
  16. EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;
  17. EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;
  18. } EXTRACT_GUIDED_SECTION_HANDLER_INFO;
  19. /**
  20. HandlerInfo table address is set by PcdGuidedExtractHandlerTableAddress, which is used to store
  21. the registered guid and Handler list. When it is initialized, it will be directly returned.
  22. Or, HandlerInfo table will be initialized in this function.
  23. @param[in, out] InfoPointer The pointer to the handler information structure.
  24. @retval RETURN_SUCCESS HandlerInfo table can be used to store guid and function tables.
  25. @retval RETURN_OUT_OF_RESOURCES HandlerInfo table address is not writable.
  26. **/
  27. RETURN_STATUS
  28. GetExtractGuidedSectionHandlerInfo (
  29. IN OUT EXTRACT_GUIDED_SECTION_HANDLER_INFO **InfoPointer
  30. )
  31. {
  32. EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
  33. //
  34. // Set the available memory address to handler info.
  35. //
  36. HandlerInfo = (EXTRACT_GUIDED_SECTION_HANDLER_INFO *)(VOID *)(UINTN)PcdGet64 (PcdGuidedExtractHandlerTableAddress);
  37. if (HandlerInfo == NULL) {
  38. *InfoPointer = NULL;
  39. return EFI_OUT_OF_RESOURCES;
  40. }
  41. //
  42. // First check whether the handler information structure is initialized.
  43. //
  44. if (HandlerInfo->Signature == EXTRACT_HANDLER_INFO_SIGNATURE) {
  45. //
  46. // The handler information has been initialized and is returned.
  47. //
  48. *InfoPointer = HandlerInfo;
  49. return RETURN_SUCCESS;
  50. }
  51. //
  52. // Try to initialize the handler information structure
  53. //
  54. HandlerInfo->Signature = EXTRACT_HANDLER_INFO_SIGNATURE;
  55. if (HandlerInfo->Signature != EXTRACT_HANDLER_INFO_SIGNATURE) {
  56. //
  57. // The handler information structure was not writeable because the memory is not ready.
  58. //
  59. *InfoPointer = NULL;
  60. return RETURN_OUT_OF_RESOURCES;
  61. }
  62. //
  63. // Init HandlerInfo structure
  64. //
  65. HandlerInfo->NumberOfExtractHandler = 0;
  66. HandlerInfo->ExtractHandlerGuidTable = (GUID *)(HandlerInfo + 1);
  67. HandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *)(
  68. (UINT8 *)HandlerInfo->ExtractHandlerGuidTable +
  69. PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID)
  70. );
  71. HandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *)(
  72. (UINT8 *)HandlerInfo->ExtractDecodeHandlerTable +
  73. PcdGet32 (PcdMaximumGuidedExtractHandler) *
  74. sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER)
  75. );
  76. *InfoPointer = HandlerInfo;
  77. return RETURN_SUCCESS;
  78. }
  79. /**
  80. Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
  81. Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
  82. The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
  83. and caller must treat this array of GUIDs as read-only data.
  84. If ExtractHandlerGuidTable is NULL, then ASSERT().
  85. @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
  86. ExtractGuidedSectionRegisterHandlers().
  87. @return The number of the supported extract guided Handler.
  88. **/
  89. UINTN
  90. EFIAPI
  91. ExtractGuidedSectionGetGuidList (
  92. OUT GUID **ExtractHandlerGuidTable
  93. )
  94. {
  95. RETURN_STATUS Status;
  96. EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
  97. ASSERT (ExtractHandlerGuidTable != NULL);
  98. //
  99. // Get all registered handler information
  100. //
  101. Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
  102. if (RETURN_ERROR (Status)) {
  103. *ExtractHandlerGuidTable = NULL;
  104. return 0;
  105. }
  106. //
  107. // Get GuidTable and Table Number
  108. //
  109. ASSERT (HandlerInfo != NULL);
  110. *ExtractHandlerGuidTable = HandlerInfo->ExtractHandlerGuidTable;
  111. return HandlerInfo->NumberOfExtractHandler;
  112. }
  113. /**
  114. Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
  115. for a specific GUID section type.
  116. Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
  117. If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
  118. If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
  119. If SectionGuid is NULL, then ASSERT().
  120. If GetInfoHandler is NULL, then ASSERT().
  121. If DecodeHandler is NULL, then ASSERT().
  122. @param[in] SectionGuid A pointer to the GUID associated with the the handlers
  123. of the GUIDed section type being registered.
  124. @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the
  125. size of the decoded buffer and the size of an optional scratch buffer
  126. required to actually decode the data in a GUIDed section.
  127. @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller
  128. allocated output buffer.
  129. @retval RETURN_SUCCESS The handlers were registered.
  130. @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
  131. **/
  132. RETURN_STATUS
  133. EFIAPI
  134. ExtractGuidedSectionRegisterHandlers (
  135. IN CONST GUID *SectionGuid,
  136. IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
  137. IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
  138. )
  139. {
  140. UINT32 Index;
  141. RETURN_STATUS Status;
  142. EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
  143. //
  144. // Check input parameter
  145. //
  146. ASSERT (SectionGuid != NULL);
  147. ASSERT (GetInfoHandler != NULL);
  148. ASSERT (DecodeHandler != NULL);
  149. //
  150. // Get the registered handler information
  151. //
  152. Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
  153. if (RETURN_ERROR (Status)) {
  154. return Status;
  155. }
  156. //
  157. // Search the match registered GetInfo handler for the input guided section.
  158. //
  159. ASSERT (HandlerInfo != NULL);
  160. for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
  161. if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {
  162. //
  163. // If the guided handler has been registered before, only update its handler.
  164. //
  165. HandlerInfo->ExtractDecodeHandlerTable[Index] = DecodeHandler;
  166. HandlerInfo->ExtractGetInfoHandlerTable[Index] = GetInfoHandler;
  167. return RETURN_SUCCESS;
  168. }
  169. }
  170. //
  171. // Check the global table is enough to contain new Handler.
  172. //
  173. if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
  174. return RETURN_OUT_OF_RESOURCES;
  175. }
  176. //
  177. // Register new Handler and guid value.
  178. //
  179. CopyGuid (HandlerInfo->ExtractHandlerGuidTable + HandlerInfo->NumberOfExtractHandler, SectionGuid);
  180. HandlerInfo->ExtractDecodeHandlerTable[HandlerInfo->NumberOfExtractHandler] = DecodeHandler;
  181. HandlerInfo->ExtractGetInfoHandlerTable[HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;
  182. return RETURN_SUCCESS;
  183. }
  184. /**
  185. Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
  186. EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
  187. The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
  188. optional scratch buffer required to actually decode the data in a GUIDed section.
  189. Examines a GUIDed section specified by InputSection.
  190. If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
  191. then RETURN_UNSUPPORTED is returned.
  192. If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
  193. of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
  194. is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
  195. type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
  196. If InputSection is NULL, then ASSERT().
  197. If OutputBufferSize is NULL, then ASSERT().
  198. If ScratchBufferSize is NULL, then ASSERT().
  199. If SectionAttribute is NULL, then ASSERT().
  200. @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
  201. @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
  202. specified by InputSection were decoded.
  203. @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
  204. InputSection were decoded.
  205. @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
  206. EFI_GUID_DEFINED_SECTION in the PI Specification.
  207. @retval RETURN_SUCCESS Successfully retrieved the required information.
  208. @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
  209. the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
  210. @retval Others The return status from the handler associated with the GUID retrieved from
  211. the section specified by InputSection.
  212. **/
  213. RETURN_STATUS
  214. EFIAPI
  215. ExtractGuidedSectionGetInfo (
  216. IN CONST VOID *InputSection,
  217. OUT UINT32 *OutputBufferSize,
  218. OUT UINT32 *ScratchBufferSize,
  219. OUT UINT16 *SectionAttribute
  220. )
  221. {
  222. UINT32 Index;
  223. RETURN_STATUS Status;
  224. EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
  225. EFI_GUID *SectionDefinitionGuid;
  226. //
  227. // Check input parameter
  228. //
  229. ASSERT (InputSection != NULL);
  230. ASSERT (OutputBufferSize != NULL);
  231. ASSERT (ScratchBufferSize != NULL);
  232. ASSERT (SectionAttribute != NULL);
  233. //
  234. // Get all registered handler information.
  235. //
  236. Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
  237. if (RETURN_ERROR (Status)) {
  238. return Status;
  239. }
  240. if (IS_SECTION2 (InputSection)) {
  241. SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);
  242. } else {
  243. SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);
  244. }
  245. //
  246. // Search the match registered GetInfo handler for the input guided section.
  247. //
  248. ASSERT (HandlerInfo != NULL);
  249. for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
  250. if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {
  251. //
  252. // Call the match handler to get information for the input section data.
  253. //
  254. return HandlerInfo->ExtractGetInfoHandlerTable[Index](
  255. InputSection,
  256. OutputBufferSize,
  257. ScratchBufferSize,
  258. SectionAttribute
  259. );
  260. }
  261. }
  262. //
  263. // Not found, the input guided section is not supported.
  264. //
  265. return RETURN_UNSUPPORTED;
  266. }
  267. /**
  268. Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
  269. EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
  270. The selected handler is used to decode the data in a GUIDed section and return the result in a caller
  271. allocated output buffer.
  272. Decodes the GUIDed section specified by InputSection.
  273. If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
  274. then RETURN_UNSUPPORTED is returned.
  275. If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
  276. of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
  277. is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
  278. decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
  279. then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in a caller
  280. allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
  281. bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
  282. If InputSection is NULL, then ASSERT().
  283. If OutputBuffer is NULL, then ASSERT().
  284. If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
  285. If AuthenticationStatus is NULL, then ASSERT().
  286. @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
  287. @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
  288. @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
  289. @param[out] AuthenticationStatus
  290. A pointer to the authentication status of the decoded output buffer. See the definition
  291. of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
  292. Specification.
  293. @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
  294. @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
  295. @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
  296. **/
  297. RETURN_STATUS
  298. EFIAPI
  299. ExtractGuidedSectionDecode (
  300. IN CONST VOID *InputSection,
  301. OUT VOID **OutputBuffer,
  302. IN VOID *ScratchBuffer OPTIONAL,
  303. OUT UINT32 *AuthenticationStatus
  304. )
  305. {
  306. UINT32 Index;
  307. RETURN_STATUS Status;
  308. EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
  309. EFI_GUID *SectionDefinitionGuid;
  310. //
  311. // Check input parameter
  312. //
  313. ASSERT (InputSection != NULL);
  314. ASSERT (OutputBuffer != NULL);
  315. ASSERT (AuthenticationStatus != NULL);
  316. //
  317. // Get all registered handler information.
  318. //
  319. Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
  320. if (RETURN_ERROR (Status)) {
  321. return Status;
  322. }
  323. if (IS_SECTION2 (InputSection)) {
  324. SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);
  325. } else {
  326. SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);
  327. }
  328. //
  329. // Search the match registered Extract handler for the input guided section.
  330. //
  331. ASSERT (HandlerInfo != NULL);
  332. for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
  333. if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {
  334. //
  335. // Call the match handler to extract raw data for the input guided section.
  336. //
  337. return HandlerInfo->ExtractDecodeHandlerTable[Index](
  338. InputSection,
  339. OutputBuffer,
  340. ScratchBuffer,
  341. AuthenticationStatus
  342. );
  343. }
  344. }
  345. //
  346. // Not found, the input guided section is not supported.
  347. //
  348. return RETURN_UNSUPPORTED;
  349. }
  350. /**
  351. Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and
  352. EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.
  353. Retrieves the handlers associated with SectionGuid and returns them in
  354. GetInfoHandler and DecodeHandler.
  355. If the GUID value specified by SectionGuid has not been registered, then
  356. return RETURN_NOT_FOUND.
  357. If SectionGuid is NULL, then ASSERT().
  358. @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed
  359. section type being retrieved.
  360. @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns
  361. the size of the decoded buffer and the size of an optional scratch
  362. buffer required to actually decode the data in a GUIDed section.
  363. This is an optional parameter that may be NULL. If it is NULL, then
  364. the previously registered handler is not returned.
  365. @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
  366. allocated output buffer. This is an optional parameter that may be NULL.
  367. If it is NULL, then the previously registered handler is not returned.
  368. @retval RETURN_SUCCESS The handlers were retrieved.
  369. @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.
  370. **/
  371. RETURN_STATUS
  372. EFIAPI
  373. ExtractGuidedSectionGetHandlers (
  374. IN CONST GUID *SectionGuid,
  375. OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *GetInfoHandler OPTIONAL,
  376. OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER *DecodeHandler OPTIONAL
  377. )
  378. {
  379. UINT32 Index;
  380. RETURN_STATUS Status;
  381. EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
  382. //
  383. // Check input parameter
  384. //
  385. ASSERT (SectionGuid != NULL);
  386. //
  387. // Get the registered handler information
  388. //
  389. Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
  390. if (RETURN_ERROR (Status)) {
  391. return Status;
  392. }
  393. //
  394. // Search the match registered GetInfo handler for the input guided section.
  395. //
  396. ASSERT (HandlerInfo != NULL);
  397. for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
  398. if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {
  399. //
  400. // If the guided handler has been registered before, then return the registered handlers.
  401. //
  402. if (GetInfoHandler != NULL) {
  403. *GetInfoHandler = HandlerInfo->ExtractGetInfoHandlerTable[Index];
  404. }
  405. if (DecodeHandler != NULL) {
  406. *DecodeHandler = HandlerInfo->ExtractDecodeHandlerTable[Index];
  407. }
  408. return RETURN_SUCCESS;
  409. }
  410. }
  411. return RETURN_NOT_FOUND;
  412. }