2 Member functions of EFI_SHELL_PROTOCOL and functions for creation,
3 manipulation, and initialization of EFI_SHELL_PROTOCOL.
5 (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
6 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
7 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #define INIT_NAME_BUFFER_SIZE 128
17 Close an open file handle.
19 This function closes a specified file handle. All "dirty" cached file data is
20 flushed to the device, and the file is closed. In all cases the handle is
23 @param[in] FileHandle The file handle to close.
25 @retval EFI_SUCCESS The file handle was closed successfully.
30 IN SHELL_FILE_HANDLE FileHandle
33 ShellFileHandleRemove(FileHandle
);
34 return (FileHandleClose(ConvertShellHandleToEfiFileProtocol(FileHandle
)));
38 Internal worker to determine whether there is a BlockIo somewhere
39 upon the device path specified.
41 @param[in] DevicePath The device path to test.
43 @retval TRUE gEfiBlockIoProtocolGuid was installed on a handle with this device path
44 @retval FALSE gEfiBlockIoProtocolGuid was not found.
47 InternalShellProtocolIsBlockIoPresent(
48 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
51 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
57 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
58 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
, &DevicePathCopy
, &Handle
);
60 if ((Handle
!= NULL
) && (!EFI_ERROR(Status
))) {
67 Internal worker to determine whether there is a file system somewhere
68 upon the device path specified.
70 @param[in] DevicePath The device path to test.
72 @retval TRUE gEfiSimpleFileSystemProtocolGuid was installed on a handle with this device path
73 @retval FALSE gEfiSimpleFileSystemProtocolGuid was not found.
76 InternalShellProtocolIsSimpleFileSystemPresent(
77 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
80 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
86 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
87 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
89 if ((Handle
!= NULL
) && (!EFI_ERROR(Status
))) {
97 This function creates a mapping for a device path.
99 If both DeviecPath and Mapping are NULL, this will reset the mapping to default values.
101 @param DevicePath Points to the device path. If this is NULL and Mapping points to a valid mapping,
102 then the mapping will be deleted.
103 @param Mapping Points to the NULL-terminated mapping for the device path. Must end with a ':'
105 @retval EFI_SUCCESS Mapping created or deleted successfully.
106 @retval EFI_NO_MAPPING There is no handle that corresponds exactly to DevicePath. See the
107 boot service function LocateDevicePath().
108 @retval EFI_ACCESS_DENIED The mapping is a built-in alias.
109 @retval EFI_INVALID_PARAMETER Mapping was NULL
110 @retval EFI_INVALID_PARAMETER Mapping did not end with a ':'
111 @retval EFI_INVALID_PARAMETER DevicePath was not pointing at a device that had a SIMPLE_FILE_SYSTEM_PROTOCOL installed.
112 @retval EFI_NOT_FOUND There was no mapping found to delete
113 @retval EFI_OUT_OF_RESOURCES Memory allocation failed
118 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath OPTIONAL
,
119 IN CONST CHAR16
*Mapping
123 SHELL_MAP_LIST
*MapListNode
;
125 if (Mapping
== NULL
){
126 return (EFI_INVALID_PARAMETER
);
129 if (Mapping
[StrLen(Mapping
)-1] != ':') {
130 return (EFI_INVALID_PARAMETER
);
134 // Delete the mapping
136 if (DevicePath
== NULL
) {
137 if (IsListEmpty(&gShellMapList
.Link
)) {
138 return (EFI_NOT_FOUND
);
140 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
141 ; !IsNull(&gShellMapList
.Link
, &MapListNode
->Link
)
142 ; MapListNode
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListNode
->Link
)
144 if (StringNoCaseCompare(&MapListNode
->MapName
, &Mapping
) == 0) {
145 RemoveEntryList(&MapListNode
->Link
);
146 SHELL_FREE_NON_NULL(MapListNode
->DevicePath
);
147 SHELL_FREE_NON_NULL(MapListNode
->MapName
);
148 SHELL_FREE_NON_NULL(MapListNode
->CurrentDirectoryPath
);
149 FreePool(MapListNode
);
150 return (EFI_SUCCESS
);
155 // We didnt find one to delete
157 return (EFI_NOT_FOUND
);
161 // make sure this is a valid to add device path
163 ///@todo add BlockIo to this test...
164 if (!InternalShellProtocolIsSimpleFileSystemPresent(DevicePath
)
165 && !InternalShellProtocolIsBlockIoPresent(DevicePath
)) {
166 return (EFI_INVALID_PARAMETER
);
170 // First make sure there is no old mapping
172 Status
= EfiShellSetMap(NULL
, Mapping
);
173 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_FOUND
)) {
178 // now add the new one.
180 Status
= ShellCommandAddMapItemAndUpdatePath(Mapping
, DevicePath
, 0, FALSE
);
186 Gets the device path from the mapping.
188 This function gets the device path associated with a mapping.
190 @param Mapping A pointer to the mapping
192 @retval !=NULL Pointer to the device path that corresponds to the
193 device mapping. The returned pointer does not need
195 @retval NULL There is no device path associated with the
198 CONST EFI_DEVICE_PATH_PROTOCOL
*
200 EfiShellGetDevicePathFromMap(
201 IN CONST CHAR16
*Mapping
204 SHELL_MAP_LIST
*MapListItem
;
211 StrnCatGrow(&NewName
, &Size
, Mapping
, 0);
212 if (Mapping
[StrLen(Mapping
)-1] != L
':') {
213 StrnCatGrow(&NewName
, &Size
, L
":", 0);
216 MapListItem
= ShellCommandFindMapItem(NewName
);
220 if (MapListItem
!= NULL
) {
221 return (MapListItem
->DevicePath
);
227 Gets the mapping(s) that most closely matches the device path.
229 This function gets the mapping which corresponds to the device path *DevicePath. If
230 there is no exact match, then the mapping which most closely matches *DevicePath
231 is returned, and *DevicePath is updated to point to the remaining portion of the
232 device path. If there is an exact match, the mapping is returned and *DevicePath
233 points to the end-of-device-path node.
235 If there are multiple map names they will be semi-colon seperated in the
236 NULL-terminated string.
238 @param DevicePath On entry, points to a device path pointer. On
239 exit, updates the pointer to point to the
240 portion of the device path after the mapping.
242 @retval NULL No mapping was found.
243 @return !=NULL Pointer to NULL-terminated mapping. The buffer
244 is callee allocated and should be freed by the caller.
248 EfiShellGetMapFromDevicePath(
249 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
252 SHELL_MAP_LIST
*Node
;
253 CHAR16
*PathForReturn
;
255 // EFI_HANDLE PathHandle;
256 // EFI_HANDLE MapHandle;
257 // EFI_STATUS Status;
258 // EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
259 // EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
261 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
265 PathForReturn
= NULL
;
268 for ( Node
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
269 ; !IsNull(&gShellMapList
.Link
, &Node
->Link
)
270 ; Node
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &Node
->Link
)
273 // check for exact match
275 if (DevicePathCompare(DevicePath
, &Node
->DevicePath
) == 0) {
276 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
278 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, L
";", 0);
280 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, Node
->MapName
, 0);
283 if (PathForReturn
!= NULL
) {
284 while (!IsDevicePathEndType (*DevicePath
)) {
285 *DevicePath
= NextDevicePathNode (*DevicePath
);
287 SetDevicePathEndNode (*DevicePath
);
290 ///@todo finish code for inexact matches.
291 if (PathForReturn == NULL) {
294 DevicePathCopy = DuplicateDevicePath(*DevicePath);
295 ASSERT(DevicePathCopy != NULL);
296 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
297 ASSERT_EFI_ERROR(Status);
299 // check each of the device paths we have to get the root of the path for consist mappings
301 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
302 ; !IsNull(&gShellMapList.Link, &Node->Link)
303 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
305 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) == 0) {
308 MapPathCopy = DuplicateDevicePath(Node->DevicePath);
309 ASSERT(MapPathCopy != NULL);
310 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
311 if (MapHandle == PathHandle) {
313 *DevicePath = DevicePathCopy;
316 DevicePathCopy = NULL;
317 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
318 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
323 // now add on the non-consistent mappings
325 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
326 ; !IsNull(&gShellMapList.Link, &Node->Link)
327 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
329 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) != 0) {
332 MapPathCopy = Node->DevicePath;
333 ASSERT(MapPathCopy != NULL);
334 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
335 if (MapHandle == PathHandle) {
336 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
337 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
344 return (AddBufferToFreeList(PathForReturn
));
348 Converts a device path to a file system-style path.
350 This function converts a device path to a file system path by replacing part, or all, of
351 the device path with the file-system mapping. If there are more than one application
352 file system mappings, the one that most closely matches Path will be used.
354 @param Path The pointer to the device path
356 @retval NULL the device path could not be found.
357 @return all The pointer of the NULL-terminated file path. The path
358 is callee-allocated and should be freed by the caller.
362 EfiShellGetFilePathFromDevicePath(
363 IN CONST EFI_DEVICE_PATH_PROTOCOL
*Path
366 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
367 EFI_DEVICE_PATH_PROTOCOL
*MapPathCopy
;
368 SHELL_MAP_LIST
*MapListItem
;
369 CHAR16
*PathForReturn
;
371 EFI_HANDLE PathHandle
;
372 EFI_HANDLE MapHandle
;
374 FILEPATH_DEVICE_PATH
*FilePath
;
375 FILEPATH_DEVICE_PATH
*AlignedNode
;
377 PathForReturn
= NULL
;
380 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)Path
;
381 ASSERT(DevicePathCopy
!= NULL
);
382 if (DevicePathCopy
== NULL
) {
386 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &PathHandle
);
388 if (EFI_ERROR(Status
)) {
392 // check each of the device paths we have to get the root of the path
394 for ( MapListItem
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
395 ; !IsNull(&gShellMapList
.Link
, &MapListItem
->Link
)
396 ; MapListItem
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListItem
->Link
)
398 MapPathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)MapListItem
->DevicePath
;
399 ASSERT(MapPathCopy
!= NULL
);
401 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &MapPathCopy
, &MapHandle
);
402 if (MapHandle
== PathHandle
) {
403 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
404 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, MapListItem
->MapName
, 0);
406 // go through all the remaining nodes in the device path
408 for ( FilePath
= (FILEPATH_DEVICE_PATH
*)DevicePathCopy
409 ; !IsDevicePathEnd (&FilePath
->Header
)
410 ; FilePath
= (FILEPATH_DEVICE_PATH
*)NextDevicePathNode (&FilePath
->Header
)
413 // If any node is not a file path node, then the conversion can not be completed
415 if ((DevicePathType(&FilePath
->Header
) != MEDIA_DEVICE_PATH
) ||
416 (DevicePathSubType(&FilePath
->Header
) != MEDIA_FILEPATH_DP
)) {
417 FreePool(PathForReturn
);
422 // append the path part onto the filepath.
424 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
426 AlignedNode
= AllocateCopyPool (DevicePathNodeLength(FilePath
), FilePath
);
427 if (AlignedNode
== NULL
) {
428 FreePool (PathForReturn
);
432 // File Path Device Path Nodes 'can optionally add a "\" separator to
433 // the beginning and/or the end of the Path Name string.'
434 // (UEFI Spec 2.4 section 9.3.6.4).
435 // If necessary, add a "\", but otherwise don't
436 // (This is specified in the above section, and also implied by the
437 // UEFI Shell spec section 3.7)
438 if ((PathSize
!= 0) &&
439 (PathForReturn
!= NULL
) &&
440 (PathForReturn
[PathSize
/ sizeof (CHAR16
) - 1] != L
'\\') &&
441 (AlignedNode
->PathName
[0] != L
'\\')) {
442 PathForReturn
= StrnCatGrow (&PathForReturn
, &PathSize
, L
"\\", 1);
445 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, AlignedNode
->PathName
, 0);
446 FreePool(AlignedNode
);
447 } // for loop of remaining nodes
449 if (PathForReturn
!= NULL
) {
452 } // for loop of paths to check
453 return(PathForReturn
);
457 Converts a file system style name to a device path.
459 This function converts a file system style name to a device path, by replacing any
460 mapping references to the associated device path.
462 @param[in] Path The pointer to the path.
464 @return The pointer of the file path. The file path is callee
465 allocated and should be freed by the caller.
466 @retval NULL The path could not be found.
467 @retval NULL There was not enough available memory.
469 EFI_DEVICE_PATH_PROTOCOL
*
471 EfiShellGetDevicePathFromFilePath(
472 IN CONST CHAR16
*Path
479 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
480 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
481 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopyForFree
;
482 EFI_DEVICE_PATH_PROTOCOL
*DevicePathForReturn
;
493 if (StrStr(Path
, L
":") == NULL
) {
494 Cwd
= EfiShellGetCurDir(NULL
);
498 Size
= StrSize(Cwd
) + StrSize(Path
);
499 NewPath
= AllocateZeroPool(Size
);
500 if (NewPath
== NULL
) {
503 StrCpyS(NewPath
, Size
/sizeof(CHAR16
), Cwd
);
504 StrCatS(NewPath
, Size
/sizeof(CHAR16
), L
"\\");
505 if (*Path
== L
'\\') {
507 while (PathRemoveLastItem(NewPath
)) ;
509 StrCatS(NewPath
, Size
/sizeof(CHAR16
), Path
);
510 DevicePathForReturn
= EfiShellGetDevicePathFromFilePath(NewPath
);
512 return (DevicePathForReturn
);
517 // find the part before (but including) the : for the map name
519 ASSERT((MapName
== NULL
&& Size
== 0) || (MapName
!= NULL
));
520 MapName
= StrnCatGrow(&MapName
, &Size
, Path
, (StrStr(Path
, L
":")-Path
+1));
521 if (MapName
== NULL
|| MapName
[StrLen(MapName
)-1] != L
':') {
526 // look up the device path in the map
528 DevicePath
= EfiShellGetDevicePathFromMap(MapName
);
529 if (DevicePath
== NULL
) {
531 // Must have been a bad Mapname
537 // make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
539 DevicePathCopyForFree
= DevicePathCopy
= DuplicateDevicePath(DevicePath
);
540 if (DevicePathCopy
== NULL
) {
549 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
550 if (EFI_ERROR(Status
)) {
551 if (DevicePathCopyForFree
!= NULL
) {
552 FreePool(DevicePathCopyForFree
);
559 // build the full device path
561 if ((*(Path
+StrLen(MapName
)) != CHAR_NULL
) &&
562 (*(Path
+StrLen(MapName
)+1) == CHAR_NULL
)) {
563 DevicePathForReturn
= FileDevicePath(Handle
, L
"\\");
565 DevicePathForReturn
= FileDevicePath(Handle
, Path
+StrLen(MapName
));
569 if (DevicePathCopyForFree
!= NULL
) {
570 FreePool(DevicePathCopyForFree
);
573 return (DevicePathForReturn
);
577 Gets the name of the device specified by the device handle.
579 This function gets the user-readable name of the device specified by the device
580 handle. If no user-readable name could be generated, then *BestDeviceName will be
581 NULL and EFI_NOT_FOUND will be returned.
583 If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
584 device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
587 If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
588 device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
589 If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
590 EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
591 EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
593 @param DeviceHandle The handle of the device.
594 @param Flags Determines the possible sources of component names.
596 EFI_DEVICE_NAME_USE_COMPONENT_NAME
597 EFI_DEVICE_NAME_USE_DEVICE_PATH
598 @param Language A pointer to the language specified for the device
599 name, in the same format as described in the UEFI
600 specification, Appendix M
601 @param BestDeviceName On return, points to the callee-allocated NULL-
602 terminated name of the device. If no device name
603 could be found, points to NULL. The name must be
604 freed by the caller...
606 @retval EFI_SUCCESS Get the name successfully.
607 @retval EFI_NOT_FOUND Fail to get the device name.
608 @retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
609 @retval EFI_INVALID_PARAMETER BestDeviceName was NULL
610 @retval EFI_INVALID_PARAMETER DeviceHandle was NULL
614 EfiShellGetDeviceName(
615 IN EFI_HANDLE DeviceHandle
,
616 IN EFI_SHELL_DEVICE_NAME_FLAGS Flags
,
618 OUT CHAR16
**BestDeviceName
622 EFI_COMPONENT_NAME2_PROTOCOL
*CompName2
;
623 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
624 EFI_HANDLE
*HandleList
;
627 CHAR16
*DeviceNameToReturn
;
629 UINTN ParentControllerCount
;
630 EFI_HANDLE
*ParentControllerBuffer
;
631 UINTN ParentDriverCount
;
632 EFI_HANDLE
*ParentDriverBuffer
;
634 if (BestDeviceName
== NULL
||
637 return (EFI_INVALID_PARAMETER
);
641 // make sure one of the 2 supported bits is on
643 if (((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) == 0) &&
644 ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) == 0)) {
645 return (EFI_INVALID_PARAMETER
);
648 DeviceNameToReturn
= NULL
;
649 *BestDeviceName
= NULL
;
654 if ((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) != 0) {
655 Status
= ParseHandleDatabaseByRelationship(
658 HR_DRIVER_BINDING_HANDLE
|HR_DEVICE_DRIVER
,
661 for (LoopVar
= 0; LoopVar
< HandleCount
; LoopVar
++){
663 // Go through those handles until we get one that passes for GetComponentName
665 Status
= gBS
->OpenProtocol(
667 &gEfiComponentName2ProtocolGuid
,
671 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
672 if (EFI_ERROR(Status
)) {
673 Status
= gBS
->OpenProtocol(
675 &gEfiComponentNameProtocolGuid
,
679 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
682 if (EFI_ERROR(Status
)) {
685 Lang
= GetBestLanguageForDriver(CompName2
->SupportedLanguages
, Language
, FALSE
);
686 Status
= CompName2
->GetControllerName(CompName2
, DeviceHandle
, NULL
, Lang
, &DeviceNameToReturn
);
689 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
693 if (HandleList
!= NULL
) {
694 FreePool(HandleList
);
698 // Now check the parent controller using this as the child.
700 if (DeviceNameToReturn
== NULL
){
701 PARSE_HANDLE_DATABASE_PARENTS(DeviceHandle
, &ParentControllerCount
, &ParentControllerBuffer
);
702 for (LoopVar
= 0 ; LoopVar
< ParentControllerCount
; LoopVar
++) {
703 PARSE_HANDLE_DATABASE_UEFI_DRIVERS(ParentControllerBuffer
[LoopVar
], &ParentDriverCount
, &ParentDriverBuffer
);
704 for (HandleCount
= 0 ; HandleCount
< ParentDriverCount
; HandleCount
++) {
706 // try using that driver's component name with controller and our driver as the child.
708 Status
= gBS
->OpenProtocol(
709 ParentDriverBuffer
[HandleCount
],
710 &gEfiComponentName2ProtocolGuid
,
714 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
715 if (EFI_ERROR(Status
)) {
716 Status
= gBS
->OpenProtocol(
717 ParentDriverBuffer
[HandleCount
],
718 &gEfiComponentNameProtocolGuid
,
722 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
725 if (EFI_ERROR(Status
)) {
728 Lang
= GetBestLanguageForDriver(CompName2
->SupportedLanguages
, Language
, FALSE
);
729 Status
= CompName2
->GetControllerName(CompName2
, ParentControllerBuffer
[LoopVar
], DeviceHandle
, Lang
, &DeviceNameToReturn
);
732 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
739 SHELL_FREE_NON_NULL(ParentDriverBuffer
);
740 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
744 SHELL_FREE_NON_NULL(ParentControllerBuffer
);
747 // dont return on fail since we will try device path if that bit is on
749 if (DeviceNameToReturn
!= NULL
){
750 ASSERT(BestDeviceName
!= NULL
);
751 StrnCatGrow(BestDeviceName
, NULL
, DeviceNameToReturn
, 0);
752 return (EFI_SUCCESS
);
755 if ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) != 0) {
756 Status
= gBS
->OpenProtocol(
758 &gEfiDevicePathProtocolGuid
,
762 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
763 if (!EFI_ERROR(Status
)) {
765 // use device path to text on the device path
767 *BestDeviceName
= ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
768 return (EFI_SUCCESS
);
772 // none of the selected bits worked.
774 return (EFI_NOT_FOUND
);
778 Opens the root directory of a device on a handle
780 This function opens the root directory of a device and returns a file handle to it.
782 @param DeviceHandle The handle of the device that contains the volume.
783 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
786 @retval EFI_SUCCESS Root opened successfully.
787 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
789 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
790 @retval EFI_DEVICE_ERROR The device had an error.
791 @retval Others Error status returned from EFI_SIMPLE_FILE_SYSTEM_PROTOCOL->OpenVolume().
795 EfiShellOpenRootByHandle(
796 IN EFI_HANDLE DeviceHandle
,
797 OUT SHELL_FILE_HANDLE
*FileHandle
801 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*SimpleFileSystem
;
802 EFI_FILE_PROTOCOL
*RealFileHandle
;
803 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
806 // get the simple file system interface
808 Status
= gBS
->OpenProtocol(DeviceHandle
,
809 &gEfiSimpleFileSystemProtocolGuid
,
810 (VOID
**)&SimpleFileSystem
,
813 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
814 if (EFI_ERROR(Status
)) {
815 return (EFI_NOT_FOUND
);
818 Status
= gBS
->OpenProtocol(DeviceHandle
,
819 &gEfiDevicePathProtocolGuid
,
823 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
824 if (EFI_ERROR(Status
)) {
825 return (EFI_NOT_FOUND
);
828 // Open the root volume now...
830 Status
= SimpleFileSystem
->OpenVolume(SimpleFileSystem
, &RealFileHandle
);
831 if (EFI_ERROR(Status
)) {
835 *FileHandle
= ConvertEfiFileProtocolToShellHandle(RealFileHandle
, EfiShellGetMapFromDevicePath(&DevPath
));
836 return (EFI_SUCCESS
);
840 Opens the root directory of a device.
842 This function opens the root directory of a device and returns a file handle to it.
844 @param DevicePath Points to the device path corresponding to the device where the
845 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
846 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
849 @retval EFI_SUCCESS Root opened successfully.
850 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
852 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
853 @retval EFI_DEVICE_ERROR The device had an error
854 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
859 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
860 OUT SHELL_FILE_HANDLE
*FileHandle
866 if (FileHandle
== NULL
) {
867 return (EFI_INVALID_PARAMETER
);
871 // find the handle of the device with that device handle and the file system
874 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
877 if (EFI_ERROR(Status
)) {
878 return (EFI_NOT_FOUND
);
881 return (EfiShellOpenRootByHandle(Handle
, FileHandle
));
885 Returns whether any script files are currently being processed.
887 @retval TRUE There is at least one script file active.
888 @retval FALSE No script files are active now.
893 EfiShellBatchIsActive (
897 if (ShellCommandGetCurrentScriptFile() == NULL
) {
904 Worker function to open a file based on a device path. this will open the root
905 of the volume and then traverse down to the file itself.
907 @param DevicePath Device Path of the file.
908 @param FileHandle Pointer to the file upon a successful return.
909 @param OpenMode mode to open file in.
910 @param Attributes the File Attributes to use when creating a new file.
912 @retval EFI_SUCCESS the file is open and FileHandle is valid
913 @retval EFI_UNSUPPORTED the device path cotained non-path elements
914 @retval other an error ocurred.
917 InternalOpenFileDevicePath(
918 IN OUT EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
919 OUT SHELL_FILE_HANDLE
*FileHandle
,
921 IN UINT64 Attributes OPTIONAL
925 FILEPATH_DEVICE_PATH
*FilePathNode
;
927 SHELL_FILE_HANDLE ShellHandle
;
928 EFI_FILE_PROTOCOL
*Handle1
;
929 EFI_FILE_PROTOCOL
*Handle2
;
930 FILEPATH_DEVICE_PATH
*AlignedNode
;
932 if (FileHandle
== NULL
) {
933 return (EFI_INVALID_PARAMETER
);
943 Status
= EfiShellOpenRoot(DevicePath
, &ShellHandle
);
945 if (!EFI_ERROR(Status
)) {
946 Handle1
= ConvertShellHandleToEfiFileProtocol(ShellHandle
);
947 if (Handle1
!= NULL
) {
949 // chop off the begining part before the file system part...
952 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
955 if (!EFI_ERROR(Status
)) {
957 // To access as a file system, the file path should only
958 // contain file path components. Follow the file path nodes
959 // and find the target file
961 for ( FilePathNode
= (FILEPATH_DEVICE_PATH
*)DevicePath
962 ; !IsDevicePathEnd (&FilePathNode
->Header
)
963 ; FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
)
965 SHELL_FREE_NON_NULL(AlignedNode
);
966 AlignedNode
= AllocateCopyPool (DevicePathNodeLength(FilePathNode
), FilePathNode
);
968 // For file system access each node should be a file path component
970 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
971 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
973 Status
= EFI_UNSUPPORTED
;
978 // Open this file path node
984 // if this is the last node in the DevicePath always create (if that was requested).
986 if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode
->Header
)))) {
987 Status
= Handle2
->Open (
990 AlignedNode
->PathName
,
997 // This is not the last node and we dont want to 'create' existing
998 // directory entries...
1002 // open without letting it create
1003 // prevents error on existing files/directories
1005 Status
= Handle2
->Open (
1008 AlignedNode
->PathName
,
1009 OpenMode
&~EFI_FILE_MODE_CREATE
,
1013 // if above failed now open and create the 'item'
1014 // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)
1016 if ((EFI_ERROR (Status
)) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)) {
1017 Status
= Handle2
->Open (
1020 AlignedNode
->PathName
,
1027 // Close the last node
1029 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Handle2
);
1032 // If there's been an error, stop
1034 if (EFI_ERROR (Status
)) {
1041 SHELL_FREE_NON_NULL(AlignedNode
);
1042 if (EFI_ERROR(Status
)) {
1043 if (Handle1
!= NULL
) {
1044 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(Handle1
);
1047 *FileHandle
= ConvertEfiFileProtocolToShellHandle(Handle1
, ShellFileHandleGetPath(ShellHandle
));
1053 Creates a file or directory by name.
1055 This function creates an empty new file or directory with the specified attributes and
1056 returns the new file's handle. If the file already exists and is read-only, then
1057 EFI_INVALID_PARAMETER will be returned.
1059 If the file already existed, it is truncated and its attributes updated. If the file is
1060 created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.
1062 If the file name begins with >v, then the file handle which is returned refers to the
1063 shell environment variable with the specified name. If the shell environment variable
1064 already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.
1066 @param FileName Pointer to NULL-terminated file path
1067 @param FileAttribs The new file's attrbiutes. the different attributes are
1068 described in EFI_FILE_PROTOCOL.Open().
1069 @param FileHandle On return, points to the created file handle or directory's handle
1071 @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.
1072 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1073 @retval EFI_UNSUPPORTED could not open the file path
1074 @retval EFI_NOT_FOUND the specified file could not be found on the devide, or could not
1075 file the file system on the device.
1076 @retval EFI_NO_MEDIA the device has no medium.
1077 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1079 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1081 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1082 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1083 when the media is write-protected.
1084 @retval EFI_ACCESS_DENIED The service denied access to the file.
1085 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
1086 @retval EFI_VOLUME_FULL The volume is full.
1091 IN CONST CHAR16
*FileName
,
1092 IN UINT64 FileAttribs
,
1093 OUT SHELL_FILE_HANDLE
*FileHandle
1096 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1101 // Is this for an environment variable
1102 // do we start with >v
1104 if (StrStr(FileName
, L
">v") == FileName
) {
1105 Status
= IsVolatileEnv (FileName
+ 2, &Volatile
);
1106 if (EFI_ERROR (Status
)) {
1110 return (EFI_INVALID_PARAMETER
);
1112 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1113 return (EFI_SUCCESS
);
1117 // We are opening a regular file.
1119 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1120 if (DevicePath
== NULL
) {
1121 return (EFI_NOT_FOUND
);
1124 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, FileAttribs
);
1125 FreePool(DevicePath
);
1131 Register a GUID and a localized human readable name for it.
1133 If Guid is not assigned a name, then assign GuidName to Guid. This list of GUID
1134 names must be used whenever a shell command outputs GUID information.
1136 This function is only available when the major and minor versions in the
1137 EfiShellProtocol are greater than or equal to 2 and 1, respectively.
1139 @param[in] Guid A pointer to the GUID being registered.
1140 @param[in] GuidName A pointer to the localized name for the GUID being registered.
1142 @retval EFI_SUCCESS The operation was successful.
1143 @retval EFI_INVALID_PARAMETER Guid was NULL.
1144 @retval EFI_INVALID_PARAMETER GuidName was NULL.
1145 @retval EFI_ACCESS_DENIED Guid already is assigned a name.
1149 EfiShellRegisterGuidName(
1150 IN CONST EFI_GUID
*Guid
,
1151 IN CONST CHAR16
*GuidName
1154 return (AddNewGuidNameMapping(Guid
, GuidName
, NULL
));
1158 Opens a file or a directory by file name.
1160 This function opens the specified file in the specified OpenMode and returns a file
1162 If the file name begins with >v, then the file handle which is returned refers to the
1163 shell environment variable with the specified name. If the shell environment variable
1164 exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then
1165 EFI_INVALID_PARAMETER is returned.
1167 If the file name is >i, then the file handle which is returned refers to the standard
1168 input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER
1171 If the file name is >o, then the file handle which is returned refers to the standard
1172 output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1175 If the file name is >e, then the file handle which is returned refers to the standard
1176 error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1179 If the file name is NUL, then the file handle that is returned refers to the standard NUL
1180 file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is
1183 If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the
1186 @param FileName Points to the NULL-terminated UCS-2 encoded file name.
1187 @param FileHandle On return, points to the file handle.
1188 @param OpenMode File open mode. Either EFI_FILE_MODE_READ or
1189 EFI_FILE_MODE_WRITE from section 12.4 of the UEFI
1191 @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.
1192 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.
1193 @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.
1194 @retval EFI_NOT_FOUND The specified file could not be found on the device or the file
1195 system could not be found on the device. FileHandle is NULL.
1196 @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.
1197 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1198 longer supported. FileHandle is NULL.
1199 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1200 the FileName. FileHandle is NULL.
1201 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.
1202 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1203 when the media is write-protected. FileHandle is NULL.
1204 @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.
1205 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle
1207 @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.
1211 EfiShellOpenFileByName(
1212 IN CONST CHAR16
*FileName
,
1213 OUT SHELL_FILE_HANDLE
*FileHandle
,
1217 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1224 // Is this for StdIn
1226 if (StrCmp(FileName
, L
">i") == 0) {
1228 // make sure not writing to StdIn
1230 if ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0) {
1231 return (EFI_INVALID_PARAMETER
);
1233 *FileHandle
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1234 ASSERT(*FileHandle
!= NULL
);
1235 return (EFI_SUCCESS
);
1239 // Is this for StdOut
1241 if (StrCmp(FileName
, L
">o") == 0) {
1243 // make sure not writing to StdIn
1245 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1246 return (EFI_INVALID_PARAMETER
);
1248 *FileHandle
= &FileInterfaceStdOut
;
1249 return (EFI_SUCCESS
);
1253 // Is this for NUL / NULL file
1255 if ((gUnicodeCollation
->StriColl (gUnicodeCollation
, (CHAR16
*)FileName
, L
"NUL") == 0) ||
1256 (gUnicodeCollation
->StriColl (gUnicodeCollation
, (CHAR16
*)FileName
, L
"NULL") == 0)) {
1257 *FileHandle
= &FileInterfaceNulFile
;
1258 return (EFI_SUCCESS
);
1262 // Is this for StdErr
1264 if (StrCmp(FileName
, L
">e") == 0) {
1266 // make sure not writing to StdIn
1268 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1269 return (EFI_INVALID_PARAMETER
);
1271 *FileHandle
= &FileInterfaceStdErr
;
1272 return (EFI_SUCCESS
);
1276 // Is this for an environment variable
1277 // do we start with >v
1279 if (StrStr(FileName
, L
">v") == FileName
) {
1280 Status
= IsVolatileEnv (FileName
+ 2, &Volatile
);
1281 if (EFI_ERROR (Status
)) {
1285 ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0)) {
1286 return (EFI_INVALID_PARAMETER
);
1288 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1289 return (EFI_SUCCESS
);
1293 // We are opening a regular file.
1295 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1297 if (DevicePath
== NULL
) {
1298 return (EFI_NOT_FOUND
);
1302 // Copy the device path, open the file, then free the memory
1304 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, OpenMode
, 0); // 0 = no specific file attributes
1305 FreePool(DevicePath
);
1311 Deletes the file specified by the file name.
1313 This function deletes a file.
1315 @param FileName Points to the NULL-terminated file name.
1317 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.
1318 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
1319 @sa EfiShellCreateFile
1323 EfiShellDeleteFileByName(
1324 IN CONST CHAR16
*FileName
1327 SHELL_FILE_HANDLE FileHandle
;
1333 // get a handle to the file
1335 Status
= EfiShellCreateFile(FileName
,
1338 if (EFI_ERROR(Status
)) {
1342 // now delete the file
1344 ShellFileHandleRemove(FileHandle
);
1345 return (ShellInfoObject
.NewEfiShellProtocol
->DeleteFile(FileHandle
));
1349 Disables the page break output mode.
1353 EfiShellDisablePageBreak (
1357 ShellInfoObject
.PageBreakEnabled
= FALSE
;
1361 Enables the page break output mode.
1365 EfiShellEnablePageBreak (
1369 ShellInfoObject
.PageBreakEnabled
= TRUE
;
1373 internal worker function to load and run an image via device path.
1375 @param ParentImageHandle A handle of the image that is executing the specified
1377 @param DevicePath device path of the file to execute
1378 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1379 containing the command line. If NULL then the command-
1381 @param Environment Points to a NULL-terminated array of environment
1382 variables with the format 'x=y', where x is the
1383 environment variable name and y is the value. If this
1384 is NULL, then the current shell environment is used.
1386 @param[out] StartImageStatus Returned status from gBS->StartImage.
1388 @retval EFI_SUCCESS The command executed successfully. The status code
1389 returned by the command is pointed to by StatusCode.
1390 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1391 @retval EFI_OUT_OF_RESOURCES Out of resources.
1392 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1395 InternalShellExecuteDevicePath(
1396 IN CONST EFI_HANDLE
*ParentImageHandle
,
1397 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1398 IN CONST CHAR16
*CommandLine OPTIONAL
,
1399 IN CONST CHAR16
**Environment OPTIONAL
,
1400 OUT EFI_STATUS
*StartImageStatus OPTIONAL
1404 EFI_STATUS StartStatus
;
1405 EFI_STATUS CleanupStatus
;
1406 EFI_HANDLE NewHandle
;
1407 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1408 LIST_ENTRY OrigEnvs
;
1409 EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol
;
1415 if (ParentImageHandle
== NULL
) {
1416 return (EFI_INVALID_PARAMETER
);
1419 InitializeListHead(&OrigEnvs
);
1420 ZeroMem(&ShellParamsProtocol
, sizeof(EFI_SHELL_PARAMETERS_PROTOCOL
));
1424 NewCmdLine
= AllocateCopyPool (StrSize (CommandLine
), CommandLine
);
1425 if (NewCmdLine
== NULL
) {
1426 return EFI_OUT_OF_RESOURCES
;
1429 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
1430 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
1431 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
1436 // Load the image with:
1437 // FALSE - not from boot manager and NULL, 0 being not already in memory
1439 Status
= gBS
->LoadImage(
1442 (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
,
1447 if (EFI_ERROR(Status
)) {
1448 if (NewHandle
!= NULL
) {
1449 gBS
->UnloadImage(NewHandle
);
1451 FreePool (NewCmdLine
);
1454 Status
= gBS
->OpenProtocol(
1456 &gEfiLoadedImageProtocolGuid
,
1457 (VOID
**)&LoadedImage
,
1460 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1462 if (!EFI_ERROR(Status
)) {
1464 // If the image is not an app abort it.
1466 if (LoadedImage
->ImageCodeType
!= EfiLoaderCode
){
1471 STRING_TOKEN (STR_SHELL_IMAGE_NOT_APP
),
1472 ShellInfoObject
.HiiHandle
1477 ASSERT(LoadedImage
->LoadOptionsSize
== 0);
1478 if (NewCmdLine
!= NULL
) {
1479 LoadedImage
->LoadOptionsSize
= (UINT32
)StrSize(NewCmdLine
);
1480 LoadedImage
->LoadOptions
= (VOID
*)NewCmdLine
;
1484 // Save our current environment settings for later restoration if necessary
1486 if (Environment
!= NULL
) {
1487 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1488 if (!EFI_ERROR(Status
)) {
1489 Status
= SetEnvironmentVariables(Environment
);
1494 // Initialize and install a shell parameters protocol on the image.
1496 ShellParamsProtocol
.StdIn
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1497 ShellParamsProtocol
.StdOut
= ShellInfoObject
.NewShellParametersProtocol
->StdOut
;
1498 ShellParamsProtocol
.StdErr
= ShellInfoObject
.NewShellParametersProtocol
->StdErr
;
1499 Status
= UpdateArgcArgv(&ShellParamsProtocol
, NewCmdLine
, Efi_Application
, NULL
, NULL
);
1500 if (EFI_ERROR (Status
)) {
1505 // Replace Argv[0] with the full path of the binary we're executing:
1506 // If the command line was "foo", the binary might be called "foo.efi".
1507 // "The first entry in [Argv] is always the full file path of the
1508 // executable" - UEFI Shell Spec section 2.3
1510 ImagePath
= EfiShellGetFilePathFromDevicePath (DevicePath
);
1511 // The image we're executing isn't necessarily in a filesystem - it might
1512 // be memory mapped. In this case EfiShellGetFilePathFromDevicePath will
1513 // return NULL, and we'll leave Argv[0] as UpdateArgcArgv set it.
1514 if (ImagePath
!= NULL
) {
1515 if (ShellParamsProtocol
.Argv
== NULL
) {
1516 // Command line was empty or null.
1517 // (UpdateArgcArgv sets Argv to NULL when CommandLine is "" or NULL)
1518 ShellParamsProtocol
.Argv
= AllocatePool (sizeof (CHAR16
*));
1519 if (ShellParamsProtocol
.Argv
== NULL
) {
1520 Status
= EFI_OUT_OF_RESOURCES
;
1523 ShellParamsProtocol
.Argc
= 1;
1525 // Free the string UpdateArgcArgv put in Argv[0];
1526 FreePool (ShellParamsProtocol
.Argv
[0]);
1528 ShellParamsProtocol
.Argv
[0] = ImagePath
;
1531 Status
= gBS
->InstallProtocolInterface(&NewHandle
, &gEfiShellParametersProtocolGuid
, EFI_NATIVE_INTERFACE
, &ShellParamsProtocol
);
1532 ASSERT_EFI_ERROR(Status
);
1534 ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)
1537 // now start the image and if the caller wanted the return code pass it to them...
1539 if (!EFI_ERROR(Status
)) {
1540 StartStatus
= gBS
->StartImage(
1545 if (StartImageStatus
!= NULL
) {
1546 *StartImageStatus
= StartStatus
;
1549 CleanupStatus
= gBS
->UninstallProtocolInterface(
1551 &gEfiShellParametersProtocolGuid
,
1552 &ShellParamsProtocol
1554 ASSERT_EFI_ERROR(CleanupStatus
);
1560 // Unload image - We should only get here if we didn't call StartImage
1561 gBS
->UnloadImage (NewHandle
);
1564 // Free Argv (Allocated in UpdateArgcArgv)
1565 if (ShellParamsProtocol
.Argv
!= NULL
) {
1566 for (Index
= 0; Index
< ShellParamsProtocol
.Argc
; Index
++) {
1567 if (ShellParamsProtocol
.Argv
[Index
] != NULL
) {
1568 FreePool (ShellParamsProtocol
.Argv
[Index
]);
1571 FreePool (ShellParamsProtocol
.Argv
);
1575 // Restore environment variables
1576 if (!IsListEmpty(&OrigEnvs
)) {
1577 CleanupStatus
= SetEnvironmentVariableList(&OrigEnvs
);
1578 ASSERT_EFI_ERROR (CleanupStatus
);
1581 FreePool (NewCmdLine
);
1587 internal worker function to load and run an image in the current shell.
1589 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1590 containing the command line. If NULL then the command-
1592 @param Environment Points to a NULL-terminated array of environment
1593 variables with the format 'x=y', where x is the
1594 environment variable name and y is the value. If this
1595 is NULL, then the current shell environment is used.
1597 @param[out] StartImageStatus Returned status from the command line.
1599 @retval EFI_SUCCESS The command executed successfully. The status code
1600 returned by the command is pointed to by StatusCode.
1601 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1602 @retval EFI_OUT_OF_RESOURCES Out of resources.
1603 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1606 InternalShellExecute(
1607 IN CONST CHAR16
*CommandLine OPTIONAL
,
1608 IN CONST CHAR16
**Environment OPTIONAL
,
1609 OUT EFI_STATUS
*StartImageStatus OPTIONAL
1613 EFI_STATUS CleanupStatus
;
1614 LIST_ENTRY OrigEnvs
;
1616 InitializeListHead(&OrigEnvs
);
1619 // Save our current environment settings for later restoration if necessary
1621 if (Environment
!= NULL
) {
1622 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1623 if (!EFI_ERROR(Status
)) {
1624 Status
= SetEnvironmentVariables(Environment
);
1630 Status
= RunShellCommand(CommandLine
, StartImageStatus
);
1632 // Restore environment variables
1633 if (!IsListEmpty(&OrigEnvs
)) {
1634 CleanupStatus
= SetEnvironmentVariableList(&OrigEnvs
);
1635 ASSERT_EFI_ERROR (CleanupStatus
);
1642 Determine if the UEFI Shell is currently running with nesting enabled or disabled.
1644 @retval FALSE nesting is required
1645 @retval other nesting is enabled
1663 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
) {
1666 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(mNoNestingEnvVarName
, &TempSize
, Temp
);
1667 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1668 Temp
= AllocateZeroPool(TempSize
+ sizeof(CHAR16
));
1670 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(mNoNestingEnvVarName
, &TempSize
, Temp
);
1673 Temp2
= StrnCatGrow(&Temp2
, NULL
, mNoNestingTrue
, 0);
1674 if (Temp
!= NULL
&& Temp2
!= NULL
&& StringNoCaseCompare(&Temp
, &Temp2
) == 0) {
1676 // Use the no nesting method.
1682 SHELL_FREE_NON_NULL(Temp
);
1683 SHELL_FREE_NON_NULL(Temp2
);
1688 Execute the command line.
1690 This function creates a nested instance of the shell and executes the specified
1691 command (CommandLine) with the specified environment (Environment). Upon return,
1692 the status code returned by the specified command is placed in StatusCode.
1694 If Environment is NULL, then the current environment is used and all changes made
1695 by the commands executed will be reflected in the current environment. If the
1696 Environment is non-NULL, then the changes made will be discarded.
1698 The CommandLine is executed from the current working directory on the current
1701 @param ParentImageHandle A handle of the image that is executing the specified
1703 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1704 containing the command line. If NULL then the command-
1706 @param Environment Points to a NULL-terminated array of environment
1707 variables with the format 'x=y', where x is the
1708 environment variable name and y is the value. If this
1709 is NULL, then the current shell environment is used.
1710 @param StatusCode Points to the status code returned by the CommandLine.
1712 @retval EFI_SUCCESS The command executed successfully. The status code
1713 returned by the command is pointed to by StatusCode.
1714 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1715 @retval EFI_OUT_OF_RESOURCES Out of resources.
1716 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1717 @retval EFI_UNSUPPORTED The support level required for this function is not present.
1719 @sa InternalShellExecuteDevicePath
1724 IN EFI_HANDLE
*ParentImageHandle
,
1725 IN CHAR16
*CommandLine OPTIONAL
,
1726 IN CHAR16
**Environment OPTIONAL
,
1727 OUT EFI_STATUS
*StatusCode OPTIONAL
1732 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1735 if ((PcdGet8(PcdShellSupportLevel
) < 1)) {
1736 return (EFI_UNSUPPORTED
);
1739 if (NestingEnabled()) {
1740 DevPath
= AppendDevicePath (ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
1743 Temp
= ConvertDevicePathToText(ShellInfoObject
.FileDevPath
, TRUE
, TRUE
);
1745 Temp
= ConvertDevicePathToText(ShellInfoObject
.ImageDevPath
, TRUE
, TRUE
);
1747 Temp
= ConvertDevicePathToText(DevPath
, TRUE
, TRUE
);
1753 ASSERT((Temp
== NULL
&& Size
== 0) || (Temp
!= NULL
));
1754 StrnCatGrow(&Temp
, &Size
, L
"Shell.efi -exit ", 0);
1755 StrnCatGrow(&Temp
, &Size
, CommandLine
, 0);
1757 Status
= InternalShellExecuteDevicePath(
1761 (CONST CHAR16
**)Environment
,
1765 // de-allocate and return
1770 Status
= InternalShellExecute(
1771 (CONST CHAR16
*)CommandLine
,
1772 (CONST CHAR16
**)Environment
,
1780 Utility cleanup function for EFI_SHELL_FILE_INFO objects.
1782 1) frees all pointers (non-NULL)
1783 2) Closes the SHELL_FILE_HANDLE
1785 @param FileListNode pointer to the list node to free
1788 InternalFreeShellFileInfoNode(
1789 IN EFI_SHELL_FILE_INFO
*FileListNode
1792 if (FileListNode
->Info
!= NULL
) {
1793 FreePool((VOID
*)FileListNode
->Info
);
1795 if (FileListNode
->FileName
!= NULL
) {
1796 FreePool((VOID
*)FileListNode
->FileName
);
1798 if (FileListNode
->FullName
!= NULL
) {
1799 FreePool((VOID
*)FileListNode
->FullName
);
1801 if (FileListNode
->Handle
!= NULL
) {
1802 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileListNode
->Handle
);
1804 FreePool(FileListNode
);
1807 Frees the file list.
1809 This function cleans up the file list and any related data structures. It has no
1810 impact on the files themselves.
1812 @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is
1813 defined in OpenFileList()
1815 @retval EFI_SUCCESS Free the file list successfully.
1816 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1820 EfiShellFreeFileList(
1821 IN EFI_SHELL_FILE_INFO
**FileList
1824 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1826 if (FileList
== NULL
|| *FileList
== NULL
) {
1827 return (EFI_INVALID_PARAMETER
);
1830 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1831 ; !IsListEmpty(&(*FileList
)->Link
)
1832 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1834 RemoveEntryList(&ShellFileListItem
->Link
);
1835 InternalFreeShellFileInfoNode(ShellFileListItem
);
1837 InternalFreeShellFileInfoNode(*FileList
);
1839 return(EFI_SUCCESS
);
1843 Deletes the duplicate file names files in the given file list.
1845 This function deletes the reduplicate files in the given file list.
1847 @param FileList A pointer to the first entry in the file list.
1849 @retval EFI_SUCCESS Always success.
1850 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1854 EfiShellRemoveDupInFileList(
1855 IN EFI_SHELL_FILE_INFO
**FileList
1858 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1859 EFI_SHELL_FILE_INFO
*ShellFileListItem2
;
1860 EFI_SHELL_FILE_INFO
*TempNode
;
1862 if (FileList
== NULL
|| *FileList
== NULL
) {
1863 return (EFI_INVALID_PARAMETER
);
1865 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1866 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1867 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1869 for ( ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1870 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1871 ; ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1873 if (gUnicodeCollation
->StriColl(
1875 (CHAR16
*)ShellFileListItem
->FullName
,
1876 (CHAR16
*)ShellFileListItem2
->FullName
) == 0
1878 TempNode
= (EFI_SHELL_FILE_INFO
*)GetPreviousNode(
1880 &ShellFileListItem2
->Link
1882 RemoveEntryList(&ShellFileListItem2
->Link
);
1883 InternalFreeShellFileInfoNode(ShellFileListItem2
);
1884 // Set ShellFileListItem2 to PreviousNode so we don't access Freed
1885 // memory in GetNextNode in the loop expression above.
1886 ShellFileListItem2
= TempNode
;
1890 return (EFI_SUCCESS
);
1894 // This is the same structure as the external version, but it has no CONST qualifiers.
1897 LIST_ENTRY Link
; ///< Linked list members.
1898 EFI_STATUS Status
; ///< Status of opening the file. Valid only if Handle != NULL.
1899 CHAR16
*FullName
; ///< Fully qualified filename.
1900 CHAR16
*FileName
; ///< name of this file.
1901 SHELL_FILE_HANDLE Handle
; ///< Handle for interacting with the opened file or NULL if closed.
1902 EFI_FILE_INFO
*Info
; ///< Pointer to the FileInfo struct for this file or NULL.
1903 } EFI_SHELL_FILE_INFO_NO_CONST
;
1906 Allocates and duplicates a EFI_SHELL_FILE_INFO node.
1908 @param[in] Node The node to copy from.
1909 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.
1911 @retval NULL a memory allocation error ocurred
1912 @return != NULL a pointer to the new node
1914 EFI_SHELL_FILE_INFO
*
1915 InternalDuplicateShellFileInfo(
1916 IN EFI_SHELL_FILE_INFO
*Node
,
1920 EFI_SHELL_FILE_INFO_NO_CONST
*NewNode
;
1923 // try to confirm that the objects are in sync
1925 ASSERT(sizeof(EFI_SHELL_FILE_INFO_NO_CONST
) == sizeof(EFI_SHELL_FILE_INFO
));
1927 NewNode
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1928 if (NewNode
== NULL
) {
1931 NewNode
->FullName
= AllocateCopyPool(StrSize(Node
->FullName
), Node
->FullName
);
1932 NewNode
->FileName
= AllocateCopyPool(StrSize(Node
->FileName
), Node
->FileName
);
1933 NewNode
->Info
= AllocateCopyPool((UINTN
)Node
->Info
->Size
, Node
->Info
);
1934 if ( NewNode
->FullName
== NULL
1935 || NewNode
->FileName
== NULL
1936 || NewNode
->Info
== NULL
1938 SHELL_FREE_NON_NULL(NewNode
->FullName
);
1939 SHELL_FREE_NON_NULL(NewNode
->FileName
);
1940 SHELL_FREE_NON_NULL(NewNode
->Info
);
1941 SHELL_FREE_NON_NULL(NewNode
);
1944 NewNode
->Status
= Node
->Status
;
1945 NewNode
->Handle
= Node
->Handle
;
1947 Node
->Handle
= NULL
;
1950 return((EFI_SHELL_FILE_INFO
*)NewNode
);
1954 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation
1955 failed it will return NULL.
1957 @param[in] BasePath the Path to prepend onto filename for FullPath
1958 @param[in] Status Status member initial value.
1959 @param[in] FileName FileName member initial value.
1960 @param[in] Handle Handle member initial value.
1961 @param[in] Info Info struct to copy.
1963 @retval NULL An error ocurred.
1964 @return a pointer to the newly allocated structure.
1966 EFI_SHELL_FILE_INFO
*
1967 CreateAndPopulateShellFileInfo(
1968 IN CONST CHAR16
*BasePath
,
1969 IN CONST EFI_STATUS Status
,
1970 IN CONST CHAR16
*FileName
,
1971 IN CONST SHELL_FILE_HANDLE Handle
,
1972 IN CONST EFI_FILE_INFO
*Info
1975 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1982 ShellFileListItem
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1983 if (ShellFileListItem
== NULL
) {
1986 if (Info
!= NULL
&& Info
->Size
!= 0) {
1987 ShellFileListItem
->Info
= AllocateZeroPool((UINTN
)Info
->Size
);
1988 if (ShellFileListItem
->Info
== NULL
) {
1989 FreePool(ShellFileListItem
);
1992 CopyMem(ShellFileListItem
->Info
, Info
, (UINTN
)Info
->Size
);
1994 ShellFileListItem
->Info
= NULL
;
1996 if (FileName
!= NULL
) {
1997 ASSERT(TempString
== NULL
);
1998 ShellFileListItem
->FileName
= StrnCatGrow(&TempString
, 0, FileName
, 0);
1999 if (ShellFileListItem
->FileName
== NULL
) {
2000 FreePool(ShellFileListItem
->Info
);
2001 FreePool(ShellFileListItem
);
2005 ShellFileListItem
->FileName
= NULL
;
2009 if (BasePath
!= NULL
) {
2010 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2011 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
2012 if (TempString
== NULL
) {
2013 FreePool((VOID
*)ShellFileListItem
->FileName
);
2014 SHELL_FREE_NON_NULL(ShellFileListItem
->Info
);
2015 FreePool(ShellFileListItem
);
2019 if (ShellFileListItem
->FileName
!= NULL
) {
2020 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2021 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileListItem
->FileName
, 0);
2022 if (TempString
== NULL
) {
2023 FreePool((VOID
*)ShellFileListItem
->FileName
);
2024 FreePool(ShellFileListItem
->Info
);
2025 FreePool(ShellFileListItem
);
2030 TempString
= PathCleanUpDirectories(TempString
);
2032 ShellFileListItem
->FullName
= TempString
;
2033 ShellFileListItem
->Status
= Status
;
2034 ShellFileListItem
->Handle
= Handle
;
2036 return (ShellFileListItem
);
2040 Find all files in a specified directory.
2042 @param FileDirHandle Handle of the directory to search.
2043 @param FileList On return, points to the list of files in the directory
2044 or NULL if there are no files in the directory.
2046 @retval EFI_SUCCESS File information was returned successfully.
2047 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.
2048 @retval EFI_DEVICE_ERROR The device reported an error.
2049 @retval EFI_NO_MEDIA The device media is not present.
2050 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.
2051 @return An error from FileHandleGetFileName().
2055 EfiShellFindFilesInDir(
2056 IN SHELL_FILE_HANDLE FileDirHandle
,
2057 OUT EFI_SHELL_FILE_INFO
**FileList
2060 EFI_SHELL_FILE_INFO
*ShellFileList
;
2061 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2062 EFI_FILE_INFO
*FileInfo
;
2071 Status
= FileHandleGetFileName(FileDirHandle
, &BasePath
);
2072 if (EFI_ERROR(Status
)) {
2076 if (ShellFileHandleGetPath(FileDirHandle
) != NULL
) {
2079 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileHandleGetPath(FileDirHandle
), 0);
2080 if (TempString
== NULL
) {
2081 SHELL_FREE_NON_NULL(BasePath
);
2082 return (EFI_OUT_OF_RESOURCES
);
2084 TempSpot
= StrStr(TempString
, L
";");
2086 if (TempSpot
!= NULL
) {
2087 *TempSpot
= CHAR_NULL
;
2090 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
2091 if (TempString
== NULL
) {
2092 SHELL_FREE_NON_NULL(BasePath
);
2093 return (EFI_OUT_OF_RESOURCES
);
2095 SHELL_FREE_NON_NULL(BasePath
);
2096 BasePath
= TempString
;
2100 ShellFileList
= NULL
;
2101 ShellFileListItem
= NULL
;
2103 Status
= EFI_SUCCESS
;
2106 for ( Status
= FileHandleFindFirstFile(FileDirHandle
, &FileInfo
)
2107 ; !EFI_ERROR(Status
) && !NoFile
2108 ; Status
= FileHandleFindNextFile(FileDirHandle
, FileInfo
, &NoFile
)
2110 if (ShellFileList
== NULL
) {
2111 ShellFileList
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2112 if (ShellFileList
== NULL
) {
2113 SHELL_FREE_NON_NULL (BasePath
);
2114 return EFI_OUT_OF_RESOURCES
;
2116 InitializeListHead(&ShellFileList
->Link
);
2119 // allocate a new EFI_SHELL_FILE_INFO and populate it...
2121 ShellFileListItem
= CreateAndPopulateShellFileInfo(
2123 EFI_SUCCESS
, // success since we didnt fail to open it...
2125 NULL
, // no handle since not open
2127 if (ShellFileListItem
== NULL
) {
2128 Status
= EFI_OUT_OF_RESOURCES
;
2130 // Free resources outside the loop.
2134 InsertTailList(&ShellFileList
->Link
, &ShellFileListItem
->Link
);
2136 if (EFI_ERROR(Status
)) {
2137 EfiShellFreeFileList(&ShellFileList
);
2140 *FileList
= ShellFileList
;
2142 SHELL_FREE_NON_NULL(BasePath
);
2147 Get the GUID value from a human readable name.
2149 If GuidName is a known GUID name, then update Guid to have the correct value for
2152 This function is only available when the major and minor versions in the
2153 EfiShellProtocol are greater than or equal to 2 and 1, respectively.
2155 @param[in] GuidName A pointer to the localized name for the GUID being queried.
2156 @param[out] Guid A pointer to the GUID structure to be filled in.
2158 @retval EFI_SUCCESS The operation was successful.
2159 @retval EFI_INVALID_PARAMETER Guid was NULL.
2160 @retval EFI_INVALID_PARAMETER GuidName was NULL.
2161 @retval EFI_NOT_FOUND GuidName is not a known GUID Name.
2165 EfiShellGetGuidFromName(
2166 IN CONST CHAR16
*GuidName
,
2173 if (Guid
== NULL
|| GuidName
== NULL
) {
2174 return (EFI_INVALID_PARAMETER
);
2177 Status
= GetGuidFromStringName(GuidName
, NULL
, &NewGuid
);
2179 if (!EFI_ERROR(Status
)) {
2180 CopyGuid(Guid
, NewGuid
);
2187 Get the human readable name for a GUID from the value.
2189 If Guid is assigned a name, then update *GuidName to point to the name. The callee
2190 should not modify the value.
2192 This function is only available when the major and minor versions in the
2193 EfiShellProtocol are greater than or equal to 2 and 1, respectively.
2195 @param[in] Guid A pointer to the GUID being queried.
2196 @param[out] GuidName A pointer to a pointer the localized to name for the GUID being requested
2198 @retval EFI_SUCCESS The operation was successful.
2199 @retval EFI_INVALID_PARAMETER Guid was NULL.
2200 @retval EFI_INVALID_PARAMETER GuidName was NULL.
2201 @retval EFI_NOT_FOUND Guid is not assigned a name.
2205 EfiShellGetGuidName(
2206 IN CONST EFI_GUID
*Guid
,
2207 OUT CONST CHAR16
**GuidName
2212 if (Guid
== NULL
|| GuidName
== NULL
) {
2213 return (EFI_INVALID_PARAMETER
);
2216 Name
= GetStringNameFromGuid(Guid
, NULL
);
2217 if (Name
== NULL
|| StrLen(Name
) == 0) {
2218 SHELL_FREE_NON_NULL(Name
);
2219 return (EFI_NOT_FOUND
);
2222 *GuidName
= AddBufferToFreeList(Name
);
2224 return (EFI_SUCCESS
);
2230 If FileHandle is a directory then the function reads from FileHandle and reads in
2231 each of the FileInfo structures. If one of them matches the Pattern's first
2232 "level" then it opens that handle and calls itself on that handle.
2234 If FileHandle is a file and matches all of the remaining Pattern (which would be
2235 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.
2237 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call
2238 FreeFileList with FileList.
2240 @param[in] FilePattern The FilePattern to check against.
2241 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure
2242 @param[in] FileHandle The FileHandle to start with
2243 @param[in, out] FileList pointer to pointer to list of found files.
2244 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.
2245 @param[in] MapName The file system name this file is on.
2247 @retval EFI_SUCCESS all files were found and the FileList contains a list.
2248 @retval EFI_NOT_FOUND no files were found
2249 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
2253 IN CONST CHAR16
*FilePattern
,
2254 IN EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
,
2255 IN SHELL_FILE_HANDLE FileHandle
,
2256 IN OUT EFI_SHELL_FILE_INFO
**FileList
,
2257 IN CONST EFI_SHELL_FILE_INFO
*ParentNode OPTIONAL
,
2258 IN CONST CHAR16
*MapName
2262 CONST CHAR16
*NextFilePatternStart
;
2263 CHAR16
*CurrentFilePattern
;
2264 EFI_SHELL_FILE_INFO
*ShellInfo
;
2265 EFI_SHELL_FILE_INFO
*ShellInfoNode
;
2266 EFI_SHELL_FILE_INFO
*NewShellNode
;
2267 EFI_FILE_INFO
*FileInfo
;
2269 CHAR16
*NewFullName
;
2272 if ( FilePattern
== NULL
2273 || UnicodeCollation
== NULL
2276 return (EFI_INVALID_PARAMETER
);
2279 CurrentFilePattern
= NULL
;
2281 if (*FilePattern
== L
'\\') {
2285 for( NextFilePatternStart
= FilePattern
2286 ; *NextFilePatternStart
!= CHAR_NULL
&& *NextFilePatternStart
!= L
'\\'
2287 ; NextFilePatternStart
++);
2289 CurrentFilePattern
= AllocateZeroPool((NextFilePatternStart
-FilePattern
+1)*sizeof(CHAR16
));
2290 if (CurrentFilePattern
== NULL
) {
2291 return EFI_OUT_OF_RESOURCES
;
2294 StrnCpyS(CurrentFilePattern
, NextFilePatternStart
-FilePattern
+1, FilePattern
, NextFilePatternStart
-FilePattern
);
2296 if (CurrentFilePattern
[0] == CHAR_NULL
2297 &&NextFilePatternStart
[0] == CHAR_NULL
2300 // we want the parent or root node (if no parent)
2302 if (ParentNode
== NULL
) {
2304 // We want the root node. create the node.
2306 FileInfo
= FileHandleGetInfo(FileHandle
);
2307 NewShellNode
= CreateAndPopulateShellFileInfo(
2314 SHELL_FREE_NON_NULL(FileInfo
);
2317 // Add the current parameter FileHandle to the list, then end...
2319 NewShellNode
= InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO
*)ParentNode
, TRUE
);
2321 if (NewShellNode
== NULL
) {
2322 Status
= EFI_OUT_OF_RESOURCES
;
2324 NewShellNode
->Handle
= NULL
;
2325 if (*FileList
== NULL
) {
2326 *FileList
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2327 InitializeListHead(&((*FileList
)->Link
));
2331 // Add to the returning to use list
2333 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2335 Status
= EFI_SUCCESS
;
2338 Status
= EfiShellFindFilesInDir(FileHandle
, &ShellInfo
);
2340 if (!EFI_ERROR(Status
)){
2341 if (StrStr(NextFilePatternStart
, L
"\\") != NULL
){
2346 for ( ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ShellInfo
->Link
)
2347 ; !IsNull (&ShellInfo
->Link
, &ShellInfoNode
->Link
)
2348 ; ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ShellInfo
->Link
, &ShellInfoNode
->Link
)
2350 if (UnicodeCollation
->MetaiMatch(UnicodeCollation
, (CHAR16
*)ShellInfoNode
->FileName
, CurrentFilePattern
)){
2351 if (ShellInfoNode
->FullName
!= NULL
&& StrStr(ShellInfoNode
->FullName
, L
":") == NULL
) {
2352 Size
= StrSize (ShellInfoNode
->FullName
) + StrSize (MapName
);
2353 NewFullName
= AllocateZeroPool(Size
);
2354 if (NewFullName
== NULL
) {
2355 Status
= EFI_OUT_OF_RESOURCES
;
2357 StrCpyS(NewFullName
, Size
/ sizeof(CHAR16
), MapName
);
2358 StrCatS(NewFullName
, Size
/ sizeof(CHAR16
), ShellInfoNode
->FullName
);
2359 FreePool ((VOID
*) ShellInfoNode
->FullName
);
2360 ShellInfoNode
->FullName
= NewFullName
;
2363 if (Directory
&& !EFI_ERROR(Status
) && ShellInfoNode
->FullName
!= NULL
&& ShellInfoNode
->FileName
!= NULL
){
2365 // should be a directory
2369 // don't open the . and .. directories
2371 if ( (StrCmp(ShellInfoNode
->FileName
, L
".") != 0)
2372 && (StrCmp(ShellInfoNode
->FileName
, L
"..") != 0)
2377 if (EFI_ERROR(Status
)) {
2381 // Open the directory since we need that handle in the next recursion.
2383 ShellInfoNode
->Status
= EfiShellOpenFileByName (ShellInfoNode
->FullName
, &ShellInfoNode
->Handle
, EFI_FILE_MODE_READ
);
2386 // recurse with the next part of the pattern
2388 Status
= ShellSearchHandle(NextFilePatternStart
, UnicodeCollation
, ShellInfoNode
->Handle
, FileList
, ShellInfoNode
, MapName
);
2389 EfiShellClose(ShellInfoNode
->Handle
);
2390 ShellInfoNode
->Handle
= NULL
;
2392 } else if (!EFI_ERROR(Status
)) {
2398 // copy the information we need into a new Node
2400 NewShellNode
= InternalDuplicateShellFileInfo(ShellInfoNode
, FALSE
);
2401 if (NewShellNode
== NULL
) {
2402 Status
= EFI_OUT_OF_RESOURCES
;
2404 if (*FileList
== NULL
) {
2405 *FileList
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2406 InitializeListHead(&((*FileList
)->Link
));
2410 // Add to the returning to use list
2412 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2415 if (EFI_ERROR(Status
)) {
2419 if (EFI_ERROR(Status
)) {
2420 EfiShellFreeFileList(&ShellInfo
);
2422 Status
= EfiShellFreeFileList(&ShellInfo
);
2427 if (*FileList
== NULL
|| (*FileList
!= NULL
&& IsListEmpty(&(*FileList
)->Link
))) {
2428 Status
= EFI_NOT_FOUND
;
2431 FreePool(CurrentFilePattern
);
2436 Find files that match a specified pattern.
2438 This function searches for all files and directories that match the specified
2439 FilePattern. The FilePattern can contain wild-card characters. The resulting file
2440 information is placed in the file list FileList.
2442 Wildcards are processed
2443 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.
2445 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo
2446 field is set to NULL.
2448 if *FileList is not NULL then it must be a pre-existing and properly initialized list.
2450 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.
2451 @param FileList On return, points to the start of a file list containing the names
2452 of all matching files or else points to NULL if no matching files
2453 were found. only on a EFI_SUCCESS return will; this be non-NULL.
2455 @retval EFI_SUCCESS Files found. FileList is a valid list.
2456 @retval EFI_NOT_FOUND No files found.
2457 @retval EFI_NO_MEDIA The device has no media
2458 @retval EFI_DEVICE_ERROR The device reported an error
2459 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted
2464 IN CONST CHAR16
*FilePattern
,
2465 OUT EFI_SHELL_FILE_INFO
**FileList
2469 CHAR16
*PatternCopy
;
2470 CHAR16
*PatternCurrentLocation
;
2471 EFI_DEVICE_PATH_PROTOCOL
*RootDevicePath
;
2472 SHELL_FILE_HANDLE RootFileHandle
;
2476 if ( FilePattern
== NULL
2478 || StrStr(FilePattern
, L
":") == NULL
2480 return (EFI_INVALID_PARAMETER
);
2482 Status
= EFI_SUCCESS
;
2483 RootDevicePath
= NULL
;
2484 RootFileHandle
= NULL
;
2486 PatternCopy
= AllocateCopyPool(StrSize(FilePattern
), FilePattern
);
2487 if (PatternCopy
== NULL
) {
2488 return (EFI_OUT_OF_RESOURCES
);
2491 PatternCopy
= PathCleanUpDirectories(PatternCopy
);
2493 Count
= StrStr(PatternCopy
, L
":") - PatternCopy
+ 1;
2494 ASSERT (Count
<= StrLen (PatternCopy
));
2496 ASSERT(MapName
== NULL
);
2497 MapName
= StrnCatGrow(&MapName
, NULL
, PatternCopy
, Count
);
2498 if (MapName
== NULL
) {
2499 Status
= EFI_OUT_OF_RESOURCES
;
2501 RootDevicePath
= EfiShellGetDevicePathFromFilePath(PatternCopy
);
2502 if (RootDevicePath
== NULL
) {
2503 Status
= EFI_INVALID_PARAMETER
;
2505 Status
= EfiShellOpenRoot(RootDevicePath
, &RootFileHandle
);
2506 if (!EFI_ERROR(Status
)) {
2507 for ( PatternCurrentLocation
= PatternCopy
2508 ; *PatternCurrentLocation
!= ':'
2509 ; PatternCurrentLocation
++);
2510 PatternCurrentLocation
++;
2511 Status
= ShellSearchHandle(PatternCurrentLocation
, gUnicodeCollation
, RootFileHandle
, FileList
, NULL
, MapName
);
2512 EfiShellClose(RootFileHandle
);
2514 FreePool(RootDevicePath
);
2518 SHELL_FREE_NON_NULL(PatternCopy
);
2519 SHELL_FREE_NON_NULL(MapName
);
2525 Opens the files that match the path specified.
2527 This function opens all of the files specified by Path. Wildcards are processed
2528 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each
2529 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.
2531 @param Path A pointer to the path string.
2532 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or
2533 EFI_FILE_MODE_WRITE.
2534 @param FileList Points to the start of a list of files opened.
2536 @retval EFI_SUCCESS Create the file list successfully.
2537 @return Others Can't create the file list.
2541 EfiShellOpenFileList(
2544 IN OUT EFI_SHELL_FILE_INFO
**FileList
2548 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2551 CONST CHAR16
*CurDir
;
2554 PathCleanUpDirectories(Path
);
2559 if (FileList
== NULL
|| *FileList
== NULL
) {
2560 return (EFI_INVALID_PARAMETER
);
2563 if (*Path
== L
'.' && *(Path
+1) == L
'\\') {
2568 // convert a local path to an absolute path
2570 if (StrStr(Path
, L
":") == NULL
) {
2571 CurDir
= EfiShellGetCurDir(NULL
);
2572 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2573 StrnCatGrow(&Path2
, &Path2Size
, CurDir
, 0);
2574 StrnCatGrow(&Path2
, &Path2Size
, L
"\\", 0);
2575 if (*Path
== L
'\\') {
2577 while (PathRemoveLastItem(Path2
)) ;
2579 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2580 StrnCatGrow(&Path2
, &Path2Size
, Path
, 0);
2582 ASSERT(Path2
== NULL
);
2583 StrnCatGrow(&Path2
, NULL
, Path
, 0);
2586 PathCleanUpDirectories (Path2
);
2591 Status
= EfiShellFindFiles(Path2
, FileList
);
2595 if (EFI_ERROR(Status
)) {
2601 // We had no errors so open all the files (that are not already opened...)
2603 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
2604 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2605 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2607 if (ShellFileListItem
->Status
== 0 && ShellFileListItem
->Handle
== NULL
) {
2608 ShellFileListItem
->Status
= EfiShellOpenFileByName (ShellFileListItem
->FullName
, &ShellFileListItem
->Handle
, OpenMode
);
2614 return (EFI_NOT_FOUND
);
2616 return(EFI_SUCCESS
);
2620 Gets the environment variable and Attributes, or list of environment variables. Can be
2621 used instead of GetEnv().
2623 This function returns the current value of the specified environment variable and
2624 the Attributes. If no variable name was specified, then all of the known
2625 variables will be returned.
2627 @param[in] Name A pointer to the environment variable name. If Name is NULL,
2628 then the function will return all of the defined shell
2629 environment variables. In the case where multiple environment
2630 variables are being returned, each variable will be terminated
2631 by a NULL, and the list will be terminated by a double NULL.
2632 @param[out] Attributes If not NULL, a pointer to the returned attributes bitmask for
2633 the environment variable. In the case where Name is NULL, and
2634 multiple environment variables are being returned, Attributes
2637 @retval NULL The environment variable doesn't exist.
2638 @return A non-NULL value points to the variable's value. The returned
2639 pointer does not need to be freed by the caller.
2644 IN CONST CHAR16
*Name
,
2645 OUT UINT32
*Attributes OPTIONAL
2652 CHAR16
*CurrentWriteLocation
;
2660 // Build the semi-colon delimited list. (2 passes)
2662 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
2663 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
2664 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
2666 ASSERT(Node
->Key
!= NULL
);
2667 Size
+= StrSize(Node
->Key
);
2670 Size
+= 2*sizeof(CHAR16
);
2672 Buffer
= AllocateZeroPool(Size
);
2673 if (Buffer
== NULL
) {
2676 CurrentWriteLocation
= (CHAR16
*)Buffer
;
2678 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
2679 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
2680 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
2682 ASSERT(Node
->Key
!= NULL
);
2683 StrCpyS( CurrentWriteLocation
,
2684 (Size
)/sizeof(CHAR16
) - (CurrentWriteLocation
- ((CHAR16
*)Buffer
)),
2687 CurrentWriteLocation
+= StrLen(CurrentWriteLocation
) + 1;
2692 // We are doing a specific environment variable
2694 Status
= ShellFindEnvVarInList(Name
, (CHAR16
**)&Buffer
, &Size
, Attributes
);
2696 if (EFI_ERROR(Status
)){
2698 // get the size we need for this EnvVariable
2700 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name
, Attributes
, &Size
, Buffer
);
2701 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2703 // Allocate the space and recall the get function
2705 Buffer
= AllocateZeroPool(Size
);
2706 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name
, Attributes
, &Size
, Buffer
);
2709 // we didnt get it (might not exist)
2710 // free the memory if we allocated any and return NULL
2712 if (EFI_ERROR(Status
)) {
2713 if (Buffer
!= NULL
) {
2719 // If we did not find the environment variable in the gShellEnvVarList
2720 // but get it from UEFI variable storage successfully then we need update
2721 // the gShellEnvVarList.
2723 ShellFreeEnvVarList ();
2724 Status
= ShellInitEnvVarList ();
2725 ASSERT (Status
== EFI_SUCCESS
);
2731 // return the buffer
2733 return (AddBufferToFreeList(Buffer
));
2737 Gets either a single or list of environment variables.
2739 If name is not NULL then this function returns the current value of the specified
2740 environment variable.
2742 If Name is NULL, then a list of all environment variable names is returned. Each is a
2743 NULL terminated string with a double NULL terminating the list.
2745 @param Name A pointer to the environment variable name. If
2746 Name is NULL, then the function will return all
2747 of the defined shell environment variables. In
2748 the case where multiple environment variables are
2749 being returned, each variable will be terminated by
2750 a NULL, and the list will be terminated by a double
2753 @retval !=NULL A pointer to the returned string.
2754 The returned pointer does not need to be freed by the caller.
2756 @retval NULL The environment variable doesn't exist or there are
2757 no environment variables.
2762 IN CONST CHAR16
*Name
2765 return (EfiShellGetEnvEx(Name
, NULL
));
2769 Internal variable setting function. Allows for setting of the read only variables.
2771 @param Name Points to the NULL-terminated environment variable name.
2772 @param Value Points to the NULL-terminated environment variable value. If the value is an
2773 empty string then the environment variable is deleted.
2774 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2776 @retval EFI_SUCCESS The environment variable was successfully updated.
2779 InternalEfiShellSetEnv(
2780 IN CONST CHAR16
*Name
,
2781 IN CONST CHAR16
*Value
,
2787 if (Value
== NULL
|| StrLen(Value
) == 0) {
2788 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2789 if (!EFI_ERROR(Status
)) {
2790 ShellRemvoeEnvVarFromList(Name
);
2793 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2794 Status
= ShellAddEnvVarToList(
2795 Name
, Value
, StrSize(Value
),
2796 EFI_VARIABLE_BOOTSERVICE_ACCESS
| (Volatile
? 0 : EFI_VARIABLE_NON_VOLATILE
)
2798 if (!EFI_ERROR (Status
)) {
2800 ? SHELL_SET_ENVIRONMENT_VARIABLE_V (Name
, StrSize (Value
) - sizeof (CHAR16
), Value
)
2801 : SHELL_SET_ENVIRONMENT_VARIABLE_NV (Name
, StrSize (Value
) - sizeof (CHAR16
), Value
);
2802 if (EFI_ERROR (Status
)) {
2803 ShellRemvoeEnvVarFromList(Name
);
2811 Sets the environment variable.
2813 This function changes the current value of the specified environment variable. If the
2814 environment variable exists and the Value is an empty string, then the environment
2815 variable is deleted. If the environment variable exists and the Value is not an empty
2816 string, then the value of the environment variable is changed. If the environment
2817 variable does not exist and the Value is an empty string, there is no action. If the
2818 environment variable does not exist and the Value is a non-empty string, then the
2819 environment variable is created and assigned the specified value.
2821 For a description of volatile and non-volatile environment variables, see UEFI Shell
2822 2.0 specification section 3.6.1.
2824 @param Name Points to the NULL-terminated environment variable name.
2825 @param Value Points to the NULL-terminated environment variable value. If the value is an
2826 empty string then the environment variable is deleted.
2827 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2829 @retval EFI_SUCCESS The environment variable was successfully updated.
2834 IN CONST CHAR16
*Name
,
2835 IN CONST CHAR16
*Value
,
2839 if (Name
== NULL
|| *Name
== CHAR_NULL
) {
2840 return (EFI_INVALID_PARAMETER
);
2843 // Make sure we dont 'set' a predefined read only variable
2845 if ((StrCmp (Name
, L
"cwd") == 0) ||
2846 (StrCmp (Name
, L
"lasterror") == 0) ||
2847 (StrCmp (Name
, L
"profiles") == 0) ||
2848 (StrCmp (Name
, L
"uefishellsupport") == 0) ||
2849 (StrCmp (Name
, L
"uefishellversion") == 0) ||
2850 (StrCmp (Name
, L
"uefiversion") == 0) ||
2851 (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
&&
2852 StrCmp (Name
, mNoNestingEnvVarName
) == 0)
2854 return (EFI_INVALID_PARAMETER
);
2856 return (InternalEfiShellSetEnv(Name
, Value
, Volatile
));
2860 Returns the current directory on the specified device.
2862 If FileSystemMapping is NULL, it returns the current working directory. If the
2863 FileSystemMapping is not NULL, it returns the current directory associated with the
2864 FileSystemMapping. In both cases, the returned name includes the file system
2865 mapping (i.e. fs0:\current-dir).
2867 Note that the current directory string should exclude the tailing backslash character.
2869 @param FileSystemMapping A pointer to the file system mapping. If NULL,
2870 then the current working directory is returned.
2872 @retval !=NULL The current directory.
2873 @retval NULL Current directory does not exist.
2878 IN CONST CHAR16
*FileSystemMapping OPTIONAL
2881 CHAR16
*PathToReturn
;
2883 SHELL_MAP_LIST
*MapListItem
;
2884 if (!IsListEmpty(&gShellMapList
.Link
)) {
2886 // if parameter is NULL, use current
2888 if (FileSystemMapping
== NULL
) {
2889 return (EfiShellGetEnv(L
"cwd"));
2892 PathToReturn
= NULL
;
2893 MapListItem
= ShellCommandFindMapItem(FileSystemMapping
);
2894 if (MapListItem
!= NULL
) {
2895 ASSERT((PathToReturn
== NULL
&& Size
== 0) || (PathToReturn
!= NULL
));
2896 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->MapName
, 0);
2897 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
2900 return (AddBufferToFreeList(PathToReturn
));
2907 Changes the current directory on the specified device.
2909 If the FileSystem is NULL, and the directory Dir does not contain a file system's
2910 mapped name, this function changes the current working directory.
2912 If the FileSystem is NULL and the directory Dir contains a mapped name, then the
2913 current file system and the current directory on that file system are changed.
2915 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file
2918 If FileSystem is not NULL and Dir is not NULL, then this function changes the current
2919 directory on the specified file system.
2921 If the current working directory or the current working file system is changed then the
2922 %cwd% environment variable will be updated
2924 Note that the current directory string should exclude the tailing backslash character.
2926 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working
2927 directory is changed.
2928 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.
2930 @retval EFI_SUCCESS The operation was sucessful
2931 @retval EFI_NOT_FOUND The file system could not be found
2936 IN CONST CHAR16
*FileSystem OPTIONAL
,
2937 IN CONST CHAR16
*Dir
2941 SHELL_MAP_LIST
*MapListItem
;
2945 CHAR16
*DirectoryName
;
2952 DirectoryName
= NULL
;
2954 if ((FileSystem
== NULL
&& Dir
== NULL
) || Dir
== NULL
) {
2955 return (EFI_INVALID_PARAMETER
);
2958 if (IsListEmpty(&gShellMapList
.Link
)){
2959 return (EFI_NOT_FOUND
);
2962 DirectoryName
= StrnCatGrow(&DirectoryName
, NULL
, Dir
, 0);
2963 ASSERT(DirectoryName
!= NULL
);
2965 PathCleanUpDirectories(DirectoryName
);
2967 if (FileSystem
== NULL
) {
2969 // determine the file system mapping to use
2971 if (StrStr(DirectoryName
, L
":") != NULL
) {
2972 ASSERT(MapName
== NULL
);
2973 MapName
= StrnCatGrow(&MapName
, NULL
, DirectoryName
, (StrStr(DirectoryName
, L
":")-DirectoryName
+1));
2976 // find the file system mapping's entry in the list
2979 if (MapName
!= NULL
) {
2980 MapListItem
= ShellCommandFindMapItem(MapName
);
2983 // make that the current file system mapping
2985 if (MapListItem
!= NULL
) {
2986 gShellCurMapping
= MapListItem
;
2989 MapListItem
= gShellCurMapping
;
2992 if (MapListItem
== NULL
) {
2993 FreePool (DirectoryName
);
2994 SHELL_FREE_NON_NULL(MapName
);
2995 return (EFI_NOT_FOUND
);
2999 // now update the MapListItem's current directory
3001 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& DirectoryName
[StrLen(DirectoryName
) - 1] != L
':') {
3002 FreePool(MapListItem
->CurrentDirectoryPath
);
3003 MapListItem
->CurrentDirectoryPath
= NULL
;
3005 if (MapName
!= NULL
) {
3006 TempLen
= StrLen(MapName
);
3007 if (TempLen
!= StrLen(DirectoryName
)) {
3008 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3009 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
+StrLen(MapName
), 0);
3013 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3014 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
3016 if ((MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] == L
'\\') || (MapListItem
->CurrentDirectoryPath
== NULL
)) {
3017 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3018 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
3019 MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] = CHAR_NULL
;
3024 // cant have a mapping in the directory...
3026 if (StrStr(DirectoryName
, L
":") != NULL
) {
3027 FreePool (DirectoryName
);
3028 return (EFI_INVALID_PARAMETER
);
3031 // FileSystem != NULL
3033 MapListItem
= ShellCommandFindMapItem(FileSystem
);
3034 if (MapListItem
== NULL
) {
3035 FreePool (DirectoryName
);
3036 return (EFI_INVALID_PARAMETER
);
3038 // gShellCurMapping = MapListItem;
3039 if (DirectoryName
!= NULL
) {
3041 // change current dir on that file system
3044 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
3045 FreePool(MapListItem
->CurrentDirectoryPath
);
3046 DEBUG_CODE(MapListItem
->CurrentDirectoryPath
= NULL
;);
3048 // ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
3049 // MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);
3050 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3051 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
3052 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3053 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
3054 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] == L
'\\') {
3055 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3056 MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] = CHAR_NULL
;
3060 FreePool (DirectoryName
);
3062 // if updated the current directory then update the environment variable
3064 if (MapListItem
== gShellCurMapping
) {
3066 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
3067 StrnCatGrow(&TempString
, &Size
, MapListItem
->MapName
, 0);
3068 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
3069 StrnCatGrow(&TempString
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
3070 Status
= InternalEfiShellSetEnv(L
"cwd", TempString
, TRUE
);
3071 FreePool(TempString
);
3074 return(EFI_SUCCESS
);
3078 Return help information about a specific command.
3080 This function returns the help information for the specified command. The help text
3081 can be internal to the shell or can be from a UEFI Shell manual page.
3083 If Sections is specified, then each section name listed will be compared in a casesensitive
3084 manner, to the section names described in Appendix B. If the section exists,
3085 it will be appended to the returned help text. If the section does not exist, no
3086 information will be returned. If Sections is NULL, then all help text information
3087 available will be returned.
3089 @param Command Points to the NULL-terminated UEFI Shell command name.
3090 @param Sections Points to the NULL-terminated comma-delimited
3091 section names to return. If NULL, then all
3092 sections will be returned.
3093 @param HelpText On return, points to a callee-allocated buffer
3094 containing all specified help text.
3096 @retval EFI_SUCCESS The help text was returned.
3097 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
3099 @retval EFI_INVALID_PARAMETER HelpText is NULL
3100 @retval EFI_NOT_FOUND There is no help text available for Command.
3104 EfiShellGetHelpText(
3105 IN CONST CHAR16
*Command
,
3106 IN CONST CHAR16
*Sections OPTIONAL
,
3107 OUT CHAR16
**HelpText
3110 CONST CHAR16
*ManFileName
;
3114 ASSERT(HelpText
!= NULL
);
3117 ManFileName
= ShellCommandGetManFileNameHandler(Command
);
3119 if (ManFileName
!= NULL
) {
3120 return (ProcessManFile(ManFileName
, Command
, Sections
, NULL
, HelpText
));
3122 if ((StrLen(Command
)> 4)
3123 && (Command
[StrLen(Command
)-1] == L
'i' || Command
[StrLen(Command
)-1] == L
'I')
3124 && (Command
[StrLen(Command
)-2] == L
'f' || Command
[StrLen(Command
)-2] == L
'F')
3125 && (Command
[StrLen(Command
)-3] == L
'e' || Command
[StrLen(Command
)-3] == L
'E')
3126 && (Command
[StrLen(Command
)-4] == L
'.')
3128 FixCommand
= AllocateZeroPool(StrSize(Command
) - 4 * sizeof (CHAR16
));
3129 if (FixCommand
== NULL
) {
3130 return EFI_OUT_OF_RESOURCES
;
3133 StrnCpyS( FixCommand
,
3134 (StrSize(Command
) - 4 * sizeof (CHAR16
))/sizeof(CHAR16
),
3138 Status
= ProcessManFile(FixCommand
, FixCommand
, Sections
, NULL
, HelpText
);
3139 FreePool(FixCommand
);
3142 return (ProcessManFile(Command
, Command
, Sections
, NULL
, HelpText
));
3148 Gets the enable status of the page break output mode.
3150 User can use this function to determine current page break mode.
3152 @retval TRUE The page break output mode is enabled.
3153 @retval FALSE The page break output mode is disabled.
3157 EfiShellGetPageBreak(
3161 return(ShellInfoObject
.PageBreakEnabled
);
3165 Judges whether the active shell is the root shell.
3167 This function makes the user to know that whether the active Shell is the root shell.
3169 @retval TRUE The active Shell is the root Shell.
3170 @retval FALSE The active Shell is NOT the root Shell.
3174 EfiShellIsRootShell(
3178 return(ShellInfoObject
.RootShellInstance
);
3182 function to return a semi-colon delimeted list of all alias' in the current shell
3184 up to caller to free the memory.
3186 @retval NULL No alias' were found
3187 @retval NULL An error ocurred getting alias'
3188 @return !NULL a list of all alias'
3191 InternalEfiShellGetListAlias(
3198 CHAR16
*VariableName
;
3200 UINTN NameBufferSize
;
3204 NameBufferSize
= INIT_NAME_BUFFER_SIZE
;
3205 VariableName
= AllocateZeroPool(NameBufferSize
);
3209 if (VariableName
== NULL
) {
3213 VariableName
[0] = CHAR_NULL
;
3216 NameSize
= NameBufferSize
;
3217 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
3218 if (Status
== EFI_NOT_FOUND
){
3220 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
3221 NameBufferSize
= NameSize
> NameBufferSize
* 2 ? NameSize
: NameBufferSize
* 2;
3222 SHELL_FREE_NON_NULL(VariableName
);
3223 VariableName
= AllocateZeroPool(NameBufferSize
);
3224 if (VariableName
== NULL
) {
3225 Status
= EFI_OUT_OF_RESOURCES
;
3226 SHELL_FREE_NON_NULL(RetVal
);
3231 NameSize
= NameBufferSize
;
3232 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
3235 if (EFI_ERROR (Status
)) {
3236 SHELL_FREE_NON_NULL(RetVal
);
3241 if (CompareGuid(&Guid
, &gShellAliasGuid
)){
3242 ASSERT((RetVal
== NULL
&& RetSize
== 0) || (RetVal
!= NULL
));
3243 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, VariableName
, 0);
3244 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, L
";", 0);
3247 SHELL_FREE_NON_NULL(VariableName
);
3253 Convert a null-terminated unicode string, in-place, to all lowercase.
3256 @param Str The null-terminated string to be converted to all lowercase.
3258 @return The null-terminated string converted into all lowercase.
3267 for (Index
= 0; Str
[Index
] != L
'\0'; Index
++) {
3268 if (Str
[Index
] >= L
'A' && Str
[Index
] <= L
'Z') {
3269 Str
[Index
] -= (CHAR16
)(L
'A' - L
'a');
3276 This function returns the command associated with a alias or a list of all
3279 @param[in] Alias Points to the NULL-terminated shell alias.
3280 If this parameter is NULL, then all
3281 aliases will be returned in ReturnedData.
3282 @param[out] Volatile upon return of a single command if TRUE indicates
3283 this is stored in a volatile fashion. FALSE otherwise.
3285 @return If Alias is not NULL, it will return a pointer to
3286 the NULL-terminated command for that alias.
3287 If Alias is NULL, ReturnedData points to a ';'
3288 delimited list of alias (e.g.
3289 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.
3290 @retval NULL an error ocurred
3291 @retval NULL Alias was not a valid Alias
3296 IN CONST CHAR16
*Alias
,
3297 OUT BOOLEAN
*Volatile OPTIONAL
3307 // Convert to lowercase to make aliases case-insensitive
3308 if (Alias
!= NULL
) {
3309 AliasLower
= AllocateCopyPool (StrSize (Alias
), Alias
);
3310 if (AliasLower
== NULL
) {
3313 ToLower (AliasLower
);
3315 if (Volatile
== NULL
) {
3316 GetVariable2 (AliasLower
, &gShellAliasGuid
, (VOID
**)&AliasVal
, NULL
);
3317 FreePool(AliasLower
);
3318 return (AddBufferToFreeList(AliasVal
));
3322 Status
= gRT
->GetVariable(AliasLower
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
3323 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3324 RetVal
= AllocateZeroPool(RetSize
);
3325 Status
= gRT
->GetVariable(AliasLower
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
3327 if (EFI_ERROR(Status
)) {
3328 if (RetVal
!= NULL
) {
3331 FreePool(AliasLower
);
3334 if ((EFI_VARIABLE_NON_VOLATILE
& Attribs
) == EFI_VARIABLE_NON_VOLATILE
) {
3340 FreePool (AliasLower
);
3341 return (AddBufferToFreeList(RetVal
));
3343 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));
3347 Changes a shell command alias.
3349 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
3351 this function does not check for built in alias'.
3353 @param[in] Command Points to the NULL-terminated shell command or existing alias.
3354 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
3355 Command refers to an alias, that alias will be deleted.
3356 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
3357 Alias being set will be stored in a non-volatile fashion.
3359 @retval EFI_SUCCESS Alias created or deleted successfully.
3360 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
3364 IN CONST CHAR16
*Command
,
3365 IN CONST CHAR16
*Alias
,
3371 BOOLEAN DeleteAlias
;
3373 DeleteAlias
= FALSE
;
3374 if (Alias
== NULL
) {
3376 // We must be trying to remove one if Alias is NULL
3377 // remove an alias (but passed in COMMAND parameter)
3382 ASSERT (Alias
!= NULL
);
3385 // Convert to lowercase to make aliases case-insensitive
3387 AliasLower
= AllocateCopyPool (StrSize (Alias
), Alias
);
3388 if (AliasLower
== NULL
) {
3389 return EFI_OUT_OF_RESOURCES
;
3391 ToLower (AliasLower
);
3394 Status
= gRT
->SetVariable (AliasLower
, &gShellAliasGuid
, 0, 0, NULL
);
3396 Status
= gRT
->SetVariable (
3397 AliasLower
, &gShellAliasGuid
,
3398 EFI_VARIABLE_BOOTSERVICE_ACCESS
| (Volatile
? 0 : EFI_VARIABLE_NON_VOLATILE
),
3399 StrSize (Command
), (VOID
*) Command
3403 FreePool (AliasLower
);
3409 Changes a shell command alias.
3411 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
3414 @param[in] Command Points to the NULL-terminated shell command or existing alias.
3415 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
3416 Command refers to an alias, that alias will be deleted.
3417 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
3418 FALSE and the alias already exists, then the existing alias is unchanged and
3419 EFI_ACCESS_DENIED is returned.
3420 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
3421 Alias being set will be stored in a non-volatile fashion.
3423 @retval EFI_SUCCESS Alias created or deleted successfully.
3424 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
3425 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
3427 @retval EFI_INVALID_PARAMETER Command is null or the empty string.
3432 IN CONST CHAR16
*Command
,
3433 IN CONST CHAR16
*Alias
,
3438 if (ShellCommandIsOnAliasList(Alias
==NULL
?Command
:Alias
)) {
3440 // cant set over a built in alias
3442 return (EFI_ACCESS_DENIED
);
3443 } else if (Command
== NULL
|| *Command
== CHAR_NULL
|| StrLen(Command
) == 0) {
3445 // Command is null or empty
3447 return (EFI_INVALID_PARAMETER
);
3448 } else if (EfiShellGetAlias(Command
, NULL
) != NULL
&& !Replace
) {
3450 // Alias already exists, Replace not set
3452 return (EFI_ACCESS_DENIED
);
3454 return (InternalSetAlias(Command
, Alias
, Volatile
));
3458 // Pure FILE_HANDLE operations are passed to FileHandleLib
3459 // these functions are indicated by the *
3460 EFI_SHELL_PROTOCOL mShellProtocol
= {
3466 EfiShellGetHelpText
,
3467 EfiShellGetDevicePathFromMap
,
3468 EfiShellGetMapFromDevicePath
,
3469 EfiShellGetDevicePathFromFilePath
,
3470 EfiShellGetFilePathFromDevicePath
,
3474 EfiShellOpenFileList
,
3475 EfiShellFreeFileList
,
3476 EfiShellRemoveDupInFileList
,
3477 EfiShellBatchIsActive
,
3478 EfiShellIsRootShell
,
3479 EfiShellEnablePageBreak
,
3480 EfiShellDisablePageBreak
,
3481 EfiShellGetPageBreak
,
3482 EfiShellGetDeviceName
,
3483 (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
, //*
3484 (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
, //*
3485 EfiShellOpenFileByName
,
3488 (EFI_SHELL_READ_FILE
)FileHandleRead
, //*
3489 (EFI_SHELL_WRITE_FILE
)FileHandleWrite
, //*
3490 (EFI_SHELL_DELETE_FILE
)FileHandleDelete
, //*
3491 EfiShellDeleteFileByName
,
3492 (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
, //*
3493 (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
, //*
3494 (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
, //*
3496 EfiShellFindFilesInDir
,
3497 (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
, //*
3499 EfiShellOpenRootByHandle
,
3501 SHELL_MAJOR_VERSION
,
3502 SHELL_MINOR_VERSION
,
3504 // New for UEFI Shell 2.1
3505 EfiShellRegisterGuidName
,
3506 EfiShellGetGuidName
,
3507 EfiShellGetGuidFromName
,
3512 Function to create and install on the current handle.
3514 Will overwrite any existing ShellProtocols in the system to be sure that
3515 the current shell is in control.
3517 This must be removed via calling CleanUpShellProtocol().
3519 @param[in, out] NewShell The pointer to the pointer to the structure
3522 @retval EFI_SUCCESS The operation was successful.
3523 @return An error from LocateHandle, CreateEvent, or other core function.
3526 CreatePopulateInstallShellProtocol (
3527 IN OUT EFI_SHELL_PROTOCOL
**NewShell
3533 UINTN HandleCounter
;
3534 SHELL_PROTOCOL_HANDLE_LIST
*OldProtocolNode
;
3535 EFI_SHELL_PROTOCOL
*OldShell
;
3537 if (NewShell
== NULL
) {
3538 return (EFI_INVALID_PARAMETER
);
3543 OldProtocolNode
= NULL
;
3544 InitializeListHead(&ShellInfoObject
.OldShellList
.Link
);
3547 // Initialize EfiShellProtocol object...
3549 Status
= gBS
->CreateEvent(0,
3553 &mShellProtocol
.ExecutionBreak
);
3554 if (EFI_ERROR(Status
)) {
3559 // Get the size of the buffer we need.
3561 Status
= gBS
->LocateHandle(ByProtocol
,
3562 &gEfiShellProtocolGuid
,
3566 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3568 // Allocate and recall with buffer of correct size
3570 Buffer
= AllocateZeroPool(BufferSize
);
3571 if (Buffer
== NULL
) {
3572 return (EFI_OUT_OF_RESOURCES
);
3574 Status
= gBS
->LocateHandle(ByProtocol
,
3575 &gEfiShellProtocolGuid
,
3579 if (EFI_ERROR(Status
)) {
3584 // now overwrite each of them, but save the info to restore when we end.
3586 for (HandleCounter
= 0 ; HandleCounter
< (BufferSize
/sizeof(EFI_HANDLE
)) ; HandleCounter
++) {
3587 Status
= gBS
->OpenProtocol(Buffer
[HandleCounter
],
3588 &gEfiShellProtocolGuid
,
3589 (VOID
**) &OldShell
,
3592 EFI_OPEN_PROTOCOL_GET_PROTOCOL
3594 if (!EFI_ERROR(Status
)) {
3595 OldProtocolNode
= AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST
));
3596 if (OldProtocolNode
== NULL
) {
3597 if (!IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)) {
3598 CleanUpShellProtocol (&mShellProtocol
);
3600 Status
= EFI_OUT_OF_RESOURCES
;
3604 // reinstall over the old one...
3606 OldProtocolNode
->Handle
= Buffer
[HandleCounter
];
3607 OldProtocolNode
->Interface
= OldShell
;
3608 Status
= gBS
->ReinstallProtocolInterface(
3609 OldProtocolNode
->Handle
,
3610 &gEfiShellProtocolGuid
,
3611 OldProtocolNode
->Interface
,
3612 (VOID
*)(&mShellProtocol
));
3613 if (!EFI_ERROR(Status
)) {
3615 // we reinstalled sucessfully. log this so we can reverse it later.
3619 // add to the list for subsequent...
3621 InsertTailList(&ShellInfoObject
.OldShellList
.Link
, &OldProtocolNode
->Link
);
3626 } else if (Status
== EFI_NOT_FOUND
) {
3627 ASSERT(IsListEmpty(&ShellInfoObject
.OldShellList
.Link
));
3629 // no one else published yet. just publish it ourselves.
3631 Status
= gBS
->InstallProtocolInterface (
3633 &gEfiShellProtocolGuid
,
3634 EFI_NATIVE_INTERFACE
,
3635 (VOID
*)(&mShellProtocol
));
3638 if (PcdGetBool(PcdShellSupportOldProtocols
)){
3639 ///@todo support ShellEnvironment2
3640 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?
3643 if (!EFI_ERROR(Status
)) {
3644 *NewShell
= &mShellProtocol
;
3650 Opposite of CreatePopulateInstallShellProtocol.
3652 Free all memory and restore the system to the state it was in before calling
3653 CreatePopulateInstallShellProtocol.
3655 @param[in, out] NewShell The pointer to the new shell protocol structure.
3657 @retval EFI_SUCCESS The operation was successful.
3660 CleanUpShellProtocol (
3661 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3664 SHELL_PROTOCOL_HANDLE_LIST
*Node2
;
3667 // if we need to restore old protocols...
3669 if (!IsListEmpty(&ShellInfoObject
.OldShellList
.Link
)) {
3670 for (Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*) GetFirstNode (&ShellInfoObject
.OldShellList
.Link
)
3671 ; !IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)
3672 ; Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*) GetFirstNode (&ShellInfoObject
.OldShellList
.Link
)
3674 RemoveEntryList (&Node2
->Link
);
3675 gBS
->ReinstallProtocolInterface (Node2
->Handle
, &gEfiShellProtocolGuid
, NewShell
, Node2
->Interface
);
3680 // no need to restore
3682 gBS
->UninstallProtocolInterface (gImageHandle
, &gEfiShellProtocolGuid
, NewShell
);
3688 Cleanup the shell environment.
3690 @param[in, out] NewShell The pointer to the new shell protocol structure.
3692 @retval EFI_SUCCESS The operation was successful.
3695 CleanUpShellEnvironment (
3696 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3700 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
3702 CleanUpShellProtocol (NewShell
);
3704 Status
= gBS
->CloseEvent(NewShell
->ExecutionBreak
);
3705 NewShell
->ExecutionBreak
= NULL
;
3707 Status
= gBS
->OpenProtocol(
3708 gST
->ConsoleInHandle
,
3709 &gEfiSimpleTextInputExProtocolGuid
,
3713 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
3715 if (!EFI_ERROR (Status
)) {
3716 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle1
);
3717 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle2
);
3718 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle3
);
3719 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle4
);
3720 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle1
);
3721 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle2
);
3722 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle3
);
3723 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle4
);
3729 Notification function for keystrokes.
3731 @param[in] KeyData The key that was pressed.
3733 @retval EFI_SUCCESS The operation was successful.
3737 NotificationFunction(
3738 IN EFI_KEY_DATA
*KeyData
3741 if ( ((KeyData
->Key
.UnicodeChar
== L
'c') &&
3742 (KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
) || KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
))) ||
3743 (KeyData
->Key
.UnicodeChar
== 3)
3745 if (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
== NULL
) {
3746 return (EFI_UNSUPPORTED
);
3748 return (gBS
->SignalEvent(ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
));
3749 } else if ((KeyData
->Key
.UnicodeChar
== L
's') &&
3750 (KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
) || KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
))
3752 ShellInfoObject
.HaltOutput
= TRUE
;
3754 return (EFI_SUCCESS
);
3758 Function to start monitoring for CTRL-C using SimpleTextInputEx. This
3759 feature's enabled state was not known when the shell initially launched.
3761 @retval EFI_SUCCESS The feature is enabled.
3762 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
3765 InernalEfiShellStartMonitor(
3769 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
3770 EFI_KEY_DATA KeyData
;
3773 Status
= gBS
->OpenProtocol(
3774 gST
->ConsoleInHandle
,
3775 &gEfiSimpleTextInputExProtocolGuid
,
3779 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
3780 if (EFI_ERROR(Status
)) {
3785 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
3786 ShellInfoObject
.HiiHandle
);
3787 return (EFI_SUCCESS
);
3790 if (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
== NULL
) {
3791 return (EFI_UNSUPPORTED
);
3794 KeyData
.KeyState
.KeyToggleState
= 0;
3795 KeyData
.Key
.ScanCode
= 0;
3796 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
3797 KeyData
.Key
.UnicodeChar
= L
'c';
3799 Status
= SimpleEx
->RegisterKeyNotify(
3802 NotificationFunction
,
3803 &ShellInfoObject
.CtrlCNotifyHandle1
);
3805 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
3806 if (!EFI_ERROR(Status
)) {
3807 Status
= SimpleEx
->RegisterKeyNotify(
3810 NotificationFunction
,
3811 &ShellInfoObject
.CtrlCNotifyHandle2
);
3813 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
3814 KeyData
.Key
.UnicodeChar
= 3;
3815 if (!EFI_ERROR(Status
)) {
3816 Status
= SimpleEx
->RegisterKeyNotify(
3819 NotificationFunction
,
3820 &ShellInfoObject
.CtrlCNotifyHandle3
);
3822 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
3823 if (!EFI_ERROR(Status
)) {
3824 Status
= SimpleEx
->RegisterKeyNotify(
3827 NotificationFunction
,
3828 &ShellInfoObject
.CtrlCNotifyHandle4
);