FmpPayloadHeaderLib.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /** @file
  2. Provides services to retrieve values from Version 1 of a capsule's FMP Payload
  3. Header. The FMP Payload Header structure is not defined in the library class.
  4. Instead, services are provided to retrieve information from the FMP Payload
  5. Header. If information is added to the FMP Payload Header, then new services
  6. may be added to this library class to retrieve the new information.
  7. Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
  8. Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
  9. SPDX-License-Identifier: BSD-2-Clause-Patent
  10. **/
  11. #include <PiDxe.h>
  12. #include <Library/FmpPayloadHeaderLib.h>
  13. ///
  14. /// Define FMP Payload Header structure here so it is not public
  15. ///
  16. #pragma pack(1)
  17. typedef struct {
  18. UINT32 Signature;
  19. UINT32 HeaderSize;
  20. UINT32 FwVersion;
  21. UINT32 LowestSupportedVersion;
  22. } FMP_PAYLOAD_HEADER;
  23. #pragma pack()
  24. ///
  25. /// Identifier is used to make sure the data in the header is for this structure
  26. /// and version. If the structure changes update the last digit.
  27. ///
  28. #define FMP_PAYLOAD_HEADER_SIGNATURE SIGNATURE_32 ('M', 'S', 'S', '1')
  29. /**
  30. Returns the FMP Payload Header size in bytes.
  31. @param[in] Header FMP Payload Header to evaluate
  32. @param[in] FmpPayloadSize Size of FMP payload
  33. @param[out] Size The size, in bytes, of the FMP Payload Header.
  34. @retval EFI_SUCCESS The firmware version was returned.
  35. @retval EFI_INVALID_PARAMETER Header is NULL.
  36. @retval EFI_INVALID_PARAMETER Size is NULL.
  37. @retval EFI_INVALID_PARAMETER Header is not a valid FMP Payload Header.
  38. **/
  39. EFI_STATUS
  40. EFIAPI
  41. GetFmpPayloadHeaderSize (
  42. IN CONST VOID *Header,
  43. IN CONST UINTN FmpPayloadSize,
  44. OUT UINT32 *Size
  45. )
  46. {
  47. FMP_PAYLOAD_HEADER *FmpPayloadHeader;
  48. FmpPayloadHeader = NULL;
  49. if (Header == NULL || Size == NULL) {
  50. return EFI_INVALID_PARAMETER;
  51. }
  52. FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;
  53. if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||
  54. (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||
  55. FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {
  56. return EFI_INVALID_PARAMETER;
  57. }
  58. if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {
  59. return EFI_INVALID_PARAMETER;
  60. }
  61. *Size = FmpPayloadHeader->HeaderSize;
  62. return EFI_SUCCESS;
  63. }
  64. /**
  65. Returns the version described in the FMP Payload Header.
  66. @param[in] Header FMP Payload Header to evaluate
  67. @param[in] FmpPayloadSize Size of FMP payload
  68. @param[out] Version The firmware version described in the FMP Payload
  69. Header.
  70. @retval EFI_SUCCESS The firmware version was returned.
  71. @retval EFI_INVALID_PARAMETER Header is NULL.
  72. @retval EFI_INVALID_PARAMETER Version is NULL.
  73. @retval EFI_INVALID_PARAMETER Header is not a valid FMP Payload Header.
  74. **/
  75. EFI_STATUS
  76. EFIAPI
  77. GetFmpPayloadHeaderVersion (
  78. IN CONST VOID *Header,
  79. IN CONST UINTN FmpPayloadSize,
  80. OUT UINT32 *Version
  81. )
  82. {
  83. FMP_PAYLOAD_HEADER *FmpPayloadHeader;
  84. FmpPayloadHeader = NULL;
  85. if (Header == NULL || Version == NULL) {
  86. return EFI_INVALID_PARAMETER;
  87. }
  88. FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;
  89. if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||
  90. (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||
  91. FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {
  92. return EFI_INVALID_PARAMETER;
  93. }
  94. if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {
  95. return EFI_INVALID_PARAMETER;
  96. }
  97. *Version = FmpPayloadHeader->FwVersion;
  98. return EFI_SUCCESS;
  99. }
  100. /**
  101. Returns the lowest supported version described in the FMP Payload Header.
  102. @param[in] Header FMP Payload Header to evaluate
  103. @param[in] FmpPayloadSize Size of FMP payload
  104. @param[out] LowestSupportedVersion The lowest supported version described in
  105. the FMP Payload Header.
  106. @retval EFI_SUCCESS The lowest support version was returned.
  107. @retval EFI_INVALID_PARAMETER Header is NULL.
  108. @retval EFI_INVALID_PARAMETER LowestSupportedVersion is NULL.
  109. @retval EFI_INVALID_PARAMETER Header is not a valid FMP Payload Header.
  110. **/
  111. EFI_STATUS
  112. EFIAPI
  113. GetFmpPayloadHeaderLowestSupportedVersion (
  114. IN CONST VOID *Header,
  115. IN CONST UINTN FmpPayloadSize,
  116. OUT UINT32 *LowestSupportedVersion
  117. )
  118. {
  119. FMP_PAYLOAD_HEADER *FmpPayloadHeader;
  120. FmpPayloadHeader = NULL;
  121. if (Header == NULL || LowestSupportedVersion == NULL) {
  122. return EFI_INVALID_PARAMETER;
  123. }
  124. FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;
  125. if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||
  126. (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||
  127. FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {
  128. return EFI_INVALID_PARAMETER;
  129. }
  130. if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {
  131. return EFI_INVALID_PARAMETER;
  132. }
  133. *LowestSupportedVersion = FmpPayloadHeader->LowestSupportedVersion;
  134. return EFI_SUCCESS;
  135. }