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