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