2 The file operation functions for WiFi Connection Manager.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "WifiConnectionMgrFileUtil.h"
18 CHAR16
* mDerPemEncodedSuffix
[] = {
27 This code checks if the FileSuffix is one of the possible DER/PEM-encoded certificate suffix.
29 @param[in] FileSuffix The suffix of the input certificate file
31 @retval TRUE It's a DER/PEM-encoded certificate.
32 @retval FALSE It's NOT a DER/PEM-encoded certificate.
36 IsDerPemEncodeCertificate (
37 IN CONST CHAR16
*FileSuffix
41 for (Index
= 0; mDerPemEncodedSuffix
[Index
] != NULL
; Index
++) {
42 if (StrCmp (FileSuffix
, mDerPemEncodedSuffix
[Index
]) == 0) {
50 Read file content into BufferPtr, the size of the allocate buffer
51 is *FileSize plus AddtionAllocateSize.
53 @param[in] FileHandle The file to be read.
54 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
55 @param[out] FileSize Size of input file
56 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.
57 In case the buffer need to contain others besides the file content.
59 @retval EFI_SUCCESS The file was read into the buffer.
60 @retval EFI_INVALID_PARAMETER A parameter was invalid.
61 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
62 @retval others Unexpected error.
67 IN EFI_FILE_HANDLE FileHandle
,
68 IN OUT VOID
**BufferPtr
,
70 IN UINTN AddtionAllocateSize
74 UINT64 SourceFileSize
;
78 if ((FileHandle
== NULL
) || (FileSize
== NULL
)) {
79 return EFI_INVALID_PARAMETER
;
87 Status
= FileHandle
->SetPosition (FileHandle
, (UINT64
) -1);
88 if (EFI_ERROR (Status
)) {
92 Status
= FileHandle
->GetPosition (FileHandle
, &SourceFileSize
);
93 if (EFI_ERROR (Status
)) {
97 Status
= FileHandle
->SetPosition (FileHandle
, 0);
98 if (EFI_ERROR (Status
)) {
102 BufferSize
= (UINTN
) SourceFileSize
+ AddtionAllocateSize
;
103 Buffer
= AllocateZeroPool(BufferSize
);
104 if (Buffer
== NULL
) {
105 return EFI_OUT_OF_RESOURCES
;
108 BufferSize
= (UINTN
) SourceFileSize
;
109 *FileSize
= BufferSize
;
111 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, Buffer
);
112 if (EFI_ERROR (Status
) || BufferSize
!= *FileSize
) {
115 Status
= EFI_BAD_BUFFER_SIZE
;
126 This function converts an input device structure to a Unicode string.
128 @param[in] DevPath A pointer to the device path structure.
130 @return A new allocated Unicode string that represents the device path.
136 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
139 return ConvertDevicePathToText (
147 Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.
148 The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL
149 means not enough memory resource.
151 @param DevicePath Device path.
153 @retval NULL Not enough memory resourece for AllocateCopyPool.
154 @retval Other A new allocated string that represents the file name.
158 ExtractFileNameFromDevicePath (
159 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
168 ASSERT(DevicePath
!= NULL
);
170 String
= DevicePathToStr(DevicePath
);
171 if (String
== NULL
) {
174 MatchString
= String
;
178 while(MatchString
!= NULL
){
179 LastMatch
= MatchString
+ 1;
180 MatchString
= StrStr(LastMatch
,L
"\\");
183 Length
= StrLen(LastMatch
);
184 FileName
= AllocateCopyPool ((Length
+ 1) * sizeof(CHAR16
), LastMatch
);
185 if (FileName
!= NULL
) {
186 *(FileName
+ Length
) = 0;
195 Update the form base on the selected file.
197 @param[in] Private The pointer to the global private data structure.
198 @param[in] FilePath Point to the file path.
199 @param[in] FormId The form needs to display.
201 @retval TRUE Exit caller function.
202 @retval FALSE Not exit caller function.
207 IN WIFI_MGR_PRIVATE_DATA
*Private
,
208 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
209 IN EFI_FORM_ID FormId
217 if (FilePath
!= NULL
) {
218 FileName
= ExtractFileNameFromDevicePath(FilePath
);
220 if (FileName
== NULL
) {
222 // FileName = NULL has two cases:
223 // 1. FilePath == NULL, not select file.
224 // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.
225 // In these two case, no need to update the form, and exit the caller function.
231 // Close the previous file handle before open a new one.
233 if (Private
->FileContext
->FHandle
!= NULL
) {
234 Private
->FileContext
->FHandle
->Close (Private
->FileContext
->FHandle
);
236 Private
->FileContext
->FHandle
= NULL
;
238 Status
= EfiOpenFileByDevicePath (
240 &Private
->FileContext
->FHandle
,
244 if (EFI_ERROR (Status
)) {
245 if (FormId
== FORMID_ENROLL_CERT
) {
246 HiiSetString (Private
->RegisteredHandle
,
247 STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME
), L
"", NULL
);
248 } else if (FormId
== FORMID_ENROLL_PRIVATE_KEY
){
249 HiiSetString (Private
->RegisteredHandle
,
250 STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME
), L
"", NULL
);
254 if (Private
->FileContext
->FileName
!= NULL
) {
255 FreePool (Private
->FileContext
->FileName
);
256 Private
->FileContext
->FileName
= NULL
;
258 Private
->FileContext
->FileName
= FileName
;
260 if (FormId
== FORMID_ENROLL_CERT
) {
261 HiiSetString (Private
->RegisteredHandle
,
262 STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME
), FileName
, NULL
);
263 } else if (FormId
== FORMID_ENROLL_PRIVATE_KEY
){
264 HiiSetString (Private
->RegisteredHandle
,
265 STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME
), FileName
, NULL
);
273 Update the CA form base on the input file path info.
275 @param[in] Private The pointer to the global private data structure.
276 @param[in] FilePath Point to the file path.
278 @retval TRUE Exit caller function.
279 @retval FALSE Not exit caller function.
284 IN WIFI_MGR_PRIVATE_DATA
*Private
,
285 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
288 return UpdatePage(Private
, FilePath
, FORMID_ENROLL_CERT
);
292 Update the Private Key form base on the input file path info.
294 @param[in] Private The pointer to the global private data structure.
295 @param[in] FilePath Point to the file path.
297 @retval TRUE Exit caller function.
298 @retval FALSE Not exit caller function.
302 UpdatePrivateKeyFromFile (
303 IN WIFI_MGR_PRIVATE_DATA
*Private
,
304 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
307 return UpdatePage(Private
, FilePath
, FORMID_ENROLL_PRIVATE_KEY
);