]> git.proxmox.com Git - mirror_edk2.git/blobdiff - FmpDevicePkg/Library/FmpPayloadHeaderLibV1/FmpPayloadHeaderLib.c
FmpDevicePkg: Add library instances
[mirror_edk2.git] / FmpDevicePkg / Library / FmpPayloadHeaderLibV1 / FmpPayloadHeaderLib.c
diff --git a/FmpDevicePkg/Library/FmpPayloadHeaderLibV1/FmpPayloadHeaderLib.c b/FmpDevicePkg/Library/FmpPayloadHeaderLibV1/FmpPayloadHeaderLib.c
new file mode 100644 (file)
index 0000000..5f08e8b
--- /dev/null
@@ -0,0 +1,188 @@
+/**  @file\r
+  Provides services to retrieve values from Version 1 of a capsule's FMP Payload\r
+  Header. The FMP Payload Header structure is not defined in the library class.\r
+  Instead, services are provided to retrieve information from the FMP Payload\r
+  Header.  If information is added to the FMP Payload Header, then new services\r
+  may be added to this library class to retrieve the new information.\r
+\r
+  Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>\r
+\r
+  Redistribution and use in source and binary forms, with or without\r
+  modification, are permitted provided that the following conditions are met:\r
+  1. Redistributions of source code must retain the above copyright notice,\r
+  this list of conditions and the following disclaimer.\r
+  2. Redistributions in binary form must reproduce the above copyright notice,\r
+  this list of conditions and the following disclaimer in the documentation\r
+  and/or other materials provided with the distribution.\r
+\r
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND\r
+  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\r
+  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
+  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\r
+  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+#include <Library/FmpPayloadHeaderLib.h>\r
+\r
+///\r
+/// Define FMP Payload Header structure here so it is not public\r
+///\r
+\r
+#pragma pack(1)\r
+\r
+typedef struct {\r
+  UINT32  Signature;\r
+  UINT32  HeaderSize;\r
+  UINT32  FwVersion;\r
+  UINT32  LowestSupportedVersion;\r
+} FMP_PAYLOAD_HEADER;\r
+\r
+#pragma pack()\r
+\r
+///\r
+/// Identifier is used to make sure the data in the header is for this structure\r
+/// and version.  If the structure changes update the last digit.\r
+///\r
+#define FMP_PAYLOAD_HEADER_SIGNATURE SIGNATURE_32 ('M', 'S', 'S', '1')\r
+\r
+/**\r
+  Returns the FMP Payload Header size in bytes.\r
+\r
+  @param[in]  Header          FMP Payload Header to evaluate\r
+  @param[in]  FmpPayloadSize  Size of FMP payload\r
+  @param[out] Size            The size, in bytes, of the FMP Payload Header.\r
+\r
+  @retval EFI_SUCCESS            The firmware version was returned.\r
+  @retval EFI_INVALID_PARAMETER  Header is NULL.\r
+  @retval EFI_INVALID_PARAMETER  Size is NULL.\r
+  @retval EFI_INVALID_PARAMETER  Header is not a valid FMP Payload Header.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetFmpPayloadHeaderSize (\r
+  IN  CONST VOID   *Header,\r
+  IN  CONST UINTN  FmpPayloadSize,\r
+  OUT UINT32       *Size\r
+  )\r
+{\r
+  FMP_PAYLOAD_HEADER  *FmpPayloadHeader;\r
+\r
+  FmpPayloadHeader = NULL;\r
+\r
+  if (Header == NULL || Size == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;\r
+  if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||\r
+      (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||\r
+      FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Size = FmpPayloadHeader->HeaderSize;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Returns the version described in the FMP Payload Header.\r
+\r
+  @param[in]  Header          FMP Payload Header to evaluate\r
+  @param[in]  FmpPayloadSize  Size of FMP payload\r
+  @param[out] Version         The firmware version described in the FMP Payload\r
+                              Header.\r
+\r
+  @retval EFI_SUCCESS            The firmware version was returned.\r
+  @retval EFI_INVALID_PARAMETER  Header is NULL.\r
+  @retval EFI_INVALID_PARAMETER  Version is NULL.\r
+  @retval EFI_INVALID_PARAMETER  Header is not a valid FMP Payload Header.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetFmpPayloadHeaderVersion (\r
+  IN  CONST VOID   *Header,\r
+  IN  CONST UINTN  FmpPayloadSize,\r
+  OUT UINT32       *Version\r
+  )\r
+{\r
+  FMP_PAYLOAD_HEADER  *FmpPayloadHeader;\r
+\r
+  FmpPayloadHeader = NULL;\r
+\r
+  if (Header == NULL || Version == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;\r
+  if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||\r
+      (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||\r
+      FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  *Version = FmpPayloadHeader->FwVersion;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Returns the lowest supported version described in the FMP Payload Header.\r
+\r
+  @param[in]  Header                  FMP Payload Header to evaluate\r
+  @param[in]  FmpPayloadSize          Size of FMP payload\r
+  @param[out] LowestSupportedVersion  The lowest supported version described in\r
+                                      the FMP Payload Header.\r
+\r
+  @retval EFI_SUCCESS            The lowest support version was returned.\r
+  @retval EFI_INVALID_PARAMETER  Header is NULL.\r
+  @retval EFI_INVALID_PARAMETER  LowestSupportedVersion is NULL.\r
+  @retval EFI_INVALID_PARAMETER  Header is not a valid FMP Payload Header.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetFmpPayloadHeaderLowestSupportedVersion (\r
+  IN     CONST VOID   *Header,\r
+  IN     CONST UINTN  FmpPayloadSize,\r
+  IN OUT UINT32       *LowestSupportedVersion\r
+  )\r
+{\r
+  FMP_PAYLOAD_HEADER  *FmpPayloadHeader;\r
+\r
+  FmpPayloadHeader = NULL;\r
+\r
+  if (Header == NULL || LowestSupportedVersion == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;\r
+  if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||\r
+      (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||\r
+      FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  *LowestSupportedVersion = FmpPayloadHeader->LowestSupportedVersion;\r
+  return EFI_SUCCESS;\r
+}\r