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