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 - 2017, 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.
53 InternalShellProtocolIsBlockIoPresent(
54 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
57 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
63 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
64 Status
= gBS
->LocateDevicePath(&gEfiBlockIoProtocolGuid
, &DevicePathCopy
, &Handle
);
66 if ((Handle
!= NULL
) && (!EFI_ERROR(Status
))) {
73 Internal worker to determine whether there is a file system somewhere
74 upon the device path specified.
76 @param[in] DevicePath The device path to test.
78 @retval TRUE gEfiSimpleFileSystemProtocolGuid was installed on a handle with this device path
79 @retval FALSE gEfiSimpleFileSystemProtocolGuid was not found.
82 InternalShellProtocolIsSimpleFileSystemPresent(
83 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
86 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
92 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
;
93 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
95 if ((Handle
!= NULL
) && (!EFI_ERROR(Status
))) {
102 Internal worker debug helper function to print out maps as they are added.
104 @param[in] Mapping string mapping that has been added
105 @param[in] DevicePath pointer to device path that has been mapped.
107 @retval EFI_SUCCESS the operation was successful.
108 @return other an error ocurred
114 InternalShellProtocolDebugPrintMessage (
115 IN CONST CHAR16
*Mapping
,
116 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
122 Status
= EFI_SUCCESS
;
125 if (Mapping
!= NULL
) {
126 DEBUG((EFI_D_INFO
, "Added new map item:\"%S\"\r\n", Mapping
));
128 Temp
= ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
129 DEBUG((EFI_D_INFO
, "DevicePath: %S\r\n", Temp
));
137 This function creates a mapping for a device path.
139 If both DeviecPath and Mapping are NULL, this will reset the mapping to default values.
141 @param DevicePath Points to the device path. If this is NULL and Mapping points to a valid mapping,
142 then the mapping will be deleted.
143 @param Mapping Points to the NULL-terminated mapping for the device path. Must end with a ':'
145 @retval EFI_SUCCESS Mapping created or deleted successfully.
146 @retval EFI_NO_MAPPING There is no handle that corresponds exactly to DevicePath. See the
147 boot service function LocateDevicePath().
148 @retval EFI_ACCESS_DENIED The mapping is a built-in alias.
149 @retval EFI_INVALID_PARAMETER Mapping was NULL
150 @retval EFI_INVALID_PARAMETER Mapping did not end with a ':'
151 @retval EFI_INVALID_PARAMETER DevicePath was not pointing at a device that had a SIMPLE_FILE_SYSTEM_PROTOCOL installed.
152 @retval EFI_NOT_FOUND There was no mapping found to delete
153 @retval EFI_OUT_OF_RESOURCES Memory allocation failed
158 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath OPTIONAL
,
159 IN CONST CHAR16
*Mapping
163 SHELL_MAP_LIST
*MapListNode
;
165 if (Mapping
== NULL
){
166 return (EFI_INVALID_PARAMETER
);
169 if (Mapping
[StrLen(Mapping
)-1] != ':') {
170 return (EFI_INVALID_PARAMETER
);
174 // Delete the mapping
176 if (DevicePath
== NULL
) {
177 if (IsListEmpty(&gShellMapList
.Link
)) {
178 return (EFI_NOT_FOUND
);
180 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
181 ; !IsNull(&gShellMapList
.Link
, &MapListNode
->Link
)
182 ; MapListNode
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListNode
->Link
)
184 if (StringNoCaseCompare(&MapListNode
->MapName
, &Mapping
) == 0) {
185 RemoveEntryList(&MapListNode
->Link
);
186 SHELL_FREE_NON_NULL(MapListNode
->DevicePath
);
187 SHELL_FREE_NON_NULL(MapListNode
->MapName
);
188 SHELL_FREE_NON_NULL(MapListNode
->CurrentDirectoryPath
);
189 FreePool(MapListNode
);
190 return (EFI_SUCCESS
);
195 // We didnt find one to delete
197 return (EFI_NOT_FOUND
);
201 // make sure this is a valid to add device path
203 ///@todo add BlockIo to this test...
204 if (!InternalShellProtocolIsSimpleFileSystemPresent(DevicePath
)
205 && !InternalShellProtocolIsBlockIoPresent(DevicePath
)) {
206 return (EFI_INVALID_PARAMETER
);
210 // First make sure there is no old mapping
212 Status
= EfiShellSetMap(NULL
, Mapping
);
213 if ((Status
!= EFI_SUCCESS
) && (Status
!= EFI_NOT_FOUND
)) {
218 // now add the new one.
220 Status
= ShellCommandAddMapItemAndUpdatePath(Mapping
, DevicePath
, 0, FALSE
);
226 Gets the device path from the mapping.
228 This function gets the device path associated with a mapping.
230 @param Mapping A pointer to the mapping
232 @retval !=NULL Pointer to the device path that corresponds to the
233 device mapping. The returned pointer does not need
235 @retval NULL There is no device path associated with the
238 CONST EFI_DEVICE_PATH_PROTOCOL
*
240 EfiShellGetDevicePathFromMap(
241 IN CONST CHAR16
*Mapping
244 SHELL_MAP_LIST
*MapListItem
;
251 StrnCatGrow(&NewName
, &Size
, Mapping
, 0);
252 if (Mapping
[StrLen(Mapping
)-1] != L
':') {
253 StrnCatGrow(&NewName
, &Size
, L
":", 0);
256 MapListItem
= ShellCommandFindMapItem(NewName
);
260 if (MapListItem
!= NULL
) {
261 return (MapListItem
->DevicePath
);
267 Gets the mapping(s) that most closely matches the device path.
269 This function gets the mapping which corresponds to the device path *DevicePath. If
270 there is no exact match, then the mapping which most closely matches *DevicePath
271 is returned, and *DevicePath is updated to point to the remaining portion of the
272 device path. If there is an exact match, the mapping is returned and *DevicePath
273 points to the end-of-device-path node.
275 If there are multiple map names they will be semi-colon seperated in the
276 NULL-terminated string.
278 @param DevicePath On entry, points to a device path pointer. On
279 exit, updates the pointer to point to the
280 portion of the device path after the mapping.
282 @retval NULL No mapping was found.
283 @return !=NULL Pointer to NULL-terminated mapping. The buffer
284 is callee allocated and should be freed by the caller.
288 EfiShellGetMapFromDevicePath(
289 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
292 SHELL_MAP_LIST
*Node
;
293 CHAR16
*PathForReturn
;
295 // EFI_HANDLE PathHandle;
296 // EFI_HANDLE MapHandle;
297 // EFI_STATUS Status;
298 // EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
299 // EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
301 if (DevicePath
== NULL
|| *DevicePath
== NULL
) {
305 PathForReturn
= NULL
;
308 for ( Node
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
309 ; !IsNull(&gShellMapList
.Link
, &Node
->Link
)
310 ; Node
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &Node
->Link
)
313 // check for exact match
315 if (DevicePathCompare(DevicePath
, &Node
->DevicePath
) == 0) {
316 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
318 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, L
";", 0);
320 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, Node
->MapName
, 0);
323 if (PathForReturn
!= NULL
) {
324 while (!IsDevicePathEndType (*DevicePath
)) {
325 *DevicePath
= NextDevicePathNode (*DevicePath
);
327 SetDevicePathEndNode (*DevicePath
);
330 ///@todo finish code for inexact matches.
331 if (PathForReturn == NULL) {
334 DevicePathCopy = DuplicateDevicePath(*DevicePath);
335 ASSERT(DevicePathCopy != NULL);
336 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
337 ASSERT_EFI_ERROR(Status);
339 // check each of the device paths we have to get the root of the path for consist mappings
341 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
342 ; !IsNull(&gShellMapList.Link, &Node->Link)
343 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
345 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) == 0) {
348 MapPathCopy = DuplicateDevicePath(Node->DevicePath);
349 ASSERT(MapPathCopy != NULL);
350 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
351 if (MapHandle == PathHandle) {
353 *DevicePath = DevicePathCopy;
356 DevicePathCopy = NULL;
357 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
358 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
363 // now add on the non-consistent mappings
365 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
366 ; !IsNull(&gShellMapList.Link, &Node->Link)
367 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
369 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) != 0) {
372 MapPathCopy = Node->DevicePath;
373 ASSERT(MapPathCopy != NULL);
374 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
375 if (MapHandle == PathHandle) {
376 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
377 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
384 return (AddBufferToFreeList(PathForReturn
));
388 Converts a device path to a file system-style path.
390 This function converts a device path to a file system path by replacing part, or all, of
391 the device path with the file-system mapping. If there are more than one application
392 file system mappings, the one that most closely matches Path will be used.
394 @param Path The pointer to the device path
396 @retval NULL the device path could not be found.
397 @return all The pointer of the NULL-terminated file path. The path
398 is callee-allocated and should be freed by the caller.
402 EfiShellGetFilePathFromDevicePath(
403 IN CONST EFI_DEVICE_PATH_PROTOCOL
*Path
406 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
407 EFI_DEVICE_PATH_PROTOCOL
*MapPathCopy
;
408 SHELL_MAP_LIST
*MapListItem
;
409 CHAR16
*PathForReturn
;
411 EFI_HANDLE PathHandle
;
412 EFI_HANDLE MapHandle
;
414 FILEPATH_DEVICE_PATH
*FilePath
;
415 FILEPATH_DEVICE_PATH
*AlignedNode
;
417 PathForReturn
= NULL
;
420 DevicePathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)Path
;
421 ASSERT(DevicePathCopy
!= NULL
);
422 if (DevicePathCopy
== NULL
) {
426 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &PathHandle
);
428 if (EFI_ERROR(Status
)) {
432 // check each of the device paths we have to get the root of the path
434 for ( MapListItem
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
435 ; !IsNull(&gShellMapList
.Link
, &MapListItem
->Link
)
436 ; MapListItem
= (SHELL_MAP_LIST
*)GetNextNode(&gShellMapList
.Link
, &MapListItem
->Link
)
438 MapPathCopy
= (EFI_DEVICE_PATH_PROTOCOL
*)MapListItem
->DevicePath
;
439 ASSERT(MapPathCopy
!= NULL
);
441 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &MapPathCopy
, &MapHandle
);
442 if (MapHandle
== PathHandle
) {
443 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
444 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, MapListItem
->MapName
, 0);
446 // go through all the remaining nodes in the device path
448 for ( FilePath
= (FILEPATH_DEVICE_PATH
*)DevicePathCopy
449 ; !IsDevicePathEnd (&FilePath
->Header
)
450 ; FilePath
= (FILEPATH_DEVICE_PATH
*)NextDevicePathNode (&FilePath
->Header
)
453 // If any node is not a file path node, then the conversion can not be completed
455 if ((DevicePathType(&FilePath
->Header
) != MEDIA_DEVICE_PATH
) ||
456 (DevicePathSubType(&FilePath
->Header
) != MEDIA_FILEPATH_DP
)) {
457 FreePool(PathForReturn
);
462 // append the path part onto the filepath.
464 ASSERT((PathForReturn
== NULL
&& PathSize
== 0) || (PathForReturn
!= NULL
));
466 AlignedNode
= AllocateCopyPool (DevicePathNodeLength(FilePath
), FilePath
);
467 if (AlignedNode
== NULL
) {
468 FreePool (PathForReturn
);
472 // File Path Device Path Nodes 'can optionally add a "\" separator to
473 // the beginning and/or the end of the Path Name string.'
474 // (UEFI Spec 2.4 section 9.3.6.4).
475 // If necessary, add a "\", but otherwise don't
476 // (This is specified in the above section, and also implied by the
477 // UEFI Shell spec section 3.7)
478 if ((PathSize
!= 0) &&
479 (PathForReturn
!= NULL
) &&
480 (PathForReturn
[PathSize
/ sizeof (CHAR16
) - 1] != L
'\\') &&
481 (AlignedNode
->PathName
[0] != L
'\\')) {
482 PathForReturn
= StrnCatGrow (&PathForReturn
, &PathSize
, L
"\\", 1);
485 PathForReturn
= StrnCatGrow(&PathForReturn
, &PathSize
, AlignedNode
->PathName
, 0);
486 FreePool(AlignedNode
);
487 } // for loop of remaining nodes
489 if (PathForReturn
!= NULL
) {
492 } // for loop of paths to check
493 return(PathForReturn
);
497 Converts a file system style name to a device path.
499 This function converts a file system style name to a device path, by replacing any
500 mapping references to the associated device path.
502 @param[in] Path The pointer to the path.
504 @return The pointer of the file path. The file path is callee
505 allocated and should be freed by the caller.
506 @retval NULL The path could not be found.
507 @retval NULL There was not enough available memory.
509 EFI_DEVICE_PATH_PROTOCOL
*
511 EfiShellGetDevicePathFromFilePath(
512 IN CONST CHAR16
*Path
519 CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
520 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopy
;
521 EFI_DEVICE_PATH_PROTOCOL
*DevicePathCopyForFree
;
522 EFI_DEVICE_PATH_PROTOCOL
*DevicePathForReturn
;
533 if (StrStr(Path
, L
":") == NULL
) {
534 Cwd
= EfiShellGetCurDir(NULL
);
538 Size
= StrSize(Cwd
) + StrSize(Path
);
539 NewPath
= AllocateZeroPool(Size
);
540 if (NewPath
== NULL
) {
543 StrCpyS(NewPath
, Size
/sizeof(CHAR16
), Cwd
);
544 StrCatS(NewPath
, Size
/sizeof(CHAR16
), L
"\\");
545 if (*Path
== L
'\\') {
547 while (PathRemoveLastItem(NewPath
)) ;
549 StrCatS(NewPath
, Size
/sizeof(CHAR16
), Path
);
550 DevicePathForReturn
= EfiShellGetDevicePathFromFilePath(NewPath
);
552 return (DevicePathForReturn
);
557 // find the part before (but including) the : for the map name
559 ASSERT((MapName
== NULL
&& Size
== 0) || (MapName
!= NULL
));
560 MapName
= StrnCatGrow(&MapName
, &Size
, Path
, (StrStr(Path
, L
":")-Path
+1));
561 if (MapName
== NULL
|| MapName
[StrLen(MapName
)-1] != L
':') {
566 // look up the device path in the map
568 DevicePath
= EfiShellGetDevicePathFromMap(MapName
);
569 if (DevicePath
== NULL
) {
571 // Must have been a bad Mapname
577 // make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
579 DevicePathCopyForFree
= DevicePathCopy
= DuplicateDevicePath(DevicePath
);
580 if (DevicePathCopy
== NULL
) {
589 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
, &DevicePathCopy
, &Handle
);
590 if (EFI_ERROR(Status
)) {
591 if (DevicePathCopyForFree
!= NULL
) {
592 FreePool(DevicePathCopyForFree
);
599 // build the full device path
601 if ((*(Path
+StrLen(MapName
)) != CHAR_NULL
) &&
602 (*(Path
+StrLen(MapName
)+1) == CHAR_NULL
)) {
603 DevicePathForReturn
= FileDevicePath(Handle
, L
"\\");
605 DevicePathForReturn
= FileDevicePath(Handle
, Path
+StrLen(MapName
));
609 if (DevicePathCopyForFree
!= NULL
) {
610 FreePool(DevicePathCopyForFree
);
613 return (DevicePathForReturn
);
617 Gets the name of the device specified by the device handle.
619 This function gets the user-readable name of the device specified by the device
620 handle. If no user-readable name could be generated, then *BestDeviceName will be
621 NULL and EFI_NOT_FOUND will be returned.
623 If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
624 device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
627 If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
628 device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
629 If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
630 EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
631 EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
633 @param DeviceHandle The handle of the device.
634 @param Flags Determines the possible sources of component names.
636 EFI_DEVICE_NAME_USE_COMPONENT_NAME
637 EFI_DEVICE_NAME_USE_DEVICE_PATH
638 @param Language A pointer to the language specified for the device
639 name, in the same format as described in the UEFI
640 specification, Appendix M
641 @param BestDeviceName On return, points to the callee-allocated NULL-
642 terminated name of the device. If no device name
643 could be found, points to NULL. The name must be
644 freed by the caller...
646 @retval EFI_SUCCESS Get the name successfully.
647 @retval EFI_NOT_FOUND Fail to get the device name.
648 @retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
649 @retval EFI_INVALID_PARAMETER BestDeviceName was NULL
650 @retval EFI_INVALID_PARAMETER DeviceHandle was NULL
654 EfiShellGetDeviceName(
655 IN EFI_HANDLE DeviceHandle
,
656 IN EFI_SHELL_DEVICE_NAME_FLAGS Flags
,
658 OUT CHAR16
**BestDeviceName
662 EFI_COMPONENT_NAME2_PROTOCOL
*CompName2
;
663 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
664 EFI_HANDLE
*HandleList
;
667 CHAR16
*DeviceNameToReturn
;
669 UINTN ParentControllerCount
;
670 EFI_HANDLE
*ParentControllerBuffer
;
671 UINTN ParentDriverCount
;
672 EFI_HANDLE
*ParentDriverBuffer
;
674 if (BestDeviceName
== NULL
||
677 return (EFI_INVALID_PARAMETER
);
681 // make sure one of the 2 supported bits is on
683 if (((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) == 0) &&
684 ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) == 0)) {
685 return (EFI_INVALID_PARAMETER
);
688 DeviceNameToReturn
= NULL
;
689 *BestDeviceName
= NULL
;
694 if ((Flags
& EFI_DEVICE_NAME_USE_COMPONENT_NAME
) != 0) {
695 Status
= ParseHandleDatabaseByRelationship(
698 HR_DRIVER_BINDING_HANDLE
|HR_DEVICE_DRIVER
,
701 for (LoopVar
= 0; LoopVar
< HandleCount
; LoopVar
++){
703 // Go through those handles until we get one that passes for GetComponentName
705 Status
= gBS
->OpenProtocol(
707 &gEfiComponentName2ProtocolGuid
,
711 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
712 if (EFI_ERROR(Status
)) {
713 Status
= gBS
->OpenProtocol(
715 &gEfiComponentNameProtocolGuid
,
719 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
722 if (EFI_ERROR(Status
)) {
725 Lang
= GetBestLanguageForDriver(CompName2
->SupportedLanguages
, Language
, FALSE
);
726 Status
= CompName2
->GetControllerName(CompName2
, DeviceHandle
, NULL
, Lang
, &DeviceNameToReturn
);
729 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
733 if (HandleList
!= NULL
) {
734 FreePool(HandleList
);
738 // Now check the parent controller using this as the child.
740 if (DeviceNameToReturn
== NULL
){
741 PARSE_HANDLE_DATABASE_PARENTS(DeviceHandle
, &ParentControllerCount
, &ParentControllerBuffer
);
742 for (LoopVar
= 0 ; LoopVar
< ParentControllerCount
; LoopVar
++) {
743 PARSE_HANDLE_DATABASE_UEFI_DRIVERS(ParentControllerBuffer
[LoopVar
], &ParentDriverCount
, &ParentDriverBuffer
);
744 for (HandleCount
= 0 ; HandleCount
< ParentDriverCount
; HandleCount
++) {
746 // try using that driver's component name with controller and our driver as the child.
748 Status
= gBS
->OpenProtocol(
749 ParentDriverBuffer
[HandleCount
],
750 &gEfiComponentName2ProtocolGuid
,
754 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
755 if (EFI_ERROR(Status
)) {
756 Status
= gBS
->OpenProtocol(
757 ParentDriverBuffer
[HandleCount
],
758 &gEfiComponentNameProtocolGuid
,
762 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
765 if (EFI_ERROR(Status
)) {
768 Lang
= GetBestLanguageForDriver(CompName2
->SupportedLanguages
, Language
, FALSE
);
769 Status
= CompName2
->GetControllerName(CompName2
, ParentControllerBuffer
[LoopVar
], DeviceHandle
, Lang
, &DeviceNameToReturn
);
772 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
779 SHELL_FREE_NON_NULL(ParentDriverBuffer
);
780 if (!EFI_ERROR(Status
) && DeviceNameToReturn
!= NULL
) {
784 SHELL_FREE_NON_NULL(ParentControllerBuffer
);
787 // dont return on fail since we will try device path if that bit is on
789 if (DeviceNameToReturn
!= NULL
){
790 ASSERT(BestDeviceName
!= NULL
);
791 StrnCatGrow(BestDeviceName
, NULL
, DeviceNameToReturn
, 0);
792 return (EFI_SUCCESS
);
795 if ((Flags
& EFI_DEVICE_NAME_USE_DEVICE_PATH
) != 0) {
796 Status
= gBS
->OpenProtocol(
798 &gEfiDevicePathProtocolGuid
,
802 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
803 if (!EFI_ERROR(Status
)) {
805 // use device path to text on the device path
807 *BestDeviceName
= ConvertDevicePathToText(DevicePath
, TRUE
, TRUE
);
808 return (EFI_SUCCESS
);
812 // none of the selected bits worked.
814 return (EFI_NOT_FOUND
);
818 Opens the root directory of a device on a handle
820 This function opens the root directory of a device and returns a file handle to it.
822 @param DeviceHandle The handle of the device that contains the volume.
823 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
826 @retval EFI_SUCCESS Root opened successfully.
827 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
829 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
830 @retval EFI_DEVICE_ERROR The device had an error
834 EfiShellOpenRootByHandle(
835 IN EFI_HANDLE DeviceHandle
,
836 OUT SHELL_FILE_HANDLE
*FileHandle
840 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*SimpleFileSystem
;
841 EFI_FILE_PROTOCOL
*RealFileHandle
;
842 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
845 // get the simple file system interface
847 Status
= gBS
->OpenProtocol(DeviceHandle
,
848 &gEfiSimpleFileSystemProtocolGuid
,
849 (VOID
**)&SimpleFileSystem
,
852 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
853 if (EFI_ERROR(Status
)) {
854 return (EFI_NOT_FOUND
);
857 Status
= gBS
->OpenProtocol(DeviceHandle
,
858 &gEfiDevicePathProtocolGuid
,
862 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
863 if (EFI_ERROR(Status
)) {
864 return (EFI_NOT_FOUND
);
867 // Open the root volume now...
869 Status
= SimpleFileSystem
->OpenVolume(SimpleFileSystem
, &RealFileHandle
);
870 *FileHandle
= ConvertEfiFileProtocolToShellHandle(RealFileHandle
, EfiShellGetMapFromDevicePath(&DevPath
));
875 Opens the root directory of a device.
877 This function opens the root directory of a device and returns a file handle to it.
879 @param DevicePath Points to the device path corresponding to the device where the
880 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
881 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
884 @retval EFI_SUCCESS Root opened successfully.
885 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
887 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
888 @retval EFI_DEVICE_ERROR The device had an error
889 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
894 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
895 OUT SHELL_FILE_HANDLE
*FileHandle
901 if (FileHandle
== NULL
) {
902 return (EFI_INVALID_PARAMETER
);
906 // find the handle of the device with that device handle and the file system
909 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
912 if (EFI_ERROR(Status
)) {
913 return (EFI_NOT_FOUND
);
916 return (EfiShellOpenRootByHandle(Handle
, FileHandle
));
920 Returns whether any script files are currently being processed.
922 @retval TRUE There is at least one script file active.
923 @retval FALSE No script files are active now.
928 EfiShellBatchIsActive (
932 if (ShellCommandGetCurrentScriptFile() == NULL
) {
939 Worker function to open a file based on a device path. this will open the root
940 of the volume and then traverse down to the file itself.
942 @param DevicePath Device Path of the file.
943 @param FileHandle Pointer to the file upon a successful return.
944 @param OpenMode mode to open file in.
945 @param Attributes the File Attributes to use when creating a new file.
947 @retval EFI_SUCCESS the file is open and FileHandle is valid
948 @retval EFI_UNSUPPORTED the device path cotained non-path elements
949 @retval other an error ocurred.
952 InternalOpenFileDevicePath(
953 IN OUT EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
954 OUT SHELL_FILE_HANDLE
*FileHandle
,
956 IN UINT64 Attributes OPTIONAL
960 FILEPATH_DEVICE_PATH
*FilePathNode
;
962 SHELL_FILE_HANDLE ShellHandle
;
963 EFI_FILE_PROTOCOL
*Handle1
;
964 EFI_FILE_PROTOCOL
*Handle2
;
965 FILEPATH_DEVICE_PATH
*AlignedNode
;
967 if (FileHandle
== NULL
) {
968 return (EFI_INVALID_PARAMETER
);
978 Status
= EfiShellOpenRoot(DevicePath
, &ShellHandle
);
980 if (!EFI_ERROR(Status
)) {
981 Handle1
= ConvertShellHandleToEfiFileProtocol(ShellHandle
);
982 if (Handle1
!= NULL
) {
984 // chop off the begining part before the file system part...
987 Status
= gBS
->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid
,
990 if (!EFI_ERROR(Status
)) {
992 // To access as a file system, the file path should only
993 // contain file path components. Follow the file path nodes
994 // and find the target file
996 for ( FilePathNode
= (FILEPATH_DEVICE_PATH
*)DevicePath
997 ; !IsDevicePathEnd (&FilePathNode
->Header
)
998 ; FilePathNode
= (FILEPATH_DEVICE_PATH
*) NextDevicePathNode (&FilePathNode
->Header
)
1000 SHELL_FREE_NON_NULL(AlignedNode
);
1001 AlignedNode
= AllocateCopyPool (DevicePathNodeLength(FilePathNode
), FilePathNode
);
1003 // For file system access each node should be a file path component
1005 if (DevicePathType (&FilePathNode
->Header
) != MEDIA_DEVICE_PATH
||
1006 DevicePathSubType (&FilePathNode
->Header
) != MEDIA_FILEPATH_DP
1008 Status
= EFI_UNSUPPORTED
;
1013 // Open this file path node
1019 // if this is the last node in the DevicePath always create (if that was requested).
1021 if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode
->Header
)))) {
1022 Status
= Handle2
->Open (
1025 AlignedNode
->PathName
,
1032 // This is not the last node and we dont want to 'create' existing
1033 // directory entries...
1037 // open without letting it create
1038 // prevents error on existing files/directories
1040 Status
= Handle2
->Open (
1043 AlignedNode
->PathName
,
1044 OpenMode
&~EFI_FILE_MODE_CREATE
,
1048 // if above failed now open and create the 'item'
1049 // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)
1051 if ((EFI_ERROR (Status
)) && ((OpenMode
& EFI_FILE_MODE_CREATE
) != 0)) {
1052 Status
= Handle2
->Open (
1055 AlignedNode
->PathName
,
1062 // Close the last node
1064 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Handle2
);
1067 // If there's been an error, stop
1069 if (EFI_ERROR (Status
)) {
1076 SHELL_FREE_NON_NULL(AlignedNode
);
1077 if (EFI_ERROR(Status
)) {
1078 if (Handle1
!= NULL
) {
1079 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(Handle1
);
1082 *FileHandle
= ConvertEfiFileProtocolToShellHandle(Handle1
, ShellFileHandleGetPath(ShellHandle
));
1088 Creates a file or directory by name.
1090 This function creates an empty new file or directory with the specified attributes and
1091 returns the new file's handle. If the file already exists and is read-only, then
1092 EFI_INVALID_PARAMETER will be returned.
1094 If the file already existed, it is truncated and its attributes updated. If the file is
1095 created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.
1097 If the file name begins with >v, then the file handle which is returned refers to the
1098 shell environment variable with the specified name. If the shell environment variable
1099 already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.
1101 @param FileName Pointer to NULL-terminated file path
1102 @param FileAttribs The new file's attrbiutes. the different attributes are
1103 described in EFI_FILE_PROTOCOL.Open().
1104 @param FileHandle On return, points to the created file handle or directory's handle
1106 @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.
1107 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1108 @retval EFI_UNSUPPORTED could not open the file path
1109 @retval EFI_NOT_FOUND the specified file could not be found on the devide, or could not
1110 file the file system on the device.
1111 @retval EFI_NO_MEDIA the device has no medium.
1112 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1114 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1116 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1117 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1118 when the media is write-protected.
1119 @retval EFI_ACCESS_DENIED The service denied access to the file.
1120 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
1121 @retval EFI_VOLUME_FULL The volume is full.
1126 IN CONST CHAR16
*FileName
,
1127 IN UINT64 FileAttribs
,
1128 OUT SHELL_FILE_HANDLE
*FileHandle
1131 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1136 // Is this for an environment variable
1137 // do we start with >v
1139 if (StrStr(FileName
, L
">v") == FileName
) {
1140 Status
= IsVolatileEnv (FileName
+ 2, &Volatile
);
1141 if (EFI_ERROR (Status
)) {
1145 return (EFI_INVALID_PARAMETER
);
1147 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1148 return (EFI_SUCCESS
);
1152 // We are opening a regular file.
1154 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1155 if (DevicePath
== NULL
) {
1156 return (EFI_NOT_FOUND
);
1159 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, FileAttribs
);
1160 FreePool(DevicePath
);
1166 Register a GUID and a localized human readable name for it.
1168 If Guid is not assigned a name, then assign GuidName to Guid. This list of GUID
1169 names must be used whenever a shell command outputs GUID information.
1171 This function is only available when the major and minor versions in the
1172 EfiShellProtocol are greater than or equal to 2 and 1, respectively.
1174 @param[in] Guid A pointer to the GUID being registered.
1175 @param[in] GuidName A pointer to the localized name for the GUID being registered.
1177 @retval EFI_SUCCESS The operation was successful.
1178 @retval EFI_INVALID_PARAMETER Guid was NULL.
1179 @retval EFI_INVALID_PARAMETER GuidName was NULL.
1180 @retval EFI_ACCESS_DENIED Guid already is assigned a name.
1184 EfiShellRegisterGuidName(
1185 IN CONST EFI_GUID
*Guid
,
1186 IN CONST CHAR16
*GuidName
1189 return (AddNewGuidNameMapping(Guid
, GuidName
, NULL
));
1193 Opens a file or a directory by file name.
1195 This function opens the specified file in the specified OpenMode and returns a file
1197 If the file name begins with >v, then the file handle which is returned refers to the
1198 shell environment variable with the specified name. If the shell environment variable
1199 exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then
1200 EFI_INVALID_PARAMETER is returned.
1202 If the file name is >i, then the file handle which is returned refers to the standard
1203 input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER
1206 If the file name is >o, then the file handle which is returned refers to the standard
1207 output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1210 If the file name is >e, then the file handle which is returned refers to the standard
1211 error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1214 If the file name is NUL, then the file handle that is returned refers to the standard NUL
1215 file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is
1218 If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the
1221 @param FileName Points to the NULL-terminated UCS-2 encoded file name.
1222 @param FileHandle On return, points to the file handle.
1223 @param OpenMode File open mode. Either EFI_FILE_MODE_READ or
1224 EFI_FILE_MODE_WRITE from section 12.4 of the UEFI
1226 @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.
1227 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.
1228 @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.
1229 @retval EFI_NOT_FOUND The specified file could not be found on the device or the file
1230 system could not be found on the device. FileHandle is NULL.
1231 @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.
1232 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1233 longer supported. FileHandle is NULL.
1234 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1235 the FileName. FileHandle is NULL.
1236 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.
1237 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1238 when the media is write-protected. FileHandle is NULL.
1239 @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.
1240 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle
1242 @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.
1246 EfiShellOpenFileByName(
1247 IN CONST CHAR16
*FileName
,
1248 OUT SHELL_FILE_HANDLE
*FileHandle
,
1252 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1259 // Is this for StdIn
1261 if (StrCmp(FileName
, L
">i") == 0) {
1263 // make sure not writing to StdIn
1265 if ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0) {
1266 return (EFI_INVALID_PARAMETER
);
1268 *FileHandle
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1269 ASSERT(*FileHandle
!= NULL
);
1270 return (EFI_SUCCESS
);
1274 // Is this for StdOut
1276 if (StrCmp(FileName
, L
">o") == 0) {
1278 // make sure not writing to StdIn
1280 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1281 return (EFI_INVALID_PARAMETER
);
1283 *FileHandle
= &FileInterfaceStdOut
;
1284 return (EFI_SUCCESS
);
1288 // Is this for NUL / NULL file
1290 if ((gUnicodeCollation
->StriColl (gUnicodeCollation
, (CHAR16
*)FileName
, L
"NUL") == 0) ||
1291 (gUnicodeCollation
->StriColl (gUnicodeCollation
, (CHAR16
*)FileName
, L
"NULL") == 0)) {
1292 *FileHandle
= &FileInterfaceNulFile
;
1293 return (EFI_SUCCESS
);
1297 // Is this for StdErr
1299 if (StrCmp(FileName
, L
">e") == 0) {
1301 // make sure not writing to StdIn
1303 if ((OpenMode
& EFI_FILE_MODE_READ
) != 0) {
1304 return (EFI_INVALID_PARAMETER
);
1306 *FileHandle
= &FileInterfaceStdErr
;
1307 return (EFI_SUCCESS
);
1311 // Is this for an environment variable
1312 // do we start with >v
1314 if (StrStr(FileName
, L
">v") == FileName
) {
1315 Status
= IsVolatileEnv (FileName
+ 2, &Volatile
);
1316 if (EFI_ERROR (Status
)) {
1320 ((OpenMode
& EFI_FILE_MODE_WRITE
) != 0)) {
1321 return (EFI_INVALID_PARAMETER
);
1323 *FileHandle
= CreateFileInterfaceEnv(FileName
+2);
1324 return (EFI_SUCCESS
);
1328 // We are opening a regular file.
1330 DevicePath
= EfiShellGetDevicePathFromFilePath(FileName
);
1331 // DEBUG_CODE(InternalShellProtocolDebugPrintMessage (NULL, DevicePath););
1332 if (DevicePath
== NULL
) {
1333 return (EFI_NOT_FOUND
);
1337 // Copy the device path, open the file, then free the memory
1339 Status
= InternalOpenFileDevicePath(DevicePath
, FileHandle
, OpenMode
, 0); // 0 = no specific file attributes
1340 FreePool(DevicePath
);
1346 Deletes the file specified by the file name.
1348 This function deletes a file.
1350 @param FileName Points to the NULL-terminated file name.
1352 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.
1353 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
1354 @sa EfiShellCreateFile
1358 EfiShellDeleteFileByName(
1359 IN CONST CHAR16
*FileName
1362 SHELL_FILE_HANDLE FileHandle
;
1368 // get a handle to the file
1370 Status
= EfiShellCreateFile(FileName
,
1373 if (EFI_ERROR(Status
)) {
1377 // now delete the file
1379 ShellFileHandleRemove(FileHandle
);
1380 return (ShellInfoObject
.NewEfiShellProtocol
->DeleteFile(FileHandle
));
1384 Disables the page break output mode.
1388 EfiShellDisablePageBreak (
1392 ShellInfoObject
.PageBreakEnabled
= FALSE
;
1396 Enables the page break output mode.
1400 EfiShellEnablePageBreak (
1404 ShellInfoObject
.PageBreakEnabled
= TRUE
;
1408 internal worker function to load and run an image via device path.
1410 @param ParentImageHandle A handle of the image that is executing the specified
1412 @param DevicePath device path of the file to execute
1413 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1414 containing the command line. If NULL then the command-
1416 @param Environment Points to a NULL-terminated array of environment
1417 variables with the format 'x=y', where x is the
1418 environment variable name and y is the value. If this
1419 is NULL, then the current shell environment is used.
1421 @param[out] StartImageStatus Returned status from gBS->StartImage.
1423 @retval EFI_SUCCESS The command executed successfully. The status code
1424 returned by the command is pointed to by StatusCode.
1425 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1426 @retval EFI_OUT_OF_RESOURCES Out of resources.
1427 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1430 InternalShellExecuteDevicePath(
1431 IN CONST EFI_HANDLE
*ParentImageHandle
,
1432 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1433 IN CONST CHAR16
*CommandLine OPTIONAL
,
1434 IN CONST CHAR16
**Environment OPTIONAL
,
1435 OUT EFI_STATUS
*StartImageStatus OPTIONAL
1439 EFI_STATUS StartStatus
;
1440 EFI_STATUS CleanupStatus
;
1441 EFI_HANDLE NewHandle
;
1442 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
1443 LIST_ENTRY OrigEnvs
;
1444 EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol
;
1450 if (ParentImageHandle
== NULL
) {
1451 return (EFI_INVALID_PARAMETER
);
1454 InitializeListHead(&OrigEnvs
);
1455 ZeroMem(&ShellParamsProtocol
, sizeof(EFI_SHELL_PARAMETERS_PROTOCOL
));
1459 NewCmdLine
= AllocateCopyPool (StrSize (CommandLine
), CommandLine
);
1460 if (NewCmdLine
== NULL
) {
1461 return EFI_OUT_OF_RESOURCES
;
1464 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
1465 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
1466 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
1471 // Load the image with:
1472 // FALSE - not from boot manager and NULL, 0 being not already in memory
1474 Status
= gBS
->LoadImage(
1477 (EFI_DEVICE_PATH_PROTOCOL
*)DevicePath
,
1482 if (EFI_ERROR(Status
)) {
1483 if (NewHandle
!= NULL
) {
1484 gBS
->UnloadImage(NewHandle
);
1486 FreePool (NewCmdLine
);
1489 Status
= gBS
->OpenProtocol(
1491 &gEfiLoadedImageProtocolGuid
,
1492 (VOID
**)&LoadedImage
,
1495 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
1497 if (!EFI_ERROR(Status
)) {
1499 // If the image is not an app abort it.
1501 if (LoadedImage
->ImageCodeType
!= EfiLoaderCode
){
1506 STRING_TOKEN (STR_SHELL_IMAGE_NOT_APP
),
1507 ShellInfoObject
.HiiHandle
1512 ASSERT(LoadedImage
->LoadOptionsSize
== 0);
1513 if (NewCmdLine
!= NULL
) {
1514 LoadedImage
->LoadOptionsSize
= (UINT32
)StrSize(NewCmdLine
);
1515 LoadedImage
->LoadOptions
= (VOID
*)NewCmdLine
;
1519 // Save our current environment settings for later restoration if necessary
1521 if (Environment
!= NULL
) {
1522 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1523 if (!EFI_ERROR(Status
)) {
1524 Status
= SetEnvironmentVariables(Environment
);
1529 // Initialize and install a shell parameters protocol on the image.
1531 ShellParamsProtocol
.StdIn
= ShellInfoObject
.NewShellParametersProtocol
->StdIn
;
1532 ShellParamsProtocol
.StdOut
= ShellInfoObject
.NewShellParametersProtocol
->StdOut
;
1533 ShellParamsProtocol
.StdErr
= ShellInfoObject
.NewShellParametersProtocol
->StdErr
;
1534 Status
= UpdateArgcArgv(&ShellParamsProtocol
, NewCmdLine
, Efi_Application
, NULL
, NULL
);
1535 ASSERT_EFI_ERROR(Status
);
1537 // Replace Argv[0] with the full path of the binary we're executing:
1538 // If the command line was "foo", the binary might be called "foo.efi".
1539 // "The first entry in [Argv] is always the full file path of the
1540 // executable" - UEFI Shell Spec section 2.3
1542 ImagePath
= EfiShellGetFilePathFromDevicePath (DevicePath
);
1543 // The image we're executing isn't necessarily in a filesystem - it might
1544 // be memory mapped. In this case EfiShellGetFilePathFromDevicePath will
1545 // return NULL, and we'll leave Argv[0] as UpdateArgcArgv set it.
1546 if (ImagePath
!= NULL
) {
1547 if (ShellParamsProtocol
.Argv
== NULL
) {
1548 // Command line was empty or null.
1549 // (UpdateArgcArgv sets Argv to NULL when CommandLine is "" or NULL)
1550 ShellParamsProtocol
.Argv
= AllocatePool (sizeof (CHAR16
*));
1551 if (ShellParamsProtocol
.Argv
== NULL
) {
1552 Status
= EFI_OUT_OF_RESOURCES
;
1555 ShellParamsProtocol
.Argc
= 1;
1557 // Free the string UpdateArgcArgv put in Argv[0];
1558 FreePool (ShellParamsProtocol
.Argv
[0]);
1560 ShellParamsProtocol
.Argv
[0] = ImagePath
;
1563 Status
= gBS
->InstallProtocolInterface(&NewHandle
, &gEfiShellParametersProtocolGuid
, EFI_NATIVE_INTERFACE
, &ShellParamsProtocol
);
1564 ASSERT_EFI_ERROR(Status
);
1566 ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)
1569 // now start the image and if the caller wanted the return code pass it to them...
1571 if (!EFI_ERROR(Status
)) {
1572 StartStatus
= gBS
->StartImage(
1577 if (StartImageStatus
!= NULL
) {
1578 *StartImageStatus
= StartStatus
;
1581 CleanupStatus
= gBS
->UninstallProtocolInterface(
1583 &gEfiShellParametersProtocolGuid
,
1584 &ShellParamsProtocol
1586 ASSERT_EFI_ERROR(CleanupStatus
);
1592 // Unload image - We should only get here if we didn't call StartImage
1593 gBS
->UnloadImage (NewHandle
);
1596 // Free Argv (Allocated in UpdateArgcArgv)
1597 if (ShellParamsProtocol
.Argv
!= NULL
) {
1598 for (Index
= 0; Index
< ShellParamsProtocol
.Argc
; Index
++) {
1599 if (ShellParamsProtocol
.Argv
[Index
] != NULL
) {
1600 FreePool (ShellParamsProtocol
.Argv
[Index
]);
1603 FreePool (ShellParamsProtocol
.Argv
);
1607 // Restore environment variables
1608 if (!IsListEmpty(&OrigEnvs
)) {
1609 CleanupStatus
= SetEnvironmentVariableList(&OrigEnvs
);
1610 ASSERT_EFI_ERROR (CleanupStatus
);
1613 FreePool (NewCmdLine
);
1619 internal worker function to load and run an image in the current shell.
1621 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1622 containing the command line. If NULL then the command-
1624 @param Environment Points to a NULL-terminated array of environment
1625 variables with the format 'x=y', where x is the
1626 environment variable name and y is the value. If this
1627 is NULL, then the current shell environment is used.
1629 @param[out] StartImageStatus Returned status from the command line.
1631 @retval EFI_SUCCESS The command executed successfully. The status code
1632 returned by the command is pointed to by StatusCode.
1633 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1634 @retval EFI_OUT_OF_RESOURCES Out of resources.
1635 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1638 InternalShellExecute(
1639 IN CONST CHAR16
*CommandLine OPTIONAL
,
1640 IN CONST CHAR16
**Environment OPTIONAL
,
1641 OUT EFI_STATUS
*StartImageStatus OPTIONAL
1645 EFI_STATUS CleanupStatus
;
1646 LIST_ENTRY OrigEnvs
;
1648 InitializeListHead(&OrigEnvs
);
1651 // Save our current environment settings for later restoration if necessary
1653 if (Environment
!= NULL
) {
1654 Status
= GetEnvironmentVariableList(&OrigEnvs
);
1655 if (!EFI_ERROR(Status
)) {
1656 Status
= SetEnvironmentVariables(Environment
);
1662 Status
= RunShellCommand(CommandLine
, StartImageStatus
);
1664 // Restore environment variables
1665 if (!IsListEmpty(&OrigEnvs
)) {
1666 CleanupStatus
= SetEnvironmentVariableList(&OrigEnvs
);
1667 ASSERT_EFI_ERROR (CleanupStatus
);
1674 Determine if the UEFI Shell is currently running with nesting enabled or disabled.
1676 @retval FALSE nesting is required
1677 @retval other nesting is enabled
1695 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
) {
1698 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(mNoNestingEnvVarName
, &TempSize
, Temp
);
1699 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1700 Temp
= AllocateZeroPool(TempSize
+ sizeof(CHAR16
));
1702 Status
= SHELL_GET_ENVIRONMENT_VARIABLE(mNoNestingEnvVarName
, &TempSize
, Temp
);
1705 Temp2
= StrnCatGrow(&Temp2
, NULL
, mNoNestingTrue
, 0);
1706 if (Temp
!= NULL
&& Temp2
!= NULL
&& StringNoCaseCompare(&Temp
, &Temp2
) == 0) {
1708 // Use the no nesting method.
1714 SHELL_FREE_NON_NULL(Temp
);
1715 SHELL_FREE_NON_NULL(Temp2
);
1720 Execute the command line.
1722 This function creates a nested instance of the shell and executes the specified
1723 command (CommandLine) with the specified environment (Environment). Upon return,
1724 the status code returned by the specified command is placed in StatusCode.
1726 If Environment is NULL, then the current environment is used and all changes made
1727 by the commands executed will be reflected in the current environment. If the
1728 Environment is non-NULL, then the changes made will be discarded.
1730 The CommandLine is executed from the current working directory on the current
1733 @param ParentImageHandle A handle of the image that is executing the specified
1735 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1736 containing the command line. If NULL then the command-
1738 @param Environment Points to a NULL-terminated array of environment
1739 variables with the format 'x=y', where x is the
1740 environment variable name and y is the value. If this
1741 is NULL, then the current shell environment is used.
1742 @param StatusCode Points to the status code returned by the CommandLine.
1744 @retval EFI_SUCCESS The command executed successfully. The status code
1745 returned by the command is pointed to by StatusCode.
1746 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1747 @retval EFI_OUT_OF_RESOURCES Out of resources.
1748 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1749 @retval EFI_UNSUPPORTED The support level required for this function is not present.
1751 @sa InternalShellExecuteDevicePath
1756 IN EFI_HANDLE
*ParentImageHandle
,
1757 IN CHAR16
*CommandLine OPTIONAL
,
1758 IN CHAR16
**Environment OPTIONAL
,
1759 OUT EFI_STATUS
*StatusCode OPTIONAL
1764 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1767 if ((PcdGet8(PcdShellSupportLevel
) < 1)) {
1768 return (EFI_UNSUPPORTED
);
1771 if (NestingEnabled()) {
1772 DevPath
= AppendDevicePath (ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
1775 Temp
= ConvertDevicePathToText(ShellInfoObject
.FileDevPath
, TRUE
, TRUE
);
1777 Temp
= ConvertDevicePathToText(ShellInfoObject
.ImageDevPath
, TRUE
, TRUE
);
1779 Temp
= ConvertDevicePathToText(DevPath
, TRUE
, TRUE
);
1785 ASSERT((Temp
== NULL
&& Size
== 0) || (Temp
!= NULL
));
1786 StrnCatGrow(&Temp
, &Size
, L
"Shell.efi -exit ", 0);
1787 StrnCatGrow(&Temp
, &Size
, CommandLine
, 0);
1789 Status
= InternalShellExecuteDevicePath(
1793 (CONST CHAR16
**)Environment
,
1797 // de-allocate and return
1802 Status
= InternalShellExecute(
1803 (CONST CHAR16
*)CommandLine
,
1804 (CONST CHAR16
**)Environment
,
1812 Utility cleanup function for EFI_SHELL_FILE_INFO objects.
1814 1) frees all pointers (non-NULL)
1815 2) Closes the SHELL_FILE_HANDLE
1817 @param FileListNode pointer to the list node to free
1820 InternalFreeShellFileInfoNode(
1821 IN EFI_SHELL_FILE_INFO
*FileListNode
1824 if (FileListNode
->Info
!= NULL
) {
1825 FreePool((VOID
*)FileListNode
->Info
);
1827 if (FileListNode
->FileName
!= NULL
) {
1828 FreePool((VOID
*)FileListNode
->FileName
);
1830 if (FileListNode
->FullName
!= NULL
) {
1831 FreePool((VOID
*)FileListNode
->FullName
);
1833 if (FileListNode
->Handle
!= NULL
) {
1834 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileListNode
->Handle
);
1836 FreePool(FileListNode
);
1839 Frees the file list.
1841 This function cleans up the file list and any related data structures. It has no
1842 impact on the files themselves.
1844 @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is
1845 defined in OpenFileList()
1847 @retval EFI_SUCCESS Free the file list successfully.
1848 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1852 EfiShellFreeFileList(
1853 IN EFI_SHELL_FILE_INFO
**FileList
1856 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1858 if (FileList
== NULL
|| *FileList
== NULL
) {
1859 return (EFI_INVALID_PARAMETER
);
1862 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1863 ; !IsListEmpty(&(*FileList
)->Link
)
1864 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1866 RemoveEntryList(&ShellFileListItem
->Link
);
1867 InternalFreeShellFileInfoNode(ShellFileListItem
);
1869 InternalFreeShellFileInfoNode(*FileList
);
1871 return(EFI_SUCCESS
);
1875 Deletes the duplicate file names files in the given file list.
1877 This function deletes the reduplicate files in the given file list.
1879 @param FileList A pointer to the first entry in the file list.
1881 @retval EFI_SUCCESS Always success.
1882 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1886 EfiShellRemoveDupInFileList(
1887 IN EFI_SHELL_FILE_INFO
**FileList
1890 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
1891 EFI_SHELL_FILE_INFO
*ShellFileListItem2
;
1892 EFI_SHELL_FILE_INFO
*TempNode
;
1894 if (FileList
== NULL
|| *FileList
== NULL
) {
1895 return (EFI_INVALID_PARAMETER
);
1897 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
1898 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1899 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1901 for ( ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
1902 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1903 ; ShellFileListItem2
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem2
->Link
)
1905 if (gUnicodeCollation
->StriColl(
1907 (CHAR16
*)ShellFileListItem
->FullName
,
1908 (CHAR16
*)ShellFileListItem2
->FullName
) == 0
1910 TempNode
= (EFI_SHELL_FILE_INFO
*)GetPreviousNode(
1912 &ShellFileListItem2
->Link
1914 RemoveEntryList(&ShellFileListItem2
->Link
);
1915 InternalFreeShellFileInfoNode(ShellFileListItem2
);
1916 // Set ShellFileListItem2 to PreviousNode so we don't access Freed
1917 // memory in GetNextNode in the loop expression above.
1918 ShellFileListItem2
= TempNode
;
1922 return (EFI_SUCCESS
);
1926 // This is the same structure as the external version, but it has no CONST qualifiers.
1929 LIST_ENTRY Link
; ///< Linked list members.
1930 EFI_STATUS Status
; ///< Status of opening the file. Valid only if Handle != NULL.
1931 CHAR16
*FullName
; ///< Fully qualified filename.
1932 CHAR16
*FileName
; ///< name of this file.
1933 SHELL_FILE_HANDLE Handle
; ///< Handle for interacting with the opened file or NULL if closed.
1934 EFI_FILE_INFO
*Info
; ///< Pointer to the FileInfo struct for this file or NULL.
1935 } EFI_SHELL_FILE_INFO_NO_CONST
;
1938 Allocates and duplicates a EFI_SHELL_FILE_INFO node.
1940 @param[in] Node The node to copy from.
1941 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.
1943 @retval NULL a memory allocation error ocurred
1944 @return != NULL a pointer to the new node
1946 EFI_SHELL_FILE_INFO
*
1947 InternalDuplicateShellFileInfo(
1948 IN EFI_SHELL_FILE_INFO
*Node
,
1952 EFI_SHELL_FILE_INFO_NO_CONST
*NewNode
;
1955 // try to confirm that the objects are in sync
1957 ASSERT(sizeof(EFI_SHELL_FILE_INFO_NO_CONST
) == sizeof(EFI_SHELL_FILE_INFO
));
1959 NewNode
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
1960 if (NewNode
== NULL
) {
1963 NewNode
->FullName
= AllocateCopyPool(StrSize(Node
->FullName
), Node
->FullName
);
1964 NewNode
->FileName
= AllocateCopyPool(StrSize(Node
->FileName
), Node
->FileName
);
1965 NewNode
->Info
= AllocateCopyPool((UINTN
)Node
->Info
->Size
, Node
->Info
);
1966 if ( NewNode
->FullName
== NULL
1967 || NewNode
->FileName
== NULL
1968 || NewNode
->Info
== NULL
1970 SHELL_FREE_NON_NULL(NewNode
->FullName
);
1971 SHELL_FREE_NON_NULL(NewNode
->FileName
);
1972 SHELL_FREE_NON_NULL(NewNode
->Info
);
1973 SHELL_FREE_NON_NULL(NewNode
);
1976 NewNode
->Status
= Node
->Status
;
1977 NewNode
->Handle
= Node
->Handle
;
1979 Node
->Handle
= NULL
;
1982 return((EFI_SHELL_FILE_INFO
*)NewNode
);
1986 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation
1987 failed it will return NULL.
1989 @param[in] BasePath the Path to prepend onto filename for FullPath
1990 @param[in] Status Status member initial value.
1991 @param[in] FileName FileName member initial value.
1992 @param[in] Handle Handle member initial value.
1993 @param[in] Info Info struct to copy.
1995 @retval NULL An error ocurred.
1996 @return a pointer to the newly allocated structure.
1998 EFI_SHELL_FILE_INFO
*
1999 CreateAndPopulateShellFileInfo(
2000 IN CONST CHAR16
*BasePath
,
2001 IN CONST EFI_STATUS Status
,
2002 IN CONST CHAR16
*FileName
,
2003 IN CONST SHELL_FILE_HANDLE Handle
,
2004 IN CONST EFI_FILE_INFO
*Info
2007 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2014 ShellFileListItem
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2015 if (ShellFileListItem
== NULL
) {
2018 if (Info
!= NULL
&& Info
->Size
!= 0) {
2019 ShellFileListItem
->Info
= AllocateZeroPool((UINTN
)Info
->Size
);
2020 if (ShellFileListItem
->Info
== NULL
) {
2021 FreePool(ShellFileListItem
);
2024 CopyMem(ShellFileListItem
->Info
, Info
, (UINTN
)Info
->Size
);
2026 ShellFileListItem
->Info
= NULL
;
2028 if (FileName
!= NULL
) {
2029 ASSERT(TempString
== NULL
);
2030 ShellFileListItem
->FileName
= StrnCatGrow(&TempString
, 0, FileName
, 0);
2031 if (ShellFileListItem
->FileName
== NULL
) {
2032 FreePool(ShellFileListItem
->Info
);
2033 FreePool(ShellFileListItem
);
2037 ShellFileListItem
->FileName
= NULL
;
2041 if (BasePath
!= NULL
) {
2042 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2043 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
2044 if (TempString
== NULL
) {
2045 FreePool((VOID
*)ShellFileListItem
->FileName
);
2046 SHELL_FREE_NON_NULL(ShellFileListItem
->Info
);
2047 FreePool(ShellFileListItem
);
2051 if (ShellFileListItem
->FileName
!= NULL
) {
2052 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
2053 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileListItem
->FileName
, 0);
2054 if (TempString
== NULL
) {
2055 FreePool((VOID
*)ShellFileListItem
->FileName
);
2056 FreePool(ShellFileListItem
->Info
);
2057 FreePool(ShellFileListItem
);
2062 TempString
= PathCleanUpDirectories(TempString
);
2064 ShellFileListItem
->FullName
= TempString
;
2065 ShellFileListItem
->Status
= Status
;
2066 ShellFileListItem
->Handle
= Handle
;
2068 return (ShellFileListItem
);
2072 Find all files in a specified directory.
2074 @param FileDirHandle Handle of the directory to search.
2075 @param FileList On return, points to the list of files in the directory
2076 or NULL if there are no files in the directory.
2078 @retval EFI_SUCCESS File information was returned successfully.
2079 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.
2080 @retval EFI_DEVICE_ERROR The device reported an error.
2081 @retval EFI_NO_MEDIA The device media is not present.
2082 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.
2083 @return An error from FileHandleGetFileName().
2087 EfiShellFindFilesInDir(
2088 IN SHELL_FILE_HANDLE FileDirHandle
,
2089 OUT EFI_SHELL_FILE_INFO
**FileList
2092 EFI_SHELL_FILE_INFO
*ShellFileList
;
2093 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2094 EFI_FILE_INFO
*FileInfo
;
2103 Status
= FileHandleGetFileName(FileDirHandle
, &BasePath
);
2104 if (EFI_ERROR(Status
)) {
2108 if (ShellFileHandleGetPath(FileDirHandle
) != NULL
) {
2111 TempString
= StrnCatGrow(&TempString
, &Size
, ShellFileHandleGetPath(FileDirHandle
), 0);
2112 if (TempString
== NULL
) {
2113 SHELL_FREE_NON_NULL(BasePath
);
2114 return (EFI_OUT_OF_RESOURCES
);
2116 TempSpot
= StrStr(TempString
, L
";");
2118 if (TempSpot
!= NULL
) {
2119 *TempSpot
= CHAR_NULL
;
2122 TempString
= StrnCatGrow(&TempString
, &Size
, BasePath
, 0);
2123 if (TempString
== NULL
) {
2124 SHELL_FREE_NON_NULL(BasePath
);
2125 return (EFI_OUT_OF_RESOURCES
);
2127 SHELL_FREE_NON_NULL(BasePath
);
2128 BasePath
= TempString
;
2132 ShellFileList
= NULL
;
2133 ShellFileListItem
= NULL
;
2135 Status
= EFI_SUCCESS
;
2138 for ( Status
= FileHandleFindFirstFile(FileDirHandle
, &FileInfo
)
2139 ; !EFI_ERROR(Status
) && !NoFile
2140 ; Status
= FileHandleFindNextFile(FileDirHandle
, FileInfo
, &NoFile
)
2142 if (ShellFileList
== NULL
) {
2143 ShellFileList
= (EFI_SHELL_FILE_INFO
*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2144 if (ShellFileList
== NULL
) {
2145 SHELL_FREE_NON_NULL (BasePath
);
2146 return EFI_OUT_OF_RESOURCES
;
2148 InitializeListHead(&ShellFileList
->Link
);
2151 // allocate a new EFI_SHELL_FILE_INFO and populate it...
2153 ShellFileListItem
= CreateAndPopulateShellFileInfo(
2155 EFI_SUCCESS
, // success since we didnt fail to open it...
2157 NULL
, // no handle since not open
2159 if (ShellFileListItem
== NULL
) {
2160 Status
= EFI_OUT_OF_RESOURCES
;
2162 // Free resources outside the loop.
2166 InsertTailList(&ShellFileList
->Link
, &ShellFileListItem
->Link
);
2168 if (EFI_ERROR(Status
)) {
2169 EfiShellFreeFileList(&ShellFileList
);
2172 *FileList
= ShellFileList
;
2174 SHELL_FREE_NON_NULL(BasePath
);
2179 Get the GUID value from a human readable name.
2181 If GuidName is a known GUID name, then update Guid to have the correct value for
2184 This function is only available when the major and minor versions in the
2185 EfiShellProtocol are greater than or equal to 2 and 1, respectively.
2187 @param[in] GuidName A pointer to the localized name for the GUID being queried.
2188 @param[out] Guid A pointer to the GUID structure to be filled in.
2190 @retval EFI_SUCCESS The operation was successful.
2191 @retval EFI_INVALID_PARAMETER Guid was NULL.
2192 @retval EFI_INVALID_PARAMETER GuidName was NULL.
2193 @retval EFI_NOT_FOUND GuidName is not a known GUID Name.
2197 EfiShellGetGuidFromName(
2198 IN CONST CHAR16
*GuidName
,
2205 if (Guid
== NULL
|| GuidName
== NULL
) {
2206 return (EFI_INVALID_PARAMETER
);
2209 Status
= GetGuidFromStringName(GuidName
, NULL
, &NewGuid
);
2211 if (!EFI_ERROR(Status
)) {
2212 CopyGuid(Guid
, NewGuid
);
2219 Get the human readable name for a GUID from the value.
2221 If Guid is assigned a name, then update *GuidName to point to the name. The callee
2222 should not modify the value.
2224 This function is only available when the major and minor versions in the
2225 EfiShellProtocol are greater than or equal to 2 and 1, respectively.
2227 @param[in] Guid A pointer to the GUID being queried.
2228 @param[out] GuidName A pointer to a pointer the localized to name for the GUID being requested
2230 @retval EFI_SUCCESS The operation was successful.
2231 @retval EFI_INVALID_PARAMETER Guid was NULL.
2232 @retval EFI_INVALID_PARAMETER GuidName was NULL.
2233 @retval EFI_NOT_FOUND Guid is not assigned a name.
2237 EfiShellGetGuidName(
2238 IN CONST EFI_GUID
*Guid
,
2239 OUT CONST CHAR16
**GuidName
2244 if (Guid
== NULL
|| GuidName
== NULL
) {
2245 return (EFI_INVALID_PARAMETER
);
2248 Name
= GetStringNameFromGuid(Guid
, NULL
);
2249 if (Name
== NULL
|| StrLen(Name
) == 0) {
2250 SHELL_FREE_NON_NULL(Name
);
2251 return (EFI_NOT_FOUND
);
2254 *GuidName
= AddBufferToFreeList(Name
);
2256 return (EFI_SUCCESS
);
2260 Updates a file name to be preceeded by the mapped drive name
2262 @param[in] BasePath the Mapped drive name to prepend
2263 @param[in, out] Path pointer to pointer to the file name to update.
2266 @retval EFI_OUT_OF_RESOURCES
2270 IN CONST CHAR16
*BasePath
,
2271 IN OUT CHAR16
**Path
2280 ASSERT(Path
!= NULL
);
2281 ASSERT(*Path
!= NULL
);
2282 ASSERT(BasePath
!= NULL
);
2285 // convert a local path to an absolute path
2287 if (StrStr(*Path
, L
":") == NULL
) {
2288 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2289 StrnCatGrow(&Path2
, &Path2Size
, BasePath
, 0);
2290 if (Path2
== NULL
) {
2291 return (EFI_OUT_OF_RESOURCES
);
2293 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2294 StrnCatGrow(&Path2
, &Path2Size
, (*Path
)[0] == L
'\\'?(*Path
) + 1 :*Path
, 0);
2295 if (Path2
== NULL
) {
2296 return (EFI_OUT_OF_RESOURCES
);
2303 return (EFI_SUCCESS
);
2307 If FileHandle is a directory then the function reads from FileHandle and reads in
2308 each of the FileInfo structures. If one of them matches the Pattern's first
2309 "level" then it opens that handle and calls itself on that handle.
2311 If FileHandle is a file and matches all of the remaining Pattern (which would be
2312 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.
2314 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call
2315 FreeFileList with FileList.
2317 @param[in] FilePattern The FilePattern to check against.
2318 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure
2319 @param[in] FileHandle The FileHandle to start with
2320 @param[in, out] FileList pointer to pointer to list of found files.
2321 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.
2322 @param[in] MapName The file system name this file is on.
2324 @retval EFI_SUCCESS all files were found and the FileList contains a list.
2325 @retval EFI_NOT_FOUND no files were found
2326 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
2330 IN CONST CHAR16
*FilePattern
,
2331 IN EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
,
2332 IN SHELL_FILE_HANDLE FileHandle
,
2333 IN OUT EFI_SHELL_FILE_INFO
**FileList
,
2334 IN CONST EFI_SHELL_FILE_INFO
*ParentNode OPTIONAL
,
2335 IN CONST CHAR16
*MapName
2339 CONST CHAR16
*NextFilePatternStart
;
2340 CHAR16
*CurrentFilePattern
;
2341 EFI_SHELL_FILE_INFO
*ShellInfo
;
2342 EFI_SHELL_FILE_INFO
*ShellInfoNode
;
2343 EFI_SHELL_FILE_INFO
*NewShellNode
;
2344 EFI_FILE_INFO
*FileInfo
;
2346 CHAR16
*NewFullName
;
2349 if ( FilePattern
== NULL
2350 || UnicodeCollation
== NULL
2353 return (EFI_INVALID_PARAMETER
);
2356 CurrentFilePattern
= NULL
;
2358 if (*FilePattern
== L
'\\') {
2362 for( NextFilePatternStart
= FilePattern
2363 ; *NextFilePatternStart
!= CHAR_NULL
&& *NextFilePatternStart
!= L
'\\'
2364 ; NextFilePatternStart
++);
2366 CurrentFilePattern
= AllocateZeroPool((NextFilePatternStart
-FilePattern
+1)*sizeof(CHAR16
));
2367 if (CurrentFilePattern
== NULL
) {
2368 return EFI_OUT_OF_RESOURCES
;
2371 StrnCpyS(CurrentFilePattern
, NextFilePatternStart
-FilePattern
+1, FilePattern
, NextFilePatternStart
-FilePattern
);
2373 if (CurrentFilePattern
[0] == CHAR_NULL
2374 &&NextFilePatternStart
[0] == CHAR_NULL
2377 // we want the parent or root node (if no parent)
2379 if (ParentNode
== NULL
) {
2381 // We want the root node. create the node.
2383 FileInfo
= FileHandleGetInfo(FileHandle
);
2384 NewShellNode
= CreateAndPopulateShellFileInfo(
2391 SHELL_FREE_NON_NULL(FileInfo
);
2394 // Add the current parameter FileHandle to the list, then end...
2396 NewShellNode
= InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO
*)ParentNode
, TRUE
);
2398 if (NewShellNode
== NULL
) {
2399 Status
= EFI_OUT_OF_RESOURCES
;
2401 NewShellNode
->Handle
= NULL
;
2402 if (*FileList
== NULL
) {
2403 *FileList
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2404 InitializeListHead(&((*FileList
)->Link
));
2408 // Add to the returning to use list
2410 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2412 Status
= EFI_SUCCESS
;
2415 Status
= EfiShellFindFilesInDir(FileHandle
, &ShellInfo
);
2417 if (!EFI_ERROR(Status
)){
2418 if (StrStr(NextFilePatternStart
, L
"\\") != NULL
){
2423 for ( ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&ShellInfo
->Link
)
2424 ; !IsNull (&ShellInfo
->Link
, &ShellInfoNode
->Link
)
2425 ; ShellInfoNode
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&ShellInfo
->Link
, &ShellInfoNode
->Link
)
2427 if (UnicodeCollation
->MetaiMatch(UnicodeCollation
, (CHAR16
*)ShellInfoNode
->FileName
, CurrentFilePattern
)){
2428 if (ShellInfoNode
->FullName
!= NULL
&& StrStr(ShellInfoNode
->FullName
, L
":") == NULL
) {
2429 Size
= StrSize (ShellInfoNode
->FullName
) + StrSize (MapName
);
2430 NewFullName
= AllocateZeroPool(Size
);
2431 if (NewFullName
== NULL
) {
2432 Status
= EFI_OUT_OF_RESOURCES
;
2434 StrCpyS(NewFullName
, Size
/ sizeof(CHAR16
), MapName
);
2435 StrCatS(NewFullName
, Size
/ sizeof(CHAR16
), ShellInfoNode
->FullName
);
2436 FreePool ((VOID
*) ShellInfoNode
->FullName
);
2437 ShellInfoNode
->FullName
= NewFullName
;
2440 if (Directory
&& !EFI_ERROR(Status
) && ShellInfoNode
->FullName
!= NULL
&& ShellInfoNode
->FileName
!= NULL
){
2442 // should be a directory
2446 // don't open the . and .. directories
2448 if ( (StrCmp(ShellInfoNode
->FileName
, L
".") != 0)
2449 && (StrCmp(ShellInfoNode
->FileName
, L
"..") != 0)
2454 if (EFI_ERROR(Status
)) {
2458 // Open the directory since we need that handle in the next recursion.
2460 ShellInfoNode
->Status
= EfiShellOpenFileByName (ShellInfoNode
->FullName
, &ShellInfoNode
->Handle
, EFI_FILE_MODE_READ
);
2463 // recurse with the next part of the pattern
2465 Status
= ShellSearchHandle(NextFilePatternStart
, UnicodeCollation
, ShellInfoNode
->Handle
, FileList
, ShellInfoNode
, MapName
);
2466 EfiShellClose(ShellInfoNode
->Handle
);
2467 ShellInfoNode
->Handle
= NULL
;
2469 } else if (!EFI_ERROR(Status
)) {
2475 // copy the information we need into a new Node
2477 NewShellNode
= InternalDuplicateShellFileInfo(ShellInfoNode
, FALSE
);
2478 if (NewShellNode
== NULL
) {
2479 Status
= EFI_OUT_OF_RESOURCES
;
2481 if (*FileList
== NULL
) {
2482 *FileList
= AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO
));
2483 InitializeListHead(&((*FileList
)->Link
));
2487 // Add to the returning to use list
2489 InsertTailList(&(*FileList
)->Link
, &NewShellNode
->Link
);
2492 if (EFI_ERROR(Status
)) {
2496 if (EFI_ERROR(Status
)) {
2497 EfiShellFreeFileList(&ShellInfo
);
2499 Status
= EfiShellFreeFileList(&ShellInfo
);
2504 if (*FileList
== NULL
|| (*FileList
!= NULL
&& IsListEmpty(&(*FileList
)->Link
))) {
2505 Status
= EFI_NOT_FOUND
;
2508 FreePool(CurrentFilePattern
);
2513 Find files that match a specified pattern.
2515 This function searches for all files and directories that match the specified
2516 FilePattern. The FilePattern can contain wild-card characters. The resulting file
2517 information is placed in the file list FileList.
2519 Wildcards are processed
2520 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.
2522 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo
2523 field is set to NULL.
2525 if *FileList is not NULL then it must be a pre-existing and properly initialized list.
2527 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.
2528 @param FileList On return, points to the start of a file list containing the names
2529 of all matching files or else points to NULL if no matching files
2530 were found. only on a EFI_SUCCESS return will; this be non-NULL.
2532 @retval EFI_SUCCESS Files found. FileList is a valid list.
2533 @retval EFI_NOT_FOUND No files found.
2534 @retval EFI_NO_MEDIA The device has no media
2535 @retval EFI_DEVICE_ERROR The device reported an error
2536 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted
2541 IN CONST CHAR16
*FilePattern
,
2542 OUT EFI_SHELL_FILE_INFO
**FileList
2546 CHAR16
*PatternCopy
;
2547 CHAR16
*PatternCurrentLocation
;
2548 EFI_DEVICE_PATH_PROTOCOL
*RootDevicePath
;
2549 SHELL_FILE_HANDLE RootFileHandle
;
2553 if ( FilePattern
== NULL
2555 || StrStr(FilePattern
, L
":") == NULL
2557 return (EFI_INVALID_PARAMETER
);
2559 Status
= EFI_SUCCESS
;
2560 RootDevicePath
= NULL
;
2561 RootFileHandle
= NULL
;
2563 PatternCopy
= AllocateCopyPool(StrSize(FilePattern
), FilePattern
);
2564 if (PatternCopy
== NULL
) {
2565 return (EFI_OUT_OF_RESOURCES
);
2568 PatternCopy
= PathCleanUpDirectories(PatternCopy
);
2570 Count
= StrStr(PatternCopy
, L
":") - PatternCopy
+ 1;
2571 ASSERT (Count
<= StrLen (PatternCopy
));
2573 ASSERT(MapName
== NULL
);
2574 MapName
= StrnCatGrow(&MapName
, NULL
, PatternCopy
, Count
);
2575 if (MapName
== NULL
) {
2576 Status
= EFI_OUT_OF_RESOURCES
;
2578 RootDevicePath
= EfiShellGetDevicePathFromFilePath(PatternCopy
);
2579 if (RootDevicePath
== NULL
) {
2580 Status
= EFI_INVALID_PARAMETER
;
2582 Status
= EfiShellOpenRoot(RootDevicePath
, &RootFileHandle
);
2583 if (!EFI_ERROR(Status
)) {
2584 for ( PatternCurrentLocation
= PatternCopy
2585 ; *PatternCurrentLocation
!= ':'
2586 ; PatternCurrentLocation
++);
2587 PatternCurrentLocation
++;
2588 Status
= ShellSearchHandle(PatternCurrentLocation
, gUnicodeCollation
, RootFileHandle
, FileList
, NULL
, MapName
);
2589 EfiShellClose(RootFileHandle
);
2591 FreePool(RootDevicePath
);
2595 SHELL_FREE_NON_NULL(PatternCopy
);
2596 SHELL_FREE_NON_NULL(MapName
);
2602 Opens the files that match the path specified.
2604 This function opens all of the files specified by Path. Wildcards are processed
2605 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each
2606 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.
2608 @param Path A pointer to the path string.
2609 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or
2610 EFI_FILE_MODE_WRITE.
2611 @param FileList Points to the start of a list of files opened.
2613 @retval EFI_SUCCESS Create the file list successfully.
2614 @return Others Can't create the file list.
2618 EfiShellOpenFileList(
2621 IN OUT EFI_SHELL_FILE_INFO
**FileList
2625 EFI_SHELL_FILE_INFO
*ShellFileListItem
;
2628 CONST CHAR16
*CurDir
;
2631 PathCleanUpDirectories(Path
);
2636 if (FileList
== NULL
|| *FileList
== NULL
) {
2637 return (EFI_INVALID_PARAMETER
);
2640 if (*Path
== L
'.' && *(Path
+1) == L
'\\') {
2645 // convert a local path to an absolute path
2647 if (StrStr(Path
, L
":") == NULL
) {
2648 CurDir
= EfiShellGetCurDir(NULL
);
2649 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2650 StrnCatGrow(&Path2
, &Path2Size
, CurDir
, 0);
2651 StrnCatGrow(&Path2
, &Path2Size
, L
"\\", 0);
2652 if (*Path
== L
'\\') {
2654 while (PathRemoveLastItem(Path2
)) ;
2656 ASSERT((Path2
== NULL
&& Path2Size
== 0) || (Path2
!= NULL
));
2657 StrnCatGrow(&Path2
, &Path2Size
, Path
, 0);
2659 ASSERT(Path2
== NULL
);
2660 StrnCatGrow(&Path2
, NULL
, Path
, 0);
2663 PathCleanUpDirectories (Path2
);
2668 Status
= EfiShellFindFiles(Path2
, FileList
);
2672 if (EFI_ERROR(Status
)) {
2678 // We had no errors so open all the files (that are not already opened...)
2680 for ( ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&(*FileList
)->Link
)
2681 ; !IsNull(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2682 ; ShellFileListItem
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&(*FileList
)->Link
, &ShellFileListItem
->Link
)
2684 if (ShellFileListItem
->Status
== 0 && ShellFileListItem
->Handle
== NULL
) {
2685 ShellFileListItem
->Status
= EfiShellOpenFileByName (ShellFileListItem
->FullName
, &ShellFileListItem
->Handle
, OpenMode
);
2691 return (EFI_NOT_FOUND
);
2693 return(EFI_SUCCESS
);
2697 Gets the environment variable and Attributes, or list of environment variables. Can be
2698 used instead of GetEnv().
2700 This function returns the current value of the specified environment variable and
2701 the Attributes. If no variable name was specified, then all of the known
2702 variables will be returned.
2704 @param[in] Name A pointer to the environment variable name. If Name is NULL,
2705 then the function will return all of the defined shell
2706 environment variables. In the case where multiple environment
2707 variables are being returned, each variable will be terminated
2708 by a NULL, and the list will be terminated by a double NULL.
2709 @param[out] Attributes If not NULL, a pointer to the returned attributes bitmask for
2710 the environment variable. In the case where Name is NULL, and
2711 multiple environment variables are being returned, Attributes
2714 @retval NULL The environment variable doesn't exist.
2715 @return A non-NULL value points to the variable's value. The returned
2716 pointer does not need to be freed by the caller.
2721 IN CONST CHAR16
*Name
,
2722 OUT UINT32
*Attributes OPTIONAL
2729 CHAR16
*CurrentWriteLocation
;
2737 // Build the semi-colon delimited list. (2 passes)
2739 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
2740 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
2741 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
2743 ASSERT(Node
->Key
!= NULL
);
2744 Size
+= StrSize(Node
->Key
);
2747 Size
+= 2*sizeof(CHAR16
);
2749 Buffer
= AllocateZeroPool(Size
);
2750 if (Buffer
== NULL
) {
2753 CurrentWriteLocation
= (CHAR16
*)Buffer
;
2755 for ( Node
= (ENV_VAR_LIST
*)GetFirstNode(&gShellEnvVarList
.Link
)
2756 ; !IsNull(&gShellEnvVarList
.Link
, &Node
->Link
)
2757 ; Node
= (ENV_VAR_LIST
*)GetNextNode(&gShellEnvVarList
.Link
, &Node
->Link
)
2759 ASSERT(Node
->Key
!= NULL
);
2760 StrCpyS( CurrentWriteLocation
,
2761 (Size
)/sizeof(CHAR16
) - (CurrentWriteLocation
- ((CHAR16
*)Buffer
)),
2764 CurrentWriteLocation
+= StrLen(CurrentWriteLocation
) + 1;
2769 // We are doing a specific environment variable
2771 Status
= ShellFindEnvVarInList(Name
, (CHAR16
**)&Buffer
, &Size
, Attributes
);
2773 if (EFI_ERROR(Status
)){
2775 // get the size we need for this EnvVariable
2777 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name
, Attributes
, &Size
, Buffer
);
2778 if (Status
== EFI_BUFFER_TOO_SMALL
) {
2780 // Allocate the space and recall the get function
2782 Buffer
= AllocateZeroPool(Size
);
2783 Status
= SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name
, Attributes
, &Size
, Buffer
);
2786 // we didnt get it (might not exist)
2787 // free the memory if we allocated any and return NULL
2789 if (EFI_ERROR(Status
)) {
2790 if (Buffer
!= NULL
) {
2796 // If we did not find the environment variable in the gShellEnvVarList
2797 // but get it from UEFI variable storage successfully then we need update
2798 // the gShellEnvVarList.
2800 ShellFreeEnvVarList ();
2801 Status
= ShellInitEnvVarList ();
2802 ASSERT (Status
== EFI_SUCCESS
);
2808 // return the buffer
2810 return (AddBufferToFreeList(Buffer
));
2814 Gets either a single or list of environment variables.
2816 If name is not NULL then this function returns the current value of the specified
2817 environment variable.
2819 If Name is NULL, then a list of all environment variable names is returned. Each is a
2820 NULL terminated string with a double NULL terminating the list.
2822 @param Name A pointer to the environment variable name. If
2823 Name is NULL, then the function will return all
2824 of the defined shell environment variables. In
2825 the case where multiple environment variables are
2826 being returned, each variable will be terminated by
2827 a NULL, and the list will be terminated by a double
2830 @retval !=NULL A pointer to the returned string.
2831 The returned pointer does not need to be freed by the caller.
2833 @retval NULL The environment variable doesn't exist or there are
2834 no environment variables.
2839 IN CONST CHAR16
*Name
2842 return (EfiShellGetEnvEx(Name
, NULL
));
2846 Internal variable setting function. Allows for setting of the read only variables.
2848 @param Name Points to the NULL-terminated environment variable name.
2849 @param Value Points to the NULL-terminated environment variable value. If the value is an
2850 empty string then the environment variable is deleted.
2851 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2853 @retval EFI_SUCCESS The environment variable was successfully updated.
2856 InternalEfiShellSetEnv(
2857 IN CONST CHAR16
*Name
,
2858 IN CONST CHAR16
*Value
,
2864 if (Value
== NULL
|| StrLen(Value
) == 0) {
2865 Status
= SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2866 if (!EFI_ERROR(Status
)) {
2867 ShellRemvoeEnvVarFromList(Name
);
2870 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name
);
2871 Status
= ShellAddEnvVarToList(
2872 Name
, Value
, StrSize(Value
),
2873 EFI_VARIABLE_BOOTSERVICE_ACCESS
| (Volatile
? 0 : EFI_VARIABLE_NON_VOLATILE
)
2875 if (!EFI_ERROR (Status
)) {
2877 ? SHELL_SET_ENVIRONMENT_VARIABLE_V (Name
, StrSize (Value
) - sizeof (CHAR16
), Value
)
2878 : SHELL_SET_ENVIRONMENT_VARIABLE_NV (Name
, StrSize (Value
) - sizeof (CHAR16
), Value
);
2879 if (EFI_ERROR (Status
)) {
2880 ShellRemvoeEnvVarFromList(Name
);
2888 Sets the environment variable.
2890 This function changes the current value of the specified environment variable. If the
2891 environment variable exists and the Value is an empty string, then the environment
2892 variable is deleted. If the environment variable exists and the Value is not an empty
2893 string, then the value of the environment variable is changed. If the environment
2894 variable does not exist and the Value is an empty string, there is no action. If the
2895 environment variable does not exist and the Value is a non-empty string, then the
2896 environment variable is created and assigned the specified value.
2898 For a description of volatile and non-volatile environment variables, see UEFI Shell
2899 2.0 specification section 3.6.1.
2901 @param Name Points to the NULL-terminated environment variable name.
2902 @param Value Points to the NULL-terminated environment variable value. If the value is an
2903 empty string then the environment variable is deleted.
2904 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2906 @retval EFI_SUCCESS The environment variable was successfully updated.
2911 IN CONST CHAR16
*Name
,
2912 IN CONST CHAR16
*Value
,
2916 if (Name
== NULL
|| *Name
== CHAR_NULL
) {
2917 return (EFI_INVALID_PARAMETER
);
2920 // Make sure we dont 'set' a predefined read only variable
2922 if (gUnicodeCollation
->StriColl(
2926 ||gUnicodeCollation
->StriColl(
2930 ||gUnicodeCollation
->StriColl(
2934 ||gUnicodeCollation
->StriColl(
2937 L
"uefishellsupport") == 0
2938 ||gUnicodeCollation
->StriColl(
2941 L
"uefishellversion") == 0
2942 ||gUnicodeCollation
->StriColl(
2945 L
"uefiversion") == 0
2946 ||(!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
&&
2947 gUnicodeCollation
->StriColl(
2950 (CHAR16
*)mNoNestingEnvVarName
) == 0)
2952 return (EFI_INVALID_PARAMETER
);
2954 return (InternalEfiShellSetEnv(Name
, Value
, Volatile
));
2958 Returns the current directory on the specified device.
2960 If FileSystemMapping is NULL, it returns the current working directory. If the
2961 FileSystemMapping is not NULL, it returns the current directory associated with the
2962 FileSystemMapping. In both cases, the returned name includes the file system
2963 mapping (i.e. fs0:\current-dir).
2965 Note that the current directory string should exclude the tailing backslash character.
2967 @param FileSystemMapping A pointer to the file system mapping. If NULL,
2968 then the current working directory is returned.
2970 @retval !=NULL The current directory.
2971 @retval NULL Current directory does not exist.
2976 IN CONST CHAR16
*FileSystemMapping OPTIONAL
2979 CHAR16
*PathToReturn
;
2981 SHELL_MAP_LIST
*MapListItem
;
2982 if (!IsListEmpty(&gShellMapList
.Link
)) {
2984 // if parameter is NULL, use current
2986 if (FileSystemMapping
== NULL
) {
2987 return (EfiShellGetEnv(L
"cwd"));
2990 PathToReturn
= NULL
;
2991 MapListItem
= ShellCommandFindMapItem(FileSystemMapping
);
2992 if (MapListItem
!= NULL
) {
2993 ASSERT((PathToReturn
== NULL
&& Size
== 0) || (PathToReturn
!= NULL
));
2994 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->MapName
, 0);
2995 PathToReturn
= StrnCatGrow(&PathToReturn
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
2998 return (AddBufferToFreeList(PathToReturn
));
3005 Changes the current directory on the specified device.
3007 If the FileSystem is NULL, and the directory Dir does not contain a file system's
3008 mapped name, this function changes the current working directory.
3010 If the FileSystem is NULL and the directory Dir contains a mapped name, then the
3011 current file system and the current directory on that file system are changed.
3013 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file
3016 If FileSystem is not NULL and Dir is not NULL, then this function changes the current
3017 directory on the specified file system.
3019 If the current working directory or the current working file system is changed then the
3020 %cwd% environment variable will be updated
3022 Note that the current directory string should exclude the tailing backslash character.
3024 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working
3025 directory is changed.
3026 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.
3028 @retval EFI_SUCCESS The operation was sucessful
3029 @retval EFI_NOT_FOUND The file system could not be found
3034 IN CONST CHAR16
*FileSystem OPTIONAL
,
3035 IN CONST CHAR16
*Dir
3039 SHELL_MAP_LIST
*MapListItem
;
3043 CHAR16
*DirectoryName
;
3050 DirectoryName
= NULL
;
3052 if ((FileSystem
== NULL
&& Dir
== NULL
) || Dir
== NULL
) {
3053 return (EFI_INVALID_PARAMETER
);
3056 if (IsListEmpty(&gShellMapList
.Link
)){
3057 return (EFI_NOT_FOUND
);
3060 DirectoryName
= StrnCatGrow(&DirectoryName
, NULL
, Dir
, 0);
3061 ASSERT(DirectoryName
!= NULL
);
3063 PathCleanUpDirectories(DirectoryName
);
3065 if (FileSystem
== NULL
) {
3067 // determine the file system mapping to use
3069 if (StrStr(DirectoryName
, L
":") != NULL
) {
3070 ASSERT(MapName
== NULL
);
3071 MapName
= StrnCatGrow(&MapName
, NULL
, DirectoryName
, (StrStr(DirectoryName
, L
":")-DirectoryName
+1));
3074 // find the file system mapping's entry in the list
3077 if (MapName
!= NULL
) {
3078 MapListItem
= ShellCommandFindMapItem(MapName
);
3081 // make that the current file system mapping
3083 if (MapListItem
!= NULL
) {
3084 gShellCurMapping
= MapListItem
;
3087 MapListItem
= gShellCurMapping
;
3090 if (MapListItem
== NULL
) {
3091 FreePool (DirectoryName
);
3092 SHELL_FREE_NON_NULL(MapName
);
3093 return (EFI_NOT_FOUND
);
3097 // now update the MapListItem's current directory
3099 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& DirectoryName
[StrLen(DirectoryName
) - 1] != L
':') {
3100 FreePool(MapListItem
->CurrentDirectoryPath
);
3101 MapListItem
->CurrentDirectoryPath
= NULL
;
3103 if (MapName
!= NULL
) {
3104 TempLen
= StrLen(MapName
);
3105 if (TempLen
!= StrLen(DirectoryName
)) {
3106 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3107 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
+StrLen(MapName
), 0);
3111 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3112 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
3114 if ((MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] == L
'\\') || (MapListItem
->CurrentDirectoryPath
== NULL
)) {
3115 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3116 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
3117 MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] = CHAR_NULL
;
3122 // cant have a mapping in the directory...
3124 if (StrStr(DirectoryName
, L
":") != NULL
) {
3125 FreePool (DirectoryName
);
3126 return (EFI_INVALID_PARAMETER
);
3129 // FileSystem != NULL
3131 MapListItem
= ShellCommandFindMapItem(FileSystem
);
3132 if (MapListItem
== NULL
) {
3133 FreePool (DirectoryName
);
3134 return (EFI_INVALID_PARAMETER
);
3136 // gShellCurMapping = MapListItem;
3137 if (DirectoryName
!= NULL
) {
3139 // change current dir on that file system
3142 if (MapListItem
->CurrentDirectoryPath
!= NULL
) {
3143 FreePool(MapListItem
->CurrentDirectoryPath
);
3144 DEBUG_CODE(MapListItem
->CurrentDirectoryPath
= NULL
;);
3146 // ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
3147 // MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);
3148 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3149 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, L
"\\", 0);
3150 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3151 MapListItem
->CurrentDirectoryPath
= StrnCatGrow(&MapListItem
->CurrentDirectoryPath
, &Size
, DirectoryName
, 0);
3152 if (MapListItem
->CurrentDirectoryPath
!= NULL
&& MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] == L
'\\') {
3153 ASSERT((MapListItem
->CurrentDirectoryPath
== NULL
&& Size
== 0) || (MapListItem
->CurrentDirectoryPath
!= NULL
));
3154 MapListItem
->CurrentDirectoryPath
[StrLen(MapListItem
->CurrentDirectoryPath
)-1] = CHAR_NULL
;
3158 FreePool (DirectoryName
);
3160 // if updated the current directory then update the environment variable
3162 if (MapListItem
== gShellCurMapping
) {
3164 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
3165 StrnCatGrow(&TempString
, &Size
, MapListItem
->MapName
, 0);
3166 ASSERT((TempString
== NULL
&& Size
== 0) || (TempString
!= NULL
));
3167 StrnCatGrow(&TempString
, &Size
, MapListItem
->CurrentDirectoryPath
, 0);
3168 Status
= InternalEfiShellSetEnv(L
"cwd", TempString
, TRUE
);
3169 FreePool(TempString
);
3172 return(EFI_SUCCESS
);
3176 Return help information about a specific command.
3178 This function returns the help information for the specified command. The help text
3179 can be internal to the shell or can be from a UEFI Shell manual page.
3181 If Sections is specified, then each section name listed will be compared in a casesensitive
3182 manner, to the section names described in Appendix B. If the section exists,
3183 it will be appended to the returned help text. If the section does not exist, no
3184 information will be returned. If Sections is NULL, then all help text information
3185 available will be returned.
3187 @param Command Points to the NULL-terminated UEFI Shell command name.
3188 @param Sections Points to the NULL-terminated comma-delimited
3189 section names to return. If NULL, then all
3190 sections will be returned.
3191 @param HelpText On return, points to a callee-allocated buffer
3192 containing all specified help text.
3194 @retval EFI_SUCCESS The help text was returned.
3195 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
3197 @retval EFI_INVALID_PARAMETER HelpText is NULL
3198 @retval EFI_NOT_FOUND There is no help text available for Command.
3202 EfiShellGetHelpText(
3203 IN CONST CHAR16
*Command
,
3204 IN CONST CHAR16
*Sections OPTIONAL
,
3205 OUT CHAR16
**HelpText
3208 CONST CHAR16
*ManFileName
;
3212 ASSERT(HelpText
!= NULL
);
3215 ManFileName
= ShellCommandGetManFileNameHandler(Command
);
3217 if (ManFileName
!= NULL
) {
3218 return (ProcessManFile(ManFileName
, Command
, Sections
, NULL
, HelpText
));
3220 if ((StrLen(Command
)> 4)
3221 && (Command
[StrLen(Command
)-1] == L
'i' || Command
[StrLen(Command
)-1] == L
'I')
3222 && (Command
[StrLen(Command
)-2] == L
'f' || Command
[StrLen(Command
)-2] == L
'F')
3223 && (Command
[StrLen(Command
)-3] == L
'e' || Command
[StrLen(Command
)-3] == L
'E')
3224 && (Command
[StrLen(Command
)-4] == L
'.')
3226 FixCommand
= AllocateZeroPool(StrSize(Command
) - 4 * sizeof (CHAR16
));
3227 if (FixCommand
== NULL
) {
3228 return EFI_OUT_OF_RESOURCES
;
3231 StrnCpyS( FixCommand
,
3232 (StrSize(Command
) - 4 * sizeof (CHAR16
))/sizeof(CHAR16
),
3236 Status
= ProcessManFile(FixCommand
, FixCommand
, Sections
, NULL
, HelpText
);
3237 FreePool(FixCommand
);
3240 return (ProcessManFile(Command
, Command
, Sections
, NULL
, HelpText
));
3246 Gets the enable status of the page break output mode.
3248 User can use this function to determine current page break mode.
3250 @retval TRUE The page break output mode is enabled.
3251 @retval FALSE The page break output mode is disabled.
3255 EfiShellGetPageBreak(
3259 return(ShellInfoObject
.PageBreakEnabled
);
3263 Judges whether the active shell is the root shell.
3265 This function makes the user to know that whether the active Shell is the root shell.
3267 @retval TRUE The active Shell is the root Shell.
3268 @retval FALSE The active Shell is NOT the root Shell.
3272 EfiShellIsRootShell(
3276 return(ShellInfoObject
.RootShellInstance
);
3280 function to return a semi-colon delimeted list of all alias' in the current shell
3282 up to caller to free the memory.
3284 @retval NULL No alias' were found
3285 @retval NULL An error ocurred getting alias'
3286 @return !NULL a list of all alias'
3289 InternalEfiShellGetListAlias(
3296 CHAR16
*VariableName
;
3298 UINTN NameBufferSize
;
3302 NameBufferSize
= INIT_NAME_BUFFER_SIZE
;
3303 VariableName
= AllocateZeroPool(NameBufferSize
);
3307 if (VariableName
== NULL
) {
3311 VariableName
[0] = CHAR_NULL
;
3314 NameSize
= NameBufferSize
;
3315 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
3316 if (Status
== EFI_NOT_FOUND
){
3318 } else if (Status
== EFI_BUFFER_TOO_SMALL
) {
3319 NameBufferSize
= NameSize
> NameBufferSize
* 2 ? NameSize
: NameBufferSize
* 2;
3320 SHELL_FREE_NON_NULL(VariableName
);
3321 VariableName
= AllocateZeroPool(NameBufferSize
);
3322 if (VariableName
== NULL
) {
3323 Status
= EFI_OUT_OF_RESOURCES
;
3324 SHELL_FREE_NON_NULL(RetVal
);
3329 NameSize
= NameBufferSize
;
3330 Status
= gRT
->GetNextVariableName(&NameSize
, VariableName
, &Guid
);
3333 if (EFI_ERROR (Status
)) {
3334 SHELL_FREE_NON_NULL(RetVal
);
3339 if (CompareGuid(&Guid
, &gShellAliasGuid
)){
3340 ASSERT((RetVal
== NULL
&& RetSize
== 0) || (RetVal
!= NULL
));
3341 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, VariableName
, 0);
3342 RetVal
= StrnCatGrow(&RetVal
, &RetSize
, L
";", 0);
3345 SHELL_FREE_NON_NULL(VariableName
);
3351 Convert a null-terminated unicode string, in-place, to all lowercase.
3354 @param Str The null-terminated string to be converted to all lowercase.
3356 @return The null-terminated string converted into all lowercase.
3365 for (Index
= 0; Str
[Index
] != L
'\0'; Index
++) {
3366 if (Str
[Index
] >= L
'A' && Str
[Index
] <= L
'Z') {
3367 Str
[Index
] -= (CHAR16
)(L
'A' - L
'a');
3374 This function returns the command associated with a alias or a list of all
3377 @param[in] Alias Points to the NULL-terminated shell alias.
3378 If this parameter is NULL, then all
3379 aliases will be returned in ReturnedData.
3380 @param[out] Volatile upon return of a single command if TRUE indicates
3381 this is stored in a volatile fashion. FALSE otherwise.
3383 @return If Alias is not NULL, it will return a pointer to
3384 the NULL-terminated command for that alias.
3385 If Alias is NULL, ReturnedData points to a ';'
3386 delimited list of alias (e.g.
3387 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.
3388 @retval NULL an error ocurred
3389 @retval NULL Alias was not a valid Alias
3394 IN CONST CHAR16
*Alias
,
3395 OUT BOOLEAN
*Volatile OPTIONAL
3405 // Convert to lowercase to make aliases case-insensitive
3406 if (Alias
!= NULL
) {
3407 AliasLower
= AllocateCopyPool (StrSize (Alias
), Alias
);
3408 if (AliasLower
== NULL
) {
3411 ToLower (AliasLower
);
3413 if (Volatile
== NULL
) {
3414 GetVariable2 (AliasLower
, &gShellAliasGuid
, (VOID
**)&AliasVal
, NULL
);
3415 FreePool(AliasLower
);
3416 return (AddBufferToFreeList(AliasVal
));
3420 Status
= gRT
->GetVariable(AliasLower
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
3421 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3422 RetVal
= AllocateZeroPool(RetSize
);
3423 Status
= gRT
->GetVariable(AliasLower
, &gShellAliasGuid
, &Attribs
, &RetSize
, RetVal
);
3425 if (EFI_ERROR(Status
)) {
3426 if (RetVal
!= NULL
) {
3429 FreePool(AliasLower
);
3432 if ((EFI_VARIABLE_NON_VOLATILE
& Attribs
) == EFI_VARIABLE_NON_VOLATILE
) {
3438 FreePool (AliasLower
);
3439 return (AddBufferToFreeList(RetVal
));
3441 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));
3445 Changes a shell command alias.
3447 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
3449 this function does not check for built in alias'.
3451 @param[in] Command Points to the NULL-terminated shell command or existing alias.
3452 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
3453 Command refers to an alias, that alias will be deleted.
3454 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
3455 Alias being set will be stored in a non-volatile fashion.
3457 @retval EFI_SUCCESS Alias created or deleted successfully.
3458 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
3462 IN CONST CHAR16
*Command
,
3463 IN CONST CHAR16
*Alias
,
3469 BOOLEAN DeleteAlias
;
3471 DeleteAlias
= FALSE
;
3472 if (Alias
== NULL
) {
3474 // We must be trying to remove one if Alias is NULL
3475 // remove an alias (but passed in COMMAND parameter)
3480 ASSERT (Alias
!= NULL
);
3483 // Convert to lowercase to make aliases case-insensitive
3485 AliasLower
= AllocateCopyPool (StrSize (Alias
), Alias
);
3486 if (AliasLower
== NULL
) {
3487 return EFI_OUT_OF_RESOURCES
;
3489 ToLower (AliasLower
);
3492 Status
= gRT
->SetVariable (AliasLower
, &gShellAliasGuid
, 0, 0, NULL
);
3494 Status
= gRT
->SetVariable (
3495 AliasLower
, &gShellAliasGuid
,
3496 EFI_VARIABLE_BOOTSERVICE_ACCESS
| (Volatile
? 0 : EFI_VARIABLE_NON_VOLATILE
),
3497 StrSize (Command
), (VOID
*) Command
3501 FreePool (AliasLower
);
3507 Changes a shell command alias.
3509 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
3512 @param[in] Command Points to the NULL-terminated shell command or existing alias.
3513 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
3514 Command refers to an alias, that alias will be deleted.
3515 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
3516 FALSE and the alias already exists, then the existing alias is unchanged and
3517 EFI_ACCESS_DENIED is returned.
3518 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
3519 Alias being set will be stored in a non-volatile fashion.
3521 @retval EFI_SUCCESS Alias created or deleted successfully.
3522 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
3523 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
3525 @retval EFI_INVALID_PARAMETER Command is null or the empty string.
3530 IN CONST CHAR16
*Command
,
3531 IN CONST CHAR16
*Alias
,
3536 if (ShellCommandIsOnAliasList(Alias
==NULL
?Command
:Alias
)) {
3538 // cant set over a built in alias
3540 return (EFI_ACCESS_DENIED
);
3541 } else if (Command
== NULL
|| *Command
== CHAR_NULL
|| StrLen(Command
) == 0) {
3543 // Command is null or empty
3545 return (EFI_INVALID_PARAMETER
);
3546 } else if (EfiShellGetAlias(Command
, NULL
) != NULL
&& !Replace
) {
3548 // Alias already exists, Replace not set
3550 return (EFI_ACCESS_DENIED
);
3552 return (InternalSetAlias(Command
, Alias
, Volatile
));
3556 // Pure FILE_HANDLE operations are passed to FileHandleLib
3557 // these functions are indicated by the *
3558 EFI_SHELL_PROTOCOL mShellProtocol
= {
3564 EfiShellGetHelpText
,
3565 EfiShellGetDevicePathFromMap
,
3566 EfiShellGetMapFromDevicePath
,
3567 EfiShellGetDevicePathFromFilePath
,
3568 EfiShellGetFilePathFromDevicePath
,
3572 EfiShellOpenFileList
,
3573 EfiShellFreeFileList
,
3574 EfiShellRemoveDupInFileList
,
3575 EfiShellBatchIsActive
,
3576 EfiShellIsRootShell
,
3577 EfiShellEnablePageBreak
,
3578 EfiShellDisablePageBreak
,
3579 EfiShellGetPageBreak
,
3580 EfiShellGetDeviceName
,
3581 (EFI_SHELL_GET_FILE_INFO
)FileHandleGetInfo
, //*
3582 (EFI_SHELL_SET_FILE_INFO
)FileHandleSetInfo
, //*
3583 EfiShellOpenFileByName
,
3586 (EFI_SHELL_READ_FILE
)FileHandleRead
, //*
3587 (EFI_SHELL_WRITE_FILE
)FileHandleWrite
, //*
3588 (EFI_SHELL_DELETE_FILE
)FileHandleDelete
, //*
3589 EfiShellDeleteFileByName
,
3590 (EFI_SHELL_GET_FILE_POSITION
)FileHandleGetPosition
, //*
3591 (EFI_SHELL_SET_FILE_POSITION
)FileHandleSetPosition
, //*
3592 (EFI_SHELL_FLUSH_FILE
)FileHandleFlush
, //*
3594 EfiShellFindFilesInDir
,
3595 (EFI_SHELL_GET_FILE_SIZE
)FileHandleGetSize
, //*
3597 EfiShellOpenRootByHandle
,
3599 SHELL_MAJOR_VERSION
,
3600 SHELL_MINOR_VERSION
,
3602 // New for UEFI Shell 2.1
3603 EfiShellRegisterGuidName
,
3604 EfiShellGetGuidName
,
3605 EfiShellGetGuidFromName
,
3610 Function to create and install on the current handle.
3612 Will overwrite any existing ShellProtocols in the system to be sure that
3613 the current shell is in control.
3615 This must be removed via calling CleanUpShellProtocol().
3617 @param[in, out] NewShell The pointer to the pointer to the structure
3620 @retval EFI_SUCCESS The operation was successful.
3621 @return An error from LocateHandle, CreateEvent, or other core function.
3624 CreatePopulateInstallShellProtocol (
3625 IN OUT EFI_SHELL_PROTOCOL
**NewShell
3631 UINTN HandleCounter
;
3632 SHELL_PROTOCOL_HANDLE_LIST
*OldProtocolNode
;
3633 EFI_SHELL_PROTOCOL
*OldShell
;
3635 if (NewShell
== NULL
) {
3636 return (EFI_INVALID_PARAMETER
);
3641 OldProtocolNode
= NULL
;
3642 InitializeListHead(&ShellInfoObject
.OldShellList
.Link
);
3645 // Initialize EfiShellProtocol object...
3647 Status
= gBS
->CreateEvent(0,
3651 &mShellProtocol
.ExecutionBreak
);
3652 if (EFI_ERROR(Status
)) {
3657 // Get the size of the buffer we need.
3659 Status
= gBS
->LocateHandle(ByProtocol
,
3660 &gEfiShellProtocolGuid
,
3664 if (Status
== EFI_BUFFER_TOO_SMALL
) {
3666 // Allocate and recall with buffer of correct size
3668 Buffer
= AllocateZeroPool(BufferSize
);
3669 if (Buffer
== NULL
) {
3670 return (EFI_OUT_OF_RESOURCES
);
3672 Status
= gBS
->LocateHandle(ByProtocol
,
3673 &gEfiShellProtocolGuid
,
3677 if (EFI_ERROR(Status
)) {
3682 // now overwrite each of them, but save the info to restore when we end.
3684 for (HandleCounter
= 0 ; HandleCounter
< (BufferSize
/sizeof(EFI_HANDLE
)) ; HandleCounter
++) {
3685 Status
= gBS
->OpenProtocol(Buffer
[HandleCounter
],
3686 &gEfiShellProtocolGuid
,
3687 (VOID
**) &OldShell
,
3690 EFI_OPEN_PROTOCOL_GET_PROTOCOL
3692 if (!EFI_ERROR(Status
)) {
3693 OldProtocolNode
= AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST
));
3694 if (OldProtocolNode
== NULL
) {
3695 if (!IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)) {
3696 CleanUpShellProtocol (&mShellProtocol
);
3698 Status
= EFI_OUT_OF_RESOURCES
;
3702 // reinstall over the old one...
3704 OldProtocolNode
->Handle
= Buffer
[HandleCounter
];
3705 OldProtocolNode
->Interface
= OldShell
;
3706 Status
= gBS
->ReinstallProtocolInterface(
3707 OldProtocolNode
->Handle
,
3708 &gEfiShellProtocolGuid
,
3709 OldProtocolNode
->Interface
,
3710 (VOID
*)(&mShellProtocol
));
3711 if (!EFI_ERROR(Status
)) {
3713 // we reinstalled sucessfully. log this so we can reverse it later.
3717 // add to the list for subsequent...
3719 InsertTailList(&ShellInfoObject
.OldShellList
.Link
, &OldProtocolNode
->Link
);
3724 } else if (Status
== EFI_NOT_FOUND
) {
3725 ASSERT(IsListEmpty(&ShellInfoObject
.OldShellList
.Link
));
3727 // no one else published yet. just publish it ourselves.
3729 Status
= gBS
->InstallProtocolInterface (
3731 &gEfiShellProtocolGuid
,
3732 EFI_NATIVE_INTERFACE
,
3733 (VOID
*)(&mShellProtocol
));
3736 if (PcdGetBool(PcdShellSupportOldProtocols
)){
3737 ///@todo support ShellEnvironment2
3738 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?
3741 if (!EFI_ERROR(Status
)) {
3742 *NewShell
= &mShellProtocol
;
3748 Opposite of CreatePopulateInstallShellProtocol.
3750 Free all memory and restore the system to the state it was in before calling
3751 CreatePopulateInstallShellProtocol.
3753 @param[in, out] NewShell The pointer to the new shell protocol structure.
3755 @retval EFI_SUCCESS The operation was successful.
3758 CleanUpShellProtocol (
3759 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3762 SHELL_PROTOCOL_HANDLE_LIST
*Node2
;
3765 // if we need to restore old protocols...
3767 if (!IsListEmpty(&ShellInfoObject
.OldShellList
.Link
)) {
3768 for (Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*) GetFirstNode (&ShellInfoObject
.OldShellList
.Link
)
3769 ; !IsListEmpty (&ShellInfoObject
.OldShellList
.Link
)
3770 ; Node2
= (SHELL_PROTOCOL_HANDLE_LIST
*) GetFirstNode (&ShellInfoObject
.OldShellList
.Link
)
3772 RemoveEntryList (&Node2
->Link
);
3773 gBS
->ReinstallProtocolInterface (Node2
->Handle
, &gEfiShellProtocolGuid
, NewShell
, Node2
->Interface
);
3778 // no need to restore
3780 gBS
->UninstallProtocolInterface (gImageHandle
, &gEfiShellProtocolGuid
, NewShell
);
3786 Cleanup the shell environment.
3788 @param[in, out] NewShell The pointer to the new shell protocol structure.
3790 @retval EFI_SUCCESS The operation was successful.
3793 CleanUpShellEnvironment (
3794 IN OUT EFI_SHELL_PROTOCOL
*NewShell
3798 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
3800 CleanUpShellProtocol (NewShell
);
3802 Status
= gBS
->CloseEvent(NewShell
->ExecutionBreak
);
3803 NewShell
->ExecutionBreak
= NULL
;
3805 Status
= gBS
->OpenProtocol(
3806 gST
->ConsoleInHandle
,
3807 &gEfiSimpleTextInputExProtocolGuid
,
3811 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
3813 if (!EFI_ERROR (Status
)) {
3814 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle1
);
3815 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle2
);
3816 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle3
);
3817 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlCNotifyHandle4
);
3818 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle1
);
3819 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle2
);
3820 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle3
);
3821 Status
= SimpleEx
->UnregisterKeyNotify(SimpleEx
, ShellInfoObject
.CtrlSNotifyHandle4
);
3827 Notification function for keystrokes.
3829 @param[in] KeyData The key that was pressed.
3831 @retval EFI_SUCCESS The operation was successful.
3835 NotificationFunction(
3836 IN EFI_KEY_DATA
*KeyData
3839 if ( ((KeyData
->Key
.UnicodeChar
== L
'c') &&
3840 (KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
) || KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
))) ||
3841 (KeyData
->Key
.UnicodeChar
== 3)
3843 if (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
== NULL
) {
3844 return (EFI_UNSUPPORTED
);
3846 return (gBS
->SignalEvent(ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
));
3847 } else if ((KeyData
->Key
.UnicodeChar
== L
's') &&
3848 (KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
) || KeyData
->KeyState
.KeyShiftState
== (EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
))
3850 ShellInfoObject
.HaltOutput
= TRUE
;
3852 return (EFI_SUCCESS
);
3856 Function to start monitoring for CTRL-C using SimpleTextInputEx. This
3857 feature's enabled state was not known when the shell initially launched.
3859 @retval EFI_SUCCESS The feature is enabled.
3860 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
3863 InernalEfiShellStartMonitor(
3867 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
3868 EFI_KEY_DATA KeyData
;
3871 Status
= gBS
->OpenProtocol(
3872 gST
->ConsoleInHandle
,
3873 &gEfiSimpleTextInputExProtocolGuid
,
3877 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
3878 if (EFI_ERROR(Status
)) {
3883 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
3884 ShellInfoObject
.HiiHandle
);
3885 return (EFI_SUCCESS
);
3888 if (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
== NULL
) {
3889 return (EFI_UNSUPPORTED
);
3892 KeyData
.KeyState
.KeyToggleState
= 0;
3893 KeyData
.Key
.ScanCode
= 0;
3894 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
3895 KeyData
.Key
.UnicodeChar
= L
'c';
3897 Status
= SimpleEx
->RegisterKeyNotify(
3900 NotificationFunction
,
3901 &ShellInfoObject
.CtrlCNotifyHandle1
);
3903 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
3904 if (!EFI_ERROR(Status
)) {
3905 Status
= SimpleEx
->RegisterKeyNotify(
3908 NotificationFunction
,
3909 &ShellInfoObject
.CtrlCNotifyHandle2
);
3911 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
3912 KeyData
.Key
.UnicodeChar
= 3;
3913 if (!EFI_ERROR(Status
)) {
3914 Status
= SimpleEx
->RegisterKeyNotify(
3917 NotificationFunction
,
3918 &ShellInfoObject
.CtrlCNotifyHandle3
);
3920 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
3921 if (!EFI_ERROR(Status
)) {
3922 Status
= SimpleEx
->RegisterKeyNotify(
3925 NotificationFunction
,
3926 &ShellInfoObject
.CtrlCNotifyHandle4
);