]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/FwVol/Ffs.c
Update to fix minor coding style issues.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / Ffs.c
CommitLineData
23c98c94 1/** @file\r
504214c4
LG
2 FFS file access utilities.\r
3\r
23c98c94 4Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
28a00297 12\r
504214c4 13**/\r
28a00297 14\r
15\r
16#include <DxeMain.h>\r
17\r
18#define PHYSICAL_ADDRESS_TO_POINTER(Address) ((VOID *)((UINTN)(Address)))\r
19\r
20\r
162ed594 21/**\r
22 Get the FFS file state by checking the highest bit set in the header's state field.\r
23\r
24 @param ErasePolarity Erase polarity attribute of the firmware volume \r
25 @param FfsHeader Points to the FFS file header \r
26\r
27 @return FFS File state\r
28\r
29**/\r
28a00297 30EFI_FFS_FILE_STATE\r
31GetFileState (\r
32 IN UINT8 ErasePolarity,\r
33 IN EFI_FFS_FILE_HEADER *FfsHeader\r
34 )\r
28a00297 35{\r
36 EFI_FFS_FILE_STATE FileState;\r
37 UINT8 HighestBit;\r
38\r
39 FileState = FfsHeader->State;\r
40\r
41 if (ErasePolarity != 0) {\r
42 FileState = (EFI_FFS_FILE_STATE)~FileState;\r
43 }\r
44\r
45 HighestBit = 0x80;\r
46 while (HighestBit != 0 && ((HighestBit & FileState) == 0)) {\r
47 HighestBit >>= 1;\r
48 }\r
49\r
50 return (EFI_FFS_FILE_STATE)HighestBit;\r
51}\r
52\r
53\r
162ed594 54\r
55/**\r
56 Check if a block of buffer is erased.\r
57\r
58 @param ErasePolarity Erase polarity attribute of the firmware volume \r
59 @param InBuffer The buffer to be checked \r
60 @param BufferSize Size of the buffer in bytes \r
61\r
62 @retval TRUE The block of buffer is erased \r
63 @retval FALSE The block of buffer is not erased\r
64\r
65**/\r
28a00297 66BOOLEAN\r
67IsBufferErased (\r
68 IN UINT8 ErasePolarity,\r
69 IN VOID *InBuffer,\r
70 IN UINTN BufferSize\r
71 )\r
28a00297 72{\r
73 UINTN Count;\r
74 UINT8 EraseByte;\r
75 UINT8 *Buffer;\r
76\r
77 if(ErasePolarity == 1) {\r
78 EraseByte = 0xFF;\r
79 } else {\r
80 EraseByte = 0;\r
81 }\r
82\r
83 Buffer = InBuffer;\r
84 for (Count = 0; Count < BufferSize; Count++) {\r
85 if (Buffer[Count] != EraseByte) {\r
86 return FALSE;\r
87 }\r
88 }\r
89\r
90 return TRUE;\r
91}\r
92\r
93\r
162ed594 94\r
95/**\r
96 Verify checksum of the firmware volume header.\r
97\r
98 @param FvHeader Points to the firmware volume header to be checked \r
99\r
100 @retval TRUE Checksum verification passed \r
101 @retval FALSE Checksum verification failed\r
102\r
103**/\r
28a00297 104BOOLEAN\r
105VerifyFvHeaderChecksum (\r
106 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader\r
107 )\r
28a00297 108{\r
109 UINT32 Index;\r
110 UINT32 HeaderLength;\r
111 UINT16 Checksum;\r
162ed594 112 UINT16 *Ptr;\r
28a00297 113\r
114 HeaderLength = FvHeader->HeaderLength;\r
162ed594 115 Ptr = (UINT16 *)FvHeader;\r
28a00297 116 Checksum = 0;\r
117\r
118 for (Index = 0; Index < HeaderLength / sizeof (UINT16); Index++) {\r
162ed594 119 Checksum = (UINT16)(Checksum + Ptr[Index]);\r
28a00297 120 }\r
121\r
122 if (Checksum == 0) {\r
123 return TRUE;\r
124 } else {\r
125 return FALSE;\r
126 }\r
127}\r
128\r
162ed594 129\r
130/**\r
131 Verify checksum of the FFS file header.\r
132\r
133 @param FfsHeader Points to the FFS file header to be checked \r
134\r
135 @retval TRUE Checksum verification passed \r
136 @retval FALSE Checksum verification failed\r
137\r
138**/\r
28a00297 139BOOLEAN\r
140VerifyHeaderChecksum (\r
141 IN EFI_FFS_FILE_HEADER *FfsHeader\r
142 )\r
28a00297 143{\r
144 UINT32 Index;\r
162ed594 145 UINT8 *Ptr;\r
28a00297 146 UINT8 HeaderChecksum;\r
147\r
162ed594 148 Ptr = (UINT8 *)FfsHeader;\r
28a00297 149 HeaderChecksum = 0;\r
150 for (Index = 0; Index < sizeof(EFI_FFS_FILE_HEADER); Index++) {\r
162ed594 151 HeaderChecksum = (UINT8)(HeaderChecksum + Ptr[Index]);\r
28a00297 152 }\r
153\r
154 HeaderChecksum = (UINT8) (HeaderChecksum - FfsHeader->State - FfsHeader->IntegrityCheck.Checksum.File);\r
155\r
156 if (HeaderChecksum == 0) {\r
157 return TRUE;\r
158 } else {\r
159 return FALSE;\r
160 }\r
161}\r
162\r
163\r
162ed594 164\r
165/**\r
166 Check if it's a valid FFS file header.\r
167\r
168 @param ErasePolarity Erase polarity attribute of the firmware volume \r
169 @param FfsHeader Points to the FFS file header to be checked \r
170 @param FileState FFS file state to be returned \r
171\r
172 @retval TRUE Valid FFS file header \r
173 @retval FALSE Invalid FFS file header\r
174\r
175**/\r
28a00297 176BOOLEAN\r
177IsValidFfsHeader (\r
178 IN UINT8 ErasePolarity,\r
179 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
180 OUT EFI_FFS_FILE_STATE *FileState\r
181 )\r
28a00297 182{\r
183 *FileState = GetFileState (ErasePolarity, FfsHeader);\r
184\r
185 switch (*FileState) {\r
186 case EFI_FILE_HEADER_VALID:\r
187 case EFI_FILE_DATA_VALID:\r
188 case EFI_FILE_MARKED_FOR_UPDATE:\r
189 case EFI_FILE_DELETED:\r
190 //\r
191 // Here we need to verify header checksum\r
192 //\r
193 return VerifyHeaderChecksum (FfsHeader);\r
194 \r
195 case EFI_FILE_HEADER_CONSTRUCTION:\r
196 case EFI_FILE_HEADER_INVALID:\r
197 default:\r
198 return FALSE;\r
199 }\r
200}\r
201\r
202\r
162ed594 203\r
204/**\r
205 Check if it's a valid FFS file.\r
206 Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.\r
207\r
208 @param ErasePolarity Erase polarity attribute of the firmware volume \r
209 @param FfsHeader Points to the FFS file to be checked \r
210\r
211 @retval TRUE Valid FFS file \r
212 @retval FALSE Invalid FFS file\r
213\r
214**/\r
28a00297 215BOOLEAN\r
216IsValidFfsFile (\r
217 IN UINT8 ErasePolarity,\r
218 IN EFI_FFS_FILE_HEADER *FfsHeader\r
219 )\r
28a00297 220{\r
221 EFI_FFS_FILE_STATE FileState;\r
222\r
223 FileState = GetFileState (ErasePolarity, FfsHeader);\r
224 switch (FileState) {\r
225\r
226 case EFI_FILE_DELETED:\r
227 case EFI_FILE_DATA_VALID:\r
228 case EFI_FILE_MARKED_FOR_UPDATE:\r
229 //\r
230 // Some other vliadation like file content checksum might be done here.\r
231 // For performance issue, Tiano only do FileState check.\r
232 //\r
233 return TRUE;\r
234\r
235 default:\r
236 return FALSE;\r
237 }\r
238}\r
239\r
162ed594 240\r