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