2 The file operation functions for WiFi Connection Manager.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "WifiConnectionMgrFileUtil.h"
12 CHAR16
*mDerPemEncodedSuffix
[] = {
21 This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix.
23 @param[in] FileSuffix The suffix of the input certificate file
25 @retval TRUE It's a DER/PEM-encoded certificate.
26 @retval FALSE It's NOT a DER/PEM-encoded certificate.
30 IsDerPemEncodeCertificate (
31 IN CONST CHAR16
*FileSuffix
36 for (Index
= 0; mDerPemEncodedSuffix
[Index
] != NULL
; Index
++) {
37 if (StrCmp (FileSuffix
, mDerPemEncodedSuffix
[Index
]) == 0) {
46 Read file content into BufferPtr, the size of the allocate buffer
47 is *FileSize plus AddtionAllocateSize.
49 @param[in] FileHandle The file to be read.
50 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
51 @param[out] FileSize Size of input file
52 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.
53 In case the buffer need to contain others besides the file content.
55 @retval EFI_SUCCESS The file was read into the buffer.
56 @retval EFI_INVALID_PARAMETER A parameter was invalid.
57 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
58 @retval others Unexpected error.
63 IN EFI_FILE_HANDLE FileHandle
,
64 IN OUT VOID
**BufferPtr
,
66 IN UINTN AddtionAllocateSize
70 UINT64 SourceFileSize
;
74 if ((FileHandle
== NULL
) || (FileSize
== NULL
)) {
75 return EFI_INVALID_PARAMETER
;
83 Status
= FileHandle
->SetPosition (FileHandle
, (UINT64
)-1);
84 if (EFI_ERROR (Status
)) {
88 Status
= FileHandle
->GetPosition (FileHandle
, &SourceFileSize
);
89 if (EFI_ERROR (Status
)) {
93 Status
= FileHandle
->SetPosition (FileHandle
, 0);
94 if (EFI_ERROR (Status
)) {
98 BufferSize
= (UINTN
)SourceFileSize
+ AddtionAllocateSize
;
99 Buffer
= AllocateZeroPool (BufferSize
);
100 if (Buffer
== NULL
) {
101 return EFI_OUT_OF_RESOURCES
;
104 BufferSize
= (UINTN
)SourceFileSize
;
105 *FileSize
= BufferSize
;
107 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, Buffer
);
108 if (EFI_ERROR (Status
) || (BufferSize
!= *FileSize
)) {
111 Status
= EFI_BAD_BUFFER_SIZE
;
122 This function converts an input device structure to a Unicode string.
124 @param[in] DevPath A pointer to the device path structure.
126 @return A new allocated Unicode string that represents the device path.
132 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
135 return ConvertDevicePathToText (
143 Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.
144 The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL
145 means not enough memory resource.
147 @param DevicePath Device path.
149 @retval NULL Not enough memory resourece for AllocateCopyPool.
150 @retval Other A new allocated string that represents the file name.
154 ExtractFileNameFromDevicePath (
155 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
164 ASSERT (DevicePath
!= NULL
);
166 String
= DevicePathToStr (DevicePath
);
167 if (String
== NULL
) {
171 MatchString
= String
;
175 while (MatchString
!= NULL
) {
176 LastMatch
= MatchString
+ 1;
177 MatchString
= StrStr (LastMatch
, L
"\\");
180 Length
= StrLen (LastMatch
);
181 FileName
= AllocateCopyPool ((Length
+ 1) * sizeof (CHAR16
), LastMatch
);
182 if (FileName
!= NULL
) {
183 *(FileName
+ Length
) = 0;
192 Update the form base on the selected file.
194 @param[in] Private The pointer to the global private data structure.
195 @param[in] FilePath Point to the file path.
196 @param[in] FormId The form needs to display.
198 @retval TRUE Exit caller function.
199 @retval FALSE Not exit caller function.
204 IN WIFI_MGR_PRIVATE_DATA
*Private
,
205 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
206 IN EFI_FORM_ID FormId
214 if (FilePath
!= NULL
) {
215 FileName
= ExtractFileNameFromDevicePath (FilePath
);
218 if (FileName
== NULL
) {
220 // FileName = NULL has two cases:
221 // 1. FilePath == NULL, not select file.
222 // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.
223 // In these two case, no need to update the form, and exit the caller function.
229 // Close the previous file handle before open a new one.
231 if (Private
->FileContext
->FHandle
!= NULL
) {
232 Private
->FileContext
->FHandle
->Close (Private
->FileContext
->FHandle
);
235 Private
->FileContext
->FHandle
= NULL
;
237 Status
= EfiOpenFileByDevicePath (
239 &Private
->FileContext
->FHandle
,
243 if (EFI_ERROR (Status
)) {
244 if (FormId
== FORMID_ENROLL_CERT
) {
246 Private
->RegisteredHandle
,
247 STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME
),
251 } else if (FormId
== FORMID_ENROLL_PRIVATE_KEY
) {
253 Private
->RegisteredHandle
,
254 STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME
),
260 if (Private
->FileContext
->FileName
!= NULL
) {
261 FreePool (Private
->FileContext
->FileName
);
262 Private
->FileContext
->FileName
= NULL
;
265 Private
->FileContext
->FileName
= FileName
;
267 if (FormId
== FORMID_ENROLL_CERT
) {
269 Private
->RegisteredHandle
,
270 STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME
),
274 } else if (FormId
== FORMID_ENROLL_PRIVATE_KEY
) {
276 Private
->RegisteredHandle
,
277 STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME
),
288 Update the CA form base on the input file path info.
290 @param[in] Private The pointer to the global private data structure.
291 @param[in] FilePath Point to the file path.
293 @retval TRUE Exit caller function.
294 @retval FALSE Not exit caller function.
299 IN WIFI_MGR_PRIVATE_DATA
*Private
,
300 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
303 return UpdatePage (Private
, FilePath
, FORMID_ENROLL_CERT
);
307 Update the Private Key form base on the input file path info.
309 @param[in] Private The pointer to the global private data structure.
310 @param[in] FilePath Point to the file path.
312 @retval TRUE Exit caller function.
313 @retval FALSE Not exit caller function.
317 UpdatePrivateKeyFromFile (
318 IN WIFI_MGR_PRIVATE_DATA
*Private
,
319 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
322 return UpdatePage (Private
, FilePath
, FORMID_ENROLL_PRIVATE_KEY
);