FmpDevicePkg: Add library instances
[mirror_edk2.git] / FmpDevicePkg / Library / FmpPayloadHeaderLibV1 / FmpPayloadHeaderLib.c
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
8 Copyright (c) 2016, Microsoft Corporation. All rights reserved.<BR>
9 Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
10
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 1. Redistributions of source code must retain the above copyright notice,
14 this list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 **/
31
32 #include <PiDxe.h>
33 #include <Library/FmpPayloadHeaderLib.h>
34
35 ///
36 /// Define FMP Payload Header structure here so it is not public
37 ///
38
39 #pragma pack(1)
40
41 typedef struct {
42 UINT32 Signature;
43 UINT32 HeaderSize;
44 UINT32 FwVersion;
45 UINT32 LowestSupportedVersion;
46 } FMP_PAYLOAD_HEADER;
47
48 #pragma pack()
49
50 ///
51 /// Identifier is used to make sure the data in the header is for this structure
52 /// and version. If the structure changes update the last digit.
53 ///
54 #define FMP_PAYLOAD_HEADER_SIGNATURE SIGNATURE_32 ('M', 'S', 'S', '1')
55
56 /**
57 Returns the FMP Payload Header size in bytes.
58
59 @param[in] Header FMP Payload Header to evaluate
60 @param[in] FmpPayloadSize Size of FMP payload
61 @param[out] Size The size, in bytes, of the FMP Payload Header.
62
63 @retval EFI_SUCCESS The firmware version was returned.
64 @retval EFI_INVALID_PARAMETER Header is NULL.
65 @retval EFI_INVALID_PARAMETER Size is NULL.
66 @retval EFI_INVALID_PARAMETER Header is not a valid FMP Payload Header.
67
68 **/
69 EFI_STATUS
70 EFIAPI
71 GetFmpPayloadHeaderSize (
72 IN CONST VOID *Header,
73 IN CONST UINTN FmpPayloadSize,
74 OUT UINT32 *Size
75 )
76 {
77 FMP_PAYLOAD_HEADER *FmpPayloadHeader;
78
79 FmpPayloadHeader = NULL;
80
81 if (Header == NULL || Size == NULL) {
82 return EFI_INVALID_PARAMETER;
83 }
84
85 FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;
86 if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||
87 (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||
88 FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {
89 return EFI_INVALID_PARAMETER;
90 }
91
92 if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {
93 return EFI_INVALID_PARAMETER;
94 }
95
96 *Size = FmpPayloadHeader->HeaderSize;
97 return EFI_SUCCESS;
98 }
99
100 /**
101 Returns the version described in the FMP Payload Header.
102
103 @param[in] Header FMP Payload Header to evaluate
104 @param[in] FmpPayloadSize Size of FMP payload
105 @param[out] Version The firmware version described in the FMP Payload
106 Header.
107
108 @retval EFI_SUCCESS The firmware version was returned.
109 @retval EFI_INVALID_PARAMETER Header is NULL.
110 @retval EFI_INVALID_PARAMETER Version is NULL.
111 @retval EFI_INVALID_PARAMETER Header is not a valid FMP Payload Header.
112
113 **/
114 EFI_STATUS
115 EFIAPI
116 GetFmpPayloadHeaderVersion (
117 IN CONST VOID *Header,
118 IN CONST UINTN FmpPayloadSize,
119 OUT UINT32 *Version
120 )
121 {
122 FMP_PAYLOAD_HEADER *FmpPayloadHeader;
123
124 FmpPayloadHeader = NULL;
125
126 if (Header == NULL || Version == NULL) {
127 return EFI_INVALID_PARAMETER;
128 }
129
130 FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;
131 if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||
132 (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||
133 FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {
134 return EFI_INVALID_PARAMETER;
135 }
136
137 if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {
138 return EFI_INVALID_PARAMETER;
139 }
140
141 *Version = FmpPayloadHeader->FwVersion;
142 return EFI_SUCCESS;
143 }
144
145 /**
146 Returns the lowest supported version described in the FMP Payload Header.
147
148 @param[in] Header FMP Payload Header to evaluate
149 @param[in] FmpPayloadSize Size of FMP payload
150 @param[out] LowestSupportedVersion The lowest supported version described in
151 the FMP Payload Header.
152
153 @retval EFI_SUCCESS The lowest support version was returned.
154 @retval EFI_INVALID_PARAMETER Header is NULL.
155 @retval EFI_INVALID_PARAMETER LowestSupportedVersion is NULL.
156 @retval EFI_INVALID_PARAMETER Header is not a valid FMP Payload Header.
157
158 **/
159 EFI_STATUS
160 EFIAPI
161 GetFmpPayloadHeaderLowestSupportedVersion (
162 IN CONST VOID *Header,
163 IN CONST UINTN FmpPayloadSize,
164 IN OUT UINT32 *LowestSupportedVersion
165 )
166 {
167 FMP_PAYLOAD_HEADER *FmpPayloadHeader;
168
169 FmpPayloadHeader = NULL;
170
171 if (Header == NULL || LowestSupportedVersion == NULL) {
172 return EFI_INVALID_PARAMETER;
173 }
174
175 FmpPayloadHeader = (FMP_PAYLOAD_HEADER *)Header;
176 if ((UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) < (UINTN)FmpPayloadHeader ||
177 (UINTN)FmpPayloadHeader + sizeof (FMP_PAYLOAD_HEADER) >= (UINTN)FmpPayloadHeader + FmpPayloadSize ||
178 FmpPayloadHeader->HeaderSize < sizeof (FMP_PAYLOAD_HEADER)) {
179 return EFI_INVALID_PARAMETER;
180 }
181
182 if (FmpPayloadHeader->Signature != FMP_PAYLOAD_HEADER_SIGNATURE) {
183 return EFI_INVALID_PARAMETER;
184 }
185
186 *LowestSupportedVersion = FmpPayloadHeader->LowestSupportedVersion;
187 return EFI_SUCCESS;
188 }