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