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