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