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 - 2016, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #define INIT_NAME_BUFFER_SIZE 128
23 Close an open file handle.
25 This function closes a specified file handle. All "dirty" cached file data is
26 flushed to the device, and the file is closed. In all cases the handle is
29 @param[in] FileHandle The file handle to close.
31 @retval EFI_SUCCESS The file handle was closed successfully.
36 IN SHELL_FILE_HANDLE FileHandle
39 ShellFileHandleRemove(FileHandle
);
40 return (FileHandleClose(ConvertShellHandleToEfiFileProtocol(FileHandle
)));
44 Internal worker to determine whether there is a BlockIo somewhere
45 upon the device path specified.
47 @param[in] DevicePath The device path to test.
49 @retval TRUE gEfiBlockIoProtocolGuid was installed on a handle with this device path
50 @retval FALSE gEfiBlockIoProtocolGuid was not found.
54 InternalShellProtocolIsBlockIoPresent(
55 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
58 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
64 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
65 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
, &DevicePathCopy
, &Handle
);
67 if ((Handle
!= NULL
) && (!EFI_ERROR(Status
))) {
74 Internal worker to determine whether there is a file system somewhere
75 upon the device path specified.
77 @param[in] DevicePath The device path to test.
79 @retval TRUE gEfiSimpleFileSystemProtocolGuid was installed on a handle with this device path
80 @retval FALSE gEfiSimpleFileSystemProtocolGuid was not found.
84 InternalShellProtocolIsSimpleFileSystemPresent(
85 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
88 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
94 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
95 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
97 if ((Handle
!= NULL
) && (!EFI_ERROR(Status
))) {
104 Internal worker debug helper function to print out maps as they are added.
106 @param[in] Mapping string mapping that has been added
107 @param[in] DevicePath pointer to device path that has been mapped.
109 @retval EFI_SUCCESS the operation was successful.
110 @return other an error ocurred
117 InternalShellProtocolDebugPrintMessage (
118 IN CONST CHAR16
*Mapping
,
119 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
125 Status
= EFI_SUCCESS
;
128 if (Mapping
!= NULL
) {
129 DEBUG((EFI_D_INFO
, "Added new map item:\"%S\"\r\n", Mapping
));
131 Temp
= ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
132 DEBUG((EFI_D_INFO
, "DevicePath: %S\r\n", Temp
));
140 This function creates a mapping for a device path.
142 If both DeviecPath and Mapping are NULL, this will reset the mapping to default values.
144 @param DevicePath Points to the device path. If this is NULL and Mapping points to a valid mapping,
145 then the mapping will be deleted.
146 @param Mapping Points to the NULL-terminated mapping for the device path. Must end with a ':'
148 @retval EFI_SUCCESS Mapping created or deleted successfully.
149 @retval EFI_NO_MAPPING There is no handle that corresponds exactly to DevicePath. See the
150 boot service function LocateDevicePath().
151 @retval EFI_ACCESS_DENIED The mapping is a built-in alias.
152 @retval EFI_INVALID_PARAMETER Mapping was NULL
153 @retval EFI_INVALID_PARAMETER Mapping did not end with a ':'
154 @retval EFI_INVALID_PARAMETER DevicePath was not pointing at a device that had a SIMPLE_FILE_SYSTEM_PROTOCOL installed.
155 @retval EFI_NOT_FOUND There was no mapping found to delete
156 @retval EFI_OUT_OF_RESOURCES Memory allocation failed
161 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath OPTIONAL
,
162 IN CONST CHAR16
*Mapping
166 SHELL_MAP_LIST
*MapListNode
;
168 if (Mapping
== NULL
){
169 return (EFI_INVALID_PARAMETER
);
172 if (Mapping
[StrLen(Mapping
)-1] != ':') {
173 return (EFI_INVALID_PARAMETER
);
177 // Delete the mapping
179 if (DevicePath
== NULL
) {
180 if (IsListEmpty(&gShellMapList
.Link
)) {
181 return (EFI_NOT_FOUND
);
183 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
184 ; !IsNull(&gShellMapList
.Link
, &MapListNode
->Link
)
185 ; MapListNode
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListNode
->Link
)
187 if (StringNoCaseCompare(&MapListNode
->MapName
, &Mapping
) == 0) {
188 RemoveEntryList(&MapListNode
->Link
);
189 SHELL_FREE_NON_NULL(MapListNode
->DevicePath
);
190 SHELL_FREE_NON_NULL(MapListNode
->MapName
);
191 SHELL_FREE_NON_NULL(MapListNode
->CurrentDirectoryPath
);
192 FreePool(MapListNode
);
193 return (EFI_SUCCESS
);
198 // We didnt find one to delete
200 return (EFI_NOT_FOUND
);
204 // make sure this is a valid to add device path
206 ///@todo add BlockIo to this test...
207 if (!InternalShellProtocolIsSimpleFileSystemPresent(DevicePath
)
208 && !InternalShellProtocolIsBlockIoPresent(DevicePath
)) {
209 return (EFI_INVALID_PARAMETER
);
213 // First make sure there is no old mapping
215 Status
= EfiShellSetMap(NULL
, Mapping
);
216 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_FOUND
)) {
221 // now add the new one.
223 Status
= ShellCommandAddMapItemAndUpdatePath(Mapping
, DevicePath
, 0, FALSE
);
229 Gets the device path from the mapping.
231 This function gets the device path associated with a mapping.
233 @param Mapping A pointer to the mapping
235 @retval !=NULL Pointer to the device path that corresponds to the
236 device mapping. The returned pointer does not need
238 @retval NULL There is no device path associated with the
241 CONST EFI_DEVICE_PATH_PROTOCOL
*
243 EfiShellGetDevicePathFromMap(
244 IN CONST CHAR16
*Mapping
247 SHELL_MAP_LIST
*MapListItem
;
254 StrnCatGrow(&NewName
, &Size
, Mapping
, 0);
255 if (Mapping
[StrLen(Mapping
)-1] != L
':') {
256 StrnCatGrow(&NewName
, &Size
, L
":", 0);
259 MapListItem
= ShellCommandFindMapItem(NewName
);
263 if (MapListItem
!= NULL
) {
264 return (MapListItem
->DevicePath
);
270 Gets the mapping(s) that most closely matches the device path.
272 This function gets the mapping which corresponds to the device path *DevicePath. If
273 there is no exact match, then the mapping which most closely matches *DevicePath
274 is returned, and *DevicePath is updated to point to the remaining portion of the
275 device path. If there is an exact match, the mapping is returned and *DevicePath
276 points to the end-of-device-path node.
278 If there are multiple map names they will be semi-colon seperated in the
279 NULL-terminated string.
281 @param DevicePath On entry, points to a device path pointer. On
282 exit, updates the pointer to point to the
283 portion of the device path after the mapping.
285 @retval NULL No mapping was found.
286 @return !=NULL Pointer to NULL-terminated mapping. The buffer
287 is callee allocated and should be freed by the caller.
291 EfiShellGetMapFromDevicePath(
292 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
295 SHELL_MAP_LIST
*Node
;
296 CHAR16
*PathForReturn
;
298 // EFI_HANDLE PathHandle;
299 // EFI_HANDLE MapHandle;
300 // EFI_STATUS Status;
301 // EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
302 // EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
304 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
308 PathForReturn
= NULL
;
311 for ( Node
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
312 ; !IsNull(&gShellMapList
.Link
, &Node
->Link
)
313 ; Node
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &Node
->Link
)
316 // check for exact match
318 if (DevicePathCompare(DevicePath
, &Node
->DevicePath
) == 0) {
319 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
321 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, L
";", 0);
323 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, Node
->MapName
, 0);
326 if (PathForReturn
!= NULL
) {
327 while (!IsDevicePathEndType (*DevicePath
)) {
328 *DevicePath
= NextDevicePathNode (*DevicePath
);
330 SetDevicePathEndNode (*DevicePath
);
333 ///@todo finish code for inexact matches.
334 if (PathForReturn == NULL) {
337 DevicePathCopy = DuplicateDevicePath(*DevicePath);
338 ASSERT(DevicePathCopy != NULL);
339 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
340 ASSERT_EFI_ERROR(Status);
342 // check each of the device paths we have to get the root of the path for consist mappings
344 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
345 ; !IsNull(&gShellMapList.Link, &Node->Link)
346 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
348 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) == 0) {
351 MapPathCopy = DuplicateDevicePath(Node->DevicePath);
352 ASSERT(MapPathCopy != NULL);
353 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
354 if (MapHandle == PathHandle) {
356 *DevicePath = DevicePathCopy;
359 DevicePathCopy = NULL;
360 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
361 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
366 // now add on the non-consistent mappings
368 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
369 ; !IsNull(&gShellMapList.Link, &Node->Link)
370 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
372 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) != 0) {
375 MapPathCopy = Node->DevicePath;
376 ASSERT(MapPathCopy != NULL);
377 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
378 if (MapHandle == PathHandle) {
379 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
380 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
387 return (AddBufferToFreeList(PathForReturn
));
391 Converts a device path to a file system-style path.
393 This function converts a device path to a file system path by replacing part, or all, of
394 the device path with the file-system mapping. If there are more than one application
395 file system mappings, the one that most closely matches Path will be used.
397 @param Path The pointer to the device path
399 @retval NULL the device path could not be found.
400 @return all The pointer of the NULL-terminated file path. The path
401 is callee-allocated and should be freed by the caller.
405 EfiShellGetFilePathFromDevicePath(
406 IN CONST EFI_DEVICE_PATH_PROTOCOL
*Path
409 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
410 EFI_DEVICE_PATH_PROTOCOL
*MapPathCopy
;
411 SHELL_MAP_LIST
*MapListItem
;
412 CHAR16
*PathForReturn
;
414 EFI_HANDLE PathHandle
;
415 EFI_HANDLE MapHandle
;
417 FILEPATH_DEVICE_PATH
*FilePath
;
418 FILEPATH_DEVICE_PATH
*AlignedNode
;
420 PathForReturn
= NULL
;
423 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)Path
;
424 ASSERT(DevicePathCopy
!= NULL
);
425 if (DevicePathCopy
== NULL
) {
429 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &PathHandle
);
431 if (EFI_ERROR(Status
)) {
435 // check each of the device paths we have to get the root of the path
437 for ( MapListItem
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
438 ; !IsNull(&gShellMapList
.Link
, &MapListItem
->Link
)
439 ; MapListItem
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListItem
->Link
)
441 MapPathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)MapListItem
->DevicePath
;
442 ASSERT(MapPathCopy
!= NULL
);
444 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &MapPathCopy
, &MapHandle
);
445 if (MapHandle
== PathHandle
) {
446 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
447 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, MapListItem
->MapName
, 0);
449 // go through all the remaining nodes in the device path
451 for ( FilePath
= (FILEPATH_DEVICE_PATH
*)DevicePathCopy
452 ; !IsDevicePathEnd (&FilePath
->Header
)
453 ; FilePath
= (FILEPATH_DEVICE_PATH
*)NextDevicePathNode (&FilePath
->Header
)
456 // If any node is not a file path node, then the conversion can not be completed
458 if ((DevicePathType(&FilePath
->Header
) != MEDIA_DEVICE_PATH
) ||
459 (DevicePathSubType(&FilePath
->Header
) != MEDIA_FILEPATH_DP
)) {
460 FreePool(PathForReturn
);
465 // append the path part onto the filepath.
467 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
469 AlignedNode
= AllocateCopyPool (DevicePathNodeLength(FilePath
), FilePath
);
470 if (AlignedNode
== NULL
) {
471 FreePool (PathForReturn
);
475 // File Path Device Path Nodes 'can optionally add a "\" separator to
476 // the beginning and/or the end of the Path Name string.'
477 // (UEFI Spec 2.4 section 9.3.6.4).
478 // If necessary, add a "\", but otherwise don't
479 // (This is specified in the above section, and also implied by the
480 // UEFI Shell spec section 3.7)
481 if ((PathSize
!= 0) &&
482 (PathForReturn
!= NULL
) &&
483 (PathForReturn
[PathSize
- 1] != L
'\\') &&
484 (AlignedNode
->PathName
[0] != L
'\\')) {
485 PathForReturn
= StrnCatGrow (&PathForReturn
, &PathSize
, L
"\\", 1);
488 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, AlignedNode
->PathName
, 0);
489 FreePool(AlignedNode
);
490 } // for loop of remaining nodes
492 if (PathForReturn
!= NULL
) {
495 } // for loop of paths to check
496 return(PathForReturn
);
500 Converts a file system style name to a device path.
502 This function converts a file system style name to a device path, by replacing any
503 mapping references to the associated device path.
505 @param[in] Path The pointer to the path.
507 @return The pointer of the file path. The file path is callee
508 allocated and should be freed by the caller.
509 @retval NULL The path could not be found.
510 @retval NULL There was not enough available memory.
512 EFI_DEVICE_PATH_PROTOCOL
*
514 EfiShellGetDevicePathFromFilePath(
515 IN CONST CHAR16
*Path
522 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
523 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
524 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopyForFree
;
525 EFI_DEVICE_PATH_PROTOCOL
*DevicePathForReturn
;
536 if (StrStr(Path
, L
":") == NULL
) {
537 Cwd
= EfiShellGetCurDir(NULL
);
541 Size
= StrSize(Cwd
) + StrSize(Path
);
542 NewPath
= AllocateZeroPool(Size
);
543 if (NewPath
== NULL
) {
546 StrCpyS(NewPath
, Size
/sizeof(CHAR16
), Cwd
);
547 StrCatS(NewPath
, Size
/sizeof(CHAR16
), L
"\\");
548 if (*Path
== L
'\\') {
550 while (PathRemoveLastItem(NewPath
)) ;
552 StrCatS(NewPath
, Size
/sizeof(CHAR16
), Path
);
553 DevicePathForReturn
= EfiShellGetDevicePathFromFilePath(NewPath
);
555 return (DevicePathForReturn
);
560 // find the part before (but including) the : for the map name
562 ASSERT((MapName
== NULL
&& Size
== 0) || (MapName
!= NULL
));
563 MapName
= StrnCatGrow(&MapName
, &Size
, Path
, (StrStr(Path
, L
":")-Path
+1));
564 if (MapName
== NULL
|| MapName
[StrLen(MapName
)-1] != L
':') {
569 // look up the device path in the map
571 DevicePath
= EfiShellGetDevicePathFromMap(MapName
);
572 if (DevicePath
== NULL
) {
574 // Must have been a bad Mapname
580 // make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
582 DevicePathCopyForFree
= DevicePathCopy
= DuplicateDevicePath(DevicePath
);
583 if (DevicePathCopy
== NULL
) {
592 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
593 if (EFI_ERROR(Status
)) {
594 if (DevicePathCopyForFree
!= NULL
) {
595 FreePool(DevicePathCopyForFree
);
602 // build the full device path
604 if (*(Path
+StrLen(MapName
)+1) == CHAR_NULL
) {
605 DevicePathForReturn
= FileDevicePath(Handle
, L
"\\");
607 DevicePathForReturn
= FileDevicePath(Handle
, Path
+StrLen(MapName
));
611 if (DevicePathCopyForFree
!= NULL
) {
612 FreePool(DevicePathCopyForFree
);
615 return (DevicePathForReturn
);
619 Gets the name of the device specified by the device handle.
621 This function gets the user-readable name of the device specified by the device
622 handle. If no user-readable name could be generated, then *BestDeviceName will be
623 NULL and EFI_NOT_FOUND will be returned.
625 If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
626 device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
629 If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
630 device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
631 If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
632 EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
633 EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
635 @param DeviceHandle The handle of the device.
636 @param Flags Determines the possible sources of component names.
638 EFI_DEVICE_NAME_USE_COMPONENT_NAME
639 EFI_DEVICE_NAME_USE_DEVICE_PATH
640 @param Language A pointer to the language specified for the device
641 name, in the same format as described in the UEFI
642 specification, Appendix M
643 @param BestDeviceName On return, points to the callee-allocated NULL-
644 terminated name of the device. If no device name
645 could be found, points to NULL. The name must be
646 freed by the caller...
648 @retval EFI_SUCCESS Get the name successfully.
649 @retval EFI_NOT_FOUND Fail to get the device name.
650 @retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
651 @retval EFI_INVALID_PARAMETER BestDeviceName was NULL
652 @retval EFI_INVALID_PARAMETER DeviceHandle was NULL
656 EfiShellGetDeviceName(
657 IN EFI_HANDLE DeviceHandle
,
658 IN EFI_SHELL_DEVICE_NAME_FLAGS Flags
,
660 OUT CHAR16
**BestDeviceName
664 EFI_COMPONENT_NAME2_PROTOCOL
*CompName2
;
665 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
666 EFI_HANDLE
*HandleList
;
669 CHAR16
*DeviceNameToReturn
;
671 UINTN ParentControllerCount
;
672 EFI_HANDLE
*ParentControllerBuffer
;
673 UINTN ParentDriverCount
;
674 EFI_HANDLE
*ParentDriverBuffer
;
676 if (BestDeviceName
== NULL
||
679 return (EFI_INVALID_PARAMETER
);
683 // make sure one of the 2 supported bits is on
685 if (((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) == 0) &&
686 ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) == 0)) {
687 return (EFI_INVALID_PARAMETER
);
690 DeviceNameToReturn
= NULL
;
691 *BestDeviceName
= NULL
;
696 if ((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) != 0) {
697 Status
= ParseHandleDatabaseByRelationship(
700 HR_DRIVER_BINDING_HANDLE
|HR_DEVICE_DRIVER
,
703 for (LoopVar
= 0; LoopVar
< HandleCount
; LoopVar
++){
705 // Go through those handles until we get one that passes for GetComponentName
707 Status
= gBS
->OpenProtocol(
709 &gEfiComponentName2ProtocolGuid
,
713 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
714 if (EFI_ERROR(Status
)) {
715 Status
= gBS
->OpenProtocol(
717 &gEfiComponentNameProtocolGuid
,
721 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
724 if (EFI_ERROR(Status
)) {
727 Lang
= GetBestLanguageForDriver(CompName2
->SupportedLanguages
, Language
, FALSE
);
728 Status
= CompName2
->GetControllerName(CompName2
, DeviceHandle
, NULL
, Lang
, &DeviceNameToReturn
);
731 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
735 if (HandleList
!= NULL
) {
736 FreePool(HandleList
);
740 // Now check the parent controller using this as the child.
742 if (DeviceNameToReturn
== NULL
){
743 PARSE_HANDLE_DATABASE_PARENTS(DeviceHandle
, &ParentControllerCount
, &ParentControllerBuffer
);
744 for (LoopVar
= 0 ; LoopVar
< ParentControllerCount
; LoopVar
++) {
745 PARSE_HANDLE_DATABASE_UEFI_DRIVERS(ParentControllerBuffer
[LoopVar
], &ParentDriverCount
, &ParentDriverBuffer
);
746 for (HandleCount
= 0 ; HandleCount
< ParentDriverCount
; HandleCount
++) {
748 // try using that driver's component name with controller and our driver as the child.
750 Status
= gBS
->OpenProtocol(
751 ParentDriverBuffer
[HandleCount
],
752 &gEfiComponentName2ProtocolGuid
,
756 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
757 if (EFI_ERROR(Status
)) {
758 Status
= gBS
->OpenProtocol(
759 ParentDriverBuffer
[HandleCount
],
760 &gEfiComponentNameProtocolGuid
,
764 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
767 if (EFI_ERROR(Status
)) {
770 Lang
= GetBestLanguageForDriver(CompName2
->SupportedLanguages
, Language
, FALSE
);
771 Status
= CompName2
->GetControllerName(CompName2
, ParentControllerBuffer
[LoopVar
], DeviceHandle
, Lang
, &DeviceNameToReturn
);
774 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
781 SHELL_FREE_NON_NULL(ParentDriverBuffer
);
782 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
786 SHELL_FREE_NON_NULL(ParentControllerBuffer
);
789 // dont return on fail since we will try device path if that bit is on
791 if (DeviceNameToReturn
!= NULL
){
792 ASSERT(BestDeviceName
!= NULL
);
793 StrnCatGrow(BestDeviceName
, NULL
, DeviceNameToReturn
, 0);
794 return (EFI_SUCCESS
);
797 if ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) != 0) {
798 Status
= gBS
->OpenProtocol(
800 &gEfiDevicePathProtocolGuid
,
804 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
805 if (!EFI_ERROR(Status
)) {
807 // use device path to text on the device path
809 *BestDeviceName
= ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
810 return (EFI_SUCCESS
);
814 // none of the selected bits worked.
816 return (EFI_NOT_FOUND
);
820 Opens the root directory of a device on a handle
822 This function opens the root directory of a device and returns a file handle to it.
824 @param DeviceHandle The handle of the device that contains the volume.
825 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
828 @retval EFI_SUCCESS Root opened successfully.
829 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
831 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
832 @retval EFI_DEVICE_ERROR The device had an error
836 EfiShellOpenRootByHandle(
837 IN EFI_HANDLE DeviceHandle
,
838 OUT SHELL_FILE_HANDLE
*FileHandle
842 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*SimpleFileSystem
;
843 EFI_FILE_PROTOCOL
*RealFileHandle
;
844 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
847 // get the simple file system interface
849 Status
= gBS
->OpenProtocol(DeviceHandle
,
850 &gEfiSimpleFileSystemProtocolGuid
,
851 (VOID
**)&SimpleFileSystem
,
854 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
855 if (EFI_ERROR(Status
)) {
856 return (EFI_NOT_FOUND
);
859 Status
= gBS
->OpenProtocol(DeviceHandle
,
860 &gEfiDevicePathProtocolGuid
,
864 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
865 if (EFI_ERROR(Status
)) {
866 return (EFI_NOT_FOUND
);
869 // Open the root volume now...
871 Status
= SimpleFileSystem
->OpenVolume(SimpleFileSystem
, &RealFileHandle
);
872 *FileHandle
= ConvertEfiFileProtocolToShellHandle(RealFileHandle
, EfiShellGetMapFromDevicePath(&DevPath
));
877 Opens the root directory of a device.
879 This function opens the root directory of a device and returns a file handle to it.
881 @param DevicePath Points to the device path corresponding to the device where the
882 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
883 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
886 @retval EFI_SUCCESS Root opened successfully.
887 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
889 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
890 @retval EFI_DEVICE_ERROR The device had an error
891 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
896 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
897 OUT SHELL_FILE_HANDLE
*FileHandle
903 if (FileHandle
== NULL
) {
904 return (EFI_INVALID_PARAMETER
);
908 // find the handle of the device with that device handle and the file system
911 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
914 if (EFI_ERROR(Status
)) {
915 return (EFI_NOT_FOUND
);
918 return (EfiShellOpenRootByHandle(Handle
, FileHandle
));
922 Returns whether any script files are currently being processed.
924 @retval TRUE There is at least one script file active.
925 @retval FALSE No script files are active now.
930 EfiShellBatchIsActive (
934 if (ShellCommandGetCurrentScriptFile() == NULL
) {
941 Worker function to open a file based on a device path. this will open the root
942 of the volume and then traverse down to the file itself.
944 @param DevicePath Device Path of the file.
945 @param FileHandle Pointer to the file upon a successful return.
946 @param OpenMode mode to open file in.
947 @param Attributes the File Attributes to use when creating a new file.
949 @retval EFI_SUCCESS the file is open and FileHandle is valid
950 @retval EFI_UNSUPPORTED the device path cotained non-path elements
951 @retval other an error ocurred.
955 InternalOpenFileDevicePath(
956 IN OUT EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
957 OUT SHELL_FILE_HANDLE
*FileHandle
,
959 IN UINT64 Attributes OPTIONAL
963 FILEPATH_DEVICE_PATH
*FilePathNode
;
965 SHELL_FILE_HANDLE ShellHandle
;
966 EFI_FILE_PROTOCOL
*Handle1
;
967 EFI_FILE_PROTOCOL
*Handle2
;
968 FILEPATH_DEVICE_PATH
*AlignedNode
;
970 if (FileHandle
== NULL
) {
971 return (EFI_INVALID_PARAMETER
);
981 Status
= EfiShellOpenRoot(DevicePath
, &ShellHandle
);
983 if (!EFI_ERROR(Status
)) {
984 Handle1
= ConvertShellHandleToEfiFileProtocol(ShellHandle
);
985 if (Handle1
!= NULL
) {
987 // chop off the begining part before the file system part...
990 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
993 if (!EFI_ERROR(Status
)) {
995 // To access as a file system, the file path should only
996 // contain file path components. Follow the file path nodes
997 // and find the target file
999 for ( FilePathNode
= (FILEPATH_DEVICE_PATH
*)DevicePath
1000 ; !IsDevicePathEnd (&FilePathNode
->Header
)
1001 ; FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
)
1003 SHELL_FREE_NON_NULL(AlignedNode
);
1004 AlignedNode
= AllocateCopyPool (DevicePathNodeLength(FilePathNode
), FilePathNode
);
1006 // For file system access each node should be a file path component
1008 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
1009 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
1011 Status
= EFI_UNSUPPORTED
;
1016 // Open this file path node
1022 // if this is the last node in the DevicePath always create (if that was requested).
1024 if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode
->Header
)))) {
1025 Status
= Handle2
->Open (
1028 AlignedNode
->PathName
,
1035 // This is not the last node and we dont want to 'create' existing
1036 // directory entries...
1040 // open without letting it create
1041 // prevents error on existing files/directories
1043 Status
= Handle2
->Open (
1046 AlignedNode
->PathName
,
1047 OpenMode
&~EFI_FILE_MODE_CREATE
,
1051 // if above failed now open and create the 'item'
1052 // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)
1054 if ((EFI_ERROR (Status
)) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)) {
1055 Status
= Handle2
->Open (
1058 AlignedNode
->PathName
,
1065 // Close the last node
1067 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Handle2
);
1070 // If there's been an error, stop
1072 if (EFI_ERROR (Status
)) {
1079 SHELL_FREE_NON_NULL(AlignedNode
);
1080 if (EFI_ERROR(Status
)) {
1081 if (Handle1
!= NULL
) {
1082 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(Handle1
);
1085 *FileHandle
= ConvertEfiFileProtocolToShellHandle(Handle1
, ShellFileHandleGetPath(ShellHandle
));
1091 Creates a file or directory by name.
1093 This function creates an empty new file or directory with the specified attributes and
1094 returns the new file's handle. If the file already exists and is read-only, then
1095 EFI_INVALID_PARAMETER will be returned.
1097 If the file already existed, it is truncated and its attributes updated. If the file is
1098 created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.
1100 If the file name begins with >v, then the file handle which is returned refers to the
1101 shell environment variable with the specified name. If the shell environment variable
1102 already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.
1104 @param FileName Pointer to NULL-terminated file path
1105 @param FileAttribs The new file's attrbiutes. the different attributes are
1106 described in EFI_FILE_PROTOCOL.Open().
1107 @param FileHandle On return, points to the created file handle or directory's handle
1109 @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.
1110 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1111 @retval EFI_UNSUPPORTED could not open the file path
1112 @retval EFI_NOT_FOUND the specified file could not be found on the devide, or could not
1113 file the file system on the device.
1114 @retval EFI_NO_MEDIA the device has no medium.
1115 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1117 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1119 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1120 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1121 when the media is write-protected.
1122 @retval EFI_ACCESS_DENIED The service denied access to the file.
1123 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
1124 @retval EFI_VOLUME_FULL The volume is full.
1129 IN CONST CHAR16
*FileName
,
1130 IN UINT64 FileAttribs
,
1131 OUT SHELL_FILE_HANDLE
*FileHandle
1134 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1139 // Is this for an environment variable
1140 // do we start with >v
1142 if (StrStr(FileName
, L
">v") == FileName
) {
1143 Status
= IsVolatileEnv (FileName
+ 2, &Volatile
);
1144 if (EFI_ERROR (Status
)) {
1148 return (EFI_INVALID_PARAMETER
);
1150 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1151 return (EFI_SUCCESS
);
1155 // We are opening a regular file.
1157 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1158 if (DevicePath
== NULL
) {
1159 return (EFI_NOT_FOUND
);
1162 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, FileAttribs
);
1163 FreePool(DevicePath
);
1169 Register a GUID and a localized human readable name for it.
1171 If Guid is not assigned a name, then assign GuidName to Guid. This list of GUID
1172 names must be used whenever a shell command outputs GUID information.
1174 This function is only available when the major and minor versions in the
1175 EfiShellProtocol are greater than or equal to 2 and 1, respectively.
1177 @param[in] Guid A pointer to the GUID being registered.
1178 @param[in] GuidName A pointer to the localized name for the GUID being registered.
1180 @retval EFI_SUCCESS The operation was successful.
1181 @retval EFI_INVALID_PARAMETER Guid was NULL.
1182 @retval EFI_INVALID_PARAMETER GuidName was NULL.
1183 @retval EFI_ACCESS_DENIED Guid already is assigned a name.
1187 EfiShellRegisterGuidName(
1188 IN CONST EFI_GUID
*Guid
,
1189 IN CONST CHAR16
*GuidName
1192 return (AddNewGuidNameMapping(Guid
, GuidName
, NULL
));
1196 Opens a file or a directory by file name.
1198 This function opens the specified file in the specified OpenMode and returns a file
1200 If the file name begins with >v, then the file handle which is returned refers to the
1201 shell environment variable with the specified name. If the shell environment variable
1202 exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then
1203 EFI_INVALID_PARAMETER is returned.
1205 If the file name is >i, then the file handle which is returned refers to the standard
1206 input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER
1209 If the file name is >o, then the file handle which is returned refers to the standard
1210 output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1213 If the file name is >e, then the file handle which is returned refers to the standard
1214 error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1217 If the file name is NUL, then the file handle that is returned refers to the standard NUL
1218 file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is
1221 If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the
1224 @param FileName Points to the NULL-terminated UCS-2 encoded file name.
1225 @param FileHandle On return, points to the file handle.
1226 @param OpenMode File open mode. Either EFI_FILE_MODE_READ or
1227 EFI_FILE_MODE_WRITE from section 12.4 of the UEFI
1229 @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.
1230 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.
1231 @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.
1232 @retval EFI_NOT_FOUND The specified file could not be found on the device or the file
1233 system could not be found on the device. FileHandle is NULL.
1234 @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.
1235 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1236 longer supported. FileHandle is NULL.
1237 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1238 the FileName. FileHandle is NULL.
1239 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.
1240 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1241 when the media is write-protected. FileHandle is NULL.
1242 @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.
1243 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle
1245 @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.
1249 EfiShellOpenFileByName(
1250 IN CONST CHAR16
*FileName
,
1251 OUT SHELL_FILE_HANDLE
*FileHandle
,
1255 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1262 // Is this for StdIn
1264 if (StrCmp(FileName
, L
">i") == 0) {
1266 // make sure not writing to StdIn
1268 if ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0) {
1269 return (EFI_INVALID_PARAMETER
);
1271 *FileHandle
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1272 ASSERT(*FileHandle
!= NULL
);
1273 return (EFI_SUCCESS
);
1277 // Is this for StdOut
1279 if (StrCmp(FileName
, L
">o") == 0) {
1281 // make sure not writing to StdIn
1283 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1284 return (EFI_INVALID_PARAMETER
);
1286 *FileHandle
= &FileInterfaceStdOut
;
1287 return (EFI_SUCCESS
);
1291 // Is this for NUL / NULL file
1293 if ((gUnicodeCollation
->StriColl (gUnicodeCollation
, (CHAR16
*)FileName
, L
"NUL") == 0) ||
1294 (gUnicodeCollation
->StriColl (gUnicodeCollation
, (CHAR16
*)FileName
, L
"NULL") == 0)) {
1295 *FileHandle
= &FileInterfaceNulFile
;
1296 return (EFI_SUCCESS
);
1300 // Is this for StdErr
1302 if (StrCmp(FileName
, L
">e") == 0) {
1304 // make sure not writing to StdIn
1306 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1307 return (EFI_INVALID_PARAMETER
);
1309 *FileHandle
= &FileInterfaceStdErr
;
1310 return (EFI_SUCCESS
);
1314 // Is this for an environment variable
1315 // do we start with >v
1317 if (StrStr(FileName
, L
">v") == FileName
) {
1318 Status
= IsVolatileEnv (FileName
+ 2, &Volatile
);
1319 if (EFI_ERROR (Status
)) {
1323 ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0)) {
1324 return (EFI_INVALID_PARAMETER
);
1326 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1327 return (EFI_SUCCESS
);
1331 // We are opening a regular file.
1333 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1334 // DEBUG_CODE(InternalShellProtocolDebugPrintMessage (NULL, DevicePath););
1335 if (DevicePath
== NULL
) {
1336 return (EFI_NOT_FOUND
);
1340 // Copy the device path, open the file, then free the memory
1342 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, OpenMode
, 0); // 0 = no specific file attributes
1343 FreePool(DevicePath
);
1349 Deletes the file specified by the file name.
1351 This function deletes a file.
1353 @param FileName Points to the NULL-terminated file name.
1355 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.
1356 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
1357 @sa EfiShellCreateFile
1361 EfiShellDeleteFileByName(
1362 IN CONST CHAR16
*FileName
1365 SHELL_FILE_HANDLE FileHandle
;
1371 // get a handle to the file
1373 Status
= EfiShellCreateFile(FileName
,
1376 if (EFI_ERROR(Status
)) {
1380 // now delete the file
1382 ShellFileHandleRemove(FileHandle
);
1383 return (ShellInfoObject
.NewEfiShellProtocol
->DeleteFile(FileHandle
));
1387 Disables the page break output mode.
1391 EfiShellDisablePageBreak (
1395 ShellInfoObject
.PageBreakEnabled
= FALSE
;
1399 Enables the page break output mode.
1403 EfiShellEnablePageBreak (
1407 ShellInfoObject
.PageBreakEnabled
= TRUE
;
1411 internal worker function to load and run an image via device path.
1413 @param ParentImageHandle A handle of the image that is executing the specified
1415 @param DevicePath device path of the file to execute
1416 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1417 containing the command line. If NULL then the command-
1419 @param Environment Points to a NULL-terminated array of environment
1420 variables with the format 'x=y', where x is the
1421 environment variable name and y is the value. If this
1422 is NULL, then the current shell environment is used.
1424 @param[out] StartImageStatus Returned status from gBS->StartImage.
1426 @retval EFI_SUCCESS The command executed successfully. The status code
1427 returned by the command is pointed to by StatusCode.
1428 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1429 @retval EFI_OUT_OF_RESOURCES Out of resources.
1430 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1434 InternalShellExecuteDevicePath(
1435 IN CONST EFI_HANDLE
*ParentImageHandle
,
1436 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1437 IN CONST CHAR16
*CommandLine OPTIONAL
,
1438 IN CONST CHAR16
**Environment OPTIONAL
,
1439 OUT EFI_STATUS
*StartImageStatus OPTIONAL
1443 EFI_STATUS StartStatus
;
1444 EFI_STATUS CleanupStatus
;
1445 EFI_HANDLE NewHandle
;
1446 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1447 LIST_ENTRY OrigEnvs
;
1448 EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol
;
1454 if (ParentImageHandle
== NULL
) {
1455 return (EFI_INVALID_PARAMETER
);
1458 InitializeListHead(&OrigEnvs
);
1459 ZeroMem(&ShellParamsProtocol
, sizeof(EFI_SHELL_PARAMETERS_PROTOCOL
));
1463 NewCmdLine
= AllocateCopyPool (StrSize (CommandLine
), CommandLine
);
1464 if (NewCmdLine
== NULL
) {
1465 return EFI_OUT_OF_RESOURCES
;
1468 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
1469 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
1470 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
1475 // Load the image with:
1476 // FALSE - not from boot manager and NULL, 0 being not already in memory
1478 Status
= gBS
->LoadImage(
1481 (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
,
1486 if (EFI_ERROR(Status
)) {
1487 if (NewHandle
!= NULL
) {
1488 gBS
->UnloadImage(NewHandle
);
1490 FreePool (NewCmdLine
);
1493 Status
= gBS
->OpenProtocol(
1495 &gEfiLoadedImageProtocolGuid
,
1496 (VOID
**)&LoadedImage
,
1499 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1501 if (!EFI_ERROR(Status
)) {
1503 // If the image is not an app abort it.
1505 if (LoadedImage
->ImageCodeType
!= EfiLoaderCode
){
1510 STRING_TOKEN (STR_SHELL_IMAGE_NOT_APP
),
1511 ShellInfoObject
.HiiHandle
1516 ASSERT(LoadedImage
->LoadOptionsSize
== 0);
1517 if (NewCmdLine
!= NULL
) {
1518 LoadedImage
->LoadOptionsSize
= (UINT32
)StrSize(NewCmdLine
);
1519 LoadedImage
->LoadOptions
= (VOID
*)NewCmdLine
;
1523 // Save our current environment settings for later restoration if necessary
1525 if (Environment
!= NULL
) {
1526 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1527 if (!EFI_ERROR(Status
)) {
1528 Status
= SetEnvironmentVariables(Environment
);
1533 // Initialize and install a shell parameters protocol on the image.
1535 ShellParamsProtocol
.StdIn
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1536 ShellParamsProtocol
.StdOut
= ShellInfoObject
.NewShellParametersProtocol
->StdOut
;
1537 ShellParamsProtocol
.StdErr
= ShellInfoObject
.NewShellParametersProtocol
->StdErr
;
1538 Status
= UpdateArgcArgv(&ShellParamsProtocol
, NewCmdLine
, Efi_Application
, NULL
, NULL
);
1539 ASSERT_EFI_ERROR(Status
);
1541 // Replace Argv[0] with the full path of the binary we're executing:
1542 // If the command line was "foo", the binary might be called "foo.efi".
1543 // "The first entry in [Argv] is always the full file path of the
1544 // executable" - UEFI Shell Spec section 2.3
1546 ImagePath
= EfiShellGetFilePathFromDevicePath (DevicePath
);
1547 // The image we're executing isn't necessarily in a filesystem - it might
1548 // be memory mapped. In this case EfiShellGetFilePathFromDevicePath will
1549 // return NULL, and we'll leave Argv[0] as UpdateArgcArgv set it.
1550 if (ImagePath
!= NULL
) {
1551 if (ShellParamsProtocol
.Argv
== NULL
) {
1552 // Command line was empty or null.
1553 // (UpdateArgcArgv sets Argv to NULL when CommandLine is "" or NULL)
1554 ShellParamsProtocol
.Argv
= AllocatePool (sizeof (CHAR16
*));
1555 if (ShellParamsProtocol
.Argv
== NULL
) {
1556 Status
= EFI_OUT_OF_RESOURCES
;
1559 ShellParamsProtocol
.Argc
= 1;
1561 // Free the string UpdateArgcArgv put in Argv[0];
1562 FreePool (ShellParamsProtocol
.Argv
[0]);
1564 ShellParamsProtocol
.Argv
[0] = ImagePath
;
1567 Status
= gBS
->InstallProtocolInterface(&NewHandle
, &gEfiShellParametersProtocolGuid
, EFI_NATIVE_INTERFACE
, &ShellParamsProtocol
);
1568 ASSERT_EFI_ERROR(Status
);
1570 ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)
1573 // now start the image and if the caller wanted the return code pass it to them...
1575 if (!EFI_ERROR(Status
)) {
1576 StartStatus
= gBS
->StartImage(
1581 if (StartImageStatus
!= NULL
) {
1582 *StartImageStatus
= StartStatus
;
1585 CleanupStatus
= gBS
->UninstallProtocolInterface(
1587 &gEfiShellParametersProtocolGuid
,
1588 &ShellParamsProtocol
1590 ASSERT_EFI_ERROR(CleanupStatus
);
1596 // Unload image - We should only get here if we didn't call StartImage
1597 gBS
->UnloadImage (NewHandle
);
1600 // Free Argv (Allocated in UpdateArgcArgv)
1601 if (ShellParamsProtocol
.Argv
!= NULL
) {
1602 for (Index
= 0; Index
< ShellParamsProtocol
.Argc
; Index
++) {
1603 if (ShellParamsProtocol
.Argv
[Index
] != NULL
) {
1604 FreePool (ShellParamsProtocol
.Argv
[Index
]);
1607 FreePool (ShellParamsProtocol
.Argv
);
1611 // Restore environment variables
1612 if (!IsListEmpty(&OrigEnvs
)) {
1613 CleanupStatus
= SetEnvironmentVariableList(&OrigEnvs
);
1614 ASSERT_EFI_ERROR (CleanupStatus
);
1617 FreePool (NewCmdLine
);
1623 internal worker function to load and run an image in the current shell.
1625 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1626 containing the command line. If NULL then the command-
1628 @param Environment Points to a NULL-terminated array of environment
1629 variables with the format 'x=y', where x is the
1630 environment variable name and y is the value. If this
1631 is NULL, then the current shell environment is used.
1633 @param[out] StartImageStatus Returned status from the command line.
1635 @retval EFI_SUCCESS The command executed successfully. The status code
1636 returned by the command is pointed to by StatusCode.
1637 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1638 @retval EFI_OUT_OF_RESOURCES Out of resources.
1639 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1643 InternalShellExecute(
1644 IN CONST CHAR16
*CommandLine OPTIONAL
,
1645 IN CONST CHAR16
**Environment OPTIONAL
,
1646 OUT EFI_STATUS
*StartImageStatus OPTIONAL
1650 EFI_STATUS CleanupStatus
;
1651 LIST_ENTRY OrigEnvs
;
1653 InitializeListHead(&OrigEnvs
);
1656 // Save our current environment settings for later restoration if necessary
1658 if (Environment
!= NULL
) {
1659 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1660 if (!EFI_ERROR(Status
)) {
1661 Status
= SetEnvironmentVariables(Environment
);
1667 Status
= RunShellCommand(CommandLine
, StartImageStatus
);
1669 // Restore environment variables
1670 if (!IsListEmpty(&OrigEnvs
)) {
1671 CleanupStatus
= SetEnvironmentVariableList(&OrigEnvs
);
1672 ASSERT_EFI_ERROR (CleanupStatus
);
1679 Determine if the UEFI Shell is currently running with nesting enabled or disabled.
1681 @retval FALSE nesting is required
1682 @retval other nesting is enabled
1700 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
) {
1703 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(mNoNestingEnvVarName
, &TempSize
, Temp
);
1704 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1705 Temp
= AllocateZeroPool(TempSize
+ sizeof(CHAR16
));
1707 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(mNoNestingEnvVarName
, &TempSize
, Temp
);
1710 Temp2
= StrnCatGrow(&Temp2
, NULL
, mNoNestingTrue
, 0);
1711 if (Temp
!= NULL
&& Temp2
!= NULL
&& StringNoCaseCompare(&Temp
, &Temp2
) == 0) {
1713 // Use the no nesting method.
1719 SHELL_FREE_NON_NULL(Temp
);
1720 SHELL_FREE_NON_NULL(Temp2
);
1725 Execute the command line.
1727 This function creates a nested instance of the shell and executes the specified
1728 command (CommandLine) with the specified environment (Environment). Upon return,
1729 the status code returned by the specified command is placed in StatusCode.
1731 If Environment is NULL, then the current environment is used and all changes made
1732 by the commands executed will be reflected in the current environment. If the
1733 Environment is non-NULL, then the changes made will be discarded.
1735 The CommandLine is executed from the current working directory on the current
1738 @param ParentImageHandle A handle of the image that is executing the specified
1740 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1741 containing the command line. If NULL then the command-
1743 @param Environment Points to a NULL-terminated array of environment
1744 variables with the format 'x=y', where x is the
1745 environment variable name and y is the value. If this
1746 is NULL, then the current shell environment is used.
1747 @param StatusCode Points to the status code returned by the CommandLine.
1749 @retval EFI_SUCCESS The command executed successfully. The status code
1750 returned by the command is pointed to by StatusCode.
1751 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1752 @retval EFI_OUT_OF_RESOURCES Out of resources.
1753 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1754 @retval EFI_UNSUPPORTED The support level required for this function is not present.
1756 @sa InternalShellExecuteDevicePath
1761 IN EFI_HANDLE
*ParentImageHandle
,
1762 IN CHAR16
*CommandLine OPTIONAL
,
1763 IN CHAR16
**Environment OPTIONAL
,
1764 OUT EFI_STATUS
*StatusCode OPTIONAL
1769 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1772 if ((PcdGet8(PcdShellSupportLevel
) < 1)) {
1773 return (EFI_UNSUPPORTED
);
1776 if (NestingEnabled()) {
1777 DevPath
= AppendDevicePath (ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
1780 Temp
= ConvertDevicePathToText(ShellInfoObject
.FileDevPath
, TRUE
, TRUE
);
1782 Temp
= ConvertDevicePathToText(ShellInfoObject
.ImageDevPath
, TRUE
, TRUE
);
1784 Temp
= ConvertDevicePathToText(DevPath
, TRUE
, TRUE
);
1790 ASSERT((Temp
== NULL
&& Size
== 0) || (Temp
!= NULL
));
1791 StrnCatGrow(&Temp
, &Size
, L
"Shell.efi -_exit ", 0);
1792 StrnCatGrow(&Temp
, &Size
, CommandLine
, 0);
1794 Status
= InternalShellExecuteDevicePath(
1798 (CONST CHAR16
**)Environment
,
1802 // de-allocate and return
1807 Status
= InternalShellExecute(
1808 (CONST CHAR16
*)CommandLine
,
1809 (CONST CHAR16
**)Environment
,
1817 Utility cleanup function for EFI_SHELL_FILE_INFO objects.
1819 1) frees all pointers (non-NULL)
1820 2) Closes the SHELL_FILE_HANDLE
1822 @param FileListNode pointer to the list node to free
1826 InternalFreeShellFileInfoNode(
1827 IN EFI_SHELL_FILE_INFO
*FileListNode
1830 if (FileListNode
->Info
!= NULL
) {
1831 FreePool((VOID
*)FileListNode
->Info
);
1833 if (FileListNode
->FileName
!= NULL
) {
1834 FreePool((VOID
*)FileListNode
->FileName
);
1836 if (FileListNode
->FullName
!= NULL
) {
1837 FreePool((VOID
*)FileListNode
->FullName
);
1839 if (FileListNode
->Handle
!= NULL
) {
1840 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileListNode
->Handle
);
1842 FreePool(FileListNode
);
1845 Frees the file list.
1847 This function cleans up the file list and any related data structures. It has no
1848 impact on the files themselves.
1850 @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is
1851 defined in OpenFileList()
1853 @retval EFI_SUCCESS Free the file list successfully.
1854 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1858 EfiShellFreeFileList(
1859 IN EFI_SHELL_FILE_INFO
**FileList
1862 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1864 if (FileList
== NULL
|| *FileList
== NULL
) {
1865 return (EFI_INVALID_PARAMETER
);
1868 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1869 ; !IsListEmpty(&(*FileList
)->Link
)
1870 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1872 RemoveEntryList(&ShellFileListItem
->Link
);
1873 InternalFreeShellFileInfoNode(ShellFileListItem
);
1875 InternalFreeShellFileInfoNode(*FileList
);
1877 return(EFI_SUCCESS
);
1881 Deletes the duplicate file names files in the given file list.
1883 This function deletes the reduplicate files in the given file list.
1885 @param FileList A pointer to the first entry in the file list.
1887 @retval EFI_SUCCESS Always success.
1888 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1892 EfiShellRemoveDupInFileList(
1893 IN EFI_SHELL_FILE_INFO
**FileList
1896 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1897 EFI_SHELL_FILE_INFO
*ShellFileListItem2
;
1898 EFI_SHELL_FILE_INFO
*TempNode
;
1900 if (FileList
== NULL
|| *FileList
== NULL
) {
1901 return (EFI_INVALID_PARAMETER
);
1903 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1904 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1905 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1907 for ( ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1908 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1909 ; ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1911 if (gUnicodeCollation
->StriColl(
1913 (CHAR16
*)ShellFileListItem
->FullName
,
1914 (CHAR16
*)ShellFileListItem2
->FullName
) == 0
1916 TempNode
= (EFI_SHELL_FILE_INFO
*)GetPreviousNode(
1918 &ShellFileListItem2
->Link
1920 RemoveEntryList(&ShellFileListItem2
->Link
);
1921 InternalFreeShellFileInfoNode(ShellFileListItem2
);
1922 // Set ShellFileListItem2 to PreviousNode so we don't access Freed
1923 // memory in GetNextNode in the loop expression above.
1924 ShellFileListItem2
= TempNode
;
1928 return (EFI_SUCCESS
);
1932 // This is the same structure as the external version, but it has no CONST qualifiers.
1935 LIST_ENTRY Link
; ///< Linked list members.
1936 EFI_STATUS Status
; ///< Status of opening the file. Valid only if Handle != NULL.
1937 CHAR16
*FullName
; ///< Fully qualified filename.
1938 CHAR16
*FileName
; ///< name of this file.
1939 SHELL_FILE_HANDLE Handle
; ///< Handle for interacting with the opened file or NULL if closed.
1940 EFI_FILE_INFO
*Info
; ///< Pointer to the FileInfo struct for this file or NULL.
1941 } EFI_SHELL_FILE_INFO_NO_CONST
;
1944 Allocates and duplicates a EFI_SHELL_FILE_INFO node.
1946 @param[in] Node The node to copy from.
1947 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.
1949 @retval NULL a memory allocation error ocurred
1950 @return != NULL a pointer to the new node
1952 EFI_SHELL_FILE_INFO
*
1954 InternalDuplicateShellFileInfo(
1955 IN EFI_SHELL_FILE_INFO
*Node
,
1959 EFI_SHELL_FILE_INFO_NO_CONST
*NewNode
;
1962 // try to confirm that the objects are in sync
1964 ASSERT(sizeof(EFI_SHELL_FILE_INFO_NO_CONST
) == sizeof(EFI_SHELL_FILE_INFO
));
1966 NewNode
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1967 if (NewNode
== NULL
) {
1970 NewNode
->FullName
= AllocateCopyPool(StrSize(Node
->FullName
), Node
->FullName
);
1971 NewNode
->FileName
= AllocateCopyPool(StrSize(Node
->FileName
), Node
->FileName
);
1972 NewNode
->Info
= AllocateCopyPool((UINTN
)Node
->Info
->Size
, Node
->Info
);
1973 if ( NewNode
->FullName
== NULL
1974 || NewNode
->FileName
== NULL
1975 || NewNode
->Info
== NULL
1977 SHELL_FREE_NON_NULL(NewNode
->FullName
);
1978 SHELL_FREE_NON_NULL(NewNode
->FileName
);
1979 SHELL_FREE_NON_NULL(NewNode
->Info
);
1980 SHELL_FREE_NON_NULL(NewNode
);
1983 NewNode
->Status
= Node
->Status
;
1984 NewNode
->Handle
= Node
->Handle
;
1986 Node
->Handle
= NULL
;
1989 return((EFI_SHELL_FILE_INFO
*)NewNode
);
1993 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation
1994 failed it will return NULL.
1996 @param[in] BasePath the Path to prepend onto filename for FullPath
1997 @param[in] Status Status member initial value.
1998 @param[in] FileName FileName member initial value.
1999 @param[in] Handle Handle member initial value.
2000 @param[in] Info Info struct to copy.
2002 @retval NULL An error ocurred.
2003 @return a pointer to the newly allocated structure.
2005 EFI_SHELL_FILE_INFO
*
2007 CreateAndPopulateShellFileInfo(
2008 IN CONST CHAR16
*BasePath
,
2009 IN CONST EFI_STATUS Status
,
2010 IN CONST CHAR16
*FileName
,
2011 IN CONST SHELL_FILE_HANDLE Handle
,
2012 IN CONST EFI_FILE_INFO
*Info
2015 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2022 ShellFileListItem
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2023 if (ShellFileListItem
== NULL
) {
2026 if (Info
!= NULL
&& Info
->Size
!= 0) {
2027 ShellFileListItem
->Info
= AllocateZeroPool((UINTN
)Info
->Size
);
2028 if (ShellFileListItem
->Info
== NULL
) {
2029 FreePool(ShellFileListItem
);
2032 CopyMem(ShellFileListItem
->Info
, Info
, (UINTN
)Info
->Size
);
2034 ShellFileListItem
->Info
= NULL
;
2036 if (FileName
!= NULL
) {
2037 ASSERT(TempString
== NULL
);
2038 ShellFileListItem
->FileName
= StrnCatGrow(&TempString
, 0, FileName
, 0);
2039 if (ShellFileListItem
->FileName
== NULL
) {
2040 FreePool(ShellFileListItem
->Info
);
2041 FreePool(ShellFileListItem
);
2045 ShellFileListItem
->FileName
= NULL
;
2049 if (BasePath
!= NULL
) {
2050 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2051 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
2052 if (TempString
== NULL
) {
2053 FreePool((VOID
*)ShellFileListItem
->FileName
);
2054 SHELL_FREE_NON_NULL(ShellFileListItem
->Info
);
2055 FreePool(ShellFileListItem
);
2059 if (ShellFileListItem
->FileName
!= NULL
) {
2060 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2061 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileListItem
->FileName
, 0);
2062 if (TempString
== NULL
) {
2063 FreePool((VOID
*)ShellFileListItem
->FileName
);
2064 FreePool(ShellFileListItem
->Info
);
2065 FreePool(ShellFileListItem
);
2070 TempString
= PathCleanUpDirectories(TempString
);
2072 ShellFileListItem
->FullName
= TempString
;
2073 ShellFileListItem
->Status
= Status
;
2074 ShellFileListItem
->Handle
= Handle
;
2076 return (ShellFileListItem
);
2080 Find all files in a specified directory.
2082 @param FileDirHandle Handle of the directory to search.
2083 @param FileList On return, points to the list of files in the directory
2084 or NULL if there are no files in the directory.
2086 @retval EFI_SUCCESS File information was returned successfully.
2087 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.
2088 @retval EFI_DEVICE_ERROR The device reported an error.
2089 @retval EFI_NO_MEDIA The device media is not present.
2090 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.
2091 @return An error from FileHandleGetFileName().
2095 EfiShellFindFilesInDir(
2096 IN SHELL_FILE_HANDLE FileDirHandle
,
2097 OUT EFI_SHELL_FILE_INFO
**FileList
2100 EFI_SHELL_FILE_INFO
*ShellFileList
;
2101 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2102 EFI_FILE_INFO
*FileInfo
;
2111 Status
= FileHandleGetFileName(FileDirHandle
, &BasePath
);
2112 if (EFI_ERROR(Status
)) {
2116 if (ShellFileHandleGetPath(FileDirHandle
) != NULL
) {
2119 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileHandleGetPath(FileDirHandle
), 0);
2120 if (TempString
== NULL
) {
2121 SHELL_FREE_NON_NULL(BasePath
);
2122 return (EFI_OUT_OF_RESOURCES
);
2124 TempSpot
= StrStr(TempString
, L
";");
2126 if (TempSpot
!= NULL
) {
2127 *TempSpot
= CHAR_NULL
;
2130 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
2131 if (TempString
== NULL
) {
2132 SHELL_FREE_NON_NULL(BasePath
);
2133 return (EFI_OUT_OF_RESOURCES
);
2135 SHELL_FREE_NON_NULL(BasePath
);
2136 BasePath
= TempString
;
2140 ShellFileList
= NULL
;
2141 ShellFileListItem
= NULL
;
2143 Status
= EFI_SUCCESS
;
2146 for ( Status
= FileHandleFindFirstFile(FileDirHandle
, &FileInfo
)
2147 ; !EFI_ERROR(Status
) && !NoFile
2148 ; Status
= FileHandleFindNextFile(FileDirHandle
, FileInfo
, &NoFile
)
2150 if (ShellFileList
== NULL
) {
2151 ShellFileList
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2152 if (ShellFileList
== NULL
) {
2153 SHELL_FREE_NON_NULL (BasePath
);
2154 return EFI_OUT_OF_RESOURCES
;
2156 InitializeListHead(&ShellFileList
->Link
);
2159 // allocate a new EFI_SHELL_FILE_INFO and populate it...
2161 ShellFileListItem
= CreateAndPopulateShellFileInfo(
2163 EFI_SUCCESS
, // success since we didnt fail to open it...
2165 NULL
, // no handle since not open
2167 if (ShellFileListItem
== NULL
) {
2168 Status
= EFI_OUT_OF_RESOURCES
;
2170 // Free resources outside the loop.
2174 InsertTailList(&ShellFileList
->Link
, &ShellFileListItem
->Link
);
2176 if (EFI_ERROR(Status
)) {
2177 EfiShellFreeFileList(&ShellFileList
);
2180 *FileList
= ShellFileList
;
2182 SHELL_FREE_NON_NULL(BasePath
);
2187 Get the GUID value from a human readable name.
2189 If GuidName is a known GUID name, then update Guid to have the correct value for
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] GuidName A pointer to the localized name for the GUID being queried.
2196 @param[out] Guid A pointer to the GUID structure to be filled in.
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 GuidName is not a known GUID Name.
2205 EfiShellGetGuidFromName(
2206 IN CONST CHAR16
*GuidName
,
2213 if (Guid
== NULL
|| GuidName
== NULL
) {
2214 return (EFI_INVALID_PARAMETER
);
2217 Status
= GetGuidFromStringName(GuidName
, NULL
, &NewGuid
);
2219 if (!EFI_ERROR(Status
)) {
2220 CopyGuid(NewGuid
, Guid
);
2227 Get the human readable name for a GUID from the value.
2229 If Guid is assigned a name, then update *GuidName to point to the name. The callee
2230 should not modify the value.
2232 This function is only available when the major and minor versions in the
2233 EfiShellProtocol are greater than or equal to 2 and 1, respectively.
2235 @param[in] Guid A pointer to the GUID being queried.
2236 @param[out] GuidName A pointer to a pointer the localized to name for the GUID being requested
2238 @retval EFI_SUCCESS The operation was successful.
2239 @retval EFI_INVALID_PARAMETER Guid was NULL.
2240 @retval EFI_INVALID_PARAMETER GuidName was NULL.
2241 @retval EFI_NOT_FOUND Guid is not assigned a name.
2245 EfiShellGetGuidName(
2246 IN CONST EFI_GUID
*Guid
,
2247 OUT CONST CHAR16
**GuidName
2252 if (Guid
== NULL
|| GuidName
== NULL
) {
2253 return (EFI_INVALID_PARAMETER
);
2256 Name
= GetStringNameFromGuid(Guid
, NULL
);
2257 if (Name
== NULL
|| StrLen(Name
) == 0) {
2258 SHELL_FREE_NON_NULL(Name
);
2259 return (EFI_NOT_FOUND
);
2262 *GuidName
= AddBufferToFreeList(Name
);
2264 return (EFI_SUCCESS
);
2268 Updates a file name to be preceeded by the mapped drive name
2270 @param[in] BasePath the Mapped drive name to prepend
2271 @param[in, out] Path pointer to pointer to the file name to update.
2274 @retval EFI_OUT_OF_RESOURCES
2279 IN CONST CHAR16
*BasePath
,
2280 IN OUT CHAR16
**Path
2289 ASSERT(Path
!= NULL
);
2290 ASSERT(*Path
!= NULL
);
2291 ASSERT(BasePath
!= NULL
);
2294 // convert a local path to an absolute path
2296 if (StrStr(*Path
, L
":") == NULL
) {
2297 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2298 StrnCatGrow(&Path2
, &Path2Size
, BasePath
, 0);
2299 if (Path2
== NULL
) {
2300 return (EFI_OUT_OF_RESOURCES
);
2302 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2303 StrnCatGrow(&Path2
, &Path2Size
, (*Path
)[0] == L
'\\'?(*Path
) + 1 :*Path
, 0);
2304 if (Path2
== NULL
) {
2305 return (EFI_OUT_OF_RESOURCES
);
2312 return (EFI_SUCCESS
);
2316 If FileHandle is a directory then the function reads from FileHandle and reads in
2317 each of the FileInfo structures. If one of them matches the Pattern's first
2318 "level" then it opens that handle and calls itself on that handle.
2320 If FileHandle is a file and matches all of the remaining Pattern (which would be
2321 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.
2323 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call
2324 FreeFileList with FileList.
2326 @param[in] FilePattern The FilePattern to check against.
2327 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure
2328 @param[in] FileHandle The FileHandle to start with
2329 @param[in, out] FileList pointer to pointer to list of found files.
2330 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.
2331 @param[in] MapName The file system name this file is on.
2333 @retval EFI_SUCCESS all files were found and the FileList contains a list.
2334 @retval EFI_NOT_FOUND no files were found
2335 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
2340 IN CONST CHAR16
*FilePattern
,
2341 IN EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
,
2342 IN SHELL_FILE_HANDLE FileHandle
,
2343 IN OUT EFI_SHELL_FILE_INFO
**FileList
,
2344 IN CONST EFI_SHELL_FILE_INFO
*ParentNode OPTIONAL
,
2345 IN CONST CHAR16
*MapName
2349 CONST CHAR16
*NextFilePatternStart
;
2350 CHAR16
*CurrentFilePattern
;
2351 EFI_SHELL_FILE_INFO
*ShellInfo
;
2352 EFI_SHELL_FILE_INFO
*ShellInfoNode
;
2353 EFI_SHELL_FILE_INFO
*NewShellNode
;
2354 EFI_FILE_INFO
*FileInfo
;
2356 CHAR16
*NewFullName
;
2359 if ( FilePattern
== NULL
2360 || UnicodeCollation
== NULL
2363 return (EFI_INVALID_PARAMETER
);
2366 CurrentFilePattern
= NULL
;
2368 if (*FilePattern
== L
'\\') {
2372 for( NextFilePatternStart
= FilePattern
2373 ; *NextFilePatternStart
!= CHAR_NULL
&& *NextFilePatternStart
!= L
'\\'
2374 ; NextFilePatternStart
++);
2376 CurrentFilePattern
= AllocateZeroPool((NextFilePatternStart
-FilePattern
+1)*sizeof(CHAR16
));
2377 if (CurrentFilePattern
== NULL
) {
2378 return EFI_OUT_OF_RESOURCES
;
2381 StrnCpyS(CurrentFilePattern
, NextFilePatternStart
-FilePattern
+1, FilePattern
, NextFilePatternStart
-FilePattern
);
2383 if (CurrentFilePattern
[0] == CHAR_NULL
2384 &&NextFilePatternStart
[0] == CHAR_NULL
2387 // we want the parent or root node (if no parent)
2389 if (ParentNode
== NULL
) {
2391 // We want the root node. create the node.
2393 FileInfo
= FileHandleGetInfo(FileHandle
);
2394 NewShellNode
= CreateAndPopulateShellFileInfo(
2401 SHELL_FREE_NON_NULL(FileInfo
);
2404 // Add the current parameter FileHandle to the list, then end...
2406 NewShellNode
= InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO
*)ParentNode
, TRUE
);
2408 if (NewShellNode
== NULL
) {
2409 Status
= EFI_OUT_OF_RESOURCES
;
2411 NewShellNode
->Handle
= NULL
;
2412 if (*FileList
== NULL
) {
2413 *FileList
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2414 InitializeListHead(&((*FileList
)->Link
));
2418 // Add to the returning to use list
2420 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2422 Status
= EFI_SUCCESS
;
2425 Status
= EfiShellFindFilesInDir(FileHandle
, &ShellInfo
);
2427 if (!EFI_ERROR(Status
)){
2428 if (StrStr(NextFilePatternStart
, L
"\\") != NULL
){
2433 for ( ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ShellInfo
->Link
)
2434 ; !IsNull (&ShellInfo
->Link
, &ShellInfoNode
->Link
)
2435 ; ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ShellInfo
->Link
, &ShellInfoNode
->Link
)
2437 if (UnicodeCollation
->MetaiMatch(UnicodeCollation
, (CHAR16
*)ShellInfoNode
->FileName
, CurrentFilePattern
)){
2438 if (ShellInfoNode
->FullName
!= NULL
&& StrStr(ShellInfoNode
->FullName
, L
":") == NULL
) {
2439 Size
= StrSize (ShellInfoNode
->FullName
) + StrSize (MapName
);
2440 NewFullName
= AllocateZeroPool(Size
);
2441 if (NewFullName
== NULL
) {
2442 Status
= EFI_OUT_OF_RESOURCES
;
2444 StrCpyS(NewFullName
, Size
/ sizeof(CHAR16
), MapName
);
2445 StrCatS(NewFullName
, Size
/ sizeof(CHAR16
), ShellInfoNode
->FullName
);
2446 FreePool ((VOID
*) ShellInfoNode
->FullName
);
2447 ShellInfoNode
->FullName
= NewFullName
;
2450 if (Directory
&& !EFI_ERROR(Status
) && ShellInfoNode
->FullName
!= NULL
&& ShellInfoNode
->FileName
!= NULL
){
2452 // should be a directory
2456 // don't open the . and .. directories
2458 if ( (StrCmp(ShellInfoNode
->FileName
, L
".") != 0)
2459 && (StrCmp(ShellInfoNode
->FileName
, L
"..") != 0)
2464 if (EFI_ERROR(Status
)) {
2468 // Open the directory since we need that handle in the next recursion.
2470 ShellInfoNode
->Status
= EfiShellOpenFileByName (ShellInfoNode
->FullName
, &ShellInfoNode
->Handle
, EFI_FILE_MODE_READ
);
2473 // recurse with the next part of the pattern
2475 Status
= ShellSearchHandle(NextFilePatternStart
, UnicodeCollation
, ShellInfoNode
->Handle
, FileList
, ShellInfoNode
, MapName
);
2476 EfiShellClose(ShellInfoNode
->Handle
);
2477 ShellInfoNode
->Handle
= NULL
;
2479 } else if (!EFI_ERROR(Status
)) {
2485 // copy the information we need into a new Node
2487 NewShellNode
= InternalDuplicateShellFileInfo(ShellInfoNode
, FALSE
);
2488 if (NewShellNode
== NULL
) {
2489 Status
= EFI_OUT_OF_RESOURCES
;
2491 if (*FileList
== NULL
) {
2492 *FileList
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2493 InitializeListHead(&((*FileList
)->Link
));
2497 // Add to the returning to use list
2499 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2502 if (EFI_ERROR(Status
)) {
2506 if (EFI_ERROR(Status
)) {
2507 EfiShellFreeFileList(&ShellInfo
);
2509 Status
= EfiShellFreeFileList(&ShellInfo
);
2514 if (*FileList
== NULL
|| (*FileList
!= NULL
&& IsListEmpty(&(*FileList
)->Link
))) {
2515 Status
= EFI_NOT_FOUND
;
2518 FreePool(CurrentFilePattern
);
2523 Find files that match a specified pattern.
2525 This function searches for all files and directories that match the specified
2526 FilePattern. The FilePattern can contain wild-card characters. The resulting file
2527 information is placed in the file list FileList.
2529 Wildcards are processed
2530 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.
2532 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo
2533 field is set to NULL.
2535 if *FileList is not NULL then it must be a pre-existing and properly initialized list.
2537 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.
2538 @param FileList On return, points to the start of a file list containing the names
2539 of all matching files or else points to NULL if no matching files
2540 were found. only on a EFI_SUCCESS return will; this be non-NULL.
2542 @retval EFI_SUCCESS Files found. FileList is a valid list.
2543 @retval EFI_NOT_FOUND No files found.
2544 @retval EFI_NO_MEDIA The device has no media
2545 @retval EFI_DEVICE_ERROR The device reported an error
2546 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted
2551 IN CONST CHAR16
*FilePattern
,
2552 OUT EFI_SHELL_FILE_INFO
**FileList
2556 CHAR16
*PatternCopy
;
2557 CHAR16
*PatternCurrentLocation
;
2558 EFI_DEVICE_PATH_PROTOCOL
*RootDevicePath
;
2559 SHELL_FILE_HANDLE RootFileHandle
;
2563 if ( FilePattern
== NULL
2565 || StrStr(FilePattern
, L
":") == NULL
2567 return (EFI_INVALID_PARAMETER
);
2569 Status
= EFI_SUCCESS
;
2570 RootDevicePath
= NULL
;
2571 RootFileHandle
= NULL
;
2573 PatternCopy
= AllocateCopyPool(StrSize(FilePattern
), FilePattern
);
2574 if (PatternCopy
== NULL
) {
2575 return (EFI_OUT_OF_RESOURCES
);
2578 PatternCopy
= PathCleanUpDirectories(PatternCopy
);
2580 Count
= StrStr(PatternCopy
, L
":") - PatternCopy
+ 1;
2581 ASSERT (Count
<= StrLen (PatternCopy
));
2583 ASSERT(MapName
== NULL
);
2584 MapName
= StrnCatGrow(&MapName
, NULL
, PatternCopy
, Count
);
2585 if (MapName
== NULL
) {
2586 Status
= EFI_OUT_OF_RESOURCES
;
2588 RootDevicePath
= EfiShellGetDevicePathFromFilePath(PatternCopy
);
2589 if (RootDevicePath
== NULL
) {
2590 Status
= EFI_INVALID_PARAMETER
;
2592 Status
= EfiShellOpenRoot(RootDevicePath
, &RootFileHandle
);
2593 if (!EFI_ERROR(Status
)) {
2594 for ( PatternCurrentLocation
= PatternCopy
2595 ; *PatternCurrentLocation
!= ':'
2596 ; PatternCurrentLocation
++);
2597 PatternCurrentLocation
++;
2598 Status
= ShellSearchHandle(PatternCurrentLocation
, gUnicodeCollation
, RootFileHandle
, FileList
, NULL
, MapName
);
2599 EfiShellClose(RootFileHandle
);
2601 FreePool(RootDevicePath
);
2605 SHELL_FREE_NON_NULL(PatternCopy
);
2606 SHELL_FREE_NON_NULL(MapName
);
2612 Opens the files that match the path specified.
2614 This function opens all of the files specified by Path. Wildcards are processed
2615 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each
2616 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.
2618 @param Path A pointer to the path string.
2619 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or
2620 EFI_FILE_MODE_WRITE.
2621 @param FileList Points to the start of a list of files opened.
2623 @retval EFI_SUCCESS Create the file list successfully.
2624 @return Others Can't create the file list.
2628 EfiShellOpenFileList(
2631 IN OUT EFI_SHELL_FILE_INFO
**FileList
2635 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2638 CONST CHAR16
*CurDir
;
2641 PathCleanUpDirectories(Path
);
2646 if (FileList
== NULL
|| *FileList
== NULL
) {
2647 return (EFI_INVALID_PARAMETER
);
2650 if (*Path
== L
'.' && *(Path
+1) == L
'\\') {
2655 // convert a local path to an absolute path
2657 if (StrStr(Path
, L
":") == NULL
) {
2658 CurDir
= EfiShellGetCurDir(NULL
);
2659 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2660 StrnCatGrow(&Path2
, &Path2Size
, CurDir
, 0);
2661 StrnCatGrow(&Path2
, &Path2Size
, L
"\\", 0);
2662 if (*Path
== L
'\\') {
2664 while (PathRemoveLastItem(Path2
)) ;
2666 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2667 StrnCatGrow(&Path2
, &Path2Size
, Path
, 0);
2669 ASSERT(Path2
== NULL
);
2670 StrnCatGrow(&Path2
, NULL
, Path
, 0);
2673 PathCleanUpDirectories (Path2
);
2678 Status
= EfiShellFindFiles(Path2
, FileList
);
2682 if (EFI_ERROR(Status
)) {
2688 // We had no errors so open all the files (that are not already opened...)
2690 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
2691 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2692 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2694 if (ShellFileListItem
->Status
== 0 && ShellFileListItem
->Handle
== NULL
) {
2695 ShellFileListItem
->Status
= EfiShellOpenFileByName (ShellFileListItem
->FullName
, &ShellFileListItem
->Handle
, OpenMode
);
2701 return (EFI_NOT_FOUND
);
2703 return(EFI_SUCCESS
);
2707 Gets the environment variable and Attributes, or list of environment variables. Can be
2708 used instead of GetEnv().
2710 This function returns the current value of the specified environment variable and
2711 the Attributes. If no variable name was specified, then all of the known
2712 variables will be returned.
2714 @param[in] Name A pointer to the environment variable name. If Name is NULL,
2715 then the function will return all of the defined shell
2716 environment variables. In the case where multiple environment
2717 variables are being returned, each variable will be terminated
2718 by a NULL, and the list will be terminated by a double NULL.
2719 @param[out] Attributes If not NULL, a pointer to the returned attributes bitmask for
2720 the environment variable. In the case where Name is NULL, and
2721 multiple environment variables are being returned, Attributes
2724 @retval NULL The environment variable doesn't exist.
2725 @return A non-NULL value points to the variable's value. The returned
2726 pointer does not need to be freed by the caller.
2731 IN CONST CHAR16
*Name
,
2732 OUT UINT32
*Attributes OPTIONAL
2739 CHAR16
*CurrentWriteLocation
;
2747 // Build the semi-colon delimited list. (2 passes)
2749 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
2750 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
2751 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
2753 ASSERT(Node
->Key
!= NULL
);
2754 Size
+= StrSize(Node
->Key
);
2757 Size
+= 2*sizeof(CHAR16
);
2759 Buffer
= AllocateZeroPool(Size
);
2760 if (Buffer
== NULL
) {
2763 CurrentWriteLocation
= (CHAR16
*)Buffer
;
2765 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
2766 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
2767 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
2769 ASSERT(Node
->Key
!= NULL
);
2770 StrCpyS( CurrentWriteLocation
,
2771 (Size
)/sizeof(CHAR16
) - (CurrentWriteLocation
- ((CHAR16
*)Buffer
)),
2774 CurrentWriteLocation
+= StrLen(CurrentWriteLocation
) + 1;
2779 // We are doing a specific environment variable
2781 Status
= ShellFindEnvVarInList(Name
, (CHAR16
**)&Buffer
, &Size
, Attributes
);
2783 if (EFI_ERROR(Status
)){
2785 // get the size we need for this EnvVariable
2787 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name
, Attributes
, &Size
, Buffer
);
2788 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2790 // Allocate the space and recall the get function
2792 Buffer
= AllocateZeroPool(Size
);
2793 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name
, Attributes
, &Size
, Buffer
);
2796 // we didnt get it (might not exist)
2797 // free the memory if we allocated any and return NULL
2799 if (EFI_ERROR(Status
)) {
2800 if (Buffer
!= NULL
) {
2806 // If we did not find the environment variable in the gShellEnvVarList
2807 // but get it from UEFI variable storage successfully then we need update
2808 // the gShellEnvVarList.
2810 ShellFreeEnvVarList ();
2811 Status
= ShellInitEnvVarList ();
2812 ASSERT (Status
== EFI_SUCCESS
);
2818 // return the buffer
2820 return (AddBufferToFreeList(Buffer
));
2824 Gets either a single or list of environment variables.
2826 If name is not NULL then this function returns the current value of the specified
2827 environment variable.
2829 If Name is NULL, then a list of all environment variable names is returned. Each is a
2830 NULL terminated string with a double NULL terminating the list.
2832 @param Name A pointer to the environment variable name. If
2833 Name is NULL, then the function will return all
2834 of the defined shell environment variables. In
2835 the case where multiple environment variables are
2836 being returned, each variable will be terminated by
2837 a NULL, and the list will be terminated by a double
2840 @retval !=NULL A pointer to the returned string.
2841 The returned pointer does not need to be freed by the caller.
2843 @retval NULL The environment variable doesn't exist or there are
2844 no environment variables.
2849 IN CONST CHAR16
*Name
2852 return (EfiShellGetEnvEx(Name
, NULL
));
2856 Internal variable setting function. Allows for setting of the read only variables.
2858 @param Name Points to the NULL-terminated environment variable name.
2859 @param Value Points to the NULL-terminated environment variable value. If the value is an
2860 empty string then the environment variable is deleted.
2861 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2863 @retval EFI_SUCCESS The environment variable was successfully updated.
2867 InternalEfiShellSetEnv(
2868 IN CONST CHAR16
*Name
,
2869 IN CONST CHAR16
*Value
,
2875 if (Value
== NULL
|| StrLen(Value
) == 0) {
2876 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2877 if (!EFI_ERROR(Status
)) {
2878 ShellRemvoeEnvVarFromList(Name
);
2881 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2882 Status
= ShellAddEnvVarToList(
2883 Name
, Value
, StrSize(Value
),
2884 EFI_VARIABLE_BOOTSERVICE_ACCESS
| (Volatile
? 0 : EFI_VARIABLE_NON_VOLATILE
)
2886 if (!EFI_ERROR (Status
)) {
2888 ? SHELL_SET_ENVIRONMENT_VARIABLE_V(Name
, StrSize(Value
), Value
)
2889 : SHELL_SET_ENVIRONMENT_VARIABLE_NV(Name
, StrSize(Value
), Value
);
2890 if (EFI_ERROR (Status
)) {
2891 ShellRemvoeEnvVarFromList(Name
);
2899 Sets the environment variable.
2901 This function changes the current value of the specified environment variable. If the
2902 environment variable exists and the Value is an empty string, then the environment
2903 variable is deleted. If the environment variable exists and the Value is not an empty
2904 string, then the value of the environment variable is changed. If the environment
2905 variable does not exist and the Value is an empty string, there is no action. If the
2906 environment variable does not exist and the Value is a non-empty string, then the
2907 environment variable is created and assigned the specified value.
2909 For a description of volatile and non-volatile environment variables, see UEFI Shell
2910 2.0 specification section 3.6.1.
2912 @param Name Points to the NULL-terminated environment variable name.
2913 @param Value Points to the NULL-terminated environment variable value. If the value is an
2914 empty string then the environment variable is deleted.
2915 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2917 @retval EFI_SUCCESS The environment variable was successfully updated.
2922 IN CONST CHAR16
*Name
,
2923 IN CONST CHAR16
*Value
,
2927 if (Name
== NULL
|| *Name
== CHAR_NULL
) {
2928 return (EFI_INVALID_PARAMETER
);
2931 // Make sure we dont 'set' a predefined read only variable
2933 if (gUnicodeCollation
->StriColl(
2937 ||gUnicodeCollation
->StriColl(
2941 ||gUnicodeCollation
->StriColl(
2945 ||gUnicodeCollation
->StriColl(
2948 L
"uefishellsupport") == 0
2949 ||gUnicodeCollation
->StriColl(
2952 L
"uefishellversion") == 0
2953 ||gUnicodeCollation
->StriColl(
2956 L
"uefiversion") == 0
2957 ||(!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
&&
2958 gUnicodeCollation
->StriColl(
2961 (CHAR16
*)mNoNestingEnvVarName
) == 0)
2963 return (EFI_INVALID_PARAMETER
);
2965 return (InternalEfiShellSetEnv(Name
, Value
, Volatile
));
2969 Returns the current directory on the specified device.
2971 If FileSystemMapping is NULL, it returns the current working directory. If the
2972 FileSystemMapping is not NULL, it returns the current directory associated with the
2973 FileSystemMapping. In both cases, the returned name includes the file system
2974 mapping (i.e. fs0:\current-dir).
2976 Note that the current directory string should exclude the tailing backslash character.
2978 @param FileSystemMapping A pointer to the file system mapping. If NULL,
2979 then the current working directory is returned.
2981 @retval !=NULL The current directory.
2982 @retval NULL Current directory does not exist.
2987 IN CONST CHAR16
*FileSystemMapping OPTIONAL
2990 CHAR16
*PathToReturn
;
2992 SHELL_MAP_LIST
*MapListItem
;
2993 if (!IsListEmpty(&gShellMapList
.Link
)) {
2995 // if parameter is NULL, use current
2997 if (FileSystemMapping
== NULL
) {
2998 return (EfiShellGetEnv(L
"cwd"));
3001 PathToReturn
= NULL
;
3002 MapListItem
= ShellCommandFindMapItem(FileSystemMapping
);
3003 if (MapListItem
!= NULL
) {
3004 ASSERT((PathToReturn
== NULL
&& Size
== 0) || (PathToReturn
!= NULL
));
3005 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->MapName
, 0);
3006 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
3009 return (AddBufferToFreeList(PathToReturn
));
3016 Changes the current directory on the specified device.
3018 If the FileSystem is NULL, and the directory Dir does not contain a file system's
3019 mapped name, this function changes the current working directory.
3021 If the FileSystem is NULL and the directory Dir contains a mapped name, then the
3022 current file system and the current directory on that file system are changed.
3024 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file
3027 If FileSystem is not NULL and Dir is not NULL, then this function changes the current
3028 directory on the specified file system.
3030 If the current working directory or the current working file system is changed then the
3031 %cwd% environment variable will be updated
3033 Note that the current directory string should exclude the tailing backslash character.
3035 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working
3036 directory is changed.
3037 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.
3039 @retval EFI_SUCCESS The operation was sucessful
3040 @retval EFI_NOT_FOUND The file system could not be found
3045 IN CONST CHAR16
*FileSystem OPTIONAL
,
3046 IN CONST CHAR16
*Dir
3050 SHELL_MAP_LIST
*MapListItem
;
3054 CHAR16
*DirectoryName
;
3061 DirectoryName
= NULL
;
3063 if ((FileSystem
== NULL
&& Dir
== NULL
) || Dir
== NULL
) {
3064 return (EFI_INVALID_PARAMETER
);
3067 if (IsListEmpty(&gShellMapList
.Link
)){
3068 return (EFI_NOT_FOUND
);
3071 DirectoryName
= StrnCatGrow(&DirectoryName
, NULL
, Dir
, 0);
3072 ASSERT(DirectoryName
!= NULL
);
3074 PathCleanUpDirectories(DirectoryName
);
3076 if (FileSystem
== NULL
) {
3078 // determine the file system mapping to use
3080 if (StrStr(DirectoryName
, L
":") != NULL
) {
3081 ASSERT(MapName
== NULL
);
3082 MapName
= StrnCatGrow(&MapName
, NULL
, DirectoryName
, (StrStr(DirectoryName
, L
":")-DirectoryName
+1));
3085 // find the file system mapping's entry in the list
3088 if (MapName
!= NULL
) {
3089 MapListItem
= ShellCommandFindMapItem(MapName
);
3092 // make that the current file system mapping
3094 if (MapListItem
!= NULL
) {
3095 gShellCurDir
= MapListItem
;
3098 MapListItem
= gShellCurDir
;
3101 if (MapListItem
== NULL
) {
3102 FreePool (DirectoryName
);
3103 SHELL_FREE_NON_NULL(MapName
);
3104 return (EFI_NOT_FOUND
);
3108 // now update the MapListItem's current directory
3110 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& DirectoryName
[StrLen(DirectoryName
) - 1] != L
':') {
3111 FreePool(MapListItem
->CurrentDirectoryPath
);
3112 MapListItem
->CurrentDirectoryPath
= NULL
;
3114 if (MapName
!= NULL
) {
3115 TempLen
= StrLen(MapName
);
3116 if (TempLen
!= StrLen(DirectoryName
)) {
3117 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3118 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
+StrLen(MapName
), 0);
3122 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3123 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
3125 if ((MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] == L
'\\') || (MapListItem
->CurrentDirectoryPath
== NULL
)) {
3126 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3127 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
3128 MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] = CHAR_NULL
;
3133 // cant have a mapping in the directory...
3135 if (StrStr(DirectoryName
, L
":") != NULL
) {
3136 FreePool (DirectoryName
);
3137 return (EFI_INVALID_PARAMETER
);
3140 // FileSystem != NULL
3142 MapListItem
= ShellCommandFindMapItem(FileSystem
);
3143 if (MapListItem
== NULL
) {
3144 FreePool (DirectoryName
);
3145 return (EFI_INVALID_PARAMETER
);
3147 // gShellCurDir = MapListItem;
3148 if (DirectoryName
!= NULL
) {
3150 // change current dir on that file system
3153 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
3154 FreePool(MapListItem
->CurrentDirectoryPath
);
3155 DEBUG_CODE(MapListItem
->CurrentDirectoryPath
= NULL
;);
3157 // ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
3158 // MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);
3159 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3160 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
3161 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3162 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
3163 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] == L
'\\') {
3164 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3165 MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] = CHAR_NULL
;
3169 FreePool (DirectoryName
);
3171 // if updated the current directory then update the environment variable
3173 if (MapListItem
== gShellCurDir
) {
3175 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
3176 StrnCatGrow(&TempString
, &Size
, MapListItem
->MapName
, 0);
3177 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
3178 StrnCatGrow(&TempString
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
3179 Status
= InternalEfiShellSetEnv(L
"cwd", TempString
, TRUE
);
3180 FreePool(TempString
);
3183 return(EFI_SUCCESS
);
3187 Return help information about a specific command.
3189 This function returns the help information for the specified command. The help text
3190 can be internal to the shell or can be from a UEFI Shell manual page.
3192 If Sections is specified, then each section name listed will be compared in a casesensitive
3193 manner, to the section names described in Appendix B. If the section exists,
3194 it will be appended to the returned help text. If the section does not exist, no
3195 information will be returned. If Sections is NULL, then all help text information
3196 available will be returned.
3198 @param Command Points to the NULL-terminated UEFI Shell command name.
3199 @param Sections Points to the NULL-terminated comma-delimited
3200 section names to return. If NULL, then all
3201 sections will be returned.
3202 @param HelpText On return, points to a callee-allocated buffer
3203 containing all specified help text.
3205 @retval EFI_SUCCESS The help text was returned.
3206 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
3208 @retval EFI_INVALID_PARAMETER HelpText is NULL
3209 @retval EFI_NOT_FOUND There is no help text available for Command.
3213 EfiShellGetHelpText(
3214 IN CONST CHAR16
*Command
,
3215 IN CONST CHAR16
*Sections OPTIONAL
,
3216 OUT CHAR16
**HelpText
3219 CONST CHAR16
*ManFileName
;
3223 ASSERT(HelpText
!= NULL
);
3226 ManFileName
= ShellCommandGetManFileNameHandler(Command
);
3228 if (ManFileName
!= NULL
) {
3229 return (ProcessManFile(ManFileName
, Command
, Sections
, NULL
, HelpText
));
3231 if ((StrLen(Command
)> 4)
3232 && (Command
[StrLen(Command
)-1] == L
'i' || Command
[StrLen(Command
)-1] == L
'I')
3233 && (Command
[StrLen(Command
)-2] == L
'f' || Command
[StrLen(Command
)-2] == L
'F')
3234 && (Command
[StrLen(Command
)-3] == L
'e' || Command
[StrLen(Command
)-3] == L
'E')
3235 && (Command
[StrLen(Command
)-4] == L
'.')
3237 FixCommand
= AllocateZeroPool(StrSize(Command
) - 4 * sizeof (CHAR16
));
3238 if (FixCommand
== NULL
) {
3239 return EFI_OUT_OF_RESOURCES
;
3242 StrnCpyS( FixCommand
,
3243 (StrSize(Command
) - 4 * sizeof (CHAR16
))/sizeof(CHAR16
),
3247 Status
= ProcessManFile(FixCommand
, FixCommand
, Sections
, NULL
, HelpText
);
3248 FreePool(FixCommand
);
3251 return (ProcessManFile(Command
, Command
, Sections
, NULL
, HelpText
));
3257 Gets the enable status of the page break output mode.
3259 User can use this function to determine current page break mode.
3261 @retval TRUE The page break output mode is enabled.
3262 @retval FALSE The page break output mode is disabled.
3266 EfiShellGetPageBreak(
3270 return(ShellInfoObject
.PageBreakEnabled
);
3274 Judges whether the active shell is the root shell.
3276 This function makes the user to know that whether the active Shell is the root shell.
3278 @retval TRUE The active Shell is the root Shell.
3279 @retval FALSE The active Shell is NOT the root Shell.
3283 EfiShellIsRootShell(
3287 return(ShellInfoObject
.RootShellInstance
);
3291 function to return a semi-colon delimeted list of all alias' in the current shell
3293 up to caller to free the memory.
3295 @retval NULL No alias' were found
3296 @retval NULL An error ocurred getting alias'
3297 @return !NULL a list of all alias'
3301 InternalEfiShellGetListAlias(
3307 CHAR16
*VariableName
;
3309 UINTN NameBufferSize
;
3313 NameBufferSize
= INIT_NAME_BUFFER_SIZE
;
3314 VariableName
= AllocateZeroPool(NameBufferSize
);
3318 if (VariableName
== NULL
) {
3322 VariableName
[0] = CHAR_NULL
;
3325 NameSize
= NameBufferSize
;
3326 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
3327 if (Status
== EFI_NOT_FOUND
){
3329 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
3330 NameBufferSize
= NameSize
> NameBufferSize
* 2 ? NameSize
: NameBufferSize
* 2;
3331 SHELL_FREE_NON_NULL(VariableName
);
3332 VariableName
= AllocateZeroPool(NameBufferSize
);
3333 if (VariableName
== NULL
) {
3334 Status
= EFI_OUT_OF_RESOURCES
;
3335 SHELL_FREE_NON_NULL(RetVal
);
3340 NameSize
= NameBufferSize
;
3341 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
3344 if (EFI_ERROR (Status
)) {
3345 SHELL_FREE_NON_NULL(RetVal
);
3350 if (CompareGuid(&Guid
, &gShellAliasGuid
)){
3351 ASSERT((RetVal
== NULL
&& RetSize
== 0) || (RetVal
!= NULL
));
3352 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, VariableName
, 0);
3353 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, L
";", 0);
3356 SHELL_FREE_NON_NULL(VariableName
);
3362 Convert a null-terminated unicode string, in-place, to all lowercase.
3365 @param Str The null-terminated string to be converted to all lowercase.
3367 @return The null-terminated string converted into all lowercase.
3376 for (Index
= 0; Str
[Index
] != L
'\0'; Index
++) {
3377 if (Str
[Index
] >= L
'A' && Str
[Index
] <= L
'Z') {
3378 Str
[Index
] -= (CHAR16
)(L
'A' - L
'a');
3385 This function returns the command associated with a alias or a list of all
3388 @param[in] Alias Points to the NULL-terminated shell alias.
3389 If this parameter is NULL, then all
3390 aliases will be returned in ReturnedData.
3391 @param[out] Volatile upon return of a single command if TRUE indicates
3392 this is stored in a volatile fashion. FALSE otherwise.
3394 @return If Alias is not NULL, it will return a pointer to
3395 the NULL-terminated command for that alias.
3396 If Alias is NULL, ReturnedData points to a ';'
3397 delimited list of alias (e.g.
3398 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.
3399 @retval NULL an error ocurred
3400 @retval NULL Alias was not a valid Alias
3405 IN CONST CHAR16
*Alias
,
3406 OUT BOOLEAN
*Volatile OPTIONAL
3416 // Convert to lowercase to make aliases case-insensitive
3417 if (Alias
!= NULL
) {
3418 AliasLower
= AllocateCopyPool (StrSize (Alias
), Alias
);
3419 if (AliasLower
== NULL
) {
3422 ToLower (AliasLower
);
3424 if (Volatile
== NULL
) {
3425 GetVariable2 (AliasLower
, &gShellAliasGuid
, (VOID
**)&AliasVal
, NULL
);
3426 FreePool(AliasLower
);
3427 return (AddBufferToFreeList(AliasVal
));
3431 Status
= gRT
->GetVariable(AliasLower
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
3432 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3433 RetVal
= AllocateZeroPool(RetSize
);
3434 Status
= gRT
->GetVariable(AliasLower
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
3436 if (EFI_ERROR(Status
)) {
3437 if (RetVal
!= NULL
) {
3440 FreePool(AliasLower
);
3443 if ((EFI_VARIABLE_NON_VOLATILE
& Attribs
) == EFI_VARIABLE_NON_VOLATILE
) {
3449 FreePool (AliasLower
);
3450 return (AddBufferToFreeList(RetVal
));
3452 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));
3456 Changes a shell command alias.
3458 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
3460 this function does not check for built in alias'.
3462 @param[in] Command Points to the NULL-terminated shell command or existing alias.
3463 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
3464 Command refers to an alias, that alias will be deleted.
3465 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
3466 Alias being set will be stored in a non-volatile fashion.
3468 @retval EFI_SUCCESS Alias created or deleted successfully.
3469 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
3474 IN CONST CHAR16
*Command
,
3475 IN CONST CHAR16
*Alias
,
3482 // Convert to lowercase to make aliases case-insensitive
3483 if (Alias
!= NULL
) {
3484 AliasLower
= AllocateCopyPool (StrSize (Alias
), Alias
);
3485 if (AliasLower
== NULL
) {
3486 return EFI_OUT_OF_RESOURCES
;
3488 ToLower (AliasLower
);
3494 // We must be trying to remove one if Alias is NULL
3496 if (Alias
== NULL
) {
3498 // remove an alias (but passed in COMMAND parameter)
3500 Status
= (gRT
->SetVariable((CHAR16
*)Command
, &gShellAliasGuid
, 0, 0, NULL
));
3503 // Add and replace are the same
3506 // We dont check the error return on purpose since the variable may not exist.
3507 gRT
->SetVariable((CHAR16
*)Command
, &gShellAliasGuid
, 0, 0, NULL
);
3509 Status
= (gRT
->SetVariable((CHAR16
*)Alias
, &gShellAliasGuid
, EFI_VARIABLE_BOOTSERVICE_ACCESS
|(Volatile
?0:EFI_VARIABLE_NON_VOLATILE
), StrSize(Command
), (VOID
*)Command
));
3512 if (Alias
!= NULL
) {
3513 FreePool (AliasLower
);
3519 Changes a shell command alias.
3521 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
3524 @param[in] Command Points to the NULL-terminated shell command or existing alias.
3525 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
3526 Command refers to an alias, that alias will be deleted.
3527 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
3528 FALSE and the alias already exists, then the existing alias is unchanged and
3529 EFI_ACCESS_DENIED is returned.
3530 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
3531 Alias being set will be stored in a non-volatile fashion.
3533 @retval EFI_SUCCESS Alias created or deleted successfully.
3534 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
3535 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
3537 @retval EFI_INVALID_PARAMETER Command is null or the empty string.
3542 IN CONST CHAR16
*Command
,
3543 IN CONST CHAR16
*Alias
,
3548 if (ShellCommandIsOnAliasList(Alias
==NULL
?Command
:Alias
)) {
3550 // cant set over a built in alias
3552 return (EFI_ACCESS_DENIED
);
3553 } else if (Command
== NULL
|| *Command
== CHAR_NULL
|| StrLen(Command
) == 0) {
3555 // Command is null or empty
3557 return (EFI_INVALID_PARAMETER
);
3558 } else if (EfiShellGetAlias(Command
, NULL
) != NULL
&& !Replace
) {
3560 // Alias already exists, Replace not set
3562 return (EFI_ACCESS_DENIED
);
3564 return (InternalSetAlias(Command
, Alias
, Volatile
));
3568 // Pure FILE_HANDLE operations are passed to FileHandleLib
3569 // these functions are indicated by the *
3570 EFI_SHELL_PROTOCOL mShellProtocol
= {
3576 EfiShellGetHelpText
,
3577 EfiShellGetDevicePathFromMap
,
3578 EfiShellGetMapFromDevicePath
,
3579 EfiShellGetDevicePathFromFilePath
,
3580 EfiShellGetFilePathFromDevicePath
,
3584 EfiShellOpenFileList
,
3585 EfiShellFreeFileList
,
3586 EfiShellRemoveDupInFileList
,
3587 EfiShellBatchIsActive
,
3588 EfiShellIsRootShell
,
3589 EfiShellEnablePageBreak
,
3590 EfiShellDisablePageBreak
,
3591 EfiShellGetPageBreak
,
3592 EfiShellGetDeviceName
,
3593 (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
, //*
3594 (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
, //*
3595 EfiShellOpenFileByName
,
3598 (EFI_SHELL_READ_FILE
)FileHandleRead
, //*
3599 (EFI_SHELL_WRITE_FILE
)FileHandleWrite
, //*
3600 (EFI_SHELL_DELETE_FILE
)FileHandleDelete
, //*
3601 EfiShellDeleteFileByName
,
3602 (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
, //*
3603 (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
, //*
3604 (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
, //*
3606 EfiShellFindFilesInDir
,
3607 (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
, //*
3609 EfiShellOpenRootByHandle
,
3611 SHELL_MAJOR_VERSION
,
3612 SHELL_MINOR_VERSION
,
3614 // New for UEFI Shell 2.1
3615 EfiShellRegisterGuidName
,
3616 EfiShellGetGuidName
,
3617 EfiShellGetGuidFromName
,
3622 Function to create and install on the current handle.
3624 Will overwrite any existing ShellProtocols in the system to be sure that
3625 the current shell is in control.
3627 This must be removed via calling CleanUpShellProtocol().
3629 @param[in, out] NewShell The pointer to the pointer to the structure
3632 @retval EFI_SUCCESS The operation was successful.
3633 @return An error from LocateHandle, CreateEvent, or other core function.
3637 CreatePopulateInstallShellProtocol (
3638 IN OUT EFI_SHELL_PROTOCOL
**NewShell
3644 UINTN HandleCounter
;
3645 SHELL_PROTOCOL_HANDLE_LIST
*OldProtocolNode
;
3646 EFI_SHELL_PROTOCOL
*OldShell
;
3648 if (NewShell
== NULL
) {
3649 return (EFI_INVALID_PARAMETER
);
3654 OldProtocolNode
= NULL
;
3655 InitializeListHead(&ShellInfoObject
.OldShellList
.Link
);
3658 // Initialize EfiShellProtocol object...
3660 Status
= gBS
->CreateEvent(0,
3664 &mShellProtocol
.ExecutionBreak
);
3665 if (EFI_ERROR(Status
)) {
3670 // Get the size of the buffer we need.
3672 Status
= gBS
->LocateHandle(ByProtocol
,
3673 &gEfiShellProtocolGuid
,
3677 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3679 // Allocate and recall with buffer of correct size
3681 Buffer
= AllocateZeroPool(BufferSize
);
3682 if (Buffer
== NULL
) {
3683 return (EFI_OUT_OF_RESOURCES
);
3685 Status
= gBS
->LocateHandle(ByProtocol
,
3686 &gEfiShellProtocolGuid
,
3690 if (EFI_ERROR(Status
)) {
3695 // now overwrite each of them, but save the info to restore when we end.
3697 for (HandleCounter
= 0 ; HandleCounter
< (BufferSize
/sizeof(EFI_HANDLE
)) ; HandleCounter
++) {
3698 Status
= gBS
->OpenProtocol(Buffer
[HandleCounter
],
3699 &gEfiShellProtocolGuid
,
3700 (VOID
**) &OldShell
,
3703 EFI_OPEN_PROTOCOL_GET_PROTOCOL
3705 if (!EFI_ERROR(Status
)) {
3706 OldProtocolNode
= AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST
));
3707 if (OldProtocolNode
== NULL
) {
3708 if (!IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)) {
3709 CleanUpShellProtocol (&mShellProtocol
);
3711 Status
= EFI_OUT_OF_RESOURCES
;
3715 // reinstall over the old one...
3717 OldProtocolNode
->Handle
= Buffer
[HandleCounter
];
3718 OldProtocolNode
->Interface
= OldShell
;
3719 Status
= gBS
->ReinstallProtocolInterface(
3720 OldProtocolNode
->Handle
,
3721 &gEfiShellProtocolGuid
,
3722 OldProtocolNode
->Interface
,
3723 (VOID
*)(&mShellProtocol
));
3724 if (!EFI_ERROR(Status
)) {
3726 // we reinstalled sucessfully. log this so we can reverse it later.
3730 // add to the list for subsequent...
3732 InsertTailList(&ShellInfoObject
.OldShellList
.Link
, &OldProtocolNode
->Link
);
3737 } else if (Status
== EFI_NOT_FOUND
) {
3738 ASSERT(IsListEmpty(&ShellInfoObject
.OldShellList
.Link
));
3740 // no one else published yet. just publish it ourselves.
3742 Status
= gBS
->InstallProtocolInterface (
3744 &gEfiShellProtocolGuid
,
3745 EFI_NATIVE_INTERFACE
,
3746 (VOID
*)(&mShellProtocol
));
3749 if (PcdGetBool(PcdShellSupportOldProtocols
)){
3750 ///@todo support ShellEnvironment2
3751 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?
3754 if (!EFI_ERROR(Status
)) {
3755 *NewShell
= &mShellProtocol
;
3761 Opposite of CreatePopulateInstallShellProtocol.
3763 Free all memory and restore the system to the state it was in before calling
3764 CreatePopulateInstallShellProtocol.
3766 @param[in, out] NewShell The pointer to the new shell protocol structure.
3768 @retval EFI_SUCCESS The operation was successful.
3771 CleanUpShellProtocol (
3772 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3775 SHELL_PROTOCOL_HANDLE_LIST
*Node2
;
3778 // if we need to restore old protocols...
3780 if (!IsListEmpty(&ShellInfoObject
.OldShellList
.Link
)) {
3781 for (Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*) GetFirstNode (&ShellInfoObject
.OldShellList
.Link
)
3782 ; !IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)
3783 ; Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*) GetFirstNode (&ShellInfoObject
.OldShellList
.Link
)
3785 RemoveEntryList (&Node2
->Link
);
3786 gBS
->ReinstallProtocolInterface (Node2
->Handle
, &gEfiShellProtocolGuid
, NewShell
, Node2
->Interface
);
3791 // no need to restore
3793 gBS
->UninstallProtocolInterface (gImageHandle
, &gEfiShellProtocolGuid
, NewShell
);
3799 Cleanup the shell environment.
3801 @param[in, out] NewShell The pointer to the new shell protocol structure.
3803 @retval EFI_SUCCESS The operation was successful.
3806 CleanUpShellEnvironment (
3807 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3811 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
3813 CleanUpShellProtocol (NewShell
);
3815 Status
= gBS
->CloseEvent(NewShell
->ExecutionBreak
);
3816 NewShell
->ExecutionBreak
= NULL
;
3818 Status
= gBS
->OpenProtocol(
3819 gST
->ConsoleInHandle
,
3820 &gEfiSimpleTextInputExProtocolGuid
,
3824 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
3826 if (!EFI_ERROR (Status
)) {
3827 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle1
);
3828 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle2
);
3829 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle3
);
3830 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle4
);
3831 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle1
);
3832 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle2
);
3833 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle3
);
3834 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle4
);
3840 Notification function for keystrokes.
3842 @param[in] KeyData The key that was pressed.
3844 @retval EFI_SUCCESS The operation was successful.
3848 NotificationFunction(
3849 IN EFI_KEY_DATA
*KeyData
3852 if ( ((KeyData
->Key
.UnicodeChar
== L
'c') &&
3853 (KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
) || KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
))) ||
3854 (KeyData
->Key
.UnicodeChar
== 3)
3856 if (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
== NULL
) {
3857 return (EFI_UNSUPPORTED
);
3859 return (gBS
->SignalEvent(ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
));
3860 } else if ((KeyData
->Key
.UnicodeChar
== L
's') &&
3861 (KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
) || KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
))
3863 ShellInfoObject
.HaltOutput
= TRUE
;
3865 return (EFI_SUCCESS
);
3869 Function to start monitoring for CTRL-C using SimpleTextInputEx. This
3870 feature's enabled state was not known when the shell initially launched.
3872 @retval EFI_SUCCESS The feature is enabled.
3873 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
3877 InernalEfiShellStartMonitor(
3881 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
3882 EFI_KEY_DATA KeyData
;
3885 Status
= gBS
->OpenProtocol(
3886 gST
->ConsoleInHandle
,
3887 &gEfiSimpleTextInputExProtocolGuid
,
3891 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
3892 if (EFI_ERROR(Status
)) {
3897 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
3898 ShellInfoObject
.HiiHandle
);
3899 return (EFI_SUCCESS
);
3902 if (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
== NULL
) {
3903 return (EFI_UNSUPPORTED
);
3906 KeyData
.KeyState
.KeyToggleState
= 0;
3907 KeyData
.Key
.ScanCode
= 0;
3908 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
3909 KeyData
.Key
.UnicodeChar
= L
'c';
3911 Status
= SimpleEx
->RegisterKeyNotify(
3914 NotificationFunction
,
3915 &ShellInfoObject
.CtrlCNotifyHandle1
);
3917 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
3918 if (!EFI_ERROR(Status
)) {
3919 Status
= SimpleEx
->RegisterKeyNotify(
3922 NotificationFunction
,
3923 &ShellInfoObject
.CtrlCNotifyHandle2
);
3925 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
3926 KeyData
.Key
.UnicodeChar
= 3;
3927 if (!EFI_ERROR(Status
)) {
3928 Status
= SimpleEx
->RegisterKeyNotify(
3931 NotificationFunction
,
3932 &ShellInfoObject
.CtrlCNotifyHandle3
);
3934 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
3935 if (!EFI_ERROR(Status
)) {
3936 Status
= SimpleEx
->RegisterKeyNotify(
3939 NotificationFunction
,
3940 &ShellInfoObject
.CtrlCNotifyHandle4
);