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