]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/VariableAuthenticated/SecureBootConfigDxe/SecureBootConfigMisc.c
SecurityPkg:Replace unsafe string functions.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / SecureBootConfigDxe / SecureBootConfigMisc.c
CommitLineData
ecc722ad 1/** @file\r
2 Helper functions for SecureBoot configuration module.\r
3\r
c2a65e23 4Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
ecc722ad 5This 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
12\r
13**/\r
14\r
15#include "SecureBootConfigImpl.h"\r
16\r
17/**\r
18 Read file content into BufferPtr, the size of the allocate buffer \r
19 is *FileSize plus AddtionAllocateSize.\r
20\r
21 @param[in] FileHandle The file to be read.\r
22 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.\r
23 @param[out] FileSize Size of input file\r
24 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated. \r
25 In case the buffer need to contain others besides the file content.\r
26 \r
27 @retval EFI_SUCCESS The file was read into the buffer.\r
28 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
29 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
30 @retval others Unexpected error.\r
31\r
32**/\r
33EFI_STATUS\r
34ReadFileContent (\r
35 IN EFI_FILE_HANDLE FileHandle,\r
36 IN OUT VOID **BufferPtr,\r
37 OUT UINTN *FileSize,\r
38 IN UINTN AddtionAllocateSize\r
39 )\r
40\r
41{\r
42 UINTN BufferSize;\r
43 UINT64 SourceFileSize;\r
44 VOID *Buffer;\r
45 EFI_STATUS Status;\r
46\r
47 if ((FileHandle == NULL) || (FileSize == NULL)) {\r
48 return EFI_INVALID_PARAMETER;\r
49 }\r
50\r
51 Buffer = NULL;\r
52\r
53 //\r
54 // Get the file size\r
55 //\r
56 Status = FileHandle->SetPosition (FileHandle, (UINT64) -1);\r
57 if (EFI_ERROR (Status)) {\r
58 goto ON_EXIT;\r
59 }\r
60\r
61 Status = FileHandle->GetPosition (FileHandle, &SourceFileSize);\r
62 if (EFI_ERROR (Status)) {\r
63 goto ON_EXIT;\r
64 }\r
65 \r
66 Status = FileHandle->SetPosition (FileHandle, 0);\r
67 if (EFI_ERROR (Status)) {\r
68 goto ON_EXIT;\r
69 }\r
70\r
71 BufferSize = (UINTN) SourceFileSize + AddtionAllocateSize;\r
72 Buffer = AllocateZeroPool(BufferSize);\r
73 if (Buffer == NULL) {\r
74 return EFI_OUT_OF_RESOURCES;\r
75 }\r
76\r
77 BufferSize = (UINTN) SourceFileSize;\r
78 *FileSize = BufferSize;\r
79\r
80 Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);\r
81 if (EFI_ERROR (Status) || BufferSize != *FileSize) {\r
82 FreePool (Buffer);\r
83 Buffer = NULL;\r
84 Status = EFI_BAD_BUFFER_SIZE;\r
85 goto ON_EXIT;\r
86 }\r
87\r
88ON_EXIT:\r
89 \r
90 *BufferPtr = Buffer;\r
91 return Status;\r
92}\r
93\r
94/**\r
95 Close an open file handle.\r
96\r
97 @param[in] FileHandle The file handle to close.\r
98 \r
99**/\r
100VOID\r
101CloseFile (\r
102 IN EFI_FILE_HANDLE FileHandle\r
103 )\r
104{\r
105 if (FileHandle != NULL) {\r
106 FileHandle->Close (FileHandle); \r
107 }\r
108}\r
109\r
110/**\r
111 Convert a nonnegative integer to an octet string of a specified length.\r
112\r
113 @param[in] Integer Pointer to the nonnegative integer to be converted\r
114 @param[in] IntSizeInWords Length of integer buffer in words\r
115 @param[out] OctetString Converted octet string of the specified length \r
116 @param[in] OSSizeInBytes Intended length of resulting octet string in bytes\r
117\r
118Returns:\r
119\r
120 @retval EFI_SUCCESS Data conversion successfully\r
121 @retval EFI_BUFFER_TOOL_SMALL Buffer is too small for output string\r
122\r
123**/\r
124EFI_STATUS\r
125EFIAPI\r
126Int2OctStr (\r
127 IN CONST UINTN *Integer,\r
128 IN UINTN IntSizeInWords,\r
129 OUT UINT8 *OctetString,\r
130 IN UINTN OSSizeInBytes\r
131 )\r
132{\r
133 CONST UINT8 *Ptr1;\r
134 UINT8 *Ptr2;\r
135\r
136 for (Ptr1 = (CONST UINT8 *)Integer, Ptr2 = OctetString + OSSizeInBytes - 1;\r
137 Ptr1 < (UINT8 *)(Integer + IntSizeInWords) && Ptr2 >= OctetString;\r
138 Ptr1++, Ptr2--) {\r
139 *Ptr2 = *Ptr1;\r
140 }\r
141 \r
142 for (; Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords) && *Ptr1 == 0; Ptr1++);\r
143 \r
144 if (Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords)) {\r
145 return EFI_BUFFER_TOO_SMALL;\r
146 }\r
147 \r
148 if (Ptr2 >= OctetString) {\r
149 ZeroMem (OctetString, Ptr2 - OctetString + 1);\r
150 }\r
151 \r
152 return EFI_SUCCESS;\r
153}\r
154\r
155\r
156\r
157/**\r
158 Convert a String to Guid Value.\r
159\r
160 @param[in] Str Specifies the String to be converted.\r
161 @param[in] StrLen Number of Unicode Characters of String (exclusive \0)\r
162 @param[out] Guid Return the result Guid value.\r
163\r
164 @retval EFI_SUCCESS The operation is finished successfully.\r
165 @retval EFI_NOT_FOUND Invalid string.\r
166\r
167**/\r
168EFI_STATUS\r
169StringToGuid (\r
170 IN CHAR16 *Str, \r
171 IN UINTN StrLen, \r
172 OUT EFI_GUID *Guid\r
173 )\r
174{\r
175 CHAR16 *PtrBuffer;\r
176 CHAR16 *PtrPosition;\r
177 UINT16 *Buffer;\r
178 UINTN Data;\r
179 UINTN Index;\r
180 UINT16 Digits[3];\r
181\r
182 Buffer = (CHAR16 *) AllocateZeroPool (sizeof (CHAR16) * (StrLen + 1));\r
183 if (Buffer == NULL) {\r
184 return EFI_OUT_OF_RESOURCES;\r
185 }\r
186\r
c2a65e23 187 StrCpyS (Buffer, (StrLen + 1), Str);\r
ecc722ad 188\r
189 //\r
190 // Data1\r
191 //\r
192 PtrBuffer = Buffer;\r
193 PtrPosition = PtrBuffer; \r
194 while (*PtrBuffer != L'\0') {\r
195 if (*PtrBuffer == L'-') {\r
196 break;\r
197 }\r
198 PtrBuffer++;\r
199 }\r
200 if (*PtrBuffer == L'\0') {\r
201 FreePool (Buffer);\r
202 return EFI_NOT_FOUND;\r
203 }\r
204\r
205 *PtrBuffer = L'\0';\r
206 Data = StrHexToUintn (PtrPosition);\r
207 Guid->Data1 = (UINT32)Data;\r
208\r
209 //\r
210 // Data2\r
211 //\r
212 PtrBuffer++;\r
213 PtrPosition = PtrBuffer;\r
214 while (*PtrBuffer != L'\0') {\r
215 if (*PtrBuffer == L'-') {\r
216 break;\r
217 }\r
218 PtrBuffer++;\r
219 }\r
220 if (*PtrBuffer == L'\0') {\r
221 FreePool (Buffer);\r
222 return EFI_NOT_FOUND;\r
223 }\r
224 *PtrBuffer = L'\0';\r
225 Data = StrHexToUintn (PtrPosition);\r
226 Guid->Data2 = (UINT16)Data;\r
227\r
228 //\r
229 // Data3\r
230 //\r
231 PtrBuffer++;\r
232 PtrPosition = PtrBuffer;\r
233 while (*PtrBuffer != L'\0') {\r
234 if (*PtrBuffer == L'-') {\r
235 break;\r
236 }\r
237 PtrBuffer++;\r
238 }\r
239 if (*PtrBuffer == L'\0') {\r
240 FreePool (Buffer);\r
241 return EFI_NOT_FOUND;\r
242 }\r
243 *PtrBuffer = L'\0';\r
244 Data = StrHexToUintn (PtrPosition);\r
245 Guid->Data3 = (UINT16)Data;\r
246\r
247 //\r
248 // Data4[0..1]\r
249 //\r
250 for ( Index = 0 ; Index < 2 ; Index++) {\r
251 PtrBuffer++;\r
252 if ((*PtrBuffer == L'\0') || ( *(PtrBuffer + 1) == L'\0')) {\r
253 FreePool (Buffer);\r
254 return EFI_NOT_FOUND;\r
255 }\r
256 Digits[0] = *PtrBuffer;\r
257 PtrBuffer++;\r
258 Digits[1] = *PtrBuffer;\r
259 Digits[2] = L'\0';\r
260 Data = StrHexToUintn (Digits);\r
261 Guid->Data4[Index] = (UINT8)Data;\r
262 }\r
263\r
264 //\r
265 // skip the '-'\r
266 //\r
267 PtrBuffer++;\r
268 if ((*PtrBuffer != L'-' ) || ( *PtrBuffer == L'\0')) {\r
269 return EFI_NOT_FOUND;\r
270 }\r
271\r
272 //\r
273 // Data4[2..7]\r
274 //\r
275 for ( ; Index < 8; Index++) {\r
276 PtrBuffer++;\r
277 if ((*PtrBuffer == L'\0') || ( *(PtrBuffer + 1) == L'\0')) {\r
278 FreePool (Buffer);\r
279 return EFI_NOT_FOUND;\r
280 }\r
281 Digits[0] = *PtrBuffer;\r
282 PtrBuffer++;\r
283 Digits[1] = *PtrBuffer;\r
284 Digits[2] = L'\0';\r
285 Data = StrHexToUintn (Digits);\r
286 Guid->Data4[Index] = (UINT8)Data;\r
287 }\r
288\r
289 FreePool (Buffer);\r
290 \r
291 return EFI_SUCCESS;\r
292}\r
293\r
294/**\r
295 Worker function that prints an EFI_GUID into specified Buffer.\r
296\r
297 @param[in] Guid Pointer to GUID to print.\r
298 @param[in] Buffer Buffer to print Guid into.\r
299 @param[in] BufferSize Size of Buffer.\r
300 \r
301 @retval Number of characters printed.\r
302\r
303**/\r
304UINTN\r
305GuidToString (\r
306 IN EFI_GUID *Guid,\r
307 IN CHAR16 *Buffer,\r
308 IN UINTN BufferSize\r
309 )\r
310{\r
311 UINTN Size;\r
312\r
313 Size = UnicodeSPrint (\r
314 Buffer,\r
315 BufferSize, \r
316 L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
317 (UINTN)Guid->Data1, \r
318 (UINTN)Guid->Data2,\r
319 (UINTN)Guid->Data3,\r
320 (UINTN)Guid->Data4[0],\r
321 (UINTN)Guid->Data4[1],\r
322 (UINTN)Guid->Data4[2],\r
323 (UINTN)Guid->Data4[3],\r
324 (UINTN)Guid->Data4[4],\r
325 (UINTN)Guid->Data4[5],\r
326 (UINTN)Guid->Data4[6],\r
327 (UINTN)Guid->Data4[7]\r
328 );\r
329\r
330 //\r
331 // SPrint will null terminate the string. The -1 skips the null\r
332 //\r
333 return Size - 1;\r
334}\r