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
[StrLen(NewPath
)-1] == Path
[0] == (CHAR16
)L
'\\') {
492 ((CHAR16
*)NewPath
)[StrLen(NewPath
)-1] = CHAR_NULL
;
494 StrCat(NewPath
, Path
);
495 DevicePathForReturn
= EfiShellGetDevicePathFromFilePath(NewPath
);
497 return (DevicePathForReturn
);
502 // find the part before (but including) the : for the map name
504 ASSERT((MapName
== NULL
&& Size
== 0) || (MapName
!= NULL
));
505 MapName
= StrnCatGrow(&MapName
, &Size
, Path
, (StrStr(Path
, L
":")-Path
+1));
506 if (MapName
[StrLen(MapName
)-1] != L
':') {
512 // look up the device path in the map
514 DevicePath
= EfiShellGetDevicePathFromMap(MapName
);
515 if (DevicePath
== NULL
) {
517 // Must have been a bad Mapname
523 // make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
525 DevicePathCopyForFree
= DevicePathCopy
= DuplicateDevicePath(DevicePath
);
526 if (DevicePathCopy
== NULL
) {
536 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
537 if (EFI_ERROR(Status
)) {
538 if (DevicePathCopyForFree
!= NULL
) {
539 FreePool(DevicePathCopyForFree
);
546 // build the full device path
548 DevicePathForReturn
= FileDevicePath(Handle
, Path
+StrLen(MapName
)+1);
551 if (DevicePathCopyForFree
!= NULL
) {
552 FreePool(DevicePathCopyForFree
);
555 return (DevicePathForReturn
);
559 Gets the name of the device specified by the device handle.
561 This function gets the user-readable name of the device specified by the device
562 handle. If no user-readable name could be generated, then *BestDeviceName will be
563 NULL and EFI_NOT_FOUND will be returned.
565 If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
566 device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
569 If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
570 device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
571 If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
572 EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
573 EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
575 @param DeviceHandle The handle of the device.
576 @param Flags Determines the possible sources of component names.
578 EFI_DEVICE_NAME_USE_COMPONENT_NAME
579 EFI_DEVICE_NAME_USE_DEVICE_PATH
580 @param Language A pointer to the language specified for the device
581 name, in the same format as described in the UEFI
582 specification, Appendix M
583 @param BestDeviceName On return, points to the callee-allocated NULL-
584 terminated name of the device. If no device name
585 could be found, points to NULL. The name must be
586 freed by the caller...
588 @retval EFI_SUCCESS Get the name successfully.
589 @retval EFI_NOT_FOUND Fail to get the device name.
590 @retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
591 @retval EFI_INVALID_PARAMETER BestDeviceName was NULL
592 @retval EFI_INVALID_PARAMETER DeviceHandle was NULL
596 EfiShellGetDeviceName(
597 IN EFI_HANDLE DeviceHandle
,
598 IN EFI_SHELL_DEVICE_NAME_FLAGS Flags
,
600 OUT CHAR16
**BestDeviceName
604 EFI_COMPONENT_NAME2_PROTOCOL
*CompName2
;
605 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevicePathToText
;
606 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
607 EFI_HANDLE
*HandleList
;
610 CHAR16
*DeviceNameToReturn
;
614 if (BestDeviceName
== NULL
||
617 return (EFI_INVALID_PARAMETER
);
621 // make sure one of the 2 supported bits is on
623 if (((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) == 0) &&
624 ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) == 0)) {
625 return (EFI_INVALID_PARAMETER
);
628 DeviceNameToReturn
= NULL
;
629 *BestDeviceName
= NULL
;
634 if ((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) != 0) {
635 Status
= ParseHandleDatabaseByRelationship(
638 HR_DRIVER_BINDING_HANDLE
|HR_DEVICE_DRIVER
,
641 for (LoopVar
= 0; LoopVar
< HandleCount
; LoopVar
++){
643 // Go through those handles until we get one that passes for GetComponentName
645 Status
= gBS
->OpenProtocol(
647 &gEfiComponentName2ProtocolGuid
,
651 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
652 if (EFI_ERROR(Status
)) {
653 Status
= gBS
->OpenProtocol(
655 &gEfiComponentNameProtocolGuid
,
659 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
662 if (EFI_ERROR(Status
)) {
665 if (Language
== NULL
) {
666 Lang
= AllocatePool(AsciiStrSize(CompName2
->SupportedLanguages
));
667 AsciiStrCpy(Lang
, CompName2
->SupportedLanguages
);
668 TempChar
= AsciiStrStr(Lang
, ";");
669 if (TempChar
!= NULL
){
670 *TempChar
= CHAR_NULL
;
673 Lang
= AllocatePool(AsciiStrSize(Language
));
674 AsciiStrCpy(Lang
, Language
);
676 Status
= CompName2
->GetControllerName(CompName2
, DeviceHandle
, NULL
, Lang
, &DeviceNameToReturn
);
679 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
683 if (HandleList
!= NULL
) {
684 FreePool(HandleList
);
686 if (DeviceNameToReturn
!= NULL
){
687 ASSERT(BestDeviceName
== NULL
);
688 StrnCatGrow(BestDeviceName
, NULL
, DeviceNameToReturn
, 0);
689 return (EFI_SUCCESS
);
692 // dont return on fail since we will try device path if that bit is on
695 if ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) != 0) {
696 Status
= gBS
->LocateProtocol(
697 &gEfiDevicePathToTextProtocolGuid
,
699 (VOID
**)&DevicePathToText
);
701 // we now have the device path to text protocol
703 if (!EFI_ERROR(Status
)) {
704 Status
= gBS
->OpenProtocol(
706 &gEfiDevicePathProtocolGuid
,
710 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
711 if (!EFI_ERROR(Status
)) {
713 // use device path to text on the device path
715 *BestDeviceName
= DevicePathToText
->ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
716 return (EFI_SUCCESS
);
721 // none of the selected bits worked.
723 return (EFI_NOT_FOUND
);
727 Opens the root directory of a device on a handle
729 This function opens the root directory of a device and returns a file handle to it.
731 @param DeviceHandle The handle of the device that contains the volume.
732 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
735 @retval EFI_SUCCESS Root opened successfully.
736 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
738 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
739 @retval EFI_DEVICE_ERROR The device had an error
743 EfiShellOpenRootByHandle(
744 IN EFI_HANDLE DeviceHandle
,
745 OUT SHELL_FILE_HANDLE
*FileHandle
749 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*SimpleFileSystem
;
750 EFI_FILE_PROTOCOL
*RealFileHandle
;
751 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
754 // get the simple file system interface
756 Status
= gBS
->OpenProtocol(DeviceHandle
,
757 &gEfiSimpleFileSystemProtocolGuid
,
758 (VOID
**)&SimpleFileSystem
,
761 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
762 if (EFI_ERROR(Status
)) {
763 return (EFI_NOT_FOUND
);
766 Status
= gBS
->OpenProtocol(DeviceHandle
,
767 &gEfiDevicePathProtocolGuid
,
771 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
772 if (EFI_ERROR(Status
)) {
773 return (EFI_NOT_FOUND
);
776 // Open the root volume now...
778 Status
= SimpleFileSystem
->OpenVolume(SimpleFileSystem
, &RealFileHandle
);
779 *FileHandle
= ConvertEfiFileProtocolToShellHandle(RealFileHandle
, EfiShellGetMapFromDevicePath(&DevPath
));
784 Opens the root directory of a device.
786 This function opens the root directory of a device and returns a file handle to it.
788 @param DevicePath Points to the device path corresponding to the device where the
789 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
790 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
793 @retval EFI_SUCCESS Root opened successfully.
794 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
796 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
797 @retval EFI_DEVICE_ERROR The device had an error
802 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
803 OUT SHELL_FILE_HANDLE
*FileHandle
810 // find the handle of the device with that device handle and the file system
813 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
816 if (EFI_ERROR(Status
)) {
817 return (EFI_NOT_FOUND
);
820 return (EfiShellOpenRootByHandle(Handle
, FileHandle
));
824 Returns whether any script files are currently being processed.
826 @retval TRUE There is at least one script file active.
827 @retval FALSE No script files are active now.
832 EfiShellBatchIsActive (
836 if (ShellCommandGetCurrentScriptFile() == NULL
) {
843 Worker function to open a file based on a device path. this will open the root
844 of the volume and then traverse down to the file itself.
846 @param DevicePath Device Path of the file.
847 @param FileHandle Pointer to the file upon a successful return.
848 @param OpenMode mode to open file in.
849 @param Attributes the File Attributes to use when creating a new file.
851 @retval EFI_SUCCESS the file is open and FileHandle is valid
852 @retval EFI_UNSUPPORTED the device path cotained non-path elements
853 @retval other an error ocurred.
857 InternalOpenFileDevicePath(
858 IN OUT EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
859 OUT SHELL_FILE_HANDLE
*FileHandle
,
861 IN UINT64 Attributes OPTIONAL
865 FILEPATH_DEVICE_PATH
*FilePathNode
;
867 SHELL_FILE_HANDLE ShellHandle
;
868 EFI_FILE_PROTOCOL
*Handle1
;
869 EFI_FILE_PROTOCOL
*Handle2
;
870 EFI_DEVICE_PATH_PROTOCOL
*DpCopy
;
872 ASSERT(FileHandle
!= NULL
);
877 Status
= EfiShellOpenRoot(DevicePath
, &ShellHandle
);
879 if (!EFI_ERROR(Status
)) {
880 Handle1
= ConvertShellHandleToEfiFileProtocol(ShellHandle
);
882 // chop off the begining part before the file system part...
885 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
888 if (!EFI_ERROR(Status
)) {
890 // To access as a file system, the file path should only
891 // contain file path components. Follow the file path nodes
892 // and find the target file
894 for ( FilePathNode
= (FILEPATH_DEVICE_PATH
*)DevicePath
895 ; !IsDevicePathEnd (&FilePathNode
->Header
)
896 ; FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
)
899 // For file system access each node should be a file path component
901 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
902 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
904 Status
= EFI_UNSUPPORTED
;
909 // Open this file path node
915 // if this is the last node in the DevicePath always create (if that was requested).
917 if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode
->Header
)))) {
918 Status
= Handle2
->Open (
921 FilePathNode
->PathName
,
928 // This is not the last node and we dont want to 'create' existing
929 // directory entries...
933 // open without letting it create
934 // prevents error on existing files/directories
936 Status
= Handle2
->Open (
939 FilePathNode
->PathName
,
940 OpenMode
&~EFI_FILE_MODE_CREATE
,
944 // if above failed now open and create the 'item'
945 // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)
947 if ((EFI_ERROR (Status
)) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)) {
948 Status
= Handle2
->Open (
951 FilePathNode
->PathName
,
958 // Close the last node
960 Handle2
->Close (Handle2
);
963 // If there's been an error, stop
965 if (EFI_ERROR (Status
)) {
971 if (EFI_ERROR(Status
)) {
972 if (Handle1
!= NULL
) {
973 Handle1
->Close(Handle1
);
976 *FileHandle
= ConvertEfiFileProtocolToShellHandle(Handle1
, ShellFileHandleGetPath(ShellHandle
));
982 Creates a file or directory by name.
984 This function creates an empty new file or directory with the specified attributes and
985 returns the new file's handle. If the file already exists and is read-only, then
986 EFI_INVALID_PARAMETER will be returned.
988 If the file already existed, it is truncated and its attributes updated. If the file is
989 created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.
991 If the file name begins with >v, then the file handle which is returned refers to the
992 shell environment variable with the specified name. If the shell environment variable
993 already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.
995 @param FileName Pointer to NULL-terminated file path
996 @param FileAttribs The new file's attrbiutes. the different attributes are
997 described in EFI_FILE_PROTOCOL.Open().
998 @param FileHandle On return, points to the created file handle or directory's handle
1000 @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.
1001 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1002 @retval EFI_UNSUPPORTED could not open the file path
1003 @retval EFI_NOT_FOUND the specified file could not be found on the devide, or could not
1004 file the file system on the device.
1005 @retval EFI_NO_MEDIA the device has no medium.
1006 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1008 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1010 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1011 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1012 when the media is write-protected.
1013 @retval EFI_ACCESS_DENIED The service denied access to the file.
1014 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
1015 @retval EFI_VOLUME_FULL The volume is full.
1020 IN CONST CHAR16
*FileName
,
1021 IN UINT64 FileAttribs
,
1022 OUT SHELL_FILE_HANDLE
*FileHandle
1025 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1029 // Is this for an environment variable
1030 // do we start with >v
1032 if (StrStr(FileName
, L
">v") == FileName
) {
1033 if (!IsVolatileEnv(FileName
+2)) {
1034 return (EFI_INVALID_PARAMETER
);
1036 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1037 return (EFI_SUCCESS
);
1041 // We are opening a regular file.
1043 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1044 if (DevicePath
== NULL
) {
1045 return (EFI_NOT_FOUND
);
1048 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, FileAttribs
); // 0 = no specific file attributes
1049 FreePool(DevicePath
);
1055 Opens a file or a directory by file name.
1057 This function opens the specified file in the specified OpenMode and returns a file
1059 If the file name begins with >v, then the file handle which is returned refers to the
1060 shell environment variable with the specified name. If the shell environment variable
1061 exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then
1062 EFI_INVALID_PARAMETER is returned.
1064 If the file name is >i, then the file handle which is returned refers to the standard
1065 input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER
1068 If the file name is >o, then the file handle which is returned refers to the standard
1069 output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1072 If the file name is >e, then the file handle which is returned refers to the standard
1073 error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1076 If the file name is NUL, then the file handle that is returned refers to the standard NUL
1077 file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is
1080 If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the
1083 @param FileName Points to the NULL-terminated UCS-2 encoded file name.
1084 @param FileHandle On return, points to the file handle.
1085 @param OpenMode File open mode. Either EFI_FILE_MODE_READ or
1086 EFI_FILE_MODE_WRITE from section 12.4 of the UEFI
1088 @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.
1089 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.
1090 @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.
1091 @retval EFI_NOT_FOUND The specified file could not be found on the device or the file
1092 system could not be found on the device. FileHandle is NULL.
1093 @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.
1094 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1095 longer supported. FileHandle is NULL.
1096 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1097 the FileName. FileHandle is NULL.
1098 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.
1099 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1100 when the media is write-protected. FileHandle is NULL.
1101 @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.
1102 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle
1104 @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.
1108 EfiShellOpenFileByName(
1109 IN CONST CHAR16
*FileName
,
1110 OUT SHELL_FILE_HANDLE
*FileHandle
,
1114 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1120 // Is this for StdIn
1122 if (StrCmp(FileName
, L
">i") == 0) {
1124 // make sure not writing to StdIn
1126 if ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0) {
1127 return (EFI_INVALID_PARAMETER
);
1129 *FileHandle
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1130 ASSERT(*FileHandle
!= NULL
);
1131 return (EFI_SUCCESS
);
1135 // Is this for StdOut
1137 if (StrCmp(FileName
, L
">o") == 0) {
1139 // make sure not writing to StdIn
1141 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1142 return (EFI_INVALID_PARAMETER
);
1144 *FileHandle
= &FileInterfaceStdOut
;
1145 return (EFI_SUCCESS
);
1149 // Is this for NUL file
1151 if (StrCmp(FileName
, L
"NUL") == 0) {
1152 *FileHandle
= &FileInterfaceNulFile
;
1153 return (EFI_SUCCESS
);
1157 // Is this for StdErr
1159 if (StrCmp(FileName
, L
">e") == 0) {
1161 // make sure not writing to StdIn
1163 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1164 return (EFI_INVALID_PARAMETER
);
1166 *FileHandle
= &FileInterfaceStdErr
;
1167 return (EFI_SUCCESS
);
1171 // Is this for an environment variable
1172 // do we start with >v
1174 if (StrStr(FileName
, L
">v") == FileName
) {
1175 if (!IsVolatileEnv(FileName
+2) &&
1176 ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0)) {
1177 return (EFI_INVALID_PARAMETER
);
1179 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1180 return (EFI_SUCCESS
);
1184 // We are opening a regular file.
1186 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1187 // DEBUG_CODE(InternalShellProtocolDebugPrintMessage (NULL, DevicePath););
1188 if (DevicePath
== NULL
) {
1189 return (EFI_NOT_FOUND
);
1193 // Copy the device path, open the file, then free the memory
1195 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, OpenMode
, 0); // 0 = no specific file attributes
1196 FreePool(DevicePath
);
1202 Deletes the file specified by the file name.
1204 This function deletes a file.
1206 @param FileName Points to the NULL-terminated file name.
1208 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.
1209 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
1210 @sa EfiShellCreateFile
1214 EfiShellDeleteFileByName(
1215 IN CONST CHAR16
*FileName
1218 SHELL_FILE_HANDLE FileHandle
;
1222 // get a handle to the file
1224 Status
= EfiShellCreateFile(FileName
,
1227 if (EFI_ERROR(Status
)) {
1231 // now delete the file
1233 return (ShellInfoObject
.NewEfiShellProtocol
->DeleteFile(FileHandle
));
1237 Disables the page break output mode.
1241 EfiShellDisablePageBreak (
1245 ShellInfoObject
.PageBreakEnabled
= FALSE
;
1249 Enables the page break output mode.
1253 EfiShellEnablePageBreak (
1257 ShellInfoObject
.PageBreakEnabled
= TRUE
;
1261 internal worker function to load and run an image via device path.
1263 @param ParentImageHandle A handle of the image that is executing the specified
1265 @param DevicePath device path of the file to execute
1266 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1267 containing the command line. If NULL then the command-
1269 @param Environment Points to a NULL-terminated array of environment
1270 variables with the format 'x=y', where x is the
1271 environment variable name and y is the value. If this
1272 is NULL, then the current shell environment is used.
1273 @param StatusCode Points to the status code returned by the command.
1275 @retval EFI_SUCCESS The command executed successfully. The status code
1276 returned by the command is pointed to by StatusCode.
1277 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1278 @retval EFI_OUT_OF_RESOURCES Out of resources.
1279 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1283 InternalShellExecuteDevicePath(
1284 IN CONST EFI_HANDLE
*ParentImageHandle
,
1285 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1286 IN CONST CHAR16
*CommandLine OPTIONAL
,
1287 IN CONST CHAR16
**Environment OPTIONAL
,
1288 OUT EFI_STATUS
*StatusCode OPTIONAL
1292 EFI_HANDLE NewHandle
;
1293 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1294 LIST_ENTRY OrigEnvs
;
1295 EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol
;
1297 if (ParentImageHandle
== NULL
) {
1298 return (EFI_INVALID_PARAMETER
);
1301 InitializeListHead(&OrigEnvs
);
1306 // Load the image with:
1307 // FALSE - not from boot manager and NULL, 0 being not already in memory
1309 Status
= gBS
->LoadImage(
1312 (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
,
1317 if (EFI_ERROR(Status
)) {
1318 if (NewHandle
!= NULL
) {
1319 gBS
->UnloadImage(NewHandle
);
1323 Status
= gBS
->OpenProtocol(
1325 &gEfiLoadedImageProtocolGuid
,
1326 (VOID
**)&LoadedImage
,
1329 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1331 if (!EFI_ERROR(Status
)) {
1332 ASSERT(LoadedImage
->LoadOptionsSize
== 0);
1333 if (CommandLine
!= NULL
) {
1334 LoadedImage
->LoadOptionsSize
= (UINT32
)StrSize(CommandLine
);
1335 LoadedImage
->LoadOptions
= (VOID
*)CommandLine
;
1339 // Save our current environment settings for later restoration if necessary
1341 if (Environment
!= NULL
) {
1342 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1343 if (!EFI_ERROR(Status
)) {
1344 Status
= SetEnvironmentVariables(Environment
);
1349 // Initialize and install a shell parameters protocol on the image.
1351 ShellParamsProtocol
.StdIn
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1352 ShellParamsProtocol
.StdOut
= ShellInfoObject
.NewShellParametersProtocol
->StdOut
;
1353 ShellParamsProtocol
.StdErr
= ShellInfoObject
.NewShellParametersProtocol
->StdErr
;
1354 Status
= UpdateArgcArgv(&ShellParamsProtocol
, CommandLine
, NULL
, NULL
);
1355 ASSERT_EFI_ERROR(Status
);
1356 Status
= gBS
->InstallProtocolInterface(&NewHandle
, &gEfiShellParametersProtocolGuid
, EFI_NATIVE_INTERFACE
, &ShellParamsProtocol
);
1357 ASSERT_EFI_ERROR(Status
);
1359 ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)
1362 // now start the image and if the caller wanted the return code pass it to them...
1364 if (!EFI_ERROR(Status
)) {
1365 if (StatusCode
!= NULL
) {
1366 *StatusCode
= gBS
->StartImage(NewHandle
, NULL
, NULL
);
1368 Status
= gBS
->StartImage(NewHandle
, NULL
, NULL
);
1373 // Cleanup (and dont overwrite errors)
1375 if (EFI_ERROR(Status
)) {
1376 gBS
->UninstallProtocolInterface(NewHandle
, &gEfiShellParametersProtocolGuid
, &ShellParamsProtocol
);
1378 Status
= gBS
->UninstallProtocolInterface(NewHandle
, &gEfiShellParametersProtocolGuid
, &ShellParamsProtocol
);
1379 ASSERT_EFI_ERROR(Status
);
1383 if (!IsListEmpty(&OrigEnvs
)) {
1384 if (EFI_ERROR(Status
)) {
1385 SetEnvironmentVariableList(&OrigEnvs
);
1387 Status
= SetEnvironmentVariableList(&OrigEnvs
);
1394 Execute the command line.
1396 This function creates a nested instance of the shell and executes the specified
1397 command (CommandLine) with the specified environment (Environment). Upon return,
1398 the status code returned by the specified command is placed in StatusCode.
1400 If Environment is NULL, then the current environment is used and all changes made
1401 by the commands executed will be reflected in the current environment. If the
1402 Environment is non-NULL, then the changes made will be discarded.
1404 The CommandLine is executed from the current working directory on the current
1407 @param ParentImageHandle A handle of the image that is executing the specified
1409 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1410 containing the command line. If NULL then the command-
1412 @param Environment Points to a NULL-terminated array of environment
1413 variables with the format 'x=y', where x is the
1414 environment variable name and y is the value. If this
1415 is NULL, then the current shell environment is used.
1416 @param StatusCode Points to the status code returned by the command.
1418 @retval EFI_SUCCESS The command executed successfully. The status code
1419 returned by the command is pointed to by StatusCode.
1420 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1421 @retval EFI_OUT_OF_RESOURCES Out of resources.
1422 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1423 @retval EFI_UNSUPPORTED The support level required for this function is not present.
1425 @sa InternalShellExecuteDevicePath
1430 IN EFI_HANDLE
*ParentImageHandle
,
1431 IN CHAR16
*CommandLine OPTIONAL
,
1432 IN CHAR16
**Environment OPTIONAL
,
1433 OUT EFI_STATUS
*StatusCode OPTIONAL
1438 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1441 if ((PcdGet8(PcdShellSupportLevel
) < 1)) {
1442 return (EFI_UNSUPPORTED
);
1445 DevPath
= AppendDevicePath (ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
1448 Temp
= gDevPathToText
->ConvertDevicePathToText(ShellInfoObject
.FileDevPath
, TRUE
, TRUE
);
1450 Temp
= gDevPathToText
->ConvertDevicePathToText(ShellInfoObject
.ImageDevPath
, TRUE
, TRUE
);
1452 Temp
= gDevPathToText
->ConvertDevicePathToText(DevPath
, TRUE
, TRUE
);
1458 ASSERT((Temp
== NULL
&& Size
== 0) || (Temp
!= NULL
));
1459 StrnCatGrow(&Temp
, &Size
, L
"Shell.efi ", 0);
1460 StrnCatGrow(&Temp
, &Size
, CommandLine
, 0);
1462 Status
= InternalShellExecuteDevicePath(
1466 (CONST CHAR16
**)Environment
,
1470 // de-allocate and return
1478 Utility cleanup function for EFI_SHELL_FILE_INFO objects.
1480 1) frees all pointers (non-NULL)
1481 2) Closes the SHELL_FILE_HANDLE
1483 @param FileListNode pointer to the list node to free
1487 InternalFreeShellFileInfoNode(
1488 IN EFI_SHELL_FILE_INFO
*FileListNode
1491 if (FileListNode
->Info
!= NULL
) {
1492 FreePool((VOID
*)FileListNode
->Info
);
1494 if (FileListNode
->FileName
!= NULL
) {
1495 FreePool((VOID
*)FileListNode
->FileName
);
1497 if (FileListNode
->FullName
!= NULL
) {
1498 FreePool((VOID
*)FileListNode
->FullName
);
1500 if (FileListNode
->Handle
!= NULL
) {
1501 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileListNode
->Handle
);
1503 FreePool(FileListNode
);
1506 Frees the file list.
1508 This function cleans up the file list and any related data structures. It has no
1509 impact on the files themselves.
1511 @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is
1512 defined in OpenFileList()
1514 @retval EFI_SUCCESS Free the file list successfully.
1515 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1519 EfiShellFreeFileList(
1520 IN EFI_SHELL_FILE_INFO
**FileList
1523 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1525 if (FileList
== NULL
|| *FileList
== NULL
) {
1526 return (EFI_INVALID_PARAMETER
);
1529 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1530 ; !IsListEmpty(&(*FileList
)->Link
)
1531 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1533 RemoveEntryList(&ShellFileListItem
->Link
);
1534 InternalFreeShellFileInfoNode(ShellFileListItem
);
1536 return(EFI_SUCCESS
);
1540 Deletes the duplicate file names files in the given file list.
1542 This function deletes the reduplicate files in the given file list.
1544 @param FileList A pointer to the first entry in the file list.
1546 @retval EFI_SUCCESS Always success.
1547 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1551 EfiShellRemoveDupInFileList(
1552 IN EFI_SHELL_FILE_INFO
**FileList
1555 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1556 EFI_SHELL_FILE_INFO
*ShellFileListItem2
;
1558 if (FileList
== NULL
|| *FileList
== NULL
) {
1559 return (EFI_INVALID_PARAMETER
);
1561 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1562 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1563 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1565 for ( ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1566 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1567 ; ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1569 if (gUnicodeCollation
->StriColl(
1571 (CHAR16
*)ShellFileListItem
->FullName
,
1572 (CHAR16
*)ShellFileListItem2
->FullName
) == 0
1574 RemoveEntryList(&ShellFileListItem2
->Link
);
1575 InternalFreeShellFileInfoNode(ShellFileListItem2
);
1579 return (EFI_SUCCESS
);
1582 Allocates and duplicates a EFI_SHELL_FILE_INFO node.
1584 @param[in] Node The node to copy from.
1585 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.
1587 @retval NULL a memory allocation error ocurred
1588 @return != NULL a pointer to the new node
1590 EFI_SHELL_FILE_INFO
*
1592 InternalDuplicateShellFileInfo(
1593 IN EFI_SHELL_FILE_INFO
*Node
,
1597 EFI_SHELL_FILE_INFO
*NewNode
;
1599 NewNode
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
1600 if (NewNode
== NULL
) {
1603 NewNode
->FullName
= AllocateZeroPool(StrSize(Node
->FullName
));
1605 NewNode
->FileName
= AllocateZeroPool(StrSize(Node
->FileName
));
1606 NewNode
->Info
= AllocatePool((UINTN
)Node
->Info
->Size
);
1607 if ( NewNode
->FullName
== NULL
1608 || NewNode
->FileName
== NULL
1609 || NewNode
->Info
== NULL
1613 NewNode
->Status
= Node
->Status
;
1614 NewNode
->Handle
= Node
->Handle
;
1616 Node
->Handle
= NULL
;
1618 StrCpy((CHAR16
*)NewNode
->FullName
, Node
->FullName
);
1619 StrCpy((CHAR16
*)NewNode
->FileName
, Node
->FileName
);
1620 CopyMem(NewNode
->Info
, Node
->Info
, (UINTN
)Node
->Info
->Size
);
1626 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation
1627 failed it will return NULL.
1629 @param[in] BasePath the Path to prepend onto filename for FullPath
1630 @param[in] Status Status member initial value.
1631 @param[in] FullName FullName member initial value.
1632 @param[in] FileName FileName member initial value.
1633 @param[in] Handle Handle member initial value.
1634 @param[in] Info Info struct to copy.
1636 @retval NULL An error ocurred.
1637 @return a pointer to the newly allocated structure.
1639 EFI_SHELL_FILE_INFO
*
1641 CreateAndPopulateShellFileInfo(
1642 IN CONST CHAR16
*BasePath
,
1643 IN CONST EFI_STATUS Status
,
1644 IN CONST CHAR16
*FullName
,
1645 IN CONST CHAR16
*FileName
,
1646 IN CONST SHELL_FILE_HANDLE Handle
,
1647 IN CONST EFI_FILE_INFO
*Info
1650 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1657 ShellFileListItem
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1658 if (ShellFileListItem
== NULL
) {
1662 ShellFileListItem
->Info
= AllocateZeroPool((UINTN
)Info
->Size
);
1663 if (ShellFileListItem
->Info
== NULL
) {
1664 FreePool(ShellFileListItem
);
1667 CopyMem(ShellFileListItem
->Info
, Info
, (UINTN
)Info
->Size
);
1669 ShellFileListItem
->Info
= NULL
;
1671 if (FileName
!= NULL
) {
1672 ASSERT(TempString
== NULL
);
1673 ShellFileListItem
->FileName
= StrnCatGrow(&TempString
, 0, FileName
, 0);
1674 if (ShellFileListItem
->FileName
== NULL
) {
1675 FreePool(ShellFileListItem
->Info
);
1676 FreePool(ShellFileListItem
);
1680 ShellFileListItem
->FileName
= NULL
;
1684 if (BasePath
!= NULL
) {
1685 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1686 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1687 if (TempString
== NULL
) {
1688 FreePool((VOID
*)ShellFileListItem
->FileName
);
1689 FreePool(ShellFileListItem
->Info
);
1690 FreePool(ShellFileListItem
);
1694 if (ShellFileListItem
->FileName
!= NULL
) {
1695 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1696 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileListItem
->FileName
, 0);
1697 if (TempString
== NULL
) {
1698 FreePool((VOID
*)ShellFileListItem
->FileName
);
1699 FreePool(ShellFileListItem
->Info
);
1700 FreePool(ShellFileListItem
);
1705 ShellFileListItem
->FullName
= TempString
;
1706 ShellFileListItem
->Status
= Status
;
1707 ShellFileListItem
->Handle
= Handle
;
1709 return (ShellFileListItem
);
1713 Find all files in a specified directory.
1715 @param FileDirHandle Handle of the directory to search.
1716 @param FileList On return, points to the list of files in the directory
1717 or NULL if there are no files in the directory.
1719 @retval EFI_SUCCESS File information was returned successfully.
1720 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.
1721 @retval EFI_DEVICE_ERROR The device reported an error.
1722 @retval EFI_NO_MEDIA The device media is not present.
1723 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.
1724 @return An error from FileHandleGetFileName().
1728 EfiShellFindFilesInDir(
1729 IN SHELL_FILE_HANDLE FileDirHandle
,
1730 OUT EFI_SHELL_FILE_INFO
**FileList
1733 EFI_SHELL_FILE_INFO
*ShellFileList
;
1734 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1735 EFI_FILE_INFO
*FileInfo
;
1743 Status
= FileHandleGetFileName(FileDirHandle
, &BasePath
);
1744 if (EFI_ERROR(Status
)) {
1748 if (ShellFileHandleGetPath(FileDirHandle
) != NULL
) {
1751 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileHandleGetPath(FileDirHandle
), 0);
1752 TempSpot
= StrStr(TempString
, L
";");
1754 if (TempSpot
!= NULL
) {
1755 *TempSpot
= CHAR_NULL
;
1758 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1759 BasePath
= TempString
;
1763 ShellFileList
= NULL
;
1764 ShellFileListItem
= NULL
;
1766 Status
= EFI_SUCCESS
;
1769 for ( Status
= FileHandleFindFirstFile(FileDirHandle
, &FileInfo
)
1770 ; !EFI_ERROR(Status
) && !NoFile
1771 ; Status
= FileHandleFindNextFile(FileDirHandle
, FileInfo
, &NoFile
)
1776 // allocate a new EFI_SHELL_FILE_INFO and populate it...
1778 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
1779 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
1780 TempString
= StrnCatGrow(&TempString
, &Size
, FileInfo
->FileName
, 0);
1781 ShellFileListItem
= CreateAndPopulateShellFileInfo(
1783 EFI_SUCCESS
, // success since we didnt fail to open it...
1786 NULL
, // no handle since not open
1789 if (ShellFileList
== NULL
) {
1790 ShellFileList
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1791 ASSERT(ShellFileList
!= NULL
);
1792 InitializeListHead(&ShellFileList
->Link
);
1794 InsertTailList(&ShellFileList
->Link
, &ShellFileListItem
->Link
);
1796 if (EFI_ERROR(Status
)) {
1797 EfiShellFreeFileList(&ShellFileList
);
1800 *FileList
= ShellFileList
;
1802 SHELL_FREE_NON_NULL(BasePath
);
1807 Updates a file name to be preceeded by the mapped drive name
1809 @param[in] BasePath the Mapped drive name to prepend
1810 @param[in,out] Path pointer to pointer to the file name to update.
1813 @retval EFI_OUT_OF_RESOURCES
1818 IN CONST CHAR16
*BasePath
,
1819 IN OUT CHAR16
**Path
1828 ASSERT(Path
!= NULL
);
1829 ASSERT(*Path
!= NULL
);
1830 ASSERT(BasePath
!= NULL
);
1833 // convert a local path to an absolute path
1835 if (StrStr(*Path
, L
":") == NULL
) {
1836 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
1837 StrnCatGrow(&Path2
, &Path2Size
, BasePath
, 0);
1838 if (Path2
== NULL
) {
1839 return (EFI_OUT_OF_RESOURCES
);
1841 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
1842 StrnCatGrow(&Path2
, &Path2Size
, (*Path
)[0] == L
'\\'?(*Path
) + 1 :*Path
, 0);
1843 if (Path2
== NULL
) {
1844 return (EFI_OUT_OF_RESOURCES
);
1851 return (EFI_SUCCESS
);
1855 If FileHandle is a directory then the function reads from FileHandle and reads in
1856 each of the FileInfo structures. If one of them matches the Pattern's first
1857 "level" then it opens that handle and calls itself on that handle.
1859 If FileHandle is a file and matches all of the remaining Pattern (which would be
1860 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.
1862 if FileList is NULL, then ASSERT
1863 if FilePattern is NULL, then ASSERT
1864 if UnicodeCollation is NULL, then ASSERT
1865 if FileHandle is NULL, then ASSERT
1867 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call
1868 FreeFileList with FileList.
1870 @param[in] FilePattern The FilePattern to check against.
1871 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure
1872 @param[in] FileHandle The FileHandle to start with
1873 @param[in,out] FileList pointer to pointer to list of found files.
1874 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.
1876 @retval EFI_SUCCESS all files were found and the FileList contains a list.
1877 @retval EFI_NOT_FOUND no files were found
1878 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
1883 IN CONST CHAR16
*FilePattern
,
1884 IN EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
,
1885 IN SHELL_FILE_HANDLE FileHandle
,
1886 IN OUT EFI_SHELL_FILE_INFO
**FileList
,
1887 IN CONST EFI_SHELL_FILE_INFO
*ParentNode OPTIONAL
1891 CONST CHAR16
*NextFilePatternStart
;
1892 CHAR16
*CurrentFilePattern
;
1893 EFI_SHELL_FILE_INFO
*ShellInfo
;
1894 EFI_SHELL_FILE_INFO
*ShellInfoNode
;
1895 EFI_SHELL_FILE_INFO
*NewShellNode
;
1898 if ( FilePattern
== NULL
1899 || UnicodeCollation
== NULL
1902 return (EFI_INVALID_PARAMETER
);
1905 CurrentFilePattern
= NULL
;
1907 if (*FilePattern
== L
'\\') {
1911 for( NextFilePatternStart
= FilePattern
1912 ; *NextFilePatternStart
!= CHAR_NULL
&& *NextFilePatternStart
!= L
'\\'
1913 ; NextFilePatternStart
++);
1915 CurrentFilePattern
= AllocateZeroPool((NextFilePatternStart
-FilePattern
+1)*sizeof(CHAR16
));
1916 ASSERT(CurrentFilePattern
!= NULL
);
1917 StrnCpy(CurrentFilePattern
, FilePattern
, NextFilePatternStart
-FilePattern
);
1919 if (CurrentFilePattern
[0] == CHAR_NULL
1920 &&NextFilePatternStart
[0] == CHAR_NULL
1923 // Add the current parameter FileHandle to the list, then end...
1925 if (ParentNode
== NULL
) {
1926 Status
= EFI_INVALID_PARAMETER
;
1928 NewShellNode
= InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO
*)ParentNode
, TRUE
);
1929 if (NewShellNode
== NULL
) {
1930 Status
= EFI_OUT_OF_RESOURCES
;
1932 NewShellNode
->Handle
= NULL
;
1933 if (*FileList
== NULL
) {
1934 *FileList
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
1935 InitializeListHead(&((*FileList
)->Link
));
1939 // Add to the returning to use list
1941 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
1943 Status
= EFI_SUCCESS
;
1947 Status
= EfiShellFindFilesInDir(FileHandle
, &ShellInfo
);
1949 if (!EFI_ERROR(Status
)){
1950 if (StrStr(NextFilePatternStart
, L
"\\") != NULL
){
1955 for ( ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ShellInfo
->Link
)
1956 ; !IsNull (&ShellInfo
->Link
, &ShellInfoNode
->Link
)
1957 ; ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ShellInfo
->Link
, &ShellInfoNode
->Link
)
1959 if (UnicodeCollation
->MetaiMatch(UnicodeCollation
, (CHAR16
*)ShellInfoNode
->FileName
, CurrentFilePattern
)){
1962 // should be a directory
1966 // don't open the . and .. directories
1968 if ( (StrCmp(ShellInfoNode
->FileName
, L
".") != 0)
1969 && (StrCmp(ShellInfoNode
->FileName
, L
"..") != 0)
1974 ASSERT_EFI_ERROR(Status
);
1975 if (EFI_ERROR(Status
)) {
1979 // Open the directory since we need that handle in the next recursion.
1981 ShellInfoNode
->Status
= EfiShellOpenFileByName (ShellInfoNode
->FullName
, &ShellInfoNode
->Handle
, EFI_FILE_MODE_READ
);
1984 // recurse with the next part of the pattern
1986 Status
= ShellSearchHandle(NextFilePatternStart
, UnicodeCollation
, ShellInfoNode
->Handle
, FileList
, ShellInfoNode
);
1994 // copy the information we need into a new Node
1996 NewShellNode
= InternalDuplicateShellFileInfo(ShellInfoNode
, FALSE
);
1997 ASSERT(NewShellNode
!= NULL
);
1998 if (NewShellNode
== NULL
) {
1999 Status
= EFI_OUT_OF_RESOURCES
;
2001 if (*FileList
== NULL
) {
2002 *FileList
= AllocatePool(sizeof(EFI_SHELL_FILE_INFO
));
2003 InitializeListHead(&((*FileList
)->Link
));
2007 // Add to the returning to use list
2009 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2012 if (EFI_ERROR(Status
)) {
2016 if (EFI_ERROR(Status
)) {
2017 EfiShellFreeFileList(&ShellInfo
);
2019 Status
= EfiShellFreeFileList(&ShellInfo
);
2024 FreePool(CurrentFilePattern
);
2029 Find files that match a specified pattern.
2031 This function searches for all files and directories that match the specified
2032 FilePattern. The FilePattern can contain wild-card characters. The resulting file
2033 information is placed in the file list FileList.
2035 Wildcards are processed
2036 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.
2038 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo
2039 field is set to NULL.
2041 if *FileList is not NULL then it must be a pre-existing and properly initialized list.
2043 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.
2044 @param FileList On return, points to the start of a file list containing the names
2045 of all matching files or else points to NULL if no matching files
2046 were found. only on a EFI_SUCCESS return will; this be non-NULL.
2048 @retval EFI_SUCCESS Files found. FileList is a valid list.
2049 @retval EFI_NOT_FOUND No files found.
2050 @retval EFI_NO_MEDIA The device has no media
2051 @retval EFI_DEVICE_ERROR The device reported an error
2052 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted
2057 IN CONST CHAR16
*FilePattern
,
2058 OUT EFI_SHELL_FILE_INFO
**FileList
2062 CHAR16
*PatternCopy
;
2063 CHAR16
*PatternCurrentLocation
;
2064 EFI_DEVICE_PATH_PROTOCOL
*RootDevicePath
;
2065 SHELL_FILE_HANDLE RootFileHandle
;
2069 if ( FilePattern
== NULL
2071 || StrStr(FilePattern
, L
":") == NULL
2073 return (EFI_INVALID_PARAMETER
);
2075 Status
= EFI_SUCCESS
;
2076 RootDevicePath
= NULL
;
2077 RootFileHandle
= NULL
;
2079 PatternCopy
= AllocatePool(StrSize(FilePattern
));
2080 if (PatternCopy
== NULL
) {
2081 return (EFI_OUT_OF_RESOURCES
);
2083 StrCpy(PatternCopy
, FilePattern
);
2085 PatternCopy
= CleanPath(PatternCopy
);
2087 Count
= StrStr(PatternCopy
, L
":") - PatternCopy
;
2090 ASSERT(MapName
== NULL
);
2091 MapName
= StrnCatGrow(&MapName
, NULL
, PatternCopy
, Count
);
2093 if (!EFI_ERROR(Status
)) {
2094 RootDevicePath
= EfiShellGetDevicePathFromFilePath(PatternCopy
);
2095 if (RootDevicePath
== NULL
) {
2096 Status
= EFI_INVALID_PARAMETER
;
2098 Status
= EfiShellOpenRoot(RootDevicePath
, &RootFileHandle
);
2099 if (!EFI_ERROR(Status
)) {
2100 for ( PatternCurrentLocation
= PatternCopy
2101 ; *PatternCurrentLocation
!= ':'
2102 ; PatternCurrentLocation
++);
2103 PatternCurrentLocation
++;
2104 Status
= ShellSearchHandle(PatternCurrentLocation
, gUnicodeCollation
, RootFileHandle
, FileList
, NULL
);
2106 FreePool(RootDevicePath
);
2110 if (PatternCopy
!= NULL
) {
2111 FreePool(PatternCopy
);
2113 if (MapName
!= NULL
) {
2121 Opens the files that match the path specified.
2123 This function opens all of the files specified by Path. Wildcards are processed
2124 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each
2125 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.
2127 @param Path A pointer to the path string.
2128 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or
2129 EFI_FILE_MODE_WRITE.
2130 @param FileList Points to the start of a list of files opened.
2132 @retval EFI_SUCCESS Create the file list successfully.
2133 @return Others Can't create the file list.
2137 EfiShellOpenFileList(
2140 IN OUT EFI_SHELL_FILE_INFO
**FileList
2144 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2147 CONST CHAR16
*CurDir
;
2149 ShellCommandCleanPath(Path
);
2154 ASSERT(FileList
!= NULL
);
2155 ASSERT(*FileList
!= NULL
);
2157 if (*Path
== L
'.' && *(Path
+1) == L
'\\') {
2162 // convert a local path to an absolute path
2164 if (StrStr(Path
, L
":") == NULL
) {
2165 CurDir
= EfiShellGetCurDir(NULL
);
2166 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2167 StrnCatGrow(&Path2
, &Path2Size
, CurDir
, 0);
2168 if (*Path
== L
'\\') {
2171 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2172 StrnCatGrow(&Path2
, &Path2Size
, Path
, 0);
2174 ASSERT(Path2
== NULL
);
2175 StrnCatGrow(&Path2
, NULL
, Path
, 0);
2183 Status
= EfiShellFindFiles(Path2
, FileList
);
2187 if (EFI_ERROR(Status
)) {
2192 // We had no errors so open all the files (that are not already opened...)
2194 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
2195 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2196 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2198 if (ShellFileListItem
->Status
== 0 && ShellFileListItem
->Handle
== NULL
) {
2199 ShellFileListItem
->Status
= EfiShellOpenFileByName (ShellFileListItem
->FullName
, &ShellFileListItem
->Handle
, OpenMode
);
2203 return(EFI_SUCCESS
);
2207 This function updated with errata.
2209 Gets either a single or list of environment variables.
2211 If name is not NULL then this function returns the current value of the specified
2212 environment variable.
2214 If Name is NULL, then a list of all environment variable names is returned. Each is a
2215 NULL terminated string with a double NULL terminating the list.
2217 @param Name A pointer to the environment variable name. If
2218 Name is NULL, then the function will return all
2219 of the defined shell environment variables. In
2220 the case where multiple environment variables are
2221 being returned, each variable will be terminated by
2222 a NULL, and the list will be terminated by a double
2225 @return !=NULL A pointer to the returned string.
2226 The returned pointer does not need to be freed by the caller.
2228 @retval NULL The environment variable doesn't exist or there are
2229 no environment variables.
2234 IN CONST CHAR16
*Name
2242 CHAR16
*CurrentWriteLocation
;
2249 // Get all our environment variables
2251 InitializeListHead(&List
);
2252 Status
= GetEnvironmentVariableList(&List
);
2253 if (EFI_ERROR(Status
)){
2258 // Build the semi-colon delimited list. (2 passes)
2260 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&List
)
2261 ; !IsNull(&List
, &Node
->Link
)
2262 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&List
, &Node
->Link
)
2264 ASSERT(Node
->Key
!= NULL
);
2265 Size
+= StrSize(Node
->Key
);
2268 Size
+= 2*sizeof(CHAR16
);
2270 Buffer
= AllocateZeroPool(Size
);
2271 CurrentWriteLocation
= (CHAR16
*)Buffer
;
2273 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&List
)
2274 ; !IsNull(&List
, &Node
->Link
)
2275 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&List
, &Node
->Link
)
2277 ASSERT(Node
->Key
!= NULL
);
2278 StrCpy(CurrentWriteLocation
, Node
->Key
);
2279 CurrentWriteLocation
+= StrLen(CurrentWriteLocation
) + 1;
2285 FreeEnvironmentVariableList(&List
);
2288 // We are doing a specific environment variable
2292 // get the size we need for this EnvVariable
2294 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(Name
, &Size
, Buffer
);
2295 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2297 // Allocate the space and recall the get function
2299 Buffer
= AllocateZeroPool(Size
);
2300 ASSERT(Buffer
!= NULL
);
2301 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(Name
, &Size
, Buffer
);
2304 // we didnt get it (might not exist)
2305 // free the memory if we allocated any and return NULL
2307 if (EFI_ERROR(Status
)) {
2308 if (Buffer
!= NULL
) {
2316 // return the buffer
2318 return (AddBufferToFreeList(Buffer
));
2322 Internal variable setting function. Allows for setting of the read only variables.
2324 @param Name Points to the NULL-terminated environment variable name.
2325 @param Value Points to the NULL-terminated environment variable value. If the value is an
2326 empty string then the environment variable is deleted.
2327 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2329 @retval EFI_SUCCESS The environment variable was successfully updated.
2333 InternalEfiShellSetEnv(
2334 IN CONST CHAR16
*Name
,
2335 IN CONST CHAR16
*Value
,
2339 if (Value
== NULL
|| StrLen(Value
) == 0) {
2340 return (SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
));
2342 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2344 return (SHELL_SET_ENVIRONMENT_VARIABLE_V(Name
, StrSize(Value
), Value
));
2346 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(Name
, StrSize(Value
), Value
));
2352 Sets the environment variable.
2354 This function changes the current value of the specified environment variable. If the
2355 environment variable exists and the Value is an empty string, then the environment
2356 variable is deleted. If the environment variable exists and the Value is not an empty
2357 string, then the value of the environment variable is changed. If the environment
2358 variable does not exist and the Value is an empty string, there is no action. If the
2359 environment variable does not exist and the Value is a non-empty string, then the
2360 environment variable is created and assigned the specified value.
2362 For a description of volatile and non-volatile environment variables, see UEFI Shell
2363 2.0 specification section 3.6.1.
2365 @param Name Points to the NULL-terminated environment variable name.
2366 @param Value Points to the NULL-terminated environment variable value. If the value is an
2367 empty string then the environment variable is deleted.
2368 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2370 @retval EFI_SUCCESS The environment variable was successfully updated.
2375 IN CONST CHAR16
*Name
,
2376 IN CONST CHAR16
*Value
,
2380 if (Name
== NULL
|| *Name
== CHAR_NULL
) {
2381 return (EFI_INVALID_PARAMETER
);
2384 // Make sure we dont 'set' a predefined read only variable
2386 if (gUnicodeCollation
->StriColl(
2390 ||gUnicodeCollation
->StriColl(
2394 ||gUnicodeCollation
->StriColl(
2398 ||gUnicodeCollation
->StriColl(
2401 L
"uefishellsupport") == 0
2402 ||gUnicodeCollation
->StriColl(
2405 L
"uefishellversion") == 0
2406 ||gUnicodeCollation
->StriColl(
2409 L
"uefiversion") == 0
2411 return (EFI_INVALID_PARAMETER
);
2413 return (InternalEfiShellSetEnv(Name
, Value
, Volatile
));
2417 Returns the current directory on the specified device.
2419 If FileSystemMapping is NULL, it returns the current working directory. If the
2420 FileSystemMapping is not NULL, it returns the current directory associated with the
2421 FileSystemMapping. In both cases, the returned name includes the file system
2422 mapping (i.e. fs0:\current-dir).
2424 @param FileSystemMapping A pointer to the file system mapping. If NULL,
2425 then the current working directory is returned.
2427 @retval !=NULL The current directory.
2428 @retval NULL Current directory does not exist.
2433 IN CONST CHAR16
*FileSystemMapping OPTIONAL
2436 CHAR16
*PathToReturn
;
2438 SHELL_MAP_LIST
*MapListItem
;
2439 if (!IsListEmpty(&gShellMapList
.Link
)) {
2441 // if parameter is NULL, use current
2443 if (FileSystemMapping
== NULL
) {
2444 return (EfiShellGetEnv(L
"cwd"));
2447 PathToReturn
= NULL
;
2448 MapListItem
= ShellCommandFindMapItem(FileSystemMapping
);
2449 if (MapListItem
!= NULL
) {
2450 ASSERT((PathToReturn
== NULL
&& Size
== 0) || (PathToReturn
!= NULL
));
2451 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->MapName
, 0);
2452 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
2455 return (AddBufferToFreeList(PathToReturn
));
2462 Changes the current directory on the specified device.
2464 If the FileSystem is NULL, and the directory Dir does not contain a file system's
2465 mapped name, this function changes the current working directory.
2467 If the FileSystem is NULL and the directory Dir contains a mapped name, then the
2468 current file system and the current directory on that file system are changed.
2470 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file
2473 If FileSystem is not NULL and Dir is not NULL, then this function changes the current
2474 directory on the specified file system.
2476 If the current working directory or the current working file system is changed then the
2477 %cwd% environment variable will be updated
2479 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working
2480 directory is changed.
2481 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.
2483 @retval EFI_SUCCESS The operation was sucessful
2484 @retval EFI_NOT_FOUND The file system could not be found
2489 IN CONST CHAR16
*FileSystem OPTIONAL
,
2490 IN CONST CHAR16
*Dir
2494 SHELL_MAP_LIST
*MapListItem
;
2498 CHAR16
*DirectoryName
;
2505 DirectoryName
= NULL
;
2507 if (FileSystem
== NULL
&& Dir
== NULL
) {
2508 return (EFI_INVALID_PARAMETER
);
2511 if (IsListEmpty(&gShellMapList
.Link
)){
2512 return (EFI_NOT_FOUND
);
2515 DirectoryName
= StrnCatGrow(&DirectoryName
, NULL
, Dir
, 0);
2516 ASSERT(DirectoryName
!= NULL
);
2518 CleanPath(DirectoryName
);
2520 if (FileSystem
== NULL
) {
2522 // determine the file system mapping to use
2524 if (StrStr(DirectoryName
, L
":") != NULL
) {
2525 ASSERT(MapName
== NULL
);
2526 MapName
= StrnCatGrow(&MapName
, NULL
, DirectoryName
, (StrStr(DirectoryName
, L
":")-DirectoryName
+1));
2529 // find the file system mapping's entry in the list
2532 if (MapName
!= NULL
) {
2533 MapListItem
= ShellCommandFindMapItem(MapName
);
2536 // make that the current file system mapping
2538 if (MapListItem
!= NULL
) {
2539 gShellCurDir
= MapListItem
;
2542 MapListItem
= gShellCurDir
;
2545 if (MapListItem
== NULL
) {
2546 return (EFI_NOT_FOUND
);
2550 // now update the MapListItem's current directory
2552 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& DirectoryName
[StrLen(DirectoryName
) - 1] != L
':') {
2553 FreePool(MapListItem
->CurrentDirectoryPath
);
2554 MapListItem
->CurrentDirectoryPath
= NULL
;
2556 if (MapName
!= NULL
) {
2557 TempLen
= StrLen(MapName
);
2558 if (TempLen
!= StrLen(DirectoryName
)) {
2559 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2560 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
+StrLen(MapName
), 0);
2563 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2564 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
2566 if ((MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] != L
'\\') || (MapListItem
->CurrentDirectoryPath
== NULL
)) {
2567 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2568 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2572 // cant have a mapping in the directory...
2574 if (StrStr(DirectoryName
, L
":") != NULL
) {
2575 return (EFI_INVALID_PARAMETER
);
2578 // FileSystem != NULL
2580 MapListItem
= ShellCommandFindMapItem(FileSystem
);
2581 if (MapListItem
== NULL
) {
2582 return (EFI_INVALID_PARAMETER
);
2584 // gShellCurDir = MapListItem;
2585 if (DirectoryName
!= NULL
) {
2587 // change current dir on that file system
2590 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
2591 FreePool(MapListItem
->CurrentDirectoryPath
);
2592 DEBUG_CODE(MapListItem
->CurrentDirectoryPath
= NULL
;);
2594 // ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2595 // MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);
2596 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2597 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2598 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2599 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
2600 if (MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] != L
'\\') {
2601 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
2602 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
2607 // if updated the current directory then update the environment variable
2609 if (MapListItem
== gShellCurDir
) {
2611 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2612 StrnCatGrow(&TempString
, &Size
, MapListItem
->MapName
, 0);
2613 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2614 StrnCatGrow(&TempString
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
2615 Status
= InternalEfiShellSetEnv(L
"cwd", TempString
, TRUE
);
2616 FreePool(TempString
);
2619 return(EFI_SUCCESS
);
2623 Return help information about a specific command.
2625 This function returns the help information for the specified command. The help text
2626 can be internal to the shell or can be from a UEFI Shell manual page.
2628 If Sections is specified, then each section name listed will be compared in a casesensitive
2629 manner, to the section names described in Appendix B. If the section exists,
2630 it will be appended to the returned help text. If the section does not exist, no
2631 information will be returned. If Sections is NULL, then all help text information
2632 available will be returned.
2634 @param Command Points to the NULL-terminated UEFI Shell command name.
2635 @param Sections Points to the NULL-terminated comma-delimited
2636 section names to return. If NULL, then all
2637 sections will be returned.
2638 @param HelpText On return, points to a callee-allocated buffer
2639 containing all specified help text.
2641 @retval EFI_SUCCESS The help text was returned.
2642 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
2644 @retval EFI_INVALID_PARAMETER HelpText is NULL
2645 @retval EFI_NOT_FOUND There is no help text available for Command.
2649 EfiShellGetHelpText(
2650 IN CONST CHAR16
*Command
,
2651 IN CONST CHAR16
*Sections OPTIONAL
,
2652 OUT CHAR16
**HelpText
2655 CONST CHAR16
*ManFileName
;
2657 ASSERT(HelpText
!= NULL
);
2659 ManFileName
= ShellCommandGetManFileNameHandler(Command
);
2661 if (ManFileName
!= NULL
) {
2662 return (ProcessManFile(ManFileName
, Command
, Sections
, NULL
, HelpText
));
2664 return (ProcessManFile(Command
, Command
, Sections
, NULL
, HelpText
));
2669 Gets the enable status of the page break output mode.
2671 User can use this function to determine current page break mode.
2673 @retval TRUE The page break output mode is enabled.
2674 @retval FALSE The page break output mode is disabled.
2678 EfiShellGetPageBreak(
2682 return(ShellInfoObject
.PageBreakEnabled
);
2686 Judges whether the active shell is the root shell.
2688 This function makes the user to know that whether the active Shell is the root shell.
2690 @retval TRUE The active Shell is the root Shell.
2691 @retval FALSE The active Shell is NOT the root Shell.
2695 EfiShellIsRootShell(
2699 return(ShellInfoObject
.RootShellInstance
);
2703 function to return a semi-colon delimeted list of all alias' in the current shell
2705 up to caller to free the memory.
2707 @retval NULL No alias' were found
2708 @retval NULL An error ocurred getting alias'
2709 @return !NULL a list of all alias'
2713 InternalEfiShellGetListAlias(
2721 CHAR16
*VariableName
;
2727 Status
= gRT
->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE
|EFI_VARIABLE_BOOTSERVICE_ACCESS
, &MaxStorSize
, &RemStorSize
, &MaxVarSize
);
2728 ASSERT_EFI_ERROR(Status
);
2730 VariableName
= AllocateZeroPool((UINTN
)MaxVarSize
);
2734 VariableName
[0] = CHAR_NULL
;
2737 NameSize
= (UINTN
)MaxVarSize
;
2738 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
2739 if (Status
== EFI_NOT_FOUND
){
2742 ASSERT_EFI_ERROR(Status
);
2743 if (EFI_ERROR(Status
)) {
2746 if (CompareGuid(&Guid
, &gShellAliasGuid
)){
2747 Alias
= GetVariable(VariableName
, &gShellAliasGuid
);
2748 ASSERT((RetVal
== NULL
&& RetSize
== 0) || (RetVal
!= NULL
));
2749 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, VariableName
, 0);
2750 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, L
";", 0);
2753 FreePool(VariableName
);
2759 This function returns the command associated with a alias or a list of all
2762 @param[in] Alias Points to the NULL-terminated shell alias.
2763 If this parameter is NULL, then all
2764 aliases will be returned in ReturnedData.
2765 @param[out] Volatile upon return of a single command if TRUE indicates
2766 this is stored in a volatile fashion. FALSE otherwise.
2768 @return If Alias is not NULL, it will return a pointer to
2769 the NULL-terminated command for that alias.
2770 If Alias is NULL, ReturnedData points to a ';'
2771 delimited list of alias (e.g.
2772 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.
2773 @retval NULL an error ocurred
2774 @retval NULL Alias was not a valid Alias
2779 IN CONST CHAR16
*Alias
,
2780 OUT BOOLEAN
*Volatile OPTIONAL
2788 if (Alias
!= NULL
) {
2789 if (Volatile
== NULL
) {
2790 return (AddBufferToFreeList(GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
)));
2794 Status
= gRT
->GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
2795 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2796 RetVal
= AllocateZeroPool(RetSize
);
2797 Status
= gRT
->GetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
2799 if (EFI_ERROR(Status
)) {
2800 if (RetVal
!= NULL
) {
2805 if ((EFI_VARIABLE_NON_VOLATILE
& Attribs
) == EFI_VARIABLE_NON_VOLATILE
) {
2811 return (AddBufferToFreeList(RetVal
));
2813 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));
2817 Changes a shell command alias.
2819 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
2821 this function does not check for built in alias'.
2823 @param[in] Command Points to the NULL-terminated shell command or existing alias.
2824 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
2825 Command refers to an alias, that alias will be deleted.
2826 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
2827 Alias being set will be stored in a non-volatile fashion.
2829 @retval EFI_SUCCESS Alias created or deleted successfully.
2830 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
2835 IN CONST CHAR16
*Command
,
2836 IN CONST CHAR16
*Alias
,
2841 // We must be trying to remove one if Alias is NULL
2843 if (Alias
== NULL
) {
2845 // remove an alias (but passed in COMMAND parameter)
2847 return (gRT
->SetVariable((CHAR16
*)Command
, &gShellAliasGuid
, 0, 0, NULL
));
2850 // Add and replace are the same
2853 // We dont check the error return on purpose since the variable may not exist.
2854 gRT
->SetVariable((CHAR16
*)Command
, &gShellAliasGuid
, 0, 0, NULL
);
2856 return (gRT
->SetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, EFI_VARIABLE_BOOTSERVICE_ACCESS
|(Volatile
?0:EFI_VARIABLE_NON_VOLATILE
), StrSize(Command
), (VOID
*)Command
));
2861 Changes a shell command alias.
2863 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
2866 @param[in] Command Points to the NULL-terminated shell command or existing alias.
2867 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
2868 Command refers to an alias, that alias will be deleted.
2869 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
2870 FALSE and the alias already exists, then the existing alias is unchanged and
2871 EFI_ACCESS_DENIED is returned.
2872 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
2873 Alias being set will be stored in a non-volatile fashion.
2875 @retval EFI_SUCCESS Alias created or deleted successfully.
2876 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
2877 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
2883 IN CONST CHAR16
*Command
,
2884 IN CONST CHAR16
*Alias
,
2890 // cant set over a built in alias
2892 if (ShellCommandIsOnAliasList(Alias
==NULL
?Command
:Alias
)) {
2893 return (EFI_ACCESS_DENIED
);
2895 if (Command
== NULL
|| *Command
== CHAR_NULL
|| StrLen(Command
) == 0) {
2896 return (EFI_INVALID_PARAMETER
);
2899 if (EfiShellGetAlias(Command
, NULL
) != NULL
&& !Replace
) {
2900 return (EFI_ACCESS_DENIED
);
2903 return (InternalSetAlias(Command
, Alias
, Volatile
));
2906 // Pure FILE_HANDLE operations are passed to FileHandleLib
2907 // these functions are indicated by the *
2908 EFI_SHELL_PROTOCOL mShellProtocol
= {
2914 EfiShellGetHelpText
,
2915 EfiShellGetDevicePathFromMap
,
2916 EfiShellGetMapFromDevicePath
,
2917 EfiShellGetDevicePathFromFilePath
,
2918 EfiShellGetFilePathFromDevicePath
,
2922 EfiShellOpenFileList
,
2923 EfiShellFreeFileList
,
2924 EfiShellRemoveDupInFileList
,
2925 EfiShellBatchIsActive
,
2926 EfiShellIsRootShell
,
2927 EfiShellEnablePageBreak
,
2928 EfiShellDisablePageBreak
,
2929 EfiShellGetPageBreak
,
2930 EfiShellGetDeviceName
,
2931 (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
, //*
2932 (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
, //*
2933 EfiShellOpenFileByName
,
2936 (EFI_SHELL_READ_FILE
)FileHandleRead
, //*
2937 (EFI_SHELL_WRITE_FILE
)FileHandleWrite
, //*
2938 (EFI_SHELL_DELETE_FILE
)FileHandleDelete
, //*
2939 EfiShellDeleteFileByName
,
2940 (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
, //*
2941 (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
, //*
2942 (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
, //*
2944 EfiShellFindFilesInDir
,
2945 (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
, //*
2947 EfiShellOpenRootByHandle
,
2949 SHELL_MAJOR_VERSION
,
2954 Function to create and install on the current handle.
2956 Will overwrite any existing ShellProtocols in the system to be sure that
2957 the current shell is in control.
2959 This must be removed via calling CleanUpShellProtocol().
2961 @param[in,out] NewShell The pointer to the pointer to the structure
2964 @retval EFI_SUCCESS The operation was successful.
2965 @return An error from LocateHandle, CreateEvent, or other core function.
2969 CreatePopulateInstallShellProtocol (
2970 IN OUT EFI_SHELL_PROTOCOL
**NewShell
2976 UINTN HandleCounter
;
2977 SHELL_PROTOCOL_HANDLE_LIST
*OldProtocolNode
;
2981 OldProtocolNode
= NULL
;
2982 InitializeListHead(&ShellInfoObject
.OldShellList
.Link
);
2984 ASSERT(NewShell
!= NULL
);
2987 // Initialize EfiShellProtocol object...
2989 *NewShell
= &mShellProtocol
;
2990 Status
= gBS
->CreateEvent(0,
2994 &mShellProtocol
.ExecutionBreak
);
2995 ASSERT_EFI_ERROR(Status
);
2998 // Get the size of the buffer we need.
3000 Status
= gBS
->LocateHandle(ByProtocol
,
3001 &gEfiShellProtocolGuid
,
3005 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3007 // Allocate and recall with buffer of correct size
3009 Buffer
= AllocateZeroPool(BufferSize
);
3010 ASSERT(Buffer
!= NULL
);
3011 Status
= gBS
->LocateHandle(ByProtocol
,
3012 &gEfiShellProtocolGuid
,
3016 ASSERT_EFI_ERROR(Status
);
3018 // now overwrite each of them, but save the info to restore when we end.
3020 for (HandleCounter
= 0 ; HandleCounter
< (BufferSize
/sizeof(EFI_HANDLE
)) ; HandleCounter
++) {
3021 OldProtocolNode
= AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST
));
3022 ASSERT(OldProtocolNode
!= NULL
);
3023 Status
= gBS
->OpenProtocol(Buffer
[HandleCounter
],
3024 &gEfiShellProtocolGuid
,
3025 (VOID
**) &(OldProtocolNode
->Interface
),
3028 EFI_OPEN_PROTOCOL_GET_PROTOCOL
3030 if (!EFI_ERROR(Status
)) {
3032 // reinstall over the old one...
3034 OldProtocolNode
->Handle
= Buffer
[HandleCounter
];
3035 Status
= gBS
->ReinstallProtocolInterface(
3036 OldProtocolNode
->Handle
,
3037 &gEfiShellProtocolGuid
,
3038 OldProtocolNode
->Interface
,
3039 (VOID
*)(*NewShell
));
3040 if (!EFI_ERROR(Status
)) {
3042 // we reinstalled sucessfully. log this so we can reverse it later.
3046 // add to the list for subsequent...
3048 InsertTailList(&ShellInfoObject
.OldShellList
.Link
, &OldProtocolNode
->Link
);
3053 } else if (Status
== EFI_NOT_FOUND
) {
3054 ASSERT(IsListEmpty(&ShellInfoObject
.OldShellList
.Link
));
3056 // no one else published yet. just publish it ourselves.
3058 Status
= gBS
->InstallProtocolInterface (
3060 &gEfiShellProtocolGuid
,
3061 EFI_NATIVE_INTERFACE
,
3062 (VOID
*)(*NewShell
));
3065 if (PcdGetBool(PcdShellSupportOldProtocols
)){
3066 ///@todo support ShellEnvironment2
3067 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?
3074 Opposite of CreatePopulateInstallShellProtocol.
3076 Free all memory and restore the system to the state it was in before calling
3077 CreatePopulateInstallShellProtocol.
3079 @param[in,out] NewShell The pointer to the new shell protocol structure.
3081 @retval EFI_SUCCESS The operation was successful.
3085 CleanUpShellProtocol (
3086 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3090 SHELL_PROTOCOL_HANDLE_LIST
*Node2
;
3093 // if we need to restore old protocols...
3095 if (!IsListEmpty(&ShellInfoObject
.OldShellList
.Link
)) {
3096 for (Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*)GetFirstNode(&ShellInfoObject
.OldShellList
.Link
)
3097 ; !IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)
3098 ; Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*)GetFirstNode(&ShellInfoObject
.OldShellList
.Link
)
3100 RemoveEntryList(&Node2
->Link
);
3101 Status
= gBS
->ReinstallProtocolInterface(Node2
->Handle
,
3102 &gEfiShellProtocolGuid
,
3105 ASSERT_EFI_ERROR(Status
);
3110 // no need to restore
3112 Status
= gBS
->UninstallProtocolInterface(gImageHandle
,
3113 &gEfiShellProtocolGuid
,
3115 ASSERT_EFI_ERROR(Status
);
3117 Status
= gBS
->CloseEvent(NewShell
->ExecutionBreak
);