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