]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - NetworkPkg/WifiConnectionManagerDxe/WifiConnectionMgrFileUtil.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / NetworkPkg / WifiConnectionManagerDxe / WifiConnectionMgrFileUtil.c
... / ...
CommitLineData
1/** @file\r
2 The file operation functions for WiFi Connection Manager.\r
3\r
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
5\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10#include "WifiConnectionMgrFileUtil.h"\r
11\r
12CHAR16 *mDerPemEncodedSuffix[] = {\r
13 L".cer",\r
14 L".der",\r
15 L".crt",\r
16 L".pem",\r
17 NULL\r
18};\r
19\r
20/**\r
21 This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix.\r
22\r
23 @param[in] FileSuffix The suffix of the input certificate file\r
24\r
25 @retval TRUE It's a DER/PEM-encoded certificate.\r
26 @retval FALSE It's NOT a DER/PEM-encoded certificate.\r
27\r
28**/\r
29BOOLEAN\r
30IsDerPemEncodeCertificate (\r
31 IN CONST CHAR16 *FileSuffix\r
32 )\r
33{\r
34 UINTN Index;\r
35\r
36 for (Index = 0; mDerPemEncodedSuffix[Index] != NULL; Index++) {\r
37 if (StrCmp (FileSuffix, mDerPemEncodedSuffix[Index]) == 0) {\r
38 return TRUE;\r
39 }\r
40 }\r
41\r
42 return FALSE;\r
43}\r
44\r
45/**\r
46 Read file content into BufferPtr, the size of the allocate buffer\r
47 is *FileSize plus AddtionAllocateSize.\r
48\r
49 @param[in] FileHandle The file to be read.\r
50 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.\r
51 @param[out] FileSize Size of input file\r
52 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.\r
53 In case the buffer need to contain others besides the file content.\r
54\r
55 @retval EFI_SUCCESS The file was read into the buffer.\r
56 @retval EFI_INVALID_PARAMETER A parameter was invalid.\r
57 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
58 @retval others Unexpected error.\r
59\r
60**/\r
61EFI_STATUS\r
62ReadFileContent (\r
63 IN EFI_FILE_HANDLE FileHandle,\r
64 IN OUT VOID **BufferPtr,\r
65 OUT UINTN *FileSize,\r
66 IN UINTN AddtionAllocateSize\r
67 )\r
68{\r
69 UINTN BufferSize;\r
70 UINT64 SourceFileSize;\r
71 VOID *Buffer;\r
72 EFI_STATUS Status;\r
73\r
74 if ((FileHandle == NULL) || (FileSize == NULL)) {\r
75 return EFI_INVALID_PARAMETER;\r
76 }\r
77\r
78 Buffer = NULL;\r
79\r
80 //\r
81 // Get the file size\r
82 //\r
83 Status = FileHandle->SetPosition (FileHandle, (UINT64)-1);\r
84 if (EFI_ERROR (Status)) {\r
85 goto ON_EXIT;\r
86 }\r
87\r
88 Status = FileHandle->GetPosition (FileHandle, &SourceFileSize);\r
89 if (EFI_ERROR (Status)) {\r
90 goto ON_EXIT;\r
91 }\r
92\r
93 Status = FileHandle->SetPosition (FileHandle, 0);\r
94 if (EFI_ERROR (Status)) {\r
95 goto ON_EXIT;\r
96 }\r
97\r
98 BufferSize = (UINTN)SourceFileSize + AddtionAllocateSize;\r
99 Buffer = AllocateZeroPool (BufferSize);\r
100 if (Buffer == NULL) {\r
101 return EFI_OUT_OF_RESOURCES;\r
102 }\r
103\r
104 BufferSize = (UINTN)SourceFileSize;\r
105 *FileSize = BufferSize;\r
106\r
107 Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);\r
108 if (EFI_ERROR (Status) || (BufferSize != *FileSize)) {\r
109 FreePool (Buffer);\r
110 Buffer = NULL;\r
111 Status = EFI_BAD_BUFFER_SIZE;\r
112 goto ON_EXIT;\r
113 }\r
114\r
115ON_EXIT:\r
116\r
117 *BufferPtr = Buffer;\r
118 return Status;\r
119}\r
120\r
121/**\r
122 This function converts an input device structure to a Unicode string.\r
123\r
124 @param[in] DevPath A pointer to the device path structure.\r
125\r
126 @return A new allocated Unicode string that represents the device path.\r
127\r
128**/\r
129CHAR16 *\r
130EFIAPI\r
131DevicePathToStr (\r
132 IN EFI_DEVICE_PATH_PROTOCOL *DevPath\r
133 )\r
134{\r
135 return ConvertDevicePathToText (\r
136 DevPath,\r
137 FALSE,\r
138 TRUE\r
139 );\r
140}\r
141\r
142/**\r
143 Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.\r
144 The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL\r
145 means not enough memory resource.\r
146\r
147 @param DevicePath Device path.\r
148\r
149 @retval NULL Not enough memory resourece for AllocateCopyPool.\r
150 @retval Other A new allocated string that represents the file name.\r
151\r
152**/\r
153CHAR16 *\r
154ExtractFileNameFromDevicePath (\r
155 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath\r
156 )\r
157{\r
158 CHAR16 *String;\r
159 CHAR16 *MatchString;\r
160 CHAR16 *LastMatch;\r
161 CHAR16 *FileName;\r
162 UINTN Length;\r
163\r
164 ASSERT (DevicePath != NULL);\r
165\r
166 String = DevicePathToStr (DevicePath);\r
167 if (String == NULL) {\r
168 return NULL;\r
169 }\r
170\r
171 MatchString = String;\r
172 LastMatch = String;\r
173 FileName = NULL;\r
174\r
175 while (MatchString != NULL) {\r
176 LastMatch = MatchString + 1;\r
177 MatchString = StrStr (LastMatch, L"\\");\r
178 }\r
179\r
180 Length = StrLen (LastMatch);\r
181 FileName = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), LastMatch);\r
182 if (FileName != NULL) {\r
183 *(FileName + Length) = 0;\r
184 }\r
185\r
186 FreePool (String);\r
187\r
188 return FileName;\r
189}\r
190\r
191/**\r
192 Update the form base on the selected file.\r
193\r
194 @param[in] Private The pointer to the global private data structure.\r
195 @param[in] FilePath Point to the file path.\r
196 @param[in] FormId The form needs to display.\r
197\r
198 @retval TRUE Exit caller function.\r
199 @retval FALSE Not exit caller function.\r
200\r
201**/\r
202BOOLEAN\r
203UpdatePage (\r
204 IN WIFI_MGR_PRIVATE_DATA *Private,\r
205 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
206 IN EFI_FORM_ID FormId\r
207 )\r
208{\r
209 CHAR16 *FileName;\r
210 EFI_STATUS Status;\r
211\r
212 FileName = NULL;\r
213\r
214 if (FilePath != NULL) {\r
215 FileName = ExtractFileNameFromDevicePath (FilePath);\r
216 }\r
217\r
218 if (FileName == NULL) {\r
219 //\r
220 // FileName = NULL has two cases:\r
221 // 1. FilePath == NULL, not select file.\r
222 // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.\r
223 // In these two case, no need to update the form, and exit the caller function.\r
224 //\r
225 return TRUE;\r
226 }\r
227\r
228 //\r
229 // Close the previous file handle before open a new one.\r
230 //\r
231 if (Private->FileContext->FHandle != NULL) {\r
232 Private->FileContext->FHandle->Close (Private->FileContext->FHandle);\r
233 }\r
234\r
235 Private->FileContext->FHandle = NULL;\r
236\r
237 Status = EfiOpenFileByDevicePath (\r
238 &FilePath,\r
239 &Private->FileContext->FHandle,\r
240 EFI_FILE_MODE_READ,\r
241 0\r
242 );\r
243 if (EFI_ERROR (Status)) {\r
244 if (FormId == FORMID_ENROLL_CERT) {\r
245 HiiSetString (\r
246 Private->RegisteredHandle,\r
247 STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME),\r
248 L"",\r
249 NULL\r
250 );\r
251 } else if (FormId == FORMID_ENROLL_PRIVATE_KEY) {\r
252 HiiSetString (\r
253 Private->RegisteredHandle,\r
254 STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME),\r
255 L"",\r
256 NULL\r
257 );\r
258 }\r
259 } else {\r
260 if (Private->FileContext->FileName != NULL) {\r
261 FreePool (Private->FileContext->FileName);\r
262 Private->FileContext->FileName = NULL;\r
263 }\r
264\r
265 Private->FileContext->FileName = FileName;\r
266\r
267 if (FormId == FORMID_ENROLL_CERT) {\r
268 HiiSetString (\r
269 Private->RegisteredHandle,\r
270 STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME),\r
271 FileName,\r
272 NULL\r
273 );\r
274 } else if (FormId == FORMID_ENROLL_PRIVATE_KEY) {\r
275 HiiSetString (\r
276 Private->RegisteredHandle,\r
277 STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME),\r
278 FileName,\r
279 NULL\r
280 );\r
281 }\r
282 }\r
283\r
284 return TRUE;\r
285}\r
286\r
287/**\r
288 Update the CA form base on the input file path info.\r
289\r
290 @param[in] Private The pointer to the global private data structure.\r
291 @param[in] FilePath Point to the file path.\r
292\r
293 @retval TRUE Exit caller function.\r
294 @retval FALSE Not exit caller function.\r
295\r
296**/\r
297BOOLEAN\r
298UpdateCAFromFile (\r
299 IN WIFI_MGR_PRIVATE_DATA *Private,\r
300 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
301 )\r
302{\r
303 return UpdatePage (Private, FilePath, FORMID_ENROLL_CERT);\r
304}\r
305\r
306/**\r
307 Update the Private Key form base on the input file path info.\r
308\r
309 @param[in] Private The pointer to the global private data structure.\r
310 @param[in] FilePath Point to the file path.\r
311\r
312 @retval TRUE Exit caller function.\r
313 @retval FALSE Not exit caller function.\r
314\r
315**/\r
316BOOLEAN\r
317UpdatePrivateKeyFromFile (\r
318 IN WIFI_MGR_PRIVATE_DATA *Private,\r
319 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
320 )\r
321{\r
322 return UpdatePage (Private, FilePath, FORMID_ENROLL_PRIVATE_KEY);\r
323}\r