]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.c
Adjust directory structures.
[mirror_edk2.git] / MdeModulePkg / Universal / FirmwareVolume / Crc32SectionExtractDxe / Crc32SectionExtract.c
diff --git a/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.c b/MdeModulePkg/Universal/FirmwareVolume/Crc32SectionExtractDxe/Crc32SectionExtract.c
new file mode 100644 (file)
index 0000000..ca5ee21
--- /dev/null
@@ -0,0 +1,265 @@
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  Crc32SectionExtract.c\r
+\r
+Abstract:\r
+\r
+  Implements GUIDed section extraction protocol interface with \r
+  a specific GUID: CRC32.\r
+\r
+  Please refer to the Framewokr Firmware Volume Specification 0.9.\r
+\r
+--*/\r
+\r
+\r
+#include <Crc32SectionExtract.h>\r
+\r
+EFI_STATUS\r
+GuidedSectionExtractionProtocolConstructor (\r
+  OUT EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL      **GuidedSep,\r
+  IN  EFI_EXTRACT_GUIDED_SECTION                  ExtractSection\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Constructor for the GUIDed section extraction protocol.  Initializes\r
+  instance data.\r
+\r
+Arguments:\r
+\r
+  This      Instance to construct\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS:  Instance initialized.\r
+\r
+--*/\r
+// TODO:    GuidedSep - add argument and description to function comment\r
+// TODO:    ExtractSection - add argument and description to function comment\r
+// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
+{\r
+  *GuidedSep = AllocatePool (sizeof (EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL));\r
+  if (*GuidedSep == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  (*GuidedSep)->ExtractSection = ExtractSection;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeCrc32GuidedSectionExtractionProtocol (\r
+  IN EFI_HANDLE                   ImageHandle,\r
+  IN EFI_SYSTEM_TABLE             *SystemTable\r
+  )\r
+/*++\r
+\r
+Routine Description: \r
+\r
+  Entry point of the CRC32 GUIDed section extraction protocol. \r
+  Creates and initializes an instance of the GUIDed section \r
+  extraction protocol with CRC32 GUID.\r
+\r
+Arguments:  \r
+\r
+  ImageHandle   EFI_HANDLE: A handle for the image that is initializing \r
+                this driver\r
+  SystemTable   EFI_SYSTEM_TABLE: A pointer to the EFI system table        \r
+\r
+Returns:  \r
+\r
+  EFI_SUCCESS:          Driver initialized successfully\r
+  EFI_LOAD_ERROR:       Failed to Initialize or has been loaded \r
+  EFI_OUT_OF_RESOURCES: Could not allocate needed resources\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                              Status;\r
+  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *Crc32GuidedSep;\r
+  EFI_HANDLE                              Handle;\r
+\r
+  //\r
+  // Call all constructors per produced protocols\r
+  //\r
+  Status = GuidedSectionExtractionProtocolConstructor (\r
+            &Crc32GuidedSep,\r
+            (EFI_EXTRACT_GUIDED_SECTION) Crc32ExtractSection\r
+            );\r
+  if (EFI_ERROR (Status)) {\r
+    if (Crc32GuidedSep != NULL) {\r
+      FreePool (Crc32GuidedSep);\r
+    }\r
+\r
+    return Status;\r
+  }\r
+  //\r
+  // Pass in a NULL to install to a new handle\r
+  //\r
+  Handle = NULL;\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &Handle,\r
+                  &gEfiCrc32GuidedSectionExtractionProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  Crc32GuidedSep\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (Crc32GuidedSep);\r
+    return EFI_LOAD_ERROR;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+UINT32\r
+EFIAPI\r
+GetSectionLength (\r
+  IN EFI_COMMON_SECTION_HEADER  *CommonHeader\r
+  )\r
+/*++\r
+\r
+  Routine Description:\r
+    Get a length of section.\r
+\r
+  Parameters:\r
+    CommonHeader      -   Pointer to the common section header.\r
+\r
+  Return Value:\r
+    The length of the section, including the section header.\r
+\r
+--*/\r
+// TODO: function comment is missing 'Arguments:'\r
+// TODO: function comment is missing 'Returns:'\r
+// TODO:    CommonHeader - add argument and description to function comment\r
+{\r
+  UINT32  Size;\r
+\r
+  Size = *(UINT32 *) CommonHeader->Size & 0x00FFFFFF;\r
+\r
+  return Size;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+Crc32ExtractSection (\r
+  IN  EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL  *This,\r
+  IN  VOID                                    *InputSection,\r
+  OUT VOID                                    **OutputBuffer,\r
+  OUT UINTN                                   *OutputSize,\r
+  OUT UINT32                                  *AuthenticationStatus\r
+  )\r
+/*++\r
+\r
+  Routine Description:\r
+    This function reads and extracts contents of a section from an\r
+    encapsulating section.\r
+\r
+  Parameters:\r
+    This                    - Indicates the calling context.\r
+    InputSection            - Buffer containing the input GUIDed section \r
+                              to be processed.\r
+    OutputBuffer            - *OutputBuffer is allocated from boot services\r
+                              pool memory and containing the new section\r
+                              stream. The caller is responsible for freeing\r
+                              this buffer.\r
+    AuthenticationStatus    - Pointer to a caller allocated UINT32 that\r
+                              indicates the authentication status of the\r
+                              output buffer\r
+\r
+  Return Value:\r
+    EFI_SUCCESS\r
+    EFI_OUT_OF_RESOURCES\r
+    EFI_INVALID_PARAMETER\r
+    EFI_NOT_AVAILABLE_YET\r
+\r
+--*/\r
+// TODO: function comment is missing 'Arguments:'\r
+// TODO: function comment is missing 'Returns:'\r
+// TODO:    This - add argument and description to function comment\r
+// TODO:    InputSection - add argument and description to function comment\r
+// TODO:    OutputBuffer - add argument and description to function comment\r
+// TODO:    OutputSize - add argument and description to function comment\r
+// TODO:    AuthenticationStatus - add argument and description to function comment\r
+// TODO:    EFI_INVALID_PARAMETER - add return value to function comment\r
+// TODO:    EFI_INVALID_PARAMETER - add return value to function comment\r
+// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
+// TODO:    EFI_SUCCESS - add return value to function comment\r
+{\r
+  EFI_STATUS                Status;\r
+  CRC32_SECTION_HEADER      *Crc32SectionHeader;\r
+  EFI_GUID_DEFINED_SECTION  *GuidedSectionHeader;\r
+  UINT8                     *Image;\r
+  UINT32                    Crc32Checksum;\r
+  VOID                      *DummyInterface;\r
+\r
+  if (OutputBuffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  *OutputBuffer = NULL;\r
+\r
+  //\r
+  // Points to the section header\r
+  //\r
+  Crc32SectionHeader  = (CRC32_SECTION_HEADER *) InputSection;\r
+  GuidedSectionHeader = (EFI_GUID_DEFINED_SECTION *) InputSection;\r
+\r
+  //\r
+  // Check if the GUID is a CRC32 section GUID\r
+  //\r
+  if (!CompareGuid (\r
+        &(GuidedSectionHeader->SectionDefinitionGuid),\r
+        &gEfiCrc32GuidedSectionExtractionProtocolGuid\r
+        )) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Image = (UINT8 *) InputSection + (UINT32) (GuidedSectionHeader->DataOffset);\r
+  *OutputSize = GetSectionLength ((EFI_COMMON_SECTION_HEADER *) InputSection) - (UINT32) GuidedSectionHeader->DataOffset;\r
+\r
+  *OutputBuffer = AllocatePool (*OutputSize);\r
+  if (*OutputBuffer == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Implictly CRC32 GUIDed section should have STATUS_VALID bit set\r
+  //\r
+  ASSERT (GuidedSectionHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID);\r
+  *AuthenticationStatus = EFI_AUTH_STATUS_IMAGE_SIGNED;\r
+\r
+  //\r
+  // Check whether there exists EFI_SECURITY_POLICY_PROTOCOL_GUID.\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiSecurityPolicyProtocolGuid, NULL, &DummyInterface);\r
+  if (!EFI_ERROR (Status)) {\r
+    *AuthenticationStatus |= EFI_AUTH_STATUS_PLATFORM_OVERRIDE;\r
+  } else {\r
+    //\r
+    // Calculate CRC32 Checksum of Image\r
+    //\r
+    gBS->CalculateCrc32 (Image, *OutputSize, &Crc32Checksum);\r
+    if (Crc32Checksum != Crc32SectionHeader->CRC32Checksum) {\r
+      *AuthenticationStatus |= EFI_AUTH_STATUS_TEST_FAILED;\r
+    }\r
+  }\r
+\r
+  CopyMem (*OutputBuffer, Image, *OutputSize);\r
+\r
+  return EFI_SUCCESS;\r
+}\r