2 File explorer related functions.
4 Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "FileExplorer.h"
11 EFI_GUID FileExplorerGuid
= EFI_FILE_EXPLORE_FORMSET_GUID
;
14 /// File system selection menu
16 MENU_OPTION mFsOptionMenu
= {
17 MENU_OPTION_SIGNATURE
,
23 FILE_EXPLORER_CALLBACK_DATA gFileExplorerPrivate
= {
24 FILE_EXPLORER_CALLBACK_DATA_SIGNATURE
,
37 HII_VENDOR_DEVICE_PATH
*gHiiVendorDevicePath
;
39 HII_VENDOR_DEVICE_PATH FeHiiVendorDevicePath
= {
45 (UINT8
)(sizeof (VENDOR_DEVICE_PATH
)),
46 (UINT8
)((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
50 // Will be replace with gEfiCallerIdGuid in code.
52 { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
57 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
59 (UINT8
)(END_DEVICE_PATH_LENGTH
),
60 (UINT8
)((END_DEVICE_PATH_LENGTH
) >> 8)
65 VOID
*mLibStartOpCodeHandle
= NULL
;
66 VOID
*mLibEndOpCodeHandle
= NULL
;
67 EFI_IFR_GUID_LABEL
*mLibStartLabel
= NULL
;
68 EFI_IFR_GUID_LABEL
*mLibEndLabel
= NULL
;
69 UINT16 mQuestionIdUpdate
;
70 CHAR16 mNewFileName
[MAX_FILE_NAME_LEN
];
71 CHAR16 mNewFolderName
[MAX_FOLDER_NAME_LEN
];
72 UINTN mNewFileQuestionId
= NEW_FILE_QUESTION_ID_BASE
;
73 UINTN mNewFolderQuestionId
= NEW_FOLDER_QUESTION_ID_BASE
;
76 Create a new file or folder in current directory.
78 @param FileName Point to the fileNmae or folder.
79 @param CreateFile CreateFile== TRUE means create a new file.
80 CreateFile== FALSE means create a new Folder.
90 This function allows a caller to extract the current configuration for one
91 or more named elements from the target driver.
94 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
95 @param Request A null-terminated Unicode string in <ConfigRequest> format.
96 @param Progress On return, points to a character in the Request string.
97 Points to the string's null terminator if request was successful.
98 Points to the most recent '&' before the first failing name/value
99 pair (or the beginning of the string if the failure is in the
100 first name/value pair) if the request was not successful.
101 @param Results A null-terminated Unicode string in <ConfigAltResp> format which
102 has all values filled in for the names in the Request string.
103 String to be allocated by the called function.
105 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
106 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
112 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
113 IN CONST EFI_STRING Request
,
114 OUT EFI_STRING
*Progress
,
115 OUT EFI_STRING
*Results
118 if ((Progress
== NULL
) || (Results
== NULL
)) {
119 return EFI_INVALID_PARAMETER
;
123 return EFI_NOT_FOUND
;
127 This function processes the results of changes in configuration.
130 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
131 @param Configuration A null-terminated Unicode string in <ConfigResp> format.
132 @param Progress A pointer to a string filled in with the offset of the most
133 recent '&' before the first failing name/value pair (or the
134 beginning of the string if the failure is in the first
135 name/value pair) or the terminating NULL if all was successful.
137 @retval EFI_INVALID_PARAMETER Configuration is NULL.
138 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
144 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
145 IN CONST EFI_STRING Configuration
,
146 OUT EFI_STRING
*Progress
149 if ((Configuration
== NULL
) || (Progress
== NULL
)) {
150 return EFI_INVALID_PARAMETER
;
153 *Progress
= Configuration
;
154 return EFI_NOT_FOUND
;
158 This function processes the results of changes in configuration.
159 When user select a interactive opcode, this callback will be triggered.
160 Based on the Question(QuestionId) that triggers the callback, the corresponding
161 actions is performed. It handles:
163 1) Process the axtra action or exit file explorer when user select one file .
164 2) update of file content if a dir is selected.
166 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
167 @param Action Specifies the type of action taken by the browser.
168 @param QuestionId A unique value which is sent to the original exporting driver
169 so that it can identify the type of data to expect.
170 @param Type The type of value for the question.
171 @param Value A pointer to the data being sent to the original exporting driver.
172 @param ActionRequest On return, points to the action requested by the callback function.
174 @retval EFI_SUCCESS The callback successfully handled the action.
175 @retval other error Error occur when parse one directory.
180 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
181 IN EFI_BROWSER_ACTION Action
,
182 IN EFI_QUESTION_ID QuestionId
,
184 IN EFI_IFR_TYPE_VALUE
*Value
,
185 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
191 CHAR16
*NewFolderName
;
195 NewFolderName
= NULL
;
197 if ((Action
!= EFI_BROWSER_ACTION_CHANGING
) && (Action
!= EFI_BROWSER_ACTION_CHANGED
)) {
199 // Do nothing for other UEFI Action. Only do call back when data is changed.
201 return EFI_UNSUPPORTED
;
204 if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
205 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
206 return EFI_INVALID_PARAMETER
;
209 if (QuestionId
== KEY_VALUE_CREATE_FILE_AND_EXIT
) {
210 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
211 if (!IsZeroBuffer (mNewFileName
, sizeof (mNewFileName
))) {
212 Status
= LibCreateNewFile (mNewFileName
, TRUE
);
213 ZeroMem (mNewFileName
, sizeof (mNewFileName
));
217 if (QuestionId
== KEY_VALUE_NO_CREATE_FILE_AND_EXIT
) {
218 ZeroMem (mNewFileName
, sizeof (mNewFileName
));
219 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
222 if (QuestionId
== KEY_VALUE_CREATE_FOLDER_AND_EXIT
) {
223 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
224 if (!IsZeroBuffer (mNewFolderName
, sizeof (mNewFolderName
))) {
225 Status
= LibCreateNewFile (mNewFolderName
, FALSE
);
226 ZeroMem (mNewFolderName
, sizeof (mNewFolderName
));
230 if (QuestionId
== KEY_VALUE_NO_CREATE_FOLDER_AND_EXIT
) {
231 ZeroMem (mNewFolderName
, sizeof (mNewFolderName
));
232 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
235 if (QuestionId
== NEW_FILE_NAME_ID
) {
236 NewFileName
= HiiGetString (gFileExplorerPrivate
.FeHiiHandle
, Value
->string
, NULL
);
237 if (NewFileName
!= NULL
) {
238 StrCpyS (mNewFileName
, MAX_FILE_NAME_LEN
, NewFileName
);
239 FreePool (NewFileName
);
242 return EFI_INVALID_PARAMETER
;
246 if (QuestionId
== NEW_FOLDER_NAME_ID
) {
247 NewFolderName
= HiiGetString (gFileExplorerPrivate
.FeHiiHandle
, Value
->string
, NULL
);
248 if (NewFolderName
!= NULL
) {
249 StrCpyS (mNewFolderName
, MAX_FOLDER_NAME_LEN
, NewFolderName
);
250 FreePool (NewFolderName
);
251 NewFolderName
= NULL
;
253 return EFI_INVALID_PARAMETER
;
257 if (QuestionId
>= FILE_OPTION_OFFSET
) {
258 LibGetDevicePath (QuestionId
);
261 // Process the extra action.
263 if (gFileExplorerPrivate
.ChooseHandler
!= NULL
) {
264 NeedExit
= gFileExplorerPrivate
.ChooseHandler (gFileExplorerPrivate
.RetDevicePath
);
268 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
271 } else if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
273 return EFI_INVALID_PARAMETER
;
276 if (QuestionId
>= FILE_OPTION_OFFSET
) {
277 LibGetDevicePath (QuestionId
);
278 Status
= LibUpdateFileExplorer (QuestionId
);
279 if (EFI_ERROR (Status
)) {
289 Create a menu entry by given menu type.
291 @retval NULL If failed to create the menu.
292 @return the new menu entry.
300 MENU_ENTRY
*MenuEntry
;
303 // Create new menu entry
305 MenuEntry
= AllocateZeroPool (sizeof (MENU_ENTRY
));
306 if (MenuEntry
== NULL
) {
310 MenuEntry
->VariableContext
= AllocateZeroPool (sizeof (FILE_CONTEXT
));
311 if (MenuEntry
->VariableContext
== NULL
) {
312 FreePool (MenuEntry
);
316 MenuEntry
->Signature
= MENU_ENTRY_SIGNATURE
;
321 Get the Menu Entry from the list in Menu Entry List.
323 If MenuNumber is great or equal to the number of Menu
324 Entry in the list, then ASSERT.
326 @param MenuOption The Menu Entry List to read the menu entry.
327 @param MenuNumber The index of Menu Entry.
329 @return The Menu Entry.
334 MENU_OPTION
*MenuOption
,
338 MENU_ENTRY
*NewMenuEntry
;
342 ASSERT (MenuNumber
< MenuOption
->MenuNumber
);
344 List
= MenuOption
->Head
.ForwardLink
;
345 for (Index
= 0; Index
< MenuNumber
; Index
++) {
346 List
= List
->ForwardLink
;
349 NewMenuEntry
= CR (List
, MENU_ENTRY
, Link
, MENU_ENTRY_SIGNATURE
);
355 Free up all resource allocated for a BM_MENU_ENTRY.
357 @param MenuEntry A pointer to BM_MENU_ENTRY.
361 LibDestroyMenuEntry (
362 MENU_ENTRY
*MenuEntry
365 FILE_CONTEXT
*FileContext
;
367 FileContext
= (FILE_CONTEXT
*)MenuEntry
->VariableContext
;
369 if (!FileContext
->IsRoot
) {
370 if (FileContext
->DevicePath
!= NULL
) {
371 FreePool (FileContext
->DevicePath
);
374 if (FileContext
->FileHandle
!= NULL
) {
375 FileContext
->FileHandle
->Close (FileContext
->FileHandle
);
379 if (FileContext
->FileName
!= NULL
) {
380 FreePool (FileContext
->FileName
);
383 FreePool (FileContext
);
385 if (MenuEntry
->DisplayString
!= NULL
) {
386 FreePool (MenuEntry
->DisplayString
);
389 if (MenuEntry
->HelpString
!= NULL
) {
390 FreePool (MenuEntry
->HelpString
);
393 FreePool (MenuEntry
);
397 Free resources allocated in Allocate Rountine.
399 @param FreeMenu Menu to be freed
403 MENU_OPTION
*FreeMenu
406 MENU_ENTRY
*MenuEntry
;
408 while (!IsListEmpty (&FreeMenu
->Head
)) {
410 FreeMenu
->Head
.ForwardLink
,
415 RemoveEntryList (&MenuEntry
->Link
);
416 LibDestroyMenuEntry (MenuEntry
);
419 FreeMenu
->MenuNumber
= 0;
424 Function opens and returns a file handle to the root directory of a volume.
426 @param DeviceHandle A handle for a device
428 @return A valid file handle or NULL is returned
433 IN EFI_HANDLE DeviceHandle
437 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
438 EFI_FILE_HANDLE File
;
443 // File the file system interface to the device
445 Status
= gBS
->HandleProtocol (
447 &gEfiSimpleFileSystemProtocolGuid
,
452 // Open the root directory of the volume
454 if (!EFI_ERROR (Status
)) {
455 Status
= Volume
->OpenVolume (
464 return EFI_ERROR (Status
) ? NULL
: File
;
468 This function converts an input device structure to a Unicode string.
470 @param DevPath A pointer to the device path structure.
472 @return A new allocated Unicode string that represents the device path.
477 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
482 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevPathToText
;
484 if (DevPath
== NULL
) {
488 Status
= gBS
->LocateProtocol (
489 &gEfiDevicePathToTextProtocolGuid
,
491 (VOID
**)&DevPathToText
493 ASSERT_EFI_ERROR (Status
);
494 ToText
= DevPathToText
->ConvertDevicePathToText (
499 ASSERT (ToText
!= NULL
);
507 @param Src The source.
509 @return A new string which is duplicated copy of the source.
510 @retval NULL If there is not enough memory.
521 Size
= StrSize (Src
);
522 Dest
= AllocateZeroPool (Size
);
523 ASSERT (Dest
!= NULL
);
525 CopyMem (Dest
, Src
, Size
);
533 Function gets the file information from an open file descriptor, and stores it
534 in a buffer allocated from pool.
536 @param FHand File Handle.
537 @param InfoType Info type need to get.
539 @retval A pointer to a buffer with file information or NULL is returned
544 IN EFI_FILE_HANDLE FHand
,
545 IN EFI_GUID
*InfoType
549 EFI_FILE_INFO
*Buffer
;
555 Status
= FHand
->GetInfo (
561 if (Status
== EFI_BUFFER_TOO_SMALL
) {
562 Buffer
= AllocatePool (BufferSize
);
563 ASSERT (Buffer
!= NULL
);
566 Status
= FHand
->GetInfo (
578 Get file type base on the file name.
579 Just cut the file name, from the ".". eg ".efi"
581 @param FileName File need to be checked.
583 @retval the file type string.
593 Index
= StrLen (FileName
) - 1;
594 while ((FileName
[Index
] != L
'.') && (Index
!= 0)) {
598 return Index
== 0 ? NULL
: &FileName
[Index
];
602 Converts the unicode character of the string from uppercase to lowercase.
603 This is a internal function.
605 @param ConfigString String to be converted
615 for (TmpStr
= String
; *TmpStr
!= L
'\0'; TmpStr
++) {
616 if ((*TmpStr
>= L
'A') && (*TmpStr
<= L
'Z')) {
617 *TmpStr
= (CHAR16
)(*TmpStr
- L
'A' + L
'a');
624 Check whether current FileName point to a valid
627 @param FileName File need to be checked.
629 @retval TRUE Is Efi Image
630 @retval FALSE Not a valid Efi Image
634 LibIsSupportedFileType (
638 CHAR16
*InputFileType
;
642 if (gFileExplorerPrivate
.FileType
== NULL
) {
646 InputFileType
= LibGetTypeFromName (FileName
);
648 // If the file not has *.* style, always return TRUE.
650 if (InputFileType
== NULL
) {
654 TmpStr
= AllocateCopyPool (StrSize (InputFileType
), InputFileType
);
655 ASSERT (TmpStr
!= NULL
);
656 LibToLowerString (TmpStr
);
658 IsSupported
= (StrStr (gFileExplorerPrivate
.FileType
, TmpStr
) == NULL
? FALSE
: TRUE
);
666 Append file name to existing file name.
668 @param Str1 The existing file name
669 @param Str2 The file name to be appended
671 @return Allocate a new string to hold the appended result.
672 Caller is responsible to free the returned string.
689 Size1
= StrSize (Str1
);
690 Size2
= StrSize (Str2
);
695 if (((MAX_UINTN
- Size1
) < Size2
) || ((MAX_UINTN
- Size1
- Size2
) < sizeof (CHAR16
))) {
699 MaxLen
= (Size1
+ Size2
+ sizeof (CHAR16
))/ sizeof (CHAR16
);
700 Str
= AllocateZeroPool (Size1
+ Size2
+ sizeof (CHAR16
));
701 ASSERT (Str
!= NULL
);
703 TmpStr
= AllocateZeroPool (Size1
+ Size2
+ sizeof (CHAR16
));
704 ASSERT (TmpStr
!= NULL
);
706 StrCpyS (Str
, MaxLen
, Str1
);
707 if (!((*Str
== '\\') && (*(Str
+ 1) == 0))) {
708 StrCatS (Str
, MaxLen
, L
"\\");
711 StrCatS (Str
, MaxLen
, Str2
);
716 if ((*Ptr
== '\\') && (*(Ptr
+ 1) == '.') && (*(Ptr
+ 2) == '.') && (*(Ptr
+ 3) == L
'\\')) {
718 // Convert "\Name\..\" to "\"
719 // DO NOT convert the .. if it is at the end of the string. This will
720 // break the .. behavior in changing directories.
724 // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings
727 StrCpyS (TmpStr
, MaxLen
, Ptr
+ 3);
728 StrCpyS (LastSlash
, MaxLen
- ((UINTN
)LastSlash
- (UINTN
)Str
) / sizeof (CHAR16
), TmpStr
);
730 } else if ((*Ptr
== '\\') && (*(Ptr
+ 1) == '.') && (*(Ptr
+ 2) == '\\')) {
732 // Convert a "\.\" to a "\"
736 // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings
739 StrCpyS (TmpStr
, MaxLen
, Ptr
+ 2);
740 StrCpyS (Ptr
, MaxLen
- ((UINTN
)Ptr
- (UINTN
)Str
) / sizeof (CHAR16
), TmpStr
);
742 } else if (*Ptr
== '\\') {
755 This function build the FsOptionMenu list which records all
756 available file system in the system. They includes all instances
757 of EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, all instances of EFI_LOAD_FILE_SYSTEM.
760 @retval EFI_SUCCESS Success find the file system
761 @retval EFI_OUT_OF_RESOURCES Can not create menu entry
769 UINTN NoSimpleFsHandles
;
770 EFI_HANDLE
*SimpleFsHandle
;
774 MENU_ENTRY
*MenuEntry
;
775 FILE_CONTEXT
*FileContext
;
777 EFI_FILE_SYSTEM_VOLUME_LABEL
*Info
;
779 NoSimpleFsHandles
= 0;
783 // Locate Handles that support Simple File System protocol
785 Status
= gBS
->LocateHandleBuffer (
787 &gEfiSimpleFileSystemProtocolGuid
,
792 if (!EFI_ERROR (Status
)) {
794 // Find all the instances of the File System prototocol
796 for (Index
= 0; Index
< NoSimpleFsHandles
; Index
++) {
798 // Allocate pool for this load option
800 MenuEntry
= LibCreateMenuEntry ();
801 if (NULL
== MenuEntry
) {
802 FreePool (SimpleFsHandle
);
803 return EFI_OUT_OF_RESOURCES
;
806 FileContext
= (FILE_CONTEXT
*)MenuEntry
->VariableContext
;
807 FileContext
->DeviceHandle
= SimpleFsHandle
[Index
];
808 FileContext
->FileHandle
= LibOpenRoot (FileContext
->DeviceHandle
);
809 if (FileContext
->FileHandle
== NULL
) {
810 LibDestroyMenuEntry (MenuEntry
);
814 MenuEntry
->HelpString
= LibDevicePathToStr (DevicePathFromHandle (FileContext
->DeviceHandle
));
815 FileContext
->FileName
= LibStrDuplicate (L
"\\");
816 FileContext
->DevicePath
= FileDevicePath (FileContext
->DeviceHandle
, FileContext
->FileName
);
817 FileContext
->IsDir
= TRUE
;
818 FileContext
->IsRoot
= TRUE
;
821 // Get current file system's Volume Label
823 Info
= (EFI_FILE_SYSTEM_VOLUME_LABEL
*)LibFileInfo (FileContext
->FileHandle
, &gEfiFileSystemVolumeLabelInfoIdGuid
);
825 VolumeLabel
= L
"NO FILE SYSTEM INFO";
827 VolumeLabel
= Info
->VolumeLabel
;
828 if (*VolumeLabel
== 0x0000) {
829 VolumeLabel
= L
"NO VOLUME LABEL";
833 MenuEntry
->DisplayString
= AllocateZeroPool (MAX_CHAR
);
834 ASSERT (MenuEntry
->DisplayString
!= NULL
);
836 MenuEntry
->DisplayString
,
840 MenuEntry
->HelpString
842 MenuEntry
->DisplayStringToken
= HiiSetString (
843 gFileExplorerPrivate
.FeHiiHandle
,
845 MenuEntry
->DisplayString
,
854 InsertTailList (&gFileExplorerPrivate
.FsOptionMenu
->Head
, &MenuEntry
->Link
);
858 if (NoSimpleFsHandles
!= 0) {
859 FreePool (SimpleFsHandle
);
862 gFileExplorerPrivate
.FsOptionMenu
->MenuNumber
= OptionNumber
;
868 Find the file handle from the input menu info.
870 @param MenuEntry Input Menu info.
871 @param RetFileHandle Return the file handle for the input device path.
873 @retval EFI_SUCESS Find the file handle success.
874 @retval Other Find the file handle failure.
877 LibGetFileHandleFromMenu (
878 IN MENU_ENTRY
*MenuEntry
,
879 OUT EFI_FILE_HANDLE
*RetFileHandle
883 EFI_FILE_HANDLE NewDir
;
884 FILE_CONTEXT
*FileContext
;
887 FileContext
= (FILE_CONTEXT
*)MenuEntry
->VariableContext
;
888 Dir
= FileContext
->FileHandle
;
891 // Open current directory to get files from it
896 FileContext
->FileName
,
900 if (EFI_ERROR (Status
)) {
904 if (!FileContext
->IsRoot
) {
908 *RetFileHandle
= NewDir
;
914 Find the file handle from the input device path info.
916 @param RootDirectory Device path info.
917 @param RetFileHandle Return the file handle for the input device path.
918 @param ParentFileName Parent file name.
919 @param DeviceHandle Driver handle for this partition.
921 @retval EFI_SUCESS Find the file handle success.
922 @retval Other Find the file handle failure.
925 LibGetFileHandleFromDevicePath (
926 IN EFI_DEVICE_PATH_PROTOCOL
*RootDirectory
,
927 OUT EFI_FILE_HANDLE
*RetFileHandle
,
928 OUT UINT16
**ParentFileName
,
929 OUT EFI_HANDLE
*DeviceHandle
932 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
933 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
936 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
937 EFI_FILE_HANDLE FileHandle
;
938 EFI_FILE_HANDLE LastHandle
;
941 *ParentFileName
= NULL
;
944 // Attempt to access the file via a file system interface
946 DevicePathNode
= RootDirectory
;
947 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
948 if (EFI_ERROR (Status
)) {
952 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
953 if (EFI_ERROR (Status
)) {
958 // Open the Volume to get the File System handle
960 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
961 if (EFI_ERROR (Status
)) {
965 *DeviceHandle
= Handle
;
967 if (IsDevicePathEnd (DevicePathNode
)) {
968 *ParentFileName
= AllocateCopyPool (StrSize (L
"\\"), L
"\\");
969 *RetFileHandle
= FileHandle
;
974 // Duplicate the device path to avoid the access to unaligned device path node.
975 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
976 // nodes, It assures the fields in device path nodes are 2 byte aligned.
978 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
979 if (TempDevicePathNode
== NULL
) {
981 // Setting Status to an EFI_ERROR value will cause the rest of
982 // the file system support below to be skipped.
984 Status
= EFI_OUT_OF_RESOURCES
;
989 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
990 // directory information and filename can be seperate. The goal is to inch
991 // our way down each device path node and close the previous node
993 DevicePathNode
= TempDevicePathNode
;
994 while (!EFI_ERROR (Status
) && !IsDevicePathEnd (DevicePathNode
)) {
995 if ((DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
) ||
996 (DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
))
998 Status
= EFI_UNSUPPORTED
;
1002 LastHandle
= FileHandle
;
1005 Status
= LastHandle
->Open (
1008 ((FILEPATH_DEVICE_PATH
*)DevicePathNode
)->PathName
,
1012 if (*ParentFileName
== NULL
) {
1013 *ParentFileName
= AllocateCopyPool (StrSize (((FILEPATH_DEVICE_PATH
*)DevicePathNode
)->PathName
), ((FILEPATH_DEVICE_PATH
*)DevicePathNode
)->PathName
);
1015 TempPath
= LibAppendFileName (*ParentFileName
, ((FILEPATH_DEVICE_PATH
*)DevicePathNode
)->PathName
);
1016 if (TempPath
== NULL
) {
1017 LastHandle
->Close (LastHandle
);
1018 Status
= EFI_OUT_OF_RESOURCES
;
1022 FreePool (*ParentFileName
);
1023 *ParentFileName
= TempPath
;
1027 // Close the previous node
1029 LastHandle
->Close (LastHandle
);
1031 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
1034 if (EFI_ERROR (Status
)) {
1038 *RetFileHandle
= FileHandle
;
1040 Status
= EFI_SUCCESS
;
1043 if (TempDevicePathNode
!= NULL
) {
1044 FreePool (TempDevicePathNode
);
1047 if ((FileHandle
!= NULL
) && (EFI_ERROR (Status
))) {
1048 FileHandle
->Close (FileHandle
);
1055 Create a new file or folder in current directory.
1057 @param FileName Point to the fileNmae or folder name.
1058 @param CreateFile CreateFile== TRUE means create a new file.
1059 CreateFile== FALSE means create a new Folder.
1064 IN CHAR16
*FileName
,
1065 IN BOOLEAN CreateFile
1068 EFI_FILE_HANDLE FileHandle
;
1069 EFI_FILE_HANDLE NewHandle
;
1070 EFI_HANDLE DeviceHandle
;
1073 CHAR16
*FullFileName
;
1076 FullFileName
= NULL
;
1078 LibGetFileHandleFromDevicePath (gFileExplorerPrivate
.RetDevicePath
, &FileHandle
, &ParentName
, &DeviceHandle
);
1079 FullFileName
= LibAppendFileName (ParentName
, FileName
);
1080 if (FullFileName
== NULL
) {
1081 return EFI_OUT_OF_RESOURCES
;
1085 Status
= FileHandle
->Open (
1089 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
1092 if (EFI_ERROR (Status
)) {
1093 FileHandle
->Close (FileHandle
);
1097 Status
= FileHandle
->Open (
1101 EFI_FILE_MODE_READ
| EFI_FILE_MODE_WRITE
| EFI_FILE_MODE_CREATE
,
1104 if (EFI_ERROR (Status
)) {
1105 FileHandle
->Close (FileHandle
);
1110 FileHandle
->Close (FileHandle
);
1113 // Return the DevicePath of the new created file or folder.
1115 gFileExplorerPrivate
.RetDevicePath
= FileDevicePath (DeviceHandle
, FullFileName
);
1121 Find files under current directory.
1123 All files and sub-directories in current directory
1124 will be stored in DirectoryMenu for future use.
1126 @param FileHandle Parent file handle.
1127 @param FileName Parent file name.
1128 @param DeviceHandle Driver handle for this partition.
1130 @retval EFI_SUCCESS Get files from current dir successfully.
1131 @return Other value if can't get files from current dir.
1136 IN EFI_FILE_HANDLE FileHandle
,
1137 IN UINT16
*FileName
,
1138 IN EFI_HANDLE DeviceHandle
1141 EFI_FILE_INFO
*DirInfo
;
1143 UINTN DirBufferSize
;
1144 MENU_ENTRY
*NewMenuEntry
;
1145 FILE_CONTEXT
*NewFileContext
;
1152 DirBufferSize
= sizeof (EFI_FILE_INFO
) + 1024;
1153 DirInfo
= AllocateZeroPool (DirBufferSize
);
1154 if (DirInfo
== NULL
) {
1155 return EFI_OUT_OF_RESOURCES
;
1159 // Get all files in current directory
1160 // Pass 1 to get Directories
1161 // Pass 2 to get files that are EFI images
1163 Status
= EFI_SUCCESS
;
1164 for (Pass
= 1; Pass
<= 2; Pass
++) {
1165 FileHandle
->SetPosition (FileHandle
, 0);
1167 BufferSize
= DirBufferSize
;
1168 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, DirInfo
);
1169 if (EFI_ERROR (Status
) || (BufferSize
== 0)) {
1170 Status
= EFI_SUCCESS
;
1174 if ((((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) != 0) && (Pass
== 2)) ||
1175 (((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0) && (Pass
== 1))
1179 // Pass 1 is for Directories
1180 // Pass 2 is for file names
1185 if (!(((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) != 0) || LibIsSupportedFileType (DirInfo
->FileName
))) {
1187 // Slip file unless it is a directory entry or a .EFI file
1192 NewMenuEntry
= LibCreateMenuEntry ();
1193 if (NULL
== NewMenuEntry
) {
1194 Status
= EFI_OUT_OF_RESOURCES
;
1198 NewFileContext
= (FILE_CONTEXT
*)NewMenuEntry
->VariableContext
;
1199 NewFileContext
->DeviceHandle
= DeviceHandle
;
1200 NewFileContext
->FileName
= LibAppendFileName (FileName
, DirInfo
->FileName
);
1201 if (NewFileContext
->FileName
== NULL
) {
1202 LibDestroyMenuEntry (NewMenuEntry
);
1203 Status
= EFI_OUT_OF_RESOURCES
;
1207 NewFileContext
->FileHandle
= FileHandle
;
1208 NewFileContext
->DevicePath
= FileDevicePath (NewFileContext
->DeviceHandle
, NewFileContext
->FileName
);
1209 NewMenuEntry
->HelpString
= NULL
;
1210 NewFileContext
->IsDir
= (BOOLEAN
)((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
);
1212 if (NewFileContext
->IsDir
) {
1213 BufferSize
= StrLen (DirInfo
->FileName
) * 2 + 6;
1214 NewMenuEntry
->DisplayString
= AllocateZeroPool (BufferSize
);
1216 NewMenuEntry
->DisplayString
,
1222 NewMenuEntry
->DisplayString
= LibStrDuplicate (DirInfo
->FileName
);
1225 NewMenuEntry
->DisplayStringToken
= HiiSetString (
1226 gFileExplorerPrivate
.FeHiiHandle
,
1228 NewMenuEntry
->DisplayString
,
1232 NewFileContext
->IsRoot
= FALSE
;
1235 InsertTailList (&gFileExplorerPrivate
.FsOptionMenu
->Head
, &NewMenuEntry
->Link
);
1239 gFileExplorerPrivate
.FsOptionMenu
->MenuNumber
= OptionNumber
;
1249 Refresh the global UpdateData structure.
1253 LibRefreshUpdateData (
1258 // Free current updated date
1260 if (mLibStartOpCodeHandle
!= NULL
) {
1261 HiiFreeOpCodeHandle (mLibStartOpCodeHandle
);
1264 if (mLibEndOpCodeHandle
!= NULL
) {
1265 HiiFreeOpCodeHandle (mLibEndOpCodeHandle
);
1269 // Create new OpCode Handle
1271 mLibStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1272 mLibEndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1275 // Create Hii Extend Label OpCode as the start opcode
1277 mLibStartLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (
1278 mLibStartOpCodeHandle
,
1281 sizeof (EFI_IFR_GUID_LABEL
)
1283 mLibStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1285 mLibStartLabel
->Number
= FORM_FILE_EXPLORER_ID
;
1288 // Create Hii Extend Label OpCode as the start opcode
1290 mLibEndLabel
= (EFI_IFR_GUID_LABEL
*)HiiCreateGuidOpCode (
1291 mLibEndOpCodeHandle
,
1294 sizeof (EFI_IFR_GUID_LABEL
)
1296 mLibEndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1298 mLibEndLabel
->Number
= LABEL_END
;
1303 Update the File Explore page.
1307 LibUpdateFileExplorePage (
1312 MENU_ENTRY
*NewMenuEntry
;
1313 FILE_CONTEXT
*NewFileContext
;
1314 MENU_OPTION
*MenuOption
;
1315 BOOLEAN CreateNewFile
;
1317 NewMenuEntry
= NULL
;
1318 NewFileContext
= NULL
;
1319 CreateNewFile
= FALSE
;
1321 LibRefreshUpdateData ();
1322 MenuOption
= gFileExplorerPrivate
.FsOptionMenu
;
1324 mQuestionIdUpdate
+= QUESTION_ID_UPDATE_STEP
;
1326 for (Index
= 0; Index
< MenuOption
->MenuNumber
; Index
++) {
1327 NewMenuEntry
= LibGetMenuEntry (MenuOption
, Index
);
1328 NewFileContext
= (FILE_CONTEXT
*)NewMenuEntry
->VariableContext
;
1330 if (!NewFileContext
->IsRoot
&& !CreateNewFile
) {
1331 HiiCreateGotoOpCode (
1332 mLibStartOpCodeHandle
,
1333 FORM_ADD_NEW_FILE_ID
,
1334 STRING_TOKEN (STR_NEW_FILE
),
1335 STRING_TOKEN (STR_NEW_FILE_HELP
),
1336 EFI_IFR_FLAG_CALLBACK
,
1337 (UINT16
)(mNewFileQuestionId
++)
1339 HiiCreateGotoOpCode (
1340 mLibStartOpCodeHandle
,
1341 FORM_ADD_NEW_FOLDER_ID
,
1342 STRING_TOKEN (STR_NEW_FOLDER
),
1343 STRING_TOKEN (STR_NEW_FOLDER_HELP
),
1344 EFI_IFR_FLAG_CALLBACK
,
1345 (UINT16
)(mNewFolderQuestionId
++)
1347 HiiCreateTextOpCode (
1348 mLibStartOpCodeHandle
,
1349 STRING_TOKEN (STR_NULL_STRING
),
1350 STRING_TOKEN (STR_NULL_STRING
),
1353 CreateNewFile
= TRUE
;
1356 if (!NewFileContext
->IsDir
) {
1358 // Create Text opcode for directory, also create Text opcode for file in FileExplorerStateBootFromFile.
1360 HiiCreateActionOpCode (
1361 mLibStartOpCodeHandle
,
1362 (UINT16
)(FILE_OPTION_OFFSET
+ Index
+ mQuestionIdUpdate
),
1363 NewMenuEntry
->DisplayStringToken
,
1364 STRING_TOKEN (STR_NULL_STRING
),
1365 EFI_IFR_FLAG_CALLBACK
,
1370 // Create Goto opcode for file in FileExplorerStateAddBootOption or FileExplorerStateAddDriverOptionState.
1372 HiiCreateGotoOpCode (
1373 mLibStartOpCodeHandle
,
1374 FORM_FILE_EXPLORER_ID
,
1375 NewMenuEntry
->DisplayStringToken
,
1376 STRING_TOKEN (STR_NULL_STRING
),
1377 EFI_IFR_FLAG_CALLBACK
,
1378 (UINT16
)(FILE_OPTION_OFFSET
+ Index
+ mQuestionIdUpdate
)
1384 gFileExplorerPrivate
.FeHiiHandle
,
1386 FORM_FILE_EXPLORER_ID
,
1387 mLibStartOpCodeHandle
, // Label FORM_FILE_EXPLORER_ID
1388 mLibEndOpCodeHandle
// LABEL_END
1393 Update the file explower page with the refershed file system.
1395 @param KeyValue Key value to identify the type of data to expect.
1397 @retval EFI_SUCCESS Update the file explorer form success.
1398 @retval other errors Error occur when parse one directory.
1402 LibUpdateFileExplorer (
1406 UINT16 FileOptionMask
;
1407 MENU_ENTRY
*NewMenuEntry
;
1408 FILE_CONTEXT
*NewFileContext
;
1410 EFI_FILE_HANDLE FileHandle
;
1412 Status
= EFI_SUCCESS
;
1413 FileOptionMask
= (UINT16
)(FILE_OPTION_MASK
& KeyValue
) - mQuestionIdUpdate
;
1414 NewMenuEntry
= LibGetMenuEntry (gFileExplorerPrivate
.FsOptionMenu
, FileOptionMask
);
1415 NewFileContext
= (FILE_CONTEXT
*)NewMenuEntry
->VariableContext
;
1417 if (NewFileContext
->IsDir
) {
1418 RemoveEntryList (&NewMenuEntry
->Link
);
1419 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1420 Status
= LibGetFileHandleFromMenu (NewMenuEntry
, &FileHandle
);
1421 if (!EFI_ERROR (Status
)) {
1422 Status
= LibFindFiles (FileHandle
, NewFileContext
->FileName
, NewFileContext
->DeviceHandle
);
1423 if (!EFI_ERROR (Status
)) {
1424 LibUpdateFileExplorePage ();
1426 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1430 LibDestroyMenuEntry (NewMenuEntry
);
1437 Get the device path info saved in the menu structure.
1439 @param KeyValue Key value to identify the type of data to expect.
1447 UINT16 FileOptionMask
;
1448 MENU_ENTRY
*NewMenuEntry
;
1449 FILE_CONTEXT
*NewFileContext
;
1451 FileOptionMask
= (UINT16
)(FILE_OPTION_MASK
& KeyValue
) - mQuestionIdUpdate
;
1453 NewMenuEntry
= LibGetMenuEntry (gFileExplorerPrivate
.FsOptionMenu
, FileOptionMask
);
1455 NewFileContext
= (FILE_CONTEXT
*)NewMenuEntry
->VariableContext
;
1457 if (gFileExplorerPrivate
.RetDevicePath
!= NULL
) {
1458 FreePool (gFileExplorerPrivate
.RetDevicePath
);
1461 gFileExplorerPrivate
.RetDevicePath
= DuplicateDevicePath (NewFileContext
->DevicePath
);
1465 Choose a file in the specified directory.
1467 If user input NULL for the RootDirectory, will choose file in the system.
1469 If user input *File != NULL, function will return the allocate device path
1470 info for the choosed file, caller has to free the memory after use it.
1472 @param RootDirectory Pointer to the root directory.
1473 @param FileType The file type need to choose.
1474 @param ChooseHandler Function pointer to the extra task need to do
1475 after choose one file.
1476 @param File Return the device path for the last time chosed file.
1478 @retval EFI_SUCESS Choose file success.
1479 @retval EFI_INVALID_PARAMETER Both ChooseHandler and return device path are NULL
1480 One of them must not NULL.
1481 @retval Other errors Choose file failed.
1486 IN EFI_DEVICE_PATH_PROTOCOL
*RootDirectory
,
1487 IN CHAR16
*FileType OPTIONAL
,
1488 IN CHOOSE_HANDLER ChooseHandler OPTIONAL
,
1489 OUT EFI_DEVICE_PATH_PROTOCOL
**File OPTIONAL
1492 EFI_FILE_HANDLE FileHandle
;
1495 EFI_HANDLE DeviceHandle
;
1497 if ((ChooseHandler
== NULL
) && (File
== NULL
)) {
1498 return EFI_INVALID_PARAMETER
;
1501 mQuestionIdUpdate
= 0;
1504 gFileExplorerPrivate
.RetDevicePath
= NULL
;
1505 gFileExplorerPrivate
.ChooseHandler
= ChooseHandler
;
1506 if (FileType
!= NULL
) {
1507 gFileExplorerPrivate
.FileType
= AllocateCopyPool (StrSize (FileType
), FileType
);
1508 ASSERT (gFileExplorerPrivate
.FileType
!= NULL
);
1509 LibToLowerString (gFileExplorerPrivate
.FileType
);
1511 gFileExplorerPrivate
.FileType
= NULL
;
1514 if (RootDirectory
== NULL
) {
1515 Status
= LibFindFileSystem ();
1517 Status
= LibGetFileHandleFromDevicePath (RootDirectory
, &FileHandle
, &FileName
, &DeviceHandle
);
1518 if (EFI_ERROR (Status
)) {
1522 Status
= LibFindFiles (FileHandle
, FileName
, DeviceHandle
);
1525 if (EFI_ERROR (Status
)) {
1529 LibUpdateFileExplorePage ();
1531 gFileExplorerPrivate
.FormBrowser2
->SendForm (
1532 gFileExplorerPrivate
.FormBrowser2
,
1533 &gFileExplorerPrivate
.FeHiiHandle
,
1542 if ((Status
== EFI_SUCCESS
) && (File
!= NULL
)) {
1543 *File
= gFileExplorerPrivate
.RetDevicePath
;
1544 } else if (gFileExplorerPrivate
.RetDevicePath
!= NULL
) {
1545 FreePool (gFileExplorerPrivate
.RetDevicePath
);
1548 if (gFileExplorerPrivate
.FileType
!= NULL
) {
1549 FreePool (gFileExplorerPrivate
.FileType
);
1552 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1554 if (FileName
!= NULL
) {
1555 FreePool (FileName
);
1563 Install Boot Manager Menu driver.
1565 @param ImageHandle The image handle.
1566 @param SystemTable The system table.
1568 @retval EFI_SUCEESS Install File explorer library success.
1573 FileExplorerLibConstructor (
1574 IN EFI_HANDLE ImageHandle
,
1575 IN EFI_SYSTEM_TABLE
*SystemTable
1580 gHiiVendorDevicePath
= (HII_VENDOR_DEVICE_PATH
*)DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL
*)&FeHiiVendorDevicePath
);
1581 ASSERT (gHiiVendorDevicePath
!= NULL
);
1582 CopyGuid (&gHiiVendorDevicePath
->VendorDevicePath
.Guid
, &gEfiCallerIdGuid
);
1585 // Install Device Path Protocol and Config Access protocol to driver handle
1587 Status
= gBS
->InstallMultipleProtocolInterfaces (
1588 &gFileExplorerPrivate
.FeDriverHandle
,
1589 &gEfiDevicePathProtocolGuid
,
1590 gHiiVendorDevicePath
,
1591 &gEfiHiiConfigAccessProtocolGuid
,
1592 &gFileExplorerPrivate
.FeConfigAccess
,
1595 if (Status
== EFI_ALREADY_STARTED
) {
1599 if (EFI_ERROR (Status
)) {
1604 // Post our File Explorer VFR binary to the HII database.
1606 gFileExplorerPrivate
.FeHiiHandle
= HiiAddPackages (
1608 gFileExplorerPrivate
.FeDriverHandle
,
1610 FileExplorerLibStrings
,
1613 ASSERT (gFileExplorerPrivate
.FeHiiHandle
!= NULL
);
1616 // Locate Formbrowser2 protocol
1618 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**)&gFileExplorerPrivate
.FormBrowser2
);
1619 ASSERT_EFI_ERROR (Status
);
1621 InitializeListHead (&gFileExplorerPrivate
.FsOptionMenu
->Head
);
1627 Unloads the application and its installed protocol.
1629 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1630 @param[in] SystemTable The system table.
1632 @retval EFI_SUCCESS The image has been unloaded.
1636 FileExplorerLibDestructor (
1637 IN EFI_HANDLE ImageHandle
,
1638 IN EFI_SYSTEM_TABLE
*SystemTable
1643 ASSERT (gHiiVendorDevicePath
!= NULL
);
1645 if (gFileExplorerPrivate
.FeDriverHandle
!= NULL
) {
1646 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1647 gFileExplorerPrivate
.FeDriverHandle
,
1648 &gEfiDevicePathProtocolGuid
,
1649 gHiiVendorDevicePath
,
1650 &gEfiHiiConfigAccessProtocolGuid
,
1651 &gFileExplorerPrivate
.FeConfigAccess
,
1654 ASSERT_EFI_ERROR (Status
);
1656 HiiRemovePackages (gFileExplorerPrivate
.FeHiiHandle
);
1657 gFileExplorerPrivate
.FeDriverHandle
= NULL
;
1660 FreePool (gHiiVendorDevicePath
);