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
35 for (Index
= 0; mDerPemEncodedSuffix
[Index
] != NULL
; Index
++) {
36 if (StrCmp (FileSuffix
, mDerPemEncodedSuffix
[Index
]) == 0) {
44 Read file content into BufferPtr, the size of the allocate buffer
45 is *FileSize plus AddtionAllocateSize.
47 @param[in] FileHandle The file to be read.
48 @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
49 @param[out] FileSize Size of input file
50 @param[in] AddtionAllocateSize Addtion size the buffer need to be allocated.
51 In case the buffer need to contain others besides the file content.
53 @retval EFI_SUCCESS The file was read into the buffer.
54 @retval EFI_INVALID_PARAMETER A parameter was invalid.
55 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
56 @retval others Unexpected error.
61 IN EFI_FILE_HANDLE FileHandle
,
62 IN OUT VOID
**BufferPtr
,
64 IN UINTN AddtionAllocateSize
68 UINT64 SourceFileSize
;
72 if ((FileHandle
== NULL
) || (FileSize
== NULL
)) {
73 return EFI_INVALID_PARAMETER
;
81 Status
= FileHandle
->SetPosition (FileHandle
, (UINT64
) -1);
82 if (EFI_ERROR (Status
)) {
86 Status
= FileHandle
->GetPosition (FileHandle
, &SourceFileSize
);
87 if (EFI_ERROR (Status
)) {
91 Status
= FileHandle
->SetPosition (FileHandle
, 0);
92 if (EFI_ERROR (Status
)) {
96 BufferSize
= (UINTN
) SourceFileSize
+ AddtionAllocateSize
;
97 Buffer
= AllocateZeroPool(BufferSize
);
99 return EFI_OUT_OF_RESOURCES
;
102 BufferSize
= (UINTN
) SourceFileSize
;
103 *FileSize
= BufferSize
;
105 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, Buffer
);
106 if (EFI_ERROR (Status
) || BufferSize
!= *FileSize
) {
109 Status
= EFI_BAD_BUFFER_SIZE
;
120 This function converts an input device structure to a Unicode string.
122 @param[in] DevPath A pointer to the device path structure.
124 @return A new allocated Unicode string that represents the device path.
130 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
133 return ConvertDevicePathToText (
141 Extract filename from device path. The returned buffer is allocated using AllocateCopyPool.
142 The caller is responsible for freeing the allocated buffer using FreePool(). If return NULL
143 means not enough memory resource.
145 @param DevicePath Device path.
147 @retval NULL Not enough memory resourece for AllocateCopyPool.
148 @retval Other A new allocated string that represents the file name.
152 ExtractFileNameFromDevicePath (
153 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
162 ASSERT(DevicePath
!= NULL
);
164 String
= DevicePathToStr(DevicePath
);
165 if (String
== NULL
) {
168 MatchString
= String
;
172 while(MatchString
!= NULL
){
173 LastMatch
= MatchString
+ 1;
174 MatchString
= StrStr(LastMatch
,L
"\\");
177 Length
= StrLen(LastMatch
);
178 FileName
= AllocateCopyPool ((Length
+ 1) * sizeof(CHAR16
), LastMatch
);
179 if (FileName
!= NULL
) {
180 *(FileName
+ Length
) = 0;
189 Update the form base on the selected file.
191 @param[in] Private The pointer to the global private data structure.
192 @param[in] FilePath Point to the file path.
193 @param[in] FormId The form needs to display.
195 @retval TRUE Exit caller function.
196 @retval FALSE Not exit caller function.
201 IN WIFI_MGR_PRIVATE_DATA
*Private
,
202 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
203 IN EFI_FORM_ID FormId
211 if (FilePath
!= NULL
) {
212 FileName
= ExtractFileNameFromDevicePath(FilePath
);
214 if (FileName
== NULL
) {
216 // FileName = NULL has two cases:
217 // 1. FilePath == NULL, not select file.
218 // 2. FilePath != NULL, but ExtractFileNameFromDevicePath return NULL not enough memory resource.
219 // In these two case, no need to update the form, and exit the caller function.
225 // Close the previous file handle before open a new one.
227 if (Private
->FileContext
->FHandle
!= NULL
) {
228 Private
->FileContext
->FHandle
->Close (Private
->FileContext
->FHandle
);
230 Private
->FileContext
->FHandle
= NULL
;
232 Status
= EfiOpenFileByDevicePath (
234 &Private
->FileContext
->FHandle
,
238 if (EFI_ERROR (Status
)) {
239 if (FormId
== FORMID_ENROLL_CERT
) {
240 HiiSetString (Private
->RegisteredHandle
,
241 STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME
), L
"", NULL
);
242 } else if (FormId
== FORMID_ENROLL_PRIVATE_KEY
){
243 HiiSetString (Private
->RegisteredHandle
,
244 STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME
), L
"", NULL
);
248 if (Private
->FileContext
->FileName
!= NULL
) {
249 FreePool (Private
->FileContext
->FileName
);
250 Private
->FileContext
->FileName
= NULL
;
252 Private
->FileContext
->FileName
= FileName
;
254 if (FormId
== FORMID_ENROLL_CERT
) {
255 HiiSetString (Private
->RegisteredHandle
,
256 STRING_TOKEN (STR_EAP_ENROLLED_CERT_NAME
), FileName
, NULL
);
257 } else if (FormId
== FORMID_ENROLL_PRIVATE_KEY
){
258 HiiSetString (Private
->RegisteredHandle
,
259 STRING_TOKEN (STR_EAP_ENROLLED_PRIVATE_KEY_NAME
), FileName
, NULL
);
267 Update the CA form base on the input file path info.
269 @param[in] Private The pointer to the global private data structure.
270 @param[in] FilePath Point to the file path.
272 @retval TRUE Exit caller function.
273 @retval FALSE Not exit caller function.
278 IN WIFI_MGR_PRIVATE_DATA
*Private
,
279 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
282 return UpdatePage(Private
, FilePath
, FORMID_ENROLL_CERT
);
286 Update the Private Key form base on the input file path info.
288 @param[in] Private The pointer to the global private data structure.
289 @param[in] FilePath Point to the file path.
291 @retval TRUE Exit caller function.
292 @retval FALSE Not exit caller function.
296 UpdatePrivateKeyFromFile (
297 IN WIFI_MGR_PRIVATE_DATA
*Private
,
298 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
301 return UpdatePage(Private
, FilePath
, FORMID_ENROLL_PRIVATE_KEY
);