]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Application/Shell/ShellProtocol.c
1) Removing ASSERTs for proper return values.
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellProtocol.c
1 /** @file
2 Member functions of EFI_SHELL_PROTOCOL and functions for creation,
3 manipulation, and initialization of EFI_SHELL_PROTOCOL.
4
5 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Shell.h"
17 #include <Library/FileHandleLib.h>
18
19 /**
20 Close an open file handle.
21
22 This function closes a specified file handle. All "dirty" cached file data is
23 flushed to the device, and the file is closed. In all cases the handle is
24 closed.
25
26 @param[in] FileHandle The file handle to close.
27
28 @retval EFI_SUCCESS The file handle was closed successfully.
29 **/
30 EFI_STATUS
31 EFIAPI
32 EfiShellClose (
33 IN SHELL_FILE_HANDLE FileHandle
34 )
35 {
36 ShellFileHandleRemove(FileHandle);
37 return (FileHandleClose(FileHandle));
38 }
39
40 /**
41 Internal worker to determine whether there is a file system somewhere
42 upon the device path specified.
43
44 @param[in] DevicePath The device path to test.
45
46 @retval TRUE gEfiSimpleFileSystemProtocolGuid was installed on a handle with this device path
47 @retval FALSE gEfiSimpleFileSystemProtocolGuid was not found.
48 **/
49 BOOLEAN
50 EFIAPI
51 InternalShellProtocolIsSimpleFileSystemPresent(
52 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
53 )
54 {
55 EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
56 EFI_STATUS Status;
57 EFI_HANDLE Handle;
58
59 Handle = NULL;
60
61 DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL*)DevicePath;
62 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &Handle);
63
64 if ((Handle != NULL) && (!EFI_ERROR(Status))) {
65 return (TRUE);
66 }
67 return (FALSE);
68 }
69
70 /**
71 Internal worker debug helper function to print out maps as they are added.
72
73 @param[in] Mapping string mapping that has been added
74 @param[in] DevicePath pointer to device path that has been mapped.
75
76 @retval EFI_SUCCESS the operation was successful.
77 @return other an error ocurred
78
79 @sa LocateHandle
80 @sa OpenProtocol
81 **/
82 EFI_STATUS
83 EFIAPI
84 InternalShellProtocolDebugPrintMessage (
85 IN CONST CHAR16 *Mapping,
86 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
87 )
88 {
89 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevicePathToText;
90 EFI_STATUS Status;
91 CHAR16 *Temp;
92
93 Status = EFI_SUCCESS;
94 DEBUG_CODE_BEGIN();
95 DevicePathToText = NULL;
96
97 Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid,
98 NULL,
99 (VOID**)&DevicePathToText);
100 if (Mapping != NULL) {
101 DEBUG((EFI_D_INFO, "Added new map item:\"%S\"\r\n", Mapping));
102 }
103 if (!EFI_ERROR(Status)) {
104 if (DevicePath != NULL) {
105 Temp = DevicePathToText->ConvertDevicePathToText(DevicePath, TRUE, TRUE);
106 DEBUG((EFI_D_INFO, "DevicePath: %S\r\n", Temp));
107 FreePool(Temp);
108 }
109 }
110 DEBUG_CODE_END();
111 return (Status);
112 }
113
114 /**
115 This function creates a mapping for a device path.
116
117 If both DeviecPath and Mapping are NULL, this will reset the mapping to default values.
118
119 @param DevicePath Points to the device path. If this is NULL and Mapping points to a valid mapping,
120 then the mapping will be deleted.
121 @param Mapping Points to the NULL-terminated mapping for the device path. Must end with a ':'
122
123 @retval EFI_SUCCESS Mapping created or deleted successfully.
124 @retval EFI_NO_MAPPING There is no handle that corresponds exactly to DevicePath. See the
125 boot service function LocateDevicePath().
126 @retval EFI_ACCESS_DENIED The mapping is a built-in alias.
127 @retval EFI_INVALID_PARAMETER Mapping was NULL
128 @retval EFI_INVALID_PARAMETER Mapping did not end with a ':'
129 @retval EFI_INVALID_PARAMETER DevicePath was not pointing at a device that had a SIMPLE_FILE_SYSTEM_PROTOCOL installed.
130 @retval EFI_NOT_FOUND There was no mapping found to delete
131 @retval EFI_OUT_OF_RESOURCES Memory allocation failed
132 **/
133 EFI_STATUS
134 EFIAPI
135 EfiShellSetMap(
136 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,
137 IN CONST CHAR16 *Mapping
138 )
139 {
140 EFI_STATUS Status;
141 SHELL_MAP_LIST *MapListNode;
142
143 if (Mapping == NULL){
144 return (EFI_INVALID_PARAMETER);
145 }
146
147 if (Mapping[StrLen(Mapping)-1] != ':') {
148 return (EFI_INVALID_PARAMETER);
149 }
150
151 //
152 // Delete the mapping
153 //
154 if (DevicePath == NULL) {
155 if (IsListEmpty(&gShellMapList.Link)) {
156 return (EFI_NOT_FOUND);
157 }
158 for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
159 ; !IsNull(&gShellMapList.Link, &MapListNode->Link)
160 ; MapListNode = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &MapListNode->Link)
161 ){
162 if (StringNoCaseCompare(&MapListNode->MapName, &Mapping) == 0) {
163 RemoveEntryList(&MapListNode->Link);
164 FreePool(MapListNode);
165 return (EFI_SUCCESS);
166 }
167 } // for loop
168
169 //
170 // We didnt find one to delete
171 //
172 return (EFI_NOT_FOUND);
173 }
174
175 //
176 // make sure this is a valid to add device path
177 //
178 ///@todo add BlockIo to this test...
179 if (!InternalShellProtocolIsSimpleFileSystemPresent(DevicePath)) {
180 return (EFI_INVALID_PARAMETER);
181 }
182
183 //
184 // First make sure there is no old mapping
185 //
186 Status = EfiShellSetMap(NULL, Mapping);
187 if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_FOUND)) {
188 return (Status);
189 }
190
191 //
192 // now add the new one.
193 //
194 Status = ShellCommandAddMapItemAndUpdatePath(Mapping, DevicePath, 0, FALSE);
195
196 return(Status);
197 }
198
199 /**
200 Gets the device path from the mapping.
201
202 This function gets the device path associated with a mapping.
203
204 @param Mapping A pointer to the mapping
205
206 @retval !=NULL Pointer to the device path that corresponds to the
207 device mapping. The returned pointer does not need
208 to be freed.
209 @retval NULL There is no device path associated with the
210 specified mapping.
211 **/
212 CONST EFI_DEVICE_PATH_PROTOCOL *
213 EFIAPI
214 EfiShellGetDevicePathFromMap(
215 IN CONST CHAR16 *Mapping
216 )
217 {
218 SHELL_MAP_LIST *MapListItem;
219 CHAR16 *NewName;
220 UINTN Size;
221
222 NewName = NULL;
223 Size = 0;
224
225 StrnCatGrow(&NewName, &Size, Mapping, 0);
226 if (Mapping[StrLen(Mapping)-1] != L':') {
227 StrnCatGrow(&NewName, &Size, L":", 0);
228 }
229
230 MapListItem = ShellCommandFindMapItem(NewName);
231
232 FreePool(NewName);
233
234 if (MapListItem != NULL) {
235 return (MapListItem->DevicePath);
236 }
237 return(NULL);
238 }
239
240 /**
241 Gets the mapping(s) that most closely matches the device path.
242
243 This function gets the mapping which corresponds to the device path *DevicePath. If
244 there is no exact match, then the mapping which most closely matches *DevicePath
245 is returned, and *DevicePath is updated to point to the remaining portion of the
246 device path. If there is an exact match, the mapping is returned and *DevicePath
247 points to the end-of-device-path node.
248
249 If there are multiple map names they will be semi-colon seperated in the
250 NULL-terminated string.
251
252 @param DevicePath On entry, points to a device path pointer. On
253 exit, updates the pointer to point to the
254 portion of the device path after the mapping.
255
256 @retval NULL No mapping was found.
257 @return !=NULL Pointer to NULL-terminated mapping. The buffer
258 is callee allocated and should be freed by the caller.
259 **/
260 CONST CHAR16 *
261 EFIAPI
262 EfiShellGetMapFromDevicePath(
263 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
264 )
265 {
266 SHELL_MAP_LIST *Node;
267 CHAR16 *PathForReturn;
268 UINTN PathSize;
269 // EFI_HANDLE PathHandle;
270 // EFI_HANDLE MapHandle;
271 // EFI_STATUS Status;
272 // EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
273 // EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
274
275 if (DevicePath == NULL || *DevicePath == NULL) {
276 return (NULL);
277 }
278
279 PathForReturn = NULL;
280 PathSize = 0;
281
282 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
283 ; !IsNull(&gShellMapList.Link, &Node->Link)
284 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
285 ){
286 //
287 // check for exact match
288 //
289 if (DevicePathCompare(DevicePath, &Node->DevicePath) == 0) {
290 ASSERT((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
291 if (PathSize != 0) {
292 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
293 }
294 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
295 }
296 }
297 if (PathForReturn != NULL) {
298 while (!IsDevicePathEndType (*DevicePath)) {
299 *DevicePath = NextDevicePathNode (*DevicePath);
300 }
301 SetDevicePathEndNode (*DevicePath);
302 }
303 /*
304 ///@todo finish code for inexact matches.
305 if (PathForReturn == NULL) {
306 PathSize = 0;
307
308 DevicePathCopy = DuplicateDevicePath(*DevicePath);
309 ASSERT(DevicePathCopy != NULL);
310 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
311 ASSERT_EFI_ERROR(Status);
312 //
313 // check each of the device paths we have to get the root of the path for consist mappings
314 //
315 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
316 ; !IsNull(&gShellMapList.Link, &Node->Link)
317 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
318 ){
319 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) == 0) {
320 continue;
321 }
322 MapPathCopy = DuplicateDevicePath(Node->DevicePath);
323 ASSERT(MapPathCopy != NULL);
324 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
325 if (MapHandle == PathHandle) {
326
327 *DevicePath = DevicePathCopy;
328
329 MapPathCopy = NULL;
330 DevicePathCopy = NULL;
331 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
332 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
333 break;
334 }
335 }
336 //
337 // now add on the non-consistent mappings
338 //
339 for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
340 ; !IsNull(&gShellMapList.Link, &Node->Link)
341 ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
342 ){
343 if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) != 0) {
344 continue;
345 }
346 MapPathCopy = Node->DevicePath;
347 ASSERT(MapPathCopy != NULL);
348 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
349 if (MapHandle == PathHandle) {
350 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
351 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
352 break;
353 }
354 }
355 }
356 */
357
358 return (AddBufferToFreeList(PathForReturn));
359 }
360
361 /**
362 Converts a device path to a file system-style path.
363
364 This function converts a device path to a file system path by replacing part, or all, of
365 the device path with the file-system mapping. If there are more than one application
366 file system mappings, the one that most closely matches Path will be used.
367
368 @param Path The pointer to the device path
369
370 @retval NULL the device path could not be found.
371 @return all The pointer of the NULL-terminated file path. The path
372 is callee-allocated and should be freed by the caller.
373 **/
374 CHAR16 *
375 EFIAPI
376 EfiShellGetFilePathFromDevicePath(
377 IN CONST EFI_DEVICE_PATH_PROTOCOL *Path
378 )
379 {
380 EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
381 EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
382 SHELL_MAP_LIST *MapListItem;
383 CHAR16 *PathForReturn;
384 UINTN PathSize;
385 EFI_HANDLE PathHandle;
386 EFI_HANDLE MapHandle;
387 EFI_STATUS Status;
388 FILEPATH_DEVICE_PATH *FilePath;
389
390 PathForReturn = NULL;
391 PathSize = 0;
392
393 DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL*)Path;
394 ASSERT(DevicePathCopy != NULL);
395 if (DevicePathCopy == NULL) {
396 return (NULL);
397 }
398 ///@todo BlockIo?
399 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
400
401 if (EFI_ERROR(Status)) {
402 return (NULL);
403 }
404 //
405 // check each of the device paths we have to get the root of the path
406 //
407 for ( MapListItem = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
408 ; !IsNull(&gShellMapList.Link, &MapListItem->Link)
409 ; MapListItem = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &MapListItem->Link)
410 ){
411 MapPathCopy = (EFI_DEVICE_PATH_PROTOCOL*)MapListItem->DevicePath;
412 ASSERT(MapPathCopy != NULL);
413 ///@todo BlockIo?
414 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
415 if (MapHandle == PathHandle) {
416 ASSERT((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
417 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, MapListItem->MapName, 0);
418 //
419 // go through all the remaining nodes in the device path
420 //
421 for ( FilePath = (FILEPATH_DEVICE_PATH*)DevicePathCopy
422 ; !IsDevicePathEnd (&FilePath->Header)
423 ; FilePath = (FILEPATH_DEVICE_PATH*)NextDevicePathNode (&FilePath->Header)
424 ){
425 //
426 // all the rest should be file path nodes
427 //
428 if ((DevicePathType(&FilePath->Header) != MEDIA_DEVICE_PATH) ||
429 (DevicePathSubType(&FilePath->Header) != MEDIA_FILEPATH_DP)) {
430 FreePool(PathForReturn);
431 PathForReturn = NULL;
432 ASSERT(FALSE);
433 } else {
434 //
435 // append the path part onto the filepath.
436 //
437 ASSERT((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
438 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L"\\", 1);
439 PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, FilePath->PathName, 0);
440 }
441 } // for loop of remaining nodes
442 }
443 if (PathForReturn != NULL) {
444 break;
445 }
446 } // for loop of paths to check
447 return(PathForReturn);
448 }
449
450 /**
451 Converts a file system style name to a device path.
452
453 This function converts a file system style name to a device path, by replacing any
454 mapping references to the associated device path.
455
456 @param Path the pointer to the path
457
458 @return all The pointer of the file path. The file path is callee
459 allocated and should be freed by the caller.
460 **/
461 EFI_DEVICE_PATH_PROTOCOL *
462 EFIAPI
463 EfiShellGetDevicePathFromFilePath(
464 IN CONST CHAR16 *Path
465 )
466 {
467 CHAR16 *MapName;
468 CHAR16 *NewPath;
469 CONST CHAR16 *Cwd;
470 UINTN Size;
471 CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath;
472 EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
473 EFI_DEVICE_PATH_PROTOCOL *DevicePathCopyForFree;
474 EFI_DEVICE_PATH_PROTOCOL *DevicePathForReturn;
475 EFI_HANDLE Handle;
476 EFI_STATUS Status;
477
478 MapName = NULL;
479 ASSERT(Path != NULL);
480
481 if (StrStr(Path, L":") == NULL) {
482 Cwd = EfiShellGetCurDir(NULL);
483 if (Cwd == NULL) {
484 return (NULL);
485 }
486 Size = StrSize(Cwd);
487 Size += StrSize(Path);
488 NewPath = AllocateZeroPool(Size);
489 ASSERT(NewPath != NULL);
490 StrCpy(NewPath, Cwd);
491 if ((NewPath[0] == (CHAR16)L'\\') &&
492 (NewPath[StrLen(NewPath)-1] == (CHAR16)L'\\')
493 ) {
494 ((CHAR16*)NewPath)[StrLen(NewPath)-1] = CHAR_NULL;
495 }
496 StrCat(NewPath, Path);
497 DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath);
498 FreePool(NewPath);
499 return (DevicePathForReturn);
500 }
501
502 Size = 0;
503 //
504 // find the part before (but including) the : for the map name
505 //
506 ASSERT((MapName == NULL && Size == 0) || (MapName != NULL));
507 MapName = StrnCatGrow(&MapName, &Size, Path, (StrStr(Path, L":")-Path+1));
508 if (MapName[StrLen(MapName)-1] != L':') {
509 ASSERT(FALSE);
510 return (NULL);
511 }
512
513 //
514 // look up the device path in the map
515 //
516 DevicePath = EfiShellGetDevicePathFromMap(MapName);
517 if (DevicePath == NULL) {
518 //
519 // Must have been a bad Mapname
520 //
521 return (NULL);
522 }
523
524 //
525 // make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
526 //
527 DevicePathCopyForFree = DevicePathCopy = DuplicateDevicePath(DevicePath);
528 if (DevicePathCopy == NULL) {
529 ASSERT(FALSE);
530 FreePool(MapName);
531 return (NULL);
532 }
533
534 //
535 // get the handle
536 //
537 ///@todo BlockIo?
538 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &Handle);
539 if (EFI_ERROR(Status)) {
540 if (DevicePathCopyForFree != NULL) {
541 FreePool(DevicePathCopyForFree);
542 }
543 FreePool(MapName);
544 return (NULL);
545 }
546
547 //
548 // build the full device path
549 //
550 DevicePathForReturn = FileDevicePath(Handle, Path+StrLen(MapName)+1);
551
552 FreePool(MapName);
553 if (DevicePathCopyForFree != NULL) {
554 FreePool(DevicePathCopyForFree);
555 }
556
557 return (DevicePathForReturn);
558 }
559
560 /**
561 Gets the name of the device specified by the device handle.
562
563 This function gets the user-readable name of the device specified by the device
564 handle. If no user-readable name could be generated, then *BestDeviceName will be
565 NULL and EFI_NOT_FOUND will be returned.
566
567 If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
568 device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
569 DeviceHandle.
570
571 If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
572 device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
573 If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
574 EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
575 EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
576
577 @param DeviceHandle The handle of the device.
578 @param Flags Determines the possible sources of component names.
579 Valid bits are:
580 EFI_DEVICE_NAME_USE_COMPONENT_NAME
581 EFI_DEVICE_NAME_USE_DEVICE_PATH
582 @param Language A pointer to the language specified for the device
583 name, in the same format as described in the UEFI
584 specification, Appendix M
585 @param BestDeviceName On return, points to the callee-allocated NULL-
586 terminated name of the device. If no device name
587 could be found, points to NULL. The name must be
588 freed by the caller...
589
590 @retval EFI_SUCCESS Get the name successfully.
591 @retval EFI_NOT_FOUND Fail to get the device name.
592 @retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
593 @retval EFI_INVALID_PARAMETER BestDeviceName was NULL
594 @retval EFI_INVALID_PARAMETER DeviceHandle was NULL
595 **/
596 EFI_STATUS
597 EFIAPI
598 EfiShellGetDeviceName(
599 IN EFI_HANDLE DeviceHandle,
600 IN EFI_SHELL_DEVICE_NAME_FLAGS Flags,
601 IN CHAR8 *Language,
602 OUT CHAR16 **BestDeviceName
603 )
604 {
605 EFI_STATUS Status;
606 EFI_COMPONENT_NAME2_PROTOCOL *CompName2;
607 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *DevicePathToText;
608 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
609 EFI_HANDLE *HandleList;
610 UINTN HandleCount;
611 UINTN LoopVar;
612 CHAR16 *DeviceNameToReturn;
613 CHAR8 *Lang;
614 CHAR8 *TempChar;
615
616 if (BestDeviceName == NULL ||
617 DeviceHandle == NULL
618 ){
619 return (EFI_INVALID_PARAMETER);
620 }
621
622 //
623 // make sure one of the 2 supported bits is on
624 //
625 if (((Flags & EFI_DEVICE_NAME_USE_COMPONENT_NAME) == 0) &&
626 ((Flags & EFI_DEVICE_NAME_USE_DEVICE_PATH) == 0)) {
627 return (EFI_INVALID_PARAMETER);
628 }
629
630 DeviceNameToReturn = NULL;
631 *BestDeviceName = NULL;
632 HandleList = NULL;
633 HandleCount = 0;
634 Lang = NULL;
635
636 if ((Flags & EFI_DEVICE_NAME_USE_COMPONENT_NAME) != 0) {
637 Status = ParseHandleDatabaseByRelationship(
638 NULL,
639 DeviceHandle,
640 HR_DRIVER_BINDING_HANDLE|HR_DEVICE_DRIVER,
641 &HandleCount,
642 &HandleList);
643 for (LoopVar = 0; LoopVar < HandleCount ; LoopVar++){
644 //
645 // Go through those handles until we get one that passes for GetComponentName
646 //
647 Status = gBS->OpenProtocol(
648 HandleList[LoopVar],
649 &gEfiComponentName2ProtocolGuid,
650 (VOID**)&CompName2,
651 gImageHandle,
652 NULL,
653 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
654 if (EFI_ERROR(Status)) {
655 Status = gBS->OpenProtocol(
656 HandleList[LoopVar],
657 &gEfiComponentNameProtocolGuid,
658 (VOID**)&CompName2,
659 gImageHandle,
660 NULL,
661 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
662 }
663
664 if (EFI_ERROR(Status)) {
665 continue;
666 }
667 if (Language == NULL) {
668 Lang = AllocatePool(AsciiStrSize(CompName2->SupportedLanguages));
669 if (Lang == NULL) {
670 return (EFI_OUT_OF_RESOURCES);
671 }
672 AsciiStrCpy(Lang, CompName2->SupportedLanguages);
673 TempChar = AsciiStrStr(Lang, ";");
674 if (TempChar != NULL){
675 *TempChar = CHAR_NULL;
676 }
677 } else {
678 Lang = AllocatePool(AsciiStrSize(Language));
679 if (Lang == NULL) {
680 return (EFI_OUT_OF_RESOURCES);
681 }
682 AsciiStrCpy(Lang, Language);
683 }
684 Status = CompName2->GetControllerName(CompName2, DeviceHandle, NULL, Lang, &DeviceNameToReturn);
685 FreePool(Lang);
686 Lang = NULL;
687 if (!EFI_ERROR(Status) && DeviceNameToReturn != NULL) {
688 break;
689 }
690 }
691 if (HandleList != NULL) {
692 FreePool(HandleList);
693 }
694 if (DeviceNameToReturn != NULL){
695 ASSERT(BestDeviceName != NULL);
696 StrnCatGrow(BestDeviceName, NULL, DeviceNameToReturn, 0);
697 return (EFI_SUCCESS);
698 }
699 //
700 // dont return on fail since we will try device path if that bit is on
701 //
702 }
703 if ((Flags & EFI_DEVICE_NAME_USE_DEVICE_PATH) != 0) {
704 Status = gBS->LocateProtocol(
705 &gEfiDevicePathToTextProtocolGuid,
706 NULL,
707 (VOID**)&DevicePathToText);
708 //
709 // we now have the device path to text protocol
710 //
711 if (!EFI_ERROR(Status)) {
712 Status = gBS->OpenProtocol(
713 DeviceHandle,
714 &gEfiDevicePathProtocolGuid,
715 (VOID**)&DevicePath,
716 gImageHandle,
717 NULL,
718 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
719 if (!EFI_ERROR(Status)) {
720 //
721 // use device path to text on the device path
722 //
723 *BestDeviceName = DevicePathToText->ConvertDevicePathToText(DevicePath, TRUE, TRUE);
724 return (EFI_SUCCESS);
725 }
726 }
727 }
728 //
729 // none of the selected bits worked.
730 //
731 return (EFI_NOT_FOUND);
732 }
733
734 /**
735 Opens the root directory of a device on a handle
736
737 This function opens the root directory of a device and returns a file handle to it.
738
739 @param DeviceHandle The handle of the device that contains the volume.
740 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
741 device.
742
743 @retval EFI_SUCCESS Root opened successfully.
744 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
745 could not be opened.
746 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
747 @retval EFI_DEVICE_ERROR The device had an error
748 **/
749 EFI_STATUS
750 EFIAPI
751 EfiShellOpenRootByHandle(
752 IN EFI_HANDLE DeviceHandle,
753 OUT SHELL_FILE_HANDLE *FileHandle
754 )
755 {
756 EFI_STATUS Status;
757 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
758 EFI_FILE_PROTOCOL *RealFileHandle;
759 EFI_DEVICE_PATH_PROTOCOL *DevPath;
760
761 //
762 // get the simple file system interface
763 //
764 Status = gBS->OpenProtocol(DeviceHandle,
765 &gEfiSimpleFileSystemProtocolGuid,
766 (VOID**)&SimpleFileSystem,
767 gImageHandle,
768 NULL,
769 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
770 if (EFI_ERROR(Status)) {
771 return (EFI_NOT_FOUND);
772 }
773
774 Status = gBS->OpenProtocol(DeviceHandle,
775 &gEfiDevicePathProtocolGuid,
776 (VOID**)&DevPath,
777 gImageHandle,
778 NULL,
779 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
780 if (EFI_ERROR(Status)) {
781 return (EFI_NOT_FOUND);
782 }
783 //
784 // Open the root volume now...
785 //
786 Status = SimpleFileSystem->OpenVolume(SimpleFileSystem, &RealFileHandle);
787 *FileHandle = ConvertEfiFileProtocolToShellHandle(RealFileHandle, EfiShellGetMapFromDevicePath(&DevPath));
788 return (Status);
789 }
790
791 /**
792 Opens the root directory of a device.
793
794 This function opens the root directory of a device and returns a file handle to it.
795
796 @param DevicePath Points to the device path corresponding to the device where the
797 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
798 @param FileHandle On exit, points to the file handle corresponding to the root directory on the
799 device.
800
801 @retval EFI_SUCCESS Root opened successfully.
802 @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
803 could not be opened.
804 @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
805 @retval EFI_DEVICE_ERROR The device had an error
806 **/
807 EFI_STATUS
808 EFIAPI
809 EfiShellOpenRoot(
810 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
811 OUT SHELL_FILE_HANDLE *FileHandle
812 )
813 {
814 EFI_STATUS Status;
815 EFI_HANDLE Handle;
816
817 //
818 // find the handle of the device with that device handle and the file system
819 //
820 ///@todo BlockIo?
821 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid,
822 &DevicePath,
823 &Handle);
824 if (EFI_ERROR(Status)) {
825 return (EFI_NOT_FOUND);
826 }
827
828 return (EfiShellOpenRootByHandle(Handle, FileHandle));
829 }
830
831 /**
832 Returns whether any script files are currently being processed.
833
834 @retval TRUE There is at least one script file active.
835 @retval FALSE No script files are active now.
836
837 **/
838 BOOLEAN
839 EFIAPI
840 EfiShellBatchIsActive (
841 VOID
842 )
843 {
844 if (ShellCommandGetCurrentScriptFile() == NULL) {
845 return (FALSE);
846 }
847 return (TRUE);
848 }
849
850 /**
851 Worker function to open a file based on a device path. this will open the root
852 of the volume and then traverse down to the file itself.
853
854 @param DevicePath Device Path of the file.
855 @param FileHandle Pointer to the file upon a successful return.
856 @param OpenMode mode to open file in.
857 @param Attributes the File Attributes to use when creating a new file.
858
859 @retval EFI_SUCCESS the file is open and FileHandle is valid
860 @retval EFI_UNSUPPORTED the device path cotained non-path elements
861 @retval other an error ocurred.
862 **/
863 EFI_STATUS
864 EFIAPI
865 InternalOpenFileDevicePath(
866 IN OUT EFI_DEVICE_PATH_PROTOCOL *DevicePath,
867 OUT SHELL_FILE_HANDLE *FileHandle,
868 IN UINT64 OpenMode,
869 IN UINT64 Attributes OPTIONAL
870 )
871 {
872 EFI_STATUS Status;
873 FILEPATH_DEVICE_PATH *FilePathNode;
874 EFI_HANDLE Handle;
875 SHELL_FILE_HANDLE ShellHandle;
876 EFI_FILE_PROTOCOL *Handle1;
877 EFI_FILE_PROTOCOL *Handle2;
878 EFI_DEVICE_PATH_PROTOCOL *DpCopy;
879
880 ASSERT(FileHandle != NULL);
881 *FileHandle = NULL;
882 Handle1 = NULL;
883 DpCopy = DevicePath;
884
885 Status = EfiShellOpenRoot(DevicePath, &ShellHandle);
886
887 if (!EFI_ERROR(Status)) {
888 Handle1 = ConvertShellHandleToEfiFileProtocol(ShellHandle);
889 //
890 // chop off the begining part before the file system part...
891 //
892 ///@todo BlockIo?
893 Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid,
894 &DevicePath,
895 &Handle);
896 if (!EFI_ERROR(Status)) {
897 //
898 // To access as a file system, the file path should only
899 // contain file path components. Follow the file path nodes
900 // and find the target file
901 //
902 for ( FilePathNode = (FILEPATH_DEVICE_PATH *)DevicePath
903 ; !IsDevicePathEnd (&FilePathNode->Header)
904 ; FilePathNode = (FILEPATH_DEVICE_PATH *) NextDevicePathNode (&FilePathNode->Header)
905 ){
906 //
907 // For file system access each node should be a file path component
908 //
909 if (DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH ||
910 DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP
911 ) {
912 Status = EFI_UNSUPPORTED;
913 break;
914 }
915
916 //
917 // Open this file path node
918 //
919 Handle2 = Handle1;
920 Handle1 = NULL;
921
922 //
923 // if this is the last node in the DevicePath always create (if that was requested).
924 //
925 if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode->Header)))) {
926 Status = Handle2->Open (
927 Handle2,
928 &Handle1,
929 FilePathNode->PathName,
930 OpenMode,
931 Attributes
932 );
933 } else {
934
935 //
936 // This is not the last node and we dont want to 'create' existing
937 // directory entries...
938 //
939
940 //
941 // open without letting it create
942 // prevents error on existing files/directories
943 //
944 Status = Handle2->Open (
945 Handle2,
946 &Handle1,
947 FilePathNode->PathName,
948 OpenMode &~EFI_FILE_MODE_CREATE,
949 Attributes
950 );
951 //
952 // if above failed now open and create the 'item'
953 // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)
954 //
955 if ((EFI_ERROR (Status)) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)) {
956 Status = Handle2->Open (
957 Handle2,
958 &Handle1,
959 FilePathNode->PathName,
960 OpenMode,
961 Attributes
962 );
963 }
964 }
965 //
966 // Close the last node
967 //
968 Handle2->Close (Handle2);
969
970 //
971 // If there's been an error, stop
972 //
973 if (EFI_ERROR (Status)) {
974 break;
975 }
976 } // for loop
977 }
978 }
979 if (EFI_ERROR(Status)) {
980 if (Handle1 != NULL) {
981 Handle1->Close(Handle1);
982 }
983 } else {
984 *FileHandle = ConvertEfiFileProtocolToShellHandle(Handle1, ShellFileHandleGetPath(ShellHandle));
985 }
986 return (Status);
987 }
988
989 /**
990 Creates a file or directory by name.
991
992 This function creates an empty new file or directory with the specified attributes and
993 returns the new file's handle. If the file already exists and is read-only, then
994 EFI_INVALID_PARAMETER will be returned.
995
996 If the file already existed, it is truncated and its attributes updated. If the file is
997 created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.
998
999 If the file name begins with >v, then the file handle which is returned refers to the
1000 shell environment variable with the specified name. If the shell environment variable
1001 already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.
1002
1003 @param FileName Pointer to NULL-terminated file path
1004 @param FileAttribs The new file's attrbiutes. the different attributes are
1005 described in EFI_FILE_PROTOCOL.Open().
1006 @param FileHandle On return, points to the created file handle or directory's handle
1007
1008 @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.
1009 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1010 @retval EFI_UNSUPPORTED could not open the file path
1011 @retval EFI_NOT_FOUND the specified file could not be found on the devide, or could not
1012 file the file system on the device.
1013 @retval EFI_NO_MEDIA the device has no medium.
1014 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1015 longer supported.
1016 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1017 the DirName.
1018 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
1019 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1020 when the media is write-protected.
1021 @retval EFI_ACCESS_DENIED The service denied access to the file.
1022 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
1023 @retval EFI_VOLUME_FULL The volume is full.
1024 **/
1025 EFI_STATUS
1026 EFIAPI
1027 EfiShellCreateFile(
1028 IN CONST CHAR16 *FileName,
1029 IN UINT64 FileAttribs,
1030 OUT SHELL_FILE_HANDLE *FileHandle
1031 )
1032 {
1033 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1034 EFI_STATUS Status;
1035
1036 //
1037 // Is this for an environment variable
1038 // do we start with >v
1039 //
1040 if (StrStr(FileName, L">v") == FileName) {
1041 if (!IsVolatileEnv(FileName+2)) {
1042 return (EFI_INVALID_PARAMETER);
1043 }
1044 *FileHandle = CreateFileInterfaceEnv(FileName+2);
1045 return (EFI_SUCCESS);
1046 }
1047
1048 //
1049 // We are opening a regular file.
1050 //
1051 DevicePath = EfiShellGetDevicePathFromFilePath(FileName);
1052 if (DevicePath == NULL) {
1053 return (EFI_NOT_FOUND);
1054 }
1055
1056 Status = InternalOpenFileDevicePath(DevicePath, FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, FileAttribs); // 0 = no specific file attributes
1057 FreePool(DevicePath);
1058
1059 return(Status);
1060 }
1061
1062 /**
1063 Opens a file or a directory by file name.
1064
1065 This function opens the specified file in the specified OpenMode and returns a file
1066 handle.
1067 If the file name begins with >v, then the file handle which is returned refers to the
1068 shell environment variable with the specified name. If the shell environment variable
1069 exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then
1070 EFI_INVALID_PARAMETER is returned.
1071
1072 If the file name is >i, then the file handle which is returned refers to the standard
1073 input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER
1074 is returned.
1075
1076 If the file name is >o, then the file handle which is returned refers to the standard
1077 output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1078 is returned.
1079
1080 If the file name is >e, then the file handle which is returned refers to the standard
1081 error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
1082 is returned.
1083
1084 If the file name is NUL, then the file handle that is returned refers to the standard NUL
1085 file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is
1086 returned.
1087
1088 If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the
1089 FileHandle is NULL.
1090
1091 @param FileName Points to the NULL-terminated UCS-2 encoded file name.
1092 @param FileHandle On return, points to the file handle.
1093 @param OpenMode File open mode. Either EFI_FILE_MODE_READ or
1094 EFI_FILE_MODE_WRITE from section 12.4 of the UEFI
1095 Specification.
1096 @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.
1097 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.
1098 @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.
1099 @retval EFI_NOT_FOUND The specified file could not be found on the device or the file
1100 system could not be found on the device. FileHandle is NULL.
1101 @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.
1102 @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
1103 longer supported. FileHandle is NULL.
1104 @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
1105 the FileName. FileHandle is NULL.
1106 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.
1107 @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
1108 when the media is write-protected. FileHandle is NULL.
1109 @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.
1110 @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle
1111 is NULL.
1112 @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.
1113 **/
1114 EFI_STATUS
1115 EFIAPI
1116 EfiShellOpenFileByName(
1117 IN CONST CHAR16 *FileName,
1118 OUT SHELL_FILE_HANDLE *FileHandle,
1119 IN UINT64 OpenMode
1120 )
1121 {
1122 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1123 EFI_STATUS Status;
1124
1125 *FileHandle = NULL;
1126
1127 //
1128 // Is this for StdIn
1129 //
1130 if (StrCmp(FileName, L">i") == 0) {
1131 //
1132 // make sure not writing to StdIn
1133 //
1134 if ((OpenMode & EFI_FILE_MODE_WRITE) != 0) {
1135 return (EFI_INVALID_PARAMETER);
1136 }
1137 *FileHandle = ShellInfoObject.NewShellParametersProtocol->StdIn;
1138 ASSERT(*FileHandle != NULL);
1139 return (EFI_SUCCESS);
1140 }
1141
1142 //
1143 // Is this for StdOut
1144 //
1145 if (StrCmp(FileName, L">o") == 0) {
1146 //
1147 // make sure not writing to StdIn
1148 //
1149 if ((OpenMode & EFI_FILE_MODE_READ) != 0) {
1150 return (EFI_INVALID_PARAMETER);
1151 }
1152 *FileHandle = &FileInterfaceStdOut;
1153 return (EFI_SUCCESS);
1154 }
1155
1156 //
1157 // Is this for NUL file
1158 //
1159 if (StrCmp(FileName, L"NUL") == 0) {
1160 *FileHandle = &FileInterfaceNulFile;
1161 return (EFI_SUCCESS);
1162 }
1163
1164 //
1165 // Is this for StdErr
1166 //
1167 if (StrCmp(FileName, L">e") == 0) {
1168 //
1169 // make sure not writing to StdIn
1170 //
1171 if ((OpenMode & EFI_FILE_MODE_READ) != 0) {
1172 return (EFI_INVALID_PARAMETER);
1173 }
1174 *FileHandle = &FileInterfaceStdErr;
1175 return (EFI_SUCCESS);
1176 }
1177
1178 //
1179 // Is this for an environment variable
1180 // do we start with >v
1181 //
1182 if (StrStr(FileName, L">v") == FileName) {
1183 if (!IsVolatileEnv(FileName+2) &&
1184 ((OpenMode & EFI_FILE_MODE_WRITE) != 0)) {
1185 return (EFI_INVALID_PARAMETER);
1186 }
1187 *FileHandle = CreateFileInterfaceEnv(FileName+2);
1188 return (EFI_SUCCESS);
1189 }
1190
1191 //
1192 // We are opening a regular file.
1193 //
1194 DevicePath = EfiShellGetDevicePathFromFilePath(FileName);
1195 // DEBUG_CODE(InternalShellProtocolDebugPrintMessage (NULL, DevicePath););
1196 if (DevicePath == NULL) {
1197 return (EFI_NOT_FOUND);
1198 }
1199
1200 //
1201 // Copy the device path, open the file, then free the memory
1202 //
1203 Status = InternalOpenFileDevicePath(DevicePath, FileHandle, OpenMode, 0); // 0 = no specific file attributes
1204 FreePool(DevicePath);
1205
1206 return(Status);
1207 }
1208
1209 /**
1210 Deletes the file specified by the file name.
1211
1212 This function deletes a file.
1213
1214 @param FileName Points to the NULL-terminated file name.
1215
1216 @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.
1217 @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
1218 @sa EfiShellCreateFile
1219 **/
1220 EFI_STATUS
1221 EFIAPI
1222 EfiShellDeleteFileByName(
1223 IN CONST CHAR16 *FileName
1224 )
1225 {
1226 SHELL_FILE_HANDLE FileHandle;
1227 EFI_STATUS Status;
1228
1229 //
1230 // get a handle to the file
1231 //
1232 Status = EfiShellCreateFile(FileName,
1233 0,
1234 &FileHandle);
1235 if (EFI_ERROR(Status)) {
1236 return (Status);
1237 }
1238 //
1239 // now delete the file
1240 //
1241 return (ShellInfoObject.NewEfiShellProtocol->DeleteFile(FileHandle));
1242 }
1243
1244 /**
1245 Disables the page break output mode.
1246 **/
1247 VOID
1248 EFIAPI
1249 EfiShellDisablePageBreak (
1250 VOID
1251 )
1252 {
1253 ShellInfoObject.PageBreakEnabled = FALSE;
1254 }
1255
1256 /**
1257 Enables the page break output mode.
1258 **/
1259 VOID
1260 EFIAPI
1261 EfiShellEnablePageBreak (
1262 VOID
1263 )
1264 {
1265 ShellInfoObject.PageBreakEnabled = TRUE;
1266 }
1267
1268 /**
1269 internal worker function to load and run an image via device path.
1270
1271 @param ParentImageHandle A handle of the image that is executing the specified
1272 command line.
1273 @param DevicePath device path of the file to execute
1274 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1275 containing the command line. If NULL then the command-
1276 line will be empty.
1277 @param Environment Points to a NULL-terminated array of environment
1278 variables with the format 'x=y', where x is the
1279 environment variable name and y is the value. If this
1280 is NULL, then the current shell environment is used.
1281 @param StatusCode Points to the status code returned by the command.
1282
1283 @retval EFI_SUCCESS The command executed successfully. The status code
1284 returned by the command is pointed to by StatusCode.
1285 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1286 @retval EFI_OUT_OF_RESOURCES Out of resources.
1287 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1288 **/
1289 EFI_STATUS
1290 EFIAPI
1291 InternalShellExecuteDevicePath(
1292 IN CONST EFI_HANDLE *ParentImageHandle,
1293 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
1294 IN CONST CHAR16 *CommandLine OPTIONAL,
1295 IN CONST CHAR16 **Environment OPTIONAL,
1296 OUT EFI_STATUS *StatusCode OPTIONAL
1297 )
1298 {
1299 EFI_STATUS Status;
1300 EFI_HANDLE NewHandle;
1301 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
1302 LIST_ENTRY OrigEnvs;
1303 EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol;
1304
1305 if (ParentImageHandle == NULL) {
1306 return (EFI_INVALID_PARAMETER);
1307 }
1308
1309 InitializeListHead(&OrigEnvs);
1310
1311 NewHandle = NULL;
1312
1313 //
1314 // Load the image with:
1315 // FALSE - not from boot manager and NULL, 0 being not already in memory
1316 //
1317 Status = gBS->LoadImage(
1318 FALSE,
1319 *ParentImageHandle,
1320 (EFI_DEVICE_PATH_PROTOCOL*)DevicePath,
1321 NULL,
1322 0,
1323 &NewHandle);
1324
1325 if (EFI_ERROR(Status)) {
1326 if (NewHandle != NULL) {
1327 gBS->UnloadImage(NewHandle);
1328 }
1329 return (Status);
1330 }
1331 Status = gBS->OpenProtocol(
1332 NewHandle,
1333 &gEfiLoadedImageProtocolGuid,
1334 (VOID**)&LoadedImage,
1335 gImageHandle,
1336 NULL,
1337 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
1338
1339 if (!EFI_ERROR(Status)) {
1340 ASSERT(LoadedImage->LoadOptionsSize == 0);
1341 if (CommandLine != NULL) {
1342 LoadedImage->LoadOptionsSize = (UINT32)StrSize(CommandLine);
1343 LoadedImage->LoadOptions = (VOID*)CommandLine;
1344 }
1345
1346 //
1347 // Save our current environment settings for later restoration if necessary
1348 //
1349 if (Environment != NULL) {
1350 Status = GetEnvironmentVariableList(&OrigEnvs);
1351 if (!EFI_ERROR(Status)) {
1352 Status = SetEnvironmentVariables(Environment);
1353 }
1354 }
1355
1356 //
1357 // Initialize and install a shell parameters protocol on the image.
1358 //
1359 ShellParamsProtocol.StdIn = ShellInfoObject.NewShellParametersProtocol->StdIn;
1360 ShellParamsProtocol.StdOut = ShellInfoObject.NewShellParametersProtocol->StdOut;
1361 ShellParamsProtocol.StdErr = ShellInfoObject.NewShellParametersProtocol->StdErr;
1362 Status = UpdateArgcArgv(&ShellParamsProtocol, CommandLine, NULL, NULL);
1363 ASSERT_EFI_ERROR(Status);
1364 Status = gBS->InstallProtocolInterface(&NewHandle, &gEfiShellParametersProtocolGuid, EFI_NATIVE_INTERFACE, &ShellParamsProtocol);
1365 ASSERT_EFI_ERROR(Status);
1366
1367 ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)
1368
1369 //
1370 // now start the image and if the caller wanted the return code pass it to them...
1371 //
1372 if (!EFI_ERROR(Status)) {
1373 if (StatusCode != NULL) {
1374 *StatusCode = gBS->StartImage(NewHandle, NULL, NULL);
1375 } else {
1376 Status = gBS->StartImage(NewHandle, NULL, NULL);
1377 }
1378 }
1379
1380 //
1381 // Cleanup (and dont overwrite errors)
1382 //
1383 if (EFI_ERROR(Status)) {
1384 gBS->UninstallProtocolInterface(NewHandle, &gEfiShellParametersProtocolGuid, &ShellParamsProtocol);
1385 } else {
1386 Status = gBS->UninstallProtocolInterface(NewHandle, &gEfiShellParametersProtocolGuid, &ShellParamsProtocol);
1387 ASSERT_EFI_ERROR(Status);
1388 }
1389 }
1390
1391 if (!IsListEmpty(&OrigEnvs)) {
1392 if (EFI_ERROR(Status)) {
1393 SetEnvironmentVariableList(&OrigEnvs);
1394 } else {
1395 Status = SetEnvironmentVariableList(&OrigEnvs);
1396 }
1397 }
1398
1399 return(Status);
1400 }
1401 /**
1402 Execute the command line.
1403
1404 This function creates a nested instance of the shell and executes the specified
1405 command (CommandLine) with the specified environment (Environment). Upon return,
1406 the status code returned by the specified command is placed in StatusCode.
1407
1408 If Environment is NULL, then the current environment is used and all changes made
1409 by the commands executed will be reflected in the current environment. If the
1410 Environment is non-NULL, then the changes made will be discarded.
1411
1412 The CommandLine is executed from the current working directory on the current
1413 device.
1414
1415 @param ParentImageHandle A handle of the image that is executing the specified
1416 command line.
1417 @param CommandLine Points to the NULL-terminated UCS-2 encoded string
1418 containing the command line. If NULL then the command-
1419 line will be empty.
1420 @param Environment Points to a NULL-terminated array of environment
1421 variables with the format 'x=y', where x is the
1422 environment variable name and y is the value. If this
1423 is NULL, then the current shell environment is used.
1424 @param StatusCode Points to the status code returned by the command.
1425
1426 @retval EFI_SUCCESS The command executed successfully. The status code
1427 returned by the command is pointed to by StatusCode.
1428 @retval EFI_INVALID_PARAMETER The parameters are invalid.
1429 @retval EFI_OUT_OF_RESOURCES Out of resources.
1430 @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
1431 @retval EFI_UNSUPPORTED The support level required for this function is not present.
1432
1433 @sa InternalShellExecuteDevicePath
1434 **/
1435 EFI_STATUS
1436 EFIAPI
1437 EfiShellExecute(
1438 IN EFI_HANDLE *ParentImageHandle,
1439 IN CHAR16 *CommandLine OPTIONAL,
1440 IN CHAR16 **Environment OPTIONAL,
1441 OUT EFI_STATUS *StatusCode OPTIONAL
1442 )
1443 {
1444 EFI_STATUS Status;
1445 CHAR16 *Temp;
1446 EFI_DEVICE_PATH_PROTOCOL *DevPath;
1447 UINTN Size;
1448
1449 if ((PcdGet8(PcdShellSupportLevel) < 1)) {
1450 return (EFI_UNSUPPORTED);
1451 }
1452
1453 DevPath = AppendDevicePath (ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);
1454
1455 DEBUG_CODE_BEGIN();
1456 Temp = gDevPathToText->ConvertDevicePathToText(ShellInfoObject.FileDevPath, TRUE, TRUE);
1457 FreePool(Temp);
1458 Temp = gDevPathToText->ConvertDevicePathToText(ShellInfoObject.ImageDevPath, TRUE, TRUE);
1459 FreePool(Temp);
1460 Temp = gDevPathToText->ConvertDevicePathToText(DevPath, TRUE, TRUE);
1461 FreePool(Temp);
1462 DEBUG_CODE_END();
1463
1464 Temp = NULL;
1465 Size = 0;
1466 ASSERT((Temp == NULL && Size == 0) || (Temp != NULL));
1467 StrnCatGrow(&Temp, &Size, L"Shell.efi ", 0);
1468 StrnCatGrow(&Temp, &Size, CommandLine, 0);
1469
1470 Status = InternalShellExecuteDevicePath(
1471 ParentImageHandle,
1472 DevPath,
1473 Temp,
1474 (CONST CHAR16**)Environment,
1475 StatusCode);
1476
1477 //
1478 // de-allocate and return
1479 //
1480 FreePool(DevPath);
1481 FreePool(Temp);
1482 return(Status);
1483 }
1484
1485 /**
1486 Utility cleanup function for EFI_SHELL_FILE_INFO objects.
1487
1488 1) frees all pointers (non-NULL)
1489 2) Closes the SHELL_FILE_HANDLE
1490
1491 @param FileListNode pointer to the list node to free
1492 **/
1493 VOID
1494 EFIAPI
1495 InternalFreeShellFileInfoNode(
1496 IN EFI_SHELL_FILE_INFO *FileListNode
1497 )
1498 {
1499 if (FileListNode->Info != NULL) {
1500 FreePool((VOID*)FileListNode->Info);
1501 }
1502 if (FileListNode->FileName != NULL) {
1503 FreePool((VOID*)FileListNode->FileName);
1504 }
1505 if (FileListNode->FullName != NULL) {
1506 FreePool((VOID*)FileListNode->FullName);
1507 }
1508 if (FileListNode->Handle != NULL) {
1509 ShellInfoObject.NewEfiShellProtocol->CloseFile(FileListNode->Handle);
1510 }
1511 FreePool(FileListNode);
1512 }
1513 /**
1514 Frees the file list.
1515
1516 This function cleans up the file list and any related data structures. It has no
1517 impact on the files themselves.
1518
1519 @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is
1520 defined in OpenFileList()
1521
1522 @retval EFI_SUCCESS Free the file list successfully.
1523 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1524 **/
1525 EFI_STATUS
1526 EFIAPI
1527 EfiShellFreeFileList(
1528 IN EFI_SHELL_FILE_INFO **FileList
1529 )
1530 {
1531 EFI_SHELL_FILE_INFO *ShellFileListItem;
1532
1533 if (FileList == NULL || *FileList == NULL) {
1534 return (EFI_INVALID_PARAMETER);
1535 }
1536
1537 for ( ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)
1538 ; !IsListEmpty(&(*FileList)->Link)
1539 ; ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)
1540 ){
1541 RemoveEntryList(&ShellFileListItem->Link);
1542 InternalFreeShellFileInfoNode(ShellFileListItem);
1543 }
1544 return(EFI_SUCCESS);
1545 }
1546
1547 /**
1548 Deletes the duplicate file names files in the given file list.
1549
1550 This function deletes the reduplicate files in the given file list.
1551
1552 @param FileList A pointer to the first entry in the file list.
1553
1554 @retval EFI_SUCCESS Always success.
1555 @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
1556 **/
1557 EFI_STATUS
1558 EFIAPI
1559 EfiShellRemoveDupInFileList(
1560 IN EFI_SHELL_FILE_INFO **FileList
1561 )
1562 {
1563 EFI_SHELL_FILE_INFO *ShellFileListItem;
1564 EFI_SHELL_FILE_INFO *ShellFileListItem2;
1565
1566 if (FileList == NULL || *FileList == NULL) {
1567 return (EFI_INVALID_PARAMETER);
1568 }
1569 for ( ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)
1570 ; !IsNull(&(*FileList)->Link, &ShellFileListItem->Link)
1571 ; ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)
1572 ){
1573 for ( ShellFileListItem2 = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)
1574 ; !IsNull(&(*FileList)->Link, &ShellFileListItem2->Link)
1575 ; ShellFileListItem2 = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem2->Link)
1576 ){
1577 if (gUnicodeCollation->StriColl(
1578 gUnicodeCollation,
1579 (CHAR16*)ShellFileListItem->FullName,
1580 (CHAR16*)ShellFileListItem2->FullName) == 0
1581 ){
1582 RemoveEntryList(&ShellFileListItem2->Link);
1583 InternalFreeShellFileInfoNode(ShellFileListItem2);
1584 }
1585 }
1586 }
1587 return (EFI_SUCCESS);
1588 }
1589 /**
1590 Allocates and duplicates a EFI_SHELL_FILE_INFO node.
1591
1592 @param[in] Node The node to copy from.
1593 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.
1594
1595 @retval NULL a memory allocation error ocurred
1596 @return != NULL a pointer to the new node
1597 **/
1598 EFI_SHELL_FILE_INFO*
1599 EFIAPI
1600 InternalDuplicateShellFileInfo(
1601 IN EFI_SHELL_FILE_INFO *Node,
1602 IN BOOLEAN Save
1603 )
1604 {
1605 EFI_SHELL_FILE_INFO *NewNode;
1606
1607 NewNode = AllocatePool(sizeof(EFI_SHELL_FILE_INFO));
1608 if (NewNode == NULL) {
1609 return (NULL);
1610 }
1611 NewNode->FullName = AllocateZeroPool(StrSize(Node->FullName));
1612
1613 NewNode->FileName = AllocateZeroPool(StrSize(Node->FileName));
1614 NewNode->Info = AllocatePool((UINTN)Node->Info->Size);
1615 if ( NewNode->FullName == NULL
1616 || NewNode->FileName == NULL
1617 || NewNode->Info == NULL
1618 ){
1619 return(NULL);
1620 }
1621 NewNode->Status = Node->Status;
1622 NewNode->Handle = Node->Handle;
1623 if (!Save) {
1624 Node->Handle = NULL;
1625 }
1626 StrCpy((CHAR16*)NewNode->FullName, Node->FullName);
1627 StrCpy((CHAR16*)NewNode->FileName, Node->FileName);
1628 CopyMem(NewNode->Info, Node->Info, (UINTN)Node->Info->Size);
1629
1630 return(NewNode);
1631 }
1632
1633 /**
1634 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation
1635 failed it will return NULL.
1636
1637 @param[in] BasePath the Path to prepend onto filename for FullPath
1638 @param[in] Status Status member initial value.
1639 @param[in] FullName FullName member initial value.
1640 @param[in] FileName FileName member initial value.
1641 @param[in] Handle Handle member initial value.
1642 @param[in] Info Info struct to copy.
1643
1644 @retval NULL An error ocurred.
1645 @return a pointer to the newly allocated structure.
1646 **/
1647 EFI_SHELL_FILE_INFO *
1648 EFIAPI
1649 CreateAndPopulateShellFileInfo(
1650 IN CONST CHAR16 *BasePath,
1651 IN CONST EFI_STATUS Status,
1652 IN CONST CHAR16 *FullName,
1653 IN CONST CHAR16 *FileName,
1654 IN CONST SHELL_FILE_HANDLE Handle,
1655 IN CONST EFI_FILE_INFO *Info
1656 )
1657 {
1658 EFI_SHELL_FILE_INFO *ShellFileListItem;
1659 CHAR16 *TempString;
1660 UINTN Size;
1661
1662 TempString = NULL;
1663 Size = 0;
1664
1665 ShellFileListItem = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1666 if (ShellFileListItem == NULL) {
1667 return (NULL);
1668 }
1669 if (Info != NULL) {
1670 ShellFileListItem->Info = AllocateZeroPool((UINTN)Info->Size);
1671 if (ShellFileListItem->Info == NULL) {
1672 FreePool(ShellFileListItem);
1673 return (NULL);
1674 }
1675 CopyMem(ShellFileListItem->Info, Info, (UINTN)Info->Size);
1676 } else {
1677 ShellFileListItem->Info = NULL;
1678 }
1679 if (FileName != NULL) {
1680 ASSERT(TempString == NULL);
1681 ShellFileListItem->FileName = StrnCatGrow(&TempString, 0, FileName, 0);
1682 if (ShellFileListItem->FileName == NULL) {
1683 FreePool(ShellFileListItem->Info);
1684 FreePool(ShellFileListItem);
1685 return (NULL);
1686 }
1687 } else {
1688 ShellFileListItem->FileName = NULL;
1689 }
1690 Size = 0;
1691 TempString = NULL;
1692 if (BasePath != NULL) {
1693 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));
1694 TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);
1695 if (TempString == NULL) {
1696 FreePool((VOID*)ShellFileListItem->FileName);
1697 FreePool(ShellFileListItem->Info);
1698 FreePool(ShellFileListItem);
1699 return (NULL);
1700 }
1701 }
1702 if (ShellFileListItem->FileName != NULL) {
1703 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));
1704 TempString = StrnCatGrow(&TempString, &Size, ShellFileListItem->FileName, 0);
1705 if (TempString == NULL) {
1706 FreePool((VOID*)ShellFileListItem->FileName);
1707 FreePool(ShellFileListItem->Info);
1708 FreePool(ShellFileListItem);
1709 return (NULL);
1710 }
1711 }
1712
1713 ShellFileListItem->FullName = TempString;
1714 ShellFileListItem->Status = Status;
1715 ShellFileListItem->Handle = Handle;
1716
1717 return (ShellFileListItem);
1718 }
1719
1720 /**
1721 Find all files in a specified directory.
1722
1723 @param FileDirHandle Handle of the directory to search.
1724 @param FileList On return, points to the list of files in the directory
1725 or NULL if there are no files in the directory.
1726
1727 @retval EFI_SUCCESS File information was returned successfully.
1728 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.
1729 @retval EFI_DEVICE_ERROR The device reported an error.
1730 @retval EFI_NO_MEDIA The device media is not present.
1731 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.
1732 @return An error from FileHandleGetFileName().
1733 **/
1734 EFI_STATUS
1735 EFIAPI
1736 EfiShellFindFilesInDir(
1737 IN SHELL_FILE_HANDLE FileDirHandle,
1738 OUT EFI_SHELL_FILE_INFO **FileList
1739 )
1740 {
1741 EFI_SHELL_FILE_INFO *ShellFileList;
1742 EFI_SHELL_FILE_INFO *ShellFileListItem;
1743 EFI_FILE_INFO *FileInfo;
1744 EFI_STATUS Status;
1745 BOOLEAN NoFile;
1746 CHAR16 *TempString;
1747 CHAR16 *BasePath;
1748 UINTN Size;
1749 CHAR16 *TempSpot;
1750
1751 Status = FileHandleGetFileName(FileDirHandle, &BasePath);
1752 if (EFI_ERROR(Status)) {
1753 return (Status);
1754 }
1755
1756 if (ShellFileHandleGetPath(FileDirHandle) != NULL) {
1757 TempString = NULL;
1758 Size = 0;
1759 TempString = StrnCatGrow(&TempString, &Size, ShellFileHandleGetPath(FileDirHandle), 0);
1760 TempSpot = StrStr(TempString, L";");
1761
1762 if (TempSpot != NULL) {
1763 *TempSpot = CHAR_NULL;
1764 }
1765
1766 TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);
1767 BasePath = TempString;
1768 }
1769
1770 NoFile = FALSE;
1771 ShellFileList = NULL;
1772 ShellFileListItem = NULL;
1773 FileInfo = NULL;
1774 Status = EFI_SUCCESS;
1775
1776
1777 for ( Status = FileHandleFindFirstFile(FileDirHandle, &FileInfo)
1778 ; !EFI_ERROR(Status) && !NoFile
1779 ; Status = FileHandleFindNextFile(FileDirHandle, FileInfo, &NoFile)
1780 ){
1781 TempString = NULL;
1782 Size = 0;
1783 //
1784 // allocate a new EFI_SHELL_FILE_INFO and populate it...
1785 //
1786 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));
1787 TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);
1788 TempString = StrnCatGrow(&TempString, &Size, FileInfo->FileName, 0);
1789 ShellFileListItem = CreateAndPopulateShellFileInfo(
1790 BasePath,
1791 EFI_SUCCESS, // success since we didnt fail to open it...
1792 TempString,
1793 FileInfo->FileName,
1794 NULL, // no handle since not open
1795 FileInfo);
1796
1797 if (ShellFileList == NULL) {
1798 ShellFileList = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));
1799 ASSERT(ShellFileList != NULL);
1800 InitializeListHead(&ShellFileList->Link);
1801 }
1802 InsertTailList(&ShellFileList->Link, &ShellFileListItem->Link);
1803 }
1804 if (EFI_ERROR(Status)) {
1805 EfiShellFreeFileList(&ShellFileList);
1806 *FileList = NULL;
1807 } else {
1808 *FileList = ShellFileList;
1809 }
1810 SHELL_FREE_NON_NULL(BasePath);
1811 return(Status);
1812 }
1813
1814 /**
1815 Updates a file name to be preceeded by the mapped drive name
1816
1817 @param[in] BasePath the Mapped drive name to prepend
1818 @param[in,out] Path pointer to pointer to the file name to update.
1819
1820 @retval EFI_SUCCESS
1821 @retval EFI_OUT_OF_RESOURCES
1822 **/
1823 EFI_STATUS
1824 EFIAPI
1825 UpdateFileName(
1826 IN CONST CHAR16 *BasePath,
1827 IN OUT CHAR16 **Path
1828 )
1829 {
1830 CHAR16 *Path2;
1831 UINTN Path2Size;
1832
1833 Path2Size = 0;
1834 Path2 = NULL;
1835
1836 ASSERT(Path != NULL);
1837 ASSERT(*Path != NULL);
1838 ASSERT(BasePath != NULL);
1839
1840 //
1841 // convert a local path to an absolute path
1842 //
1843 if (StrStr(*Path, L":") == NULL) {
1844 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));
1845 StrnCatGrow(&Path2, &Path2Size, BasePath, 0);
1846 if (Path2 == NULL) {
1847 return (EFI_OUT_OF_RESOURCES);
1848 }
1849 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));
1850 StrnCatGrow(&Path2, &Path2Size, (*Path)[0] == L'\\'?(*Path) + 1 :*Path, 0);
1851 if (Path2 == NULL) {
1852 return (EFI_OUT_OF_RESOURCES);
1853 }
1854 }
1855
1856 FreePool(*Path);
1857 (*Path) = Path2;
1858
1859 return (EFI_SUCCESS);
1860 }
1861
1862 /**
1863 If FileHandle is a directory then the function reads from FileHandle and reads in
1864 each of the FileInfo structures. If one of them matches the Pattern's first
1865 "level" then it opens that handle and calls itself on that handle.
1866
1867 If FileHandle is a file and matches all of the remaining Pattern (which would be
1868 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.
1869
1870 if FileList is NULL, then ASSERT
1871 if FilePattern is NULL, then ASSERT
1872 if UnicodeCollation is NULL, then ASSERT
1873 if FileHandle is NULL, then ASSERT
1874
1875 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call
1876 FreeFileList with FileList.
1877
1878 @param[in] FilePattern The FilePattern to check against.
1879 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure
1880 @param[in] FileHandle The FileHandle to start with
1881 @param[in,out] FileList pointer to pointer to list of found files.
1882 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.
1883
1884 @retval EFI_SUCCESS all files were found and the FileList contains a list.
1885 @retval EFI_NOT_FOUND no files were found
1886 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
1887 **/
1888 EFI_STATUS
1889 EFIAPI
1890 ShellSearchHandle(
1891 IN CONST CHAR16 *FilePattern,
1892 IN EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation,
1893 IN SHELL_FILE_HANDLE FileHandle,
1894 IN OUT EFI_SHELL_FILE_INFO **FileList,
1895 IN CONST EFI_SHELL_FILE_INFO *ParentNode OPTIONAL
1896 )
1897 {
1898 EFI_STATUS Status;
1899 CONST CHAR16 *NextFilePatternStart;
1900 CHAR16 *CurrentFilePattern;
1901 EFI_SHELL_FILE_INFO *ShellInfo;
1902 EFI_SHELL_FILE_INFO *ShellInfoNode;
1903 EFI_SHELL_FILE_INFO *NewShellNode;
1904 BOOLEAN Directory;
1905
1906 if ( FilePattern == NULL
1907 || UnicodeCollation == NULL
1908 || FileList == NULL
1909 ){
1910 return (EFI_INVALID_PARAMETER);
1911 }
1912 ShellInfo = NULL;
1913 CurrentFilePattern = NULL;
1914
1915 if (*FilePattern == L'\\') {
1916 FilePattern++;
1917 }
1918
1919 for( NextFilePatternStart = FilePattern
1920 ; *NextFilePatternStart != CHAR_NULL && *NextFilePatternStart != L'\\'
1921 ; NextFilePatternStart++);
1922
1923 CurrentFilePattern = AllocateZeroPool((NextFilePatternStart-FilePattern+1)*sizeof(CHAR16));
1924 ASSERT(CurrentFilePattern != NULL);
1925 StrnCpy(CurrentFilePattern, FilePattern, NextFilePatternStart-FilePattern);
1926
1927 if (CurrentFilePattern[0] == CHAR_NULL
1928 &&NextFilePatternStart[0] == CHAR_NULL
1929 ){
1930 //
1931 // Add the current parameter FileHandle to the list, then end...
1932 //
1933 if (ParentNode == NULL) {
1934 Status = EFI_INVALID_PARAMETER;
1935 } else {
1936 NewShellNode = InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO*)ParentNode, TRUE);
1937 if (NewShellNode == NULL) {
1938 Status = EFI_OUT_OF_RESOURCES;
1939 } else {
1940 NewShellNode->Handle = NULL;
1941 if (*FileList == NULL) {
1942 *FileList = AllocatePool(sizeof(EFI_SHELL_FILE_INFO));
1943 InitializeListHead(&((*FileList)->Link));
1944 }
1945
1946 //
1947 // Add to the returning to use list
1948 //
1949 InsertTailList(&(*FileList)->Link, &NewShellNode->Link);
1950
1951 Status = EFI_SUCCESS;
1952 }
1953 }
1954 } else {
1955 Status = EfiShellFindFilesInDir(FileHandle, &ShellInfo);
1956
1957 if (!EFI_ERROR(Status)){
1958 if (StrStr(NextFilePatternStart, L"\\") != NULL){
1959 Directory = TRUE;
1960 } else {
1961 Directory = FALSE;
1962 }
1963 for ( ShellInfoNode = (EFI_SHELL_FILE_INFO*)GetFirstNode(&ShellInfo->Link)
1964 ; !IsNull (&ShellInfo->Link, &ShellInfoNode->Link)
1965 ; ShellInfoNode = (EFI_SHELL_FILE_INFO*)GetNextNode(&ShellInfo->Link, &ShellInfoNode->Link)
1966 ){
1967 if (UnicodeCollation->MetaiMatch(UnicodeCollation, (CHAR16*)ShellInfoNode->FileName, CurrentFilePattern)){
1968 if (Directory){
1969 //
1970 // should be a directory
1971 //
1972
1973 //
1974 // don't open the . and .. directories
1975 //
1976 if ( (StrCmp(ShellInfoNode->FileName, L".") != 0)
1977 && (StrCmp(ShellInfoNode->FileName, L"..") != 0)
1978 ){
1979 //
1980 //
1981 //
1982 ASSERT_EFI_ERROR(Status);
1983 if (EFI_ERROR(Status)) {
1984 break;
1985 }
1986 //
1987 // Open the directory since we need that handle in the next recursion.
1988 //
1989 ShellInfoNode->Status = EfiShellOpenFileByName (ShellInfoNode->FullName, &ShellInfoNode->Handle, EFI_FILE_MODE_READ);
1990
1991 //
1992 // recurse with the next part of the pattern
1993 //
1994 Status = ShellSearchHandle(NextFilePatternStart, UnicodeCollation, ShellInfoNode->Handle, FileList, ShellInfoNode);
1995 }
1996 } else {
1997 //
1998 // should be a file
1999 //
2000
2001 //
2002 // copy the information we need into a new Node
2003 //
2004 NewShellNode = InternalDuplicateShellFileInfo(ShellInfoNode, FALSE);
2005 ASSERT(NewShellNode != NULL);
2006 if (NewShellNode == NULL) {
2007 Status = EFI_OUT_OF_RESOURCES;
2008 }
2009 if (*FileList == NULL) {
2010 *FileList = AllocatePool(sizeof(EFI_SHELL_FILE_INFO));
2011 InitializeListHead(&((*FileList)->Link));
2012 }
2013
2014 //
2015 // Add to the returning to use list
2016 //
2017 InsertTailList(&(*FileList)->Link, &NewShellNode->Link);
2018 }
2019 }
2020 if (EFI_ERROR(Status)) {
2021 break;
2022 }
2023 }
2024 if (EFI_ERROR(Status)) {
2025 EfiShellFreeFileList(&ShellInfo);
2026 } else {
2027 Status = EfiShellFreeFileList(&ShellInfo);
2028 }
2029 }
2030 }
2031
2032 FreePool(CurrentFilePattern);
2033 return (Status);
2034 }
2035
2036 /**
2037 Find files that match a specified pattern.
2038
2039 This function searches for all files and directories that match the specified
2040 FilePattern. The FilePattern can contain wild-card characters. The resulting file
2041 information is placed in the file list FileList.
2042
2043 Wildcards are processed
2044 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.
2045
2046 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo
2047 field is set to NULL.
2048
2049 if *FileList is not NULL then it must be a pre-existing and properly initialized list.
2050
2051 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.
2052 @param FileList On return, points to the start of a file list containing the names
2053 of all matching files or else points to NULL if no matching files
2054 were found. only on a EFI_SUCCESS return will; this be non-NULL.
2055
2056 @retval EFI_SUCCESS Files found. FileList is a valid list.
2057 @retval EFI_NOT_FOUND No files found.
2058 @retval EFI_NO_MEDIA The device has no media
2059 @retval EFI_DEVICE_ERROR The device reported an error
2060 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted
2061 **/
2062 EFI_STATUS
2063 EFIAPI
2064 EfiShellFindFiles(
2065 IN CONST CHAR16 *FilePattern,
2066 OUT EFI_SHELL_FILE_INFO **FileList
2067 )
2068 {
2069 EFI_STATUS Status;
2070 CHAR16 *PatternCopy;
2071 CHAR16 *PatternCurrentLocation;
2072 EFI_DEVICE_PATH_PROTOCOL *RootDevicePath;
2073 SHELL_FILE_HANDLE RootFileHandle;
2074 CHAR16 *MapName;
2075 UINTN Count;
2076
2077 if ( FilePattern == NULL
2078 || FileList == NULL
2079 || StrStr(FilePattern, L":") == NULL
2080 ){
2081 return (EFI_INVALID_PARAMETER);
2082 }
2083 Status = EFI_SUCCESS;
2084 RootDevicePath = NULL;
2085 RootFileHandle = NULL;
2086 MapName = NULL;
2087 PatternCopy = AllocatePool(StrSize(FilePattern));
2088 if (PatternCopy == NULL) {
2089 return (EFI_OUT_OF_RESOURCES);
2090 }
2091 StrCpy(PatternCopy, FilePattern);
2092
2093 PatternCopy = CleanPath(PatternCopy);
2094
2095 Count = StrStr(PatternCopy, L":") - PatternCopy;
2096 Count += 2;
2097
2098 ASSERT(MapName == NULL);
2099 MapName = StrnCatGrow(&MapName, NULL, PatternCopy, Count);
2100
2101 if (!EFI_ERROR(Status)) {
2102 RootDevicePath = EfiShellGetDevicePathFromFilePath(PatternCopy);
2103 if (RootDevicePath == NULL) {
2104 Status = EFI_INVALID_PARAMETER;
2105 } else {
2106 Status = EfiShellOpenRoot(RootDevicePath, &RootFileHandle);
2107 if (!EFI_ERROR(Status)) {
2108 for ( PatternCurrentLocation = PatternCopy
2109 ; *PatternCurrentLocation != ':'
2110 ; PatternCurrentLocation++);
2111 PatternCurrentLocation++;
2112 Status = ShellSearchHandle(PatternCurrentLocation, gUnicodeCollation, RootFileHandle, FileList, NULL);
2113 }
2114 FreePool(RootDevicePath);
2115 }
2116 }
2117
2118 if (PatternCopy != NULL) {
2119 FreePool(PatternCopy);
2120 }
2121 if (MapName != NULL) {
2122 FreePool(MapName);
2123 }
2124
2125 return(Status);
2126 }
2127
2128 /**
2129 Opens the files that match the path specified.
2130
2131 This function opens all of the files specified by Path. Wildcards are processed
2132 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each
2133 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.
2134
2135 @param Path A pointer to the path string.
2136 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or
2137 EFI_FILE_MODE_WRITE.
2138 @param FileList Points to the start of a list of files opened.
2139
2140 @retval EFI_SUCCESS Create the file list successfully.
2141 @return Others Can't create the file list.
2142 **/
2143 EFI_STATUS
2144 EFIAPI
2145 EfiShellOpenFileList(
2146 IN CHAR16 *Path,
2147 IN UINT64 OpenMode,
2148 IN OUT EFI_SHELL_FILE_INFO **FileList
2149 )
2150 {
2151 EFI_STATUS Status;
2152 EFI_SHELL_FILE_INFO *ShellFileListItem;
2153 CHAR16 *Path2;
2154 UINTN Path2Size;
2155 CONST CHAR16 *CurDir;
2156
2157 ShellCommandCleanPath(Path);
2158
2159 Path2Size = 0;
2160 Path2 = NULL;
2161
2162 ASSERT(FileList != NULL);
2163 ASSERT(*FileList != NULL);
2164
2165 if (*Path == L'.' && *(Path+1) == L'\\') {
2166 Path++;
2167 }
2168
2169 //
2170 // convert a local path to an absolute path
2171 //
2172 if (StrStr(Path, L":") == NULL) {
2173 CurDir = EfiShellGetCurDir(NULL);
2174 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));
2175 StrnCatGrow(&Path2, &Path2Size, CurDir, 0);
2176 if (*Path == L'\\') {
2177 Path++;
2178 }
2179 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));
2180 StrnCatGrow(&Path2, &Path2Size, Path, 0);
2181 } else {
2182 ASSERT(Path2 == NULL);
2183 StrnCatGrow(&Path2, NULL, Path, 0);
2184 }
2185
2186 CleanPath (Path2);
2187
2188 //
2189 // do the search
2190 //
2191 Status = EfiShellFindFiles(Path2, FileList);
2192
2193 FreePool(Path2);
2194
2195 if (EFI_ERROR(Status)) {
2196 return (Status);
2197 }
2198
2199 //
2200 // We had no errors so open all the files (that are not already opened...)
2201 //
2202 for ( ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)
2203 ; !IsNull(&(*FileList)->Link, &ShellFileListItem->Link)
2204 ; ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)
2205 ){
2206 if (ShellFileListItem->Status == 0 && ShellFileListItem->Handle == NULL) {
2207 ShellFileListItem->Status = EfiShellOpenFileByName (ShellFileListItem->FullName, &ShellFileListItem->Handle, OpenMode);
2208 }
2209 }
2210
2211 return(EFI_SUCCESS);
2212 }
2213
2214 /**
2215 This function updated with errata.
2216
2217 Gets either a single or list of environment variables.
2218
2219 If name is not NULL then this function returns the current value of the specified
2220 environment variable.
2221
2222 If Name is NULL, then a list of all environment variable names is returned. Each is a
2223 NULL terminated string with a double NULL terminating the list.
2224
2225 @param Name A pointer to the environment variable name. If
2226 Name is NULL, then the function will return all
2227 of the defined shell environment variables. In
2228 the case where multiple environment variables are
2229 being returned, each variable will be terminated by
2230 a NULL, and the list will be terminated by a double
2231 NULL.
2232
2233 @return !=NULL A pointer to the returned string.
2234 The returned pointer does not need to be freed by the caller.
2235
2236 @retval NULL The environment variable doesn't exist or there are
2237 no environment variables.
2238 **/
2239 CONST CHAR16 *
2240 EFIAPI
2241 EfiShellGetEnv(
2242 IN CONST CHAR16 *Name
2243 )
2244 {
2245 EFI_STATUS Status;
2246 VOID *Buffer;
2247 UINTN Size;
2248 LIST_ENTRY List;
2249 ENV_VAR_LIST *Node;
2250 CHAR16 *CurrentWriteLocation;
2251
2252 Size = 0;
2253 Buffer = NULL;
2254
2255 if (Name == NULL) {
2256 //
2257 // Get all our environment variables
2258 //
2259 InitializeListHead(&List);
2260 Status = GetEnvironmentVariableList(&List);
2261 if (EFI_ERROR(Status)){
2262 return (NULL);
2263 }
2264
2265 //
2266 // Build the semi-colon delimited list. (2 passes)
2267 //
2268 for ( Node = (ENV_VAR_LIST*)GetFirstNode(&List)
2269 ; !IsNull(&List, &Node->Link)
2270 ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)
2271 ){
2272 ASSERT(Node->Key != NULL);
2273 Size += StrSize(Node->Key);
2274 }
2275
2276 Size += 2*sizeof(CHAR16);
2277
2278 Buffer = AllocateZeroPool(Size);
2279 if (Buffer == NULL) {
2280 if (!IsListEmpty (&List)) {
2281 FreeEnvironmentVariableList(&List);
2282 }
2283 return (NULL);
2284 }
2285 CurrentWriteLocation = (CHAR16*)Buffer;
2286
2287 for ( Node = (ENV_VAR_LIST*)GetFirstNode(&List)
2288 ; !IsNull(&List, &Node->Link)
2289 ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)
2290 ){
2291 ASSERT(Node->Key != NULL);
2292 StrCpy(CurrentWriteLocation, Node->Key);
2293 CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1;
2294 }
2295
2296 //
2297 // Free the list...
2298 //
2299 if (!IsListEmpty (&List)) {
2300 FreeEnvironmentVariableList(&List);
2301 }
2302 } else {
2303 //
2304 // We are doing a specific environment variable
2305 //
2306
2307 //
2308 // get the size we need for this EnvVariable
2309 //
2310 Status = SHELL_GET_ENVIRONMENT_VARIABLE(Name, &Size, Buffer);
2311 if (Status == EFI_BUFFER_TOO_SMALL) {
2312 //
2313 // Allocate the space and recall the get function
2314 //
2315 Buffer = AllocateZeroPool(Size);
2316 ASSERT(Buffer != NULL);
2317 Status = SHELL_GET_ENVIRONMENT_VARIABLE(Name, &Size, Buffer);
2318 }
2319 //
2320 // we didnt get it (might not exist)
2321 // free the memory if we allocated any and return NULL
2322 //
2323 if (EFI_ERROR(Status)) {
2324 if (Buffer != NULL) {
2325 FreePool(Buffer);
2326 }
2327 return (NULL);
2328 }
2329 }
2330
2331 //
2332 // return the buffer
2333 //
2334 return (AddBufferToFreeList(Buffer));
2335 }
2336
2337 /**
2338 Internal variable setting function. Allows for setting of the read only variables.
2339
2340 @param Name Points to the NULL-terminated environment variable name.
2341 @param Value Points to the NULL-terminated environment variable value. If the value is an
2342 empty string then the environment variable is deleted.
2343 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2344
2345 @retval EFI_SUCCESS The environment variable was successfully updated.
2346 **/
2347 EFI_STATUS
2348 EFIAPI
2349 InternalEfiShellSetEnv(
2350 IN CONST CHAR16 *Name,
2351 IN CONST CHAR16 *Value,
2352 IN BOOLEAN Volatile
2353 )
2354 {
2355 if (Value == NULL || StrLen(Value) == 0) {
2356 return (SHELL_DELETE_ENVIRONMENT_VARIABLE(Name));
2357 } else {
2358 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name);
2359 if (Volatile) {
2360 return (SHELL_SET_ENVIRONMENT_VARIABLE_V(Name, StrSize(Value), Value));
2361 } else {
2362 return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(Name, StrSize(Value), Value));
2363 }
2364 }
2365 }
2366
2367 /**
2368 Sets the environment variable.
2369
2370 This function changes the current value of the specified environment variable. If the
2371 environment variable exists and the Value is an empty string, then the environment
2372 variable is deleted. If the environment variable exists and the Value is not an empty
2373 string, then the value of the environment variable is changed. If the environment
2374 variable does not exist and the Value is an empty string, there is no action. If the
2375 environment variable does not exist and the Value is a non-empty string, then the
2376 environment variable is created and assigned the specified value.
2377
2378 For a description of volatile and non-volatile environment variables, see UEFI Shell
2379 2.0 specification section 3.6.1.
2380
2381 @param Name Points to the NULL-terminated environment variable name.
2382 @param Value Points to the NULL-terminated environment variable value. If the value is an
2383 empty string then the environment variable is deleted.
2384 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
2385
2386 @retval EFI_SUCCESS The environment variable was successfully updated.
2387 **/
2388 EFI_STATUS
2389 EFIAPI
2390 EfiShellSetEnv(
2391 IN CONST CHAR16 *Name,
2392 IN CONST CHAR16 *Value,
2393 IN BOOLEAN Volatile
2394 )
2395 {
2396 if (Name == NULL || *Name == CHAR_NULL) {
2397 return (EFI_INVALID_PARAMETER);
2398 }
2399 //
2400 // Make sure we dont 'set' a predefined read only variable
2401 //
2402 if (gUnicodeCollation->StriColl(
2403 gUnicodeCollation,
2404 (CHAR16*)Name,
2405 L"cwd") == 0
2406 ||gUnicodeCollation->StriColl(
2407 gUnicodeCollation,
2408 (CHAR16*)Name,
2409 L"Lasterror") == 0
2410 ||gUnicodeCollation->StriColl(
2411 gUnicodeCollation,
2412 (CHAR16*)Name,
2413 L"profiles") == 0
2414 ||gUnicodeCollation->StriColl(
2415 gUnicodeCollation,
2416 (CHAR16*)Name,
2417 L"uefishellsupport") == 0
2418 ||gUnicodeCollation->StriColl(
2419 gUnicodeCollation,
2420 (CHAR16*)Name,
2421 L"uefishellversion") == 0
2422 ||gUnicodeCollation->StriColl(
2423 gUnicodeCollation,
2424 (CHAR16*)Name,
2425 L"uefiversion") == 0
2426 ){
2427 return (EFI_INVALID_PARAMETER);
2428 }
2429 return (InternalEfiShellSetEnv(Name, Value, Volatile));
2430 }
2431
2432 /**
2433 Returns the current directory on the specified device.
2434
2435 If FileSystemMapping is NULL, it returns the current working directory. If the
2436 FileSystemMapping is not NULL, it returns the current directory associated with the
2437 FileSystemMapping. In both cases, the returned name includes the file system
2438 mapping (i.e. fs0:\current-dir).
2439
2440 @param FileSystemMapping A pointer to the file system mapping. If NULL,
2441 then the current working directory is returned.
2442
2443 @retval !=NULL The current directory.
2444 @retval NULL Current directory does not exist.
2445 **/
2446 CONST CHAR16 *
2447 EFIAPI
2448 EfiShellGetCurDir(
2449 IN CONST CHAR16 *FileSystemMapping OPTIONAL
2450 )
2451 {
2452 CHAR16 *PathToReturn;
2453 UINTN Size;
2454 SHELL_MAP_LIST *MapListItem;
2455 if (!IsListEmpty(&gShellMapList.Link)) {
2456 //
2457 // if parameter is NULL, use current
2458 //
2459 if (FileSystemMapping == NULL) {
2460 return (EfiShellGetEnv(L"cwd"));
2461 } else {
2462 Size = 0;
2463 PathToReturn = NULL;
2464 MapListItem = ShellCommandFindMapItem(FileSystemMapping);
2465 if (MapListItem != NULL) {
2466 ASSERT((PathToReturn == NULL && Size == 0) || (PathToReturn != NULL));
2467 PathToReturn = StrnCatGrow(&PathToReturn, &Size, MapListItem->MapName, 0);
2468 PathToReturn = StrnCatGrow(&PathToReturn, &Size, MapListItem->CurrentDirectoryPath, 0);
2469 }
2470 }
2471 return (AddBufferToFreeList(PathToReturn));
2472 } else {
2473 return (NULL);
2474 }
2475 }
2476
2477 /**
2478 Changes the current directory on the specified device.
2479
2480 If the FileSystem is NULL, and the directory Dir does not contain a file system's
2481 mapped name, this function changes the current working directory.
2482
2483 If the FileSystem is NULL and the directory Dir contains a mapped name, then the
2484 current file system and the current directory on that file system are changed.
2485
2486 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file
2487 system.
2488
2489 If FileSystem is not NULL and Dir is not NULL, then this function changes the current
2490 directory on the specified file system.
2491
2492 If the current working directory or the current working file system is changed then the
2493 %cwd% environment variable will be updated
2494
2495 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working
2496 directory is changed.
2497 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.
2498
2499 @retval EFI_SUCCESS The operation was sucessful
2500 @retval EFI_NOT_FOUND The file system could not be found
2501 **/
2502 EFI_STATUS
2503 EFIAPI
2504 EfiShellSetCurDir(
2505 IN CONST CHAR16 *FileSystem OPTIONAL,
2506 IN CONST CHAR16 *Dir
2507 )
2508 {
2509 CHAR16 *MapName;
2510 SHELL_MAP_LIST *MapListItem;
2511 UINTN Size;
2512 EFI_STATUS Status;
2513 CHAR16 *TempString;
2514 CHAR16 *DirectoryName;
2515 UINTN TempLen;
2516
2517 Size = 0;
2518 MapName = NULL;
2519 MapListItem = NULL;
2520 TempString = NULL;
2521 DirectoryName = NULL;
2522
2523 if ((FileSystem == NULL && Dir == NULL) || Dir == NULL) {
2524 return (EFI_INVALID_PARAMETER);
2525 }
2526
2527 if (IsListEmpty(&gShellMapList.Link)){
2528 return (EFI_NOT_FOUND);
2529 }
2530
2531 DirectoryName = StrnCatGrow(&DirectoryName, NULL, Dir, 0);
2532 ASSERT(DirectoryName != NULL);
2533
2534 CleanPath(DirectoryName);
2535
2536 if (FileSystem == NULL) {
2537 //
2538 // determine the file system mapping to use
2539 //
2540 if (StrStr(DirectoryName, L":") != NULL) {
2541 ASSERT(MapName == NULL);
2542 MapName = StrnCatGrow(&MapName, NULL, DirectoryName, (StrStr(DirectoryName, L":")-DirectoryName+1));
2543 }
2544 //
2545 // find the file system mapping's entry in the list
2546 // or use current
2547 //
2548 if (MapName != NULL) {
2549 MapListItem = ShellCommandFindMapItem(MapName);
2550
2551 //
2552 // make that the current file system mapping
2553 //
2554 if (MapListItem != NULL) {
2555 gShellCurDir = MapListItem;
2556 }
2557 } else {
2558 MapListItem = gShellCurDir;
2559 }
2560
2561 if (MapListItem == NULL) {
2562 return (EFI_NOT_FOUND);
2563 }
2564
2565 //
2566 // now update the MapListItem's current directory
2567 //
2568 if (MapListItem->CurrentDirectoryPath != NULL && DirectoryName[StrLen(DirectoryName) - 1] != L':') {
2569 FreePool(MapListItem->CurrentDirectoryPath);
2570 MapListItem->CurrentDirectoryPath = NULL;
2571 }
2572 if (MapName != NULL) {
2573 TempLen = StrLen(MapName);
2574 if (TempLen != StrLen(DirectoryName)) {
2575 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2576 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName+StrLen(MapName), 0);
2577 }
2578 } else {
2579 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2580 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);
2581 }
2582 if ((MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') || (MapListItem->CurrentDirectoryPath == NULL)) {
2583 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2584 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);
2585 }
2586 } else {
2587 //
2588 // cant have a mapping in the directory...
2589 //
2590 if (StrStr(DirectoryName, L":") != NULL) {
2591 return (EFI_INVALID_PARAMETER);
2592 }
2593 //
2594 // FileSystem != NULL
2595 //
2596 MapListItem = ShellCommandFindMapItem(FileSystem);
2597 if (MapListItem == NULL) {
2598 return (EFI_INVALID_PARAMETER);
2599 }
2600 // gShellCurDir = MapListItem;
2601 if (DirectoryName != NULL) {
2602 //
2603 // change current dir on that file system
2604 //
2605
2606 if (MapListItem->CurrentDirectoryPath != NULL) {
2607 FreePool(MapListItem->CurrentDirectoryPath);
2608 DEBUG_CODE(MapListItem->CurrentDirectoryPath = NULL;);
2609 }
2610 // ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2611 // MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);
2612 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2613 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);
2614 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2615 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);
2616 if (MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') {
2617 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
2618 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);
2619 }
2620 }
2621 }
2622 //
2623 // if updated the current directory then update the environment variable
2624 //
2625 if (MapListItem == gShellCurDir) {
2626 Size = 0;
2627 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));
2628 StrnCatGrow(&TempString, &Size, MapListItem->MapName, 0);
2629 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));
2630 StrnCatGrow(&TempString, &Size, MapListItem->CurrentDirectoryPath, 0);
2631 Status = InternalEfiShellSetEnv(L"cwd", TempString, TRUE);
2632 FreePool(TempString);
2633 return (Status);
2634 }
2635 return(EFI_SUCCESS);
2636 }
2637
2638 /**
2639 Return help information about a specific command.
2640
2641 This function returns the help information for the specified command. The help text
2642 can be internal to the shell or can be from a UEFI Shell manual page.
2643
2644 If Sections is specified, then each section name listed will be compared in a casesensitive
2645 manner, to the section names described in Appendix B. If the section exists,
2646 it will be appended to the returned help text. If the section does not exist, no
2647 information will be returned. If Sections is NULL, then all help text information
2648 available will be returned.
2649
2650 @param Command Points to the NULL-terminated UEFI Shell command name.
2651 @param Sections Points to the NULL-terminated comma-delimited
2652 section names to return. If NULL, then all
2653 sections will be returned.
2654 @param HelpText On return, points to a callee-allocated buffer
2655 containing all specified help text.
2656
2657 @retval EFI_SUCCESS The help text was returned.
2658 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
2659 returned help text.
2660 @retval EFI_INVALID_PARAMETER HelpText is NULL
2661 @retval EFI_NOT_FOUND There is no help text available for Command.
2662 **/
2663 EFI_STATUS
2664 EFIAPI
2665 EfiShellGetHelpText(
2666 IN CONST CHAR16 *Command,
2667 IN CONST CHAR16 *Sections OPTIONAL,
2668 OUT CHAR16 **HelpText
2669 )
2670 {
2671 CONST CHAR16 *ManFileName;
2672
2673 ASSERT(HelpText != NULL);
2674
2675 ManFileName = ShellCommandGetManFileNameHandler(Command);
2676
2677 if (ManFileName != NULL) {
2678 return (ProcessManFile(ManFileName, Command, Sections, NULL, HelpText));
2679 } else {
2680 return (ProcessManFile(Command, Command, Sections, NULL, HelpText));
2681 }
2682 }
2683
2684 /**
2685 Gets the enable status of the page break output mode.
2686
2687 User can use this function to determine current page break mode.
2688
2689 @retval TRUE The page break output mode is enabled.
2690 @retval FALSE The page break output mode is disabled.
2691 **/
2692 BOOLEAN
2693 EFIAPI
2694 EfiShellGetPageBreak(
2695 VOID
2696 )
2697 {
2698 return(ShellInfoObject.PageBreakEnabled);
2699 }
2700
2701 /**
2702 Judges whether the active shell is the root shell.
2703
2704 This function makes the user to know that whether the active Shell is the root shell.
2705
2706 @retval TRUE The active Shell is the root Shell.
2707 @retval FALSE The active Shell is NOT the root Shell.
2708 **/
2709 BOOLEAN
2710 EFIAPI
2711 EfiShellIsRootShell(
2712 VOID
2713 )
2714 {
2715 return(ShellInfoObject.RootShellInstance);
2716 }
2717
2718 /**
2719 function to return a semi-colon delimeted list of all alias' in the current shell
2720
2721 up to caller to free the memory.
2722
2723 @retval NULL No alias' were found
2724 @retval NULL An error ocurred getting alias'
2725 @return !NULL a list of all alias'
2726 **/
2727 CHAR16 *
2728 EFIAPI
2729 InternalEfiShellGetListAlias(
2730 )
2731 {
2732 UINT64 MaxStorSize;
2733 UINT64 RemStorSize;
2734 UINT64 MaxVarSize;
2735 EFI_STATUS Status;
2736 EFI_GUID Guid;
2737 CHAR16 *VariableName;
2738 UINTN NameSize;
2739 CHAR16 *RetVal;
2740 UINTN RetSize;
2741 CHAR16 *Alias;
2742
2743 Status = gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS, &MaxStorSize, &RemStorSize, &MaxVarSize);
2744 ASSERT_EFI_ERROR(Status);
2745
2746 VariableName = AllocateZeroPool((UINTN)MaxVarSize);
2747 RetSize = 0;
2748 RetVal = NULL;
2749
2750 if (VariableName == NULL) {
2751 return (NULL);
2752 }
2753
2754 VariableName[0] = CHAR_NULL;
2755
2756 while (TRUE) {
2757 NameSize = (UINTN)MaxVarSize;
2758 Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);
2759 if (Status == EFI_NOT_FOUND){
2760 break;
2761 }
2762 ASSERT_EFI_ERROR(Status);
2763 if (EFI_ERROR(Status)) {
2764 break;
2765 }
2766 if (CompareGuid(&Guid, &gShellAliasGuid)){
2767 Alias = GetVariable(VariableName, &gShellAliasGuid);
2768 ASSERT((RetVal == NULL && RetSize == 0) || (RetVal != NULL));
2769 RetVal = StrnCatGrow(&RetVal, &RetSize, VariableName, 0);
2770 RetVal = StrnCatGrow(&RetVal, &RetSize, L";", 0);
2771 } // compare guid
2772 } // while
2773 FreePool(VariableName);
2774
2775 return (RetVal);
2776 }
2777
2778 /**
2779 This function returns the command associated with a alias or a list of all
2780 alias'.
2781
2782 @param[in] Alias Points to the NULL-terminated shell alias.
2783 If this parameter is NULL, then all
2784 aliases will be returned in ReturnedData.
2785 @param[out] Volatile upon return of a single command if TRUE indicates
2786 this is stored in a volatile fashion. FALSE otherwise.
2787
2788 @return If Alias is not NULL, it will return a pointer to
2789 the NULL-terminated command for that alias.
2790 If Alias is NULL, ReturnedData points to a ';'
2791 delimited list of alias (e.g.
2792 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.
2793 @retval NULL an error ocurred
2794 @retval NULL Alias was not a valid Alias
2795 **/
2796 CONST CHAR16 *
2797 EFIAPI
2798 EfiShellGetAlias(
2799 IN CONST CHAR16 *Alias,
2800 OUT BOOLEAN *Volatile OPTIONAL
2801 )
2802 {
2803 CHAR16 *RetVal;
2804 UINTN RetSize;
2805 UINT32 Attribs;
2806 EFI_STATUS Status;
2807
2808 if (Alias != NULL) {
2809 if (Volatile == NULL) {
2810 return (AddBufferToFreeList(GetVariable((CHAR16*)Alias, &gShellAliasGuid)));
2811 }
2812 RetSize = 0;
2813 RetVal = NULL;
2814 Status = gRT->GetVariable((CHAR16*)Alias, &gShellAliasGuid, &Attribs, &RetSize, RetVal);
2815 if (Status == EFI_BUFFER_TOO_SMALL) {
2816 RetVal = AllocateZeroPool(RetSize);
2817 Status = gRT->GetVariable((CHAR16*)Alias, &gShellAliasGuid, &Attribs, &RetSize, RetVal);
2818 }
2819 if (EFI_ERROR(Status)) {
2820 if (RetVal != NULL) {
2821 FreePool(RetVal);
2822 }
2823 return (NULL);
2824 }
2825 if ((EFI_VARIABLE_NON_VOLATILE & Attribs) == EFI_VARIABLE_NON_VOLATILE) {
2826 *Volatile = FALSE;
2827 } else {
2828 *Volatile = TRUE;
2829 }
2830
2831 return (AddBufferToFreeList(RetVal));
2832 }
2833 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));
2834 }
2835
2836 /**
2837 Changes a shell command alias.
2838
2839 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
2840
2841 this function does not check for built in alias'.
2842
2843 @param[in] Command Points to the NULL-terminated shell command or existing alias.
2844 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
2845 Command refers to an alias, that alias will be deleted.
2846 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
2847 Alias being set will be stored in a non-volatile fashion.
2848
2849 @retval EFI_SUCCESS Alias created or deleted successfully.
2850 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
2851 **/
2852 EFI_STATUS
2853 EFIAPI
2854 InternalSetAlias(
2855 IN CONST CHAR16 *Command,
2856 IN CONST CHAR16 *Alias,
2857 IN BOOLEAN Volatile
2858 )
2859 {
2860 //
2861 // We must be trying to remove one if Alias is NULL
2862 //
2863 if (Alias == NULL) {
2864 //
2865 // remove an alias (but passed in COMMAND parameter)
2866 //
2867 return (gRT->SetVariable((CHAR16*)Command, &gShellAliasGuid, 0, 0, NULL));
2868 } else {
2869 //
2870 // Add and replace are the same
2871 //
2872
2873 // We dont check the error return on purpose since the variable may not exist.
2874 gRT->SetVariable((CHAR16*)Command, &gShellAliasGuid, 0, 0, NULL);
2875
2876 return (gRT->SetVariable((CHAR16*)Alias, &gShellAliasGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS|(Volatile?0:EFI_VARIABLE_NON_VOLATILE), StrSize(Command), (VOID*)Command));
2877 }
2878 }
2879
2880 /**
2881 Changes a shell command alias.
2882
2883 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
2884
2885
2886 @param[in] Command Points to the NULL-terminated shell command or existing alias.
2887 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
2888 Command refers to an alias, that alias will be deleted.
2889 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
2890 FALSE and the alias already exists, then the existing alias is unchanged and
2891 EFI_ACCESS_DENIED is returned.
2892 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
2893 Alias being set will be stored in a non-volatile fashion.
2894
2895 @retval EFI_SUCCESS Alias created or deleted successfully.
2896 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
2897 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
2898 FALSE.
2899 **/
2900 EFI_STATUS
2901 EFIAPI
2902 EfiShellSetAlias(
2903 IN CONST CHAR16 *Command,
2904 IN CONST CHAR16 *Alias,
2905 IN BOOLEAN Replace,
2906 IN BOOLEAN Volatile
2907 )
2908 {
2909 //
2910 // cant set over a built in alias
2911 //
2912 if (ShellCommandIsOnAliasList(Alias==NULL?Command:Alias)) {
2913 return (EFI_ACCESS_DENIED);
2914 }
2915 if (Command == NULL || *Command == CHAR_NULL || StrLen(Command) == 0) {
2916 return (EFI_INVALID_PARAMETER);
2917 }
2918
2919 if (EfiShellGetAlias(Command, NULL) != NULL && !Replace) {
2920 return (EFI_ACCESS_DENIED);
2921 }
2922
2923 return (InternalSetAlias(Command, Alias, Volatile));
2924 }
2925
2926 // Pure FILE_HANDLE operations are passed to FileHandleLib
2927 // these functions are indicated by the *
2928 EFI_SHELL_PROTOCOL mShellProtocol = {
2929 EfiShellExecute,
2930 EfiShellGetEnv,
2931 EfiShellSetEnv,
2932 EfiShellGetAlias,
2933 EfiShellSetAlias,
2934 EfiShellGetHelpText,
2935 EfiShellGetDevicePathFromMap,
2936 EfiShellGetMapFromDevicePath,
2937 EfiShellGetDevicePathFromFilePath,
2938 EfiShellGetFilePathFromDevicePath,
2939 EfiShellSetMap,
2940 EfiShellGetCurDir,
2941 EfiShellSetCurDir,
2942 EfiShellOpenFileList,
2943 EfiShellFreeFileList,
2944 EfiShellRemoveDupInFileList,
2945 EfiShellBatchIsActive,
2946 EfiShellIsRootShell,
2947 EfiShellEnablePageBreak,
2948 EfiShellDisablePageBreak,
2949 EfiShellGetPageBreak,
2950 EfiShellGetDeviceName,
2951 (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo, //*
2952 (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo, //*
2953 EfiShellOpenFileByName,
2954 EfiShellClose,
2955 EfiShellCreateFile,
2956 (EFI_SHELL_READ_FILE)FileHandleRead, //*
2957 (EFI_SHELL_WRITE_FILE)FileHandleWrite, //*
2958 (EFI_SHELL_DELETE_FILE)FileHandleDelete, //*
2959 EfiShellDeleteFileByName,
2960 (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition, //*
2961 (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition, //*
2962 (EFI_SHELL_FLUSH_FILE)FileHandleFlush, //*
2963 EfiShellFindFiles,
2964 EfiShellFindFilesInDir,
2965 (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize, //*
2966 EfiShellOpenRoot,
2967 EfiShellOpenRootByHandle,
2968 NULL,
2969 SHELL_MAJOR_VERSION,
2970 SHELL_MINOR_VERSION
2971 };
2972
2973 /**
2974 Function to create and install on the current handle.
2975
2976 Will overwrite any existing ShellProtocols in the system to be sure that
2977 the current shell is in control.
2978
2979 This must be removed via calling CleanUpShellProtocol().
2980
2981 @param[in,out] NewShell The pointer to the pointer to the structure
2982 to install.
2983
2984 @retval EFI_SUCCESS The operation was successful.
2985 @return An error from LocateHandle, CreateEvent, or other core function.
2986 **/
2987 EFI_STATUS
2988 EFIAPI
2989 CreatePopulateInstallShellProtocol (
2990 IN OUT EFI_SHELL_PROTOCOL **NewShell
2991 )
2992 {
2993 EFI_STATUS Status;
2994 UINTN BufferSize;
2995 EFI_HANDLE *Buffer;
2996 UINTN HandleCounter;
2997 SHELL_PROTOCOL_HANDLE_LIST *OldProtocolNode;
2998
2999 BufferSize = 0;
3000 Buffer = NULL;
3001 OldProtocolNode = NULL;
3002 InitializeListHead(&ShellInfoObject.OldShellList.Link);
3003
3004 ASSERT(NewShell != NULL);
3005
3006 //
3007 // Initialize EfiShellProtocol object...
3008 //
3009 *NewShell = &mShellProtocol;
3010 Status = gBS->CreateEvent(0,
3011 0,
3012 NULL,
3013 NULL,
3014 &mShellProtocol.ExecutionBreak);
3015 ASSERT_EFI_ERROR(Status);
3016
3017 //
3018 // Get the size of the buffer we need.
3019 //
3020 Status = gBS->LocateHandle(ByProtocol,
3021 &gEfiShellProtocolGuid,
3022 NULL,
3023 &BufferSize,
3024 Buffer);
3025 if (Status == EFI_BUFFER_TOO_SMALL) {
3026 //
3027 // Allocate and recall with buffer of correct size
3028 //
3029 Buffer = AllocateZeroPool(BufferSize);
3030 ASSERT(Buffer != NULL);
3031 Status = gBS->LocateHandle(ByProtocol,
3032 &gEfiShellProtocolGuid,
3033 NULL,
3034 &BufferSize,
3035 Buffer);
3036 ASSERT_EFI_ERROR(Status);
3037 //
3038 // now overwrite each of them, but save the info to restore when we end.
3039 //
3040 for (HandleCounter = 0 ; HandleCounter < (BufferSize/sizeof(EFI_HANDLE)) ; HandleCounter++) {
3041 OldProtocolNode = AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST));
3042 ASSERT(OldProtocolNode != NULL);
3043 Status = gBS->OpenProtocol(Buffer[HandleCounter],
3044 &gEfiShellProtocolGuid,
3045 (VOID **) &(OldProtocolNode->Interface),
3046 gImageHandle,
3047 NULL,
3048 EFI_OPEN_PROTOCOL_GET_PROTOCOL
3049 );
3050 if (!EFI_ERROR(Status)) {
3051 //
3052 // reinstall over the old one...
3053 //
3054 OldProtocolNode->Handle = Buffer[HandleCounter];
3055 Status = gBS->ReinstallProtocolInterface(
3056 OldProtocolNode->Handle,
3057 &gEfiShellProtocolGuid,
3058 OldProtocolNode->Interface,
3059 (VOID*)(*NewShell));
3060 if (!EFI_ERROR(Status)) {
3061 //
3062 // we reinstalled sucessfully. log this so we can reverse it later.
3063 //
3064
3065 //
3066 // add to the list for subsequent...
3067 //
3068 InsertTailList(&ShellInfoObject.OldShellList.Link, &OldProtocolNode->Link);
3069 }
3070 }
3071 }
3072 FreePool(Buffer);
3073 } else if (Status == EFI_NOT_FOUND) {
3074 ASSERT(IsListEmpty(&ShellInfoObject.OldShellList.Link));
3075 //
3076 // no one else published yet. just publish it ourselves.
3077 //
3078 Status = gBS->InstallProtocolInterface (
3079 &gImageHandle,
3080 &gEfiShellProtocolGuid,
3081 EFI_NATIVE_INTERFACE,
3082 (VOID*)(*NewShell));
3083 }
3084
3085 if (PcdGetBool(PcdShellSupportOldProtocols)){
3086 ///@todo support ShellEnvironment2
3087 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?
3088 }
3089
3090 return (Status);
3091 }
3092
3093 /**
3094 Opposite of CreatePopulateInstallShellProtocol.
3095
3096 Free all memory and restore the system to the state it was in before calling
3097 CreatePopulateInstallShellProtocol.
3098
3099 @param[in,out] NewShell The pointer to the new shell protocol structure.
3100
3101 @retval EFI_SUCCESS The operation was successful.
3102 **/
3103 EFI_STATUS
3104 EFIAPI
3105 CleanUpShellProtocol (
3106 IN OUT EFI_SHELL_PROTOCOL *NewShell
3107 )
3108 {
3109 EFI_STATUS Status;
3110 SHELL_PROTOCOL_HANDLE_LIST *Node2;
3111
3112 //
3113 // if we need to restore old protocols...
3114 //
3115 if (!IsListEmpty(&ShellInfoObject.OldShellList.Link)) {
3116 for (Node2 = (SHELL_PROTOCOL_HANDLE_LIST *)GetFirstNode(&ShellInfoObject.OldShellList.Link)
3117 ; !IsListEmpty (&ShellInfoObject.OldShellList.Link)
3118 ; Node2 = (SHELL_PROTOCOL_HANDLE_LIST *)GetFirstNode(&ShellInfoObject.OldShellList.Link)
3119 ){
3120 RemoveEntryList(&Node2->Link);
3121 Status = gBS->ReinstallProtocolInterface(Node2->Handle,
3122 &gEfiShellProtocolGuid,
3123 NewShell,
3124 Node2->Interface);
3125 ASSERT_EFI_ERROR(Status);
3126 FreePool(Node2);
3127 }
3128 } else {
3129 //
3130 // no need to restore
3131 //
3132 Status = gBS->UninstallProtocolInterface(gImageHandle,
3133 &gEfiShellProtocolGuid,
3134 NewShell);
3135 ASSERT_EFI_ERROR(Status);
3136 }
3137 Status = gBS->CloseEvent(NewShell->ExecutionBreak);
3138
3139 return (Status);
3140 }
3141
3142