]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/FwVol/Ffs.c
Clean up DxeCore to remove duplicate memory allocation & device path utility services...
[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
9c4ac31c 16#include "DxeMain.h"\r
28a00297 17\r
28a00297 18\r
162ed594 19/**\r
20 Get the FFS file state by checking the highest bit set in the header's state field.\r
21\r
022c6d45 22 @param ErasePolarity Erase polarity attribute of the firmware volume\r
23 @param FfsHeader Points to the FFS file header\r
162ed594 24\r
25 @return FFS File state\r
26\r
27**/\r
28a00297 28EFI_FFS_FILE_STATE\r
29GetFileState (\r
30 IN UINT8 ErasePolarity,\r
31 IN EFI_FFS_FILE_HEADER *FfsHeader\r
32 )\r
28a00297 33{\r
34 EFI_FFS_FILE_STATE FileState;\r
35 UINT8 HighestBit;\r
36\r
37 FileState = FfsHeader->State;\r
38\r
39 if (ErasePolarity != 0) {\r
40 FileState = (EFI_FFS_FILE_STATE)~FileState;\r
41 }\r
42\r
43 HighestBit = 0x80;\r
44 while (HighestBit != 0 && ((HighestBit & FileState) == 0)) {\r
45 HighestBit >>= 1;\r
46 }\r
47\r
e94a9ff7 48 return (EFI_FFS_FILE_STATE) HighestBit;\r
28a00297 49}\r
50\r
51\r
162ed594 52\r
53/**\r
54 Check if a block of buffer is erased.\r
55\r
022c6d45 56 @param ErasePolarity Erase polarity attribute of the firmware volume\r
57 @param InBuffer The buffer to be checked\r
58 @param BufferSize Size of the buffer in bytes\r
162ed594 59\r
022c6d45 60 @retval TRUE The block of buffer is erased\r
162ed594 61 @retval FALSE The block of buffer is not erased\r
62\r
63**/\r
28a00297 64BOOLEAN\r
65IsBufferErased (\r
66 IN UINT8 ErasePolarity,\r
67 IN VOID *InBuffer,\r
68 IN UINTN BufferSize\r
69 )\r
28a00297 70{\r
71 UINTN Count;\r
72 UINT8 EraseByte;\r
73 UINT8 *Buffer;\r
74\r
75 if(ErasePolarity == 1) {\r
76 EraseByte = 0xFF;\r
77 } else {\r
78 EraseByte = 0;\r
79 }\r
80\r
81 Buffer = InBuffer;\r
82 for (Count = 0; Count < BufferSize; Count++) {\r
83 if (Buffer[Count] != EraseByte) {\r
84 return FALSE;\r
85 }\r
86 }\r
87\r
88 return TRUE;\r
89}\r
90\r
91\r
162ed594 92\r
93/**\r
94 Verify checksum of the firmware volume header.\r
95\r
022c6d45 96 @param FvHeader Points to the firmware volume header to be checked\r
162ed594 97\r
022c6d45 98 @retval TRUE Checksum verification passed\r
162ed594 99 @retval FALSE Checksum verification failed\r
100\r
101**/\r
28a00297 102BOOLEAN\r
103VerifyFvHeaderChecksum (\r
104 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader\r
105 )\r
28a00297 106{\r
107 UINT32 Index;\r
108 UINT32 HeaderLength;\r
109 UINT16 Checksum;\r
162ed594 110 UINT16 *Ptr;\r
28a00297 111\r
112 HeaderLength = FvHeader->HeaderLength;\r
162ed594 113 Ptr = (UINT16 *)FvHeader;\r
28a00297 114 Checksum = 0;\r
115\r
116 for (Index = 0; Index < HeaderLength / sizeof (UINT16); Index++) {\r
162ed594 117 Checksum = (UINT16)(Checksum + Ptr[Index]);\r
28a00297 118 }\r
119\r
120 if (Checksum == 0) {\r
121 return TRUE;\r
122 } else {\r
123 return FALSE;\r
124 }\r
125}\r
126\r
162ed594 127\r
128/**\r
129 Verify checksum of the FFS file header.\r
130\r
022c6d45 131 @param FfsHeader Points to the FFS file header to be checked\r
162ed594 132\r
022c6d45 133 @retval TRUE Checksum verification passed\r
162ed594 134 @retval FALSE Checksum verification failed\r
135\r
136**/\r
28a00297 137BOOLEAN\r
138VerifyHeaderChecksum (\r
139 IN EFI_FFS_FILE_HEADER *FfsHeader\r
140 )\r
28a00297 141{\r
142 UINT32 Index;\r
162ed594 143 UINT8 *Ptr;\r
28a00297 144 UINT8 HeaderChecksum;\r
145\r
162ed594 146 Ptr = (UINT8 *)FfsHeader;\r
28a00297 147 HeaderChecksum = 0;\r
148 for (Index = 0; Index < sizeof(EFI_FFS_FILE_HEADER); Index++) {\r
162ed594 149 HeaderChecksum = (UINT8)(HeaderChecksum + Ptr[Index]);\r
28a00297 150 }\r
151\r
152 HeaderChecksum = (UINT8) (HeaderChecksum - FfsHeader->State - FfsHeader->IntegrityCheck.Checksum.File);\r
153\r
154 if (HeaderChecksum == 0) {\r
155 return TRUE;\r
156 } else {\r
157 return FALSE;\r
158 }\r
159}\r
160\r
161\r
162ed594 162\r
163/**\r
164 Check if it's a valid FFS file header.\r
165\r
022c6d45 166 @param ErasePolarity Erase polarity attribute of the firmware volume\r
167 @param FfsHeader Points to the FFS file header to be checked\r
168 @param FileState FFS file state to be returned\r
162ed594 169\r
022c6d45 170 @retval TRUE Valid FFS file header\r
162ed594 171 @retval FALSE Invalid FFS file header\r
172\r
173**/\r
28a00297 174BOOLEAN\r
175IsValidFfsHeader (\r
176 IN UINT8 ErasePolarity,\r
177 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
178 OUT EFI_FFS_FILE_STATE *FileState\r
179 )\r
28a00297 180{\r
181 *FileState = GetFileState (ErasePolarity, FfsHeader);\r
182\r
183 switch (*FileState) {\r
e94a9ff7 184 case EFI_FILE_HEADER_VALID:\r
185 case EFI_FILE_DATA_VALID:\r
186 case EFI_FILE_MARKED_FOR_UPDATE:\r
187 case EFI_FILE_DELETED:\r
188 //\r
189 // Here we need to verify header checksum\r
190 //\r
191 return VerifyHeaderChecksum (FfsHeader);\r
022c6d45 192\r
e94a9ff7 193 case EFI_FILE_HEADER_CONSTRUCTION:\r
194 case EFI_FILE_HEADER_INVALID:\r
195 default:\r
196 return FALSE;\r
28a00297 197 }\r
198}\r
199\r
200\r
162ed594 201/**\r
202 Check if it's a valid FFS file.\r
203 Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.\r
204\r
022c6d45 205 @param ErasePolarity Erase polarity attribute of the firmware volume\r
206 @param FfsHeader Points to the FFS file to be checked\r
162ed594 207\r
022c6d45 208 @retval TRUE Valid FFS file\r
162ed594 209 @retval FALSE Invalid FFS file\r
210\r
211**/\r
28a00297 212BOOLEAN\r
213IsValidFfsFile (\r
214 IN UINT8 ErasePolarity,\r
215 IN EFI_FFS_FILE_HEADER *FfsHeader\r
216 )\r
28a00297 217{\r
218 EFI_FFS_FILE_STATE FileState;\r
219\r
220 FileState = GetFileState (ErasePolarity, FfsHeader);\r
221 switch (FileState) {\r
222\r
e94a9ff7 223 case EFI_FILE_DELETED:\r
224 case EFI_FILE_DATA_VALID:\r
225 case EFI_FILE_MARKED_FOR_UPDATE:\r
226 //\r
227 // Some other vliadation like file content checksum might be done here.\r
228 // For performance issue, Tiano only do FileState check.\r
229 //\r
230 return TRUE;\r
28a00297 231\r
e94a9ff7 232 default:\r
233 return FALSE;\r
28a00297 234 }\r
235}\r
236\r
162ed594 237\r