2 File explorer related functions.
4 Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>
5 This software and associated documentation (if any) is furnished
6 under a license and may only be used or copied in accordance
7 with the terms of the license. Except as permitted by such
8 license, no part of this software or documentation may be
9 reproduced, stored in a retrieval system, or transmitted in any
10 form or by any means without the express written consent of
15 #include "FileExplorer.h"
17 EFI_GUID FileExplorerGuid
= EFI_FILE_EXPLORE_FORMSET_GUID
;
20 /// File system selection menu
22 MENU_OPTION mFsOptionMenu
= {
23 MENU_OPTION_SIGNATURE
,
29 FILE_EXPLORER_CALLBACK_DATA gFileExplorerPrivate
= {
30 FILE_EXPLORER_CALLBACK_DATA_SIGNATURE
,
43 HII_VENDOR_DEVICE_PATH
*gHiiVendorDevicePath
;
45 HII_VENDOR_DEVICE_PATH FeHiiVendorDevicePath
= {
51 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
52 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
56 // Will be replace with gEfiCallerIdGuid in code.
58 { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } }
62 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
64 (UINT8
) (END_DEVICE_PATH_LENGTH
),
65 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
70 VOID
*mLibStartOpCodeHandle
= NULL
;
71 VOID
*mLibEndOpCodeHandle
= NULL
;
72 EFI_IFR_GUID_LABEL
*mLibStartLabel
= NULL
;
73 EFI_IFR_GUID_LABEL
*mLibEndLabel
= NULL
;
76 This function allows a caller to extract the current configuration for one
77 or more named elements from the target driver.
80 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
81 @param Request A null-terminated Unicode string in <ConfigRequest> format.
82 @param Progress On return, points to a character in the Request string.
83 Points to the string's null terminator if request was successful.
84 Points to the most recent '&' before the first failing name/value
85 pair (or the beginning of the string if the failure is in the
86 first name/value pair) if the request was not successful.
87 @param Results A null-terminated Unicode string in <ConfigAltResp> format which
88 has all values filled in for the names in the Request string.
89 String to be allocated by the called function.
91 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
92 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
98 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
99 IN CONST EFI_STRING Request
,
100 OUT EFI_STRING
*Progress
,
101 OUT EFI_STRING
*Results
104 if (Progress
== NULL
|| Results
== NULL
) {
105 return EFI_INVALID_PARAMETER
;
109 return EFI_NOT_FOUND
;
113 This function processes the results of changes in configuration.
116 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
117 @param Configuration A null-terminated Unicode string in <ConfigResp> format.
118 @param Progress A pointer to a string filled in with the offset of the most
119 recent '&' before the first failing name/value pair (or the
120 beginning of the string if the failure is in the first
121 name/value pair) or the terminating NULL if all was successful.
123 @retval EFI_INVALID_PARAMETER Configuration is NULL.
124 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this driver.
130 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
131 IN CONST EFI_STRING Configuration
,
132 OUT EFI_STRING
*Progress
135 if (Configuration
== NULL
|| Progress
== NULL
) {
136 return EFI_INVALID_PARAMETER
;
139 *Progress
= Configuration
;
140 return EFI_NOT_FOUND
;
144 This function processes the results of changes in configuration.
145 When user select a interactive opcode, this callback will be triggered.
146 Based on the Question(QuestionId) that triggers the callback, the corresponding
147 actions is performed. It handles:
149 1) Process the axtra action or exit file explorer when user select one file .
150 2) update of file content if a dir is selected.
152 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
153 @param Action Specifies the type of action taken by the browser.
154 @param QuestionId A unique value which is sent to the original exporting driver
155 so that it can identify the type of data to expect.
156 @param Type The type of value for the question.
157 @param Value A pointer to the data being sent to the original exporting driver.
158 @param ActionRequest On return, points to the action requested by the callback function.
160 @retval EFI_SUCCESS The callback successfully handled the action.
161 @retval other error Error occur when parse one directory.
166 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
167 IN EFI_BROWSER_ACTION Action
,
168 IN EFI_QUESTION_ID QuestionId
,
170 IN EFI_IFR_TYPE_VALUE
*Value
,
171 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
179 if (Action
!= EFI_BROWSER_ACTION_CHANGING
&& Action
!= EFI_BROWSER_ACTION_CHANGED
) {
181 // Do nothing for other UEFI Action. Only do call back when data is changed.
183 return EFI_UNSUPPORTED
;
186 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_NONE
;
188 if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
189 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
190 return EFI_INVALID_PARAMETER
;
193 if (QuestionId
>= FILE_OPTION_OFFSET
) {
194 LibGetDevicePath(QuestionId
);
197 // Process the extra action.
199 if (gFileExplorerPrivate
.ChooseHandler
!= NULL
) {
200 NeedExit
= gFileExplorerPrivate
.ChooseHandler (gFileExplorerPrivate
.RetDevicePath
);
204 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
207 } else if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
209 return EFI_INVALID_PARAMETER
;
212 if (QuestionId
>= FILE_OPTION_OFFSET
) {
213 Status
= LibUpdateFileExplorer (QuestionId
);
214 if (EFI_ERROR (Status
)) {
224 Create a menu entry by given menu type.
226 @retval NULL If failed to create the menu.
227 @return the new menu entry.
235 MENU_ENTRY
*MenuEntry
;
238 // Create new menu entry
240 MenuEntry
= AllocateZeroPool (sizeof (MENU_ENTRY
));
241 if (MenuEntry
== NULL
) {
245 MenuEntry
->VariableContext
= AllocateZeroPool (sizeof (FILE_CONTEXT
));
246 if (MenuEntry
->VariableContext
== NULL
) {
247 FreePool (MenuEntry
);
251 MenuEntry
->Signature
= MENU_ENTRY_SIGNATURE
;
257 Get the Menu Entry from the list in Menu Entry List.
259 If MenuNumber is great or equal to the number of Menu
260 Entry in the list, then ASSERT.
262 @param MenuOption The Menu Entry List to read the menu entry.
263 @param MenuNumber The index of Menu Entry.
265 @return The Menu Entry.
270 MENU_OPTION
*MenuOption
,
274 MENU_ENTRY
*NewMenuEntry
;
278 ASSERT (MenuNumber
< MenuOption
->MenuNumber
);
280 List
= MenuOption
->Head
.ForwardLink
;
281 for (Index
= 0; Index
< MenuNumber
; Index
++) {
282 List
= List
->ForwardLink
;
285 NewMenuEntry
= CR (List
, MENU_ENTRY
, Link
, MENU_ENTRY_SIGNATURE
);
291 Free up all resource allocated for a BM_MENU_ENTRY.
293 @param MenuEntry A pointer to BM_MENU_ENTRY.
297 LibDestroyMenuEntry (
298 MENU_ENTRY
*MenuEntry
301 FILE_CONTEXT
*FileContext
;
303 FileContext
= (FILE_CONTEXT
*) MenuEntry
->VariableContext
;
305 if (!FileContext
->IsRoot
) {
306 FreePool (FileContext
->DevicePath
);
308 if (FileContext
->FileHandle
!= NULL
) {
309 FileContext
->FileHandle
->Close (FileContext
->FileHandle
);
313 if (FileContext
->FileName
!= NULL
) {
314 FreePool (FileContext
->FileName
);
317 FreePool (FileContext
);
319 FreePool (MenuEntry
->DisplayString
);
320 if (MenuEntry
->HelpString
!= NULL
) {
321 FreePool (MenuEntry
->HelpString
);
324 FreePool (MenuEntry
);
329 Free resources allocated in Allocate Rountine.
331 @param FreeMenu Menu to be freed
335 MENU_OPTION
*FreeMenu
338 MENU_ENTRY
*MenuEntry
;
339 while (!IsListEmpty (&FreeMenu
->Head
)) {
341 FreeMenu
->Head
.ForwardLink
,
346 RemoveEntryList (&MenuEntry
->Link
);
347 LibDestroyMenuEntry (MenuEntry
);
349 FreeMenu
->MenuNumber
= 0;
354 Function opens and returns a file handle to the root directory of a volume.
356 @param DeviceHandle A handle for a device
358 @return A valid file handle or NULL is returned
363 IN EFI_HANDLE DeviceHandle
367 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
368 EFI_FILE_HANDLE File
;
373 // File the file system interface to the device
375 Status
= gBS
->HandleProtocol (
377 &gEfiSimpleFileSystemProtocolGuid
,
382 // Open the root directory of the volume
384 if (!EFI_ERROR (Status
)) {
385 Status
= Volume
->OpenVolume (
393 return EFI_ERROR (Status
) ? NULL
: File
;
397 This function converts an input device structure to a Unicode string.
399 @param DevPath A pointer to the device path structure.
401 @return A new allocated Unicode string that represents the device path.
406 IN EFI_DEVICE_PATH_PROTOCOL
*DevPath
411 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevPathToText
;
413 if (DevPath
== NULL
) {
417 Status
= gBS
->LocateProtocol (
418 &gEfiDevicePathToTextProtocolGuid
,
420 (VOID
**) &DevPathToText
422 ASSERT_EFI_ERROR (Status
);
423 ToText
= DevPathToText
->ConvertDevicePathToText (
428 ASSERT (ToText
!= NULL
);
436 @param Src The source.
438 @return A new string which is duplicated copy of the source.
439 @retval NULL If there is not enough memory.
450 Size
= StrSize (Src
);
451 Dest
= AllocateZeroPool (Size
);
452 ASSERT (Dest
!= NULL
);
454 CopyMem (Dest
, Src
, Size
);
462 Function gets the file information from an open file descriptor, and stores it
463 in a buffer allocated from pool.
465 @param FHand File Handle.
466 @param InfoType Info type need to get.
468 @retval A pointer to a buffer with file information or NULL is returned
473 IN EFI_FILE_HANDLE FHand
,
474 IN EFI_GUID
*InfoType
478 EFI_FILE_INFO
*Buffer
;
484 Status
= FHand
->GetInfo (
490 if (Status
== EFI_BUFFER_TOO_SMALL
) {
491 Buffer
= AllocatePool (BufferSize
);
492 ASSERT (Buffer
!= NULL
);
495 Status
= FHand
->GetInfo (
507 Get file type base on the file name.
508 Just cut the file name, from the ".". eg ".efi"
510 @param FileName File need to be checked.
512 @retval the file type string.
522 Index
= StrLen (FileName
) - 1;
523 while ((FileName
[Index
] != L
'.') && (Index
!= 0)) {
527 return Index
== 0 ? NULL
: &FileName
[Index
];
531 Converts the unicode character of the string from uppercase to lowercase.
532 This is a internal function.
534 @param ConfigString String to be converted
544 for (TmpStr
= String
; *TmpStr
!= L
'\0'; TmpStr
++) {
545 if (*TmpStr
>= L
'A' && *TmpStr
<= L
'Z') {
546 *TmpStr
= (CHAR16
) (*TmpStr
- L
'A' + L
'a');
553 Check whether current FileName point to a valid
556 @param FileName File need to be checked.
558 @retval TRUE Is Efi Image
559 @retval FALSE Not a valid Efi Image
563 LibIsSupportedFileType (
567 CHAR16
*InputFileType
;
571 if (gFileExplorerPrivate
.FileType
== NULL
) {
575 InputFileType
= LibGetTypeFromName (FileName
);
577 // If the file not has *.* style, always return TRUE.
579 if (InputFileType
== NULL
) {
583 TmpStr
= AllocateCopyPool (StrSize (InputFileType
), InputFileType
);
584 LibToLowerString(TmpStr
);
586 IsSupported
= (StrStr (gFileExplorerPrivate
.FileType
, TmpStr
) == NULL
? FALSE
: TRUE
);
594 Append file name to existing file name.
596 @param Str1 The existing file name
597 @param Str2 The file name to be appended
599 @return Allocate a new string to hold the appended result.
600 Caller is responsible to free the returned string.
617 Size1
= StrSize (Str1
);
618 Size2
= StrSize (Str2
);
619 MaxLen
= (Size1
+ Size2
+ sizeof (CHAR16
))/ sizeof (CHAR16
);
620 Str
= AllocateZeroPool (Size1
+ Size2
+ sizeof (CHAR16
));
621 ASSERT (Str
!= NULL
);
623 TmpStr
= AllocateZeroPool (Size1
+ Size2
+ sizeof (CHAR16
));
624 ASSERT (TmpStr
!= NULL
);
626 StrCpyS (Str
, MaxLen
, Str1
);
627 if (!((*Str
== '\\') && (*(Str
+ 1) == 0))) {
628 StrCatS (Str
, MaxLen
, L
"\\");
631 StrCatS (Str
, MaxLen
, Str2
);
636 if (*Ptr
== '\\' && *(Ptr
+ 1) == '.' && *(Ptr
+ 2) == '.' && *(Ptr
+ 3) == L
'\\') {
638 // Convert "\Name\..\" to "\"
639 // DO NOT convert the .. if it is at the end of the string. This will
640 // break the .. behavior in changing directories.
644 // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings
647 StrCpyS (TmpStr
, MaxLen
, Ptr
+ 3);
648 StrCpyS (LastSlash
, MaxLen
- (UINTN
) (LastSlash
- Str
), TmpStr
);
650 } else if (*Ptr
== '\\' && *(Ptr
+ 1) == '.' && *(Ptr
+ 2) == '\\') {
652 // Convert a "\.\" to a "\"
656 // Use TmpStr as a backup, as StrCpyS in BaseLib does not handle copy of two strings
659 StrCpyS (TmpStr
, MaxLen
, Ptr
+ 2);
660 StrCpyS (Ptr
, MaxLen
- (UINTN
) (Ptr
- Str
), TmpStr
);
662 } else if (*Ptr
== '\\') {
675 This function build the FsOptionMenu list which records all
676 available file system in the system. They includes all instances
677 of EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, all instances of EFI_LOAD_FILE_SYSTEM.
680 @retval EFI_SUCCESS Success find the file system
681 @retval EFI_OUT_OF_RESOURCES Can not create menu entry
689 UINTN NoSimpleFsHandles
;
690 UINTN NoLoadFileHandles
;
691 EFI_HANDLE
*SimpleFsHandle
;
692 EFI_HANDLE
*LoadFileHandle
;
696 MENU_ENTRY
*MenuEntry
;
697 FILE_CONTEXT
*FileContext
;
699 EFI_FILE_SYSTEM_VOLUME_LABEL
*Info
;
701 NoSimpleFsHandles
= 0;
702 NoLoadFileHandles
= 0;
706 // Locate Handles that support Simple File System protocol
708 Status
= gBS
->LocateHandleBuffer (
710 &gEfiSimpleFileSystemProtocolGuid
,
715 if (!EFI_ERROR (Status
)) {
717 // Find all the instances of the File System prototocol
719 for (Index
= 0; Index
< NoSimpleFsHandles
; Index
++) {
721 // Allocate pool for this load option
723 MenuEntry
= LibCreateMenuEntry ();
724 if (NULL
== MenuEntry
) {
725 FreePool (SimpleFsHandle
);
726 return EFI_OUT_OF_RESOURCES
;
729 FileContext
= (FILE_CONTEXT
*) MenuEntry
->VariableContext
;
730 FileContext
->DeviceHandle
= SimpleFsHandle
[Index
];
731 FileContext
->FileHandle
= LibOpenRoot (FileContext
->DeviceHandle
);
732 if (FileContext
->FileHandle
== NULL
) {
733 LibDestroyMenuEntry (MenuEntry
);
737 MenuEntry
->HelpString
= LibDevicePathToStr (DevicePathFromHandle (FileContext
->DeviceHandle
));
738 FileContext
->FileName
= LibStrDuplicate (L
"\\");
739 FileContext
->DevicePath
= FileDevicePath (FileContext
->DeviceHandle
, FileContext
->FileName
);
740 FileContext
->IsDir
= TRUE
;
741 FileContext
->IsRoot
= TRUE
;
744 // Get current file system's Volume Label
746 Info
= (EFI_FILE_SYSTEM_VOLUME_LABEL
*) LibFileInfo (FileContext
->FileHandle
, &gEfiFileSystemVolumeLabelInfoIdGuid
);
748 VolumeLabel
= L
"NO FILE SYSTEM INFO";
750 if (Info
->VolumeLabel
== NULL
) {
751 VolumeLabel
= L
"NULL VOLUME LABEL";
753 VolumeLabel
= Info
->VolumeLabel
;
754 if (*VolumeLabel
== 0x0000) {
755 VolumeLabel
= L
"NO VOLUME LABEL";
759 MenuEntry
->DisplayString
= AllocateZeroPool (MAX_CHAR
);
760 ASSERT (MenuEntry
->DisplayString
!= NULL
);
762 MenuEntry
->DisplayString
,
766 MenuEntry
->HelpString
768 MenuEntry
->DisplayStringToken
= HiiSetString (
769 gFileExplorerPrivate
.FeHiiHandle
,
771 MenuEntry
->DisplayString
,
777 InsertTailList (&gFileExplorerPrivate
.FsOptionMenu
->Head
, &MenuEntry
->Link
);
781 if (NoSimpleFsHandles
!= 0) {
782 FreePool (SimpleFsHandle
);
786 // Searching for handles that support Load File protocol
788 Status
= gBS
->LocateHandleBuffer (
790 &gEfiLoadFileProtocolGuid
,
796 if (!EFI_ERROR (Status
)) {
797 for (Index
= 0; Index
< NoLoadFileHandles
; Index
++) {
798 MenuEntry
= LibCreateMenuEntry ();
799 if (NULL
== MenuEntry
) {
800 FreePool (LoadFileHandle
);
801 return EFI_OUT_OF_RESOURCES
;
804 FileContext
= (FILE_CONTEXT
*) MenuEntry
->VariableContext
;
805 FileContext
->DeviceHandle
= LoadFileHandle
[Index
];
806 FileContext
->IsRoot
= TRUE
;
808 FileContext
->DevicePath
= DevicePathFromHandle (FileContext
->DeviceHandle
);
809 FileContext
->FileName
= LibDevicePathToStr (FileContext
->DevicePath
);
811 MenuEntry
->HelpString
= LibDevicePathToStr (FileContext
->DevicePath
);
812 MenuEntry
->DisplayString
= AllocateZeroPool (MAX_CHAR
);
813 ASSERT (MenuEntry
->DisplayString
!= NULL
);
815 MenuEntry
->DisplayString
,
818 MenuEntry
->HelpString
820 MenuEntry
->DisplayStringToken
= HiiSetString (
821 gFileExplorerPrivate
.FeHiiHandle
,
823 MenuEntry
->DisplayString
,
828 InsertTailList (&gFileExplorerPrivate
.FsOptionMenu
->Head
, &MenuEntry
->Link
);
832 if (NoLoadFileHandles
!= 0) {
833 FreePool (LoadFileHandle
);
836 gFileExplorerPrivate
.FsOptionMenu
->MenuNumber
= OptionNumber
;
842 Find the file handle from the input menu info.
844 @param MenuEntry Input Menu info.
845 @param RetFileHandle Return the file handle for the input device path.
847 @retval EFI_SUCESS Find the file handle success.
848 @retval Other Find the file handle failure.
851 LibGetFileHandleFromMenu (
852 IN MENU_ENTRY
*MenuEntry
,
853 OUT EFI_FILE_HANDLE
*RetFileHandle
857 EFI_FILE_HANDLE NewDir
;
858 FILE_CONTEXT
*FileContext
;
861 FileContext
= (FILE_CONTEXT
*) MenuEntry
->VariableContext
;
862 Dir
= FileContext
->FileHandle
;
865 // Open current directory to get files from it
870 FileContext
->FileName
,
874 if (EFI_ERROR (Status
)) {
878 if (!FileContext
->IsRoot
) {
882 *RetFileHandle
= NewDir
;
888 Find the file handle from the input device path info.
890 @param RootDirectory Device path info.
891 @param RetFileHandle Return the file handle for the input device path.
892 @param ParentFileName Parent file name.
893 @param DeviceHandle Driver handle for this partition.
895 @retval EFI_SUCESS Find the file handle success.
896 @retval Other Find the file handle failure.
899 LibGetFileHandleFromDevicePath (
900 IN EFI_DEVICE_PATH_PROTOCOL
*RootDirectory
,
901 OUT EFI_FILE_HANDLE
*RetFileHandle
,
902 OUT UINT16
**ParentFileName
,
903 OUT EFI_HANDLE
*DeviceHandle
906 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
907 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePathNode
;
910 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*Volume
;
911 EFI_FILE_HANDLE FileHandle
;
912 EFI_FILE_HANDLE LastHandle
;
915 *ParentFileName
= NULL
;
918 // Attempt to access the file via a file system interface
920 DevicePathNode
= RootDirectory
;
921 Status
= gBS
->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid
, &DevicePathNode
, &Handle
);
922 if (EFI_ERROR (Status
)) {
926 Status
= gBS
->HandleProtocol (Handle
, &gEfiSimpleFileSystemProtocolGuid
, (VOID
**)&Volume
);
927 if (EFI_ERROR (Status
)) {
932 // Open the Volume to get the File System handle
934 Status
= Volume
->OpenVolume (Volume
, &FileHandle
);
935 if (EFI_ERROR (Status
)) {
939 *DeviceHandle
= Handle
;
941 if (IsDevicePathEnd(DevicePathNode
)) {
942 *ParentFileName
= AllocateCopyPool (StrSize (L
"\\"), L
"\\");
943 *RetFileHandle
= FileHandle
;
948 // Duplicate the device path to avoid the access to unaligned device path node.
949 // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH
950 // nodes, It assures the fields in device path nodes are 2 byte aligned.
952 TempDevicePathNode
= DuplicateDevicePath (DevicePathNode
);
953 if (TempDevicePathNode
== NULL
) {
956 // Setting Status to an EFI_ERROR value will cause the rest of
957 // the file system support below to be skipped.
959 Status
= EFI_OUT_OF_RESOURCES
;
963 // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the
964 // directory information and filename can be seperate. The goal is to inch
965 // our way down each device path node and close the previous node
967 DevicePathNode
= TempDevicePathNode
;
968 while (!EFI_ERROR (Status
) && !IsDevicePathEnd (DevicePathNode
)) {
969 if (DevicePathType (DevicePathNode
) != MEDIA_DEVICE_PATH
||
970 DevicePathSubType (DevicePathNode
) != MEDIA_FILEPATH_DP
) {
971 Status
= EFI_UNSUPPORTED
;
975 LastHandle
= FileHandle
;
978 Status
= LastHandle
->Open (
981 ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
,
985 if (*ParentFileName
== NULL
) {
986 *ParentFileName
= AllocateCopyPool (StrSize (((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
), ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
);
988 TempPath
= LibAppendFileName (*ParentFileName
, ((FILEPATH_DEVICE_PATH
*) DevicePathNode
)->PathName
);
989 FreePool (*ParentFileName
);
990 *ParentFileName
= TempPath
;
994 // Close the previous node
996 LastHandle
->Close (LastHandle
);
998 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
1001 if (EFI_ERROR (Status
)) {
1005 *RetFileHandle
= FileHandle
;
1007 Status
= EFI_SUCCESS
;
1010 if (TempDevicePathNode
!= NULL
) {
1011 FreePool (TempDevicePathNode
);
1014 if ((FileHandle
!= NULL
) && (EFI_ERROR (Status
))) {
1015 FileHandle
->Close (FileHandle
);
1022 Find files under current directory.
1024 All files and sub-directories in current directory
1025 will be stored in DirectoryMenu for future use.
1027 @param FileHandle Parent file handle.
1028 @param FileName Parent file name.
1029 @param DeviceHandle Driver handle for this partition.
1031 @retval EFI_SUCCESS Get files from current dir successfully.
1032 @return Other value if can't get files from current dir.
1037 IN EFI_FILE_HANDLE FileHandle
,
1038 IN UINT16
*FileName
,
1039 IN EFI_HANDLE DeviceHandle
1042 EFI_FILE_INFO
*DirInfo
;
1044 UINTN DirBufferSize
;
1045 MENU_ENTRY
*NewMenuEntry
;
1046 FILE_CONTEXT
*NewFileContext
;
1053 DirBufferSize
= sizeof (EFI_FILE_INFO
) + 1024;
1054 DirInfo
= AllocateZeroPool (DirBufferSize
);
1055 if (DirInfo
== NULL
) {
1056 return EFI_OUT_OF_RESOURCES
;
1060 // Get all files in current directory
1061 // Pass 1 to get Directories
1062 // Pass 2 to get files that are EFI images
1064 for (Pass
= 1; Pass
<= 2; Pass
++) {
1065 FileHandle
->SetPosition (FileHandle
, 0);
1067 BufferSize
= DirBufferSize
;
1068 Status
= FileHandle
->Read (FileHandle
, &BufferSize
, DirInfo
);
1069 if (EFI_ERROR (Status
) || BufferSize
== 0) {
1073 if (((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) != 0 && Pass
== 2) ||
1074 ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == 0 && Pass
== 1)
1077 // Pass 1 is for Directories
1078 // Pass 2 is for file names
1083 if (!((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) != 0 || LibIsSupportedFileType (DirInfo
->FileName
))) {
1085 // Slip file unless it is a directory entry or a .EFI file
1090 NewMenuEntry
= LibCreateMenuEntry ();
1091 if (NULL
== NewMenuEntry
) {
1092 return EFI_OUT_OF_RESOURCES
;
1095 NewFileContext
= (FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1096 NewFileContext
->DeviceHandle
= DeviceHandle
;
1097 NewFileContext
->FileName
= LibAppendFileName (FileName
, DirInfo
->FileName
);
1098 NewFileContext
->FileHandle
= FileHandle
;
1099 NewFileContext
->DevicePath
= FileDevicePath (NewFileContext
->DeviceHandle
, NewFileContext
->FileName
);
1100 NewMenuEntry
->HelpString
= NULL
;
1101 NewFileContext
->IsDir
= (BOOLEAN
) ((DirInfo
->Attribute
& EFI_FILE_DIRECTORY
) == EFI_FILE_DIRECTORY
);
1103 if (NewFileContext
->IsDir
) {
1104 BufferSize
= StrLen (DirInfo
->FileName
) * 2 + 6;
1105 NewMenuEntry
->DisplayString
= AllocateZeroPool (BufferSize
);
1107 NewMenuEntry
->DisplayString
,
1113 NewMenuEntry
->DisplayString
= LibStrDuplicate (DirInfo
->FileName
);
1116 NewMenuEntry
->DisplayStringToken
= HiiSetString (
1117 gFileExplorerPrivate
.FeHiiHandle
,
1119 NewMenuEntry
->DisplayString
,
1123 NewFileContext
->IsRoot
= FALSE
;
1126 InsertTailList (&gFileExplorerPrivate
.FsOptionMenu
->Head
, &NewMenuEntry
->Link
);
1130 gFileExplorerPrivate
.FsOptionMenu
->MenuNumber
= OptionNumber
;
1138 Refresh the global UpdateData structure.
1142 LibRefreshUpdateData (
1147 // Free current updated date
1149 if (mLibStartOpCodeHandle
!= NULL
) {
1150 HiiFreeOpCodeHandle (mLibStartOpCodeHandle
);
1152 if (mLibEndOpCodeHandle
!= NULL
) {
1153 HiiFreeOpCodeHandle (mLibEndOpCodeHandle
);
1157 // Create new OpCode Handle
1159 mLibStartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1160 mLibEndOpCodeHandle
= HiiAllocateOpCodeHandle ();
1163 // Create Hii Extend Label OpCode as the start opcode
1165 mLibStartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1166 mLibStartOpCodeHandle
,
1169 sizeof (EFI_IFR_GUID_LABEL
)
1171 mLibStartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1173 mLibStartLabel
->Number
= FORM_FILE_EXPLORER_ID
;
1176 // Create Hii Extend Label OpCode as the start opcode
1178 mLibEndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
1179 mLibEndOpCodeHandle
,
1182 sizeof (EFI_IFR_GUID_LABEL
)
1184 mLibEndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1186 mLibEndLabel
->Number
= LABEL_END
;
1191 Update the File Explore page.
1195 LibUpdateFileExplorePage (
1200 MENU_ENTRY
*NewMenuEntry
;
1201 FILE_CONTEXT
*NewFileContext
;
1202 MENU_OPTION
*MenuOption
;
1204 NewMenuEntry
= NULL
;
1205 NewFileContext
= NULL
;
1207 LibRefreshUpdateData ();
1208 MenuOption
= gFileExplorerPrivate
.FsOptionMenu
;
1210 for (Index
= 0; Index
< MenuOption
->MenuNumber
; Index
++) {
1211 NewMenuEntry
= LibGetMenuEntry (MenuOption
, Index
);
1212 NewFileContext
= (FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1214 if (!NewFileContext
->IsDir
) {
1216 // Create Text opcode for directory, also create Text opcode for file in FileExplorerStateBootFromFile.
1218 HiiCreateActionOpCode (
1219 mLibStartOpCodeHandle
,
1220 (UINT16
) (FILE_OPTION_OFFSET
+ Index
),
1221 NewMenuEntry
->DisplayStringToken
,
1222 STRING_TOKEN (STR_NULL_STRING
),
1223 EFI_IFR_FLAG_CALLBACK
,
1228 // Create Goto opcode for file in FileExplorerStateAddBootOption or FileExplorerStateAddDriverOptionState.
1230 HiiCreateGotoOpCode (
1231 mLibStartOpCodeHandle
,
1232 FORM_FILE_EXPLORER_ID
,
1233 NewMenuEntry
->DisplayStringToken
,
1234 STRING_TOKEN (STR_NULL_STRING
),
1235 EFI_IFR_FLAG_CALLBACK
,
1236 (UINT16
) (FILE_OPTION_OFFSET
+ Index
)
1242 gFileExplorerPrivate
.FeHiiHandle
,
1244 FORM_FILE_EXPLORER_ID
,
1245 mLibStartOpCodeHandle
, // Label FORM_FILE_EXPLORER_ID
1246 mLibEndOpCodeHandle
// LABEL_END
1251 Update the file explower page with the refershed file system.
1253 @param KeyValue Key value to identify the type of data to expect.
1255 @retval EFI_SUCCESS Update the file explorer form success.
1256 @retval other errors Error occur when parse one directory.
1260 LibUpdateFileExplorer (
1264 UINT16 FileOptionMask
;
1265 MENU_ENTRY
*NewMenuEntry
;
1266 FILE_CONTEXT
*NewFileContext
;
1268 EFI_FILE_HANDLE FileHandle
;
1270 Status
= EFI_SUCCESS
;
1271 FileOptionMask
= (UINT16
) (FILE_OPTION_MASK
& KeyValue
);
1272 NewMenuEntry
= LibGetMenuEntry (gFileExplorerPrivate
.FsOptionMenu
, FileOptionMask
);
1273 NewFileContext
= (FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1275 if (NewFileContext
->IsDir
) {
1276 RemoveEntryList (&NewMenuEntry
->Link
);
1277 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1278 LibGetFileHandleFromMenu (NewMenuEntry
, &FileHandle
);
1279 Status
= LibFindFiles (FileHandle
, NewFileContext
->FileName
, NewFileContext
->DeviceHandle
);
1280 if (!EFI_ERROR (Status
)) {
1281 LibUpdateFileExplorePage ();
1283 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1285 LibDestroyMenuEntry (NewMenuEntry
);
1292 Get the device path info saved in the menu structure.
1294 @param KeyValue Key value to identify the type of data to expect.
1302 UINT16 FileOptionMask
;
1303 MENU_ENTRY
*NewMenuEntry
;
1304 FILE_CONTEXT
*NewFileContext
;
1306 FileOptionMask
= (UINT16
) (FILE_OPTION_MASK
& KeyValue
);
1308 NewMenuEntry
= LibGetMenuEntry (gFileExplorerPrivate
.FsOptionMenu
, FileOptionMask
);
1310 NewFileContext
= (FILE_CONTEXT
*) NewMenuEntry
->VariableContext
;
1312 if (gFileExplorerPrivate
.RetDevicePath
!= NULL
) {
1313 FreePool (gFileExplorerPrivate
.RetDevicePath
);
1315 gFileExplorerPrivate
.RetDevicePath
= DuplicateDevicePath (NewFileContext
->DevicePath
);
1319 Choose a file in the specified directory.
1321 If user input NULL for the RootDirectory, will choose file in the system.
1323 If user input *File != NULL, function will return the allocate device path
1324 info for the choosed file, caller has to free the memory after use it.
1326 @param RootDirectory Pointer to the root directory.
1327 @param FileType The file type need to choose.
1328 @param ChooseHandler Function pointer to the extra task need to do
1329 after choose one file.
1330 @param File Return the device path for the last time chosed file.
1332 @retval EFI_SUCESS Choose file success.
1333 @retval EFI_INVALID_PARAMETER Both ChooseHandler and return device path are NULL
1334 One of them must not NULL.
1335 @retval Other errors Choose file failed.
1340 IN EFI_DEVICE_PATH_PROTOCOL
*RootDirectory
,
1341 IN CHAR16
*FileType
, OPTIONAL
1342 IN CHOOSE_HANDLER ChooseHandler
, OPTIONAL
1343 OUT EFI_DEVICE_PATH_PROTOCOL
**File OPTIONAL
1346 EFI_FILE_HANDLE FileHandle
;
1349 EFI_HANDLE DeviceHandle
;
1351 if ((ChooseHandler
== NULL
) && (File
== NULL
)) {
1352 return EFI_INVALID_PARAMETER
;
1357 gFileExplorerPrivate
.RetDevicePath
= NULL
;
1358 gFileExplorerPrivate
.ChooseHandler
= ChooseHandler
;
1359 if (FileType
!= NULL
) {
1360 gFileExplorerPrivate
.FileType
= AllocateCopyPool (StrSize (FileType
), FileType
);
1361 LibToLowerString(gFileExplorerPrivate
.FileType
);
1363 gFileExplorerPrivate
.FileType
= NULL
;
1366 if (RootDirectory
== NULL
) {
1367 Status
= LibFindFileSystem();
1369 Status
= LibGetFileHandleFromDevicePath(RootDirectory
, &FileHandle
, &FileName
, &DeviceHandle
);
1370 if (EFI_ERROR (Status
)) {
1374 Status
= LibFindFiles (FileHandle
, FileName
, DeviceHandle
);
1376 if (EFI_ERROR (Status
)) {
1380 LibUpdateFileExplorePage();
1382 gFileExplorerPrivate
.FormBrowser2
->SendForm (
1383 gFileExplorerPrivate
.FormBrowser2
,
1384 &gFileExplorerPrivate
.FeHiiHandle
,
1393 if ((Status
== EFI_SUCCESS
) && (File
!= NULL
)) {
1394 *File
= gFileExplorerPrivate
.RetDevicePath
;
1395 } else if (gFileExplorerPrivate
.RetDevicePath
!= NULL
) {
1396 FreePool (gFileExplorerPrivate
.RetDevicePath
);
1399 if (gFileExplorerPrivate
.FileType
!= NULL
) {
1400 FreePool (gFileExplorerPrivate
.FileType
);
1403 LibFreeMenu (gFileExplorerPrivate
.FsOptionMenu
);
1405 if (FileName
!= NULL
) {
1406 FreePool (FileName
);
1414 Install Boot Manager Menu driver.
1416 @param ImageHandle The image handle.
1417 @param SystemTable The system table.
1419 @retval EFI_SUCEESS Install File explorer library success.
1424 FileExplorerLibConstructor (
1425 IN EFI_HANDLE ImageHandle
,
1426 IN EFI_SYSTEM_TABLE
*SystemTable
1431 gHiiVendorDevicePath
= (HII_VENDOR_DEVICE_PATH
*) DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL
*)&FeHiiVendorDevicePath
);
1432 ASSERT (gHiiVendorDevicePath
!= NULL
);
1433 CopyGuid (&gHiiVendorDevicePath
->VendorDevicePath
.Guid
, &gEfiCallerIdGuid
);
1436 // Install Device Path Protocol and Config Access protocol to driver handle
1438 Status
= gBS
->InstallMultipleProtocolInterfaces (
1439 &gFileExplorerPrivate
.FeDriverHandle
,
1440 &gEfiDevicePathProtocolGuid
,
1441 gHiiVendorDevicePath
,
1442 &gEfiHiiConfigAccessProtocolGuid
,
1443 &gFileExplorerPrivate
.FeConfigAccess
,
1446 ASSERT_EFI_ERROR (Status
);
1449 // Post our File Explorer VFR binary to the HII database.
1451 gFileExplorerPrivate
.FeHiiHandle
= HiiAddPackages (
1453 gFileExplorerPrivate
.FeDriverHandle
,
1455 FileExplorerLibStrings
,
1458 ASSERT (gFileExplorerPrivate
.FeHiiHandle
!= NULL
);
1461 // Locate Formbrowser2 protocol
1463 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &gFileExplorerPrivate
.FormBrowser2
);
1464 ASSERT_EFI_ERROR (Status
);
1466 InitializeListHead (&gFileExplorerPrivate
.FsOptionMenu
->Head
);
1472 Unloads the application and its installed protocol.
1474 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1475 @param[in] SystemTable The system table.
1477 @retval EFI_SUCCESS The image has been unloaded.
1481 FileExplorerLibDestructor (
1482 IN EFI_HANDLE ImageHandle
,
1483 IN EFI_SYSTEM_TABLE
*SystemTable
1488 ASSERT (gHiiVendorDevicePath
!= NULL
);
1490 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1491 gFileExplorerPrivate
.FeDriverHandle
,
1492 &gEfiDevicePathProtocolGuid
,
1493 gHiiVendorDevicePath
,
1494 &gEfiHiiConfigAccessProtocolGuid
,
1495 &gFileExplorerPrivate
.FeConfigAccess
,
1498 ASSERT_EFI_ERROR (Status
);
1500 HiiRemovePackages (gFileExplorerPrivate
.FeHiiHandle
);
1502 FreePool (gHiiVendorDevicePath
);