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