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