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