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