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