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