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