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