2 File explorer related functions.
4 Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "FileExplorer.h"
18 EFI_GUID FileExplorerGuid
= EFI_FILE_EXPLORE_FORMSET_GUID
;
21 /// File system selection menu
23 MENU_OPTION mFsOptionMenu
= {
24 MENU_OPTION_SIGNATURE
,
30 FILE_EXPLORER_CALLBACK_DATA gFileExplorerPrivate
= {
31 FILE_EXPLORER_CALLBACK_DATA_SIGNATURE
,
44 HII_VENDOR_DEVICE_PATH
*gHiiVendorDevicePath
;
46 HII_VENDOR_DEVICE_PATH FeHiiVendorDevicePath
= {
52 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
53 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
57 // Will be replace with gEfiCallerIdGuid in code.
59 { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } }
63 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
65 (UINT8
) (END_DEVICE_PATH_LENGTH
),
66 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
71 VOID
*mLibStartOpCodeHandle
= NULL
;
72 VOID
*mLibEndOpCodeHandle
= NULL
;
73 EFI_IFR_GUID_LABEL
*mLibStartLabel
= NULL
;
74 EFI_IFR_GUID_LABEL
*mLibEndLabel
= NULL
;
77 This function allows a caller to extract the current configuration for one
78 or more named elements from the target driver.
81 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
82 @param Request A null-terminated Unicode string in <ConfigRequest> format.
83 @param Progress On return, points to a character in the Request string.
84 Points to the string's null terminator if request was successful.
85 Points to the most recent '&' before the first failing name/value
86 pair (or the beginning of the string if the failure is in the
87 first name/value pair) if the request was not successful.
88 @param Results A null-terminated Unicode string in <ConfigAltResp> format which
89 has all values filled in for the names in the Request string.
90 String to be allocated by the called function.
92 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
93 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
99 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
100 IN CONST EFI_STRING Request
,
101 OUT EFI_STRING
*Progress
,
102 OUT EFI_STRING
*Results
105 if (Progress
== NULL
|| Results
== NULL
) {
106 return EFI_INVALID_PARAMETER
;
110 return EFI_NOT_FOUND
;
114 This function processes the results of changes in configuration.
117 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
118 @param Configuration A null-terminated Unicode string in <ConfigResp> format.
119 @param Progress A pointer to a string filled in with the offset of the most
120 recent '&' before the first failing name/value pair (or the
121 beginning of the string if the failure is in the first
122 name/value pair) or the terminating NULL if all was successful.
124 @retval EFI_INVALID_PARAMETER Configuration is NULL.
125 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
131 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
132 IN CONST EFI_STRING Configuration
,
133 OUT EFI_STRING
*Progress
136 if (Configuration
== NULL
|| Progress
== NULL
) {
137 return EFI_INVALID_PARAMETER
;
140 *Progress
= Configuration
;
141 return EFI_NOT_FOUND
;
145 This function processes the results of changes in configuration.
146 When user select a interactive opcode, this callback will be triggered.
147 Based on the Question(QuestionId) that triggers the callback, the corresponding
148 actions is performed. It handles:
150 1) Process the axtra action or exit file explorer when user select one file .
151 2) update of file content if a dir is selected.
153 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
154 @param Action Specifies the type of action taken by the browser.
155 @param QuestionId A unique value which is sent to the original exporting driver
156 so that it can identify the type of data to expect.
157 @param Type The type of value for the question.
158 @param Value A pointer to the data being sent to the original exporting driver.
159 @param ActionRequest On return, points to the action requested by the callback function.
161 @retval EFI_SUCCESS The callback successfully handled the action.
162 @retval other error Error occur when parse one directory.
167 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
168 IN EFI_BROWSER_ACTION Action
,
169 IN EFI_QUESTION_ID QuestionId
,
171 IN EFI_IFR_TYPE_VALUE
*Value
,
172 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
180 if (Action
!= EFI_BROWSER_ACTION_CHANGING
&& Action
!= EFI_BROWSER_ACTION_CHANGED
) {
182 // Do nothing for other UEFI Action. Only do call back when data is changed.
184 return EFI_UNSUPPORTED
;
187 if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
188 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
189 return EFI_INVALID_PARAMETER
;
192 if (QuestionId
>= FILE_OPTION_OFFSET
) {
193 LibGetDevicePath(QuestionId
);
196 // Process the extra action.
198 if (gFileExplorerPrivate
.ChooseHandler
!= NULL
) {
199 NeedExit
= gFileExplorerPrivate
.ChooseHandler (gFileExplorerPrivate
.RetDevicePath
);
203 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
206 } else if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
208 return EFI_INVALID_PARAMETER
;
211 if (QuestionId
>= FILE_OPTION_OFFSET
) {
212 Status
= LibUpdateFileExplorer (QuestionId
);
213 if (EFI_ERROR (Status
)) {
223 Create a menu entry by given menu type.
225 @retval NULL If failed to create the menu.
226 @return the new menu entry.
234 MENU_ENTRY
*MenuEntry
;
237 // Create new menu entry
239 MenuEntry
= AllocateZeroPool (sizeof (MENU_ENTRY
));
240 if (MenuEntry
== NULL
) {
244 MenuEntry
->VariableContext
= AllocateZeroPool (sizeof (FILE_CONTEXT
));
245 if (MenuEntry
->VariableContext
== NULL
) {
246 FreePool (MenuEntry
);
250 MenuEntry
->Signature
= MENU_ENTRY_SIGNATURE
;
256 Get the Menu Entry from the list in Menu Entry List.
258 If MenuNumber is great or equal to the number of Menu
259 Entry in the list, then ASSERT.
261 @param MenuOption The Menu Entry List to read the menu entry.
262 @param MenuNumber The index of Menu Entry.
264 @return The Menu Entry.
269 MENU_OPTION
*MenuOption
,
273 MENU_ENTRY
*NewMenuEntry
;
277 ASSERT (MenuNumber
< MenuOption
->MenuNumber
);
279 List
= MenuOption
->Head
.ForwardLink
;
280 for (Index
= 0; Index
< MenuNumber
; Index
++) {
281 List
= List
->ForwardLink
;
284 NewMenuEntry
= CR (List
, MENU_ENTRY
, Link
, MENU_ENTRY_SIGNATURE
);
290 Free up all resource allocated for a BM_MENU_ENTRY.
292 @param MenuEntry A pointer to BM_MENU_ENTRY.
296 LibDestroyMenuEntry (
297 MENU_ENTRY
*MenuEntry
300 FILE_CONTEXT
*FileContext
;
302 FileContext
= (FILE_CONTEXT
*) MenuEntry
->VariableContext
;
304 if (!FileContext
->IsRoot
) {
305 if (FileContext
->DevicePath
!= NULL
) {
306 FreePool (FileContext
->DevicePath
);
309 if (FileContext
->FileHandle
!= NULL
) {
310 FileContext
->FileHandle
->Close (FileContext
->FileHandle
);
314 if (FileContext
->FileName
!= NULL
) {
315 FreePool (FileContext
->FileName
);
318 FreePool (FileContext
);
320 if (MenuEntry
->DisplayString
!= NULL
) {
321 FreePool (MenuEntry
->DisplayString
);
323 if (MenuEntry
->HelpString
!= NULL
) {
324 FreePool (MenuEntry
->HelpString
);
327 FreePool (MenuEntry
);
332 Free resources allocated in Allocate Rountine.
334 @param FreeMenu Menu to be freed
338 MENU_OPTION
*FreeMenu
341 MENU_ENTRY
*MenuEntry
;
342 while (!IsListEmpty (&FreeMenu
->Head
)) {
344 FreeMenu
->Head
.ForwardLink
,
349 RemoveEntryList (&MenuEntry
->Link
);
350 LibDestroyMenuEntry (MenuEntry
);
352 FreeMenu
->MenuNumber
= 0;
357 Function opens and returns a file handle to the root directory of a volume.
359 @param DeviceHandle A handle for a device
361 @return A valid file handle or NULL is returned
366 IN EFI_HANDLE DeviceHandle
370 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
371 EFI_FILE_HANDLE File
;
376 // File the file system interface to the device
378 Status
= gBS
->HandleProtocol (
380 &gEfiSimpleFileSystemProtocolGuid
,
385 // Open the root directory of the volume
387 if (!EFI_ERROR (Status
)) {
388 Status
= Volume
->OpenVolume (
396 return EFI_ERROR (Status
) ? NULL
: File
;
400 This function converts an input device structure to a Unicode string.
402 @param DevPath A pointer to the device path structure.
404 @return A new allocated Unicode string that represents the device path.
409 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
414 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevPathToText
;
416 if (DevPath
== NULL
) {
420 Status
= gBS
->LocateProtocol (
421 &gEfiDevicePathToTextProtocolGuid
,
423 (VOID
**) &DevPathToText
425 ASSERT_EFI_ERROR (Status
);
426 ToText
= DevPathToText
->ConvertDevicePathToText (
431 ASSERT (ToText
!= NULL
);
439 @param Src The source.
441 @return A new string which is duplicated copy of the source.
442 @retval NULL If there is not enough memory.
453 Size
= StrSize (Src
);
454 Dest
= AllocateZeroPool (Size
);
455 ASSERT (Dest
!= NULL
);
457 CopyMem (Dest
, Src
, Size
);
465 Function gets the file information from an open file descriptor, and stores it
466 in a buffer allocated from pool.
468 @param FHand File Handle.
469 @param InfoType Info type need to get.
471 @retval A pointer to a buffer with file information or NULL is returned
476 IN EFI_FILE_HANDLE FHand
,
477 IN EFI_GUID
*InfoType
481 EFI_FILE_INFO
*Buffer
;
487 Status
= FHand
->GetInfo (
493 if (Status
== EFI_BUFFER_TOO_SMALL
) {
494 Buffer
= AllocatePool (BufferSize
);
495 ASSERT (Buffer
!= NULL
);
498 Status
= FHand
->GetInfo (
510 Get file type base on the file name.
511 Just cut the file name, from the ".". eg ".efi"
513 @param FileName File need to be checked.
515 @retval the file type string.
525 Index
= StrLen (FileName
) - 1;
526 while ((FileName
[Index
] != L
'.') && (Index
!= 0)) {
530 return Index
== 0 ? NULL
: &FileName
[Index
];
534 Converts the unicode character of the string from uppercase to lowercase.
535 This is a internal function.
537 @param ConfigString String to be converted
547 for (TmpStr
= String
; *TmpStr
!= L
'\0'; TmpStr
++) {
548 if (*TmpStr
>= L
'A' && *TmpStr
<= L
'Z') {
549 *TmpStr
= (CHAR16
) (*TmpStr
- L
'A' + L
'a');
556 Check whether current FileName point to a valid
559 @param FileName File need to be checked.
561 @retval TRUE Is Efi Image
562 @retval FALSE Not a valid Efi Image
566 LibIsSupportedFileType (
570 CHAR16
*InputFileType
;
574 if (gFileExplorerPrivate
.FileType
== NULL
) {
578 InputFileType
= LibGetTypeFromName (FileName
);
580 // If the file not has *.* style, always return TRUE.
582 if (InputFileType
== NULL
) {
586 TmpStr
= AllocateCopyPool (StrSize (InputFileType
), InputFileType
);
587 ASSERT(TmpStr
!= NULL
);
588 LibToLowerString(TmpStr
);
590 IsSupported
= (StrStr (gFileExplorerPrivate
.FileType
, TmpStr
) == NULL
? FALSE
: TRUE
);
598 Append file name to existing file name.
600 @param Str1 The existing file name
601 @param Str2 The file name to be appended
603 @return Allocate a new string to hold the appended result.
604 Caller is responsible to free the returned string.
621 Size1
= StrSize (Str1
);
622 Size2
= StrSize (Str2
);
627 if (((MAX_UINTN
- Size1
) < Size2
) || ((MAX_UINTN
- Size1
- Size2
) < sizeof(CHAR16
))) {
631 MaxLen
= (Size1
+ Size2
+ sizeof (CHAR16
))/ sizeof (CHAR16
);
632 Str
= AllocateZeroPool (Size1
+ Size2
+ sizeof (CHAR16
));
633 ASSERT (Str
!= NULL
);
635 TmpStr
= AllocateZeroPool (Size1
+ Size2
+ sizeof (CHAR16
));
636 ASSERT (TmpStr
!= NULL
);
638 StrCpyS (Str
, MaxLen
, Str1
);
639 if (!((*Str
== '\\') && (*(Str
+ 1) == 0))) {
640 StrCatS (Str
, MaxLen
, L
"\\");
643 StrCatS (Str
, MaxLen
, Str2
);
648 if (*Ptr
== '\\' && *(Ptr
+ 1) == '.' && *(Ptr
+ 2) == '.' && *(Ptr
+ 3) == L
'\\') {
650 // Convert "\Name\..\" to "\"
651 // DO NOT convert the .. if it is at the end of the string. This will
652 // break the .. behavior in changing directories.
656 // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings
659 StrCpyS (TmpStr
, MaxLen
, Ptr
+ 3);
660 StrCpyS (LastSlash
, MaxLen
- (UINTN
) (LastSlash
- Str
), TmpStr
);
662 } else if (*Ptr
== '\\' && *(Ptr
+ 1) == '.' && *(Ptr
+ 2) == '\\') {
664 // Convert a "\.\" to a "\"
668 // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings
671 StrCpyS (TmpStr
, MaxLen
, Ptr
+ 2);
672 StrCpyS (Ptr
, MaxLen
- (UINTN
) (Ptr
- Str
), TmpStr
);
674 } else if (*Ptr
== '\\') {
687 This function build the FsOptionMenu list which records all
688 available file system in the system. They includes all instances
689 of EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, all instances of EFI_LOAD_FILE_SYSTEM.
692 @retval EFI_SUCCESS Success find the file system
693 @retval EFI_OUT_OF_RESOURCES Can not create menu entry
701 UINTN NoSimpleFsHandles
;
702 EFI_HANDLE
*SimpleFsHandle
;
706 MENU_ENTRY
*MenuEntry
;
707 FILE_CONTEXT
*FileContext
;
709 EFI_FILE_SYSTEM_VOLUME_LABEL
*Info
;
711 NoSimpleFsHandles
= 0;
715 // Locate Handles that support Simple File System protocol
717 Status
= gBS
->LocateHandleBuffer (
719 &gEfiSimpleFileSystemProtocolGuid
,
724 if (!EFI_ERROR (Status
)) {
726 // Find all the instances of the File System prototocol
728 for (Index
= 0; Index
< NoSimpleFsHandles
; Index
++) {
730 // Allocate pool for this load option
732 MenuEntry
= LibCreateMenuEntry ();
733 if (NULL
== MenuEntry
) {
734 FreePool (SimpleFsHandle
);
735 return EFI_OUT_OF_RESOURCES
;
738 FileContext
= (FILE_CONTEXT
*) MenuEntry
->VariableContext
;
739 FileContext
->DeviceHandle
= SimpleFsHandle
[Index
];
740 FileContext
->FileHandle
= LibOpenRoot (FileContext
->DeviceHandle
);
741 if (FileContext
->FileHandle
== NULL
) {
742 LibDestroyMenuEntry (MenuEntry
);
746 MenuEntry
->HelpString
= LibDevicePathToStr (DevicePathFromHandle (FileContext
->DeviceHandle
));
747 FileContext
->FileName
= LibStrDuplicate (L
"\\");
748 FileContext
->DevicePath
= FileDevicePath (FileContext
->DeviceHandle
, FileContext
->FileName
);
749 FileContext
->IsDir
= TRUE
;
750 FileContext
->IsRoot
= TRUE
;
753 // Get current file system's Volume Label
755 Info
= (EFI_FILE_SYSTEM_VOLUME_LABEL
*) LibFileInfo (FileContext
->FileHandle
, &gEfiFileSystemVolumeLabelInfoIdGuid
);
757 VolumeLabel
= L
"NO FILE SYSTEM INFO";
759 if (Info
->VolumeLabel
== NULL
) {
760 VolumeLabel
= L
"NULL VOLUME LABEL";
762 VolumeLabel
= Info
->VolumeLabel
;
763 if (*VolumeLabel
== 0x0000) {
764 VolumeLabel
= L
"NO VOLUME LABEL";
768 MenuEntry
->DisplayString
= AllocateZeroPool (MAX_CHAR
);
769 ASSERT (MenuEntry
->DisplayString
!= NULL
);
771 MenuEntry
->DisplayString
,
775 MenuEntry
->HelpString
777 MenuEntry
->DisplayStringToken
= HiiSetString (
778 gFileExplorerPrivate
.FeHiiHandle
,
780 MenuEntry
->DisplayString
,
788 InsertTailList (&gFileExplorerPrivate
.FsOptionMenu
->Head
, &MenuEntry
->Link
);
792 if (NoSimpleFsHandles
!= 0) {
793 FreePool (SimpleFsHandle
);
796 gFileExplorerPrivate
.FsOptionMenu
->MenuNumber
= OptionNumber
;
802 Find the file handle from the input menu info.
804 @param MenuEntry Input Menu info.
805 @param RetFileHandle Return the file handle for the input device path.
807 @retval EFI_SUCESS Find the file handle success.
808 @retval Other Find the file handle failure.
811 LibGetFileHandleFromMenu (
812 IN MENU_ENTRY
*MenuEntry
,
813 OUT EFI_FILE_HANDLE
*RetFileHandle
817 EFI_FILE_HANDLE NewDir
;
818 FILE_CONTEXT
*FileContext
;
821 FileContext
= (FILE_CONTEXT
*) MenuEntry
->VariableContext
;
822 Dir
= FileContext
->FileHandle
;
825 // Open current directory to get files from it
830 FileContext
->FileName
,
834 if (EFI_ERROR (Status
)) {
838 if (!FileContext
->IsRoot
) {
842 *RetFileHandle
= NewDir
;
848 Find the file handle from the input device path info.
850 @param RootDirectory Device path info.
851 @param RetFileHandle Return the file handle for the input device path.
852 @param ParentFileName Parent file name.
853 @param DeviceHandle Driver handle for this partition.
855 @retval EFI_SUCESS Find the file handle success.
856 @retval Other Find the file handle failure.
859 LibGetFileHandleFromDevicePath (
860 IN EFI_DEVICE_PATH_PROTOCOL
*RootDirectory
,
861 OUT EFI_FILE_HANDLE
*RetFileHandle
,
862 OUT UINT16
**ParentFileName
,
863 OUT EFI_HANDLE
*DeviceHandle
866 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
867 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
870 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
871 EFI_FILE_HANDLE FileHandle
;
872 EFI_FILE_HANDLE LastHandle
;
875 *ParentFileName
= NULL
;
878 // Attempt to access the file via a file system interface
880 DevicePathNode
= RootDirectory
;
881 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
882 if (EFI_ERROR (Status
)) {
886 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
887 if (EFI_ERROR (Status
)) {
892 // Open the Volume to get the File System handle
894 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
895 if (EFI_ERROR (Status
)) {
899 *DeviceHandle
= Handle
;
901 if (IsDevicePathEnd(DevicePathNode
)) {
902 *ParentFileName
= AllocateCopyPool (StrSize (L
"\\"), L
"\\");
903 *RetFileHandle
= FileHandle
;
908 // Duplicate the device path to avoid the access to unaligned device path node.
909 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
910 // nodes, It assures the fields in device path nodes are 2 byte aligned.
912 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
913 if (TempDevicePathNode
== NULL
) {
916 // Setting Status to an EFI_ERROR value will cause the rest of
917 // the file system support below to be skipped.
919 Status
= EFI_OUT_OF_RESOURCES
;
924 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
925 // directory information and filename can be seperate. The goal is to inch
926 // our way down each device path node and close the previous node
928 DevicePathNode
= TempDevicePathNode
;
929 while (!EFI_ERROR (Status
) && !IsDevicePathEnd (DevicePathNode
)) {
930 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
931 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
932 Status
= EFI_UNSUPPORTED
;
936 LastHandle
= FileHandle
;
939 Status
= LastHandle
->Open (
942 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
946 if (*ParentFileName
== NULL
) {
947 *ParentFileName
= AllocateCopyPool (StrSize (((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
), ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
);
949 TempPath
= LibAppendFileName (*ParentFileName
, ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
);
950 if (TempPath
== NULL
) {
951 LastHandle
->Close (LastHandle
);
952 Status
= EFI_OUT_OF_RESOURCES
;
955 FreePool (*ParentFileName
);
956 *ParentFileName
= TempPath
;
960 // Close the previous node
962 LastHandle
->Close (LastHandle
);
964 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
967 if (EFI_ERROR (Status
)) {
971 *RetFileHandle
= FileHandle
;
973 Status
= EFI_SUCCESS
;
976 if (TempDevicePathNode
!= NULL
) {
977 FreePool (TempDevicePathNode
);
980 if ((FileHandle
!= NULL
) && (EFI_ERROR (Status
))) {
981 FileHandle
->Close (FileHandle
);
988 Find files under current directory.
990 All files and sub-directories in current directory
991 will be stored in DirectoryMenu for future use.
993 @param FileHandle Parent file handle.
994 @param FileName Parent file name.
995 @param DeviceHandle Driver handle for this partition.
997 @retval EFI_SUCCESS Get files from current dir successfully.
998 @return Other value if can't get files from current dir.
1003 IN EFI_FILE_HANDLE FileHandle
,
1004 IN UINT16
*FileName
,
1005 IN EFI_HANDLE DeviceHandle
1008 EFI_FILE_INFO
*DirInfo
;
1010 UINTN DirBufferSize
;
1011 MENU_ENTRY
*NewMenuEntry
;
1012 FILE_CONTEXT
*NewFileContext
;
1019 DirBufferSize
= sizeof (EFI_FILE_INFO
) + 1024;
1020 DirInfo
= AllocateZeroPool (DirBufferSize
);
1021 if (DirInfo
== NULL
) {
1022 return EFI_OUT_OF_RESOURCES
;
1026 // Get all files in current directory
1027 // Pass 1 to get Directories
1028 // Pass 2 to get files that are EFI images
1030 Status
= EFI_SUCCESS
;
1031 for (Pass
= 1; Pass
<= 2; Pass
++) {
1032 FileHandle
->SetPosition (FileHandle
, 0);
1034 BufferSize
= DirBufferSize
;
1035 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, DirInfo
);
1036 if (EFI_ERROR (Status
) || BufferSize
== 0) {
1037 Status
= EFI_SUCCESS
;
1041 if (((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) != 0 && Pass
== 2) ||
1042 ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0 && Pass
== 1)
1045 // Pass 1 is for Directories
1046 // Pass 2 is for file names
1051 if (!((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) != 0 || LibIsSupportedFileType (DirInfo
->FileName
))) {
1053 // Slip file unless it is a directory entry or a .EFI file
1058 NewMenuEntry
= LibCreateMenuEntry ();
1059 if (NULL
== NewMenuEntry
) {
1060 Status
= EFI_OUT_OF_RESOURCES
;
1064 NewFileContext
= (FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1065 NewFileContext
->DeviceHandle
= DeviceHandle
;
1066 NewFileContext
->FileName
= LibAppendFileName (FileName
, DirInfo
->FileName
);
1067 if (NewFileContext
->FileName
== NULL
) {
1068 LibDestroyMenuEntry (NewMenuEntry
);
1069 Status
= EFI_OUT_OF_RESOURCES
;
1072 NewFileContext
->FileHandle
= FileHandle
;
1073 NewFileContext
->DevicePath
= FileDevicePath (NewFileContext
->DeviceHandle
, NewFileContext
->FileName
);
1074 NewMenuEntry
->HelpString
= NULL
;
1075 NewFileContext
->IsDir
= (BOOLEAN
) ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
);
1077 if (NewFileContext
->IsDir
) {
1078 BufferSize
= StrLen (DirInfo
->FileName
) * 2 + 6;
1079 NewMenuEntry
->DisplayString
= AllocateZeroPool (BufferSize
);
1081 NewMenuEntry
->DisplayString
,
1087 NewMenuEntry
->DisplayString
= LibStrDuplicate (DirInfo
->FileName
);
1090 NewMenuEntry
->DisplayStringToken
= HiiSetString (
1091 gFileExplorerPrivate
.FeHiiHandle
,
1093 NewMenuEntry
->DisplayString
,
1097 NewFileContext
->IsRoot
= FALSE
;
1100 InsertTailList (&gFileExplorerPrivate
.FsOptionMenu
->Head
, &NewMenuEntry
->Link
);
1104 gFileExplorerPrivate
.FsOptionMenu
->MenuNumber
= OptionNumber
;
1114 Refresh the global UpdateData structure.
1118 LibRefreshUpdateData (
1123 // Free current updated date
1125 if (mLibStartOpCodeHandle
!= NULL
) {
1126 HiiFreeOpCodeHandle (mLibStartOpCodeHandle
);
1128 if (mLibEndOpCodeHandle
!= NULL
) {
1129 HiiFreeOpCodeHandle (mLibEndOpCodeHandle
);
1133 // Create new OpCode Handle
1135 mLibStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1136 mLibEndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1139 // Create Hii Extend Label OpCode as the start opcode
1141 mLibStartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1142 mLibStartOpCodeHandle
,
1145 sizeof (EFI_IFR_GUID_LABEL
)
1147 mLibStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1149 mLibStartLabel
->Number
= FORM_FILE_EXPLORER_ID
;
1152 // Create Hii Extend Label OpCode as the start opcode
1154 mLibEndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1155 mLibEndOpCodeHandle
,
1158 sizeof (EFI_IFR_GUID_LABEL
)
1160 mLibEndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1162 mLibEndLabel
->Number
= LABEL_END
;
1167 Update the File Explore page.
1171 LibUpdateFileExplorePage (
1176 MENU_ENTRY
*NewMenuEntry
;
1177 FILE_CONTEXT
*NewFileContext
;
1178 MENU_OPTION
*MenuOption
;
1180 NewMenuEntry
= NULL
;
1181 NewFileContext
= NULL
;
1183 LibRefreshUpdateData ();
1184 MenuOption
= gFileExplorerPrivate
.FsOptionMenu
;
1186 for (Index
= 0; Index
< MenuOption
->MenuNumber
; Index
++) {
1187 NewMenuEntry
= LibGetMenuEntry (MenuOption
, Index
);
1188 NewFileContext
= (FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1190 if (!NewFileContext
->IsDir
) {
1192 // Create Text opcode for directory, also create Text opcode for file in FileExplorerStateBootFromFile.
1194 HiiCreateActionOpCode (
1195 mLibStartOpCodeHandle
,
1196 (UINT16
) (FILE_OPTION_OFFSET
+ Index
),
1197 NewMenuEntry
->DisplayStringToken
,
1198 STRING_TOKEN (STR_NULL_STRING
),
1199 EFI_IFR_FLAG_CALLBACK
,
1204 // Create Goto opcode for file in FileExplorerStateAddBootOption or FileExplorerStateAddDriverOptionState.
1206 HiiCreateGotoOpCode (
1207 mLibStartOpCodeHandle
,
1208 FORM_FILE_EXPLORER_ID
,
1209 NewMenuEntry
->DisplayStringToken
,
1210 STRING_TOKEN (STR_NULL_STRING
),
1211 EFI_IFR_FLAG_CALLBACK
,
1212 (UINT16
) (FILE_OPTION_OFFSET
+ Index
)
1218 gFileExplorerPrivate
.FeHiiHandle
,
1220 FORM_FILE_EXPLORER_ID
,
1221 mLibStartOpCodeHandle
, // Label FORM_FILE_EXPLORER_ID
1222 mLibEndOpCodeHandle
// LABEL_END
1227 Update the file explower page with the refershed file system.
1229 @param KeyValue Key value to identify the type of data to expect.
1231 @retval EFI_SUCCESS Update the file explorer form success.
1232 @retval other errors Error occur when parse one directory.
1236 LibUpdateFileExplorer (
1240 UINT16 FileOptionMask
;
1241 MENU_ENTRY
*NewMenuEntry
;
1242 FILE_CONTEXT
*NewFileContext
;
1244 EFI_FILE_HANDLE FileHandle
;
1246 Status
= EFI_SUCCESS
;
1247 FileOptionMask
= (UINT16
) (FILE_OPTION_MASK
& KeyValue
);
1248 NewMenuEntry
= LibGetMenuEntry (gFileExplorerPrivate
.FsOptionMenu
, FileOptionMask
);
1249 NewFileContext
= (FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1251 if (NewFileContext
->IsDir
) {
1252 RemoveEntryList (&NewMenuEntry
->Link
);
1253 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1254 LibGetFileHandleFromMenu (NewMenuEntry
, &FileHandle
);
1255 Status
= LibFindFiles (FileHandle
, NewFileContext
->FileName
, NewFileContext
->DeviceHandle
);
1256 if (!EFI_ERROR (Status
)) {
1257 LibUpdateFileExplorePage ();
1259 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1261 LibDestroyMenuEntry (NewMenuEntry
);
1268 Get the device path info saved in the menu structure.
1270 @param KeyValue Key value to identify the type of data to expect.
1278 UINT16 FileOptionMask
;
1279 MENU_ENTRY
*NewMenuEntry
;
1280 FILE_CONTEXT
*NewFileContext
;
1282 FileOptionMask
= (UINT16
) (FILE_OPTION_MASK
& KeyValue
);
1284 NewMenuEntry
= LibGetMenuEntry (gFileExplorerPrivate
.FsOptionMenu
, FileOptionMask
);
1286 NewFileContext
= (FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1288 if (gFileExplorerPrivate
.RetDevicePath
!= NULL
) {
1289 FreePool (gFileExplorerPrivate
.RetDevicePath
);
1291 gFileExplorerPrivate
.RetDevicePath
= DuplicateDevicePath (NewFileContext
->DevicePath
);
1295 Choose a file in the specified directory.
1297 If user input NULL for the RootDirectory, will choose file in the system.
1299 If user input *File != NULL, function will return the allocate device path
1300 info for the choosed file, caller has to free the memory after use it.
1302 @param RootDirectory Pointer to the root directory.
1303 @param FileType The file type need to choose.
1304 @param ChooseHandler Function pointer to the extra task need to do
1305 after choose one file.
1306 @param File Return the device path for the last time chosed file.
1308 @retval EFI_SUCESS Choose file success.
1309 @retval EFI_INVALID_PARAMETER Both ChooseHandler and return device path are NULL
1310 One of them must not NULL.
1311 @retval Other errors Choose file failed.
1316 IN EFI_DEVICE_PATH_PROTOCOL
*RootDirectory
,
1317 IN CHAR16
*FileType
, OPTIONAL
1318 IN CHOOSE_HANDLER ChooseHandler
, OPTIONAL
1319 OUT EFI_DEVICE_PATH_PROTOCOL
**File OPTIONAL
1322 EFI_FILE_HANDLE FileHandle
;
1325 EFI_HANDLE DeviceHandle
;
1327 if ((ChooseHandler
== NULL
) && (File
== NULL
)) {
1328 return EFI_INVALID_PARAMETER
;
1333 gFileExplorerPrivate
.RetDevicePath
= NULL
;
1334 gFileExplorerPrivate
.ChooseHandler
= ChooseHandler
;
1335 if (FileType
!= NULL
) {
1336 gFileExplorerPrivate
.FileType
= AllocateCopyPool (StrSize (FileType
), FileType
);
1337 ASSERT(gFileExplorerPrivate
.FileType
!= NULL
);
1338 LibToLowerString(gFileExplorerPrivate
.FileType
);
1340 gFileExplorerPrivate
.FileType
= NULL
;
1343 if (RootDirectory
== NULL
) {
1344 Status
= LibFindFileSystem();
1346 Status
= LibGetFileHandleFromDevicePath(RootDirectory
, &FileHandle
, &FileName
, &DeviceHandle
);
1347 if (EFI_ERROR (Status
)) {
1351 Status
= LibFindFiles (FileHandle
, FileName
, DeviceHandle
);
1353 if (EFI_ERROR (Status
)) {
1357 LibUpdateFileExplorePage();
1359 gFileExplorerPrivate
.FormBrowser2
->SendForm (
1360 gFileExplorerPrivate
.FormBrowser2
,
1361 &gFileExplorerPrivate
.FeHiiHandle
,
1370 if ((Status
== EFI_SUCCESS
) && (File
!= NULL
)) {
1371 *File
= gFileExplorerPrivate
.RetDevicePath
;
1372 } else if (gFileExplorerPrivate
.RetDevicePath
!= NULL
) {
1373 FreePool (gFileExplorerPrivate
.RetDevicePath
);
1376 if (gFileExplorerPrivate
.FileType
!= NULL
) {
1377 FreePool (gFileExplorerPrivate
.FileType
);
1380 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1382 if (FileName
!= NULL
) {
1383 FreePool (FileName
);
1391 Install Boot Manager Menu driver.
1393 @param ImageHandle The image handle.
1394 @param SystemTable The system table.
1396 @retval EFI_SUCEESS Install File explorer library success.
1401 FileExplorerLibConstructor (
1402 IN EFI_HANDLE ImageHandle
,
1403 IN EFI_SYSTEM_TABLE
*SystemTable
1408 gHiiVendorDevicePath
= (HII_VENDOR_DEVICE_PATH
*) DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL
*)&FeHiiVendorDevicePath
);
1409 ASSERT (gHiiVendorDevicePath
!= NULL
);
1410 CopyGuid (&gHiiVendorDevicePath
->VendorDevicePath
.Guid
, &gEfiCallerIdGuid
);
1413 // Install Device Path Protocol and Config Access protocol to driver handle
1415 Status
= gBS
->InstallMultipleProtocolInterfaces (
1416 &gFileExplorerPrivate
.FeDriverHandle
,
1417 &gEfiDevicePathProtocolGuid
,
1418 gHiiVendorDevicePath
,
1419 &gEfiHiiConfigAccessProtocolGuid
,
1420 &gFileExplorerPrivate
.FeConfigAccess
,
1423 if (Status
== EFI_ALREADY_STARTED
) {
1426 if (EFI_ERROR (Status
)) {
1431 // Post our File Explorer VFR binary to the HII database.
1433 gFileExplorerPrivate
.FeHiiHandle
= HiiAddPackages (
1435 gFileExplorerPrivate
.FeDriverHandle
,
1437 FileExplorerLibStrings
,
1440 ASSERT (gFileExplorerPrivate
.FeHiiHandle
!= NULL
);
1443 // Locate Formbrowser2 protocol
1445 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &gFileExplorerPrivate
.FormBrowser2
);
1446 ASSERT_EFI_ERROR (Status
);
1448 InitializeListHead (&gFileExplorerPrivate
.FsOptionMenu
->Head
);
1454 Unloads the application and its installed protocol.
1456 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1457 @param[in] SystemTable The system table.
1459 @retval EFI_SUCCESS The image has been unloaded.
1463 FileExplorerLibDestructor (
1464 IN EFI_HANDLE ImageHandle
,
1465 IN EFI_SYSTEM_TABLE
*SystemTable
1470 ASSERT (gHiiVendorDevicePath
!= NULL
);
1472 if (gFileExplorerPrivate
.FeDriverHandle
!= NULL
) {
1473 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1474 gFileExplorerPrivate
.FeDriverHandle
,
1475 &gEfiDevicePathProtocolGuid
,
1476 gHiiVendorDevicePath
,
1477 &gEfiHiiConfigAccessProtocolGuid
,
1478 &gFileExplorerPrivate
.FeConfigAccess
,
1481 ASSERT_EFI_ERROR (Status
);
1483 HiiRemovePackages (gFileExplorerPrivate
.FeHiiHandle
);
1486 FreePool (gHiiVendorDevicePath
);