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(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
;
390 PathForReturn
= NULL
;
393 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)Path
;
394 ASSERT(DevicePathCopy
!= NULL
);
395 if (DevicePathCopy
== NULL
) {
399 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &PathHandle
);
401 if (EFI_ERROR(Status
)) {
405 // check each of the device paths we have to get the root of the path
407 for ( MapListItem
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
408 ; !IsNull(&gShellMapList
.Link
, &MapListItem
->Link
)
409 ; MapListItem
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListItem
->Link
)
411 MapPathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)MapListItem
->DevicePath
;
412 ASSERT(MapPathCopy
!= NULL
);
414 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &MapPathCopy
, &MapHandle
);
415 if (MapHandle
== PathHandle
) {
416 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
417 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, MapListItem
->MapName
, 0);
419 // go through all the remaining nodes in the device path
421 for ( FilePath
= (FILEPATH_DEVICE_PATH
*)DevicePathCopy
422 ; !IsDevicePathEnd (&FilePath
->Header
)
423 ; FilePath
= (FILEPATH_DEVICE_PATH
*)NextDevicePathNode (&FilePath
->Header
)
426 // all the rest should be file path nodes
428 if ((DevicePathType(&FilePath
->Header
) != MEDIA_DEVICE_PATH
) ||
429 (DevicePathSubType(&FilePath
->Header
) != MEDIA_FILEPATH_DP
)) {
430 FreePool(PathForReturn
);
431 PathForReturn
= NULL
;
435 // append the path part onto the filepath.
437 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
438 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, L
"\\", 1);
439 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, FilePath
->PathName
, 0);
441 } // for loop of remaining nodes
443 if (PathForReturn
!= NULL
) {
446 } // for loop of paths to check
447 return(PathForReturn
);
451 Converts a file system style name to a device path.
453 This function converts a file system style name to a device path, by replacing any
454 mapping references to the associated device path.
456 @param Path the pointer to the path
458 @return all The pointer of the file path. The file path is callee
459 allocated and should be freed by the caller.
461 EFI_DEVICE_PATH_PROTOCOL
*
463 EfiShellGetDevicePathFromFilePath(
464 IN CONST CHAR16
*Path
471 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
472 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
473 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopyForFree
;
474 EFI_DEVICE_PATH_PROTOCOL
*DevicePathForReturn
;
479 ASSERT(Path
!= NULL
);
481 if (StrStr(Path
, L
":") == NULL
) {
482 Cwd
= EfiShellGetCurDir(NULL
);
487 Size
+= StrSize(Path
);
488 NewPath
= AllocateZeroPool(Size
);
489 ASSERT(NewPath
!= NULL
);
490 StrCpy(NewPath
, Cwd
);
491 if ((NewPath
[0] == (CHAR16
)L
'\\') &&
492 (NewPath
[StrLen(NewPath
)-1] == (CHAR16
)L
'\\')
494 ((CHAR16
*)NewPath
)[StrLen(NewPath
)-1] = CHAR_NULL
;
496 StrCat(NewPath
, Path
);
497 DevicePathForReturn
= EfiShellGetDevicePathFromFilePath(NewPath
);
499 return (DevicePathForReturn
);
504 // find the part before (but including) the : for the map name
506 ASSERT((MapName
== NULL
&& Size
== 0) || (MapName
!= NULL
));
507 MapName
= StrnCatGrow(&MapName
, &Size
, Path
, (StrStr(Path
, L
":")-Path
+1));
508 if (MapName
[StrLen(MapName
)-1] != L
':') {
514 // look up the device path in the map
516 DevicePath
= EfiShellGetDevicePathFromMap(MapName
);
517 if (DevicePath
== NULL
) {
519 // Must have been a bad Mapname
525 // make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
527 DevicePathCopyForFree
= DevicePathCopy
= DuplicateDevicePath(DevicePath
);
528 if (DevicePathCopy
== NULL
) {
538 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
539 if (EFI_ERROR(Status
)) {
540 if (DevicePathCopyForFree
!= NULL
) {
541 FreePool(DevicePathCopyForFree
);
548 // build the full device path
550 DevicePathForReturn
= FileDevicePath(Handle
, Path
+StrLen(MapName
)+1);
553 if (DevicePathCopyForFree
!= NULL
) {
554 FreePool(DevicePathCopyForFree
);
557 return (DevicePathForReturn
);
561 Gets the name of the device specified by the device handle.
563 This function gets the user-readable name of the device specified by the device
564 handle. If no user-readable name could be generated, then *BestDeviceName will be
565 NULL and EFI_NOT_FOUND will be returned.
567 If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
568 device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
571 If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
572 device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
573 If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
574 EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
575 EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
577 @param DeviceHandle The handle of the device.
578 @param Flags Determines the possible sources of component names.
580 EFI_DEVICE_NAME_USE_COMPONENT_NAME
581 EFI_DEVICE_NAME_USE_DEVICE_PATH
582 @param Language A pointer to the language specified for the device
583 name, in the same format as described in the UEFI
584 specification, Appendix M
585 @param BestDeviceName On return, points to the callee-allocated NULL-
586 terminated name of the device. If no device name
587 could be found, points to NULL. The name must be
588 freed by the caller...
590 @retval EFI_SUCCESS Get the name successfully.
591 @retval EFI_NOT_FOUND Fail to get the device name.
592 @retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
593 @retval EFI_INVALID_PARAMETER BestDeviceName was NULL
594 @retval EFI_INVALID_PARAMETER DeviceHandle was NULL
598 EfiShellGetDeviceName(
599 IN EFI_HANDLE DeviceHandle
,
600 IN EFI_SHELL_DEVICE_NAME_FLAGS Flags
,
602 OUT CHAR16
**BestDeviceName
606 EFI_COMPONENT_NAME2_PROTOCOL
*CompName2
;
607 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevicePathToText
;
608 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
609 EFI_HANDLE
*HandleList
;
612 CHAR16
*DeviceNameToReturn
;
616 if (BestDeviceName
== NULL
||
619 return (EFI_INVALID_PARAMETER
);
623 // make sure one of the 2 supported bits is on
625 if (((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) == 0) &&
626 ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) == 0)) {
627 return (EFI_INVALID_PARAMETER
);
630 DeviceNameToReturn
= NULL
;
631 *BestDeviceName
= NULL
;
636 if ((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) != 0) {
637 Status
= ParseHandleDatabaseByRelationship(
640 HR_DRIVER_BINDING_HANDLE
|HR_DEVICE_DRIVER
,
643 for (LoopVar
= 0; LoopVar
< HandleCount
; LoopVar
++){
645 // Go through those handles until we get one that passes for GetComponentName
647 Status
= gBS
->OpenProtocol(
649 &gEfiComponentName2ProtocolGuid
,
653 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
654 if (EFI_ERROR(Status
)) {
655 Status
= gBS
->OpenProtocol(
657 &gEfiComponentNameProtocolGuid
,
661 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
664 if (EFI_ERROR(Status
)) {
667 if (Language
== NULL
) {
668 Lang
= AllocatePool(AsciiStrSize(CompName2
->SupportedLanguages
));
669 AsciiStrCpy(Lang
, CompName2
->SupportedLanguages
);
670 TempChar
= AsciiStrStr(Lang
, ";");
671 if (TempChar
!= NULL
){
672 *TempChar
= CHAR_NULL
;
675 Lang
= AllocatePool(AsciiStrSize(Language
));
676 AsciiStrCpy(Lang
, Language
);
678 Status
= CompName2
->GetControllerName(CompName2
, DeviceHandle
, NULL
, Lang
, &DeviceNameToReturn
);
681 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
685 if (HandleList
!= NULL
) {
686 FreePool(HandleList
);
688 if (DeviceNameToReturn
!= NULL
){
689 ASSERT(BestDeviceName
== NULL
);
690 StrnCatGrow(BestDeviceName
, NULL
, DeviceNameToReturn
, 0);
691 return (EFI_SUCCESS
);
694 // dont return on fail since we will try device path if that bit is on
697 if ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) != 0) {
698 Status
= gBS
->LocateProtocol(
699 &gEfiDevicePathToTextProtocolGuid
,
701 (VOID
**)&DevicePathToText
);
703 // we now have the device path to text protocol
705 if (!EFI_ERROR(Status
)) {
706 Status
= gBS
->OpenProtocol(
708 &gEfiDevicePathProtocolGuid
,
712 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
713 if (!EFI_ERROR(Status
)) {
715 // use device path to text on the device path
717 *BestDeviceName
= DevicePathToText
->ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
718 return (EFI_SUCCESS
);
723 // none of the selected bits worked.
725 return (EFI_NOT_FOUND
);
729 Opens the root directory of a device on a handle
731 This function opens the root directory of a device and returns a file handle to it.
733 @param DeviceHandle The handle of the device that contains the volume.
734 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
737 @retval EFI_SUCCESS Root opened successfully.
738 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
740 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
741 @retval EFI_DEVICE_ERROR The device had an error
745 EfiShellOpenRootByHandle(
746 IN EFI_HANDLE DeviceHandle
,
747 OUT SHELL_FILE_HANDLE
*FileHandle
751 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*SimpleFileSystem
;
752 EFI_FILE_PROTOCOL
*RealFileHandle
;
753 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
756 // get the simple file system interface
758 Status
= gBS
->OpenProtocol(DeviceHandle
,
759 &gEfiSimpleFileSystemProtocolGuid
,
760 (VOID
**)&SimpleFileSystem
,
763 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
764 if (EFI_ERROR(Status
)) {
765 return (EFI_NOT_FOUND
);
768 Status
= gBS
->OpenProtocol(DeviceHandle
,
769 &gEfiDevicePathProtocolGuid
,
773 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
774 if (EFI_ERROR(Status
)) {
775 return (EFI_NOT_FOUND
);
778 // Open the root volume now...
780 Status
= SimpleFileSystem
->OpenVolume(SimpleFileSystem
, &RealFileHandle
);
781 *FileHandle
= ConvertEfiFileProtocolToShellHandle(RealFileHandle
, EfiShellGetMapFromDevicePath(&DevPath
));
786 Opens the root directory of a device.
788 This function opens the root directory of a device and returns a file handle to it.
790 @param DevicePath Points to the device path corresponding to the device where the
791 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
792 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
795 @retval EFI_SUCCESS Root opened successfully.
796 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
798 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
799 @retval EFI_DEVICE_ERROR The device had an error
804 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
805 OUT SHELL_FILE_HANDLE
*FileHandle
812 // find the handle of the device with that device handle and the file system
815 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
818 if (EFI_ERROR(Status
)) {
819 return (EFI_NOT_FOUND
);
822 return (EfiShellOpenRootByHandle(Handle
, FileHandle
));
826 Returns whether any script files are currently being processed.
828 @retval TRUE There is at least one script file active.
829 @retval FALSE No script files are active now.
834 EfiShellBatchIsActive (
838 if (ShellCommandGetCurrentScriptFile() == NULL
) {
845 Worker function to open a file based on a device path. this will open the root
846 of the volume and then traverse down to the file itself.
848 @param DevicePath Device Path of the file.
849 @param FileHandle Pointer to the file upon a successful return.
850 @param OpenMode mode to open file in.
851 @param Attributes the File Attributes to use when creating a new file.
853 @retval EFI_SUCCESS the file is open and FileHandle is valid
854 @retval EFI_UNSUPPORTED the device path cotained non-path elements
855 @retval other an error ocurred.
859 InternalOpenFileDevicePath(
860 IN OUT EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
861 OUT SHELL_FILE_HANDLE
*FileHandle
,
863 IN UINT64 Attributes OPTIONAL
867 FILEPATH_DEVICE_PATH
*FilePathNode
;
869 SHELL_FILE_HANDLE ShellHandle
;
870 EFI_FILE_PROTOCOL
*Handle1
;
871 EFI_FILE_PROTOCOL
*Handle2
;
872 EFI_DEVICE_PATH_PROTOCOL
*DpCopy
;
874 ASSERT(FileHandle
!= NULL
);
879 Status
= EfiShellOpenRoot(DevicePath
, &ShellHandle
);
881 if (!EFI_ERROR(Status
)) {
882 Handle1
= ConvertShellHandleToEfiFileProtocol(ShellHandle
);
884 // chop off the begining part before the file system part...
887 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
890 if (!EFI_ERROR(Status
)) {
892 // To access as a file system, the file path should only
893 // contain file path components. Follow the file path nodes
894 // and find the target file
896 for ( FilePathNode
= (FILEPATH_DEVICE_PATH
*)DevicePath
897 ; !IsDevicePathEnd (&FilePathNode
->Header
)
898 ; FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
)
901 // For file system access each node should be a file path component
903 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
904 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
906 Status
= EFI_UNSUPPORTED
;
911 // Open this file path node
917 // if this is the last node in the DevicePath always create (if that was requested).
919 if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode
->Header
)))) {
920 Status
= Handle2
->Open (
923 FilePathNode
->PathName
,
930 // This is not the last node and we dont want to 'create' existing
931 // directory entries...
935 // open without letting it create
936 // prevents error on existing files/directories
938 Status
= Handle2
->Open (
941 FilePathNode
->PathName
,
942 OpenMode
&~EFI_FILE_MODE_CREATE
,
946 // if above failed now open and create the 'item'
947 // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)
949 if ((EFI_ERROR (Status
)) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)) {
950 Status
= Handle2
->Open (
953 FilePathNode
->PathName
,
960 // Close the last node
962 Handle2
->Close (Handle2
);
965 // If there's been an error, stop
967 if (EFI_ERROR (Status
)) {
973 if (EFI_ERROR(Status
)) {
974 if (Handle1
!= NULL
) {
975 Handle1
->Close(Handle1
);
978 *FileHandle
= ConvertEfiFileProtocolToShellHandle(Handle1
, ShellFileHandleGetPath(ShellHandle
));
984 Creates a file or directory by name.
986 This function creates an empty new file or directory with the specified attributes and
987 returns the new file's handle. If the file already exists and is read-only, then
988 EFI_INVALID_PARAMETER will be returned.
990 If the file already existed, it is truncated and its attributes updated. If the file is
991 created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.
993 If the file name begins with >v, then the file handle which is returned refers to the
994 shell environment variable with the specified name. If the shell environment variable
995 already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.
997 @param FileName Pointer to NULL-terminated file path
998 @param FileAttribs The new file's attrbiutes. the different attributes are
999 described in EFI_FILE_PROTOCOL.Open().
1000 @param FileHandle On return, points to the created file handle or directory's handle
1002 @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.
1003 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1004 @retval EFI_UNSUPPORTED could not open the file path
1005 @retval EFI_NOT_FOUND the specified file could not be found on the devide, or could not
1006 file the file system on the device.
1007 @retval EFI_NO_MEDIA the device has no medium.
1008 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1010 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1012 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1013 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1014 when the media is write-protected.
1015 @retval EFI_ACCESS_DENIED The service denied access to the file.
1016 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
1017 @retval EFI_VOLUME_FULL The volume is full.
1022 IN CONST CHAR16
*FileName
,
1023 IN UINT64 FileAttribs
,
1024 OUT SHELL_FILE_HANDLE
*FileHandle
1027 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1031 // Is this for an environment variable
1032 // do we start with >v
1034 if (StrStr(FileName
, L
">v") == FileName
) {
1035 if (!IsVolatileEnv(FileName
+2)) {
1036 return (EFI_INVALID_PARAMETER
);
1038 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1039 return (EFI_SUCCESS
);
1043 // We are opening a regular file.
1045 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1046 if (DevicePath
== NULL
) {
1047 return (EFI_NOT_FOUND
);
1050 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, FileAttribs
); // 0 = no specific file attributes
1051 FreePool(DevicePath
);
1057 Opens a file or a directory by file name.
1059 This function opens the specified file in the specified OpenMode and returns a file
1061 If the file name begins with >v, then the file handle which is returned refers to the
1062 shell environment variable with the specified name. If the shell environment variable
1063 exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then
1064 EFI_INVALID_PARAMETER is returned.
1066 If the file name is >i, then the file handle which is returned refers to the standard
1067 input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER
1070 If the file name is >o, then the file handle which is returned refers to the standard
1071 output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1074 If the file name is >e, then the file handle which is returned refers to the standard
1075 error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1078 If the file name is NUL, then the file handle that is returned refers to the standard NUL
1079 file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is
1082 If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the
1085 @param FileName Points to the NULL-terminated UCS-2 encoded file name.
1086 @param FileHandle On return, points to the file handle.
1087 @param OpenMode File open mode. Either EFI_FILE_MODE_READ or
1088 EFI_FILE_MODE_WRITE from section 12.4 of the UEFI
1090 @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.
1091 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.
1092 @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.
1093 @retval EFI_NOT_FOUND The specified file could not be found on the device or the file
1094 system could not be found on the device. FileHandle is NULL.
1095 @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.
1096 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1097 longer supported. FileHandle is NULL.
1098 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1099 the FileName. FileHandle is NULL.
1100 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.
1101 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1102 when the media is write-protected. FileHandle is NULL.
1103 @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.
1104 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle
1106 @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.
1110 EfiShellOpenFileByName(
1111 IN CONST CHAR16
*FileName
,
1112 OUT SHELL_FILE_HANDLE
*FileHandle
,
1116 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1122 // Is this for StdIn
1124 if (StrCmp(FileName
, L
">i") == 0) {
1126 // make sure not writing to StdIn
1128 if ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0) {
1129 return (EFI_INVALID_PARAMETER
);
1131 *FileHandle
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1132 ASSERT(*FileHandle
!= NULL
);
1133 return (EFI_SUCCESS
);
1137 // Is this for StdOut
1139 if (StrCmp(FileName
, L
">o") == 0) {
1141 // make sure not writing to StdIn
1143 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1144 return (EFI_INVALID_PARAMETER
);
1146 *FileHandle
= &FileInterfaceStdOut
;
1147 return (EFI_SUCCESS
);
1151 // Is this for NUL file
1153 if (StrCmp(FileName
, L
"NUL") == 0) {
1154 *FileHandle
= &FileInterfaceNulFile
;
1155 return (EFI_SUCCESS
);
1159 // Is this for StdErr
1161 if (StrCmp(FileName
, L
">e") == 0) {
1163 // make sure not writing to StdIn
1165 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1166 return (EFI_INVALID_PARAMETER
);
1168 *FileHandle
= &FileInterfaceStdErr
;
1169 return (EFI_SUCCESS
);
1173 // Is this for an environment variable
1174 // do we start with >v
1176 if (StrStr(FileName
, L
">v") == FileName
) {
1177 if (!IsVolatileEnv(FileName
+2) &&
1178 ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0)) {
1179 return (EFI_INVALID_PARAMETER
);
1181 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1182 return (EFI_SUCCESS
);
1186 // We are opening a regular file.
1188 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1189 // DEBUG_CODE(InternalShellProtocolDebugPrintMessage (NULL, DevicePath););
1190 if (DevicePath
== NULL
) {
1191 return (EFI_NOT_FOUND
);
1195 // Copy the device path, open the file, then free the memory
1197 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, OpenMode
, 0); // 0 = no specific file attributes
1198 FreePool(DevicePath
);
1204 Deletes the file specified by the file name.
1206 This function deletes a file.
1208 @param FileName Points to the NULL-terminated file name.
1210 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.
1211 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
1212 @sa EfiShellCreateFile
1216 EfiShellDeleteFileByName(
1217 IN CONST CHAR16
*FileName
1220 SHELL_FILE_HANDLE FileHandle
;
1224 // get a handle to the file
1226 Status
= EfiShellCreateFile(FileName
,
1229 if (EFI_ERROR(Status
)) {
1233 // now delete the file
1235 return (ShellInfoObject
.NewEfiShellProtocol
->DeleteFile(FileHandle
));
1239 Disables the page break output mode.
1243 EfiShellDisablePageBreak (
1247 ShellInfoObject
.PageBreakEnabled
= FALSE
;
1251 Enables the page break output mode.
1255 EfiShellEnablePageBreak (
1259 ShellInfoObject
.PageBreakEnabled
= TRUE
;
1263 internal worker function to load and run an image via device path.
1265 @param ParentImageHandle A handle of the image that is executing the specified
1267 @param DevicePath device path of the file to execute
1268 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1269 containing the command line. If NULL then the command-
1271 @param Environment Points to a NULL-terminated array of environment
1272 variables with the format 'x=y', where x is the
1273 environment variable name and y is the value. If this
1274 is NULL, then the current shell environment is used.
1275 @param StatusCode Points to the status code returned by the command.
1277 @retval EFI_SUCCESS The command executed successfully. The status code
1278 returned by the command is pointed to by StatusCode.
1279 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1280 @retval EFI_OUT_OF_RESOURCES Out of resources.
1281 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1285 InternalShellExecuteDevicePath(
1286 IN CONST EFI_HANDLE
*ParentImageHandle
,
1287 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1288 IN CONST CHAR16
*CommandLine OPTIONAL
,
1289 IN CONST CHAR16
**Environment OPTIONAL
,
1290 OUT EFI_STATUS
*StatusCode OPTIONAL
1294 EFI_HANDLE NewHandle
;
1295 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1296 LIST_ENTRY OrigEnvs
;
1297 EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol
;
1299 if (ParentImageHandle
== NULL
) {
1300 return (EFI_INVALID_PARAMETER
);
1303 InitializeListHead(&OrigEnvs
);
1308 // Load the image with:
1309 // FALSE - not from boot manager and NULL, 0 being not already in memory
1311 Status
= gBS
->LoadImage(
1314 (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
,
1319 if (EFI_ERROR(Status
)) {
1320 if (NewHandle
!= NULL
) {
1321 gBS
->UnloadImage(NewHandle
);
1325 Status
= gBS
->OpenProtocol(
1327 &gEfiLoadedImageProtocolGuid
,
1328 (VOID
**)&LoadedImage
,
1331 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1333 if (!EFI_ERROR(Status
)) {
1334 ASSERT(LoadedImage
->LoadOptionsSize
== 0);
1335 if (CommandLine
!= NULL
) {
1336 LoadedImage
->LoadOptionsSize
= (UINT32
)StrSize(CommandLine
);
1337 LoadedImage
->LoadOptions
= (VOID
*)CommandLine
;
1341 // Save our current environment settings for later restoration if necessary
1343 if (Environment
!= NULL
) {
1344 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1345 if (!EFI_ERROR(Status
)) {
1346 Status
= SetEnvironmentVariables(Environment
);
1351 // Initialize and install a shell parameters protocol on the image.
1353 ShellParamsProtocol
.StdIn
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1354 ShellParamsProtocol
.StdOut
= ShellInfoObject
.NewShellParametersProtocol
->StdOut
;
1355 ShellParamsProtocol
.StdErr
= ShellInfoObject
.NewShellParametersProtocol
->StdErr
;
1356 Status
= UpdateArgcArgv(&ShellParamsProtocol
, CommandLine
, NULL
, NULL
);
1357 ASSERT_EFI_ERROR(Status
);
1358 Status
= gBS
->InstallProtocolInterface(&NewHandle
, &gEfiShellParametersProtocolGuid
, EFI_NATIVE_INTERFACE
, &ShellParamsProtocol
);
1359 ASSERT_EFI_ERROR(Status
);
1361 ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)
1364 // now start the image and if the caller wanted the return code pass it to them...
1366 if (!EFI_ERROR(Status
)) {
1367 if (StatusCode
!= NULL
) {
1368 *StatusCode
= gBS
->StartImage(NewHandle
, NULL
, NULL
);
1370 Status
= gBS
->StartImage(NewHandle
, NULL
, NULL
);
1375 // Cleanup (and dont overwrite errors)
1377 if (EFI_ERROR(Status
)) {
1378 gBS
->UninstallProtocolInterface(NewHandle
, &gEfiShellParametersProtocolGuid
, &ShellParamsProtocol
);
1380 Status
= gBS
->UninstallProtocolInterface(NewHandle
, &gEfiShellParametersProtocolGuid
, &ShellParamsProtocol
);
1381 ASSERT_EFI_ERROR(Status
);
1385 if (!IsListEmpty(&OrigEnvs
)) {
1386 if (EFI_ERROR(Status
)) {
1387 SetEnvironmentVariableList(&OrigEnvs
);
1389 Status
= SetEnvironmentVariableList(&OrigEnvs
);
1396 Execute the command line.
1398 This function creates a nested instance of the shell and executes the specified
1399 command (CommandLine) with the specified environment (Environment). Upon return,
1400 the status code returned by the specified command is placed in StatusCode.
1402 If Environment is NULL, then the current environment is used and all changes made
1403 by the commands executed will be reflected in the current environment. If the
1404 Environment is non-NULL, then the changes made will be discarded.
1406 The CommandLine is executed from the current working directory on the current
1409 @param ParentImageHandle A handle of the image that is executing the specified
1411 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1412 containing the command line. If NULL then the command-
1414 @param Environment Points to a NULL-terminated array of environment
1415 variables with the format 'x=y', where x is the
1416 environment variable name and y is the value. If this
1417 is NULL, then the current shell environment is used.
1418 @param StatusCode Points to the status code returned by the command.
1420 @retval EFI_SUCCESS The command executed successfully. The status code
1421 returned by the command is pointed to by StatusCode.
1422 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1423 @retval EFI_OUT_OF_RESOURCES Out of resources.
1424 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1425 @retval EFI_UNSUPPORTED The support level required for this function is not present.
1427 @sa InternalShellExecuteDevicePath
1432 IN EFI_HANDLE
*ParentImageHandle
,
1433 IN CHAR16
*CommandLine OPTIONAL
,
1434 IN CHAR16
**Environment OPTIONAL
,
1435 OUT EFI_STATUS
*StatusCode OPTIONAL
1440 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1443 if ((PcdGet8(PcdShellSupportLevel
) < 1)) {
1444 return (EFI_UNSUPPORTED
);
1447 DevPath
= AppendDevicePath (ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
1450 Temp
= gDevPathToText
->ConvertDevicePathToText(ShellInfoObject
.FileDevPath
, TRUE
, TRUE
);
1452 Temp
= gDevPathToText
->ConvertDevicePathToText(ShellInfoObject
.ImageDevPath
, TRUE
, TRUE
);
1454 Temp
= gDevPathToText
->ConvertDevicePathToText(DevPath
, TRUE
, TRUE
);
1460 ASSERT((Temp
== NULL
&& Size
== 0) || (Temp
!= NULL
));
1461 StrnCatGrow(&Temp
, &Size
, L
"Shell.efi ", 0);
1462 StrnCatGrow(&Temp
, &Size
, CommandLine
, 0);
1464 Status
= InternalShellExecuteDevicePath(
1468 (CONST CHAR16
**)Environment
,
1472 // de-allocate and return
1480 Utility cleanup function for EFI_SHELL_FILE_INFO objects.
1482 1) frees all pointers (non-NULL)
1483 2) Closes the SHELL_FILE_HANDLE
1485 @param FileListNode pointer to the list node to free
1489 InternalFreeShellFileInfoNode(
1490 IN EFI_SHELL_FILE_INFO
*FileListNode
1493 if (FileListNode
->Info
!= NULL
) {
1494 FreePool((VOID
*)FileListNode
->Info
);
1496 if (FileListNode
->FileName
!= NULL
) {
1497 FreePool((VOID
*)FileListNode
->FileName
);
1499 if (FileListNode
->FullName
!= NULL
) {
1500 FreePool((VOID
*)FileListNode
->FullName
);
1502 if (FileListNode
->Handle
!= NULL
) {
1503 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileListNode
->Handle
);
1505 FreePool(FileListNode
);
1508 Frees the file list.
1510 This function cleans up the file list and any related data structures. It has no
1511 impact on the files themselves.
1513 @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is
1514 defined in OpenFileList()
1516 @retval EFI_SUCCESS Free the file list successfully.
1517 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1521 EfiShellFreeFileList(
1522 IN EFI_SHELL_FILE_INFO
**FileList
1525 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1527 if (FileList
== NULL
|| *FileList
== NULL
) {
1528 return (EFI_INVALID_PARAMETER
);
1531 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1532 ; !IsListEmpty(&(*FileList
)->Link
)
1533 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1535 RemoveEntryList(&ShellFileListItem
->Link
);
1536 InternalFreeShellFileInfoNode(ShellFileListItem
);
1538 return(EFI_SUCCESS
);
1542 Deletes the duplicate file names files in the given file list.
1544 This function deletes the reduplicate files in the given file list.
1546 @param FileList A pointer to the first entry in the file list.
1548 @retval EFI_SUCCESS Always success.
1549 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1553 EfiShellRemoveDupInFileList(
1554 IN EFI_SHELL_FILE_INFO
**FileList
1557 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1558 EFI_SHELL_FILE_INFO
*ShellFileListItem2
;
1560 if (FileList
== NULL
|| *FileList
== NULL
) {
1561 return (EFI_INVALID_PARAMETER
);
1563 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1564 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1565 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1567 for ( ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1568 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1569 ; ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1571 if (gUnicodeCollation
->StriColl(
1573 (CHAR16
*)ShellFileListItem
->FullName
,
1574 (CHAR16
*)ShellFileListItem2
->FullName
) == 0
1576 RemoveEntryList(&ShellFileListItem2
->Link
);
1577 InternalFreeShellFileInfoNode(ShellFileListItem2
);
1581 return (EFI_SUCCESS
);
1584 Allocates and duplicates a EFI_SHELL_FILE_INFO node.
1586 @param[in] Node The node to copy from.
1587 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.
1589 @retval NULL a memory allocation error ocurred
1590 @return != NULL a pointer to the new node
1592 EFI_SHELL_FILE_INFO
*
1594 InternalDuplicateShellFileInfo(
1595 IN EFI_SHELL_FILE_INFO
*Node
,
1599 EFI_SHELL_FILE_INFO
*NewNode
;
1601 NewNode
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
1602 if (NewNode
== NULL
) {
1605 NewNode
->FullName
= AllocateZeroPool(StrSize(Node
->FullName
));
1607 NewNode
->FileName
= AllocateZeroPool(StrSize(Node
->FileName
));
1608 NewNode
->Info
= AllocatePool((UINTN
)Node
->Info
->Size
);
1609 if ( NewNode
->FullName
== NULL
1610 || NewNode
->FileName
== NULL
1611 || NewNode
->Info
== NULL
1615 NewNode
->Status
= Node
->Status
;
1616 NewNode
->Handle
= Node
->Handle
;
1618 Node
->Handle
= NULL
;
1620 StrCpy((CHAR16
*)NewNode
->FullName
, Node
->FullName
);
1621 StrCpy((CHAR16
*)NewNode
->FileName
, Node
->FileName
);
1622 CopyMem(NewNode
->Info
, Node
->Info
, (UINTN
)Node
->Info
->Size
);
1628 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation
1629 failed it will return NULL.
1631 @param[in] BasePath the Path to prepend onto filename for FullPath
1632 @param[in] Status Status member initial value.
1633 @param[in] FullName FullName member initial value.
1634 @param[in] FileName FileName member initial value.
1635 @param[in] Handle Handle member initial value.
1636 @param[in] Info Info struct to copy.
1638 @retval NULL An error ocurred.
1639 @return a pointer to the newly allocated structure.
1641 EFI_SHELL_FILE_INFO
*
1643 CreateAndPopulateShellFileInfo(
1644 IN CONST CHAR16
*BasePath
,
1645 IN CONST EFI_STATUS Status
,
1646 IN CONST CHAR16
*FullName
,
1647 IN CONST CHAR16
*FileName
,
1648 IN CONST SHELL_FILE_HANDLE Handle
,
1649 IN CONST EFI_FILE_INFO
*Info
1652 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1659 ShellFileListItem
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1660 if (ShellFileListItem
== NULL
) {
1664 ShellFileListItem
->Info
= AllocateZeroPool((UINTN
)Info
->Size
);
1665 if (ShellFileListItem
->Info
== NULL
) {
1666 FreePool(ShellFileListItem
);
1669 CopyMem(ShellFileListItem
->Info
, Info
, (UINTN
)Info
->Size
);
1671 ShellFileListItem
->Info
= NULL
;
1673 if (FileName
!= NULL
) {
1674 ASSERT(TempString
== NULL
);
1675 ShellFileListItem
->FileName
= StrnCatGrow(&TempString
, 0, FileName
, 0);
1676 if (ShellFileListItem
->FileName
== NULL
) {
1677 FreePool(ShellFileListItem
->Info
);
1678 FreePool(ShellFileListItem
);
1682 ShellFileListItem
->FileName
= NULL
;
1686 if (BasePath
!= NULL
) {
1687 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1688 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1689 if (TempString
== NULL
) {
1690 FreePool((VOID
*)ShellFileListItem
->FileName
);
1691 FreePool(ShellFileListItem
->Info
);
1692 FreePool(ShellFileListItem
);
1696 if (ShellFileListItem
->FileName
!= NULL
) {
1697 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1698 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileListItem
->FileName
, 0);
1699 if (TempString
== NULL
) {
1700 FreePool((VOID
*)ShellFileListItem
->FileName
);
1701 FreePool(ShellFileListItem
->Info
);
1702 FreePool(ShellFileListItem
);
1707 ShellFileListItem
->FullName
= TempString
;
1708 ShellFileListItem
->Status
= Status
;
1709 ShellFileListItem
->Handle
= Handle
;
1711 return (ShellFileListItem
);
1715 Find all files in a specified directory.
1717 @param FileDirHandle Handle of the directory to search.
1718 @param FileList On return, points to the list of files in the directory
1719 or NULL if there are no files in the directory.
1721 @retval EFI_SUCCESS File information was returned successfully.
1722 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.
1723 @retval EFI_DEVICE_ERROR The device reported an error.
1724 @retval EFI_NO_MEDIA The device media is not present.
1725 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.
1726 @return An error from FileHandleGetFileName().
1730 EfiShellFindFilesInDir(
1731 IN SHELL_FILE_HANDLE FileDirHandle
,
1732 OUT EFI_SHELL_FILE_INFO
**FileList
1735 EFI_SHELL_FILE_INFO
*ShellFileList
;
1736 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1737 EFI_FILE_INFO
*FileInfo
;
1745 Status
= FileHandleGetFileName(FileDirHandle
, &BasePath
);
1746 if (EFI_ERROR(Status
)) {
1750 if (ShellFileHandleGetPath(FileDirHandle
) != NULL
) {
1753 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileHandleGetPath(FileDirHandle
), 0);
1754 TempSpot
= StrStr(TempString
, L
";");
1756 if (TempSpot
!= NULL
) {
1757 *TempSpot
= CHAR_NULL
;
1760 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1761 BasePath
= TempString
;
1765 ShellFileList
= NULL
;
1766 ShellFileListItem
= NULL
;
1768 Status
= EFI_SUCCESS
;
1771 for ( Status
= FileHandleFindFirstFile(FileDirHandle
, &FileInfo
)
1772 ; !EFI_ERROR(Status
) && !NoFile
1773 ; Status
= FileHandleFindNextFile(FileDirHandle
, FileInfo
, &NoFile
)
1778 // allocate a new EFI_SHELL_FILE_INFO and populate it...
1780 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1781 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1782 TempString
= StrnCatGrow(&TempString
, &Size
, FileInfo
->FileName
, 0);
1783 ShellFileListItem
= CreateAndPopulateShellFileInfo(
1785 EFI_SUCCESS
, // success since we didnt fail to open it...
1788 NULL
, // no handle since not open
1791 if (ShellFileList
== NULL
) {
1792 ShellFileList
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1793 ASSERT(ShellFileList
!= NULL
);
1794 InitializeListHead(&ShellFileList
->Link
);
1796 InsertTailList(&ShellFileList
->Link
, &ShellFileListItem
->Link
);
1798 if (EFI_ERROR(Status
)) {
1799 EfiShellFreeFileList(&ShellFileList
);
1802 *FileList
= ShellFileList
;
1804 SHELL_FREE_NON_NULL(BasePath
);
1809 Updates a file name to be preceeded by the mapped drive name
1811 @param[in] BasePath the Mapped drive name to prepend
1812 @param[in,out] Path pointer to pointer to the file name to update.
1815 @retval EFI_OUT_OF_RESOURCES
1820 IN CONST CHAR16
*BasePath
,
1821 IN OUT CHAR16
**Path
1830 ASSERT(Path
!= NULL
);
1831 ASSERT(*Path
!= NULL
);
1832 ASSERT(BasePath
!= NULL
);
1835 // convert a local path to an absolute path
1837 if (StrStr(*Path
, L
":") == NULL
) {
1838 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
1839 StrnCatGrow(&Path2
, &Path2Size
, BasePath
, 0);
1840 if (Path2
== NULL
) {
1841 return (EFI_OUT_OF_RESOURCES
);
1843 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
1844 StrnCatGrow(&Path2
, &Path2Size
, (*Path
)[0] == L
'\\'?(*Path
) + 1 :*Path
, 0);
1845 if (Path2
== NULL
) {
1846 return (EFI_OUT_OF_RESOURCES
);
1853 return (EFI_SUCCESS
);
1857 If FileHandle is a directory then the function reads from FileHandle and reads in
1858 each of the FileInfo structures. If one of them matches the Pattern's first
1859 "level" then it opens that handle and calls itself on that handle.
1861 If FileHandle is a file and matches all of the remaining Pattern (which would be
1862 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.
1864 if FileList is NULL, then ASSERT
1865 if FilePattern is NULL, then ASSERT
1866 if UnicodeCollation is NULL, then ASSERT
1867 if FileHandle is NULL, then ASSERT
1869 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call
1870 FreeFileList with FileList.
1872 @param[in] FilePattern The FilePattern to check against.
1873 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure
1874 @param[in] FileHandle The FileHandle to start with
1875 @param[in,out] FileList pointer to pointer to list of found files.
1876 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.
1878 @retval EFI_SUCCESS all files were found and the FileList contains a list.
1879 @retval EFI_NOT_FOUND no files were found
1880 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
1885 IN CONST CHAR16
*FilePattern
,
1886 IN EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
,
1887 IN SHELL_FILE_HANDLE FileHandle
,
1888 IN OUT EFI_SHELL_FILE_INFO
**FileList
,
1889 IN CONST EFI_SHELL_FILE_INFO
*ParentNode OPTIONAL
1893 CONST CHAR16
*NextFilePatternStart
;
1894 CHAR16
*CurrentFilePattern
;
1895 EFI_SHELL_FILE_INFO
*ShellInfo
;
1896 EFI_SHELL_FILE_INFO
*ShellInfoNode
;
1897 EFI_SHELL_FILE_INFO
*NewShellNode
;
1900 if ( FilePattern
== NULL
1901 || UnicodeCollation
== NULL
1904 return (EFI_INVALID_PARAMETER
);
1907 CurrentFilePattern
= NULL
;
1909 if (*FilePattern
== L
'\\') {
1913 for( NextFilePatternStart
= FilePattern
1914 ; *NextFilePatternStart
!= CHAR_NULL
&& *NextFilePatternStart
!= L
'\\'
1915 ; NextFilePatternStart
++);
1917 CurrentFilePattern
= AllocateZeroPool((NextFilePatternStart
-FilePattern
+1)*sizeof(CHAR16
));
1918 ASSERT(CurrentFilePattern
!= NULL
);
1919 StrnCpy(CurrentFilePattern
, FilePattern
, NextFilePatternStart
-FilePattern
);
1921 if (CurrentFilePattern
[0] == CHAR_NULL
1922 &&NextFilePatternStart
[0] == CHAR_NULL
1925 // Add the current parameter FileHandle to the list, then end...
1927 if (ParentNode
== NULL
) {
1928 Status
= EFI_INVALID_PARAMETER
;
1930 NewShellNode
= InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO
*)ParentNode
, TRUE
);
1931 if (NewShellNode
== NULL
) {
1932 Status
= EFI_OUT_OF_RESOURCES
;
1934 NewShellNode
->Handle
= NULL
;
1935 if (*FileList
== NULL
) {
1936 *FileList
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
1937 InitializeListHead(&((*FileList
)->Link
));
1941 // Add to the returning to use list
1943 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
1945 Status
= EFI_SUCCESS
;
1949 Status
= EfiShellFindFilesInDir(FileHandle
, &ShellInfo
);
1951 if (!EFI_ERROR(Status
)){
1952 if (StrStr(NextFilePatternStart
, L
"\\") != NULL
){
1957 for ( ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ShellInfo
->Link
)
1958 ; !IsNull (&ShellInfo
->Link
, &ShellInfoNode
->Link
)
1959 ; ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ShellInfo
->Link
, &ShellInfoNode
->Link
)
1961 if (UnicodeCollation
->MetaiMatch(UnicodeCollation
, (CHAR16
*)ShellInfoNode
->FileName
, CurrentFilePattern
)){
1964 // should be a directory
1968 // don't open the . and .. directories
1970 if ( (StrCmp(ShellInfoNode
->FileName
, L
".") != 0)
1971 && (StrCmp(ShellInfoNode
->FileName
, L
"..") != 0)
1976 ASSERT_EFI_ERROR(Status
);
1977 if (EFI_ERROR(Status
)) {
1981 // Open the directory since we need that handle in the next recursion.
1983 ShellInfoNode
->Status
= EfiShellOpenFileByName (ShellInfoNode
->FullName
, &ShellInfoNode
->Handle
, EFI_FILE_MODE_READ
);
1986 // recurse with the next part of the pattern
1988 Status
= ShellSearchHandle(NextFilePatternStart
, UnicodeCollation
, ShellInfoNode
->Handle
, FileList
, ShellInfoNode
);
1996 // copy the information we need into a new Node
1998 NewShellNode
= InternalDuplicateShellFileInfo(ShellInfoNode
, FALSE
);
1999 ASSERT(NewShellNode
!= NULL
);
2000 if (NewShellNode
== NULL
) {
2001 Status
= EFI_OUT_OF_RESOURCES
;
2003 if (*FileList
== NULL
) {
2004 *FileList
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
2005 InitializeListHead(&((*FileList
)->Link
));
2009 // Add to the returning to use list
2011 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2014 if (EFI_ERROR(Status
)) {
2018 if (EFI_ERROR(Status
)) {
2019 EfiShellFreeFileList(&ShellInfo
);
2021 Status
= EfiShellFreeFileList(&ShellInfo
);
2026 FreePool(CurrentFilePattern
);
2031 Find files that match a specified pattern.
2033 This function searches for all files and directories that match the specified
2034 FilePattern. The FilePattern can contain wild-card characters. The resulting file
2035 information is placed in the file list FileList.
2037 Wildcards are processed
2038 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.
2040 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo
2041 field is set to NULL.
2043 if *FileList is not NULL then it must be a pre-existing and properly initialized list.
2045 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.
2046 @param FileList On return, points to the start of a file list containing the names
2047 of all matching files or else points to NULL if no matching files
2048 were found. only on a EFI_SUCCESS return will; this be non-NULL.
2050 @retval EFI_SUCCESS Files found. FileList is a valid list.
2051 @retval EFI_NOT_FOUND No files found.
2052 @retval EFI_NO_MEDIA The device has no media
2053 @retval EFI_DEVICE_ERROR The device reported an error
2054 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted
2059 IN CONST CHAR16
*FilePattern
,
2060 OUT EFI_SHELL_FILE_INFO
**FileList
2064 CHAR16
*PatternCopy
;
2065 CHAR16
*PatternCurrentLocation
;
2066 EFI_DEVICE_PATH_PROTOCOL
*RootDevicePath
;
2067 SHELL_FILE_HANDLE RootFileHandle
;
2071 if ( FilePattern
== NULL
2073 || StrStr(FilePattern
, L
":") == NULL
2075 return (EFI_INVALID_PARAMETER
);
2077 Status
= EFI_SUCCESS
;
2078 RootDevicePath
= NULL
;
2079 RootFileHandle
= NULL
;
2081 PatternCopy
= AllocatePool(StrSize(FilePattern
));
2082 if (PatternCopy
== NULL
) {
2083 return (EFI_OUT_OF_RESOURCES
);
2085 StrCpy(PatternCopy
, FilePattern
);
2087 PatternCopy
= CleanPath(PatternCopy
);
2089 Count
= StrStr(PatternCopy
, L
":") - PatternCopy
;
2092 ASSERT(MapName
== NULL
);
2093 MapName
= StrnCatGrow(&MapName
, NULL
, PatternCopy
, Count
);
2095 if (!EFI_ERROR(Status
)) {
2096 RootDevicePath
= EfiShellGetDevicePathFromFilePath(PatternCopy
);
2097 if (RootDevicePath
== NULL
) {
2098 Status
= EFI_INVALID_PARAMETER
;
2100 Status
= EfiShellOpenRoot(RootDevicePath
, &RootFileHandle
);
2101 if (!EFI_ERROR(Status
)) {
2102 for ( PatternCurrentLocation
= PatternCopy
2103 ; *PatternCurrentLocation
!= ':'
2104 ; PatternCurrentLocation
++);
2105 PatternCurrentLocation
++;
2106 Status
= ShellSearchHandle(PatternCurrentLocation
, gUnicodeCollation
, RootFileHandle
, FileList
, NULL
);
2108 FreePool(RootDevicePath
);
2112 if (PatternCopy
!= NULL
) {
2113 FreePool(PatternCopy
);
2115 if (MapName
!= NULL
) {
2123 Opens the files that match the path specified.
2125 This function opens all of the files specified by Path. Wildcards are processed
2126 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each
2127 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.
2129 @param Path A pointer to the path string.
2130 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or
2131 EFI_FILE_MODE_WRITE.
2132 @param FileList Points to the start of a list of files opened.
2134 @retval EFI_SUCCESS Create the file list successfully.
2135 @return Others Can't create the file list.
2139 EfiShellOpenFileList(
2142 IN OUT EFI_SHELL_FILE_INFO
**FileList
2146 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2149 CONST CHAR16
*CurDir
;
2151 ShellCommandCleanPath(Path
);
2156 ASSERT(FileList
!= NULL
);
2157 ASSERT(*FileList
!= NULL
);
2159 if (*Path
== L
'.' && *(Path
+1) == L
'\\') {
2164 // convert a local path to an absolute path
2166 if (StrStr(Path
, L
":") == NULL
) {
2167 CurDir
= EfiShellGetCurDir(NULL
);
2168 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2169 StrnCatGrow(&Path2
, &Path2Size
, CurDir
, 0);
2170 if (*Path
== L
'\\') {
2173 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2174 StrnCatGrow(&Path2
, &Path2Size
, Path
, 0);
2176 ASSERT(Path2
== NULL
);
2177 StrnCatGrow(&Path2
, NULL
, Path
, 0);
2185 Status
= EfiShellFindFiles(Path2
, FileList
);
2189 if (EFI_ERROR(Status
)) {
2194 // We had no errors so open all the files (that are not already opened...)
2196 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
2197 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2198 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2200 if (ShellFileListItem
->Status
== 0 && ShellFileListItem
->Handle
== NULL
) {
2201 ShellFileListItem
->Status
= EfiShellOpenFileByName (ShellFileListItem
->FullName
, &ShellFileListItem
->Handle
, OpenMode
);
2205 return(EFI_SUCCESS
);
2209 This function updated with errata.
2211 Gets either a single or list of environment variables.
2213 If name is not NULL then this function returns the current value of the specified
2214 environment variable.
2216 If Name is NULL, then a list of all environment variable names is returned. Each is a
2217 NULL terminated string with a double NULL terminating the list.
2219 @param Name A pointer to the environment variable name. If
2220 Name is NULL, then the function will return all
2221 of the defined shell environment variables. In
2222 the case where multiple environment variables are
2223 being returned, each variable will be terminated by
2224 a NULL, and the list will be terminated by a double
2227 @return !=NULL A pointer to the returned string.
2228 The returned pointer does not need to be freed by the caller.
2230 @retval NULL The environment variable doesn't exist or there are
2231 no environment variables.
2236 IN CONST CHAR16
*Name
2244 CHAR16
*CurrentWriteLocation
;
2251 // Get all our environment variables
2253 InitializeListHead(&List
);
2254 Status
= GetEnvironmentVariableList(&List
);
2255 if (EFI_ERROR(Status
)){
2260 // Build the semi-colon delimited list. (2 passes)
2262 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&List
)
2263 ; !IsNull(&List
, &Node
->Link
)
2264 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&List
, &Node
->Link
)
2266 ASSERT(Node
->Key
!= NULL
);
2267 Size
+= StrSize(Node
->Key
);
2270 Size
+= 2*sizeof(CHAR16
);
2272 Buffer
= AllocateZeroPool(Size
);
2273 CurrentWriteLocation
= (CHAR16
*)Buffer
;
2275 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&List
)
2276 ; !IsNull(&List
, &Node
->Link
)
2277 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&List
, &Node
->Link
)
2279 ASSERT(Node
->Key
!= NULL
);
2280 StrCpy(CurrentWriteLocation
, Node
->Key
);
2281 CurrentWriteLocation
+= StrLen(CurrentWriteLocation
) + 1;
2287 FreeEnvironmentVariableList(&List
);
2290 // We are doing a specific environment variable
2294 // get the size we need for this EnvVariable
2296 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(Name
, &Size
, Buffer
);
2297 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2299 // Allocate the space and recall the get function
2301 Buffer
= AllocateZeroPool(Size
);
2302 ASSERT(Buffer
!= NULL
);
2303 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(Name
, &Size
, Buffer
);
2306 // we didnt get it (might not exist)
2307 // free the memory if we allocated any and return NULL
2309 if (EFI_ERROR(Status
)) {
2310 if (Buffer
!= NULL
) {
2318 // return the buffer
2320 return (AddBufferToFreeList(Buffer
));
2324 Internal variable setting function. Allows for setting of the read only variables.
2326 @param Name Points to the NULL-terminated environment variable name.
2327 @param Value Points to the NULL-terminated environment variable value. If the value is an
2328 empty string then the environment variable is deleted.
2329 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2331 @retval EFI_SUCCESS The environment variable was successfully updated.
2335 InternalEfiShellSetEnv(
2336 IN CONST CHAR16
*Name
,
2337 IN CONST CHAR16
*Value
,
2341 if (Value
== NULL
|| StrLen(Value
) == 0) {
2342 return (SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
));
2344 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2346 return (SHELL_SET_ENVIRONMENT_VARIABLE_V(Name
, StrSize(Value
), Value
));
2348 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(Name
, StrSize(Value
), Value
));
2354 Sets the environment variable.
2356 This function changes the current value of the specified environment variable. If the
2357 environment variable exists and the Value is an empty string, then the environment
2358 variable is deleted. If the environment variable exists and the Value is not an empty
2359 string, then the value of the environment variable is changed. If the environment
2360 variable does not exist and the Value is an empty string, there is no action. If the
2361 environment variable does not exist and the Value is a non-empty string, then the
2362 environment variable is created and assigned the specified value.
2364 For a description of volatile and non-volatile environment variables, see UEFI Shell
2365 2.0 specification section 3.6.1.
2367 @param Name Points to the NULL-terminated environment variable name.
2368 @param Value Points to the NULL-terminated environment variable value. If the value is an
2369 empty string then the environment variable is deleted.
2370 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2372 @retval EFI_SUCCESS The environment variable was successfully updated.
2377 IN CONST CHAR16
*Name
,
2378 IN CONST CHAR16
*Value
,
2382 if (Name
== NULL
|| *Name
== CHAR_NULL
) {
2383 return (EFI_INVALID_PARAMETER
);
2386 // Make sure we dont 'set' a predefined read only variable
2388 if (gUnicodeCollation
->StriColl(
2392 ||gUnicodeCollation
->StriColl(
2396 ||gUnicodeCollation
->StriColl(
2400 ||gUnicodeCollation
->StriColl(
2403 L
"uefishellsupport") == 0
2404 ||gUnicodeCollation
->StriColl(
2407 L
"uefishellversion") == 0
2408 ||gUnicodeCollation
->StriColl(
2411 L
"uefiversion") == 0
2413 return (EFI_INVALID_PARAMETER
);
2415 return (InternalEfiShellSetEnv(Name
, Value
, Volatile
));
2419 Returns the current directory on the specified device.
2421 If FileSystemMapping is NULL, it returns the current working directory. If the
2422 FileSystemMapping is not NULL, it returns the current directory associated with the
2423 FileSystemMapping. In both cases, the returned name includes the file system
2424 mapping (i.e. fs0:\current-dir).
2426 @param FileSystemMapping A pointer to the file system mapping. If NULL,
2427 then the current working directory is returned.
2429 @retval !=NULL The current directory.
2430 @retval NULL Current directory does not exist.
2435 IN CONST CHAR16
*FileSystemMapping OPTIONAL
2438 CHAR16
*PathToReturn
;
2440 SHELL_MAP_LIST
*MapListItem
;
2441 if (!IsListEmpty(&gShellMapList
.Link
)) {
2443 // if parameter is NULL, use current
2445 if (FileSystemMapping
== NULL
) {
2446 return (EfiShellGetEnv(L
"cwd"));
2449 PathToReturn
= NULL
;
2450 MapListItem
= ShellCommandFindMapItem(FileSystemMapping
);
2451 if (MapListItem
!= NULL
) {
2452 ASSERT((PathToReturn
== NULL
&& Size
== 0) || (PathToReturn
!= NULL
));
2453 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->MapName
, 0);
2454 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
2457 return (AddBufferToFreeList(PathToReturn
));
2464 Changes the current directory on the specified device.
2466 If the FileSystem is NULL, and the directory Dir does not contain a file system's
2467 mapped name, this function changes the current working directory.
2469 If the FileSystem is NULL and the directory Dir contains a mapped name, then the
2470 current file system and the current directory on that file system are changed.
2472 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file
2475 If FileSystem is not NULL and Dir is not NULL, then this function changes the current
2476 directory on the specified file system.
2478 If the current working directory or the current working file system is changed then the
2479 %cwd% environment variable will be updated
2481 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working
2482 directory is changed.
2483 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.
2485 @retval EFI_SUCCESS The operation was sucessful
2486 @retval EFI_NOT_FOUND The file system could not be found
2491 IN CONST CHAR16
*FileSystem OPTIONAL
,
2492 IN CONST CHAR16
*Dir
2496 SHELL_MAP_LIST
*MapListItem
;
2500 CHAR16
*DirectoryName
;
2507 DirectoryName
= NULL
;
2509 if (FileSystem
== NULL
&& Dir
== NULL
) {
2510 return (EFI_INVALID_PARAMETER
);
2513 if (IsListEmpty(&gShellMapList
.Link
)){
2514 return (EFI_NOT_FOUND
);
2517 DirectoryName
= StrnCatGrow(&DirectoryName
, NULL
, Dir
, 0);
2518 ASSERT(DirectoryName
!= NULL
);
2520 CleanPath(DirectoryName
);
2522 if (FileSystem
== NULL
) {
2524 // determine the file system mapping to use
2526 if (StrStr(DirectoryName
, L
":") != NULL
) {
2527 ASSERT(MapName
== NULL
);
2528 MapName
= StrnCatGrow(&MapName
, NULL
, DirectoryName
, (StrStr(DirectoryName
, L
":")-DirectoryName
+1));
2531 // find the file system mapping's entry in the list
2534 if (MapName
!= NULL
) {
2535 MapListItem
= ShellCommandFindMapItem(MapName
);
2538 // make that the current file system mapping
2540 if (MapListItem
!= NULL
) {
2541 gShellCurDir
= MapListItem
;
2544 MapListItem
= gShellCurDir
;
2547 if (MapListItem
== NULL
) {
2548 return (EFI_NOT_FOUND
);
2552 // now update the MapListItem's current directory
2554 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& DirectoryName
[StrLen(DirectoryName
) - 1] != L
':') {
2555 FreePool(MapListItem
->CurrentDirectoryPath
);
2556 MapListItem
->CurrentDirectoryPath
= NULL
;
2558 if (MapName
!= NULL
) {
2559 TempLen
= StrLen(MapName
);
2560 if (TempLen
!= StrLen(DirectoryName
)) {
2561 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2562 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
+StrLen(MapName
), 0);
2565 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2566 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
2568 if ((MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] != L
'\\') || (MapListItem
->CurrentDirectoryPath
== NULL
)) {
2569 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2570 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2574 // cant have a mapping in the directory...
2576 if (StrStr(DirectoryName
, L
":") != NULL
) {
2577 return (EFI_INVALID_PARAMETER
);
2580 // FileSystem != NULL
2582 MapListItem
= ShellCommandFindMapItem(FileSystem
);
2583 if (MapListItem
== NULL
) {
2584 return (EFI_INVALID_PARAMETER
);
2586 // gShellCurDir = MapListItem;
2587 if (DirectoryName
!= NULL
) {
2589 // change current dir on that file system
2592 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
2593 FreePool(MapListItem
->CurrentDirectoryPath
);
2594 DEBUG_CODE(MapListItem
->CurrentDirectoryPath
= NULL
;);
2596 // ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2597 // MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);
2598 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2599 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2600 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2601 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
2602 if (MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] != L
'\\') {
2603 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2604 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2609 // if updated the current directory then update the environment variable
2611 if (MapListItem
== gShellCurDir
) {
2613 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2614 StrnCatGrow(&TempString
, &Size
, MapListItem
->MapName
, 0);
2615 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2616 StrnCatGrow(&TempString
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
2617 Status
= InternalEfiShellSetEnv(L
"cwd", TempString
, TRUE
);
2618 FreePool(TempString
);
2621 return(EFI_SUCCESS
);
2625 Return help information about a specific command.
2627 This function returns the help information for the specified command. The help text
2628 can be internal to the shell or can be from a UEFI Shell manual page.
2630 If Sections is specified, then each section name listed will be compared in a casesensitive
2631 manner, to the section names described in Appendix B. If the section exists,
2632 it will be appended to the returned help text. If the section does not exist, no
2633 information will be returned. If Sections is NULL, then all help text information
2634 available will be returned.
2636 @param Command Points to the NULL-terminated UEFI Shell command name.
2637 @param Sections Points to the NULL-terminated comma-delimited
2638 section names to return. If NULL, then all
2639 sections will be returned.
2640 @param HelpText On return, points to a callee-allocated buffer
2641 containing all specified help text.
2643 @retval EFI_SUCCESS The help text was returned.
2644 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
2646 @retval EFI_INVALID_PARAMETER HelpText is NULL
2647 @retval EFI_NOT_FOUND There is no help text available for Command.
2651 EfiShellGetHelpText(
2652 IN CONST CHAR16
*Command
,
2653 IN CONST CHAR16
*Sections OPTIONAL
,
2654 OUT CHAR16
**HelpText
2657 CONST CHAR16
*ManFileName
;
2659 ASSERT(HelpText
!= NULL
);
2661 ManFileName
= ShellCommandGetManFileNameHandler(Command
);
2663 if (ManFileName
!= NULL
) {
2664 return (ProcessManFile(ManFileName
, Command
, Sections
, NULL
, HelpText
));
2666 return (ProcessManFile(Command
, Command
, Sections
, NULL
, HelpText
));
2671 Gets the enable status of the page break output mode.
2673 User can use this function to determine current page break mode.
2675 @retval TRUE The page break output mode is enabled.
2676 @retval FALSE The page break output mode is disabled.
2680 EfiShellGetPageBreak(
2684 return(ShellInfoObject
.PageBreakEnabled
);
2688 Judges whether the active shell is the root shell.
2690 This function makes the user to know that whether the active Shell is the root shell.
2692 @retval TRUE The active Shell is the root Shell.
2693 @retval FALSE The active Shell is NOT the root Shell.
2697 EfiShellIsRootShell(
2701 return(ShellInfoObject
.RootShellInstance
);
2705 function to return a semi-colon delimeted list of all alias' in the current shell
2707 up to caller to free the memory.
2709 @retval NULL No alias' were found
2710 @retval NULL An error ocurred getting alias'
2711 @return !NULL a list of all alias'
2715 InternalEfiShellGetListAlias(
2723 CHAR16
*VariableName
;
2729 Status
= gRT
->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
, &MaxStorSize
, &RemStorSize
, &MaxVarSize
);
2730 ASSERT_EFI_ERROR(Status
);
2732 VariableName
= AllocateZeroPool((UINTN
)MaxVarSize
);
2736 VariableName
[0] = CHAR_NULL
;
2739 NameSize
= (UINTN
)MaxVarSize
;
2740 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
2741 if (Status
== EFI_NOT_FOUND
){
2744 ASSERT_EFI_ERROR(Status
);
2745 if (EFI_ERROR(Status
)) {
2748 if (CompareGuid(&Guid
, &gShellAliasGuid
)){
2749 Alias
= GetVariable(VariableName
, &gShellAliasGuid
);
2750 ASSERT((RetVal
== NULL
&& RetSize
== 0) || (RetVal
!= NULL
));
2751 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, VariableName
, 0);
2752 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, L
";", 0);
2755 FreePool(VariableName
);
2761 This function returns the command associated with a alias or a list of all
2764 @param[in] Alias Points to the NULL-terminated shell alias.
2765 If this parameter is NULL, then all
2766 aliases will be returned in ReturnedData.
2767 @param[out] Volatile upon return of a single command if TRUE indicates
2768 this is stored in a volatile fashion. FALSE otherwise.
2770 @return If Alias is not NULL, it will return a pointer to
2771 the NULL-terminated command for that alias.
2772 If Alias is NULL, ReturnedData points to a ';'
2773 delimited list of alias (e.g.
2774 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.
2775 @retval NULL an error ocurred
2776 @retval NULL Alias was not a valid Alias
2781 IN CONST CHAR16
*Alias
,
2782 OUT BOOLEAN
*Volatile OPTIONAL
2790 if (Alias
!= NULL
) {
2791 if (Volatile
== NULL
) {
2792 return (AddBufferToFreeList(GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
)));
2796 Status
= gRT
->GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
2797 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2798 RetVal
= AllocateZeroPool(RetSize
);
2799 Status
= gRT
->GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
2801 if (EFI_ERROR(Status
)) {
2802 if (RetVal
!= NULL
) {
2807 if ((EFI_VARIABLE_NON_VOLATILE
& Attribs
) == EFI_VARIABLE_NON_VOLATILE
) {
2813 return (AddBufferToFreeList(RetVal
));
2815 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));
2819 Changes a shell command alias.
2821 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
2823 this function does not check for built in alias'.
2825 @param[in] Command Points to the NULL-terminated shell command or existing alias.
2826 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
2827 Command refers to an alias, that alias will be deleted.
2828 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
2829 Alias being set will be stored in a non-volatile fashion.
2831 @retval EFI_SUCCESS Alias created or deleted successfully.
2832 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
2837 IN CONST CHAR16
*Command
,
2838 IN CONST CHAR16
*Alias
,
2843 // We must be trying to remove one if Alias is NULL
2845 if (Alias
== NULL
) {
2847 // remove an alias (but passed in COMMAND parameter)
2849 return (gRT
->SetVariable((CHAR16
*)Command
, &gShellAliasGuid
, 0, 0, NULL
));
2852 // Add and replace are the same
2855 // We dont check the error return on purpose since the variable may not exist.
2856 gRT
->SetVariable((CHAR16
*)Command
, &gShellAliasGuid
, 0, 0, NULL
);
2858 return (gRT
->SetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, EFI_VARIABLE_BOOTSERVICE_ACCESS
|(Volatile
?0:EFI_VARIABLE_NON_VOLATILE
), StrSize(Command
), (VOID
*)Command
));
2863 Changes a shell command alias.
2865 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
2868 @param[in] Command Points to the NULL-terminated shell command or existing alias.
2869 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
2870 Command refers to an alias, that alias will be deleted.
2871 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
2872 FALSE and the alias already exists, then the existing alias is unchanged and
2873 EFI_ACCESS_DENIED is returned.
2874 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
2875 Alias being set will be stored in a non-volatile fashion.
2877 @retval EFI_SUCCESS Alias created or deleted successfully.
2878 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
2879 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
2885 IN CONST CHAR16
*Command
,
2886 IN CONST CHAR16
*Alias
,
2892 // cant set over a built in alias
2894 if (ShellCommandIsOnAliasList(Alias
==NULL
?Command
:Alias
)) {
2895 return (EFI_ACCESS_DENIED
);
2897 if (Command
== NULL
|| *Command
== CHAR_NULL
|| StrLen(Command
) == 0) {
2898 return (EFI_INVALID_PARAMETER
);
2901 if (EfiShellGetAlias(Command
, NULL
) != NULL
&& !Replace
) {
2902 return (EFI_ACCESS_DENIED
);
2905 return (InternalSetAlias(Command
, Alias
, Volatile
));
2908 // Pure FILE_HANDLE operations are passed to FileHandleLib
2909 // these functions are indicated by the *
2910 EFI_SHELL_PROTOCOL mShellProtocol
= {
2916 EfiShellGetHelpText
,
2917 EfiShellGetDevicePathFromMap
,
2918 EfiShellGetMapFromDevicePath
,
2919 EfiShellGetDevicePathFromFilePath
,
2920 EfiShellGetFilePathFromDevicePath
,
2924 EfiShellOpenFileList
,
2925 EfiShellFreeFileList
,
2926 EfiShellRemoveDupInFileList
,
2927 EfiShellBatchIsActive
,
2928 EfiShellIsRootShell
,
2929 EfiShellEnablePageBreak
,
2930 EfiShellDisablePageBreak
,
2931 EfiShellGetPageBreak
,
2932 EfiShellGetDeviceName
,
2933 (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
, //*
2934 (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
, //*
2935 EfiShellOpenFileByName
,
2938 (EFI_SHELL_READ_FILE
)FileHandleRead
, //*
2939 (EFI_SHELL_WRITE_FILE
)FileHandleWrite
, //*
2940 (EFI_SHELL_DELETE_FILE
)FileHandleDelete
, //*
2941 EfiShellDeleteFileByName
,
2942 (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
, //*
2943 (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
, //*
2944 (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
, //*
2946 EfiShellFindFilesInDir
,
2947 (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
, //*
2949 EfiShellOpenRootByHandle
,
2951 SHELL_MAJOR_VERSION
,
2956 Function to create and install on the current handle.
2958 Will overwrite any existing ShellProtocols in the system to be sure that
2959 the current shell is in control.
2961 This must be removed via calling CleanUpShellProtocol().
2963 @param[in,out] NewShell The pointer to the pointer to the structure
2966 @retval EFI_SUCCESS The operation was successful.
2967 @return An error from LocateHandle, CreateEvent, or other core function.
2971 CreatePopulateInstallShellProtocol (
2972 IN OUT EFI_SHELL_PROTOCOL
**NewShell
2978 UINTN HandleCounter
;
2979 SHELL_PROTOCOL_HANDLE_LIST
*OldProtocolNode
;
2983 OldProtocolNode
= NULL
;
2984 InitializeListHead(&ShellInfoObject
.OldShellList
.Link
);
2986 ASSERT(NewShell
!= NULL
);
2989 // Initialize EfiShellProtocol object...
2991 *NewShell
= &mShellProtocol
;
2992 Status
= gBS
->CreateEvent(0,
2996 &mShellProtocol
.ExecutionBreak
);
2997 ASSERT_EFI_ERROR(Status
);
3000 // Get the size of the buffer we need.
3002 Status
= gBS
->LocateHandle(ByProtocol
,
3003 &gEfiShellProtocolGuid
,
3007 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3009 // Allocate and recall with buffer of correct size
3011 Buffer
= AllocateZeroPool(BufferSize
);
3012 ASSERT(Buffer
!= NULL
);
3013 Status
= gBS
->LocateHandle(ByProtocol
,
3014 &gEfiShellProtocolGuid
,
3018 ASSERT_EFI_ERROR(Status
);
3020 // now overwrite each of them, but save the info to restore when we end.
3022 for (HandleCounter
= 0 ; HandleCounter
< (BufferSize
/sizeof(EFI_HANDLE
)) ; HandleCounter
++) {
3023 OldProtocolNode
= AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST
));
3024 ASSERT(OldProtocolNode
!= NULL
);
3025 Status
= gBS
->OpenProtocol(Buffer
[HandleCounter
],
3026 &gEfiShellProtocolGuid
,
3027 (VOID
**) &(OldProtocolNode
->Interface
),
3030 EFI_OPEN_PROTOCOL_GET_PROTOCOL
3032 if (!EFI_ERROR(Status
)) {
3034 // reinstall over the old one...
3036 OldProtocolNode
->Handle
= Buffer
[HandleCounter
];
3037 Status
= gBS
->ReinstallProtocolInterface(
3038 OldProtocolNode
->Handle
,
3039 &gEfiShellProtocolGuid
,
3040 OldProtocolNode
->Interface
,
3041 (VOID
*)(*NewShell
));
3042 if (!EFI_ERROR(Status
)) {
3044 // we reinstalled sucessfully. log this so we can reverse it later.
3048 // add to the list for subsequent...
3050 InsertTailList(&ShellInfoObject
.OldShellList
.Link
, &OldProtocolNode
->Link
);
3055 } else if (Status
== EFI_NOT_FOUND
) {
3056 ASSERT(IsListEmpty(&ShellInfoObject
.OldShellList
.Link
));
3058 // no one else published yet. just publish it ourselves.
3060 Status
= gBS
->InstallProtocolInterface (
3062 &gEfiShellProtocolGuid
,
3063 EFI_NATIVE_INTERFACE
,
3064 (VOID
*)(*NewShell
));
3067 if (PcdGetBool(PcdShellSupportOldProtocols
)){
3068 ///@todo support ShellEnvironment2
3069 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?
3076 Opposite of CreatePopulateInstallShellProtocol.
3078 Free all memory and restore the system to the state it was in before calling
3079 CreatePopulateInstallShellProtocol.
3081 @param[in,out] NewShell The pointer to the new shell protocol structure.
3083 @retval EFI_SUCCESS The operation was successful.
3087 CleanUpShellProtocol (
3088 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3092 SHELL_PROTOCOL_HANDLE_LIST
*Node2
;
3095 // if we need to restore old protocols...
3097 if (!IsListEmpty(&ShellInfoObject
.OldShellList
.Link
)) {
3098 for (Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*)GetFirstNode(&ShellInfoObject
.OldShellList
.Link
)
3099 ; !IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)
3100 ; Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*)GetFirstNode(&ShellInfoObject
.OldShellList
.Link
)
3102 RemoveEntryList(&Node2
->Link
);
3103 Status
= gBS
->ReinstallProtocolInterface(Node2
->Handle
,
3104 &gEfiShellProtocolGuid
,
3107 ASSERT_EFI_ERROR(Status
);
3112 // no need to restore
3114 Status
= gBS
->UninstallProtocolInterface(gImageHandle
,
3115 &gEfiShellProtocolGuid
,
3117 ASSERT_EFI_ERROR(Status
);
3119 Status
= gBS
->CloseEvent(NewShell
->ExecutionBreak
);