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