]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Application/Shell/ShellProtocol.c
ShellPkg/Application: Fix various typos
[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
1858 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
1859 EFI_SHELL_FILE_INFO *ShellFileListItem2;\r
3e2b20a1 1860 EFI_SHELL_FILE_INFO *TempNode;\r
a405b86d 1861\r
1862 if (FileList == NULL || *FileList == NULL) {\r
1863 return (EFI_INVALID_PARAMETER);\r
1864 }\r
1865 for ( ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)\r
1866 ; !IsNull(&(*FileList)->Link, &ShellFileListItem->Link)\r
1867 ; ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)\r
1868 ){\r
1869 for ( ShellFileListItem2 = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)\r
1870 ; !IsNull(&(*FileList)->Link, &ShellFileListItem2->Link)\r
1871 ; ShellFileListItem2 = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem2->Link)\r
1872 ){\r
1873 if (gUnicodeCollation->StriColl(\r
1874 gUnicodeCollation,\r
1875 (CHAR16*)ShellFileListItem->FullName,\r
1876 (CHAR16*)ShellFileListItem2->FullName) == 0\r
1877 ){\r
3e2b20a1
BJ
1878 TempNode = (EFI_SHELL_FILE_INFO *)GetPreviousNode(\r
1879 &(*FileList)->Link,\r
1880 &ShellFileListItem2->Link\r
1881 );\r
a405b86d 1882 RemoveEntryList(&ShellFileListItem2->Link);\r
1883 InternalFreeShellFileInfoNode(ShellFileListItem2);\r
3e2b20a1
BJ
1884 // Set ShellFileListItem2 to PreviousNode so we don't access Freed\r
1885 // memory in GetNextNode in the loop expression above.\r
1886 ShellFileListItem2 = TempNode;\r
a405b86d 1887 }\r
1888 }\r
1889 }\r
1890 return (EFI_SUCCESS);\r
1891}\r
4b5168d8
JC
1892\r
1893//\r
1894// This is the same structure as the external version, but it has no CONST qualifiers.\r
1895//\r
1896typedef struct {\r
1897 LIST_ENTRY Link; ///< Linked list members.\r
1898 EFI_STATUS Status; ///< Status of opening the file. Valid only if Handle != NULL.\r
1899 CHAR16 *FullName; ///< Fully qualified filename.\r
1900 CHAR16 *FileName; ///< name of this file.\r
1901 SHELL_FILE_HANDLE Handle; ///< Handle for interacting with the opened file or NULL if closed.\r
1902 EFI_FILE_INFO *Info; ///< Pointer to the FileInfo struct for this file or NULL.\r
1903} EFI_SHELL_FILE_INFO_NO_CONST;\r
1904\r
a405b86d 1905/**\r
1906 Allocates and duplicates a EFI_SHELL_FILE_INFO node.\r
1907\r
1908 @param[in] Node The node to copy from.\r
1909 @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.\r
1910\r
6a5033ca 1911 @retval NULL a memory allocation error occurred\r
a405b86d 1912 @return != NULL a pointer to the new node\r
1913**/\r
1914EFI_SHELL_FILE_INFO*\r
a405b86d 1915InternalDuplicateShellFileInfo(\r
1916 IN EFI_SHELL_FILE_INFO *Node,\r
1917 IN BOOLEAN Save\r
1918 )\r
1919{\r
4b5168d8
JC
1920 EFI_SHELL_FILE_INFO_NO_CONST *NewNode;\r
1921\r
1922 //\r
1923 // try to confirm that the objects are in sync\r
1924 //\r
1925 ASSERT(sizeof(EFI_SHELL_FILE_INFO_NO_CONST) == sizeof(EFI_SHELL_FILE_INFO));\r
a405b86d 1926\r
733f138d 1927 NewNode = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
a405b86d 1928 if (NewNode == NULL) {\r
1929 return (NULL);\r
1930 }\r
7f79b01e
JC
1931 NewNode->FullName = AllocateCopyPool(StrSize(Node->FullName), Node->FullName);\r
1932 NewNode->FileName = AllocateCopyPool(StrSize(Node->FileName), Node->FileName);\r
1933 NewNode->Info = AllocateCopyPool((UINTN)Node->Info->Size, Node->Info);\r
a405b86d 1934 if ( NewNode->FullName == NULL\r
1935 || NewNode->FileName == NULL\r
1936 || NewNode->Info == NULL\r
4b5168d8
JC
1937 ){\r
1938 SHELL_FREE_NON_NULL(NewNode->FullName);\r
1939 SHELL_FREE_NON_NULL(NewNode->FileName);\r
1940 SHELL_FREE_NON_NULL(NewNode->Info);\r
1941 SHELL_FREE_NON_NULL(NewNode);\r
a405b86d 1942 return(NULL);\r
1943 }\r
1944 NewNode->Status = Node->Status;\r
1945 NewNode->Handle = Node->Handle;\r
1946 if (!Save) {\r
1947 Node->Handle = NULL;\r
1948 }\r
a405b86d 1949\r
4b5168d8 1950 return((EFI_SHELL_FILE_INFO*)NewNode);\r
a405b86d 1951}\r
1952\r
1953/**\r
1954 Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation\r
1955 failed it will return NULL.\r
1956\r
1957 @param[in] BasePath the Path to prepend onto filename for FullPath\r
1958 @param[in] Status Status member initial value.\r
a405b86d 1959 @param[in] FileName FileName member initial value.\r
1960 @param[in] Handle Handle member initial value.\r
1961 @param[in] Info Info struct to copy.\r
1962\r
6a5033ca 1963 @retval NULL An error occurred.\r
a405b86d 1964 @return a pointer to the newly allocated structure.\r
1965**/\r
1966EFI_SHELL_FILE_INFO *\r
a405b86d 1967CreateAndPopulateShellFileInfo(\r
1968 IN CONST CHAR16 *BasePath,\r
1969 IN CONST EFI_STATUS Status,\r
a405b86d 1970 IN CONST CHAR16 *FileName,\r
1971 IN CONST SHELL_FILE_HANDLE Handle,\r
1972 IN CONST EFI_FILE_INFO *Info\r
1973 )\r
1974{\r
1975 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
1976 CHAR16 *TempString;\r
1977 UINTN Size;\r
1978\r
1979 TempString = NULL;\r
1980 Size = 0;\r
1981\r
1982 ShellFileListItem = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
1983 if (ShellFileListItem == NULL) {\r
1984 return (NULL);\r
1985 }\r
74fa83fd 1986 if (Info != NULL && Info->Size != 0) {\r
a405b86d 1987 ShellFileListItem->Info = AllocateZeroPool((UINTN)Info->Size);\r
1988 if (ShellFileListItem->Info == NULL) {\r
1989 FreePool(ShellFileListItem);\r
1990 return (NULL);\r
1991 }\r
1992 CopyMem(ShellFileListItem->Info, Info, (UINTN)Info->Size);\r
1993 } else {\r
1994 ShellFileListItem->Info = NULL;\r
1995 }\r
1996 if (FileName != NULL) {\r
1997 ASSERT(TempString == NULL);\r
1998 ShellFileListItem->FileName = StrnCatGrow(&TempString, 0, FileName, 0);\r
1999 if (ShellFileListItem->FileName == NULL) {\r
2000 FreePool(ShellFileListItem->Info);\r
2001 FreePool(ShellFileListItem);\r
2002 return (NULL);\r
2003 }\r
2004 } else {\r
2005 ShellFileListItem->FileName = NULL;\r
2006 }\r
2007 Size = 0;\r
2008 TempString = NULL;\r
2009 if (BasePath != NULL) {\r
2010 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
2011 TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);\r
2012 if (TempString == NULL) {\r
2013 FreePool((VOID*)ShellFileListItem->FileName);\r
d0a5723f 2014 SHELL_FREE_NON_NULL(ShellFileListItem->Info);\r
a405b86d 2015 FreePool(ShellFileListItem);\r
2016 return (NULL);\r
2017 }\r
2018 }\r
2019 if (ShellFileListItem->FileName != NULL) {\r
2020 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
2021 TempString = StrnCatGrow(&TempString, &Size, ShellFileListItem->FileName, 0);\r
2022 if (TempString == NULL) {\r
2023 FreePool((VOID*)ShellFileListItem->FileName);\r
2024 FreePool(ShellFileListItem->Info);\r
2025 FreePool(ShellFileListItem);\r
2026 return (NULL);\r
2027 }\r
2028 }\r
2029\r
bbf904d1
JC
2030 TempString = PathCleanUpDirectories(TempString);\r
2031\r
a405b86d 2032 ShellFileListItem->FullName = TempString;\r
2033 ShellFileListItem->Status = Status;\r
2034 ShellFileListItem->Handle = Handle;\r
2035\r
2036 return (ShellFileListItem);\r
2037}\r
2038\r
2039/**\r
2040 Find all files in a specified directory.\r
2041\r
2042 @param FileDirHandle Handle of the directory to search.\r
2043 @param FileList On return, points to the list of files in the directory\r
2044 or NULL if there are no files in the directory.\r
2045\r
2046 @retval EFI_SUCCESS File information was returned successfully.\r
2047 @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.\r
2048 @retval EFI_DEVICE_ERROR The device reported an error.\r
2049 @retval EFI_NO_MEDIA The device media is not present.\r
2050 @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.\r
2051 @return An error from FileHandleGetFileName().\r
2052**/\r
2053EFI_STATUS\r
2054EFIAPI\r
2055EfiShellFindFilesInDir(\r
2056 IN SHELL_FILE_HANDLE FileDirHandle,\r
2057 OUT EFI_SHELL_FILE_INFO **FileList\r
2058 )\r
2059{\r
2060 EFI_SHELL_FILE_INFO *ShellFileList;\r
2061 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
2062 EFI_FILE_INFO *FileInfo;\r
2063 EFI_STATUS Status;\r
2064 BOOLEAN NoFile;\r
2065 CHAR16 *TempString;\r
2066 CHAR16 *BasePath;\r
2067 UINTN Size;\r
2068 CHAR16 *TempSpot;\r
2069\r
0d807dae 2070 BasePath = NULL;\r
a405b86d 2071 Status = FileHandleGetFileName(FileDirHandle, &BasePath);\r
2072 if (EFI_ERROR(Status)) {\r
2073 return (Status);\r
2074 }\r
2075\r
2076 if (ShellFileHandleGetPath(FileDirHandle) != NULL) {\r
2077 TempString = NULL;\r
2078 Size = 0;\r
2079 TempString = StrnCatGrow(&TempString, &Size, ShellFileHandleGetPath(FileDirHandle), 0);\r
532691c8 2080 if (TempString == NULL) {\r
c1f9c346 2081 SHELL_FREE_NON_NULL(BasePath);\r
532691c8 2082 return (EFI_OUT_OF_RESOURCES);\r
2083 }\r
a405b86d 2084 TempSpot = StrStr(TempString, L";");\r
2085\r
2086 if (TempSpot != NULL) {\r
2087 *TempSpot = CHAR_NULL;\r
2088 }\r
2089\r
2090 TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);\r
532691c8 2091 if (TempString == NULL) {\r
c1f9c346 2092 SHELL_FREE_NON_NULL(BasePath);\r
532691c8 2093 return (EFI_OUT_OF_RESOURCES);\r
2094 }\r
c1f9c346 2095 SHELL_FREE_NON_NULL(BasePath);\r
a405b86d 2096 BasePath = TempString;\r
2097 }\r
2098\r
2099 NoFile = FALSE;\r
2100 ShellFileList = NULL;\r
2101 ShellFileListItem = NULL;\r
2102 FileInfo = NULL;\r
2103 Status = EFI_SUCCESS;\r
2104\r
2105\r
2106 for ( Status = FileHandleFindFirstFile(FileDirHandle, &FileInfo)\r
2107 ; !EFI_ERROR(Status) && !NoFile\r
2108 ; Status = FileHandleFindNextFile(FileDirHandle, FileInfo, &NoFile)\r
2109 ){\r
9168df3d
RN
2110 if (ShellFileList == NULL) {\r
2111 ShellFileList = (EFI_SHELL_FILE_INFO*)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
2112 if (ShellFileList == NULL) {\r
2113 SHELL_FREE_NON_NULL (BasePath);\r
2114 return EFI_OUT_OF_RESOURCES;\r
2115 }\r
2116 InitializeListHead(&ShellFileList->Link);\r
2117 }\r
a405b86d 2118 //\r
2119 // allocate a new EFI_SHELL_FILE_INFO and populate it...\r
2120 //\r
a405b86d 2121 ShellFileListItem = CreateAndPopulateShellFileInfo(\r
2122 BasePath,\r
6a5033ca 2123 EFI_SUCCESS, // success since we didn't fail to open it...\r
a405b86d 2124 FileInfo->FileName,\r
c1f9c346 2125 NULL, // no handle since not open\r
a405b86d 2126 FileInfo);\r
9168df3d
RN
2127 if (ShellFileListItem == NULL) {\r
2128 Status = EFI_OUT_OF_RESOURCES;\r
2129 //\r
2130 // Free resources outside the loop.\r
2131 //\r
2132 break;\r
a405b86d 2133 }\r
2134 InsertTailList(&ShellFileList->Link, &ShellFileListItem->Link);\r
2135 }\r
2136 if (EFI_ERROR(Status)) {\r
2137 EfiShellFreeFileList(&ShellFileList);\r
2138 *FileList = NULL;\r
2139 } else {\r
2140 *FileList = ShellFileList;\r
2141 }\r
2142 SHELL_FREE_NON_NULL(BasePath);\r
2143 return(Status);\r
33fe8308 2144}\r
09fd5328
JC
2145\r
2146/**\r
2147 Get the GUID value from a human readable name.\r
2148\r
2149 If GuidName is a known GUID name, then update Guid to have the correct value for\r
2150 that GUID.\r
2151\r
2152 This function is only available when the major and minor versions in the\r
2153 EfiShellProtocol are greater than or equal to 2 and 1, respectively.\r
2154\r
2155 @param[in] GuidName A pointer to the localized name for the GUID being queried.\r
2156 @param[out] Guid A pointer to the GUID structure to be filled in.\r
2157\r
2158 @retval EFI_SUCCESS The operation was successful.\r
2159 @retval EFI_INVALID_PARAMETER Guid was NULL.\r
2160 @retval EFI_INVALID_PARAMETER GuidName was NULL.\r
2161 @retval EFI_NOT_FOUND GuidName is not a known GUID Name.\r
2162**/\r
2163EFI_STATUS\r
c20bd8e1 2164EFIAPI\r
09fd5328
JC
2165EfiShellGetGuidFromName(\r
2166 IN CONST CHAR16 *GuidName,\r
2167 OUT EFI_GUID *Guid\r
2168 )\r
2169{\r
2170 EFI_GUID *NewGuid;\r
2171 EFI_STATUS Status;\r
2172\r
2173 if (Guid == NULL || GuidName == NULL) {\r
2174 return (EFI_INVALID_PARAMETER);\r
2175 }\r
ba0014b9 2176\r
09fd5328
JC
2177 Status = GetGuidFromStringName(GuidName, NULL, &NewGuid);\r
2178\r
2179 if (!EFI_ERROR(Status)) {\r
09bd67f0 2180 CopyGuid(Guid, NewGuid);\r
09fd5328
JC
2181 }\r
2182\r
2183 return (Status);\r
2184}\r
2185\r
2186/**\r
2187 Get the human readable name for a GUID from the value.\r
2188\r
2189 If Guid is assigned a name, then update *GuidName to point to the name. The callee\r
2190 should not modify the value.\r
2191\r
2192 This function is only available when the major and minor versions in the\r
2193 EfiShellProtocol are greater than or equal to 2 and 1, respectively.\r
2194\r
2195 @param[in] Guid A pointer to the GUID being queried.\r
2196 @param[out] GuidName A pointer to a pointer the localized to name for the GUID being requested\r
2197\r
2198 @retval EFI_SUCCESS The operation was successful.\r
2199 @retval EFI_INVALID_PARAMETER Guid was NULL.\r
2200 @retval EFI_INVALID_PARAMETER GuidName was NULL.\r
2201 @retval EFI_NOT_FOUND Guid is not assigned a name.\r
2202**/\r
2203EFI_STATUS\r
c20bd8e1 2204EFIAPI\r
09fd5328
JC
2205EfiShellGetGuidName(\r
2206 IN CONST EFI_GUID *Guid,\r
2207 OUT CONST CHAR16 **GuidName\r
2208 )\r
2209{\r
2210 CHAR16 *Name;\r
2211\r
2212 if (Guid == NULL || GuidName == NULL) {\r
2213 return (EFI_INVALID_PARAMETER);\r
2214 }\r
2215\r
2216 Name = GetStringNameFromGuid(Guid, NULL);\r
2217 if (Name == NULL || StrLen(Name) == 0) {\r
2218 SHELL_FREE_NON_NULL(Name);\r
2219 return (EFI_NOT_FOUND);\r
2220 }\r
2221\r
2222 *GuidName = AddBufferToFreeList(Name);\r
2223\r
2224 return (EFI_SUCCESS);\r
a405b86d 2225}\r
2226\r
a405b86d 2227\r
a405b86d 2228\r
2229/**\r
2230 If FileHandle is a directory then the function reads from FileHandle and reads in\r
2231 each of the FileInfo structures. If one of them matches the Pattern's first\r
2232 "level" then it opens that handle and calls itself on that handle.\r
2233\r
2234 If FileHandle is a file and matches all of the remaining Pattern (which would be\r
2235 on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.\r
2236\r
a405b86d 2237 Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call\r
2238 FreeFileList with FileList.\r
2239\r
4ff7e37b
ED
2240 @param[in] FilePattern The FilePattern to check against.\r
2241 @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure\r
2242 @param[in] FileHandle The FileHandle to start with\r
2243 @param[in, out] FileList pointer to pointer to list of found files.\r
2244 @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.\r
2245 @param[in] MapName The file system name this file is on.\r
a405b86d 2246\r
2247 @retval EFI_SUCCESS all files were found and the FileList contains a list.\r
2248 @retval EFI_NOT_FOUND no files were found\r
2249 @retval EFI_OUT_OF_RESOURCES a memory allocation failed\r
2250**/\r
2251EFI_STATUS\r
a405b86d 2252ShellSearchHandle(\r
2253 IN CONST CHAR16 *FilePattern,\r
2254 IN EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation,\r
2255 IN SHELL_FILE_HANDLE FileHandle,\r
2256 IN OUT EFI_SHELL_FILE_INFO **FileList,\r
8be0ba36 2257 IN CONST EFI_SHELL_FILE_INFO *ParentNode OPTIONAL,\r
2258 IN CONST CHAR16 *MapName\r
a405b86d 2259 )\r
2260{\r
2261 EFI_STATUS Status;\r
2262 CONST CHAR16 *NextFilePatternStart;\r
2263 CHAR16 *CurrentFilePattern;\r
2264 EFI_SHELL_FILE_INFO *ShellInfo;\r
2265 EFI_SHELL_FILE_INFO *ShellInfoNode;\r
2266 EFI_SHELL_FILE_INFO *NewShellNode;\r
d0a5723f 2267 EFI_FILE_INFO *FileInfo;\r
a405b86d 2268 BOOLEAN Directory;\r
8be0ba36 2269 CHAR16 *NewFullName;\r
2270 UINTN Size;\r
a405b86d 2271\r
2272 if ( FilePattern == NULL\r
2273 || UnicodeCollation == NULL\r
2274 || FileList == NULL\r
2275 ){\r
2276 return (EFI_INVALID_PARAMETER);\r
2277 }\r
2278 ShellInfo = NULL;\r
2279 CurrentFilePattern = NULL;\r
2280\r
2281 if (*FilePattern == L'\\') {\r
2282 FilePattern++;\r
2283 }\r
2284\r
2285 for( NextFilePatternStart = FilePattern\r
2286 ; *NextFilePatternStart != CHAR_NULL && *NextFilePatternStart != L'\\'\r
2287 ; NextFilePatternStart++);\r
2288\r
2289 CurrentFilePattern = AllocateZeroPool((NextFilePatternStart-FilePattern+1)*sizeof(CHAR16));\r
9168df3d
RN
2290 if (CurrentFilePattern == NULL) {\r
2291 return EFI_OUT_OF_RESOURCES;\r
2292 }\r
2293\r
a5e28cc1 2294 StrnCpyS(CurrentFilePattern, NextFilePatternStart-FilePattern+1, FilePattern, NextFilePatternStart-FilePattern);\r
a405b86d 2295\r
2296 if (CurrentFilePattern[0] == CHAR_NULL\r
2297 &&NextFilePatternStart[0] == CHAR_NULL\r
d0a5723f 2298 ){\r
a405b86d 2299 //\r
d0a5723f 2300 // we want the parent or root node (if no parent)\r
a405b86d 2301 //\r
2302 if (ParentNode == NULL) {\r
d0a5723f
JC
2303 //\r
2304 // We want the root node. create the node.\r
2305 //\r
2306 FileInfo = FileHandleGetInfo(FileHandle);\r
2307 NewShellNode = CreateAndPopulateShellFileInfo(\r
bbf904d1 2308 MapName,\r
d0a5723f
JC
2309 EFI_SUCCESS,\r
2310 L"\\",\r
2311 FileHandle,\r
2312 FileInfo\r
2313 );\r
2314 SHELL_FREE_NON_NULL(FileInfo);\r
a405b86d 2315 } else {\r
d0a5723f
JC
2316 //\r
2317 // Add the current parameter FileHandle to the list, then end...\r
2318 //\r
a405b86d 2319 NewShellNode = InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO*)ParentNode, TRUE);\r
d0a5723f
JC
2320 }\r
2321 if (NewShellNode == NULL) {\r
2322 Status = EFI_OUT_OF_RESOURCES;\r
2323 } else {\r
2324 NewShellNode->Handle = NULL;\r
2325 if (*FileList == NULL) {\r
2326 *FileList = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
2327 InitializeListHead(&((*FileList)->Link));\r
2328 }\r
a405b86d 2329\r
d0a5723f
JC
2330 //\r
2331 // Add to the returning to use list\r
2332 //\r
2333 InsertTailList(&(*FileList)->Link, &NewShellNode->Link);\r
a405b86d 2334\r
d0a5723f 2335 Status = EFI_SUCCESS;\r
a405b86d 2336 }\r
2337 } else {\r
2338 Status = EfiShellFindFilesInDir(FileHandle, &ShellInfo);\r
2339\r
2340 if (!EFI_ERROR(Status)){\r
2341 if (StrStr(NextFilePatternStart, L"\\") != NULL){\r
2342 Directory = TRUE;\r
2343 } else {\r
2344 Directory = FALSE;\r
2345 }\r
2346 for ( ShellInfoNode = (EFI_SHELL_FILE_INFO*)GetFirstNode(&ShellInfo->Link)\r
2347 ; !IsNull (&ShellInfo->Link, &ShellInfoNode->Link)\r
2348 ; ShellInfoNode = (EFI_SHELL_FILE_INFO*)GetNextNode(&ShellInfo->Link, &ShellInfoNode->Link)\r
2349 ){\r
2350 if (UnicodeCollation->MetaiMatch(UnicodeCollation, (CHAR16*)ShellInfoNode->FileName, CurrentFilePattern)){\r
8be0ba36 2351 if (ShellInfoNode->FullName != NULL && StrStr(ShellInfoNode->FullName, L":") == NULL) {\r
d25d59cb 2352 Size = StrSize (ShellInfoNode->FullName) + StrSize (MapName);\r
8be0ba36 2353 NewFullName = AllocateZeroPool(Size);\r
2354 if (NewFullName == NULL) {\r
2355 Status = EFI_OUT_OF_RESOURCES;\r
2356 } else {\r
d25d59cb
RN
2357 StrCpyS(NewFullName, Size / sizeof(CHAR16), MapName);\r
2358 StrCatS(NewFullName, Size / sizeof(CHAR16), ShellInfoNode->FullName);\r
2359 FreePool ((VOID *) ShellInfoNode->FullName);\r
8be0ba36 2360 ShellInfoNode->FullName = NewFullName;\r
2361 }\r
2362 }\r
c154b997 2363 if (Directory && !EFI_ERROR(Status) && ShellInfoNode->FullName != NULL && ShellInfoNode->FileName != NULL){\r
a405b86d 2364 //\r
2365 // should be a directory\r
2366 //\r
2367\r
2368 //\r
2369 // don't open the . and .. directories\r
2370 //\r
2371 if ( (StrCmp(ShellInfoNode->FileName, L".") != 0)\r
2372 && (StrCmp(ShellInfoNode->FileName, L"..") != 0)\r
2373 ){\r
2374 //\r
2375 //\r
2376 //\r
a405b86d 2377 if (EFI_ERROR(Status)) {\r
2378 break;\r
2379 }\r
2380 //\r
2381 // Open the directory since we need that handle in the next recursion.\r
2382 //\r
2383 ShellInfoNode->Status = EfiShellOpenFileByName (ShellInfoNode->FullName, &ShellInfoNode->Handle, EFI_FILE_MODE_READ);\r
2384\r
2385 //\r
2386 // recurse with the next part of the pattern\r
2387 //\r
8be0ba36 2388 Status = ShellSearchHandle(NextFilePatternStart, UnicodeCollation, ShellInfoNode->Handle, FileList, ShellInfoNode, MapName);\r
06e5ae77
QS
2389 EfiShellClose(ShellInfoNode->Handle);\r
2390 ShellInfoNode->Handle = NULL;\r
a405b86d 2391 }\r
8be0ba36 2392 } else if (!EFI_ERROR(Status)) {\r
a405b86d 2393 //\r
2394 // should be a file\r
2395 //\r
2396\r
2397 //\r
2398 // copy the information we need into a new Node\r
2399 //\r
2400 NewShellNode = InternalDuplicateShellFileInfo(ShellInfoNode, FALSE);\r
a405b86d 2401 if (NewShellNode == NULL) {\r
2402 Status = EFI_OUT_OF_RESOURCES;\r
2403 }\r
2404 if (*FileList == NULL) {\r
733f138d 2405 *FileList = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
a405b86d 2406 InitializeListHead(&((*FileList)->Link));\r
2407 }\r
2408\r
2409 //\r
2410 // Add to the returning to use list\r
2411 //\r
2412 InsertTailList(&(*FileList)->Link, &NewShellNode->Link);\r
2413 }\r
2414 }\r
2415 if (EFI_ERROR(Status)) {\r
2416 break;\r
2417 }\r
2418 }\r
2419 if (EFI_ERROR(Status)) {\r
2420 EfiShellFreeFileList(&ShellInfo);\r
2421 } else {\r
2422 Status = EfiShellFreeFileList(&ShellInfo);\r
2423 }\r
2424 }\r
2425 }\r
2426\r
00afc8f8
DB
2427 if (*FileList == NULL || (*FileList != NULL && IsListEmpty(&(*FileList)->Link))) {\r
2428 Status = EFI_NOT_FOUND;\r
2429 }\r
2430\r
a405b86d 2431 FreePool(CurrentFilePattern);\r
2432 return (Status);\r
2433}\r
2434\r
2435/**\r
2436 Find files that match a specified pattern.\r
2437\r
2438 This function searches for all files and directories that match the specified\r
2439 FilePattern. The FilePattern can contain wild-card characters. The resulting file\r
2440 information is placed in the file list FileList.\r
2441\r
2442 Wildcards are processed\r
2443 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.\r
2444\r
2445 The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo\r
2446 field is set to NULL.\r
2447\r
2448 if *FileList is not NULL then it must be a pre-existing and properly initialized list.\r
2449\r
2450 @param FilePattern Points to a NULL-terminated shell file path, including wildcards.\r
2451 @param FileList On return, points to the start of a file list containing the names\r
2452 of all matching files or else points to NULL if no matching files\r
2453 were found. only on a EFI_SUCCESS return will; this be non-NULL.\r
2454\r
2455 @retval EFI_SUCCESS Files found. FileList is a valid list.\r
2456 @retval EFI_NOT_FOUND No files found.\r
2457 @retval EFI_NO_MEDIA The device has no media\r
2458 @retval EFI_DEVICE_ERROR The device reported an error\r
2459 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted\r
2460**/\r
2461EFI_STATUS\r
2462EFIAPI\r
2463EfiShellFindFiles(\r
2464 IN CONST CHAR16 *FilePattern,\r
2465 OUT EFI_SHELL_FILE_INFO **FileList\r
2466 )\r
2467{\r
2468 EFI_STATUS Status;\r
2469 CHAR16 *PatternCopy;\r
2470 CHAR16 *PatternCurrentLocation;\r
2471 EFI_DEVICE_PATH_PROTOCOL *RootDevicePath;\r
2472 SHELL_FILE_HANDLE RootFileHandle;\r
2473 CHAR16 *MapName;\r
2474 UINTN Count;\r
2475\r
2476 if ( FilePattern == NULL\r
2477 || FileList == NULL\r
2478 || StrStr(FilePattern, L":") == NULL\r
2479 ){\r
2480 return (EFI_INVALID_PARAMETER);\r
2481 }\r
2482 Status = EFI_SUCCESS;\r
2483 RootDevicePath = NULL;\r
2484 RootFileHandle = NULL;\r
2485 MapName = NULL;\r
7f79b01e 2486 PatternCopy = AllocateCopyPool(StrSize(FilePattern), FilePattern);\r
a405b86d 2487 if (PatternCopy == NULL) {\r
2488 return (EFI_OUT_OF_RESOURCES);\r
2489 }\r
a405b86d 2490\r
ab94587a 2491 PatternCopy = PathCleanUpDirectories(PatternCopy);\r
a405b86d 2492\r
d25d59cb
RN
2493 Count = StrStr(PatternCopy, L":") - PatternCopy + 1;\r
2494 ASSERT (Count <= StrLen (PatternCopy));\r
a405b86d 2495\r
2496 ASSERT(MapName == NULL);\r
2497 MapName = StrnCatGrow(&MapName, NULL, PatternCopy, Count);\r
532691c8 2498 if (MapName == NULL) {\r
2499 Status = EFI_OUT_OF_RESOURCES;\r
c8c22591 2500 } else {\r
a405b86d 2501 RootDevicePath = EfiShellGetDevicePathFromFilePath(PatternCopy);\r
2502 if (RootDevicePath == NULL) {\r
2503 Status = EFI_INVALID_PARAMETER;\r
2504 } else {\r
2505 Status = EfiShellOpenRoot(RootDevicePath, &RootFileHandle);\r
2506 if (!EFI_ERROR(Status)) {\r
2507 for ( PatternCurrentLocation = PatternCopy\r
2508 ; *PatternCurrentLocation != ':'\r
2509 ; PatternCurrentLocation++);\r
2510 PatternCurrentLocation++;\r
8be0ba36 2511 Status = ShellSearchHandle(PatternCurrentLocation, gUnicodeCollation, RootFileHandle, FileList, NULL, MapName);\r
06e5ae77 2512 EfiShellClose(RootFileHandle);\r
a405b86d 2513 }\r
2514 FreePool(RootDevicePath);\r
2515 }\r
2516 }\r
2517\r
8be0ba36 2518 SHELL_FREE_NON_NULL(PatternCopy);\r
2519 SHELL_FREE_NON_NULL(MapName);\r
a405b86d 2520\r
2521 return(Status);\r
2522}\r
2523\r
2524/**\r
2525 Opens the files that match the path specified.\r
2526\r
2527 This function opens all of the files specified by Path. Wildcards are processed\r
2528 according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each\r
2529 matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.\r
2530\r
2531 @param Path A pointer to the path string.\r
2532 @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or\r
2533 EFI_FILE_MODE_WRITE.\r
2534 @param FileList Points to the start of a list of files opened.\r
2535\r
2536 @retval EFI_SUCCESS Create the file list successfully.\r
2537 @return Others Can't create the file list.\r
2538**/\r
2539EFI_STATUS\r
2540EFIAPI\r
2541EfiShellOpenFileList(\r
2542 IN CHAR16 *Path,\r
2543 IN UINT64 OpenMode,\r
2544 IN OUT EFI_SHELL_FILE_INFO **FileList\r
2545 )\r
2546{\r
2547 EFI_STATUS Status;\r
2548 EFI_SHELL_FILE_INFO *ShellFileListItem;\r
2549 CHAR16 *Path2;\r
2550 UINTN Path2Size;\r
2551 CONST CHAR16 *CurDir;\r
733f138d 2552 BOOLEAN Found;\r
a405b86d 2553\r
ab94587a 2554 PathCleanUpDirectories(Path);\r
a405b86d 2555\r
2556 Path2Size = 0;\r
2557 Path2 = NULL;\r
2558\r
733f138d 2559 if (FileList == NULL || *FileList == NULL) {\r
2560 return (EFI_INVALID_PARAMETER);\r
2561 }\r
a405b86d 2562\r
2563 if (*Path == L'.' && *(Path+1) == L'\\') {\r
733f138d 2564 Path+=2;\r
a405b86d 2565 }\r
2566\r
2567 //\r
2568 // convert a local path to an absolute path\r
2569 //\r
2570 if (StrStr(Path, L":") == NULL) {\r
2571 CurDir = EfiShellGetCurDir(NULL);\r
2572 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));\r
2573 StrnCatGrow(&Path2, &Path2Size, CurDir, 0);\r
fbd2dfad 2574 StrnCatGrow(&Path2, &Path2Size, L"\\", 0);\r
a405b86d 2575 if (*Path == L'\\') {\r
2576 Path++;\r
ab94587a 2577 while (PathRemoveLastItem(Path2)) ;\r
a405b86d 2578 }\r
2579 ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));\r
2580 StrnCatGrow(&Path2, &Path2Size, Path, 0);\r
2581 } else {\r
2582 ASSERT(Path2 == NULL);\r
2583 StrnCatGrow(&Path2, NULL, Path, 0);\r
2584 }\r
2585\r
ab94587a 2586 PathCleanUpDirectories (Path2);\r
a405b86d 2587\r
2588 //\r
2589 // do the search\r
2590 //\r
2591 Status = EfiShellFindFiles(Path2, FileList);\r
2592\r
2593 FreePool(Path2);\r
2594\r
2595 if (EFI_ERROR(Status)) {\r
2596 return (Status);\r
2597 }\r
2598\r
733f138d 2599 Found = FALSE;\r
a405b86d 2600 //\r
2601 // We had no errors so open all the files (that are not already opened...)\r
2602 //\r
2603 for ( ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetFirstNode(&(*FileList)->Link)\r
2604 ; !IsNull(&(*FileList)->Link, &ShellFileListItem->Link)\r
2605 ; ShellFileListItem = (EFI_SHELL_FILE_INFO*)GetNextNode(&(*FileList)->Link, &ShellFileListItem->Link)\r
2606 ){\r
2607 if (ShellFileListItem->Status == 0 && ShellFileListItem->Handle == NULL) {\r
2608 ShellFileListItem->Status = EfiShellOpenFileByName (ShellFileListItem->FullName, &ShellFileListItem->Handle, OpenMode);\r
733f138d 2609 Found = TRUE;\r
a405b86d 2610 }\r
2611 }\r
2612\r
733f138d 2613 if (!Found) {\r
2614 return (EFI_NOT_FOUND);\r
2615 }\r
a405b86d 2616 return(EFI_SUCCESS);\r
2617}\r
2618\r
2619/**\r
09fd5328
JC
2620 Gets the environment variable and Attributes, or list of environment variables. Can be\r
2621 used instead of GetEnv().\r
2622\r
2623 This function returns the current value of the specified environment variable and\r
2624 the Attributes. If no variable name was specified, then all of the known\r
2625 variables will be returned.\r
2626\r
2627 @param[in] Name A pointer to the environment variable name. If Name is NULL,\r
2628 then the function will return all of the defined shell\r
2629 environment variables. In the case where multiple environment\r
2630 variables are being returned, each variable will be terminated\r
2631 by a NULL, and the list will be terminated by a double NULL.\r
2632 @param[out] Attributes If not NULL, a pointer to the returned attributes bitmask for\r
2633 the environment variable. In the case where Name is NULL, and\r
2634 multiple environment variables are being returned, Attributes\r
2635 is undefined.\r
2636\r
f5ba4007
QS
2637 @retval NULL The environment variable doesn't exist.\r
2638 @return A non-NULL value points to the variable's value. The returned\r
09fd5328 2639 pointer does not need to be freed by the caller.\r
a405b86d 2640**/\r
2641CONST CHAR16 *\r
c20bd8e1 2642EFIAPI\r
09fd5328
JC
2643EfiShellGetEnvEx(\r
2644 IN CONST CHAR16 *Name,\r
2645 OUT UINT32 *Attributes OPTIONAL\r
a405b86d 2646 )\r
2647{\r
2648 EFI_STATUS Status;\r
2649 VOID *Buffer;\r
2650 UINTN Size;\r
a405b86d 2651 ENV_VAR_LIST *Node;\r
2652 CHAR16 *CurrentWriteLocation;\r
2653\r
2654 Size = 0;\r
2655 Buffer = NULL;\r
2656\r
2657 if (Name == NULL) {\r
a405b86d 2658\r
2659 //\r
2660 // Build the semi-colon delimited list. (2 passes)\r
2661 //\r
b62bb885
QS
2662 for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link)\r
2663 ; !IsNull(&gShellEnvVarList.Link, &Node->Link)\r
2664 ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link)\r
a405b86d 2665 ){\r
2666 ASSERT(Node->Key != NULL);\r
2667 Size += StrSize(Node->Key);\r
2668 }\r
2669\r
2670 Size += 2*sizeof(CHAR16);\r
2671\r
2672 Buffer = AllocateZeroPool(Size);\r
3c865f20 2673 if (Buffer == NULL) {\r
3c865f20 2674 return (NULL);\r
2675 }\r
a405b86d 2676 CurrentWriteLocation = (CHAR16*)Buffer;\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
ba0014b9
LG
2683 StrCpyS( CurrentWriteLocation,\r
2684 (Size)/sizeof(CHAR16) - (CurrentWriteLocation - ((CHAR16*)Buffer)),\r
e75390f0
QS
2685 Node->Key\r
2686 );\r
a405b86d 2687 CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1;\r
2688 }\r
2689\r
a405b86d 2690 } else {\r
2691 //\r
2692 // We are doing a specific environment variable\r
2693 //\r
b62bb885 2694 Status = ShellFindEnvVarInList(Name, (CHAR16**)&Buffer, &Size, Attributes);\r
a405b86d 2695\r
b62bb885 2696 if (EFI_ERROR(Status)){\r
a405b86d 2697 //\r
b62bb885 2698 // get the size we need for this EnvVariable\r
a405b86d 2699 //\r
09fd5328 2700 Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer);\r
b62bb885
QS
2701 if (Status == EFI_BUFFER_TOO_SMALL) {\r
2702 //\r
2703 // Allocate the space and recall the get function\r
2704 //\r
2705 Buffer = AllocateZeroPool(Size);\r
2706 Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer);\r
2707 }\r
2708 //\r
6a5033ca 2709 // we didn't get it (might not exist)\r
b62bb885
QS
2710 // free the memory if we allocated any and return NULL\r
2711 //\r
2712 if (EFI_ERROR(Status)) {\r
2713 if (Buffer != NULL) {\r
2714 FreePool(Buffer);\r
2715 }\r
2716 return (NULL);\r
2717 } else {\r
2718 //\r
2719 // If we did not find the environment variable in the gShellEnvVarList\r
2720 // but get it from UEFI variable storage successfully then we need update\r
2721 // the gShellEnvVarList.\r
2722 //\r
2723 ShellFreeEnvVarList ();\r
2724 Status = ShellInitEnvVarList ();\r
2725 ASSERT (Status == EFI_SUCCESS);\r
a405b86d 2726 }\r
a405b86d 2727 }\r
2728 }\r
2729\r
2730 //\r
2731 // return the buffer\r
2732 //\r
2733 return (AddBufferToFreeList(Buffer));\r
2734}\r
2735\r
09fd5328
JC
2736/**\r
2737 Gets either a single or list of environment variables.\r
2738\r
2739 If name is not NULL then this function returns the current value of the specified\r
2740 environment variable.\r
2741\r
2742 If Name is NULL, then a list of all environment variable names is returned. Each is a\r
2743 NULL terminated string with a double NULL terminating the list.\r
2744\r
2745 @param Name A pointer to the environment variable name. If\r
2746 Name is NULL, then the function will return all\r
2747 of the defined shell environment variables. In\r
2748 the case where multiple environment variables are\r
2749 being returned, each variable will be terminated by\r
2750 a NULL, and the list will be terminated by a double\r
2751 NULL.\r
2752\r
2753 @retval !=NULL A pointer to the returned string.\r
2754 The returned pointer does not need to be freed by the caller.\r
2755\r
2756 @retval NULL The environment variable doesn't exist or there are\r
2757 no environment variables.\r
2758**/\r
2759CONST CHAR16 *\r
2760EFIAPI\r
2761EfiShellGetEnv(\r
2762 IN CONST CHAR16 *Name\r
2763 )\r
2764{\r
2765 return (EfiShellGetEnvEx(Name, NULL));\r
2766}\r
2767\r
a405b86d 2768/**\r
2769 Internal variable setting function. Allows for setting of the read only variables.\r
2770\r
2771 @param Name Points to the NULL-terminated environment variable name.\r
2772 @param Value Points to the NULL-terminated environment variable value. If the value is an\r
2773 empty string then the environment variable is deleted.\r
2774 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
2775\r
2776 @retval EFI_SUCCESS The environment variable was successfully updated.\r
2777**/\r
2778EFI_STATUS\r
a405b86d 2779InternalEfiShellSetEnv(\r
2780 IN CONST CHAR16 *Name,\r
2781 IN CONST CHAR16 *Value,\r
2782 IN BOOLEAN Volatile\r
2783 )\r
2784{\r
b62bb885 2785 EFI_STATUS Status;\r
b62bb885 2786\r
a405b86d 2787 if (Value == NULL || StrLen(Value) == 0) {\r
b62bb885
QS
2788 Status = SHELL_DELETE_ENVIRONMENT_VARIABLE(Name);\r
2789 if (!EFI_ERROR(Status)) {\r
2790 ShellRemvoeEnvVarFromList(Name);\r
2791 }\r
a405b86d 2792 } else {\r
2793 SHELL_DELETE_ENVIRONMENT_VARIABLE(Name);\r
ffbc60a0
RN
2794 Status = ShellAddEnvVarToList(\r
2795 Name, Value, StrSize(Value),\r
2796 EFI_VARIABLE_BOOTSERVICE_ACCESS | (Volatile ? 0 : EFI_VARIABLE_NON_VOLATILE)\r
2797 );\r
2798 if (!EFI_ERROR (Status)) {\r
2799 Status = Volatile\r
c5c994c5
CC
2800 ? SHELL_SET_ENVIRONMENT_VARIABLE_V (Name, StrSize (Value) - sizeof (CHAR16), Value)\r
2801 : SHELL_SET_ENVIRONMENT_VARIABLE_NV (Name, StrSize (Value) - sizeof (CHAR16), Value);\r
ffbc60a0
RN
2802 if (EFI_ERROR (Status)) {\r
2803 ShellRemvoeEnvVarFromList(Name);\r
b62bb885 2804 }\r
a405b86d 2805 }\r
2806 }\r
ffbc60a0 2807 return Status;\r
a405b86d 2808}\r
2809\r
2810/**\r
2811 Sets the environment variable.\r
2812\r
2813 This function changes the current value of the specified environment variable. If the\r
2814 environment variable exists and the Value is an empty string, then the environment\r
2815 variable is deleted. If the environment variable exists and the Value is not an empty\r
2816 string, then the value of the environment variable is changed. If the environment\r
2817 variable does not exist and the Value is an empty string, there is no action. If the\r
2818 environment variable does not exist and the Value is a non-empty string, then the\r
2819 environment variable is created and assigned the specified value.\r
2820\r
2821 For a description of volatile and non-volatile environment variables, see UEFI Shell\r
2822 2.0 specification section 3.6.1.\r
2823\r
2824 @param Name Points to the NULL-terminated environment variable name.\r
2825 @param Value Points to the NULL-terminated environment variable value. If the value is an\r
2826 empty string then the environment variable is deleted.\r
2827 @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).\r
2828\r
2829 @retval EFI_SUCCESS The environment variable was successfully updated.\r
2830**/\r
2831EFI_STATUS\r
2832EFIAPI\r
2833EfiShellSetEnv(\r
2834 IN CONST CHAR16 *Name,\r
2835 IN CONST CHAR16 *Value,\r
2836 IN BOOLEAN Volatile\r
2837 )\r
2838{\r
2839 if (Name == NULL || *Name == CHAR_NULL) {\r
2840 return (EFI_INVALID_PARAMETER);\r
2841 }\r
2842 //\r
2843 // Make sure we dont 'set' a predefined read only variable\r
2844 //\r
0e967dff
RN
2845 if ((StrCmp (Name, L"cwd") == 0) ||\r
2846 (StrCmp (Name, L"lasterror") == 0) ||\r
2847 (StrCmp (Name, L"profiles") == 0) ||\r
2848 (StrCmp (Name, L"uefishellsupport") == 0) ||\r
2849 (StrCmp (Name, L"uefishellversion") == 0) ||\r
2850 (StrCmp (Name, L"uefiversion") == 0) ||\r
2851 (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest &&\r
2852 StrCmp (Name, mNoNestingEnvVarName) == 0)\r
2853 ) {\r
a405b86d 2854 return (EFI_INVALID_PARAMETER);\r
2855 }\r
2856 return (InternalEfiShellSetEnv(Name, Value, Volatile));\r
2857}\r
2858\r
2859/**\r
2860 Returns the current directory on the specified device.\r
2861\r
2862 If FileSystemMapping is NULL, it returns the current working directory. If the\r
2863 FileSystemMapping is not NULL, it returns the current directory associated with the\r
2864 FileSystemMapping. In both cases, the returned name includes the file system\r
2865 mapping (i.e. fs0:\current-dir).\r
dcbdb8bf 2866\r
fbd2dfad 2867 Note that the current directory string should exclude the tailing backslash character.\r
a405b86d 2868\r
2869 @param FileSystemMapping A pointer to the file system mapping. If NULL,\r
2870 then the current working directory is returned.\r
2871\r
2872 @retval !=NULL The current directory.\r
2873 @retval NULL Current directory does not exist.\r
2874**/\r
2875CONST CHAR16 *\r
2876EFIAPI\r
2877EfiShellGetCurDir(\r
2878 IN CONST CHAR16 *FileSystemMapping OPTIONAL\r
2879 )\r
2880{\r
2881 CHAR16 *PathToReturn;\r
2882 UINTN Size;\r
2883 SHELL_MAP_LIST *MapListItem;\r
2884 if (!IsListEmpty(&gShellMapList.Link)) {\r
2885 //\r
2886 // if parameter is NULL, use current\r
2887 //\r
2888 if (FileSystemMapping == NULL) {\r
2889 return (EfiShellGetEnv(L"cwd"));\r
2890 } else {\r
2891 Size = 0;\r
2892 PathToReturn = NULL;\r
2893 MapListItem = ShellCommandFindMapItem(FileSystemMapping);\r
2894 if (MapListItem != NULL) {\r
2895 ASSERT((PathToReturn == NULL && Size == 0) || (PathToReturn != NULL));\r
2896 PathToReturn = StrnCatGrow(&PathToReturn, &Size, MapListItem->MapName, 0);\r
2897 PathToReturn = StrnCatGrow(&PathToReturn, &Size, MapListItem->CurrentDirectoryPath, 0);\r
2898 }\r
2899 }\r
2900 return (AddBufferToFreeList(PathToReturn));\r
2901 } else {\r
2902 return (NULL);\r
2903 }\r
2904}\r
2905\r
2906/**\r
2907 Changes the current directory on the specified device.\r
2908\r
2909 If the FileSystem is NULL, and the directory Dir does not contain a file system's\r
2910 mapped name, this function changes the current working directory.\r
2911\r
2912 If the FileSystem is NULL and the directory Dir contains a mapped name, then the\r
2913 current file system and the current directory on that file system are changed.\r
2914\r
2915 If FileSystem is NULL, and Dir is not NULL, then this changes the current working file\r
2916 system.\r
2917\r
2918 If FileSystem is not NULL and Dir is not NULL, then this function changes the current\r
2919 directory on the specified file system.\r
2920\r
2921 If the current working directory or the current working file system is changed then the\r
2922 %cwd% environment variable will be updated\r
2923\r
fbd2dfad
QS
2924 Note that the current directory string should exclude the tailing backslash character.\r
2925\r
a405b86d 2926 @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working\r
2927 directory is changed.\r
2928 @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.\r
2929\r
6a5033ca 2930 @retval EFI_SUCCESS The operation was successful\r
a405b86d 2931 @retval EFI_NOT_FOUND The file system could not be found\r
2932**/\r
2933EFI_STATUS\r
2934EFIAPI\r
2935EfiShellSetCurDir(\r
2936 IN CONST CHAR16 *FileSystem OPTIONAL,\r
2937 IN CONST CHAR16 *Dir\r
2938 )\r
2939{\r
2940 CHAR16 *MapName;\r
2941 SHELL_MAP_LIST *MapListItem;\r
2942 UINTN Size;\r
2943 EFI_STATUS Status;\r
2944 CHAR16 *TempString;\r
2945 CHAR16 *DirectoryName;\r
2946 UINTN TempLen;\r
2947\r
2948 Size = 0;\r
2949 MapName = NULL;\r
2950 MapListItem = NULL;\r
2951 TempString = NULL;\r
2952 DirectoryName = NULL;\r
2953\r
3c865f20 2954 if ((FileSystem == NULL && Dir == NULL) || Dir == NULL) {\r
a405b86d 2955 return (EFI_INVALID_PARAMETER);\r
2956 }\r
2957\r
2958 if (IsListEmpty(&gShellMapList.Link)){\r
2959 return (EFI_NOT_FOUND);\r
2960 }\r
2961\r
2962 DirectoryName = StrnCatGrow(&DirectoryName, NULL, Dir, 0);\r
2963 ASSERT(DirectoryName != NULL);\r
2964\r
ab94587a 2965 PathCleanUpDirectories(DirectoryName);\r
a405b86d 2966\r
2967 if (FileSystem == NULL) {\r
2968 //\r
2969 // determine the file system mapping to use\r
2970 //\r
2971 if (StrStr(DirectoryName, L":") != NULL) {\r
2972 ASSERT(MapName == NULL);\r
2973 MapName = StrnCatGrow(&MapName, NULL, DirectoryName, (StrStr(DirectoryName, L":")-DirectoryName+1));\r
2974 }\r
2975 //\r
2976 // find the file system mapping's entry in the list\r
2977 // or use current\r
2978 //\r
2979 if (MapName != NULL) {\r
2980 MapListItem = ShellCommandFindMapItem(MapName);\r
2981\r
2982 //\r
2983 // make that the current file system mapping\r
2984 //\r
2985 if (MapListItem != NULL) {\r
9cf45187 2986 gShellCurMapping = MapListItem;\r
a405b86d 2987 }\r
2988 } else {\r
9cf45187 2989 MapListItem = gShellCurMapping;\r
a405b86d 2990 }\r
2991\r
2992 if (MapListItem == NULL) {\r
4aec9fe3
JY
2993 FreePool (DirectoryName);\r
2994 SHELL_FREE_NON_NULL(MapName);\r
a405b86d 2995 return (EFI_NOT_FOUND);\r
2996 }\r
2997\r
2998 //\r
2999 // now update the MapListItem's current directory\r
3000 //\r
3001 if (MapListItem->CurrentDirectoryPath != NULL && DirectoryName[StrLen(DirectoryName) - 1] != L':') {\r
3002 FreePool(MapListItem->CurrentDirectoryPath);\r
3003 MapListItem->CurrentDirectoryPath = NULL;\r
3004 }\r
3005 if (MapName != NULL) {\r
3006 TempLen = StrLen(MapName);\r
3007 if (TempLen != StrLen(DirectoryName)) {\r
3008 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
3009 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName+StrLen(MapName), 0);\r
3010 }\r
4aec9fe3 3011 FreePool (MapName);\r
a405b86d 3012 } else {\r
3013 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
3014 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);\r
3015 }\r
fbd2dfad 3016 if ((MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] == L'\\') || (MapListItem->CurrentDirectoryPath == NULL)) {\r
a405b86d 3017 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
fbd2dfad
QS
3018 if (MapListItem->CurrentDirectoryPath != NULL) {\r
3019 MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] = CHAR_NULL;\r
dcbdb8bf 3020 }\r
a405b86d 3021 }\r
3022 } else {\r
3023 //\r
3024 // cant have a mapping in the directory...\r
3025 //\r
3026 if (StrStr(DirectoryName, L":") != NULL) {\r
4aec9fe3 3027 FreePool (DirectoryName);\r
a405b86d 3028 return (EFI_INVALID_PARAMETER);\r
3029 }\r
3030 //\r
3031 // FileSystem != NULL\r
3032 //\r
3033 MapListItem = ShellCommandFindMapItem(FileSystem);\r
3034 if (MapListItem == NULL) {\r
4aec9fe3 3035 FreePool (DirectoryName);\r
a405b86d 3036 return (EFI_INVALID_PARAMETER);\r
3037 }\r
9cf45187 3038// gShellCurMapping = MapListItem;\r
a405b86d 3039 if (DirectoryName != NULL) {\r
3040 //\r
3041 // change current dir on that file system\r
3042 //\r
3043\r
3044 if (MapListItem->CurrentDirectoryPath != NULL) {\r
3045 FreePool(MapListItem->CurrentDirectoryPath);\r
3046 DEBUG_CODE(MapListItem->CurrentDirectoryPath = NULL;);\r
3047 }\r
3048// ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
3049// MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);\r
3050 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
3051 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);\r
3052 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
3053 MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);\r
fbd2dfad 3054 if (MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] == L'\\') {\r
a405b86d 3055 ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
fbd2dfad 3056 MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] = CHAR_NULL;\r
a405b86d 3057 }\r
3058 }\r
3059 }\r
4aec9fe3 3060 FreePool (DirectoryName);\r
a405b86d 3061 //\r
3062 // if updated the current directory then update the environment variable\r
3063 //\r
9cf45187 3064 if (MapListItem == gShellCurMapping) {\r
a405b86d 3065 Size = 0;\r
3066 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
3067 StrnCatGrow(&TempString, &Size, MapListItem->MapName, 0);\r
3068 ASSERT((TempString == NULL && Size == 0) || (TempString != NULL));\r
3069 StrnCatGrow(&TempString, &Size, MapListItem->CurrentDirectoryPath, 0);\r
3070 Status = InternalEfiShellSetEnv(L"cwd", TempString, TRUE);\r
3071 FreePool(TempString);\r
3072 return (Status);\r
3073 }\r
3074 return(EFI_SUCCESS);\r
3075}\r
3076\r
3077/**\r
3078 Return help information about a specific command.\r
3079\r
3080 This function returns the help information for the specified command. The help text\r
3081 can be internal to the shell or can be from a UEFI Shell manual page.\r
3082\r
3083 If Sections is specified, then each section name listed will be compared in a casesensitive\r
3084 manner, to the section names described in Appendix B. If the section exists,\r
3085 it will be appended to the returned help text. If the section does not exist, no\r
3086 information will be returned. If Sections is NULL, then all help text information\r
3087 available will be returned.\r
3088\r
3089 @param Command Points to the NULL-terminated UEFI Shell command name.\r
3090 @param Sections Points to the NULL-terminated comma-delimited\r
3091 section names to return. If NULL, then all\r
3092 sections will be returned.\r
3093 @param HelpText On return, points to a callee-allocated buffer\r
3094 containing all specified help text.\r
3095\r
3096 @retval EFI_SUCCESS The help text was returned.\r
3097 @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the\r
3098 returned help text.\r
3099 @retval EFI_INVALID_PARAMETER HelpText is NULL\r
3100 @retval EFI_NOT_FOUND There is no help text available for Command.\r
3101**/\r
3102EFI_STATUS\r
3103EFIAPI\r
3104EfiShellGetHelpText(\r
3105 IN CONST CHAR16 *Command,\r
3106 IN CONST CHAR16 *Sections OPTIONAL,\r
3107 OUT CHAR16 **HelpText\r
3108 )\r
3109{\r
3110 CONST CHAR16 *ManFileName;\r
42f75495
SQ
3111 CHAR16 *FixCommand;\r
3112 EFI_STATUS Status;\r
a405b86d 3113\r
3114 ASSERT(HelpText != NULL);\r
42f75495 3115 FixCommand = NULL;\r
a405b86d 3116\r
3117 ManFileName = ShellCommandGetManFileNameHandler(Command);\r
3118\r
3119 if (ManFileName != NULL) {\r
3120 return (ProcessManFile(ManFileName, Command, Sections, NULL, HelpText));\r
3121 } else {\r
42f75495
SQ
3122 if ((StrLen(Command)> 4)\r
3123 && (Command[StrLen(Command)-1] == L'i' || Command[StrLen(Command)-1] == L'I')\r
3124 && (Command[StrLen(Command)-2] == L'f' || Command[StrLen(Command)-2] == L'F')\r
3125 && (Command[StrLen(Command)-3] == L'e' || Command[StrLen(Command)-3] == L'E')\r
3126 && (Command[StrLen(Command)-4] == L'.')\r
3127 ) {\r
3128 FixCommand = AllocateZeroPool(StrSize(Command) - 4 * sizeof (CHAR16));\r
9168df3d
RN
3129 if (FixCommand == NULL) {\r
3130 return EFI_OUT_OF_RESOURCES;\r
3131 }\r
42f75495 3132\r
ba0014b9
LG
3133 StrnCpyS( FixCommand,\r
3134 (StrSize(Command) - 4 * sizeof (CHAR16))/sizeof(CHAR16),\r
3135 Command,\r
e75390f0
QS
3136 StrLen(Command)-4\r
3137 );\r
42f75495
SQ
3138 Status = ProcessManFile(FixCommand, FixCommand, Sections, NULL, HelpText);\r
3139 FreePool(FixCommand);\r
3140 return Status;\r
3141 } else {\r
3142 return (ProcessManFile(Command, Command, Sections, NULL, HelpText));\r
3143 }\r
a405b86d 3144 }\r
3145}\r
3146\r
3147/**\r
3148 Gets the enable status of the page break output mode.\r
3149\r
3150 User can use this function to determine current page break mode.\r
3151\r
3152 @retval TRUE The page break output mode is enabled.\r
3153 @retval FALSE The page break output mode is disabled.\r
3154**/\r
3155BOOLEAN\r
3156EFIAPI\r
3157EfiShellGetPageBreak(\r
3158 VOID\r
3159 )\r
3160{\r
3161 return(ShellInfoObject.PageBreakEnabled);\r
3162}\r
3163\r
3164/**\r
3165 Judges whether the active shell is the root shell.\r
3166\r
3167 This function makes the user to know that whether the active Shell is the root shell.\r
3168\r
3169 @retval TRUE The active Shell is the root Shell.\r
3170 @retval FALSE The active Shell is NOT the root Shell.\r
3171**/\r
3172BOOLEAN\r
3173EFIAPI\r
3174EfiShellIsRootShell(\r
3175 VOID\r
3176 )\r
3177{\r
3178 return(ShellInfoObject.RootShellInstance);\r
3179}\r
3180\r
3181/**\r
6a5033ca 3182 function to return a semi-colon delimited list of all alias' in the current shell\r
a405b86d 3183\r
3184 up to caller to free the memory.\r
3185\r
3186 @retval NULL No alias' were found\r
6a5033ca 3187 @retval NULL An error occurred getting alias'\r
a405b86d 3188 @return !NULL a list of all alias'\r
3189**/\r
3190CHAR16 *\r
a405b86d 3191InternalEfiShellGetListAlias(\r
e7275d3f 3192 VOID\r
a405b86d 3193 )\r
3194{\r
ba0014b9 3195\r
a405b86d 3196 EFI_STATUS Status;\r
3197 EFI_GUID Guid;\r
3198 CHAR16 *VariableName;\r
3199 UINTN NameSize;\r
654a012b 3200 UINTN NameBufferSize;\r
a405b86d 3201 CHAR16 *RetVal;\r
3202 UINTN RetSize;\r
a405b86d 3203\r
654a012b
QS
3204 NameBufferSize = INIT_NAME_BUFFER_SIZE;\r
3205 VariableName = AllocateZeroPool(NameBufferSize);\r
a405b86d 3206 RetSize = 0;\r
3207 RetVal = NULL;\r
3208\r
3c865f20 3209 if (VariableName == NULL) {\r
3210 return (NULL);\r
3211 }\r
3212\r
a405b86d 3213 VariableName[0] = CHAR_NULL;\r
3214\r
3215 while (TRUE) {\r
654a012b 3216 NameSize = NameBufferSize;\r
a405b86d 3217 Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);\r
3218 if (Status == EFI_NOT_FOUND){\r
3219 break;\r
654a012b
QS
3220 } else if (Status == EFI_BUFFER_TOO_SMALL) {\r
3221 NameBufferSize = NameSize > NameBufferSize * 2 ? NameSize : NameBufferSize * 2;\r
3222 SHELL_FREE_NON_NULL(VariableName);\r
3223 VariableName = AllocateZeroPool(NameBufferSize);\r
3224 if (VariableName == NULL) {\r
3225 Status = EFI_OUT_OF_RESOURCES;\r
3226 SHELL_FREE_NON_NULL(RetVal);\r
3227 RetVal = NULL;\r
3228 break;\r
3229 }\r
ba0014b9 3230\r
654a012b
QS
3231 NameSize = NameBufferSize;\r
3232 Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);\r
3233 }\r
ba0014b9 3234\r
654a012b
QS
3235 if (EFI_ERROR (Status)) {\r
3236 SHELL_FREE_NON_NULL(RetVal);\r
3237 RetVal = NULL;\r
a405b86d 3238 break;\r
3239 }\r
ba0014b9 3240\r
a405b86d 3241 if (CompareGuid(&Guid, &gShellAliasGuid)){\r
a405b86d 3242 ASSERT((RetVal == NULL && RetSize == 0) || (RetVal != NULL));\r
3243 RetVal = StrnCatGrow(&RetVal, &RetSize, VariableName, 0);\r
3244 RetVal = StrnCatGrow(&RetVal, &RetSize, L";", 0);\r
3245 } // compare guid\r
3246 } // while\r
654a012b 3247 SHELL_FREE_NON_NULL(VariableName);\r
a405b86d 3248\r
3249 return (RetVal);\r
3250}\r
3251\r
b9b77ab1
BJ
3252/**\r
3253 Convert a null-terminated unicode string, in-place, to all lowercase.\r
3254 Then return it.\r
ba0014b9 3255\r
4f344fff 3256 @param Str The null-terminated string to be converted to all lowercase.\r
ba0014b9
LG
3257\r
3258 @return The null-terminated string converted into all lowercase.\r
b9b77ab1 3259**/\r
de4caceb 3260CHAR16 *\r
b9b77ab1
BJ
3261ToLower (\r
3262 CHAR16 *Str\r
3263 )\r
3264{\r
3265 UINTN Index;\r
3266\r
3267 for (Index = 0; Str[Index] != L'\0'; Index++) {\r
3268 if (Str[Index] >= L'A' && Str[Index] <= L'Z') {\r
f716d7b8 3269 Str[Index] -= (CHAR16)(L'A' - L'a');\r
b9b77ab1
BJ
3270 }\r
3271 }\r
3272 return Str;\r
3273}\r
3274\r
a405b86d 3275/**\r
3276 This function returns the command associated with a alias or a list of all\r
3277 alias'.\r
3278\r
3279 @param[in] Alias Points to the NULL-terminated shell alias.\r
3280 If this parameter is NULL, then all\r
3281 aliases will be returned in ReturnedData.\r
3282 @param[out] Volatile upon return of a single command if TRUE indicates\r
3283 this is stored in a volatile fashion. FALSE otherwise.\r
3284\r
ba0014b9 3285 @return If Alias is not NULL, it will return a pointer to\r
a405b86d 3286 the NULL-terminated command for that alias.\r
3287 If Alias is NULL, ReturnedData points to a ';'\r
3288 delimited list of alias (e.g.\r
3289 ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.\r
6a5033ca 3290 @retval NULL an error occurred\r
a405b86d 3291 @retval NULL Alias was not a valid Alias\r
3292**/\r
3293CONST CHAR16 *\r
3294EFIAPI\r
3295EfiShellGetAlias(\r
3296 IN CONST CHAR16 *Alias,\r
3297 OUT BOOLEAN *Volatile OPTIONAL\r
3298 )\r
3299{\r
3300 CHAR16 *RetVal;\r
3301 UINTN RetSize;\r
3302 UINT32 Attribs;\r
3303 EFI_STATUS Status;\r
b9b77ab1 3304 CHAR16 *AliasLower;\r
4c33aace 3305 CHAR16 *AliasVal;\r
a405b86d 3306\r
b9b77ab1 3307 // Convert to lowercase to make aliases case-insensitive\r
a405b86d 3308 if (Alias != NULL) {\r
b9b77ab1 3309 AliasLower = AllocateCopyPool (StrSize (Alias), Alias);\r
9168df3d
RN
3310 if (AliasLower == NULL) {\r
3311 return NULL;\r
3312 }\r
b9b77ab1
BJ
3313 ToLower (AliasLower);\r
3314\r
a405b86d 3315 if (Volatile == NULL) {\r
4c33aace 3316 GetVariable2 (AliasLower, &gShellAliasGuid, (VOID **)&AliasVal, NULL);\r
06e5ae77 3317 FreePool(AliasLower);\r
4c33aace 3318 return (AddBufferToFreeList(AliasVal));\r
a405b86d 3319 }\r
3320 RetSize = 0;\r
3321 RetVal = NULL;\r
b9b77ab1 3322 Status = gRT->GetVariable(AliasLower, &gShellAliasGuid, &Attribs, &RetSize, RetVal);\r
a405b86d 3323 if (Status == EFI_BUFFER_TOO_SMALL) {\r
3324 RetVal = AllocateZeroPool(RetSize);\r
b9b77ab1 3325 Status = gRT->GetVariable(AliasLower, &gShellAliasGuid, &Attribs, &RetSize, RetVal);\r
a405b86d 3326 }\r
3327 if (EFI_ERROR(Status)) {\r
3328 if (RetVal != NULL) {\r
3329 FreePool(RetVal);\r
3330 }\r
06e5ae77 3331 FreePool(AliasLower);\r
a405b86d 3332 return (NULL);\r
3333 }\r
3334 if ((EFI_VARIABLE_NON_VOLATILE & Attribs) == EFI_VARIABLE_NON_VOLATILE) {\r
3335 *Volatile = FALSE;\r
3336 } else {\r
3337 *Volatile = TRUE;\r
3338 }\r
3339\r
b9b77ab1 3340 FreePool (AliasLower);\r
a405b86d 3341 return (AddBufferToFreeList(RetVal));\r
3342 }\r
3343 return (AddBufferToFreeList(InternalEfiShellGetListAlias()));\r
3344}\r
3345\r
3346/**\r
3347 Changes a shell command alias.\r
3348\r
3349 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.\r
3350\r
3351 this function does not check for built in alias'.\r
3352\r
3353 @param[in] Command Points to the NULL-terminated shell command or existing alias.\r
3354 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and\r
3355 Command refers to an alias, that alias will be deleted.\r
3356 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the\r
3357 Alias being set will be stored in a non-volatile fashion.\r
3358\r
3359 @retval EFI_SUCCESS Alias created or deleted successfully.\r
3360 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found\r
3361**/\r
3362EFI_STATUS\r
a405b86d 3363InternalSetAlias(\r
3364 IN CONST CHAR16 *Command,\r
3365 IN CONST CHAR16 *Alias,\r
3366 IN BOOLEAN Volatile\r
3367 )\r
3368{\r
b9b77ab1
BJ
3369 EFI_STATUS Status;\r
3370 CHAR16 *AliasLower;\r
7ec69844 3371 BOOLEAN DeleteAlias;\r
b9b77ab1 3372\r
7ec69844 3373 DeleteAlias = FALSE;\r
a405b86d 3374 if (Alias == NULL) {\r
3375 //\r
7ec69844 3376 // We must be trying to remove one if Alias is NULL\r
a405b86d 3377 // remove an alias (but passed in COMMAND parameter)\r
3378 //\r
7ec69844
RN
3379 Alias = Command;\r
3380 DeleteAlias = TRUE;\r
3381 }\r
3382 ASSERT (Alias != NULL);\r
a405b86d 3383\r
7ec69844
RN
3384 //\r
3385 // Convert to lowercase to make aliases case-insensitive\r
3386 //\r
3387 AliasLower = AllocateCopyPool (StrSize (Alias), Alias);\r
3388 if (AliasLower == NULL) {\r
3389 return EFI_OUT_OF_RESOURCES;\r
a405b86d 3390 }\r
7ec69844 3391 ToLower (AliasLower);\r
b9b77ab1 3392\r
7ec69844
RN
3393 if (DeleteAlias) {\r
3394 Status = gRT->SetVariable (AliasLower, &gShellAliasGuid, 0, 0, NULL);\r
3395 } else {\r
3396 Status = gRT->SetVariable (\r
3397 AliasLower, &gShellAliasGuid,\r
3398 EFI_VARIABLE_BOOTSERVICE_ACCESS | (Volatile ? 0 : EFI_VARIABLE_NON_VOLATILE),\r
3399 StrSize (Command), (VOID *) Command\r
3400 );\r
b9b77ab1 3401 }\r
7ec69844
RN
3402\r
3403 FreePool (AliasLower);\r
3404\r
b9b77ab1 3405 return Status;\r
a405b86d 3406}\r
3407\r
3408/**\r
3409 Changes a shell command alias.\r
3410\r
3411 This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.\r
3412\r
3413\r
3414 @param[in] Command Points to the NULL-terminated shell command or existing alias.\r
3415 @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and\r
3416 Command refers to an alias, that alias will be deleted.\r
3417 @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If\r
3418 FALSE and the alias already exists, then the existing alias is unchanged and\r
3419 EFI_ACCESS_DENIED is returned.\r
3420 @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the\r
3421 Alias being set will be stored in a non-volatile fashion.\r
3422\r
3423 @retval EFI_SUCCESS Alias created or deleted successfully.\r
3424 @retval EFI_NOT_FOUND the Alias intended to be deleted was not found\r
3425 @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to\r
3426 FALSE.\r
b9b77ab1 3427 @retval EFI_INVALID_PARAMETER Command is null or the empty string.\r
a405b86d 3428**/\r
3429EFI_STATUS\r
3430EFIAPI\r
3431EfiShellSetAlias(\r
3432 IN CONST CHAR16 *Command,\r
3433 IN CONST CHAR16 *Alias,\r
3434 IN BOOLEAN Replace,\r
3435 IN BOOLEAN Volatile\r
3436 )\r
3437{\r
a405b86d 3438 if (ShellCommandIsOnAliasList(Alias==NULL?Command:Alias)) {\r
b9b77ab1
BJ
3439 //\r
3440 // cant set over a built in alias\r
3441 //\r
a405b86d 3442 return (EFI_ACCESS_DENIED);\r
b9b77ab1
BJ
3443 } else if (Command == NULL || *Command == CHAR_NULL || StrLen(Command) == 0) {\r
3444 //\r
3445 // Command is null or empty\r
3446 //\r
a405b86d 3447 return (EFI_INVALID_PARAMETER);\r
b9b77ab1
BJ
3448 } else if (EfiShellGetAlias(Command, NULL) != NULL && !Replace) {\r
3449 //\r
3450 // Alias already exists, Replace not set\r
3451 //\r
a405b86d 3452 return (EFI_ACCESS_DENIED);\r
b9b77ab1
BJ
3453 } else {\r
3454 return (InternalSetAlias(Command, Alias, Volatile));\r
a405b86d 3455 }\r
a405b86d 3456}\r
3457\r
3458// Pure FILE_HANDLE operations are passed to FileHandleLib\r
3459// these functions are indicated by the *\r
838b31a6 3460EFI_SHELL_PROTOCOL mShellProtocol = {\r
a405b86d 3461 EfiShellExecute,\r
3462 EfiShellGetEnv,\r
3463 EfiShellSetEnv,\r
3464 EfiShellGetAlias,\r
3465 EfiShellSetAlias,\r
3466 EfiShellGetHelpText,\r
3467 EfiShellGetDevicePathFromMap,\r
3468 EfiShellGetMapFromDevicePath,\r
3469 EfiShellGetDevicePathFromFilePath,\r
3470 EfiShellGetFilePathFromDevicePath,\r
3471 EfiShellSetMap,\r
3472 EfiShellGetCurDir,\r
3473 EfiShellSetCurDir,\r
3474 EfiShellOpenFileList,\r
3475 EfiShellFreeFileList,\r
3476 EfiShellRemoveDupInFileList,\r
3477 EfiShellBatchIsActive,\r
3478 EfiShellIsRootShell,\r
3479 EfiShellEnablePageBreak,\r
3480 EfiShellDisablePageBreak,\r
3481 EfiShellGetPageBreak,\r
3482 EfiShellGetDeviceName,\r
3483 (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo, //*\r
3484 (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo, //*\r
3485 EfiShellOpenFileByName,\r
3486 EfiShellClose,\r
3487 EfiShellCreateFile,\r
3488 (EFI_SHELL_READ_FILE)FileHandleRead, //*\r
3489 (EFI_SHELL_WRITE_FILE)FileHandleWrite, //*\r
3490 (EFI_SHELL_DELETE_FILE)FileHandleDelete, //*\r
3491 EfiShellDeleteFileByName,\r
3492 (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition, //*\r
3493 (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition, //*\r
3494 (EFI_SHELL_FLUSH_FILE)FileHandleFlush, //*\r
3495 EfiShellFindFiles,\r
3496 EfiShellFindFilesInDir,\r
3497 (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize, //*\r
3498 EfiShellOpenRoot,\r
3499 EfiShellOpenRootByHandle,\r
3500 NULL,\r
838b31a6
CP
3501 SHELL_MAJOR_VERSION,\r
3502 SHELL_MINOR_VERSION,\r
09fd5328
JC
3503\r
3504 // New for UEFI Shell 2.1\r
3505 EfiShellRegisterGuidName,\r
3506 EfiShellGetGuidName,\r
3507 EfiShellGetGuidFromName,\r
3508 EfiShellGetEnvEx\r
a405b86d 3509};\r
3510\r
3511/**\r
3512 Function to create and install on the current handle.\r
3513\r
3514 Will overwrite any existing ShellProtocols in the system to be sure that\r
3515 the current shell is in control.\r
3516\r
3517 This must be removed via calling CleanUpShellProtocol().\r
3518\r
4ff7e37b 3519 @param[in, out] NewShell The pointer to the pointer to the structure\r
a405b86d 3520 to install.\r
3521\r
3522 @retval EFI_SUCCESS The operation was successful.\r
3523 @return An error from LocateHandle, CreateEvent, or other core function.\r
3524**/\r
3525EFI_STATUS\r
a405b86d 3526CreatePopulateInstallShellProtocol (\r
838b31a6 3527 IN OUT EFI_SHELL_PROTOCOL **NewShell\r
a405b86d 3528 )\r
3529{\r
3530 EFI_STATUS Status;\r
3531 UINTN BufferSize;\r
3532 EFI_HANDLE *Buffer;\r
3533 UINTN HandleCounter;\r
3534 SHELL_PROTOCOL_HANDLE_LIST *OldProtocolNode;\r
9168df3d 3535 EFI_SHELL_PROTOCOL *OldShell;\r
a405b86d 3536\r
8be0ba36 3537 if (NewShell == NULL) {\r
3538 return (EFI_INVALID_PARAMETER);\r
3539 }\r
3540\r
a405b86d 3541 BufferSize = 0;\r
3542 Buffer = NULL;\r
3543 OldProtocolNode = NULL;\r
3544 InitializeListHead(&ShellInfoObject.OldShellList.Link);\r
3545\r
a405b86d 3546 //\r
3547 // Initialize EfiShellProtocol object...\r
3548 //\r
a405b86d 3549 Status = gBS->CreateEvent(0,\r
3550 0,\r
3551 NULL,\r
3552 NULL,\r
3553 &mShellProtocol.ExecutionBreak);\r
8be0ba36 3554 if (EFI_ERROR(Status)) {\r
3555 return (Status);\r
3556 }\r
a405b86d 3557\r
3558 //\r
3559 // Get the size of the buffer we need.\r
3560 //\r
3561 Status = gBS->LocateHandle(ByProtocol,\r
3562 &gEfiShellProtocolGuid,\r
3563 NULL,\r
3564 &BufferSize,\r
3565 Buffer);\r
3566 if (Status == EFI_BUFFER_TOO_SMALL) {\r
3567 //\r
3568 // Allocate and recall with buffer of correct size\r
3569 //\r
3570 Buffer = AllocateZeroPool(BufferSize);\r
8be0ba36 3571 if (Buffer == NULL) {\r
3572 return (EFI_OUT_OF_RESOURCES);\r
3573 }\r
a405b86d 3574 Status = gBS->LocateHandle(ByProtocol,\r
3575 &gEfiShellProtocolGuid,\r
3576 NULL,\r
3577 &BufferSize,\r
3578 Buffer);\r
8be0ba36 3579 if (EFI_ERROR(Status)) {\r
3580 FreePool(Buffer);\r
3581 return (Status);\r
3582 }\r
a405b86d 3583 //\r
3584 // now overwrite each of them, but save the info to restore when we end.\r
3585 //\r
3586 for (HandleCounter = 0 ; HandleCounter < (BufferSize/sizeof(EFI_HANDLE)) ; HandleCounter++) {\r
a405b86d 3587 Status = gBS->OpenProtocol(Buffer[HandleCounter],\r
3588 &gEfiShellProtocolGuid,\r
9168df3d 3589 (VOID **) &OldShell,\r
a405b86d 3590 gImageHandle,\r
3591 NULL,\r
3592 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
3593 );\r
3594 if (!EFI_ERROR(Status)) {\r
9168df3d 3595 OldProtocolNode = AllocateZeroPool(sizeof(SHELL_PROTOCOL_HANDLE_LIST));\r
51686a7a
RN
3596 if (OldProtocolNode == NULL) {\r
3597 if (!IsListEmpty (&ShellInfoObject.OldShellList.Link)) {\r
3598 CleanUpShellProtocol (&mShellProtocol);\r
3599 }\r
9168df3d
RN
3600 Status = EFI_OUT_OF_RESOURCES;\r
3601 break;\r
3602 }\r
a405b86d 3603 //\r
3604 // reinstall over the old one...\r
3605 //\r
9168df3d
RN
3606 OldProtocolNode->Handle = Buffer[HandleCounter];\r
3607 OldProtocolNode->Interface = OldShell;\r
a405b86d 3608 Status = gBS->ReinstallProtocolInterface(\r
3609 OldProtocolNode->Handle,\r
3610 &gEfiShellProtocolGuid,\r
3611 OldProtocolNode->Interface,\r
8be0ba36 3612 (VOID*)(&mShellProtocol));\r
a405b86d 3613 if (!EFI_ERROR(Status)) {\r
3614 //\r
6a5033ca 3615 // we reinstalled successfully. log this so we can reverse it later.\r
a405b86d 3616 //\r
3617\r
3618 //\r
3619 // add to the list for subsequent...\r
3620 //\r
3621 InsertTailList(&ShellInfoObject.OldShellList.Link, &OldProtocolNode->Link);\r
3622 }\r
3623 }\r
3624 }\r
3625 FreePool(Buffer);\r
3626 } else if (Status == EFI_NOT_FOUND) {\r
3627 ASSERT(IsListEmpty(&ShellInfoObject.OldShellList.Link));\r
3628 //\r
3629 // no one else published yet. just publish it ourselves.\r
3630 //\r
3631 Status = gBS->InstallProtocolInterface (\r
3632 &gImageHandle,\r
3633 &gEfiShellProtocolGuid,\r
3634 EFI_NATIVE_INTERFACE,\r
8be0ba36 3635 (VOID*)(&mShellProtocol));\r
a405b86d 3636 }\r
3637\r
3638 if (PcdGetBool(PcdShellSupportOldProtocols)){\r
3639 ///@todo support ShellEnvironment2\r
3640 ///@todo do we need to support ShellEnvironment (not ShellEnvironment2) also?\r
3641 }\r
3642\r
8be0ba36 3643 if (!EFI_ERROR(Status)) {\r
3644 *NewShell = &mShellProtocol;\r
3645 }\r
a405b86d 3646 return (Status);\r
3647}\r
3648\r
3649/**\r
e9723321 3650 Opposite of CreatePopulateInstallShellProtocol.\r
a405b86d 3651\r
3652 Free all memory and restore the system to the state it was in before calling\r
3653 CreatePopulateInstallShellProtocol.\r
3654\r
4ff7e37b 3655 @param[in, out] NewShell The pointer to the new shell protocol structure.\r
a405b86d 3656\r
3657 @retval EFI_SUCCESS The operation was successful.\r
3658**/\r
3659EFI_STATUS\r
a405b86d 3660CleanUpShellProtocol (\r
838b31a6 3661 IN OUT EFI_SHELL_PROTOCOL *NewShell\r
a405b86d 3662 )\r
3663{\r
8be0ba36 3664 SHELL_PROTOCOL_HANDLE_LIST *Node2;\r
a405b86d 3665\r
3666 //\r
3667 // if we need to restore old protocols...\r
3668 //\r
3669 if (!IsListEmpty(&ShellInfoObject.OldShellList.Link)) {\r
9168df3d 3670 for (Node2 = (SHELL_PROTOCOL_HANDLE_LIST *) GetFirstNode (&ShellInfoObject.OldShellList.Link)\r
a405b86d 3671 ; !IsListEmpty (&ShellInfoObject.OldShellList.Link)\r
9168df3d
RN
3672 ; Node2 = (SHELL_PROTOCOL_HANDLE_LIST *) GetFirstNode (&ShellInfoObject.OldShellList.Link)\r
3673 ) {\r
3674 RemoveEntryList (&Node2->Link);\r
3675 gBS->ReinstallProtocolInterface (Node2->Handle, &gEfiShellProtocolGuid, NewShell, Node2->Interface);\r
3676 FreePool (Node2);\r
a405b86d 3677 }\r
3678 } else {\r
3679 //\r
3680 // no need to restore\r
3681 //\r
9168df3d 3682 gBS->UninstallProtocolInterface (gImageHandle, &gEfiShellProtocolGuid, NewShell);\r
a405b86d 3683 }\r
9168df3d
RN
3684 return EFI_SUCCESS;\r
3685}\r
3686\r
3687/**\r
3688 Cleanup the shell environment.\r
3689\r
3690 @param[in, out] NewShell The pointer to the new shell protocol structure.\r
3691\r
3692 @retval EFI_SUCCESS The operation was successful.\r
3693**/\r
3694EFI_STATUS\r
3695CleanUpShellEnvironment (\r
3696 IN OUT EFI_SHELL_PROTOCOL *NewShell\r
3697 )\r
3698{\r
3699 EFI_STATUS Status;\r
3700 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;\r
ba0014b9 3701\r
9168df3d
RN
3702 CleanUpShellProtocol (NewShell);\r
3703\r
a405b86d 3704 Status = gBS->CloseEvent(NewShell->ExecutionBreak);\r
8be0ba36 3705 NewShell->ExecutionBreak = NULL;\r
3706\r
3707 Status = gBS->OpenProtocol(\r
3708 gST->ConsoleInHandle,\r
3709 &gEfiSimpleTextInputExProtocolGuid,\r
3710 (VOID**)&SimpleEx,\r
3711 gImageHandle,\r
3712 NULL,\r
3713 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
3714\r
a7a394a4 3715 if (!EFI_ERROR (Status)) {\r
3716 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle1);\r
3717 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle2);\r
3718 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle3);\r
3719 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlCNotifyHandle4);\r
3720 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlSNotifyHandle1);\r
3721 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlSNotifyHandle2);\r
3722 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlSNotifyHandle3);\r
3723 Status = SimpleEx->UnregisterKeyNotify(SimpleEx, ShellInfoObject.CtrlSNotifyHandle4);\r
3724 }\r
a405b86d 3725 return (Status);\r
3726}\r
3727\r
8be0ba36 3728/**\r
3729 Notification function for keystrokes.\r
3730\r
3731 @param[in] KeyData The key that was pressed.\r
3732\r
3733 @retval EFI_SUCCESS The operation was successful.\r
3734**/\r
3735EFI_STATUS\r
3736EFIAPI\r
3737NotificationFunction(\r
3738 IN EFI_KEY_DATA *KeyData\r
3739 )\r
3740{\r
41cf3e45 3741 if ( ((KeyData->Key.UnicodeChar == L'c') &&\r
3742 (KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED) || KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED))) ||\r
3743 (KeyData->Key.UnicodeChar == 3)\r
ba0014b9 3744 ){\r
733f138d 3745 if (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak == NULL) {\r
3746 return (EFI_UNSUPPORTED);\r
3747 }\r
3748 return (gBS->SignalEvent(ShellInfoObject.NewEfiShellProtocol->ExecutionBreak));\r
3749 } else if ((KeyData->Key.UnicodeChar == L's') &&\r
3750 (KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED) || KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED))\r
ba0014b9 3751 ){\r
a49f6a2f 3752 ShellInfoObject.HaltOutput = TRUE;\r
8be0ba36 3753 }\r
733f138d 3754 return (EFI_SUCCESS);\r
8be0ba36 3755}\r
3756\r
3757/**\r
ba0014b9 3758 Function to start monitoring for CTRL-C using SimpleTextInputEx. This\r
8be0ba36 3759 feature's enabled state was not known when the shell initially launched.\r
3760\r
3761 @retval EFI_SUCCESS The feature is enabled.\r
6a5033ca 3762 @retval EFI_OUT_OF_RESOURCES There is not enough memory available.\r
8be0ba36 3763**/\r
3764EFI_STATUS\r
8be0ba36 3765InernalEfiShellStartMonitor(\r
3766 VOID\r
3767 )\r
3768{\r
3769 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;\r
3770 EFI_KEY_DATA KeyData;\r
3771 EFI_STATUS Status;\r
3772\r
3773 Status = gBS->OpenProtocol(\r
3774 gST->ConsoleInHandle,\r
3775 &gEfiSimpleTextInputExProtocolGuid,\r
3776 (VOID**)&SimpleEx,\r
3777 gImageHandle,\r
3778 NULL,\r
3779 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
3780 if (EFI_ERROR(Status)) {\r
3781 ShellPrintHiiEx(\r
ba0014b9
LG
3782 -1,\r
3783 -1,\r
8be0ba36 3784 NULL,\r
3785 STRING_TOKEN (STR_SHELL_NO_IN_EX),\r
3786 ShellInfoObject.HiiHandle);\r
3787 return (EFI_SUCCESS);\r
3788 }\r
a405b86d 3789\r
8be0ba36 3790 if (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak == NULL) {\r
3791 return (EFI_UNSUPPORTED);\r
3792 }\r
3793\r
3794 KeyData.KeyState.KeyToggleState = 0;\r
3795 KeyData.Key.ScanCode = 0;\r
3796 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r
3797 KeyData.Key.UnicodeChar = L'c';\r
3798\r
3799 Status = SimpleEx->RegisterKeyNotify(\r
3800 SimpleEx,\r
3801 &KeyData,\r
3802 NotificationFunction,\r
3803 &ShellInfoObject.CtrlCNotifyHandle1);\r
ba0014b9 3804\r
8be0ba36 3805 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
3806 if (!EFI_ERROR(Status)) {\r
3807 Status = SimpleEx->RegisterKeyNotify(\r
3808 SimpleEx,\r
3809 &KeyData,\r
3810 NotificationFunction,\r
3811 &ShellInfoObject.CtrlCNotifyHandle2);\r
3812 }\r
3813 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;\r
3814 KeyData.Key.UnicodeChar = 3;\r
3815 if (!EFI_ERROR(Status)) {\r
3816 Status = SimpleEx->RegisterKeyNotify(\r
3817 SimpleEx,\r
3818 &KeyData,\r
3819 NotificationFunction,\r
3820 &ShellInfoObject.CtrlCNotifyHandle3);\r
3821 }\r
3822 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
3823 if (!EFI_ERROR(Status)) {\r
3824 Status = SimpleEx->RegisterKeyNotify(\r
3825 SimpleEx,\r
3826 &KeyData,\r
3827 NotificationFunction,\r
3828 &ShellInfoObject.CtrlCNotifyHandle4);\r
3829 }\r
3830 return (Status);\r
3831}\r
4f344fff 3832\r