2 Member functions of EFI_SHELL_PROTOCOL and functions for creation,
3 manipulation, and initialization of EFI_SHELL_PROTOCOL.
5 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/FileHandleLib.h>
20 Close an open file handle.
22 This function closes a specified file handle. All "dirty" cached file data is
23 flushed to the device, and the file is closed. In all cases the handle is
26 @param[in] FileHandle The file handle to close.
28 @retval EFI_SUCCESS The file handle was closed successfully.
33 IN SHELL_FILE_HANDLE FileHandle
36 ShellFileHandleRemove(FileHandle
);
37 return (FileHandleClose(ConvertShellHandleToEfiFileProtocol(FileHandle
)));
41 Internal worker to determine whether there is a file system somewhere
42 upon the device path specified.
44 @param[in] DevicePath The device path to test.
46 @retval TRUE gEfiSimpleFileSystemProtocolGuid was installed on a handle with this device path
47 @retval FALSE gEfiSimpleFileSystemProtocolGuid was not found.
51 InternalShellProtocolIsSimpleFileSystemPresent(
52 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
55 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
61 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
62 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
64 if ((Handle
!= NULL
) && (!EFI_ERROR(Status
))) {
71 Internal worker debug helper function to print out maps as they are added.
73 @param[in] Mapping string mapping that has been added
74 @param[in] DevicePath pointer to device path that has been mapped.
76 @retval EFI_SUCCESS the operation was successful.
77 @return other an error ocurred
84 InternalShellProtocolDebugPrintMessage (
85 IN CONST CHAR16
*Mapping
,
86 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
89 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevicePathToText
;
95 DevicePathToText
= NULL
;
97 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
,
99 (VOID
**)&DevicePathToText
);
100 if (Mapping
!= NULL
) {
101 DEBUG((EFI_D_INFO
, "Added new map item:\"%S\"\r\n", Mapping
));
103 if (!EFI_ERROR(Status
)) {
104 if (DevicePath
!= NULL
) {
105 Temp
= DevicePathToText
->ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
106 DEBUG((EFI_D_INFO
, "DevicePath: %S\r\n", Temp
));
115 This function creates a mapping for a device path.
117 If both DeviecPath and Mapping are NULL, this will reset the mapping to default values.
119 @param DevicePath Points to the device path. If this is NULL and Mapping points to a valid mapping,
120 then the mapping will be deleted.
121 @param Mapping Points to the NULL-terminated mapping for the device path. Must end with a ':'
123 @retval EFI_SUCCESS Mapping created or deleted successfully.
124 @retval EFI_NO_MAPPING There is no handle that corresponds exactly to DevicePath. See the
125 boot service function LocateDevicePath().
126 @retval EFI_ACCESS_DENIED The mapping is a built-in alias.
127 @retval EFI_INVALID_PARAMETER Mapping was NULL
128 @retval EFI_INVALID_PARAMETER Mapping did not end with a ':'
129 @retval EFI_INVALID_PARAMETER DevicePath was not pointing at a device that had a SIMPLE_FILE_SYSTEM_PROTOCOL installed.
130 @retval EFI_NOT_FOUND There was no mapping found to delete
131 @retval EFI_OUT_OF_RESOURCES Memory allocation failed
136 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath OPTIONAL
,
137 IN CONST CHAR16
*Mapping
141 SHELL_MAP_LIST
*MapListNode
;
143 if (Mapping
== NULL
){
144 return (EFI_INVALID_PARAMETER
);
147 if (Mapping
[StrLen(Mapping
)-1] != ':') {
148 return (EFI_INVALID_PARAMETER
);
152 // Delete the mapping
154 if (DevicePath
== NULL
) {
155 if (IsListEmpty(&gShellMapList
.Link
)) {
156 return (EFI_NOT_FOUND
);
158 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
159 ; !IsNull(&gShellMapList
.Link
, &MapListNode
->Link
)
160 ; MapListNode
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListNode
->Link
)
162 if (StringNoCaseCompare(&MapListNode
->MapName
, &Mapping
) == 0) {
163 RemoveEntryList(&MapListNode
->Link
);
164 FreePool(MapListNode
);
165 return (EFI_SUCCESS
);
170 // We didnt find one to delete
172 return (EFI_NOT_FOUND
);
176 // make sure this is a valid to add device path
178 ///@todo add BlockIo to this test...
179 if (!InternalShellProtocolIsSimpleFileSystemPresent(DevicePath
)) {
180 return (EFI_INVALID_PARAMETER
);
184 // First make sure there is no old mapping
186 Status
= EfiShellSetMap(NULL
, Mapping
);
187 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_FOUND
)) {
192 // now add the new one.
194 Status
= ShellCommandAddMapItemAndUpdatePath(Mapping
, DevicePath
, 0, FALSE
);
200 Gets the device path from the mapping.
202 This function gets the device path associated with a mapping.
204 @param Mapping A pointer to the mapping
206 @retval !=NULL Pointer to the device path that corresponds to the
207 device mapping. The returned pointer does not need
209 @retval NULL There is no device path associated with the
212 CONST EFI_DEVICE_PATH_PROTOCOL
*
214 EfiShellGetDevicePathFromMap(
215 IN CONST CHAR16
*Mapping
218 SHELL_MAP_LIST
*MapListItem
;
225 StrnCatGrow(&NewName
, &Size
, Mapping
, 0);
226 if (Mapping
[StrLen(Mapping
)-1] != L
':') {
227 StrnCatGrow(&NewName
, &Size
, L
":", 0);
230 MapListItem
= ShellCommandFindMapItem(NewName
);
234 if (MapListItem
!= NULL
) {
235 return (MapListItem
->DevicePath
);
241 Gets the mapping(s) that most closely matches the device path.
243 This function gets the mapping which corresponds to the device path *DevicePath. If
244 there is no exact match, then the mapping which most closely matches *DevicePath
245 is returned, and *DevicePath is updated to point to the remaining portion of the
246 device path. If there is an exact match, the mapping is returned and *DevicePath
247 points to the end-of-device-path node.
249 If there are multiple map names they will be semi-colon seperated in the
250 NULL-terminated string.
252 @param DevicePath On entry, points to a device path pointer. On
253 exit, updates the pointer to point to the
254 portion of the device path after the mapping.
256 @retval NULL No mapping was found.
257 @return !=NULL Pointer to NULL-terminated mapping. The buffer
258 is callee allocated and should be freed by the caller.
262 EfiShellGetMapFromDevicePath(
263 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
266 SHELL_MAP_LIST
*Node
;
267 CHAR16
*PathForReturn
;
269 // EFI_HANDLE PathHandle;
270 // EFI_HANDLE MapHandle;
271 // EFI_STATUS Status;
272 // EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
273 // EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
275 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
279 PathForReturn
= NULL
;
282 for ( Node
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
283 ; !IsNull(&gShellMapList
.Link
, &Node
->Link
)
284 ; Node
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &Node
->Link
)
287 // check for exact match
289 if (DevicePathCompare(DevicePath
, &Node
->DevicePath
) == 0) {
290 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
292 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, L
";", 0);
294 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, Node
->MapName
, 0);
297 if (PathForReturn
!= NULL
) {
298 while (!IsDevicePathEndType (*DevicePath
)) {
299 *DevicePath
= NextDevicePathNode (*DevicePath
);
301 SetDevicePathEndNode (*DevicePath
);
304 ///@todo finish code for inexact matches.
305 if (PathForReturn == NULL) {
308 DevicePathCopy = DuplicateDevicePath(*DevicePath);
309 ASSERT(DevicePathCopy != NULL);
310 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
311 ASSERT_EFI_ERROR(Status);
313 // check each of the device paths we have to get the root of the path for consist mappings
315 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
316 ; !IsNull(&gShellMapList.Link, &Node->Link)
317 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
319 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) == 0) {
322 MapPathCopy = DuplicateDevicePath(Node->DevicePath);
323 ASSERT(MapPathCopy != NULL);
324 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
325 if (MapHandle == PathHandle) {
327 *DevicePath = DevicePathCopy;
330 DevicePathCopy = NULL;
331 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
332 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
337 // now add on the non-consistent mappings
339 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
340 ; !IsNull(&gShellMapList.Link, &Node->Link)
341 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
343 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) != 0) {
346 MapPathCopy = Node->DevicePath;
347 ASSERT(MapPathCopy != NULL);
348 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
349 if (MapHandle == PathHandle) {
350 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
351 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
358 return (AddBufferToFreeList(PathForReturn
));
362 Converts a device path to a file system-style path.
364 This function converts a device path to a file system path by replacing part, or all, of
365 the device path with the file-system mapping. If there are more than one application
366 file system mappings, the one that most closely matches Path will be used.
368 @param Path The pointer to the device path
370 @retval NULL the device path could not be found.
371 @return all The pointer of the NULL-terminated file path. The path
372 is callee-allocated and should be freed by the caller.
376 EfiShellGetFilePathFromDevicePath(
377 IN CONST EFI_DEVICE_PATH_PROTOCOL
*Path
380 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
381 EFI_DEVICE_PATH_PROTOCOL
*MapPathCopy
;
382 SHELL_MAP_LIST
*MapListItem
;
383 CHAR16
*PathForReturn
;
385 EFI_HANDLE PathHandle
;
386 EFI_HANDLE MapHandle
;
388 FILEPATH_DEVICE_PATH
*FilePath
;
389 FILEPATH_DEVICE_PATH
*AlignedNode
;
391 PathForReturn
= NULL
;
394 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)Path
;
395 ASSERT(DevicePathCopy
!= NULL
);
396 if (DevicePathCopy
== NULL
) {
400 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &PathHandle
);
402 if (EFI_ERROR(Status
)) {
406 // check each of the device paths we have to get the root of the path
408 for ( MapListItem
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
409 ; !IsNull(&gShellMapList
.Link
, &MapListItem
->Link
)
410 ; MapListItem
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListItem
->Link
)
412 MapPathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)MapListItem
->DevicePath
;
413 ASSERT(MapPathCopy
!= NULL
);
415 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &MapPathCopy
, &MapHandle
);
416 if (MapHandle
== PathHandle
) {
417 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
418 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, MapListItem
->MapName
, 0);
420 // go through all the remaining nodes in the device path
422 for ( FilePath
= (FILEPATH_DEVICE_PATH
*)DevicePathCopy
423 ; !IsDevicePathEnd (&FilePath
->Header
)
424 ; FilePath
= (FILEPATH_DEVICE_PATH
*)NextDevicePathNode (&FilePath
->Header
)
427 // all the rest should be file path nodes
429 if ((DevicePathType(&FilePath
->Header
) != MEDIA_DEVICE_PATH
) ||
430 (DevicePathSubType(&FilePath
->Header
) != MEDIA_FILEPATH_DP
)) {
431 FreePool(PathForReturn
);
432 PathForReturn
= NULL
;
436 // append the path part onto the filepath.
438 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
439 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, L
"\\", 1);
441 AlignedNode
= AllocateCopyPool (DevicePathNodeLength(FilePath
), FilePath
);
442 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, AlignedNode
->PathName
, 0);
443 FreePool(AlignedNode
);
445 } // for loop of remaining nodes
447 if (PathForReturn
!= NULL
) {
450 } // for loop of paths to check
451 return(PathForReturn
);
455 Converts a file system style name to a device path.
457 This function converts a file system style name to a device path, by replacing any
458 mapping references to the associated device path.
460 @param Path the pointer to the path
462 @return all The pointer of the file path. The file path is callee
463 allocated and should be freed by the caller.
465 EFI_DEVICE_PATH_PROTOCOL
*
467 EfiShellGetDevicePathFromFilePath(
468 IN CONST CHAR16
*Path
475 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
476 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
477 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopyForFree
;
478 EFI_DEVICE_PATH_PROTOCOL
*DevicePathForReturn
;
489 if (StrStr(Path
, L
":") == NULL
) {
490 Cwd
= EfiShellGetCurDir(NULL
);
495 Size
+= StrSize(Path
);
496 NewPath
= AllocateZeroPool(Size
);
497 ASSERT(NewPath
!= NULL
);
498 StrCpy(NewPath
, Cwd
);
499 if ((Path
[0] == (CHAR16
)L
'\\') &&
500 (NewPath
[StrLen(NewPath
)-1] == (CHAR16
)L
'\\')
502 ((CHAR16
*)NewPath
)[StrLen(NewPath
)-1] = CHAR_NULL
;
504 StrCat(NewPath
, Path
);
505 DevicePathForReturn
= EfiShellGetDevicePathFromFilePath(NewPath
);
507 return (DevicePathForReturn
);
512 // find the part before (but including) the : for the map name
514 ASSERT((MapName
== NULL
&& Size
== 0) || (MapName
!= NULL
));
515 MapName
= StrnCatGrow(&MapName
, &Size
, Path
, (StrStr(Path
, L
":")-Path
+1));
516 if (MapName
[StrLen(MapName
)-1] != L
':') {
522 // look up the device path in the map
524 DevicePath
= EfiShellGetDevicePathFromMap(MapName
);
525 if (DevicePath
== NULL
) {
527 // Must have been a bad Mapname
533 // make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
535 DevicePathCopyForFree
= DevicePathCopy
= DuplicateDevicePath(DevicePath
);
536 if (DevicePathCopy
== NULL
) {
546 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
547 if (EFI_ERROR(Status
)) {
548 if (DevicePathCopyForFree
!= NULL
) {
549 FreePool(DevicePathCopyForFree
);
556 // build the full device path
558 DevicePathForReturn
= FileDevicePath(Handle
, Path
+StrLen(MapName
)+1);
561 if (DevicePathCopyForFree
!= NULL
) {
562 FreePool(DevicePathCopyForFree
);
565 return (DevicePathForReturn
);
569 Gets the name of the device specified by the device handle.
571 This function gets the user-readable name of the device specified by the device
572 handle. If no user-readable name could be generated, then *BestDeviceName will be
573 NULL and EFI_NOT_FOUND will be returned.
575 If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
576 device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
579 If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
580 device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
581 If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
582 EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
583 EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
585 @param DeviceHandle The handle of the device.
586 @param Flags Determines the possible sources of component names.
588 EFI_DEVICE_NAME_USE_COMPONENT_NAME
589 EFI_DEVICE_NAME_USE_DEVICE_PATH
590 @param Language A pointer to the language specified for the device
591 name, in the same format as described in the UEFI
592 specification, Appendix M
593 @param BestDeviceName On return, points to the callee-allocated NULL-
594 terminated name of the device. If no device name
595 could be found, points to NULL. The name must be
596 freed by the caller...
598 @retval EFI_SUCCESS Get the name successfully.
599 @retval EFI_NOT_FOUND Fail to get the device name.
600 @retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
601 @retval EFI_INVALID_PARAMETER BestDeviceName was NULL
602 @retval EFI_INVALID_PARAMETER DeviceHandle was NULL
606 EfiShellGetDeviceName(
607 IN EFI_HANDLE DeviceHandle
,
608 IN EFI_SHELL_DEVICE_NAME_FLAGS Flags
,
610 OUT CHAR16
**BestDeviceName
614 EFI_COMPONENT_NAME2_PROTOCOL
*CompName2
;
615 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevicePathToText
;
616 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
617 EFI_HANDLE
*HandleList
;
620 CHAR16
*DeviceNameToReturn
;
624 if (BestDeviceName
== NULL
||
627 return (EFI_INVALID_PARAMETER
);
631 // make sure one of the 2 supported bits is on
633 if (((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) == 0) &&
634 ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) == 0)) {
635 return (EFI_INVALID_PARAMETER
);
638 DeviceNameToReturn
= NULL
;
639 *BestDeviceName
= NULL
;
644 if ((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) != 0) {
645 Status
= ParseHandleDatabaseByRelationship(
648 HR_DRIVER_BINDING_HANDLE
|HR_DEVICE_DRIVER
,
651 for (LoopVar
= 0; LoopVar
< HandleCount
; LoopVar
++){
653 // Go through those handles until we get one that passes for GetComponentName
655 Status
= gBS
->OpenProtocol(
657 &gEfiComponentName2ProtocolGuid
,
661 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
662 if (EFI_ERROR(Status
)) {
663 Status
= gBS
->OpenProtocol(
665 &gEfiComponentNameProtocolGuid
,
669 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
672 if (EFI_ERROR(Status
)) {
675 if (Language
== NULL
) {
676 Lang
= AllocatePool(AsciiStrSize(CompName2
->SupportedLanguages
));
678 return (EFI_OUT_OF_RESOURCES
);
680 AsciiStrCpy(Lang
, CompName2
->SupportedLanguages
);
681 TempChar
= AsciiStrStr(Lang
, ";");
682 if (TempChar
!= NULL
){
683 *TempChar
= CHAR_NULL
;
686 Lang
= AllocatePool(AsciiStrSize(Language
));
688 return (EFI_OUT_OF_RESOURCES
);
690 AsciiStrCpy(Lang
, Language
);
692 Status
= CompName2
->GetControllerName(CompName2
, DeviceHandle
, NULL
, Lang
, &DeviceNameToReturn
);
695 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
699 if (HandleList
!= NULL
) {
700 FreePool(HandleList
);
702 if (DeviceNameToReturn
!= NULL
){
703 ASSERT(BestDeviceName
!= NULL
);
704 StrnCatGrow(BestDeviceName
, NULL
, DeviceNameToReturn
, 0);
705 return (EFI_SUCCESS
);
708 // dont return on fail since we will try device path if that bit is on
711 if ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) != 0) {
712 Status
= gBS
->LocateProtocol(
713 &gEfiDevicePathToTextProtocolGuid
,
715 (VOID
**)&DevicePathToText
);
717 // we now have the device path to text protocol
719 if (!EFI_ERROR(Status
)) {
720 Status
= gBS
->OpenProtocol(
722 &gEfiDevicePathProtocolGuid
,
726 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
727 if (!EFI_ERROR(Status
)) {
729 // use device path to text on the device path
731 *BestDeviceName
= DevicePathToText
->ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
732 return (EFI_SUCCESS
);
737 // none of the selected bits worked.
739 return (EFI_NOT_FOUND
);
743 Opens the root directory of a device on a handle
745 This function opens the root directory of a device and returns a file handle to it.
747 @param DeviceHandle The handle of the device that contains the volume.
748 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
751 @retval EFI_SUCCESS Root opened successfully.
752 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
754 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
755 @retval EFI_DEVICE_ERROR The device had an error
759 EfiShellOpenRootByHandle(
760 IN EFI_HANDLE DeviceHandle
,
761 OUT SHELL_FILE_HANDLE
*FileHandle
765 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*SimpleFileSystem
;
766 EFI_FILE_PROTOCOL
*RealFileHandle
;
767 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
770 // get the simple file system interface
772 Status
= gBS
->OpenProtocol(DeviceHandle
,
773 &gEfiSimpleFileSystemProtocolGuid
,
774 (VOID
**)&SimpleFileSystem
,
777 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
778 if (EFI_ERROR(Status
)) {
779 return (EFI_NOT_FOUND
);
782 Status
= gBS
->OpenProtocol(DeviceHandle
,
783 &gEfiDevicePathProtocolGuid
,
787 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
788 if (EFI_ERROR(Status
)) {
789 return (EFI_NOT_FOUND
);
792 // Open the root volume now...
794 Status
= SimpleFileSystem
->OpenVolume(SimpleFileSystem
, &RealFileHandle
);
795 *FileHandle
= ConvertEfiFileProtocolToShellHandle(RealFileHandle
, EfiShellGetMapFromDevicePath(&DevPath
));
800 Opens the root directory of a device.
802 This function opens the root directory of a device and returns a file handle to it.
804 @param DevicePath Points to the device path corresponding to the device where the
805 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
806 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
809 @retval EFI_SUCCESS Root opened successfully.
810 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
812 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
813 @retval EFI_DEVICE_ERROR The device had an error
814 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
819 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
820 OUT SHELL_FILE_HANDLE
*FileHandle
826 if (FileHandle
== NULL
) {
827 return (EFI_INVALID_PARAMETER
);
831 // find the handle of the device with that device handle and the file system
834 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
837 if (EFI_ERROR(Status
)) {
838 return (EFI_NOT_FOUND
);
841 return (EfiShellOpenRootByHandle(Handle
, FileHandle
));
845 Returns whether any script files are currently being processed.
847 @retval TRUE There is at least one script file active.
848 @retval FALSE No script files are active now.
853 EfiShellBatchIsActive (
857 if (ShellCommandGetCurrentScriptFile() == NULL
) {
864 Worker function to open a file based on a device path. this will open the root
865 of the volume and then traverse down to the file itself.
867 @param DevicePath Device Path of the file.
868 @param FileHandle Pointer to the file upon a successful return.
869 @param OpenMode mode to open file in.
870 @param Attributes the File Attributes to use when creating a new file.
872 @retval EFI_SUCCESS the file is open and FileHandle is valid
873 @retval EFI_UNSUPPORTED the device path cotained non-path elements
874 @retval other an error ocurred.
878 InternalOpenFileDevicePath(
879 IN OUT EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
880 OUT SHELL_FILE_HANDLE
*FileHandle
,
882 IN UINT64 Attributes OPTIONAL
886 FILEPATH_DEVICE_PATH
*FilePathNode
;
888 SHELL_FILE_HANDLE ShellHandle
;
889 EFI_FILE_PROTOCOL
*Handle1
;
890 EFI_FILE_PROTOCOL
*Handle2
;
891 EFI_DEVICE_PATH_PROTOCOL
*DpCopy
;
892 FILEPATH_DEVICE_PATH
*AlignedNode
;
894 if (FileHandle
== NULL
) {
895 return (EFI_INVALID_PARAMETER
);
906 Status
= EfiShellOpenRoot(DevicePath
, &ShellHandle
);
908 if (!EFI_ERROR(Status
)) {
909 Handle1
= ConvertShellHandleToEfiFileProtocol(ShellHandle
);
911 // chop off the begining part before the file system part...
914 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
917 if (!EFI_ERROR(Status
)) {
919 // To access as a file system, the file path should only
920 // contain file path components. Follow the file path nodes
921 // and find the target file
923 for ( FilePathNode
= (FILEPATH_DEVICE_PATH
*)DevicePath
924 ; !IsDevicePathEnd (&FilePathNode
->Header
)
925 ; FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
)
927 SHELL_FREE_NON_NULL(AlignedNode
);
928 AlignedNode
= AllocateCopyPool (DevicePathNodeLength(FilePathNode
), FilePathNode
);
930 // For file system access each node should be a file path component
932 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
933 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
935 Status
= EFI_UNSUPPORTED
;
940 // Open this file path node
946 // if this is the last node in the DevicePath always create (if that was requested).
948 if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode
->Header
)))) {
949 Status
= Handle2
->Open (
952 AlignedNode
->PathName
,
959 // This is not the last node and we dont want to 'create' existing
960 // directory entries...
964 // open without letting it create
965 // prevents error on existing files/directories
967 Status
= Handle2
->Open (
970 AlignedNode
->PathName
,
971 OpenMode
&~EFI_FILE_MODE_CREATE
,
975 // if above failed now open and create the 'item'
976 // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)
978 if ((EFI_ERROR (Status
)) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)) {
979 Status
= Handle2
->Open (
982 AlignedNode
->PathName
,
989 // Close the last node
991 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Handle2
);
994 // If there's been an error, stop
996 if (EFI_ERROR (Status
)) {
1002 SHELL_FREE_NON_NULL(AlignedNode
);
1003 if (EFI_ERROR(Status
)) {
1004 if (Handle1
!= NULL
) {
1005 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(Handle1
);
1008 *FileHandle
= ConvertEfiFileProtocolToShellHandle(Handle1
, ShellFileHandleGetPath(ShellHandle
));
1014 Creates a file or directory by name.
1016 This function creates an empty new file or directory with the specified attributes and
1017 returns the new file's handle. If the file already exists and is read-only, then
1018 EFI_INVALID_PARAMETER will be returned.
1020 If the file already existed, it is truncated and its attributes updated. If the file is
1021 created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.
1023 If the file name begins with >v, then the file handle which is returned refers to the
1024 shell environment variable with the specified name. If the shell environment variable
1025 already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.
1027 @param FileName Pointer to NULL-terminated file path
1028 @param FileAttribs The new file's attrbiutes. the different attributes are
1029 described in EFI_FILE_PROTOCOL.Open().
1030 @param FileHandle On return, points to the created file handle or directory's handle
1032 @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.
1033 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1034 @retval EFI_UNSUPPORTED could not open the file path
1035 @retval EFI_NOT_FOUND the specified file could not be found on the devide, or could not
1036 file the file system on the device.
1037 @retval EFI_NO_MEDIA the device has no medium.
1038 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1040 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1042 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1043 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1044 when the media is write-protected.
1045 @retval EFI_ACCESS_DENIED The service denied access to the file.
1046 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
1047 @retval EFI_VOLUME_FULL The volume is full.
1052 IN CONST CHAR16
*FileName
,
1053 IN UINT64 FileAttribs
,
1054 OUT SHELL_FILE_HANDLE
*FileHandle
1057 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1061 // Is this for an environment variable
1062 // do we start with >v
1064 if (StrStr(FileName
, L
">v") == FileName
) {
1065 if (!IsVolatileEnv(FileName
+2)) {
1066 return (EFI_INVALID_PARAMETER
);
1068 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1069 return (EFI_SUCCESS
);
1073 // We are opening a regular file.
1075 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1076 if (DevicePath
== NULL
) {
1077 return (EFI_NOT_FOUND
);
1080 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, FileAttribs
); // 0 = no specific file attributes
1081 FreePool(DevicePath
);
1087 Opens a file or a directory by file name.
1089 This function opens the specified file in the specified OpenMode and returns a file
1091 If the file name begins with >v, then the file handle which is returned refers to the
1092 shell environment variable with the specified name. If the shell environment variable
1093 exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then
1094 EFI_INVALID_PARAMETER is returned.
1096 If the file name is >i, then the file handle which is returned refers to the standard
1097 input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER
1100 If the file name is >o, then the file handle which is returned refers to the standard
1101 output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1104 If the file name is >e, then the file handle which is returned refers to the standard
1105 error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1108 If the file name is NUL, then the file handle that is returned refers to the standard NUL
1109 file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is
1112 If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the
1115 @param FileName Points to the NULL-terminated UCS-2 encoded file name.
1116 @param FileHandle On return, points to the file handle.
1117 @param OpenMode File open mode. Either EFI_FILE_MODE_READ or
1118 EFI_FILE_MODE_WRITE from section 12.4 of the UEFI
1120 @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.
1121 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.
1122 @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.
1123 @retval EFI_NOT_FOUND The specified file could not be found on the device or the file
1124 system could not be found on the device. FileHandle is NULL.
1125 @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.
1126 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1127 longer supported. FileHandle is NULL.
1128 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1129 the FileName. FileHandle is NULL.
1130 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.
1131 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1132 when the media is write-protected. FileHandle is NULL.
1133 @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.
1134 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle
1136 @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.
1140 EfiShellOpenFileByName(
1141 IN CONST CHAR16
*FileName
,
1142 OUT SHELL_FILE_HANDLE
*FileHandle
,
1146 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1152 // Is this for StdIn
1154 if (StrCmp(FileName
, L
">i") == 0) {
1156 // make sure not writing to StdIn
1158 if ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0) {
1159 return (EFI_INVALID_PARAMETER
);
1161 *FileHandle
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1162 ASSERT(*FileHandle
!= NULL
);
1163 return (EFI_SUCCESS
);
1167 // Is this for StdOut
1169 if (StrCmp(FileName
, L
">o") == 0) {
1171 // make sure not writing to StdIn
1173 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1174 return (EFI_INVALID_PARAMETER
);
1176 *FileHandle
= &FileInterfaceStdOut
;
1177 return (EFI_SUCCESS
);
1181 // Is this for NUL file
1183 if (StrCmp(FileName
, L
"NUL") == 0) {
1184 *FileHandle
= &FileInterfaceNulFile
;
1185 return (EFI_SUCCESS
);
1189 // Is this for StdErr
1191 if (StrCmp(FileName
, L
">e") == 0) {
1193 // make sure not writing to StdIn
1195 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1196 return (EFI_INVALID_PARAMETER
);
1198 *FileHandle
= &FileInterfaceStdErr
;
1199 return (EFI_SUCCESS
);
1203 // Is this for an environment variable
1204 // do we start with >v
1206 if (StrStr(FileName
, L
">v") == FileName
) {
1207 if (!IsVolatileEnv(FileName
+2) &&
1208 ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0)) {
1209 return (EFI_INVALID_PARAMETER
);
1211 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1212 return (EFI_SUCCESS
);
1216 // We are opening a regular file.
1218 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1219 // DEBUG_CODE(InternalShellProtocolDebugPrintMessage (NULL, DevicePath););
1220 if (DevicePath
== NULL
) {
1221 return (EFI_NOT_FOUND
);
1225 // Copy the device path, open the file, then free the memory
1227 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, OpenMode
, 0); // 0 = no specific file attributes
1228 FreePool(DevicePath
);
1234 Deletes the file specified by the file name.
1236 This function deletes a file.
1238 @param FileName Points to the NULL-terminated file name.
1240 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.
1241 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
1242 @sa EfiShellCreateFile
1246 EfiShellDeleteFileByName(
1247 IN CONST CHAR16
*FileName
1250 SHELL_FILE_HANDLE FileHandle
;
1254 // get a handle to the file
1256 Status
= EfiShellCreateFile(FileName
,
1259 if (EFI_ERROR(Status
)) {
1263 // now delete the file
1265 return (ShellInfoObject
.NewEfiShellProtocol
->DeleteFile(FileHandle
));
1269 Disables the page break output mode.
1273 EfiShellDisablePageBreak (
1277 ShellInfoObject
.PageBreakEnabled
= FALSE
;
1281 Enables the page break output mode.
1285 EfiShellEnablePageBreak (
1289 ShellInfoObject
.PageBreakEnabled
= TRUE
;
1293 internal worker function to load and run an image via device path.
1295 @param ParentImageHandle A handle of the image that is executing the specified
1297 @param DevicePath device path of the file to execute
1298 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1299 containing the command line. If NULL then the command-
1301 @param Environment Points to a NULL-terminated array of environment
1302 variables with the format 'x=y', where x is the
1303 environment variable name and y is the value. If this
1304 is NULL, then the current shell environment is used.
1305 @param StatusCode Points to the status code returned by the command.
1307 @retval EFI_SUCCESS The command executed successfully. The status code
1308 returned by the command is pointed to by StatusCode.
1309 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1310 @retval EFI_OUT_OF_RESOURCES Out of resources.
1311 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1315 InternalShellExecuteDevicePath(
1316 IN CONST EFI_HANDLE
*ParentImageHandle
,
1317 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1318 IN CONST CHAR16
*CommandLine OPTIONAL
,
1319 IN CONST CHAR16
**Environment OPTIONAL
,
1320 OUT EFI_STATUS
*StatusCode OPTIONAL
1324 EFI_HANDLE NewHandle
;
1325 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1326 LIST_ENTRY OrigEnvs
;
1327 EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol
;
1329 if (ParentImageHandle
== NULL
) {
1330 return (EFI_INVALID_PARAMETER
);
1333 InitializeListHead(&OrigEnvs
);
1338 // Load the image with:
1339 // FALSE - not from boot manager and NULL, 0 being not already in memory
1341 Status
= gBS
->LoadImage(
1344 (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
,
1349 if (EFI_ERROR(Status
)) {
1350 if (NewHandle
!= NULL
) {
1351 gBS
->UnloadImage(NewHandle
);
1355 Status
= gBS
->OpenProtocol(
1357 &gEfiLoadedImageProtocolGuid
,
1358 (VOID
**)&LoadedImage
,
1361 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1363 if (!EFI_ERROR(Status
)) {
1364 ASSERT(LoadedImage
->LoadOptionsSize
== 0);
1365 if (CommandLine
!= NULL
) {
1366 LoadedImage
->LoadOptionsSize
= (UINT32
)StrSize(CommandLine
);
1367 LoadedImage
->LoadOptions
= (VOID
*)CommandLine
;
1371 // Save our current environment settings for later restoration if necessary
1373 if (Environment
!= NULL
) {
1374 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1375 if (!EFI_ERROR(Status
)) {
1376 Status
= SetEnvironmentVariables(Environment
);
1381 // Initialize and install a shell parameters protocol on the image.
1383 ShellParamsProtocol
.StdIn
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1384 ShellParamsProtocol
.StdOut
= ShellInfoObject
.NewShellParametersProtocol
->StdOut
;
1385 ShellParamsProtocol
.StdErr
= ShellInfoObject
.NewShellParametersProtocol
->StdErr
;
1386 Status
= UpdateArgcArgv(&ShellParamsProtocol
, CommandLine
, NULL
, NULL
);
1387 ASSERT_EFI_ERROR(Status
);
1388 Status
= gBS
->InstallProtocolInterface(&NewHandle
, &gEfiShellParametersProtocolGuid
, EFI_NATIVE_INTERFACE
, &ShellParamsProtocol
);
1389 ASSERT_EFI_ERROR(Status
);
1391 ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)
1394 // now start the image and if the caller wanted the return code pass it to them...
1396 if (!EFI_ERROR(Status
)) {
1397 if (StatusCode
!= NULL
) {
1398 *StatusCode
= gBS
->StartImage(NewHandle
, NULL
, NULL
);
1400 Status
= gBS
->StartImage(NewHandle
, NULL
, NULL
);
1405 // Cleanup (and dont overwrite errors)
1407 if (EFI_ERROR(Status
)) {
1408 gBS
->UninstallProtocolInterface(NewHandle
, &gEfiShellParametersProtocolGuid
, &ShellParamsProtocol
);
1410 Status
= gBS
->UninstallProtocolInterface(NewHandle
, &gEfiShellParametersProtocolGuid
, &ShellParamsProtocol
);
1411 ASSERT_EFI_ERROR(Status
);
1415 if (!IsListEmpty(&OrigEnvs
)) {
1416 if (EFI_ERROR(Status
)) {
1417 SetEnvironmentVariableList(&OrigEnvs
);
1419 Status
= SetEnvironmentVariableList(&OrigEnvs
);
1426 Execute the command line.
1428 This function creates a nested instance of the shell and executes the specified
1429 command (CommandLine) with the specified environment (Environment). Upon return,
1430 the status code returned by the specified command is placed in StatusCode.
1432 If Environment is NULL, then the current environment is used and all changes made
1433 by the commands executed will be reflected in the current environment. If the
1434 Environment is non-NULL, then the changes made will be discarded.
1436 The CommandLine is executed from the current working directory on the current
1439 @param ParentImageHandle A handle of the image that is executing the specified
1441 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1442 containing the command line. If NULL then the command-
1444 @param Environment Points to a NULL-terminated array of environment
1445 variables with the format 'x=y', where x is the
1446 environment variable name and y is the value. If this
1447 is NULL, then the current shell environment is used.
1448 @param StatusCode Points to the status code returned by the command.
1450 @retval EFI_SUCCESS The command executed successfully. The status code
1451 returned by the command is pointed to by StatusCode.
1452 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1453 @retval EFI_OUT_OF_RESOURCES Out of resources.
1454 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1455 @retval EFI_UNSUPPORTED The support level required for this function is not present.
1457 @sa InternalShellExecuteDevicePath
1462 IN EFI_HANDLE
*ParentImageHandle
,
1463 IN CHAR16
*CommandLine OPTIONAL
,
1464 IN CHAR16
**Environment OPTIONAL
,
1465 OUT EFI_STATUS
*StatusCode OPTIONAL
1470 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1473 if ((PcdGet8(PcdShellSupportLevel
) < 1)) {
1474 return (EFI_UNSUPPORTED
);
1477 DevPath
= AppendDevicePath (ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
1480 Temp
= gDevPathToText
->ConvertDevicePathToText(ShellInfoObject
.FileDevPath
, TRUE
, TRUE
);
1482 Temp
= gDevPathToText
->ConvertDevicePathToText(ShellInfoObject
.ImageDevPath
, TRUE
, TRUE
);
1484 Temp
= gDevPathToText
->ConvertDevicePathToText(DevPath
, TRUE
, TRUE
);
1490 ASSERT((Temp
== NULL
&& Size
== 0) || (Temp
!= NULL
));
1491 StrnCatGrow(&Temp
, &Size
, L
"Shell.efi ", 0);
1492 StrnCatGrow(&Temp
, &Size
, CommandLine
, 0);
1494 Status
= InternalShellExecuteDevicePath(
1498 (CONST CHAR16
**)Environment
,
1502 // de-allocate and return
1510 Utility cleanup function for EFI_SHELL_FILE_INFO objects.
1512 1) frees all pointers (non-NULL)
1513 2) Closes the SHELL_FILE_HANDLE
1515 @param FileListNode pointer to the list node to free
1519 InternalFreeShellFileInfoNode(
1520 IN EFI_SHELL_FILE_INFO
*FileListNode
1523 if (FileListNode
->Info
!= NULL
) {
1524 FreePool((VOID
*)FileListNode
->Info
);
1526 if (FileListNode
->FileName
!= NULL
) {
1527 FreePool((VOID
*)FileListNode
->FileName
);
1529 if (FileListNode
->FullName
!= NULL
) {
1530 FreePool((VOID
*)FileListNode
->FullName
);
1532 if (FileListNode
->Handle
!= NULL
) {
1533 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileListNode
->Handle
);
1535 FreePool(FileListNode
);
1538 Frees the file list.
1540 This function cleans up the file list and any related data structures. It has no
1541 impact on the files themselves.
1543 @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is
1544 defined in OpenFileList()
1546 @retval EFI_SUCCESS Free the file list successfully.
1547 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1551 EfiShellFreeFileList(
1552 IN EFI_SHELL_FILE_INFO
**FileList
1555 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1557 if (FileList
== NULL
|| *FileList
== NULL
) {
1558 return (EFI_INVALID_PARAMETER
);
1561 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1562 ; !IsListEmpty(&(*FileList
)->Link
)
1563 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1565 RemoveEntryList(&ShellFileListItem
->Link
);
1566 InternalFreeShellFileInfoNode(ShellFileListItem
);
1568 return(EFI_SUCCESS
);
1572 Deletes the duplicate file names files in the given file list.
1574 This function deletes the reduplicate files in the given file list.
1576 @param FileList A pointer to the first entry in the file list.
1578 @retval EFI_SUCCESS Always success.
1579 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1583 EfiShellRemoveDupInFileList(
1584 IN EFI_SHELL_FILE_INFO
**FileList
1587 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1588 EFI_SHELL_FILE_INFO
*ShellFileListItem2
;
1590 if (FileList
== NULL
|| *FileList
== NULL
) {
1591 return (EFI_INVALID_PARAMETER
);
1593 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1594 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1595 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1597 for ( ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1598 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1599 ; ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1601 if (gUnicodeCollation
->StriColl(
1603 (CHAR16
*)ShellFileListItem
->FullName
,
1604 (CHAR16
*)ShellFileListItem2
->FullName
) == 0
1606 RemoveEntryList(&ShellFileListItem2
->Link
);
1607 InternalFreeShellFileInfoNode(ShellFileListItem2
);
1611 return (EFI_SUCCESS
);
1614 Allocates and duplicates a EFI_SHELL_FILE_INFO node.
1616 @param[in] Node The node to copy from.
1617 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.
1619 @retval NULL a memory allocation error ocurred
1620 @return != NULL a pointer to the new node
1622 EFI_SHELL_FILE_INFO
*
1624 InternalDuplicateShellFileInfo(
1625 IN EFI_SHELL_FILE_INFO
*Node
,
1629 EFI_SHELL_FILE_INFO
*NewNode
;
1631 NewNode
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
1632 if (NewNode
== NULL
) {
1635 NewNode
->FullName
= AllocateZeroPool(StrSize(Node
->FullName
));
1637 NewNode
->FileName
= AllocateZeroPool(StrSize(Node
->FileName
));
1638 NewNode
->Info
= AllocatePool((UINTN
)Node
->Info
->Size
);
1639 if ( NewNode
->FullName
== NULL
1640 || NewNode
->FileName
== NULL
1641 || NewNode
->Info
== NULL
1645 NewNode
->Status
= Node
->Status
;
1646 NewNode
->Handle
= Node
->Handle
;
1648 Node
->Handle
= NULL
;
1650 StrCpy((CHAR16
*)NewNode
->FullName
, Node
->FullName
);
1651 StrCpy((CHAR16
*)NewNode
->FileName
, Node
->FileName
);
1652 CopyMem(NewNode
->Info
, Node
->Info
, (UINTN
)Node
->Info
->Size
);
1658 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation
1659 failed it will return NULL.
1661 @param[in] BasePath the Path to prepend onto filename for FullPath
1662 @param[in] Status Status member initial value.
1663 @param[in] FullName FullName member initial value.
1664 @param[in] FileName FileName member initial value.
1665 @param[in] Handle Handle member initial value.
1666 @param[in] Info Info struct to copy.
1668 @retval NULL An error ocurred.
1669 @return a pointer to the newly allocated structure.
1671 EFI_SHELL_FILE_INFO
*
1673 CreateAndPopulateShellFileInfo(
1674 IN CONST CHAR16
*BasePath
,
1675 IN CONST EFI_STATUS Status
,
1676 IN CONST CHAR16
*FullName
,
1677 IN CONST CHAR16
*FileName
,
1678 IN CONST SHELL_FILE_HANDLE Handle
,
1679 IN CONST EFI_FILE_INFO
*Info
1682 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1689 ShellFileListItem
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1690 if (ShellFileListItem
== NULL
) {
1694 ShellFileListItem
->Info
= AllocateZeroPool((UINTN
)Info
->Size
);
1695 if (ShellFileListItem
->Info
== NULL
) {
1696 FreePool(ShellFileListItem
);
1699 CopyMem(ShellFileListItem
->Info
, Info
, (UINTN
)Info
->Size
);
1701 ShellFileListItem
->Info
= NULL
;
1703 if (FileName
!= NULL
) {
1704 ASSERT(TempString
== NULL
);
1705 ShellFileListItem
->FileName
= StrnCatGrow(&TempString
, 0, FileName
, 0);
1706 if (ShellFileListItem
->FileName
== NULL
) {
1707 FreePool(ShellFileListItem
->Info
);
1708 FreePool(ShellFileListItem
);
1712 ShellFileListItem
->FileName
= NULL
;
1716 if (BasePath
!= NULL
) {
1717 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1718 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1719 if (TempString
== NULL
) {
1720 FreePool((VOID
*)ShellFileListItem
->FileName
);
1721 FreePool(ShellFileListItem
->Info
);
1722 FreePool(ShellFileListItem
);
1726 if (ShellFileListItem
->FileName
!= NULL
) {
1727 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1728 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileListItem
->FileName
, 0);
1729 if (TempString
== NULL
) {
1730 FreePool((VOID
*)ShellFileListItem
->FileName
);
1731 FreePool(ShellFileListItem
->Info
);
1732 FreePool(ShellFileListItem
);
1737 ShellFileListItem
->FullName
= TempString
;
1738 ShellFileListItem
->Status
= Status
;
1739 ShellFileListItem
->Handle
= Handle
;
1741 return (ShellFileListItem
);
1745 Find all files in a specified directory.
1747 @param FileDirHandle Handle of the directory to search.
1748 @param FileList On return, points to the list of files in the directory
1749 or NULL if there are no files in the directory.
1751 @retval EFI_SUCCESS File information was returned successfully.
1752 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.
1753 @retval EFI_DEVICE_ERROR The device reported an error.
1754 @retval EFI_NO_MEDIA The device media is not present.
1755 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.
1756 @return An error from FileHandleGetFileName().
1760 EfiShellFindFilesInDir(
1761 IN SHELL_FILE_HANDLE FileDirHandle
,
1762 OUT EFI_SHELL_FILE_INFO
**FileList
1765 EFI_SHELL_FILE_INFO
*ShellFileList
;
1766 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1767 EFI_FILE_INFO
*FileInfo
;
1775 Status
= FileHandleGetFileName(FileDirHandle
, &BasePath
);
1776 if (EFI_ERROR(Status
)) {
1780 if (ShellFileHandleGetPath(FileDirHandle
) != NULL
) {
1783 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileHandleGetPath(FileDirHandle
), 0);
1784 TempSpot
= StrStr(TempString
, L
";");
1786 if (TempSpot
!= NULL
) {
1787 *TempSpot
= CHAR_NULL
;
1790 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1791 BasePath
= TempString
;
1795 ShellFileList
= NULL
;
1796 ShellFileListItem
= NULL
;
1798 Status
= EFI_SUCCESS
;
1801 for ( Status
= FileHandleFindFirstFile(FileDirHandle
, &FileInfo
)
1802 ; !EFI_ERROR(Status
) && !NoFile
1803 ; Status
= FileHandleFindNextFile(FileDirHandle
, FileInfo
, &NoFile
)
1808 // allocate a new EFI_SHELL_FILE_INFO and populate it...
1810 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1811 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1812 TempString
= StrnCatGrow(&TempString
, &Size
, FileInfo
->FileName
, 0);
1813 ShellFileListItem
= CreateAndPopulateShellFileInfo(
1815 EFI_SUCCESS
, // success since we didnt fail to open it...
1818 NULL
, // no handle since not open
1821 if (ShellFileList
== NULL
) {
1822 ShellFileList
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1823 ASSERT(ShellFileList
!= NULL
);
1824 InitializeListHead(&ShellFileList
->Link
);
1826 InsertTailList(&ShellFileList
->Link
, &ShellFileListItem
->Link
);
1828 if (EFI_ERROR(Status
)) {
1829 EfiShellFreeFileList(&ShellFileList
);
1832 *FileList
= ShellFileList
;
1834 SHELL_FREE_NON_NULL(BasePath
);
1839 Updates a file name to be preceeded by the mapped drive name
1841 @param[in] BasePath the Mapped drive name to prepend
1842 @param[in,out] Path pointer to pointer to the file name to update.
1845 @retval EFI_OUT_OF_RESOURCES
1850 IN CONST CHAR16
*BasePath
,
1851 IN OUT CHAR16
**Path
1860 ASSERT(Path
!= NULL
);
1861 ASSERT(*Path
!= NULL
);
1862 ASSERT(BasePath
!= NULL
);
1865 // convert a local path to an absolute path
1867 if (StrStr(*Path
, L
":") == NULL
) {
1868 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
1869 StrnCatGrow(&Path2
, &Path2Size
, BasePath
, 0);
1870 if (Path2
== NULL
) {
1871 return (EFI_OUT_OF_RESOURCES
);
1873 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
1874 StrnCatGrow(&Path2
, &Path2Size
, (*Path
)[0] == L
'\\'?(*Path
) + 1 :*Path
, 0);
1875 if (Path2
== NULL
) {
1876 return (EFI_OUT_OF_RESOURCES
);
1883 return (EFI_SUCCESS
);
1887 If FileHandle is a directory then the function reads from FileHandle and reads in
1888 each of the FileInfo structures. If one of them matches the Pattern's first
1889 "level" then it opens that handle and calls itself on that handle.
1891 If FileHandle is a file and matches all of the remaining Pattern (which would be
1892 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.
1894 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call
1895 FreeFileList with FileList.
1897 @param[in] FilePattern The FilePattern to check against.
1898 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure
1899 @param[in] FileHandle The FileHandle to start with
1900 @param[in,out] FileList pointer to pointer to list of found files.
1901 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.
1902 @param[in] MapName The file system name this file is on.
1904 @retval EFI_SUCCESS all files were found and the FileList contains a list.
1905 @retval EFI_NOT_FOUND no files were found
1906 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
1911 IN CONST CHAR16
*FilePattern
,
1912 IN EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
,
1913 IN SHELL_FILE_HANDLE FileHandle
,
1914 IN OUT EFI_SHELL_FILE_INFO
**FileList
,
1915 IN CONST EFI_SHELL_FILE_INFO
*ParentNode OPTIONAL
,
1916 IN CONST CHAR16
*MapName
1920 CONST CHAR16
*NextFilePatternStart
;
1921 CHAR16
*CurrentFilePattern
;
1922 EFI_SHELL_FILE_INFO
*ShellInfo
;
1923 EFI_SHELL_FILE_INFO
*ShellInfoNode
;
1924 EFI_SHELL_FILE_INFO
*NewShellNode
;
1926 CHAR16
*NewFullName
;
1929 if ( FilePattern
== NULL
1930 || UnicodeCollation
== NULL
1933 return (EFI_INVALID_PARAMETER
);
1936 CurrentFilePattern
= NULL
;
1938 if (*FilePattern
== L
'\\') {
1942 for( NextFilePatternStart
= FilePattern
1943 ; *NextFilePatternStart
!= CHAR_NULL
&& *NextFilePatternStart
!= L
'\\'
1944 ; NextFilePatternStart
++);
1946 CurrentFilePattern
= AllocateZeroPool((NextFilePatternStart
-FilePattern
+1)*sizeof(CHAR16
));
1947 ASSERT(CurrentFilePattern
!= NULL
);
1948 StrnCpy(CurrentFilePattern
, FilePattern
, NextFilePatternStart
-FilePattern
);
1950 if (CurrentFilePattern
[0] == CHAR_NULL
1951 &&NextFilePatternStart
[0] == CHAR_NULL
1954 // Add the current parameter FileHandle to the list, then end...
1956 if (ParentNode
== NULL
) {
1957 Status
= EFI_INVALID_PARAMETER
;
1959 NewShellNode
= InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO
*)ParentNode
, TRUE
);
1960 if (NewShellNode
== NULL
) {
1961 Status
= EFI_OUT_OF_RESOURCES
;
1963 NewShellNode
->Handle
= NULL
;
1964 if (*FileList
== NULL
) {
1965 *FileList
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
1966 InitializeListHead(&((*FileList
)->Link
));
1970 // Add to the returning to use list
1972 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
1974 Status
= EFI_SUCCESS
;
1978 Status
= EfiShellFindFilesInDir(FileHandle
, &ShellInfo
);
1980 if (!EFI_ERROR(Status
)){
1981 if (StrStr(NextFilePatternStart
, L
"\\") != NULL
){
1986 for ( ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ShellInfo
->Link
)
1987 ; !IsNull (&ShellInfo
->Link
, &ShellInfoNode
->Link
)
1988 ; ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ShellInfo
->Link
, &ShellInfoNode
->Link
)
1990 if (UnicodeCollation
->MetaiMatch(UnicodeCollation
, (CHAR16
*)ShellInfoNode
->FileName
, CurrentFilePattern
)){
1991 if (ShellInfoNode
->FullName
!= NULL
&& StrStr(ShellInfoNode
->FullName
, L
":") == NULL
) {
1992 Size
= StrSize(ShellInfoNode
->FullName
);
1993 Size
+= StrSize(MapName
) + sizeof(CHAR16
);
1994 NewFullName
= AllocateZeroPool(Size
);
1995 if (NewFullName
== NULL
) {
1996 Status
= EFI_OUT_OF_RESOURCES
;
1998 StrCpy(NewFullName
, MapName
);
1999 StrCat(NewFullName
, ShellInfoNode
->FullName
+1);
2000 FreePool((VOID
*)ShellInfoNode
->FullName
);
2001 ShellInfoNode
->FullName
= NewFullName
;
2004 if (Directory
&& !EFI_ERROR(Status
)){
2006 // should be a directory
2010 // don't open the . and .. directories
2012 if ( (StrCmp(ShellInfoNode
->FileName
, L
".") != 0)
2013 && (StrCmp(ShellInfoNode
->FileName
, L
"..") != 0)
2018 ASSERT_EFI_ERROR(Status
);
2019 if (EFI_ERROR(Status
)) {
2023 // Open the directory since we need that handle in the next recursion.
2025 ShellInfoNode
->Status
= EfiShellOpenFileByName (ShellInfoNode
->FullName
, &ShellInfoNode
->Handle
, EFI_FILE_MODE_READ
);
2028 // recurse with the next part of the pattern
2030 Status
= ShellSearchHandle(NextFilePatternStart
, UnicodeCollation
, ShellInfoNode
->Handle
, FileList
, ShellInfoNode
, MapName
);
2032 } else if (!EFI_ERROR(Status
)) {
2038 // copy the information we need into a new Node
2040 NewShellNode
= InternalDuplicateShellFileInfo(ShellInfoNode
, FALSE
);
2041 ASSERT(NewShellNode
!= NULL
);
2042 if (NewShellNode
== NULL
) {
2043 Status
= EFI_OUT_OF_RESOURCES
;
2045 if (*FileList
== NULL
) {
2046 *FileList
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
2047 InitializeListHead(&((*FileList
)->Link
));
2051 // Add to the returning to use list
2053 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2056 if (EFI_ERROR(Status
)) {
2060 if (EFI_ERROR(Status
)) {
2061 EfiShellFreeFileList(&ShellInfo
);
2063 Status
= EfiShellFreeFileList(&ShellInfo
);
2068 FreePool(CurrentFilePattern
);
2073 Find files that match a specified pattern.
2075 This function searches for all files and directories that match the specified
2076 FilePattern. The FilePattern can contain wild-card characters. The resulting file
2077 information is placed in the file list FileList.
2079 Wildcards are processed
2080 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.
2082 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo
2083 field is set to NULL.
2085 if *FileList is not NULL then it must be a pre-existing and properly initialized list.
2087 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.
2088 @param FileList On return, points to the start of a file list containing the names
2089 of all matching files or else points to NULL if no matching files
2090 were found. only on a EFI_SUCCESS return will; this be non-NULL.
2092 @retval EFI_SUCCESS Files found. FileList is a valid list.
2093 @retval EFI_NOT_FOUND No files found.
2094 @retval EFI_NO_MEDIA The device has no media
2095 @retval EFI_DEVICE_ERROR The device reported an error
2096 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted
2101 IN CONST CHAR16
*FilePattern
,
2102 OUT EFI_SHELL_FILE_INFO
**FileList
2106 CHAR16
*PatternCopy
;
2107 CHAR16
*PatternCurrentLocation
;
2108 EFI_DEVICE_PATH_PROTOCOL
*RootDevicePath
;
2109 SHELL_FILE_HANDLE RootFileHandle
;
2113 if ( FilePattern
== NULL
2115 || StrStr(FilePattern
, L
":") == NULL
2117 return (EFI_INVALID_PARAMETER
);
2119 Status
= EFI_SUCCESS
;
2120 RootDevicePath
= NULL
;
2121 RootFileHandle
= NULL
;
2123 PatternCopy
= AllocatePool(StrSize(FilePattern
));
2124 if (PatternCopy
== NULL
) {
2125 return (EFI_OUT_OF_RESOURCES
);
2127 StrCpy(PatternCopy
, FilePattern
);
2129 PatternCopy
= CleanPath(PatternCopy
);
2131 Count
= StrStr(PatternCopy
, L
":") - PatternCopy
;
2134 ASSERT(MapName
== NULL
);
2135 MapName
= StrnCatGrow(&MapName
, NULL
, PatternCopy
, Count
);
2137 if (!EFI_ERROR(Status
)) {
2138 RootDevicePath
= EfiShellGetDevicePathFromFilePath(PatternCopy
);
2139 if (RootDevicePath
== NULL
) {
2140 Status
= EFI_INVALID_PARAMETER
;
2142 Status
= EfiShellOpenRoot(RootDevicePath
, &RootFileHandle
);
2143 if (!EFI_ERROR(Status
)) {
2144 for ( PatternCurrentLocation
= PatternCopy
2145 ; *PatternCurrentLocation
!= ':'
2146 ; PatternCurrentLocation
++);
2147 PatternCurrentLocation
++;
2148 Status
= ShellSearchHandle(PatternCurrentLocation
, gUnicodeCollation
, RootFileHandle
, FileList
, NULL
, MapName
);
2150 FreePool(RootDevicePath
);
2154 SHELL_FREE_NON_NULL(PatternCopy
);
2155 SHELL_FREE_NON_NULL(MapName
);
2161 Opens the files that match the path specified.
2163 This function opens all of the files specified by Path. Wildcards are processed
2164 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each
2165 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.
2167 @param Path A pointer to the path string.
2168 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or
2169 EFI_FILE_MODE_WRITE.
2170 @param FileList Points to the start of a list of files opened.
2172 @retval EFI_SUCCESS Create the file list successfully.
2173 @return Others Can't create the file list.
2177 EfiShellOpenFileList(
2180 IN OUT EFI_SHELL_FILE_INFO
**FileList
2184 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2187 CONST CHAR16
*CurDir
;
2189 ShellCommandCleanPath(Path
);
2194 ASSERT(FileList
!= NULL
);
2195 ASSERT(*FileList
!= NULL
);
2197 if (*Path
== L
'.' && *(Path
+1) == L
'\\') {
2202 // convert a local path to an absolute path
2204 if (StrStr(Path
, L
":") == NULL
) {
2205 CurDir
= EfiShellGetCurDir(NULL
);
2206 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2207 StrnCatGrow(&Path2
, &Path2Size
, CurDir
, 0);
2208 if (*Path
== L
'\\') {
2211 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2212 StrnCatGrow(&Path2
, &Path2Size
, Path
, 0);
2214 ASSERT(Path2
== NULL
);
2215 StrnCatGrow(&Path2
, NULL
, Path
, 0);
2223 Status
= EfiShellFindFiles(Path2
, FileList
);
2227 if (EFI_ERROR(Status
)) {
2232 // We had no errors so open all the files (that are not already opened...)
2234 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
2235 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2236 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2238 if (ShellFileListItem
->Status
== 0 && ShellFileListItem
->Handle
== NULL
) {
2239 ShellFileListItem
->Status
= EfiShellOpenFileByName (ShellFileListItem
->FullName
, &ShellFileListItem
->Handle
, OpenMode
);
2243 return(EFI_SUCCESS
);
2247 This function updated with errata.
2249 Gets either a single or list of environment variables.
2251 If name is not NULL then this function returns the current value of the specified
2252 environment variable.
2254 If Name is NULL, then a list of all environment variable names is returned. Each is a
2255 NULL terminated string with a double NULL terminating the list.
2257 @param Name A pointer to the environment variable name. If
2258 Name is NULL, then the function will return all
2259 of the defined shell environment variables. In
2260 the case where multiple environment variables are
2261 being returned, each variable will be terminated by
2262 a NULL, and the list will be terminated by a double
2265 @return !=NULL A pointer to the returned string.
2266 The returned pointer does not need to be freed by the caller.
2268 @retval NULL The environment variable doesn't exist or there are
2269 no environment variables.
2274 IN CONST CHAR16
*Name
2282 CHAR16
*CurrentWriteLocation
;
2289 // Get all our environment variables
2291 InitializeListHead(&List
);
2292 Status
= GetEnvironmentVariableList(&List
);
2293 if (EFI_ERROR(Status
)){
2298 // Build the semi-colon delimited list. (2 passes)
2300 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&List
)
2301 ; !IsNull(&List
, &Node
->Link
)
2302 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&List
, &Node
->Link
)
2304 ASSERT(Node
->Key
!= NULL
);
2305 Size
+= StrSize(Node
->Key
);
2308 Size
+= 2*sizeof(CHAR16
);
2310 Buffer
= AllocateZeroPool(Size
);
2311 if (Buffer
== NULL
) {
2312 if (!IsListEmpty (&List
)) {
2313 FreeEnvironmentVariableList(&List
);
2317 CurrentWriteLocation
= (CHAR16
*)Buffer
;
2319 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&List
)
2320 ; !IsNull(&List
, &Node
->Link
)
2321 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&List
, &Node
->Link
)
2323 ASSERT(Node
->Key
!= NULL
);
2324 StrCpy(CurrentWriteLocation
, Node
->Key
);
2325 CurrentWriteLocation
+= StrLen(CurrentWriteLocation
) + 1;
2331 if (!IsListEmpty (&List
)) {
2332 FreeEnvironmentVariableList(&List
);
2336 // We are doing a specific environment variable
2340 // get the size we need for this EnvVariable
2342 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(Name
, &Size
, Buffer
);
2343 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2345 // Allocate the space and recall the get function
2347 Buffer
= AllocateZeroPool(Size
);
2348 ASSERT(Buffer
!= NULL
);
2349 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(Name
, &Size
, Buffer
);
2352 // we didnt get it (might not exist)
2353 // free the memory if we allocated any and return NULL
2355 if (EFI_ERROR(Status
)) {
2356 if (Buffer
!= NULL
) {
2364 // return the buffer
2366 return (AddBufferToFreeList(Buffer
));
2370 Internal variable setting function. Allows for setting of the read only variables.
2372 @param Name Points to the NULL-terminated environment variable name.
2373 @param Value Points to the NULL-terminated environment variable value. If the value is an
2374 empty string then the environment variable is deleted.
2375 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2377 @retval EFI_SUCCESS The environment variable was successfully updated.
2381 InternalEfiShellSetEnv(
2382 IN CONST CHAR16
*Name
,
2383 IN CONST CHAR16
*Value
,
2387 if (Value
== NULL
|| StrLen(Value
) == 0) {
2388 return (SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
));
2390 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2392 return (SHELL_SET_ENVIRONMENT_VARIABLE_V(Name
, StrSize(Value
), Value
));
2394 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(Name
, StrSize(Value
), Value
));
2400 Sets the environment variable.
2402 This function changes the current value of the specified environment variable. If the
2403 environment variable exists and the Value is an empty string, then the environment
2404 variable is deleted. If the environment variable exists and the Value is not an empty
2405 string, then the value of the environment variable is changed. If the environment
2406 variable does not exist and the Value is an empty string, there is no action. If the
2407 environment variable does not exist and the Value is a non-empty string, then the
2408 environment variable is created and assigned the specified value.
2410 For a description of volatile and non-volatile environment variables, see UEFI Shell
2411 2.0 specification section 3.6.1.
2413 @param Name Points to the NULL-terminated environment variable name.
2414 @param Value Points to the NULL-terminated environment variable value. If the value is an
2415 empty string then the environment variable is deleted.
2416 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2418 @retval EFI_SUCCESS The environment variable was successfully updated.
2423 IN CONST CHAR16
*Name
,
2424 IN CONST CHAR16
*Value
,
2428 if (Name
== NULL
|| *Name
== CHAR_NULL
) {
2429 return (EFI_INVALID_PARAMETER
);
2432 // Make sure we dont 'set' a predefined read only variable
2434 if (gUnicodeCollation
->StriColl(
2438 ||gUnicodeCollation
->StriColl(
2442 ||gUnicodeCollation
->StriColl(
2446 ||gUnicodeCollation
->StriColl(
2449 L
"uefishellsupport") == 0
2450 ||gUnicodeCollation
->StriColl(
2453 L
"uefishellversion") == 0
2454 ||gUnicodeCollation
->StriColl(
2457 L
"uefiversion") == 0
2459 return (EFI_INVALID_PARAMETER
);
2461 return (InternalEfiShellSetEnv(Name
, Value
, Volatile
));
2465 Returns the current directory on the specified device.
2467 If FileSystemMapping is NULL, it returns the current working directory. If the
2468 FileSystemMapping is not NULL, it returns the current directory associated with the
2469 FileSystemMapping. In both cases, the returned name includes the file system
2470 mapping (i.e. fs0:\current-dir).
2472 @param FileSystemMapping A pointer to the file system mapping. If NULL,
2473 then the current working directory is returned.
2475 @retval !=NULL The current directory.
2476 @retval NULL Current directory does not exist.
2481 IN CONST CHAR16
*FileSystemMapping OPTIONAL
2484 CHAR16
*PathToReturn
;
2486 SHELL_MAP_LIST
*MapListItem
;
2487 if (!IsListEmpty(&gShellMapList
.Link
)) {
2489 // if parameter is NULL, use current
2491 if (FileSystemMapping
== NULL
) {
2492 return (EfiShellGetEnv(L
"cwd"));
2495 PathToReturn
= NULL
;
2496 MapListItem
= ShellCommandFindMapItem(FileSystemMapping
);
2497 if (MapListItem
!= NULL
) {
2498 ASSERT((PathToReturn
== NULL
&& Size
== 0) || (PathToReturn
!= NULL
));
2499 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->MapName
, 0);
2500 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
2503 return (AddBufferToFreeList(PathToReturn
));
2510 Changes the current directory on the specified device.
2512 If the FileSystem is NULL, and the directory Dir does not contain a file system's
2513 mapped name, this function changes the current working directory.
2515 If the FileSystem is NULL and the directory Dir contains a mapped name, then the
2516 current file system and the current directory on that file system are changed.
2518 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file
2521 If FileSystem is not NULL and Dir is not NULL, then this function changes the current
2522 directory on the specified file system.
2524 If the current working directory or the current working file system is changed then the
2525 %cwd% environment variable will be updated
2527 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working
2528 directory is changed.
2529 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.
2531 @retval EFI_SUCCESS The operation was sucessful
2532 @retval EFI_NOT_FOUND The file system could not be found
2537 IN CONST CHAR16
*FileSystem OPTIONAL
,
2538 IN CONST CHAR16
*Dir
2542 SHELL_MAP_LIST
*MapListItem
;
2546 CHAR16
*DirectoryName
;
2553 DirectoryName
= NULL
;
2555 if ((FileSystem
== NULL
&& Dir
== NULL
) || Dir
== NULL
) {
2556 return (EFI_INVALID_PARAMETER
);
2559 if (IsListEmpty(&gShellMapList
.Link
)){
2560 return (EFI_NOT_FOUND
);
2563 DirectoryName
= StrnCatGrow(&DirectoryName
, NULL
, Dir
, 0);
2564 ASSERT(DirectoryName
!= NULL
);
2566 CleanPath(DirectoryName
);
2568 if (FileSystem
== NULL
) {
2570 // determine the file system mapping to use
2572 if (StrStr(DirectoryName
, L
":") != NULL
) {
2573 ASSERT(MapName
== NULL
);
2574 MapName
= StrnCatGrow(&MapName
, NULL
, DirectoryName
, (StrStr(DirectoryName
, L
":")-DirectoryName
+1));
2577 // find the file system mapping's entry in the list
2580 if (MapName
!= NULL
) {
2581 MapListItem
= ShellCommandFindMapItem(MapName
);
2584 // make that the current file system mapping
2586 if (MapListItem
!= NULL
) {
2587 gShellCurDir
= MapListItem
;
2590 MapListItem
= gShellCurDir
;
2593 if (MapListItem
== NULL
) {
2594 return (EFI_NOT_FOUND
);
2598 // now update the MapListItem's current directory
2600 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& DirectoryName
[StrLen(DirectoryName
) - 1] != L
':') {
2601 FreePool(MapListItem
->CurrentDirectoryPath
);
2602 MapListItem
->CurrentDirectoryPath
= NULL
;
2604 if (MapName
!= NULL
) {
2605 TempLen
= StrLen(MapName
);
2606 if (TempLen
!= StrLen(DirectoryName
)) {
2607 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2608 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
+StrLen(MapName
), 0);
2611 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2612 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
2614 if ((MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] != L
'\\') || (MapListItem
->CurrentDirectoryPath
== NULL
)) {
2615 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2616 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2620 // cant have a mapping in the directory...
2622 if (StrStr(DirectoryName
, L
":") != NULL
) {
2623 return (EFI_INVALID_PARAMETER
);
2626 // FileSystem != NULL
2628 MapListItem
= ShellCommandFindMapItem(FileSystem
);
2629 if (MapListItem
== NULL
) {
2630 return (EFI_INVALID_PARAMETER
);
2632 // gShellCurDir = MapListItem;
2633 if (DirectoryName
!= NULL
) {
2635 // change current dir on that file system
2638 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
2639 FreePool(MapListItem
->CurrentDirectoryPath
);
2640 DEBUG_CODE(MapListItem
->CurrentDirectoryPath
= NULL
;);
2642 // ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2643 // MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);
2644 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2645 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2646 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2647 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
2648 if (MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] != L
'\\') {
2649 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2650 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2655 // if updated the current directory then update the environment variable
2657 if (MapListItem
== gShellCurDir
) {
2659 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2660 StrnCatGrow(&TempString
, &Size
, MapListItem
->MapName
, 0);
2661 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2662 StrnCatGrow(&TempString
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
2663 Status
= InternalEfiShellSetEnv(L
"cwd", TempString
, TRUE
);
2664 FreePool(TempString
);
2667 return(EFI_SUCCESS
);
2671 Return help information about a specific command.
2673 This function returns the help information for the specified command. The help text
2674 can be internal to the shell or can be from a UEFI Shell manual page.
2676 If Sections is specified, then each section name listed will be compared in a casesensitive
2677 manner, to the section names described in Appendix B. If the section exists,
2678 it will be appended to the returned help text. If the section does not exist, no
2679 information will be returned. If Sections is NULL, then all help text information
2680 available will be returned.
2682 @param Command Points to the NULL-terminated UEFI Shell command name.
2683 @param Sections Points to the NULL-terminated comma-delimited
2684 section names to return. If NULL, then all
2685 sections will be returned.
2686 @param HelpText On return, points to a callee-allocated buffer
2687 containing all specified help text.
2689 @retval EFI_SUCCESS The help text was returned.
2690 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
2692 @retval EFI_INVALID_PARAMETER HelpText is NULL
2693 @retval EFI_NOT_FOUND There is no help text available for Command.
2697 EfiShellGetHelpText(
2698 IN CONST CHAR16
*Command
,
2699 IN CONST CHAR16
*Sections OPTIONAL
,
2700 OUT CHAR16
**HelpText
2703 CONST CHAR16
*ManFileName
;
2705 ASSERT(HelpText
!= NULL
);
2707 ManFileName
= ShellCommandGetManFileNameHandler(Command
);
2709 if (ManFileName
!= NULL
) {
2710 return (ProcessManFile(ManFileName
, Command
, Sections
, NULL
, HelpText
));
2712 return (ProcessManFile(Command
, Command
, Sections
, NULL
, HelpText
));
2717 Gets the enable status of the page break output mode.
2719 User can use this function to determine current page break mode.
2721 @retval TRUE The page break output mode is enabled.
2722 @retval FALSE The page break output mode is disabled.
2726 EfiShellGetPageBreak(
2730 return(ShellInfoObject
.PageBreakEnabled
);
2734 Judges whether the active shell is the root shell.
2736 This function makes the user to know that whether the active Shell is the root shell.
2738 @retval TRUE The active Shell is the root Shell.
2739 @retval FALSE The active Shell is NOT the root Shell.
2743 EfiShellIsRootShell(
2747 return(ShellInfoObject
.RootShellInstance
);
2751 function to return a semi-colon delimeted list of all alias' in the current shell
2753 up to caller to free the memory.
2755 @retval NULL No alias' were found
2756 @retval NULL An error ocurred getting alias'
2757 @return !NULL a list of all alias'
2761 InternalEfiShellGetListAlias(
2769 CHAR16
*VariableName
;
2775 Status
= gRT
->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
, &MaxStorSize
, &RemStorSize
, &MaxVarSize
);
2776 ASSERT_EFI_ERROR(Status
);
2778 VariableName
= AllocateZeroPool((UINTN
)MaxVarSize
);
2782 if (VariableName
== NULL
) {
2786 VariableName
[0] = CHAR_NULL
;
2789 NameSize
= (UINTN
)MaxVarSize
;
2790 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
2791 if (Status
== EFI_NOT_FOUND
){
2794 ASSERT_EFI_ERROR(Status
);
2795 if (EFI_ERROR(Status
)) {
2798 if (CompareGuid(&Guid
, &gShellAliasGuid
)){
2799 Alias
= GetVariable(VariableName
, &gShellAliasGuid
);
2800 ASSERT((RetVal
== NULL
&& RetSize
== 0) || (RetVal
!= NULL
));
2801 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, VariableName
, 0);
2802 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, L
";", 0);
2805 FreePool(VariableName
);
2811 This function returns the command associated with a alias or a list of all
2814 @param[in] Alias Points to the NULL-terminated shell alias.
2815 If this parameter is NULL, then all
2816 aliases will be returned in ReturnedData.
2817 @param[out] Volatile upon return of a single command if TRUE indicates
2818 this is stored in a volatile fashion. FALSE otherwise.
2820 @return If Alias is not NULL, it will return a pointer to
2821 the NULL-terminated command for that alias.
2822 If Alias is NULL, ReturnedData points to a ';'
2823 delimited list of alias (e.g.
2824 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.
2825 @retval NULL an error ocurred
2826 @retval NULL Alias was not a valid Alias
2831 IN CONST CHAR16
*Alias
,
2832 OUT BOOLEAN
*Volatile OPTIONAL
2840 if (Alias
!= NULL
) {
2841 if (Volatile
== NULL
) {
2842 return (AddBufferToFreeList(GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
)));
2846 Status
= gRT
->GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
2847 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2848 RetVal
= AllocateZeroPool(RetSize
);
2849 Status
= gRT
->GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
2851 if (EFI_ERROR(Status
)) {
2852 if (RetVal
!= NULL
) {
2857 if ((EFI_VARIABLE_NON_VOLATILE
& Attribs
) == EFI_VARIABLE_NON_VOLATILE
) {
2863 return (AddBufferToFreeList(RetVal
));
2865 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));
2869 Changes a shell command alias.
2871 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
2873 this function does not check for built in alias'.
2875 @param[in] Command Points to the NULL-terminated shell command or existing alias.
2876 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
2877 Command refers to an alias, that alias will be deleted.
2878 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
2879 Alias being set will be stored in a non-volatile fashion.
2881 @retval EFI_SUCCESS Alias created or deleted successfully.
2882 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
2887 IN CONST CHAR16
*Command
,
2888 IN CONST CHAR16
*Alias
,
2893 // We must be trying to remove one if Alias is NULL
2895 if (Alias
== NULL
) {
2897 // remove an alias (but passed in COMMAND parameter)
2899 return (gRT
->SetVariable((CHAR16
*)Command
, &gShellAliasGuid
, 0, 0, NULL
));
2902 // Add and replace are the same
2905 // We dont check the error return on purpose since the variable may not exist.
2906 gRT
->SetVariable((CHAR16
*)Command
, &gShellAliasGuid
, 0, 0, NULL
);
2908 return (gRT
->SetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, EFI_VARIABLE_BOOTSERVICE_ACCESS
|(Volatile
?0:EFI_VARIABLE_NON_VOLATILE
), StrSize(Command
), (VOID
*)Command
));
2913 Changes a shell command alias.
2915 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
2918 @param[in] Command Points to the NULL-terminated shell command or existing alias.
2919 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
2920 Command refers to an alias, that alias will be deleted.
2921 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
2922 FALSE and the alias already exists, then the existing alias is unchanged and
2923 EFI_ACCESS_DENIED is returned.
2924 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
2925 Alias being set will be stored in a non-volatile fashion.
2927 @retval EFI_SUCCESS Alias created or deleted successfully.
2928 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
2929 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
2935 IN CONST CHAR16
*Command
,
2936 IN CONST CHAR16
*Alias
,
2942 // cant set over a built in alias
2944 if (ShellCommandIsOnAliasList(Alias
==NULL
?Command
:Alias
)) {
2945 return (EFI_ACCESS_DENIED
);
2947 if (Command
== NULL
|| *Command
== CHAR_NULL
|| StrLen(Command
) == 0) {
2948 return (EFI_INVALID_PARAMETER
);
2951 if (EfiShellGetAlias(Command
, NULL
) != NULL
&& !Replace
) {
2952 return (EFI_ACCESS_DENIED
);
2955 return (InternalSetAlias(Command
, Alias
, Volatile
));
2958 // Pure FILE_HANDLE operations are passed to FileHandleLib
2959 // these functions are indicated by the *
2960 EFI_SHELL_PROTOCOL mShellProtocol
= {
2966 EfiShellGetHelpText
,
2967 EfiShellGetDevicePathFromMap
,
2968 EfiShellGetMapFromDevicePath
,
2969 EfiShellGetDevicePathFromFilePath
,
2970 EfiShellGetFilePathFromDevicePath
,
2974 EfiShellOpenFileList
,
2975 EfiShellFreeFileList
,
2976 EfiShellRemoveDupInFileList
,
2977 EfiShellBatchIsActive
,
2978 EfiShellIsRootShell
,
2979 EfiShellEnablePageBreak
,
2980 EfiShellDisablePageBreak
,
2981 EfiShellGetPageBreak
,
2982 EfiShellGetDeviceName
,
2983 (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
, //*
2984 (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
, //*
2985 EfiShellOpenFileByName
,
2988 (EFI_SHELL_READ_FILE
)FileHandleRead
, //*
2989 (EFI_SHELL_WRITE_FILE
)FileHandleWrite
, //*
2990 (EFI_SHELL_DELETE_FILE
)FileHandleDelete
, //*
2991 EfiShellDeleteFileByName
,
2992 (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
, //*
2993 (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
, //*
2994 (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
, //*
2996 EfiShellFindFilesInDir
,
2997 (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
, //*
2999 EfiShellOpenRootByHandle
,
3001 SHELL_MAJOR_VERSION
,
3006 Function to create and install on the current handle.
3008 Will overwrite any existing ShellProtocols in the system to be sure that
3009 the current shell is in control.
3011 This must be removed via calling CleanUpShellProtocol().
3013 @param[in,out] NewShell The pointer to the pointer to the structure
3016 @retval EFI_SUCCESS The operation was successful.
3017 @return An error from LocateHandle, CreateEvent, or other core function.
3021 CreatePopulateInstallShellProtocol (
3022 IN OUT EFI_SHELL_PROTOCOL
**NewShell
3028 UINTN HandleCounter
;
3029 SHELL_PROTOCOL_HANDLE_LIST
*OldProtocolNode
;
3031 if (NewShell
== NULL
) {
3032 return (EFI_INVALID_PARAMETER
);
3037 OldProtocolNode
= NULL
;
3038 InitializeListHead(&ShellInfoObject
.OldShellList
.Link
);
3041 // Initialize EfiShellProtocol object...
3043 Status
= gBS
->CreateEvent(0,
3047 &mShellProtocol
.ExecutionBreak
);
3048 if (EFI_ERROR(Status
)) {
3053 // Get the size of the buffer we need.
3055 Status
= gBS
->LocateHandle(ByProtocol
,
3056 &gEfiShellProtocolGuid
,
3060 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3062 // Allocate and recall with buffer of correct size
3064 Buffer
= AllocateZeroPool(BufferSize
);
3065 if (Buffer
== NULL
) {
3066 return (EFI_OUT_OF_RESOURCES
);
3068 Status
= gBS
->LocateHandle(ByProtocol
,
3069 &gEfiShellProtocolGuid
,
3073 if (EFI_ERROR(Status
)) {
3078 // now overwrite each of them, but save the info to restore when we end.
3080 for (HandleCounter
= 0 ; HandleCounter
< (BufferSize
/sizeof(EFI_HANDLE
)) ; HandleCounter
++) {
3081 OldProtocolNode
= AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST
));
3082 ASSERT(OldProtocolNode
!= NULL
);
3083 Status
= gBS
->OpenProtocol(Buffer
[HandleCounter
],
3084 &gEfiShellProtocolGuid
,
3085 (VOID
**) &(OldProtocolNode
->Interface
),
3088 EFI_OPEN_PROTOCOL_GET_PROTOCOL
3090 if (!EFI_ERROR(Status
)) {
3092 // reinstall over the old one...
3094 OldProtocolNode
->Handle
= Buffer
[HandleCounter
];
3095 Status
= gBS
->ReinstallProtocolInterface(
3096 OldProtocolNode
->Handle
,
3097 &gEfiShellProtocolGuid
,
3098 OldProtocolNode
->Interface
,
3099 (VOID
*)(&mShellProtocol
));
3100 if (!EFI_ERROR(Status
)) {
3102 // we reinstalled sucessfully. log this so we can reverse it later.
3106 // add to the list for subsequent...
3108 InsertTailList(&ShellInfoObject
.OldShellList
.Link
, &OldProtocolNode
->Link
);
3113 } else if (Status
== EFI_NOT_FOUND
) {
3114 ASSERT(IsListEmpty(&ShellInfoObject
.OldShellList
.Link
));
3116 // no one else published yet. just publish it ourselves.
3118 Status
= gBS
->InstallProtocolInterface (
3120 &gEfiShellProtocolGuid
,
3121 EFI_NATIVE_INTERFACE
,
3122 (VOID
*)(&mShellProtocol
));
3125 if (PcdGetBool(PcdShellSupportOldProtocols
)){
3126 ///@todo support ShellEnvironment2
3127 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?
3130 if (!EFI_ERROR(Status
)) {
3131 *NewShell
= &mShellProtocol
;
3137 Opposite of CreatePopulateInstallShellProtocol.
3139 Free all memory and restore the system to the state it was in before calling
3140 CreatePopulateInstallShellProtocol.
3142 @param[in,out] NewShell The pointer to the new shell protocol structure.
3144 @retval EFI_SUCCESS The operation was successful.
3148 CleanUpShellProtocol (
3149 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3153 SHELL_PROTOCOL_HANDLE_LIST
*Node2
;
3154 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
3157 // if we need to restore old protocols...
3159 if (!IsListEmpty(&ShellInfoObject
.OldShellList
.Link
)) {
3160 for (Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*)GetFirstNode(&ShellInfoObject
.OldShellList
.Link
)
3161 ; !IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)
3162 ; Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*)GetFirstNode(&ShellInfoObject
.OldShellList
.Link
)
3164 RemoveEntryList(&Node2
->Link
);
3165 Status
= gBS
->ReinstallProtocolInterface(Node2
->Handle
,
3166 &gEfiShellProtocolGuid
,
3173 // no need to restore
3175 Status
= gBS
->UninstallProtocolInterface(gImageHandle
,
3176 &gEfiShellProtocolGuid
,
3179 Status
= gBS
->CloseEvent(NewShell
->ExecutionBreak
);
3180 NewShell
->ExecutionBreak
= NULL
;
3182 Status
= gBS
->OpenProtocol(
3183 gST
->ConsoleInHandle
,
3184 &gEfiSimpleTextInputExProtocolGuid
,
3188 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
3190 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle1
);
3191 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle2
);
3197 Notification function for keystrokes.
3199 @param[in] KeyData The key that was pressed.
3201 @retval EFI_SUCCESS The operation was successful.
3205 NotificationFunction(
3206 IN EFI_KEY_DATA
*KeyData
3209 if (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
== NULL
) {
3210 return (EFI_UNSUPPORTED
);
3212 return (gBS
->SignalEvent(ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
));
3216 Function to start monitoring for CTRL-C using SimpleTextInputEx. This
3217 feature's enabled state was not known when the shell initially launched.
3219 @retval EFI_SUCCESS The feature is enabled.
3220 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
3224 InernalEfiShellStartMonitor(
3228 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
3229 EFI_KEY_DATA KeyData
;
3232 Status
= gBS
->OpenProtocol(
3233 gST
->ConsoleInHandle
,
3234 &gEfiSimpleTextInputExProtocolGuid
,
3238 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
3239 if (EFI_ERROR(Status
)) {
3244 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
3245 ShellInfoObject
.HiiHandle
);
3246 return (EFI_SUCCESS
);
3249 if (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
== NULL
) {
3250 return (EFI_UNSUPPORTED
);
3253 KeyData
.KeyState
.KeyToggleState
= 0;
3254 KeyData
.Key
.ScanCode
= 0;
3255 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
3256 KeyData
.Key
.UnicodeChar
= L
'c';
3258 Status
= SimpleEx
->RegisterKeyNotify(
3261 NotificationFunction
,
3262 &ShellInfoObject
.CtrlCNotifyHandle1
);
3264 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
3265 if (!EFI_ERROR(Status
)) {
3266 Status
= SimpleEx
->RegisterKeyNotify(
3269 NotificationFunction
,
3270 &ShellInfoObject
.CtrlCNotifyHandle2
);
3272 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
3273 KeyData
.Key
.UnicodeChar
= 3;
3274 if (!EFI_ERROR(Status
)) {
3275 Status
= SimpleEx
->RegisterKeyNotify(
3278 NotificationFunction
,
3279 &ShellInfoObject
.CtrlCNotifyHandle3
);
3281 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
3282 if (!EFI_ERROR(Status
)) {
3283 Status
= SimpleEx
->RegisterKeyNotify(
3286 NotificationFunction
,
3287 &ShellInfoObject
.CtrlCNotifyHandle4
);